-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add add_foo() methods for EntityList foo properties #117
Comments
I generally dislike this approach (which is why I never add them to classes 😉). It feels unpythonic to me, and more like Java or the generateDS bindings. The goal of I suppose for the sake of consistency it would be good to add them (though I would prefer removing all the others 😀). You could also use some magic Python and put this (WARNING: pseudocode) on the stix.Entity class to prevent the boilerplate code: def __getattr__(self, name):
if not name.startswith("add_"):
return object.__getattr__(self, name)
# get everything after the first _
real_attr = name.split("_", 1)[1]
# return the append() function of whatever type is trying to be added,
# which then gets called with whatever argument the caller tried to pass.
return getattr(self, real_attr).append I haven't actually tried running that function, but something like that is better (IMO) than 73 copies of almost-identical functions scattered throughout the code. |
Hmm, this might work, though it would require ripping out all the old ...
if hasattr(self, name):
return object.__getattr__(self, name)
... |
It might just work as is, meaning we can leave the existing functions as is for now (that is, until I convince you to remove them 😉)
https://docs.python.org/2/reference/datamodel.html#object.__getattr__ Again, I haven't actually tested any of this. |
Oh, nice! I was wondering if that was the behavior and considered looking up the docs...but didn't ;) |
The introduction of
EntityList
greatly helped reduce the amount of code needed to write for python-stix classes, but also established a new pattern for working with list types. We should go back through the codebase and add theadd_foo()
methods where missing. This is related to #110.The text was updated successfully, but these errors were encountered: