The announcement yesterday wasn’t formatted properly, so I repost it,
hopefully, more legible.

This is to announce the release of version 1.1.4 of ycecream, a package to
help printing (debug) information in a more intuitive and more versatile
way than just print statements. And you get simple benchmarking (timing)
functionality as a bonus.

The package can be installed from PyPI with pip install ycecream or
directly from GitHub: https://github.com/salabim/ycecream
Short introduction Inspect variables and expressions

Have you ever printed variables or expressions to debug your program? If
you’ve ever typed something like

print(add2(1000))

or the more thorough

print("add2(1000)", add2(1000)))
`

or (for Python >= 3.8 only):

print(f"{add2(1000) =}")

then y() is here to help. With arguments, y() inspects itself and prints
both its own arguments and the values of those arguments.

from ycecream import y
def add2(i):
    return i + 2
y(add2(1000))

prints

y| add2(1000): 1002

Similarly,

from ycecream import y
class X:
    a = 3
world = {"EN": "world", "NL": "wereld", "FR": "monde", "DE": "Welt"}
y(world, X.a)

prints

y| world: {"EN": "world", "NL": "wereld", "FR": "monde", "DE": "Welt"}, X.a: 3

Just give y() a variable or expression and you’re done. Sweet, isn’t it?
Inspect execution

Have you ever used print() to determine which parts of your program are
executed, and in which order they’re executed? For example, if you’ve ever
added print statements to debug code like

def add2(i):
    print("enter")
    result = i + 2
    print("exit")
    return result

then y() helps here, too. Without arguments, y() inspects itself and prints
the calling filename, line number, and parent function.

from ycecream import y
def add2(i):
    y()
    result = i + 2
    y()
    return result
y(add2(1000))

prints something like

y| x.py:3 in add2()
y| x.py:5 in add2()
y| add2(1000): 1002

Just call y() and you’re done. Isn’t that sweet?
Debug entry and exit of function calls

When you apply y as a decorator to a function or method, both the entry and
exit can be tracked. The (keyword) arguments passed will be shown and upon
return, the return value.

from ycecream import y
@y
def mul(x, y):
    return x * y
print(mul(5, 7))

prints

y| called mul(5, 7)
y| returned 35 from mul(5, 7) in 0.000006 seconds
35

Benchmarking with ycecream

If you decorate a function or method with y, you will be offered the
duration between entry and exit (in seconds) as a bonus.

That opens the door to simple benchmarking, like:

from ycecream import y
import time
@y(show_enter=False)
def do_sort(n):
    x = sorted(list(range(10 ** n)))
for i in range(8):
    do_sort(i)

the ouput will show the effects of the population size on the sort speed:

y| returned None from do_sort(0) in 0.000011 seconds
y| returned None from do_sort(1) in 0.000032 seconds
y| returned None from do_sort(2) in 0.000010 seconds
y| returned None from do_sort(3) in 0.000042 seconds
y| returned None from do_sort(4) in 0.000716 seconds
y| returned None from do_sort(5) in 0.004501 seconds
y| returned None from do_sort(6) in 0.049840 seconds
y| returned None from do_sort(7) in 0.490177 seconds

Acknowledgement

The ycecream pacakage is a fork of the IceCream package. See
https://github.com/gruns/icecream

Many thanks to the author Ansgar Grunseid / grunseid.com /
gruns...@gmail.com
Differences with IceCream

The ycecream module is a fork of IceCream with a number of differences:

   - ycecream can’t colourize the output (a nice feature of IceCream)
   - ycecream runs only on Python 3.6 and higher. (IceCream runs even on
   Python 2.7).
   - ycecream uses y as the standard interface, whereas IceCream uses ic.
   For compatibility, ycecream also supports ic.
   - yceceam has no dependencies. IceCream on the other hand has many
   (asttoken, colorize, pyglets, …).
   - ycecream is just one .py file, whereas IceCream consists of a number
   of .py files. That makes it possible to use ycecream without even (pip)
   installing it. Just copy ycecream.py to your work directory.
   - ycecream can be used as a decorator of a function showing the enter
   and/or exit event as well as the duration.
   - ycecream can be used as a context manager to benchamrk code.
   - ycecream has a PEP8 (Pythonic) API. Less important for the user, the
   actual code is also (more) PEP8 compatible. IceCream does not follow the
   PEP8 standard.
   - ycecream uses a completely different API to configure (rather than
   IceCream’s configureOutput method)
   - ycecream time showing can be controlled independently from context
   showing
   - ycecream can optionally show a delta (time since start of the program)
   - ycecream does not sort dicts by default. This behaviour can be
   controlled with the sort_dict parameter. (This is implemented by including
   the pprint 3.8 source code)
   - ycecream can be configured from a json file, thus overriding some or
   all default settings at import time.
   - ycecream uses pytest for the test scripts rather than IceCream’s
   unittest script.
_______________________________________________
Python-announce-list mailing list -- python-announce-list@python.org
To unsubscribe send an email to python-announce-list-le...@python.org
https://mail.python.org/mailman3/lists/python-announce-list.python.org/
Member address: arch...@mail-archive.com

Reply via email to