This commit is contained in:
osiris account 2023-03-12 15:32:23 -07:00
parent 0696b08d04
commit 314bdf533e
94 changed files with 11 additions and 7 deletions

View file

@ -0,0 +1,28 @@
.PHONY: setup install clean test lint
default: test
setup:
pip install -r requirements.txt
install:
python setup.py install
clean:
@find . -type f -name '*.pyc' -delete
@find . -type d -name '__pycache__' | xargs rm -rf
@find . -type d -name '*.ropeproject' | xargs rm -rf
@rm -rf build/
@rm -rf dist/
@rm -rf venv/
@rm -f src/*.egg*
@rm -f MANIFEST
@rm -rf docs/build/
@rm -f .coverage.*
test:
@tox -- -s
lint:
@tox -e lint

View file

@ -0,0 +1,85 @@
# Enconding decimals
This program:
i) converts and encodes a 14-bit decimal input value to a 2-byte hexadecimal,
ii) decodes hexadecimal representations to 14-bit decimal
## Installing
Create and source virtual enviroment. You can use [virtualenv](https://virtualenv.pypa.io/en/latest/) or [conda](https://docs.conda.io/en/latest/):
```bash
virtualenv venv
source venv/bin/activate
```
Install dependencies:
```bash
make setup
```
Install Efun:
```bash
make install
```
## Usage
### Encoding
To enconde a integer, run:
```bash
efun -e <integer>
```
Note that the value must be in the range `[-8192, 8191]`.
#### Decoding
To decode an integer, run:
```bash
efun -d <integer>
```
Note that the value must be in the range `[0x0000, 0x7F7F]`.
## Developer
### Running tests
You can run tests with:
```bash
make test
```
### Linting
You can lint your code with:
```bash
make lint
```
### Cleaning up
Clean residual compilation and installation files with:
```bash
make clean
```
----
Thank you for reading my code!

View file

@ -0,0 +1,13 @@
# Encoding function
6111 -> 6f5f
340 -> 4254
-2628 -> 2b3c
-255 -> 3e01
7550 -> 7a7e
# Decoding function
0A0A -> -6902
0029 -> -8151
3F0F -> -113
4400 -> 512
5E7F -> 3967

View file

@ -0,0 +1,41 @@
#!/usr/bin/env python
import unittest
import src.main as m
class EfunTest(unittest.TestCase):
def setUp(self):
self.e = m.Efun()
self.test_data = [
(-4096, 0x2000, (0x20, 0x00)),
(-8192, 0x0000, (0x00, 0x00)),
(0, 0x4000, (0x40, 0x00)),
(2048, 0x5000, (0x50, 0x00)),
(8191, 0x7F7F, (0x7F, 0x7F)),
(6111, 0x6F5F, (0x6F, 0x5F)),
(340, 0x4254, (0x42, 0x54)),
(-2628, 0x2B3C, (0x2B, 0x3C)),
(-255, 0x3E01, (0x3E, 0x01)),
(-6902, 0x0A0A, (0x0A, 0x0A)),
(-8151, 0x0029, (0x00, 0x29)),
(-113, 0x3F0F, (0x3F, 0x0F)),
(512, 0x4400, (0x44, 0x00)),
(3967, 0x5E7F, (0x5E, 0x7F)),
]
def test_extract(self):
for item in self.test_data:
self.assertEqual(self.e._extract_two_bytes(item[1]), item[2])
def test_decode(self):
for item in self.test_data:
by1, by2 = self.e._extract_two_bytes(item[1])
self.e._decode(by1, by2)
self.assertEqual(self.e.dec_byte, item[0])
def test_encode(self):
for item in self.test_data:
self.e._encode(item[0])
self.assertEqual(self.e.enc_byte, hex(item[1]))

View file

@ -0,0 +1,2 @@
tox==3.14.3
virtualenv==20.0.1

View file

@ -0,0 +1,15 @@
from setuptools import setup, find_packages
setup(
name='efun',
version='0.0.1',
packages=find_packages(),
include_package_data=True,
author='steinkirch',
install_requires=[
],
entry_points='''
[console_scripts]
efun=src.main:main
''',
)

View file

@ -0,0 +1 @@
# -*- coding: utf8 -*-

View file

@ -0,0 +1,154 @@
#!/usr/bin/env python
import argparse
class Efun(object):
def __init__(self):
self.enc_byte = None
self.dec_byte = None
self.enc_range = [-8192, 8191]
self.dec_range = [0x0000, 0x7F7F]
def _decode(self, higher_byte, lower_byte):
"""
Converts encoded hex value back into the original signed integer.
Argument:
higher_byte {string} -- higher byter hex input to be decoded.
lower_byte {string} -- lower byter hex input to be decoded.
Returns:
Decoded value string in decimal base.
"""
# Left shift 7 to get higher byte.
higher_byte <<= 7
# 'OR' byte withing the decimal lower limit.
self.dec_byte = (lower_byte | higher_byte) + self.enc_range[0]
def _encode(self, value):
"""
Gets a signed integer and return a 4 character string,
such that after the encoding is complete, the most
significant bit of each byte has been cleared.
Argument:
value {string} -- integer input value to be encoded.
Returns:
Encoded 2-byte string.
"""
# Add 8192 for unsigned
sign_int = int(value)
unsign_int = sign_int + 8192
# 'AND' with 0b0000000001111111 for lower byte and
# 'AND' with 0b0011111110000000 for higher byter
lower_byte = unsign_int & 0x007F
higher_byte = ((unsign_int & 0x3F80) << 1)
# Return hex enconding of sum of the two bytes
self.enc_byte = hex(lower_byte + higher_byte)
def _extract_two_bytes(self, value):
"""
Extract two bytes from a hexadecimal string.
Arguments:
value {hex} -- hexadecimal between 0x0000 and 0x7F7F.
Returns:
higher_byte, lower_byte -- byte strings.
"""
# 'And' with 0b0000000001111111 for lower byte
lower_byte = value & 0x00FF
# Left shift 8 positions for higher byte
higher_byte = value >> 8
return higher_byte, lower_byte
def run_decode(self, value):
"""
Verifies if an input is valid 2-byte hexadecimal,
returning its decoded value.
Arguments:
value {string} -- hexadecimal input value.
Returns:
None.
"""
# Convert to hexadecimal.
try:
value = int(value, 16)
except ValueError as e:
print('Could not convert value to hexadecimal: {}.'.fomart(e))
return -1
# Verify whether the value is within the range,
# and then decode it.
if self.dec_range[0] <= value <= self.dec_range[1]:
self._decode(*self._extract_two_bytes(value))
print('{0} decodes as {1}'.format(hex(value), self.dec_byte))
else:
print('Value {0} is out of range of [{1}, {2}]'.format(hex(value),
hex(self.dec_range[0], hex(self.dec_range[1]))))
def run_encode(self, value):
"""
Verifies whether an input is valid 14-bit integer,
returning its encoded value.
Arguments:
value {byte} -- 14-bit signed integer.
Returns:
None.
"""
int_value = int.from_bytes(value, byteorder='big', signed=True)
if self.enc_range[0] <= int_value <= self.enc_range[1]:
self._encode(int_value)
print('Value {0} encodes as {1}'.format(int_value, self.enc_byte))
else:
print('Value {0} out of range: {1}'.format(int_value, self.enc_byte))
def main():
# Creater an instance of Efun().
e = Efun()
# Set strings for Argparse.
description = 'Efun is an Art + Logic Enconding + Decoding application.'
encode_help = 'Enter an INT value in the range {}.'.format(e.enc_range)
decode_help = 'Enter an HEX value in the range {}.'.format(e.dec_range)
# Run CLI menu.
parser = argparse.ArgumentParser(description=description)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encode', type=int, help=encode_help)
group.add_argument('-d', '--decode', help=decode_help)
args = parser.parse_args()
# Get the 14-byte input, enconde/decode it, and print results.
bit_range = 14
if args.encode:
sig_bytes = args.encode.to_bytes(bit_range, byteorder='big', signed=True)
e.run_encode(sig_bytes)
else:
e.run_decode(args.decode)
if __name__ == "__main__":
main()

View file

@ -0,0 +1 @@
# -*- coding: utf8 -*-

View file

@ -0,0 +1,12 @@
[tox]
envlist = py27
[testenv]
deps = pytest
commands =
pytest
[testenv:lint]
skip_install = true
deps = flake8
commands = flake8 src/