← Back
Editing: ipstruct.cpython-311.pyc
� �@�cP. � �, � d Z dgZ G d� de� � ZdS )z�A dict subclass that supports attribute style access. Authors: * Fernando Perez (original) * Brian Granger (refactoring to a dict subclass) �Structc �n � e Zd ZdZdZd� Zd� Zd� Zd� Zd� Z d� Z d � Zd � Zd� Z d� Zd � Zd� Zdd�Zdd�ZdS )r aY A dict subclass with attribute style access. This dict subclass has a a few extra features: * Attribute style access. * Protection of class members (like keys, items) when using attribute style access. * The ability to restrict assignment to only existing keys. * Intelligent merging. * Overloaded operators. Tc �h � t � | dd� � t j | g|�R i |�� dS )a� Initialize with a dictionary, another Struct, or data. Parameters ---------- *args : dict, Struct Initialize with one dict or Struct **kw : dict Initialize with key, value pairs. Examples -------- >>> s = Struct(a=10,b=30) >>> s.a 10 >>> s.b 30 >>> s2 = Struct(s,c=30) >>> sorted(s2.keys()) ['a', 'b', 'c'] � _allownewTN)�object�__setattr__�dict�__init__)�self�args�kws �8/usr/lib/python3/dist-packages/IPython/utils/ipstruct.pyr zStruct.__init__) sB � �* ���4��d�3�3�3�� �d�(�T�(�(�(�R�(�(�(�(�(� c �x � | j s|| vrt d|z � � �t � | ||� � dS )az Set an item with check for allownew. Examples -------- >>> s = Struct() >>> s['a'] = 10 >>> s.allow_new_attr(False) >>> s['a'] = 10 >>> s['a'] 10 >>> try: ... s['b'] = 20 ... except KeyError: ... print('this is not allowed') ... this is not allowed z8can't create new attribute %s when allow_new_attr(False)N)r �KeyErrorr �__setitem__)r �key�values r r zStruct.__setitem__A sR � �$ �~� R�#�T�/�/��J�S�P�R� R� R�����s�E�*�*�*�*�*r c � � t |t � � r0|| j v st t |� � rt d|z � � � | � ||� � dS # t $ r}t |� � |�d}~ww xY w)a� Set an attr with protection of class members. This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to :exc:`AttributeError`. Examples -------- >>> s = Struct() >>> s.a = 10 >>> s.a 10 >>> try: ... s.get = 10 ... except AttributeError: ... print("you can't set a class member") ... you can't set a class member z.attr %s is a protected member of class Struct.N)� isinstance�str�__dict__�hasattrr �AttributeErrorr r )r r r �es r r zStruct.__setattr__X s� � �( �c�3��� � �d�m�#�#�w�v�s�';�';�#�$�D�s�J�� � � +����S�%�(�(�(�(�(��� +� +� +� ��#�#��*����� +���s �A � A>�)A9�9A>c �\ � | | }|S # t $ r}t |� � |�d}~ww xY w)a� Get an attr by calling :meth:`dict.__getitem__`. Like :meth:`__setattr__`, this method converts :exc:`KeyError` to :exc:`AttributeError`. Examples -------- >>> s = Struct(a=10) >>> s.a 10 >>> type(s.get) <...method'> >>> try: ... s.b ... except AttributeError: ... print("I don't have that key") ... I don't have that key N)r r )r r �resultr s r �__getattr__zStruct.__getattr__z sH � �( ��#�Y�F� �M�� � -� -� -� ��%�%�1�,����� -���s � � +�&�+c �0 � | � |� � | S )z�s += s2 is a shorthand for s.merge(s2). Examples -------- >>> s = Struct(a=10,b=30) >>> s2 = Struct(a=20,c=40) >>> s += s2 >>> sorted(s.keys()) ['a', 'b', 'c'] )�merge)r �others r �__iadd__zStruct.__iadd__� s � � � � �5�����r c �X � | � � � }|� |� � |S )z�s + s2 -> New Struct made from s.merge(s2). Examples -------- >>> s1 = Struct(a=10,b=30) >>> s2 = Struct(a=20,c=40) >>> s = s1 + s2 >>> sorted(s.keys()) ['a', 'b', 'c'] )�copyr �r r �souts r �__add__zStruct.__add__� s( � � �y�y�{�{��� � �5�����r c �8 � | � � � }||z }|S )z�s1 - s2 -> remove keys in s2 from s1. Examples -------- >>> s1 = Struct(a=10,b=30) >>> s2 = Struct(a=40) >>> s = s1 - s2 >>> s {'b': 30} )r# r$ s r �__sub__zStruct.__sub__� s � � �y�y�{�{���� ���r c �B � |� � � D ] }|| v r| |= � | S )z�Inplace remove keys from self that are in other. Examples -------- >>> s1 = Struct(a=10,b=30) >>> s2 = Struct(a=40) >>> s1 -= s2 >>> s1 {'b': 30} )�keys)r r �ks r �__isub__zStruct.__isub__� s1 � � ����� � �A��D�y�y���G���r c � � i }|� � � D ]8\ }}t |t � � r|� � � }|D ]}|||<