byte-util 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.4
2
+ Name: byte-util
3
+ Version: 0.1.0
4
+ Summary: Utility functions for converting bytes and bytearrays to various formats
5
+ Author: Adam Blakney
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "byte-util"
7
+ version = "0.1.0"
8
+ description = "Utility functions for converting bytes and bytearrays to various formats"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ authors = [
12
+ {name = "Adam Blakney"}
13
+ ]
14
+
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Operating System :: OS Independent"
18
+ ]
19
+
20
+ [tool.setuptools.packages.find]
21
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .convert import hex, ranged_int, custom_chars, ascii, alpha_numeric, alpha_numeric_case_sensitive, decimal
2
+
3
+ __all__ = ['hex', 'ranged_int', 'custom_chars', 'ascii', 'alpha_numeric', 'alph_numeric_case_sensitive', 'decimal']
@@ -0,0 +1,88 @@
1
+ ''' helper methods '''
2
+
3
+ ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz'
4
+ ALPHA_UPPER = ALPHA_LOWER.upper()
5
+ DIGITS = '0123456789'
6
+ SYMBOLS = '~!@#$%^&*()-_=+[{]}|;:\'\",<.>/?'
7
+
8
+ # return hexadecimal string from bytes
9
+ def hex(b: bytes | bytearray) -> str:
10
+ return b.hex()
11
+
12
+ # random in in range [a, b)
13
+ # using the "division with rejection" method from here:
14
+ # https://www.pcg-random.org/posts/bounded-rands.html
15
+ # return None if sample fails
16
+ def ranged_int(_bytes: bytes | bytearray, a: int, b: int) -> int:
17
+
18
+ if b <= a:
19
+ raise BaseException('invalid range')
20
+
21
+ i = int.from_bytes(_bytes, byteorder='little')
22
+
23
+ _range = b - a
24
+ _max = 2 ** (len(_bytes) * 8)
25
+
26
+ if _range > _max:
27
+ raise BaseException('not enough bytes to sample that range')
28
+
29
+ # this essentially finds which bin the number is in
30
+ # there are _range bins, each of size _max // _range
31
+ ret = (i // (_max // _range))
32
+
33
+ # reject if needed
34
+ if ret >= _range:
35
+ return None
36
+
37
+ # adjust to fit in range
38
+ return ret + a
39
+
40
+ # return string containing random characters from l based on byte values
41
+ # in b
42
+ def custom_chars(b: bytes | bytearray, l: list) -> str:
43
+
44
+ n = len(l)
45
+ ret = ''
46
+
47
+ for i in range(len(b)):
48
+ r = ranged_int(b[i:i+1], 0, n)
49
+ if r is None:
50
+ continue
51
+ curr = l[r]
52
+ ret = '{}{}'.format(ret, curr)
53
+
54
+ return ret
55
+
56
+ # get ascii from bytearray
57
+ def ascii(b: bytes | bytearray) -> str:
58
+ return custom_chars(b, ALPHA_LOWER + ALPHA_UPPER + DIGITS + SYMBOLS)
59
+
60
+ # alphanumeric with lower case
61
+ def alpha_numeric(b: bytes | bytearray) -> str:
62
+ return custom_chars(b, ALPHA_LOWER + DIGITS)
63
+
64
+ # alphanumeric thats case sensitive
65
+ def alpha_numeric_case_sensitive(b: bytes | bytearray) -> str:
66
+ return custom_chars(b, ALPHA_LOWER + ALPHA_UPPER + DIGITS)
67
+
68
+ # digits from bytes
69
+ def decimal(b: bytes | bytearray) -> str:
70
+ return custom_chars(b, DIGITS)
71
+
72
+ # Helper method to convert bytes to different formats
73
+ # take in bytes (s) and a format, output the bytes converted to the format
74
+ def convert_bytes(s, _format):
75
+ if _format == 'hex':
76
+ return hex_from_bytes(s)
77
+ elif _format == 'ascii':
78
+ return ascii_from_bytes(s)
79
+ elif _format == 'alpha-numeric':
80
+ return alpha_numeric_from_bytes(s)
81
+ elif _format == 'Alpha-numeric':
82
+ return alpha_numeric_case_sensitive_from_bytes(s)
83
+ elif _format == 'digits':
84
+ return digits_from_bytes(s)
85
+ elif _format == 'bytes':
86
+ return s
87
+ else:
88
+ raise BaseException('invalid format type')
@@ -0,0 +1,23 @@
1
+ import convert
2
+
3
+ chars = 'abjk243'
4
+
5
+ b = bytearray()
6
+ b.append(2)
7
+ b.append(23)
8
+ b.append(44)
9
+ b.append(101)
10
+ b.append(222)
11
+ b.append(56)
12
+ b.append(153)
13
+
14
+ print(b)
15
+ print(convert.hex(b))
16
+ print(convert.ascii(b))
17
+ print(convert.custom_chars(b, chars))
18
+ print(convert.ranged_int(b, 0, 10))
19
+ print(convert.alpha_numeric(b))
20
+ print(convert.alpha_numeric_case_sensitive(b))
21
+ print(convert.decimal(b))
22
+
23
+
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.4
2
+ Name: byte-util
3
+ Version: 0.1.0
4
+ Summary: Utility functions for converting bytes and bytearrays to various formats
5
+ Author: Adam Blakney
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
@@ -0,0 +1,8 @@
1
+ pyproject.toml
2
+ src/byte_util/__init__.py
3
+ src/byte_util/convert.py
4
+ src/byte_util/test.py
5
+ src/byte_util.egg-info/PKG-INFO
6
+ src/byte_util.egg-info/SOURCES.txt
7
+ src/byte_util.egg-info/dependency_links.txt
8
+ src/byte_util.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ byte_util