client-python icon indicating copy to clipboard operation
client-python copied to clipboard

refactor: enums to use CaseInsensitiveMixin for has_value

Open allrob23 opened this issue 8 months ago • 4 comments

Proposed changes

  • Added CaseInsensitiveMixin to improve code reuse for case-insensitive value checking across Enum classes.
  • Applied the mixin to the Enuns StixCyberObservableTypes, IdentityTypes, ThreatActorTypes, LocationTypes, ContainerTypes, StixMetaTypes, and MultipleRefRelationship to remove duplicated has_value function logic.
  • Updated the logic in has_value to use a set (O(1) lookup) instead of a list (O(N) lookup) and added caching for lower_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

  • [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...

allrob23 avatar Apr 11 '25 18:04 allrob23

Thanks @allrob23 for your contribution, we're going to look at it !

nino-filigran avatar Apr 25 '25 08:04 nino-filigran

There is a conflit to resolve here. Thanks in advance :)

CelineSebe avatar Sep 26 '25 09:09 CelineSebe

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

github-actions[bot] avatar Sep 26 '25 15:09 github-actions[bot]

Everything looks good :) However, we need the signature of the commit please.

CelineSebe avatar Oct 09 '25 12:10 CelineSebe

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

aHenryJard avatar Nov 21 '25 08:11 aHenryJard