Skip to content

MOD:add parameter 'filter' to some method and show lambda more friendly#33

Merged
mgedmin merged 5 commits intomgedmin:masterfrom
xybaby:master
Dec 20, 2017
Merged

MOD:add parameter 'filter' to some method and show lambda more friendly#33
mgedmin merged 5 commits intomgedmin:masterfrom
xybaby:master

Conversation

@xybaby
Copy link
Copy Markdown
Contributor

@xybaby xybaby commented Dec 17, 2017

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

Copy link
Copy Markdown
Owner

@mgedmin mgedmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread objgraph.py


def typestats(objects=None, shortnames=True):
def typestats(objects=None, shortnames=True, filter=None):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new parameter needs documentation in the docstring, and a .. versionchanged: ... directive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, and i have do some fix with your advise, please check again

Comment thread objgraph.py


def most_common_types(limit=10, objects=None, shortnames=True):
def most_common_types(limit=10, objects=None, shortnames=True, filter=None):
Copy link
Copy Markdown
Owner

@mgedmin mgedmin Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

EDIT: I mean this needs a docstring update. No unit test necessary.

Comment thread objgraph.py
shortnames=True,
file=None):
file=None,
filter=None):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

Comment thread objgraph.py Outdated


def show_growth(limit=10, peak_stats={}, shortnames=True, file=None):
def show_growth(limit=10, peak_stats={}, shortnames=True, file=None, filter=None):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

Comment thread objgraph.py Outdated
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)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread objgraph.py
stats = {}
for o in objects:
if filter and not filter(o):
continue
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Owner

@mgedmin mgedmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix the surprising test failure on Python 3.4/Windows and it looks good to merge!

Comment thread CHANGES.rst Outdated

3.1.3 (unreleased)(2017-12-19)
------------------

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread objgraph.py
will be lumped together if ``shortnames`` is True.

Use ``filter`` (a predicate) to remove undesired objects.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

?

Comment thread tests.py Outdated

class TypestatsFilterArguTest(GarbageCollectedMixin, unittest.TestCase):
"""Tests for the typestats function, especially for augument
``fiter`` which is added at version 3.1.3"""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: fiter -> filter.

Comment thread tests.py
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,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a clever way of getting the line number!

Comment thread tests.py Outdated
y.magic_attr = False
stats = objgraph.typestats(
shortnames=False,
filter=lambda e: hasattr(e, 'magic_attr') and e.magic_attr)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

@xybaby xybaby Dec 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Owner

@mgedmin mgedmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

@mgedmin mgedmin merged commit 0bf74b2 into mgedmin:master Dec 20, 2017
@mgedmin
Copy link
Copy Markdown
Owner

mgedmin commented Dec 20, 2017

Oops, a regression: this makes regular functions show as lambdas.

I'll fix it in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants