Skip to content

Commit d886d32

Browse files
author
Kefkius
committed
Initial commit
0 parents  commit d886d32

File tree

9 files changed

+128
-0
lines changed

9 files changed

+128
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.pyc
2+
*.swp
3+
build/
4+
dist/
5+
hexs.egg-info/

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# hexs
2+
3+
This is a very small package which contains functions that format hex strings.
4+
The purpose of this formatting is to make a given hex string of even length,
5+
remove a "0x" prefix if present, and remove a "L" suffix if present.
6+
7+
It also includes a convenience function, `hexs()`, which calls the built-in `hex()`
8+
function and formats the result.
9+
10+
## Usage
11+
12+
```
13+
>>> from hexs import is_hex, hexs, format_hex
14+
>>> hexs(5)
15+
'05'
16+
>>> format_hex('0x5')
17+
'05'
18+
>>> is_hex('g')
19+
False
20+
>>> is_hex(29)
21+
False
22+
>>> is_hex('f')
23+
True
24+
```
25+
26+
## License
27+
28+
MIT License.

hexs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .hexs import *

hexs/hexs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Hex string formating."""
2+
3+
# Append 0 if the length of s is odd.
4+
make_even = lambda s: '0' + s if len(s) % 2 else s
5+
6+
def is_hex(s):
7+
"""Return whether s is a hex string."""
8+
try:
9+
int(s, 16)
10+
except (TypeError, ValueError):
11+
return False
12+
return True
13+
14+
def format_hex(s):
15+
"""Remove extraneous characters from s and make its length even."""
16+
return make_even(s.lstrip('0x').rstrip('L'))
17+
18+
def hexs(arg):
19+
"""Return the formatted hex representation of arg."""
20+
return format_hex(hex(arg))
21+
22+
__all__ = (
23+
'is_hex',
24+
'format_hex',
25+
'hexs',
26+
)

hexs/tests/__init__.py

Whitespace-only changes.

hexs/tests/test_hex.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
3+
from hexs import is_hex, format_hex, hexs
4+
5+
class IsHexTest(unittest.TestCase):
6+
def test_is_hex(self):
7+
self.assertTrue(is_hex('2'))
8+
self.assertTrue(is_hex('0xf'))
9+
10+
def test_is_not_hex(self):
11+
self.assertFalse(is_hex('0xg'))
12+
self.assertFalse(is_hex(5))
13+
14+
class FormatHexTest(unittest.TestCase):
15+
def test_format_hex(self):
16+
self.assertEqual('07', format_hex('0x7'))
17+
self.assertEqual('01ff', format_hex('1ff'))
18+
self.assertEqual('0528', format_hex('0528'))
19+
20+
class HexsTest(unittest.TestCase):
21+
def test_hexs(self):
22+
self.assertEqual('07', hexs(7))
23+
self.assertEqual('0100', hexs(256))

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from setuptools import setup, find_packages
2+
3+
version = '1.0.0'
4+
5+
6+
setup(
7+
name = 'hexs',
8+
version = version,
9+
description = 'Hex string formatting.',
10+
url = 'https://github.com/kefkius/hexs',
11+
author = 'Tyler Willis',
12+
author_email = 'kefkius@mail.com',
13+
keywords = [
14+
'hex',
15+
'formatting',
16+
],
17+
classifiers = [
18+
'License :: OSI Approved :: MIT License',
19+
],
20+
packages = find_packages(),
21+
test_suite = 'hexs.tests'
22+
)

0 commit comments

Comments
 (0)