def categories_edit(category_id): try: category = Category.find_by_id(category_id) if category: if request.method == 'GET': category_dict = category.__dict__ form = Form(category_dict, form_fields) return render_template('category/edit.html', form=form, category_id=category_id, success=True) elif request.method == 'POST': form = Form(request.form, form_fields) if form.validate(): values = form.named_values() category.from_dict(values) category.save() miner = miner_cls(category) miners[category_id] = miner miner.start() return redirect(url_for('categories')) else: return render_template('category/edit.html', form=form, category_id=category_id, success=True) elif request.method == 'DELETE': miner = miners[category_id] miner.stop() del miners[category_id] Category.delete(category_id) return 'Category {} deleted.'.format(category_id), 200 else: return 'Unsupported request method.', 400 else: return 'Category {} not found.'.format(category_id), 400 except ValueError, e: print e return '{} is not a valid category id.'.format(category_id), 400
def _add_category(self, cate_name, post): cate = self.categories.get(cate_name) if not cate: url = os.path.join('categories', cate_name) cate = Category(self, cate_name, url) self.categories[cate_name] = cate cate.add_post(post)
def test_basic(self): name = 'music' c = Category(name) self.assertEqual(name, c.name) self.assertEqual(0, len(c.descriptions)) self.assertEqual(0, c.fn(1))
def load_categories(self): for filename in os.listdir(self.database): category_name = os.path.splitext(filename)[0] category = Category(self.database, category_name, self.stop_list) if category.load(): self.total_articles += category.article_count self.categories[category_name] = category
def category_relationships(): response = None if request.method == 'POST': response = Category.create_relationships(id, related_collection_name, eval(request.data)) elif request.method == 'PATCH': response = Category.update_relationship(id, related_collection_name, json.loads(request.data)) elif request.method == 'DELETE': response = Category.disconnect_relationship(id, related_collection_name, eval(request.data)) elif request.method == 'GET': response = Category.get_relationship(request.args, id, related_collection_name) return response
def new_category(): if request.method == "GET": return render_template("new_category.html") elif request.method == "POST": category = Category(None, request.form["name"]) category.create() logging.info('%s with id: %s added new categoty %s named %s', User.find_by_id(session['USERNAME']), session['USERNAME'], category.id, category.name) return redirect("/categories")
def get_category(self, id, name=None): """ Returns a Category for the passed ID, associated with this API object. """ id = int(id) if id in self.cached_categories.keys(): cat = self.cached_categories[id] else: cat = Category(id, self) if name: cat.name = name return cat
def new_category(): print("-"*30) print("Please Enter New Category Name : ") new_cat = input("Category : ") ref_var = db.reference('Category').order_by_child('category_name').equal_to(new_cat).get() if ref_var: return False category_object = Category() new_category_id = category_object.add_category(new_cat) return new_cat
def edit_post(id): post = Post.find(id) if request.method == 'GET': return render_template('edit_post.html', post=post, categories=Category.all()) elif request.method == 'POST': post.name = request.form['name'] post.author = request.form['author'] post.content = request.form['content'] post.category = Category.find(request.form['category_id']) post.save() return redirect(url_for('show_post', id=post.id))
def creation(): """ create all the tables by calling static method from these tables six tables including two association tables: - CategoryProduct and StoreProduct .""" with Connection.get_instance() as cursor: sql = """ CREATE DATABASE IF NOT EXISTS openfoodbase CHARACTER SET utf8; """ cursor.execute(sql) Category.create() Product.create() Store.create() CategoryProduct.create() StoreProduct.create() Historic.create()
def add_filter(): """ nie kasowac tej funkcji !!!! :return: """ """ manual filters """ channelgroup = FilterF("channelgroup", 30, (0, 17), 'manual') brand_final = FilterF('brand_final', 37, [11], 'manual') #dane z kolumn brand final brand_type = FilterF('brand_type', 39, [37], 'manual') segment_detailed = FilterF('segment_detailed', 40, [38], 'manual') segment = FilterF('segment', 41, [40], 'manual') segment_combined = FilterF('segment_combined', 42, [40], 'manual') campaign_type = FilterF('campaign_type', 43, [13], 'manual') """ words """ wyprz = FilterF('WYPRZ', 32, [13], 'words') wyprz_cat = Category('WYPZ', words=['wyprz']) wyprz.categorys.append(wyprz_cat) upus = FilterF('UPUS', 33, [13], 'words') upus_cat = Category('UPUS', words=['upus']) upus.categorys.append(upus_cat) rabat = FilterF('RABAT', 34, [13], 'words') rabat_cat = Category('RABAT', words=['rabat ']) rabat.categorys.append(rabat_cat) wy_up_rab = FilterF('WY_UP_RAB', 35, [32, 33, 36], 'words') wy_up_rab_cat = Category('WY_UP_RAB', words=['wyprz', 'upus', 'rabat']) wy_up_rab.categorys.append(wy_up_rab_cat) """ cut filter """ # Brand 'Sub Brand' obcina pierwsze słowo z slownika modelf = FilterF('model', 36, [11, 12], 'cut') # Brand 'Sub Brand' #Subbrand (brand+model) # Useing Model, Brand Final; conectinc word from both cells subbrand_brand_model = FilterF('subbrand_brand_model', 38, [37, 36], 'cut') session.add_all([ channelgroup, brand_final, wyprz, upus, rabat, wy_up_rab, modelf, subbrand_brand_model, brand_type, segment_detailed, segment, segment_combined, campaign_type ])
def categories(): if request.method == "GET": # return an html list of all the categories return render_template("category/index.html", categories=Category.all(), success=True) elif request.method == "POST": # create new category based on the posted id params = request.get_json() new_category_id = int(params["id"]) fields = {key: "" for key, value in form_fields.iteritems()} new_category = Category(new_category_id, fields) new_category.save() return "OK", 200 else: return "error", 400
def category_append(self, category, description=None, parent=None, slug=None, ordering=99999): category = Category(name=category, description=description, slug=slug, ordering=ordering) # get parent if parent is not None: for cat in self.categories: if isinstance(parent, int) and cat.id == parent: category.parent = cat break elif isinstance(parent, str) and cat.name == parent: category.parent = cat else: category.parent = None if category not in self.categories: self.categories.append(category)
def save(self, name=None, price=None, category=None, off=None): ctg = Category().exits(category) if ctg: g = { "id": next(Product.__id), "name": name, "price": int(price), "category_id": ctg.get("id"), "off": float(off) } self.__db.append(g) return True else: return False
def recognize(img, category, surf, centroids, linear_clf, ids): keypoints, descriptors = surf.detectAndCompute(img, None) category = Category(label=category) category.add_feature(descriptors) category.calc_bagofwords(centroids) bow = category.bagofwords[0] id = int(linear_clf.predict(bow)[0]) for i in ids: if ids[i] == id: label = i print category.label, ":", label del category
def build_default_template(rule_pattern, ccg_tree): category = rule_pattern.category if len(ccg_tree) == 0: num_arguments = category.get_num_args() elif len(ccg_tree) == 1: category2 = Category(ccg_tree.get('category')) num_arguments = category.get_num_args() - category2.get_num_args() variable_names = ['x' + str(i) for i in range(num_arguments)] if not variable_names: template_string = r'\P.P' else: template_string = r'\E O.O' template = lexpr(template_string) return template
def __init__(self, category, semantics, attributes={}): if not isinstance(category, Category): self.category = Category(category) else: self.category = category if semantics and not isinstance(semantics, Expression): self.semantics = lexpr(semantics) else: self.semantics = semantics self.attributes = copy.deepcopy(attributes) if 'surf' in self.attributes: self.attributes['surf'] = normalize_token(self.attributes['surf']) if 'base' in self.attributes: self.attributes['base'] = normalize_token(self.attributes['base'])
def new_post(): if request.method == 'GET': return render_template('new_post.html', categories=Category.all()) elif request.method == 'POST': categ = Category.find(request.form['category_id']) values = ( None, request.form['name'], request.form['author'], request.form['content'], ) Post(*values).create() return redirect('/')
def categories(): if request.method == 'GET': # return an html list of all the categories return render_template('category/index.html', categories=Category.all(), success=True) elif request.method == 'POST': # create new category based on the posted id params = request.get_json() new_category_id = int(params['id']) fields = {key: "" for key, value in form_fields.iteritems()} new_category = Category(new_category_id, fields) new_category.save() return 'OK', 200 else: return 'error', 400
def build_default_template(rule_pattern, ccg_tree): category = rule_pattern.category if len(ccg_tree) == 0: num_arguments = category.get_num_args() elif len(ccg_tree) == 1: category2 = Category(ccg_tree.get('category')) num_arguments = category.get_num_args() - category2.get_num_args() variable_names = ['x' + str(i) for i in range(num_arguments)] if not variable_names: template_string = r'\P.P' else: template_string = r'\P ' + ' '.join(variable_names) \ + '.P(' + ', '.join(reversed(variable_names)) + ')' template = lexpr(template_string) return template
def learn(self): # for each directory in the document root... for category_name in os.listdir(self.docroot): category_path = os.path.join(self.docroot, category_name) # if the category is a file (not a directory), then skip it if os.path.isfile(category_path): continue category = Category(self.database, category_name, self.stop_list) self.categories[category_name] = category # call the function to handle the directory/category category.categorize(category_path) category.save()
def getvalue(self, value): print(value) print(type(value)) if value == 1: pass elif value == 2: self.hide() view = Employee(self) elif value == 3: self.hide() view = Table(self) elif value == 4: self.hide() view = Reservations(self) elif value == 5: self.hide() view = Category(self) elif value == 6: self.hide() view = Settings(self) elif value == 7: self.hide() view = Orders(self) elif value == 8: self.hide() view = Menu(self) elif value == 9: self.hide() view = Bill(self)
def output_cloze_import(infinitive, tense, translation, sound, conj): ''' Combines the output of the output_cloze function with optional translation and sound fields and combines them to produce the format required for Anki's import function ''' cloze = output_cloze(infinitive, tense, conj) if translation: add_trn = [ cz + ('|{}'.format(trn)) for cz, trn in zip(cloze, translation) ] else: add_trn = [cz + '|' for cz in cloze] if sound: add_snd = [ trn + ('|[sound:{}]'.format(snd)) for trn, snd in zip(add_trn, sound) ] else: add_snd = [trn + '|' for trn in add_trn] add_tag = [snd + ('|{}'.format(infinitive)) for snd in add_snd] return Category._make(add_tag)
def test_cloze_import(self): expected = Category._make([ 'je {{c1::parle::parler, présent}}|I speak|[sound:je parle.mp3]|parler', 'tu {{c1::parles::parler, présent}}|you speak|[sound:tu parles.mp3]|parler', 'il/elle/on {{c1::parle::parler, présent}}|he/she/it speaks|[sound:il parle.mp3]|parler', 'nous {{c1::parlons::parler, présent}}|we speak|[sound:nous parlons.mp3]|parler', 'vous {{c1::parlez::parler, présent}}|you speak|[sound:vous parlez.mp3]|parler', 'ils/elles {{c1::parlent::parler, présent}}|they speak|[sound:ils parlent.mp3]|parler' ]) translation = [ 'I speak', 'you speak', 'he/she/it speaks', 'we speak', 'you speak', 'they speak' ] sound = [ 'je parle.mp3', 'tu parles.mp3', 'il parle.mp3', 'nous parlons.mp3', 'vous parlez.mp3', 'ils parlent.mp3' ] conj = French.construct_inflection('parler', 'présent') actual = French.output_cloze_import('parler', 'présent', translation, sound, conj) with self.subTest(): self.assertEqual(expected.fps, actual.fps) self.assertEqual(expected.sps, actual.sps) self.assertEqual(expected.tps, actual.tps) self.assertEqual(expected.fpp, actual.fpp) self.assertEqual(expected.spp, actual.spp) self.assertEqual(expected.tpp, actual.tpp)
def run(self): for node in self.nodes: time.sleep(1) self.progress += 1 if self.progress % 50 == 0: print(f'Progress: {self.progress} / {self.total_nodes}. Updated: {self.artists_updated}. Unupdated: {len(self.unupdated)}. Unlabeled: {len(self.unlabeled)}') self.save_tmp_file(self.graph, 'tmp-graph.pickle') self.save_tmp_file(self.get_report(), 'tmp-report.pickle') if str(node.wikidataID) == 'nan': continue wd = Wikidata(node.wikidataID) labels = wd.fetch() if labels == False: self.unupdated.add(node.id) elif len(labels) == 0: self.unlabeled.add(node.id) else: for label in labels: node.categories.add(label) if label not in self.graph.nodes: self.graph.add(Category(label)) self.categories_added.add(label) self.graph.add_edge(label, node) self.edges_added += 1 self.artists_updated += 1 return self.graph, self.get_report()
def __init__(self): self.hier = Hier() self.category = Category() self.dashuser = Dashuser() host = 'gnats.juniper.net' db = 'default' # username = os.getusername() ... for Linux username = getpass.getuser() #... for Windows and linux # Get a database object, which holds the metadata (field names, etc.) self.db_obj = gnats.get_database(host, db) # Get a db handle, a connection to the server self.db_handle = self.db_obj.get_handle(username, passwd='*')
def get_category(self, key): with dbapi2.connect(self.app.config['dsn']) as connection: cursor = connection.cursor() query = "SELECT TITLE,TYPEE FROM CATEGORY WHERE (ID = %s)" cursor.execute(query, (key,)) title,typee = cursor.fetchone() return Category(title,typee)
def __build_pair_xml(self, fields_dict): self.prem = fields_dict["prem"] self.conc = fields_dict["conc"] self.pcat = fields_dict["pcat"] self.mcat = Category(self.pcat) self.lex = fields_dict["lex"] self.projs = self.__build_projectivity_functions(fields_dict["proj"])
def get_encoder(type, **kargs): """ Creates an encoder of the appropriate type and returns an instance of it. @param type: The type of encoder to create. The supported types are: "unity", "threshold", "scalar", "category", and "multi". @param kargs: Any keyword arguments to pass to the encoder. @return: An encoder instance. @raise UnsupportedEncoder: Raised if the requested encoder is not supported. """ t = type.lower() if t == 'unity': return Unity(**kargs) elif t == 'threshold': return Threshold(**kargs) elif t == 'scalar': return Scalar(**kargs) elif t == 'multi': return Multi(**kargs) elif t == 'category': return Category(**kargs) else: raise UnsupportedEncoder(t)
def __read_categories(self): print 'Reading Categories...' df = pd.read_csv(self.categories_url, delimiter=',') category_names = list(df['Category']) self.categories = [Category(name=name, cat_id=ind) for ind, name in enumerate(category_names)] self.num_categories = len(self.categories)
def __init__(self): self.user = User() self.course = Course() self.category = Category() self.quiz = Quiz() self.announcement = Announcement() self.discussion = Discussion()
def create_category(): token = request.headers.get('Authorization') if token is None: return make_response( jsonify({ 'success': False, 'error': 'No Auth Token' }), 400) else: db = Database() user_id = AuthSystem.validate_token(token) query = {'_id': user_id} user = db.findUser(query) if user: category = request.json user = User.from_dict(user) user.addCategory(Category(category['name'], category['limit'])) updates = {'categories': user.as_dict()['categories']} did_update = db.updateUser(query, updates) if did_update: return make_response(jsonify({'success': True}), 201) else: return make_response( jsonify({ 'success': False, 'error': 'Failed to update User!' }), 304) else: return make_response( jsonify({ 'success': False, 'error': 'User not found!' }), 404)
def test_one_task_one_user_assign(self): start_a = datetime.datetime(2000, 1, 1, 6, 0, 0) end_a = datetime.datetime(2000, 1, 1, 22, 0, 0) availability = [Availability(Duration(start_a, end_a))] category = Category(1, 'TestCategory') preferences = [Preference(category, 0, True)] groups = [Group(1, 'Employee')] generic = CommonTestFunctions() staff_member = StaffMember(1, 'TestMember', 0, 40, 80, 10, availability, preferences, groups) start_t = datetime.datetime(2000, 1, 1, 9, 0, 0) end_t = datetime.datetime(2000, 1, 1, 16, 0, 0) task_time = Duration(start_t, end_t) location = Location(1, 'TestLocation') task = Task(1, 'TestTask', task_time, 1, groups[0], location, category) staff_members = [staff_member] roles = [] tasks = [task] settings = generic.settings(40, True) generator = ScheduleGenerator(staff_members, roles, tasks) generator.schedule(settings) self.assertTrue(staff_member.has_task(task)) self.assertFalse(generator.has_unassigned_tasks())
def find(user_product): """ used for finding substitute from selected product """ user_product.categories = Category.load_from_id(user_product.id) i = 1 substitute = list() cur = Database.createCursor() cur.execute(""" SELECT product_id FROM categories_products INNER JOIN categories ON categories_products.category_id = categories.id INNER JOIN products ON categories_products.product_id = products.id WHERE categories.name = %s AND products.nutriscore = 'a' limit 5""", (user_product.categories[0], )) for elt in cur.fetchall(): substituts_with_id = dict() substituts_with_id['id'] = i substituts_with_id['product'] = Product.get(elt[0]) substitute.append(substituts_with_id) i += 1 cur.close() return substitute
def check_node_existence(self, data): """Checks if the nodes for the relationship exists, if not creates new nodes.""" if data: permalink = Transform.to_str(data.get('Permalink')) label = Transform.to_str(data.get('PrimaryRole')) name = Transform.to_str(data.get('Name')) try: organization = Organization() organization.check_create_organization_node(label, permalink) category = Category() category.check_create_category_node(name) except Exception as e: pass
def test_categories(self): hashedpw = bcrypt.hashpw('test'.encode('ascii'), bcrypt.gensalt()) user = User(1, 'UnitTest', '*****@*****.**', hashedpw) user.addCategory(Category('cat', 50.00)) self.assertEqual(len(user.getUserCategories()), 2) user.removeCategory(1) self.assertEqual(len(user.getUserCategories()), 1)
def getAnalysisByYearMonthAndRecord(self, year, month, costType=0): responseStr = '' start = '%s-%s-01 00:00:00' % (year, month) if int(month) + 1 > 12: endYear = int(year) + 1 endMonth = 1 else: endYear = year endMonth = int(month) + 1 end = '%s-%s-01 00:00:00' % (endYear, endMonth) startStamp = time.mktime(time.strptime(start, '%Y-%m-%d %H:%M:%S')) endStamp = time.mktime(time.strptime(end, '%Y-%m-%d %H:%M:%S')) res = Analysis().getPercent(int(startStamp), int(endStamp), self.MAX_RECORD_LIMIT, costType) resRecord = AccountBookDB().getAllRecordByTime( startStamp, endStamp, self.MAX_RECORD_LIMIT, 'category_id,cost desc') for r in res: for record in resRecord: if len(record) < 6: continue if Category().getCategoryNameById(record[4]) != r['name']: continue responseStr += '%s,%.2f,%s,%s\n' % (record[6], record[2], record[5], r['name']) return responseStr
def _repr_object_names(self): r""" Return the name of the objects of this category. .. SEEALSO:: :meth:`Category._repr_object_names` EXAMPLES:: sage: Algebras(QQ)._repr_object_names() 'algebras over Rational Field' sage: Algebras(Fields())._repr_object_names() 'algebras over fields' sage: Algebras(GF(2).category())._repr_object_names() 'algebras over (finite fields and subquotients of monoids and quotients of semigroups)' """ base = self.__base if isinstance(base, Category): if isinstance(base, JoinCategory): name = '(' + ' and '.join( C._repr_object_names() for C in base.super_categories()) + ')' else: name = base._repr_object_names() else: name = base return Category._repr_object_names(self) + " over %s" % name
def load_data_from_web(self): service = build('sheets', 'v4', credentials=self.creds) # Call the Sheets API sheet = service.spreadsheets() sheet_metadata = sheet.get(spreadsheetId=self.SPREADSHEET_ID).execute() sheets = sheet_metadata.get("sheets", "") print("Reading", len(sheets), "sheets") for s in sheets: cat_name = s.get("properties", {}).get("title") self.categories.append(Category(cat_name)) # read this sheet result = sheet.values().get(spreadsheetId=self.SPREADSHEET_ID, range=cat_name + "!A:Z").execute() values = result.get('values', []) if not values: print('No data found.') else: for v in values: clue = v[0] for sol in v[1:]: self.categories[-1].add_word(Word(clue, sol))
def construct_compound_tense_subjunctive(infinitive, tense): past_participle = _construct_past_participle(infinitive) inflection = [("qu'" if f(aux)[0] in _VOWELS else 'que', f(aux), aux, past_participle) for f, aux in zip(_PRONOUNS, _COMPOUND_TENSE[tense])] return Category._make(inflection)
def preliminary_labeling(category_tree: Category, segs: List[List[str]]): """ TODO: 应支持自定义的预标注规则, 或读取预标注结果 遍历文档,根据初始关键词做预标注, 同时得到文档词频矩阵 :param category_tree: 分类树根节点 :param segs: 文档集分词结果 :return: 返回单词索引表({word: index})和文档词频矩阵(documents_size, vocab_size), type='csr_matrix' """ # 默认的token_pattern会过滤掉单字 cv = CountVectorizer(analyzer="word", max_df=0.8, min_df=0.00001, token_pattern=r"(?u)\b\w+\b") logging.info("初始化文档词频矩阵") document_vectors = cv.fit_transform([" ".join(seg) for seg in segs]) # csr_matrix vocabulary = cv.vocabulary_ logging.info("词典大小: {}".format(len(vocabulary))) logging.info("文档预标注") for i, seg in tqdm(enumerate(segs)): for word in seg: category = category_tree.find_category_by_word(word) if category is not None: category.add_document(i) break return vocabulary, document_vectors
def __init__(self, *args, **kwargs): self.__categories = kwargs.pop('categories') self.__filterOnlyWhenAllCategoriesMatch = \ kwargs.pop('filterOnlyWhenAllCategoriesMatch', False) for eventType in (self.__categories.addItemEventType(), self.__categories.removeItemEventType()): patterns.Publisher().registerObserver(self.onCategoryChanged, eventType=eventType, eventSource=self.__categories) eventTypes = (Category.categorizableAddedEventType(), Category.categorizableRemovedEventType(), Category.filterChangedEventType()) for eventType in eventTypes: patterns.Publisher().registerObserver(self.onCategoryChanged, eventType=eventType) pub.subscribe(self.onFilterMatchingChanged, 'settings.view.categoryfiltermatchall') super(CategoryFilter, self).__init__(*args, **kwargs)
def __init__(self, base, name=None): r""" Initialize ``self``. EXAMPLES:: sage: S = Spec(ZZ) sage: C = Schemes(S); C Category of schemes over Integer Ring sage: C.__class__.__init__ == sage.categories.category_types.Category_over_base.__init__ True sage: C.base() is S True sage: TestSuite(C).run() """ self.__base = base Category.__init__(self, name)
def any_attribute_matches(attribute_name, src_attributes, trg_attributes): wildcard_names = re.findall(r'_any_(.*)', attribute_name) assert wildcard_names, 'Attribute name invalid: {0}'.format(attribute_name) wildcard_name = wildcard_names[0] src_attr_value = src_attributes.get(attribute_name, None) assert src_attr_value trg_attr_values = [value for key, value in trg_attributes.items() \ if key.endswith(wildcard_name)] for trg_attr_value in trg_attr_values: if wildcard_name == 'category': src_category = Category(src_attr_value) trg_category = Category(trg_attr_value) if src_category.match(trg_category): return True else: if src_attr_value.lower() == trg_attr_value.lower(): return True return False
def attributes_match(attribute_name, src_attr_value, trg_attr_value): # If this attribute is an arbitrary type specification, it doesn't count # as a rule attribute to match against the CCG tree. Thus, return True. if 'coq_type' in attribute_name: return True # If the attribute is a wildcard, we do not check for match here. return True. if '_any_' in attribute_name: return True if src_attr_value is not None and trg_attr_value is None: return False if src_attr_value is None and trg_attr_value is not None: return True if src_attr_value is None and trg_attr_value is None: return True # Case: src_attr_value is not None and trg_attr_value is not None if not 'category' in attribute_name: return src_attr_value.lower() == trg_attr_value.lower() # Comparing categories needs feature unification: src_category = Category(src_attr_value) trg_category = Category(trg_attr_value) return src_category.match(trg_category)
class SemanticRule(object): def __init__(self, category, semantics, attributes = {}): if not isinstance(category, Category): self.category = Category(category) else: self.category = category if semantics and not isinstance(semantics, Expression): self.semantics = lexpr(semantics) else: self.semantics = semantics self.attributes = copy.deepcopy(attributes) if 'surf' in self.attributes: self.attributes['surf'] = normalize_token(self.attributes['surf']) if 'base' in self.attributes: self.attributes['base'] = normalize_token(self.attributes['base']) def match(self, other): # Check class membership and special attribute matches. if not isinstance(other, self.__class__) \ or not isinstance(other.category, self.category.__class__) \ or not self.category.match(other.category): return False # If one rule is terminal but not the other, then they do not match. if self.is_terminal_rule() != other.is_terminal_rule(): return False # Obtain generic list of attributes from both semantic rules, # and check whether they match each other (or are underspecified). self_attr_names = set(self.attributes.keys()) other_attr_names = set(other.attributes.keys()) attribute_names = self_attr_names.union(other_attr_names) attribute_names = self.remove_control_attribute_names(attribute_names) for attribute_name in attribute_names: self_attr_value = self.attributes.get(attribute_name) other_attr_value = other.attributes.get(attribute_name) if not attributes_match(attribute_name, self_attr_value, other_attr_value): return False if not wildcard_match(attribute_names, self.attributes, other.attributes): return False return True def remove_control_attribute_names(self, attribute_names): control_attrs = ['var_paths'] return [a for a in attribute_names if a not in control_attrs] def is_terminal_rule(self): if 'rule' in self.attributes: return False for attribute_name in self.attributes: if attribute_name.startswith('child'): return False return True
def __init__(self, category, semantics, attributes = {}): if not isinstance(category, Category): self.category = Category(category) else: self.category = category if semantics and not isinstance(semantics, Expression): self.semantics = lexpr(semantics) else: self.semantics = semantics self.attributes = copy.deepcopy(attributes) if 'surf' in self.attributes: self.attributes['surf'] = normalize_token(self.attributes['surf']) if 'base' in self.attributes: self.attributes['base'] = normalize_token(self.attributes['base'])
def categories_edit(category_id): try: category = Category.find_by_id(category_id) if category: if request.method == "GET": category_dict = category.__dict__ form = Form(category_dict, form_fields) return render_template("category/edit.html", form=form, category_id=category_id, success=True) elif request.method == "POST": form = Form(request.form, form_fields) if form.validate(): values = form.named_values() category.from_dict(values) category.save() miner = miner_cls(category) miners[category_id] = miner miner.start() return redirect(url_for("categories")) else: return render_template("category/edit.html", form=form, category_id=category_id, success=True) elif request.method == "DELETE": if category_id in miners: miner = miners[category_id] miner.stop() del miners[category_id] Category.delete(category_id) return "Category {} deleted.".format(category_id), 200 else: return "Category {} not present on miner.".format(category_id), 400 else: return "Unsupported request method.", 400 else: return "Category {} not found.".format(category_id), 400 except ValueError, e: print e return "{} is not a valid category id.".format(category_id), 400
class Admin(object): db = None ''' classdocs ''' #constructor def __init__(self): self.user = User() self.course = Course() self.category = Category() self.quiz = Quiz() self.announcement = Announcement() self.discussion = Discussion() def resetDB(self): self.user.reset() self.course.reset() self.category.reset() self.quiz.reset() self.announcement.reset() self.discussion.reset()
def output_cloze(infinitive, tense, conj): ''' Combines the different parts of a verb conjugation with Anki's required formatting to produce a form suitable for a cloze-deletion card ''' result = [] # TODO - make this pythonic, it's an ugly hack as it is for i, item in enumerate(conj): if i == 0 and infinitive[0] in _VOWELS: result.append(_FPS_CLOZE_FORMAT.format(item[0], item[1], infinitive, tense)) else: result.append(_STD_CLOZE_FORMAT.format(item[0], item[1], infinitive, tense)) return Category._make(result)
def construct_stem_and_ending(infinitive, tense): ''' Given an infinitive and a tense, looks up the appropriate transformation rules and concatenates the resultant stem and ending ''' stem = _STEM_RULES[tense](infinitive) verb_type = infinitive[-2:] with sqlite3.connect('verb_trainer.db') as con: cur = con.cursor() cur.execute("SELECT fps, sps, tps, fpp, spp, tpp FROM tense_endings" "WHERE verb_type = ? AND tense_id = " "(SELECT tense_id FROM tenses WHERE tense_name = ?)", (verb_type, tense)) endings = cur.fetchone() return Category._make([stem + end for end in endings])
def construct_simple_tense_output(inflection): output = [] if inflection.fps.verb[0] in _VOWELS: output.append(_FPS_FORMAT.format(inflection.fps.pronoun, inflection.fps.verb)) else: output.append(_STD_FORMAT.format(inflection.fps.pronoun, inflection.fps.verb)) output.append(_STD_FORMAT.format(inflection.sps.pronoun, inflection.sps.verb)) output.append(_STD_FORMAT.format(inflection.tps.pronoun, inflection.tps.verb)) output.append(_STD_FORMAT.format(inflection.fpp.pronoun, inflection.fpp.verb)) output.append(_STD_FORMAT.format(inflection.spp.pronoun, inflection.spp.verb)) output.append(_STD_FORMAT.format(inflection.tpp.pronoun, inflection.tpp.verb)) return Category._make(output)
def output_cloze_import(infinitive, tense, translation, sound, conj): ''' Combines the output of the output_cloze function with optional translation and sound fields and combines them to produce the format required for Anki's import function ''' cloze = output_cloze(infinitive, tense, conj) if translation: add_trn = [cz + ('|{}'.format(trn)) for cz, trn in zip(cloze, translation)] else: add_trn = [cz + '|' for cz in cloze] if sound: add_snd = [trn + ('|[sound:{}]'.format(snd)) for trn, snd in zip(add_trn, sound)] else: add_snd = [trn + '|' for trn in add_trn] add_tag = [snd + ('|{}'.format(infinitive)) for snd in add_snd] return Category._make(add_tag)
def go_categories(xmldoc): categorieslist = xmldoc.getElementsByTagName('category') for s in categorieslist: category_id = s.attributes['id'].value name = s.childNodes[0].nodeValue try: parent_id = s.attributes['parentId'].value except: parent_id = None print name print Category.get_by_folder_id(parent_id) print category_id c = Category(name=name.replace('"', '"'), parent=Category.get_by_folder_id(parent_id), folder_id=category_id) print c c.save()
def go_items(xmldoc): itemlist = xmldoc.getElementsByTagName('offer') for s in itemlist : product_id = s.attributes['id'].value price = s.getElementsByTagName('price')[0].childNodes[0].nodeValue name = s.getElementsByTagName('name')[0].childNodes[0].nodeValue vendor = s.getElementsByTagName('vendor')[0].childNodes[0].nodeValue if s.getElementsByTagName('description')[0].childNodes: description = s.getElementsByTagName('description')[0].childNodes[0].nodeValue else: description = '' category_id = s.getElementsByTagName('categoryId')[-1].childNodes[0].nodeValue producer = Producer.get_or_create(vendor) category = Category.get_by_folder_id(category_id) if category: Item(category=category, producer=producer, name=name, price=float(price), description=description, product_id=product_id, ).save()
def get_by_category_id(cls, cat_id, filters={}): category = Category.get_by_id(cat_id) ids = [] if not category.parent: ids = [x.id for x in category.children] ids.append(cat_id) sqa_filters = [cls.category_id.in_(ids)] order_by = cls.id.desc() brand_filter = filters.get('brand_filter') if brand_filter: sqa_filters.append(cls.brand_id == int(brand_filter)) price_filter = filters.get('price_filter') if price_filter and price_filter == 'asc': order_by = cls.price.asc() if price_filter and price_filter == 'desc': order_by = cls.price.desc() return DBSession.query(cls).filter( and_(*sqa_filters) ).order_by(cls.state.asc(), order_by).all()