예제 #1
0
  def testMutableDict(self):
    frozen = frozendict(a=1, b=frozendict(x=1, y=2))
    mutable = frozen.mutableDict()

    # Copy should equal the original contents
    self.assertEqual(
        mutable,
        frozen
    )

    # All 'frozendict' must be mutable
    try:
      mutable['c'] = 10
    except TypeError:
      self.fail("Failed to assign to mutable copy")
    self.assertEqual(
        mutable,
        {'a': 1, 'b': {'x': 1, 'y': 2}, 'c': 10}
    )

    try:
      mutable['b']['z'] = 20
    except TypeError:
      self.fail("Failed to assign to mutable subcopy")
    self.assertEqual(
        mutable,
        {'a': 1, 'b': {'x': 1, 'y': 2, 'z': 20}, 'c': 10}
    )
예제 #2
0
    def testDictKey(self):
        d = {
            frozendict(a=1, b=2): 1,
            'b': 2,
        }

        self.assertEqual(d[frozendict(a=1, b=2)], 1)
예제 #3
0
    def testMutableDict(self):
        frozen = frozendict(a=1, b=frozendict(x=1, y=2))
        mutable = frozen.mutableDict()

        # Copy should equal the original contents
        self.assertEqual(mutable, frozen)

        # All 'frozendict' must be mutable
        try:
            mutable['c'] = 10
        except TypeError:
            self.fail("Failed to assign to mutable copy")
        self.assertEqual(mutable, {'a': 1, 'b': {'x': 1, 'y': 2}, 'c': 10})

        try:
            mutable['b']['z'] = 20
        except TypeError:
            self.fail("Failed to assign to mutable subcopy")
        self.assertEqual(mutable, {
            'a': 1,
            'b': {
                'x': 1,
                'y': 2,
                'z': 20
            },
            'c': 10
        })
예제 #4
0
  def testDictKey(self):
    d = {
        frozendict(a=1, b=2): 1,
        'b': 2,
    }

    self.assertEqual(
        d[frozendict(a=1, b=2)],
        1
    )
예제 #5
0
  def testEquality(self):
    self.assertEqual(
        frozendict(a=1, b=2),
        frozendict(a=1, b=2),
    )

    self.assertNotEqual(
        frozendict(a=1),
        frozendict(a=1, b=2),
    )
예제 #6
0
  def testDictEquality(self):
    self.assertEqual(
        frozendict(a=1, b=2),
        {'a': 1, 'b': 2,},
    )

    self.assertNotEqual(
        frozendict(a=1),
        {'a': 1, 'b': 2,},
    )
예제 #7
0
  def testItemTuple(self):
    self.assertEqual(
        frozendict().itemtuple(),
        ()
    )

    self.assertEqual(
        frozendict(a=1, b=2).itemtuple(),
        (('a', 1), ('b', 2))
    )
  def testDictEquality(self):
    self.assertEqual(
        frozendict(a=1, b=2),
        {'a': 1, 'b': 2,},
    )

    self.assertNotEqual(
        frozendict(a=1),
        {'a': 1, 'b': 2,},
    )
예제 #9
0
    def testDeepCopyRecursive(self):
        frozen = frozendict(a=1, b=frozendict(x=1, y=2))
        cpy = copy.deepcopy(frozen)

        # All 'frozendict' should be 'dict'
        self.assertIs(type(cpy), dict)
        self.assertIs(type(cpy['b']), dict)

        # Copy should equal the original contents
        self.assertEqual(cpy, frozen)
  def testItemTuple(self):
    self.assertEqual(
        frozendict().itemtuple(),
        ()
    )

    self.assertEqual(
        frozendict(a=1, b=2).itemtuple(),
        (('a', 1), ('b', 2))
    )
예제 #11
0
    def testEquality(self):
        self.assertEqual(
            frozendict(a=1, b=2),
            frozendict(a=1, b=2),
        )

        self.assertNotEqual(
            frozendict(a=1),
            frozendict(a=1, b=2),
        )
예제 #12
0
 def testExtendReplace(self):
   frozen = frozendict(a=1, b=2)
   ext = frozen.extend(b=10, c=3)
   self.assertEqual(
       ext,
       {'a': 1, 'b': 10, 'c': 3}
   )
 def testExtendReplace(self):
   frozen = frozendict(a=1, b=2)
   ext = frozen.extend(b=10, c=3)
   self.assertEqual(
       ext,
       {'a': 1, 'b': 10, 'c': 3}
   )
예제 #14
0
    def testDeepCopy(self):
        frozen = frozendict(a=1, b=2)
        cpy = copy.deepcopy(frozen)

        # Copy is regular 'dict'
        self.assertIs(type(cpy), dict)

        # Copy should equal the original contents
        self.assertEqual(cpy, frozen)
예제 #15
0
  def testDeepCopyRecursive(self):
    frozen = frozendict(a=1, b=frozendict(x=1, y=2))
    cpy = copy.deepcopy(frozen)

    # All 'frozendict' should be 'dict'
    self.assertIs(
        type(cpy),
        dict
    )
    self.assertIs(
        type(cpy['b']),
        dict
    )

    # Copy should equal the original contents
    self.assertEqual(
        cpy,
        frozen
    )
예제 #16
0
 def testExtend(self):
   frozen = frozendict(a=1, b=2)
   ext = frozen.extend(c=3)
   self.assertEqual(
       type(ext),
       frozendict
   )
   self.assertEqual(
       ext,
       {'a': 1, 'b': 2, 'c': 3}
   )
 def testExtend(self):
   frozen = frozendict(a=1, b=2)
   ext = frozen.extend(c=3)
   self.assertEqual(
       type(ext),
       frozendict
   )
   self.assertEqual(
       ext,
       {'a': 1, 'b': 2, 'c': 3}
   )
예제 #18
0
 def _wrap(cls, value):
   """Wraps a JSON object value in a suitable immutable structure. Since JSON
   only deserializes into specific field types, this is easily scoped."""
   if isinstance(value, (dict, frozendict)):
     d = {}
     for k, v in value.iteritems():
       if not isinstance(k, basestring):
         raise TypeError("JSON key is not a string")
       d[k] = cls._wrap(v)
     return frozendict(**d)
   elif isinstance(value, (list, tuple)):
     return tuple([cls._wrap(x) for x in value])
   else:
     return value
예제 #19
0
 def _wrap(cls, value):
     """Wraps a JSON object value in a suitable immutable structure. Since JSON
 only deserializes into specific field types, this is easily scoped."""
     if isinstance(value, (dict, frozendict)):
         d = {}
         for k, v in value.iteritems():
             if not isinstance(k, basestring):
                 raise TypeError("JSON key is not a string")
             d[k] = cls._wrap(v)
         return frozendict(**d)
     elif isinstance(value, (list, tuple)):
         return tuple([cls._wrap(x) for x in value])
     else:
         return value
예제 #20
0
  def testDeepCopy(self):
    frozen = frozendict(a=1, b=2)
    cpy = copy.deepcopy(frozen)

    # Copy is regular 'dict'
    self.assertIs(
        type(cpy),
        dict
    )

    # Copy should equal the original contents
    self.assertEqual(
        cpy,
        frozen
    )
예제 #21
0
    def testImmutable(self):
        frozen = frozendict(a=1, b=2)

        def assign():
            frozen['b'] = 0

        self.assertRaises(TypeError, assign)

        def delete():
            del (frozen['b'])

        self.assertRaises(TypeError, delete)

        def assignNew():
            frozen['c'] = 3

        self.assertRaises(TypeError, assignNew)
예제 #22
0
  def testImmutable(self):
    frozen = frozendict(a=1, b=2)

    def assign():
      frozen['b'] = 0
    self.assertRaises(
        TypeError,
        assign
    )

    def delete():
      del(frozen['b'])
    self.assertRaises(
        TypeError,
        delete
    )

    def assignNew():
      frozen['c'] = 3
    self.assertRaises(
        TypeError,
        assignNew
    )
예제 #23
0
 def _revisions(self):
   """Constructs our forward-revision mapping on-demand."""
   return frozendict(
       (k, RevisionInfo(k, **v))
           for k, v in self.get('revisions', {}).iteritems())
예제 #24
0
 def labels(self):
   return frozendict((label_name, LabelInfo(**label_dict))
                     for label_name, label_dict in
                         self.get('labels', {}).iteritems())
예제 #25
0
 def _revisions(self):
     """Constructs our forward-revision mapping on-demand."""
     return frozendict((k, RevisionInfo(k, **v))
                       for k, v in self.get('revisions', {}).iteritems())
예제 #26
0
 def _revision_number_map(self):
     """Constructs our reverse-revision mapping on-demand."""
     return frozendict(
         (v.get('_number'), v) for v in self._revisions.itervalues())
예제 #27
0
 def testKwargInstantiation(self):
   self.assertEqual(
       frozendict(a=1, b=2),
       {'a': 1, 'b': 2,}
   )
예제 #28
0
 def testTupleInstantiation(self):
   self.assertEqual(
       frozendict((('a', 1), ('b', 2))),
       {'a': 1, 'b': 2,}
   )
예제 #29
0
 def labels(self):
     return frozendict(
         (label_name, LabelInfo(**label_dict))
         for label_name, label_dict in self.get('labels', {}).iteritems())
예제 #30
0
 def testTupleInstantiation(self):
     self.assertEqual(frozendict((('a', 1), ('b', 2))), {
         'a': 1,
         'b': 2,
     })
예제 #31
0
 def testKwargInstantiation(self):
     self.assertEqual(frozendict(a=1, b=2), {
         'a': 1,
         'b': 2,
     })
예제 #32
0
 def _revision_number_map(self):
   """Constructs our reverse-revision mapping on-demand."""
   return frozendict(
       (v.get('_number'), v)
           for v in self._revisions.itervalues())