MOD:add parameter 'filter' to some method and show lambda more friendly#33
MOD:add parameter 'filter' to some method and show lambda more friendly#33mgedmin merged 5 commits intomgedmin:masterfrom
Conversation
mgedmin
left a comment
There was a problem hiding this comment.
Thank you for your contribution! In order for it to be included, it needs just a few little touches:
- a couple of lines at the top of the CHANGES.rst file: one mentioning the new function argument and one mentioning the nicer lambda representation
- documentation for the new function argument in the docstrings of functions that take it
- two unit tests for the new code.
It would be ideal if you could update your PR to do at least some of the above. Please let me know if you are unable or unwilling, and I'll try to find the time to do them myself.
|
|
||
|
|
||
| def typestats(objects=None, shortnames=True): | ||
| def typestats(objects=None, shortnames=True, filter=None): |
There was a problem hiding this comment.
This new parameter needs documentation in the docstring, and a .. versionchanged: ... directive.
There was a problem hiding this comment.
Thanks very much, and i have do some fix with your advise, please check again
|
|
||
|
|
||
| def most_common_types(limit=10, objects=None, shortnames=True): | ||
| def most_common_types(limit=10, objects=None, shortnames=True, filter=None): |
There was a problem hiding this comment.
Likewise.
EDIT: I mean this needs a docstring update. No unit test necessary.
| shortnames=True, | ||
| file=None): | ||
| file=None, | ||
| filter=None): |
|
|
||
|
|
||
| def show_growth(limit=10, peak_stats={}, shortnames=True, file=None): | ||
| def show_growth(limit=10, peak_stats={}, shortnames=True, file=None, filter=None): |
| else: | ||
| return name | ||
| if _isinstance(obj, types.LambdaType): | ||
| return 'lambda: %s:%s' % (os.path.basename(obj.func_code.co_filename), obj.func_code.co_firstlineno) |
There was a problem hiding this comment.
This is nice.
What is missing is a unit test. Currently objgraph has 100% test coverage, and I want to maintain that.
There are a bunch of copy-able examples called test_short_repr_... in tests.py. I suggest using assertRegex so you don't have to hardcode the line number of the lambda.
| stats = {} | ||
| for o in objects: | ||
| if filter and not filter(o): | ||
| continue |
There was a problem hiding this comment.
This change needs a unit test. See TypestatsTest in tests.py. I suggest defining a new class, creating a couple of instances, then asserting that typestats()[name-of-that-class] returns 2 without a filter, and 1 with a filter that rejects one of the instances (e.g. by attribute value).
mgedmin
left a comment
There was a problem hiding this comment.
Let's fix the surprising test failure on Python 3.4/Windows and it looks good to merge!
|
|
||
| 3.1.3 (unreleased)(2017-12-19) | ||
| ------------------ | ||
|
|
There was a problem hiding this comment.
Thank you for these! But you accidentally duplicated the 3.1.3 version header. Also, the new entries should be below the header (replace the existing "- Nothing changed yet." text). And there's no need to add a date -- I'll add it when I make the release.
| will be lumped together if ``shortnames`` is True. | ||
|
|
||
| Use ``filter`` (a predicate) to remove undesired objects. | ||
|
|
There was a problem hiding this comment.
Very good, only I'd also like to clarify whether the objects for which filter() returns True are included or excluded.
Perhaps something like
If ``filter` is specified, it should be a function taking one argument and returning a boolean.
Objects for which ``filter(obj)`` returns ``False`` will be ignored.
?
|
|
||
| class TypestatsFilterArguTest(GarbageCollectedMixin, unittest.TestCase): | ||
| """Tests for the typestats function, especially for augument | ||
| ``fiter`` which is added at version 3.1.3""" |
| def test_short_repr_lambda(self): | ||
| f = lambda x: x # noqa | ||
| lambda_lineno = sys._getframe().f_lineno - 1 | ||
| self.assertEqual('lambda: tests.py:%s' % lambda_lineno, |
There was a problem hiding this comment.
That is a clever way of getting the line number!
| y.magic_attr = False | ||
| stats = objgraph.typestats( | ||
| shortnames=False, | ||
| filter=lambda e: hasattr(e, 'magic_attr') and e.magic_attr) |
There was a problem hiding this comment.
This fails on Python 3.4 on Windows because, apparently, hasattr(ctypes.cdll, 'magic_attr') tries to load a magic_attr.dll and raises an OSError. Amazing.
I suggest replacing the hasattr check with an isinstance(e, MyClass) check.
There was a problem hiding this comment.
This is a magic error, i had run the test on my computer,(windows, python3.4) and no error .
Thank for your advice, it works!
|
Oops, a regression: this makes regular functions show as lambdas. I'll fix it in master. |
add parameter 'filter' to some function(mainly to typestats), which is very helpful with complex project
show lambda function with filename and lineno, which is more meaningful