Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_commodities(self, session):
# no commodities in an empty gnucash file
commodities = session.query(Commodity.mnemonic).all()
assert commodities == [("EUR",)]
def get_all(self):
""" Loads all non-currency commodities, assuming they are stocks. """
query = (
self.__get_base_query()
.order_by(Commodity.mnemonic)
)
return query.all()
def get_by_symbol(self, symbol: str) -> Commodity:
""" Loads currency by symbol """
assert isinstance(symbol, str)
query = (
self.currencies_query
.filter(Commodity.mnemonic == symbol)
)
return query.one()
def get_stocks(self, symbols: List[str]) -> List[Commodity]:
""" loads stocks by symbol """
query = (
self.query
.filter(Commodity.mnemonic.in_(symbols))
).order_by(Commodity.namespace, Commodity.mnemonic)
return query.all()
def find(self, search_term: str) -> List[Commodity]:
""" Searches for security by part of the name """
query = (
self.query
.filter(Commodity.mnemonic.like('%' + search_term + '%') |
Commodity.fullname.like('%' + search_term + '%'))
)
return query.all()
session = book.session
# query example:
#accountsFiltered = session.query(Account).filter(Account.name >= "T").all()
# SQLAlchemy methods: count, first, all, one...
# Get all the currencies in the book (i.e. for update).
print("All currencies used in the book:")
currencies = session.query(Commodity).filter(Commodity.namespace == "CURRENCY").all()
for c in currencies:
print(c)
# Accessing individual records.
print("\nSelected single currency details (" + symbol + "):")
cdty = session.query(Commodity).filter(Commodity.namespace == "CURRENCY", Commodity.mnemonic == symbol).first()
# Accessing attributes of a commodity.
print("Commodity namespace={cdty.namespace}\n"
" mnemonic={cdty.mnemonic}\n"
" cusip={cdty.cusip}\n"
" fraction={cdty.fraction}".format(cdty=cdty))
# Loop through the existing commodity prices.
# This can be used to fetch the points for a price graph.
print("\nHistorical prices:")
for pr in cdty.prices:
print("Price date={pr.date}"
" value={pr.value} {pr.currency.mnemonic}/{pr.commodity.mnemonic}".format(pr=pr))
def get_by_symbol(self, symbol: str) -> List[Commodity]:
""" Returns all commodities with the given symbol """
query = (
self.__get_base_query()
.filter(Commodity.mnemonic == symbol)
)
return query.all()
def get_stocks(self, symbols: List[str]) -> List[Commodity]:
""" loads stocks by symbol """
query = (
self.query
.filter(Commodity.mnemonic.in_(symbols))
).order_by(Commodity.namespace, Commodity.mnemonic)
return query.all()
def get_book_currencies(self) -> List[Commodity]:
""" Returns currencies used in the book """
query = (
self.currencies_query
.order_by(Commodity.mnemonic)
)
return query.all()
def __search(svc: BookAggregate, model: CurrencySearchModel):
""" performs the search """
if not model.currency:
return None
query = svc.currencies.currencies_query_sorted
if model.currency:
query = query.filter(Commodity.mnemonic == model.currency)
# TODO if not the main currency, load exchange rates and display chart
if model.ref.currencies != svc.currencies.get_default_currency():
print("not the default currency. load data.")
return query.one()