Types.hx 13 KB

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