Types.hx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. static function __init__ ():Void
  182. {
  183. Syntax.importFromAs("builtins", "dict", "python.lib.Dict");
  184. }
  185. }
  186. class DictImpl {
  187. public static inline function fromObject (x:{}) {
  188. var d = new Dict();
  189. for (f in Reflect.fields(x)) {
  190. d.set(f, Reflect.field(x,f));
  191. }
  192. return d;
  193. }
  194. public static inline function hasKey <X>(d:Dict<X, Dynamic>, key:X) {
  195. return python.Syntax.isIn(key, d);
  196. }
  197. public static inline function remove <X>(d:Dict<X, Dynamic>, key:X) {
  198. python.Syntax.delete(python.Syntax.arrayAccess(d, key));
  199. }
  200. public static inline function set <K,V>(d:Dict<K, V>, key:K, val:V) {
  201. python.Syntax.arraySet(d, key, val);
  202. }
  203. }
  204. extern class Tuple<X> implements ArrayAccess<X> {
  205. public static inline function empty<X>():Tuple<X> {
  206. return Builtin.tuple();
  207. }
  208. public static inline function fromArray<X>(a:Array<X>):Tuple<X> {
  209. return Builtin.tuple(a);
  210. }
  211. public var length(get_length, null):Int;
  212. inline function get_length():Int {
  213. return Builtin.len(this);
  214. }
  215. public inline function at (i:Int):X {
  216. return python.Syntax.arrayAccess(this, i);
  217. }
  218. public inline function toArray ():Array<X>
  219. {
  220. return Builtin.list(this);
  221. }
  222. }
  223. extern class Tup2 <A,B> extends Tuple<Dynamic>
  224. {
  225. public static inline function create <A,B>(a:A, b:B):Tup2<A,B> return python.Syntax.tuple(a,b);
  226. public var _1(get, null):A;
  227. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  228. public var _2(get, null):B;
  229. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  230. }
  231. extern class Tup3 <A,B,C> extends Tuple<Dynamic>
  232. {
  233. 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);
  234. public var _1(get, null):A;
  235. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  236. public var _2(get, null):B;
  237. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  238. public var _3(get, null):C;
  239. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  240. }
  241. extern class Tup4 <A,B,C,D> extends Tuple<Dynamic>
  242. {
  243. 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);
  244. public var _1(get, null):A;
  245. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  246. public var _2(get, null):B;
  247. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  248. public var _3(get, null):C;
  249. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  250. public var _4(get, null):D;
  251. public inline function get__4():D return python.Syntax.arrayAccess(this, 3);
  252. }
  253. extern class Tup5 <A,B,C,D,E> extends Tuple<Dynamic>
  254. {
  255. 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);
  256. public var _1(get, null):A;
  257. public inline function get__1():A return python.Syntax.arrayAccess(this, 0);
  258. public var _2(get, null):B;
  259. public inline function get__2():B return python.Syntax.arrayAccess(this, 1);
  260. public var _3(get, null):C;
  261. public inline function get__3():C return python.Syntax.arrayAccess(this, 2);
  262. public var _4(get, null):D;
  263. public inline function get__4():D return python.Syntax.arrayAccess(this, 3);
  264. public var _5(get, null):E;
  265. public inline function get__5():E return python.Syntax.arrayAccess(this, 4);
  266. }
  267. @:native("BaseException")
  268. extern class BaseException
  269. {
  270. public function new (msg:String):Void;
  271. }
  272. @:native("BufferError")
  273. extern class BufferError extends BaseException
  274. {
  275. }
  276. @:native("GeneratorExit")
  277. extern class GeneratorExit extends BaseException
  278. {
  279. }
  280. @:native("KeyboardInterrupt")
  281. extern class KeyboardInterrupt extends BaseException
  282. {
  283. }
  284. @:native("Exception")
  285. extern class Exception extends BaseException
  286. {
  287. }
  288. @:native("SyntaxError")
  289. extern class SyntaxError extends Exception
  290. {
  291. }
  292. @:native("StopIteration")
  293. extern class StopIteration extends Exception
  294. {
  295. public function new (?message:String);
  296. }
  297. @:native("RuntimeError")
  298. extern class RuntimeError extends Exception
  299. {
  300. }
  301. @:native("NotImplementedError")
  302. extern class NotImplementedError extends RuntimeError
  303. {
  304. }
  305. @:native("IndentationError")
  306. extern class IndentationError extends SyntaxError
  307. {
  308. }
  309. @:native("EnvironmentError")
  310. extern class EnvironmentError extends Exception
  311. {
  312. }
  313. @:native("OSError")
  314. extern class OSError extends EnvironmentError
  315. {
  316. }
  317. @:native("BlockingIOError")
  318. extern class BlockingIOError extends OSError
  319. {
  320. }
  321. @:native("ChildProcessError")
  322. extern class ChildProcessError extends OSError
  323. {
  324. }
  325. @:native("ConnectionError")
  326. extern class ConnectionError extends OSError
  327. {
  328. }
  329. @:native("BrokenPipeError")
  330. extern class BrokenPipeError extends ConnectionError
  331. {
  332. }
  333. @:native("ConnectionAbortedError")
  334. extern class ConnectionAbortedError extends ConnectionError
  335. {
  336. }
  337. @:native("ConnectionRefusedError")
  338. extern class ConnectionRefusedError extends ConnectionError
  339. {
  340. }
  341. @:native("ConnectionResetError")
  342. extern class ConnectionResetError extends ConnectionError
  343. {
  344. }
  345. @:native("FileExistsError")
  346. extern class FileExistsError extends OSError
  347. {
  348. }
  349. @:native("FileNotFoundError")
  350. extern class FileNotFoundError extends OSError
  351. {
  352. }
  353. @:native("InterruptedError")
  354. extern class InterruptedError extends OSError
  355. {
  356. }
  357. @:native("IsADirectoryError")
  358. extern class IsADirectoryError extends OSError
  359. {
  360. }
  361. @:native("NotADirectoryError")
  362. extern class NotADirectoryError extends OSError
  363. {
  364. }
  365. @:native("PermissionError")
  366. extern class PermissionError extends OSError
  367. {
  368. }
  369. @:native("ProcessLookupError")
  370. extern class ProcessLookupError extends OSError
  371. {
  372. }
  373. @:native("TimeoutError")
  374. extern class TimeoutError extends OSError
  375. {
  376. }
  377. @:native("NameError")
  378. extern class NameError extends Exception
  379. {
  380. }
  381. @:native("UnboundLocalError")
  382. extern class UnboundLocalError extends NameError
  383. {
  384. }
  385. @:native("MemoryError")
  386. extern class MemoryError extends Exception
  387. {
  388. }
  389. @:native("AssertionError")
  390. extern class AssertionError extends Exception
  391. {
  392. }
  393. @:native("AttributeError")
  394. extern class AttributeError extends Exception
  395. {
  396. }
  397. @:native("EOFError")
  398. extern class EOFError extends Exception
  399. {
  400. }
  401. @:native("ArithmeticError")
  402. extern class ArithmeticError extends Exception
  403. {
  404. }
  405. @:native("FloatingPointError")
  406. extern class FloatingPointError extends ArithmeticError
  407. {
  408. }
  409. @:native("OverflowError")
  410. extern class OverflowError extends ArithmeticError
  411. {
  412. }
  413. @:native("ZeroDivisionError")
  414. extern class ZeroDivisionError extends ArithmeticError
  415. {
  416. }
  417. @:native("ImportError")
  418. extern class ImportError extends Exception
  419. {
  420. }
  421. @:native("LookupError")
  422. extern class LookupError extends Exception
  423. {
  424. }
  425. @:native("IndexError")
  426. extern class IndexError extends LookupError
  427. {
  428. }
  429. @:native("KeyError")
  430. extern class KeyError extends LookupError
  431. {
  432. }
  433. @:native("IOError")
  434. extern class IOError extends EnvironmentError
  435. {
  436. }
  437. @:native("VMSError")
  438. extern class VMSError extends OSError
  439. {
  440. }
  441. @:native("WindowsError")
  442. extern class WindowsError extends OSError
  443. {
  444. }
  445. @:native("ValueError")
  446. extern class ValueError extends Exception
  447. {
  448. }
  449. @:native("UnicodeError")
  450. extern class UnicodeError extends ValueError
  451. {
  452. }
  453. @:native("UnicodeDecodeError")
  454. extern class UnicodeDecodeError extends UnicodeError
  455. {
  456. }
  457. @:native("UnicodeEncodeError")
  458. extern class UnicodeEncodeError extends UnicodeError
  459. {
  460. }
  461. @:native("UnicodeTranslateError")
  462. extern class UnicodeTranslateError extends UnicodeError
  463. {
  464. }
  465. @:native("Warning")
  466. extern class Warning extends Exception
  467. {
  468. }
  469. @:native("DeprecationWarning")
  470. extern class DeprecationWarning extends Warning
  471. {
  472. }
  473. @:native("PendingDeprecationWarning")
  474. extern class PendingDeprecationWarning extends Warning
  475. {
  476. }
  477. @:native("RuntimeWarning")
  478. extern class RuntimeWarning extends Warning
  479. {
  480. }
  481. @:native("SyntaxWarning")
  482. extern class SyntaxWarning extends Warning
  483. {
  484. }
  485. @:native("UserWarning")
  486. extern class UserWarning extends Warning
  487. {
  488. }
  489. @:native("FutureWarning")
  490. extern class FutureWarning extends Warning
  491. {
  492. }
  493. @:native("ImportWarning")
  494. extern class ImportWarning extends Warning
  495. {
  496. }
  497. @:native("UnicodeWarning")
  498. extern class UnicodeWarning extends Warning
  499. {
  500. }
  501. @:native("BytesWarning")
  502. extern class BytesWarning extends Warning
  503. {
  504. }
  505. @:native("ResourceWarning")
  506. extern class ResourceWarning extends Warning
  507. {
  508. }
  509. extern class TB {}
  510. extern class Frame {}