I have many instances that look like the code below. How can I abstract them and make them reusable?
#json_generator() is a function that yields paginated json
#Foo and Bar are sqlalchemy models
def fetch_foo_data():
URL = BASE_URL + ','.join(Foo.__table__.columns.keys())
return json_generator(URL)
def fetch_bar_data():
#Bar model has id as primary key that doesn't exist in the JSON data
#so I am excluding it
URL = BASE_URL + ','.join(Bar.__table__.columns.keys()[1:])
return json_generator(URL)
def bulk_load_foo_data():
for json in fetch_foo_data():
foos = map(lambda foo: Foo(**foo), json)
try:
session.bulk_save_objects(foos)
session.commit()
except Exception as e:
print e
session.rollback()
def bulk_load_bar_data():
for json in fetch_bar_data():
bars = map(lambda bar: Bar(**bar), json)
try:
session.bulk_save_objects(bars)
session.commit()
except Exception as e:
print e
session.rollback()