Skip to main content

Seagrass

PyPI package version Build status

A Python event auditing and profiling multitool

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

Note: Seagrass is only supported for Python versions >= 3.8

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

auditor = Auditor()

class MyTypeCheckHook:

    prehook_priority: int = 0
    posthook_priority: int = 0

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

    def posthook(self, event_name, result, context):
        pass

    def reset(self):
        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)

Metadata

Release files for seagrass 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for seagrass 0.1.1
File Size Uploaded
seagrass-0.1.1.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for seagrass 0.1.1
File Interpreter ABI Platform
seagrass-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.4 kB

Release files / seagrass-0.1.1.tar.gz

Download URL seagrass-0.1.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
79c26766eee62617c3b2c4079558d46636480693b2adafc1bb892a1efc8041ba
BLAKE2b-256 checksum
How to use checksums
76c852fee9282a9de507dd4e44125c53e3a4c142f89623e54f98e4243d122e02
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / seagrass-0.1.1-py3-none-any.whl

Download URL seagrass-0.1.1-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
993ef85b81a312848b1d21876380405fb38dc151b777fdb5e0df4d4146bb9d1f
BLAKE2b-256 checksum
How to use checksums
5ea96836d490a63608d4b6dc903c22d6db2751e864db171e145d10e8fe5be608
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

0.10.2

2 release files

0.10.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page