Skip to content

SUGGESTION: string version of sequence of attributes #298

@projetmbc

Description

@projetmbc

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.

#!/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions