def get_medals(years, season): for year in years: print year medal_chart = olympic_stats(str(year), season) rows = medal_chart.split('\n') session = models.create_db_session() for row in rows: data = row.split(' | ') if data[0]=='country': # header continue print data[0] c = models.Country(name=data[0]) matches = c.duplicates(session) if not matches: session.add(c) session.commit() c.reload_stats(session) else: c = matches[0] m = models.Medals(country_id=c.id, year=str(year), gold=data[1], silver=data[2], bronze=data[3]) matches = m.duplicates(session) if not matches: session.add(m) session.commit() models.end_db_session(session)
vcf_file = 'data/00003.vcf' with open(vcf_file, 'r') as f: data = f.read() f.close() entries = data.split('BEGIN:VCARD')[1:] contacts = [] for entry in entries: vcard_json = dict(tuple(re.split(':', e, maxsplit=1)) for e in entry.strip().split('\r\n') if ':' in e and 'PHOTO' not in e) vcard_json.pop('END', None) name = vcard_json.get('FN') session = models.create_db_session() c = models.Contact(name=name) matches = c.duplicates(session) if not matches: session.add(c) # print c.name else: c = matches[0] session.commit() v = models.Vcard(vcard=vcard_json, contact_id=c.id) session.add(v) for key in vcard_json.keys(): if 'EMAIL' in key:
from db import models session = models.create_db_session() results = session.query(models.SomeTable).filter(models.SomeTable.field1>0) print results models.end_db_session(session)