This use case where one needs to keep related values together as logical group:
Properties, getattr, setattr are quite interesting option but I prefer collections.namedtuple Example EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csvfor emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): print emp.name, emp.title import sqlite3conn = sqlite3.connect('/companydata')cursor = conn.cursor()cursor.execute('SELECT name, age, title, department, paygrade FROM employees')for emp in map(EmployeeRecord._make, cursor.fetchall()): print emp.name, emp.title My contribution to bag of tricks On Tue, Sep 10, 2013 at 9:59 PM, Gopalakrishnan Subramani < gopalakrishnan.subram...@gmail.com> wrote: > Attributes are stylish, readable, feel native as object compared to > dictionary. Sometimes I use setter methods to override the default > assignments, modify getter to format the values and getter for virtual > attributes for DSL stuffs. > > > class DictObj(dict): > def __getattr__(self, key): > if key.startswith("has_"): > arr = key.split("_") > return arr[1] in self > > return self[key] > > def __setattr__(self, key, value): > self[key] = value > > > od = DictObj() > > od.name = "Nila" > > print ("Has Name? ", od.has_name) > print ("Has Age? ", od.has_age) > > We have this sort of implementation for functional test verification. We > are into Industrial Automation, we deal with measurement devices like flow, > pressure, density, etc. Each device has got 100+ attributes like tag, > address, current value, calibration etc. > > Injecting 'has' and 'is' helps test engineers write unit test based on > device property itself. > > if (device.is_calibration_running): # here calibration_running is a value > read from the device or cached and is is applied for dynamic conditions. > ... > > Although it seems like magic, but certainly not a black magic, we have > control over there. > > Python must have got similar stuff like dynamic function hooks, we could > done still better. But we are missing. > > > > > > > > On Tue, Sep 10, 2013 at 8:48 PM, BibhasD <m...@bibhas.in> wrote: > > > I'm just curious. I faced a similar issue before while answering a > > question about Django on StackOverflow. Django turns context variable > > dictionaries into objects with attributes in template. So in one > > question in SO someone posted this question. Had to answer that Django > > doesn't really support distinguishing such keys. So when I saw the same > > thing discussed, thought of throwing it out here. :) > > > > On Tuesday 10 September 2013 08:42 PM, Dhananjay Nene wrote: > > > Ignoring classes for the moment, how likely do you think you would > > > have a dict like that :) > > > > > > On a separate note if you are using primitive types, I cannot think of > > > any scenarios, where not coercing keys to be of the same type would be > > > considered inappropriate (except in case of reverse dicts) > > > > > > On Tue, Sep 10, 2013 at 3:52 PM, Me@Bibhas <m...@bibhas.in> wrote: > > >> What would happen for a dictionary like this? > > >> > > >>>>> d = {'1': 'foo', 1: 'bar'} > > >>>>> d > > >> {'1': 'foo', 1: 'bar'} > > >> > > >> > > >> On Tuesday 10 September 2013 10:00 AM, Noufal Ibrahim wrote: > > >>> Shabda Raaj <sha...@agiliq.com> writes: > > >>> > > >>>> > > > http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ > > >>>> > > >>>> With api responses after you have parsed the json, you start doing > > things > > >>>> like: > > >>>> > > >>>> api_response["attribute"] > > >>>> > > >>>> I would much prefer to do > > >>>> > > >>>> api_response.attribute > > >>> I generally like to use attributes instead of keys. One additional > > >>> advantage is that I can, if necessary, later convert the attribute > into > > >>> a property that does more than just return a value. > > >>> > > >>> [...] > > >>> > > >>> > > >> _______________________________________________ > > >> BangPypers mailing list > > >> BangPypers@python.org > > >> https://mail.python.org/mailman/listinfo/bangpypers > > > > > > > > > > _______________________________________________ > > BangPypers mailing list > > BangPypers@python.org > > https://mail.python.org/mailman/listinfo/bangpypers > > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > https://mail.python.org/mailman/listinfo/bangpypers > -- Supreet Sethi Ph UK: +447859172473 Ph IN: +919811143517 Ph Skype: d_j_i_n_n Profile: http://www.google.com/profiles/supreet.sethi Twt: http://twitter.com/djinn _______________________________________________ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers