One file = one module = one purpose: Each file must be explicitly importable as a module and serve exactly one main purpose.
Portability to C++: Code must be directly portable to C++ (avoid dynamic, Python-specific idioms).
Do not default to generic Python ecosystem “best practices” when they conflict with the user’s stated project shape or these repository rules.
Prefer the simplest implementation that directly matches the user’s wording over reusable abstraction layers added speculatively.
Python Version Policy
All code must run and be tested on both Python 2 and Python 3.
Version-conditional logic: Only use sys.version_info for version checks. Do not use except ImportError, sys.version, or similar.
String Types
Encoding/decoding UTF-8, URI, stdin/out, file names etc. can use our textcompat package.
Only use the legacy % formatting syntax. No .format() or f-strings.
Path Handling
The canonical representation of filesystem paths in code should be an absolute list of path components derived by applying verbs compiled with fspathverbs to the tokenized current working directory.
Treat that path component list as the authoritative path value.
Example on POSIX: [u'/', u'home', u'alice', u'project', u'file.txt'] represents /home/alice/project/file.txt.
Do not do ad hoc string manipulation for path parsing, normalization, basename extraction, dirname extraction, separator stripping, or parent traversal.
Parse paths into verbs, apply the verbs, and operate on the resulting path component list.
Do not repeatedly convert back and forth between filesystem path strings and filesystem component lists within the same code path. Convert to the canonical component-list representation once, do the path logic there, and convert back to a filesystem string only at the boundary where an OS API or external interface requires it.
Do not catch exceptions unless there is a clear, documented reason to transform them, add essential context, or implement a specific recovery strategy.
Never silence exceptions or convert them into fallback behavior by default.
Platform Policy
All code must run and be tested on both NT and POSIX.
Determine platform with posix-or-nt (returns 'nt' or 'posix').
Never use the subprocess, or platform modules. Overly complicated and convoluted codebases, and large parts are not available or behave differently on NT, Android, and iOS, etc.
Use sys.platform if you absolutely need an OS name.
System API Access
The threading and multiprocessing modules are not allowed.
Direct system calls via ctypes are strictly limited as follows:
On NT:
Only functions in MSVCRT (Microsoft C Runtime) and the standard Win32 API may be called.
On POSIX:
Only standardized POSIX functions (from libc or standard headers) may be called.
Should be factored as a standalone, well-tested PyPI package similar to our other infra tools.
If code needs capabilities beyond these, use PyQt/PySide and the Qt API as your system abstraction layer.
Examples: advanced file I/O, process control, IPC, networking, device enumeration, drag-and-drop, clipboard, graphics, etc.
Use our detect-qt-binding to automatically detect which Qt binding is available in your environment.
File and Folder Structure
Import all files (modules) via absolute import. No relative import, no sys.path manipulation.
All files (modules) must only have public functions and classes - no private/internal APIs.
All directories must include an explicit __init__.py within them.
Preserve the requested application shape: if the user describes the project as a single application / single-file program, keep it as exactly one project .py file, one module, and one entrypoint unless the user explicitly asks for further splitting.
Do not proactively refactor a single-file application into a package with multiple local modules just for organization.
Utility Code: No “utils.py” - Publish Generalized Tools
No local “utils.py” files: Do not keep grab-bag or miscellaneous utility functions/classes in a project-private utils.py file.
General-purpose tools must be published as standalone packages to PyPI, each focusing on one well-defined function, class, or concept.
Move helpers that would otherwise go into “utils.py” into their own properly documented, versioned packages.
These utility packages should be:
Named after their actual purpose;
Well-tested and actively maintained;
Equipped with a README, proper docstrings, testable usage examples, semantic versioning, and a clear license.
All projects share utilities via explicit dependencies rather than duplicating or copying helpers.
When a utility is improved or a bug is fixed, updating the package ensures all dependent projects benefit automatically - “write once, run everywhere.”
This approach avoids hidden technical debt and promotes code quality, documentation, reuse, and maintainability across your entire codebase.
It also contributes to the wider Python ecosystem.
Regular Expressions
Regular expressions only for simple parsing.
Use Unix-style/basic regex features:
.: any single character
[ ]: character set/class
[^ ]: negated class
^, $: line start/end
( ): grouping/subexpression
*: zero or more
For complicated input:
Use a context-free grammar parser (e.g. Lark), hand-written parser, or an LLM.
Argument Parsing
Use argparse.
Use ambiguous str for all argument values (default API behavior).
All arguments should have a help=... string.
By default, parse arguments directly where the program starts instead of adding speculative wrappers such as run(argv=None) or custom argument namespace classes, unless the user explicitly asks for such an abstraction.
Flag Arguments
Presence only:action='store_true' -> bool
Counted:action='count' -> int
Single value:type=str, required=True/False, explicit default=...
0/1 (optional):nargs='?', must be last positional argument
0 or more/1 or more:nargs='*'/nargs='+', with plural argument name and singular metavar, must be last positional argument
Testing & Documentation
Tests must:
Run successfully on both Python 2 and 3, POSIX and NT.
Be suitable for inclusion in README.md under “Usage” or “Quickstart”.
Simultaneously serve as usage documentation (idiomatic examples).
Self-contained and runnable as a script or documentation block.
Do not assume the word “test” means a dedicated unit-test file or a special self-test CLI flag unless the user explicitly asks for that. Prefer README-based smoke checks / walkthroughs that a user can quickly follow to verify behavior and understand capabilities.
Python Packaging & Distribution
File Layout
All files start with a copyright and license block:
Boilerplate: # Copyright (c) 2025 Jifeng Wu\n# Licensed under the <license> License. See LICENSE file in the project root for full license information.
simple infrastructure: MIT/BSD
complex infra: Apache-2.0
applications: AGPL-3.0
Required Files & Metadata
README.md with standard boilerplate (see below)
LICENSE
requirements.txt (see example)
pyproject.toml (see template)
setup.cfg (see template)
README.md Boilerplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<Project Description>
## Installation
...
## Usage
...
## Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
## License
This project is licensed under the [<license> License](LICENSE).
All projects are intended to be published to PyPI.
Therefore, installation instructions should treat the canonical install command as pip install <package-name>.
Example requirements.txt
1 2 3 4
enum34; python_version < '3.4' pyreadline six typing; python_version < '3.5'