コード例 #1
0
 def __getitem__(self, key):
     """
     Generated by @autodict.
     Implements the __getitem__ method from collections.Mapping by relying on a filtered getattr(self, key)
     """
     if hasattr(self, key):
         key = possibly_replace_with_property_name(self.__class__, key)
         if is_attr_selected(key, include=include, exclude=exclude) and \
                 (not public_fields_only or not key.startswith(private_name_prefix)):
             return getattr(self, key)
         else:
             try:
                 # noinspection PyUnresolvedReferences
                 return super(cls, self).__getitem__(key)
             except Exception as e:
                 raise KeyError(
                     '@autodict generated dict view - {key} is a '
                     'hidden field and super[{key}] raises an exception: {etyp} {err}'
                     ''.format(key=key, etyp=type(e).__name__, err=e))
     else:
         try:
             # noinspection PyUnresolvedReferences
             return super(cls, self).__getitem__(key)
         except Exception as e:
             raise KeyError(
                 '@autodict generated dict view - {key} is an '
                 'invalid field name (was the constructor called?). Delegating to '
                 'super[{key}] raises an exception: {etyp} {err}'
                 ''.format(key=key, etyp=type(e).__name__, err=e))
コード例 #2
0
 def __iter__(self):
     """
     Generated by @autodict. Relying on a filtered vars(self) for the keys iterable
     """
     for att_name in iterate_on_vars(self):
         # filter based on the name (include/exclude + private/public)
         if is_attr_selected(att_name, include=include, exclude=exclude) and \
                 (not public_fields_only or not att_name.startswith(private_name_prefix)):
             # use that name
             yield att_name
コード例 #3
0
    def _vars_iterator(self):
        """
        Filters the vars(self) according to include/exclude/public_fields_only

        :param self:
        :return:
        """
        for att_name in iterate_on_vars(self):
            # filter based on the name (include/exclude + private/public)
            if is_attr_selected(att_name, include=include, exclude=exclude) and \
                    (not public_fields_only or not att_name.startswith(private_name_prefix)):
                # use it
                yield att_name
コード例 #4
0
 def __iter__(self):
     """
     Generated by @autodict.
     Implements the __iter__ method from collections.Iterable by relying on a filtered vars(self)
     :param self:
     :return:
     """
     myattrs = tuple(att_name for att_name in iterate_on_vars(self))
     for att_name in chain(myattrs, (o for o in super(cls, self).__iter__() if o not in myattrs)):
         # filter based on the name (include/exclude + private/public)
         if is_attr_selected(att_name, include=include, exclude=exclude) and \
                 (not public_fields_only or not att_name.startswith(private_name_prefix)):
             # use that name
             yield att_name
コード例 #5
0
 def __getitem__(self, key):
     """
     Generated by @autodict.
     Implements the __getitem__ method from collections.Mapping by relying on a filtered getattr(self, key)
     :param self:
     :param key:
     :return:
     """
     if hasattr(self, key):
         key = possibly_replace_with_property_name(self.__class__, key)
         if is_attr_selected(key, include=include, exclude=exclude) and \
                 (not public_fields_only or not key.startswith(private_name_prefix)):
             return getattr(self, key)
         else:
             raise KeyError('@autodict generated dict view - hidden field name: ' + key)
     else:
         raise KeyError('@autodict generated dict view - {key} is an invalid field name (was the '
                        'constructor called? are the constructor arg names identical to the field '
                        'names ?)'.format(key=key))