-
Notifications
You must be signed in to change notification settings - Fork 179
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
Convert depset into a list before iterating #67
Changes from all commits
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 |
---|---|---|
|
@@ -43,6 +43,34 @@ def _precondition_only_sets_or_lists(*args): | |
fail("Expected arguments to be depset or list, but found type %s: %r" % | ||
(t, a)) | ||
|
||
def _to_set(a): | ||
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. cc @c-parsons 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.
Is there a migration plan for current users? I don't see a deprecation warning in the file. For example:
I have tried to use an 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. The first step will be done with #70 |
||
"""Converts `a` into a set. | ||
|
||
Args: | ||
a: A depset or a list. | ||
|
||
Returns: | ||
A dict of keys created from elements of `a`. | ||
""" | ||
t = type(a) | ||
if t == "depset": | ||
a = a.to_list() | ||
return {e: None for e in a} | ||
|
||
def _to_list(a): | ||
"""Converts `a` into a list. | ||
|
||
Args: | ||
a: A depset, list, or an output of `_to_set()` | ||
|
||
Returns: | ||
A list of unique elements of `a`. | ||
""" | ||
t = type(a) | ||
if t in ("depset", "list"): | ||
a = _to_set(a) | ||
return a.keys() | ||
|
||
def _is_equal(a, b): | ||
"""Returns whether two sets are equal. | ||
|
||
|
@@ -54,7 +82,7 @@ def _is_equal(a, b): | |
True if `a` is equal to `b`, False otherwise. | ||
""" | ||
_precondition_only_sets_or_lists(a, b) | ||
return sorted(depset(a)) == sorted(depset(b)) | ||
return sorted(_to_list(a)) == sorted(_to_list(b)) | ||
|
||
def _is_subset(a, b): | ||
"""Returns whether `a` is a subset of `b`. | ||
|
@@ -67,6 +95,7 @@ def _is_subset(a, b): | |
True if `a` is a subset of `b`, False otherwise. | ||
""" | ||
_precondition_only_sets_or_lists(a, b) | ||
a, b = _to_set(a), _to_set(b) | ||
for e in a: | ||
if e not in b: | ||
return False | ||
|
@@ -85,6 +114,7 @@ def _disjoint(a, b): | |
True if `a` and `b` are disjoint, False otherwise. | ||
""" | ||
_precondition_only_sets_or_lists(a, b) | ||
a, b = _to_set(a), _to_set(b) | ||
for e in a: | ||
if e in b: | ||
return False | ||
|
@@ -101,6 +131,7 @@ def _intersection(a, b): | |
A set containing the elements that are in both `a` and `b`. | ||
""" | ||
_precondition_only_sets_or_lists(a, b) | ||
a, b = _to_set(a), _to_set(b) | ||
return depset([e for e in a if e in b]) | ||
|
||
def _union(*args): | ||
|
@@ -127,6 +158,7 @@ def _difference(a, b): | |
A set containing the elements that are in `a` but not in `b`. | ||
""" | ||
_precondition_only_sets_or_lists(a, b) | ||
a, b = _to_set(a), _to_set(b) | ||
return depset([e for e in a if e not in b]) | ||
|
||
sets = struct( | ||
|
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 moved this change to #69