Re: [PLUG] Python nested dict data structure

Gary Duzan via plug on 10 Jul 2021 10:22:46 -0700


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

Re: [PLUG] Python nested dict data structure


> Wow, that *would* work like Perl! But 40+ lines to make it Just Work.
>
> Also, I'm getting an error I don't understand.
 Adding "import numbers" on a line before "class PerlObject:" should do it.
 Gary Duzan
> Modified code:
> ```
> 1	#!/usr/bin/env python3
> 2	# dict3.py--do I REALLY have to build a CLASS just for NESTED
> dicts!?
> 3	# 2021年07月10日
> 4	# From Luke Brooks in EM "Re: [PLUG] Python nested dict data
> structure"
> 5
> 6	class PerlObject:
> 7
> 8	 def __init__(self, value=None):
> 9	 self.Value = value
> 10
> 11	 def __getitem__(self, key):
> 12	 self.checkType(dict)
> 13	 if key not in self.Value:
> 14	 self.Value[key] = PerlObject()
> 15	 return self.Value[key]
> 16
> 17	 def __setitem__(self, key, value):
> 18	 self.checkType(dict)
> 19	 if isinstance(value, PerlObject):
> 20	 self.Value[key] = value
> 21	 else:
> 22	 self.Value[key] = PerlObject(value)
> 23
> 24	 def __delitem__(self, key):
> 25	 self.checkType(dict)
> 26	 if key in self.Value:
> 27	 del self.Value[key]
> 28
> 29	 def __add__(self, other):
> 30	 self.checkType(numbers.Number, 0)
> 31	 if isinstance(other, PerlObject):
> 32	 other.checkType(numbers.Number, 0)
> 33	 return PerlObject(self.Value + other.Value)
> 34	 return PerlObject(self.Value + other)
> 35
> 36	 def checkType(self, t, dfv=None):
> 37	 if self.Value == None:
> 38	 if dfv != None:
> 39	 self.Value = dfv
> 40	 else:
> 41	 self.Value = t()
> 42	 elif not isinstance(self.Value, t):
> 43	 raise Exception("Object does not support operation")
> 44
> 45	 def __repr__(self):
> 46	 return str(self)
> 47
> 48	 def __str__(self):
> 49	 return str(self.Value)
> 50
> 51	d = PerlObject()
> 52
> 53	# Main
> 54	company = 'Acme Inc' # Key in both (all) files
> 55
> 56	# First read file 1, containing:
> Company\tRegion\tOther-stuff-I-don't-care-about-here
> 57	d.company = 'US'
> 58
> 59	# ...LATER...read file 2, containing *multiple records* of:
> Company\tthis\tthat\Counter
> 60	d[company]['counter'] += 2
> 61	d[company]['counter'] += 3
> 62
> 63	# ...STILL LATER...read file 3, containing even more crazy stuff
> 64	d[company]['subkey']['subsubkey'] = 'foo'
> 65	d[company]['subkey']['subsubint'] += 6
> 66
> 67	print(json.dumps(d, indent=2, sort_keys=True)) # Pretty but
> needs: import json
> ```
>
> Output:
> ```
> $ ./dict3.py
> Traceback (most recent call last):
> File "./dict3.py", line 60, in <module>
> d[company]['counter'] += 2
> File "./dict3.py", line 30, in __add__
> self.checkType(numbers.Number, 0)
> NameError: name 'numbers' is not defined
> ```
>
>
> On 7/6/21 1:06 PM, Luke Brooks via plug wrote:
>> 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
>
> <snip>
>
> Thanks,
> JP
> -- -------------------------------------------------------------------
> JP Vossen, CISSP | http://www.jpsdomain.org/ | http://bashcookbook.com/
> ___________________________________________________________________________
> Philadelphia Linux Users Group --
> http://www.phillylinux.org
> Announcements -
> http://lists.phillylinux.org/mailman/listinfo/plug-announce
> General Discussion --
> http://lists.phillylinux.org/mailman/listinfo/plug
>
___________________________________________________________________________
Philadelphia Linux Users Group -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug

AltStyle によって変換されたページ (->オリジナル) /