Пример #1
0
  def test_has_value(self):
    class A(object):
      def __init__(self):
        self.n = 10

    self.assertEqual(h.has_value(A(), "n"), (True, 10))
    self.assertEqual(h.has_value(A(), "d"), (False, None))
    self.assertEqual(h.has_value(dict(n=10), "n"), (True, 10))
    self.assertEqual(h.has_value(dict(v=10), "n"), (False, None))
    self.assertEqual(h.has_value(None, "n"), (False, None))
Пример #2
0
  def get_value(self, source):
    """Apply self.convert to the source. The parameter passed to convert depends on
    self.source_name. If source_name is given, self.convert(getattr(source, source_name)) is called,
    otherwise self.convert(source) is called.
    """
    if self.source_name is None:
      present, value = True, self.convert(source)
      converted = True
    else:
      present, value = has_value(source, self.source_name)
      converted = False

    if not present or value is None:
      if self.is_required:
        raise ValueError("required value not present")
      else:
        return None
    else:
      if converted:
        return value
      else:
        return self.convert(value)