diff trafficintelligence/utils.py @ 1030:aafbc0bab925

moved method around to avoid cross-dependencies
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 19 Jun 2018 10:04:52 -0400
parents c6cf75a2ed08
children 045cb04ad7b8
line wrap: on
line diff
--- a/trafficintelligence/utils.py	Mon Jun 18 22:50:59 2018 -0400
+++ b/trafficintelligence/utils.py	Tue Jun 19 10:04:52 2018 -0400
@@ -15,13 +15,48 @@
 from pandas import DataFrame, concat
 import matplotlib.pyplot as plt
 
-from trafficintelligence.storage import openCheck
-
 datetimeFormat = "%Y-%m-%d %H:%M:%S"
 
 sjcamDatetimeFormat = "%Y_%m%d_%H%M%S"#2017_0626_143720
 
 #########################
+# txt files
+#########################
+
+commentChar = '#'
+
+delimiterChar = '%';
+
+def openCheck(filename, option = 'r', quitting = False):
+    '''Open file filename in read mode by default
+    and checks it is open'''
+    try:
+        return open(filename, option)
+    except IOError:
+        print('File {} could not be opened.'.format(filename))
+        if quitting:
+            from sys import exit
+            exit()
+        return None
+
+def readline(f, commentCharacters = commentChar):
+    '''Modified readline function to skip comments
+    Can take a list of characters or a string (in will work in both)'''
+    s = f.readline()
+    while (len(s) > 0) and s[0] in commentCharacters:
+        s = f.readline()
+    return s.strip()
+
+def getLines(f, delimiterChar = delimiterChar, commentCharacters = commentChar):
+    '''Gets a complete entry (all the lines) in between delimiterChar.'''
+    dataStrings = []
+    s = readline(f, commentCharacters)
+    while len(s) > 0 and s[0] != delimiterChar:
+        dataStrings += [s.strip()]
+        s = readline(f, commentCharacters)
+    return dataStrings
+
+#########################
 # Strings
 #########################