Skip to main content

Asynchronous PySnooper

Project description

ASnooper

ASnooper is the asynchronous version of Pysnooper, incorporating most of its functionality for easy debugging in async functions as an alternative to print statements.

Installation

$ pip install ASnooper

Example

Here is a base common func. While adding the @snoop() decorator:

import asyncio
from ASnooper import snoop


@snoop(prefix="name")
async def print_name(name: str) -> str:
    await asyncio.sleep(2)  # io

    name = f"hello {name}"  # modify var

    return name


async def main():
    await print_name("Ling")


if __name__ == '__main__':
    asyncio.run(main())

The output is:

name  11:53:19     call    5 print_name()
name           var: name = 'Ling'
name  11:53:19     line    7 print_name() await asyncio.sleep(2)    # io
name  11:53:21     call    7 print_name()
name           var: name = 'Ling'
name  11:53:21     line    9 print_name() name = f"hello {name}"    # modify var
name  11:53:21     line   11 print_name() return name
name      modified: name = 'hello Ling'
name  11:53:21   return   11 print_name()
name         watch: return = 'hello Ling'
name  Elapsed time: 2001.539ms

Or if you don't want to trace an entire function, you can use async with snoop_context():

import asyncio
from ASnooper import snoop_context


async def print_name(name: str) -> str:
    await asyncio.sleep(2)   # io

    name = f"hello {name}"   # modify var | no trace

    async with snoop_context(prefix="name-context"):
        name += f"-context"  # modify var | trace

    return name


async def main():
    await print_name("Ling")


if __name__ == '__main__':
    asyncio.run(main())

The output is:

name-context 14:11:24     line   11 print_name() name += f"-context"   # modify var, will trace
name-context          var: name = 'hello Ling'
name-context 14:11:24     line   10 print_name() async with snoop_context(prefix="name-context"):
name-context     modified: name = 'hello Ling-context'

More

Output stderr to file:

@snoop(output='debug.log')

See values of some expressions that aren't local variables:

@snoop(watch=('user.id',))

No color output:

@snoop(color=False)

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

asnooper-1.0.0.tar.gz (9.9 kB view details)

Uploaded Source

File details

Details for the file asnooper-1.0.0.tar.gz.

File metadata

  • Download URL: asnooper-1.0.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for asnooper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6418ac0ab1f52247205dbabf8535961b8f9c8392480ff7b8bb08d318359a47ba
MD5 d28dfc8a05707a023433156e83262427
BLAKE2b-256 d04e958c5b705657f308c6eee232ad46d5f7b96e69caf19d359cb175385560c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page