This document provides guidelines, conventions, and architectural context for AI
assistants and developers contributing to bitvector-modern.
bitvector-modern is a modern, pure-Python package for memory-efficient packed
representation of bit arrays and bit vectors.
- Origin: An unofficial git fork and modernization of Avi Kak's original
BitVector 3.5.0(Purdue University). - Core Design: Uses standard library
array('H'/ unsigned short integer arrays) for compact bitwise storage, manipulation, and boolean logic operations. - Python Target: Requires Python
>=3.13. Employs modern Python features including type annotations (Self,Sequence), advanced f-strings, and clean modular structures.
The project repository is structured as a modern Python package:
BitVector/: Core package directory.BitVector.py: Primary module implementingBitVector.__init__.py: Package initialization and top-level symbols export.
tests/: Automated test suites executed viapytest.test_*.py: Unit and functional tests covering constructors, boolean logic, slicing, string output, and mathematical operations.test_benchmarks.py: Performance regression benchmarks executed viapytest-benchmark.
examples/: Reference usage scripts.demo.py: Working demo script illustrating package features.
.github/: CI/CD automation and templates, including GitHub Actions workflows (test.yml,lychee.yml),CODEOWNERS, andPULL_REQUEST_TEMPLATE.md.pyproject.toml: Modern project configuration defining package metadata, build system (hatchling), dependency groups (uv), and tool settings (ruff,pytest,ty,codespell,mdformat,coverage).uv.lock: Exact locked dependency resolution file managed byuv..pre-commit-config.yaml: Configuration for pre-commit git hooks.AGENTS.md: AI assistant and developer guidelines (this document).README.md&LICENSE: Project documentation and PSF-2.0 license file.
The core functionality of bitvector-modern is implemented in
BitVector/BitVector.py. Below is an overview of its data structures and API:
BitVectorClass: The primary class representing packed bit vectors.- Memory Array (
self.vector): Store bits in anarray.array('H')(unsigned short integers, 16 bits per element) for memory compactness. - Size (
self.size): Tracks the exact integer number of valid bits. - Iteration (
__iter__,__reversed__): Supports sequentially yielding individual bits forward and in reverse via generator expressions.
- Memory Array (
- Supports keyword arguments:
sizeandbitlist. - Factory Methods: Can be initialized from an integer (
from_int), raw bytes (from_bytes), a binary string (from_bitstring), a sequence of bits (from_bitlist), a string (from_string), or a hex string (from_hex). - File & Streaming Input: Can read incrementally from files or file objects
(
read_bits_from_file,read_bits_from_fileobject), tracking remaining data viaself.more_to_read.
- Bit Access & Mutators: Indexing and slicing via
__getitem__and__setitem__, bit assignment viaset_value, andreset(). - Boolean Logic (Dunders): Bitwise AND (
&/__and__), OR (|/__or__), XOR (^/__xor__), and NOT (~/__invert__). - Concatenation & Splitting: Vector addition (
+/__add__,+=/__iadd__),divide_into_two(), and padding (pad_from_left,pad_from_right). - Shifts & Rotations: Logical shifts (
shift_left,shift_right) and circular rotations (<</__lshift__,>>/__rshift__). - Permutations: Reordering bits via
permute(),unpermute(), andreverse(). - Comparisons & Equality: Full rich comparison support (
==,!=,<,<=,>,>=) supportingBitVector,int, andfloatoperands, and canonical ordering (min_canonical).==and!=also allow comparison with other types (evaluating toFalseandTruerespectively). - Conversions & Output: Integer conversion (
int()), string output (get_bitvector_in_ascii(),get_bitvector_in_hex(),__str__), and stream writing (write_to_file,write_bits_to_stream_object). - Analysis & Metrics: Bit counting (
bit_count,bit_count_sparse), distance/similarity (hamming_distance,jaccard_similarity,jaccard_distance),runs(),next_set_bit(),rank_of_bit_set_at_index(), and power-of-two checks (is_power_of_2). - Galois Field & Number Theory: Modular arithmetic and polynomial operations
over GF(2^n) (
gf_multiply,gf_divide_by_modulus,gf_multiply_modular,gf_MI), greatest common divisor (gcd), multiplicative inverse (multiplicative_inverse), and primality testing (test_for_primality). - Utilities: Random bit vector generation (
gen_random_bits) and deep copying (__deepcopy__).
We use modern Python tooling for dependency management, building, linting, and static analysis:
- Dependency Management (
uv): Useuvfor all virtual environments and package installations.uv sync
- Linting & Formatting (
ruff,pylint): Enforces code style, import sorting, formatting, and code quality checks.uv run ruff check --fix uv run ruff format uv run pylint BitVector tests
- Static Type Checking (
ty,mypy,pyrefly, &pyright): Enforces strict type annotations across all modules.uv run ty check uv run mypy . uv run pyrefly check uv run pyright - Markdown Formatting (
mdformat): Enforces 80-column line wrapping and standard GFM formatting across Markdown files.uv run mdformat --wrap 80 . - Spelling (
codespell): Checks for typos and misspelled identifiers.uv run codespell
- Static Analysis & Security Scanning (
semgrep): Enforces static analysis and security rules.uv run semgrep scan --config p/default
- Pre-commit Hooks: Enforces standards prior to commits. Must be installed
when setting up a workspace:
uv run pre-commit install --hook-type pre-commit --hook-type commit-msg uv run pre-commit run --all-files
All testing is orchestrated via pytest, pytest-cov, and pytest-benchmark.
- Running Tests:
uv run pytest
- Continuous Fuzz Testing (
hypofuzz): To continuously run adaptive, coverage-guided property-based fuzzing on our hypothesis test suite (tests/test_properties.py):uv run hypothesis fuzz tests/test_properties.py
- Cross-Platform CI: Automated matrix testing in GitHub Actions executes
across Linux (
ubuntu-latest), macOS (macos-latest), and Windows (windows-latest) for Python 3.14, while Python 3.13 testing is scoped to Linux. Benchmarks and type checks are also scoped to Linux. - Best Pytest Form:
- CRITICAL RULE: Write all new and refactored tests in the best modern
pytestform using standard Pythonassertstatements (e.g.,assert bv.size == 8). - Do NOT use legacy
unitteststyle assertions (self.assertEqual,self.assertTrue,self.assertRaises, etc.) or inherit fromunittest.TestCase. - Use
pytest.raises(...)for expected exceptions. - Use
@pytest.mark.parametrizeto cleanly test multiple input combinations without repetitive boilerplate. - Use standard pytest fixtures (like
tmp_pathfor temporary files) instead of manual cleanup ortempfile.
- CRITICAL RULE: Write all new and refactored tests in the best modern
- Coverage & Performance: Maintain 100% test coverage for new features and
bug fixes (enforced with a 95% minimum threshold via
--fail-under=95). Ensure benchmark suites (test_benchmarks.py) remain functional and non-regressing. Continuous integration automatically formats and outputs read-only Markdown coverage reports to GitHub Actions job summaries.
- Docstrings:
- CRITICAL RULE: All module, class, method, and function docstrings must strictly follow Standard Google Python Docstring Style.
- Include clearly formatted
Args:,Returns:,Raises:,Yields:, andAttributes:sections as applicable. - Avoid unstructured, verbose, or legacy docstring formatting.
- String Formatting:
- Always use modern Python f-strings (
f"Value: {val}") for string concatenation and formatting. Never use legacy%formatting or.format().
- Always use modern Python f-strings (
- Type Annotations:
- Provide precise, tight type annotations for all function signatures and return types.
- Avoid generic
Anytypes; prefer specific types such asSequence[int],Buffer,Self, orLiteral. - Avoid explicit
Union/Optionaltypes. Use '|'.
- Feature Branches:
- CRITICAL RULE: All code changes and refactoring work MUST be performed
on dedicated git feature branches (e.g.,
git checkout -b <branch-name>). - Never make direct commits on the
mainbranch.
- CRITICAL RULE: All code changes and refactoring work MUST be performed
on dedicated git feature branches (e.g.,
- Code Review:
- Always do a code review before committing. In addition to finding and suggesting fixes to issues, try to create 1-3 suggestions for improvement to the code based on the current changes.
- See if there needs to be any changes to
AGENTS.mdbased on the current changes and propose improvements.
- Conventional Commits:
- All git commit messages MUST adhere to the Conventional Commits
specification (
<type>(<optional scope>): <subject>). - Examples:
feat(dunder): enable modern __add__ and __iadd__ supportrefactor(tests): switch test_init.py from unittest to pytestchore(license): replace __copyright__ variable with SPDX headerdocs: import legacy manuals into docs/ directory
- All git commit messages MUST adhere to the Conventional Commits
specification (
- NO Tag or Conversation ID Entries:
- CRITICAL RULE: Commit messages must NEVER contain
TAG=orCONV=lines or entries. These are reserved for internal Piper/CL tools and must be omitted from all git commits in this repository.
- CRITICAL RULE: Commit messages must NEVER contain
When picking up work or assisting with refactoring, align with the active project goals tracked in our GitHub Issues:
- Issue #16 (Release Automation): Set up CI/CD workflows (GitHub Actions) for automating package publishing and release management.
- Issue #13 (Documentation Migration): Create a dedicated
docs/directory and import/format prior documentation and user manuals. - Issue #11 (f-strings): Convert remaining legacy string formatting across
BitVector.pyto modern f-strings. - Issue #10 (Modern Random Number Generator): Replace default
import randomusage with cryptographically secure or modern RNG practices (secrets/ modern generators). - Issue #8 (Tighten Type Annotations): Replace ambiguous
Anyannotations with strict, precise type definitions. - Issue #7 (Pytest Migration): Systematically convert remaining
unittest.TestCasetest suites intests/to idiomaticpytestfunctions and assertions.