PFython / cleverdict

A JSON-friendly data structure which allows both object attributes and dictionary keys and values to be used simultaneously and interchangeably.
MIT License
102 stars 9 forks source link

Added from_csv and to_csv functions #33

Open gouravkr opened 2 years ago

gouravkr commented 2 years ago

This is for issue#31 I have added two functions in cleverdict.py file to handle creation of CleverDict from csv files and exporting of CleverDict objects into csv files. The CleverDicts created with from_csv function will be a nested CleverDict where top level dict will be indexed from 0 and each row will be converted to a CleverDict object, allowing people to use it like a list of dicts. For example:

>>> c_dict = CleverDict.from_csv('test.csv')
>>> print(c_dict[0].name)
Banana
>>> print(c_dict._0.name)
Banana

The to_csv function requires a similarly formatted CleverDict of CleverDicts and each underlying cleverdict is exported as a row, allowing the use of ignore, only parameters.

Ample tests have also been added in the existing tests file. The code has been tested under Python 3.7 on Linux and 3.9 on Windows and all tests pass successfully. I have made use of the csv library to read and write csv files.

PFython commented 2 years ago

This is brilliant thanks so much Gourav. Please bear with me while I have a good look through before merging officially. All the best, Pete

gouravkr commented 2 years ago

Hi. Any update on this? Do I need to make any changes?