-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
79 lines (64 loc) · 1.63 KB
/
misc.py
File metadata and controls
79 lines (64 loc) · 1.63 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
#!/usr/bin/env python
# -*- coding: utf8 -*-
from itertools import chain, combinations
from functools import reduce
import operator as op
import resource
def childrens_time():
time = resource.getrusage(resource.RUSAGE_CHILDREN)
time = time[0]+time[1] #user + system
return time
def ncr(n, r):
r = min(r, n-r)
if r == 0: return 1
numer = reduce(op.mul, range(n, n-r, -1))
denom = reduce(op.mul, range(1, r+1))
return numer//denom
def indent(text):
r"""
Indenta un parrafo
>>> print(indent("hola\n hola\nhola"))
hola
hola
hola
<BLANKLINE>
>>> print(indent(indent("hola\n hola\nhola")))
hola
hola
hola
<BLANKLINE>
"""
text = " " + text.strip("\n")
return text.replace('\n', '\n ') + "\n"
def comment(text):
r"""
comenta un parrafo
>>> print(comment("hola\n hola\nhola"))
# hola
# hola
# hola
<BLANKLINE>
"""
text = "# " + text.strip("\n")
return text.replace('\n', '\n# ') + "\n"
def powerset(iterable):
"""
Devuelve un generador que itera sobre partes del iterable,
va de mayor a menor.
>>> list(powerset([1,2,3]))
[[1, 2, 3], [1, 2], [1, 3], [2, 3], [1], [2], [3], []]
"""
s = list(iterable)
return map(list, chain.from_iterable(combinations(s, r) for r in range(len(s) + 1, -1, -1)))
def compose(f,g):
"""
Compone funciones de Python
>>> compose(lambda x: x+1, lambda x,y: x+y)(5,6)
12
"""
def composition(*args):
return f(g(*args))
return composition
if __name__ == "__main__":
import doctest
doctest.testmod()