-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Recursive assignment of fields in settings.py to the app.settings object #78
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution!
I have some concerns I've mentioned in comments in the PR, as I think we can achieve the same effect with a lot less code; by instead re-using the Settings
class and leveraging a class method or alike, which could return an instance of self, since it already supports dot notation. This would cause all dicts and sub-dicts being assigned to the Settings
object to be cast to an instance of self, with all the benefit it brings.
tests/core/test_settings.py
Outdated
from . import mocksettings | ||
|
||
class PyttmanInternalSettingsPyttmanApp(PyttmanInternalBaseTestCase): | ||
def __init__(self, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use setUp
hook in tests instead of overriding __init__
. The setUp
hook is left unimplemented for these suites to implement them for this purpose.
tests/core/mocksettings.py
Outdated
d = { | ||
"k1": "v1", | ||
"k2":{"a":"a","b":"b"} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file is redundant, and the relative import can be troublesome in a shipped package due to its relativity. Can the mockup dict object be placed in the test suite instead, such as in setUp
, or as a property in the test suite?
pyttman/core/internals.py
Outdated
setattr(self, k, tmp) | ||
|
||
def __dict2obj(self, dictionary): | ||
return json.loads(json.dumps(dictionary), object_hook=CustomUserDict) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some thoughts about this method.
-
Is there a specific reason as to why
__
is used? I don't think it should be named with two leading underscores, since name mangling here is unlikely to be utilized.
I would prefer a single_
in the method and attribute name. -
Is dumping the entire dictionary to a string and then reloading it again in to the custom dict class
CustomUserDict
really necessary? Can it not be done with a cast? -
I can't see
self
being used here. I think this should be a class method in the CustomUserDict instead, returning an instance of itself. -
Method names should not contain digits
pyttman/core/internals.py
Outdated
# constructor | ||
def __init__(self, dictionary): | ||
self.data = dictionary | ||
self.__dict__.update(dictionary) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this class is redundant, since we already have the Settings
class which could implement this API instead.. I'd prefer if the Settings
class would handle a recursive assignment of self for all sub-dictionaries, instead of the first level being a Settings
object and all subnodes being of a different type, for no apparent reason.
pyttman/core/internals.py
Outdated
if not inspect.ismodule(v) | ||
and not inspect.isfunction(v)] | ||
|
||
def __set_attr(self, k, v): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to PEP-8, single _
hint to a private method - unless mangling is required, but it' highly unlikely to be needed here.
Updated PR, I managed to fix most of the requested changes except item#2 on: #78 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better, except for the memory leak in Settings.__init__
and the _ _
naming in __set_attr
, a single underscore is sufficient (name mangling is not required)
pyttman/core/internals.py
Outdated
@@ -48,7 +50,8 @@ class Settings: | |||
aren't valid settings. | |||
""" | |||
|
|||
def __init__(self, **kwargs): | |||
def __init__(self, dictionary={}, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that in Python, default keyword arguments are parsed once, not for each call.
The argument default dictionary={}
statement, is creating one dictionary used over and over again for every call. The same is true for anything non-primitive. It could cause serious implications where multiple instances of this class will share the same dictionary
memory reference.
An example
If you create multiple instances of a class with this syntax, this is the result of the leak:
class A:
def __init__(self, dictionary={})
self.dictionary = dictionary
first = A()
second = A()
first.dictionary["foo"] = "bar"
# leak in plain sight
print(second.dictionary["foo"]
>>> "bar"
# cross-mutation
second.dictionary["foo"] = None
print(first.dictionary)
>>> None
print(first.dictionary is second.dictionary)
>>> True # Not what we want :)
For an example, see this stack overflow post
To fix this, use the default argument None
as:
def foo(data=None):
if data is None:
data = {} # Created in the function scope, fixes the issue
New feature to support Dot notation when working with
settings.py
.Dot notation can be used to fetch keys/values from dictionaries specifed in
settings.py
, see code snippets below.Closes #76