-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataInput.py
More file actions
59 lines (46 loc) · 1.67 KB
/
dataInput.py
File metadata and controls
59 lines (46 loc) · 1.67 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
from re import match
import helpers
## Prefix data
METRIC_PREFIX_VALUES = {"p":0.000000000001, "n":0.000000001, "µ":0.000001,
"u":0.000001, "m":0.001, "":1, "k":1000, "M":1000000,
"G":1000000000, "T":1000000000000}
class Data:
def __init__(self, pathToData):
self.pathToData = pathToData
self.dataLines = self.getDataLinesFromFile(self.pathToData)
## Properties
@property
def pathToData(self):
return self._pathToData
@pathToData.setter
def pathToData(self, value):
self._pathToData = value
@property
def dataLines(self):
return self._dataLines
@dataLines.setter
def dataLines(self, value):
self._dataLines = value
## Methods
def getDataLinesFromFile(self, file):
data = []
for line in file: # File opened via click
data.append(self.parseValueFormat(line))
return data
def parseValueFormat(self, value):
value = helpers.strip(value)
## Integer/floating portion of number = group 1
## Metric prefix = group 2
tokens = match(r'([0-9]+\.?[0-9]*)\s?([a-zA-Z]?)', value)
try:
componentValue = float(tokens.group(1))
prefixValue = METRIC_PREFIX_VALUES[tokens.group(2)]
except AttributeError as ae:
alert = "Invalid value for the input '{0}'. Input should be of the form <number> <metric prefix> where the number is a float or integer. Quitting now."
helpers.eclbPrint(alert.format(value))
return None
except KeyError as ke:
alert = "Invalid metric prefix for the input '{0}'. Input should be of the form <number> <metric prefix> where the prefix is in the list {1}. Quitting now."
helpers.eclbPrint(alert.format(value, [prefix for prefix in METRIC_PREFIX_VALUES]))
return None
return float(componentValue * prefixValue)