Fujio-Turner / sg-log-reader-demo

Parsing and Aggregating Sync Gateway Logs
https://fujio-turner.github.io/sg-log-reader-demo/
Apache License 2.0
5 stars 1 forks source link

House Keeping - Moving Functions Out #38

Open Fujio-Turner opened 2 months ago

Fujio-Turner commented 2 months ago

Organizing your code into separate modules for better maintainability and modularity is a good practice. Here's how you can refactor your sg_log_reader.py to move string checking and parsing functions into separate files:

Step 1: Create a New Module for String Operations

Create a new file, let's call it string_utils.py, in a directory like utils/. Here's how you might structure it:

# utils/string_utils.py

def is_valid_log_entry(log_entry):
    # Your validation logic here
    pass

def parse_log_entry(log_entry):
    # Your parsing logic here
    pass

# Add other string-related functions here

Step 2: Adjust the Main Class

Modify sg_log_reader.py to import these functions:

# sg_log_reader.py

from utils.string_utils import is_valid_log_entry, parse_log_entry

class SGLogReader:
    def __init__(self):
        # Your initialization code

    def read_log_file(self, file_path):
        # Your code to read the file
        with open(file_path, 'r') as file:
            for line in file:
                if is_valid_log_entry(line):
                    parsed_entry = parse_log_entry(line)
                    # Process parsed entry

    # Other methods...

Step 3: Project Structure

Here's how your project structure might look:

project_root/
│
├── sg_log_reader.py
│
└── utils/
    └── string_utils.py

Step 4: Ensure Proper Import Paths

Step 5: Testing

# test_sg_log_reader.py

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils')))

from string_utils import is_valid_log_entry, parse_log_entry
from sg_log_reader import SGLogReader

# Your tests here

Benefits:

Considerations:

This approach keeps your main class cleaner, focusing on the high-level logic, while the utility functions are neatly organized elsewhere.