Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created July 11, 2020 23:49
Show Gist options
  • Save kalda341/fca7d354ea2c7172948f6b97a4a24c32 to your computer and use it in GitHub Desktop.
Save kalda341/fca7d354ea2c7172948f6b97a4a24c32 to your computer and use it in GitHub Desktop.
from functools import partial
from stringcase import snakecase
def recursively_convert_keys(conversion_fun, data):
"""
The first major change we're making is pulling the type check out of the loop. This reduces the number of cases
which we need to handle, and allows us to run the conversion with a dictionary or a list.
The second change is that we're not mutating the original input. This simplifies things because we don't have
to delete the old keys, and we don't need to worry about breaking things in code elsewhere.
"""
if isinstance(data, dict):
# Convert keys and recursively process values
return { conversion_fun(k): recursively_convert_keys(conversion_fun, v) for k, v in data.items() }
elif isinstance(data, list):
# Recursively process values
return [recursively_convert_keys(conversion_fun, x) for x in data]
else:
# If data isn't a dictionary or a list then it can be left unchanged
return data
# Here we define the function we're actually going to use - which is just a recursive conversion
# bound with snakecase as the conversion function
convert_to_snakecase = partial(recursively_convert_keys, snakecase)
a = { "entityStatusCode": "80", "entityName": "ASIA PACIFIC DISTRIBUTORS INVESTMENTS NO 2 LIMITED", "nzbn": "9429035530732", "entityTypeCode": "LTD", "entityTypeDescription": "NZ Limited Company", "entityStatusDescription": "Removed", "registrationDate": "2003-11-20T00:00:00.000+1300", "sourceRegister": "COMPANY", "sourceRegisterUniqueIdentifier": "1447962", "lastUpdatedDate": "2013-10-18T10:54:28.000+1300", "australianBusinessNumber": None, "emailAddresses": [], "addresses": { "links": [], "addressList": [ { "uniqueIdentifier": "7020375", "startDate": "2003-11-20T00:00:00.000+1300", "endDate": None, "careOf": None, "address1": "87 Braemar Road", "address2": "Castor Bay", "address3": "Auckland", "address4": None, "postCode": None, "countryCode": "NZ", "addressType": "REGISTERED", "pafId": None }, { "uniqueIdentifier": "7020374", "startDate": "2003-11-20T00:00:00.000+1300", "endDate": None, "careOf": None, "address1": "87 Braemar Road", "address2": "Castor Bay", "address3": "Auckland", "address4": None, "postCode": None, "countryCode": "NZ", "addressType": "SERVICE", "pafId": None} ] }, "industryClassifications": [], "phoneNumbers": [], "websites": [], "tradingNames": [], "company-details": { "annualReturnFilingMonth": None, "financialReportFilingMonth": None, "nzsxCode": None, "annualReturnLastFiled": "2007-02-27T15:38:21.000+1300", "hasConstitutionFiled": True, "countryOfOrigin": "NZ", "overseasCompany": "No", "extensiveShareholding": False, "stockExchangeListed": None, "shareholding": { "numberOfShares": 100, "shareAllocation": [ { "allocation": 100, "shareholder": [ { "type": "entity", "appointmentDate": "2003-11-20T00:00:00.000+1300", "vacationDate": None, "individualShareholder": None, "otherShareholder": { "currentEntityName": "951456 - ASIA PACIFIC DISTRIBUTORS INVESTMENTS LIMITED", "nzbn": "9429033563992", "companyNumber": "951456", "entityType": "LTD" }, "shareholderAddress": None} ] } ], "historicShareholder": [] }, "ultimateHoldingCompany": None, "australianCompanyNumber": None, "insolvencies": [], "removalCommenced": False}, "roles": [ { "roleType": "Director", "roleStatus": "ACTIVE", "startDate": "2003-11-20T00:00:00.000+1300", "endDate": None, "asicDirectorshipYn": False, "asicName": None, "acn": None, "roleEntity": None, "rolePerson": { "title": None, "firstName": "Clemens", "middleNames": "Albert", "lastName": "WETZELL" }, "roleAddress": [ { "uniqueIdentifier": "9601742", "startDate": "2003-11-20T00:00:00.000+1300", "endDate": None, "careOf": None, "address1": "87 Braemar Road", "address2": "Castor Bay", "address3": "Auckland", "address4": None, "postCode": "0620", "countryCode": "NZ", "addressType": None, "pafId": "160056" } ], "roleAsicAddress": None, "uniqueIdentifier": "3325400" } ], "tradingAreas": [], "gstNumbers": [], "supporting-information": None, "organisationParts": None, "hibernationStatusCode": None, "hibernationStatusDescription": None}
print(a)
print(convert_to_snakecase(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment