mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
re-add operator
module
This commit is contained in:
parent
148dd96c09
commit
8745f323b8
41
docs/modules/operator.md
Normal file
41
docs/modules/operator.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
---
|
||||||
|
icon: package
|
||||||
|
label: operator
|
||||||
|
---
|
||||||
|
|
||||||
|
The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, `operator.add(x, y)` is equivalent to the expression `x+y`. Many function names are those used for special methods, without the double underscores.
|
||||||
|
|
||||||
|
## Supported operators
|
||||||
|
|
||||||
|
| Operation | Syntax | Function |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| Ordering | `a <= b` | `le(a, b)` |
|
||||||
|
| Ordering | `a < b` | `lt(a, b)` |
|
||||||
|
| Ordering | `a >= b` | `ge(a, b)` |
|
||||||
|
| Ordering | `a > b` | `gt(a, b)` |
|
||||||
|
| Equality | `a == b` | `eq(a, b)` |
|
||||||
|
| Equality | `a != b` | `ne(a, b)` |
|
||||||
|
| Bitwise AND | `a & b` | `and_(a, b)` |
|
||||||
|
| Bitwise OR | `a | b` | `or_(a, b)` |
|
||||||
|
| Bitwise XOR | `a ^ b` | `xor(a, b)` |
|
||||||
|
| Bitwise Inversion | `~a` | `invert(a)` |
|
||||||
|
| Left Shift | `a << b` | `lshift(a, b)` |
|
||||||
|
| Right Shift | `a >> b` | `rshift(a, b)` |
|
||||||
|
| Identity | `a is b` | `is_(a, b)` |
|
||||||
|
| Identity | `a is not b` | `is_not(a, b)` |
|
||||||
|
| Negation (Logical) | `not a` | `not_(a)` |
|
||||||
|
| Negation (Arithmetic) | `-a` | `neg(a)` |
|
||||||
|
| Truth Test | `bool(a)` | `truth(a)` |
|
||||||
|
| Containment Test | `b in a` | `contains(a, b)` |
|
||||||
|
| Addition | `a + b` | `add(a, b)` |
|
||||||
|
| Subtraction | `a - b` | `sub(a, b)` |
|
||||||
|
| Multiplication | `a * b` | `mul(a, b)` |
|
||||||
|
| Division | `a / b` | `truediv(a, b)` |
|
||||||
|
| Division | `a // b` | `floordiv(a, b)` |
|
||||||
|
| Modulo | `a % b` | `mod(a, b)` |
|
||||||
|
| Exponentiation | `a ** b` | `pow(a, b)` |
|
||||||
|
| Matrix Multiplication | `a @ b` | `matmul(a, b)` |
|
||||||
|
| Indexing | `a[b]` | `getitem(a, b)` |
|
||||||
|
| Index Assignment | `a[b] = c` | `setitem(a, b, c)` |
|
||||||
|
| Index Deletion | `del a[b]` | `delitem(a, b)` |
|
||||||
|
|
@ -14,6 +14,7 @@ namespace pkpy{
|
|||||||
extern const char kPythonLibs_functools[];
|
extern const char kPythonLibs_functools[];
|
||||||
extern const char kPythonLibs_heapq[];
|
extern const char kPythonLibs_heapq[];
|
||||||
extern const char kPythonLibs_itertools[];
|
extern const char kPythonLibs_itertools[];
|
||||||
|
extern const char kPythonLibs_operator[];
|
||||||
extern const char kPythonLibs_pickle[];
|
extern const char kPythonLibs_pickle[];
|
||||||
extern const char kPythonLibs_this[];
|
extern const char kPythonLibs_this[];
|
||||||
extern const char kPythonLibs_typing[];
|
extern const char kPythonLibs_typing[];
|
||||||
|
35
python/operator.py
Normal file
35
python/operator.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
|
||||||
|
|
||||||
|
def le(a, b): return a <= b
|
||||||
|
def lt(a, b): return a < b
|
||||||
|
def ge(a, b): return a >= b
|
||||||
|
def gt(a, b): return a > b
|
||||||
|
def eq(a, b): return a == b
|
||||||
|
def ne(a, b): return a != b
|
||||||
|
|
||||||
|
def and_(a, b): return a & b
|
||||||
|
def or_(a, b): return a | b
|
||||||
|
def xor(a, b): return a ^ b
|
||||||
|
def invert(a): return ~a
|
||||||
|
def lshift(a, b): return a << b
|
||||||
|
def rshift(a, b): return a >> b
|
||||||
|
|
||||||
|
def is_(a, b): return a is b
|
||||||
|
def is_not(a, b): return a is not b
|
||||||
|
def not_(a): return not a
|
||||||
|
def truth(a): return bool(a)
|
||||||
|
def contains(a, b): return b in a
|
||||||
|
|
||||||
|
def add(a, b): return a + b
|
||||||
|
def sub(a, b): return a - b
|
||||||
|
def mul(a, b): return a * b
|
||||||
|
def truediv(a, b): return a / b
|
||||||
|
def floordiv(a, b): return a // b
|
||||||
|
def mod(a, b): return a % b
|
||||||
|
def pow(a, b): return a ** b
|
||||||
|
def neg(a): return -a
|
||||||
|
def matmul(a, b): return a @ b
|
||||||
|
|
||||||
|
def getitem(a, b): return a[b]
|
||||||
|
def setitem(a, b, c): a[b] = c
|
||||||
|
def delitem(a, b): del a[b]
|
File diff suppressed because one or more lines are too long
@ -1607,6 +1607,7 @@ void VM::__post_init_builtin_types(){
|
|||||||
_lazy_modules["datetime"] = kPythonLibs_datetime;
|
_lazy_modules["datetime"] = kPythonLibs_datetime;
|
||||||
_lazy_modules["cmath"] = kPythonLibs_cmath;
|
_lazy_modules["cmath"] = kPythonLibs_cmath;
|
||||||
_lazy_modules["itertools"] = kPythonLibs_itertools;
|
_lazy_modules["itertools"] = kPythonLibs_itertools;
|
||||||
|
_lazy_modules["operator"] = kPythonLibs_operator;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
// initialize dummy func_decl for exec/eval
|
// initialize dummy func_decl for exec/eval
|
||||||
|
37
tests/89_operator.py
Normal file
37
tests/89_operator.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import operator as op
|
||||||
|
|
||||||
|
assert op.le(1, 2) == True
|
||||||
|
assert op.lt(1, 2) == True
|
||||||
|
assert op.ge(1, 2) == False
|
||||||
|
assert op.gt(1, 2) == False
|
||||||
|
assert op.eq(1, 2) == False
|
||||||
|
assert op.ne(1, 2) == True
|
||||||
|
assert op.and_(0b01, 0b11) == 0b01
|
||||||
|
assert op.or_(0b01, 0b11) == 0b11
|
||||||
|
assert op.xor(0b01, 0b11) == 0b10
|
||||||
|
assert op.invert(0b01) == -0b10
|
||||||
|
assert op.lshift(0b01, 1) == 0b10
|
||||||
|
assert op.rshift(0b10, 1) == 0b01
|
||||||
|
assert op.is_('a', 'a') == True
|
||||||
|
assert op.is_not('a', 1) == True
|
||||||
|
assert op.not_(True) == False
|
||||||
|
assert op.neg(1) == -1
|
||||||
|
assert op.truth(1) == True
|
||||||
|
assert op.contains([1, 2], 1) == True
|
||||||
|
assert op.add(1, 2) == 3
|
||||||
|
assert op.sub(1, 2) == -1
|
||||||
|
assert op.mul(1, 2) == 2
|
||||||
|
assert op.truediv(1, 2) == 0.5
|
||||||
|
assert op.floordiv(1, 2) == 0
|
||||||
|
assert op.mod(1, 2) == 1
|
||||||
|
assert op.pow(2, 3) == 8
|
||||||
|
|
||||||
|
from linalg import mat3x3
|
||||||
|
assert op.matmul(mat3x3.identity(), mat3x3.identity()) == mat3x3.identity()
|
||||||
|
|
||||||
|
a = [1, 2]
|
||||||
|
assert op.getitem(a, 0) == 1
|
||||||
|
op.setitem(a, 0, 3)
|
||||||
|
assert a == [3, 2]
|
||||||
|
op.delitem(a, 0)
|
||||||
|
assert a == [2]
|
Loading…
x
Reference in New Issue
Block a user