Skip to content

Commit

Permalink
Switched to Black linter
Browse files Browse the repository at this point in the history
  • Loading branch information
WaughB committed Jan 7, 2024
1 parent 7a39e05 commit ce3e42f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: Lint Code Base
name: Python Code Formatting with Black

on: [push, pull_request]

jobs:
build:
name: Lint Code Base
black:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Checkout Code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install Black
run: pip install black

- name: Lint Code Base
uses: github/super-linter@v4
env:
VALIDATE_ALL_CODEBASE: true
VALIDATE_PYTHON_BLACK: true
- name: Run Black Check
run: black --check .
61 changes: 32 additions & 29 deletions dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ def generate_sample_data(size, max_list_length):
:return: List of tuples (key, value) where key is a string and value is an integer.
"""
if size <= 0 or max_list_length <= 0:
logger.error(
"Size and max list length must be non-negative, and non-zero.")
raise ValueError(
"Size and max list length must be non-negative, and non-zero.")
logger.error("Size and max list length must be non-negative, and non-zero.")
raise ValueError("Size and max list length must be non-negative, and non-zero.")

try:
logger.info("Generating sample data.")
sample_data = []

for _ in range(size):
key = ''.join(random.choice(string.ascii_lowercase)
for _ in range(3)) # Generate a random 3-letter string
key = "".join(
random.choice(string.ascii_lowercase) for _ in range(3)
) # Generate a random 3-letter string
if max_list_length > 0:
for _ in range(random.randint(1, max_list_length)):
# Generate a random integer between 1 and 100
Expand Down Expand Up @@ -73,9 +72,11 @@ def convert_to_dictionary(sample_data):
raise TypeError("Each item in sample_data must be a tuple.")
if len(item) != 2:
logger.error(
"Each tuple in sample data must contain exactly 2 elements.")
"Each tuple in sample data must contain exactly 2 elements."
)
raise ValueError(
"Each tuple in sample_data must contain exactly 2 elements.")
"Each tuple in sample_data must contain exactly 2 elements."
)

key, value = item
if key in dictionary:
Expand All @@ -84,8 +85,10 @@ def convert_to_dictionary(sample_data):
dictionary[key] = [value]

except Exception as convert_to_dictionary_error:
logger.error("Error converting sample data to a Python dictionary: " +
str(convert_to_dictionary_error))
logger.error(
"Error converting sample data to a Python dictionary: "
+ str(convert_to_dictionary_error)
)
raise convert_to_dictionary_error

logger.info("Successfully converted sample data to a Python dictionary.")
Expand Down Expand Up @@ -113,17 +116,17 @@ def convert_to_numpy_array(sample_data):

# Verify that all tuples have the same length
if len(set(len(item) for item in sample_data if isinstance(item, tuple))) != 1:
logger.error(
"All tuples in sample data must have the same length.")
raise ValueError(
"All tuples in sample_data must have the same length.")
logger.error("All tuples in sample data must have the same length.")
raise ValueError("All tuples in sample_data must have the same length.")

# Convert to NumPy array
np_array = np.array(sample_data)

except Exception as convert_to_numpy_array_error:
logger.error("Error converting sample data to a NumPy array: " +
str(convert_to_numpy_array_error))
logger.error(
"Error converting sample data to a NumPy array: "
+ str(convert_to_numpy_array_error)
)
raise convert_to_numpy_array_error

logger.info("Successfully converted sample data to a NumPy array.")
Expand All @@ -146,17 +149,17 @@ def convert_to_pandas_df(sample_data):
logger.info("Converting sample data to a Pandas DataFrame.")

if not all(isinstance(item, tuple) and len(item) == 2 for item in sample_data):
logger.error(
"Each item in sample data must be a 2-element tuple.")
raise ValueError(
"Each item in sample_data must be a 2-element tuple")
logger.error("Each item in sample data must be a 2-element tuple.")
raise ValueError("Each item in sample_data must be a 2-element tuple")

# Creating DataFrame from sample_data
df = pd.DataFrame(sample_data, columns=['Key', 'Value'])
df = pd.DataFrame(sample_data, columns=["Key", "Value"])

except Exception as convert_to_pandas_df_error:
logger.error("Error converting sample data to a Pandas DataFrame: " +
str(convert_to_pandas_df_error))
logger.error(
"Error converting sample data to a Pandas DataFrame: "
+ str(convert_to_pandas_df_error)
)
raise convert_to_pandas_df_error

logger.info("Successfully converted sample data to a Pandas DataFrame.")
Expand Down Expand Up @@ -184,17 +187,17 @@ def convert_to_polars_df(sample_data):

# Verify that all tuples have the same length
if len(set(len(item) for item in sample_data if isinstance(item, tuple))) != 1:
logger.error(
"All tuples in sample data must have the same length.")
raise ValueError(
"All tuples in sample_data must have the same length.")
logger.error("All tuples in sample data must have the same length.")
raise ValueError("All tuples in sample_data must have the same length.")

# Convert to Polars DataFrame
df = pl.DataFrame(sample_data)

except Exception as convert_to_polars_df_error:
logger.error("Error converting sample data to a Polars DataFrame: " +
str(convert_to_polars_df_error))
logger.error(
"Error converting sample data to a Polars DataFrame: "
+ str(convert_to_polars_df_error)
)
raise convert_to_polars_df_error

logger.info("Successfully converted sample data to a Polars DataFrame.")
Expand Down
3 changes: 2 additions & 1 deletion logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(self, name, log_file=None, level=logging.INFO):
self.logger.setLevel(level)

formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

# File handler to log messages in a file
if log_file is None:
Expand Down
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import string
from logger import log_this
import logging
from dictionaries import generate_sample_data, convert_to_dictionary, convert_to_numpy_array, convert_to_pandas_df, convert_to_polars_df
from dictionaries import (
generate_sample_data,
convert_to_dictionary,
convert_to_numpy_array,
convert_to_pandas_df,
convert_to_polars_df,
)
from speed_tests import measure_execution_time

CONST_SIZE = 10000
Expand All @@ -25,7 +31,6 @@
pandas_df = convert_to_pandas_df(sample_data)
polars_df = convert_to_polars_df(sample_data)


# Speed tests
measure_execution_time(convert_to_dictionary, 10, sample_data)
measure_execution_time(convert_to_numpy_array, 10, sample_data)
Expand Down
6 changes: 4 additions & 2 deletions speed_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def measure_execution_time(func, repetitions=10, *args, **kwargs):

average_elapsed_time = total_elapsed_time / repetitions
average_elapsed_time_ms = int(
average_elapsed_time.microseconds) # Convert to milliseconds
average_elapsed_time.microseconds
) # Convert to milliseconds
logger.info(
f"Function {func} Average execution time (microseconds): {average_elapsed_time_ms}")
f"Function {func} Average execution time (microseconds): {average_elapsed_time_ms}"
)
10 changes: 5 additions & 5 deletions test_convert_to_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


def test_normal_input():
sample_data = [('a', 1), ('b', 2), ('a', 3)]
expected_output = {'a': [1, 3], 'b': [2]}
sample_data = [("a", 1), ("b", 2), ("a", 3)]
expected_output = {"a": [1, 3], "b": [2]}
assert convert_to_dictionary(sample_data) == expected_output


Expand All @@ -15,8 +15,8 @@ def test_empty_input():


def test_multiple_entries_same_key():
sample_data = [('a', 1), ('a', 2), ('a', 3)]
expected_output = {'a': [1, 2, 3]}
sample_data = [("a", 1), ("a", 2), ("a", 3)]
expected_output = {"a": [1, 2, 3]}
assert convert_to_dictionary(sample_data) == expected_output


Expand All @@ -26,6 +26,6 @@ def test_invalid_input_data():


def test_incorrect_tuple_length():
sample_data = [('a', 1, 'extra'), ('b', 2)]
sample_data = [("a", 1, "extra"), ("b", 2)]
with pytest.raises(ValueError):
convert_to_dictionary(sample_data)
15 changes: 6 additions & 9 deletions test_convert_to_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
def test_normal_input():
sample_data = [(1, 2), (3, 4)]
expected_output = np.array([[1, 2], [3, 4]])
np.testing.assert_array_equal(
convert_to_numpy_array(sample_data), expected_output)
np.testing.assert_array_equal(convert_to_numpy_array(sample_data), expected_output)


def test_empty_input():
sample_data = []
expected_output = np.array([])
np.testing.assert_array_equal(
convert_to_numpy_array(sample_data), expected_output)
np.testing.assert_array_equal(convert_to_numpy_array(sample_data), expected_output)


def test_multiple_entries():
sample_data = [('a', 1), ('a', 2), ('a', 3)]
expected_output = np.array([('a', 1), ('a', 2), ('a', 3)])
np.testing.assert_array_equal(
convert_to_numpy_array(sample_data), expected_output)
sample_data = [("a", 1), ("a", 2), ("a", 3)]
expected_output = np.array([("a", 1), ("a", 2), ("a", 3)])
np.testing.assert_array_equal(convert_to_numpy_array(sample_data), expected_output)


def test_invalid_input_data():
Expand All @@ -30,6 +27,6 @@ def test_invalid_input_data():


def test_incorrect_tuple_length():
sample_data = [('a', 1, 'extra'), ('b', 2)]
sample_data = [("a", 1, "extra"), ("b", 2)]
with pytest.raises(ValueError):
convert_to_numpy_array(sample_data)

0 comments on commit ce3e42f

Please sign in to comment.