hello,
i would like if this behaviour can be obtained from python : trap an
attributeError from inside a subclassing dict class... (here is a silly
examples to explain my question)
class Test(dict):
"""subclass ing a dict and each key will permit access to a tuple of fized
size"""
def __init__(self):
pass
def load(self, adict):
"""actually it will be done from a text file with fixed size fields, each
tuple will have the same size"""
for k in adict:
self[k] = tuple(adict[k])
t = Test()
t.load({1: ('a', 'aa'), 2: ('(b', 'bb')})
here is what i would have :[color=blue][color=green][color=darkred]
>>> t[1].get('first')[/color][/color][/color]
'a'
without having to define tuple as an object or subclassing tuple, of course
now i had :[color=blue][color=green][color=darkred]
>>> t[1].get('first')[/color][/color][/color]
Traceback (most recent call last):
File "<pyshell#1 5>", line 1, in -toplevel-
t['A'].get('first')
AttributeError: 'tuple' object has no attribute 'get'
is it possible ?
btw my question is something like is there a way from inside Test code to
say to python that when it first get t[1] it should not go further, trap the
things one ask to do on t[key] and then calling a function such as
(inside Test)
def __get(self, key, readable):
mapping = {'first' : 1, 'second' : 2}
return self[key][mapping[readable]]
(maybe it is a silly design...but i need this because i do want some kind of
readable object access without having to create all these objects)
i would like if this behaviour can be obtained from python : trap an
attributeError from inside a subclassing dict class... (here is a silly
examples to explain my question)
class Test(dict):
"""subclass ing a dict and each key will permit access to a tuple of fized
size"""
def __init__(self):
pass
def load(self, adict):
"""actually it will be done from a text file with fixed size fields, each
tuple will have the same size"""
for k in adict:
self[k] = tuple(adict[k])
t = Test()
t.load({1: ('a', 'aa'), 2: ('(b', 'bb')})
here is what i would have :[color=blue][color=green][color=darkred]
>>> t[1].get('first')[/color][/color][/color]
'a'
without having to define tuple as an object or subclassing tuple, of course
now i had :[color=blue][color=green][color=darkred]
>>> t[1].get('first')[/color][/color][/color]
Traceback (most recent call last):
File "<pyshell#1 5>", line 1, in -toplevel-
t['A'].get('first')
AttributeError: 'tuple' object has no attribute 'get'
is it possible ?
btw my question is something like is there a way from inside Test code to
say to python that when it first get t[1] it should not go further, trap the
things one ask to do on t[key] and then calling a function such as
(inside Test)
def __get(self, key, readable):
mapping = {'first' : 1, 'second' : 2}
return self[key][mapping[readable]]
(maybe it is a silly design...but i need this because i do want some kind of
readable object access without having to create all these objects)
Comment