hexathon

Common and uncommon hex string operations for python3
git clone git://git.defalsify.org/python-hexathon.git
Log | Files | Refs | LICENSE

commit 3656c05d71e548b0439971f52c5f7a5d494a0a23
parent 8d4d9ddf94db6358e7fbb60ce1a0142220cd2089
Author: nolash <dev@holbrook.no>
Date:   Fri,  7 Jan 2022 12:48:00 +0000

Add compact value option to 0x methods

Diffstat:
Mhexathon/parse.py | 21+++++++++++++++------
Msetup.cfg | 32++++++++++----------------------
Msetup.py | 7++++++-
Mtests/test_basic.py | 4++++
4 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/hexathon/parse.py b/hexathon/parse.py @@ -23,7 +23,7 @@ def uniform(hx): return even(hx).lower() -def strip_0x(hx, allow_empty=False): +def strip_0x(hx, allow_empty=False, compact_value=False): if len(hx) == 0 and not allow_empty: raise ValueError('invalid hex') elif len(hx) < 2: @@ -31,19 +31,28 @@ def strip_0x(hx, allow_empty=False): if hx[:2] == '0x': hx = hx[2:] - return even(hx, allow_empty) + if compact_value: + v = compact(hx, allow_empty) + else: + v = even(hx, allow_empty) + return v -def add_0x(hx, allow_empty=False): +def add_0x(hx, allow_empty=False, compact_value=False): if len(hx) == 0 and not allow_empty: raise ValueError('invalid hex') if hx[:2] == '0x': hx = hx[2:] - return '0x' + even(hx, allow_empty) + v = '' + if compact_value: + v = compact(hx, allow_empty) + else: + v = even(hx, allow_empty) + return '0x' + v -def compact(hx): - hx = strip_0x(hx) +def compact(hx, allow_empty=False): + hx = strip_0x(hx, allow_empty) i = 0 for i in range(len(hx)): if hx[i] != '0': diff --git a/setup.cfg b/setup.cfg @@ -1,24 +1,12 @@ +; Config::Simple 4.59 +; Sun Nov 7 20:13:41 2021 + [metadata] -name = hexathon -version = 0.1.0 -description = Common and uncommon hex string operations -author = Louis Holbrook -author_email = dev@holbrook.no -url = https://gitlab.com/nolash/python-hexathon -keywords = - hex -classifiers = - Programming Language :: Python :: 3 - Operating System :: OS Independent - Development Status :: 3 - Alpha - Topic :: Software Development :: Libraries - Intended Audience :: Developers - License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+) -license = WTFPL -licence_files = - LICENSE +url=https://gitlab.com/nolash/python-hexathon +author_email=dev@holbrook.no +name=hexathon +version=0.1.1 +description=Common and uncommon hex string operations +author=Louis Holbrook + -[options] -python_requires = >= 3.6 -packages = - hexathon diff --git a/setup.py b/setup.py @@ -1,3 +1,8 @@ from setuptools import setup -setup() +setup( + packages=[ + 'hexathon', + ], + license='WTFPL', + ) diff --git a/tests/test_basic.py b/tests/test_basic.py @@ -21,6 +21,10 @@ class HexTest(unittest.TestCase): def test_0x(self): self.assertEqual(hexathon.strip_0x('0xabcd'), 'abcd') self.assertEqual(hexathon.add_0x('abcd'), '0xabcd') + self.assertEqual(hexathon.strip_0x('0x000abcd'), '0000abcd') + self.assertEqual(hexathon.add_0x('000abcd'), '0x0000abcd') + self.assertEqual(hexathon.strip_0x('0x000abcd', compact_value=True), 'abcd') + self.assertEqual(hexathon.add_0x('000abcd', compact_value=True), '0xabcd') def test_even(self):