Skip to content

Commit

Permalink
hex_str_to_dec() enhanced. to_file() supported
Browse files Browse the repository at this point in the history
  • Loading branch information
impratikjaiswal committed Jul 16, 2023
1 parent b6c38d2 commit 331bb45
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 28 deletions.
2 changes: 1 addition & 1 deletion python_helpers/ph_constants_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PhConfigConst:
TOOL_VERSION = '2.2.8'
TOOL_VERSION = '2.2.9'
TOOL_VERSION_DETAILED = f'v{TOOL_VERSION}'
TOOL_NAME = 'pythonHelpers'
94 changes: 84 additions & 10 deletions python_helpers/ph_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,24 @@ def find_offset_of_section(cls, data, char_to_find, corresponding_char_to_find):
return end_char

@classmethod
def dec_to_hex(cls, dec_num, digit_required=None, even_digits=True):
def dec_to_hex(cls, dec_num, digit_required=None, even_digits=True, signed_byte_handling=True):
"""
:param dec_num:
:param digit_required:
:param even_digits:
:param signed_byte_handling: Byte in Java is represented by signed int in range (-128, 127), Byte Python is represented by unsigned int in range(0, 255).
:return:
"""
# return hex(dec_num).split('x')[-1].upper()
# return '0x{:02x}'.format(dec_num)
# return binascii.hexlify(str(dec_num))
if isinstance(dec_num, list):
res = [cls.dec_to_hex(dec_num=x, digit_required=digit_required, even_digits=even_digits,
signed_byte_handling=signed_byte_handling) for x in dec_num]
return ''.join(res)
if dec_num < 0 and signed_byte_handling:
dec_num = dec_num + 256
if digit_required is None:
digit_required = 0
temp = format(dec_num, 'X')
Expand All @@ -787,16 +801,31 @@ def dec_to_hex(cls, dec_num, digit_required=None, even_digits=True):
return temp.rjust(digit_required, '0')

@classmethod
def hex_str_to_dec(cls, hex_str):
if isinstance(hex_str, str):
return int(hex_str, 16)
return hex_str
def hex_str_to_dec(cls, hex_str, signed_byte_handling=False):
"""
:param hex_str:
:param signed_byte_handling: Byte in Java is represented by signed int in range (-128, 127), Byte Python is represented by unsigned int in range(0, 255).
:return:
"""
dec_num = int(hex_str, 16) if isinstance(hex_str, str) else hex_str
if 127 < dec_num < 256 and signed_byte_handling:
dec_num = dec_num - 256
return dec_num

@classmethod
def hex_str_to_hex_list(cls, hex_str):
def hex_str_to_dec_list(cls, hex_str, signed_byte_handling=False):
"""
:param hex_str:
:param signed_byte_handling: Byte in Java is represented by signed int in range (-128, 127), Byte Python is represented by unsigned int in range(0, 255).
:return:
"""
hex_str = cls.trim_and_kill_all_white_spaces(hex_str)
chunk_size = 2
return [cls.hex_str_to_dec(hex_str[i:i + chunk_size]) for i in range(0, len(hex_str), chunk_size)]
if len(hex_str) > 2:
chunk_size = 2
return [cls.hex_str_to_dec(hex_str[i:i + chunk_size], signed_byte_handling=signed_byte_handling) for i in
range(0, len(hex_str), chunk_size)]

@classmethod
def rstrip_hex_str(cls, hex_str):
Expand Down Expand Up @@ -965,6 +994,25 @@ def read_csv(cls,
def to_csv(cls, output, file_name, str_append='', new_ext='', sep=' ', index=False, print_shape=True,
print_frame=False,
encoding=None, log=None):
"""
:param output: DataFrame
:param file_name:
:param str_append:
:param new_ext:
:param sep:
:param index:
:param print_shape:
:param print_frame:
:param encoding:
:param log:
:return:
"""
if output is None:
return None
if not isinstance(output, DataFrame):
return cls.to_file(output_lines=output, file_name=file_name, str_append=str_append, new_ext=new_ext,
encoding=encoding)
if print_shape:
cls.print_data_frame_shape(output, file_name, log=log)
if print_frame:
Expand All @@ -975,6 +1023,30 @@ def to_csv(cls, output, file_name, str_append='', new_ext='', sep=' ', index=Fal
output.to_csv(path_or_buf=file_name, index=index, sep=sep, encoding=encoding)
return file_name

@classmethod
def to_file(cls, output_lines, file_name, str_append='', new_ext='', lines_sep='\n', encoding='utf-8'):
"""
:param output_lines:
:param file_name:
:param str_append:
:param new_ext:
:param lines_sep:
:param encoding:
:return:
"""
if output_lines is None:
return None
if str_append or new_ext:
file_name = cls.append_in_file_name(str_file_path=file_name, str_append=str_append, new_ext=new_ext)
cls.makedirs(cls.get_file_name_and_extn(file_name, only_path=True))
with open(file_name, 'w', encoding=encoding) as file_write:
if isinstance(output_lines, list):
file_write.writelines(lines_sep.join(output_lines))
else:
file_write.write(output_lines)
return file_name

@classmethod
def compare_two_data_frame(cls, file_input_left, file_input_right, col_name, file_result='', sort=False,
print_shape=True,
Expand Down Expand Up @@ -1529,11 +1601,13 @@ def combine_list_items(cls, list_data, trim_data=True, clean_data=True):
list_data.append(temp)
list_data = list(filter(None, list_data))
if trim_data:
list_data = [x.strip() if x is not None else x for x in list_data]
list_data = [x.strip() if x is not None and isinstance(x, str) else x for x in list_data]
if clean_data:
list_data = [re.sub(r'^(;+ *)*|(;+ *)*$', '', x) if x is not None else x for x in list_data]
list_data = [re.sub(r'^(;+ *)*|(;+ *)*$', '', x) if x is not None and isinstance(x, str) else x for x in
list_data]
if 0 < len(list_data) < 2:
return list_data[0]
list_data = [str(x) for x in list_data]
res = PhConstants.SEPERATOR_MULTI_OBJ.join(list_data)
return res

Expand Down
9 changes: 5 additions & 4 deletions test/log/pycharm.log
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ User Name is Pratik Jaiswal
--------------------------------------------------------------------------------
User Account is impra
--------------------------------------------------------------------------------
Time Stamp is Tuesday, Jul 04 2023, 21:09:27:687837, IST (GMT+0530)
Time Stamp is Sunday, Jul 16 2023, 22:47:17:397799, IST (GMT+0530)
--------------------------------------------------------------------------------
pythonHelpers version is v2.2.8
pythonHelpers version is v2.2.9
--------------------------------------------------------------------------------
Test Tool version is v1.0.1
--------------------------------------------------------------------------------
Expand All @@ -24,9 +24,9 @@ User Name is Pratik Jaiswal
--------------------------------------------------------------------------------
User Account is impra
--------------------------------------------------------------------------------
Time Stamp is Tuesday, Jul 04 2023, 21:09:27:687837, IST (GMT+0530)
Time Stamp is Sunday, Jul 16 2023, 22:47:17:398797, IST (GMT+0530)
--------------------------------------------------------------------------------
pythonHelpers version is v2.2.8
pythonHelpers version is v2.2.9
--------------------------------------------------------------------------------
Python version is v3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
----------------------------------------------------------- -----------------------------------------------------------
Expand Down Expand Up @@ -94,6 +94,7 @@ P;j; Lp; nn
Pj
Remarks semi colon 1; Remarks semi colon 2
Remarks semi colon 1; Remarks semi colon 2
5; 6; 7
----------------------------------------------------- test_heading -----------------------------------------------------
- ..\..\Data\UserData\GSMA\SGP_22\v3_0_0\UpdateMetadataRequest\DerInput_Asn1Output_DirectInput_Asn1Element_UpdateMetad -
-- This is a long dataaaa for the testing of Trimming of remarks of individual item of the pool in asn play.; Item 100 -
Expand Down
1 change: 1 addition & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_list():
print(PhUtil.combine_list_items(['Pj;'], clean_data=True))
print(PhUtil.combine_list_items(['Remarks semi colon 1;', 'Remarks semi colon 2; '], clean_data=True))
print(PhUtil.combine_list_items(['Remarks semi colon 1;', 'Remarks semi colon 2; ; '], clean_data=True))
print(PhUtil.combine_list_items([5,6,7], clean_data=True))


def test_heading():
Expand Down
38 changes: 25 additions & 13 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ def __init__(self, name, all_caps_keywords=None, expected_op=None):


class test_obj_dec_to_hex:
def __init__(self, dec_num, digit_required=None, even_digits=None, inbuilt=False, expected_op=None):
def __init__(self, dec_num, digit_required=None, even_digits=None, assert_w_inbuilt=False, expected_op=None):
self.dec_num = dec_num
self.digit_required = digit_required
self.even_digits = even_digits
self.inbuilt = inbuilt
self.assert_w_inbuilt = assert_w_inbuilt
self.expected_op = expected_op


class test_obj_hex_str_to_dec:
def __init__(self, hex_str, expected_op=None):
def __init__(self, hex_str, signed_byte_handling=False, expected_op=None):
self.hex_str = hex_str
self.signed_byte_handling = signed_byte_handling
self.expected_op = expected_op


Expand All @@ -76,9 +77,10 @@ def __init__(self, hex_bytes, format=None, expected_op=None):
self.expected_op = expected_op


class test_obj_hex_str_to_hex_list:
def __init__(self, hex_str, expected_op=None):
class test_obj_hex_str_to_dec_list:
def __init__(self, hex_str, signed_byte_handling=False, expected_op=None):
self.hex_str = hex_str
self.signed_byte_handling = signed_byte_handling
self.expected_op = expected_op


Expand Down Expand Up @@ -338,13 +340,15 @@ def test_dec_to_hex(self):
test_obj_dec_to_hex(16, expected_op='10', even_digits=False),
test_obj_dec_to_hex(15, digit_required=32, expected_op='0000000000000000000000000000000F'),
test_obj_dec_to_hex(16, digit_required=32, expected_op='00000000000000000000000000000010'),
test_obj_dec_to_hex(15, expected_op='0xf', inbuilt=True),
test_obj_dec_to_hex(16, expected_op='0x10', inbuilt=True),
test_obj_dec_to_hex(15, expected_op='0xf', assert_w_inbuilt=True),
test_obj_dec_to_hex(16, expected_op='0x10', assert_w_inbuilt=True),
test_obj_dec_to_hex(-68, expected_op='BC'),
test_obj_dec_to_hex([10, -68, -46, 85], expected_op='0ABCD255'),
]

for count, test_obj in enumerate(test_obj_pool, start=1):
with self.subTest(STR_TEST_OBJ + str(count)):
if test_obj.inbuilt:
if test_obj.assert_w_inbuilt:
self.assertEqual(hex(test_obj.dec_num), test_obj.expected_op)
continue
if test_obj.digit_required is not None:
Expand All @@ -371,11 +375,15 @@ def test_hex_str_to_dec(self):
test_obj_hex_str_to_dec('10', expected_op=16),
test_obj_hex_str_to_dec('0x10', expected_op=16),
test_obj_hex_str_to_dec('0x0A', expected_op=10),
test_obj_hex_str_to_dec('0xBC', signed_byte_handling=True, expected_op=-68),
test_obj_hex_str_to_dec('0xBC', signed_byte_handling=False, expected_op=188),
]

for count, test_obj in enumerate(test_obj_pool, start=1):
with self.subTest(STR_TEST_OBJ + str(count)):
self.assertEqual(PhUtil.hex_str_to_dec(test_obj.hex_str), test_obj.expected_op)
self.assertEqual(
PhUtil.hex_str_to_dec(test_obj.hex_str, signed_byte_handling=test_obj.signed_byte_handling),
test_obj.expected_op)

def test_to_hex_string(self):
"""
Expand Down Expand Up @@ -409,21 +417,25 @@ def test_to_hex_string(self):
else:
self.assertEqual(PhUtil.to_hex_string(test_obj.hex_bytes), test_obj.expected_op)

def test_hex_str_to_hex_list(self):
def test_hex_str_to_dec_list(self):
"""
:return:
"""
test_obj_pool = [
test_obj_hex_str_to_hex_list('A000000559 1010 FFFFFF FF89 000001 00',
test_obj_hex_str_to_dec_list('A000000559 1010 FFFFFF FF89 000001 0',
expected_op=[160, 0, 0, 5, 89, 16, 16, 255, 255, 255, 255, 137, 0, 0, 1, 0]),
test_obj_hex_str_to_hex_list('A000000559 1010 FFFFFF FF89 000001 0',
test_obj_hex_str_to_dec_list('A000000559 1010 FFFFFF FF89 000001 00', signed_byte_handling=False,
expected_op=[160, 0, 0, 5, 89, 16, 16, 255, 255, 255, 255, 137, 0, 0, 1, 0]),
test_obj_hex_str_to_dec_list('A000000559 1010 FFFFFF FF89 000001 00', signed_byte_handling=True,
expected_op=[-96, 0, 0, 5, 89, 16, 16, -1, -1, -1, -1, -119, 0, 0, 1, 0]),
]

for count, test_obj in enumerate(test_obj_pool, start=1):
with self.subTest(STR_TEST_OBJ + str(count)):
self.assertEqual(PhUtil.hex_str_to_hex_list(test_obj.hex_str), test_obj.expected_op)
self.assertEqual(PhUtil.hex_str_to_dec_list(test_obj.hex_str,
signed_byte_handling=test_obj.signed_byte_handling),
test_obj.expected_op)

def test_rstrip_hex(self):
"""
Expand Down

0 comments on commit 331bb45

Please sign in to comment.