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

Generator support in ExternalSource #1832

Merged
merged 3 commits into from
Mar 23, 2020

Conversation

mzient
Copy link
Contributor

@mzient mzient commented Mar 23, 2020

Why we need this PR?

Pick one, remove the rest

  • It adds new feature needed because apparently some people use generators

What happened in this PR?

Fill relevant points, put NA otherwise. Replace anything inside []

  • What solution was applied:
    • Added special handling of generator functions as sources - they will be re-invoked when cycling
    • Added specific test that the iterator is not a generator when cycle is attempted - the error message points to a possible solution
    • Updated documentation for cycle and source
  • Affected modules and functionalities:
    • external source python module
  • Key points relevant for the review:
    • look at documentation and error messages, if they are clear enough
    • _get_callback_from_source - see if there's some use case not handled or badly handled
  • Validation and testing:
    • added test with a non-cycled generator and a cycled generator function
  • Documentation (including examples):
    • autodocs

JIRA TASK: N/A

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@mzient mzient requested a review from a team March 23, 2020 12:50
@mzient
Copy link
Contributor Author

mzient commented Mar 23, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1206920]: BUILD STARTED

Comment on lines 162 to 166
If `True`, the source will be wrapped. Otherwise, StopIteration error wil be raised
when end of data is reached. This flag requires that `source` is either a collection, i.e. an
iteratble object where ``iter(source)`` will return a fresh iterator on each call or a
generator function. In the latter case, the generator function will be called again when more
data is requried than was yielded by the function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If `True`, the source will be wrapped. Otherwise, StopIteration error wil be raised
when end of data is reached. This flag requires that `source` is either a collection, i.e. an
iteratble object where ``iter(source)`` will return a fresh iterator on each call or a
generator function. In the latter case, the generator function will be called again when more
data is requried than was yielded by the function.
If ``True``, the source will be wrapped. Otherwise, ``StopIteration`` will be raised
when end of data is reached. This flag requires that ``source`` is either a collection, i.e. an
iterable object where ``iter(source)`` will return a fresh iterator on each call, or a
generator function. In the latter case, the generator function will be called again when more
data is requested than was yielded by the function.

My suggestions:

  1. I'm not sure if we should call StopIteration "error", since it's regular python flow-control mechanism
  2. typo in wil
  3. typo in iteratble
  4. requried -> requested
  5. I'd wrap argument names etc. in double-back-ticks. It renders better, IMHO

if cycle is not None:
raise ValueError("The argument `cycle` can only be specified if `source` is iterable")
if not callable(source):
raise TypeError("Source must be iterable or callable")
raise TypeError("Source must be callable, iterable or a perameterless generator function")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise TypeError("Source must be callable, iterable or a perameterless generator function")
raise TypeError("Source must be callable, iterable or a parameterless generator function")

Copy link
Member

@szalpal szalpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some typos found, other than that LGTM.
I guess that using functools.partial we might be able to let the generator function have some arguments (prolly another argument to the op might be neccesary there), but that's for consideration and another PR

def _is_generator_function(x):
"""Checks whether x is a generator function or a callable object
where __call__ is a generator function"""
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we import it globally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can.

def _get_callback_from_source(source, cycle):
iterable = False
if source is not None:
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

pipe.set_outputs(fn.external_source(gen, cycle = True))
pipe.build()

for cycle in range(3):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a negative test for a generator and cycle set to True.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
@mzient
Copy link
Contributor Author

mzient commented Mar 23, 2020

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1207039]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1207088]: BUILD STARTED

@mzient mzient changed the title External source generator Generator support in ExternalSource Mar 23, 2020
@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1207088]: BUILD PASSED

@mzient mzient merged commit 3cddfa6 into NVIDIA:master Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants