Popular recipes tagged "attribute" but not "class"http://code.activestate.com/recipes/tags/attribute-class/2013-07-18T10:02:59-07:00ActiveState Code RecipesExtending 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> 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> 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-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> Dot-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>