fork download
  1. import timeit
  2.  
  3. response_content = {u'retrievePolicyBillingSummariesResponse': {u'billingSummaries': {u'policyBillingSummary': [{u'policy': {u'status': u'A', u'policyNumber': u'xyz123', u'writingCompany': u'FBI', u'renewalFlag': u'false', u'convertedRenewalOffer': u'false', u'termExpirationDate': u'2017-06-26', u'lineOfBusiness': u'PC', u'termEffectiveDate': u'2016-06-26', u'riskState': u'CA', u'insureds': {u'namedInsuredSummary': [{u'preferredPostalAddress': {u'streetAddressLine': u'1 disney', u'cityName': u'palo alto', u'zipCode': u'94100', u'isoRegionCode': u'CA'}, u'name': {u'lastName': u'DOE', u'fullName': u'john doe', u'firstName': u'john'}}]}, u'additionalInterests': {u'additionalInterest': [{u'billTo': u'N', u'name': {u'partyType': u'Organization'}}]}, u'type': u'PA', u'statusDescription': u'Active', u'dataSource': u'from_heaven'}, u'billingSummary': {u'paymentRestriction': u'false', u'nextInstallmentAmount': u'0.00', u'bill': {u'installmentNumber': u'1', u'statementDate': u'2016-06-26', u'paymentPlan': u'Direct', u'installmentAmount': u'12.00', u'totalBillAmountDue': u'1.76', u'previousBalance': u'0.00', u'dueDate': u'2016-06-26', u'billingPlan': u'ANN'}, u'lastPayment': {u'status': u'A'}, u'currentBalance': u'16.66', u'payOffAmount': u'15.66', u'isRestrictedToPay': u'false'}}]}}}
  4.  
  5. my_path = [
  6. "retrievePolicyBillingSummariesResponse",
  7. "billingSummaries",
  8. "policyBillingSummary",
  9. 0,
  10. "billingSummary",
  11. "lastPayment",
  12. "status"
  13. ]
  14.  
  15. def tne_get_value_from_content(extraction, my_path):
  16. for el in my_path:
  17. if isinstance(extraction, dict):
  18. extraction = extraction.get(el, extraction)
  19. else:
  20. extraction = extraction[el]
  21. return extraction
  22. tne_perf = timeit.timeit('tne_get_value_from_content(response_content, my_path)',
  23. setup='from __main__ import tne_get_value_from_content, response_content, my_path')
  24.  
  25.  
  26. def abc_get_value_from_content(response_content, my_path):
  27. if my_path:
  28. return abc_get_value_from_content(response_content[my_path[0]], my_path[1:])
  29. return response_content
  30. abc_perf = timeit.timeit('abc_get_value_from_content(response_content, my_path)',
  31. setup='from __main__ import abc_get_value_from_content, response_content, my_path')
  32.  
  33.  
  34.  
  35. def mgi_get_value_from_content(response_content, my_path):
  36. if len(my_path) > 1:
  37. return mgi_get_value_from_content(response_content[my_path[0]], my_path[1:])
  38. else:
  39. return response_content[my_path[0]]
  40. mgi_perf = timeit.timeit('mgi_get_value_from_content(response_content, my_path)',
  41. setup='from __main__ import mgi_get_value_from_content, response_content, my_path')
  42.  
  43. print('mgig :{perf}| {comp}% slower'.format(perf=mgi_perf, comp=100*(-1+mgi_perf/tne_perf)))
  44. print('abccd :{perf}| {comp}% slower'.format(perf=abc_perf, comp=100*(-1+abc_perf/tne_perf)))
  45. print('tnerual:{perf}'.format(perf=tne_perf))
  46.  
Success #stdin #stdout 3.78s 28384KB
stdin
Standard input is empty
stdout
mgig   :1.4471175940707326| 39.97546408427828% slower
abccd  :1.2906668449286371| 24.84243943774951% slower
tnerual:1.0338366109644994