Best Python code snippet using localstack_python
xml_ops.py
Source:xml_ops.py
...8from .utils import MetaInfo9def print_tag_file(filepath, feature):10 filehandle = codecs.open(filepath, 'w', 'utf8')11 filehandle.write('<?xml version="1.0" encoding="utf-8"?>\n')12 filehandle.write(obj_to_xml('document', feature))13 filehandle.close()14def read_tag_file(filepath, relurl):15 filehandle = codecs.open(filepath, 'r', 'utf8')16 metastring = filehandle.read()17 metainfo = xml_to_tagdict(relurl, metastring.encode('utf-8'))18 filehandle.close()19 return metainfo20def obj_to_xml(tagName, obj):21 if type(obj) in (str,):22 return get_xml_tag(tagName, obj)23 tags = ['<%s>' % tagName]24 ks = list(obj.keys())25 ks.sort()26 for k in ks:27 newobj = obj[k]28 if isinstance(newobj, dict):29 tags.append(obj_to_xml(k, newobj))30 elif isinstance(newobj, list):31 if k == 'bench':32 tags.append('<%s>'% k)33 for o in newobj:34 tags.append(obj_to_xml('name', o))35 tags.append('</%s>'% k)36 else:37 for o in newobj:38 tags.append(obj_to_xml(k, o))39 elif isinstance(newobj, datetime.datetime) or \40 isinstance(newobj, datetime.date):41 tags.append(obj_to_xml(k, date_to_xml(newobj)))42 else:43 tags.append(get_xml_tag(k, obj[k]))44 tags.append('</%s>' % tagName)45 xmltags = '\n'.join(tags)46 return xmltags47def xml_to_tagdict(docid, xmlstring):48 try:49 xmlnode = minidom.parseString(xmlstring)50 except ExpatError as e:51 logger = logging.getLogger('utils.commonfuncs')52 logger.error('Err %s in xml reading of tagfile %s' % (e, docid))53 return None54 feature = xml_to_obj(xmlnode.childNodes[0])55 metainfo = MetaInfo()...
obj_to_xml.py
Source:obj_to_xml.py
1def obj_to_xml(obj, depth=0):2 s_out = ''3 if isinstance(obj, dict):4 return dict_to_xml(depth, s_out, obj)5 elif not isinstance(obj, str) and hasattr(obj, "__iter__"):6 return iter_to_xml(depth, s_out, obj)7 elif depth == 0:8 return object_to_xml(obj)9 else:10 return obj111213def object_to_xml(obj):14 only = str(type(obj))15 only = only[only.find(' ') + 2:-2]16 return f'<obj type={only}>{obj}</obj>'171819def make_sout(depth, k, internal, fl):20 if fl == True:21 return depth * ' ' + f"<item key='{k}' type='str'>" f"{internal}</item>\n"22 else:23 return depth * ' ' + f"<item key='{k}'>{internal}</item>\n"242526def dict_to_xml(depth, s_out, obj):27 depth += 128 for k, val in obj.items():29 fl = 030 if val == obj:31 raise ValueError("infinite cycle")32 if isinstance(internal := obj_to_xml(val, depth + 1), str) and internal.isdigit():33 fl = 134 s_out += make_sout(depth, k, internal, fl)35 else:36 s_out += make_sout(depth, k, internal, fl)37 if not s_out:38 return f'<dict>{s_out}</dict>'39 else:40 begin = '' if depth == 1 else '\n'41 return begin + (depth - 1) * ' ' + f'<dict>\n{s_out}' + (depth - 1) * ' ' + '<dict>\n' + (depth - 2) * ' '424344def iter_to_xml(depth, s_out, obj):45 depth += 146 local = str(type(obj))47 st_from = local.find(' ') + 248 local = local[st_from:-2]49 for elem in obj:50 if elem == obj:51 raise ValueError(f"cycle reference in {local} cannot be handled")52 if isinstance(internal := obj_to_xml(elem, depth + 1), str) and internal.isdigit():53 s_out += depth * ' ' + f"item type='str'{internal}</item>\n"54 else:55 s_out += depth * ' ' + f'<item>{internal}</item>\n'56 if not obj:57 return f'<{local}>' + s_out + f'</local_type>'58 else:59 begin = '' if depth == 1 else '\n'60 return begin + (depth - 1) * ' ' + f'<{local}>\n' + s_out + (depth - 1) * ' ' + f'</{local}>\n' + (61 depth - 2) * ' '626364def xml_to_file(obj, filename: str):65 with open(f'{filename}', 'w+', encoding="utf8") as f:66 f.write(obj_to_xml(obj))67 f.close()686970if __name__ == '__main__':71 obj = [{3: 4}, ([9])]72 print(obj)73 print(obj_to_xml(obj))
...
io.py
Source:io.py
...19 """Returns xml for a ParameterList"""20 plist = amanzi_xml.common.parameter_list.ParameterList(par.name)21 for p in par.get().parameters():22 if p.has_value():23 plist.append(obj_to_xml(p))24 return plist25def obj_to_xml(par):26 """Returns xml for a Parameter or ParameterList"""27 if par.is_primitive():28 return primitive_to_xml(par)29 else:30 return derived_to_xml(par)31def to_xml(main):32 """Returns xml for a full ATS spec."""33 main_par = ats_input_spec.specs.Parameter('Main', value=main)34 if not main_par.is_complete():35 warnings.warn('Creating an incomplete XML object, missing entries!')36 return obj_to_xml(main_par)37def write(main, filename):38 """Write xml file for a full ATS spec."""39 xml = to_xml(main)40 amanzi_xml.utils.io.toFile(xml, filename)41 ...
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!!