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.3.tar.gz
(9.8 kB
view details)
Built Distribution
File details
Details for the file sheetsorm-0.18.3.tar.gz
.
File metadata
- Download URL: sheetsorm-0.18.3.tar.gz
- Upload date:
- Size: 9.8 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 | 9a7772ca74c3d41a3673d300e50628ef76830b3aed6834d6073119f92b9e46ea |
|
MD5 | c75683c60263a2bd3fc16208e5cc1c22 |
|
BLAKE2b-256 | 29b3add2ffc9a4b39d6f7102a4ec93ab2c5d69fd9903699b9a5061721d8a04c2 |
File details
Details for the file sheetsorm-0.18.3-py3-none-any.whl
.
File metadata
- Download URL: sheetsorm-0.18.3-py3-none-any.whl
- Upload date:
- Size: 9.8 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 | d952f2c67547fe6b1b886a860d55f3f88223204011c73d9a93a499e2e31fb6aa |
|
MD5 | 62543e1ed9435e599e6364131ac14ac3 |
|
BLAKE2b-256 | d2fc278fb696011176c658c9383830b02a614170efd7c6fd5aced03855b696be |