Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0254479

Browse files
committed
update progress bar
1 parent 2813f99 commit 0254479

File tree

7 files changed

+158
-19
lines changed

7 files changed

+158
-19
lines changed

‎code/download_progress_demo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import division
3+
import sys
4+
import time
5+
6+
def progress():
7+
for i in range(100):
8+
sys.stdout.write("\r[%s%s] %2d%%" % ('█' * i, ' ' * (99 - i), (i / 99) * 100))
9+
sys.stdout.flush()
10+
time.sleep(0.1)
11+
sys.stdout.write('\n')
12+
13+
14+
if __name__ == '__main__':
15+
progress()

‎code/socket_transfer_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, filename, path=None):
2020

2121
@property
2222
def exists(self):
23-
return os.path.exists(self.fullname)
23+
return os.path.exists(self.fullname)andos.path.isfile(self.fullname)
2424

2525
@property
2626
def md5(self):

‎code/socket_transfer_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, filename, path=None):
3434

3535
@property
3636
def exists(self):
37-
return os.path.exists(self.fullname)
37+
return os.path.exists(self.fullname)andos.path.isfile(self.fullname)
3838

3939
@property
4040
def md5(self):

‎content/file.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ f.open('file'[,'mode'])
99
|r |以读方式打开文件,可读取文件信息。|
1010
|w |以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容;如果文件不存在则创建。|
1111
|a |以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建。|
12-
|r+ |以读写方式打开文件,可对文件进行读和写操作。|
13-
|w+ |消除文件内容,然后以读写方式打开文件|
12+
|r+ |以读写方式打开文件,可对文件进行读和写操作,写入只能在文件的开头或者结尾,不能从中间开始写|
13+
|w+ |以读写方式打开文件,如文件存在,则清空该文件,再写入新内容;如果文件不存在则创建|
1414
|a+ |以读写方式打开文件,并把文件指针移到文件尾。|
1515
|b |以二进制模式打开文件,而不是以文本模式。该模式只对Windows或Dos有效,类Unix的文件是用二进制模式进行操作的。|
1616

17+
在 unix 系统下文件的读写是线程安全的,甚至文件在一个线程中等待读写都不会受到其他线程的干扰。
18+
19+
print 则不是线程安全的,sys.stdout.write 则要好一点,但是在线程内等待则会受到其他线程的干扰。
1720
#### 打开文件的方法
1821

1922
|方法 |描述 |
@@ -138,3 +141,66 @@ $filename=iconv('utf-8','gb2312',$filename);
138141
```
139142
file_get_contents(mb_convert_encoding($filename, 'gbk', 'utf-8'));
140143
```
144+
145+
#### 读取文件的最后一行的办法
146+
147+
```
148+
# -*- coding: utf-8 -*-
149+
150+
import time
151+
152+
153+
def follow(filename, s=1):
154+
with open(filename) as f:
155+
f.seek(0, 2)
156+
while True:
157+
line = f.readline()
158+
if not line:
159+
time.sleep(s)
160+
else:
161+
print 'line', len(line), repr(line)
162+
163+
164+
if __name__ == '__main__':
165+
follow('/var/log/system.log')
166+
167+
```
168+
169+
有的人是再次 seek 一遍,但是实测不用也可以。
170+
171+
```
172+
def follow(self, s=1):
173+
''' Do a tail follow. If a callback function is registered it is called with every new line.
174+
Else printed to standard out.
175+
176+
Arguments:
177+
s - Number of seconds to wait between each iteration; Defaults to 1. '''
178+
179+
with open(self.tailed_file) as file_:
180+
# Go to the end of file
181+
file_.seek(0,2)
182+
while not self._stop:
183+
curr_position = file_.tell()
184+
line = file_.readline()
185+
if not line:
186+
file_.seek(curr_position)
187+
time.sleep(s)
188+
else:
189+
self.callback(line)
190+
```
191+
192+
193+
或者使用 subprocess
194+
195+
```
196+
def follow(self):
197+
popen = subprocess.Popen(['tail', '-n0', '-f', self.filename],
198+
stdout=subprocess.PIPE,
199+
stderr=subprocess.PIPE)
200+
while True:
201+
if self._stop:
202+
popen.kill()
203+
return
204+
line = popen.stdout.readline()
205+
self.callback(line)
206+
```

‎content/progressbar.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from __future__ import division
1313
import math
1414
import time
1515
import sys
16-
16+
1717
def progressbar(cur, total):
1818
percent = '{:.2%}'.format(cur / total)
1919
sys.stdout.write('\r')
@@ -45,7 +45,7 @@ print
4545
# coding=utf-8
4646
4747
from __future__ import division
48-
48+
4949
import sys,time
5050
j = '#'
5151
if __name__ == '__main__':
@@ -71,7 +71,7 @@ print
7171
# coding=utf-8
7272
7373
from __future__ import division
74-
74+
7575
import sys,time
7676
if __name__ == '__main__':
7777
for i in range(1,61):
@@ -94,7 +94,7 @@ print
9494
```
9595
# coding=utf-8
9696
97-
class progressbarClass:
97+
class progressbarClass:
9898
def __init__(self, finalcount, progresschar=None):
9999
import sys
100100
self.finalcount=finalcount
@@ -118,7 +118,7 @@ class progressbarClass:
118118
if not self.finalcount : return
119119
self.f.write('\n------------------- % Progress -------------------\n')
120120
return
121-
121+
122122
def progress(self, count):
123123
#
124124
# Make sure I don't try to go off the end (e.g. >100%)
@@ -132,19 +132,19 @@ class progressbarClass:
132132
if percentcomplete < 1: percentcomplete=1
133133
else:
134134
percentcomplete=100
135-
135+
136136
#print "percentcomplete=",percentcomplete
137137
blockcount=int(percentcomplete/2)
138138
#print "blockcount=",blockcount
139139
if blockcount > self.blockcount:
140140
for i in range(self.blockcount,blockcount):
141141
self.f.write(self.block)
142142
self.f.flush()
143-
143+
144144
if percentcomplete == 100: self.f.write("\n")
145145
self.blockcount=blockcount
146146
return
147-
147+
148148
if __name__ == "__main__":
149149
from time import sleep
150150
# pb=progressbarClass(8,"*")
@@ -165,28 +165,56 @@ if __name__ == "__main__":
165165
##################################################
166166
```
167167

168+
但是这样的最好看
169+
170+
```
171+
# -*- coding: utf-8 -*-
172+
from __future__ import division
173+
import sys
174+
import time
175+
176+
def progress():
177+
for i in range(100):
178+
sys.stdout.write("\r[%s%s] %2d%%" % ('█' * i, ' ' * (99 - i), (i / 99) * 100))
179+
sys.stdout.flush()
180+
time.sleep(0.1)
181+
sys.stdout.write('\n')
182+
183+
184+
if __name__ == '__main__':
185+
progress()
186+
187+
```
188+
189+
效果是这样
190+
191+
```
192+
# python download_progress_demo.py
193+
[███████████████████████████████████████████████████████████████████████████████████████████████████] 100%
194+
```
195+
168196
## progressbar
169197

170198
```
171199
# coding=utf-8
172200
173201
from __future__ import division
174-
202+
175203
import sys,time
176204
import progressbar
177205
total = 1000
178-
206+
179207
# 基本用法
180208
progress = progressbar.ProgressBar()
181209
for i in progress(range(total)):
182210
time.sleep(0.01)
183-
211+
184212
pbar = progressbar.ProgressBar().start()
185213
for i in range(1,1000):
186214
pbar.update(int((i/(total-1))*100))
187215
time.sleep(0.01)
188216
pbar.finish()
189-
217+
190218
# 高级用法
191219
widgets = ['Progress: ', progressbar.Percentage(), ' ', Bar(marker=progressbar.RotatingMarker('>-=')),
192220
' ', progressbar.ETA(), ' ', progressbar.FileTransferSpeed()]
@@ -200,7 +228,7 @@ pbar = progressbar.ProgressBar(maxval=100,widgets=[progressbar.Bar('=', '[', ']'
200228
for i in xrange(100):
201229
time.sleep(0.01)
202230
pbar.update(i+1)
203-
231+
204232
pbar.finish()
205233
```
206234

‎content/socket.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ class TransferFile(object):
978978
979979
@property
980980
def exists(self):
981-
return os.path.exists(self.fullname)
981+
return os.path.exists(self.fullname) and os.path.isfile(self.fullname)
982982
983983
@property
984984
def md5(self):
@@ -1059,7 +1059,7 @@ class TransferFile(object):
10591059
10601060
@property
10611061
def exists(self):
1062-
return os.path.exists(self.fullname)
1062+
return os.path.exists(self.fullname) and os.path.isfile(self.fullname)
10631063
10641064
@property
10651065
def md5(self):

‎content/threading.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,33 @@ if __name__ == '__main__':
482482

483483
如何在单线程中使每个请求都获得一份局部变量。
484484
- 将局部变量变成实例属性。
485+
486+
#### 线程数量
487+
488+
获得仍然存活的线程数量,其中包括主线程
489+
490+
```
491+
# -*- coding: utf-8 -*-
492+
493+
import os
494+
import time
495+
import random
496+
import threading
497+
498+
def long_time_task(name):
499+
print 'Running task %s (%s)' % (name, os.getpid())
500+
start = time.time()
501+
time.sleep(random.random() * 5)
502+
end = time.time()
503+
print 'Task %s run %0.2f econds.' % (name, end - start)
504+
505+
506+
if __name__ == '__main__':
507+
for i in xrange(10):
508+
threading.Thread(target=long_time_task, args=(str(i))).start()
509+
510+
for i in xrange(10):
511+
print threading.enumerate(), len(threading.enumerate())
512+
time.sleep(1)
513+
514+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /