[TriPython] Nested Properties
Eric Dill
thedizzle at gmail.com
Tue Sep 29 08:36:29 EDT 2020
Hey Chris,
This is <https://stackoverflow.com/a/41274937> 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 <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]
> 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
More information about the TriZPUG
mailing list