eth-utils

repository·main·Indexed 43 days ago

https://github.com/ethereum/eth-utils

Common utility functions for Python code that interacts with the Ethereum network, providing tools for address validation, ABI encoding/decoding, data conversion, and functional programming applicators.

Tokens
10.5K
Snippets
56
Records
60
Agent score
55%

What's inside eth-utils

  1. Explore the eth_utils module structure

    main

    The eth_utils package is organized into several specialized submodules and subpackages to handle Ethereum-specific data types and operations. Key functional areas include:

    • Core Ethereum Types: eth_utils.address, eth_utils.abi, eth_utils.types, and eth_utils.units.
    • Data Conversion & Encoding: eth_utils.conversions, eth_utils.encoding, and eth_utils.hexadecimal.
    • Mathematical & Numeric Operations: eth_utils.numeric, eth_utils.currency, and eth_utils.units.
    • Functional Programming & Utilities: eth_utils.functional, eth_utils.applicators, eth_utils.curried, and eth_utils.toolz.
    • Security & Debugging: eth_utils.crypto, eth_utils.debug, and eth_utils.logging.
    • Developer Ergonomics: eth_utils.humanize, eth_utils.decorators, and eth_utils.exceptions.
  2. How Applicators work

    main

    Applicators are tools used to apply "formatters" to data structures. A formatter is defined as any callable that accepts a single positional argument and returns a formatted result (e.g., int(), str(), or a custom function).

    Applicators allow you to:

    • Apply formatters to dictionary values by key.
    • Apply formatters to lists/sequences by index.
    • Conditionally apply a formatter based on a predicate.
    • Apply one of several formatters based on which condition matches first.
  3. Release a new version

    main

    Use the make release command to bump the version. You must specify which part of the version to bump using the bump argument.

    Version Formats:

    • Stable: {major}.{minor}.{patch}
    • Unstable: {major}.{minor}.{patch}-{stage}.{devnum} (where stage is alpha or beta).

    Common Commands:

    • make release bump=minor: Bumps the minor version.
    • make release bump=stage: If currently in a beta version, this switches to a stable version.
    • make release bump="--new-version 4.0.0-alpha.1 devnum": Issues an unstable version from a stable state by specifying the new version explicitly.
    make release bump=$$VERSION_PART_TO_BUMP$$
  4. Importing eth_utils functions

    main

    All utility functions can be imported directly from the eth_utils module. For a functional programming style, you can import curried versions of these functions from the eth_utils.curried module.

    # Standard import
    from eth_utils import some_function
    
    # Curried import
    from eth_utils.curried import hexstr_if_str
  5. Set up a local development environment

    main

    If you are contributing to eth-utils, follow these steps to set up your environment:

    1. Clone the repository.
    2. Create and activate a Python 3 virtual environment.
    3. Install the package in editable mode with development dependencies.
    4. Install pre-commit hooks to maintain code style automatically.
    git clone git@github.com:ethereum/eth-utils.git
    cd eth-utils
    virtualenv -p python3 venv
    . venv/bin/activate
    python -m pip install -e ".[dev]"
    pre-commit install
  6. Set up a development environment for eth-utils

    main

    To contribute to eth-utils, fork the repository, clone it locally, and set up a virtual environment with development dependencies. It is recommended to use virtualenv and install the package in editable mode with the [dev] extra to include testing and linting tools.

    git clone git@github.com:your-github-username/eth-utils.git
    
    cd eth-utils
    virtualenv -p python venv
    . venv/bin/activate
    python -m pip install -e ".[dev]"
    pre-commit install
  7. Create newsfragments for release notes

    main

    To contribute to the release notes, you must create a "newsfragment" in the newsfragments/ directory. A newsfragment is a short ReST-formatted text file describing changes relevant to end-users.

    Naming Convention: Files must be named using the pattern <ISSUE>.<TYPE>.rst:

    • <ISSUE>: The issue number (or PR number if no issue exists).
    • <TYPE>: One of the following allowed types:
      • breaking
      • bugfix
      • deprecation
      • docs
      • feature
      • internal
      • misc
      • performance
      • removal

    Example filenames:

    • 123.feature.rst
    • 456.bugfix.rst

    Formatting Note: Do not use complex formatting. The towncrier tool will automatically reflow your text during the release process.

    # Example of creating a newsfragment file
    touch 123.feature.rst
  8. Configure and use logging utilities

    main

    The logging utilities allow for custom logger classes and a specialized DEBUG2 log level.

    Getting Loggers

    • get_logger(name, logger_class=None): Returns a logger instance. If logger_class is provided, it returns an instance of that class.
    • get_extended_debug_logger(name): Always returns an instance of ExtendedDebugLogger.

    Using HasLogger and HasExtendedDebugLogger

    Inherit from these classes to automatically provide a .logger attribute to your classes or instances. The logger name is derived from the class's __qualname__.

    • HasLogger: Provides a standard logger.
    • HasExtendedDebugLogger: Provides an ExtendedDebugLogger.

    Warning: HasLogger does not behave well with typing.Generic. For generic classes, assign logging instances manually.

    The DEBUG2 Log Level

    ExtendedDebugLogger provides a debug2() method for a specific log level (numeric level 8).

    • To ensure logs are labeled DEBUG2 instead of Level 8, call setup_DEBUG2_logging() once at application startup. This function is idempotent.
    import logging
    from eth_utils import get_logger, get_extended_debug_logger, HasLogger, setup_DEBUG2_logging, ExtendedDebugLogger
    
    # Setup DEBUG2 level globally
    setup_DEBUG2_logging()
    
    # Using get_logger
    logger = get_logger('my_app')
    
    # Using HasLogger inheritance
    class MyService(HasLogger):
        def do_work(self):
            self.logger.info("Working...")
    
    # Using ExtendedDebugLogger
    class DebugService(HasExtendedDebugLogger):
        def debug_info(self):
            self.logger.debug2("Detailed debug info")
  9. Update the network list

    main

    The library uses a JSON file located at eth_utils/__json/eth_networks.json to initialize Networks objects, which allow you to retrieve network information using a chain ID. To update this local JSON file with the latest information from the remote list, run the update_networks.py script.

    python update_networks.py