Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change input overload to return input var #60

Closed
Almenon opened this issue Dec 18, 2018 · 1 comment
Closed

change input overload to return input var #60

Almenon opened this issue Dec 18, 2018 · 1 comment
Assignees
Milestone

Comments

@Almenon
Copy link
Owner

Almenon commented Dec 18, 2018

areplInputIterator = None

def input(prompt=None):
    global areplInputIterator

    if prompt is not None: print(prompt)
    try:
        if areplInputIterator is not None:
            try:
                return next(areplInputIterator)
            except StopIteration:
                raise StopIteration("there is no more input")
            return next(areplInputIterator)
        else:
            if type(stdin) is str:
                areplInputIterator = iter([line for line in stdin.split()])
            elif callable(stdin):
                return next(stdin())
            else:
                areplInputIterator = iter([line for line in stdin])

            return next(areplInputIterator)
    except NameError:
        print("AREPL requires stdin to be hardcoded, like so: stdin = 'hello world'; print(input())")

stdin = "fe\nhey"
x=input()
y = input()
#z = input()
@Almenon Almenon self-assigned this Dec 18, 2018
@Almenon Almenon added this to the 1.0.5 milestone Dec 18, 2018
@Almenon
Copy link
Owner Author

Almenon commented Dec 29, 2018

fixed a bug where I did next(stdin()) instead of saving the iterator
Made StopIteration more user friendly
got rid of useless line
Added test case for generator
changed naming of mock stdin to match python naming convention

areplInputIterator = None

def input(prompt=None):
    global areplInputIterator

    if prompt is not None: print(prompt)
    try:
        if areplInputIterator is not None:
            try:
                return next(areplInputIterator)
            except StopIteration:
                # give simple user-friendly error, we dont want users worry about error in arepl source code
                raise StopIteration("There is no more input") from None
        else:
            if type(standard_input) is str:
                areplInputIterator = iter([line for line in standard_input.split()])
            elif callable(standard_input):
                areplInputIterator = standard_input()
            else:
                areplInputIterator = iter([line for line in standard_input])

            return next(areplInputIterator)
    except NameError:
        print("AREPL requires standard_input to be hardcoded, like so: standard_input = 'hello world'; print(input())")

# user can define standard input with three ways:
def standard_input():
    yield 'hello'
    yield 'world'
standard_input = ["hello", "world"]
standard_input = "hello\nworld"

x=input() # should be hello
y=input() # should be world
z=input() # should result in StopIteration error saying no more input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant