{"id":328,"date":"2008-07-10T17:51:33","date_gmt":"2008-07-11T00:51:33","guid":{"rendered":"http:\/\/www.stevenbrown.ca\/blog\/?p=328"},"modified":"2008-07-10T17:58:45","modified_gmt":"2008-07-11T00:58:45","slug":"attrdict-python-module","status":"publish","type":"post","link":"https:\/\/www.stevenbrown.ca\/blog\/archives\/328","title":{"rendered":"AttrDict Python Module"},"content":{"rendered":"<p>I&#8217;ve been doing quite a bit of Python hacking in my recent free time.  A couple days ago, I had the desire for a dictionary that I could access key values just like object attribute names.  For example, in the dictionary <code>d={'key':'value'}<\/code>, I wanted to be able to use <code>d.key<\/code> to return <code>'value'<\/code>.  I thought this would be a common desire and probably already exists (and might even be built-in somehow), so I went on to <code>#python<\/code> and asked the folk there.  Nobody knew of anything.  So I set off to write my own.  I called it <strong>AttrDict<\/strong>.<\/p>\n<p>Yesterday, I found something similar already exists.  It&#8217;s called <a href=\"http:\/\/pypi.python.org\/pypi\/ObDict\/0.2.0\"><em>ObDict<\/em><\/a>.  <em>Similar?<\/em>  <em>Very<\/em>!  \ud83d\ude1b  But different enough that I continued mine&#8230; and have a somewhat complete python module of my own.  So far it&#8217;s been a nice opportunity to learn more about Python objects and use the <code>unittest<\/code> module for unit testing.<\/p>\n<p>It&#8217;s not 100% coverage of the dict interface, but it&#8217;s pretty close and I think it&#8217;s usable enough that I can slap a quick release together and work on other stuff.  Bugs\/patches\/testing\/suggestions welcome.<\/p>\n<ul>\n<li>Grab the source: <a href=\"http:\/\/stevenbrown.ca\/src\/AttrDict-0.1.zip\">AttrDict-0.1.zip<\/a><\/li>\n<li>Browse the source: <a href=\"http:\/\/stevenbrown.ca\/src\/AttrDict\/\">http:\/\/stevenbrown.ca\/src\/AttrDict\/<\/a><\/li>\n<li>Branch the source: <code>bzr branch http:\/\/stevenbrown.ca\/src\/AttrDict<\/code><\/li>\n<\/ul>\n<p><strong>Update<\/strong>:<\/p>\n<p>Wooops, forgot to write a bit more about it.  Here&#8217;s the docstring from the module:<\/p>\n<div class=\"codeblock\">\nSimple extension of the built-in dictionary so that dictionary keys are mirrored<br \/>\nas object attributes.  This is for convenience.  You can do things like this:<\/p>\n<p>  d = dict()     #standard dict, for comparison<br \/>\n  a = AttrDict() #<\/p>\n<p>  d[&#8216;ok&#8217;] = &#8216;this is ok&#8217;<br \/>\n  d.ok -> AttributeError raised<br \/>\n  d.ok = &#8216;test&#8217; -> AttributeError raised<\/p>\n<p>You cannot assign attributes to standard Python dicts.<\/p>\n<p>  a[&#8216;ok&#8217;] = &#8216;this is ok&#8217;<br \/>\n  a.ok -> &#8216;this is ok&#8217;    #attribute is automatically created<br \/>\n  a.ok = &#8216;changed&#8217;<br \/>\n  a[&#8216;ok&#8217;] -> &#8216;changed&#8217;    #and the dict value is automatically updated<br \/>\n  a.ok2 = &#8216;new value&#8217;     #adding new attribute, ok2<br \/>\n  a[&#8216;ok2&#8217;] -> &#8216;new value&#8217; #dict key is automatically created<\/p>\n<p>This introduces a limitation on the dictionary keys such<br \/>\nthat they must be strings and provide valid Python syntax for accessing.<\/p>\n<p>For example:<\/p>\n<p>  {&#8216;123&#8242;:&#8217;valid&#8217;} #is a valid dictionary<\/p>\n<p>but<\/p>\n<p>  mydict.123 #is not valid Python syntax.<\/p>\n<p>Attempting to create a key that cannot be accessed through an attribute name<br \/>\nwill raise an AttributeError Exception.<\/p>\n<p>This module has not been built for optmization.  Some of the method<br \/>\ndocumentation has been taken from Python&#8217;s dict documentation.  For more info<br \/>\non a particular method, refer to that.  I&#8217;ve tried to mirror the effects of<br \/>\nthe standard dict as closely as possible.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing quite a bit of Python hacking in my recent free time. A couple days ago, I had the desire for a dictionary that I could access key values just like object attribute names. For example, in the dictionary d={&#8216;key&#8217;:&#8217;value&#8217;}, I wanted to be able to use d.key to return &#8216;value&#8217;. I thought&hellip; <a class=\"more-link\" href=\"https:\/\/www.stevenbrown.ca\/blog\/archives\/328\">Continue reading <span class=\"screen-reader-text\">AttrDict Python Module<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,11],"tags":[169,48,66,247,46,168],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-geek","category-projects","tag-attrdict","tag-code","tag-programming","tag-projects","tag-python","tag-python-module","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4jEMb-5i","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/comments?post=328"}],"version-history":[{"count":0,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/328\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/media?parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/categories?post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/tags?post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}