-
Notifications
You must be signed in to change notification settings - Fork 12
Examples of usage
rienafairefr edited this page Jul 15, 2017
·
5 revisions
A way to create a client and fetch data:
from pynYNAB.Client import nYnabClient
client = nYnabClient(email="############", password="######", budgetname='TestBudget')
client.sync()
You can also decouple the connection and session management from the client:
connection = nYnabConnection(email, password)
connection.init_session()
client = nYnabClient(nynabconnection=connection, budgetname='TestBudget')
client.sync()
Another way to get a client is the from_obj static method, it takes any object with the adequate attributes (budgetname,nynabconnection). I provided command line arguments parser that also looks for environment variables or a config file (in %localappdata%\pynYNAB\pynYNAB on Windows)
from pynYNAB.ClientFactory import clientfromargs
from pynYNAB.__main__ import parser
args = parser.parse_known_args()[0]
client = nYnabClient.from_obj(args)
client.sync()
Here we add two transactions and one payee, then push them
client.sync()
client.budget.be_transactions.append(transaction1)
client.budget.be_transactions.append(transaction2)
client.budget.be_payees.append(payee)
client.budget.push(3)
We can use the sqlalchemy backend in order to get interesting views to the data:
from sqlalchemy.sql import func
from datetime import datetime
session = client.sesssion
# sum of the amounts for all transactions younger than 10 weeks
session.query(func.sum(Transaction.amount)).filter(Transaction.date > datetime.datetime.now() - datetime.timedelta(weeks=10)).scalar()
# sum of the amounts for each month
session.query(func.strftime('%Y-%m',Transaction.date),func.sum(Transaction.amount)).group_by(extract('month',Transaction.date)).all()
# same for positive amounts only
session.query(func.strftime('%Y-%m',Transaction.date),func.sum(Transaction.amount)).filter(Transaction.amount>0).group_by(extract('month',Transaction.date)).all()
# see the total of the transactions, grouping per-payee
session.query(func.sum(Transaction.amount),Transaction.amount).group_by(Transaction.entities_payee).join(Payee).all()
Everything is possible, see sqlalchemy docs :-)