包资源和符号加载帮助程序库。
asset的Python项目详细描述
从python包加载资源和符号,无论是否已安装 作为一个目录,一个鸡蛋,或源形式。也提供了一些其他 包相关的帮助程序方法,包括asset.version(), asset.caller(),和asset.chunks()。
tl;dr
安装:
$ pip install asset
通过从包中加载符号(例如函数、类或变量) 姓名:
importasset# load the 'mypackage.foo.myfunc' function and call it with some parameterretval=asset.symbol('mypackage.foo.myfunc')(param='value')
从包中加载数据文件:
# load the file 'mypackage/templates/data.txt' into stringdata=asset.load('mypackage:templates/data.txt').read()# or as a file-like streamstream=asset.load('mypackage:templates/data.txt').stream()data=stream.read()
使用globre样式通配符可以同时操作多个文件:
# concatenate all 'css' files into one string:css=asset.load('mypackage:static/style/**.css').read()# load all '.txt' files, XML-escaping the data and wrapping# each file in an <node name="...">...</node> element.importxml.etree.ElementTreeasETdata=ET.Element('nodes')foriteminasset.load('asset:**.txt'):cur=ET.SubElement(data,'node',name=item.name)cur.text=item.read()data=ET.tostring(data)
查询软件包的安装版本:
asset.version('asset')# ==> '0.0.5'asset.version('python')# ==> '2.7'asset.version('no-such-package')# ==> None
找出调用当前函数的包:
# assuming the call stack is:# in package "zig" a function "x", which calls# in package "bar" a function "y", which calls# in package "foo" a function "callfoo" defined as:defcallfoo():asset.caller()# ==> 'bar'asset.caller(ignore='bar')# ==> 'zig'asset.caller(ignore=['bar','zig'])# ==> None
调用给定组的所有插件:
forplugininasset.plugins('mypackage.plugins'):plugin.handle()
通过给定组的所有插件筛选对象(如果有 不是插件,这只会返回thing):
result=asset.plugins('mypackage.plugins').filter(thing)
加载所有注册的插件,选择名为foo的插件并调用 它们(如果没有foo插件,这将失败):
result=asset.plugins('mypackage.plugins').select('foo').handle(thing)
将文件(或任何类似文件的对象)分为1个kib块:
withopen('/var/binary/data','rb')asfp:forchunkinasset.chunks(fp,1024):# ... do something with `chunk` ...
将资产流分块(这里使用.chunksalias方法):
forchunkinasset.load('mypackage:data/**.bin').chunks():# ... using the default chunk size (usually 8 KiB) ...
测试
为了正确运行单元测试,需要 作为压缩包(即“egg”)和globre安装 包需要解压缩安装为此,请执行以下操作:
$ easy_install --zip-ok pxml $ easy_install --always-unzip globre
原因是单元测试确认asset可以加载资产 从压缩包和解压缩包中,还可以识别 操作模式。
详细信息
待办事项:添加详细文档…
Asset.filename:
如果资产表示文件系统上的一个文件,则 指定文件的路径。否则就是None
AssetGroupStream.readline():
返回聚合资产组流中的下一行,就像 这些资产已合并成一项资产。
重要:如果资产以未终止的内容结尾 通过eol令牌,它按原样返回,即它不附加 下一个资产的第一行。
注意:因为asset.load()执行延迟加载,所以它只抛出 nosuchasset实际尝试使用 装配组!如果需要立即出错,请使用peek()方法。 请注意,它返回自身,因此您可以执行以下操作:
importassetdefmy_function_that_returns_an_iterable():returnasset.load(my_spec).peek()# this returns exactly the same thing as the following:## return asset.load(my_spec)## but throws an exception early if there are no matching assets.