Path: blob/main/scripts/local_fields/import_fields.py
1127 views
# -*- coding: utf-8 -*-1r""" Import local field data.23Imports from a json file directly to the database.45Data is imported directly to the table lf_fields67"""89import sys10from sage.all import QQ11import re12import json1314mypath = '../..'15sys.path.append(mypath)1617from lmfdb import db1819lf = db.lf_fields2021def list2string(li):22li2 = [str(x) for x in li]23return ','.join(li2)2425def string2list(s):26s = str(s)27if s == '':28return []29return [int(a) for a in s.split(',')]3031def string2slist(sl):32sl = str(sl)33sl = sl[1:-1]34if sl == '':35return []36return [str(a) for a in sl.split(',')]3738def make_slope_key(s):39qs = QQ(s)40sstring = str(qs*1.)41sstring += '0'*1442if qs < 10:43sstring = '0'+sstring44sstring = sstring[0:12]45sstring += str(qs)46return sstring4748def top_slope(ent):49sl = string2slist(ent['slopes'])50if len(sl)>0:51ent['top_slope'] = make_slope_key(sl[-1])52elif int(ent['t'])>1:53ent['top_slope'] = make_slope_key(1)54else:55ent['top_slope'] = make_slope_key(0)56return ent5758# Let us loop over input to load into a dictionary59fnames = ['aut','c','coeffs','e','eisen', 'f', 'gal', 'galT', 'gms', 'hw', 'inertia', 'label', 'n', 'p', 'rf', 'slopes', 't', 'u', 'unram', 'subfields', 'gsm']6061def prep_ent(l):62l[6]=l[7]63l[8]= str(l[8])64l[19] = [[list2string(u[0]),u[1]] for u in l[19]]65ent = dict(zip(fnames,l))66ent = top_slope(ent)67ent['rf'] = ent['rf']68ent['coeffs'] = ent['coeffs']69return ent7071count=07273# loop over files, and in each, loop over lines74for path in sys.argv[1:]:75print(path)76fn = open(path)77tot = 078outrecs = []79for line in fn.readlines():80line.strip()81count += 182if re.match(r'\S',line):83#print line84l = json.loads(line)85check = lf.lookup(str(l[11])) # by label86if check is None: # we don't have it yet87ent = prep_ent(l)88outrecs.append(ent)89#print str(ent['label'])90tot += 191if len(outrecs)>0:92lf.insert_many(outrecs)9394#outrecs=outrecs[0:125]95#from pprint import pprint as pp96#pp(outrecs[0])97#lf.insert_many(outrecs)9899print("Added %d records" % len(outrecs))100101102103104