Skip to content

Commit

Permalink
add function documentation to util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
toravest committed May 23, 2025
1 parent 9bc0dc1 commit 29d52b1
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/my_package/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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.")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")

Expand All @@ -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
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__)

0 comments on commit 29d52b1

Please sign in to comment.