rgwfuncs 0.0.83__py3-none-any.whl → 0.0.85__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,63 +11,21 @@ from .algebra_lib import * # noqa: F401, F403, E402
11
11
  from .str_lib import * # noqa: F401, F403, E402
12
12
  from .docs_lib import * # noqa: F401, F403, E402
13
13
 
14
- # ANSI color escape codes
15
- BLUE = "\033[34m"
16
- WHITE = "\033[37m"
17
- RESET = "\033[0m"
18
-
19
- class BlueStdout:
20
- """
21
- A wrapper for sys.stdout that automatically prepends blue
22
- color codes on output, unless the output appears to be a prompt.
23
- """
24
- def __init__(self, wrapped):
25
- self.wrapped = wrapped
26
- self.at_line_start = True
27
-
28
- def write(self, s):
29
- # If the text exactly matches one of our prompt strings, write it as-is.
30
- if s == sys.ps1 or s == sys.ps2:
31
- self.wrapped.write(s)
32
- return
33
-
34
- # Process text, coloring new lines with blue.
35
- lines = s.split('\n')
36
- for i, line in enumerate(lines):
37
- if self.at_line_start and line:
38
- self.wrapped.write(BLUE + line)
39
- else:
40
- self.wrapped.write(line)
41
- if i < len(lines) - 1:
42
- self.wrapped.write('\n' + RESET)
43
- self.at_line_start = True
44
- else:
45
- self.at_line_start = (line == "")
46
-
47
- def flush(self):
48
- self.wrapped.flush()
49
-
50
- def isatty(self):
51
- return self.wrapped.isatty()
52
-
53
- def fileno(self):
54
- return self.wrapped.fileno()
55
-
56
- def __getattr__(self, attr):
57
- # Delegate any other attribute accesses to the wrapped stdout.
58
- return getattr(self.wrapped, attr)
59
-
60
14
  def interactive_shell(local_vars: Dict[str, Any]) -> None:
61
15
  """
62
- Launches an interactive prompt for inspecting and modifying local variables,
63
- making all methods in the rgwfuncs library available by default.
64
- Persists command history across sessions.
65
-
66
- Parameters:
67
- local_vars (dict): Dictionary of local variables to be available in the interactive shell.
16
+ Launch an interactive prompt for inspecting and modifying local variables,
17
+ with a custom blue output and white prompt. All helper definitions are nested within.
18
+
19
+ local_vars: dictionary with local variables to be available in the interactive shell.
68
20
  """
21
+
22
+ # ANSI color escape codes.
23
+ BLUE = "\033[94m"
24
+ WHITE = "\033[37m"
25
+ RESET = "\033[0m"
26
+
27
+ # Helper: setup readline for history persistence.
69
28
  def setup_readline() -> None:
70
- """Set up readline for history persistence."""
71
29
  HISTORY_FILE = os.path.expanduser("~/.rgwfuncs_shell_history")
72
30
  readline.set_history_length(1000)
73
31
  readline.parse_and_bind("tab: complete")
@@ -77,34 +35,83 @@ def interactive_shell(local_vars: Dict[str, Any]) -> None:
77
35
  except Exception as e:
78
36
  print(f"Warning: Could not load history file: {e}")
79
37
  atexit.register(readline.write_history_file, HISTORY_FILE)
80
-
38
+
39
+ # Nested BlueStdout: a wrapper for sys.stdout that prepends blue color on output.
40
+ class BlueStdout:
41
+ def __init__(self, wrapped):
42
+ self.wrapped = wrapped
43
+ self.at_line_start = True
44
+
45
+ def write(self, s):
46
+ # Do not interfere with the prompt strings.
47
+ if s == sys.ps1 or s == sys.ps2:
48
+ self.wrapped.write(s)
49
+ return
50
+ # Process output line by line.
51
+ lines = s.split('\n')
52
+ for i, line in enumerate(lines):
53
+ if self.at_line_start and line:
54
+ self.wrapped.write(BLUE + line)
55
+ else:
56
+ self.wrapped.write(line)
57
+ if i < len(lines) - 1:
58
+ self.wrapped.write('\n' + RESET)
59
+ self.at_line_start = True
60
+ else:
61
+ self.at_line_start = (line == "")
62
+
63
+ def flush(self):
64
+ self.wrapped.flush()
65
+
66
+ def isatty(self):
67
+ return self.wrapped.isatty()
68
+
69
+ def fileno(self):
70
+ return self.wrapped.fileno()
71
+
72
+ def __getattr__(self, attr):
73
+ return getattr(self.wrapped, attr)
74
+
75
+ # Nested ColorInteractiveConsole: a subclass of InteractiveConsole that temporarily
76
+ # restores the original stdout when reading input so that readline functions properly.
77
+ class ColorInteractiveConsole(code.InteractiveConsole):
78
+ def raw_input(self, prompt=""):
79
+ saved_stdout = sys.stdout
80
+ sys.stdout = sys.__stdout__
81
+ try:
82
+ line = input(prompt)
83
+ except EOFError:
84
+ raise
85
+ finally:
86
+ sys.stdout = saved_stdout
87
+ return line
88
+
89
+ # Ensure that the passed local_vars is a dictionary.
81
90
  if not isinstance(local_vars, dict):
82
91
  raise TypeError("local_vars must be a dictionary")
83
-
84
- # Set up readline history and completion.
92
+
93
+ # Setup readline.
85
94
  setup_readline()
86
-
87
- # Make imported functions available in the REPL.
95
+
96
+ # Make imported functions available in the REPL by merging globals.
88
97
  local_vars.update(globals())
89
-
90
- # Correctly wrap ANSI codes in the prompt strings.
91
- # The \001 and \002 around the color codes tell readline that those characters
92
- # have zero width.
98
+
99
+ # Wrap ANSI escape sequences in markers recognized by readline so that they do not count
100
+ # toward the visible prompt length.
93
101
  sys.ps1 = "\001" + WHITE + "\002" + ">>> " + "\001" + RESET + "\002"
94
102
  sys.ps2 = "\001" + WHITE + "\002" + "... " + "\001" + RESET + "\002"
95
-
96
- # Wrap sys.stdout to emit blue-colored output.
97
- sys.stdout = BlueStdout(sys.stdout)
98
-
99
- # Create an interactive console with the provided locals.
100
- console = code.InteractiveConsole(locals=local_vars)
101
-
102
- # Custom banner and exit message.
103
+
104
+ # Replace sys.stdout with our blue-printing wrapper.
105
+ sys.stdout = BlueStdout(sys.__stdout__)
106
+
107
+ # Instantiate our custom interactive console.
108
+ console = ColorInteractiveConsole(locals=local_vars)
109
+
103
110
  banner = ("Welcome to the rgwfuncs interactive shell.\n"
104
111
  "Use up/down arrows for command history.\n"
105
112
  "Type 'exit()' or Ctrl+D to quit.")
106
113
  exitmsg = "Goodbye."
107
-
114
+
108
115
  try:
109
116
  console.interact(banner=banner)
110
117
  finally:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.83
3
+ Version: 0.0.85
4
4
  Summary: A functional programming paradigm for mathematical modelling and data science
5
5
  Home-page: https://github.com/ryangerardwilson/rgwfunc
6
6
  Author: Ryan Gerard Wilson
@@ -2,11 +2,11 @@ rgwfuncs/__init__.py,sha256=LSn54Tlyskcb6Wab_wUpPLB6UGMe5LdrB3GU88mDEbU,1712
2
2
  rgwfuncs/algebra_lib.py,sha256=rKFITfpWfgdBswnbMUuS41XgndEt-jUVz2ObO_ik7eM,42234
3
3
  rgwfuncs/df_lib.py,sha256=r6T-MwyDq9NAPW1Xf6NzSy7ZFicIKdemR-UKu6TZt5g,71111
4
4
  rgwfuncs/docs_lib.py,sha256=y3wSAOPO3qsA4HZ7xAtW8HimM8w-c8hjcEzMRLJ96ao,1960
5
- rgwfuncs/interactive_shell_lib.py,sha256=41gZn25gds5Wlqf_AVvMe2SZRdTP1jjsLc1hrPYSocU,3708
5
+ rgwfuncs/interactive_shell_lib.py,sha256=zM4WrG425WK3MsPOfSgxvJxRJsUV177IYrSGvE-uAQE,4254
6
6
  rgwfuncs/str_lib.py,sha256=rtAdRlnSJIu3JhI-tA_A0wCiPK2m-zn5RoGpBxv_g-4,2228
7
- rgwfuncs-0.0.83.dist-info/LICENSE,sha256=jLvt20gcUZYB8UOvyBvyKQ1qhYYhD__qP7ZDx2lPFkU,1062
8
- rgwfuncs-0.0.83.dist-info/METADATA,sha256=mo8XbCW2vv8AYoNlWedCxLaYhMdtIch312ZehLWfUZM,60288
9
- rgwfuncs-0.0.83.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
- rgwfuncs-0.0.83.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
11
- rgwfuncs-0.0.83.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
12
- rgwfuncs-0.0.83.dist-info/RECORD,,
7
+ rgwfuncs-0.0.85.dist-info/LICENSE,sha256=jLvt20gcUZYB8UOvyBvyKQ1qhYYhD__qP7ZDx2lPFkU,1062
8
+ rgwfuncs-0.0.85.dist-info/METADATA,sha256=28YYKElMTP4Fm5TnxB8iU2F088nrhzQEg8YhM1XaoI4,60288
9
+ rgwfuncs-0.0.85.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
+ rgwfuncs-0.0.85.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
11
+ rgwfuncs-0.0.85.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
12
+ rgwfuncs-0.0.85.dist-info/RECORD,,