From cbc at unc.edu Mon Sep 28 17:13:36 2020 From: cbc at unc.edu (Calloway, Chris) Date: Mon, 28 Sep 2020 21:13:36 +0000 Subject: [TriPython] Nested Properties Message-ID: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu> A colleague asked me today about nested properties. I said I'd never seen such a thing. So, refusing the temptation to guess, I tried it. It's not pretty. It requires nested inner classes. I'm sure if you feel led to do something like this, there must be a better way. But it is possible: https://gist.github.com/cbcunc/4c797c88e96d9a2bd32d77dda3e18e21 -- Sincerely, Chris Calloway Applications Analyst University of North Carolina Renaissance Computing Institute (919) 599-3530 -------------- next part -------------- A colleague asked me today about nested properties. I said I'd never seen such a thing. So, refusing the temptation to guess, I tried it. It's not pretty. It requires nested inner classes. I'm sure if you feel led to do something like this, there must be a better way. But it is possible: ? https://gist.github.com/cbcunc/4c797c88e96d9a2bd32d77dda3e18e21 ? --? Sincerely, ? Chris Calloway Applications Analyst University of North Carolina Renaissance Computing Institute (919) 599-3530 ? From david at handysoftware.com Mon Sep 28 21:48:32 2020 From: david at handysoftware.com (David Handy) Date: Mon, 28 Sep 2020 21:48:32 -0400 Subject: [TriPython] Nested Properties In-Reply-To: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu> References: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu> Message-ID: The "inner" class definitions didn't have to be nested classes. They could be even classes imported from other modules if lots of other code is involved. Other than that I don't see a simpler way to do that. On 9/28/20 5:13 PM, Calloway, Chris wrote: > A colleague asked me today about nested properties. I said I'd never seen > such a thing. So, refusing the temptation to guess, I tried it. It's not > pretty. It requires nested inner classes. I'm sure if you feel led to do > something like this, there must be a better way. But it is possible: > > > > https://gist.github.com/cbcunc/4c797c88e96d9a2bd32d77dda3e18e21 > > > > -- > > Sincerely, > > > > Chris Calloway > > Applications Analyst > > University of North Carolina > > Renaissance Computing Institute > > (919) 599-3530 > > > > > _______________________________________________ > TriZPUG mailing list > TriZPUG at python.org > https://mail.python.org/mailman/listinfo/trizpug > http://tripython.org is the Triangle Python Users Group -------------- next part -------------- The "inner" class definitions didn't have to be nested classes. They could be even classes imported from other modules if lots of other code is involved. Other than that I don't see a simpler way to do that. On 9/28/20 5:13 PM, Calloway, Chris wrote: A colleague asked me today about nested properties. I said I'd never seen such a thing. So, refusing the temptation to guess, I tried it. It's not pretty. It requires nested inner classes. I'm sure if you feel led to do something like this, there must be a better way. But it is possible: ? [1]https://gist.github.com/cbcunc/4c797c88e96d9a2bd32d77dda3e18e21 ? --? Sincerely, ? Chris Calloway Applications Analyst University of North Carolina Renaissance Computing Institute (919) 599-3530 ? _______________________________________________ TriZPUG mailing list [2]TriZPUG at python.org [3]https://mail.python.org/mailman/listinfo/trizpug [4]http://tripython.org is the Triangle Python Users Group References Visible links 1. https://gist.github.com/cbcunc/4c797c88e96d9a2bd32d77dda3e18e21 2. mailto:TriZPUG at python.org 3. https://mail.python.org/mailman/listinfo/trizpug 4. http://tripython.org/ From znoop333 at hotmail.com Tue Sep 29 07:58:09 2020 From: znoop333 at hotmail.com (David Johnson) Date: Tue, 29 Sep 2020 11:58:09 +0000 Subject: [TriPython] Nested Properties In-Reply-To: References: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu>, Message-ID: IDK if this helps, but google's protobufs can automatically generate python classes with unlimited nested types [1], which is pretty similar to nested properties (but it uses python reflection instead of manually defining subclasses or messing with __getattr__). As an added bonus, you can then serialize it to disk or network sockets and read the protobuf binary string into another language [2], which I've found useful for going between python for rapid prototyping and a platform-specific language for a GUI (such as Android Java code). What is the use-case scenario that brought up the request for nested properties? [1] https://developers.google.com/protocol-buffers/docs/proto3#nested [2] https://developers.google.com/protocol-buffers/docs/pythontutorial -------------- next part -------------- IDK if this helps, but google's protobufs can automatically generate python classes with unlimited nested types [1], which is pretty similar to nested properties (but it uses python reflection instead of manually defining subclasses or messing with __getattr__). As an added bonus, you can then serialize it to disk or network sockets and read the protobuf binary string into another language [2], which I've found useful for going between python for rapid prototyping and a platform-specific language for a GUI (such as Android Java code). What is the use-case scenario that brought up the request for nested properties? [1] [1]https://developers.google.com/protocol-buffers/docs/proto3#nested [2] [2]https://developers.google.com/protocol-buffers/docs/pythontutorial References Visible links 1. https://developers.google.com/protocol-buffers/docs/proto3#nested 2. https://developers.google.com/protocol-buffers/docs/pythontutorial From thedizzle at gmail.com Tue Sep 29 08:36:29 2020 From: thedizzle at gmail.com (Eric Dill) Date: Tue, 29 Sep 2020 08:36:29 -0400 Subject: [TriPython] Nested Properties In-Reply-To: References: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu> Message-ID: Hey Chris, This is probably the cleanest implementation of nested attributes that I've seen: from collections import defaultdict class AttributeDict(defaultdict): def __init__(self): super(AttributeDict, self).__init__(AttributeDict) def __getattr__(self, key): try: return self[key] except KeyError: raise AttributeError(key) def __setattr__(self, key, value): self[key] = value Make use of it as follows: keys = AttributeDict() keys.abc.xyz.x = 123 keys.abc.xyz.a.b.c = 234 Eric On Tue, Sep 29, 2020 at 7:58 AM David Johnson wrote: > IDK if this helps, but google's protobufs can automatically generate > python classes with unlimited nested types [1], which is pretty similar > to > nested properties (but it uses python reflection instead of manually > defining subclasses or messing with __getattr__). As an added bonus, you > can then serialize it to disk or network sockets and read the protobuf > binary string into another language [2], which I've found useful for > going > between python for rapid prototyping and a platform-specific language > for > a GUI (such as Android Java code). > What is the use-case scenario that brought up the request for nested > properties? > [1] [1] > https://developers.google.com/protocol-buffers/docs/proto3#nested > [2] [2] > https://developers.google.com/protocol-buffers/docs/pythontutorial > > References > > Visible links > 1. https://developers.google.com/protocol-buffers/docs/proto3#nested > 2. https://developers.google.com/protocol-buffers/docs/pythontutorial > _______________________________________________ > TriZPUG mailing list > TriZPUG at python.org > https://mail.python.org/mailman/listinfo/trizpug > http://tripython.org is the Triangle Python Users Group > -- Eric Dill, PhD @chemisist github.com/ericdill -------------- next part -------------- Hey Chris, [1]This is probably the cleanest implementation of nested attributes that I've seen: from collections import defaultdict class AttributeDict(defaultdict): def __init__(self): super(AttributeDict, self).__init__(AttributeDict) def __getattr__(self, key): try: return self[key] except KeyError: raise AttributeError(key) def __setattr__(self, key, value): self[key] = value Make use of it as follows: keys = AttributeDict() keys.abc.xyz.x = 123 keys.abc.xyz.a.b.c = 234 Eric On Tue, Sep 29, 2020 at 7:58 AM David Johnson <[2]znoop333 at hotmail.com> wrote: ? ?IDK if this helps, but google's protobufs can automatically generate ? ?python classes with unlimited nested types [1], which is pretty similar to ? ?nested properties (but it uses python reflection instead of manually ? ?defining subclasses or messing with __getattr__). As an added bonus, you ? ?can then serialize it to disk or network sockets and read the protobuf ? ?binary string into another language [2], which I've found useful for going ? ?between python for rapid prototyping and a platform-specific language for ? ?a GUI (such as Android Java code). ? ?What is the use-case scenario that brought up the request for nested ? ?properties? ? ?[1] [1][3]https://developers.google.com/protocol-buffers/docs/proto3#nested ? ?[2] [2][4]https://developers.google.com/protocol-buffers/docs/pythontutorial References ? ?Visible links ? ?1. [5]https://developers.google.com/protocol-buffers/docs/proto3#nested ? ?2. [6]https://developers.google.com/protocol-buffers/docs/pythontutorial _______________________________________________ TriZPUG mailing list [7]TriZPUG at python.org [8]https://mail.python.org/mailman/listinfo/trizpug [9]http://tripython.org is the Triangle Python Users Group -- Eric Dill, PhD @chemisist [10]github.com/ericdill References Visible links 1. https://stackoverflow.com/a/41274937 2. mailto:znoop333 at hotmail.com 3. https://developers.google.com/protocol-buffers/docs/proto3#nested 4. https://developers.google.com/protocol-buffers/docs/pythontutorial 5. https://developers.google.com/protocol-buffers/docs/proto3#nested 6. https://developers.google.com/protocol-buffers/docs/pythontutorial 7. mailto:TriZPUG at python.org 8. https://mail.python.org/mailman/listinfo/trizpug 9. http://tripython.org/ 10. http://github.com/ericdill From znoop333 at hotmail.com Tue Sep 29 11:23:48 2020 From: znoop333 at hotmail.com (David Johnson) Date: Tue, 29 Sep 2020 15:23:48 +0000 Subject: [TriPython] Nested Properties In-Reply-To: References: <9E9C2A32-B82E-4CC8-8CF2-C48FE40A8D96@unc.edu> , Message-ID: If you want a dict-based implementation, you don't have to define a new class just to change the behavior of __getattr__. You can use SimpleNamespace and put anything you want into it with as much nesting as you care to write out: from types import SimpleNamespace aa=SimpleNamespace() aa.a=-543. aa.bb=SimpleNamespace() aa.bb.foo='bar' aa.bb.cc=SimpleNamespace() aa.bb.cc.dd=lambda x: x+1 print(aa) namespace(a=-543.0, bb=namespace(cc=namespace(dd=), foo='bar')) (I prefer the protobuf approach because it's more structured and interoperable, but SimpleNamespace works fine for internal data structures which will never be seen by other programs. I like SimpleNamespace better than namedtuple because namedtuple is immutable, but that makes it possible to use a namedtuple as a dict key, FWIW.) -------------- next part -------------- If you want a dict-based implementation, you don't have to define a new class just to change the behavior of __getattr__. You can use SimpleNamespace and put anything you want into it with as much nesting as you care to write out: from types import SimpleNamespace aa=SimpleNamespace() aa.a=-543. aa.bb=SimpleNamespace() aa.bb.foo='bar' aa.bb.cc=SimpleNamespace() aa.bb.cc.dd=lambda x: x+1 print(aa) namespace(a=-543.0, bb=namespace(cc=namespace(dd=), foo='bar')) (I prefer the protobuf approach because it's more structured and interoperable, but SimpleNamespace works fine for internal data structures which will never be seen by other programs. I like SimpleNamespace better than namedtuple because namedtuple is immutable, but that makes it possible to use a namedtuple as a dict key, FWIW.)