Newer
Older
'''
This class read a csv file describing physical parameter with ROSCOP codification
'''
# constructor with values by default
def __init__(self, file):
self.file = file
''' overload string representation '''
return 'Class Roscop, file: %s, size = %d' % (self.file, len(self))
''' overload r[key] '''
return self.__hash[name]
def __len__(self):
''' overload len() '''
return len(self.__hash)
def displayCode(self, theKey):
def returnCode(self, theKey):
return(self[theKey])
# read code roscop file
def read(self):
with open(self.file, 'rt') as f:
reader = csv.DictReader(f, delimiter=';')
# logging.debug(" %s -> %s: %s" % (theKey, k, row[k]))
logging.debug(
" {} -> {}: {}".format(theKey, k, row[k]))
# for testing in standalone context
# usage:
# > python physicalParameter.py code_roscop.csv -k TEMP
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.returnCode(key[0])['long_name']))