ohn0 / youtube-livechat-scraper

grab youtube live chat data from existing VODs
MIT License
9 stars 2 forks source link

Centralize Utility Functions for Code Reusability #17

Open repollo opened 1 year ago

repollo commented 1 year ago

Im adding this one as an issue since Im unsure whats the project direction in terms of structure/organizational, so if this is of any interest it can be moved to a PR afterwards or something.

So Im adding this method to PinnedMessage, SuperChatMessage, MembershipChatMessage, MembershipGiftedMessage and ChatMessage.

def find_key(data, target_key):
    if isinstance(data, dict):
        if target_key in data:
            return data[target_key]
        for key, value in data.items():
            result = find_key(value, target_key)
            if result:
                return result
    elif isinstance(data, list):
        for item in data:
            result = find_key(item, target_key)
            if result:
                return result
    return None

I added the method so that I can go ahead and extract 'authorExternalChannelId' from the data.

result = find_key(self.content_node, "authorExternalChannelId")
if result:
    self.authorExternalChannelId = result
else:
    self.authorExternalChannelId = ''

My question is, is there a specific directory and/or module to add utility methods/functions that can be accessed by other modules so that I dont have to add the same code to different parts, I can just call it easy and thats it?

Last one I swear, for today... lol...

ohn0 commented 1 year ago

don't really have one, I'd guess right under livechat_scraper, could add a helpers or utilities module and add it?

repollo commented 1 year ago

that would be nice, then in the other modules we can just reference from that specific module if we need a utility function or if we need to expand utility methods we can add them in there instead of writing the same code over and over in different files.

This practice is called the DRY principle (Don't Repeat Yourself) which is a fundamental principle in software engineering aimed at reducing repetition of software patterns, replacing them with abstractions or using data normalization to avoid redundancy.

Cheers!

EDIT: Actually if you need code review and are asking ChatGPT to review it you can ask him if its following the coding principles below,


# Coding Principles

In addition to Pythonic coding style, PEP 8, and the Single Responsibility Principle, several other coding guidelines and best practices are widely adopted in the industry and recommended by the Python Software Foundation. Some of these include:

1. PEP 8 is the Python Enhancement Proposal that provides a style guide for writing Python code. It is a widely accepted set of coding conventions and best practices that promote readability and consistency in Python projects. Following PEP 8 helps make code more understandable and maintainable, which is particularly important when collaborating with other developers.

Some of the key recommendations in PEP 8 include:

   Indentation: Use 4 spaces per indentation level, and don't use tabs.

   Line length: Limit lines to a maximum of 79 characters.

   Blank lines: Use blank lines sparingly to separate logical sections of code, such as function and class definitions, and groups of related statements.

   Imports: Group imports into three sections: standard library imports, third-party imports, and local application/library imports, separated by a blank line.

   Whitespace: Use whitespace consistently and avoid using it excessively. For example, avoid extra spaces around parentheses and after commas.

   Naming conventions:
   - Use `lowercase_with_underscores` for function and variable names.
   - Use `UpperCase` for class names.
   - Use `UPPERCASE_WITH_UNDERSCORES` for constants.

   Comments: Write clear and concise comments that explain your code's purpose and any non-obvious decisions. Use inline comments sparingly and prefer to write docstrings for functions and classes.

   String quotes: Be consistent with your choice of string quotes. Either single quotes (`'`) or double quotes (`"`) are acceptable, but try to use the same type of quotes throughout your code.

By following PEP 8 guidelines, you can help ensure that your Python code is consistent, easy to read, and maintainable for both yourself and others.

2. PEP 20 - The Zen of Python: A collection of software design principles for writing computer programs in Python. It consists of 19 aphorisms that emphasize simplicity, readability, and elegance.

3. PEP 257 - Docstring Conventions: This PEP provides conventions for writing docstrings in Python code. Following these conventions ensures that your code documentation is consistent and easy to understand.

4. DRY (Don't Repeat Yourself) Principle: This principle aims to reduce repetition in code by abstracting common functionality into reusable functions, classes, or modules. This leads to more maintainable and less error-prone code.

5. SOLID Principles: SOLID is an acronym for a set of five design principles aimed at making software more maintainable, scalable, and robust. Although these principles are not exclusive to Python, they can be applied to Python code:
   - Single Responsibility Principle (already mentioned)
   - Open/Closed Principle
   - Liskov Substitution Principle
   - Interface Segregation Principle
   - Dependency Inversion Principle

6. KISS (Keep It Simple, Stupid) Principle: This principle advocates for simplicity in code design. It encourages developers to avoid complexity and instead focus on creating straightforward, easy-to-understand solutions.

7. YAGNI (You Aren't Gonna Need It) Principle: This principle recommends not adding functionality to your code until it's necessary. It helps prevent code bloat and reduces the risk of over-engineering.

8. Code Reviews: Regularly reviewing code with your peers helps to ensure that the code follows best practices, is maintainable, and is free of bugs. It also promotes knowledge sharing and collaboration within the team.

9. Test-Driven Development (TDD): TDD is a development methodology where you write tests before writing the actual code. This practice ensures that your code is correct, robust, and maintainable.

10.  Continuous Integration (CI) and Continuous Deployment (CD): These practices involve automatically building, testing, and deploying code changes to ensure that the codebase remains stable and that new features and bug fixes can be delivered quickly and reliably.

By adhering to these guidelines and best practices, you can create high-quality, maintainable Python code that is consistent with industry standards.