def _check_qty_cmp(pint_en): r = repr(hszinc.Quantity(4, unit='A')) if six.PY2: assert (r == 'BasicQuantity(4, u\'A\')') or ( r == 'PintQuantity(4, u\'A\')') else: assert (r == 'BasicQuantity(4, \'A\')') or ( r == 'PintQuantity(4, \'A\')') assert str(hszinc.Quantity(4, unit='A')) == '4 A'
def _check_qty_op(pint_en, fn, a, b): _enable_pint(pint_en) qa = hszinc.Quantity(a) qb = hszinc.Quantity(b) # Reference value ref = fn(a, b) assert fn(qa, qb) == ref assert fn(qa, b) == ref assert fn(a, qb) == ref
def _check_qty_cmp(pint_en): if 'cmp' not in set(locals().keys()): def cmp(a, b): return a.__cmp__(b) _enable_pint(pint_en) a = hszinc.Quantity(-3) b = hszinc.Quantity(432) c = hszinc.Quantity(4, unit='A') d = hszinc.Quantity(10, unit='A') e = hszinc.Quantity(12, unit='V') assert cmp(a, b) < 0 assert cmp(b, a) > 0 assert cmp(a, hszinc.Quantity(-3)) == 0 assert cmp(c, d) < 0 assert cmp(d, c) > 0 assert cmp(c, hszinc.Quantity(4, unit='A')) == 0 try: cmp(c, e) except TypeError as ex: assert str(ex) == 'Quantity units differ: A vs V'
def make_grid_meta(version=hszinc.VER_2_0): grid = hszinc.Grid(version=version) grid.metadata['aString'] = 'aValue' grid.metadata['aNumber'] = 3.14159 grid.metadata['aNull'] = None grid.metadata['aMarker'] = hszinc.MARKER grid.metadata['aQuantity'] = hszinc.Quantity(123,'Hz') grid.column['empty'] = {} return grid
def make_col_meta(version=hszinc.VER_2_0): grid = hszinc.Grid(version=version) col_meta = hszinc.MetadataObject() col_meta['aString'] = 'aValue' col_meta['aNumber'] = 3.14159 col_meta['aNull'] = None col_meta['aMarker'] = hszinc.MARKER col_meta['aQuantity'] = hszinc.Quantity(123,'Hz') grid.column['empty'] = col_meta return grid
def make_metadata_grid(version=hszinc.VER_2_0): grid = hszinc.Grid(version=version) grid.metadata['database'] = 'test' grid.metadata['dis'] = 'Site Energy Summary' grid.column['siteName'] = {'dis': 'Sites'} grid.column['val'] = hszinc.MetadataObject() grid.column['val']['dis'] = 'Value' grid.column['val']['unit'] = 'kW' grid.extend([ { 'siteName': 'Site 1', 'val': hszinc.Quantity(356.214,'kW'), }, { 'siteName': 'Site 2', 'val': hszinc.Quantity(463.028,'kW'), }, ]) return grid
def test_data_types_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'A NA', 'value': hszinc.NA, }, { 'comment': 'An empty list', 'value': [], }, { 'comment': 'A null value in a list', 'value': [None], }, { 'comment': 'A marker in a list', 'value': [hszinc.MARKER], }, { 'comment': 'Booleans', 'value': [True, False], }, { 'comment': 'References', 'value': [hszinc.Ref('a-ref'), hszinc.Ref('a-ref', 'a value')], }, { 'comment': 'A quantity', 'value': [hszinc.Quantity(500, 'miles')], }, { 'comment': 'A XStr', 'value': [hszinc.XStr("hex", 'deadbeef')], }, ]) grid_str = hszinc.dump(grid) ref_str = '''ver:"3.0" comment,value "A NA",NA "An empty list",[] "A null value in a list",[N] "A marker in a list",[M] "Booleans",[T,F] "References",[@a-ref,@a-ref "a value"] "A quantity",[500miles] "A XStr",[hex("deadbeef")] ''' assert grid_str == ref_str
def test_scalar_dict_zinc_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'An empty dict', 'value': {}, }, { 'comment': 'A marker in a dict', 'value': { "marker": hszinc.MARKER }, }, { 'comment': 'A references in a dict', 'value': { "ref": hszinc.Ref('a-ref'), "ref2": hszinc.Ref('a-ref', 'a value') }, }, { 'comment': 'A quantity in a dict', 'value': { "quantity": hszinc.Quantity(500, 'miles') }, }, ]) grid_str = hszinc.dump(grid, mode=hszinc.MODE_ZINC) assert grid_str == ("ver:\"3.0\"\n" "comment,value\n" "\"An empty dict\",{}\n" "\"A marker in a dict\",{marker:M}\n" "\"A references in a dict\",{" + " ".join([str(k) + ":" + str(v) for k, v in {"ref": "@a-ref", "ref2": "@a-ref"}.items()]) \ .replace("ref2:@a-ref", "ref2:@a-ref \"a value\"") + \ "}\n" "\"A quantity in a dict\",{quantity:500miles}\n")
def json_decode(raw_json): ''' Recusively scan the de-serialised JSON tree for objects that correspond to standard Haystack types and return the Python equivalents. ''' # Simple cases if raw_json is None: return None elif raw_json == MARKER: return hszinc.MARKER elif isinstance(raw_json, dict): result = {} for key, value in raw_json.items(): result[json_decode(key)] = json_decode(value) return result elif isinstance(raw_json, list): return list(map(json_decode, raw_json)) elif isinstance(raw_json, tuple): return tuple(map(json_decode, raw_json)) # Is it a number? match = NUMBER_RE.match(raw_json) if match: # We'll get a value and a unit, amongst other tokens. matched = match.groups() value = float(matched[0]) if matched[-1] is not None: # It's a quantity return hszinc.Quantity(value, matched[-1]) else: # It's a raw value return value # Is it a string? match = STR_RE.match(raw_json) if match: return match.group(1) # Is it a reference? match = REF_RE.match(raw_json) if match: matched = match.groups() if matched[-1] is not None: return hszinc.Ref(matched[0], matched[-1], has_value=True) else: return hszinc.Ref(matched[0]) # Is it a date? match = DATE_RE.match(raw_json) if match: (year, month, day) = match.groups() return datetime.date(year=int(year), month=int(month), day=int(day)) # Is it a time? match = TIME_RE.match(raw_json) if match: (hour, minute, second) = match.groups() # Convert second to seconds and microseconds second = float(second) int_second = int(second) second -= int_second microsecond = second * 1e6 return datetime.time(hour=int(hour), minute=int(minute), second=int_second, microseconds=microsecond) # Is it a date/time? match = DATETIME_RE.match(raw_json) if match: matches = match.groups() # Parse ISO8601 component isodate = iso8601.parse_date(matches[0]) # Parse timezone tzname = matches[-1] if tzname is None: return isodate # No timezone given else: try: tz = zoneinfo.timezone(tzname) return isodate.astimezone(tz) except: return isodate # Is it a URI? match = URI_RE.match(raw_json) if match: return hszinc.Uri(match.group(1)) # Is it a Bin? match = BIN_RE.match(raw_json) if match: return hszinc.Bin(match.group(1)) # Is it a co-ordinate? match = COORD_RE.match(raw_json) if match: (lat, lng) = match.groups() return hszinc.Coord(lat, lng) # Maybe it's a bare string? return raw_json
def _check_qty_op(pint_en, fn, *vals): _enable_pint(pint_en) for v in vals: q = hszinc.Quantity(v) assert fn(q) == fn(q.value)
def _check_hash(val, unit=None): a = hszinc.Quantity(val, unit=unit) b = hszinc.Quantity(val, unit=unit) assert a is not b assert hash(a) == hash(b)
def test_data_types_json_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'An empty list', 'value': [], }, { 'comment': 'A null value in a list', 'value': [None], }, { 'comment': 'A marker in a list', 'value': [hszinc.MARKER], }, { 'comment': 'Booleans', 'value': [True, False], }, { 'comment': 'References', 'value': [hszinc.Ref('a-ref'), hszinc.Ref('a-ref', 'a value')], }, { 'comment': 'A quantity', 'value': [hszinc.Quantity(500,'miles')], }, ]) grid_json = json.loads(hszinc.dump(grid, mode=hszinc.MODE_JSON)) assert grid_json == { 'meta': { 'ver': '3.0' }, 'cols': [ {'name': 'comment'}, {'name': 'value'}, ], 'rows': [ { 'comment':"s:An empty list", 'value':[] }, { 'comment':"s:A null value in a list", 'value': [None] }, { 'comment':"s:A marker in a list", 'value': ['m:'] }, { 'comment':"s:Booleans", 'value': [True, False] }, { 'comment':"s:References", 'value': ['r:a-ref' , 'r:a-ref a value'] }, { 'comment':"s:A quantity", 'value': ['n:500.000000 miles'] # Python is more precise # than The Proclaimers } ] }
def test_data_types_json(): grid = hszinc.Grid(version=hszinc.VER_2_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'A null value', 'value': None, }, { 'comment': 'A marker', 'value': hszinc.MARKER, }, { 'comment': 'A boolean, indicating False', 'value': False, }, { 'comment': 'A boolean, indicating True', 'value': True, }, { 'comment': 'A reference, without value', 'value': hszinc.Ref('a-ref'), }, { 'comment': 'A reference, with value', 'value': hszinc.Ref('a-ref', 'a value'), }, { 'comment': 'A binary blob', 'value': hszinc.Bin('text/plain'), }, { 'comment': 'A quantity', 'value': hszinc.Quantity(500,'miles'), }, { 'comment': 'A quantity without unit', 'value': hszinc.Quantity(500,None), }, { 'comment': 'A coordinate', 'value': hszinc.Coordinate(-27.4725,153.003), }, { 'comment': 'A URI', 'value': hszinc.Uri('http://www.example.com'), }, { 'comment': 'A string', 'value': 'This is a test\n'\ 'Line two of test\n'\ '\tIndented with "quotes" and \\backslashes\\', }, { 'comment': 'A date', 'value': datetime.date(2016,1,13), }, { 'comment': 'A time', 'value': datetime.time(7,51,43,microsecond=12345), }, { 'comment': 'A timestamp (non-UTC)', 'value': pytz.timezone('Europe/Berlin').localize(\ datetime.datetime(2016,1,13,7,51,42,12345)), }, { 'comment': 'A timestamp (UTC)', 'value': pytz.timezone('UTC').localize(\ datetime.datetime(2016,1,13,7,51,42,12345)), }, ]) grid_json = json.loads(hszinc.dump(grid, mode=hszinc.MODE_JSON)) assert grid_json == { 'meta': {'ver':'2.0'}, 'cols': [ {'name':'comment'}, {'name':'value'}, ], 'rows': [ { 'comment': 's:A null value', 'value': None}, { 'comment': 's:A marker', 'value': 'm:'}, { 'comment': 's:A boolean, indicating False', 'value': False}, { 'comment': 's:A boolean, indicating True', 'value': True}, { 'comment': 's:A reference, without value', 'value': 'r:a-ref'}, { 'comment': 's:A reference, with value', 'value': 'r:a-ref a value'}, { 'comment': 's:A binary blob', 'value': 'b:text/plain'}, { 'comment': 's:A quantity', 'value': 'n:500.000000 miles'}, { 'comment': 's:A quantity without unit', 'value': 'n:500.000000'}, { 'comment': 's:A coordinate', 'value': 'c:-27.472500,153.003000'}, { 'comment': 's:A URI', 'value': 'u:http://www.example.com'}, { 'comment': 's:A string', 'value': 's:This is a test\n'\ 'Line two of test\n'\ '\tIndented with \"quotes\" '\ 'and \\backslashes\\'}, { 'comment': 's:A date', 'value': 'd:2016-01-13'}, { 'comment': 's:A time', 'value': 'h:07:51:43.012345'}, { 'comment': 's:A timestamp (non-UTC)', 'value': 't:2016-01-13T07:51:42.012345+01:00 Berlin'}, { 'comment': 's:A timestamp (UTC)', 'value': 't:2016-01-13T07:51:42.012345+00:00 UTC'}, ], }
def test_data_types_v2(): grid = hszinc.Grid(version=hszinc.VER_2_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'A null value', 'value': None, }, { 'comment': 'A marker', 'value': hszinc.MARKER, }, { 'comment': 'A "remove" object', 'value': hszinc.REMOVE, }, { 'comment': 'A boolean, indicating False', 'value': False, }, { 'comment': 'A boolean, indicating True', 'value': True, }, { 'comment': 'A reference, without value', 'value': hszinc.Ref('a-ref'), }, { 'comment': 'A reference, with value', 'value': hszinc.Ref('a-ref', 'a value'), }, { 'comment': 'A binary blob', 'value': hszinc.Bin('text/plain'), }, { 'comment': 'A quantity', 'value': hszinc.Quantity(500,'miles'), }, { 'comment': 'A quantity without unit', 'value': hszinc.Quantity(500,None), }, { 'comment': 'A coordinate', 'value': hszinc.Coordinate(-27.4725,153.003), }, { 'comment': 'A URI', 'value': hszinc.Uri(u'http://www.example.com#`unicode:\u1234\u5678`'), }, { 'comment': 'A string', 'value': u'This is a test\n'\ u'Line two of test\n'\ u'\tIndented with "quotes", \\backslashes\\ and '\ u'Unicode characters: \u1234\u5678 and a $ dollar sign', }, { 'comment': 'A date', 'value': datetime.date(2016,1,13), }, { 'comment': 'A time', 'value': datetime.time(7,51,43,microsecond=12345), }, { 'comment': 'A timestamp (non-UTC)', 'value': pytz.timezone('Europe/Berlin').localize(\ datetime.datetime(2016,1,13,7,51,42,12345)), }, { 'comment': 'A timestamp (UTC)', 'value': pytz.timezone('UTC').localize(\ datetime.datetime(2016,1,13,7,51,42,12345)), }, ]) grid_str = hszinc.dump(grid) ref_str = '''ver:"2.0" comment,value "A null value",N "A marker",M "A \\"remove\\" object",R "A boolean, indicating False",F "A boolean, indicating True",T "A reference, without value",@a-ref "A reference, with value",@a-ref "a value" "A binary blob",Bin(text/plain) "A quantity",500miles "A quantity without unit",500 "A coordinate",C(-27.472500,153.003000) "A URI",`http://www.example.com#\\`unicode:\\u1234\\u5678\\`` "A string","This is a test\\nLine two of test\\n\\tIndented with \\"quotes\\", \\\\backslashes\\\\ and Unicode characters: \\u1234\\u5678 and a \\$ dollar sign" "A date",2016-01-13 "A time",07:51:43.012345 "A timestamp (non-UTC)",2016-01-13T07:51:42.012345+01:00 Berlin "A timestamp (UTC)",2016-01-13T07:51:42.012345+00:00 UTC ''' assert grid_str == ref_str
def gen_random_quantity(): return hszinc.Quantity(gen_random_num(), to_pint('percent'))
def test_scalar_dict_json_v3(): grid = hszinc.Grid(version=hszinc.VER_3_0) grid.column['comment'] = {} grid.column['value'] = {} grid.extend([ { 'comment': 'An empty dict', 'value': {}, }, { 'comment': 'A marker in a dict', 'value': { "marker": hszinc.MARKER }, }, { 'comment': 'A references in a dict', 'value': { "ref": hszinc.Ref('a-ref'), "ref2": hszinc.Ref('a-ref', 'a value') }, }, { 'comment': 'A quantity in a dict', 'value': { "quantity": hszinc.Quantity(500, 'miles') }, }, ]) grid_json = json.loads(hszinc.dump(grid, mode=hszinc.MODE_JSON)) assert grid_json == { 'meta': { 'ver': '3.0' }, 'cols': [ { 'name': 'comment' }, { 'name': 'value' }, ], 'rows': [{ 'comment': "s:An empty dict", 'value': {} }, { 'comment': "s:A marker in a dict", 'value': { 'marker': 'm:' } }, { 'comment': "s:A references in a dict", 'value': { 'ref': 'r:a-ref', 'ref2': 'r:a-ref a value' } }, { 'comment': "s:A quantity in a dict", 'value': { "quantity": 'n:500.000000 miles' } }] }