This module includes different Python modules and scripts ubiquiously used on scripts in SomEnergia cooperative but with no entity by themselves to have their own repository.
venv
: run a command under a Python virtual enviromentsql2csv.py
: script to run parametrized sql queries and get the result as (tab separated) csv.dbutils.py
: module with db related functionsfetchNs
: a generator that wraps db cursors to fetch objects with attributes instead of psycopg arraysnsList
: uses the former to build a list of such object (slower but maybe convinient)csvTable
: turns the results of a query into a tab separated table with proper header names
sheetfetcher.py
: convenience class to retrieve data from gdrive spreadshets
This script is useful to run Python scripts under a given virtual environment. It is specially useful to run Python scripts from crontab lines.
usage: venv /PATH/TO/PYTHON/VIRTUALENV COMMAND [PARAM1 [PARAM2...]]
Runs an SQL file and outputs the result of the query as tabulator separated csv.a
You can provide query parameters either as yamlfile or as commandline options.
sql2csv.py <sqlfile> [<yamlfile>] [--<var1> <value1> [--<var2> <value2> ..] ]
Convenient cursor wrappers to make the database access code more readable.
Example:
import psycopg2, dbutils
db = psycopg2.connect(**dbconfiguration)
with db.cursor() as cursor :
cursor.execute("SELECT name, age FROM people")
for person as dbutils.fetchNs(cursor):
if person.age < 21: continue
print("{name} is {age} years old".format(person))
Convenient wraper for gdrive.
from sheetfetcher import SheetFetcher
fetcher = SheetFetcher(
documentName='My Document',
credentialFilename='drive-certificate.json',
)
table = fetcher.get_range("My Sheet", "A2:F12")
fulltable = fetcher.get_fullsheet("My Sheet")
- Document selectors can be either an uri or the title
- Sheet selectors can be either an index, a name or an id.
- Range selectors can be either a named range, index tuple or a "A2:F5" coordinates.
- You should Create a certificate and grant it access to the document