Lib.hx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package python;
  2. import python.lib.Types;
  3. typedef PySys = python.lib.Sys;
  4. typedef PyStringTools = python.lib.StringTools;
  5. class Lib {
  6. public static function print(v:Dynamic):Void {
  7. var str = Std.string(v);
  8. PySys.stdout.buffer.write( PyStringTools.encode(str, "utf-8"));
  9. PySys.stdout.flush();
  10. }
  11. public static function println(v:Dynamic):Void {
  12. var str = Std.string(v);
  13. PySys.stdout.buffer.write( PyStringTools.encode('$str\n', "utf-8"));
  14. PySys.stdout.flush();
  15. }
  16. public static function toPythonIterable <T>(it:Iterable<T>):python.lib.Types.NativeIterable<T> {
  17. return {
  18. __iter__ : function () {
  19. var it1 = it.iterator();
  20. var self:PyIterator<T> = null;
  21. self = new PyIterator({
  22. __next__ : function ():T {
  23. if (it1.hasNext()) {
  24. return it1.next();
  25. } else {
  26. throw new python.lib.Types.StopIteration();
  27. }
  28. },
  29. __iter__ : function () return self
  30. });
  31. return self;
  32. }
  33. }
  34. }
  35. public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T> {
  36. return new HaxeIterable(it);
  37. }
  38. public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T> {
  39. return new HaxeIterator(it);
  40. }
  41. }