-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Any, NamedTuple, Optional | ||
|
||
|
||
class REPLLocal(NamedTuple): | ||
var: Any | ||
name: str | ||
desc: str | ||
|
||
|
||
class REPLContext: | ||
def __init__(self): | ||
self._locals: set[REPLLocal] = set() | ||
|
||
def add_local( | ||
self, | ||
var: Any, | ||
name: Optional[str] = None, | ||
desc: Optional[str] = None, | ||
): | ||
if name is None: | ||
name = var.__name__ | ||
|
||
if desc is None: | ||
desc = var.__doc__ | ||
|
||
desc = self._truncate(desc) | ||
|
||
self._locals.add(REPLLocal(var, name, desc)) | ||
|
||
def __iter__(self): | ||
return iter(self._locals) | ||
|
||
@staticmethod | ||
def _truncate(s: str, limit: int = 40): | ||
return s[:limit] + "..." if len(s) > limit else s |