Types.hx 13 KB

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