-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2614a18
commit 70c6fd0
Showing
17 changed files
with
530 additions
and
25 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
2023_PyData_Berlin/tests/test_2_2_do_one_thing_and_do_it_well.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def test_read_csv_file(): | ||
assert False |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pathlib import Path | ||
import polars as pl | ||
|
||
if __name__ == "__main__": | ||
current_dir_path = Path(__file__).parent | ||
csv_file_path = current_dir_path.joinpath("example.csv") | ||
|
||
print( | ||
pl.read_csv(csv_file_path) | ||
.filter(pl.col("Score") != "error") | ||
.select("Score") | ||
.cast(pl.Float32, strict=False) | ||
.mean() | ||
) |
52 changes: 52 additions & 0 deletions
52
2024_PyConIT/src/pipeline/2_0_imperative_refactor_pipeline.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
|
||
def read_csv_file(file_path): | ||
try: | ||
with open(file_path, "r") as csv_file: | ||
reader = csv.reader(csv_file) | ||
return [row for row in reader] | ||
except FileNotFoundError: | ||
return None | ||
|
||
|
||
def extract_column(index, rows): | ||
try: | ||
column_values = [float(row[index]) for row in rows[1:]] | ||
return column_values | ||
except (ValueError, IndexError): | ||
return None | ||
|
||
|
||
def calculate_average(column_values): | ||
if column_values is None or not column_values: | ||
return None, "Cannot calculate average due to empty or missing column" | ||
try: | ||
average = sum(column_values) / len(column_values) | ||
return average | ||
except ZeroDivisionError: | ||
return None | ||
|
||
|
||
if __name__ == "__main__": | ||
current_dir_path = Path(__file__).parent | ||
csv_file_path = current_dir_path.joinpath("data", "example.csv") | ||
column_index = 1 # Assuming the second column (index 1) needs to be processed | ||
data = read_csv_file(csv_file_path) | ||
|
||
if data is None: | ||
print("Error reading CSV file") | ||
else: | ||
# Step 2: Extract column | ||
score_column_values = extract_column(column_index, data) | ||
if score_column_values is None: | ||
print("Error extracting column") | ||
else: | ||
# Step 3: Calculate average | ||
result = calculate_average(score_column_values) | ||
|
||
if result is None: | ||
print("Error calculating average") | ||
else: | ||
print(f"The average is: {result}") |
64 changes: 64 additions & 0 deletions
64
2024_PyConIT/src/pipeline/2_2_do_one_thing_and_do_it_well_pipeline.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
|
||
def read_csv_file(file_path): | ||
try: | ||
with open(file_path, "r") as csv_file: | ||
return [row for row in csv.reader(csv_file)] | ||
except FileNotFoundError: | ||
return None | ||
|
||
|
||
def extract_column(column_index, rows): | ||
try: | ||
return [row[column_index] for row in rows] | ||
except (ValueError, IndexError): | ||
return None | ||
|
||
|
||
def remove_row(row_index, rows): | ||
try: | ||
return rows[row_index:] | ||
except IndexError: | ||
return None | ||
|
||
|
||
def convert_to(converter, rows): | ||
try: | ||
return [converter(item) for item in rows] | ||
except ValueError: | ||
return None | ||
|
||
|
||
def calculate_average(column_values): | ||
try: | ||
return sum(column_values) / len(column_values) | ||
except ZeroDivisionError: | ||
return None | ||
|
||
|
||
if __name__ == "__main__": | ||
current_dir_path = Path(__file__).parent | ||
csv_file_path = current_dir_path.joinpath("data", "example.csv") | ||
|
||
score_column_index = 1 | ||
header_row_index = 1 | ||
|
||
data = read_csv_file(csv_file_path) | ||
|
||
if data is None: | ||
print("Error reading CSV file") | ||
else: | ||
score_column_values = extract_column(score_column_index, data) | ||
removed_header_data = remove_row(header_row_index, score_column_values) | ||
score_column_as_float = convert_to(float, removed_header_data) | ||
|
||
if score_column_as_float is None: | ||
print("Error extracting column") | ||
else: | ||
result = calculate_average(score_column_as_float) | ||
if result is None: | ||
print("Error calculating average") | ||
else: | ||
print(f"The average is: {result}") |
17 changes: 17 additions & 0 deletions
17
2024_PyConIT/src/pipeline/3_0_higher_order_function_example.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def apply_operation(operation, x, y): | ||
return operation(x, y) | ||
|
||
|
||
# Binary operation functions | ||
def add(x, y): | ||
return x + y | ||
|
||
|
||
def multiply(x, y): | ||
return x * y | ||
|
||
|
||
if __name__ == "__main__": | ||
# Usage of the higher-order function | ||
result_addition = apply_operation(add, 3, 4) | ||
result_multiplication = apply_operation(multiply, 3, 4) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def plus(a): | ||
def add(b): | ||
return a + b | ||
|
||
return add | ||
|
||
|
||
# Usage | ||
result = plus(2)(3) | ||
# Partial apply, transform binary function to unary function | ||
plusTwo = plus(2) | ||
print(plusTwo(3)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import csv | ||
|
||
|
||
def read_csv_file(file_path): | ||
try: | ||
with open(file_path, "r") as csv_file: | ||
return [row for row in csv.reader(csv_file)] | ||
except FileNotFoundError: | ||
return None | ||
|
||
|
||
def extract_column_currying(index): | ||
""" | ||
Curried function to extract column from a list of rows | ||
f(x, y) = f(x)(y) | ||
``` | ||
def extract_column(column_index, rows): | ||
try: | ||
return [row[column_index] for row in rows] | ||
except (ValueError, IndexError): | ||
return None | ||
``` | ||
:param index: index of the column to extract | ||
:return: curried function to extract column | ||
""" | ||
|
||
def curried(rows): | ||
try: | ||
score_column_values = [row[index] for row in rows] | ||
return score_column_values | ||
except (ValueError, IndexError): | ||
return None | ||
|
||
return curried | ||
|
||
|
||
if __name__ == "__main__": | ||
data = read_csv_file("data/example.csv") | ||
|
||
name_column_index = 0 | ||
score_column_index = 1 | ||
|
||
name_list = extract_column_currying(name_column_index) | ||
score_list = extract_column_currying(score_column_index) | ||
|
||
print(f"Extracted Column: {score_list(data)}") | ||
print(f"Extracted Column: {name_list(data)}") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
from toolz import curry | ||
|
||
|
||
def read_csv_file(file_path): | ||
try: | ||
with open(file_path, "r") as csv_file: | ||
return [row for row in csv.reader(csv_file)] | ||
except FileNotFoundError: | ||
return None | ||
|
||
|
||
@curry | ||
def extract_column(column_index, rows): | ||
try: | ||
return [row[column_index] for row in rows] | ||
except (ValueError, IndexError): | ||
return None | ||
|
||
|
||
@curry | ||
def remove_row(row_index, rows): | ||
try: | ||
return rows[row_index:] | ||
except IndexError: | ||
return None | ||
|
||
|
||
@curry | ||
def convert_to(converter, rows): | ||
try: | ||
return [converter(item) for item in rows] | ||
except ValueError: | ||
return None | ||
|
||
|
||
def calculate_average(column_values): | ||
try: | ||
return sum(column_values) / len(column_values) | ||
except ZeroDivisionError: | ||
return None | ||
|
||
|
||
if __name__ == "__main__": | ||
current_dir_path = Path(__file__).parent | ||
csv_file_path = current_dir_path.joinpath("data", "example.csv") | ||
|
||
score_column_index = 1 | ||
header_row_index = 1 | ||
|
||
score_column = extract_column(score_column_index) | ||
removed_header = remove_row(header_row_index) | ||
score_as_float = convert_to(float) | ||
data = read_csv_file(csv_file_path) | ||
|
||
if data is None: | ||
print("Error reading CSV file") | ||
else: | ||
score_column_values = score_column(data) | ||
removed_header_data = removed_header(score_column_values) | ||
score_column_as_float = score_as_float(removed_header_data) | ||
|
||
if score_column_as_float is None: | ||
print("Error extracting column") | ||
else: | ||
result = calculate_average(score_column_as_float) | ||
if result is None: | ||
print("Error calculating average") | ||
else: | ||
print(f"The average is: {result}") |
34 changes: 34 additions & 0 deletions
34
2024_PyConIT/src/pipeline/4_0_function_composition_example.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
def compose(*functions): | ||
""" | ||
Compose multiple functions: f(g(...(h(x)))) | ||
""" | ||
|
||
def composed_function(x): | ||
result = x | ||
for func in reversed(functions): | ||
result = func(result) | ||
return result | ||
|
||
return composed_function | ||
|
||
|
||
# Example functions | ||
def square(x): | ||
return x**2 | ||
|
||
|
||
def add_one(x): | ||
return x + 1 | ||
|
||
|
||
def double(x): | ||
return x * 2 | ||
|
||
|
||
def to_s(s): | ||
return f"final result {str(s)}" | ||
|
||
|
||
if __name__ == "__main__": | ||
pipeline = compose(to_s, square, add_one, double) | ||
print(f"Result: {pipeline(3)}") |
21 changes: 21 additions & 0 deletions
21
2024_PyConIT/src/pipeline/4_1_function_composition_toolz.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from toolz import pipe | ||
|
||
|
||
# Define functions | ||
def add_two(x): | ||
return x + 2 | ||
|
||
|
||
def multiply_by_three(x): | ||
return x * 3 | ||
|
||
|
||
def subtract_five(x): | ||
return x - 5 | ||
|
||
|
||
if __name__ == "__main__": | ||
basic = subtract_five(multiply_by_three(add_two(2))) | ||
result = pipe(2, add_two, multiply_by_three, subtract_five) | ||
print(basic) | ||
print(result) |
Oops, something went wrong.