小编SQA*_*777的帖子

有没有办法确定文件是YAML还是JSON格式?

我有一个需要配置文件的Python测试脚本.配置文件应采用JSON格式.

但是我的测试脚本的一些用户不喜欢JSON格式,因为它不可读.

所以我更改了我的测试脚本,以便它需要YAML格式的配置文件,然后将YAML文件转换为JSON文件.

我更喜欢加载配置文件以处理JSON和YAML的函数.如果配置文件是JSON或YAML,yaml或json模块中是否有一个方法可以给我一个布尔响应?

我现在的解决方法是使用两个try/except子句:

import os
import json
import yaml

# This is the configuration file - my script gets it from argparser but in
# this example, let's just say it is some file that I don't know what the format
# is
config_file = "some_config_file"

in_fh = open(config_file, "r")

config_dict = dict()
valid_json = True
valid_yaml = True

try:
    config_dict = json.load(in_fh)
except:
    print "Error trying to load the config file in JSON format"
    valid_json = False …
Run Code Online (Sandbox Code Playgroud)

python json yaml

4
推荐指数
2
解决办法
2529
查看次数

Python的time.sleep()挂起

我有一个测试脚本,可以在1到180秒之间随机睡眠几秒钟。测试程序将挂起3分钟以上。

因此,我更改了代码,使其休眠60秒(1分钟)并运行了脚本。20小时(!!!)之后,代码仍按time.sleep()挂起。

该代码是

downtime = 60
time.sleep(downtime)
Run Code Online (Sandbox Code Playgroud)

为什么Python会永远挂在time.sleep(60)上?

python sleep

1
推荐指数
3
解决办法
3982
查看次数

将向量的size()强制转换为int不起作用

我正在实时学习C ++,并且对向量有问题,所以我正在编写一些使用向量使它们熟悉的程序。

我遵循了这篇文章中有关打印出向量的size()调用值的建议:

如何获取std :: vector的大小作为int?

我的代码是一个简单的C ++代码:

#include <vector>

int main(int argc, char ** argv) {
   /* create an int vector of size 10, initialized to 0 */
   std::vector<int> int_list[10];
   int int_list_size;

   int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()
Run Code Online (Sandbox Code Playgroud)

我在Ubuntu 16.04上出现此错误:

"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'
Run Code Online (Sandbox Code Playgroud)

由于向量int_list的大小为10,因此size()不应该返回10,然后我可以将其转换为int吗?

c++ vector

-3
推荐指数
1
解决办法
93
查看次数

标签 统计

python ×2

c++ ×1

json ×1

sleep ×1

vector ×1

yaml ×1