-
Couldn't load subscription status.
- Fork 51
Fix some copying issues. #15
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,10 +113,26 @@ def __init__(self, left, right=None, op=None): | |
| Do not use this directly, use `cycler` function instead. | ||
| """ | ||
| self._keys = _process_keys(left, right) | ||
| self._left = copy.deepcopy(left) | ||
| self._right = copy.deepcopy(right) | ||
| if isinstance(left, Cycler): | ||
| self._left = left._shallow_copy() | ||
| else: | ||
| self._left = copy.copy(left) | ||
|
|
||
| if isinstance(right, Cycler): | ||
| self._right = right._shallow_copy() | ||
| else: | ||
| self._right = copy.copy(right) | ||
|
|
||
| self._op = op | ||
|
|
||
| def _shallow_copy(self): | ||
| ret = Cycler(None) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can do this block as ps, sorry for the backwards lisp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we would need to expand the constructor to allow lists of dictionaries and update _process_keys() to recognize that. |
||
| ret._keys = self.keys | ||
| ret._left = copy.copy(self._left) | ||
| ret._right = copy.copy(self._right) | ||
| ret._op = copy.copy(self._op) | ||
| return ret | ||
|
|
||
| @property | ||
| def keys(self): | ||
| """ | ||
|
|
@@ -228,11 +244,14 @@ def __iadd__(self, other): | |
| other : Cycler | ||
| The second Cycler | ||
| """ | ||
| old_self = copy.deepcopy(self) | ||
| if not isinstance(other, Cycler): | ||
| raise TypeError("Cannot += with a non-Cycler object") | ||
| # True shallow copy of self is fine since this is in-place | ||
| old_self = copy.copy(self) | ||
| self._keys = _process_keys(old_self, other) | ||
| self._left = old_self | ||
| self._op = zip | ||
| self._right = copy.deepcopy(other) | ||
| self._right = other._shallow_copy() | ||
| return self | ||
|
|
||
| def __imul__(self, other): | ||
|
|
@@ -244,12 +263,14 @@ def __imul__(self, other): | |
| other : Cycler | ||
| The second Cycler | ||
| """ | ||
|
|
||
| old_self = copy.deepcopy(self) | ||
| if not isinstance(other, Cycler): | ||
| raise TypeError("Cannot *= with a non-Cycler object") | ||
| # True shallow copy of self is fine since this is in-place | ||
| old_self = copy.copy(self) | ||
| self._keys = _process_keys(old_self, other) | ||
| self._left = old_self | ||
| self._op = product | ||
| self._right = copy.deepcopy(other) | ||
| self._right = other._shallow_copy() | ||
| return self | ||
|
|
||
| def __eq__(self, other): | ||
|
|
@@ -354,7 +375,7 @@ def cycler(*args, **kwargs): | |
| Parameters | ||
| ---------- | ||
| arg : Cycler | ||
| Copy constructor for Cycler. | ||
| Copy constructor for Cycler (does a shallow copy of iterables). | ||
|
|
||
| label : name | ||
| The property key. In the 2-arg form of the function, | ||
|
|
@@ -363,6 +384,8 @@ def cycler(*args, **kwargs): | |
|
|
||
| itr : iterable | ||
| Finite length iterable of the property values. | ||
| Can be a single-property `Cycler` that would | ||
| be like a key change, but as a shallow copy. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -378,7 +401,7 @@ def cycler(*args, **kwargs): | |
| if not isinstance(args[0], Cycler): | ||
| raise TypeError("If only one positional argument given, it must " | ||
| " be a Cycler instance.") | ||
| return copy.deepcopy(args[0]) | ||
| return Cycler(args[0]) | ||
| elif len(args) == 2: | ||
| return _cycler(*args) | ||
| elif len(args) > 2: | ||
|
|
@@ -415,10 +438,9 @@ def _cycler(label, itr): | |
| msg = "Can not create Cycler from a multi-property Cycler" | ||
| raise ValueError(msg) | ||
|
|
||
| if label in keys: | ||
| return copy.deepcopy(itr) | ||
| else: | ||
| lab = keys.pop() | ||
| itr = list(v[lab] for v in itr) | ||
| lab = keys.pop() | ||
| # Doesn't need to be a new list because | ||
| # _from_iter() will be creating that new list anyway. | ||
| itr = (v[lab] for v in itr) | ||
|
|
||
| return Cycler._from_iter(label, itr) | ||
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.
This isn't deep enough to protect against changing the key, but this depends on how #13 ends up being implemented
I think this needs to be