python编程lcd显示_Python api.lcd方法代码示例

该博客演示了如何使用Python的fabric库中的lcd方法来运行Cooja模拟实验。在实验中,对带有和不带恶意节点的情况进行了模拟,并捕获了运行结果。如果遇到FATAL或ERROR,会记录错误信息并中断执行。执行结束后,将截图整合成GIF,并保存关键帧到结果文件夹。最后,执行解析函数生成更多结果和PDF报告。
摘要由CSDN通过智能技术生成

# 需要导入模块: from fabric import api [as 别名]

# 或者: from fabric.api import lcd [as 别名]

def __run(name, **kwargs):

"""

Run an experiment.

:param name: experiment name

:param path: expanded path of the experiment (dynamically filled in through 'command' decorator with 'expand')

:param kwargs: simulation keyword arguments (see the documentation for more information)

"""

set_logging(kwargs.get('loglevel'))

path = kwargs['path']

check_structure(path, remove=True)

with settings(hide(*HIDDEN_ALL), warn_only=True):

for sim in ["without", "with"]:

sim_path = join(path, "{}-malicious".format(sim))

data, results = join(sim_path, 'data'), join(sim_path, 'results')

# the Makefile is at experiment's root ('path')

logger.debug(" > Running simulation {} the malicious mote...".format(sim))

task = kwargs.get('task', "run")

with lcd(sim_path):

output = local("make run TASK={}".format(task), capture=True)

remove_files(sim_path, '.{}'.format(task))

error, interrupt, error_buffer = False, False, []

for line in output.split('\n'):

if line.strip().startswith("FATAL") or line.strip().startswith("ERROR"):

error, interrupt = True, True

elif line.strip().startswith("INFO"):

error = False

if len(error_buffer) > 0:

logger.error('Cooja error:\n' + '\n'.join(error_buffer))

error_buffer = []

if error:

error_buffer.append(line)

if interrupt:

logger.warn("Cooja failed to execute ; 'run' interrupted (no parsing done)")

raise Exception("Cooja failed to execute")

# once the execution is over, gather the screenshots into a single GIF and keep the first and

# the last screenshots ; move these to the results folder

logger.debug(" > Gathering screenshots in an animated GIF...")

with lcd(data):

local('convert -delay 10 -loop 0 network*.png wsn-{}-malicious.gif'.format(sim), capture=True)

network_images = {int(fn.split('.')[0].split('_')[-1]): fn for fn in listdir(data)

if fn.startswith('network_')}

move_files(data, results, 'wsn-{}-malicious.gif'.format(sim))

if len(network_images) > 0:

net_start_old = network_images[min(network_images.keys())]

net_start, ext = splitext(net_start_old)

net_start_new = 'wsn-{}-malicious_start{}'.format(sim, ext)

net_end_old = network_images[max(network_images.keys())]

net_end, ext = splitext(net_end_old)

net_end_new = 'wsn-{}-malicious_end{}'.format(sim, ext)

move_files(data, results, (net_start_old, net_start_new), (net_end_old, net_end_new))

remove_files(data, *network_images.values())

# then start the parsing functions to derive more results

parsing_chain(sim_path, logger)

move_files(sim_path, results, 'COOJA.log')

# finally, generate the PDF report

generate_report(path, REPORT_THEME)

return "Both Cooja executions succeeded"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值