Convert string dates into a yyyy-mm-dd format. The function uses pl.coalesce to try to process different formats. For example, it will first try to convert m/d/y, and then if that doesn’t work it will try d/m/y. It’s not perfect, but if someone messes up the date it’s their fault.
Note: it won’t attempt to convert excel dates. If someone sends us excel dates we will file a lawsuit.
Usage
To be applied to a string date column.
Parameters
df:pl.DataFrame | pl.LazyFrame
a polars dataframe (needed to check if col is pl.Date type or not)
col:str
a string column that has a date
Returns
:pl.Expr:
a date column
Examples
import polars as plfrom wadoh_raccoon.utils import helpersdf = pl.DataFrame({"dates": ["2024-10-30", # ISO format"30/10/2024", # European format"10/20/2024", # US format"10-30-2024", # US format"October 30, 2024", # Full month name format,"45496", # an excel date LOL"2022-12-27 08:26:49" ]})output = ( df .with_columns( new_date=helpers.date_format(df=df,col='dates') ))helpers.gt_style(df_inp=output)
Retrieve secrets from Azure KeyVault. This function will utilize the keys that are passed to retrieve the corresponding secrets.
**Note: Authenication takes place via DefaultAzureCredential which attempts multiple authentication methods. One method is checking against Azure CLI if logged in.
Usage
Use this function to securely retrieve secret values from Azure KeyVault using the specified key(s). The function accepts either a single key or multiple keys as a list.
Parameters
vault:
Key vault url.
keys:
A single secret key or list of secret keys.
Returns
:str or tuple of str
If a single key is provided, returns the secret value as a string. If a list of keys is provided, returns a tuple of secret values in the same order.
Examples
from wadoh_raccoon.utils import helpers# Get a single secretdb_password = helpers.get_secrets("keyvault_url", "db-password")# Get multiple secrets at onceusername, password, api_key = helpers.get_secrets("keyvault_url", ["db-username", "db-password", "api-key"])
Upload Polars DataFrames to the Washington State Managed File Transfer (MFT) server via SFTP. This function converts DataFrames to various file formats and securely transfers them to specified directories on the MFT server.
**Note: Authentication requires explicit credentials to be provided. The function automatically adds the server’s host key for simplified connection handling.
Usage
Use this function to upload processed surveillance data, reports, or other DataFrames to the MFT server for sharing with partners. The function handles file format conversion.
Parameters
upload:polars.DataFrame
The Polars DataFrame to upload.
dir:str
Target directory path on the MFT server (e.g., ‘/outbound/partner’).
upload_file_name:str
Name of the file without extension (e.g., ‘surveillance_report’).
upload_file_extension:str
File extension including the dot. Supported formats: ‘.csv’, ‘.xlsx’, ‘.json’, ‘.parquet’.
username:str
MFT server username.
password:str
MFT server password.
host:str='mft.wa.gov'
MFT server hostname. Default is ‘mft.wa.gov’.
Returns
:None
Files are uploaded directly to the MFT server. Success message printed.
Raises
:TypeError
If upload is not a Polars DataFrame.
:ValueError
If upload is empty, required parameters are missing, or upload_file_extension is not supported.
:OSError
If the target directory does not exist or cannot be accessed on the MFT server.
:ConnectionError
If SFTP connection fails.
Examples
import polars as plfrom wadoh_raccoon.utils.helpers import mft_upload, get_secrets# Create sample DataFramedf = pl.DataFrame({'case_id': [1, 2, 3],'pathogen': ['Salmonella', 'E. coli', 'Campylobacter']})# Get credentials from Key Vaultmft_user, mft_pass = get_secrets('vault_url', ['mft-username', 'mft-password'])# Upload as CSVmft_upload( upload=df,dir='DEV_TESTING', upload_file_name='weekly_report_2024_01', upload_file_extension='.csv', username=mft_user, password=mft_pass)# Upload as Excelmft_upload( upload=df,dir='DEV_TESTING', upload_file_name='weekly_report_2024_01', upload_file_extension='.xlsx', username=mft_user, password=mft_pass)