コード例 #1
0
ファイル: dataframe.py プロジェクト: openstack/cloudkitty
 def from_dict(cls, dict_, legacy=False):
     try:
         schema = DATAFRAME_SCHEMA
         if legacy:
             validator = functools.partial(DataPoint.from_dict, legacy=True)
             # NOTE(peschk_l): __name__ is required for voluptuous exception
             # message formatting
             validator.__name__ = 'DataPoint.from_dict'
             # NOTE(peschk_l): In case the legacy format is required, we
             # create a new schema where DataPoint.from_dict is called with
             # legacy=True. The "extend" method does create a new objects,
             # and replaces existing keys with new ones.
             schema = DATAFRAME_SCHEMA.extend({
                 voluptuous.Required('usage'):
                 vutils.IterableValuesDict(str, validator),
             })
         valid = schema(dict_)
         return cls(valid["period"]["begin"],
                    valid["period"]["end"],
                    usage=valid["usage"])
     except (voluptuous.error.Invalid, KeyError) as e:
         raise ValueError("{} isn't a valid DataFrame: {}".format(dict_, e))
コード例 #2
0
ファイル: dataframe.py プロジェクト: openstack/cloudkitty
    @property
    def desc(self):
        output = dict(self.metadata)
        output.update(self.groupby)
        return datastructures.ImmutableDict(output)


DATAFRAME_SCHEMA = voluptuous.Schema({
    voluptuous.Required('period'): {
        voluptuous.Required('begin'):
        voluptuous.Any(datetime.datetime, tzutils.dt_from_iso),
        voluptuous.Required('end'):
        voluptuous.Any(datetime.datetime, tzutils.dt_from_iso),
    },
    voluptuous.Required('usage'):
    vutils.IterableValuesDict(str, DataPoint.from_dict),
})


class DataFrame(object):

    __slots__ = ("start", "end", "_usage")

    def __init__(self, start, end, usage=None):
        if not isinstance(start, datetime.datetime):
            raise TypeError(
                '"start" must be of type datetime.datetime, not {}'.format(
                    type(start)))
        if not isinstance(end, datetime.datetime):
            raise TypeError(
                '"end" must be of type datetime.datetime, not {}'.format(
コード例 #3
0
 def test_iterablevaluesdict_invalid_type_str(self):
     validator = validation_utils.IterableValuesDict(str, int)
     self.assertRaises(
         voluptuous.error.Invalid, validator, 'aaaa')
コード例 #4
0
 def test_iterablevaluesdict_invalid_iterable_with_cast(self):
     validator = validation_utils.IterableValuesDict(str, int)
     self.assertRaises(
         voluptuous.error.Invalid, validator, {'a': ['1'], 'b': 42, })
コード例 #5
0
 def test_iterablevaluesdict_invalid_dict_iterable_without_cast(self):
     validator = validation_utils.IterableValuesDict(str, str, cast=False)
     self.assertRaises(
         voluptuous.error.Invalid, validator, {'a': ['1'], 'b': (2, )})
コード例 #6
0
 def test_iterablevaluesdict_valid_list_and_tuple_without_cast(self):
     validator = validation_utils.IterableValuesDict(str, str)
     self.assertEqual(
         validator({'a': ['1', '2'], 'b': ('3', '4')}),
         {'a': ['1', '2'], 'b': ('3', '4')},
     )