Skip to content
Snippets Groups Projects
Commit f14579bf authored by jacques.grelet_ird.fr's avatar jacques.grelet_ird.fr
Browse files

give the float type to numeric value

parent bcbe6fe6
No related branches found
No related tags found
No related merge requests found
...@@ -27,11 +27,11 @@ class Roscop: ...@@ -27,11 +27,11 @@ class Roscop:
return 'Class Roscop, file: %s, size = %d' % (self.file, len(self)) return 'Class Roscop, file: %s, size = %d' % (self.file, len(self))
def __getitem__(self, key): def __getitem__(self, key):
''' overload r[key] ''' overload r[key]
for a given key return the values as a dictionary ''' for a given key return the values as a dictionary '''
if key not in self.__hash: if key not in self.__hash:
logging.error( logging.error(
" Invalid key: \"{}\"".format(key)) " physicalParametr.py: invalid key: \"{}\"".format(key))
else: else:
return self.__hash[key] return self.__hash[key]
...@@ -39,14 +39,14 @@ class Roscop: ...@@ -39,14 +39,14 @@ class Roscop:
''' overload r[key] = value ''' ''' overload r[key] = value '''
if type(value) is not dict: if type(value) is not dict:
logging.error( logging.error(
" The value: \"{}\" must be a dictionary".format(value)) " physicalParametr.py: the value: \"{}\" must be a dictionary".format(value))
return return
if key not in self.__hash: if key not in self.__hash:
self.__hash[key] = value self.__hash[key] = value
else: else:
logging.error( logging.error(
" Modify the existing key: \"{}\" is not allowed".format(key)) " physicalParametr.py: modify the existing key: \"{}\" is not allowed".format(key))
def __repr__(self): def __repr__(self):
''' overload print() ''' ''' overload print() '''
...@@ -65,18 +65,26 @@ class Roscop: ...@@ -65,18 +65,26 @@ class Roscop:
# read code roscop file # read code roscop file
def read(self): def read(self):
with open(self.file, 'rt') as f: with open(self.file, 'rt') as f:
# Create an object that maps the information in each row to an OrderedDict
# the values in the first row of file f will be used as the fieldnames.
reader = csv.DictReader(f, delimiter=';') reader = csv.DictReader(f, delimiter=';')
for row in reader: for row in reader:
theKey = row[reader.fieldnames[0]] theKey = row[reader.fieldnames[0]]
for k in reader.fieldnames: for k in reader.fieldnames:
# if the value of key is empty # if the value of key is empty
if row[k] == '' or k == 'key': if row[k] == '' or k == 'key':
# remove the key # remove the key
row.pop(k) row.pop(k)
else: else:
# use the second line with key string to convert each numeric type into float
if theKey != 'string':
if self['string'][k] == 'numeric':
row[k] = float(row[k])
logging.debug( logging.debug(
" {} -> {}: {}".format(theKey, k, row[k])) " {} -> {}, {} = {}".format(theKey, k, type(row[k]), row[k]))
self.__hash[theKey] = row self.__hash[theKey] = row
return return
...@@ -87,7 +95,7 @@ class Roscop: ...@@ -87,7 +95,7 @@ class Roscop:
if __name__ == "__main__": if __name__ == "__main__":
# usage: # usage:
# > python physicalParameter.py code_roscop.csv -k TEMP # > python physicalParameter.py code_roscop.csv -k TEMP -d
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='This class Roscop parse a csv file describing physical parameter codification') description='This class Roscop parse a csv file describing physical parameter codification')
parser.add_argument("-d", "--debug", help="display debug informations", parser.add_argument("-d", "--debug", help="display debug informations",
...@@ -116,9 +124,9 @@ if __name__ == "__main__": ...@@ -116,9 +124,9 @@ if __name__ == "__main__":
# if args list is empty, key contain NoneType # if args list is empty, key contain NoneType
if key is not None: if key is not None:
for k in key: for k in key:
r.displayCode(k) r[k]
print("{}: {}".format(key[0], r.returnCode(key[0])['long_name'])) print("{}: {}".format(key[0], r[key[0]]['long_name']))
r['TOTO'] = {'uncle': 'tata'} r['TOTO'] = {'uncle': 'tata'}
print(r['TOTO']) print(r['TOTO'])
r['TEMP'] = 'tata' r['TEMP'] = 'tata'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment