Skip to content
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

Multiple coverage items for a single file #406

Closed
larryzju opened this issue Apr 30, 2020 · 0 comments
Closed

Multiple coverage items for a single file #406

larryzju opened this issue Apr 30, 2020 · 0 comments

Comments

@larryzju
Copy link
Contributor

larryzju commented Apr 30, 2020

Describe the bug
The coverage page shows multiple lines of metrics for a single file

Screenshots
image

Reason
It seems the remove inside the inner for loop breaks the self.files list

tmp_files = []
while self.files != []:
f1 = self.files.pop(0)
for f in self.files:
if f.name == f1.name:
f1 = f1 + f
self.files.remove(f)
tmp_files.append(f1)

A more functional way of doing the same thing is

import itertools, functools
tmp_files = []
get_filename = lambda f: f.name
for _, group in itertools.groupby(sorted(self.files, key=get_filename), key=get_filename):
    tmp_files.append(functools.reduce(lambda a,b : a+b, group))

To Reproduce

class Account(object):
    def __init__(self, name, count):
        self.name = name
        self.count = count

    def __str__(self):
        return "Account[%s] = {%s}" % (self.name, self.count)

    def __add__(self, other):
        if type(other) is not Account:
            raise TypeError()

        if other.name != self.name:
            raise valueError()

        return Account(self.name, self.count + other.count)


accounts = [
        Account('a1', 1),
        Account('a1', 2),
        Account('a1', 3),
        Account('a1', 4),
        Account('a1', 5),
]

temps = []
while accounts != []:
    f1 = accounts.pop(0)

    for f in accounts:
        if f.name == f1.name:
            f1 = f1 + f
            accounts.remove(f)

    temps.append(f1)


for s in temps:
    print(s)

Which outputs

Account[a1] = {7}
Account[a1] = {8}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant