Types.hx 13 KB

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