Types.hx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. package python.lib;
  2. import python.lib.Builtin;
  3. import python.lib.io.IOBase;
  4. abstract Choice <A,B>(Dynamic) {
  5. @:from public static inline function fromA <A,B>(x:A):Choice<A,B> return cast x;
  6. @:from public static inline function fromB <A,B>(x:B):Choice<A,B> return cast x;
  7. }
  8. abstract KwArgs (Dict<String, Dynamic>)
  9. {
  10. inline function new (d:Dict<String, Dynamic>) this = d;
  11. @:to inline function toDict ():Dict<String, Dynamic>
  12. {
  13. return this;
  14. }
  15. @:from static inline function fromDict (d:Dict<String, Dynamic>):KwArgs
  16. {
  17. return new KwArgs(d);
  18. }
  19. public function get <V>(key:String, def:V):V
  20. {
  21. return this.get(key, def);
  22. }
  23. }
  24. abstract VarArgs (Array<Dynamic>)
  25. {
  26. inline function new (d:Array<Dynamic>) this = d;
  27. @:to inline function toArray ():Array<Dynamic>
  28. {
  29. return this;
  30. }
  31. @:from static inline function fromArray (d:Array<Dynamic>):VarArgs
  32. {
  33. return new VarArgs(d);
  34. }
  35. }
  36. extern class ByteArray implements ArrayAccess<Int> {
  37. public var length(get, null):Int;
  38. public inline function get_length ():Int {
  39. return Builtin.len(this);
  40. }
  41. public inline function get(i:Int):Int {
  42. return python.Syntax.arrayAccess(this, i);
  43. }
  44. public inline function set(i:Int,v:Int):Void {
  45. this.__setitem__(i,v);
  46. }
  47. public function __setitem__(i:Int,v:Int):Void;
  48. public function decode(encoding:String="utf-8", errors:String="strict"):String;
  49. }
  50. extern class Bytes extends ByteArray {
  51. //public function decode(encoding:String="utf-8", errors:String="strict"):String;
  52. static function __init__ ():Void
  53. {
  54. Syntax.importFromAs("builtins", "bytes", "python.lib.Bytes");
  55. }
  56. }
  57. typedef Variant<A,B> = Dynamic;
  58. typedef Variant3<A,B,C> = Dynamic;
  59. typedef Variant4<A,B,C,D> = Dynamic;
  60. abstract PyIterator <T>(NativeIterator<T>) to NativeIterator<T> to PyIterable<T> {
  61. public inline function new (p:NativeIterator<T>) this = p;
  62. @:to public static inline function toHaxeIterator <T>(p:NativeIterator<T>):HaxeIterator<T> return new HaxeIterator(p);
  63. @:to public static inline function toPyIterable <T>(p:NativeIterator<T>):PyIterable<T> return p;
  64. public function getNativeIterator <T>():NativeIterator<T> return this;
  65. }
  66. abstract PyIterable <T>(NativeIterable<T>) to NativeIterable<T> from NativeIterable<T> {
  67. @:to public static inline function toHaxeIterable <T>(p:NativeIterable<T>):HaxeIterable<T> return new HaxeIterable(p);
  68. //@:from public static inline function fromArray <T>(p:Array<T>):PyIterable<T> return cast p;
  69. public inline function iterator <T>() return IterHelper.iterableToIterator(this);
  70. public function getNativeIterable <T>():NativeIterable<T> return this;
  71. public function getNativeIterator <T>():NativeIterator<T> return this.__iter__();
  72. }
  73. class IterHelper {
  74. public static inline function iterableToIterator <T>(it:PyIterable<T>):Iterator<T>
  75. {
  76. return it.toHaxeIterable().iterator();
  77. }
  78. }
  79. typedef NativeIterator<T> = {
  80. function __next__ ():T;
  81. function __iter__ ():PyIterator<T>;
  82. }
  83. typedef NativeIterable<T> = {
  84. function __iter__():PyIterator<T>;
  85. }
  86. typedef List<T> = Array<T>;
  87. typedef Hashable = {
  88. public function __hash__():Int;
  89. }
  90. typedef Equal = {
  91. public function __eq__(other:Dynamic):Int;
  92. }
  93. typedef Comparable = {
  94. public function __cmp__(other:Dynamic):Int;
  95. }
  96. typedef FileObject = IOBase;
  97. extern class FileDescriptor {
  98. }
  99. //typedef DictKey<T> = {
  100. // function __hash__():Int;
  101. // function __eq__(other:Dynamic):Int;
  102. // function __cmp__(other:Dynamic):Int;
  103. //}
  104. //@:native("set")
  105. extern class Set <T>
  106. {
  107. @:overload(function (?array:Array<T>):Void {})
  108. public function new (?iterable:python.lib.Types.PyIterable<T>):Void;
  109. public inline function length ():Int
  110. {
  111. return python.lib.Builtin.len(this);
  112. }
  113. public inline function has (v:T):Bool
  114. {
  115. return python.Syntax.isIn(v, this);
  116. }
  117. public inline function minus (other:Set<T>):Set<T>
  118. {
  119. return python.Syntax.binop(this, "-", other);
  120. }
  121. public inline function plus (other:Set<T>):Set<T>
  122. {
  123. return python.Syntax.binop(this, "+", other);
  124. }
  125. static function __init__ ():Void
  126. {
  127. Syntax.importFromAs("builtins", "set", "python.lib.Set");
  128. }
  129. function __iter__ ():PyIterator<T>;
  130. public inline function iterator ():Iterator<T>
  131. {
  132. return __iter__();
  133. }
  134. }
  135. extern class DictView<T> {
  136. public inline function iter ():PyIterator<T>
  137. {
  138. return Builtin.iter(this);
  139. }
  140. public inline function length ():Int
  141. {
  142. return Builtin.len(this);
  143. }
  144. public inline function iterator ():Iterator<T>
  145. {
  146. return iter();
  147. }
  148. }
  149. //@:native("dict")
  150. extern class Dict <K, V>
  151. {
  152. public function new ():Void;
  153. public inline function length ():Int
  154. {
  155. return python.lib.Builtin.len(this);
  156. }
  157. public inline function hasKey (k:K):Bool {
  158. return DictImpl.hasKey(this,k);
  159. }
  160. public function clear ():Void;
  161. public function copy ():Dict<K,V>;
  162. public function get (key:K, def:V):V;
  163. public function update (d:Dict<K,V>):Void;
  164. public function keys ():DictView<K>;
  165. public function values ():DictView<V>;
  166. public function items ():DictView<Tup2<K,V>>;
  167. public static inline function fromObject (x:{}):Dict<String,Dynamic> {
  168. return DictImpl.fromObject(x);
  169. }
  170. public inline function set (key:K, val:V):Void {
  171. DictImpl.set(this, key, val);
  172. }
  173. public inline function remove (key:K):Void
  174. {
  175. DictImpl.remove(this, key);
  176. }
  177. public inline function iterator ():Iterator<V>
  178. {
  179. return values().iter();
  180. }
  181. public function __iter__():PyIterator<K>;
  182. static function __init__ ():Void
  183. {
  184. Syntax.importFromAs("builtins", "dict", "python.lib.Dict");
  185. }
  186. }
  187. class DictImpl {
  188. public static inline function fromObject (x:{}) {
  189. var d = new Dict();
  190. for (f in Reflect.fields(x)) {
  191. d.set(f, Reflect.field(x,f));
  192. }
  193. return d;
  194. }
  195. public static inline function hasKey <X>(d:Dict<X, Dynamic>, key:X) {
  196. return python.Syntax.isIn(key, d);
  197. }
  198. public static inline function remove <X>(d:Dict<X, Dynamic>, key:X) {
  199. python.Syntax.delete(python.Syntax.arrayAccess(d, key));
  200. }
  201. public static inline function set <K,V>(d:Dict<K, V>, key:K, val:V) {
  202. python.Syntax.arraySet(d, key, val);
  203. }
  204. }
  205. extern class Tuple<X> implements ArrayAccess<X> {
  206. public static inline function empty<X>():Tuple<X> {
  207. return Builtin.tuple();
  208. }
  209. public static inline function fromArray<X>(a:Array<X>):Tuple<X> {
  210. return Builtin.tuple(a);
  211. }
  212. public var length(get_length, null):Int;
  213. inline function get_length():Int {
  214. return Builtin.len(this);
  215. }
  216. public inline function at (i:Int):X {
  217. return python.Syntax.arrayAccess(this, i);
  218. }
  219. public inline function toArray ():Array<X>
  220. {
  221. return Builtin.list(this);
  222. }
  223. }
  224. extern class Tup2 <A,B> extends Tuple<Dynamic>
  225. {
  226. public static inline function create <A,B>(a:A, b:B):Tup2<A,B> return python.Syntax.tuple(a,b);
  227. public var _1(get, null):A;
  228. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  229. public var _2(get, null):B;
  230. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  231. }
  232. extern class Tup3 <A,B,C> extends Tuple<Dynamic>
  233. {
  234. public static inline function create <A,B,C>(a:A, b:B,c:C):Tup3<A,B,C> return python.Syntax.tuple(a,b,c);
  235. public var _1(get, null):A;
  236. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  237. public var _2(get, null):B;
  238. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  239. public var _3(get, null):C;
  240. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  241. }
  242. extern class Tup4 <A,B,C,D> extends Tuple<Dynamic>
  243. {
  244. public static inline function create <A,B,C,D>(a:A, b:B,c:C,d:D):Tup4<A,B,C,D> return python.Syntax.tuple(a,b,c,d);
  245. public var _1(get, null):A;
  246. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  247. public var _2(get, null):B;
  248. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  249. public var _3(get, null):C;
  250. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  251. public var _4(get, null):D;
  252. public inline function get__4():D return python.Syntax.arrayAccess(this, 3);
  253. }
  254. extern class Tup5 <A,B,C,D,E> extends Tuple<Dynamic>
  255. {
  256. public static inline function create <A,B,C,D,E>(a:A, b:B,c:C,d:D,e:E):Tup5<A,B,C,D,E> return python.Syntax.tuple(a,b,c,d,e);
  257. public var _1(get, null):A;
  258. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  259. public var _2(get, null):B;
  260. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  261. public var _3(get, null):C;
  262. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  263. public var _4(get, null):D;
  264. public inline function get__4():D return python.Syntax.arrayAccess(this, 3);
  265. public var _5(get, null):E;
  266. public inline function get__5():E return python.Syntax.arrayAccess(this, 4);
  267. }
  268. @:native("BaseException")
  269. extern class BaseException
  270. {
  271. public function new (msg:String):Void;
  272. }
  273. @:native("BufferError")
  274. extern class BufferError extends BaseException
  275. {
  276. }
  277. @:native("GeneratorExit")
  278. extern class GeneratorExit extends BaseException
  279. {
  280. }
  281. @:native("KeyboardInterrupt")
  282. extern class KeyboardInterrupt extends BaseException
  283. {
  284. }
  285. @:native("Exception")
  286. extern class Exception extends BaseException
  287. {
  288. }
  289. @:native("SyntaxError")
  290. extern class SyntaxError extends Exception
  291. {
  292. }
  293. @:native("StopIteration")
  294. extern class StopIteration extends Exception
  295. {
  296. public function new (?message:String);
  297. }
  298. @:native("RuntimeError")
  299. extern class RuntimeError extends Exception
  300. {
  301. }
  302. @:native("NotImplementedError")
  303. extern class NotImplementedError extends RuntimeError
  304. {
  305. }
  306. @:native("IndentationError")
  307. extern class IndentationError extends SyntaxError
  308. {
  309. }
  310. @:native("EnvironmentError")
  311. extern class EnvironmentError extends Exception
  312. {
  313. }
  314. @:native("OSError")
  315. extern class OSError extends EnvironmentError
  316. {
  317. }
  318. @:native("BlockingIOError")
  319. extern class BlockingIOError extends OSError
  320. {
  321. }
  322. @:native("ChildProcessError")
  323. extern class ChildProcessError extends OSError
  324. {
  325. }
  326. @:native("ConnectionError")
  327. extern class ConnectionError extends OSError
  328. {
  329. }
  330. @:native("BrokenPipeError")
  331. extern class BrokenPipeError extends ConnectionError
  332. {
  333. }
  334. @:native("ConnectionAbortedError")
  335. extern class ConnectionAbortedError extends ConnectionError
  336. {
  337. }
  338. @:native("ConnectionRefusedError")
  339. extern class ConnectionRefusedError extends ConnectionError
  340. {
  341. }
  342. @:native("ConnectionResetError")
  343. extern class ConnectionResetError extends ConnectionError
  344. {
  345. }
  346. @:native("FileExistsError")
  347. extern class FileExistsError extends OSError
  348. {
  349. }
  350. @:native("FileNotFoundError")
  351. extern class FileNotFoundError extends OSError
  352. {
  353. }
  354. @:native("InterruptedError")
  355. extern class InterruptedError extends OSError
  356. {
  357. }
  358. @:native("IsADirectoryError")
  359. extern class IsADirectoryError extends OSError
  360. {
  361. }
  362. @:native("NotADirectoryError")
  363. extern class NotADirectoryError extends OSError
  364. {
  365. }
  366. @:native("PermissionError")
  367. extern class PermissionError extends OSError
  368. {
  369. }
  370. @:native("ProcessLookupError")
  371. extern class ProcessLookupError extends OSError
  372. {
  373. }
  374. @:native("TimeoutError")
  375. extern class TimeoutError extends OSError
  376. {
  377. }
  378. @:native("NameError")
  379. extern class NameError extends Exception
  380. {
  381. }
  382. @:native("UnboundLocalError")
  383. extern class UnboundLocalError extends NameError
  384. {
  385. }
  386. @:native("MemoryError")
  387. extern class MemoryError extends Exception
  388. {
  389. }
  390. @:native("AssertionError")
  391. extern class AssertionError extends Exception
  392. {
  393. }
  394. @:native("AttributeError")
  395. extern class AttributeError extends Exception
  396. {
  397. }
  398. @:native("EOFError")
  399. extern class EOFError extends Exception
  400. {
  401. }
  402. @:native("ArithmeticError")
  403. extern class ArithmeticError extends Exception
  404. {
  405. }
  406. @:native("FloatingPointError")
  407. extern class FloatingPointError extends ArithmeticError
  408. {
  409. }
  410. @:native("OverflowError")
  411. extern class OverflowError extends ArithmeticError
  412. {
  413. }
  414. @:native("ZeroDivisionError")
  415. extern class ZeroDivisionError extends ArithmeticError
  416. {
  417. }
  418. @:native("ImportError")
  419. extern class ImportError extends Exception
  420. {
  421. }
  422. @:native("LookupError")
  423. extern class LookupError extends Exception
  424. {
  425. }
  426. @:native("IndexError")
  427. extern class IndexError extends LookupError
  428. {
  429. }
  430. @:native("KeyError")
  431. extern class KeyError extends LookupError
  432. {
  433. }
  434. @:native("IOError")
  435. extern class IOError extends EnvironmentError
  436. {
  437. }
  438. @:native("VMSError")
  439. extern class VMSError extends OSError
  440. {
  441. }
  442. @:native("WindowsError")
  443. extern class WindowsError extends OSError
  444. {
  445. }
  446. @:native("ValueError")
  447. extern class ValueError extends Exception
  448. {
  449. }
  450. @:native("UnicodeError")
  451. extern class UnicodeError extends ValueError
  452. {
  453. }
  454. @:native("UnicodeDecodeError")
  455. extern class UnicodeDecodeError extends UnicodeError
  456. {
  457. }
  458. @:native("UnicodeEncodeError")
  459. extern class UnicodeEncodeError extends UnicodeError
  460. {
  461. }
  462. @:native("UnicodeTranslateError")
  463. extern class UnicodeTranslateError extends UnicodeError
  464. {
  465. }
  466. @:native("Warning")
  467. extern class Warning extends Exception
  468. {
  469. }
  470. @:native("DeprecationWarning")
  471. extern class DeprecationWarning extends Warning
  472. {
  473. }
  474. @:native("PendingDeprecationWarning")
  475. extern class PendingDeprecationWarning extends Warning
  476. {
  477. }
  478. @:native("RuntimeWarning")
  479. extern class RuntimeWarning extends Warning
  480. {
  481. }
  482. @:native("SyntaxWarning")
  483. extern class SyntaxWarning extends Warning
  484. {
  485. }
  486. @:native("UserWarning")
  487. extern class UserWarning extends Warning
  488. {
  489. }
  490. @:native("FutureWarning")
  491. extern class FutureWarning extends Warning
  492. {
  493. }
  494. @:native("ImportWarning")
  495. extern class ImportWarning extends Warning
  496. {
  497. }
  498. @:native("UnicodeWarning")
  499. extern class UnicodeWarning extends Warning
  500. {
  501. }
  502. @:native("BytesWarning")
  503. extern class BytesWarning extends Warning
  504. {
  505. }
  506. @:native("ResourceWarning")
  507. extern class ResourceWarning extends Warning
  508. {
  509. }
  510. extern class TB {}
  511. extern class Frame {}