Skip to content

Commit

Permalink
Fix: edits from @willingc
Browse files Browse the repository at this point in the history
Co-authored-by: Carol Willing <[email protected]>
  • Loading branch information
lwasser and willingc committed Oct 17, 2024
1 parent 8091e5e commit db4b2c0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions clean-modular-code/activity-3/clean-code-activity-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ Once you have these functions and methods, you can add checks using conditional
In this activity, you will apply the following strategies to make your code more robust, maintainable & usable:

* **[Fail fast with useful error messages](fail-fast)**: Failing fast is a software engineering term that means allowing your
code to stop early when something goes wrong, ensuring that errors are caught
and communicated clearly. This helps the user quickly understand what went
wrong and where.
code to stop when something goes wrong, ensuring that errors are caught
and communicated promptly. This helps the user quickly understand the error, what went
wrong, and where.
* Use **[conditional statements](../checks-conditionals/python-conditionals)**
to check for specific conditions before executing certain parts. This allows you to create specific workflows that your code followed based on specific conditions.
to logically check for specific conditions before executing code. This allows you to create different pathways for code to execute based on specific conditions.
* **[Try/except blocks](../checks-conditionals/python-function-checks)** allow
you to handle potential errors by attempting an operation and catching any
exceptions if they occur, providing useful feedback. In some cases you may want the program to end on an error. In other cases, you may want to handle it in a specific way.
Expand All @@ -59,7 +59,7 @@ editable: true
slideshow:
slide_type: ''
---
# This works but is less Pythonic as it's a look before you leap approach
# This works but is less Pythonic as it's a "look before you leap" approach since it does an extra check before returning the title
def clean_title(title):
"""Notice that this function checks explicitly to see if it's a list and then processes the data.
"""
Expand All @@ -81,7 +81,7 @@ tags: [raises-exception]
def clean_title(title):
"""
Attempts to return the first character of the title.
Raises the same error with a friendly message if the input is invalid.
Raises the same error with a friendly, custom error message if the input is invalid.
"""
try:
return title[0]
Expand All @@ -100,6 +100,7 @@ print(clean_title(title)) # This will raise an IndexError with the friendly mes
def clean_title(title):
"""
Attempts to return the first character of the title.
Raises the same error with a friendly message if the input is invalid.
"""
try:
Expand All @@ -114,7 +115,7 @@ print(clean_title(title)) # This will raise an IndexError with the friendly mes

+++ {"editable": true, "slideshow": {"slide_type": ""}}

If you wish, you can shorten the amount of information returned in the exception by adding `from None` to your exception. This will look nicer to a user but you lose some information in the exception feedback.
If you wish, you can shorten the amount of information returned in the error by adding `from None` when you raise the error. This will look nicer to a user, but you lose some detail in the error traceback.

```{code-cell} ipython3
# This is the preferred way to catch an error
Expand All @@ -137,7 +138,7 @@ print(clean_title(title)) # This will raise an IndexError with the friendly mes

+++ {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["hide-output", "hide-cell"]}

In this activity, you will be working with Pandas dataframes. You may find the .apply function to be particularly useful.
In this activity, you will be working with Pandas dataframes. You may find the `.apply` function to be particularly useful.

:::{tip}
### Applying functions to DataFrame values: `.apply`
Expand Down

0 comments on commit db4b2c0

Please sign in to comment.