Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from snoop.configuration import Config
from birdseye import eye
config = Config(enabled=False)
@config.spy
def foo():
assert config.pp(1 + 2) == 3
assert config.pp(1 + 3, 6) == (4, 6)
def main():
call_id = eye._last_call_id
foo()
assert call_id is eye._last_call_id
expected_output = """
"""
from snoop.configuration import Config
snoop = Config(columns='time thread thread_ident file full_file function function_qualname').snoop
def main():
@snoop
def foo():
x = 1
y = x + 2
foo()
expected_output = """
12:34:56.78 MainThread 123456789 all_columns.py /path/to_file.py foo main..foo >>> Call to main..foo in File "/path/to_file.py", line 8
def test_callable():
string_io = io.StringIO()
def write(msg):
string_io.write(msg)
string_io = io.StringIO()
config = Config(out=write)
contents = u'stuff'
config.write(contents)
assert string_io.getvalue() == contents
def test_file_output():
_, path = mkstemp()
config = Config(out=path)
contents = u'stuff'
config.write(contents)
with open(path) as output_file:
output = output_file.read()
assert output == contents
from snoop.configuration import Config
config = Config(enabled=False)
# This test leaves out spy for the NO_ASTTOKENS versions,
# but other versions can test it too
@config.snoop
def main():
assert config.pp(1 + 2) == 3
assert config.pp(1 + 3, 6) == (4, 6)
expected_output = """
"""
from snoop.configuration import Config
snoop = Config(columns='').snoop
@snoop
def main():
x = 1
y = x + 2
expected_output = """
>>> Call to main in File "/path/to_file.py", line 7
def test_no_overwrite_by_default():
_, path = mkstemp()
with open(path, 'w') as output_file:
output_file.write(u'lala')
config = Config(str(path))
config.write(u' doo be doo')
with open(path) as output_file:
output = output_file.read()
assert output == u'lala doo be doo'
from snoop.configuration import Config
snoop = Config(prefix='ZZZ').snoop
class Baz(object):
def __init__(self):
self.x = 2
@snoop(watch='self.x')
def square(self):
foo = 7
self.x **= 2
return self
def main():
baz = Baz()
baz.square()
For more information, see https://github.com/alexmojaki/snoop
'''
from .configuration import install, Config
from .variables import Attrs, Exploding, Indices, Keys
import collections
import sys
__VersionInfo = collections.namedtuple('VersionInfo',
('major', 'minor', 'micro'))
__version__ = '0.2.3'
__version_info__ = __VersionInfo(*(map(int, __version__.split('.'))))
config = Config()
snoop = config.snoop
pp = config.pp
spy = config.spy
install = staticmethod(install)
sys.modules['snoop'] = snoop # make the module callable
# Add all the attributes to the 'module' so things can be imported normally
for key, value in list(globals().items()):
if key in 'collections sys __VersionInfo key value config':
# Avoid polluting the namespace
continue
setattr(snoop, key, value)
- `time`: The current time. This is the only column by default.
- `thread`: The name of the current thread.
- `thread_ident`: The [identifier](https://docs.python.org/3/library/threading.html#threading.Thread.ident) of the current thread, in case thread names are not unique.
- `file`: The filename (not the full path) of the current function.
- `full_file`: The full path to the file (also shown anyway when the function is called).
- `function`: The name of the current function.
- `function_qualname`: The qualified name of the current function.
If you want a custom column, please open an issue to tell me what you're interested in! In the meantime, you can pass a list, where the elements are either strings or callables. The callables should take one argument, which will be an `Event` object. It has attributes `frame`, `event`, and `arg`, as specified in [`sys.settrace()`](https://docs.python.org/3/library/sys.html#sys.settrace), and other attributes which may change.
"""
if builtins:
setattr(builtins_module, snoop, package.snoop)
setattr(builtins_module, pp, package.pp)
setattr(builtins_module, spy, package.spy)
config = Config(
out=out,
prefix=prefix,
columns=columns,
overwrite=overwrite,
color=color,
enabled=enabled,
watch_extras=watch_extras,
replace_watch_extras=replace_watch_extras,
formatter_class=formatter_class,
)
package.snoop.config = config
package.pp.config = config
package.spy.config = config