-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonstartup.py
More file actions
37 lines (28 loc) · 935 Bytes
/
Copy pathpythonstartup.py
File metadata and controls
37 lines (28 loc) · 935 Bytes
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
"""Python startup script.
This script is executed on startup of the Python interpreter.
"""
import re
from pathlib import Path
from typing import Any, Generator, List
HOME = Path("~").expanduser()
try:
from rich import pretty, traceback
pretty.install()
traceback.install(show_locals=True)
except ImportError:
pass
def iter_findattr(
obj: Any, expr: str, include_magic: bool = False
) -> Generator[str, None, None]:
"""Iterate over attributes of an object, yielding names matching a regular
expression.
"""
attrs = dir(obj)
if not include_magic:
attrs = [a for a in attrs if not a.startswith("__")]
for attr in attrs:
if re.search(expr, attr) is not None:
yield attr
def findattr(obj: Any, expr: str, include_magic: bool = False) -> List[str]:
"""Locate an attribute by regular expression."""
return list(iter_findattr(obj, expr, include_magic))