Mercurial Hosting > traffic-intelligence
diff python/storage.py @ 998:933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 27 May 2018 23:22:48 -0400 |
parents | cc89267b5ff9 |
children | 75af46516b2b |
line wrap: on
line diff
--- a/python/storage.py Fri May 25 18:15:18 2018 -0400 +++ b/python/storage.py Sun May 27 23:22:48 2018 -0400 @@ -634,7 +634,7 @@ sys.exit() try: cursor.execute('CREATE TABLE IF NOT EXISTS gaussians2d (poi_id INTEGER, id INTEGER, type VARCHAR, x_center REAL, y_center REAL, covariance VARCHAR, covariance_type VARCHAR, weight, precisions_cholesky VARCHAR, PRIMARY KEY(poi_id, id))') - for i in xrange(gmm.n_components): + for i in range(gmm.n_components): cursor.execute('INSERT INTO gaussians2d VALUES(?,?,?,?,?,?,?,?,?)', (gmmId, i, gmmType, gmm.means_[i][0], gmm.means_[i][1], str(gmm.covariances_[i].tolist()), gmm.covariance_type, gmm.weights_[i], str(gmm.precisions_cholesky_[i].tolist()))) connection.commit() except sqlite3.OperationalError as error: @@ -710,7 +710,7 @@ cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))') - for route in prototypes.keys(): + for route in prototypes: if prototypes[route]!=[]: for i in prototypes[route]: cursor.execute('insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)',(i,route[0],route[1],nMatching[route][i])) @@ -737,7 +737,7 @@ for row in cursor: route=(row[1],row[2]) - if route not in prototypes.keys(): + if route not in prototypes: prototypes[route]=[] prototypes[route].append(row[0]) nMatching[row[0]]=row[3] @@ -753,7 +753,7 @@ cursor.execute("CREATE TABLE IF NOT EXISTS labels (object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, prototype_id INTEGER, PRIMARY KEY(object_id))") - for route in labels.keys(): + for route in labels: if labels[route]!=[]: for i in labels[route]: for j in labels[route][i]: @@ -777,9 +777,9 @@ for row in cursor: route=(row[1],row[2]) p=row[3] - if route not in labels.keys(): + if route not in labels: labels[route]={} - if p not in labels[route].keys(): + if p not in labels[route]: labels[route][p]=[] labels[route][p].append(row[0]) @@ -793,7 +793,7 @@ cursor.execute("CREATE TABLE IF NOT EXISTS speedprototypes (spdprototype_id INTEGER,prototype_id INTEGER,routeID_start INTEGER, routeID_end INTEGER, nMatching INTEGER, PRIMARY KEY(spdprototype_id))") - for route in prototypes.keys(): + for route in prototypes: if prototypes[route]!={}: for i in prototypes[route]: if prototypes[route][i]!= []: @@ -820,9 +820,9 @@ for row in cursor: route=(row[2],row[3]) - if route not in prototypes.keys(): + if route not in prototypes: prototypes[route]={} - if row[1] not in prototypes[route].keys(): + if row[1] not in prototypes[route]: prototypes[route][row[1]]=[] prototypes[route][row[1]].append(row[0]) nMatching[row[0]]=row[4] @@ -838,7 +838,7 @@ cursor.execute("CREATE TABLE IF NOT EXISTS routes (object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, PRIMARY KEY(object_id))") - for route in Routes.keys(): + for route in Routes: if Routes[route]!=[]: for i in Routes[route]: cursor.execute("insert into routes (object_id, routeIDstart,routeIDend) values (?,?,?)",(i,route[0],route[1])) @@ -860,7 +860,7 @@ for row in cursor: route=(row[1],row[2]) - if route not in Routes.keys(): + if route not in Routes: Routes[route]=[] Routes[route].append(row[0]) @@ -928,21 +928,15 @@ values.append(l.split(delimiterChar)[1].strip()) return values -class FakeSecHead(object): - '''Add fake section header [asection] +def addSectionHeader(propertiesFile, headerName = 'main'): + '''Add fake section header from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 use read_file in Python 3.2+ ''' - def __init__(self, fp): - self.fp = fp - self.sechead = '[main]\n' - - def readline(self): - if self.sechead: - try: return self.sechead - finally: self.sechead = None - else: return self.fp.readline() + yield '[{}]\n'.format(headerName) + for line in propertiesFile: + yield line def loadPemsTraffic(filename): '''Loads traffic data downloaded from the http://pems.dot.ca.gov clearinghouse @@ -952,7 +946,7 @@ items = l.split(',') headers = ['time', 'station', 'district', 'route', 'direction', 'lanetype', 'length', 'nsamples', 'pctobserved', 'flow', 'occupancy', 'speed', 'delay35', 'delay40', 'delay45', 'delay50', 'delay55', 'delay60'] nLanes = (len(items)-len(headers))/3 - for i in xrange(nLanes): + for i in range(nLanes): headers += ['flow{}'.format(i+1), 'occupancy{}'.format(i+1), 'speed{}'.format(i+1)] f.close() return read_csv(filename, delimiter = ',', names = headers) @@ -1029,7 +1023,7 @@ # positions should be rounded to nDecimals decimals only objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) if objectNumbers is not None and objectNumbers > 0 and len(objects) >= objectNumbers: - objects.values() + return list(objects.values()) else: if filename.endswith(".fzp"): inputfile = openCheck(filename, quitting = True) @@ -1077,7 +1071,7 @@ printDBError(error) else: print("File type of "+filename+" not supported (only .sqlite and .fzp files)") - return objects.values() + return list(objects.values()) def selectPDLanes(data, lanes = None): '''Selects the subset of data for the right lanes @@ -1257,7 +1251,7 @@ timeInterval = obj.getTimeInterval() positions = obj.getPositions() curvilinearPositions = obj.getCurvilinearPositions() - for i in xrange(int(obj.length())): + for i in range(int(obj.length())): p1 = positions[i] s = '{},{},{},{}'.format(obj.num,timeInterval[i],p1.x,p1.y) if curvilinearPositions is not None: @@ -1279,10 +1273,10 @@ class ClassifierParameters(VideoFilenameAddable): 'Class for the parameters of object classifiers' def loadConfigFile(self, filename): - from ConfigParser import ConfigParser + from configparser import ConfigParser config = ConfigParser() - config.readfp(FakeSecHead(openCheck(filename))) + config.read_file(addSectionHeader(openCheck(filename))) self.sectionHeader = config.sections()[0] self.pedBikeCarSVMFilename = config.get(self.sectionHeader, 'pbv-svm-filename') @@ -1346,10 +1340,10 @@ Note: framerate is already taken into account''' def loadConfigFile(self, filename): - from ConfigParser import ConfigParser + from configparser import ConfigParser - config = ConfigParser() - config.readfp(FakeSecHead(openCheck(filename))) + config = ConfigParser(strict=False) + config.read_file(addSectionHeader(openCheck(filename))) self.sectionHeader = config.sections()[0] # Tracking/display parameters @@ -1441,7 +1435,7 @@ # deprecated class SceneParameters(object): def __init__(self, config, sectionName): - from ConfigParser import NoOptionError + from configparser import NoOptionError from ast import literal_eval try: self.sitename = config.get(sectionName, 'sitename') @@ -1460,7 +1454,7 @@ @staticmethod def loadConfigFile(filename): - from ConfigParser import ConfigParser + from configparser import ConfigParser config = ConfigParser() config.readfp(openCheck(filename)) configDict = dict()