From 754e9d2102ca983cdb0527a26a70a75953136e21 Mon Sep 17 00:00:00 2001 From: Rob Kiefer Date: Mon, 14 Mar 2016 12:01:50 -0400 Subject: [PATCH] utils: disallow empty strings as series names --- iobeam/resources/data.py | 9 +++------ iobeam/utils/utils.py | 18 ++++++++++++++++++ pylintrc | 2 +- tests/resources/test_data.py | 3 ++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/iobeam/resources/data.py b/iobeam/resources/data.py index bea671f..c226816 100644 --- a/iobeam/resources/data.py +++ b/iobeam/resources/data.py @@ -56,7 +56,7 @@ def asMilliseconds(self): elif self._type == TimeUnit.MILLISECONDS: return self._value elif self._type == TimeUnit.SECONDS: - return (self._value * 1000) + return self._value * 1000 def asMicroseconds(self): """This timestamp value represented in microseconds. @@ -137,8 +137,6 @@ def toDict(self): class DataStore(object): """A collection of data streams with rows batched by time.""" - _RESERVED_NAMES = ["time", "time_offset", "all"] - def __init__(self, columns): """Construct a new DataStore object with given columns. @@ -153,9 +151,8 @@ def __init__(self, columns): raise ValueError("columns cannot be None or empty") if not isinstance(columns, list): raise ValueError("columns must be a list of strings") - for name in DataStore._RESERVED_NAMES: - if name in [c.lower() for c in columns]: - raise ValueError("'{}' is a reserved column name".format(name)) + for c in columns: + utils.checkValidSeriesName(c) self._columns = list(columns) # defensive copy self._rows = [] diff --git a/iobeam/utils/utils.py b/iobeam/utils/utils.py index 3957910..8926a2c 100644 --- a/iobeam/utils/utils.py +++ b/iobeam/utils/utils.py @@ -67,6 +67,22 @@ def __checkNon0LengthString(value, valueName): elif len(value) == 0: raise ValueError("{} must be more than 0 characters".format(valueName)) +__RESERVED_COL_NAMES = ["time", "time_offset", "all"] + +def checkValidSeriesName(name): + """Check that a series name is valid. + + A valid series name is not reserved by iobeam (time, time_offset, all) and + a non-0 length string. + + Raises: + ValueError - If `name` is (a) None, (b) not a string/unicode, (c) + is of length 0, or (d) reserved. + """ + __checkNon0LengthString(name, "columns") + if name.lower() in __RESERVED_COL_NAMES: + raise ValueError("'{}' is a reserved column name".format(name)) + def checkValidDeviceId(deviceId): """Check that a deviceId is valid: string of len > 0 @@ -92,6 +108,8 @@ def checkValidProjectToken(token): __LOGGER = None def getLogger(): + """Get the logger for this library.""" + global __LOGGER if __LOGGER is not None: logger = __LOGGER diff --git a/pylintrc b/pylintrc index c2c8898..554e9c7 100644 --- a/pylintrc +++ b/pylintrc @@ -139,7 +139,7 @@ required-attributes= bad-functions=map,filter,input # Good variable names which should always be accepted, separated by a comma -good-names=i,j,k,ex,Run,_,r,p,d,f,pt +good-names=i,j,k,ex,Run,_,r,p,d,f,pt,ts,c # Bad variable names which should always be refused, separated by a comma bad-names=foo,bar,baz,toto,tutu,tata diff --git a/tests/resources/test_data.py b/tests/resources/test_data.py index a045470..023f88c 100644 --- a/tests/resources/test_data.py +++ b/tests/resources/test_data.py @@ -154,7 +154,8 @@ def verify(fields): ["time_offset", "col"], ["col", "TIME_OFfset"], ["all"], - ["AlL"] + ["AlL"], + ["fine", ""] ] for c in cases: verify(c)