refactor: enums to use CaseInsensitiveMixin for has_value #873
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.
Proposed changes
CaseInsensitiveMixin
to improve code reuse for case-insensitive value checking across Enum classes.StixCyberObservableTypes
,IdentityTypes
,ThreatActorTypes
,LocationTypes
,ContainerTypes
,StixMetaTypes
, andMultipleRefRelationship
to remove duplicatedhas_value
function logic.has_value
to use a set (O(1) lookup) instead of a list (O(N) lookup) and added caching forlower_values
to avoid recreating it on every call.This change follows the DRY principle by centralizing case-insensitive checks in a single mixin, making the code cleaner and easier to maintain. It's not a major performance optimization, but it's a nice improvement. I’ve included a benchmark below to show the results.
Benchmark code and results:
code: https://gist.github.com/allrob23/03382afaffec1a31472ed6fa90fdeb52
Result
Original: 0.004698 seconds
Refactored: 0.000227 seconds
Checklist
Further comments
If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...