PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Python基础学习代码之执行环境

黄舟
黄舟 原创
2016-12-29 17:21:09 1082浏览

class C(object):
    def __call__(self, *args, **kwargs):
        print "I'm callable! called with args:\n",args
c = C()
c('a',1)
single_code = compile("print 'hello,world!'",'','single')
exec(single_code)
eval_code = compile('100*3','','eval')
print eval(eval_code)

#exec_code = compile("""req = input('input:')
#for eachnum in range(req):
#    print eachnum""",'','exec')
#exec(exec_code)
exec """x = 0
print 'x is currently:',x
while x < 5:
    x+=1
    print &#39;incrementing x to:&#39;,x
    """
#f = open(&#39;c14.py&#39;)
#exec f
#print f.tell()
#print f.close()
#from os.path import getsize
#getsize(&#39;c14.py&#39;)
#f.seek(0)
#exec f

#loopmake
dashes = &#39;\n&#39; + &#39;-&#39; * 50
exec_dict = {
    &#39;f&#39;:"""
    for %s in %s:
        print %s
        """,
    &#39;s&#39;:"""
    %s = 0
    %s = %s
    while %s < len(%s):
        print %s[%s]
        %s = %s + 1
        """,
    &#39;n&#39;:"""
    %s = %d
    while %s < %d:
        print %s
        %s = %s + %d
        """
        }
def main():
    ltype = raw_input(&#39;Loop type?[for/while]&#39;)
    dtype = raw_input(&#39;Data type?[number/seq]&#39;)
    if dtype == &#39;n&#39;:
        start = input(&#39;start value?:&#39;)
        stop = input(&#39;ending value?:&#39;)
        step = input(&#39;steping value?:&#39;)
        seq = str(range(start,stop,step))

def foo():
    return True
def bar():
    &#39;bar() does not much&#39;
    return True
foo.__doc__ = &#39;foo() does not much&#39;
foo.tester = """
if foo():
    print &#39;passed&#39;
else:
    print &#39;failed&#39;
"""
for eachattr in dir():
    obj = eval(eachattr)
    if isinstance(obj,type(foo)):
        if hasattr(obj,&#39;__doc__&#39;):
            print &#39;\nfunction "%s" has a doc string:\n\t%s&#39; % (eachattr,obj.__doc__)
        if hasattr(obj,&#39;tester&#39;):
            print &#39;\nfunction "%s" has tester&#39; % eachattr
            exec(obj.tester)
        else:
            print &#39;%s function has no tester&#39; % eachattr
    else:
        print &#39;%s is not a function&#39; % eachattr

import os
#print os.system(&#39;ping www.qq.com&#39;)

f = os.popen(&#39;dir&#39;)
data = f.readlines()
f.close()
print data

## 替换os.system
from subprocess import call
res = call((&#39;dir&#39;),shell=True)

## 替换os.popen
from subprocess import PIPE,Popen
f = Popen((&#39;wmic&#39;,&#39;diskdrive&#39;),stdout=PIPE).stdout
data = f.readlines()
f.close()
print data

import sys
def usage():
    print &#39;At least 2 arguments&#39;
    print &#39;usage: args.py arg1 arg2 [arg3....]&#39;
    sys.exit(1)
argc = len(sys.argv)
#if argc < 3:
#    usage()

prev_exit_func = getattr(sys,&#39;exitfunc&#39;,None)
def my_exit_func(old_exit=prev_exit_func):
    if old_exit is not None and callable(old_exit):
        old_exit()
        sys.exitfunc = my_exit_func

def my_exit():
    print &#39;exit python&#39;
sys.exitfunc = my_exit
print &#39;hello,begin exit.&#39;
sys.exit(1)

 以上就是python基础学习代码之执行环境的内容,更多相关内容请关注php中文网(www.php.cn)!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。