from datetime import datetime from uuid import uuid4 # These are module level constants, so they will not change between rows during CalculateField. # However, it will change between runs, so if you want this to always be static, you can use the following format: # NOW = datetime(year=2019, month=1, day=1, hour=8, minute=35, second=26) NOW = datetime.now() NOW_UTC = datetime.utcnow() def create_guid() -> str: """ Creates a UUID that can be stored in a GUID field """ return f'{{{uuid4()}}}' def concatenate(*values: str, delimiter: str = " ") -> str: """ Concatenates multiple values together with a delimiter, removing Null values. Args: values: the values to join together. They will be cast to string before concatenation delimiter (str): the delimiter to join the fields with. Defaults to " " """ return delimiter.join(map(str, filter(None, values))) def year_to_date(year, month: int = 1, day: int = 1) -> datetime: """ Converts a 2 or 4 year date str/int representation into a datetime object """ return datetime.strptime(f'{year}/{month}/{day}', f"{'%y' if len(str(year)) == 2 else '%Y'}/%m/%d") def calculate_utility_status(MATERIAL_UTIL,INSTALLDATE_UTIL,RESYRBLT): if MATERIAL_UTIL is None and INSTALLDATE_UTIL is None and RESYRBLT is None: return 0 #Unknown elif MATERIAL_UTIL == 'LP' or (MATERIAL_UTIL is None and INSTALLDATE_UTIL is None and RESYRBLT <= 1986) or (MATERIAL_UTIL is None and INSTALLDATE_UTIL <= datetime(year=1986, month=12,day=31)): return 1 #Lead elif MATERIAL_UTIL != 'GP' and MATERIAL_UTIL != 'LP' and MATERIAL_UTIL is not None or (MATERIAL_UTIL is None and INSTALLDATE_UTIL is None and RESYRBLT >= 1987) or (MATERIAL_UTIL is None and INSTALLDATE_UTIL >= datetime(year=1987, month=1, day=1)): return 2 #Non-Lead elif MATERIAL_UTIL == 'GP': return 3 #Galvanized Requiring Replacement def calculate_customer_status(MATERIAL_CUST,INSTALLDATE_CUST,RESYRBLT): if MATERIAL_CUST is None and INSTALLDATE_CUST is None and RESYRBLT is None: return 0 #Unknown elif MATERIAL_CUST == 'LP' or (MATERIAL_CUST is None and INSTALLDATE_CUST is None and RESYRBLT <= 1986) or (MATERIAL_CUST is None and INSTALLDATE_CUST < datetime(year=1987, month=1,day=1)): return 1 #Lead elif MATERIAL_CUST != 'GP' and MATERIAL_CUST != 'LP' and MATERIAL_CUST is not None or (MATERIAL_CUST is None and INSTALLDATE_CUST is None and RESYRBLT > 1986) or (MATERIAL_CUST is None and INSTALLDATE_CUST >= datetime(year=1987, month=1, day=1)): return 2 #Non-Lead elif MATERIAL_CUST == 'GP': return 3 #Galvanized Requiring Replacement def calculate_utility_source(MATERIAL_UTIL,INSTALLDATE_UTIL,RESYRBLT): if MATERIAL_UTIL is not None: return 1 elif MATERIAL_UTIL is None and (INSTALLDATE_UTIL is not None or RESYRBLT is not None): return 5 def calculate_customer_source(MATERIAL_CUST,INSTALLDATE_CUST,RESYRBLT): if MATERIAL_CUST is not None: return 1 elif MATERIAL_CUST is None and (INSTALLDATE_CUST is not None or RESYRBLT is not None): return 5