cdgriffith / cdgriffith/Box

SUGGESTION: string version of sequence of attributes

Open
#298 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.8k
Forks
135
PR merge metrics
No merged PRs in 30d

Description

The following piece of code could be added in the source code, which allows using `myboxobj("a.b.c.d")` instead of `myboxobj.a.b.c.d`. This simplifies some dynamic coding.

~~~python
#!/usr/bin/env python3

from box import Box

# ---------------------- #
# -- BETTER BOX CLASS -- #
# ---------------------- #

###
# We make the class Box callable to allow the use of virtual
# pointed paths.
###
class BoxPlus(Box):
###
# prototype::
# content : a pointed path similar to a sequence of
# attribute request.
#
# :return: the expected value.
###
def __call__(
self,
str_attrs
):
val = self

for n in str_attrs.split('.'):
val = getattr(val, n)

return val
~~~

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.