A profiling tool that can visualize python code running
Project description
CodeSnap
CodeSnap is a pure-python, light-weighted profiling tool that can visualize python code running result. The major data CodeSnap displays is FEE(function entry/exit), or equivalently, the call stack.
With CodeSnap, the programmer can intuitively understand what their code is doing and how long each function takes.
Requirements
CodeSnap requires python 3.5+. No other package is needed.
Install
The prefered way to install CodeSnap is via pip
pip install codesnap
You can also download the source code and build it yourself.
Usage
There are a couple ways to use CodeSnap
Command Line
The easiest way to use CodeSnap it through command line. Assume you have a python script to profile and the normal way to run it is:
python3 my_script.py
You can simply use CodeSnap as
python3 -m codesnap my_script.py
which will generate a result.html
file in the directory you run this command. Open it in browser and there's your result.
If your script needs arguments like
python3 my_script.py arg1 arg2
Just feed it as it is to CodeSnap
python3 -m codesnap my_script.py arg1 arg2
Inline
Sometimes the command line may not work as you expected, or you do not want to profile the whole script. You can manually start/stop the profiling in your script as well.
First of all, you need to import CodeSnap
class from the package, and make an object of it.
from codesnap import CodeSnap
snap = CodeSnap()
If your code is executable by exec
function, you can simply call snap.run()
snap.run("import random;random.randrange(10)")
This will as well generate a result.html
file in your current directory. You can pass other file path to the function if you do not like the name result.html
snap.run("import random; random.randrange(10)", output_file = "better_name.html")
When you need a more delicate profiler, you can manually enable/disable the profile using start()
and stop()
function.
snap.start()
# Something happens here
snap.stop()
snap.save() # also takes output_file as an optional argument
With this method, you can only record the part that you are interested in
# Some code that I don't care
snap.start()
# Some code I do care
snap.stop()
# Some code that I want to skip
snap.start()
# Important code again
snap.stop()
snap.save()
It is higly recommended that start()
and stop()
function should be in the same frame(same level on call stack). Problem might happen if the condition is not met
Limitations
CodeSnap uses sys.setprofile()
for its profiler capabilities, so it will conflict with other profiling tools which also use this function. Be aware of it when using CodeSnap.
Bugs/Requirements
Please send bug reports and feature requirements through github issue tracker. CodeSnap is currently under development now and it's open to any constructive suggestions.
License
Copyright Tian Gao, 2020.
Distributed under the terms of the Apache 2.0 license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file codesnap-0.0.1.tar.gz
.
File metadata
- Download URL: codesnap-0.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b4c5bca6da522c923bea53cccb91274b6e1f93f7f4e9bd4a5cae196c436bfea |
|
MD5 | 0be067546e07acef03548b023db50f8f |
|
BLAKE2b-256 | 1066e4c6e9bedad40775e63bff3c0816bd0284a5c8a931e36524d7814dcb2a69 |
File details
Details for the file codesnap-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: codesnap-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6df1aa89e237e1489528ed107d047e693d44e52f3a67aa91cfba28aa96303b2 |
|
MD5 | 415ff96809575cf458fb00c11c21123e |
|
BLAKE2b-256 | 169b08fb3f35f0169b15d70be3c1ed65b9e0b96dedd7d14e4602a24bd06720c9 |