-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module PyCall | ||
class IterableWrapper | ||
include Enumerable | ||
|
||
def initialize(obj) | ||
@obj = check_iterable(obj) | ||
end | ||
|
||
private def check_iterable(obj) | ||
unless PyCall.hasattr?(obj, :__iter__) | ||
raise ArgumentError, "%p object is not iterable" % obj | ||
end | ||
obj | ||
end | ||
|
||
def each | ||
return enum_for(__method__) unless block_given? | ||
iter = @obj.__iter__() | ||
while true | ||
begin | ||
yield iter.__next__() | ||
rescue PyCall::PyError => err | ||
if err.type == PyCall.builtins.StopIteration | ||
break | ||
else | ||
raise err | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
require "pycall" | ||
require "pathname" | ||
require "test-unit" | ||
|
||
test_dir = Pathname.new(__dir__) | ||
python_dir = test_dir + "python" | ||
|
||
PyCall.sys.path.append(python_dir.to_s) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class IntGenerator(object): | ||
def __init__(self, start, stop): | ||
self.start = start | ||
self.stop = stop | ||
self.current = None | ||
|
||
def __iter__(self): | ||
self.current = self.start | ||
return self | ||
|
||
def __next__(self): | ||
if self.current == self.stop: | ||
raise StopIteration() | ||
value = self.current | ||
self.current += 1 | ||
return value | ||
|
||
if __name__ == "__main__": | ||
print(list(IntGenerator(105, 115))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters