揭示yield关键字的神秘面纱,cmd命令调用

2019-11-14 22:12栏目:金沙网址大全
TAG:

pip install googleapis-common-protos

不相同的是进行进度中境遇yield关键字,会阻断,yield 再次回到的是多个生成器。

举个栗子:

python cmd命令调用

关于python调用cmd命令:

关键介绍三种艺术:

1.python的OS模块。

  OS模块调用CMD命令有两种方法:os.popen(),os.system(). 都以用当下经过来调用。

  os.system是力不能支赢得再次回到值的。当运维甘休后继之往下边实行顺序。用法如:OS.system("ipconfig").

  OS.popen带再次来到值的,怎么样得到重回值。如

  p=os.popen(cmd)

  print p.read().拿到的是个字符串。

  那八个都是用当下进度来调用,约等于说它们都以窒碍式的。

2.管道subprocess模块。

  运转规律会在当下历程下边发生子进度。

  sub=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)

  sub.wait()

  print sub.read()

使用base64

 

注意,当函数中冒出yield,该函数再次来到的就是三个生成器。不在是平常函数。

那样的话,动脑是否用处非常多,作者得以把艺术名配置到文件中,读取时选取getattr动态去调用。

1.图形转成字节使用:  base64.b64encode()

 

 

#coding=utf-8

class pyMethod(object):
    def outstr(self):
        print('this is string')

    def outint(self):
        print('this is number')

    def outdate(self):
        print('this is date')


if __name__=="__main__":
    str = 'int'
    getattr(pyMethod(),'out%s'%str)()     
    str = 'str'
    getattr(pyMethod(),'out%s'%str)()
    str = 'date'
    getattr(pyMethod(),'out%s'%str)()
iVBORw0KGgoAAAANSUhEUgAAAJYAAAAyCAYAAAC+jCIaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAj3SURBVHhe7ZzRdeI6FEVTzWuD31TC13ymifSQBqgiX+kj08HrgMcFtny1ufIQYAgvy1prrzM6R7KFrRHGmDz9+8+v7cLCrdlPrCi/f/9uLPWlfk29TawcogYfNfiowUcNPmrwozw9PXU4p08GHzX4KHhfkNsE9EMNPmrwUYOPGnzU4KMGHzX4qMGP0k2sHI54lHx0kslH3DL3GEzuB7l/xU/JH2r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YOPmihtf0ecZ81vj7GaTv9Zdsy8baMGHzX4qMFHDT6amZ1YQdUp89V8dHBHnJO3g5/I+f7f+7ee1XY1+BpnxLU5Y/G4IE8ef+CorrHMn/b/XfldVywfWHzU4KMGf/qp2nTd1E5I4nUzXddEu9b/qAYfNfioieIxtHw30Z+b378tw49YsRzesn5yUJVfXU93rn0hzJfNcUFPm8B3tKf2t63nfQaHvP+jdXmS5/7dJ8f/8zUWIWrwUYOPGnzU4KMGH210f7gk3Uw8+ky2dkH8/Lb9iAv59LhMwHZRg48afNTg5xU23xpp+VG7ibVbsfBRg48afNTgowYfNfhRuomVwxGPmvf3hSam65b+MZnNur7XNdo+3CLvrp0GN2kDTyznFaN82t+Bqk0w6g/n5j9jxTrSf0Q/nTg59z2sgO2iBh81+KjB766dipujB+2/L7x2xZr2d8B5QD/U4KMGP0qbWFGqRkv9L9RPnmbQUwzV0w5XXmOxHf7t/Nb1bsUyNEYNPmrwUYOPGnzU4KMGHzX4qMFHDT5q8NHM6K0b9hf0+YvqtLIB20UNfhRv3zl9MviowUczsxMrqDpllvyKvFqZdFuhn1ynbWa3v+O78mXFSmrwUYOPGnzU4KMGHzX4qMFHDT5q8FGDj2baxFpYuC2/tv8BB256mb/wMqgAAAAASUVORK5CYII=

rpc相关文书档案:

 

getattr()这么些主意最根本的作用是促成反射机制。约等于说能够由此字符串获取方式实例。  传入差别的字符串,调用的办法分化样。

图表字节串:

python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. data.proto

答案:能够精晓yield是三个生成器;

 

from PIL import Image
import base64
img = base64.b64decode(res)

file = open('test.jpg','wb')
file.write(img)
file.close()

金沙网址大全 ,https://grpc.io/docs/tutorials/basic/python.html

先是次迭代中你的函数会执行,从上马达到 yield 关键字,然后再次来到 yield 后的值作为第三回迭代的回来值.

不前行,不倒退,甘休的情景是不曾的.

 

揭示yield关键字的神秘面纱,cmd命令调用。 

除了那么些之外for循环取值,你也能够经过next()来取下一个值。

pyMethod类下定义了几个方式,getattr(pyMethod(),'out%s'%str)()   传入的措施名区别,调用不一致的艺术。些处方法名叫字符串。

python 字节转变来图像

data_pb2_grpc.py方法映射

 

运行结果:

res为图片字节串

版权声明:本文由金沙网址大全发布于金沙网址大全,转载请注明出处:揭示yield关键字的神秘面纱,cmd命令调用