编辑:我不认为我的问题是重复的(见下面的评论),因为这个问题假设你已经知道“位置参数”的存在。如果我知道位置论点--更好--甚至在下面的最初评论中犯了一个错误--如果我知道了keyword arguments,我就不需要问这个问题了!我所能想到的就是“看上去像一项任务”,就像我在C中看到/知道的。
附注:我读过其他答案--如果没有下面的答案,我现在就不会用答案来回答我的问题“如何阅读”这段代码。下面的答案给了我一个“终身”的理解,使我能够阅读foo(a=b),然后继续前进。此外,我还学习了“关键字论证”这个术语--但我也很高兴,不知道:)
我也觉得它更简洁-直接回答我的需要。我将不得不花更多的时间来学习我从这个答案中学到的东西--从“其他”答案中学到的东西。
问题的关键是:考虑到python调用: util.find_devs_with(path=device),实际发生了什么(下面的代码细节/摘录)?
或者它是否做了以下工作:
# blkid -t/dev/sr0 2&>/dev/null
# echo $?
4
# blkid -tpath=/dev/sr0 2&>/dev/null
# echo $?
2详细信息
POSSIBLE_MOUNTS = ('sr', 'cd')
OPTICAL_DEVICES = tuple(('/dev/%s%s' % (z, i) for z in POSSIBLE_MOUNTS
for i in range(0, 2)))
...
def find_candidate_devs(probe_optical=True):
"""Return a list of devices that may contain the config drive.
The returned list is sorted by search order where the first item has
should be searched first (highest priority)
config drive v1:
Per documentation, this is "associated as the last available disk on the
instance", and should be VFAT.
Currently, we do not restrict search list to "last available disk"
config drive v2:
Disk should be:
* either vfat or iso9660 formated
* labeled with 'config-2'
"""
if probe_optical:
for device in OPTICAL_DEVICES:
try:
util.find_devs_with(path=device)
except util.ProcessExecutionError:
pass
...在util.py中
def find_devs_with(criteria=None, oformat='device',
tag=None, no_cache=False, path=None):
"""
find devices matching given criteria (via blkid)
criteria can be *one* of:
TYPE=<filesystem>
LABEL=<label>
UUID=<uuid>
"""
blk_id_cmd = ['blkid']
options = []
if criteria:
# Search for block devices with tokens named NAME that
# have the value 'value' and display any devices which are found.
# Common values for NAME include TYPE, LABEL, and UUID.
# If there are no devices specified on the command line,
# all block devices will be searched; otherwise,
# only search the devices specified by the user.
options.append("-t%s" % (criteria))
...
cmd = blk_id_cmd + options
# See man blkid for why 2 is added
try:
(out, _err) = subp(cmd, rcs=[0, 2])
except ProcessExecutionError as e:
if e.errno == errno.ENOENT:
# blkid not found...
out = ""
else:
raise
entries = []
for line in out.splitlines():
line = line.strip()
if line:
entries.append(line)
return entries我的期望是正在执行的命令相当于:
blkid "-t%s" % criteria那么,“接收”作为标准的价值是什么呢?也就是巨蟒论证“堆叠?”的一课。
第二次检查所有这些代码时,我猜参数'path=device‘只是"somegarbage“,因此会调用blkid -tsomegarbage,并在填充blkid缓存后返回一个错误。
发布于 2016-12-09 14:03:09
在Python中,可以用默认值分配参数。鉴于这一签名:
def find_devs_with(criteria=None, oformat='device',
tag=None, no_cache=False, path=None):如果您调用它时:
util.find_devs_with(path=device)则存在下列作业:
criteria = None
oformat = 'device'
tag = None
no_cache = False
path = device # according to the above code, /dev/sr0, ..., /dev/cd1因为criteria是None,所以没有附加-t选项。执行的命令是简单的blkid。
https://stackoverflow.com/questions/41062897
复制相似问题