-
Notifications
You must be signed in to change notification settings - Fork 1
/
pystartup.py
631 lines (516 loc) · 17 KB
/
pystartup.py
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
from __future__ import print_function
import sys
import os
import math
from math import *
import builtins
pow = builtins.pow
if sys.version_info < (3,6):
tau = 2 * pi
## degrees cos, sin, tan
cosd = lambda x: cos(radians(x)) if not(x % 90 == 0) else [1, 0, -1, 0][(x // 90) % 4]
sind = lambda x: sin(radians(x)) if not(x % 90 == 0) else [0, 1, 0, -1][(x // 90) % 4]
tand = lambda x: sind(x) / cosd(x)
atand = lambda x: degrees(atan(x))
atan2d = lambda y, x: degrees(atan(y, x))
acosd = lambda x: degrees(acos(x)) if not(x in (-1,0,1)) else [180, 90, 0][int(x)+1]
asind = lambda x: degrees(asin(x)) if not(x in (-1,0,1)) else [-90, 0, 90][int(x)+1]
## french
racine = sqrt
## log, ln, log10
ln = math.log
def log(x, base=None):
if base is None:
raise ValueError('Log without a base is ambiguous, use log10 or ln')
return math.log(x, base)
## import *
import itertools
from itertools import *
import functools
from functools import *
from operator import *
from collections import *
## import module
if sys.version_info[0] >= 3:
import html
import random
from random import randint, randrange, shuffle, choice
import string
from string import ascii_letters, ascii_lowercase, ascii_uppercase
import argparse
import unicodedata
import re
import json
import csv
import sqlite3
import subprocess
import subprocess as sub
from pprint import pprint
from glob import glob
from fractions import Fraction
frac = Fraction # from fractions import Fraction as F
## funcoperators
try:
from funcoperators import infix, postfix, unary
except ImportError:
infix = postfix = unary = lambda x:x
## datetime
from datetime import date, time, datetime, timedelta
class CallableTimedelta(timedelta):
def __call__(self, x):
return self * x
seconds, milliseconds, microseconds, days, hours, minutes, weeks = (CallableTimedelta(**{x:1}) for x in ('seconds', 'milliseconds', 'microseconds', 'days', 'hours', 'minutes', 'weeks'))
years = 365.25 * days # Julian year https://en.wikipedia.org/wiki/Julian_year_(astronomy)
combine = infix(datetime.combine)
now = datetime.now
today = datetime.today
## datetime utils
@postfix
def french_date(date):
return date.strftime("%d/%m/%Y %Hh%M")
@postfix
def french_date_only(date):
return date.strftime("%d/%m/%Y")
datefrench = datefr = date_french = frenchdate = french_date
datefrenchonly = dateonlyfr = date_only_french = frenchdateonly = french_date_only
## operator
import operator
ops = operators = {
'+': operator.add,
'-': operator.sub,
'/': operator.truediv,
'//': operator.floordiv,
'*': operator.mul,
'%': operator.mod,
'@': getattr(operator, 'matmul', operator.mul),
'&': operator.and_,
'^': operator.xor,
'~': operator.invert,
'|': operator.or_,
'**': operator.pow,
'<<': operator.lshift,
'>>': operator.rshift,
'<': operator.lt,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'>=': operator.ge,
'>': operator.gt,
'not': operator.not_,
'is': operator.is_,
'is not': operator.is_not,
'and': operator.and_,
'or': operator.or_,
}
## re
Re = re.compile
## numpy
try:
import numpy
import numpy as np
from numpy import array, matrix
from numpy import cross, dot
from numpy.linalg import norm
vec = lambda *args, **kwargs: numpy.array(args, **kwargs)
except:
pass
if sys.version_info[0] >= 3:
from importlib import reload
## related to current projects
try:
pass
#from mydjangoapp.views import *
#from mydjangoapp.models import *
except:
pass
## map, filter
if sys.version_info[0] < 3:
lmap = map
lfilter = filter
else:
def lmap(*a, **b):
""" return list(map(*a, **b)) """
return list(map(*a, **b))
def lfilter(*a, **b):
""" return list(filter(*a, **b)) """
return list(filter(*a, **b))
def mapwith(function):
"""
>>> sum(map(int, '1 2 7 2'.split()))
12
>>> toints = mapwith(int)
>>> sum(toints('1 2 7 2'.split()))
12
>>> '1 2 7 2'.split() |mapwith(int) |postfix(sum)
12
"""
return postfix(lambda *iterables: map(function, *iterables))
def filterwith(function):
return postfix(lambda *iterables: filter(function, *iterables))
def lmapwith(function):
""" @see mapwith """
return postfix(lambda *iterables: lmap(function, *iterables))
def lfilterwith(function):
""" @see filterwith """
return postfix(lambda *iterables: lfilter(function, *iterables))
def whichall(iterable_of_x_condition):
"""
>>> all(x % 2 == 0 for x in range(10))
False
>>> # annoying, I want to know which ones !
>>> whichall((x, x % 2 == 0) for x in range(10))
[0, 2, 4, 6, 8]
>>> # the standard solution is annoying because it has to MOVE the condition to the end
>>> [x for x in range(10) if x % 2 == 0]
[0, 2, 4, 6, 8]
"""
return [x for x,condition in iterable_of_x_condition if condition]
makemap, makefilter, makelmap, makelfilter = mapwith, filterwith, lmapwith, lfilterwith
Map, Filter = mapwith, filterwith
## unicode stuff
try:
import uniutils
from uniutils import * # uniname, hexord, uord, uordname, uniline, unicontext, unilinecat, unibasecat
except:
pass
## 3D, gl shaders like
def vec3(x=None, y=None, z=None):
'''
vec3(1) == array((1,1,1))
vec3((1,2,3)) ==
vec3((1,2), 3) ==
vec3(1,(2, 3)) ==
vec3(1,2,3) == array((1,2,3))
'''
return (array((0,0,0)) if x is None else
array(x) if y is None and z is None and hasattr(x, '__len__') and len(x) == 3 else
array((x,x,x)) if y is None and z is None else
array((x[0], x[1], y)) if z is None and hasattr(x, '__len__') and len(x) >= 2 else
array((x, y[0], y[1])) if z is None and hasattr(y, '__len__') and len(y) >= 2 else
array((x,y,z)))
@postfix
def normalized(x):
return x / norm(x)
@postfix
def fmt2(x):
return format(x, '.2f')
fmt = fmt2
## 3D + funcoperators, inline |dot| |cross| product
try:
import funcoperators
from funcoperators import infix, prefix, postfix
except ImportError:
pass
else:
tostr, tolist, totuple, toset, tochr, toord, tojoin = map(postfix, (str, list, tuple, set, chr, ord, ''.join))
from fractions import Fraction
div = frac = funcoperators.infix(Fraction)
try:
import numpy
except ImportError:
pass
else:
dot = infix(numpy.dot)
cross = infix(numpy.cross)
## Statistical stuffs
def mean(it):
s = 0
i = 0
for x in it:
s += x
i += 1
return 1.0 * s / i
def variance(it):
s = s2 = i = 0
for x in it:
s += x
s2 += x * x
i += 1
return (1.0 * s2 / i) - (1.0 * s / i) ** 2
def StatsWithData(it):
from collections import namedtuple
m1 = m2 = n = 0
data = []
for x in it:
m1 += x
m2 += x ** 2
n += 1
data.append(x)
m1 /= 1. * n or 1.
m2 /= 1. * n or 1.
mean = m1
variance = max(0, m2 - mean ** 2)
data.sort()
Min, Max = (None, ) * 2 if n == 0 else (data[0], data[-1])
q1, q2, q3 = (None, ) * 3 if n == 0 else (data[len(data)*i//4] for i in (1,2,3))
return namedtuple('StatsWithData', 'mean stdev n variance min max data q0 q1 q2 q3 q4')(mean, variance ** .5, n, variance, Min, Max, data, Min, q1, q2, q3, Max)
def Stats(it):
s = StatsWithData(it)
fields = tuple(f for f in s._fields if f != 'data')
return namedtuple('Stats', fields)(*(getattr(s,f) for f in fields))
def stdev(it):
return sqrt(variance(it))
def print_matrix(M):
M = [[format(x) for x in row] for row in M]
ncolumns = max(len(row) for row in M)
for row in M:
row.extend([''] * (ncolumns - len(row)))
widths = [max(len(M[i][j]) for i in range(len(M))) for j in range(ncolumns)]
for row in M:
for i in range(len(row)):
row[i] += ' ' * (widths[i] - len(row[i]))
print('\n'.join(' '.join(row) for row in M))
## urllib
def urlparsetotal(url, *, multiple=False, fragment_is_qs=False):
from urllib.parse import urlparse, parse_qs
result = urlparse(url)._asdict()
result['pqs'] = parse_qs(result.pop('query'))
if not multiple:
def single(a_dict):
for k,v in a_dict.items():
if not len(v) == 1:
raise ValueError("Doesn't have only one value {!r}: {}".format(k, v))
return {k:v[0] for k,v in a_dict.items()}
result['pqs'] = single(result['pqs'])
if fragment_is_qs:
result['fqs'] = parse_qs(result.pop('fragment'))
if not multiple:
result['fqs'] = single(result['fqs'])
return dict(result)
## functional tools
try:
from funcoperators import elipartial
except ImportError:
pass
try:
from funcoperators import compose, circle # (hex |circle| ord)(x) == hex(ord(x))
# french
rond = circle
except ImportError:
pass
## introspection
@postfix
def desc(x, *, underscore=False, callable=False, attrs=None):
if isinstance(attrs, str):
attrs = set(attrs.split())
elif attrs is not None and not isinstance(attrs, (set, frozenset)):
attrs = set(attrs)
def keep1(n):
return True if callable else not hasattr(getattr(x,n), '__call__')
def keep2(n):
return True if underscore else not n.startswith('_')
def keep(n):
return keep1(n) and keep2(n) and (attrs is None or n in attrs)
return {n: getattr(x, n) for n in dir(x) if keep(n)}
@postfix
def dir_decorate(x, *, underscore=False, callable=True, attrs=None):
if isinstance(attrs, str):
attrs = set(attrs.split())
elif attrs is not None and not isinstance(attrs, (set, frozenset)):
attrs = set(attrs)
def reduction(name):
return 'function' if name == 'builtin_function_or_method' else name
def decoration(obj):
""" returns '(' if obj is callable, '!' if it's a class, else '' """
return ('!' if type(obj) is type else
'(' if hasattr(obj, '__call__') else
':' + reduction(obj.__class__.__name__))
def keep1(n):
return True if callable else not hasattr(getattr(x,n), '__call__')
def keep2(n):
return True if underscore else not n.startswith('_')
def keep(n):
return keep1(n) and keep2(n) and (attrs is None or n in attrs)
def filtered(x,n):
return ((True if callable else not hasattr(getattr(x,n), '__call__')
and (True if underscore else not n.startswith('_'))))
def sorted_key(iterable_of_2tuple):
return [b for a,b in sorted(iterable_of_2tuple)]
return sorted_key((d, n + d) for n in dir(x) if keep(n) for d in [decoration(getattr(x,n))])
# return [n + decoration(getattr(x, n)) for n in dir(x)]
# return [n + d for n in dir(x) for d in [decoration(getattr(x,n))]]
desc_dir = descdir = dirdec = dirdecorate = dirdesc = dir_decorate
@postfix
def plddesc(*a, **b):
print('\n'.join(ddesc(*a, **b)))
## ranges
@infix
def irange(*args):
"""
@returns range
list(irange(5)) == [1,2,3,4,5]
list(irange(1,5)) == [1,2,3,4,5]
list(irange(2,5)) == [2,3,4,5]
list(irange(2,10,2)) == [2,4,6,8,10]
list(irange(5,2,-1)) == [5,4,3,2]
"""
if len(args) == 1:
return range(1, 1+args[0])
r = range(*args)
if r.step < 0:
return range(r.start, r.stop-1, r.step)
return range(r.start, 1+r.stop, r.step)
try:
import funcoperators
except ImportError:
exclusive = range
else:
exclusive = infix(range)
inclusive = irange
## utils
def groupdict(iterable, *, key=None, reduce=None, join=None):
"""
>>> groupdict((i%2, i) for i in range(10))
{0: [0,2,4,6,8], 1: [1,3,5,7,9]}
>>> def rest(x): return i % 2
>>> groupdict(range(10), key=rest)
{0: [0,2,4,6,8], 1: [1,3,5,7,9]}
>>> groupdict(range(10), key=rest, reduce=sum)
{0: 20, 1: 25}
>>> groupdict('abc56', key=str.isdigit, join=''.join)
{True: '56', False: 'abc'}
"""
if key is not None:
return groupdict(((key(x), x) for x in iterable), reduce=reduce, join=join)
if join is not None and reduce is not None:
raise ValueError("join and reduce are mutually exclusive")
d = {}
for x,y in iterable:
if x not in d:
d[x] = []
d[x].append(y)
if join is not None:
return {x:join(map(str,y)) for x,y in d.items()}
if reduce is not None:
return {x:reduce(y) for x,y in d.items()}
return d
def groupdictby(key, iterable, **kwargs):
return groupdict(iterable, key=key, **kwargs)
groupings = groupdict
groupingsby = groupdictby
def setdiff(A, B):
if not isinstance(A, (set, frozenset)):
A = set(A)
if not isinstance(B, (set, frozenset)):
B = set(B)
return (A - B, B - A)
def print_list_dict(dic, **kwargs):
print('\n'.join('{}: {}'.format(x,y) for x,y in dic.items()), **kwargs)
def print_list(iterable, **kwargs):
print('\n'.join(str(s) for s in iterable), **kwargs)
def dictuniq(it):
d = {}
for a,b in it:
if a in d:
raise ValueError
d[a] = b
return d
def mro(x):
return x.__class__.__mro__
## modification of some standard functions
# implicit map
def all(*args):
""" all(func, iterable) is all(map(func, iterable)) """
import builtins
return builtins.all(*args) if len(args) != 2 else builtins.all(map(args[0], args[1]))
# implicit map
def any(*args):
""" any(func, iterable) is any(map(func, iterable)) """
import builtins
return builtins.any(*args) if len(args) != 2 else builtins.any(map(args[0], args[1]))
## custom short aliases
ul = uniline
pl, pld = print_list, print_list_dict
ddesc = dir_decorate
pfmt2 = print *circle* fmt2
to = postfix # same as tolist, tostr, etc. tolist = to(list)
c = compose # vec
F = frac
nonascii = ''.join -c- filterwith(lambda x:ord(x) > 127)
uln = ulnonascii = ul -c- nonascii
mapto = mapwith
def lentype(x):
try: return len(x), type(x)
except: return None, type(x)
## clipboard
import platform
if platform.system() == 'Linux':
def pbpaste(*, selection:'primary|secondary|clipboard'='clipboard'):
import subprocess
return subprocess.check_output(['xclip', '-selection', selection, '-o']).decode('utf-8')
@postfix
def pbcopy(s, *, selection:'primary|secondary|clipboard'='clipboard'):
import subprocess
p = subprocess.Popen(['xclip', '-selection', selection], stdin=subprocess.PIPE)
p.communicate(s.encode('utf-8'))
# Warning: Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.
#p.stdin.write(s.encode('utf-8'))
#p.stdin.close()
elif platform.system() == 'Windows':
# TODO: use subprocess and 'clip' command (clip < stream and echo "hello" | clip)
@postfix
def pbcopy(x):
from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(x)
r.update() # now it stays on the clipboard after the window is closed
r.destroy()
def pbpaste():
from tkinter import Tk
r = Tk()
r.withdraw()
s = r.clipboard_get()
r.update() # now it stays on the clipboard after the window is closed
r.destroy()
return s
cc, cv = pbcopy, pbpaste
ccm = partial(pbcopy, selection='primary') # middle click
cvm = partial(pbpaste, selection='primary') # middle click
## misc.
def dictuniq(it):
d = {}
for a,b in it:
if a in d:
raise ValueError
d[a] = b
return d
def formatwith(fmt):
return postfix(partial(format, format_spec=fmt)) # postfix(lambda x:format(x, fmt))
def same_ast(a, b):
import ast
A, B = (ast.dump(ast.parse(x)) for x in (a, b))
return A == B
def pprint_ast(x, *, show_offsets=True):
import ast
import astpretty
astpretty.pprint(ast.parse(x), show_offsets=show_offsets)
#while True:
# subprocess.call('kdialog --passivepopup'.split() +
#['h'.join(str(abs(now() - lever)).split(':')[:2])] + [str(10)]); sleep(60*3)
#from urllib.parse import urlunparse
import urllib
from pathlib import Path as path
def unquotecc():
import urllib
#cv() | to(urllib.parse.unquote) | cc
cc(urllib.parse.unquote(cv()))
ccunquote = unquotecc
def strgroupn(x:str, n:int=8):
if not len(x) % 8 == 0:
raise ValueError(f"{x:!r} len must be divisible by {n}")
return [x[i*n:i*n+n] for i in range(len(x)//n)]
from urllib.parse import unquote as urlunquote
urldecode = urlunquote
@postfix
def cyr(x):
LM = (list(zip('azertyuiopqsdfghjklmwxcvbn',
'азертыуиопясдфгхйклмжьцвбн'))
+ list(zip('sh sch ye yo yu'.split(), 'шщчэёю'))
+ list(zip('ya'.split(), 'я')))
LM2 = [(a.upper(), b.upper()) for a,b in LM]
M = dict(LM + LM2) #python before dict order
get0 = lambda x: x[0]
R = re.compile('[({})]'.format('|'.join(map(re.escape -compose- get0, LM))))
return R.sub(lambda x: M.get(x.group(0), x.group(0)), x)