Aug-10-2018, 05:44 PM
(Aug-10-2018, 01:31 AM)jfong Wrote: Inspired by your new change, allow me to push it one step further.
If none of the elements has a key, then all fields are simply numbered starting at 0.
This means that values is always returned as a dictionary, get rid of the confusion between list and dictionary. With this change, for those already written scripts which treat values as a list, the only required modification is where items were accessed through unpack, such as:
name, address, phone = values
modified to
name, address, phone = values.values()
If item was accessed by index, then no change at all.
name = values[0]
it's valid on both list and dictionary.
And you're certainly inspiring me to keep pushing this package.
I got a little help from a buddy on Pythonista Cafe. He came up with a "ListDict" class which is based on Ordered Dict.
Check out this bit of Python magic!
class ListDict(OrderedDict): def __iter__(self): for v in self.values(): yield v def __getitem__(self, item): if isinstance(item, slice): return list(self.values())[item] else: return super().__getitem__(item)This way there is no need to call value.values. I can access the return values in both dictionary style and as a list! I think it's kinda brilliant. I can't take credit for it however :-) But I can freely use it!