Luke Brooks via plug on 6 Jul 2021 10:06:46 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Python nested dict data structure

This little class would do what you want although it's pretty limited in functionality so far since I made it specifically for this example

>>> x = PerlObject()

>>> company = 'Acme Inc'

>>> x[company]['region'] = 'US'

>>> x[company]['counter'] += 3

>>> x

{'Acme Inc': {'region': US, 'counter': 3}}

class PerlObject:

    def __init__(selfvalue=None):

        self.Value = value


    def __getitem__(selfkey):


        if key not in self.Value:

            self.Value[key] = PerlObject()

        return self.Value[key]


    def __setitem__(selfkeyvalue):


        if isinstance(valuePerlObject):

            self.Value[key] = value


            self.Value[key] = PerlObject(value)


    def __delitem__(selfkey):


        if key in self.Value:

            del self.Value[key]


    def __add__(selfother):

        self.checkType(numbers.Number, 0)

        if isinstance(otherPerlObject):

            other.checkType(numbers.Number, 0)

            return PerlObject(self.Value + other.Value)


        return PerlObject(self.Value + other)


    def checkType(selftdfv=None):

        if self.Value == None:

            if dfv != None:

                self.Value = dfv


                self.Value = t()

        elif not isinstance(self.Value, t):

            raise Exception("Object does not support operation")


    def __repr__(self):

        return str(self)


    def __str__(self):

        return str(self.Value)

On Tue, Jul 6, 2021, 10:41 AM Victor via plug <> wrote:
>> # Add/inc_nstkv a value in a nested d
>> def inc_nstkv(d, key, subkey, value):
>>      d.setdefault(key, dict())
>>      d[subkey] = d.get(subkey, 1) + 1
d[subkey] = d.get(subkey, 0) + value
>### But that doesn't use `value`.  I guess I should have called
>### it accumulate and not increment.  This doesn't work:
>        d[subkey] = d.get(subkey, 1) += value
>### And I think it overwrites the region value, depending on order
>### (which will be unpredictable).

Oops, you're right that .get() should be using 0 as the default and
+value; fixed inline above. What led me to think of .setdefault() and
.get() was that you wrote their exact logic using other code plus the
explanation that you're pulling data from multiple sources where you
don't have a uniform dictionary output in mind. You can even eliminate
the functions you created entirely using .setdefault() and .get(), but
it's up to you if that diminishes readability. Example below.

> And I think it overwrites the region value, depending on order
I don't believe that's a problem, but maybe I'm not understanding your
expected output.

d = dict()
company = 'Acme Inc'

# If "company" key exists, add or set the "region" subkey; else the
"company" key should default to an empty dictionary then add or set
the "region" subkey.
d.setdefault(company, dict())['region'] = 'US'

# If "counter" key exists, get it's current value and add X; if
"counter" does not exist, return 0 as the default and add X.
d['counter'] = d.get('counter', 0) + 1
d['counter'] = d.get('counter', 0) + 2

Philadelphia Linux Users Group         --
Announcements -
General Discussion  --
Philadelphia Linux Users Group         --
Announcements -
General Discussion  --