使用列表作为函数中的参数的Pylint错误:错误处理缩进/坏连续。无法找出原因

时间:2014-08-04 15:28:25

标签: python pylint pep8

我对Pylint有疑问。我收到错误“错误处理缩进”与“坏连续”。我想知道我做错了什么。我试过阅读PEP8以保持与功能括号对齐,但它不起作用。这是我尝试过的:谢谢!

WALLACE_SP1[i] = rad_intercpt_wallace(
                                      [[K_SPECIES1[i],
                                      LAI_SPECIES1[i],
                                      SP_HEIGHT[i]],
                                      [K_SPECIES2[i],
                                      LAI_SPECIES2[i],
                                      SP_HEIGHT[i]]])[0]

WALLACE_SP1[i] = rad_intercpt_wallace(
                                      [[K_SPECIES1[i],
                                      LAI_SPECIES1[i],
                                      SP_HEIGHT[i]],
                                      [K_SPECIES2[i],
                                      LAI_SPECIES2[i],
                                      SP_HEIGHT[i]]]
                                      )[0]

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i], LAI_SPECIES1[i],
                                      SP_HEIGHT[i]], [K_SPECIES2[i],
                                      LAI_SPECIES2[i], SP_HEIGHT[i]]])[0]

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i], LAI_SPECIES1[i],
                                        SP_HEIGHT[i]],
                                        [K_SPECIES2[i], LAI_SPECIES2[i],
                                         SP_HEIGHT[i]]])[0]

3 个答案:

答案 0 :(得分:4)

每个内部[都需要自己的一致缩进。例如,LAI_SPECIES必须与K_SPECIES对齐(因为它们都是同一列表的一部分)。

WALLACE_SP1[i] = rad_intercpt_wallace(
    [[K_SPECIES1[i],
      LAI_SPECIES1[i],
      SP_HEIGHT[i]],
     [K_SPECIES2[i],
      LAI_SPECIES2[i],
      SP_HEIGHT[i]]])[0]

替代方案:

WALLACE_SP1[i] = rad_intercpt_wallace(
    [[K_SPECIES1[i], LAI_SPECIES1[i], SP_HEIGHT[i]],
     [K_SPECIES2[i], LAI_SPECIES2[i], SP_HEIGHT[i]]])[0]

答案 1 :(得分:0)

这是我发现pep8停止抱怨的唯一方法:

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i],
                                      LAI_SPECIES1[i],
                                      SP_HEIGHT[i]],
                                      [K_SPECIES2[i],
                                      LAI_SPECIES2[i],
                                      SP_HEIGHT[i]]])[0]

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i],
                                      LAI_SPECIES1[i],
                                      SP_HEIGHT[i]],
                                      [K_SPECIES2[i],
                                      LAI_SPECIES2[i],
                                      SP_HEIGHT[i]]]
                                      )[0]

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i], LAI_SPECIES1[i],
                                      SP_HEIGHT[i]], [K_SPECIES2[i],
                                      LAI_SPECIES2[i], SP_HEIGHT[i]]])[0]

WALLACE_SP1[i] = rad_intercpt_wallace([[K_SPECIES1[i],
                                      LAI_SPECIES1[i],
                                      SP_HEIGHT[i]],
                                      [K_SPECIES2[i],
                                      LAI_SPECIES2[i],
                                      SP_HEIGHT[i]]])[0]

答案 2 :(得分:0)

所有(Robᵩ的第二个/最后一个版本除外)以上解决方案对我来说似乎都不可读。 另一方面,这是可读的:

public ActionResult Test()
{
    //Here I mock your DB, use EF context (context.table) instead of table variable or what you want.
    var table = new List<MyItem> {
        new MyItem { Label = "DocumentNumber", Value = "12345678", Emp_id = 1, Name = "First" },
        new MyItem { Label = "ExpiryDate", Value = "1/1/2015", Emp_id = 1, Name = "First" },
        new MyItem { Label = "IssueLoc", Value = "India", Emp_id = 1, Name = "First" },

        new MyItem { Label = "DocumentNumber", Value = "SecondValue", Emp_id = 2, Name = "Second" },
        new MyItem { Label = "ExpiryDate", Value = "SecondValue", Emp_id = 2, Name = "Second" },                
    };

    var items = table.Where(x => x.Emp_id == 1 || x.Emp_id == 2).ToList();
    var headers = items.Select(x => x.Label).Distinct().ToList();
    var employers = items.Select(x => x.Emp_id).Distinct().ToList();

    if(employers.Count > 1)
        headers.Insert(0, "Name");

    var data = new List<List<string>>();
    data.Add(headers);            

    foreach (var emp in employers)
    {
        var row = new List<string>();
        foreach (var header in headers)
        {                    
            if (header != "Name")
            {
                var cell = items.Where(x => x.Label == header && x.Emp_id == emp).FirstOrDefault();
                row.Add(cell == null ? "" : cell.Value);
            }                        
            else
                row.Add(items.Where(x => x.Emp_id == emp).First().Name);
        }
        data.Add(row);
    }

    return View(data);
}

似乎@model List<List<string>> <table> <thead> <tr> @foreach (var header in Model.First()) { <th>@header</th> } </tr> </thead> <tbody> @foreach (var row in Model.Skip(1)) { <tr> @foreach (var cell in row) { <td>@cell</td> } </tr> } </tbody> </table> 没有发出警告。 def rad_intercpt_wallace(arg): print arg WALLACE_SP1[i] = rad_intercpt_wallace([ [ K_SPECIES1[i], LAI_SPECIES1[i], SP_HEIGHT[i] ], [ K_SPECIES2[i], LAI_SPECIES2[i], SP_HEIGHT[i], ], ])[0] 仅抱怨:

pep8

即使函数名称很长,也不需要非常长的行。