[python]ÖØ¶¨ÏòÊä³ö
µ÷ÓÃÒ»¸ö¿ØÖÆÌ¨³ÌÐò£¬»ñÈ¡ËüµÄ±ê×¼Êä³ö£¬»ò°ÑËüµÄ±ê×¼Êä³öÖØ¶¨Ïòµ½½çÃæÉÏ£¬ÕâÀïÖ»½éÉÜÈçºÎ»ñÈ¡ËüµÄ±ê×¼Êä³ö£¬ÒòΪÔÀí¶¼Ò»ÑùµÄ¡£
ʹÓÃpython2.5µÄsubprocessÄ£¿éÀ´ÊµÏÖ¡£
import sys
import subprocess
def RunShellWithReturnCode(command, print_output=False,
universal_newlines=True):
"""Executes a command and returns the output from stdout and the return code.
Args:
command: Command to execute.
print_output: If True, the output is printed to stdout.
If False, both stdout and stderr are ignored.
universal_newlines: Use universal_newlines flag (default: True).
Returns:
Tuple (output, return code)
"""
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=use_shell, universal_newlines=universal_newlines)
if print_output:
output_array = []
while True:
line = p.stdout.readline()
if not line:
break
print line.strip("\n")
output_array.append(line)
output = "".join(output_array)
else:
output = p.stdout.read()
p.wait()
errout = p.stderr.read()
#if print_output and errout:
# print >>sys.stderr, errout
p.stdout.close()
p.stderr.close()
return output, p.returncode
def RunShell(command, silent_ok=False, universal_newlines=True,
print_output=False):
data, retcode = RunShellWithReturnCode(command, print_output,
universal_newlines)
if retcode:
ErrorExit("Got error status from %s:\n%s" % (command, data))
if not silent_ok and not data:
ErrorExit("No output from %s" % command)
return data
ÉÏÃæµÄ´úÂëÀ´×ÔÓÚ ¿ªÔ´µÄ´úÂëÉó²é¹¤¾ß rietveld µÄ´úÂ룬ÆäÖÐRunShellWithReturnCodeº¯Êý ¾ÍÊÇ·µ»Ø¿ØÖÆÌ¨µÄ±ê×¼Êä³öºÍ·µ»Ø½á¹û
Ïà¹ØÎĵµ£º
´´½¨Áбí
sample_list = ['a',1,('a','b')]
Python Áбí²Ù×÷
sample_list = ['a','b',0,1,3]
µÃµ½ÁбíÖеÄijһ¸öÖµ
value_start = sample_list[0]
end_value =
sample_list[-1]
ɾ³ýÁбíµÄµÚÒ»¸öÖµ
del sample_list[0]
ÔÚÁбíÖвåÈëÒ»¸öÖµ
sample_list[0:0] = ['sample value']
µÃµ½ÁбíµÄ³¤¶È
list_length = ......
1 Ó°ÏñÓëͼÐÎ×ÊÁϵĴ¦Àí
ÌÖÂÛͼÐÎ(Ó°Ïñ) ±¾ÉíµÄ´¦Àí£¬¶øÌÖÂÛµÄÄÚÈݽ«»á¼¯ÖÐÔÚPython Imaging Library (PIL) ÕâÒ»Ì׳Ìʽ¿âÉÏ¡£
PIL ÊÇPython ÏÂ×îÓÐÃûµÄÓ°Ïñ´¦ÀíÌ×¼þ£¬ÓÉÐí¶à²»Í¬µÄÄ£×éËù×é³É£¬²¢ÇÒÌṩÁËÐí¶àµÄ´¦Àí¹¦ÄÜ£¬ÔÊÐíÎÒÃÇÔÚ¼òµ¥µÄPython ³ÌʽÀï½øÐÐÓ°ÏñµÄ´¦Àí¡£ ʹÓÃÏñPIL ÐíÑùµÄ³Ìʽ¿âÌ ......
Python±ê×¼¿â-randomÄ£¿é
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉú³ÉÆ÷. »ù±¾Ëæ»úÊýÉú³ÉÆ÷(»ùÓÚ Wichmann ºÍ Hill , 1982 µÄÊýѧÔËËãÀíÂÛ) ¿ÉÒÔͨ¹ýºÜ¶à·½·¨·ÃÎÊ, Èç Example 2-29 Ëùʾ. 2.17.0.1. Example 2-29. ʹÓà random Ä£¿é»ñµÃËæ»úÊý×Ö File: random-example-1.py import random for i i
¡¡¡¡
random Ä£¿é°üº¬Ðí¶àËæ»úÊýÉ ......
PythonÖÐRangeºÍXRangeµÄÇø±ð(Difference between Range and XRange in Python)
×î½ü»úÆ÷³öÁ˵ãÎÊÌ⣬ËùÒÔһֱûÓÐдÐµĶ«Î÷³öÀ´¡£Ö®Ç°ÔÚ¶ÁPythonµÄ´úÂëµÄʱºò£¬·¢¾õºÃ¶àÈËϲ»¶ÓÃXRange£¬¶ø²»ÊÇRange£¬ÎÒÒ²Ö»ÊǼǵÃѧϰµÄʱºò¿ªÊ¼Ñ§µ½µÄÊÇRange£¬ºóÃæÓÖ¿´µ½ÓиöXRange£¬µ±Ê±Ò²Ã»ÓÐÉ£¬ÎªÊ²Ã´PythonÀïÃæÒªÓÐÁ½¸öͬÑùµÄ¹¦ ......
David ÔÚ±¾ÎÄÖн«´øÁìÎÒÃÇÁ˽âһϠsetuptools ¿ò¼Ü£¬ËüÊÇ Python Enterprise Application Kit£¨PEAK£©µÄÒ»¸ö¸±ÏîÄ¿¡£ setuptools Ìæ»»Á˱ê×¼µÄ distutils ¿â£¬²¢Îª Python Ìí¼ÓÁ˰汾»¯µÄ°üºÍÒÀÀµÐÔ¹ÜÀí¡£Perl Óû§±È½ÏÊìϤ CPAN£¬¶ø Ruby Óû§Ôò±È½ÏÊìϤ Gems£»Òýµ¼ setuptools µÄ ez_setup ¹¤¾ßºÍËæÖ®¶øÉúµÄÀ©Õ¹ºóµÄ eas ......