-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatatypes.py
More file actions
175 lines (143 loc) · 3.61 KB
/
Copy pathdatatypes.py
File metadata and controls
175 lines (143 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import re
import struct
# BYTE_LENGTH = 8
# WORD_BYTES = 4
# WORD_LENGTH = WORD_BYTES * BYTE_LENGTH
#********** DECIMAL, OCTAL, HEXADECIMAL, FLOAT, CHAR **********
def is_decimal(n):
ret = {
'number': 0,
'isDecimal': False,
'format': ''
}
# Chequeo de errores
if len(n) > 1 and n[0] == '0':
return ret
if '.' in n:
return ret
# Convierte
try:
if n.isdigit():
ret['isDecimal'] = True
ret['format'] = 'dec'
ret['number'] = int(n)
return ret
except ValueError:
return ret
return ret
def is_octal(n):
ret = {
'number': 0,
'isDecimal': False,
'format': ''
}
if n[0] == '0' and len(n) > 1:
octal = n[1:].lstrip('0')
try:
ret['number'] = int(octal, 8)
ret['isDecimal'] = (n == '0' + oct(ret['number'])[2:])
ret['format'] = 'octal'
return ret
except ValueError:
return ret
return ret
def is_hex(n):
ret = {
'number': 0,
'isDecimal': False,
'format': ''
}
if n[:2].lower() == '0x':
hex_num = n[2:].lstrip('0')
if hex_num == '':
return ret # Considerar "0x" como inválido
try:
ret['number'] = int(hex_num, 16)
ret['isDecimal'] = (n.lower() == '0x' + hex(ret['number'])[2:])
ret['format'] = 'hex'
return ret
except ValueError:
return ret
return ret
def isChar (n):
ret = {
'number':0,
'isDecimal': False,
'format': ''
}
return ret
def is_float (n):
ret = {
'number': 0.0,
'isFloat': False,
'format': ''
}
#Verificar errores
non_float = re.compile(r'[a-df-zA-DF-Z]+')
if non_float.search(n):
return ret
try:
ret['number'] = float(n)
ret['isFloat'] = True
ret['format'] = 'ieee754'
except ValueError:
ret['number'] = 0.0
ret['isFloat'] = False
ret['format'] = ''
return ret
#********* FUNCION PARA GET VALUE (API) ********
def dt_get_decimal_value(possible_value):
ret = {
'number': 0,
'isDecimal': True,
'format': ''
}
ret = is_octal (possible_value)
if ret.get('isDecimal', False):
return ret
ret = is_hex (possible_value)
if ret.get('isDecimal', False):
return ret
ret = is_decimal (possible_value)
if ret.get('isDecimal', False):
return ret
ret = isChar (possible_value)
if ret.get('isDecimal', False):
return ret
return ret
def dt_get_imm_value (value):
ret1 = {}
ret = {
'number': 0,
'isDecimal':False,
'isFloat': False
}
ret1 = dt_get_decimal_value(value)
if ret1 ['isDecimal']:
ret1['isFloat'] = False
return ret1
ret1 = is_float(value)
if ret1['isFloat']:
ret1['isDecimal'] = False
return ret1
return ret
def dt_binary2format(valbin,format):
val = int(valbin,2)
ret = 0
if format == 'dec':
ret = str(val)
elif format == 'octal':
ret = '0' + oct(val)[2:]
elif format == 'hex':
ret = '0x' + hex(val)[2:]
elif format == 'ascii':
ret = chr(val)
elif format == 'ieee754':
#valor de 32 bits
if len(valbin) == 32:
ret = struct.unpack('!f',struct.pack('!I', val))[0]
else:
raise ValueError("Se requiere una cadena binaria de 32 bits")
else:
ret = str(val)
return ret