trap attributeError from inside a dict ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GrelEns

    trap attributeError from inside a dict ?

    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)


  • Peter Otten

    #2
    Re: trap attributeError from inside a dict ?

    GrelEns wrote:
    [color=blue]
    > 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=green][color=darkred]
    >>>> t[1].get('first')[/color][/color]
    > 'a'
    >
    > without having to define tuple as an object or subclassing tuple, of
    > course now i had :[color=green][color=darkred]
    >>>> t[1].get('first')[/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)[/color]

    Here's one way to do it:
    [color=blue][color=green][color=darkred]
    >>> class Dict(dict):[/color][/color][/color]
    .... translate = {"first": 0, "second": 1}
    .... def __getitem__(sel f, key):
    .... value = dict.__getitem_ _(self, key[0])
    .... return value[self.translate[key[1]]]
    ....[color=blue][color=green][color=darkred]
    >>> d = Dict([(1,("a", "aa")), (2, ("b", "bb"))])
    >>> d[1, "first"][/color][/color][/color]
    'a'[color=blue][color=green][color=darkred]
    >>> d[2, "second"][/color][/color][/color]
    'bb'

    Here's another:
    [color=blue][color=green][color=darkred]
    >>> class Dict(dict):[/color][/color][/color]
    .... def __getitem__(sel f, key):
    .... return Wrap(dict.__get item__(self, key))
    ....[color=blue][color=green][color=darkred]
    >>> class Wrap(object):[/color][/color][/color]
    .... def __init__(self, data): self.data = data
    .... first = property(lambda self: self.data[0])
    .... second = property(lambda self: self.data[1])
    ....[color=blue][color=green][color=darkred]
    >>> d = Dict([(1,("a", "aa")), (2, ("b", "bb"))])
    >>> d[1].first[/color][/color][/color]
    'a'[color=blue][color=green][color=darkred]
    >>> d[2].second[/color][/color][/color]
    'bb'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Note that you might end with *more* objects as every call to __getitem__()
    creates a new Wrap instance. A cache could help, but if you did it properly
    with a custom class for the values the problem wouldn't arise.
    Personally, I would put custom objects into a normal dictionary instead of
    playing one of these tricks.

    Peter

    Comment

    • GrelEns

      #3
      Re: trap attributeError from inside a dict ?


      "Peter Otten" <__peter__@web. de> a écrit dans le message de news:
      c2mp83$b4l$02$1 @news.t-online.com...
      [color=blue]
      > Personally, I would put custom objects into a normal dictionary instead of
      > playing one of these tricks.[/color]

      you convince me thanks i will do it this way,

      best regards


      Comment

      Working...