2
0

Lib.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C)2005-2015 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. class Lib {
  28. public static function print(v:Dynamic):Void {
  29. var str = Std.string(v);
  30. PySys.stdout.buffer.write( NativeStringTools.encode(str, "utf-8"));
  31. PySys.stdout.flush();
  32. }
  33. public static function println(v:Dynamic):Void {
  34. var str = Std.string(v);
  35. PySys.stdout.buffer.write( NativeStringTools.encode('$str\n', "utf-8"));
  36. PySys.stdout.flush();
  37. }
  38. /**
  39. Returns an anonymous Object which holds the same data as the Dictionary `v`.
  40. **/
  41. public static function dictToAnon (v:Dict<String, Dynamic>):Dynamic {
  42. return new AnonObject(v.copy());
  43. }
  44. /**
  45. Returns a flat copy of the underlying Dictionary of `o`.
  46. **/
  47. @:access(python.Boot.isAnonObject)
  48. public static function anonToDict (o:{}):Dict<String, Dynamic> {
  49. return if (Boot.isAnonObject(o))
  50. {
  51. (Syntax.field(o, "__dict__"):Dict<String,Dynamic>).copy();
  52. }
  53. else null;
  54. }
  55. /**
  56. Returns the underlying Dictionary of the anonymous object `o`.
  57. Modifications to this dictionary are reflected in the anonymous Object too.
  58. **/
  59. @:access(python.Boot.isAnonObject)
  60. public static function anonAsDict (o:{}):Dict<String, Dynamic> {
  61. return if (Boot.isAnonObject(o))
  62. {
  63. (Syntax.field(o, "__dict__"):Dict<String,Dynamic>);
  64. }
  65. else null;
  66. }
  67. /**
  68. Returns the Dictionary `d` as an anonymous Object.
  69. Modifications to the object are reflected in the Dictionary too.
  70. **/
  71. public static function dictAsAnon (d:Dict<String, Dynamic>):Dynamic {
  72. return new AnonObject(d);
  73. }
  74. public static function toPythonIterable <T>(it:Iterable<T>):python.NativeIterable<T> {
  75. return {
  76. __iter__ : function () {
  77. var it1 = it.iterator();
  78. var self:NativeIterator<T> = null;
  79. self = new NativeIterator({
  80. __next__ : function ():T {
  81. if (it1.hasNext()) {
  82. return it1.next();
  83. } else {
  84. throw new python.Exceptions.StopIteration();
  85. }
  86. },
  87. __iter__ : function () return self
  88. });
  89. return self;
  90. }
  91. }
  92. }
  93. public static inline function toHaxeIterable <T>(it:NativeIterable<T>):HaxeIterable<T> {
  94. return new HaxeIterable(it);
  95. }
  96. public static inline function toHaxeIterator <T>(it:NativeIterator<T>):HaxeIterator<T> {
  97. return new HaxeIterator(it);
  98. }
  99. }