### Clone Repository and Install Dependencies Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This snippet shows how to clone the hexbytes repository and set up the development environment using a virtual environment and pip. It installs development dependencies and configures pre-commit hooks for code style. ```sh git clone git@github.com:your-github-username/hexbytes.git cd hexbytes virtualenv -p python venv . venv/bin/activate python -m pip install -e ".[dev]" pre-commit install ``` -------------------------------- ### Initialize HexBytes from Hex String Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Shows how to initialize a HexBytes object directly from a hexadecimal string. The class automatically handles case-insensitivity and optional '0x' prefixes. ```python from hexbytes import HexBytes # HexBytes accepts the hex string representation as well, ignoring case and 0x prefixes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') print(hb) hb = HexBytes('0x03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') print(hb) ``` -------------------------------- ### Prepare and Test Release Package Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This sequence of commands prepares for a new release by pulling the latest changes from the main branch and testing the package build. It ensures the package can be built and installed correctly in a temporary virtual environment. ```sh git checkout main && git pull make package-test ``` -------------------------------- ### Get 0x-Prefixed Hex String with to_0x_hex() Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Demonstrates the use of the `to_0x_hex()` method to obtain a hexadecimal string representation of the HexBytes object that is explicitly prefixed with '0x'. ```python from hexbytes import HexBytes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') print(hb.to_0x_hex()) ``` -------------------------------- ### Install faster-hexbytes using pip Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Installs the faster-hexbytes library using pip. This is the primary method for adding the library to your Python environment. ```bash pip install faster-hexbytes ``` -------------------------------- ### Get Length of HexBytes Object Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Demonstrates how to retrieve the total number of bytes contained within a HexBytes object using the standard `len()` function. This is useful for understanding the size of the byte data. ```python from hexbytes import HexBytes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') # show how many bytes are in the value print(len(hb)) ``` -------------------------------- ### Accessing Bytes and Slicing HexBytes Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Shows how to access individual bytes or slices of a HexBytes object, similar to standard Python bytes. This allows for granular manipulation and retrieval of byte data. ```python from hexbytes import HexBytes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') # get the first byte: print(hb[0]) # get the first 5 bytes: print(hb[:5]) ``` -------------------------------- ### Convert Bytes to HexBytes Representation Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Demonstrates converting a bytes object into a HexBytes object, which provides a prettier console representation. This is useful for displaying raw byte data in a more readable hexadecimal format. ```python from hexbytes import HexBytes # convert from bytes to a prettier representation at the console HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;") ``` -------------------------------- ### HexBytes .hex() and print() Behavior Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Illustrates that HexBytes does not override the standard .hex() or __str__ methods inherited from the parent bytes type. .hex() returns the raw hex string, while print() shows the bytes representation. ```python from hexbytes import HexBytes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') print(hb.hex()) print(hb) ``` -------------------------------- ### Cast HexBytes back to Bytes Type Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/hexbytes.rst Explains how to convert a HexBytes object back into a standard Python `bytes` object using a type cast. This is useful when interacting with libraries or functions that expect standard bytes. ```python from hexbytes import HexBytes hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') # cast back to the basic bytes type print(bytes(hb)) ``` -------------------------------- ### Release New Version to GitHub and PyPI Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This command automates the release process by bumping the version number, creating a git commit and tag, building the package, and pushing it to GitHub and PyPI. The 'bump' argument specifies which part of the version to increment. ```sh make release bump=$$VERSION_PART_TO_BUMP$$ ``` -------------------------------- ### Specify Custom New Version for Release Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This command demonstrates how to release a specific, custom version, particularly useful for issuing unstable versions (alpha, beta) when the current version is stable. It overrides the default version bumping logic. ```sh make release bump="--new-version 4.0.0-alpha.1" ``` -------------------------------- ### Run Project Tests Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This command executes all tests for the hexbytes project using the pytest framework. Running tests is recommended for exploring the codebase and ensuring code integrity. ```sh pytest tests ``` -------------------------------- ### Build Release Notes Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This command generates the release notes using towncrier, requiring a version part to bump (e.g., major, minor, patch). It helps in maintaining changelogs by compiling news fragments. ```sh make notes bump=$$VERSION_PART_TO_BUMP$$ ``` -------------------------------- ### Initialize HexBytes from various data types Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Demonstrates creating HexBytes objects from different input types including raw bytes, hex strings (with or without '0x' prefix, case-insensitive), integers, booleans, bytearrays, and memoryviews. This showcases the flexibility of the HexBytes constructor. ```python from faster_hexbytes import HexBytes # Create from raw bytes hb = HexBytes(b"\x03\x08wf\xbfh\xe7") print(repr(hb)) # HexBytes('0x03087766bf68e7') # Create from hex string (with or without 0x prefix, case-insensitive) hb = HexBytes('0x03087766BF68E78671D1EA436AE087DA') hb = HexBytes('03087766bf68e78671d1ea436ae087da') # Same result # Create from integer (non-negative only) hb = HexBytes(255) print(repr(hb)) # HexBytes('0xff') hb = HexBytes(65535) print(repr(hb)) # HexBytes('0xffff') # Create from boolean hb_true = HexBytes(True) print(repr(hb_true)) # HexBytes('0x01') hb_false = HexBytes(False) print(repr(hb_false)) # HexBytes('0x00') # Create from bytearray hb = HexBytes(bytearray([15, 26, 37])) print(repr(hb)) # HexBytes('0x0f1a25') # Create from memoryview data = b"\x0F\x1a\x2b" hb = HexBytes(memoryview(data)) print(repr(hb)) # HexBytes('0x0f1a2b') ``` -------------------------------- ### Enforce Code Style with pre-commit Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/docs/contributing.rst This command manually runs the pre-commit hooks to enforce code style consistency across the library. Pre-commit runs automatically on each commit but can be invoked manually for checks or fixes. ```sh make lint ``` -------------------------------- ### Ensure Compatibility with hexbytes using Python Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Shows how faster-hexbytes maintains full compatibility with the original hexbytes package through inheritance. Demonstrates that isinstance and issubclass checks work correctly, and faster-hexbytes can be used interchangeably where hexbytes.HexBytes is expected. ```python from faster_hexbytes import HexBytes as FasterHexBytes from hexbytes import HexBytes as OriginalHexBytes fast_hb = FasterHexBytes(b"\x0F\x1a") # isinstance checks work with both classes assert isinstance(fast_hb, FasterHexBytes) assert isinstance(fast_hb, OriginalHexBytes) # issubclass checks work too assert issubclass(FasterHexBytes, OriginalHexBytes) # Can be used anywhere hexbytes.HexBytes is expected def process_hex(data: OriginalHexBytes) -> str: return data.to_0x_hex() result = process_hex(fast_hb) # Works seamlessly print(result) # '0x0f1a' ``` -------------------------------- ### Hexbytes Performance with Byte Arrays Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/benchmarks/results/main.md This snippet demonstrates the performance of the hexbytes function when initialized with bytearray objects. It evaluates execution time and efficiency across a range of bytearray initializations, including numerical sequences, patterns, and string conversions. ```python hexbytes_new[bytearray(0-9)] hexbytes_new[bytearray(4-byte pattern)] hexbytes_new[bytearray(all byte values)] hexbytes_new[bytearray(alternating 0x00/0xff)] hexbytes_new[bytearray(alternating 0xaa/0x55)] hexbytes_new[bytearray(ascii sentence)] hexbytes_new[bytearray(b'')] hexbytes_new[bytearray(b'\x00'*32)] hexbytes_new[bytearray(b'\x00\xff\x00\xff')] hexbytes_new[bytearray(b'\x01'*100)] hexbytes_new[bytearray(b'\x01'*2048)] hexbytes_new[bytearray(b'\x01\x02\x03')] hexbytes_new[bytearray(b'\x10\x20\x30\x40\x50')] hexbytes_new[bytearray(b'\x7f'*8)] hexbytes_new[bytearray(b'\x80'*8)] hexbytes_new[bytearray(b'\xde\xad\xbe\xef')] hexbytes_new[bytearray(b'\xff'*64)] hexbytes_new[bytearray(b'a'*1024)] hexbytes_new[bytearray(b'abc')] hexbytes_new[bytearray(long alternating)] hexbytes_new[bytearray(mixed pattern)] hexbytes_new[bytearray(multiples of 0x10)] hexbytes_new[bytearray(palindrome ascii)] hexbytes_new[bytearray(palindrome numeric)] hexbytes_new[bytearray(palindrome)] hexbytes_new[bytearray(repeated 0-9)] hexbytes_new[bytearray(single 0xff)] hexbytes_new[bytearray(single null byte)] hexbytes_new[bytearray(two patterns)] ``` -------------------------------- ### Perform slicing and indexing on HexBytes Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Shows how to access individual bytes (returning integers) and slices (returning new HexBytes instances) from a HexBytes object. It covers positive, negative, and stepped indexing. ```python from faster_hexbytes import HexBytes hb = HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b') # Get individual byte (returns int) first_byte = hb[0] print(first_byte) # 3 print(type(first_byte)) # # Get slice (returns HexBytes) first_five = hb[:5] print(repr(first_five)) # HexBytes('0x03087766bf') # Negative indexing last_byte = hb[-1] print(last_byte) # 59 # Slice with step every_other = hb[::2] print(repr(every_other)) # HexBytes('0x0377bf87...') (every other byte) # Get length print(len(hb)) # 32 ``` -------------------------------- ### Utilize optimized pickling with HexBytes Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Explains and demonstrates the optimized pickling support for HexBytes objects, which uses a custom `__reduce__` method to bypass validation during unpickling for improved performance. This includes pickling individual objects and complex data structures containing HexBytes instances. ```python import pickle from faster_hexbytes import HexBytes # Create HexBytes instance original = HexBytes(b"\x0F\x1a\x2b\x3c") # Pickle and unpickle dumped = pickle.dumps(original) loaded = pickle.loads(dumped) # Verify equality assert loaded == original print(repr(loaded)) # HexBytes('0x0f1a2b3c') # Works with complex data structures data = { 'tx_hash': HexBytes('0xabcdef'), 'block_hash': HexBytes('0x123456'), 'values': [HexBytes(i) for i in range(5)] } restored = pickle.loads(pickle.dumps(data)) print(repr(restored['tx_hash'])) # HexBytes('0xabcdef') ``` -------------------------------- ### Hexbytes Performance with Byte Strings Source: https://github.com/bobthebuidler/faster-hexbytes/blob/master/benchmarks/results/main.md This snippet showcases the performance of the hexbytes function when initialized with various byte string literals. It measures execution time and efficiency for different byte patterns, including zero-filled, alternating bytes, and specific byte sequences. ```python hexbytes_new[b'\x00'*32] hexbytes_new[b'\x00\xff\x00\xff'] hexbytes_new[b'\x01'*100] hexbytes_new[b'\x01'*2048] hexbytes_new[b'\x01\x02\x03'] hexbytes_new[b'\x10\x20\x30\x40\x50'] hexbytes_new[b'\x7f'*8] hexbytes_new[b'\x80'*8] hexbytes_new[b'\xde\xad\xbe\xef'] hexbytes_new[b'\xff'*64] hexbytes_new[b'a'*1024] hexbytes_new[b'abc'] ``` -------------------------------- ### Handle Errors with HexBytes in Python Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Demonstrates how to handle ValueErrors and TypeErrors when creating HexBytes objects with invalid inputs like negative integers, invalid hex strings, or unsupported types. This ensures robust error management in your application. ```python from faster_hexbytes import HexBytes # Negative integers raise ValueError try: hb = HexBytes(-1) except ValueError as e: print(f"Error: {e}") # Error: Cannot convert negative integer -1 to bytes # Invalid hex strings raise ValueError try: hb = HexBytes("0xGHIJKL") # Invalid hex characters except Exception as e: print(f"Error: {e}") # Non-hexadecimal digit found # Invalid types raise TypeError try: hb = HexBytes([1, 2, 3]) # Lists not supported except TypeError as e: print(f"Error: {e}") # Cannot convert [1, 2, 3] of type to bytes ``` -------------------------------- ### Convert HexBytes to standard Python types Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Demonstrates converting a HexBytes object back into standard Python types such as `bytes`, unprefixed hex strings (`hex()`), 0x-prefixed hex strings (`to_0x_hex()`), and its string (`str()`) and representation (`repr()`) forms. ```python from faster_hexbytes import HexBytes hb = HexBytes('0x03087766bf') # Convert to bytes raw_bytes = bytes(hb) print(raw_bytes) # b'\x03\x08wf\xbf' # Convert to hex string (no prefix) hex_str = hb.hex() print(hex_str) # '03087766bf' # Convert to 0x-prefixed hex string hex_0x = hb.to_0x_hex() print(hex_0x) # '0x03087766bf' # String representation (shows raw bytes) print(str(hb)) # b'\x03\x08wf\xbf' # Repr shows hex format print(repr(hb)) # HexBytes('0x03087766bf') ``` -------------------------------- ### Convert HexBytes to 0x-prefixed hex string Source: https://context7.com/bobthebuidler/faster-hexbytes/llms.txt Illustrates the use of the `to_0x_hex()` method to obtain a 0x-prefixed hexadecimal string representation of the HexBytes object. This is contrasted with the standard `hex()` method which returns an unprefixed string. ```python from faster_hexbytes import HexBytes hb = HexBytes(b"\x0F\x1a") # Built-in hex() method (no prefix) print(hb.hex()) # '0f1a' # to_0x_hex() method (with 0x prefix) print(hb.to_0x_hex()) # '0x0f1a' # Useful for Ethereum addresses and transaction hashes tx_hash = HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b') print(tx_hash.to_0x_hex()) # '0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.