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97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Code Cleanliness in Python: A Comprehensive Guide

Code cleanliness is an essential aspect of software development that ensures code is easy to read, understand, and maintain. Clean code leads to fewer bugs, easier debugging, and more efficient collaboration among developers. This blog article delves into the principles of writing clean Python code, emphasizing the use of type annotations, docstrings, and the Loguru logging library. We'll explore the importance of each component and provide practical examples to illustrate best practices.

## Table of Contents
1. Introduction to Code Cleanliness
2. Importance of Type Annotations
3. Writing Effective Docstrings
4. Structuring Your Code
5. Error Handling and Logging with Loguru
6. Refactoring for Clean Code
7. Examples of Clean Code
8. Conclusion

## 1. Introduction to Code Cleanliness

Code cleanliness refers to the practice of writing code that is easy to read, understand, and maintain. Clean code follows consistent conventions and is organized logically, making it easier for developers to collaborate and for new team members to get up to speed quickly.

### Why Clean Code Matters

1. **Readability**: Clean code is easy to read and understand, which reduces the time needed to grasp what the code does.
2. **Maintainability**: Clean code is easier to maintain and modify, reducing the risk of introducing bugs when making changes.
3. **Collaboration**: Clean code facilitates collaboration among team members, as everyone can easily understand and follow the codebase.
4. **Debugging**: Clean code makes it easier to identify and fix bugs, leading to more reliable software.

## 2. Importance of Type Annotations

Type annotations in Python provide a way to specify the types of variables, function arguments, and return values. They enhance code readability and help catch type-related errors early in the development process.

### Benefits of Type Annotations

1. **Improved Readability**: Type annotations make it clear what types of values are expected, improving code readability.
2. **Error Detection**: Type annotations help catch type-related errors during development, reducing runtime errors.
3. **Better Tooling**: Many modern IDEs and editors use type annotations to provide better code completion and error checking.

### Example of Type Annotations

```python
from typing import List

def calculate_average(numbers: List[float]) -> float:
    """
    Calculates the average of a list of numbers.

    Args:
        numbers (List[float]): A list of numbers.

    Returns:
        float: The average of the numbers.
    """
    return sum(numbers) / len(numbers)
```

In this example, the `calculate_average` function takes a list of floats as input and returns a float. The type annotations make it clear what types are expected and returned, enhancing readability and maintainability.

## 3. Writing Effective Docstrings

Docstrings are an essential part of writing clean code in Python. They provide inline documentation for modules, classes, methods, and functions. Effective docstrings improve code readability and make it easier for other developers to understand and use your code.

### Benefits of Docstrings

1. **Documentation**: Docstrings serve as inline documentation, making it easier to understand the purpose and usage of code.
2. **Consistency**: Well-written docstrings ensure consistent documentation across the codebase.
3. **Ease of Use**: Docstrings make it easier for developers to use and understand code without having to read through the implementation details.

### Example of Effective Docstrings

```python
def calculate_factorial(n: int) -> int:
    """
    Calculates the factorial of a given non-negative integer.

    Args:
        n (int): The non-negative integer to calculate the factorial of.

    Returns:
        int: The factorial of the given number.

    Raises:
        ValueError: If the input is a negative integer.
    """
    if n < 0:
        raise ValueError("Input must be a non-negative integer.")
    factorial = 1
    for i in range(1, n + 1):
        factorial *= i
    return factorial
```

In this example, the docstring clearly explains the purpose of the `calculate_factorial` function, its arguments, return value, and the exception it may raise.

## 4. Structuring Your Code

Proper code structure is crucial for code cleanliness. A well-structured codebase is easier to navigate, understand, and maintain. Here are some best practices for structuring your Python code:

### Organizing Code into Modules and Packages

Organize your code into modules and packages to group related functionality together. This makes it easier to find and manage code.

```python
# project/
# β”œβ”€β”€ main.py
# β”œβ”€β”€ utils/
# β”‚   β”œβ”€β”€ __init__.py
# β”‚   β”œβ”€β”€ file_utils.py
# β”‚   └── math_utils.py
# └── models/
#     β”œβ”€β”€ __init__.py
#     β”œβ”€β”€ user.py
#     └── product.py
```

### Using Functions and Classes

Break down your code into small, reusable functions and classes. This makes your code more modular and easier to test.

```python
class User:
    def __init__(self, name: str, age: int):
        """
        Initializes a new user.

        Args:
            name (str): The name of the user.
            age (int): The age of the user.
        """
        self.name = name
        self.age = age

    def greet(self) -> str:
        """
        Greets the user.

        Returns:
            str: A greeting message.
        """
        return f"Hello, {self.name}!"
```

### Keeping Functions Small

Functions should do one thing and do it well. Keep functions small and focused on a single task.

```python
def save_user(user: User, filename: str) -> None:
    """
    Saves user data to a file.

    Args:
        user (User): The user object to save.
        filename (str): The name of the file to save the user data to.
    """
    with open(filename, 'w') as file:
        file.write(f"{user.name},{user.age}")
```

## 5. Error Handling and Logging with Loguru

Effective error handling and logging are critical components of clean code. They help you manage and diagnose issues that arise during the execution of your code.

### Error Handling Best Practices

1. **Use Specific Exceptions**: Catch specific exceptions rather than using a generic `except` clause.
2. **Provide Meaningful Messages**: When raising exceptions, provide meaningful error messages to help diagnose the issue.
3. **Clean Up Resources**: Use `finally` blocks or context managers to ensure that resources are properly cleaned up.

### Example of Error Handling

```python
def divide_numbers(numerator: float, denominator: float) -> float:
    """
    Divides the numerator by the denominator.

    Args:
        numerator (float): The number to be divided.
        denominator (float): The number to divide by.

    Returns:
        float: The result of the division.

    Raises:
        ValueError: If the denominator is zero.
    """
    if denominator == 0:
        raise ValueError("The denominator cannot be zero.")
    return numerator / denominator
```

### Logging with Loguru

Loguru is a powerful logging library for Python that makes logging simple and enjoyable. It provides a clean and easy-to-use API for logging messages with different severity levels.

#### Installing Loguru

```bash
pip install loguru
```

#### Basic Usage of Loguru

```python
from loguru import logger

logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
```

### Example of Logging in a Function

```python
from loguru import logger

def fetch_data(url: str) -> str:
    """
    Fetches data from a given URL and returns it as a string.

    Args:
        url (str): The URL to fetch data from.

    Returns:
        str: The data fetched from the URL.

    Raises:
        requests.exceptions.RequestException: If there is an error with the request.
    """
    try:
        logger.info(f"Fetching data from {url}")
        response = requests.get(url)
        response.raise_for_status()
        logger.info("Data fetched successfully")
        return response.text
    except requests.exceptions.RequestException as e:
        logger.error(f"Error fetching data: {e}")
        raise
```

In this example, Loguru is used to log messages at different severity levels. The `fetch_data` function logs informational messages when fetching data and logs an error message if an exception is raised.

## 6. Refactoring for Clean Code

Refactoring is the process of restructuring existing code without changing its external behavior. It is an essential practice for maintaining clean code. Refactoring helps improve code readability, reduce complexity, and eliminate redundancy.

### Identifying Code Smells

Code smells are indicators of potential issues in the code that may require refactoring. Common code smells include:
1. **Long Methods**: Methods that are too long and do too many things.
2. **Duplicated Code**: Code that is duplicated in multiple places.
3. **Large Classes**: Classes that have too many responsibilities.
4. **Poor Naming**: Variables, functions, or classes with unclear or misleading names.

### Refactoring Techniques

1. **Extract Method**: Break down long methods into smaller, more focused methods.
2. **Rename Variables**: Use meaningful names for variables, functions, and classes.
3. **Remove Duplicated Code**: Consolidate duplicated code into a single location.
4. **Simplify Conditional Expressions**: Simplify complex conditional expressions for

 better readability.

### Example of Refactoring

Before refactoring:
```python
def process_data(data: List[int]) -> int:
    total = 0
    for value in data:
        if value > 0:
            total += value
    return total
```

After refactoring:
```python
def filter_positive_values(data: List[int]) -> List[int]:
    """
    Filters the positive values from the input data.

    Args:
        data (List[int]): The input data.

    Returns:
        List[int]: A list of positive values.
    """
    return [value for value in data if value > 0]

def sum_values(values: List[int]) -> int:
    """
    Sums the values in the input list.

    Args:
        values (List[int]): A list of values to sum.

    Returns:
        int: The sum of the values.
    """
    return sum(values)

def process_data(data: List[int]) -> int:
    """
    Processes the data by filtering positive values and summing them.

    Args:
        data (List[int]): The input data.

    Returns:
        int: The sum of the positive values.
    """
    positive_values = filter_positive_values(data)
    return sum_values(positive_values)
```

In this example, the `process_data` function is refactored into smaller, more focused functions. This improves readability and maintainability.

## 7. Examples of Clean Code

### Example 1: Reading a File

```python
def read_file(file_path: str) -> str:
    """
    Reads the content of a file and returns it as a string.

    Args:
        file_path (str): The path to the file to read.

    Returns:
        str: The content of the file.

    Raises:
        FileNotFoundError: If the file does not exist.
        IOError: If there is an error reading the file.
    """
    try:
        with open(file_path, 'r') as file:
            return file.read()
    except FileNotFoundError as e:
        logger.error(f"File not found: {file_path}")
        raise
    except IOError as e:
        logger.error(f"Error reading file: {file_path}")
        raise
```

### Example 2: Fetching Data from a URL

```python
import requests
from loguru import logger

def fetch_data(url: str) -> str:
    """
    Fetches data from a given URL and returns it as a string.

    Args:
        url (str): The URL to fetch data from.

    Returns:
        str: The data fetched from the URL.

    Raises:
        requests.exceptions.RequestException: If there is an error with the request.
    """
    try:
        logger.info(f"Fetching data from {url}")
        response = requests.get(url)
        response.raise_for_status()
        logger.info("Data fetched successfully")
        return response.text
    except requests.exceptions.RequestException as e:
        logger.error(f"Error fetching data: {e}")
        raise
```

### Example 3: Calculating Factorial

```python
def calculate_factorial(n: int) -> int:
    """
    Calculates the factorial of a given non-negative integer.

    Args:
        n (int): The non-negative integer to calculate the factorial of.

    Returns:
        int: The factorial of the given number.

    Raises:
        ValueError: If the input is a negative integer.
    """
    if n < 0:
        raise ValueError("Input must be a non-negative integer.")
    factorial = 1
    for i in range(1, n + 1):
        factorial *= i
    return factorial
```

## 8. Conclusion

Writing clean code in Python is crucial for developing maintainable, readable, and error-free software. By using type annotations, writing effective docstrings, structuring your code properly, and leveraging logging with Loguru, you can significantly improve the quality of your codebase.

Remember to refactor your code regularly to eliminate code smells and improve readability. Clean code not only makes your life as a developer easier but also enhances collaboration and reduces the likelihood of bugs.

By following the principles and best practices outlined in this article, you'll be well on your way to writing clean, maintainable Python code.