Most viewed recipes tagged "attribute"http://code.activestate.com/recipes/tags/attribute/views/2013-07-18T10:02:59-07:00ActiveState Code RecipesDot-style nested lookups over dictionary based data structures (Python) 2008-12-14T14:32:59-08:00David Mosshttp://code.activestate.com/recipes/users/4124829/http://code.activestate.com/recipes/576586-dot-style-nested-lookups-over-dictionary-based-dat/ <p style="color: grey"> Python recipe 576586 by <a href="/recipes/users/4124829/">David Moss</a> (<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>). Revision 2. </p> <p>This recipe allows you to present dictionary based nested data stuctures in your Python code as objects with nested attributes.</p> <p>It provides dot ('.') based attribute lookups, like this :-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; x = d.foo.bar.baz </code></pre> <p>instead of the usual (and longer) Python dictionary syntax lookups :-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; x = d['foo']['bar']['baz'] </code></pre> <p>This recipe saves you <em>lot</em> of typing!</p> <p>For simplicity and readability this particular version show a read only lookup class.</p> Arbitrary order attribute writing with ElementTree (Python) 2008-08-01T19:24:34-07:00Orri Ganelhttp://code.activestate.com/recipes/users/2259404/http://code.activestate.com/recipes/576403-arbitrary-order-attribute-writing-with-elementtree/ <p style="color: grey"> Python recipe 576403 by <a href="/recipes/users/2259404/">Orri Ganel</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elementtree/">elementtree</a>, <a href="/recipes/tags/element_tree/">element_tree</a>, <a href="/recipes/tags/etree/">etree</a>, <a href="/recipes/tags/order/">order</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/write/">write</a>, <a href="/recipes/tags/writing/">writing</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 5. </p> <p>Modified version of ElementTree with two additional parameters to the write() method: "sortflag" and "sortcmp". "sortflag" defaults to "default", which results in unmodified behavior. "sortcmp" defaults to None, which results in unmodified behavior. See discussion for usage and justification. Changes made begin on line 655.</p> <p>EDIT: in most cases, unless sortflag happened to be intended for the root, it would be ignored; added sortflag and sortcmp to self._write() call on line 724. Expect another revision in the near future to allow for specifying different orders for different xml tags.</p> <p>EDIT, the second: Added tag-specific ordering.</p> Alias class attributes (Python) 2009-06-01T01:19:55-07:00Euan Goddardhttp://code.activestate.com/recipes/users/4170559/http://code.activestate.com/recipes/576787-alias-class-attributes/ <p style="color: grey"> Python recipe 576787 by <a href="/recipes/users/4170559/">Euan Goddard</a> (<a href="/recipes/tags/alias/">alias</a>, <a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). </p> <p>The alias metaclass allows a declarative way of creating aliases to attributes on a class. The main purpose of which is to adapt classes for duck-typing (see doc-string for me detail).</p> Attribute - easy way to define interface (Python) 2011-02-20T16:31:08-08:00Hui Zhanghttp://code.activestate.com/recipes/users/4177055/http://code.activestate.com/recipes/577579-attribute-easy-way-to-define-interface/ <p style="color: grey"> Python recipe 577579 by <a href="/recipes/users/4177055/">Hui Zhang</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>it is an enhanced property</p> <pre class="prettyprint"><code> * initialize attribute in class definition scope * easily keep the attribute behaviors in sub classes. * easy to define interface. * not afraid to forget the super(klass, self).__init__(...) :P </code></pre> Dictionary Who's Keys Act Like Attributes As Well (Python) 2011-05-26T20:15:16-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/ <p style="color: grey"> Python recipe 577590 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/keys/">keys</a>). </p> <p>Think of this as a JavaScript object. In JavaScript, the objects can be referenced by indexing (e.g. d[name]) or by directly using the dot (.) operator (e.g. d.name).</p> <p>This is the same concept. </p> <p><strong>Note to Python 2.4 Users:</strong> You will need to change the "except KeyError as e:" line to "except KeyError, (e):".</p> Attribute - easy way to define interface (Python) 2011-03-06T12:49:30-08:00ilon asolothttp://code.activestate.com/recipes/users/4177080/http://code.activestate.com/recipes/577598-attribute-easy-way-to-define-interface/ <p style="color: grey"> Python recipe 577598 by <a href="/recipes/users/4177080/">ilon asolot</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>it is an enhanced property</p> <pre class="prettyprint"><code> * initialize attribute in class definition scope * easily keep the attribute behaviors in sub classes. * easy to define interface. * not afraid to forget the super(klass, self).__init__(...) :P </code></pre> Extending non-extendable C++ based Python classes (Python) 2013-07-18T10:02:59-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/578576-extending-non-extendable-c-based-python-classes/ <p style="color: grey"> Python recipe 578576 by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/proxy/">proxy</a>, <a href="/recipes/tags/subclass/">subclass</a>). Revision 2. </p> <p>graph_tool library is based on boost C++ library and provides Vertex class binding for Python. If we wanted to extend this Vertex class and add some attributes and methods, it wouldn't let us do that due to private constructor in C++ code (RuntimeError: This class cannot be instantiated from Python). We can overcome this obstacle using Proxy pattern.</p> <p>In the __getattr__ method, if the attribute(or function name) is not in the Subclass MyVertex, then it looks for attributes of Vertex object that is defined inside MyVertex.</p> Find what class an attribute - ie, myObj.myAttr - comes from, and how it's defined (Python) 2012-10-26T12:59:47-07:00Paul Molodowitchhttp://code.activestate.com/recipes/users/4184064/http://code.activestate.com/recipes/578305-find-what-class-an-attribute-ie-myobjmyattr-comes-/ <p style="color: grey"> Python recipe 578305 by <a href="/recipes/users/4184064/">Paul Molodowitch</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/__dict__/">__dict__</a>, <a href="/recipes/tags/__getattribute__/">__getattribute__</a>, <a href="/recipes/tags/__getattr__/">__getattr__</a>, <a href="/recipes/tags/__slots__/">__slots__</a>). Revision 3. </p> <p>When inspecting new code (or when debugging), it can be handy to know exactly where a given attribute on an object or class comes from.</p> <p>As a simple example, if you have a class MyClass, you might want to know where MyClass().myMethod is defined.</p> <p>However, things can get tricky when things like __getattr__, __getattribute__, or even compiled objects come into play. That's where this function comes in. It returns what class a given attribute comes from, and what method was used to define it - ie, '__dict__' ('normal' definitions), '__slots__', '__getattr__', '__getattribute__', '(BUILTIN)'.</p> <p>(Note - this function should't be relied on to be 100% accurate - rather, it's a best guess, for where to look to find it. It takes some pretty infrequent edge cases for it to be wrong, though...)</p>