SheetsORM is a simple micro ORM extension for gspread.
Project description
SheetsORM
SheetsORM is a simple micro ORM extension for gspread.
Features:
- Create a entity class for each worksheet
- Columns are mapped to attributes
- Repository: Add,Remove,Update operations
- Type checking for attributes
Requirements
Python 3.9+
Installation
pip install sheetsorm
Basic usage
1.Create credentials in Google API Console
2.Creating an instance:
from sheetsorm.orm import SheetsORM
# Create a SheetsORM instance
gs = SheetsORM(scope=['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'],
credentials_file='./credentials.json', # pass the path of the credentials file, or google drive URL
is_url=False, # True if the credentials file is a google drive URL
spreadsheet_name='DEV' # pass the name of the spreadsheet
)
# if the spread sheet is not found, it will be created
gs.connect()
# You should share the spreadsheet with your preferred email address
gs.share('your@email.com','user','owner')
3.Create a class for each worksheet:
from sheetsorm.decorators import entity, column
from datetime import datetime
# define the entity
# the worksheet name by default is the class name
@entity(worksheet='Kat')
class Kat:
# our primary key auto_increment with non-nullable values
# name defines the column name in the worksheet, by default it is the attribute name
id = column(int,name='ID',primary_key=True,increment=True, required=True)
# pass the length of the column as argument to 'str'
name = column(str(200),name='NAME',required=True)
# or use as a string
color = column('20',name='COLOR')
# the entity decorator add a new constructor that allows you to create an instance
# from key values
meow = Kat(name='Meow',birth_date=datetime(2019,1,1),color='black')
repo = gs.get_repository(Kat)
# add the object
repo.add(meow)
# and then commit the changes
repo.commit()
# pass lambda function to filter the results
find_meow = repo.find(lambda x: x.name=='Meow')
if len(find_meow) == 1:
print('I Found Meow')
print(f'His id: {find_meow[0].id}')
print(f'His name: {find_meow[0].name}')
print(f'His birth date: {find_meow[0].birth_date}')
print(f'His color: {find_meow[0].color}')
else:
print('I did not find Meow')
meow = find_meow[0]
meow.name = 'Meow2'
# Runtime type checkings
# meow.color = 1 # TypeError: color must be of type 'str'
repo.update(meow)
repo.commit()
# Remove the row from the worksheet
repo.remove()
repo.commit()
# Other functions to use:
# repo.all(lambda x: x.color=='orange')
# repo.any(lambda x: x.name=='Mr. Potato')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sheetsorm-0.18.2.dev3.tar.gz
(9.9 kB
view details)
Built Distribution
File details
Details for the file sheetsorm-0.18.2.dev3.tar.gz
.
File metadata
- Download URL: sheetsorm-0.18.2.dev3.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 913148a511cdd2779c7975f898e950f73d7d1a428e45f381b2510498c00f242e |
|
MD5 | 87557d3fd7cbebb83038d404c373eb73 |
|
BLAKE2b-256 | fe2a95d9bcdc9ef61dab6f83e7f4388d122f1442477b6c9a1a3f0640b428469e |
Provenance
File details
Details for the file sheetsorm-0.18.2.dev3-py3-none-any.whl
.
File metadata
- Download URL: sheetsorm-0.18.2.dev3-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b76babccf41b968e981ee473ecab0d5ad2fb0d353996559257c38e2193c9cad |
|
MD5 | 9e60c5a20337536923f716aff14d37b7 |
|
BLAKE2b-256 | ca39fba1d0b2ac3780acbf1e5100c0dc46c526c294531e2c1bbfba86bdfba46f |