本文整理汇总了Python中sys.stdout.write函数的典型用法代码示例。如果您正苦于以下问题:Python write函数的具体用法?Python write怎么用?Python write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: out
def out(*text):
if isinstance(text, str):
stdout.write(text)
else:
for c in text:
stdout.write(str(c))
stdout.flush()
示例2: refresh_articles
def refresh_articles(articles, feeds, urls):
for author, feed_urls in feeds.items():
articles[author] = []
relevant_urls = [url for url in feed_urls
if url in urls]
for url in relevant_urls:
stdout.write('parsing feed at %s.\n' % url)
feed = feedparser.parse(urls[url]['data'])
for entry in feed['entries']:
updated = entry.get('updated_parsed')
updated = date(year=updated.tm_year,
month=updated.tm_mon,
day=updated.tm_mday)
content = entry.get('content', '')
summary = entry.get('summary', '')
summary_detail = entry.get('summary_detail', {})
if not content:
if not (summary_detail and
summary_detail.get('value')):
if not summary:
pass
else:
content = [{'type': 'text/plain',
'value': summary}]
else:
content = [summary_detail]
if content:
article = {'url': entry.get('link'),
'title': entry.get('title'),
'pub_date': updated,
'content': content}
articles[author].append(article)
示例3: __stream_audio_realtime
def __stream_audio_realtime(filepath, rate=44100):
total_chunks = 0
format = pyaudio.paInt16
channels = 1 if sys.platform == 'darwin' else 2
record_cap = 10 # seconds
p = pyaudio.PyAudio()
stream = p.open(format=format, channels=channels, rate=rate, input=True, frames_per_buffer=ASR.chunk_size)
print "o\t recording\t\t(Ctrl+C to stop)"
try:
desired_rate = float(desired_sample_rate) / rate # desired_sample_rate is an INT. convert to FLOAT for division.
for i in range(0, rate/ASR.chunk_size*record_cap):
data = stream.read(ASR.chunk_size)
_raw_data = numpy.fromstring(data, dtype=numpy.int16)
_resampled_data = resample(_raw_data, desired_rate, "sinc_best").astype(numpy.int16).tostring()
total_chunks += len(_resampled_data)
stdout.write("\r bytes sent: \t%d" % total_chunks)
stdout.flush()
yield _resampled_data
stdout.write("\n\n")
except KeyboardInterrupt:
pass
finally:
print "x\t done recording"
stream.stop_stream()
stream.close()
p.terminate()
示例4: _windows_shell
def _windows_shell(self, chan):
import threading
stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
def writeall(sock):
while True:
data = sock.recv(256)
if not data:
stdout.write("\r\n*** EOF ***\r\n\r\n")
stdout.flush()
break
stdout.write(data)
stdout.flush()
writer = threading.Thread(target=writeall, args=(chan,))
writer.start()
try:
while True:
d = stdin.read(1)
if not d:
break
chan.send(d)
except EOFError:
# user hit ^Z or F6
pass
示例5: main
def main():
if len(argv) >= 2:
out = open(argv[1], 'a')
else:
out = stdout
fmt1 = "{0:<16s} {1:<7s} {2:<7s} {3:<7s}\n"
fmt2 = "{0:<16s} {1:7d} {2:7d} {3:6.2f}%\n"
underlines = "---------------- ------- ------- -------".split()
out.write(fmt1.format(*"File Lines Covered Percent".split()))
out.write(fmt1.format(*underlines))
total_lines, total_covered = 0, 0
for percent, file, lines in sorted(coverage()):
covered = int(round(lines * percent / 100))
total_lines += lines
total_covered += covered
out.write(fmt2.format(file, lines, covered, percent))
out.write(fmt1.format(*underlines))
if total_lines == 0:
total_percent = 100.0
else:
total_percent = 100.0 * total_covered / total_lines
summary = fmt2.format("COVERAGE TOTAL", total_lines, total_covered,
total_percent)
out.write(summary)
if out != stdout:
stdout.write(summary)
示例6: waitServe
def waitServe(servert):
""" Small function used to wait for a _serve thread to receive
a GET request. See _serve for more information.
servert should be a running thread.
"""
timeout = 10
status = False
try:
while servert.is_alive() and timeout > 0:
stdout.flush()
stdout.write("\r\033[32m [%s] Waiting for remote server to "
"download file [%ds]" % (utility.timestamp(), timeout))
sleep(1.0)
timeout -= 1
except:
timeout = 0
if timeout is not 10:
print ''
if timeout is 0:
utility.Msg("Remote server failed to retrieve file.", LOG.ERROR)
else:
status = True
return status
示例7: testSoftmaxMNIST
def testSoftmaxMNIST():
x_, y_ = getData("training_images.gz", "training_labels.gz")
N = 600
x = x_[0:N].reshape(N, 784).T/255.0
y = np.zeros((10, N))
for i in xrange(N):
y [y_[i][0]][i] = 1
#nn1 = SimpleNN(784, 800, 10, 100, 0.15, 0.4, False)
#nn2 = SimpleNN(784, 800, 10, 1, 0.15, 0.4, False)
nn3 = Softmax(784, 800, 1, 10, 0.15, 0, False)
nn4 = Softmax(784, 800, 10, 10, 0.35, 0, False)
#nn1.Train(x, y)
#nn2.Train(x, y)
nn3.Train(x, y)
nn4.Train(x, y)
N = 10000
x_, y_ = getData("test_images.gz", "test_labels.gz")
x = x_.reshape(N, 784).T/255.0
y = y_.T
correct = np.zeros((4, 1))
print "Testing"
startTime = time()
for i in xrange(N):
#h1 = nn1.Evaluate(np.tile(x.T[i].T, (1, 1)).T)
#h2 = nn2.Evaluate(np.tile(x.T[i].T, (1, 1)).T)
h3 = nn3.Evaluate(np.tile(x.T[i].T, (1, 1)).T)
h4 = nn4.Evaluate(np.tile(x.T[i].T, (1, 1)).T)
#if h1[y[0][i]][0] > 0.8:
# correct[0][0] += 1
#if h2[y[0][i]][0] > 0.8:
# correct[1][0] += 1
if h3[y[0][i]][0] > 0.8:
correct[2][0] += 1
if h4[y[0][i]][0] > 0.8:
correct[3][0] += 1
if(i > 0):
stdout.write("Testing %d/%d image. Time Elapsed: %ds. \r" % (i, N, time() - startTime))
stdout.flush()
stdout.write("\n")
#print "Accuracy 1: ", correct[0][0]/10000.0 * 100, "%"
#print "Accuracy 2: ", correct[1][0]/10000.0 * 100, "%"
print "Accuracy 3: ", correct[2][0]/10000.0 * 100, "%"
print "Accuracy 4: ", correct[3][0]/10000.0 * 100, "%"
示例8: add_column
def add_column(column_title, column_data):
# This is for the column TITLE - eg. Title could be Animal Names
for m in column_title:
length = len(m)
stdout.write('+' + '=' * (length + 2) + '+')
print('\r')
for m in column_title:
line_pos_right = ' |'
stdout.write('| ' + m + ' |')
print('\r')
for m in column_title:
stdout.write('+' + '=' * (length + 2) + '+')
print('\r')
# ----------------------------------------------------
# This is for the DATA of the column - eg. Label == Animal Names, the Data would be Sparkles, Hunter, etc.
for m in column_data:
this_length = len(m)
if this_length < length:
wanted_l = length - this_length
elif this_length > length:
wanted_l = this_length - length
stdout.write('| ' + m + ' ' * (wanted_l) + ' |')
print('\r')
stdout.write('+' + '-' * (length + 2) + '+')
示例9: statusBar
def statusBar(step, total, bar_len=20, onlyReturn=False):
"""
print a ASCI-art statusbar of variable length e.g.showing 25%:
>>> step = 25
>>> total = 100
>>> print( statusBar(step, total, bar_len=20, onlyReturn=True) )
\r[=====o---------------]25%
as default onlyReturn is set to False
in this case the last printed line would be flushed every time when
the statusbar is called to create a the effect of one moving bar
"""
norm = 100.0 / total
step *= norm
step = int(step)
increment = 100 // bar_len
n = step // increment
m = bar_len - n
text = "\r[" + "=" * n + "o" + "-" * m + "]" + str(step) + "%"
if onlyReturn:
return text
stdout.write(text)
stdout.flush()
示例10: log
def log(logtype, message):
func = inspect.currentframe().f_back
log_time = time.time()
if logtype != "ERROR":
stdout.write('[%s.%s %s, line:%03u]: %s\n' % (time.strftime('%H:%M:%S', time.localtime(log_time)), str(log_time % 1)[2:8], logtype, func.f_lineno, message))
else:
stderr.write('[%s.%s %s, line:%03u]: %s\n' % (time.strftime('%H:%M:%S', time.localtime(log_time)), str(log_time % 1)[2:8], logtype, func.f_lineno, message))
示例11: download_json_files
def download_json_files():
if not os.path.exists('/tmp/xmltv_convert/json'):
os.makedirs('/tmp/xmltv_convert/json')
page = urllib2.urlopen('http://json.xmltv.se/')
soup = BeautifulSoup(page)
soup.prettify()
for anchor in soup.findAll('a', href=True):
if anchor['href'] != '../':
try:
anchor_list = anchor['href'].split("_")
channel = anchor_list[0]
filedate = datetime.datetime.strptime(anchor_list[1][0:10], "%Y-%m-%d").date()
except IndexError:
filedate = datetime.datetime.today().date()
if filedate >= datetime.datetime.today().date():
if len(channels) == 0 or channel in channels or channel == "channels.js.gz":
stdout.write("Downloading http://xmltv.tvtab.la/json/%s " % anchor['href'])
f = urllib2.urlopen('http://xmltv.tvtab.la/json/%s' % anchor['href'])
data = f.read()
with open('/tmp/xmltv_convert/json/%s' % anchor['href'].replace('.gz', ''), 'w+ ') as outfile:
outfile.write(data)
stdout.write("Done!\n")
stdout.flush()
示例12: setup
def setup(self, type="constant", verbose=False):
self.type = type
self.verbose = verbose
if self.type is "constant":
# Set up the "data"
ny = 10
val, sigma = self.get_truepars()
self.true_pars = val
self.true_error = sigma
self.npars = 1
self.npoints = ny
self.y = zeros(ny, dtype="f8")
self.y[:] = val
self.y[:] += sigma * randn(ny)
self.yerr = zeros(ny, dtype="f8")
self.yerr[:] = sigma
self.ivar = 1.0 / self.yerr ** 2
# step sizes
self.step_sizes = sigma / sqrt(ny)
# self.parguess=val + 3*sigma*randn()
self.parguess = 0.0
if verbose:
stdout.write(' type: "constant"\n')
stdout.write(
" true_pars: %s\n npars: %s\n step_sizes: %s\n" % (self.true_pars, self.npars, self.step_sizes)
)
示例13: main
def main(argv=None):
params = Params()
try:
if argv is None:
argv = sys.argv
args,quiet = params.parse_options(argv)
params.check()
inputfile = args[0]
try:
adapter = args[1]
except IndexError:
adapter = "AGATCGGAAGAGCACACGTCTGAACTCCAGTCAC" #default (5' end of Illimuna multiplexing R2 adapter)
if quiet == False:
stdout.write("Using default sequence for adapter: {0}\n".format(adapter))
stdout.flush()
unique = analyze_unique(inputfile,quiet)
clip_min,error_rate = analyze_clip_min(inputfile,adapter,quiet)
return clip_min,unique,error_rate
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, ""
return 2
示例14: progress
def progress (itr):
t0 = time()
for i in itr:
stdout.write ('.')
stdout.flush ()
yield i
stdout.write ('[%.2f]\n' %(time()-t0))
示例15: segment
def segment(text):
if not args.files and args.with_ids:
tid, text = text.split('\t', 1)
else:
tid = None
text_spans = rewrite_line_separators(
normal(text), pattern, short_sentence_length=args.bracket_spans
)
if tid is not None:
def write_ids(tid, sid):
stdout.write(tid)
stdout.write('\t')
stdout.write(str(sid))
stdout.write('\t')
last = '\n'
sid = 1
for span in text_spans:
if last == '\n' and span not in ('', '\n'):
write_ids(tid, sid)
sid += 1
stdout.write(span)
if span:
last = span
else:
for span in text_spans:
stdout.write(span)