xdev.misc module¶
- xdev.misc.quantum_random(pure=False)[source]¶
Returns a quantum random number as a 32 bit unsigned integer. Does this by making a network request to the ANU Quantum Random Number Generator web service, so an internet connection is required.
- Parameters:
pure (bool) – if False, mixes this data with pseudorandom data for security. Otherwise returns the raw quantum numbers that were sent over the web (i.e. subject to MitM attacks).
- Requirements:
quantumrandom >= 1.9.0
- Returns:
the random number
- Return type:
numpy.uint32
- xdev.misc.byte_str(num, unit='auto', precision=2)[source]¶
Automatically chooses relevant unit (KB, MB, or GB) for displaying some number of bytes.
- Parameters:
num (int) – number of bytes
unit (str) – which unit to use, can be auto, B, KB, MB, GB, TB, PB, EB, ZB, or YB.
precision (int) – number of decimals of precision
References
https://en.wikipedia.org/wiki/Orders_of_magnitude_(data)
- Returns:
string representing the number of bytes with appropriate units
- Return type:
Example
>>> num_list = [1, 100, 1024, 1048576, 1073741824, 1099511627776] >>> result = ub.repr2(list(map(byte_str, num_list)), nl=0) >>> print(result) ['0.00 KB', '0.10 KB', '1.00 KB', '1.00 MB', '1.00 GB', '1.00 TB']
- xdev.misc.set_overlaps(set1, set2, s1='s1', s2='s2')[source]¶
Return sizes about set overlaps
- Parameters:
set1 (Iterable)
set2 (Iterable)
s1 (str) – name for set1
s2 (str) – name for set2
- Returns:
sizes of sets intersections unions and differences
- Return type:
Notes
- This function needs a rename. Possible candidates brainstorm:
set_analysis
set_binary_analysis
set_binary_describe
set_relationships
describe_sets
describe_relations
describe_set_relations
sets_summary
sumarize_sets
sumerset
- xdev.misc.nested_type(obj, unions=False)[source]¶
Compute the :module:`typing` compatible annotation type.
- Parameters:
obj (Any) – a typing template based on a specific object
unions (bool) – if True use unions, otherwise use Any
- Returns:
type code (might change to return actual type)
- Return type:
Example
>>> obj = {'a': [1, 2], 'b': [3, 4, 5]} >>> print(nested_type(obj)) Dict[str, List[int]]
>>> import numpy as np >>> obj = {'b': {'a': 1.0, 'b': 'foo', 'c': np.array([1, 2])}} >>> print(nested_type(obj, unions=True)) Dict[str, Dict[str, float | ndarray | str]]
- xdev.misc.difftext(text1, text2, context_lines=0, ignore_whitespace=False, colored=False)[source]¶
Uses difflib to return a difference string between two similar texts
- Parameters:
text1 (str) – old text
text2 (str) – new text
context_lines (int) – number of lines of unchanged context
ignore_whitespace (bool)
colored (bool) – if true highlight the diff
- Returns:
formatted difference text message
- Return type:
References
http://www.java2s.com/Code/Python/Utility/IntelligentdiffbetweentextfilesTimPeters.htm
Example
>>> # build test data >>> text1 = 'one\ntwo\nthree' >>> text2 = 'one\ntwo\nfive' >>> # execute function >>> result = difftext(text1, text2) >>> # verify results >>> print(result) - three + five
Example
>>> # build test data >>> text1 = 'one\ntwo\nthree\n3.1\n3.14\n3.1415\npi\n3.4\n3.5\n4' >>> text2 = 'one\ntwo\nfive\n3.1\n3.14\n3.1415\npi\n3.4\n4' >>> # execute function >>> context_lines = 1 >>> result = difftext(text1, text2, context_lines, colored=True) >>> # verify results >>> print(result)
- xdev.misc.tree_repr(cwd=None, max_files=100, dirblocklist=None, show_nfiles='auto', return_text=False, return_tree=False, pathstyle='name', max_depth=None, with_type=False, abs_root_label=True, ignore_dotprefix=True, colors=False)[source]¶
Filesystem tree representation
Like the unix util tree, but allow writing numbers of files per directory when given -d option
- Parameters:
cwd (None | str | PathLike) – directory to print
max_files (int | None) – maximum files to print before supressing a directory
pathstyle (str) – can be rel, name, or abs
return_tree (bool) – if True return the tree
return_text (bool) – if True return the text
maxdepth (int | None) – maximum depth to descend
abs_root_label (bool) – if True force the root to always be absolute
colors (bool) – if True use rich
- SeeAlso:
xdev.tree - generator