Types.hx 13 KB

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