Best Python code snippet using prospector_python
xcrixml2json.py
Source:xcrixml2json.py
...82def _remove_attributes(element):83 for key in element.keys():84 if key.startswith("@"):85 del element[key]86def _ensure_list(parent, listable):87 if not parent.has_key(listable):88 return89 if type(parent[listable]) != list:90 parent[listable] = [parent[listable]]91def _ensure_obj_to_text(parent, key, idx=None):92 if not parent.has_key(key):93 return94 95 if idx is not None:96 text = _extract_text(parent[key][idx])97 parent[key][idx] = text98 else:99 text = _extract_text(parent[key])100 parent[key] = text101def _ensure_text(parent, key):102 if not parent.has_key(key):103 return104 if type(parent[key]) == list:105 for i in range(len(parent[key])):106 parent[key] = _extract_text(parent[key][i])107 elif _is_dict(parent[key]):108 parent[key] = _extract_text(parent[key])109 110def _extract_text(element):111 if _is_dict(element):112 return _do_strip_cdata(element.get("#text", ""))113 else:114 return _do_strip_cdata(element)115def _remove_dud_text(element):116 if _is_dict(element):117 if element.has_key("#text"):118 del element["#text"]119def _prepend_namespace(parent, element_name, namespace, default=None):120 # look for reasons not to do this (the namespace prefix is already121 # applied, or the element doesn't exist to have it applied)122 if element_name.startswith(namespace + ":"):123 return124 if not parent.has_key(element_name):125 return126 127 # construct the name of the new key, with the namespace prefix128 new_key = namespace + ":" + element_name129 130 # initialise the new key, either using the default value or131 # around an existing value132 if not parent.has_key(new_key):133 parent[new_key] = default134 else:135 if type(default) == list:136 _ensure_list(parent, new_key)137 138 # now add or overwrite the value as necessary139 if type(parent[new_key]) == list:140 if type(parent[element_name]) == list:141 for e in parent[element_name]:142 parent[new_key].append(e)143 else:144 parent[new_key].append(parent[element_name])145 else:146 parent[new_key] = parent[element_name]147 148 # finally, remove the old dictionary entry149 del parent[element_name]150def _text_to_value(parent, element_name, idx=None):151 if not parent.has_key(element_name):152 return153 154 if idx is not None:155 element = parent[element_name][idx]156 if not _is_dict(element):157 nd = {"value" : _do_strip_cdata(element)}158 parent[element_name][idx] = nd159 else:160 element["value"] = _do_strip_cdata(element.get("#text", ""))161 del element["#text"]162 else:163 element = parent[element_name]164 if not _is_dict(element):165 nd = {"value" : _do_strip_cdata(element)}166 parent[element_name] = nd167 else:168 element["value"] = _do_strip_cdata(element.get("#text", ""))169 del element["#text"]170def _descriptive_text_element(parent, element, idx):171 if not parent.has_key(element):172 return173 e = parent[element][idx]174 if _is_dict(e):175 _dte_format(e)176 else:177 _text_to_value(parent, element, idx)178 _strip_cdata(parent[element][idx], "value")179def _temporal_element(parent, element):180 if not parent.has_key(element):181 return182 if _is_dict(parent[element]):183 _te_format(parent[element])184 else:185 _text_to_value(parent, element)186def _te_format(element):187 _rename_key(element, "@dtf", "dtf")188 _strip_cdata(element, "dtf")189 _rename_key(element, "#text", "value")190 _strip_cdata(element, "value")191 _remove_attributes(element)192def _dte_format(element):193 _rename_key(element, "@xml:lang", "lang")194 _strip_cdata(element, "lang")195 _rename_key(element, "@href", "href")196 _strip_cdata(element, "href")197 _rename_key(element, "#text", "value")198 _remove_attributes(element)199 _strip_cdata(element, "value")200def _strip_cdata(element, key):201 v = element.get(key, "")202 if v is None:203 return204 if v.startswith("<![CDATA["):205 v = v[9:]206 if v.endswith("]]>"):207 v = v[:-3]208 element[key] = v209def _do_strip_cdata(v):210 if v is None:211 return212 if v.startswith("<![CDATA["):213 v = v[9:]214 if v.endswith("]]>"):215 v = v[:-3]216 return v217def _rename_key(parent, original, new_key, format=None):218 if not parent.has_key(original):219 return220 if not parent.has_key(new_key):221 parent[new_key] = format222 223 # now add or overwrite the value as necessary224 if type(parent[new_key]) == list:225 if type(parent[original]) == list:226 for e in parent[original]:227 parent[new_key].append(e)228 else:229 parent[new_key].append(parent[original])230 else:231 parent[new_key] = parent[original] 232 233 del parent[original]234def _migrate_down(parent, target, to_move, format=None):235 if not parent.has_key(target):236 parent[target] = {}237 if parent.has_key(to_move):238 parent[target][to_move] = format239 if type(parent[to_move]) == list and type(format) == list:240 for a in parent[to_move]:241 parent[target][to_move].append(a)242 elif type(parent[to_move]) != list and type(format) == list:243 parent[target][to_move].append(parent[to_move])244 else:245 parent[target][to_move] = parent[to_move]246 del parent[to_move]247def upgrade_catalog(catalog):248 _ensure_list(catalog, "provider")249 for prov in catalog.get('provider', []):250 upgrade_provider(prov)251def upgrade_provider(prov):252 _migrate_down(prov, "mlo:location", "address", [])253 _migrate_down(prov, "mlo:location", "street")254 _migrate_down(prov, "mlo:location", "town")255 _migrate_down(prov, "mlo:location", "postcode")256 _migrate_down(prov, "mlo:location", "phone")257 _migrate_down(prov, "mlo:location", "fax")258 _migrate_down(prov, "mlo:location", "email")259 260 _ensure_list(provider, "course")261 for course in provider.get('course', []):262 cleanup_course(course)263def cleanup_catalog(catalog):264 """265 "catalog" : {266 "provider" : [ ]267 }268 """269 _remove_dud_text(catalog)270 _remove_attributes(catalog)271 _ensure_list(catalog, "provider")272 for prov in catalog.get('provider', []):273 cleanup_provider(prov)274def cleanup_provider(provider):275 """276 {277 "dc:contributor" : ["contributor"],278 "dc:description" : [{"lang" : "lang", "href" : "href", "value" : "value" }],279 "dc:identifier" : [{ "type" : "type", "value" : "value"}],280 "image" : {"src" : "src","title" : "title","alt" : "alt" },281 "dc:subject" : [{"type" : "type","identifier" : "identifier","lang" : "lang","value" : "value"}],282 "dc:title" : [{"lang" : "lang","value" : "value"}],283 "dc:type" : "type",284 "mlo:url" : "url",285 "mlo:location" : {286 "mlo:street" : "street",287 "mlo:town" : "town",288 "mlo:postcode" : "postcode",289 "mlo:phone" : "phone",290 "mlo:fax" : "fax",291 "mlo:email" : "email",292 "mlo:url" : "url",293 "mlo:address" : [{"type" : "type", "value" : "value"}]294 }295 "course" : [ ]296 }297 """298 _remove_dud_text(provider)299 300 _prepend_namespace(provider, "contributor", "dc", [])301 _ensure_list(provider, "dc:contributor")302 for i in range(len(provider.get("dc:contributor", []))):303 _ensure_obj_to_text(provider, "dc:contributor", i)304 305 _prepend_namespace(provider, "description", "dc", [])306 _ensure_list(provider, "dc:description")307 for i in range(len(provider.get('dc:description', []))):308 _descriptive_text_element(provider, "dc:description", i)309 310 _prepend_namespace(provider, "identifier", "dc", [])311 _ensure_list(provider, "dc:identifier")312 for i in range(len(provider.get('dc:identifier', []))):313 cleanup_identifier(provider, "dc:identifier", i)314 315 if provider.has_key("image"):316 cleanup_image(provider['image'])317 318 _prepend_namespace(provider, "subject", "dc", [])319 _ensure_list(provider, "dc:subject")320 for i in range(len(provider.get("dc:subject", []))):321 cleanup_subject(provider, "dc:subject", i)322 323 _prepend_namespace(provider, "title", "dc", [])324 _ensure_list(provider, "dc:title")325 for i in range(len(provider.get("dc:title", []))):326 cleanup_title(provider, "dc:title", i)327 328 _prepend_namespace(provider, "type", "dc")329 _ensure_obj_to_text(provider, "dc:type")330 331 _prepend_namespace(provider, "url", "mlo")332 _ensure_obj_to_text(provider, "mlo:url")333 334 _prepend_namespace(provider, "location", "mlo")335 if provider.has_key("mlo:location"):336 cleanup_location(provider, "mlo:location")337 338 _ensure_list(provider, "course")339 for course in provider.get('course', []):340 cleanup_course(course)341def cleanup_description(parent, desc, idx):342 # first see if we need to crosswalk to another element based on the field value343 # description.type=aim -> mlo:objective344 # description.type=applicationProcedure -> applicationProcedure345 # description.type=assessmentStrategy -> mlo:assessment346 # description.type=learningOutcome -> learningOutcome347 # description.type=prerequisites -> mlo:prerequisite348 # description.type=regulations -> regulations349 # some of these 1.1 to 1.2 crosswalks are encoded in the dc:description field350 # like dc:description = "aim: to do something"351 352 # all the target formats are descriptive text elements, so do that conversion first353 _descriptive_text_element(parent, desc, idx)354 355 if parent[desc][idx]['value'].strip().startswith("aim:"):356 _desc_copy(parent, "mlo:objective", desc, idx, "aim:")357 return True358 elif parent[desc][idx]['value'].strip().startswith("applicationProcedure:"):359 _desc_desc_copy(parent, "applicationProcedure", desc, idx, "applicationProcedure:")360 return True361 elif parent[desc][idx]['value'].strip().startswith("assessmentStrategy:"):362 _desc_copy(parent, "mlo:assessment", desc, idx, "assessmentStrategy:")363 return True364 elif parent[desc][idx]['value'].strip().startswith("learningOutcome:"):365 _desc_copy(parent, "learningOutcome", desc, idx, "learningOutcome:")366 return True367 elif parent[desc][idx]['value'].strip().startswith("prerequisites:"):368 _desc_copy(parent, "mlo:prerequisite", desc, idx, "prerequisites:")369 return True370 elif parent[desc][idx]['value'].strip().startswith("regulations:"):371 _desc_copy(parent, "regulations", desc, idx, "regulations:")372 return True373 return False374def _desc_copy(parent, target, source, idx, sub):375 parent[source][idx]['value'] = parent[source][idx]['value'][len(sub):].strip()376 if not parent.has_key(target):377 parent[target] = [parent[source][idx]]378 else:379 _ensure_list(parent, target)380 parent[target].append(parent[source][idx])381 382def cleanup_course(course):383 """384 {385 "mlo:level" : "level",386 "mlo:qualification" : [ ],387 "presentation" : [ ],388 "mlo:credit" : [{"credit:scheme" : "scheme", "credit:level" : "level", "credit:value" : "value"}],389 "dc:contributor" : ["contributor"],390 "dc:description" : [{"lang" : "lang", "href" : "href", "value" : "value"}],391 "dc:identifier" : [{"type" : "type","value" : "value"}],392 "image" : {"src" : "src","title" : "title","alt" : "alt"},393 "dc:subject" : [{"type" : "type","identifier" : "identifier","lang" : "lang","value" : "value"}],394 "dc:title" : [{"lang" : "lang","value" : "value"}],395 "dc:type" : "type",396 "mlo:url" : "url",397 "abstract" : [{"lang" : "lang", "href" : "href", "value" : "value"}], 398 "applicationProcedure" : [{"lang" : "lang", "href" : "href", "value" : "value"}],399 "mlo:assessment" : [{"lang" : "lang", "href" : "href", "value" : "value"}],400 "learningOutcome" : [{"lang" : "lang", "href" : "href", "value" : "value"}],401 "mlo:objective" : [{"lang" : "lang", "href" : "href", "value" : "value"}],402 "mlo:prerequisite" : [{"lang" : "lang", "href" : "href", "value" : "value"}],403 "regulations" : [{"lang" : "lang", "href" : "href", "value" : "value"}]404 }405 """406 _remove_dud_text(course)407 408 _prepend_namespace(course, "level", "mlo")409 _ensure_text(course, "mlo:level")410 411 _prepend_namespace(course, "qualification", "mlo", [])412 _ensure_list(course, "mlo:qualification")413 for q in course.get('mlo:qualification', []):414 cleanup_qualification(q)415 416 _ensure_list(course, "presentation")417 for p in course.get("presentation", []):418 cleanup_presentation(p)419 420 _prepend_namespace(course, "credit", "mlo", [])421 _ensure_list(course, "mlo:credit")422 for c in course.get("mlo:credit", []):423 cleanup_credit(c)424 425 _prepend_namespace(course, "contributor", "dc", [])426 _ensure_list(course, "dc:contributor")427 for i in range(len(course.get("dc:contributor", []))):428 _ensure_obj_to_text(course, "dc:contributor", i)429 430 _prepend_namespace(course, "description", "dc", [])431 _ensure_list(course, "dc:description")432 removable_descs = []433 for i in range(len(course.get('dc:description', []))):434 remove = cleanup_description(course, "dc:description", i)435 if remove:436 removable_descs.append(i)437 438 removable_descs.sort(reverse=True)439 for i in removable_descs:440 del course['dc:description'][i]441 442 _prepend_namespace(course, "identifier", "dc", [])443 _ensure_list(course, "dc:identifier")444 for i in range(len(course.get('dc:identifier', []))):445 cleanup_identifier(course, "dc:identifier", i)446 447 if course.has_key("image"):448 cleanup_image(course[image])449 450 _prepend_namespace(course, "subject", "dc", [])451 _ensure_list(course, "dc:subject")452 for i in range(len(course.get("dc:subject", []))):453 cleanup_subject(course, "dc:subject", i)454 455 _prepend_namespace(course, "title", "dc", [])456 _ensure_list(course, "dc:title")457 for i in range(len(course.get("dc:title", []))):458 cleanup_title(course, "dc:title", i)459 460 _prepend_namespace(course, "type", "dc")461 _ensure_obj_to_text(course, "dc:type")462 463 _prepend_namespace(course, "url", "mlo")464 _ensure_obj_to_text(course, "mlo:url")465 466 _ensure_list(course, "abstract")467 for i in range(len(course.get('abstract', []))):468 _descriptive_text_element(course, "abstract", i)469 470 _ensure_list(course, "applicationProcedure")471 for i in range(len(course.get('applicationProcedure', []))):472 _descriptive_text_element(course, "applicationProcedure", i)473 474 _prepend_namespace(course, "assessment", "mlo", [])475 _ensure_list(course, "mlo:assessment")476 for i in range(len(course.get('mlo:assessment', []))):477 _descriptive_text_element(course, "mlo:assessment", i)478 479 _ensure_list(course, "learningOutcome")480 for i in range(len(course.get('learningOutcome', []))):481 _descriptive_text_element(course, "learningOutcome", i)482 483 _prepend_namespace(course, "objective", "mlo", [])484 _ensure_list(course, "mlo:objective")485 for i in range(len(course.get('mlo:objective', []))):486 _descriptive_text_element(course, "mlo:objective", i)487 488 _prepend_namespace(course, "prerequisite", "mlo", [])489 _ensure_list(course, "mlo:prerequisite")490 for i in range(len(course.get('mlo:prerequisite', []))):491 _descriptive_text_element(course, "mlo:prerequisite", i)492 493 _ensure_list(course, "regulations")494 for i in range(len(course.get('regulations', []))):495 _descriptive_text_element(course, "regulations", i)496def cleanup_location(parent, location):497 """498 {499 "mlo:street" : "street",500 "mlo:town" : "town",501 "mlo:postcode" : "postcode",502 "mlo:phone" : "phone",503 "mlo:fax" : "fax",504 "mlo:email" : "email",505 "mlo:url" : "url",506 "mlo:address" : [{"type" : "type", "value" : "value"}]507 }508 """509 loc = parent[location]510 _remove_dud_text(loc)511 512 _prepend_namespace(loc, "street", "mlo")513 _ensure_text(loc, "mlo:street")514 515 _prepend_namespace(loc, "town", "mlo")516 _ensure_text(loc, "mlo:town")517 518 _prepend_namespace(loc, "postcode", "mlo")519 _ensure_text(loc, "mlo:postcode")520 521 _prepend_namespace(loc, "phone", "mlo")522 _ensure_text(loc, "mlo:phone")523 524 _prepend_namespace(loc, "fax", "mlo")525 _ensure_text(loc, "mlo:fax")526 527 _prepend_namespace(loc, "email", "mlo")528 _ensure_text(loc, "mlo:email")529 530 _prepend_namespace(loc, "url", "mlo")531 _ensure_text(loc, "mlo:url")532 533 _prepend_namespace(loc, "address", "mlo")534 _ensure_list(loc, "mlo:address")535 for i in range(len(loc.get("mlo:address", []))):536 cleanup_address(loc, "mlo:address", i)537 538def cleanup_address(parent, address, idx):539 """540 {"type" : "type", "value" : "value"}541 """542 _text_to_value(parent, address, idx)543 _rename_key(parent[address][idx], "@xsi:type", "type")544 _remove_attributes(parent[address][idx])545def cleanup_identifier(parent, identifier_key, idx):546 # convert to object and set the #text field correctly547 _text_to_value(parent, identifier_key, idx)548 _rename_key(parent[identifier_key][idx], "@xsi:type", "type")549 _remove_attributes(parent[identifier_key][idx])550def cleanup_title(parent, title, idx):551 """552 {"lang" : "lang","value" : "value"}553 """554 _text_to_value(parent, title, idx)555 _rename_key(parent[title][idx], "@xml:lang", "lang")556 _remove_attributes(parent[title][idx])557def cleanup_subject(parent, subject, idx):558 """559 {"type" : "type","identifier" : "identifier","lang" : "lang","value" : "value"}560 """561 _text_to_value(parent, subject, idx)562 _rename_key(parent[subject][idx], "@xsi:type", "type")563 _rename_key(parent[subject][idx], "@identifier", "identifier")564 _rename_key(parent[subject][idx], "@xml:lang", "lang")565 _remove_attributes(parent[subject][idx])566 if parent[subject][idx]['value'].startswith("LDCS class:"):567 parent[subject][idx]['value'] = parent[subject][idx]['value'][11:].strip()568 parent[subject][idx]['type'] = "LDCS"569 570def cleanup_image(image):571 """572 {"src" : "src","title" : "title","alt" : "alt" }573 """574 _remove_dud_text(image)575 _rename_key(image, "@src", "src")576 _rename_key(image, "@title", "title")577 _rename_key(image, "@alt", "alt")578 _remove_attributes(image)579 580def cleanup_credit(credit):581 """582 {"credit:scheme" : "scheme", "credit:level" : "level", "credit:value" : "value"}583 """584 _remove_dud_text(credit)585 _prepend_namespace(credit, "scheme", "credit")586 _ensure_text(credit, "credit:scheme")587 _prepend_namespace(credit, "level", "credit")588 _ensure_text(credit, "credit:level")589 _prepend_namespace(credit, "value", "credit")590 _ensure_text(credit, "credit:value")591 _remove_attributes(credit)592def cleanup_presentation(pres):593 """594 {595 "dc:description" : [{"lang" : "lang", "href" : "href", "value" : "value"}],596 "dc:identifier" : [{"type" : "type","value" : "value"}],597 "image" : {"src" : "src","title" : "title","alt" : "alt"},598 "dc:subject" : [{"type" : "type","identifier" : "identifier","lang" : "lang","value" : "value"}],599 "dc:title" : [{"lang" : "lang","value" : "value"}],600 "dc:type" : "type",601 "mlo:url" : "url",602 "abstract" : [{"lang" : "lang", "href" : "href", "value" : "value"}], 603 "applicationProcedure" : [{"lang" : "lang", "href" : "href", "value" : "value"}],604 "mlo:assessment" : [{"lang" : "lang", "href" : "href", "value" : "value"}],605 "learningOutcome" : [{"lang" : "lang", "href" : "href", "value" : "value"}],606 "mlo:objective" : [{"lang" : "lang", "href" : "href", "value" : "value"}],607 "mlo:prerequisite" : [{"lang" : "lang", "href" : "href", "value" : "value"}],608 "regulations" : [{"lang" : "lang", "href" : "href", "value" : "value"}]609 "mlo:start" : {"dtf" : "datetime", "value" : "value"},610 "mlo:end" : {"dtf" : "datetime", "value" : "value"},611 "mlo:duration" : {"interval" : "interval", "value" : "value"},612 "applyFrom" : {"dtf" : "datetime", "value" : "value"},613 "applyUntil" : {"dtf" : "datetime", "value" : "value"},614 "applyTo" : "apply to",615 "mlo:engagement" : [{}],616 "studyMode" : {"identifier" : "identifier", "value" : "value"},617 "attendanceMode" : {"identifier" : "identifier", "value" : "value"},618 "attendancePattern" : {"identifier" : "identifier", "value" : "value"},619 "mlo:languageOfInstruction" : ["lang"],620 "languageOfAssessment" : ["lang"],621 "mlo:places" : "places",622 "mlo:cost" : "cost",623 "age" : "age",624 "venue" : [ ]625 }626 """627 _remove_dud_text(pres)628 629 _prepend_namespace(pres, "description", "dc", [])630 _ensure_list(pres, "dc:description")631 for i in range(len(pres.get('dc:description', []))):632 _descriptive_text_element(pres, "dc:description", i)633 634 _prepend_namespace(pres, "identifier", "dc", [])635 _ensure_list(pres, "dc:identifier")636 for i in range(len(pres.get('dc:identifier', []))):637 cleanup_identifier(pres, "dc:identifier", i)638 639 if pres.has_key("image"):640 cleanup_image(pres[image])641 642 _prepend_namespace(pres, "subject", "dc", [])643 _ensure_list(pres, "dc:subject")644 for i in range(len(pres.get("dc:subject", []))):645 cleanup_subject(pres, "dc:subject", i)646 647 _prepend_namespace(pres, "title", "dc", [])648 _ensure_list(pres, "dc:title")649 for i in range(len(pres.get("dc:title", []))):650 cleanup_title(pres, "dc:title", i)651 652 _prepend_namespace(pres, "type", "dc")653 _ensure_obj_to_text(pres, "dc:type")654 655 _prepend_namespace(pres, "url", "mlo")656 _ensure_obj_to_text(pres, "mlo:url")657 658 _ensure_list(pres, "abstract")659 for i in range(len(pres.get('abstract', []))):660 _descriptive_text_element(pres, "abstract", i)661 662 _ensure_list(pres, "applicationProcedure")663 for i in range(len(pres.get('applicationProcedure', []))):664 _descriptive_text_element(pres, "applicationProcedure", i)665 666 _prepend_namespace(pres, "assessment", "mlo", [])667 _ensure_list(pres, "mlo:assessment")668 for i in range(len(pres.get('mlo:assessment', []))):669 _descriptive_text_element(pres, "mlo:assessment", i)670 671 _ensure_list(pres, "learningOutcome")672 for i in range(len(pres.get('learningOutcome', []))):673 _descriptive_text_element(pres, "learningOutcome", i)674 675 _prepend_namespace(pres, "objective", "mlo", [])676 _ensure_list(pres, "mlo:objective")677 for i in range(len(pres.get('mlo:objective', []))):678 _descriptive_text_element(pres, "mlo:objective", i)679 680 _prepend_namespace(pres, "prerequisite", "mlo", [])681 _ensure_list(pres, "mlo:prerequisite")682 for i in range(len(pres.get('mlo:prerequisite', []))):683 _descriptive_text_element(pres, "mlo:prerequisite", i)684 685 _ensure_list(pres, "regulations")686 for i in range(len(pres.get('regulations', []))):687 _descriptive_text_element(pres, "regulations", i)688 689 _prepend_namespace(pres, "start", "mlo")690 _temporal_element(pres, "mlo:start")691 692 _prepend_namespace(pres, "end", "mlo")693 _temporal_element(pres, "mlo:end")694 695 _prepend_namespace(pres, "duration", "mlo")696 _temporal_element(pres, "mlo:duration")697 698 _temporal_element(pres, "applyFrom")699 700 _temporal_element(pres, "applyUntil")701 702 if pres.has_key("applyTo"):703 _ensure_text(pres, "applyTo")704 705 # not enough information about mlo:engagement to do any serious cleanup on it706 _prepend_namespace(pres, "engagement", "mlo", [])707 708 if pres.has_key("studyMode"):709 cleanup_mode(pres, "studyMode")710 711 if pres.has_key("attendanceMode"):712 cleanup_mode(pres, "attendanceMode")713 714 if pres.has_key("attendancePattern"):715 cleanup_mode(pres, "attendancePattern")716 717 _prepend_namespace(pres, "languageOfInstruction", "mlo", [])718 _ensure_list(pres, "mlo:languageOfInstruction")719 for i in range(len(pres.get("mlo:languageOfInstruction", []))):720 _ensure_obj_to_text(pres, "mlo:languageOfInstruction", i)721 722 _ensure_list(pres, "languageOfAssessment")723 for i in range(len(pres.get("languageOfAssessment", []))):724 _ensure_obj_to_text(pres, "languageOfAssessment", i)725 726 _prepend_namespace(pres, "places", "mlo")727 _ensure_text(pres, "mlo:places")728 729 _prepend_namespace(pres, "cost", "mlo")730 _ensure_text(pres, "mlo:cost")731 732 _prepend_namespace(pres, "age", "mlo")733 _ensure_text(pres, "mlo:age")734 735 _ensure_list(pres, "venue")736 for i in range(len(pres.get("venue", []))):737 cleanup_venue(pres, "venue", i)738def cleanup_venue(parent, venue, idx):739 element = parent[venue][idx]740 _remove_dud_text(element)741 if not _is_dict(element):742 # not spec conformant, so delete743 del parent[venue][idx]744 return745 if element.has_key("provider"):746 cleanup_provider(element["provider"])747def cleanup_mode(parent, mode):748 """749 {"identifier" : "identifier", "value" : "value"}750 """751 _text_to_value(parent, mode)752 _rename_key(parent[mode], "@identifier", "identifier")753 _remove_attributes(parent[mode])754def cleanup_qualification(qual):755 """756 {757 "dc:identifier" : [{"type" : "type","value" : "value"}],758 "dc:title" : [{"lang" : "lang","value" : "value"}],759 "abbr" : "abbr",760 "dc:description" : [{"lang" : "lang","href" : "href","value" : "value"}],761 "dcterms:educationLevel" : ["education level"],762 "dc:type" : "type",763 "mlo:url" : "url",764 "awardedBy" : "awarded by",765 "accreditedBy" : "accredited by"766 }767 """768 _remove_dud_text(qual)769 770 _prepend_namespace(qual, "identifier", "dc", [])771 _ensure_list(qual, "dc:identifier")772 for i in range(len(qual.get('dc:identifier', []))):773 cleanup_identifier(qual, "dc:identifier", i)774 775 _prepend_namespace(qual, "title", "dc", [])776 _ensure_list(qual, "dc:title")777 for i in range(len(qual.get("dc:title", []))):778 cleanup_title(qual, "dc:title", i)779 780 if qual.has_key("abbr"):781 _ensure_text(qual, "abbr")782 783 _prepend_namespace(qual, "description", "dc", [])784 _ensure_list(qual, "dc:description")785 for i in range(len(qual.get('dc:description', []))):786 _descriptive_text_element(qual, "dc:description", i)787 788 _prepend_namespace(qual, "educationLevel", "dcterms", [])789 _ensure_list(qual, "dcterms:educationLevel")790 for i in range(len(qual.get("dcterms:educationLevel", []))):791 _ensure_obj_to_text(qual, "dcterms:educationLevel", i)792 793 _prepend_namespace(qual, "type", "dc")794 _ensure_obj_to_text(qual, "dc:type")795 796 _prepend_namespace(qual, "url", "mlo")797 _ensure_obj_to_text(qual, "mlo:url")798 799 # we don't know enough about awardedBy and accreditedBy, so 800 # just leave them alone801if __name__ == "__main__":802 parser = argparse.ArgumentParser(description='Parse a directory of XCRI data files')803 parser.add_argument('-d', help='Source directory of XCRI XML files', required=True)...
data.py
Source:data.py
2import numpy as np3import pandas as pd4import scipy as sp5import scipy.signal6def _ensure_list(data):7 if type(data) is not list:8 data = [data]9 return data10'''11PEAKS12'''13def find_peaks(data, distance, inv=False):14 if inv:15 peaks, _ = sp.signal.find_peaks(1./data, distance=distance)16 else:17 peaks, _ = sp.signal.find_peaks(data, distance=distance)18 return peaks19def find_peaks_savgol(data, distance, inv=False, polyorder=3):20 # distance needs to be an odd int21 if distance % 2 == 0:22 distance = distance + 123 # smooth data24 if inv:25 s = scipy.signal.savgol_filter(1/data, distance, polyorder)26 else:27 s = scipy.signal.savgol_filter(data, distance, polyorder)28 # get peaks29 peaks = scipy.signal.argrelmax(s)[0]30 return peaks31'''32SUPPORT RESISTANCE33'''34def support_resistance(ltp, n):35 '''36 This function takes a numpy array of last traded price37 and returns a list of support and resistance levels38 respectively. n is the number of entries to be scanned.39 Params:40 - ltp: list with data (close)41 - n: smooth period (distance)42 Returns:43 - support: Support levels44 - resistance: Resistance levels45 '''46 from scipy.signal import savgol_filter as smooth47 # converting n to a nearest even number48 if n % 2 != 0:49 n += 150 n_ltp = ltp.shape[0]51 # smoothening the curve52 ltp_s = smooth(ltp, int(n+1), 3)53 # taking a simple derivative54 ltp_d = np.zeros(n_ltp)55 ltp_d[1:] = np.subtract(ltp_s[1:], ltp_s[:-1])56 resistance = []57 support = []58 for i in range(n_ltp - n):59 arr_sl = ltp_d[i:int(i+n)]60 first = arr_sl[:int(n/2)] # first half61 last = arr_sl[int(n/2):] # second half62 r_1 = np.sum(first > 0)63 r_2 = np.sum(last < 0)64 s_1 = np.sum(first < 0)65 s_2 = np.sum(last > 0)66 # local maxima detection67 if (r_1 == (n/2)) and (r_2 == int(n/2)):68 resistance.append(ltp[i+(int(n/2)-1)])69 # local minima detection70 if (s_1 == int(n/2)) and (s_2 == int(n/2)):71 support.append(ltp[i+(int(n/2)-1)])72 return support, resistance73'''74TRENDY TRENDS75'''76def segtrends(x, segments=2):77 '''78 Turn minitrends to iterative process more easily adaptable to79 implementation in simple trading systems; allows backtesting functionality.80 Arguments:81 x -- One-dimensional data set82 segments -- (Default 2)83 Example:84 import matplotlib.pyplot as plt85 x_maxima, maxima, x_minima, minima = segtrends(x, segments=segments)86 plt.plot(x)87 plt.grid(True)88 for i in range(0, segments - 1):89 maxslope = ((maxima[i + 1] - maxima[i])90 / (x_maxima[i + 1] - x_maxima[i]))91 a_max = maxima[i] - (maxslope * x_maxima[i])92 b_max = maxima[i] + (maxslope * (len(x) - x_maxima[i]))93 maxline = np.linspace(a_max, b_max, len(x))94 minslope = ((minima[i + 1] - minima[i])95 / (x_minima[i+1] - x_minima[i]))96 a_min = minima[i] - (minslope * x_minima[i])97 b_min = minima[i] + (minslope * (len(x) - x_minima[i]))98 minline = np.linspace(a_min, b_min, len(x))99 plt.plot(maxline, "g")100 plt.plot(minline, "r")101 plt.show()102 '''103 x = np.array(x)104 # Implement trendlines105 segments = int(segments)106 segsize = int(len(x) / segments)107 maxima = np.ones(segments)108 minima = np.ones(segments)109 x_maxima = np.ones(segments)110 x_minima = np.ones(segments)111 for i in range(1, segments + 1):112 ind2 = i * segsize113 ind1 = ind2 - segsize114 maxima[i-1] = max(x[ind1:ind2])115 minima[i-1] = min(x[ind1:ind2])116 x_maxima[i-1] = np.where(x[ind1:ind2] == maxima[i-1])[0][0] + ind1117 x_minima[i-1] = np.where(x[ind1:ind2] == minima[i-1])[0][0] + ind1118 for i in range(1, segments+1):119 ind2 = i * segsize120 ind1 = ind2 - segsize121 return x_maxima, maxima, x_minima, minima122def gentrends(x, window=1/3.0):123 '''124 Returns a Pandas dataframe with support and resistance lines.125 Arguments:126 x -- One-dimensional data set127 window -- How long the trendlines should be. If window < 1, then128 it will be taken as a percentage of the size of the129 data (Default 1/3.)130 Example:131 import matplotlib.pyplot as plt132 trends, maxslope, minslope = gentrends(x, window=window)133 plt.plot(trends)134 plt.grid()135 plt.show()136 '''137 x = np.array(x)138 if window < 1:139 window = int(window * len(x))140 max1 = np.where(x == max(x))[0][0] # find the index of the abs max141 min1 = np.where(x == min(x))[0][0] # find the index of the abs min142 # First the max143 if max1 + window >= len(x):144 max2 = max(x[0:max(1, (max1 - window))])145 else:146 max2 = max(x[(max1 + window):])147 # Now the min148 if min1 - window < 0:149 min2 = min(x[(min1 + window):])150 else:151 min2 = min(x[0:max(1, (min1 - window))])152 # Now find the indices of the secondary extrema153 max2 = np.where(x == max2)[0][0] # find the index of the 2nd max154 min2 = np.where(x == min2)[0][0] # find the index of the 2nd min155 # Create & extend the lines156 # slope between max points157 if max1 - max2 != 0:158 maxslope = (x[max1] - x[max2]) / (max1 - max2)159 else:160 maxslope = x[max1] - x[max2]161 # slope between min points162 if min1 - min2 != 0:163 minslope = (x[min1] - x[min2]) / (min1 - min2)164 else:165 minslope = x[min1] - x[min2]166 aMax = x[max1] - (maxslope * max1) # y-intercept for max trendline167 aMin = x[min1] - (minslope * min1) # y-intercept for min trendline168 bMax = x[max1] + (maxslope * (len(x) - max1)) # extend to last data pt169 bMin = x[min1] + (minslope * (len(x) - min1)) # extend to last data point170 maxline = np.linspace(171 aMax,172 bMax,173 len(x),174 endpoint=False) # Y values between max's175 minline = np.linspace(176 aMin,177 bMin,178 len(x),179 endpoint=False) # Y values between min's180 trends = np.transpose(np.array((x, maxline, minline)))181 trends = pd.DataFrame(trends, index=np.arange(0, len(x)),182 columns=["Data", "Max Line", "Min Line"])183 return trends, maxslope, minslope184def minitrends(x, window=20):185 '''186 Turn minitrends to iterative process more easily adaptable to187 implementation in simple trading systems; allows backtesting188 functionality.189 Arguments:190 x -- One-dimensional data set191 window -- How long the trendlines should be. If window < 1, then192 it will be taken as a percentage of the size of the193 data (Default 20)194 Example:195 import matplotlib.pyplot as plt196 trends, maxslope, minslope = gentrends(x, window=window)197 plt.plot(trends)198 plt.grid()199 plt.show()200 '''201 y = np.array(x)202 if window < 1: # if window is given as fraction of data length203 window = float(window)204 window = int(window * len(y))205 x = np.arange(0, len(y))206 dy = y[window:] - y[:-window]207 crit = dy[:-1] * dy[1:] < 0208 xmax = np.array([])209 xmin = np.array([])210 # Find whether max's or min's211 for i, val in enumerate(crit):212 if val is True:213 if (y[i] - y[i + window] > 0) and (y[i] - y[i - window] > 0):214 xmax = np.append(xmax, i)215 if (y[i] - y[i + window] < 0) and (y[i] - y[i - window] < 0):216 xmin = np.append(xmin, i)217 xmax = xmax.astype(int)218 xmin = xmin.astype(int)219 # See if better max or min in region220 yMax = np.array([])221 xMax = np.array([])222 for i in xmax:223 indx = np.where(xmax == i)[0][0] + 1224 try:225 Y = y[i:xmax[indx]]226 yMax = np.append(yMax, Y.max())227 xMax = np.append(xMax, np.where(y == yMax[-1])[0][0])228 except Exception:229 pass230 yMin = np.array([])231 xMin = np.array([])232 for i in xmin:233 indx = np.where(xmin == i)[0][0] + 1234 try:235 Y = y[i:xmin[indx]]236 yMin = np.append(yMin, Y.min())237 xMin = np.append(xMin, np.where(y == yMin[-1])[0][0])238 except Exception:239 pass240 if y[-1] > yMax[-1]:241 yMax = np.append(yMax, y[-1])242 xMax = np.append(xMax, x[-1])243 if y[0] not in yMax:244 yMax = np.insert(yMax, 0, y[0])245 xMax = np.insert(xMax, 0, x[0])246 if y[-1] < yMin[-1]:247 yMin = np.append(yMin, y[-1])248 xMin = np.append(xMin, x[-1])249 if y[0] not in yMin:250 yMin = np.insert(yMin, 0, y[0])251 xMin = np.insert(xMin, 0, x[0])252 # Return arrays of critical points253 return xMin, yMin, xMax, yMax254def iterlines(x, window=20):255 '''256 Turn minitrends to iterative process more easily adaptable to257 implementation in simple trading systems; allows backtesting functionality.258 Arguments:259 x -- One-dimensional data set260 window -- How long the trendlines should be. If window < 1, then261 it will be taken as a percentage of the size of the262 data (Default 20)263 Example:264 import matplotlib.pyplot as plt265 sigs, xMin, yMin, xMax, yMax = iterlines(x, window=window)266 plt.plot(x)267 plt.plot(xMin, yMin, "ro")268 plt.plot(xMax, yMax, "go")269 plt.grid(True)270 plt.show()271 '''272 x = np.array(x)273 n = len(x)274 if window < 1:275 window = int(window * n)276 sigs = np.zeros(n, dtype=float)277 i = window278 while i != n:279 if x[i] > max(x[i-window:i]):280 sigs[i] = 1281 elif x[i] < min(x[i-window:i]):282 sigs[i] = -1283 i += 1284 xMin = np.where(sigs == -1.0)[0]285 xMax = np.where(sigs == 1.0)[0]286 yMin = x[xMin]287 yMax = x[xMax]288 return sigs, xMin, yMin, xMax, yMax289'''290PIVOT POINTS291'''292def pivot_points(highList, lowList, closeList):293 '''294 Returns the standard pivot points, three support levels (s1,s2 and s3)295 and three resistance levels (r1, r2 and r3) of the296 given data series.297 These values for a given day are calculated based on the day before298 so expect n values as output for a given list of n days.299 Standard Pivot Points begin with a base Pivot Point. This is a simple300 average of the high, low and close. The middle Pivot Point is shown as301 a solid line between the support and resistance pivots. Keep in mind that302 the high, low and close are all from the prior period.303 Params:304 - highList: list of high values305 - lowList: list of low values306 - closeList: list of closing values307 Returns:308 - p: pivot point309 - s1: support first point310 - s2: support second point311 - s3: support third point312 - r1: resistance first point313 - r2: resistance second point314 - r3: resistence third point315 '''316 # ensure np array is being used317 highList = np.array(_ensure_list(highList))318 lowList = np.array(_ensure_list(lowList))319 closeList = np.array(_ensure_list(closeList))320 # calculation321 p = (highList + lowList + closeList) / 3322 s1 = (2 * p) - highList323 s2 = p - highList + lowList324 s3 = s1 - highList + lowList325 r1 = (2 * p) - lowList326 r2 = p + highList - lowList327 r3 = r1 + highList - lowList328 # return lists with results329 return p, s1, s2, s3, r1, r2, r3330def tom_demark_points(openList, highList, lowList, closeList):331 '''332 Returns the Tom Demark points, the predicted low and highs333 of the period.334 These values for a given day are calculated based on the day before335 so expect n values as output for a given list of n days.336 Demark Pivot Points start with a different base and use different337 formulas for support and resistance. These Pivot Points are conditional338 on the relationship between the close and the open.339 If Close < Open, then X = High + (2 x Low) + Close340 If Close > Open, then X = (2 x High) + Low + Close341 If Close = Open, then X = High + Low + (2 x Close)342 Pivot Point (P) = X/4343 Support 1 (S1) = X/2 - High344 Resistance 1 (R1) = X/2 - Low345 Params:346 - openList: list of open values347 - highList: list of high values348 - lowList: list of low values349 - closeList: list of closing values350 Returns:351 - p: Pivot point352 - r1: Resistance 1353 - s1: Support 1354 '''355 # ensure np array is being used356 openList = np.array(_ensure_list(openList))357 highList = np.array(_ensure_list(highList))358 lowList = np.array(_ensure_list(lowList))359 closeList = np.array(_ensure_list(closeList))360 # calculation361 p = []362 s1 = []363 r1 = []364 for o, h, l, c in np.nditer([openList, highList, lowList, closeList]):365 if c < o:366 x = h + (2 * l) + c367 elif c > o:368 x = (2 * h) + l + c369 elif c == o:370 x = h + l + (2 * c)371 p.append(x / 4)372 s1.append((x / 2) - h)373 r1.append((x / 2) - l)374 # return lists with results375 return p, s1, r1376def woodies_points(highList, lowList, closeList):377 '''378 Returns the Woodies points: pivot, supports (s1 and s2) and379 resistance values (r1 and r2).380 These values for a given day are calculated based on the day before381 so expect n values as output for a given list of n days.382 * - p: pivot value.383 *384 Params:385 - highList: list of high values386 - lowList: list of low values387 - closeList: list of closing values388 Returns:389 - pl: pivot level390 - s1: support (s1)391 - s2: secondary support (s2)392 - r1: resistance (r1)393 - r2: secondary resistance (r2)394 '''395 # ensure np array is being used396 highList = np.array(_ensure_list(highList))397 lowList = np.array(_ensure_list(lowList))398 closeList = np.array(_ensure_list(closeList))399 # calculation400 p = (highList + lowList + 2 * closeList) / 4401 s1 = (2 * p) - highList402 s2 = p - highList + lowList403 r1 = (2 * p) - lowList404 r2 = p + highList - lowList405 # return lists with results406 return p, s1, s2, r1, r2407def camarilla_points(highList, lowList, closeList):408 '''409 Returns the Camarilla points: supports (s1,s2,3 and s4)) and410 resistance values (r1, r2, r3 and r4).411 Params:412 - highList: list of high values413 - lowList: list of low values414 - closeList: list of closing values415 Returns:416 - s1: s1 support417 - s2: s2 support418 - s3: s3 support419 - s4: s4 support420 - r1: r1 resistance421 - r2: r2 resistance422 - r3: r3 resistance423 - r4: r4 resistance424 '''425 # ensure np array is being used426 highList = np.array(_ensure_list(highList))427 lowList = np.array(_ensure_list(lowList))428 closeList = np.array(_ensure_list(closeList))429 # calculation430 diff = highList - lowList431 s1 = closeList - (diff * 1.1 / 12)432 s2 = closeList - (diff * 1.1 / 6)433 s3 = closeList - (diff * 1.1 / 4)434 s4 = closeList - (diff * 1.1 / 2)435 r1 = ((diff * 1.1) / 12) + closeList436 r2 = ((diff * 1.1) / 6) + closeList437 r3 = ((diff * 1.1) / 4) + closeList438 r4 = ((diff * 1.1) / 2) + closeList439 # return lists with results440 return s1, s2, s3, s4, r1, r2, r3, r4441def fibanocci_points(highList, lowList, closeList):442 '''443 Returns the fibanocci points: supports (s1,s2,3)) and444 resistance values (r1, r2, r3).445 Params:446 - highList: list of high values447 - lowList: list of low values448 - closeList: list of closing values449 Returns:450 - p: pivot point451 - s1: s1 support452 - s2: s2 support453 - s3: s3 support454 - r1: r1 resistance455 - r2: r2 resistance456 - r3: r3 resistance457 '''458 # ensure np array is being used459 highList = np.array(_ensure_list(highList))460 lowList = np.array(_ensure_list(lowList))461 closeList = np.array(_ensure_list(closeList))462 # calculation463 p = (highList + lowList + closeList) / 3464 s1 = p - (0.382 * (highList - lowList))465 s2 = p - (0.618 * (highList - lowList))466 s3 = p - (1.0 * (highList - lowList))467 r1 = p + (0.382 * (highList - lowList))468 r2 = p + (0.618 * (highList - lowList))469 r3 = p + (1.0 * (highList - lowList))470 # return lists with results471 return p, s1, s2, s3, r1, r2, r3472def fibonacci_retracements(highList, lowList):473 '''474 Returns the fibanocci retracements.475 Params:476 - highList: list of high values477 - lowList: list of low values478 Returns:479 - upTrend480 - downTrend481 '''482 # ensure np array is being used483 highList = np.array(_ensure_list(highList))484 lowList = np.array(_ensure_list(lowList))485 # calculation486 retracements = [1, 0.618, 0.5, 0.382, 0.236, 0]487 diff = highList - lowList488 upTrend = []489 downTrend = []490 for d, h, l in np.nditer([diff, highList, lowList]):491 up = h - d * retracements492 do = l + d * retracements493 upTrend.append(up)494 downTrend.append(do)495 # return lists with results...
cmd_runner.py
Source:cmd_runner.py
...5__metaclass__ = type6from functools import wraps7from ansible.module_utils.common.collections import is_sequence8from ansible.module_utils.six import iteritems9def _ensure_list(value):10 return list(value) if is_sequence(value) else [value]11def _process_as_is(rc, out, err):12 return rc, out, err13class CmdRunnerException(Exception):14 pass15class MissingArgumentFormat(CmdRunnerException):16 def __init__(self, arg, args_order, args_formats):17 self.args_order = args_order18 self.arg = arg19 self.args_formats = args_formats20 def __repr__(self):21 return "MissingArgumentFormat({0!r}, {1!r}, {2!r})".format(22 self.arg,23 self.args_order,24 self.args_formats,25 )26 def __str__(self):27 return "Cannot find format for parameter {0} {1} in: {2}".format(28 self.arg,29 self.args_order,30 self.args_formats,31 )32class MissingArgumentValue(CmdRunnerException):33 def __init__(self, args_order, arg):34 self.args_order = args_order35 self.arg = arg36 def __repr__(self):37 return "MissingArgumentValue({0!r}, {1!r})".format(38 self.args_order,39 self.arg,40 )41 def __str__(self):42 return "Cannot find value for parameter {0} in {1}".format(43 self.arg,44 self.args_order,45 )46class FormatError(CmdRunnerException):47 def __init__(self, name, value, args_formats, exc):48 self.name = name49 self.value = value50 self.args_formats = args_formats51 self.exc = exc52 super(FormatError, self).__init__()53 def __repr__(self):54 return "FormatError({0!r}, {1!r}, {2!r}, {3!r})".format(55 self.name,56 self.value,57 self.args_formats,58 self.exc,59 )60 def __str__(self):61 return "Failed to format parameter {0} with value {1}: {2}".format(62 self.name,63 self.value,64 self.exc,65 )66class _ArgFormat(object):67 def __init__(self, func, ignore_none=None):68 self.func = func69 self.ignore_none = ignore_none70 def __call__(self, value, ctx_ignore_none):71 ignore_none = self.ignore_none if self.ignore_none is not None else ctx_ignore_none72 if value is None and ignore_none:73 return []74 f = self.func75 return [str(x) for x in f(value)]76class _Format(object):77 @staticmethod78 def as_bool(args):79 return _ArgFormat(lambda value: _ensure_list(args) if value else [])80 @staticmethod81 def as_bool_not(args):82 return _ArgFormat(lambda value: [] if value else _ensure_list(args), ignore_none=False)83 @staticmethod84 def as_optval(arg, ignore_none=None):85 return _ArgFormat(lambda value: ["{0}{1}".format(arg, value)], ignore_none=ignore_none)86 @staticmethod87 def as_opt_val(arg, ignore_none=None):88 return _ArgFormat(lambda value: [arg, value], ignore_none=ignore_none)89 @staticmethod90 def as_opt_eq_val(arg, ignore_none=None):91 return _ArgFormat(lambda value: ["{0}={1}".format(arg, value)], ignore_none=ignore_none)92 @staticmethod93 def as_list(ignore_none=None):94 return _ArgFormat(_ensure_list, ignore_none=ignore_none)95 @staticmethod96 def as_fixed(args):97 return _ArgFormat(lambda value: _ensure_list(args), ignore_none=False)98 @staticmethod99 def as_func(func, ignore_none=None):100 return _ArgFormat(func, ignore_none=ignore_none)101 @staticmethod102 def as_map(_map, default=None, ignore_none=None):103 return _ArgFormat(lambda value: _ensure_list(_map.get(value, default)), ignore_none=ignore_none)104 @staticmethod105 def as_default_type(_type, arg="", ignore_none=None):106 fmt = _Format107 if _type == "dict":108 return fmt.as_func(lambda d: ["--{0}={1}".format(*a) for a in iteritems(d)],109 ignore_none=ignore_none)110 if _type == "list":111 return fmt.as_func(lambda value: ["--{0}".format(x) for x in value], ignore_none=ignore_none)112 if _type == "bool":113 return fmt.as_bool("--{0}".format(arg))114 return fmt.as_opt_val("--{0}".format(arg), ignore_none=ignore_none)115 @staticmethod116 def unpack_args(func):117 @wraps(func)118 def wrapper(v):119 return func(*v)120 return wrapper121 @staticmethod122 def unpack_kwargs(func):123 @wraps(func)124 def wrapper(v):125 return func(**v)126 return wrapper127class CmdRunner(object):128 """129 Wrapper for ``AnsibleModule.run_command()``.130 It aims to provide a reusable runner with consistent argument formatting131 and sensible defaults.132 """133 @staticmethod134 def _prepare_args_order(order):135 return tuple(order) if is_sequence(order) else tuple(order.split())136 def __init__(self, module, command, arg_formats=None, default_args_order=(),137 check_rc=False, force_lang="C", path_prefix=None, environ_update=None):138 self.module = module139 self.command = _ensure_list(command)140 self.default_args_order = self._prepare_args_order(default_args_order)141 if arg_formats is None:142 arg_formats = {}143 self.arg_formats = dict(arg_formats)144 self.check_rc = check_rc145 self.force_lang = force_lang146 self.path_prefix = path_prefix147 if environ_update is None:148 environ_update = {}149 self.environ_update = environ_update150 self.command[0] = module.get_bin_path(command[0], opt_dirs=path_prefix, required=True)151 for mod_param_name, spec in iteritems(module.argument_spec):152 if mod_param_name not in self.arg_formats:153 self.arg_formats[mod_param_name] = _Format.as_default_type(spec['type'], mod_param_name)...
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!!