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.urepr(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', n_samples=None)[source]
Return sizes about set overlaps.
If the inputs are not sets, they will be cast to sets, and if there are duplicate they will be counted.
- Parameters:
set1 (Iterable) – the first set of items
set2 (Iterable) – the second set of items
s1 (str) – name for set1
s2 (str) – name for set2
n_samples (int | None) – provide up to n examples from each set.
- Returns:
sizes of sets intersections unions and differences
- Return type:
Example
>>> import ubelt as ub >>> from xdev.misc import set_overlaps >>> set1 = {'a', 'b', 'c', 'd', 'e'} >>> set2 = {'a', 'e', 'i', 'o', 'u'} >>> result = set_overlaps(set1, set2, 'first5', 'vowels') >>> print(f'result = {ub.urepr(result, nl=1)}') result = { 'first5': 5, 'vowels': 5, 'isect': 2, 'union': 8, 'first5 - vowels': 3, 'vowels - first5': 3, }
Example
>>> import ubelt as ub >>> from xdev.misc import set_overlaps >>> set1 = [1, 1, 2, 3, 3, 3, 4, 5,] >>> set2 = [2, 2, 2, 3] >>> result = set_overlaps(set1, set2) >>> print(f'result = {ub.urepr(result, nl=1)}')
- 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, style='ndiff', fromfile='', tofile='')[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
style (str) – can be ndiff or unified (git style)
fromfile (str) – unified diff “old” header label
tofile (str) – unified diff “new” header label
- 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 >>> from xdev.misc import * # NOQA >>> 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) >>> print(result) >>> # >>> result = difftext(text1, text2, context_lines, colored=True, style='unified') >>> print(result)
Example
>>> # build test data for a git-apply-able unified patch >>> from xdev.misc import * # NOQA >>> text1 = 'alpha\nbeta\ngamma\n' >>> text2 = 'alpha\nbeta\nGAMMA\ndelta\n' >>> patch = difftext(text1, text2, context_lines=3, style='unified', colored=True, ... fromfile='a/example.txt', tofile='b/example.txt') >>> print(patch) >>> lines = patch.splitlines() >>> assert lines[0] == '--- a/example.txt' >>> assert lines[1] == '+++ b/example.txt' >>> assert lines[2].startswith('@@')
- 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