https://github.com/duboviy/extdbg
💮 Extended debugging python utilities
https://github.com/duboviy/extdbg
debug python testing utility
Last synced: 10 months ago
JSON representation
💮 Extended debugging python utilities
- Host: GitHub
- URL: https://github.com/duboviy/extdbg
- Owner: duboviy
- License: mit
- Created: 2016-10-04T07:24:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T19:55:34.000Z (about 6 years ago)
- Last Synced: 2025-08-19T15:28:13.768Z (10 months ago)
- Topics: debug, python, testing, utility
- Language: Python
- Homepage: https://pypi.python.org/pypi/extdbg
- Size: 47.9 KB
- Stars: 21
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
extdbg
by [Eugene Duboviy](https://duboviy.github.io/)
[](https://travis-ci.org/duboviy/extdbg) [](https://www.codacy.com/app/dubovoy/extdbg?utm_source=github.com&utm_medium=referral&utm_content=duboviy/extdbg&utm_campaign=Badge_Grade) [](https://pypi.python.org/pypi/extdbg) [](https://landscape.io/github/duboviy/extdbg/master) [](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/pulls) [](https://github.com/duboviy/extdbg/)
Extended debugging python utilities
## Installation:
Install from PyPI:
```
pip install extdbg
```
Or using alternative command:
```
pip install https://github.com/duboviy/extdbg/archive/master.zip
```
Or from source use:
```
python setup.py install
```
## Supported python versions
* 2.7
* 3.3
* 3.4
* 3.5
* PyPy
## PyPI
* [Package](https://pypi.python.org/pypi/extdbg)
* [Documentation](https://pythonhosted.org/extdbg/)
Contents
--------
```
from extdbg import ... (see variants in list below)
```
- `init_except_hook` - initialise extended traceback hook with local variables in exception message
- `add_watcher_attribute(name, watch_get=False)` - when called within some class (for example `add_watcher_attribute('name')`) - instances of that class will log every change to the attribute. Also tracks every access to attribute if `watch_get` is `True`.
- `where_is(object)` - return location of the object definition in code.
- `from_where_called()` - return location in code from where function which calls this is called
- `threaded` - allow you to decorate a function in your Python code, making it run in a separate thread
- `get_func_calls` - return all function calls from a python file
- `public and internal` - allow you to use additional context decorators (like public and private in other languages)
- `hackable_properties` - properties mock setter. Use this module to mock/stub any property of New Style Class
- `watch_for_output` - log location in code where some output is performed
- `bound_func` - bind any function/lambda as instance's method
- `save_object and load_object` - pickle/unpickle objects in/from files
- `enable_debug` - use on remote process to debug it using pycharm remote server run
... and many other features
Basic usage examples
--------
- `init_except_hook` - initialise extended traceback hook with local variables in exception message
```python
from extdbg import init_except_hook
init_except_hook()
def test(a, b):
a / b
test(1, 0)
```
- `where_is(object)` - return location of the object definition in code
```python
from extdbg import where_is
location = where_is(where_is)
expected_filename = "navigate.py"
expected_line_no = 17
assert location.filename.endswith(expected_filename)
assert location.line_no == expected_line_no
```
- `from_where_called()` - return location in code from where function which calls this is called
```python
from extdbg import from_where_called
def f2():
print(from_where_called())
def f1():
f2()
f1()
```
- `threaded` - allow you to decorate a function in your Python code, making it run in a separate thread
```python
from extdbg import threaded
def fib(n):
if n < 2:
return n
return fib(n-2) + fib(n-1)
print(fib(35))
@threaded(daemon=True)
def _fib(n):
if n < 2:
return n
return fib(n-2) + fib(n-1)
print(_fib(35))
```
- `get_func_calls` - return all function calls from a python file
```python
import ast
from extdbg import get_func_calls
filename = __file__
tree = ast.parse(open(__file__).read())
print(get_func_calls(tree))
```
- `public and internal` - allow you to use additional context decorators (like public and private in other languages). You can use decorator to wrap your code:
```python
from extdbg import public, internal
class A(object):
@internal
def m1(self):
pass
@public
def m2(self):
self.m1()
a = A()
a.m1() # can't call API m1 is marked as internal!
a.m2()
```
- `hackable_properties` - properties mock setter. Use this module to mock/stub any property of New Style Class
```python
from extdbg import hackable_properties, ValueWrapper
class NewStyleCls(object):
@property
def prop_2_mock(self):
raise RuntimeError("Unmocked property!")
@hackable_properties
class NewStyleClsMocked(NewStyleCls):
def __init__(self, *args, **kwargs):
super(NewStyleClsMocked, self).__init__(*args, **kwargs)
self.prop_2_mock = ValueWrapper("MOCKING_PROP_VALUE")
mockable_cls = NewStyleClsMocked()
assert mockable_cls.prop_2_mock == "MOCKING_PROP_VALUE"
```
- `watch_for_output(condition=lambda x: True, stream='stdout')` - log location in code where some output is performed
```python
import unittest
from mock import Mock, patch
class TestWatchForOutput(unittest.TestCase):
def test_works(self):
from extdbg import watch_for_output
mock_stdout = Mock()
output = []
def write(txt):
output.append(txt)
mock_stdout.write = write
with patch('sys.stdout', mock_stdout):
watch_for_output(lambda s: 'yes' in s)
print('yes')
self.assertIn('yes', output)
```
- `bound_func` - bind any function/lambda as instance's method
```python
import unittest
import inspect
from mock import Mock, patch
class TestBoundFunc(unittest.TestCase):
def test_works(self):
from extdbg import bound_func
# Example of usage
class Cls2Bound:
pass
instance2Bound = Cls2Bound()
def func(*args, **kwargs):
print(args, kwargs)
l = lambda *args, **kwargs: None
# bound method Cls2Bound.func
bound_method1 = bound_func(func, instance2Bound, Cls2Bound)
# bound method Cls2Bound.
bound_method2 = bound_func(l, instance2Bound, Cls2Bound)
self.assertTrue(inspect.ismethod(bound_method1))
self.assertTrue(inspect.ismethod(bound_method2))
```
- `enable_debug` - use on remote process to debug it using pycharm remote server run
```python
from extdbg import enable_debug
enable_debug("/home//pydev/debug-eggs/pycharm-debug.egg", 'localhost')
```
## License
**MIT** licensed library. See [LICENSE.txt](LICENSE.txt) for details.
## Contributing
If you have suggestions for improving the extdbg, please [open an issue or
pull request on GitHub](https://github.com/duboviy/extdbg/).
## Badges
[](https://github.com/duboviy/extdbg/)
[](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/)
[](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/) [](https://github.com/duboviy/extdbg/)
[](https://github.com/ellerbrock/open-source-badge/)
[](https://github.com/duboviy/extdbg/)