Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Commit

Permalink
use returns
Browse files Browse the repository at this point in the history
  • Loading branch information
eccentricOrange authored Jul 13, 2022
1 parent d443e11 commit d311dea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
18 changes: 18 additions & 0 deletions npbc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def status_print(success: bool, message: str) -> None:
colour = Fore.GREEN if success else Fore.RED
print(f"{colour}{Style.BRIGHT}{message}{Style.RESET_ALL}\n")

return


def calculate(parsed_arguments: ArgNamespace, connection: Connection) -> None:
"""calculate the cost for a given month and year
Expand Down Expand Up @@ -267,6 +269,7 @@ def calculate(parsed_arguments: ArgNamespace, connection: Connection) -> None:
# print the results
status_print(True, "Success!")
print(f"SUMMARY:\n\n{formatted}")
return


def addudl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand Down Expand Up @@ -315,6 +318,7 @@ def addudl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
return

status_print(True, "Success!")
return


def deludl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand Down Expand Up @@ -356,6 +360,7 @@ def deludl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
return

status_print(True, "Success!")
return


def getudl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand Down Expand Up @@ -399,6 +404,8 @@ def getudl(parsed_arguments: ArgNamespace, connection: Connection) -> None:
for items in undelivered_strings:
print(', '.join([str(item) for item in items]))

return


def extract_delivery_from_user_input(input_delivery: str) -> list[bool]:
"""convert the /[YN]{7}/ user input to a Boolean list"""
Expand Down Expand Up @@ -499,6 +506,7 @@ def editpaper(parsed_arguments: ArgNamespace, connection: Connection) -> None:
return

status_print(True, "Success!")
return


def addpaper(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand Down Expand Up @@ -532,6 +540,7 @@ def addpaper(parsed_arguments: ArgNamespace, connection: Connection) -> None:
return

status_print(True, "Success!")
return


def delpaper(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand All @@ -552,6 +561,7 @@ def delpaper(parsed_arguments: ArgNamespace, connection: Connection) -> None:
return

status_print(True, "Success!")
return


def getpapers(parsed_arguments: ArgNamespace, connection: Connection) -> None:
Expand Down Expand Up @@ -661,6 +671,8 @@ def getpapers(parsed_arguments: ArgNamespace, connection: Connection) -> None:

print()

return


def getlogs(parsed_arguments: ArgNamespace, connection: Connection) -> None:
"""get a list of all logs in the database
Expand Down Expand Up @@ -699,20 +711,24 @@ def getlogs(parsed_arguments: ArgNamespace, connection: Connection) -> None:
for row in data:
print(', '.join(str(item) for item in row))

return


def update(parsed_arguments: ArgNamespace, _: Connection) -> None:
"""update the application
- under normal operation, this function should never run
- if the update CLI argument is provided, this script will never run and the updater will be run instead"""

status_print(False, "Update failed.")
return


def init(parsed_arguments: ArgNamespace, _: Connection) -> None:
"""initialize the application
- this function should run only once, when the application is first installed"""

status_print(True, "Initialized successfully.")
return


def main(arguments: list[str]) -> None:
Expand Down Expand Up @@ -743,6 +759,8 @@ def main(arguments: list[str]) -> None:
finally:
connection.close() # type: ignore

return


if __name__ == "__main__":
main(argv[1:])
40 changes: 28 additions & 12 deletions npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ def get_number_of_each_weekday(month: int, year: int) -> Generator[int, None, No
number_of_weeks = len(main_calendar)

# iterate over each possible weekday
for i in range(len(WEEKDAY_NAMES)):
for weekday_index in range(len(WEEKDAY_NAMES)):

# assume that the weekday occurs once per week in the month
number_of_weekday: int = number_of_weeks

# if the first week doesn't have the weekday, decrement its count
if main_calendar[0][i] == 0:
if main_calendar[0][weekday_index] == 0:
number_of_weekday -= 1

# if the last week doesn't have the weekday, decrement its count
if main_calendar[-1][i] == 0:
if main_calendar[-1][weekday_index] == 0:
number_of_weekday -= 1

yield number_of_weekday
Expand All @@ -101,6 +101,7 @@ def validate_undelivered_string(*strings: str) -> None:
raise npbc_exceptions.InvalidUndeliveredString(f'{string} is not a valid undelivered string.')

# if we get here, all strings passed the regex check
return

def extract_number(string: str, month: int, year: int) -> date:
"""if the date is simply a number, it's a single day. so we just identify that date"""
Expand Down Expand Up @@ -220,18 +221,18 @@ def parse_undelivered_strings(month: int, year: int, *strings: str) -> set[date]
# check for each of the patterns
for string in strings:
if string:
try:
dates.update(parse_undelivered_string(month, year, string))
try:
dates.update(parse_undelivered_string(month, year, string))

except npbc_exceptions.InvalidUndeliveredString as e:
print(
f"""Congratulations! You broke the program!
You managed to write a string that the program considers valid, but isn't actually.
Please report it to the developer.
\nThe string you wrote was: {string}
print(
f"""Congratulations! You broke the program!
You managed to write a string that the program considers valid, but isn't actually.
Please report it to the developer.
\nThe string you wrote was: {string}
This data has not been counted.\n
Exact error message: {e}"""
)
)

return dates

Expand Down Expand Up @@ -375,6 +376,8 @@ def save_results(
(log_ids[paper_id], day.strftime("%Y-%m-%d"))
)

return


def format_output(connection: Connection, costs: dict[int, float], total: float, month: int, year: int) -> Generator[str, None, None]:
"""format the output of calculating the cost of all papers"""
Expand Down Expand Up @@ -416,6 +419,8 @@ def add_new_paper(connection: Connection, name: str, days_delivered: list[bool],
(paper_id, day_id, delivered, cost)
)

return


def edit_existing_paper(
connection: Connection,
Expand Down Expand Up @@ -457,6 +462,8 @@ def edit_existing_paper(
(delivered, paper_id, day_id)
)

return


def delete_existing_paper(connection: Connection, paper_id: int) -> None:
"""delete an existing paper
Expand All @@ -481,6 +488,8 @@ def delete_existing_paper(connection: Connection, paper_id: int) -> None:
(paper_id,)
)

return


def add_undelivered_string(connection: Connection, month: int, year: int, paper_id: int | None = None, *undelivered_strings: str) -> None:
"""record strings for date(s) paper(s) were not delivered
Expand Down Expand Up @@ -526,6 +535,8 @@ def add_undelivered_string(connection: Connection, month: int, year: int, paper_

connection.executemany("INSERT INTO undelivered_strings (month, year, paper_id, string) VALUES (?, ?, ?, ?);", params)

return


def delete_undelivered_string(
connection: Connection,
Expand Down Expand Up @@ -583,6 +594,8 @@ def delete_undelivered_string(

connection.execute(f"{delete_query} WHERE {conditions};", values)

return


def get_papers(connection: Connection) -> tuple[Papers]:
"""get all papers
Expand Down Expand Up @@ -617,7 +630,7 @@ def get_undelivered_strings(
"""get undelivered strings
- the user may specify as many as they want parameters
- available parameters: string_id, month, year, paper_id, string
- returns a list of tuples containing the following fields:
- returns a tuple of tuples containing the following fields:
string_id, paper_id, year, month, string"""

# initialize parameters for the WHERE clause of the SQL query
Expand Down Expand Up @@ -764,3 +777,6 @@ def validate_month_and_year(month: int | None = None, year: int | None = None) -

if isinstance(year, int) and (year <= 0):
raise npbc_exceptions.InvalidMonthYear("Year must be greater than 0.")

# if we get here, the month and year are valid
return

0 comments on commit d311dea

Please sign in to comment.