Lib.hx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package python;
  2. import python.lib.Dict;
  3. import python.NativeStringTools;
  4. typedef PySys = python.lib.Sys;
  5. class Lib {
  6. public static function print(v:Dynamic):Void {
  7. var str = Std.string(v);
  8. PySys.stdout.buffer.write( NativeStringTools.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( NativeStringTools.encode('$str\n', "utf-8"));
  14. PySys.stdout.flush();
  15. }
  16. public static function dictToAnon (v:Dict<String, Dynamic>):Dynamic
  17. {
  18. var o = {};
  19. Syntax.assign(Syntax.field(o, "__dict__"), v);
  20. return o;
  21. }
  22. @:access(python.Boot) public static function anonToDict (o:{}):Dynamic
  23. {
  24. return if (python.Boot.isAnonObject(o))
  25. {
  26. Syntax.field(o, "__dict__");
  27. }
  28. else null;
  29. }
  30. public static function toPythonIterable <T>(it:Iterable<T>):python.NativeIterable<T> {
  31. return {
  32. __iter__ : function () {
  33. var it1 = it.iterator();
  34. var self:NativeIterator<T> = null;
  35. self = new NativeIterator({
  36. __next__ : function ():T {
  37. if (it1.hasNext()) {
  38. return it1.next();
  39. } else {
  40. throw new python.lib.Exceptions.StopIteration();
  41. }
  42. },
  43. __iter__ : function () return self
  44. });
  45. return self;
  46. }
  47. }
  48. }
  49. public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T> {
  50. return new HaxeIterable(it);
  51. }
  52. public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T> {
  53. return new HaxeIterator(it);
  54. }
  55. }