mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Update builtins.h
This commit is contained in:
parent
3d84156a19
commit
6b9002d9d6
@ -228,6 +228,35 @@ def sorted(iterable, key=None, reverse=False):
|
|||||||
a[i], a[j] = a[j], a[i]
|
a[i], a[j] = a[j], a[i]
|
||||||
b[i], b[j] = b[j], b[i]
|
b[i], b[j] = b[j], b[i]
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
class FileIO:
|
||||||
|
def __init__(self, path, mode):
|
||||||
|
assert type(path) is str
|
||||||
|
assert type(mode) is str
|
||||||
|
assert mode in ['r', 'w']
|
||||||
|
self.path = path
|
||||||
|
self.mode = mode
|
||||||
|
self.fp = jsonrpc({"method": "fopen", "params": [path, mode]})
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
assert self.mode == 'r'
|
||||||
|
return jsonrpc({"method": "fread", "params": [self.fp]})
|
||||||
|
|
||||||
|
def write(self, s):
|
||||||
|
assert self.mode == 'w'
|
||||||
|
jsonrpc({"method": "fwrite", "params": [self.fp, s]})
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
jsonrpc({"method": "fclose", "params": [self.fp]})
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def open(path, mode='r'):
|
||||||
|
return FileIO(path, mode)
|
||||||
)";
|
)";
|
||||||
|
|
||||||
const char* __RANDOM_CODE = R"(
|
const char* __RANDOM_CODE = R"(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user