Last active
December 19, 2020 07:31
-
-
Save onelharrison/8a0bdbab512e161a571109ef6041e36d to your computer and use it in GitHub Desktop.
Extract function for employee record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from dataclasses import dataclass | |
from typing import Optional | |
@dataclass | |
class Employee: | |
name: str | |
age: Optional[int] = None | |
job_title: str | |
updated_at: int | |
def extract_employee_record(employee_data): | |
employee = Employee() | |
employee.name = employee_data["name"] | |
employee.age = safe_cast(employee_data["age"], int) | |
employee.job_title = employee_data["job_title"] | |
employee.updated_at = int(time.time()) | |
return employee | |
employee_tamara = { | |
"name": "Tamara Rivers", | |
"age": "", # age is missing from the source system | |
"job_title": "Director of Engineering" | |
} | |
tamara = extract_employee_record(employee_tamara) | |
print(tamara.age) # None | |
employee_bianca = { | |
"name": "Bianca Brown", | |
"age": "39", | |
"job_title": "Chief Technology Officer" | |
} | |
bianca = extract_employee_record(employee_bianca) | |
print(bianca.age) # 39 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment