xlwings-utils 0.0.7.post2__tar.gz → 0.0.7.post4__tar.gz

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.

Potentially problematic release.


This version of xlwings-utils might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xlwings_utils
3
- Version: 0.0.7.post2
3
+ Version: 0.0.7.post4
4
4
  Summary: xlwings_utils
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/xlwings_utils
@@ -123,26 +123,33 @@ In this case, only the really processed rows are copied to the sheet.
123
123
 
124
124
  The module has support for capturing stdout and -later- using showing the captured output on a sheet.
125
125
 
126
- To do that:
126
+ This is rather important as printing in xlwings lite to the UI pane is rather slow.
127
127
 
128
- ```makes it possible to capture stdout writes, which
129
- with xwu.capture_stdout():
130
- ...
131
- ```
132
- and then the captured output can be copied to the screen, like
128
+ In order to capture stdout output, use
133
129
 
134
- ```can then be copied to a worksheet in a later stage.
135
- sheet.range(4,5).value = xwu.captured_stdout_as_value()
136
- ```
137
130
 
138
- Clearing the captured stdout buffer can be done with `xwu.clear_captured_std_out`.
131
+ ```
132
+ with xwu.capture:
133
+ """
134
+ code with print statements
135
+ """
136
+ ```
139
137
 
140
- Normally, stdout will also be sent to the xlwings lite UI panel. This can be suppressed with
138
+ and then the captured output can be copied to a sheet, like
141
139
 
142
140
  ```
143
- with xwu.capture_stdout(include_print=False):
144
- ...
141
+ sheet.range(4,5).value = xwu.capture.value
145
142
  ```
143
+ Upon reading the value, the capture buffer will be emptied.
144
+
145
+ If you don't want the buffer to be emptied after accessing the value, use `xwu.capture.value_keep`.
146
+
147
+ The capture buffer can also be retrieved as a string with `xwu.capture.str` and `xwu.capture.str_keep`.
148
+
149
+ Clearing the captured stdout buffer can be done at any time with `xwu.capture.clear()`.
150
+
151
+ Normally, stdout will not be sent to the xlwings lite UI panel when captured with the `xwu.capture` context manager. However, if you specify `xwu.capture.include_print = True`, the output will be sent to the UI panel as well. Note that this setting remains active until a `xwu.capture.include_print = False` is issued.
152
+
146
153
 
147
154
  ## Contact info
148
155
 
@@ -109,26 +109,33 @@ In this case, only the really processed rows are copied to the sheet.
109
109
 
110
110
  The module has support for capturing stdout and -later- using showing the captured output on a sheet.
111
111
 
112
- To do that:
112
+ This is rather important as printing in xlwings lite to the UI pane is rather slow.
113
113
 
114
- ```makes it possible to capture stdout writes, which
115
- with xwu.capture_stdout():
116
- ...
117
- ```
118
- and then the captured output can be copied to the screen, like
114
+ In order to capture stdout output, use
119
115
 
120
- ```can then be copied to a worksheet in a later stage.
121
- sheet.range(4,5).value = xwu.captured_stdout_as_value()
122
- ```
123
116
 
124
- Clearing the captured stdout buffer can be done with `xwu.clear_captured_std_out`.
117
+ ```
118
+ with xwu.capture:
119
+ """
120
+ code with print statements
121
+ """
122
+ ```
125
123
 
126
- Normally, stdout will also be sent to the xlwings lite UI panel. This can be suppressed with
124
+ and then the captured output can be copied to a sheet, like
127
125
 
128
126
  ```
129
- with xwu.capture_stdout(include_print=False):
130
- ...
127
+ sheet.range(4,5).value = xwu.capture.value
131
128
  ```
129
+ Upon reading the value, the capture buffer will be emptied.
130
+
131
+ If you don't want the buffer to be emptied after accessing the value, use `xwu.capture.value_keep`.
132
+
133
+ The capture buffer can also be retrieved as a string with `xwu.capture.str` and `xwu.capture.str_keep`.
134
+
135
+ Clearing the captured stdout buffer can be done at any time with `xwu.capture.clear()`.
136
+
137
+ Normally, stdout will not be sent to the xlwings lite UI panel when captured with the `xwu.capture` context manager. However, if you specify `xwu.capture.include_print = True`, the output will be sent to the UI panel as well. Note that this setting remains active until a `xwu.capture.include_print = False` is issued.
138
+
132
139
 
133
140
  ## Contact info
134
141
 
@@ -10,7 +10,7 @@ authors = [
10
10
  { name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com" },
11
11
  ]
12
12
  description = "xlwings_utils"
13
- version = "0.0.7.post2"
13
+ version = "0.0.7.post4"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.9"
16
16
  dependencies = [
@@ -1,6 +1,7 @@
1
1
  import os
2
2
  import sys
3
3
  from pathlib import Path
4
+ import io
4
5
 
5
6
  if __name__ == "__main__": # to make the tests run without the pytest cli
6
7
  file_folder = os.path.dirname(__file__)
@@ -102,37 +103,46 @@ def test_raise():
102
103
  this_block[1, 7] = 1
103
104
 
104
105
 
105
- def test_capture_stdout(capsys):
106
+ def test_capture(capsys):
106
107
  print("abc")
107
108
  print("def")
108
109
  out, err = capsys.readouterr()
109
110
  assert out == "abc\ndef\n"
110
- assert xwu.captured_stdout_as_str() == ""
111
- assert xwu.captured_stdout_as_value() == []
111
+ assert xwu.capture.str_keep == ""
112
+ assert xwu.capture.value_keep == []
112
113
 
113
- xwu.capture_stdout(do_capture=True)
114
- print("abc")
115
- print("def")
114
+ with xwu.capture:
115
+ print("abc")
116
+ print("def")
116
117
  out, err = capsys.readouterr()
117
- assert out == "abc\ndef\n"
118
-
119
- assert xwu.captured_stdout_as_str(clear=False) == "abc\ndef\n"
120
- assert xwu.captured_stdout_as_value(clear=False) == [["abc"], ["def"]]
121
-
122
- # out, err = capsys.readouterr()
123
- # assert out == "abc\ndef\n"
124
-
125
- # xwu.clear_captured_stdout()
126
- # assert xwu.captured_stdout_as_str() == ""
127
- # with xwu.capture_stdout(include_print=False):
128
- # print("ghi")
129
- # print("jkl")
130
- # assert xwu.captured_stdout_as_str() == "ghi\njkl\n"
131
- # assert xwu.captured_stdout_as_value() == [["ghi"], ["jkl"]]
132
- # out, err = capsys.readouterr()
133
- # assert out == ""
134
-
135
-
118
+ assert out == ""
119
+ assert xwu.capture.str_keep == "abc\ndef\n"
120
+ assert xwu.capture.value_keep == [['abc'], ['def']]
121
+ assert xwu.capture.str == "abc\ndef\n"
122
+ assert xwu.capture.value == []
123
+
124
+ with xwu.capture:
125
+ print("abc")
126
+ print("def")
127
+ out, err = capsys.readouterr()
128
+ xwu.capture.clear()
129
+ assert xwu.capture.str_keep == ""
130
+
131
+ with xwu.capture:
132
+ print("abc")
133
+ print("def")
134
+ with xwu.capture:
135
+ print("ghi")
136
+ print("jkl")
137
+ out, err = capsys.readouterr()
138
+ assert out == ""
139
+ assert xwu.capture.str_keep == "abc\ndef\nghi\njkl\n"
140
+ assert xwu.capture.value_keep == [['abc'], ['def'], ['ghi'], ['jkl']]
141
+ assert xwu.capture.value == [['abc'], ['def'], ['ghi'], ['jkl']]
142
+ assert xwu.capture.value == []
143
+
144
+ # include_print is not testable with pytest
145
+
136
146
  if __name__ == "__main__":
137
147
  pytest.main(["-vv", "-s", "-x", __file__])
138
148
 
@@ -13,8 +13,6 @@ from pathlib import Path
13
13
  import os
14
14
  import sys
15
15
 
16
- _org_stdout = sys.stdout
17
-
18
16
  dbx = None
19
17
  Pythonista = sys.platform == "ios"
20
18
 
@@ -375,105 +373,91 @@ class block:
375
373
  return f"block({self.value})"
376
374
 
377
375
 
378
- def clear_captured_stdout(clear=True):
376
+
377
+ class _capture:
379
378
  """
380
- empties the captured stdout
379
+ specifies how to capture stdout
381
380
 
382
381
  Parameters
383
382
  ----------
384
- clear : bool
385
- if True (default), the capture buffer is emptied
386
-
387
- if False: no action
388
- """
389
- if clear:
390
- _buffer.clear()
391
-
392
-
393
- def captured_stdout_as_str(clear=True):
394
- """
395
- returns the captured stdout as a string
396
-
397
- Returns
398
- -------
399
- captured stdout : list
400
- each line is an element of the list
401
- """
402
- result = "".join(_buffer)
403
- clear_captured_stdout(clear)
404
- return result
405
-
383
+ include_print : bool
384
+ if True (default), the output is also printed out as normal
406
385
 
407
- def captured_stdout_as_value(clear=True):
408
- """
409
- returns the captured stdout as a list of lists
410
-
411
- Returns
412
- -------
413
- captured stdout : list of lists
414
- each line is an element of the list
386
+ if False, no output is printed
415
387
 
416
388
  Note
417
389
  ----
418
- This can be used directly to fill a xlwings range
419
- """
420
- return [[line] for line in captured_stdout_as_str(clear).splitlines()]
390
+ Use this function as a context manager, like ::
421
391
 
392
+ with capture_stdout():
393
+ ...
394
+ """
422
395
 
423
- class handle_capture_stdout:
424
396
  def __init__(self):
425
- pass
397
+ self.stdout = sys.stdout
398
+ self._include_print = False
399
+ self._buffer=[]
400
+
401
+ def __enter__(self):
402
+ sys.stdout = self
426
403
 
427
- def isatty(self):
428
- return False
404
+ def __exit__(self, exc_type, exc_value, tb):
405
+ sys.stdout = self.stdout
429
406
 
430
407
  def write(self, data):
431
- _buffer.append(data)
432
- if _do_print:
433
- _org_stdout.write(data)
408
+ self._buffer.append(data)
409
+ if self._include_print:
410
+ self.stdout.write(data)
434
411
 
435
412
  def flush(self):
436
- if _do_print:
437
- _org_stdout.flush()
438
- _buffer.append("\n")
439
-
440
-
441
- def reset():
442
- global _buffer
443
- sys.stdout=_org_stdout
444
- _buffer=[]
445
-
446
- reset()
447
-
448
- def capture_stdout(do_print=True, do_capture=True, clear=True):
449
- global _do_print
450
- """
451
- start capture stdout
452
-
453
- Parameters
454
- ----------
455
- do_print : bool
456
- if True (default), the output is (also) printed out as normal
413
+ if self._include_print:
414
+ self.stdout.flush()
415
+ self._buffer.append("\n")
416
+
457
417
 
458
- if False, no output is printed
418
+ @property
419
+ def value(self):
420
+ result=self.value_keep
421
+ self.clear()
422
+ return result
423
+
424
+
425
+ @property
426
+ def value_keep(self):
427
+ result= [[line] for line in self.str_keep.splitlines()]
428
+ return result
429
+
430
+ @property
431
+ def str(self):
432
+ result = self.str_keep
433
+ self._buffer.clear()
434
+ return result
435
+
436
+ @property
437
+ def str_keep(self):
438
+ result = "".join(self._buffer)
439
+ return result
440
+
441
+ def clear(self):
442
+ self._buffer.clear()
443
+
444
+
445
+ @property
446
+ def include_print(self):
447
+ return self._include_print
448
+
449
+ @include_print.setter
450
+ def include_print(self, value):
451
+ self._include_print=value
452
+
459
453
 
460
- do_capture : bool
461
- if True (default), the output is (also) captured to a buffer
462
454
 
463
- if False, no output is captured
455
+ def reset():
456
+ capture.include_print=False
457
+ capture.clear()
458
+
459
+ capture=_capture()
464
460
 
465
- clear : bool
466
- if True (default), the capture buffer is emptied
467
- """
468
- if not (do_print or do_capture):
469
- raise ValueError("at least one of do_print and do_capture should be True")
470
-
471
- if do_capture:
472
- if sys.stdout is _org_stdout:
473
- sys.stdout = handle_capture_stdout()
474
- _do_print=do_print
475
- else:
476
- sys.stdout=_org_stdout
477
461
 
478
462
  if __name__ == "__main__":
479
463
  ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xlwings_utils
3
- Version: 0.0.7.post2
3
+ Version: 0.0.7.post4
4
4
  Summary: xlwings_utils
5
5
  Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/salabim/xlwings_utils
@@ -123,26 +123,33 @@ In this case, only the really processed rows are copied to the sheet.
123
123
 
124
124
  The module has support for capturing stdout and -later- using showing the captured output on a sheet.
125
125
 
126
- To do that:
126
+ This is rather important as printing in xlwings lite to the UI pane is rather slow.
127
127
 
128
- ```makes it possible to capture stdout writes, which
129
- with xwu.capture_stdout():
130
- ...
131
- ```
132
- and then the captured output can be copied to the screen, like
128
+ In order to capture stdout output, use
133
129
 
134
- ```can then be copied to a worksheet in a later stage.
135
- sheet.range(4,5).value = xwu.captured_stdout_as_value()
136
- ```
137
130
 
138
- Clearing the captured stdout buffer can be done with `xwu.clear_captured_std_out`.
131
+ ```
132
+ with xwu.capture:
133
+ """
134
+ code with print statements
135
+ """
136
+ ```
139
137
 
140
- Normally, stdout will also be sent to the xlwings lite UI panel. This can be suppressed with
138
+ and then the captured output can be copied to a sheet, like
141
139
 
142
140
  ```
143
- with xwu.capture_stdout(include_print=False):
144
- ...
141
+ sheet.range(4,5).value = xwu.capture.value
145
142
  ```
143
+ Upon reading the value, the capture buffer will be emptied.
144
+
145
+ If you don't want the buffer to be emptied after accessing the value, use `xwu.capture.value_keep`.
146
+
147
+ The capture buffer can also be retrieved as a string with `xwu.capture.str` and `xwu.capture.str_keep`.
148
+
149
+ Clearing the captured stdout buffer can be done at any time with `xwu.capture.clear()`.
150
+
151
+ Normally, stdout will not be sent to the xlwings lite UI panel when captured with the `xwu.capture` context manager. However, if you specify `xwu.capture.include_print = True`, the output will be sent to the UI panel as well. Note that this setting remains active until a `xwu.capture.include_print = False` is issued.
152
+
146
153
 
147
154
  ## Contact info
148
155