How to use by_and method in Selene

Best Python code snippet using selene_python

dataset.py

Source:dataset.py Github

copy

Full Screen

1from .models import DrugsApproved2from django.db.models.functions import ExtractYear3from django.db.models import Count, Max, Min4from django.db.models.query import EmptyQuerySet5from copy import deepcopy6class DrugDataset(object):7 """generate dataset for creating echarts"""8 MAX_KEYWORD_NUM = 39 TOO_LARGE_QSET = 5000010 EXCEPT_KEYWORDS = ['国药准字Z2','国药准字H2','国药准字S2','有限公司','199','200','201', '02', '03', '04']11 colnames = ['drug_name_zh', 'production_unit__production_unit_name', 'drug_name_en', 'drug_approval_num']12 view_colnames = ['drug_index', 'drug_approval_num', 'drug_name_zh', 'drug_name_en', 'drug_form','drug_spec', 'production_unit__production_unit_name','production_unit__province', 'approval_date']13 date_columns = ['approval_date']14 one_ending = '_one'15 many_ending = '_many'16 charts_by_column = {17 'default':['approval_date', 'production_unit__province'],18 'drug_name_zh'+one_ending:['approval_date', 'production_unit__province'],19 'drug_name_zh'+many_ending:['approval_date', 'production_unit__province', 'drug_form'],20 # 'drug_name_zh'+one_ending:['drug_spec', 'production_unit__production_unit_name', 'approval_date'],21 # 'drug_name_zh'+many_ending:['drug_name_zh', 'category', 'drug_spec', 'production_unit__production_unit_name', 'approval_date'],22 'production_unit__production_unit_name'+one_ending:['approval_date','drug_form'],23 'production_unit__production_unit_name'+many_ending:['approval_date', 'drug_form', 'production_unit__province'],24 'drug_approval_num'+one_ending:['approval_date','drug_form'],25 'drug_approval_num'+many_ending:['approval_date', 'drug_form', 'production_unit__production_unit_name'],26 'drug_name_en'+one_ending:['approval_date','drug_form'],27 'drug_name_en'+many_ending:['approval_date', 'drug_form', 'production_unit__province'],28 }29 chartbyids = {30 'drug_name_zh':'按药品名称',31 'drug_form':'按剂型',32 'drug_spec':'按规格',33 'production_unit__production_unit_name':'按企业名称',34 'production_unit__province':'按省份',35 'approval_date':'按批准年份',36 }37 x = 'axisX'38 y = 'axisY'39 is_by_and = True40 default_qsets = {}41 model = DrugsApproved42 querys_dict = {}43 def __init__(self):44 super(DrugDataset, self).__init__()45 def _parse_keyword(self, keyword, splitchars=[' ', '+', ',', ',']):46 # split keyword to a list47 keyword = keyword.strip()48 for c in splitchars:49 kwords = keyword.split(c)50 if len(kwords) > 1: 51 if c in [',', ',']:52 self.is_by_and = False53 else:54 self.is_by_and = True55 break56 def except_word(kword):57 if kword == '': return False58 kword = kword.upper()59 for ewords in DrugDataset.EXCEPT_KEYWORDS:60 if kword in ewords:61 return False62 return True63 #drop the space char of list64 kwords = list(filter(except_word, kwords))65 return kwords or []66 def _get_query_columns(self, kwords):67 # find out the relative columns of kwords68 columns = []69 qwords = []70 for keyword in kwords:71 column = self._get_query_column_by_one(keyword)72 if column :73 columns.append(column)74 qwords.append(keyword)75 print('query kwords:{}, columns:{}'.format(qwords, columns))76 return qwords, columns77 def _get_query_column_by_one(self, keyword):78 # find out the first correct column of a keyword79 for col in self.colnames:80 obj = self.model.objects\81 .filter(**{col+'__icontains' : keyword})\82 .first()83 if obj: return col84 def _get_query_names_by_cols(self, objs, qwords, columns):85 # collect all the different column values of every qword by column86 querys_dict = {}87 cur_words = []88 for i in range(len(columns)):89 dnames = objs.values_list(columns[i], flat = True)90 dnames = list(set(dnames))91 dnames.sort()92 if qwords[i] in dnames:93 cur_words.append(qwords[i])94 j = dnames.index(qwords[i])95 dnames[i], dnames[j] = dnames[j], dnames[i]96 else:97 cur_words.append(dnames[0])98 querys = {'kwords' : dnames, 'column': columns[i], 'active':''}99 # print('querys = {}'.format(querys))100 querys_dict[qwords[i]] = querys101 102 querys_dict = self._collect_same_column(querys_dict)103 return querys_dict, cur_words104 def _collect_same_column(self, querys_dict):105 # collect and group by the same column, space char join keys by same columns106 d = {}107 for k, v in querys_dict.items():108 col = v['column']109 if col in d.keys():110 d[col] = d[col] + ' ' + k111 else:112 d[col] = k113 dd = {}114 for col, keys in d.items():115 for value in querys_dict.values():116 if value['column'] == col:117 dd[keys] = value118 break119 return dd120 def _get_queryset_by_list(self, objs, kwords, columns, by_exact = True, by_and = True):121 # filter with all the kwords by columns from objs of db122 if len(kwords) != len(columns): return []123 if not by_exact:124 cols_methods = [col+'__icontains' for col in columns]125 else:126 cols_methods = [col+'__iexact' for col in columns]127 filter_kw = dict(zip(kwords, cols_methods))128 print('kwords = {}, columns = {}, kw={}'.format(kwords, columns, filter_kw))129 colset = set(filter_kw.values())130 return self._get_queryset_by_recursion(objs, filter_kw, colset, by_and = by_and)131 def _get_queryset_by_recursion(self, objs, filter_kw, colset, by_and = True):132 cur_col = colset.pop()133 qset = objs.none()134 for value, col in filter_kw.items():135 if col == cur_col: 136 print('col = {}, value = {}'.format(col, value))137 if isinstance(qset, EmptyQuerySet):138 qset = objs.filter(**{col: value})139 else:140 qs = objs.filter(**{col: value})141 # QuerySet & QuerySet one by one, intersection142 if by_and:143 qset = qset & qs144 else:145 qset = qset | qs146 # print('queryset by list len:{}'.format(len(qset)))147 # print('qset by list kwords:{} = {}, qset ={}'.format(col, value, qset))148 if len(colset) == 0:149 return qset150 else:151 return self._get_queryset_by_recursion(qset, filter_kw, colset, by_and = by_and)152 def _get_queryset_by_one(self, objs, name, column, is_exact = False):153 # filter withe one kword by column from objs of db154 if is_exact:155 qset = objs.filter(**{column+'__iexact': name})156 else:157 qset = objs.filter(**{column+'__icontains': name})158 print('queryset by one len:{}'.format(len(qset)))159 return qset160 def get_queryset(self, keyword, is_exact = False):161 # get queryset by keyword162 self.keyword = keyword163 kwords = self._parse_keyword(keyword)164 if kwords == []: return [], kwords, []165 qwords, columns = self._get_query_columns(kwords)166 if qwords == []: return [], kwords, []167 kn = len(qwords)168 if kn == 1:169 qset = self._get_queryset_by_one(self.model.objects, qwords[0], columns[0], is_exact = is_exact)170 elif kn > 1:171 qset = self._get_queryset_by_list(self.model.objects, qwords, columns, by_exact = is_exact, by_and = self.is_by_and)172 # print('qset = {}'.format(qset))173 return qset, qwords, columns174 def get_queryset_by_dict(self, **keys_cols):175 # get queryset by dict, to ready for request ajax176 kwords = list(keys_cols.keys())177 columns = list(keys_cols.values())178 qset = self._get_queryset_by_list(self.model.objects, kwords, columns, by_and = False)179 print('qset by dict len:', len(qset))180 return qset181 def get_datasets_by_dict(self, cur_keyword = '', **keys_cols):182 # get datasets by dict, to ready for request ajax183 if cur_keyword != '':184 qset = DrugDataset.default_qsets[cur_keyword]185 chartbycols = self.charts_by_column['default']186 kwords = []187 columns = []188 else:189 kwords = list(keys_cols.keys())190 columns = list(keys_cols.values())191 qset = self._get_queryset_by_list(self.model.objects, kwords, columns, by_and = False)192 if len(columns) > 1:193 chartbycols = self.charts_by_column[columns[0]+self.many_ending]194 else:195 chartbycols = self.charts_by_column[columns[0]+self.one_ending] 196 chartdatas = {}197 # get every chart by specified columns198 for chartbycol in chartbycols:199 # create a dict with zero values of every different chartbycol's key value200 print('chartbycol = ', chartbycol)201 chartdata = {}202 zdataset, new_col = self._init_dataset(qset, chartbycol)203 # count the item data of chartbycol filted by each filter_col and key204 if len(keys_cols) == 0:205 data = self._get_data_count_by(chartbycol, qset, zdataset, new_col)206 chartdata['Catgory with All Results'] = data207 else:208 for key, filter_col in keys_cols.items():209 print('*'*100)210 data = self._get_data_count_by(chartbycol, qset, zdataset, new_col, filter_by_col = filter_col, filter_keyword = key)211 chartdata[key] = data212 chartdatas[self.chartbyids[chartbycol]] = chartdata213 # self._save_dataset_to_db(qset, '', datasets)214 return chartdatas, kwords, columns215 # def _get_dataset_from_cache(self, keyword):216 # return [], [], [], [], ''217 def get_query_names(self, keyword, is_exact = False):218 # get query names219 qset, qwords, columns = self.get_queryset(keyword, is_exact)220 print('qset type:{}'.format(type(qset)))221 if len(qset) == 0: return {}, keyword, []222 if len(qset) > DrugDataset.TOO_LARGE_QSET: return {}, keyword, DrugDataset.TOO_LARGE_QSET223 DrugDataset.default_qsets[self.keyword] = qset224 querys_dict, cur_words = self._get_query_names_by_cols(qset, qwords, columns)225 return querys_dict, cur_words, qset226 def _get_data_count_by(self, count_by_col, qset, zdataset, new_col, filter_by_col = None, filter_keyword = None ):227 # count a column by group by itself228 if filter_by_col:229 qset = qset.filter(**{filter_by_col+'__iexact': filter_keyword})230 if count_by_col in self.date_columns:231 # print('-----------annotate date by year-------------')232 qset = qset.annotate(**{new_col : ExtractYear(count_by_col)}).values(new_col)233 data_count = qset.values(new_col).annotate(**{self.y : Count(new_col)}).values(new_col, self.y).order_by(new_col)234 print('data_count = {}'.format(data_count))235 #union the result dataset with zdataset236 zdataset = deepcopy(zdataset)237 # print('zdataset = {}'.format(zdataset))238 for d in zdataset:239 for cy in data_count:240 if d[new_col] == cy[new_col]: d[self.y] = cy[self.y]241 data_count = [tuple(d.values()) for d in zdataset]242 print('data_count tuple = {}'.format(data_count))243 return data_count or []244 def _init_dataset(self, qset, col):245 # init zero dataset246 if col in self.date_columns:247 qset = qset.annotate(**{self.x : ExtractYear(col)})248 col = self.x249 dset = qset.values_list(col, flat = True).order_by(col)250 dset = list(set(dset))251 dset.sort()252 # print('dset = {}'.format(dset))253 return [{col : i, self.y : 0 } for i in dset], col254 def _save_dataset_to_db(self, qset, query_dict, datasets):...

Full Screen

Full Screen

condition.py

Source:condition.py Github

copy

Full Screen

...5E = TypeVar('E')6R = TypeVar('R')7class Condition(Callable[[E], None]):8 @classmethod9 def by_and(cls, *conditions):10 def fn(entity):11 for condition in conditions:12 condition.call(entity)13 return cls(' and '.join(map(str, conditions)), fn)14 @classmethod15 def by_or(cls, *conditions):16 def fn(entity):17 errors: List[Exception] = []18 for condition in conditions:19 try:20 condition.call(entity)21 return22 except Exception as e:23 errors.append(e)24 raise AssertionError('; '.join(map(str, errors)))25 return cls(' or '.join(map(str, conditions)), fn)26 @classmethod27 def as_not(28 cls, condition: Condition[E], description: str = None29 ) -> Condition[E]:30 condition_words = str(condition).split(' ')31 is_or_have = condition_words[0]32 name = ' '.join(condition_words[1:])33 no_or_not = 'not' if is_or_have == 'is' else 'no'34 new_description = description or f'{is_or_have} {no_or_not} {name}'35 def fn(entity):36 try:37 condition.call(entity)38 except Exception:39 return40 raise ConditionNotMatchedError()41 return cls(new_description, fn)42 @classmethod43 def raise_if_not(44 cls, description: str, predicate: Predicate[E]45 ) -> Condition[E]:46 def fn(entity: E) -> None:47 if not predicate(entity):48 raise ConditionNotMatchedError()49 return cls(description, fn)50 @classmethod51 def raise_if_not_actual(52 cls, description: str, query: Lambda[E, R], predicate: Predicate[R]53 ) -> Condition[E]:54 def fn(entity: E) -> None:55 query_to_str = str(query)56 result = (57 query.__name__58 if query_to_str.startswith('<function')59 else query_to_str60 )61 actual = query(entity)62 if not predicate(actual):63 raise AssertionError(f'actual {result}: {actual}')64 return cls(description, fn)65 def __init__(self, description: str, fn: Lambda[E, None]):66 self._description = description67 self._fn = fn68 def call(self, entity: E) -> None:69 self._fn(entity)70 @property71 def predicate(self) -> Lambda[E, bool]:72 def fn(entity):73 try:74 self.call(entity)75 return True76 except Exception as e:77 return False78 return fn79 @property80 def not_(self) -> Condition[E]:81 return self.__class__.as_not(self)82 def __call__(self, *args, **kwargs):83 return self._fn(*args, **kwargs)84 def __str__(self):85 return self._description86 def and_(self, condition: Condition[E]) -> Condition[E]:87 return Condition.by_and(self, condition)88 def or_(self, condition: Condition[E]) -> Condition[E]:89 return Condition.by_or(self, condition)90def not_(condition_to_be_inverted: Condition):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful