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

docstring #1

Open
fabriziosalmi opened this issue Oct 2, 2023 · 5 comments
Open

docstring #1

fabriziosalmi opened this issue Oct 2, 2023 · 5 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@fabriziosalmi
Copy link
Owner

Why is this Important?

  • Good comments and documentation make the code more understandable and maintainable.
  • They provide context and explain why the code is written the way it is.
  • They help other developers, and even the author, to understand the code better when revisiting it in the future.

What to Document?

  1. Function/Method Descriptions:

    • What does the function/method do?
    • What are the parameters, and what do they represent?
    • What is the return value?
    • Are there any exceptions that can be raised?
  2. Class Descriptions:

    • What is the purpose of the class?
    • How should it be used?
  3. Module/Script Descriptions:

    • What is the purpose of the script/module?
    • How does it fit into the overall project?
  4. Inline Comments:

    • Use inline comments sparingly to explain complex or non-intuitive sections of code.

Example Improvements:

1. 1_retrieve_blacklist.py

def retrieve_and_save_blacklist():
    """
    Retrieves the domain blacklist from the specified URL and saves it locally.
    If the request fails, it logs an error message with the status code.
    If any exception occurs during the process, it logs the error message.
    """
    # ... rest of the code ...

2. 2_process_blacklist.py

def is_valid_fqdn(fqdn):
    """
    Validates the given FQDN (Fully Qualified Domain Name).
    
    :param fqdn: The domain name to validate.
    :type fqdn: str
    :return: True if the domain name is valid, False otherwise.
    :rtype: bool
    """
    # ... rest of the code ...

3. 3_web_gui.py

def load_blockchain_data():
    """
    Loads the blockchain data from the JSON file.
    
    :return: A list of blockchain data if the file exists, otherwise an empty list.
    :rtype: list
    """
    # ... rest of the code ...

Additional Tips:

  • Keep comments and documentation concise and to the point.
  • Update comments and documentation when modifying the code to keep them in sync.
  • Avoid obvious comments that don’t add any value.
@fabriziosalmi fabriziosalmi added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 2, 2023
@fabriziosalmi
Copy link
Owner Author

import requests
import datetime
import os
from requests.exceptions import HTTPError, ConnectionError, Timeout, RequestException

# Define the URL to retrieve the blacklist from
blacklist_url = "https://get.domainsblacklists.com/blacklist.txt"

# Define the local file path to save the retrieved blacklist
local_blacklist_path = "data/blacklist.txt"

def retrieve_and_save_blacklist():
    try:
        # Send an HTTP GET request to the URL
        response = requests.get(blacklist_url)
        response.raise_for_status()  # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
        
        # Get the current timestamp
        current_timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        # Save the blacklist to the local file with timestamp
        with open(local_blacklist_path, "w") as blacklist_file:
            blacklist_file.write(f"# Blacklist retrieved on {current_timestamp}\\n")
            blacklist_file.write(response.text)
        
        print("Blacklist retrieved and saved successfully.")
        
    except HTTPError as e:
        print(f"HTTP error occurred: {str(e)}")  # e.g. 404 Not Found or 500 Internal Server Error
    except ConnectionError as e:
        print(f"Error connecting: {str(e)}")  # e.g. DNS resolution, refused connection, etc.
    except Timeout as e:
        print(f"Timeout error: {str(e)}")  # e.g. request timeout
    except RequestException as e:
        print(f"An error occurred while making the request: {str(e)}")
    except Exception as e:
        print(f"An unexpected error occurred: {str(e)}")

if __name__ == "__main__":
    retrieve_and_save_blacklist()

@fabriziosalmi
Copy link
Owner Author

Enhanced Error Handling:

import hashlib
import json
import os
import time
import tldextract
import concurrent.futures

# ... (other code remains unchanged)

def save_transactions_to_blockchain(transactions):
    blockchain_data = []

    try:
        # Load existing blockchain data if available
        if os.path.exists(blockchain_data_path):
            with open(blockchain_data_path, "r") as blockchain_file:
                blockchain_data = json.load(blockchain_file)

        # Append new transactions to the existing data
        blockchain_data.extend(transactions)

        # Prune old transactions if the maximum limit is exceeded
        if len(blockchain_data) > max_transactions:
            blockchain_data = blockchain_data[-max_transactions:]

        # Save the updated blockchain data
        with open(blockchain_data_path, "w") as blockchain_file:
            json.dump(blockchain_data, blockchain_file, indent=4)

    except json.JSONDecodeError as e:
        print(f"Error decoding JSON: {str(e)}")
    except FileNotFoundError as e:
        print(f"File not found: {str(e)}")
    except IOError as e:
        print(f"I/O error occurred: {str(e)}")
    except Exception as e:
        print(f"An unexpected error occurred: {str(e)}")

# ... (other code remains unchanged)

if __name__ == "__main__":
    try:
        # ... (other code remains unchanged)

        print(f"Processing completed. {len(transactions)} valid transactions created and saved.")

    except FileNotFoundError as e:
        print(f"Blacklist file not found: {str(e)}")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

Explanation:

  1. Specific Exceptions: The enhanced version catches more specific exceptions related to file operations and JSON processing, such as FileNotFoundError, IOError, and JSONDecodeError, providing more context about the error that occurred.
  2. User-Friendly Messages: The error messages are made more user-friendly and informative, providing clear information about the type of error that occurred.

Additional Considerations:

  • Logging: Consider implementing logging to record errors and important events persistently.
  • Error Recovery: Where possible, consider implementing error recovery mechanisms to allow the script to continue processing the remaining data even if an error occurs with a specific transaction or batch.

@fabriziosalmi
Copy link
Owner Author

Enhanced Error Handling:

from flask import abort  # Import abort to handle error scenarios

# ... (other code remains unchanged)

@app.route("/block/<int:block_index>")
@memory_cache.cached(timeout=30)  # Cache the response for 30 seconds (antiflood)
def block(block_index):
    blockchain_data = load_blockchain_data()

    if block_index >= 0 and block_index < len(blockchain_data):
        block = blockchain_data[block_index]
        return render_template("block.html", block=block)
    else:
        abort(404, description="Block not found")  # Return a 404 error with a custom message

# ... (other code remains unchanged)

def load_blockchain_data():
    try:
        with open("data/blockchain_data.json", "r") as blockchain_file:
            blockchain_data = json.load(blockchain_file)
        return blockchain_data
    except json.JSONDecodeError as e:
        app.logger.error(f"Error decoding JSON: {str(e)}")
        return []
    except FileNotFoundError:
        app.logger.warning("Blockchain data file not found, returning an empty list.")
        return []
    except Exception as e:
        app.logger.error(f"An unexpected error occurred: {str(e)}")
        return []

# ... (other code remains unchanged)

Explanation:

  1. Flask abort: The abort function from Flask is used to abort a request and return an error to the client. In this case, if a block is not found, a 404 error is returned with a custom message.
  2. Logging: The app.logger is used to log warnings and errors. This is especially useful for debugging and monitoring purposes in production environments.
  3. User-Friendly Error Messages: The error messages are made more user-friendly and informative, providing clear information about the type of error that occurred.

Additional Considerations:

  • Error Pages: Consider creating custom error pages to handle different HTTP error statuses like 404, 500, etc., to enhance the user experience.
  • Input Validation: Validate user inputs and query parameters to ensure they meet the expected format and range, and handle invalid inputs gracefully.

@fabriziosalmi
Copy link
Owner Author

Why is this Important?

  • Performance optimization ensures that your application runs efficiently, conserves computational resources, and provides a better user experience.
  • It helps in reducing the load times and improving the responsiveness of your application.

What to Optimize?

  1. Optimize Data Processing:

    • Review the data processing logic and identify any bottlenecks or redundant operations.
    • Optimize loops, use efficient data structures, and avoid unnecessary computations.
  2. Caching:

    • Implement caching to store the results of expensive or frequently used computations to avoid recalculating them.
    • Use caching libraries like Flask-Caching for caching in Flask applications.
  3. Concurrency and Parallelism:

    • Use concurrency and parallelism to execute multiple tasks simultaneously and make better use of available CPU cores.
    • Consider using Python’s concurrent.futures module or asynchronous programming with asyncio.

Example Improvements:

1. Optimize Data Processing:

  • Review and optimize the logic in 2_process_blacklist.py for processing the domain blacklist.
  • Use set operations, list comprehensions, and generator expressions where appropriate for more efficient data processing.

2. Caching:

  • You are already using Flask-Caching in 3_web_gui.py, which is great! Consider expanding the use of caching to other parts of your application where appropriate.
  • Evaluate the cache timeout values and adjust them based on the frequency of data updates and available resources.

3. Concurrency and Parallelism:

  • Review the code to identify tasks that can be executed concurrently or in parallel, especially in data retrieval and processing.
  • Use ThreadPoolExecutor or ProcessPoolExecutor from concurrent.futures for parallel execution of tasks.

Additional Tips:

  • Profiling:
    • Use profiling tools like cProfile to identify bottlenecks in your code and focus your optimization efforts there.
  • Database Optimization:
    • If you are using a database, optimize database queries, use indexing, and consider denormalization where appropriate.
  • Load Testing:
    • Perform load testing to understand how your application performs under stress and identify areas for improvement.

@fabriziosalmi
Copy link
Owner Author

Implement Continuous Integration/Continuous Deployment (CI/CD).

Why is this Important?

  • CI/CD automates the testing and deployment of your applications, ensuring that you can release new changes quickly and reliably.
  • It helps in catching issues early, reducing manual errors, and accelerating the development cycle.

What to Implement?

  1. Continuous Integration:

    • Set up a CI service to automatically build and test your project every time you push changes to your repository.
    • Use CI services like GitHub Actions, Travis CI, or CircleCI.
  2. Continuous Deployment:

    • Automate the deployment process to ensure that the latest version of your application is always available.
    • Use CD services like Heroku, AWS CodeDeploy, or Google Cloud Build.
  3. Automated Testing:

    • As part of the CI process, run your test suite to catch issues early.
    • Ensure that your test suite is comprehensive and covers different parts of your application.

Example Implementations:

1. Continuous Integration:

  • Set up a .github/workflows/ci.yml file for GitHub Actions to run your tests on every push.
name: CI
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run tests
      run: python -m unittest discover

2. Continuous Deployment:

  • Set up a CD service to automatically deploy your application when you push changes to the main branch.
  • Configure the service to use your deployment scripts or commands.

3. Automated Testing:

  • Include a comprehensive test suite in your CI pipeline.
  • Ensure that the tests cover different components and use cases of your application.

Additional Tips:

  • Deployment Strategies:
    • Consider using different deployment strategies like blue-green deployment or canary releases to reduce downtime and risks.
  • Monitoring and Logging:
    • Set up monitoring and logging for your deployed application to keep track of its health and performance.
  • Configuration Management:
    • Manage your application configurations separately for different environments (development, staging, production).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant