def upgrade(): for line in open('data/people.json', 'r'): person_r = json.loads(line) candidacies_r = person_r['assembly'] person = find_person(person_r) for candidacy in person.candidacies: election = candidacy.election try: candidacy_r = candidacies_r[unicode(election.age)] except KeyError, e: continue try: structurized = markup(candidacy_r['district'], 'district') except Exception, e: print candidacy_r['district'] import sys; sys.exit(1) op.execute(candidacy_t.update().\ where(and_(candidacy_t.c.person_id == person.id, candidacy_t.c.election_id == election.id)).\ values({ candidacy_t.c.district: [term[0] for term in structurized], candidacy_t.c.district_id: [term[1] for term in structurized], }) )
def insert_person(session, r): person = guess_person(r) if person: extra_vars = json.loads(person.extra_vars) extra_vars.update(r) extra_vars['assembly']['19'] = r person.extra_vars = json.dumps(extra_vars) else: person_id = get_person_id(r) name_en = translit(r['name_kr'], 'ko', 'en', 'name') gender = map_gender[r['sex']] birthday = '%04d%02d%02d' % ( int(r.get('birthyear', 0)), int(r.get('birthmonth', 0)), int(r.get('birthday', 0)) ) education = markup(r['education'], 'education') if 'education' in r else [] address = markup(r['address'], 'district') if 'address' in r else [] extra_vars = r.copy() extra_vars['assembly'] = {} extra_vars['assembly']['19'] = r person = Person( id=person_id, name=r['name_kr'], name_en=name_en, name_cn=r['name_cn'], gender=gender, birthday=birthday, education=[term[0] for term in education], education_id=[term[1] for term in education], address=[term[0] for term in address], address_id=[term[1] for term in address], image=r.get('image', None), twitter=r.get('twitter', None), facebook=r.get('facebook', None), blog=r.get('blog', None), homepage=r.get('homepage', None), extra_vars=json.dumps(extra_vars) ) session.add(person) session.flush() return person.id
def insert_person(session, r): person = guess_person(r) if person: extra_vars = json.loads(person.extra_vars) extra_vars.update(r) extra_vars['assembly']['19'] = r person.extra_vars = json.dumps(extra_vars) else: person_id = get_person_id(r) name_en = translit(r['name_kr'], 'ko', 'en', 'name') gender = map_gender[r['sex']] birthday = '%04d%02d%02d' % (int(r.get( 'birthyear', 0)), int(r.get('birthmonth', 0)), int(r.get('birthday', 0))) education = markup(r['education'], 'education') if 'education' in r else [] address = markup(r['address'], 'district') if 'address' in r else [] extra_vars = r.copy() extra_vars['assembly'] = {} extra_vars['assembly']['19'] = r person = Person(id=person_id, name=r['name_kr'], name_en=name_en, name_cn=r['name_cn'], gender=gender, birthday=birthday, education=[term[0] for term in education], education_id=[term[1] for term in education], address=[term[0] for term in address], address_id=[term[1] for term in address], image=r.get('image', None), twitter=r.get('twitter', None), facebook=r.get('facebook', None), blog=r.get('blog', None), homepage=r.get('homepage', None), extra_vars=json.dumps(extra_vars)) session.add(person) session.flush() return person.id
def insert_person(session, r): person = guess_person(r) if person: extra_vars = json.loads(person.extra_vars) extra_vars.update(r) extra_vars["assembly"]["19"] = r person.extra_vars = json.dumps(extra_vars) else: person_id = get_person_id(r) name_en = translit(r["name_kr"], "ko", "en", "name") gender = map_gender[r["sex"]] birthday = "%04d%02d%02d" % (int(r.get("birthyear", 0)), int(r.get("birthmonth", 0)), int(r.get("birthday", 0))) education = markup(r["education"], "education") if "education" in r else [] address = markup(r["address"], "district") if "address" in r else [] extra_vars = r.copy() extra_vars["assembly"] = {} extra_vars["assembly"]["19"] = r person = Person( id=person_id, name=r["name_kr"], name_en=name_en, name_cn=r["name_cn"], gender=gender, birthday=birthday, education=[term[0] for term in education], education_id=[term[1] for term in education], address=[term[0] for term in address], address_id=[term[1] for term in address], image=r.get("image", None), twitter=r.get("twitter", None), facebook=r.get("facebook", None), blog=r.get("blog", None), homepage=r.get("homepage", None), extra_vars=json.dumps(extra_vars), ) session.add(person) session.flush() return person.id
def insert_candidacy(session, r, person_id, date): election_id = get_election_id(session, date) party_id = get_party(session, r['party']).id district = markup(r['district'], 'district') candidacy = Candidacy( person_id=person_id, election_id=election_id, party_id=party_id, is_elected=r['elected'], cand_no=to_int(r.get('cand_no', 0)), vote_score=to_int(r.get('votenum', 0)), vote_share=to_float(r.get('voterate', 0)), district=[term[0] for term in district], district_id=[term[1] for term in district], ) session.add(candidacy)
def insert_candidacy(session, r, person_id, date): election_id = get_election_id(session, date) party_id = get_party(session, r["party"]).id district = markup(r["district"], "district") candidacy = Candidacy( person_id=person_id, election_id=election_id, party_id=party_id, is_elected=r["elected"], cand_no=to_int(r.get("cand_no", 0)), vote_score=to_int(r.get("votenum", 0)), vote_share=to_float(r.get("voterate", 0)), district=[term[0] for term in district], district_id=[term[1] for term in district], ) session.add(candidacy)
def upgrade(): people = Person.query for person in people: try: extra_vars = json.loads(person.extra_vars) except ValueError, e: continue if not extra_vars.get('address'): continue structurized = markup(extra_vars['address'], 'district') op.execute(person_t.update().\ where(person_t.c.id == person.id).\ values({ person_t.c.address: [term[0] for term in structurized], person_t.c.address_id: [term[1] for term in structurized], }) )
def upgrade(): people = Person.query.all() for person in people: try: extra_vars = json.loads(person.extra_vars) except ValueError, e: continue if not extra_vars.get('education'): continue structurized = markup(extra_vars['education'], 'education') op.execute(person_t.update().\ where(person_t.c.id == person.id).\ values({ person_t.c.education: [term[0] for term in structurized], person_t.c.education_id: [term[1] for term in structurized], }) )