Best Python code snippet using localstack_python
generate_config_docs.py
Source:generate_config_docs.py
1# flake8: noqa2import json3if __name__ == "__main__":4 schema = json.load(open("./taskcat/cfg/config_schema.json", "r"))5 def resolve_ref(props):6 if "$ref" in props:7 ref = props["$ref"].split("/")[-1]8 del props["$ref"]9 props.update(schema["definitions"][ref])10 return props11 for k, v in schema["properties"].items():12 item_str = f"* `{k}` "13 v = resolve_ref(v)14 item_str += f"*type:* `{v['type']}` "15 if "description" in v:16 item_str += f'{v["description"]}'17 print(item_str)18 if "properties" in v:19 for ik, iv in v["properties"].items():20 item_str = f" * `{ik}` "21 iv = resolve_ref(iv)22 item_str += f"*type:* `{iv['type']}` "23 if "description" in iv:24 item_str += f'{iv["description"]}'25 print(item_str)26 if iv["type"] == "object":27 if "properties" in iv:28 for iik, iiv in iv["properties"].items():29 item_str = f" * `{iik}` "30 iiv = resolve_ref(iiv)31 item_str += f"*type:* `{iiv['type']}` "32 if "description" in iiv:33 item_str += f'{iiv["description"]}'34 elif "additionalProperties" in iv:35 name = ik[:-1] if ik.endswith("s") else ik36 item_str = f" * `<{name.upper()}_NAME>` "37 props = resolve_ref(iv["additionalProperties"])38 item_str += f"*type:* `{iv['type']}` "39 if "description" in props:40 item_str += f'{props["description"]}'41 print(item_str)42 elif "additionalProperties" in v:43 name = k[:-1] if k.endswith("s") else k44 item_str = f" * `<{name.upper()}_NAME>` "45 props = resolve_ref(v["additionalProperties"])46 item_str += f"*type:* `{v['type']}` "47 if "description" in props:48 item_str += f'{props["description"]}'49 if "properties" in props:50 for ik, iv in props["properties"].items():51 item_str = f" * `{ik}` "52 iv = resolve_ref(iv)53 item_str += f"*type:* `{iv['type']}` "54 if "description" in iv:55 item_str += f'{iv["description"]}'56 print(item_str)57 if iv["type"] == "object":58 if "properties" in iv:59 for iik, iiv in iv["properties"].items():60 item_str = f" * `{iik}` "61 iiv = resolve_ref(iiv)62 item_str += f"*type:* `{iiv['type']}` "63 if "description" in iiv:64 item_str += f'{iiv["description"]}'65 elif "additionalProperties" in iv:66 name = ik[:-1] if ik.endswith("s") else ik67 item_str = f" * `<{name.upper()}_NAME>` "68 iprops = resolve_ref(iv["additionalProperties"])69 item_str += f"*type:* `{iv['type']}` "70 if "description" in iprops:71 item_str += f'{iprops["description"]}'72 print(item_str)73 else:...
validator.py
Source:validator.py
...5from IPython.external import argparse6import traceback7import json8def nbvalidate(nbjson, schema='v3.withref.json', key=None,verbose=True):9 v3schema = resolve_ref(json.load(open(schema,'r')))10 if key :11 v3schema = jsonpointer.resolve_pointer(v3schema,key)12 errors = 013 v = Draft3Validator(v3schema);14 for error in v.iter_errors(nbjson):15 errors = errors + 116 if verbose:17 print(error)18 return errors19def resolve_ref(json, base=None):20 """return a json with resolved internal references21 only support local reference to the same json22 """23 if not base :24 base = json25 temp = None26 if type(json) is list:27 temp = [];28 for item in json:29 temp.append(resolve_ref(item, base=base))30 elif type(json) is dict:31 temp = {};32 for key,value in json.iteritems():33 if key == '$ref':34 return resolve_ref(jsonpointer.resolve_pointer(base,value), base=base)35 else :36 temp[key]=resolve_ref(value, base=base)37 else :38 return json39 return temp40def convert(namein, nameout, indent=2):41 """resolve the references of namein, save the result in nameout"""42 jsn = None43 with open(namein) as file :44 jsn = json.load(file)45 v = resolve_ref(jsn, base=jsn)46 x = jsonpointer.resolve_pointer(v, '/notebook')47 with open(nameout,'w') as file:48 json.dump(x,file,indent=indent)49if __name__ == '__main__':50 parser = argparse.ArgumentParser()51 parser.add_argument('-s', '--schema',52 type=str, default='v3.withref.json')53 parser.add_argument('-k', '--key',54 type=str, default='/notebook',55 help='subkey to extract json schema from json file')56 parser.add_argument("-v", "--verbose", action="store_true",57 help="increase output verbosity")58 parser.add_argument('filename',59 type=str,...
export.py
Source:export.py
1import datetime2import inspect3from bson import ObjectId4from mongoengine import *5__all__ = [6 'ExportableMixin'7]8def dt_to_timestamp(dt):9 assert isinstance(dt, datetime.datetime)10 return int((dt - datetime.datetime(1970, 1, 1)).total_seconds())11def export_entity(obj, fields=None):12 assert isinstance(obj, (Document, EmbeddedDocument))13 def export_val(val, resolve_ref=False):14 if isinstance(val, (basestring, int, float)):15 return val16 # Handle system types17 if isinstance(val, list):18 return map(export_val, val)19 elif isinstance(val, datetime.datetime):20 return dt_to_timestamp(val)21 # Resolve ref for exportable entity22 if resolve_ref and isinstance(val, ExportableMixin):23 return val.export_entity()24 if isinstance(val, EmbeddedDocument):25 if isinstance(val, ExportableMixin):26 return val.export_entity()27 return export_entity(val)28 if isinstance(val, Document):29 return str(val.id)30 if isinstance(val, ObjectId):31 return str(val)32 return val33 d = {}34 if fields:35 for name in fields:36 resolve_ref = False37 key = name38 if type(name) == tuple: # Check if extra options is specified39 assert len(name) > 1, 'Tuple given but no options found'40 if type(name[1]) == bool: # Case: (field_name, resolve_ref)41 resolve_ref = name[1]42 key = name[0]43 else:44 if len(name) == 3: # Case: (field_name, output_name, resolve_ref)45 resolve_ref = name[2]46 key = name[1] # Case: (field_name, output_name)47 value = getattr(obj, name[0])48 else:49 value = getattr(obj, key)50 d[key] = export_val(value, resolve_ref)51 else:52 for name in dir(obj):53 value = getattr(obj, name)54 if name not in ('objects', 'pk') and not name.startswith('_') and \55 not inspect.ismethod(value) and \56 not inspect.isclass(value):57 d[name] = export_val(value)58 return d59class ExportableMixin(object):60 _exported_fields = None61 def export_entity(self):...
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!!