Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

House Keeping - Moving Functions Out #38

Open
Fujio-Turner opened this issue Aug 23, 2024 · 0 comments
Open

House Keeping - Moving Functions Out #38

Fujio-Turner opened this issue Aug 23, 2024 · 0 comments
Assignees

Comments

@Fujio-Turner
Copy link
Owner

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

  • If sg_log_reader.py is in the root directory and string_utils.py is in a subdirectory, you might need to adjust your import path or add the directory to sys.path if you're running from a different directory.

Step 5: Testing

  • Update your tests in test_sg_log_reader.py to reflect the new import structure:
# 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:

  • Modularity: Functions are grouped by their purpose, making the code easier to maintain and understand.
  • Reusability: If these string operations are useful elsewhere, they're now in a module that can be easily imported.
  • Testability: You can now test these functions independently of the main class.

Considerations:

  • Namespace: If you're planning to expand this, consider creating a package (__init__.py in utils/) to manage namespace.
  • Documentation: Ensure each function in string_utils.py is well-documented for clarity when others (or you in the future) use these functions.

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

@Fujio-Turner Fujio-Turner self-assigned this Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant