Skip to main content

Auditing and profiling multi-tool

Project description

Seagrass

A Python event auditing and profiling multitool

Seagrass is a Python library for hooking and auditing events in your Python code.

Examples

Here is a simple example of using Seagrass to audit the number of times we call two example functions, add and sub:

import logging
from seagrass import Auditor
from seagrass.hooks import CounterHook

# Preliminary: configure logging
fh = logging.StreamHandler()
fh.setLevel(logging.INFO)
formatter = logging.Formatter("%(name)s: %(message)s")
fh.setFormatter(formatter)

logger = logging.getLogger("seagrass")
logger.setLevel(logging.INFO)
logger.addHandler(fh)

# Create a new Auditor instance
auditor = Auditor()

# Create a hook that will count each time an event occurs
hook = CounterHook()

# Now define some new events by hooking some example functions
@auditor.decorate("event.add", hooks=[hook])
def add(x: int, y: int) -> int:
    return x + y

@auditor.decorate("event.sub", hooks=[hook])
def sub(x: int, y: int) -> int:
    return x - y

# Now start auditing!
with auditor.audit():
    add(1, 2)
    add(3, 4)
    sub(5, 2)

# Display the results of auditing. This should produce the following output:
#
#    seagrass: Calls to events recorded by CounterHook:
#    seagrass:     event.add: 2
#    seagrass:     event.sub: 1
#
auditor.log_results()

From there we can perform more complex tasks. For instance, here's an example where we override Python's time.sleep and profile it:

import time
from seagrass import Auditor
from seagrass.hooks import CounterHook, ProfilerHook

# Omitted: logging configuration

auditor = Auditor()
hooks = [CounterHook(), ProfilerHook()]

ausleep = auditor.wrap(time.sleep, "time.sleep", hooks=hooks)
setattr(time, "sleep", ausleep)

with auditor.audit():
    for _ in range(20):
        time.sleep(0.1)

auditor.log_results()

You can also define custom auditing hooks by creating a new class that implements the interface defined by seagrass.base.ProtoHook. In the example below, we define a custom hook that raises an AssertionError if the argument to the function say_hello has the wrong type.

from seagrass import Auditor
from seagrass.base import ProtoHook

auditor = Auditor()

class MyTypeCheckHook(ProtoHook):

    def prehook(self, event_name, args, kwargs):
        assert isinstance(args[0], str)

    def posthook(self, *args):
        # Do nothing
        pass

hook = MyTypeCheckHook()

@auditor.decorate("event.say_hello", hooks=[hook])
def say_hello(name: str):
    return f"Hello, {name}!"

# Outside of an auditing context, the hook doesn't get called. The
# following calls to say_hello will both return without error, even
# though the second one uses the wrong argument type.
say_hello("Alice")
say_hello(1)

# Inside of an auditing context, the hooks will get called.
with auditor.audit():
    say_hello("Bob")
    # This call to say_hello will raise an AssertionError
    say_hello(2)

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

seagrass-0.1.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

seagrass-0.1.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file seagrass-0.1.0.tar.gz.

File metadata

  • Download URL: seagrass-0.1.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for seagrass-0.1.0.tar.gz
Algorithm Hash digest
SHA256 261f5096215f8f05b8c084fd2acd782dc9f54d3c2924bff1b5c21426e7a5e47a
MD5 e6d88b94290bd6b9f5027aae992422f5
BLAKE2b-256 1f72b6aad166b7b8baa447fce07df9947bc7d0b8aa33b496147ffb78453c0633

See more details on using hashes here.

File details

Details for the file seagrass-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: seagrass-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for seagrass-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 686136b0b0c05f194b11d199634c7f446bd9af54b6b641bcfce33786b86538af
MD5 48ec9fc4a886932db591a662c2a445d0
BLAKE2b-256 5006c6ee79143f32a23e66e91d4af486ef3919603594c174a1e2b2749be6a6e0

See more details on using hashes here.

Supported by

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