banesullivan / scooby

🐶 🕵️ Great Dane turned Python environment detective
MIT License
47 stars 12 forks source link

Add to_dict() for serialization #46

Closed prisae closed 4 years ago

prisae commented 4 years ago

Add a to_dict() method to serialize the report. This can be useful to, e.g., attach meta-data to data written to a file.

In [1]: import scooby                                                                                 

In [2]: scooby.Report().to_dict()                                                                     
Out[2]: 
{'Date': 'Wed Mar 18 06:36:40 2020 CET',
 'OS': 'Linux',
 'CPU(s)': 4,
 'Machine': 'x86_64',
 'Architecture': '64bit',
 'RAM': '7.7 GB',
 'Environment': 'IPython',
 'Python': '3.7.6 (default, Jan  8 2020, 19:59:22) \n[GCC 7.3.0]',
 'numpy': '1.18.1',
 'scipy': '1.4.1',
 'IPython': '7.12.0',
 'matplotlib': '3.1.3',
 'scooby': '0.5.2',
 'MKL': 'Intel(R) Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel(R) 64 architecture applications'}
prisae commented 4 years ago

I did some minor re-formatting to please flake8.

Thinking of the future: Maybe we should actually use the repr_dict() within __repr__ and _repr_html_ too, it could simplify a lot of code.

Meaning, e.g., in __repr__, we could do instead of

        text += '{:>18} : {}\n'.format('OS', self.system)
        text += '{:>18} : {}\n'.format('CPU(s)', self.cpu_count)
        text += '{:>18} : {}\n'.format('Machine', self.machine)
        text += '{:>18} : {}\n'.format('Architecture', self.architecture)

just this:

        for key in ['OS', 'CPU(s)', 'Machine', 'Architecture']:
            text += '{:>18} : {}\n'.format(key, repr_dict[key])
akaszynski commented 4 years ago

Changes look good. Thanks for the refactoring.

Perhaps in a different PR we can address repr_dict, and depending on the number of keys, it might be helpful to implement it as:

keys = ['OS', 'CPU(s)', 'Machine', 'Architecture']
for key in keys:
    text += '{:>18} : {}\n'.format(key, repr_dict[key])
prisae commented 4 years ago

Thanks for reviewing/merging @akaszynski !