Skip to content

Instantly share code, notes, and snippets.

@mbiemann
Created September 27, 2022 20:51
Show Gist options
  • Save mbiemann/38620e98c0ee9b8ee5a36fdba130f524 to your computer and use it in GitHub Desktop.
Save mbiemann/38620e98c0ee9b8ee5a36fdba130f524 to your computer and use it in GitHub Desktop.
from json import dumps
from datetime import datetime
# ==============================================================================
def info(msg):
print(f"{datetime.now().isoformat(' ')[:19]} [INFO] {msg}")
def debug(msg):
print(f"{datetime.now().isoformat(' ')[:19]} [DEBUG] {msg}")
# ==============================================================================
def apply(data, value, path):
reverse = path.copy()
reverse.reverse()
tmp = {reverse.pop(0): value}
for x in reverse:
tmp = {x: tmp}
for x in path:
if x in data:
data = data[x]
tmp = tmp[x]
else:
data[x] = tmp[x]
break
def main():
info("START")
columns = {
"A": ["Applicant", "Name", "Forename"],
"B": ["Applicant", "Name", "Surname"],
"C": ["Applicant", "Gender"],
"D": ["Original DOB"],
"E": ["Field1", "Field2", "Field3", "Field4", "Field5"],
"F": ["Field1", "OtherA"],
}
input = [
{
"A": "Marcell",
"B": "Biemann",
"C": "Male",
"D": "26/02/1986",
"F": "abc",
},
{
"A": "Aline",
"B": "Biemann",
"C": "Female",
"D": "13/04/1988",
"E": "xpto",
},
]
data = []
for row in input:
data_row = {}
for col in row:
apply(data_row, row[col], columns[col])
data.append(data_row)
debug(f"data =\n{dumps(data, indent=2)}\n")
info("END")
# ==============================================================================
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment