-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.py
More file actions
371 lines (255 loc) · 7.34 KB
/
stdio.py
File metadata and controls
371 lines (255 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
lp < "str\\n" # basically printf, low level sys stdout requires newlines
p < "str" # lp but with auto-newline
li < "str" # low level input with sys stdin
i < "str" # li wrapper, way better than li
hg < '' # getch, args are ignored but required
hc < '' # uses escape chars for clear, unless system is windows, then system cls
b64de < "base64string" # returns decoded str
cmd < "sh command" # os.system wrapper
pya < "PYTHON_CMD args" # just a workaround for parenthesis cus I think its funny to not use them
exe < "python" # exec without formatting
"""
import os
import sys
import base64
if os.name == "nt":
import msvcrt
else:
import tty
import termios
call="()"
# -------------------------
# Printing Functions
# -------------------------
class LowPrint:
"""Low level printing, requires newline at the end of string"""
def __lt__(self, thing):
try:
sys.stdout.write(str(thing))
sys.stdout.flush()
except IOError as e:
print(f"IO Error: {e}", file=sys.stderr)
lp = LowPrint()
class Print:
"""This is exactly the same as regular python builtin print()"""
def __lt__(self, thing):
try:
lp < f"{thing}\n"
except IOError as e:
lp < f"IO Error: {e}\n"
p = Print()
# -------------------------
# Input Functions
# -------------------------
class LowInput:
def __lt__(self, thing):
try:
sys.stdout.write(str(thing))
sys.stdout.flush()
return sys.stdin.readline().rstrip('\n')
except Exception as e:
p < f'Error: {e}'
return None
li = LowInput()
class Input:
def __lt__(self, thing):
try:
a = li < str(thing)
return a
except Exception as e:
p < e
i = Input()
class _GetchUnix:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __call__(self):
return msvcrt.getch() # type: ignore
class _Getch:
def __init__(self):
if os.name == "nt":
self.impl = _GetchWindows()
else:
self.impl = _GetchUnix()
def __call__(self):
return self.impl()
getch = _Getch()
class HighGetch:
def __lt__(self, thing):
return getch()
hg = HighGetch()
# -------------------------
# Terminal Control
# -------------------------
class clear:
def __lt__(self, thing):
if os.name == "nt":
os.system('cls')
else:
lp < '\033c'
return thing
hc = clear()
# -------------------------
# System/Utility Functions
# -------------------------
class Command:
def __lt__(self, thing):
os.system(thing)
cmd = Command()
# -------------------------
# PAPAYA WORKAROUNDS
# -------------------------
class Def:
def __lt__(self, thing):
"""
thing: a string like "foo x, y: return x + y"
Defines a function named foo with args x, y and body 'return x + y'
"""
import sys
caller_globals = sys._getframe(1).f_globals
name, rest = thing.split(' ', 1)
args, body = rest.split(':', 1)
code = f"def {name}({args.strip()}):\n {body.strip()}"
exec(code, caller_globals)
def_ = Def()
class Pyargs:
def __lt__(self, string):
caller_globals = sys.modules['__main__'].__dict__
parts = string.split(maxsplit=1)
self.cmd = parts[0] if parts else ""
self.args = parts[1] if len(parts) > 1 else ""
return eval(f"{self.cmd}({self.args})", caller_globals)
pya = Pyargs()
class Execute:
def __lt__(self, string):
caller_globals = sys.modules['__main__'].__dict__
return eval(string, caller_globals)
exe = Execute()
class MoveToStdio:
def __lt__(self, thing):
caller_globals = sys.modules['__main__'].__dict__
caller_globals[thing] = str(thing)
mov = MoveToStdio()
# Typing functions
class types:
class Type:
def __lt__(self, thing):
return type(thing)
type_ = Type()
class MakeStr:
def __lt__(self, thing):
return str(thing)
str_ = MakeStr()
class MakeInt:
def __lt__(self, thing):
return int(thing)
int_ = MakeInt()
class MakeFloat:
def __lt__(self, thing):
return float(thing)
float_ = MakeFloat()
class MakeBool:
def __lt__(self, thing):
return bool(thing)
bool_ = MakeBool()
class MakeList:
def __lt__(self, thing):
return list(thing)
list_ = MakeList()
class MakeTuple:
def __lt__(self, thing):
return tuple(thing)
tuple_ = MakeTuple()
class MakeDict:
def __lt__(self, thing):
return dict(thing)
dict_ = MakeDict()
class MakeSet:
def __lt__(self, thing):
return set(thing)
set_ = MakeSet()
class MakeBytes:
def __lt__(self, thing):
return bytes(thing)
bytes_ = MakeBytes()
class MakeComplex:
def __lt__(self, thing):
return complex(thing)
complex_ = MakeComplex()
# input validation
class IsStr:
def __lt__(self, thing):
return isinstance(thing, str)
isstr = IsStr()
class IsInt:
def __lt__(self, thing):
return isinstance(thing, int)
isint = IsInt()
class IsFloat:
def __lt__(self, thing):
return isinstance(thing, float)
isfloat = IsFloat()
class IsBool:
def __lt__(self, thing):
return isinstance(thing, bool)
isbool = IsBool()
class IsList:
def __lt__(self, thing):
return isinstance(thing, list)
islist = IsList()
class IsTuple:
def __lt__(self, thing):
return isinstance(thing, tuple)
istuple = IsTuple()
class IsDict:
def __lt__(self, thing):
return isinstance(thing, dict)
isdict = IsDict()
class IsSet:
def __lt__(self, thing):
return isinstance(thing, set)
isset = IsSet()
class IsNone:
def __lt__(self, thing):
return thing is None
isnone = IsNone()
class IsBytes:
def __lt__(self, thing):
return isinstance(thing, bytes)
isbytes = IsBytes()
class IsComplex:
def __lt__(self, thing):
return isinstance(thing, complex)
iscomplex = IsComplex()
class IsCallable:
def __lt__(self, thing):
return callable(thing)
iscallable = IsCallable()
class IsObject:
def __lt__(self, thing):
return isinstance(thing, object)
isobject = IsObject()
# -------------------------
# OTHER
# -------------------------
class B64d:
def __lt__(self, thing):
b64str = thing
b64b = b64str.encode("ascii")
strb = base64.b64decode(b64b)
return strb.decode("ascii")
b64de = B64d()
#test
def main():
def_ < "test arg1: lp<arg1"
pya < 'test \'hi\'';lp<'\n'
if __name__ == "__main__":
main()