From 29d52b16133cee0d31027c01c47abee37b4c8a9b Mon Sep 17 00:00:00 2001 From: toravest Date: Fri, 23 May 2025 08:47:33 +0200 Subject: [PATCH] add function documentation to util.py --- src/my_package/util.py | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/my_package/util.py b/src/my_package/util.py index 88682d7..3211f5d 100644 --- a/src/my_package/util.py +++ b/src/my_package/util.py @@ -3,6 +3,12 @@ # Function to replace the norcid 'æøå' def replace_nordic(city_name): + ''' + This function is taking in the parameter city_name. Thwn it goes to each letter to check for any + of the nordic letters 'æøå'. If it is one of these letters, it will be replaced like this: + 'æ' = 'ae', 'ø' = 'o', 'å' = 'aa'. And then the city_name will be returned. + ''' + for letter in city_name: if letter in 'æøå': city_name = city_name.replace('æ', 'ae') @@ -12,17 +18,33 @@ def replace_nordic(city_name): # Get the city_name input, convert nordic 'æøå' def input_place(): + ''' + This function is used to get user-input for a city_name. It will first ask the user for a city_name, + then runs the function to check for any nordic letters, before it returns the city_name. + ''' + city_name = input("Enter city name: ") city_name = replace_nordic(city_name) return city_name # Function to convert from kelvin to celsius temp def kelvin_to_celsius(temp_in_kelvin): + ''' + This function is taking in the parameter temp_in_kelvin, and make a new variable temp_in_celsius. + The function transform from kelvin to celsius by subtracting 273.15 from the kelvin_temps. + ''' + temp_in_celsius = temp_in_kelvin - 273.15 return temp_in_celsius # Ensure wqnted columns, fill with "NaN" if they dont exsist def ensure_column(df, columns): + ''' + This function is taking in the parametre df and columns. It will then check if the columns are in the df. + If the columns are not present, it will print a statement saying it is not present, before adding the columns + and fill in the value NaN. + ''' + for col in columns: if col not in df.columns: print(f"'{col}' is not present in the DataFrame. Filling with NaN.") @@ -31,6 +53,11 @@ def ensure_column(df, columns): # Fill "NaN" in wanted columns with 0 def fill_column_0(df, columns): + ''' + This function is taking in the parameters df and columns. It will then check if the columns include any NaN + values and fill these with 0. + ''' + for col in columns: try: df[col] = df[col].fillna(0) @@ -40,6 +67,11 @@ def fill_column_0(df, columns): # Cleans statistic-data, drop all columns that end with '...' using the filter function def clean_df(df): + ''' + This function is taking in the parameter df, and dataframe for statistical data. + It will then drop all the columns that end with '.p25', '.p75', '.st_dev' and '.num'. + ''' + df = df.drop(columns=df.filter(like='.p25').columns) df = df.drop(columns=df.filter(like='.p75').columns) df = df.drop(columns=df.filter(like='.st_dev').columns) @@ -49,6 +81,12 @@ def clean_df(df): # Find highest and lowest temp from statistical data def get_records(df, city_name): + ''' + This function is taking in the parameters, df and city_name from statistical data. + It will then make a new dataframe, with the maximum and minimum temperatur based on both + mean_temp and record_temp. The dataframe will be written to a new json-file with the city_name as filename. + ''' + if df.empty: print("df is empty") @@ -68,4 +106,12 @@ def get_records(df, city_name): folder = "../data/json/output_records" filename = f"records_{city_name}" - return summary_df, filename, folder \ No newline at end of file + return summary_df, filename, folder + +print(replace_nordic.__doc__) +print(input_place.__doc__) +print(kelvin_to_celsius.__doc__) +print(ensure_column.__doc__) +print(fill_column_0.__doc__) +print(clean_df.__doc__) +print(get_records.__doc__) \ No newline at end of file