Skip to main content

Tidy saving andeloading of Numpy and Pandas object attributes

Project description

zipdist

Keeping NumPy and Pandas attributes of python classes nice and tidy

Build Status Coverage Status PyPI version

The Zipdist parent class provides methods for saving and reloading NumPy arrays and Pandas DataFrame object attributes.

Complex attributes are saved to a single .tar.gz file.

The contents of the .tar.gz provides a tidy human-readable record of Pandas and Numpy Python class attributes as .csv or files.

Install

pip install zipdist

Basic Example

from zipdist.zip import Zipdist
import pandas as pd
import numpy as np
import sys

class Y(Zipdist):
	def __init__(self, name):
		self.name = name
		self.year = None

y = Y(name ='Simpsons')
y.years = [1989, 2020]
y.bart = np.zeros(10)
y.lisa = pd.DataFrame([{"Pi":3.1415,"e":2.7182}])
y._save(dest="Simpsons", dest_tar = "Simpsons.tar.gz")


ynew = Y(name = "Simpsons")
ynew._build(dest="Simpsons", dest_tar = "Simpsons.tar.gz")
sys.stdout.write(f"ynew.years: {ynew.years}\n")
sys.stdout.write(f"ynew.lisa:\n{ynew.lisa}\n")
sys.stdout.write(f"ynew.bart: {ynew.bart}\n")
Saving bart to .csv : Simpsons/bart.cs
Saving lisa to .csv : Simpsons/lisa.csv
Saving JSON with complex attribute definitions : Simpsons/complex_attributes.json
Saving JSON with simple attribute definitions : Simpsons/simple_attributes.json
Combining saved files in : [Simpsons.tar.gz].
Contents of Simpsons.tar.gz :
	Simpsons
	Simpsons/bart.cs
	Simpsons/complex_attributes.json
	Simpsons/lisa.csv
	Simpsons/simple_attributes.json
setting simple attribute name to Simpsons
setting simple attribute years to [2020, 2019]
setting [csv] to [np.ndarray] for attribute bart from: Simpsons/bart.cs
setting [csv] to [pd.DataFrame] for attribute lisa from: Simpsons/lisa.csv
ynew.years: [1989, 2020]
ynew.lisa:
       Pi       e
0  3.1415  2.7182
ynew.bart: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

Explanation

from zipdist.zip import Zipdist
import pandas as pd
import numpy as np
import sys
import os

Suppose you have some class Y. Let it inherit methods from Zipdist

class Y(Zipdist):
	def __init__(self, name):
		self.name = name
		self.year = None

Say you instantiate an instance of Y, assigning it two new complex attributes:

  • bart a basic numpy array of zeros, and
  • lisa a smart pandas DataFrame
y = Y(name ='Simpsons')
y.years = [1989, 2020]
y.bart = np.zeros(10)
y.lisa = pd.DataFrame([{"Pi":3.1415,"e":2.7182}])

_save()

Because Y inherits methods from the parent Zipdist (in particular _save(), and _build(), you can archive the non-json serializable attributes of your class, and rebuild the attributes from the original instance directly from Simpsons.tar.gz.

# ._save creates the file Simpsons.tar.gz
y._save(dest="Simpsons", dest_tar = "Simpsons.tar.gz")
assert os.path.isfile("Simpsons.tar.gz")

_build()

Suppose you want to rebuild the instance. (Note, it has no attributes bart or lisa)

ynew = Y("Simpsons")
assert 'bart' not in ynew.__dict__.keys()
assert 'years' not in ynew.__dict__.keys()

But you can rebuilt the attributes from Willy directly from a archived .tar.gz file

ynew._build(dest="Simpsons", dest_tar = "Simpsons.tar.gz")
assert isinstance(ynew.bart, np.ndarray)
assert isinstance(ynex.lisa, pd.DataFrame)

_ready()

You can also add simple (i.e., json serializable) or complex (numpy and panadas attributes) one by one as desired with _ready() and _reload_complex() and _reload_simple('years')

ynew = Y("Simpsons")
assert 'lisa' not in ynew.__dict__.keys()
assert 'bart' not in ynew.__dict__.keys()
assert 'years' not in ynew.__dict__.keys()
ynew._ready(dest="Simpsons", dest_tar = "Simpsons.tar.gz")
ynew._reload_complex('lisa')
assert isinstance(ynew.lisa, pd.DataFrame)
ynew._reload_simple('years')
assert isinstance(ynew.years, list)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zipdist-0.1.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

zipdist-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file zipdist-0.1.1.tar.gz.

File metadata

  • Download URL: zipdist-0.1.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for zipdist-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3a6f2d102f52bc298d4366afe442c12077298cde108e632d0efaf5fae75c1ff3
MD5 131ffd8765d9f44b3aaf85e0c991f1c5
BLAKE2b-256 43639e053c5555a8ec90e732d8bc2295349577a4b6f6b64a1b9ebce161c86cd2

See more details on using hashes here.

File details

Details for the file zipdist-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: zipdist-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for zipdist-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5767a7a99f8b0e4eb1bbf7ee39d808779f97d0e2ae97e813b12f1a07dbab6532
MD5 a3b5e083062596a7bafb9281fc1e3808
BLAKE2b-256 74ed4326dc79e78b2f4af13053a442453b1cbb409ce169172d11deb70658f880

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page