A simple API to store/load python objects to/from spreadsheets
module-showcase
Module Showcase; ezspreadsheet
Description
ezspreadsheet is a python package I wrote a while ago to allow for the easy serialization and deserialization of python objects to/from spreadsheets.
Definitions
Serialization
This essentially means to store something generated in a program, in most cases to a file.
Deserialization
This essentially means to load something from an external source in a program, in most cases a file.
Usage
Running
Below has demo code for testing out storing and loading to/from a spreadsheet. To run the files:
- Install ezspreadsheet:
pip install ezspreadsheet
orsudo pip3 install ezspreadsheet
- Run the files
python3 ezspreadsheet-store_demo.py
,python3 ezspreadsheet-load_demo.py
Real World Applications
There are tons, but for example if you write a script that scrapes data from a site and builds objects, you can then store those values to a spreadsheet and read them later.
Additional info
Files
ezspreadsheet-store_demo.py
from dataclasses import dataclass
from ezspreadsheet import Spreadsheet
@dataclass
class User(): # Setup class to store instances of
Name:str
Age:int
Weight:int
Family: list
file_path = "users.xlsx" # also works with .csv
user_1 = User("Kieran", 21, 202, ["sister", "mother", "father"])
user_2 = User("Jim", 12, 170, ["brother", "mother", "father"])
with Spreadsheet(file_path, User) as output_file:
# Can also pass a list or tuple of intances such as [user_1, user_2]
output_file.store(user_1, user_2)
ezspreadsheet-load_demo.py
from dataclasses import dataclass
from ezspreadsheet import Spreadsheet
@dataclass
class User():
Name:str
Age:int
Weight:int
Family: list
file_path = "users.xlsx" # also works with .csv
# Retrieve namedtuple classes when class constructor is available
with Spreadsheet(file_path, User) as input_sheet:
users, instances = input_sheet.load("users")
print(instances) # [User(Name='Kieran', Age=21, Weight=202, Family=['sister', 'mother', 'father']), User(Name='Jim', Age=12, Weight=170, Family=['brother', 'mother', 'father'])]
print(users == User) # True
# Retrieve namedtuple classes when no class constructor is available
with Spreadsheet(file_path) as input_sheet:
users, instances = input_sheet.load("users")
print(instances) # [users(Name='Kieran', Age=21, Weight=202, Family=['sister', 'mother', 'father']), users(Name='Jim', Age=12, Weight=170, Family=['brother', 'mother', 'father'])]
print(users == User) # False