Skip to content

Commit

Permalink
gen readme; add rb_alias tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Landy committed Nov 16, 2024
1 parent cc13189 commit fe8c36a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 22 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,27 @@ pip install traceback-with-variables==2.1.1
conda install -c conda-forge traceback-with-variables=2.1.1
```

to use shorter `tb` alias in interactive mode call this once:
```
python3 -c 'from traceback_with_variables.tb_alias import create_tb_alias as c; c()'
```

### 🚀 Quick Start

Using without code editing, <a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/external_script.sh">running your script/command/module</a>:
```
traceback-with-variables tested_script.py ...srcipt's args...
```

<a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple.py">Simplest usage in regular Python</a>, for the whole program:
<a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple.py">Simplest usage</a>, for the whole program:
```python
from traceback_with_variables import activate_by_import
```

<a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple_jupyter.py">Simplest usage in Jupyter or IPython</a>, for the whole program:
or just (if you added an alias by the above command)
```python
from traceback_with_variables import activate_in_ipython_by_import
import tb.a
```


<a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_for_function.py">Decorator</a>, for a single function:
```python
@prints_exc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Traceback (most recent call last):
File "...omitted for tests only.../code.py", line...omitted for tests only..., in <module>
import tb.a
ModuleNotFoundError: No module named 'tb'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Traceback (most recent call last):
File "...omitted for tests only.../code.py", line...omitted for tests only..., in <module>
import tb.a
ModuleNotFoundError: No module named 'tb'
29 changes: 29 additions & 0 deletions tests/dumps/test_tb_alias.create_and_rm_tb_alias.run_with_tb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Traceback with variables (most recent call last):
File "...omitted for tests only.../code.py", line...omitted for tests only..., in <module>
f()
...skipped... 13 vars
File "...omitted for tests only.../code.py", line...omitted for tests only..., in f
return f(
n = 10001
s1 = 'short string with n: 10001'
l1 = 'long string with 0..n: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 1...9951, 9952, 9953, 9954, 9955, 9956, 9957, 9958, 9959, 9960, 9961, 9962, 9963, 9964, 9965, 9966, 9967, 9968, 9969, 9970, 9971, 9972, 9973, 9974, 9975, 9976, 9977, 9978, 9979, 9980, 9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000'
us = <exception while printing> Traceback (most recent call last):
File "...omitted for tests only.../core.py", line...omitted for tests only..., in _to_cropped_str
raw = print_(obj)
File "...omitted for tests only.../code.py", line...omitted for tests only..., in __repr__
raise ValueError("please don't print me")
ValueError: please don't print me

File "...omitted for tests only.../code.py", line...omitted for tests only..., in f
return 1 // (n * 0)
n = 10000
s1 = 'short string with n: 10000'
l1 = 'long string with 0..n: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 1... 9950, 9951, 9952, 9953, 9954, 9955, 9956, 9957, 9958, 9959, 9960, 9961, 9962, 9963, 9964, 9965, 9966, 9967, 9968, 9969, 9970, 9971, 9972, 9973, 9974, 9975, 9976, 9977, 9978, 9979, 9980, 9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999'
us = <exception while printing> Traceback (most recent call last):
File "...omitted for tests only.../core.py", line...omitted for tests only..., in _to_cropped_str
raw = print_(obj)
File "...omitted for tests only.../code.py", line...omitted for tests only..., in __repr__
raise ValueError("please don't print me")
ValueError: please don't print me

builtins.ZeroDivisionError: integer division or modulo by zero
25 changes: 12 additions & 13 deletions tests/test_module_alias.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import importlib
import os

import pytest

import traceback_with_variabless as twv
import traceback_with_variables as twv
from traceback_with_variables.module_alias import (
create_alias,
rm_alias,
Expand All @@ -13,50 +12,50 @@
)


ROOT_PATH = Path(twv.__file__).parent
ROOT_PATH = Path(twv.__file__).parent.parent
TWV = 'traceback_with_variables'

def test_module_name_to_path():
assert module_name_to_path('os') == Path(os.__file__)
assert module_name_to_path(TWV) = ROOT_PATH / TWV
assert module_name_to_path(TWV) == ROOT_PATH / TWV
with pytest.raises(NoModuleError):
module_name_to_path('nonexistant_module')


def test_create_and_rm_alias():
with pytest.raised(ValueError) as e:
with pytest.raises(ValueError) as e:
create_alias('', TWV)
assert e.value == 'the alias must be non-empty')
assert str(e.value) == 'the alias must be non-empty'
with pytest.raises(ValueError) as e:
create_alias('bad name', TWV)
assert e.value == 'the alias must have only ascii lowecases, digits and underscores'
assert str(e.value) == 'the alias must have only ascii lowecases, digits and underscores'
with pytest.raises(NoModuleError):
create_alias('alias1', 'nonexistant_module')
with pytest.raises(ValueError) as e:
create_alias('os', TWV)
assert e.value == 'a module with the alias name already exists'
assert str(e.value) == 'a module with the alias name already exists'

with pytest.raises(NoModuleError):
rm_alias('alias1')
with pytest.raises(ValueError) as e:
rm_alias(TWV)
assert e.value == 'the module is not an alias'
assert str(e.value) == 'the module is not an alias'

create_alias('alias1', TWV)
with pytest.raises(ValueError) as e:
create_alias('alias1', TWV)
assert e.value == 'a module with the alias name already exists'
assert str(e.value) == 'a module with the alias name already exists'
with pytest.raises(ValueError) as e:
create_alias('alias1', 'os')
assert e.value == 'a module with the alias name already exists'
assert str(e.value) == 'a module with the alias name already exists'

rm_alias('alias1')
with pytest.raises(NoModuleError):
rm_alias('alias1')

os.symlink(str(ROOT_PATH / TWV), str(ROOT_PATH / 'alias1'))
os.symlink(str(ROOT_PATH / 'nonexistant'), str(ROOT_PATH / 'alias1'))
with pytest.raises(ValueError) as e:
create_alias('alias1', 'os')
assert e.value == 'the needed file system location already occupied'
assert str(e.value) == 'the needed file system location already occupied'
os.remove(str(ROOT_PATH / 'alias1'))

15 changes: 15 additions & 0 deletions tests/test_tb_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from traceback_with_variables.tb_alias import create_tb_alias, rm_tb_alias

from tests.test_utils import run_code, dummies_code, tb_reg


def test_create_and_rm_tb_alias(tmp_path, tb_reg):
tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run 1 without tb')

create_tb_alias()
tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run with tb')

rm_tb_alias()
tb_reg(run_code(tmp_path, [], ['import tb.a', dummies_code, 'f()'], [], True), 'run 2 without tb')
8 changes: 4 additions & 4 deletions traceback_with_variables/module_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def module_exists(name: str) -> bool:


def create_alias(alias: str, module_name: str) -> None:
if not name:
raise ValueError('alias must be non-empty')
if any(c not in VALID_CHARS for c in name):
if not alias:
raise ValueError('the alias must be non-empty')
if any(c not in VALID_CHARS for c in alias):
raise ValueError('the alias must have only ascii lowecases, digits and underscores')
if module_exists(alias):
raise ValueError('a module with the alias name already exists')
module_path = module_name_to_path(module_name)

try:
os.symlink(str(module_path), str(module_path.parent / name))
os.symlink(str(module_path), str(module_path.parent / alias))
except FileExistsError as e:
raise ValueError('the needed file system location already occupied')

Expand Down

0 comments on commit fe8c36a

Please sign in to comment.