package python; import python.lib.Types; class HaxeIterable { var x :NativeIterable; public inline function new (x:NativeIterable) { this.x = x; } public inline function iterator ():HaxeIterator return new HaxeIterator(x.__iter__()); } class HaxeIterator { var it :NativeIterator; var x:Null = null; var has = false; var checked = false; public function new (it:NativeIterator) { this.it = it; } public inline function next ():T { checked = false; return x; } public function hasNext ():Bool { if (checked) { return has; } else { try { x = it.__next__(); has = true; } catch (s:StopIteration) { has = false; x = null; } checked = true; return has; } } } @:preCode("import sys as _hx_sys") class Lib { public static function print(v:Dynamic):Void { var str = Std.string(v); untyped __python__('_hx_sys.stdout.buffer.write(("%s"%str).encode(\'utf-8\'))'); untyped __python__('_hx_sys.stdout.flush()'); } public static function println(v:Dynamic):Void { var str = Std.string(v); untyped __python__('_hx_sys.stdout.buffer.write(("%s\\n"%str).encode(\'utf-8\'))'); untyped __python__('_hx_sys.stdout.flush()'); } public static function toPythonIterable (it:Iterable):python.lib.Types.NativeIterable { return { __iter__ : function () { var it1 = it.iterator(); var self:PyIterator = null; self = new PyIterator({ __next__ : function ():T { if (it1.hasNext()) { return it1.next(); } else { throw new python.lib.Types.StopIteration(); } }, __iter__ : function () return self }); return self; } } } public static inline function toHaxeIterable (it:NativeIterable):HaxeIterable { return new HaxeIterable(it); } public static inline function toHaxeIterator (it:NativeIterator):HaxeIterator { return new HaxeIterator(it); } }