Skip to content
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

RFE: subscriptable properties #107

Open
jonathanunderwood opened this issue May 31, 2022 · 2 comments
Open

RFE: subscriptable properties #107

jonathanunderwood opened this issue May 31, 2022 · 2 comments
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed

Comments

@jonathanunderwood
Copy link

Is your feature request related to a problem? Please describe.
When I am using this library I find myself doing a lot of this sort of thing:

techniques = {t.id: t for t in attack.enterprise.techniques}
...
t1 = techniques["T12345"]

or

tt = [t for t in attack.enterprise.techniques if t.id == "T12345"]

etc. And similar for other lists of objects. I am aware of the ability to search, but that's quite heavy weight when I mainly just want to be able to subscript based on ATT&CK ID.

Describe the solution you'd like
It would be really helpful if the collection properties were subscriptable as well as iterable.

Describe alternatives you've considered
This is technically fairly simple to implement if we just wanted to return a dict instead of a list, breaking the old API. Personally, I'd be fine with that, but others probably have a different view.

So, the question is, does the existing iterable nature of these properties need to be preserved? And if so, how comfortable are you with breaking the existing API?

Options I can think of:

  1. Update the properties to return a dict (or IndexedOrderedDict from the indexed package) instead of a list - this changes the existing API.
  2. Add a subscriptable=False keyword argument to the Attck() constructor, which when True returns properties as dicts rather than lists. This maintains the existing API, but isn't particularly pythonic, as functions return types depending on arguments of a constructor is a undiscoverable pattern, and also makes reasoning about types difficult (for future use of type hinting)
  3. Add new properties that return dicts.

There may be others.

Add any other context or screenshots about the feature request here.
Note that since Python 3.7 insertion into dicts maintains order. Also note that the IndexedOrderedDict from the indexed package enables really fast index based lookups.

@jonathanunderwood jonathanunderwood changed the title RFE: subscriptable objects RFE: subscriptable properties May 31, 2022
@MSAdministrator MSAdministrator added enhancement New feature or request help wanted Extra attention is needed labels Jun 6, 2022
@jonathanunderwood
Copy link
Author

@MSAdministrator am happy to provide an implementation of this, but want to avoid taking a direction that wouldn't be acceptable. Here's my current thinking:

  • Create a decorator to add to all the properties that would take the result of the property method and creates a dict with keys being the object id fields.
  • This decorator function would only do this if the Attck object had been instantiated with subscriptable=True. The default would be subscriptable=False, in which case the decorator function would simply return the list of objects, preserving current behaviour.

WDYT of this implementation direction?

@MSAdministrator
Copy link
Collaborator

@jonathanunderwood Yeah that would be awesome and I like this approach as well. I would wait until #111 is merged though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants