Refactor BlockLengthSampler
for Enhanced Type Safety, Validation, and Extensibility
#175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request addresses and resolves multiple issues within the
BlockLengthSampler
class to enhance its reliability, maintainability, and usability. The key changes include:Enhanced Type Safety and Validation:
BaseModel
for automatic data validation.block_length_distribution
to accept bothstr
andDistributionTypes
enums, ensuring type consistency through validators.Centralized Distribution Management:
DistributionTypes
enum to strictly define supported distribution types.DistributionRegistry
class to manage and retrieve distribution sampling functions, promoting scalability and modularity.Replacement of Lambdas with Named Functions:
Comprehensive Logging and Error Handling:
logging
module to provide detailed logs for debugging and monitoring.warnings.warn
to notify users about non-critical issues, such as adjustingavg_block_length
.Simplified Class Structure:
BlockLengthSampler
class for clarity and simplicity.Improved Extensibility:
DistributionRegistry
without altering the core sampler class.Resolved Type Errors:
RngTypes
within the same module usingTypeAlias
to ensure recognition by type checkers like Pylance.block_length_distribution
to eliminate type mismatch errors.These changes collectively improve the
BlockLengthSampler
class's robustness, type safety, and developer experience.Type of change
How Has This Been Tested?
Type Checking:
RngTypes
as unknown.str
andDistributionTypes
toblock_length_distribution
no longer causes type mismatch errors.Functionality Testing:
sample_block_length
works correctly with various distributions and returns valid block lengths.Logging Verification:
Checklist:
Additional Information (if applicable)
No additional information.
Add All Contributors Command
Remember to acknowledge your contributions, replace
contribution_type
with your contribution (code, doc, etc.):