-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython-heapq-tempfile-pathlib-modul.py
129 lines (81 loc) · 2.41 KB
/
python-heapq-tempfile-pathlib-modul.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
import heapq
a = [1, 2, 3, 5, 6, 8, 7]
print(a) # [1, 2, 3, 5, 6, 8, 7]
print(heapq.heappop(a)) # # 1
print(a) # [2, 5, 3, 7, 6, 8]
print(heapq.heappop(a)) # # 2
print(a) # [3, 5, 8, 7, 6]
print(a.pop()) # 6
from heapq import *
def heapsort(iterable):
h = []
for value in iterable:
heappush(h,value)
return [heappop(h) for i in range(len(h))]
print(heapsort([1,3,44,11,22,244])) #[1, 3, 11, 22, 44, 244]
import math
from io import StringIO
def show_tree(tree, total_width=60, fill=' '):
output = StringIO()
last_row = -1
for i, n in enumerate(tree):
if i:
row = int(math.floor(math.log(i+1, 2)))
else:
row = 0
if row != last_row:
output.write('\n')
columns = 2**row
col_width = int(math.floor((total_width * 1.0) / columns))
output.write(str(n).center(col_width, fill))
last_row = row
print (output.getvalue())
print ('-' * total_width)
return
import heapq
heap = []
heapq.heappush(heap, (1, 'one', 2))
heapq.heappush(heap, (1, 'one', 3))
heapq.heappush(heap, (1,'one', 1))
heapq.heappush(heap, (1,'one', 4))
heapq.heappush(heap, (1,'one', 5))
heapq.heappush(heap, (1,'one', 6))
heapq.heappush(heap, (1,'one', 89))
show_tree(heap)
import heapq
mylist = [120, -34, 0, 56, 444, 78]
print(heapq.nlargest(2, mylist)) # [444, 120]
print(heapq.nsmallest(3, mylist)) # [-34, 0, 56]
from pathlib import Path
yeni_yol = Path("yenidizin", "yenidosya.txt")
print(yeni_yol)
## cıktı
## yenidizin/yenidosya.txt
from pathlib import Path
home = Path.home()
wave_absolute = Path(home, "yenidizin", "yenidosya.txt")
print(home) # /home/pcismi
print(wave_absolute) # /home/pcismi/yenidizin/yenidosya.txt
from pathlib import Path
dosya_yolu = Path("yenidizin", "yenidosya.txt")
print(dosya_yolu) #yenidizin/yenidosya.txt
print(dosya_yolu.name) # yenidosya.txt
print(dosya_yolu.suffix) # .txt
import tempfile
# gecici dosya yeri
print(tempfile.gettempdir())
#gecici dosya olusmadıysa none doner olusmus ise dosya ismini doner
#print(tempfile.tempdir) # none
import tempfile
def edit(base):
tmp = tempfile.NamedTemporaryFile(mode='w+')
print(tempfile.tempdir) # /tmp
#fname = tmp.name
tmp.write(base)
tmp.seek(0, 0) # This will rewind the cursor
#system('nano %s' % fname)
content = tmp.readlines()
tmp.close()
return content
answer = "hi"
print(edit(answer))