Skip to content

Optional import stub #1

@ringerc

Description

@ringerc

Hi, thanks for the convenient little library.

I wanted it to be optional so I wrote the following little import stub. Consider adding it to your README for the convenience of others.

Instead of:

from timer_cm import Timer

I stub out the API if the import fails with:

try:
    from timer_cm import Timer
except ImportError as ex:
    # You should pip3 install timer_cm
    # see https://github.com/mherrmann/timer-cm/blob/master/timer_cm.py
    # But we provide a dummy to reduce dependencies
    class Timer:
        def __init__(self, name, print_results=None):
            pass
        def __enter__(self):
            return self
        def __exit__(self, *_):
            pass
        def child(self, name):
            return self
        def print_results(self):
            pass

that way with Timer('foo'): etc all work as expected and just do nothing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions