Newer
Older
'''
This class read a csv file describing physical parameter with ROSCOP codification
'''
# constructor with values by default
''' overload string representation '''
return 'Class Roscop, file: %s, size = %d' % (self.file, len(self))
for a given key return the values as a dictionary '''
" physicalParametr.py: invalid key: \"{}\"".format(key))
else:
return self.__hash[key]
def __setitem__(self, key, value):
''' overload r[key] = value '''
if type(value) is not dict:
logging.error(
" physicalParametr.py: the value: \"{}\" must be a dictionary".format(value))
return
if key not in self.__hash:
self.__hash[key] = value
else:
logging.error(
" physicalParametr.py: modify the existing key: \"{}\" is not allowed".format(key))
def __len__(self):
''' overload len() '''
return len(self.__hash)
''' for a given key print it's name and values as a dictionary '''
print("%s :" % key)
print(self[key])
# read code roscop file
def read(self):
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.
# 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])
" {} -> {}, {} = {}".format(theKey, k, type(row[k]), row[k]))
# for testing in standalone context
# > python physicalParameter.py code_roscop.csv -k TEMP -d
parser = argparse.ArgumentParser(
description='This class Roscop parse a csv file describing physical parameter codification')
parser.add_argument("-d", "--debug", help="display debug informations",
action="store_true")
parser.add_argument("-k", "--key", nargs='+',
help="display dictionary for key(s), example -k TEMP [PSAL ...]")
parser.add_argument("file", type=str, help="the csv file to parse")
# display extra logging info
# see: https://stackoverflow.com/questions/14097061/easier-way-to-enable-verbose-logging
# https://docs.python.org/2/howto/argparse.html
args = parser.parse_args()
if args.debug:
logging.basicConfig(
format='%(levelname)s:%(message)s', level=logging.DEBUG)
# Read the csv file and create an instance of Roscop class
r = Roscop(args.file)
# get -k arg(s) list
key = args.key
# if args list is empty, key contain NoneType
if key is not None:
for k in key:
print("{}: {}".format(key[0], r[key[0]]['long_name']))
r['TOTO'] = {'uncle': 'tata'}
print(r['TOTO'])
r['TEMP'] = 'tata'