def test_getitems(self): self.assertEqual(t.get_items(self.DATA, ['aa']), ['1']) self.assertEqual(t.get_items(self.DATA, ['aa', 'bb'], type_=int), [1, 1]) self.assertEqual( t.get_items(self.DATA, ['aa', 'dd.aa', 'dd.bb.cc.ee.ff']), ['1', '2', '4'])
def _problem_check_server(self, item): self._update_course(item) (problem_id, user_id, time) = get_items(item, ['event.problem_id', 'context.user_id', 'time']) subtasks = get_item(item, 'event.submission', type_=dict) for (subtask_id, subtask) in subtasks.items(): (question, task_type) = get_items(subtask, ['question', 'response_type']) correct = get_item(subtask, 'correct', type_=bool) self.tasks.add_task(problem_id, subtask_id, question, task_type) self.users.score_task(user_id, problem_id, subtask_id, correct, time)
def load(self, it): logger.info("[*] Loading checkpoint ...") if it > 0: filename = self.file_prefix + '_' + str(it) + '.npz' else: filename = self.file_prefix + '.npz' filename = os.path.join(self.checkpoint_dir, filename) if not os.path.exists(filename): logger.info('[*] checkpoint not found') return 0 outdict = torch.load(filename) it = outdict['it'] for n, (o, m) in utils.get_items(self.modules): if n in outdict: o.load_state_dict(outdict[n]) else: if m: raise RuntimeError('Cannot load mandatory object') logger.info("Cannot load object: \'" + n + "\'") logger.info('[*] checkpoint loaded') return it
def write(self, fd, items_info, intervals): items = utils.get_items(intervals) self.__write_header(fd, global_xmin=intervals[0][settings.INTERVAL_KEY_XMIN], global_xmax=intervals[-1][settings.INTERVAL_KEY_XMAX], items_size=len(items)) for i, item_info in enumerate(items_info, start=0): self.__write_item(fd, **item_info) for interval in items[i]: self.__write_interval(fd, **interval)
def items(request): if not request.is_ajax(): return HttpResponseBadRequest() loc = request.GET.get('loc', '') opts = json.loads(request.GET.get('opts', '{}')) zotero_key = request.user.get_profile().zotero_key items = utils.get_items(zotero_key, loc, opts) return HttpResponse(json.dumps(items), mimetype='application/json')
def clickHandler(dom): """处理菜单点击事件""" cate = dom.find('EventKey').text context=get_context(dom) if 0<=EventKeyNo[cate]<=11: context['items']=get_items(EventKeyNo[cate]) return render_template('show_items.html',**context) else: context["content"]=u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html',**context)
def _create_submission(self, item): self._update_course(item) (submission_id, task_id, user_id, name, page) = get_items(item, [ 'event.submission_uuid', 'context.module.usage_key', 'context.user_id', 'context.module.display_name', 'referer' ]) self.users.create_submission(submission_id, user_id, task_id) self.modules.add_task(page, task_id) self.tasks.add_assessment(task_id, name)
def clickHandler(dom): """处理菜单点击事件""" cate = dom.find('EventKey').text context = get_context(dom) if 0 <= EventKeyNo[cate] <= 11: context['items'] = get_items(EventKeyNo[cate]) return render_template('show_items.html', **context) else: context["content"] = u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html', **context)
def config_hosts(vc, services_str, ntp=None, licensekey=None, fw_rule=None): # Config All Hosts services = utils.get_items(services_str) rules = utils.get_items(fw_rule) all_hosts = vc.get_hosts() for host in all_hosts: if ntp: host.enable_ntp(ntp=ntp) if licensekey: host.add_license(license_key=licensekey) for service in services: if service == 'ssh': host.enable_ssh() continue if service == 'vmotion': # Config default vMotion network host.config_vmotion() continue for rule in rules: host.config_firewall(rule)
def remove_dc_datastore(vc, dc_name, datastores): dc = vc.get_datacenter_by_name(dc_name) if dc is None: print 'Data Center {} not found on VC'.format(dc_name) return target_names = utils.get_items(datastores) ds_names = [ds.name() for ds in dc.get_datastores()] target_names = [ds_name for ds_name in target_names if ds_name in ds_names] for ds_name in target_names: ds = dc.get_datastore_by_name(ds_name) for host in ds.get_hosts(): host.remove_datastore(ds)
def _assess_submission(self, item): self._update_course(item) (submission_id, user_id) = get_items(item, ['event.submission_uuid', 'context.user_id']) scores = get_item(item, 'event.parts', type_=list) points = sum( get_item(score, 'option.points', type_=int) for score in scores) max_points = sum( get_item(score, 'criterion.points_possible', type_=int) for score in scores) self.users.assess(submission_id, user_id, points, max_points)
def remove_host_datastore(vc, host_name, datastores): host = vc.get_host_by_name(host_name) if host is None: print 'Host {} not exist in VC'.format(host_name) return dss = utils.get_items(datastores) dss_names = [ds.name() for ds in host.get_datastores()] for ds_name in dss: if ds_name not in dss_names: print 'Datastore {} not on host {}'.format(ds_name, host_name) else: ds = host.get_datastore_by_name(ds_name) host.remove_datastore(ds)
def items_json(): ''' JSON endpoint for retrieving the full list of items in the catalog ''' category_id = request.args.get('category_id') db.connect() items, cat = get_items(db.session, category_id=category_id) db.disconnect() return jsonify({ 'category': cat.serialize() if cat else None, 'items': [x.serialize() for x in items] })
def items_page(): ''' The main item catalog page ''' category_id = request.args.get('category_id') db.connect() item_categories = get_item_categories(db.session) items, cat = get_items(db.session, category_id=category_id) db.disconnect() return render_template('page-items.html', title=('Items - {}'.format(cat.name) if cat is not None else 'All Items'), item_categories=item_categories, items=items)
def validate(self, intervals): def call_plugin(interval): for plugin in self.plugins: plugin(self, interval) items = utils.get_items(intervals) if self.layer == 0: # to modify elem in the list instead of refering for i in range(len(items)): call_plugin(items[i]) else: call_plugin(items[self.layer - 1]) self.qualified = utils.flat_items(items) return self
def validate(self, intervals): def call_plugin(interval): for plugin in self.plugins: plugin(self, interval) items = utils.get_items(intervals) if self.layer == 0: # to modify elem in the list instead of refering for i in range(len(items)): call_plugin(items[i]) else: call_plugin(items[self.layer-1]) self.qualified = utils.flat_items(items) return self
def _lr_warmup_schedule(self, epoch): schedule = {0: 0.1, 100: 0.25, 200: 0.5, 300: 0.75, 400: 1.} best_step = None lr_mult = 0. for s, lr in utils.get_items(schedule): if s > epoch: continue if best_step is not None and s < best_step: continue best_step = s lr_mult = lr return lr_mult
def add_item_bid(): item_id = request.json.get('item_id', 0) bid_amount = request.json.get('bid_amount', 0) user_id = request.json.get('user_id', 0) # do something usefull with these data items = get_items() for i in items: if i['id'] == item_id: i['bids'].append({ 'user_id': user_id, 'amount': bid_amount, 'id': 5 }) update_file(items) return Response(dumps({'msg': 'Created'}), status=201) return Response(dumps({'msg': 'This item does not exists'}), status=404)
def doc_new(request): context={} context['ouraddress'] = OurAddress.objects.all()[0] context['instruction'] = Instruction.objects.get(name__contains="invoice creation") context['dtype'] = 'i'#r['dtype'].split("|")[0] if request.POST: r = request.POST try: a = r['select'] c = Contact.objects.get(pk=r['contact'].split("|")[0]) itpk = r['itype'].split("|")[0] it = InvoiceType.objects.get(pk=itpk) i = get_items(c,it) m = InvoiceMeta.objects.filter(invoice=i) context['date'] = datetime.datetime.now() context['reference'] = make_inv_ref(c, get_inv_num(c)) context['items'] = i context['inv_num'] = get_inv_num(c) context['itype'] = it try: context['box_hdr'] = m.get(choice__name='box_hdr') except: pass template = ['inv/invoice.html'] context['contact'] = c #context['request'] = r except: a = r['reference'] try: #return doc_view(request, ) slug = '/doc/%s/' % context['invoice'].ref return HttpResponseRedirect(slug) except: doc_publish(r,context) #return doc_view(request, context['invoice'].ref) slug = '/doc/%s/' % context['invoice'].ref return HttpResponseRedirect(slug) else: context['contacts0'] = Contact.objects.filter(live=1).filter(client__pk=1).order_by('id') context['contacts'] = Contact.objects.filter(live=1).exclude(client__pk=1).order_by('client', 'name') context['types'] = InvoiceType.objects.all() context['clients'] = Client.objects.filter(live=1).order_by('name') template = ['inv/select.html'] return render_to_response(template, context_instance=RequestContext(request, context))
def textMessageHandler(cls, dom): """处理text事件""" context = get_context(dom) content = dom.find('Content').text if not content.isdigit(): if content == u'是': context['content'] = u"感谢您的参与,敬请期待新功能上线~~" return render_template('welcome.html', **context) elif content == u'否': context["content"] = u"感谢您的参与,谢谢~~" return render_template('welcome.html', **context) else: context["content"] = u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html', **context) if 0 <= int(content) <= 11: context['items'] = get_items(int(content)) return render_template('show_items.html', **context) else: context["content"] = u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html', **context)
def vmotion(dc, vm_reg, host_name, ds_name, concurrency=10): host = dc.get_host_by_name(host_name) ds = dc.get_datastore_by_name(ds_name) if host is None: print 'Host {} not exist on DC.'.format(host_name) exit(1) if ds is None: print 'Datastore {} not exist on DC.'.format(ds_name) exit(1) vms = dc.get_vms_by_regex(utils.get_items(vm_reg)) vm_num = len(vms) import threadpool size = concurrency if concurrency < vm_num else vm_num pool = threadpool.ThreadPool(size) reqs = [threadpool.WorkRequest(call_func, args=(vm, 'migrate', (host, ds))) for vm in vms] for req in reqs: pool.putRequest(req) pool.wait() exit(0)
def textMessageHandler(cls,dom): """处理text事件""" context=get_context(dom) content = dom.find('Content').text if not content.isdigit(): if content==u'是': context['content']=u"感谢您的参与,敬请期待新功能上线~~" return render_template('welcome.html',**context) elif content==u'否': context["content"]=u"感谢您的参与,谢谢~~" return render_template('welcome.html',**context) else: context["content"]=u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html',**context) if 0<=int(content)<=11: context['items']=get_items(int(content)) return render_template('show_items.html',**context) else: context["content"]=u"回复对应的编号可查询最新的相关网购促销信息:" return render_template('erro.html',**context)
def vmotion(dc, vm_reg, host_name, ds_name, concurrency=10): host = dc.get_host_by_name(host_name) ds = dc.get_datastore_by_name(ds_name) if host is None: print 'Host {} not exist on DC.'.format(host_name) exit(1) if ds is None: print 'Datastore {} not exist on DC.'.format(ds_name) exit(1) vms = dc.get_vms_by_regex(utils.get_items(vm_reg)) vm_num = len(vms) import threadpool size = concurrency if concurrency < vm_num else vm_num pool = threadpool.ThreadPool(size) reqs = [ threadpool.WorkRequest(call_func, args=(vm, 'migrate', (host, ds))) for vm in vms ] for req in reqs: pool.putRequest(req) pool.wait() exit(0)
def save(self, it): logger.info("[*] Saving checkpoint ...") if it > 0: filename = self.file_prefix + '_' + str(it) + '.npz' latest_filename = self.file_prefix + '.npz' else: filename = self.file_prefix + '.npz' latest_filename = None filename = os.path.join(self.checkpoint_dir, filename) outdict = {'it':it} for n, (o, m) in utils.get_items(self.modules): outdict[n] = o.state_dict() torch.save(outdict, filename) if latest_filename is not None: latest_filename = os.path.join(self.checkpoint_dir, latest_filename) shutil.copyfile(filename, latest_filename) logger.info("[*] Saving checkpoint SUCCESS!")
def _problem_submitted(self, item): self._update_course(item) (user_id, problem_id, page, time) = get_items( item, ['context.user_id', 'event.problem_id', 'referer', 'time']) self.modules.add_task(page, problem_id) self.users.post_solution(user_id, problem_id, convert_datetime(time))
def get_item_bids(item_id): for i in get_items(): if i['id'] == item_id: return dumps(i['bids']) return Response(dumps({'msg': 'This item does not exists'}), status=404)
def get_items_api(): return dumps(get_items())
def arg_hook(self, args): for key, value in utils.get_items(args): args[key] = self.convert_value(value) return args
def detect_entities(text, data_dict): entities = dict() entities['store'] = get_store_name_from_text(text) entities['items'] = get_items(text, data_dict) return entities