Regions¶
Hierarchical code region model (file → class → function → lines).
Hierarchical code region model for curate-ipsum.
Regions identify code locations at various granularities: - File level: file:src/main.py - Class level: file:src/main.py::class:Calculator - Function level: file:src/main.py::func:compute - Line range: file:src/main.py::lines:10-25
Regions support containment queries (does region A contain region B?) and overlap detection for aggregating metrics across related code.
- class curate_ipsum.regions.Region(file_path, class_name=None, func_name=None, line_start=None, line_end=None)[source]¶
Bases:
objectHierarchical code region identifier.
Immutable, hashable, and serializable to/from string. Supports containment queries (is region A within region B?).
- Format:
file:<path> # Whole file file:<path>::class:<name> # Class level file:<path>::func:<name> # Function level file:<path>::class:<name>::func:<name> # Method within class file:<path>::lines:<start>-<end> # Line range
Examples
>>> Region.for_file("src/main.py") Region('file:src/main.py')
>>> Region.for_function("src/main.py", "compute", class_name="Calculator") Region('file:src/main.py::class:Calculator::func:compute')
>>> r = Region.from_string("file:src/main.py::lines:10-20") >>> r.line_start 10
- Parameters:
- property level: RegionLevel¶
Most specific level of this region.
- contains(other)[source]¶
Check if this region contains another region.
A region contains another if: - Same file AND - This region’s scope encompasses the other’s scope
Examples
file:a.py contains file:a.py::func:foo # True file:a.py::class:C contains file:a.py::class:C::func:m # True file:a.py::func:foo contains file:a.py # False
- overlaps(other)[source]¶
Check if this region overlaps with another.
Two regions overlap if they share any code locations.
- to_string()[source]¶
Serialize to canonical string format.
The format is designed to be: - Human readable - Lexicographically sortable by file - Parseable back to Region
- Return type:
- classmethod from_string(s)[source]¶
Parse from canonical string format.
Also handles legacy plain file paths for backward compatibility.
- Parameters:
s (str) – Region string like “file:src/main.py::func:compute” or legacy plain path like “src/main.py”
- Returns:
Parsed Region instance
- Raises:
ValueError – If string is empty or malformed
- Return type:
- class curate_ipsum.regions.RegionLevel(*values)[source]¶
Bases:
StrEnumGranularity level of a region.
- FILE = 'file'¶
- CLASS = 'class'¶
- FUNCTION = 'func'¶
- LINES = 'lines'¶
- curate_ipsum.regions.normalize_region_id(region_id)[source]¶
Convert legacy regionId string to Region, or return None.
Provides backward compatibility with existing regionId: str fields.
- curate_ipsum.regions.region_to_id(region)[source]¶
Convert Region back to string for storage.
Inverse of normalize_region_id.
Hierarchical code region model.
Inspired by pytest’s node ID format (well-established, familiar to Python developers). Supports serialization to/from strings for storage compatibility.
- class curate_ipsum.regions.models.RegionLevel(*values)[source]¶
Bases:
StrEnumGranularity level of a region.
- FILE = 'file'¶
- CLASS = 'class'¶
- FUNCTION = 'func'¶
- LINES = 'lines'¶
- class curate_ipsum.regions.models.Region(file_path, class_name=None, func_name=None, line_start=None, line_end=None)[source]¶
Bases:
objectHierarchical code region identifier.
Immutable, hashable, and serializable to/from string. Supports containment queries (is region A within region B?).
- Format:
file:<path> # Whole file file:<path>::class:<name> # Class level file:<path>::func:<name> # Function level file:<path>::class:<name>::func:<name> # Method within class file:<path>::lines:<start>-<end> # Line range
Examples
>>> Region.for_file("src/main.py") Region('file:src/main.py')
>>> Region.for_function("src/main.py", "compute", class_name="Calculator") Region('file:src/main.py::class:Calculator::func:compute')
>>> r = Region.from_string("file:src/main.py::lines:10-20") >>> r.line_start 10
- Parameters:
- property level: RegionLevel¶
Most specific level of this region.
- contains(other)[source]¶
Check if this region contains another region.
A region contains another if: - Same file AND - This region’s scope encompasses the other’s scope
Examples
file:a.py contains file:a.py::func:foo # True file:a.py::class:C contains file:a.py::class:C::func:m # True file:a.py::func:foo contains file:a.py # False
- overlaps(other)[source]¶
Check if this region overlaps with another.
Two regions overlap if they share any code locations.
- to_string()[source]¶
Serialize to canonical string format.
The format is designed to be: - Human readable - Lexicographically sortable by file - Parseable back to Region
- Return type:
- classmethod from_string(s)[source]¶
Parse from canonical string format.
Also handles legacy plain file paths for backward compatibility.
- Parameters:
s (str) – Region string like “file:src/main.py::func:compute” or legacy plain path like “src/main.py”
- Returns:
Parsed Region instance
- Raises:
ValueError – If string is empty or malformed
- Return type: