[TIPS] Refactoring - Clean Code - Tip 4 - Utilize Python's Built-in Functions and Libraries

By JoeVu, at: 13:59 Ngày 18 tháng 7 năm 2024

Thời gian đọc ước tính: __READING_TIME__ minutes

[TIPS] Refactoring - Clean Code - Tip 4 - Utilize Python's Built-in Functions and Libraries
[TIPS] Refactoring - Clean Code - Tip 4 - Utilize Python's Built-in Functions and Libraries

Refactoring Tip 4: Utilize Python's Built-in Functions and Libraries

  • Junior: May not leverage Python's rich standard library.

  • Senior: Knows and uses built-in functions and libraries to simplify code (e.g., using collections.Counter instead of writing a custom frequency counter).

Utilizing Python's built-in functions and libraries is crucial for writing efficient and clean code. Here's an example to illustrate the difference between how a junior and a senior developer might approach this principle.

 

Example 1: Reading and Summarizing Data from a CSV File


Junior Developer's Approach

A junior developer might write custom code to read data from a CSV file and calculate the total and average of a column:

import csv

def read_csv(file_path):
    data = []
    with open(file_path, 'r') as file:
        reader = csv.reader(file)
        next(reader)  # Skip header
        for row in reader:
            data.append(float(row[1]))  # Assuming the second column has the numerical data
    return data

def calculate_total_and_average(data):
    total = sum(data)
    average = total / len(data)
    return total, average

# Usage
data = read_csv('data.csv')
total, average = calculate_total_and_average(data)
print(f"Total: {total}, Average: {average}")

 

Senior Developer's Approach

A senior developer would use the pandas library to simplify reading the CSV file and performing calculations:

import pandas as pd


def read_and_summarize_csv(file_path):
    df = pd.read_csv(file_path)
    total = df['column_name'].sum()  # Assuming 'column_name' is the name of the column
    average = df['column_name'].mean()
    return total, average


# Usage
total, average = read_and_summarize_csv('data.csv')
print(f"Total: {total}, Average: {average}")

 

Example 2: Extracting and Counting Words from Text


Junior Developer's Approach

A junior developer might write a function to find the most common elements in a list manually:

def find_most_common_elements(items):
    frequency_dict = {}
    for item in items:
        if item in frequency_dict:
            frequency_dict[item] += 1
        else:
            frequency_dict[item] = 1
    most_common = max(frequency_dict.values())
    return [item for item, count in frequency_dict.items() if count == most_common]

 

# Usage
items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
most_common = find_most_common_elements(items)
print(most_common)

 

Senior Developer's Approach

A senior developer would use collections.Counter to find the most common elements:

from collections import Counter

def find_most_common_elements(items):
    counter = Counter(items)
    most_common_count = counter.most_common(1)[0][1]
    return [item for item, count in counter.items() if count == most_common_count]

 

# Usage
items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
most_common = find_most_common_elements(items)
print(most_common)

 

 

Example 3: Simplifying File Handling


Junior Developer's Approach

A junior developer might write custom code to read a file and count the number of lines, words, and characters:

def file_statistics(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
        num_lines = len(lines)
        num_words = sum(len(line.split()) for line in lines)
        num_chars = sum(len(line) for line in lines)
    return num_lines, num_words, num_chars

# Usage
num_lines, num_words, num_chars = file_statistics('example.txt')
print(f"Lines: {num_lines}, Words: {num_words}, Characters: {num_chars}")

 

Senior Developer's Approach

A senior developer would use Python's built-in functions and libraries to achieve the same task more succinctly:

from pathlib import Path

# Usage
file_path = Path('example.txt')
num_lines = sum(1 for _ in file_path.open('r'))
num_words = sum(len(line.split()) for line in file_path.open('r'))
num_chars = file_path.stat().st_size

print(f"Lines: {num_lines}, Words: {num_words}, Characters: {num_chars}")

 

Key Improvements:

  1. Leverage Built-in Libraries: Use collections.Counter for frequency counting and dictionary merging.
     
  2. Simplify File Handling: Use pathlib for more concise and readable file operations.
     
  3. Efficient Code: Reduce the amount of custom code by using built-in functions that are optimized and tested.
     

Using Python's rich standard library not only simplifies the code but also makes it more efficient and maintainable. These examples demonstrate how a senior developer's approach to refactoring can lead to more modular, maintainable, and reusable code, following principles such as DRY, single responsibility, and clear separation of concerns.

Tag list:
- Tips
- Tips and Tricks
- Refactor Code
- Refactoring
- Refactor Tips
- Python tips
- built-in functions
- core library
- Python core library

Theo dõi

Theo dõi bản tin của chúng tôi và không bao giờ bỏ lỡ những tin tức mới nhất.