Exemple #1
0
 def __new__(cls, unit, qty, price, groupby, metadata):
     return _DataPointBase.__new__(
         cls,
         unit or "undefined",
         # NOTE(peschk_l): avoids floating-point issues.
         decimal.Decimal(str(qty) if isinstance(qty, float) else qty),
         decimal.Decimal(str(price) if isinstance(price, float) else price),
         datastructures.ImmutableDict(groupby),
         datastructures.ImmutableDict(metadata),
     )
Exemple #2
0
 def reload(self):
     """read collection to dictionary"""
     with self._context:
         rows = tuple(self._collection.find({}))
         self._store = {
             row[PRIMARY_KEY]: datastruct.ImmutableDict(row)
             for row in rows
         }
Exemple #3
0
 def as_dict(self, legacy=False, mutable=False):
     output = {
         "period": {"begin": self.start, "end": self.end},
         "usage": {
             key: [v.as_dict(legacy=legacy, mutable=mutable) for v in val]
             for key, val in self._usage.items()
         },
     }
     return output if mutable else datastructures.ImmutableDict(output)
Exemple #4
0
    def as_dict(self, legacy=False, mutable=False):
        """Returns a dict representation of the object.

        The returned dict is immutable by default and has the
        following format::
           {
               "vol": {
                   "unit": "GiB",
                   "qty": 1.2,
               },
               "rating": {
                   "price": 0.04,
               },
               "groupby": {
                   "group_one": "one",
                   "group_two": "two",
               },
               "metadata": {
                   "attr_one": "one",
                   "attr_two": "two",
                },
           }

        The dict can also be returned in the legacy (v1 storage) format. In
        that case, `groupby` and `metadata` will be removed and merged together
        into the `desc` key.

        :param legacy: Defaults to False. If True, returned dict is in legacy
                       format.
        :type legacy: bool
        :param mutable: Defaults to False. If True, returns a normal dict
                        instead of an ImmutableDict.
        :type mutable: bool
        """
        output = {
            "vol": {
                "unit": self.unit,
                "qty": self.qty,
            },
            "rating": {
                "price": self.price,
            },
            "groupby": dict(self.groupby) if mutable else self.groupby,
            "metadata": dict(self.metadata) if mutable else self.metadata,
        }
        if legacy:
            desc = output.pop("metadata")
            desc.update(output.pop("groupby"))
            output['desc'] = desc

        return output if mutable else datastructures.ImmutableDict(output)
Exemple #5
0
 def __setitem__(self, key, value):
     """store key-value to collection (lazy if value isn't changed)"""
     assert PRIMARY_KEY not in value or value[PRIMARY_KEY] == key
     # copy before write
     value = dict(value)
     value[PRIMARY_KEY] = key
     # do nothing on no changes
     if key in self._store.keys() and value == self._store[key]:
         return
     with self._context:
         self._collection.find_one_and_replace({PRIMARY_KEY: key},
                                               value,
                                               upsert=True)
     self._store[key] = datastruct.ImmutableDict(value)
Exemple #6
0
 def desc(self):
     output = dict(self.metadata)
     output.update(self.groupby)
     return datastructures.ImmutableDict(output)