Best Python code snippet using tempest_python
test_formats.py
Source:test_formats.py
...11class TestFormatDict(unittest.TestCase):12 def test_dict_simple(self):13 from outlib.lib.format_vals import _format_dict14 input_val = {'a': 'b'}15 output = _format_dict(input_val)16 17 assert output['a'] == 'b'18 def test_dict_int_value(self):19 from outlib.lib.format_vals import _format_dict20 input_val = {'a': 1}21 output = _format_dict(input_val)22 23 assert output['a'] == 124 def test_dict_dict_value(self):25 from outlib.lib.format_vals import _format_dict26 input_val = {'a': {'b': 'c'}}27 output = _format_dict(input_val)28 29 assert output['a']['b'] == 'c'30 def test_dict_list_value(self):31 from outlib.lib.format_vals import _format_dict32 input_val = {'a': ['b', 'c']}33 output = _format_dict(input_val)34 35 assert output['a'][0] == 'b'36 def test_dict_object_value(self):37 from outlib.lib.format_vals import _format_dict38 input_val = {'a': TObject(test1='value1')}39 output = _format_dict(input_val)40 41 assert output['a']['test1'] == 'value1'42 def test_dict_row_proxy_value(self):43 from outlib.lib.format_vals import _format_dict44 input_val = {'a': RowProxy(test1='value2')}45 output = _format_dict(input_val)46 47 assert output['a']['test1'] == 'value2'48class TestFormatList(unittest.TestCase):49 def test_list_simple(self):50 from outlib.lib.format_vals import _format_list51 input_val = ['a', 'b']52 output = _format_list(input_val)53 54 assert output[1] == 'b'55 def test_list_int_value(self):56 from outlib.lib.format_vals import _format_list57 input_val = ['a', 1]58 output = _format_list(input_val)59 ...
c6_python_trick.py
Source:c6_python_trick.py
...43 def _format_list(k, v):44 if all(isinstance(_, dict) for _ in v):45 v_str = '[\n'46 v_str += '\n'.join(47 f'dict({_format_dict(v_)}),'48 for v_ in v).rstrip(',')49 attr_str = f'{str(k)}={v_str}'50 attr_str = attr_str + ']'51 else:52 attr_str = _format_basic_types(k, v)53 return attr_str54 def _format_dict(input_dict, outest_level=False):55 r = ''56 s = []57 for idx, (k, v) in enumerate(input_dict.items()):58 end = '' if outest_level else ',' # éè¦ç¬¦åpep8æ ¼å¼59 if isinstance(v, dict):60 v_str = '\n' + _format_dict(v) # éå½è°ç¨61 attr_str = f'{str(k)}=dict({v_str}'62 attr_str = attr_str + ')' + end63 elif isinstance(v, list):64 attr_str = _format_list(k, v) + end65 else:66 attr_str = _format_basic_types(k, v) + end67 s.append(attr_str)68 r += '\n'.join(s)69 return r70 # outest_level æå¤å±71 # ä»
ä»
éè¦ædictæç
§è¡æååå²å¥½å°±è¡ï¼åé¢çæ¯è¡å¦ä½æ ¼å¼åéç¨pep8æ åèªå¨æ§è¡72 text = _format_dict(src_dict, outest_level=True)73 # åå©yapfåºå®ç°çpep8æ ¼å¼åå符串åè½å®ç°74 yapf_style = dict(75 based_on_style='pep8',76 blank_line_before_nested_class_or_def=True,77 split_before_expression_after_opening_paren=True)78 text, _ = FormatCode(text, style_config=yapf_style, verify=True)...
requestlog.py
Source:requestlog.py
1# coding: utf-82from logging import getLogger, LoggerAdapter3def _format_dict(var, tabindent=1):4 # Do nothing if received non-dict5 if not isinstance(var, dict):6 return var7 dict_ = var # Just a better name8 keys = sorted(dict_.keys())9 lines = ['{']10 for k in keys:11 line = "%s%s: %s," % ('\t' * tabindent, repr(k), repr(dict_[k]))12 lines.append(line)13 lines.append('%s},' % ('\t' * (tabindent - 1)))14 return '\n'.join(lines)15def getRequestLogger(name):16 """Returns a logger aware of HttpRequest attributes"""17 return RequestLoggerAdapter(getLogger(name), {})18class RequestLoggerAdapter(LoggerAdapter):19 message = u'\n\t%(method)s %(path)s\n\tHEADERS: %(META)s\n\tDATA: %(POST)s\n\tCOOKIES: %(COOKIES)s'20 def _requestProperties(self, request):21 properties = {22 u'POST' : _format_dict(request.POST, 2),23 u'COOKIES': _format_dict(request.COOKIES, 2),24 u'META' : _format_dict(request.META, 2),25 u'method' : request.method,26 u'path' : request.path,27 }28 return properties29 def process(self, msg, kwargs):30 request = kwargs.pop('request', {})31 if request:32 params = self._requestProperties(request)33 msg += self.message % params...
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!!