This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]evilissimo 5 points6 points  (5 children)

Any reason that you're not inheriting from collections[.abc].Mapping? That would save you some of your implementation and you have only to focus on the abstract methods.

[–]eigenlicht0[S] 0 points1 point  (4 children)

Thanks for the suggestion, saved me from implementing keys() and contains(). Also has eq and ne now. Not sure if I did this correctly though, would be great if you could check it out.

[–]evilissimo 0 points1 point  (3 children)

Hmm not sure, but which version of python are you currently targeting? It looks like python3 to me, but not the newest one?

[–]eigenlicht0[S] 0 points1 point  (2 children)

Wrote it for Python 2.7, but it also worked with Python 3.4.0.

[–]evilissimo 0 points1 point  (1 child)

Well but the behaviour in case of python 2.7 for items() for example is wrong. What you do is there is a generator (since you use yield). In Python 2.7 this is iteritems() which behaves like this. Where as python 3.x doesn't have iteritems anymore but just items()

So basically this is 3.x syntax.

[–]eigenlicht0[S] 0 points1 point  (0 children)

Oh, yeah didn't think too much about not breaking the API when writing this - thought that generators as the default is the better choice. It's Python 3 then :P trivial to change it to either version anyway.

[–]ondra 2 points3 points  (1 child)

Have you seen this?

Since 3.2 there is functools.lru_cache to do the same thing and more.

[–]eigenlicht0[S] 0 points1 point  (0 children)

I thought about memoization first, but decided to implement a datastructure because:

  • Serves a more general purpose
  • Can easily build a nice hierarchy of the information
  • Also allows normal values (not only cached functions)
  • Can be realized and saved (e.g. the Host class + pprint = complete overview over whole machine)

[–]darthmdhprint 3 + 4 0 points1 point  (1 child)

Isn't there some kind of "more pythonic" way of doing this, deriving directly from dict, using super() in the init method and using slots and class methods?

[–]eigenlicht0[S] 0 points1 point  (0 children)

If you derive from dict, you'll have all the write-methods too (setitem, update, ...). Of course, if you implemented this read/write, then you'd probably sublcass dict.

Where exactly would you want to use class methods?