mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
This commit is contained in:
parent
a12eb4c8bc
commit
04df1dbe5b
@ -263,31 +263,8 @@ def __f(self, width: int, fillchar=' '):
|
||||
return self + fillchar * delta
|
||||
str.ljust = __f
|
||||
|
||||
##### list #####
|
||||
def __qsort(a: list, L: int, R: int, key):
|
||||
if L >= R: return;
|
||||
mid = a[(R+L)//2];
|
||||
mid = key(mid)
|
||||
i, j = L, R
|
||||
while i<=j:
|
||||
while key(a[i])<mid: ++i;
|
||||
while key(a[j])>mid: --j;
|
||||
if i<=j:
|
||||
a[i], a[j] = a[j], a[i]
|
||||
++i; --j;
|
||||
__qsort(a, L, j, key)
|
||||
__qsort(a, i, R, key)
|
||||
del __f
|
||||
|
||||
def __f(self, reverse=False, key=None):
|
||||
if key is None:
|
||||
key = lambda x:x
|
||||
__qsort(self, 0, len(self)-1, key)
|
||||
if reverse:
|
||||
self.reverse()
|
||||
list.sort = __f
|
||||
|
||||
type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
|
||||
type.__getitem__ = lambda self, *args: self # for generics
|
||||
|
||||
def help(obj):
|
||||
if hasattr(obj, '__func__'):
|
||||
@ -295,8 +272,6 @@ def help(obj):
|
||||
print(obj.__signature__)
|
||||
print(obj.__doc__)
|
||||
|
||||
del __f
|
||||
|
||||
class Exception: pass
|
||||
|
||||
from _long import long
|
@ -696,6 +696,23 @@ void init_builtins(VM* _vm) {
|
||||
});
|
||||
|
||||
/************ list ************/
|
||||
_vm->bind(_vm->_t(_vm->tp_list), "sort(self, key=None, reverse=False)", [](VM* vm, ArgsView args) {
|
||||
List& self = _CAST(List&, args[0]);
|
||||
PyObject* key = args[1];
|
||||
if(key == vm->None){
|
||||
std::stable_sort(self.begin(), self.end(), [vm](PyObject* a, PyObject* b){
|
||||
return vm->py_lt(a, b);
|
||||
});
|
||||
}else{
|
||||
std::stable_sort(self.begin(), self.end(), [vm, key](PyObject* a, PyObject* b){
|
||||
return vm->py_lt(vm->call(key, a), vm->call(key, b));
|
||||
});
|
||||
}
|
||||
bool reverse = CAST(bool, args[2]);
|
||||
if(reverse) self.reverse();
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
_vm->bind__repr__(_vm->tp_list, [](VM* vm, PyObject* _0){
|
||||
List& iterable = _CAST(List&, _0);
|
||||
SStream ss;
|
||||
@ -1618,6 +1635,19 @@ void add_module_gc(VM* vm){
|
||||
void VM::post_init(){
|
||||
init_builtins(this);
|
||||
|
||||
// type
|
||||
bind__getitem__(tp_type, [](VM* vm, PyObject* self, PyObject* _){
|
||||
PK_UNUSED(_);
|
||||
return self; // for generics
|
||||
});
|
||||
|
||||
bind__repr__(tp_type, [](VM* vm, PyObject* self){
|
||||
SStream ss;
|
||||
const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, self)];
|
||||
ss << "<class '" << info.name << "'>";
|
||||
return VAR(ss.str());
|
||||
});
|
||||
|
||||
bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0])));
|
||||
bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){
|
||||
const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];
|
||||
|
Loading…
x
Reference in New Issue
Block a user