<feed xmlns='http://www.w3.org/2005/Atom'>
<title>delta/cpython-git.git/Lib/pstats.py, branch dependabot/github_actions/actions/upload-artifact-v2.2.1</title>
<subtitle>github.com: python/cpython.git
</subtitle>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/'/>
<entry>
<title>bpo-41811: create SortKey members using first given value (GH-22316)</title>
<updated>2020-09-19T18:12:57+00:00</updated>
<author>
<name>Ethan Furman</name>
<email>ethan@stoneleaf.us</email>
</author>
<published>2020-09-19T18:12:57+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=ae0d2a33ec05aece939a959d36fcf1df1e210a08'/>
<id>ae0d2a33ec05aece939a959d36fcf1df1e210a08</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-37958: Adding get_profile_dict to pstats (GH-15495)</title>
<updated>2020-01-15T22:51:54+00:00</updated>
<author>
<name>Daniel Olshansky</name>
<email>olshansky.daniel@gmail.com</email>
</author>
<published>2020-01-15T22:51:54+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=01602ae40321ecdb375ee6d44eaeac3255857879'/>
<id>01602ae40321ecdb375ee6d44eaeac3255857879</id>
<content type='text'>
pstats is really useful or profiling and printing the output of the execution of some block of code, but I've found on multiple occasions when I'd like to access this output directly in an easily usable dictionary on which I can further analyze or manipulate.

The proposal is to add a function called get_profile_dict inside of pstats that'll automatically return this data the data in an easily accessible dict.

The output of the following script:

```
import cProfile, pstats
import pprint
from pstats import func_std_string, f8

def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)

pr = cProfile.Profile()
pr.enable()
fib(5)
pr.create_stats()

ps = pstats.Stats(pr).sort_stats('tottime', 'cumtime')

def get_profile_dict(self, keys_filter=None):
    """
        Returns a dict where the key is a function name and the value is a dict
        with the following keys:
            - ncalls
            - tottime
            - percall_tottime
            - cumtime
            - percall_cumtime
            - file_name
            - line_number

        keys_filter can be optionally set to limit the key-value pairs in the
        retrieved dict.
    """
    pstats_dict = {}
    func_list = self.fcn_list[:] if self.fcn_list else list(self.stats.keys())

    if not func_list:
        return pstats_dict

    pstats_dict["total_tt"] = float(f8(self.total_tt))
    for func in func_list:
        cc, nc, tt, ct, callers = self.stats[func]
        file, line, func_name = func
        ncalls = str(nc) if nc == cc else (str(nc) + '/' + str(cc))
        tottime = float(f8(tt))
        percall_tottime = -1 if nc == 0 else float(f8(tt/nc))
        cumtime = float(f8(ct))
        percall_cumtime = -1 if cc == 0 else float(f8(ct/cc))
        func_dict = {
            "ncalls": ncalls,
            "tottime": tottime, # time spent in this function alone
            "percall_tottime": percall_tottime,
            "cumtime": cumtime, # time spent in the function plus all functions that this function called,
            "percall_cumtime": percall_cumtime,
            "file_name": file,
            "line_number": line
        }
        func_dict_filtered = func_dict if not keys_filter else { key: func_dict[key] for key in keys_filter }
        pstats_dict[func_name] = func_dict_filtered

    return pstats_dict

pp = pprint.PrettyPrinter(depth=6)
pp.pprint(get_profile_dict(ps))
```

will produce:

```
{"&lt;method 'disable' of '_lsprof.Profiler' objects&gt;": {'cumtime': 0.0,
                                                      'file_name': '~',
                                                      'line_number': 0,
                                                      'ncalls': '1',
                                                      'percall_cumtime': 0.0,
                                                      'percall_tottime': 0.0,
                                                      'tottime': 0.0},
 'create_stats': {'cumtime': 0.0,
                  'file_name': '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/cProfile.py',
                  'line_number': 50,
                  'ncalls': '1',
                  'percall_cumtime': 0.0,
                  'percall_tottime': 0.0,
                  'tottime': 0.0},
 'fib': {'cumtime': 0.0,
         'file_name': 'get_profile_dict.py',
         'line_number': 5,
         'ncalls': '15/1',
         'percall_cumtime': 0.0,
         'percall_tottime': 0.0,
         'tottime': 0.0},
 'total_tt': 0.0}
 ```

 As an example, this can be used to generate a stacked column chart using various visualization tools which will assist in easily identifying program bottlenecks.



https://bugs.python.org/issue37958



Automerge-Triggered-By: @gpshead</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
pstats is really useful or profiling and printing the output of the execution of some block of code, but I've found on multiple occasions when I'd like to access this output directly in an easily usable dictionary on which I can further analyze or manipulate.

The proposal is to add a function called get_profile_dict inside of pstats that'll automatically return this data the data in an easily accessible dict.

The output of the following script:

```
import cProfile, pstats
import pprint
from pstats import func_std_string, f8

def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)

pr = cProfile.Profile()
pr.enable()
fib(5)
pr.create_stats()

ps = pstats.Stats(pr).sort_stats('tottime', 'cumtime')

def get_profile_dict(self, keys_filter=None):
    """
        Returns a dict where the key is a function name and the value is a dict
        with the following keys:
            - ncalls
            - tottime
            - percall_tottime
            - cumtime
            - percall_cumtime
            - file_name
            - line_number

        keys_filter can be optionally set to limit the key-value pairs in the
        retrieved dict.
    """
    pstats_dict = {}
    func_list = self.fcn_list[:] if self.fcn_list else list(self.stats.keys())

    if not func_list:
        return pstats_dict

    pstats_dict["total_tt"] = float(f8(self.total_tt))
    for func in func_list:
        cc, nc, tt, ct, callers = self.stats[func]
        file, line, func_name = func
        ncalls = str(nc) if nc == cc else (str(nc) + '/' + str(cc))
        tottime = float(f8(tt))
        percall_tottime = -1 if nc == 0 else float(f8(tt/nc))
        cumtime = float(f8(ct))
        percall_cumtime = -1 if cc == 0 else float(f8(ct/cc))
        func_dict = {
            "ncalls": ncalls,
            "tottime": tottime, # time spent in this function alone
            "percall_tottime": percall_tottime,
            "cumtime": cumtime, # time spent in the function plus all functions that this function called,
            "percall_cumtime": percall_cumtime,
            "file_name": file,
            "line_number": line
        }
        func_dict_filtered = func_dict if not keys_filter else { key: func_dict[key] for key in keys_filter }
        pstats_dict[func_name] = func_dict_filtered

    return pstats_dict

pp = pprint.PrettyPrinter(depth=6)
pp.pprint(get_profile_dict(ps))
```

will produce:

```
{"&lt;method 'disable' of '_lsprof.Profiler' objects&gt;": {'cumtime': 0.0,
                                                      'file_name': '~',
                                                      'line_number': 0,
                                                      'ncalls': '1',
                                                      'percall_cumtime': 0.0,
                                                      'percall_tottime': 0.0,
                                                      'tottime': 0.0},
 'create_stats': {'cumtime': 0.0,
                  'file_name': '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/cProfile.py',
                  'line_number': 50,
                  'ncalls': '1',
                  'percall_cumtime': 0.0,
                  'percall_tottime': 0.0,
                  'tottime': 0.0},
 'fib': {'cumtime': 0.0,
         'file_name': 'get_profile_dict.py',
         'line_number': 5,
         'ncalls': '15/1',
         'percall_cumtime': 0.0,
         'percall_tottime': 0.0,
         'tottime': 0.0},
 'total_tt': 0.0}
 ```

 As an example, this can be used to generate a stacked column chart using various visualization tools which will assist in easily identifying program bottlenecks.



https://bugs.python.org/issue37958



Automerge-Triggered-By: @gpshead</pre>
</div>
</content>
</entry>
<entry>
<title>Fix typos in docs and docstrings (GH-13745)</title>
<updated>2019-06-02T23:12:33+00:00</updated>
<author>
<name>Xtreak</name>
<email>tir.karthi@gmail.com</email>
</author>
<published>2019-06-02T23:12:33+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=0d70227e419ab78c44d81b4ea6ae8aaf769470e6'/>
<id>0d70227e419ab78c44d81b4ea6ae8aaf769470e6</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-36766: Typos in docs and code comments (GH-13116)</title>
<updated>2019-05-06T18:57:17+00:00</updated>
<author>
<name>penguindustin</name>
<email>penguindustin@gmail.com</email>
</author>
<published>2019-05-06T18:57:17+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=964663089547ca110199e23867b46b07ff4be88c'/>
<id>964663089547ca110199e23867b46b07ff4be88c</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>Revert unneccessary changes made in bpo-30296 and apply other improvements. (GH-2624)</title>
<updated>2018-02-26T14:50:11+00:00</updated>
<author>
<name>Serhiy Storchaka</name>
<email>storchaka@gmail.com</email>
</author>
<published>2018-02-26T14:50:11+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=3f2e6f15d64d81633b1fc0b308afc0d6e9026b61'/>
<id>3f2e6f15d64d81633b1fc0b308afc0d6e9026b61</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-29237: Create enum for pstats sorting options (GH-5103)</title>
<updated>2018-01-26T04:49:56+00:00</updated>
<author>
<name>mwidjaja</name>
<email>mwidj@yahoo.com</email>
</author>
<published>2018-01-26T04:49:56+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=863b1e4d0e95036bca4e97c1b8b2ca72c19790fb'/>
<id>863b1e4d0e95036bca4e97c1b8b2ca72c19790fb</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-30296 Remove unnecessary tuples, lists, sets, and dicts (#1489)</title>
<updated>2017-05-18T14:35:54+00:00</updated>
<author>
<name>Jon Dufresne</name>
<email>jon.dufresne@gmail.com</email>
</author>
<published>2017-05-18T14:35:54+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=3972628de3d569c88451a2a176a1c94d8822b8a6'/>
<id>3972628de3d569c88451a2a176a1c94d8822b8a6</id>
<content type='text'>
* Replaced list(&lt;generator expression&gt;) with list comprehension
* Replaced dict(&lt;generator expression&gt;) with dict comprehension
* Replaced set(&lt;list literal&gt;) with set literal
* Replaced builtin func(&lt;list comprehension&gt;) with func(&lt;generator
  expression&gt;) when supported (e.g. any(), all(), tuple(), min(), &amp;
  max())</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
* Replaced list(&lt;generator expression&gt;) with list comprehension
* Replaced dict(&lt;generator expression&gt;) with dict comprehension
* Replaced set(&lt;list literal&gt;) with set literal
* Replaced builtin func(&lt;list comprehension&gt;) with func(&lt;generator
  expression&gt;) when supported (e.g. any(), all(), tuple(), min(), &amp;
  max())</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-30022: Get rid of using EnvironmentError and IOError (except test… (#1051)</title>
<updated>2017-04-16T07:46:38+00:00</updated>
<author>
<name>Serhiy Storchaka</name>
<email>storchaka@gmail.com</email>
</author>
<published>2017-04-16T07:46:38+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=55fe1ae9708d81b902b6fe8f6590e2a24b1bd4b0'/>
<id>55fe1ae9708d81b902b6fe8f6590e2a24b1bd4b0</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>bpo-29554: Improve docs for pstat module and profile. (#88)</title>
<updated>2017-02-21T05:30:00+00:00</updated>
<author>
<name>Matthias Bussonnier</name>
<email>bussonniermatthias@gmail.com</email>
</author>
<published>2017-02-21T05:30:00+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=8fb1f6e039cbdeb333d83b7a62f0f37af4ce6e02'/>
<id>8fb1f6e039cbdeb333d83b7a62f0f37af4ce6e02</id>
<content type='text'>
Clarify that methods take a string which is interpreted as a regex,
not a regex object.

Also clarify what the old `-1`, `0`, `1` and `2` options were.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Clarify that methods take a string which is interpreted as a regex,
not a regex object.

Also clarify what the old `-1`, `0`, `1` and `2` options were.
</pre>
</div>
</content>
</entry>
<entry>
<title>Issue #27241: Catch exception when running pstats as main.</title>
<updated>2016-08-02T20:30:24+00:00</updated>
<author>
<name>Stefan Krah</name>
<email>skrah@bytereef.org</email>
</author>
<published>2016-08-02T20:30:24+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/cpython-git.git/commit/?id=e12a68be3580a3c7b80d384b2664b1921a7ff55b'/>
<id>e12a68be3580a3c7b80d384b2664b1921a7ff55b</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
</feed>
