LeastAuthority/kubetop

filter() is being used in a way in the partition() function which does not work on Python 3

rodrigc opened this issue · 4 comments

When trying to run some of the tests in Python 3, I ran into this:

[ERROR]
Traceback (most recent call last):
  File "/Users/crodrigues/kubetop/src/kubetop/test/test_textrenderer.py", line 226, in test_render_several
    in _render_pods(pods, pod_usage, nodes).splitlines()
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 295, in _render_pods
    in sorted(pod_usage, key=_pod_stats, reverse=True)
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 312, in _pod_stats
    ), 0,
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 378, in parse_cpu
    return parse_k8s_resource(s, default_scale=1000)
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 391, in parse_k8s_resource
    return int(amount) * scale
builtins.TypeError: int() argument must be a string, a bytes-like object or a number, not 'filter'

@exarkun I tried to change the code to list(filter()) but then got this error:

Traceback (most recent call last):
  File "/Users/crodrigues/kubetop/src/kubetop/test/test_textrenderer.py", line 226, in test_render_several
    in _render_pods(pods, pod_usage, nodes).splitlines()
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 296, in _render_pods
    in sorted(pod_usage, key=_pod_stats, reverse=True)
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 313, in _pod_stats
    ), 0,
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 379, in parse_cpu
    return parse_k8s_resource(s, default_scale=1000)
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 389, in parse_k8s_resource
    scale = suffix_scale(suffix)
  File "/Users/crodrigues/kubetop/src/kubetop/_textrenderer.py", line 410, in suffix_scale
    }[suffix]
builtins.TypeError: unhashable type: 'list'

Maybe this could just be:

u"".join(x for x in seq if pred(x))

This seems to work on both Python 2 and Python 3.

(Note to anyone not sure: filter tries to return the same type as the sequence passed to it; list comprehension and generation expression don't have this same feature so the u"".join is needed to get the data back to a unicode string)

@exarkun OK, I updated the patch as per your example. Please review.