[web2py] Re: gluon.storage.Storage interface discrepancy?
I agree. s.a is not sufficient if you want to avoid code repetition. You'll naturally want use something like s['a'] at some point if you want to write good code and you will also want to rely on the convenient returns None instead of throwing an exception facility. There are workarounds for this (personally, I just wrote a wrapper class to go around it so that I could use a natural dictionary notation with keeping the None behavior). As part of your mandate is to support backward compatibility, someone probably relies on that peculiarity at this point, so you are stuck with the behavior of s['a'] throwing an exception the way it is now. Perhaps just make sure the behavior of the storage object is properly documented so that it doesn't catch new people off guard. Still, it'd make sense for s['a'] to have the same semantics as s.a.
Re: [web2py] Re: gluon.storage.Storage interface discrepancy?
On Nov 2, 2011, at 10:15 PM, Magnitus wrote: I agree. s.a is not sufficient if you want to avoid code repetition. You'll naturally want use something like s['a'] at some point if you want to write good code and you will also want to rely on the convenient returns None instead of throwing an exception facility. There are workarounds for this (personally, I just wrote a wrapper class to go around it so that I could use a natural dictionary notation with keeping the None behavior). As part of your mandate is to support backward compatibility, someone probably relies on that peculiarity at this point, so you are stuck with the behavior of s['a'] throwing an exception the way it is now. Perhaps just make sure the behavior of the storage object is properly documented so that it doesn't catch new people off guard. Still, it'd make sense for s['a'] to have the same semantics as s.a. I'm of two minds on classes like Storage. In my non-web2py work I stick with dict, using dict.get and dict.setdefault where appropriate. But web2py has a lot of Storage objects floating around...
[web2py] Re: gluon.storage.Storage interface discrepancy?
Definitely, so much is storage (vars, sessions, DAL results, etc) that you pretty much have to use it. And for that reason, I think it warrants its own small subsection in the manual. For uses where I don't have to rely on None identifying initialized keys with the None value specifically, I tend to prefer the returning of None behavior on uninitialized keys, but I understand why they had to make the dictionary general purpose enough that they couldn't implement it that way. I'm of two minds on classes like Storage. In my non-web2py work I stick with dict, using dict.get and dict.setdefault where appropriate. But web2py has a lot of Storage objects floating around...
[web2py] Re: gluon.storage.Storage interface discrepancy?
Any suggestion for improvement? On Oct 25, 3:32 pm, Jonathan Lundell jlund...@pobox.com wrote: On Oct 25, 2011, at 1:18 PM, Anthony abasta...@gmail.com wrote: On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases. Still, it'd make sense for s['a'] to have the same semantics as s.a.
Re: [web2py] Re: gluon.storage.Storage interface discrepancy?
Sure. On the road now, but shortly. On Oct 27, 2011, at 10:57 AM, Massimo Di Pierro massimo.dipie...@gmail.com wrote: Any suggestion for improvement? On Oct 25, 3:32 pm, Jonathan Lundell jlund...@pobox.com wrote: On Oct 25, 2011, at 1:18 PM, Anthony abasta...@gmail.com wrote: On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases. Still, it'd make sense for s['a'] to have the same semantics as s.a.
Re: [web2py] Re: gluon.storage.Storage interface discrepancy?
On Oct 27, 2011, at 10:57 AM, Massimo Di Pierro wrote: Any suggestion for improvement? I'm not sure when I'll have time to get around to it, but my idea is to implement both Storage.__getattr__ and .__getitem__ in terms of super(get(whatever, None)). (That's not quite the syntax, but you get the idea.) I don't suppose that __setattr__ needs any special attention. On Oct 25, 3:32 pm, Jonathan Lundell jlund...@pobox.com wrote: On Oct 25, 2011, at 1:18 PM, Anthony abasta...@gmail.com wrote: On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases. Still, it'd make sense for s['a'] to have the same semantics as s.a.
Re: [web2py] Re: gluon.storage.Storage interface discrepancy?
On Oct 27, 2011, at 5:03 PM, Jonathan Lundell wrote: On Oct 27, 2011, at 10:57 AM, Massimo Di Pierro wrote: Any suggestion for improvement? I'm not sure when I'll have time to get around to it, but my idea is to implement both Storage.__getattr__ and .__getitem__ in terms of super(get(whatever, None)). (That's not quite the syntax, but you get the idea.) I don't suppose that __setattr__ needs any special attention. __setitem__, I meant. On Oct 25, 3:32 pm, Jonathan Lundell jlund...@pobox.com wrote: On Oct 25, 2011, at 1:18 PM, Anthony abasta...@gmail.com wrote: On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases. Still, it'd make sense for s['a'] to have the same semantics as s.a.
[web2py] Re: gluon.storage.Storage interface discrepancy?
You can do s.get(6,None) On Oct 25, 1:07 pm, pepper_bg ilief...@gmail.com wrote: So gluon.storage.Storage is an amended dict subclass with dot notation indexing added. With the additional twist that when a key is missing s['missing_key'] will still raise the standard dict KeyError, while s.missing_key will return None. All this is fine, only as long as your keys are strings though. Using integer keys is still a very legitimate dict/Storage use, but you just don't have the non-KeyError-raising functionality (s.4 is just invalid syntax), i.e. I have to write different code depending on what my keys are. Here is some code if my explanation isn't clear: from gluon.storage import Storage s=Storage({'id':12,5:'yesterday'}) #check for a missing string key s.max #None s['max'] Traceback (most recent call last): File console, line 1, in module KeyError: 'max' #check for a missing integer key s[6] Traceback (most recent call last): File console, line 1, in module KeyError: 6 s.6 File console, line 1 s.6 ^ SyntaxError: invalid syntax I thought it would be trivial to make s['missing_key'] return None, just like for example gluon.storage.List already does. Or was the intent whoever wanted to catch exceptions to use s['missing_key'] and whoever wanted to check for None to use s.missing_key? If that is the case I guess a lot of legacy code exists which already relies on catching exceptions and I will stick to my Storage subclass but wanted to check just in case this behavior could be changed sometime in gluon.storage. Appreciate your feedback...
[web2py] Re: gluon.storage.Storage interface discrepancy?
You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input...
[web2py] Re: gluon.storage.Storage interface discrepancy?
On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases.
Re: [web2py] Re: gluon.storage.Storage interface discrepancy?
On Oct 25, 2011, at 1:18 PM, Anthony abasta...@gmail.com wrote: On Tuesday, October 25, 2011 4:08:20 PM UTC-4, pepper_bg wrote: You can do s.get(6,None) Still have to anticipate what my keys are and write different code accordingly. Which is fine in most cases I guess. Or may be I am trying to be too generic here. Thanks for your input... If you're not sure whether a key might be a string or integer, you can use the s.get() method in all cases. Still, it'd make sense for s['a'] to have the same semantics as s.a.