Top-rated recipes tagged "attributes"http://code.activestate.com/recipes/tags/attributes/top/2017-01-14T22:35:17-08:00ActiveState Code RecipesPython Template Engine (Python) 2012-03-31T21:28:12-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/578090-python-template-engine/ <p style="color: grey"> Python recipe 578090 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/template/">template</a>). </p> <p>This is a simple template engine which allows you to replace macros within text. This engine allows for attributes and filters. The default implementation provides the entire string module as filters. Trying to use arguments will of course not work (since the framework supports no other arguments for the filter other than the filtered string itself).</p> getattr with arbitrary depth. (Python) 2010-08-05T07:02:29-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577346-getattr-with-arbitrary-depth/ <p style="color: grey"> Python recipe 577346 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/getattr/">getattr</a>). Revision 2. </p> <p><code>getattr</code> allows one to obtain an attribute from an object given it's name as a string. <code>multi_getattr</code> does the same thing but with dotted strings that can represent nested attributes.</p> AttrDict (Python) 2009-11-28T05:39:08-08:00Chris Joneshttp://code.activestate.com/recipes/users/4171447/http://code.activestate.com/recipes/576972-attrdict/ <p style="color: grey"> Python recipe 576972 by <a href="/recipes/users/4171447/">Chris Jones</a> (<a href="/recipes/tags/attr/">attr</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>). </p> <p>Dictionary object that can also be accessed via attributes</p> Two quick functions for object introspection (Python) 2017-01-14T22:35:17-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580747-two-quick-functions-for-object-introspection/ <p style="color: grey"> Python recipe 580747 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows two quick-and-clean :) utility functions for introspection of Python objects. They are meant to be used while working interactively in the reular Python shell or in the IPython shell. Both of them display attributes of any given object passed as the argument. The first function displays all attributes. The second function only displays atttributes that do not begin and end with a double underscore, so as to filter out "dunder" methods a.k.a. "special" methods - like __len__, __str__, __repr__, etc. The first function - oa(o) , where o is some object - does the same as dir(o), but is useful - in IPython - because, dir(o) output will scroll off the screen if the output is long, since it prints the attributes vertically, one per line, while oa(o) prints them horizontally, so has less chance of the output scrolling off, and the output also occupies fewer lines on the screen, so is easier to scan quickly. The second function - oar(o) - is like oa(o), but filters out attribute names that begin and end with a dunder. So it is useful in both IPython and Python.</p> <p>More information and outputs here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html</a></p> Python add/set attributes to list (Python) 2015-09-29T16:28:46-07:00webby1111http://code.activestate.com/recipes/users/4192908/http://code.activestate.com/recipes/579103-python-addset-attributes-to-list/ <p style="color: grey"> Python recipe 579103 by <a href="/recipes/users/4192908/">webby1111</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/subclass/">subclass</a>). Revision 3. </p> <h4 id="python-attribute-listhttpsgithubcomwebby1111python-attribute-list"><a href="https://github.com/webby1111/Python-Attribute-List">Python Attribute List</a></h4> <p>Add/set attributes to python lists.</p> <p>A google search for "add attributes to python lists" yields no good stackoverflow answer, hence the need for this.</p> <p>Useful for machine learning stuff where you need labeled feature vectors. </p> <p>This technique can be easily adapted for other built-ins (e.g. int).</p> <h5 id="the-problem">The Problem</h5> <pre class="prettyprint"><code>a = [1, 2, 4, 8] a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x' </code></pre> <h5 id="the-solution">The Solution</h5> <pre class="prettyprint"><code>a = L(1, 2, 4, 8) a.x = "Hey!" print a # [1, 2, 4, 8] print a.x # "Hey!" print len(a) # 4 # You can also do these: a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8] a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8] a = L( {1, 2, 4, 8} , x="Hey!" ) # [1, 2, 4, 8] a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8] a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ) # [2] </code></pre> Easy attribute setting and pretty representation (Python) 2014-12-03T09:55:06-08:00Joakim Petterssonhttp://code.activestate.com/recipes/users/4174760/http://code.activestate.com/recipes/578973-easy-attribute-setting-and-pretty-representation/ <p style="color: grey"> Python recipe 578973 by <a href="/recipes/users/4174760/">Joakim Pettersson</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/traits/">traits</a>). Revision 2. </p> <p>Mix in one or more of these classes to avoid those tedious lines of administrative code for setting attributes and getting useful representations. </p> <p>If you inherit HasInitableAttributes, your should be able to obj = eval(repr(obj)) without loosing data.</p> <p>enthought.traits.api.HasTraits seems to mix in well also.</p> Reading XML into dict-like object (Python) 2013-03-14T18:50:07-07:00Lucas Oliveirahttp://code.activestate.com/recipes/users/4185629/http://code.activestate.com/recipes/578492-reading-xml-into-dict-like-object/ <p style="color: grey"> Python recipe 578492 by <a href="/recipes/users/4185629/">Lucas Oliveira</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <ul> <li>Load XML, tree or root</li> <li>Make its children available through __getitem__</li> <li>Make its attributes available through __getattr__</li> <li>If child is requested, return an instance created with child as new root</li> <li>Make its text accessible through __getattr__, using attribute "text"</li> </ul> Traverse dotted attribute of an object using built-in function reduce (Python) 2013-01-05T05:49:27-08:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578398-traverse-dotted-attribute-of-an-object-using-built/ <p style="color: grey"> Python recipe 578398 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bif/">bif</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/reduce/">reduce</a>). </p> <p>Making good use of reduce() to traverse dotted attribute of an object.</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> Bunch class created from attributes in class (Python) 2011-12-31T18:03:02-08:00Fabio Zadroznyhttp://code.activestate.com/recipes/users/4180406/http://code.activestate.com/recipes/577999-bunch-class-created-from-attributes-in-class/ <p style="color: grey"> Python recipe 577999 by <a href="/recipes/users/4180406/">Fabio Zadrozny</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bunch/">bunch</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). Revision 3. </p> <p>Provide a way to construct class __init__, __slots__, __eq__, __ne__, __repr__ for the class and makes explicit which attributes each instance will have (and providing defaults).</p> <p>The __main__ session shows an example of how it should be used.</p> See How __getattribute__ Interacts With Special Methods (Python) 2011-08-12T21:30:02-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577812-see-how-__getattribute__-interacts-with-special-me/ <p style="color: grey"> Python recipe 577812 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/special_methods/">special_methods</a>). </p> <p>This recipe is how you can show that the Python operators and builtin functions directly use the special methods from an object's class, rather than using normal attribute lookup on the object.</p> Simple Abstract "Constants" to Use When @abstractproperty is Overkill or Misleading (Python) 2011-08-12T23:35:45-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577761-simple-abstract-constants-to-use-when-abstractprop/ <p style="color: grey"> Python recipe 577761 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/classes/">classes</a>). </p> <p>Use these instead of abstract properties when you don't plan on the abstract attribute being implemented with a property. And you can still give your attribute a docstring!</p>