Lib.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package python;
  23. import python.internal.AnonObject;
  24. import python.Dict;
  25. import python.NativeStringTools;
  26. typedef PySys = python.lib.Sys;
  27. /**
  28. Platform-specific Python Library. Provides some platform-specific functions
  29. for the Python target, such as conversion from Haxe types to native types
  30. and vice-versa.
  31. **/
  32. class Lib {
  33. static public var __name__(get, never):String;
  34. static inline function get___name__():String return python.Syntax.code('__name__');
  35. /**
  36. Print the specified value on the default output.
  37. **/
  38. public static inline function print(v:Dynamic):Void {
  39. printString(Std.string(v));
  40. }
  41. private static function printString (str:String):Void {
  42. PySys.stdout.buffer.write( NativeStringTools.encode(str, "utf-8"));
  43. PySys.stdout.flush();
  44. }
  45. /**
  46. Print the specified value on the default output followed by a newline character.
  47. **/
  48. public static inline function println(v:Dynamic):Void {
  49. var str = Std.string(v);
  50. printString('$str\n');
  51. }
  52. /**
  53. Returns an anonymous Object which holds the same data as the Dictionary `v`.
  54. **/
  55. public static function dictToAnon (v:Dict<String, Dynamic>):Dynamic {
  56. return new AnonObject(v.copy());
  57. }
  58. /**
  59. Returns a flat copy of the underlying Dictionary of `o`.
  60. **/
  61. @:access(python.Boot.isAnonObject)
  62. public static function anonToDict (o:{}):Dict<String, Dynamic> {
  63. return if (Boot.isAnonObject(o))
  64. {
  65. (Syntax.field(o, "__dict__"):Dict<String,Dynamic>).copy();
  66. }
  67. else null;
  68. }
  69. /**
  70. Returns the underlying Dictionary of the anonymous object `o`.
  71. Modifications to this dictionary are reflected in the anonymous Object too.
  72. **/
  73. @:access(python.Boot.isAnonObject)
  74. public static function anonAsDict (o:{}):Dict<String, Dynamic> {
  75. return if (Boot.isAnonObject(o))
  76. {
  77. (Syntax.field(o, "__dict__"):Dict<String,Dynamic>);
  78. }
  79. else null;
  80. }
  81. /**
  82. Returns the Dictionary `d` as an anonymous Object.
  83. Modifications to the object are reflected in the Dictionary too.
  84. **/
  85. public static inline function dictAsAnon (d:Dict<String, Dynamic>):Dynamic {
  86. return new AnonObject(d);
  87. }
  88. /**
  89. Return Python native iterable from Haxe iterable.
  90. **/
  91. public static function toPythonIterable <T>(it:Iterable<T>):python.NativeIterable<T> {
  92. return {
  93. __iter__ : function () {
  94. var it1 = it.iterator();
  95. var self:NativeIterator<T> = null;
  96. self = new NativeIterator({
  97. __next__ : function ():T {
  98. if (it1.hasNext()) {
  99. return it1.next();
  100. } else {
  101. throw new python.Exceptions.StopIteration();
  102. }
  103. },
  104. __iter__ : function () return self
  105. });
  106. return self;
  107. }
  108. }
  109. }
  110. /**
  111. Return Haxe iterable from Python native iterable.
  112. **/
  113. public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T> {
  114. return new HaxeIterable(it);
  115. }
  116. /**
  117. Return Haxe iterator instance from Python native iterable.
  118. **/
  119. public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T> {
  120. return new HaxeIterator(it);
  121. }
  122. }