def _load_md(rec, keys): """ Helper for load ensemble. Extracts metadata defined by keys list and posts to a Metadata container that is returned. """ # do this stupid for now without error handlers md = Metadata() for k in keys: x = rec[k] md.put(k, x) return md
def test_Metadata(): md = Metadata() assert repr(md) == "Metadata({})" dic = {1: 1} md.put("dict", dic) val = md.get("dict") val[2] = 2 del val dic[3] = 3 del dic md["dict"][4] = 4 assert md["dict"] == {1: 1, 2: 2, 3: 3, 4: 4} md = Metadata({"array": np.array([3, 4])}) md["dict"] = {1: 1, 2: 2} md["str'i\"ng"] = "str'i\"ng" md["str'ing"] = "str'ing" md["double"] = 3.14 md["bool"] = True md["int"] = 7 md["string"] = "str\0ing" md["string"] = "str\ning" md["str\ting"] = "str\ting" md["str\0ing"] = "str\0ing" md["str\\0ing"] = "str\\0ing" md_copy = pickle.loads(pickle.dumps(md)) for i in md: if i == "array": assert (md[i] == md_copy[i]).all() else: assert md[i] == md_copy[i] md_copy2 = Metadata(dict(md)) assert not md_copy2.modified() assert md.modified() == md_copy.modified() md = Metadata({ "<class 'numpy.ndarray'>": np.array([3, 4]), "<class 'dict'>": { 1: 1, 2: 2 }, "string": "string", "double": 3.14, "bool": True, "long": 7, "<class 'bytes'>": b"\xba\xd0\xba\xd0", "<class 'NoneType'>": None, }) for i in md: assert md.type(i) == i md[b"\xba\xd0"] = b"\xba\xd0" md_copy = pickle.loads(pickle.dumps(md)) for i in md: if i == "<class 'numpy.ndarray'>": assert (md[i] == md_copy[i]).all() else: assert md[i] == md_copy[i] del md["<class 'numpy.ndarray'>"] md_copy.erase("<class 'numpy.ndarray'>") assert not "<class 'numpy.ndarray'>" in md assert not "<class 'numpy.ndarray'>" in md_copy assert md.keys() == md_copy.keys() with pytest.raises(TypeError, match="Metadata"): reversed(md) md = Metadata({1: 1, 3: 3}) md_copy = Metadata({2: 2, 3: 30}) md += md_copy assert md.__repr__() == "Metadata({'1': 1, '2': 2, '3': 30})" # Test with real data dic = { "_format": "MSEED", "arrival.time": 1356901212.242550, "calib": 1.000000, "chan": "BHZ", "delta": 0.025000, "deltim": -1.000000, "endtime": 1356904168.544538, "iphase": "P", "loc": "", "mseed": { "dataquality": "D", "number_of_records": 36, "encoding": "STEIM2", "byteorder": ">", "record_length": 4096, "filesize": 726344704, }, "net": "CI", "npts": 144000, "phase": "P", "sampling_rate": 40.000000, "site.elev": 0.258000, "site.lat": 35.126900, "site.lon": -118.830090, "site_id": "5fb6a67b37f8eef2f0658e9a", "sta": "ARV", "starttime": 1356900568.569538, } md = Metadata(dic) md["mod"] = "mod" md_copy = pickle.loads(pickle.dumps(md)) for i in md: assert md[i] == md_copy[i] assert md.modified() == md_copy.modified()
def test_Metadata(): md = Metadata() assert repr(md) == 'Metadata({})' dic = {1: 1} md.put('dict', dic) val = md.get('dict') val[2] = 2 del val dic[3] = 3 del dic md['dict'][4] = 4 assert md['dict'] == {1: 1, 2: 2, 3: 3, 4: 4} md = Metadata({'array': np.array([3, 4])}) md['dict'] = {1: 1, 2: 2} md['str\'i"ng'] = 'str\'i"ng' md["str'ing"] = "str'ing" md['double'] = 3.14 md['bool'] = True md['int'] = 7 md["string"] = "str\0ing" md["string"] = "str\ning" md["str\ting"] = "str\ting" md["str\0ing"] = "str\0ing" md["str\\0ing"] = "str\\0ing" md_copy = pickle.loads(pickle.dumps(md)) for i in md: if i == 'array': assert (md[i] == md_copy[i]).all() else: assert md[i] == md_copy[i] md = Metadata({ "<class 'numpy.ndarray'>": np.array([3, 4]), "<class 'dict'>": { 1: 1, 2: 2 }, 'string': 'string', 'double': 3.14, 'bool': True, 'long': 7, "<class 'bytes'>": b'\xba\xd0\xba\xd0', "<class 'NoneType'>": None }) for i in md: assert md.type(i) == i md[b'\xba\xd0'] = b'\xba\xd0' md_copy = pickle.loads(pickle.dumps(md)) for i in md: if i == "<class 'numpy.ndarray'>": assert (md[i] == md_copy[i]).all() else: assert md[i] == md_copy[i] del md["<class 'numpy.ndarray'>"] md_copy.erase("<class 'numpy.ndarray'>") assert not "<class 'numpy.ndarray'>" in md assert not "<class 'numpy.ndarray'>" in md_copy assert md.keys() == md_copy.keys() with pytest.raises(TypeError, match='Metadata'): reversed(md) md = Metadata({1: 1, 3: 3}) md_copy = Metadata({2: 2, 3: 30}) md += md_copy assert md.__repr__() == "Metadata({'1': 1, '2': 2, '3': 30})"