def database(): db = SessionLocal() try: yield db finally: db.close()
def get_db(): db = SessionLocal() try: yield db finally: db.close()
def get_db(): db = SessionLocal() try: return db finally: db.close()
def get_db() -> Generator: try: db = SessionLocal() yield db finally: db.close()
def import_daily_report(report): db_instance = SessionLocal() to_deduplicate = defaultdict(list) for row in report: last_update_str = row.get('Last Update') or row['Last_Update'] last_update = datetime.fromisoformat(last_update_str) # Region identifiers country_region = row.get('Country_Region') or row['Country/Region'] province_state = row.get('Province_State') or row.get('Province/State') admin2 = row.get('Admin2') fips = row.get('fips') province_state = clean_optional_field(province_state) fips = clean_optional_field(fips) admin2 = clean_admin2(admin2) # Measures confirmed = int(row['Confirmed']) deaths = int(row['Deaths']) recovered = int(row['Recovered']) try: dr = get_daily_report_by_region_and_date( db=db_instance, province_state=province_state, country_region=country_region, admin2=admin2, fips=fips, last_update=last_update) except NoResultFound: dr = DailyReport( province_state=province_state, country_region=country_region, admin2=admin2, fips=fips, last_update=last_update, confirmed=confirmed, deaths=deaths, recovered=recovered, ) except Exception: print(f'Invalid row: {row!r}') raise else: if dr.confirmed > confirmed or dr.deaths > deaths or dr.recovered > recovered: print(f'Warning: NOT lowering estimates for {dr!r}') print( f'New data: confirmed={confirmed}, deaths={deaths}, recovered={recovered}' ) continue else: dr.confirmed = confirmed dr.deaths = deaths dr.recovered = recovered db_instance.add(dr) # Track potentially duplicated admin2s if dr.admin2 in DUPLICATE_ADMIN2.values(): to_deduplicate[dr.admin2].append(dr) deduplicate(db_instance, to_deduplicate) db_instance.flush() sanity_check(db_instance) db_instance.commit()
from db import crud from db.database import SessionLocal with open("dbb", "r") as file: for line in file: data = line.split(',')[1:] data[-1] = data[-1].replace('\n', '') if data[0] == 'firm': continue sail = { 'firm': data[0], 'model': data[1], 'wind_min': float(data[2]), 'wind_max': float(data[3]), 'mast_rake': float(data[4]), 'prebend': int(data[5]), 'tension': data[6], 'mast_step': int(data[7]), 'chocks': data[8], 'spreraders_leng': int(data[9]) } db = SessionLocal() crud.create_sail(db, sail) db.close()
def get_all_tasks(user_id: int): db: Session = SessionLocal() tasks = db.query( task_model.Task).filter(task_model.Task.user_id == user_id).all() return tasks
import uuid from sqlalchemy.orm import Session from db.database import SessionLocal from db import models, schemas def get_cities(db: Session): return db.query(models.City).all() def get_city_streets(db: Session, city_id: uuid.UUID): return db.query(models.Street).filter( models.Street.my_city.has(id=city_id)) if __name__ == '__main__': db = SessionLocal() cities = get_cities(db) for city in cities: print(f'City {city.name}') for street in get_city_streets(db, city.id): print(' ' * 4 + f'{street.name}')
def get_user_id(username: AnyStr, session: Session = SessionLocal()): return ( session.query(models.User).filter(models.User.username == username).first().id )