-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Lacking a dirty way to count elements #77
Comments
Why not use |
Yes, of course. That's not a dirty approach though and then what about the left hand side? What if it wraps some sort of "EqualCounter"? So it counts items using equality instead of identity. Could that help align with other dirty types? |
assert Counter([3, 2, 1, 3, 2, 1]) == {1: 2, 2: 2, 3: 2} Assuming the left side was something more complicated and you wanted to only do transformations on the right, maybe combined with other dirty operators: assert [3, 2, 1, 3, 2, 1] == IsListOrTuple(*Counter({1: 2, 2: 2, 3: 2}).elements(), check_order=False) That way you don't have to "explicitly multiply each element".
|
What if I have something non-hashable on the left hand side that I want to count? assert [{"a": 1}, {"b": 2}, {"a": 1}, {"c": 3}, {"a": 1}] == IsListOrTuple(???) If I have my assert [{"a": 1}, {"b": 2}, {"a": 1}, {"c": 3}, {"a": 1}] == EqualCounter((IsDict(a=1), 2), (IsDict(b=2), 1), (IsDict(c=3), 1)) As the "EqualCounter" in this case would compare with each expected item with |
It might be easiest to add your own custom type? |
Yeah, sure, I'll try that out |
I wonder if there's a dirty equals way to assert on element count?
I suppose it's possible to use
IsListOrTuple
withcheck_order=False
. But then it's the precondition of having a list/tuple and explicitly multiply each element.e.g.
Another one is
Contains
, but as far as I can see it can't be upper bounded on "how many of something"?Could this be some extension of
Contains
? e.g. under a parametercounts={<elem>: <count>}
.I suppose this might also imply some sort of
partial={True|False}
argument forContains
, for convenience?Additionally,
<element>
ofcounts
in a case like above could probably be useful as dirty equals types? e.g.The text was updated successfully, but these errors were encountered: