def svncode(): #from DB import engine,svnset #from sqlalchemy.orm import sessionmaker #Session = sessionmaker(bind=engine) #session = Session() if request.method == 'GET': #查找 lista=[] alldata = models.select(b for b in models.Svndb )[:] #alldata = session.query(svnset).all() for somedata in alldata: dicta={} dicta['author'] = somedata.author dicta['change'] = somedata.change dicta['diff'] = somedata.diff dicta['md5'] = somedata.md5 dicta['lock_flag'] = bool(somedata.lock_flag) lista.append(dicta) return repr(lista) else:#存储 POST from werkzeug.datastructures import ImmutableMultiDict templist = request.form if templist.has_key('md5'): #存储数据 #print templist['md5'] alldata = models.select(S for S in models.Svndb if S.change == templist['change'] and S.md5 == templist['md5'])[:] ''' alldata = session.query(svnset).filter(\ #svnset.name == templist['name'], \ svnset.change == templist['change'], \ #svnset.diff == templist['diff'], \ svnset.md5 == templist['md5']).all() ''' if alldata: for line in alldata: #返回存储值状态0,1,2 #print unicode(line.change).encode('gbk') if str(line.lock_flag) == '0': line.delete() #session.commit() return str(line.lock_flag) else:#没有这条数据,存储下来,返回3 #a = templist['change'] #chtemp = tools.ChangeCoding(a) #for ch in lockroute: #if re.compile(ch).search(chtemp): #svnset = models.Svndb(change=templist['change'], author=templist['name'],diff = templist['diff'],md5 =templist['md5'],lock_flag = '2' ) #else: #svnset = models.Svndb(change=templist['change'], author=templist['name'],diff = templist['diff'],md5 =templist['md5'],lock_flag = '0' ) create = time.strftime('%Y-%m-%d %H:%M:%S') #print templist['log'],'logs' #print templist['diff'],'diffs' svnset = models.Svndb(change=templist['change'], author=templist['name'],diff = templist['diff'],md5 =templist['md5'],lock_flag = '2',log = templist['log'],creattime = unicode(create)) alldatalist = models.select(b for b in models.Svndb )[:] print len(alldatalist),'length1' content = '有一条来自'.decode('utf8')+templist['name']+ u'的新提交,请尽快处理。\r\nlogs: '+tools.ChangeCoding(templist['log']) #############################此处可添加popo提醒########################################### return '3' else: return 'wrong data'
def home(): address = flask.request.args.get('address') nocheck = (flask.request.args.get('force') == 'true') if address: reponame = cleanname(address) if not models.Repo.get(name=reponame): try: if nocheck: if flask.request.args.get('reponame') != address: return flask.render_template( 'index.html', error='reponame not match') desc = 'unknown desc - forceadd' else: desc = checkrepo(reponame) except Exception as e: force_add = ''' If you confirm this is a go main package. <form class="pull-right"> <input type="hidden" name="address" value="{reponame}"> <input type="hidden" name="force" value="true"> <input type="text" name="reponame" placeholder="Type repo name again"> <button class="btn btn-warning btn-xs">force add</button> </form>'''.format(reponame=reponame) error = str(e) + ' <br>- ' + force_add return flask.render_template('index.html', error=flask.Markup(error)) repo = models.Repo(name=reponame) repo.created = datetime.datetime.today() repo.description = desc repo.author = 'unknown .. unfinished' # add new job build = models.Build(repo=repo, tag='branch:master', status='initing') job = models.Job(build=build, status='initing', created=datetime.datetime.today()) models.commit() taskqueue.que.put(job.id) return flask.redirect('/' + reponame, 302) new = models.select(r for r in models.Repo).\ order_by(models.desc(models.Repo.created))[:10] top = models.select(r for r in models.Repo).\ order_by(models.desc(models.Repo.down_count))[:10] #error = 'Beta version, only for test for the time now.' error = None return flask.render_template('index.html', top_repos=top, new_repos=new, error=error)
def getResData(self, id=None): cls = self.model_class if id is not None: ids = re.split('\s*,\s*', id) try: objs = select(i for i in cls if i.id in ids)[:] except (ObjectNotFound, DataError) as ex: abort(404) else: objs = select(i for i in cls)[:] return objs
def get(self, id=None): cls = self.model_class if id is not None: ids = re.split('\s*,\s*', id) try: # objs = [cls[id]] objs = select(i for i in cls if i.id in ids)[:] except (ObjectNotFound, DataError) as ex: abort(404) else: objs = select(i for i in cls)[:] return json_response(objs)
def add_item(self, name, brand, category, product_code, **kwargs): item = Item(user=self.user) self.session.add(item) spec_names = { 'name': name, 'brand': brand, 'category': category, 'product_code': product_code } spec_names.update(kwargs) spec_names = dict((k.lower(), v) for k, v in spec_names.items()) specs = Spec.ensure(self.session, self.user, spec_names.keys()) for (name, value) in spec_names.items(): spec = select(specs, 'name', name) assert (spec is not None) item_spec = ItemSpec(item=item, spec=spec, value=value, user=self.user) self.session.add(item_spec) self.session.commit() return item
def home(): address = flask.request.args.get('address') nocheck = (flask.request.args.get('force') == 'true') if address: reponame = cleanname(address) if not models.Repo.get(name=reponame): try: if nocheck: if flask.request.args.get('reponame') != address: return flask.render_template('index.html', error='reponame not match') desc = 'unknown desc - forceadd' else: desc = checkrepo(reponame) except Exception as e: force_add = ''' If you confirm this is a go main package. <form class="pull-right"> <input type="hidden" name="address" value="{reponame}"> <input type="hidden" name="force" value="true"> <input type="text" name="reponame" placeholder="Type repo name again"> <button class="btn btn-warning btn-xs">force add</button> </form>'''.format(reponame=reponame) error = str(e) + ' <br>- ' + force_add return flask.render_template('index.html', error=flask.Markup(error)) repo = models.Repo(name=reponame) repo.created = datetime.datetime.today() repo.description = desc repo.author = 'unknown .. unfinished' # add new job build = models.Build(repo=repo, tag='branch:master', status='initing') job = models.Job(build=build, status='initing', created=datetime.datetime.today()) models.commit() taskqueue.que.put(job.id) return flask.redirect('/'+reponame, 302) new = models.select(r for r in models.Repo).\ order_by(models.desc(models.Repo.created))[:10] top = models.select(r for r in models.Repo).\ order_by(models.desc(models.Repo.down_count))[:10] #error = 'Beta version, only for test for the time now.' error=None return flask.render_template('index.html', top_repos=top, new_repos=new, error=error)
def edit_item(self, item, **kwargs): item_specs = item.item_specs specs = Spec.ensure(self.session, self.user, kwargs.keys()) for (name, value) in kwargs.items(): item_spec = select(item_specs, lambda x: x.spec.name, name) spec = select(specs, 'name', name) if item_spec is not None: if item_spec.value == value: continue item_spec.active = False item_spec.expired_at = _get_datetime() item_spec = ItemSpec(item=item, spec=spec, value=value, user=self.user) self.session.add(item_spec) self.session.commit()
def edit_variant(self, variant1, **kwargs): variant_specs = variant1.variant_specs specs = Spec.ensure(self.session, self.user, kwargs.keys()) for (name, value) in kwargs.items(): variant_spec = select(variant_specs, lambda x: x.spec.name, name) spec = select(specs, 'name', name) if variant_spec is not None: if variant_spec.value == value: continue variant_spec.active = False variant_spec.expired_at = _get_datetime() new_variant_spec = VariantSpec(variant=variant1, spec=spec, value=value, user=self.user) self.session.add(new_variant_spec) self.session.commit()
def clear(): try: alldatalist = models.select(b for b in models.Svndb )[:] for data in alldatalist: data.delete() return 'succeed' except Exception,e: print e return 'something wrong '
def tasklist(bid): build = models.Build.get(id=bid) repo = build.repo jobs = models.select(b for b in models.Job \ if b.build == build).order_by(models.desc(models.Job.created)) kwargs = dict(repo=repo, build=build, jobs=jobs) return render_template('tasklist.html', **kwargs)
def repolist(): goos=request.args.get('os', 'windows') goarch=request.args.get('arch', 'amd64') data = [] for r in models.select(r for r in models.Recommend)[:]: item = dict( reponame=r.repo.name, alias=r.name, author=r.repo.author, description=r.repo.description, offical=r.repo.offcial, category=r.category.name if r.category else None, stars=r.repo.stars, ) files = [] for b in r.repo.builds: if not b.downloadable: continue # actually only one loop file = {'label':b.tag, 'updated':b.updated} for f in models.select(f for f in models.File \ if f.build==b and f.os == goos and f.arch == goarch)[:1]: file.update({'binfiles': [os.path.basename(f.reponame)], # FIXME: need to parse from gobuildrc 'size': f.size, 'url': f.outlink, 'sha1': f.sha}) files.append(file) if files: item['files'] = files data.append(item) dict( reponame = 'github.com/codeskyblue/cgotest', description='this is is just a test program', alias='cgotest', # this could be null author='unknown,lunny', offical=True, category='music', stars=18, files=[ {'label': 'branch:master', 'url': 'http://gobuild3.qiniudn.com/github.com/gogits/gogs/branch-v-master/gogs-linux-386.tar.gz', 'binfiles': ['gogs'], 'sha1': '408eebced1c2cdbd363df2fe843831bf337d4273', 'size': 7000000}, {'label': 'tag:v0.5.2', 'url': 'http://gobuild3.qiniudn.com/github.com/gogits/gogs/tag-v-v0.5.2/gogs-linux-386.tar.gz', 'binfiles': ['gogs'], 'sha1': '960e329d46ec7a79745cf3438eaf3c3151d38d97', 'size': 7100000}], ) return flask.jsonify({'status': 0, 'message': 'success', 'osarch': goos+'-'+goarch, 'data': data})
def get_model(model_name: Text, prefer_cuda: bool, kwargs: Dict): device = require_device(prefer_cuda) model_type = models.select(model_name) # see models/__init__.py # filter out options that are not set in command line # default to ignore both None and False (for flag options) model_kwargs = util.dict_filter(kwargs, lambda k, v: bool(v)) model_info = (model_type, device, model_kwargs) model = build_model(model_info, no_initialize=True) return model, model_info
def post(self): cls = self.model_class rvals = self.getReqData() obj = cls.from_dict(rvals, self._relation_handler) try: commit() except TransactionIntegrityError as ex: obj = select(i for i in cls if i.hash == obj.hash)[:][0] abort(409, id=obj.id) return json_response({'id': obj.id})
def repo(reponame): repo = models.Repo.get(name=reponame) if not repo: return flask.redirect('/?address='+reponame) builds = models.select(b for b in models.Build \ if b.repo == repo).order_by(models.Build.updated) active_tag = request.args.get('tag', 'branch:master') build = models.Build.get(repo=repo, tag=active_tag) osarchs = [] if build and build.osarchs: osarchs = json.loads(build.osarchs) categorys = [] for cg in models.select(c for c in models.Category)[:]: categorys.append(cg.name) kwargs = dict(repo=repo, builds=builds, categorys=categorys, active_tag=active_tag, build=build, osarchs=osarchs) return render_template('repo.html', **kwargs)
def _cache(data: TextIO, model_name: Text, output: BinaryIO, **kwargs): cpu = require_device(prefer_cuda=False) model_type = models.select(model_name) model = ModelInterface(model_type, cpu, False) csv = util.load_csv(data) cache = {} for smiles in csv.keys(): cache_key = (smiles, ) # memcached is indexed on argument list data = model.process(smiles) cache[cache_key] = model.encode_data(data, **kwargs) pickle.dump(cache, output)
def repo(reponame): repo = models.Repo.get(name=reponame) if not repo: return flask.redirect('/?address=' + reponame) builds = models.select(b for b in models.Build \ if b.repo == repo).order_by(models.Build.updated) active_tag = request.args.get('tag', 'branch:master') build = models.Build.get(repo=repo, tag=active_tag) osarchs = [] if build and build.osarchs: osarchs = json.loads(build.osarchs) categorys = [] for cg in models.select(c for c in models.Category)[:]: categorys.append(cg.name) kwargs = dict(repo=repo, builds=builds, categorys=categorys, active_tag=active_tag, build=build, osarchs=osarchs) return render_template('repo.html', **kwargs)
def no(): anon_ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr) today_utc = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) anon_shipped = models.select( s for s in models.Ship if s.ip_address == anon_ip and s.dt_shipped > today_utc) shipped = models.select(s for s in models.Ship if s.dt_shipped > today_utc) if anon_shipped: no = shipped.filter(lambda n: n.no).count() return render_template('no.html', shipped=shipped.count(), no=no, percent=(int(no)) / (int(shipped.count())) * 100) models.Ship(no=True, dt_shipped=datetime.utcnow(), ip_address=anon_ip) models.commit() no = shipped.filter(lambda n: n.no).count() return render_template('no.html', shipped=shipped.count(), no=no, percent=(int(no)) / (int(shipped.count())) * 100)
def create_bug(): try: if request.method == 'POST': templist = request.form #print templist,222222222222222222222222222 #print templist.get('content'),3333333333333 alldata = models.select(S for S in models.Bugs if (S.author == templist.get('author') and S.content == templist.get('content')))[:] if not alldata: bug = models.Bugs(content=templist.get('content')) bug.author=templist.get('author') bug.time = templist.get('time') return '1' except Exception,e: print e return '2'
def add_variant(self, item, cost_price, selling_price, quantity, **kwargs): variant = Variant(item=item, user=self.user) self.session.add(variant) spec_names = { 'cost_price': cost_price, 'selling_price': selling_price, 'quantity': quantity } spec_names.update(kwargs) specs = Spec.ensure(self.session, self.user, spec_names.keys()) for (name, value) in spec_names.items(): spec = select(specs, 'name', name) assert (spec is not None) variant_spec = VariantSpec(variant=variant, spec=spec, value=value, user=self.user) self.session.add(variant_spec) self.session.commit() return variant
def main(args): # preprocess original raw data with commons.PhaseLogger("Preprocess TrainSet"): preprocessor = preprocesses.select(args.preprocess) # 这里v2 先用着测试,到时候名字改回来 preprocessor.handleData_v2(args.train_input, args.intermediate_data, is_train=True) with commons.PhaseLogger("Training Model"): # train model model = models.select(args.model) model.train(args.intermediate_data) with commons.PhaseLogger("Saving Model"): # save model model.save(args.intermediate_data) with commons.PhaseLogger("Evaluating Model"): # test model preprocessor.handleData_v2(args.test_input, args.intermediate_data, is_train=False) model.evaluate(args.intermediate_data)
def get_exam(self): exam = self._get_old_in_time_exam() if exam: return self.exam_to_dict(exam) ids_query = select(el.explain.id for el in Exam) while True: try: query = Explain.select(lambda x: x.id not in ids_query) explain = self._get_query_random(query) sentence = self._get_query_random( Sentence.select(lambda x: x.explain == explain)) exam_query = Exam.select(lambda x: x.sentence == sentence) if exam_query.count(): exam = exam_query.first() exam.appear_cout += 1 else: exam = Exam(word=explain.word, explain=explain, sentence=sentence) return self.exam_to_dict(exam) except Exception: pass
def home(): page_nr = int(request.args.get('p', '0')) # page count page_size = 15 recommends = models.select(rc for rc in models.Recommend \ if rc.checked)[page_nr*page_size:page_size] return render_template('explore.html', recommends=recommends)
def masterswitch(): try: setvalue = int(request.args.get('id', 2)) # 获取GET参数,没有参数就赋值 0 except ValueError: abort(404) # 返回 404 if not(setvalue in [0,1,2,3]): return '参数错误' if request.method == 'GET': if tools.getlockstate(config_path) == 1: state = "不允许上传" else: state = "允许上传" if setvalue == 2:#显示当前状态 if request.remote_addr == '10.246.14.83': return render_template('masterswitch.html',\ state1 = state,\ locked = str(1)) return render_template('masterswitch.html',\ state1 = state,\ locked = str(tools.getlockstate(config_path))) elif setvalue in [0,1]:#更改当前状态 tools.changelockstate(config_path,setvalue) return redirect(url_for('masterswitch',\ id = 2), 302) elif setvalue == 3:#清空数据库 alldata = boxes = models.select(b for b in models.Svndb)[:] for somedata in alldata: somedata.delete() return 'all data delete' else: return 'error par' elif request.method == 'POST': from werkzeug.datastructures import ImmutableMultiDict if tools.getlockstate(config_path) == 1: state = "不允许上传" else: state = "允许上传" templist = copy.deepcopy(request.form) sss = copy.copy(str(templist['path'])) print type(sss) sss = tools.ChangeCoding(sss) print sss,111111111111111111 #templist['path'] = chiesestr #print templist['path'] if not lockroute: return render_template('masterswitch.html',\ state1 = state,\ locked = str(tools.getlockstate(config_path))) else: for locked in lockroute: print locked reroute = re.compile(locked) print reroute.search(sss) if reroute.search(sss): if tools.getlockstate(config_path) == 1: state = 0 else: state = 1 return render_template('masterswitch.html',\ state1 = state,\ locked = str(tools.getlockstate(config_path))) return render_template('masterswitch.html',\ state1 = state,\ locked = str(0)) '''
def person_query(): query = request.args['query'] people = select(p for p in Person if query in p.name) people = [p.to_dict() for p in people] return jsonify(results=people)
def all_roles(): return select(r for r in Role).order_by(Role.name)
def wrap(*args, **kwargs): kind = None # Grab the session cookie if it exists. cookie = request.cookies.get(current_app.session_cookie_name, None) # Try get the Auth header data. try: auth = request.headers['Authorization'] kind, _, value = auth.partition(' ') if kind == 'Basic': value = base64.standard_b64decode(bytes(value, 'utf8')) id, _, pw = str(value, 'utf8').partition(':') elif kind == 'Google' or kind == 'Facebook': xtra = request.headers['X-Requested-With'] # elif kind == 'Token': except (KeyError, base64.binascii.Error) as ex: # print(type(ex)) return fn(*args, **kwargs) # If there was an Auth header, autheticate with that info, # and create a session. if kind == 'Basic': with db_session: user = select(u for u in User if u.email == id or u.username == id)[:] if len(user) == 1: if Password.verify(pw, user[0].password): sessionize(user=user[0].to_dict()) else: session.clear() elif not user: with db_session: user = User(email=id, password=pw) sessionize(user=user.to_dict()) else: session.clear() elif kind in ('Google', 'Facebook') and xtra == 'Fetch': kind = kind.lower() sec = client_secrets[kind]['web'] sec['provider'] = kind sec['token'] = value try: value = oauth.upgrade_token(**sec) ouser = oauth.get_user(provider=kind, **value) print(ouser) with db_session: user_oauth = select(o for o in OAuth if o.puid == ouser['id'])[:] if len(user_oauth) == 1: print(user_oauth[0].user) user = user_oauth[0].user.to_dict( ('password', 'oauth')) try: user['picture'] =\ ouser['picture']['data']['url'] except TypeError as ex: user['picture'] = ouser.get('picture', '') sessionize(user=user) elif not user_oauth: # with db_session: user = User(name=ouser.get('name')) user_oauth = OAuth( provider=kind, puid=ouser.get('id'), access_token=value.get('access_token', ''), refresh_token=value.get('refresh_token', ''), user=user) commit() user = user.to_dict(('password', 'oauth')) try: user['picture'] =\ ouser['picture']['data']['url'] except TypeError as ex: user['picture'] = ouser.get('picture', '') sessionize(user=user) except oauth.HTTPError as ex: abort(make_response(ex.text, ex.status_code)) elif kind is not None: # An unknown kind or kind 'None' session.clear() return fn(*args, **kwargs)
def list_category(): cs = [] for cg in models.select(c for c in models.Category)[:]: cs.append(cg.name) return json.dumps(cs)
import models que = Queue.Queue() #notify = Queue.Queue() #def loopwraper(fn): # def _loopfn(): # while True: # fn() # t = threading.Thread(target=_loopfn) # t.daemon = True # t.start() # return fn with models.db_session: jobs = models.select(b for b in models.Job if \ b.status == 'pending')[:] for b in jobs: b.status = 'initing' #@loopwraper #@models.db_session #def loop_task(): # jobs = models.select(b for b in models.Job if b.status == 'initing')[:] # for b in jobs: # b.status = 'pending' # b.updated = datetime.datetime.today() # models.commit() # que.put(b.id) # msg = notify.get() #@loopwraper
def view_ja(id): """ 参照画面 """ con = get_db() result = models.select(con, id) return render_template('view_ja.html', result=result)
def repolist(): goos = request.args.get('os', 'windows') goarch = request.args.get('arch', 'amd64') data = [] for r in models.select(r for r in models.Recommend)[:]: item = dict( reponame=r.repo.name, alias=r.name, author=r.repo.author, description=r.repo.description, offical=r.repo.offcial, category=r.category.name if r.category else None, stars=r.repo.stars, ) files = [] for b in r.repo.builds: if not b.downloadable: continue # actually only one loop file = {'label': b.tag, 'updated': b.updated} for f in models.select(f for f in models.File \ if f.build==b and f.os == goos and f.arch == goarch)[:1]: file.update({ 'binfiles': [os.path.basename(f.reponame) ], # FIXME: need to parse from gobuildrc 'size': f.size, 'url': f.outlink, 'sha1': f.sha }) files.append(file) if files: item['files'] = files data.append(item) dict( reponame='github.com/codeskyblue/cgotest', description='this is is just a test program', alias='cgotest', # this could be null author='unknown,lunny', offical=True, category='music', stars=18, files=[{ 'label': 'branch:master', 'url': 'http://gobuild3.qiniudn.com/github.com/gogits/gogs/branch-v-master/gogs-linux-386.tar.gz', 'binfiles': ['gogs'], 'sha1': '408eebced1c2cdbd363df2fe843831bf337d4273', 'size': 7000000 }, { 'label': 'tag:v0.5.2', 'url': 'http://gobuild3.qiniudn.com/github.com/gogits/gogs/tag-v-v0.5.2/gogs-linux-386.tar.gz', 'binfiles': ['gogs'], 'sha1': '960e329d46ec7a79745cf3438eaf3c3151d38d97', 'size': 7100000 }], ) return flask.jsonify({ 'status': 0, 'message': 'success', 'osarch': goos + '-' + goarch, 'data': data })
def show_bugs(): alldatalist = models.select(b for b in models.Bugs )[:] return render_template('bugs.html',bugs = alldatalist)
def clear_bugs(): alldatalist = models.select(b for b in models.Bugs )[:] for bug in alldatalist: bug.delete() return 'succeed' #return 'welcome to H20 QAtools'''''
def view(pk): """ 結果参照処理 """ con = get_db() result = models.select(con, pk) return render_template('view.html', result=result)
def svnview(pageindex): alldatalist = models.select(b for b in models.Svndb )[:] print len(alldatalist),'length' totalpage = (len(alldatalist)+maxline-1)/maxline alldatalist = alldatalist index = (int(pageindex)-1)*10 listview = [] cnt = 0 cntindex = index while cntindex < len(alldatalist) and cnt < maxline: dicta={} dicta['author'] = alldatalist[cntindex].author dicta['creattime'] = alldatalist[cntindex].creattime chiesestr = tools.ChangeCoding(alldatalist[cntindex].change) dicta['change'] = tools.getchlist(chiesestr.encode('utf8')) dicta['change'] = '<br/>'.join(dicta['change']) chiesestr = tools.ChangeCoding(alldatalist[cntindex].log) dicta['log'] = tools.getchlist(chiesestr.encode('utf8')) dicta['log'] = '<br/>'.join(dicta['log']) #dicta['change'] = alldatalist[cntindex].change dicta['md5'] = alldatalist[cntindex].md5 #print dicta['md5'].encode('gbk') lockstate = "" if alldatalist[cntindex].lock_flag == '0': lockstate = "允许提交" elif alldatalist[cntindex].lock_flag == '1': lockstate = "不允许提交" else: lockstate = "未审查" dicta['lock_flag'] = lockstate dicta['index'] = cntindex listview.append(dicta) cntindex = cntindex + 1 cnt = cnt + 1 if request.method == 'GET': return render_template('svnlock.html',\ posts1 = listview, \ posts2 = "",\ now = int(pageindex), total = totalpage) elif request.method == 'POST': #from werkzeug.datastructures import ImmutableMultiDict templist = request.form if templist.has_key('next'): #下一页 gotopage = 0 if int(templist['nowpage']) < totalpage: gotopage = int(templist['nowpage']) + 1 else: gotopage = int(templist['nowpage']) return redirect('/svnlock/svnview'+'/'+str(gotopage)) elif templist.has_key('previous'): #上一页 gotopage = 0 if int(templist['nowpage']) > 1: gotopage = int(templist['nowpage']) -1 else: gotopage = int(templist['nowpage']) return redirect('/svnlock/svnview'+'/'+str(gotopage)) elif templist.has_key('submitchange'): #修改状态 state = templist['changestate'] gotopage = templist['nowpage'] if templist.has_key('users'): userindexlst = templist.getlist('users') for userindex in userindexlst: #alldatalist = models.select(S for S in models.Svndb if S.change == templist['change'] and S.md5 == templist['md5'])[:] if state == '2': alldatalist[int(userindex)].delete() else: alldatalist[int(userindex)].lock_flag=state print alldatalist[int(userindex)].lock_flag , alldatalist[int(userindex)].change return redirect('/svnlock/svnview'+'/'+gotopage)
def train(directory: Text, model_name: Text, batch_size: int, learning_rate: float, epsilon: float, cuda: bool, train_with_test: bool, min_iteration: int, max_iteration: int, ndrop: Optional[float] = None, **kwargs) -> None: # filter out options that are not set in command line kwargs = util.dict_filter(kwargs, lambda k, v: v is not None) data_folder = Path(directory) assert data_folder.is_dir(), 'Invalid data folder' dev = require_device(cuda) for fold in sorted(data_folder.iterdir()): log.info(f'Processing "{fold}"...') # model & optimizer model_type = models.select(model_name) # see models/__init__.py model = ModelInterface(model_type, dev, **kwargs) optimizer = torch.optim.Adam(params=model.inst.parameters(), lr=learning_rate) # load the fold raw = [ util.load_csv(fold / name) for name in ['train.csv', 'test.csv', 'dev.csv'] ] # let the model parse these molecules data = [] for i in range(len(raw)): buf = [] for smiles, activity in raw[i].items(): obj = model.process(smiles) buf.append(Item(obj, activity)) data.append(buf) log.debug(f'atom_map: {model.atom_map}') test_batch, _test_label = util.separate_items(data[1]) test_label = torch.tensor(_test_label) # training phase train_data = data[0] + data[1] if train_with_test else data[0] # set up to randomly drop negative samples # see util.RandomIterator for details drop_prob = ndrop if ndrop is not None else 0 drop_fn = lambda x: drop_prob if x.activity == 0 else 0 data_ptr = util.RandomIterator( train_data, drop_fn=drop_fn if ndrop is not None else None) countdown = min_iteration min_loss = 1e99 # track history minimal loss sum_loss, batch_cnt = 0.0, 0 for _ in range(max_iteration): # generate batch batch, _label = util.separate_items(data_ptr.iterate(batch_size)) label = torch.tensor(_label) # train a mini-batch batch_loss = train_step(model, optimizer, batch, label) sum_loss += batch_loss batch_cnt += 1 # log.debug(f'{batch_loss}, {sum_loss}') # convergence test if data_ptr.is_cycled(): loss = sum_loss / batch_cnt pred = model.predict(test_batch) log.debug( f'{util.stat_string(_test_label, pred)}. loss={loss},min={min_loss}' ) if countdown <= 0 and abs(min_loss - loss) < epsilon: log.debug('Converged.') break countdown -= 1 min_loss = min(min_loss, loss) sum_loss, batch_cnt = 0.0, 0 # model evaluation on `dev.csv` roc_auc, prc_auc = evaluate_model(model, data[2]) log.info(f'ROC-AUC: {roc_auc}') log.info(f'PRC-AUC: {prc_auc}')