Best Python code snippet using pypom_form_python
handler_argument.py
Source:handler_argument.py
...4import arrow # TODO cpy3.7 datetime.fromisoformat('2017-01-01T12:30:59.000000+08:00')5from datetime import datetime, date6from aiohttp.web_exceptions import HTTPException7from .exception import RESTfulArgumentError8def argument_getter_factory(route_spec):9 """ æ ¹æ®specæ¥çæhandlerå½æ°åæ°å¼è¯»åå¨ """ 10 11 func_sig = inspect.signature(route_spec.handler_func)12 param_names = list(func_sig.parameters.keys())13 arg_getters = build_argval_getters(route_spec)14 n_args = len(arg_getters)15 async def argument_getters(request):16 errors = None17 values = {}18 for i in range(n_args):19 try:20 arg_name = param_names[i]21 values[arg_name] = await arg_getters[i](request)22 except HTTPException:23 raise24 except Exception as exc :25 if errors is None: 26 errors = []27 request.app.logger.error('', exc_info=True)28 exc_type = type(exc)29 data = {30 "name": arg_name, 31 "type": f"{exc_type.__module__}.{exc_type.__qualname__}",32 "error": str(exc)33 }34 errors.append(data)35 if errors is not None:36 raise RESTfulArgumentError(errors)37 return values38 return argument_getters39_handler_argval_getters = []40def register_argument_getter(getter_factory):41 _handler_argval_getters.append(getter_factory)42def build_argval_getters(route_spec):43 # proto, method, handler_func, path_fields44 # proto, method, handler, path_params45 getters = []46 for arg_name in inspect.signature(route_spec.handler_func).parameters:47 getter = _build_argval_getter(route_spec, arg_name)48 getters.append(getter)49 return getters50def _build_argval_getter(route_spec, arg_name):51 arg_spec = inspect.signature(route_spec.handler_func).parameters[arg_name]52 # print(123, arg_name)53 ann_type = arg_spec.annotation54 argval_getter = None55 if ann_type != inspect._empty:56 for getter_factory in _handler_argval_getters[::-1]:57 # print(3222, getter_factory)58 argval_getter = getter_factory(route_spec, arg_name)59 if argval_getter is not None:60 break61 if argval_getter is None:62 argval_getter = _defaul_argval_getter(route_spec, arg_name)63 if arg_spec.default is not inspect._empty:64 async def _getter(request):65 arg_val = await argval_getter(request)66 # if arg_val is None:67 # arg_val = arg_spec.default68 return arg_val69 else:70 async def _getter(request):71 try:72 # print(arg_name, argval_getter)73 arg_val = await argval_getter(request)74 except TypeError as exc :75 raise TypeError(f"{exc} while reading '{arg_name}' with "76 f"'{argval_getter.__qualname__}'")77 return arg_val78 return _getter79#----------------------------------------------------------------------------80def default_argval_getter_factory(route_spec, arg_name):81 if arg_name in route_spec.path_fields:82 async def _path_param_getter(request):83 return request.match_info.get(arg_name)84 return _path_param_getter85 async def _argvalue_func(request):86 if request.method in request.POST_METHODS:87 arg_val = request.post().get(arg_name)88 if arg_val is not None:89 return arg_val90 arg_val = request.query.get(arg_name)91 if arg_val is not None:92 return arg_val93 return None94 return _argvalue_func95def _defaul_argval_getter(route_spec, arg_name):96 arg_spec = inspect.signature(route_spec.handler_func).parameters[arg_name]97 ann_type = arg_spec.annotation98 read_argval = default_argval_getter_factory(route_spec, arg_name)99 if (issubclass(ann_type, int) or issubclass(ann_type, float)):100 async def _getter_func(request):101 arg_val = await read_argval(request)102 if arg_val is not None:103 arg_val = ann_type(arg_val)104 return arg_val105 return _getter_func106 async def _getter_func(request):107 return await read_argval(request)108 return read_argval109async def read_json(request):110 text = await request.text()111 return json.loads(text)112def _json_arg_getter(route_spec, arg_name):113 if arg_name not in ['json_arg', 'json_body']:114 return115 return read_json116def _identity_getter(route_spec, arg_name):117 if arg_name not in ['identity']:118 return119 120 async def _identify(request):121 secure_layer = request.app.get('secure_layer')122 if secure_layer is None:123 return None124 identity = await secure_layer.identify(request)125 return identity126 return _identify127def _datetime_value_getter(route_spec, arg_name):128 arg_spec = inspect.signature(route_spec.handler_func).parameters[arg_name]129 ann_type = arg_spec.annotation130 if not issubclass(ann_type, datetime):131 return132 read_argval = default_argval_getter_factory(route_spec, arg_name)133 async def getter(request):134 arg_val = arrow.get(read_argval(request)).datetime135 return arg_val136 return getter137def _date_value_getter(route_spec, arg_name):138 arg_spec = inspect.signature(route_spec.handler_func).parameters[arg_name]139 ann_type = arg_spec.annotation140 if not issubclass(ann_type, date):141 return142 read_argval = default_argval_getter_factory(route_spec, arg_name)143 async def getter(request, arg_val):144 arg_val = arrow.get(arg_val).datetime.date()145 return arg_val146 return getter147register_argument_getter(_identity_getter)148register_argument_getter(_json_arg_getter)149register_argument_getter(_date_value_getter)...
test_models_getter.py
Source:test_models_getter.py
...35 return self.name, attr36class TestModelsGetter(common.TestCase):37 @defer.inlineCallbacks38 def check_attr(self, context, getter_factory, exp_name):39 getter = getter_factory("tata")40 res = yield getter(None, context)41 self.assertEqual(res, (exp_name, "tata"))42 getter = getter_factory("toto")43 res = yield getter("spam", context)44 self.assertEqual(res, (exp_name, "toto"))45 getter = getter_factory("tutu")46 res = yield getter("spam", context, param="foo")47 self.assertEqual(res, (exp_name, "tutu"))48 @defer.inlineCallbacks49 def check_get(self, context, getter_factory, exp_index, exp_name):50 getter = getter_factory("titi")51 res = yield getter(None, context)52 self.assertEqual(res, (exp_name, "titi", (exp_index, ), {}))53 getter = getter_factory("titi")54 res = yield getter("spam", context)55 self.assertEqual(res, (exp_name, "titi", (exp_index, ), {}))56 getter = getter_factory("titi")57 res = yield getter("spam", context, foo="bar")58 self.assertEqual(res, (exp_name, "titi", (exp_index, ), {}))59 @defer.inlineCallbacks60 def check_getattr(self, context, getter_factory, exp_index, exp_name):61 getter = getter_factory()62 res = yield getter(None, context)63 self.assertEqual(res, (exp_name, exp_index))64 getter = getter_factory()65 res = yield getter("spam", context)66 self.assertEqual(res, (exp_name, exp_index))67 getter = getter_factory()68 res = yield getter("spam", context, param="foo")69 self.assertEqual(res, (exp_name, exp_index))70 @defer.inlineCallbacks71 def testAttr(self):72 source = DummyAttr("source")73 action = DummyAttr("action")74 model = DummyAttr("model", source)75 view = DummyAttr("view")76 context = {"model": model, "view": view, "action": action}77 yield self.check_attr(context, getter.model_attr, "model")78 yield self.check_attr(context, getter.source_attr, "source")79 yield self.check_attr(context, getter.action_attr, "action")80 yield self.check_attr(context, getter.view_attr, "view")81 @defer.inlineCallbacks...
diagnostic.py
Source:diagnostic.py
...7 combos = itertools.product(*[i for i in combination])8 for c in combos:9 list_of_combinations.append(c)10 return list_of_combinations11def getter_factory(key):12 def getter(l):13 t = [i[1] for i in l if i[0] == key]14 return t[0]15 return getter16get_town = getter_factory('town')17get_year = getter_factory('year')18get_race = getter_factory('race')19get_gender = getter_factory('gender')20get_age = getter_factory('age')21get_mt = getter_factory('measure type')22get_var = getter_factory('variable')23def build_lookup_key(d):24 town = get_town(d)25 year = get_year(d)26 race = get_race(d)27 gender = get_gender(d)28 age = get_age(d)29 mt = get_mt(d)30 var = get_var(d)31 return '{}_{}_{}_{}_{}_{}_{}'.format(town, year, race, gender, age, mt, var)32def build_key(d):33 town = d['Town']34 year = d['Year'].replace(' ', '')35 race = d['Race/Ethnicity']36 gender = d['Gender']...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!