Python: profiling blocks of code [duplicate] - python

This question already has answers here:
How can I profile Python code line-by-line?
(5 answers)
Closed 7 years ago.
I have a Python function in which I want to find most slowing-down places. Just not I'm using cProfile, but I have an additional functionality.
I don't want to split my function into a dozen of sub-functions: it looks a bit bulky and annoying.
Isn't there instead a way to profile a function line-by-line? Or add something like timer_start(timer_id) and timer_stop(timer_id) before and after each block of code I want to profile execution time?

If you are not using IPython already, you should give it a look. It has magic functions like %lprun which make line-by-line profiling easy. Take a look at Timing and Profiling in IPython

Related

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

How do I process output from the command line into python? [duplicate]

This question already has answers here:
Passing IPython variables as arguments to bash commands
(4 answers)
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
So I found this tool: https://github.com/elceef/dnstwist and I want to pass a list of domains into that tool then take the output and visualize it.
It operates through the command line, but how do I automate entering each domain and process the output automatically as well?
By the way I am using colab, so I would need a solution using jupyter notebooks!
Thanks!
If you want to do this programmatically I would not use the provided cli – although this would also be possible.
Instead you can create a small python script to do this.
Import dwintwist in your script and have a look at the main function in dwintwist.py to see how you call this.
Doesn't look to hard, but I don't know your programming skill level of course.

How do I block python RuntimeWarning from printing to the terminal? [duplicate]

This question already has answers here:
How to disable Python warnings?
(12 answers)
Closed 8 years ago.
I have some python code that, at some point, takes the axis means of a reasonably sparse array with nans in. Every time I run the code it raises a RuntimeWarning.
I know what's causing the warning, it's intentional, and it doesn't affect the output. It is, however, quite irritating to be presented with the warning every time I run the program - so, is there a cheap and nasty way to prevent them from being printed to the terminal?
This may be useful to you, I think the issue has been pretty solved in this question: How to disable python warnings
All what you need is suppress it exactly as described in official documentation: https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

How to delete multiple lines in a python console window? [duplicate]

This question already has answers here:
Rewrite multiple lines in the console
(7 answers)
Closed 9 years ago.
I am currently working on a text-adventure, which should have a more or less complex fight system. To create such, I decided to generate a 16x16 tile battlefield, which will be represented by ASCII-characters. But, because a fight may take more than turn, I don't want to reprint the battlefield multiple times, but delete the "old" one and print the new situation to the same place. But I suppose that it won't work with sys.stdout.write() and sys.stdout.flush() since there have to be removed multiple lines. So my question is: how do I accomplish my goal? At the moment I open a new console window, where everything is reprinted, which is ahem... not very elegant.
Another problem would be the cross-platform use of the programm.
Check out the curses module (http://docs.python.org/2/library/curses.html).

Python 3 memory profiler or simple alternative [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there any working memory profiler for Python3
I have some script where I'd like to find out which objects are using up the memory. Moreover it should be for Python 3. I've found some modules suggested for that, but they are often old and not coded for Python 3. Also trying to install all of them to try out is quite a hassle.
Do you know which memory profiler could give me the following statistics or maybe you know a manual Python hack I can program adhoc to examine the rather short script (not written by me and hence a mess :()?
I'd like to see the object count after the run and moreover in which line this object was created. That's all :)
Any ideas?
EDIT:
One idea is to wrap all object instantiations by a class that counts the initialization in a class variable. It just would require to rewrite all instantiations of dicts, lists, etc.
I'm using objgraph which works with python3 without modifications.

Categories