Skip to content

refactor: enums to use CaseInsensitiveMixin for has_value #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

allrob23
Copy link

@allrob23 allrob23 commented Apr 11, 2025

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

  • I consider the submitted work as finished
  • 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)
  • 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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant