Skip to content

Commit 2d8ee49

Browse files
fix: cin >>
1 parent 7aaaf22 commit 2d8ee49

1 file changed

Lines changed: 58 additions & 17 deletions

File tree

src/log21/Logger.py

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,31 +322,72 @@ def __rshift__(self, obj: List[Any]):
322322
"""A way of receiving input from the stdin.
323323
This operator is meant to make a std::cin-like operation possible in Python.
324324
325+
Usage examples:
326+
>>> import log21
327+
>>> cout = cin = log21.get_logger()
328+
>>>
329+
>>> # Example 1
330+
>>> # Get three inputs of type: str, str or None, and float
331+
>>> data = [str, None, float] # first name, last name and age
332+
>>> cout << "Please enter a first name, last name and age(separated by space): "
333+
Please enter a first name, last name and age(separated by space):
334+
>>> cin >> data;
335+
M 21
336+
>>> name = data[0] + (data[1] if data[1] is not None else '')
337+
>>> age = data[2]
338+
>>> cout << name << " is " << age << " years old." << log21.endl;
339+
M is 21.0 years old.
340+
>>>
341+
>>> # Example 2
342+
>>> # Get any number of inputs
343+
>>> data = []
344+
>>> cout << "Enter something: ";
345+
Enter something:
346+
>>> cin >> data;
347+
What ever man 1 2 3 !
348+
>>> cout << "Here are the items you chose: " << data << log21.endl;
349+
Here are the items you chose: ['What', 'ever', 'man', '1', '2', '3', '!']
350+
>>>
351+
>>> # Example 3
352+
>>> # Get two inputs of type int with defaults: 1280 and 720
353+
>>> data = [1280, 720]
354+
>>> cout << "Enter the width and the height: ";
355+
Enter the width and the height:
356+
>>> cin >> data;
357+
500
358+
359+
>>> cout << "Width: " << data[0] << " Height: " << data[1] << log21.endl;
360+
Width: 500 Height: 720
361+
>>>
362+
325363
:param obj: The object to redirect the output to.
326364
:return: The Logger object.
327365
"""
328366
n = len(obj) - 1
329367
if n >= 0:
330368
data = []
331369
while n >= 0:
332-
tmp = _sys.stdin.readline()[:-1].split(maxsplit=n)
333-
data.extend(tmp)
370+
tmp = _sys.stdin.readline()[:-1].split(' ', maxsplit=n)
371+
if tmp:
372+
data.extend(tmp)
373+
else:
374+
data.append('')
334375
n -= len(tmp)
335-
tmp = []
336-
for i, item in enumerate(data):
337-
if obj[i] is None:
338-
tmp.append(item)
339-
elif isinstance(obj[i], type):
340-
try:
341-
tmp.append(obj[i](item))
342-
except ValueError:
343-
tmp.append(obj[i]())
344-
else:
345-
try:
346-
tmp.append(obj[i].__class__(item))
347-
except ValueError:
348-
tmp.append(obj[i])
349-
obj[:] = tmp
376+
tmp = []
377+
for i, item in enumerate(data):
378+
if obj[i] is None:
379+
tmp.append(item or None)
380+
elif isinstance(obj[i], type):
381+
try:
382+
tmp.append(obj[i](item))
383+
except ValueError:
384+
tmp.append(obj[i]())
385+
else:
386+
try:
387+
tmp.append(obj[i].__class__(item))
388+
except ValueError:
389+
tmp.append(obj[i])
390+
obj[:] = tmp
350391
else:
351392
obj[:] = _sys.stdin.readline()[:-1].split()
352393

0 commit comments

Comments
 (0)