refactor: enums to use CaseInsensitiveMixin for has_value
Proposed changes
- Added
CaseInsensitiveMixinto improve code reuse for case-insensitive value checking across Enum classes. - Applied the mixin to the Enuns
StixCyberObservableTypes,IdentityTypes,ThreatActorTypes,LocationTypes,ContainerTypes,StixMetaTypes, andMultipleRefRelationshipto remove duplicatedhas_valuefunction logic. - Updated the logic in
has_valueto use a set (O(1) lookup) instead of a list (O(N) lookup) and added caching forlower_valuesto 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
- [x] I consider the submitted work as finished
- [x] I tested the code for its functionality
- [ ] I wrote test cases for the relevant uses case
- [ ] I added/update the relevant documentation (either on github or on notion)
- [x] Where necessary I refactored code to improve the overall quality
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...
Thanks @allrob23 for your contribution, we're going to look at it !
There is a conflit to resolve here. Thanks in advance :)
Thank you for your contribution, but we need you to sign your commits. Please see https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits
Everything looks good :) However, we need the signature of the commit please.
Hello, this repository has been moved to client-python folder into https://github.com/OpenCTI-Platform/opencti.
All PR here in this repository will be closed, if you still need it, please reopen on https://github.com/OpenCTI-Platform/opencti