我有一个严格的预提交钩子设置与flake8,所以我不能承诺,直到它开心.这通常是一件好事,但它会导致一些看似没问题的问题.
我无法让Flake8识别出来FileNotFoundError.
example.py
try:
pass
except FileNotFoundError:
pass
Run Code Online (Sandbox Code Playgroud)
这是足以使Flake8生成错误的代码
$ flake8 example.py
example.py:3:8: F821 undefined name 'FileNotFoundError'
$ python example.py # no error
$ python3 example.py # no error
Run Code Online (Sandbox Code Playgroud)
我检查了python文档,FileNotFoundError是一个'内置'异常,所以我认为我不应该从任何地方导入它,而我的python解释器没有抱怨它,只是看起来像flake8的问题.
我编写了一个python脚本来监视某些网络资源的状态,如果你愿意,那就是无限的.它永远ping相同的3个节点,直到它收到键盘中断.我尝试使用tee将程序的输出重定向到文件,但它不起作用:
? sudo ./pingster.py
15:43:33 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:35 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:36 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:37 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:38 node1 SUCESS | node2 SUCESS | node3 SUCESS
^CTraceback (most recent call last):
File "./pingster.py", line 42, in <module>
main()
File "./pingster.py", line 39, in main
sleep(1)
KeyboardInterrupt
? sudo ./pingster.py | tee ping.log
# wait a few seconds …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Rust中创建一个打印到stdout的状态指示器.在其他语言中,我使用了一个函数来清除当前的stdout行,同时保持其他行不变.我似乎无法找到Rust等效物.有吗?这是我正在寻找的一个小例子
for i in 0..1000 {
stdio::print(format!("{}", i).as_slice));
stdio::clear();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,该函数将创建接口的新实例,并将该实例分配给具有该接口类型的变量。这是一个简单的示例程序(无法编译):
package main
import (
"fmt"
)
type Foo interface {
Foo(int) int
}
type Foo_impl struct {}
func (f *Foo_impl) Foo(x int) int {
return x * 2
}
func main() {
var x *Foo_impl
constructFoo(x)
fmt.Println("Hello, playground")
}
func constructFoo(x Foo) {
*x = Foo_impl{} // Blows up here - invalid indirect of x (type Foo)
}
Run Code Online (Sandbox Code Playgroud)
是否可以通过反射间接接口变量并分配给底层值?如果我不使用接口,我会做这样的事情,
func main() {
var x int
foo(&x)
fmt.Printf("%d\n", x)
}
func foo(x *int) {
*x = 4
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,这将打印出 4。问题是接口变量不能以正常方式间接访问。有没有解决的办法?
我正在尝试使用 ansible 删除目录中的所有文件,同时保留目录。为此,我在with_fileglob任务中使用密钥将所有文件作为item变量从该目录中取出。我创建了一个最小示例,在这里显示了我的问题:
流浪文件:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provision :ansible do |ansible|
ansible.limit = "all"
ansible.playbook = "local.yml"
end
end
Run Code Online (Sandbox Code Playgroud)
本地.yml:
- name: Test
hosts: all
become: true
tasks:
- name: Test debug
debug:
msg: "{{ item }}"
with_fileglob:
- "/vagrant/*"
Run Code Online (Sandbox Code Playgroud)
我希望得到 /vagrant 目录中每个文件的调试消息——因为这是通过 Vagrant 与 VM 同步的目录,我应该得到 Vagrantfile 和 local.yml 的消息。相反,我收到以下令人困惑的警告:
PLAY [Test] ********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [default]
TASK [Test debug] **************************************************************
[WARNING]: Unable to find '/vagrant' in expected paths (use …Run Code Online (Sandbox Code Playgroud) 我试图让 vim 使用 python 文件的制表符缩进。我不想讨论制表符与空格的优点,我只想要制表符。在我的 vimrc 中,我不仅有
set shiftwidth=4
set tabstop
Run Code Online (Sandbox Code Playgroud)
但我也有一些特定于 python 的设置:
augroup python_files
autocmd!
autocmd FileType python setlocal noexpandtab
autocmd FileType Python set tabstop=4
autocmd FileType Python set shiftwidth=4
augroup END
Run Code Online (Sandbox Code Playgroud)
这似乎应该在 python 文件中正确设置我的缩进设置,但是当我打开一个时,它显示文字制表符为 8 个字符宽,TAB 键插入 4 个空格。还有什么我可以在这里做的吗?
我试图弄清楚我的Prolog程序中是否有无限循环,或者如果我写的不好,所以它很慢.我正在尝试解决dailyprogrammer subreddit中的平方和链问题.给定数字N,找到数字1-N(包括)的排序,使得排序中的每对相邻数字的总和是完美的正方形.这个最小的N是15,具有排序[8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9].这是我试图用来解决问题的代码:
is_square(Num):- is_square_help(Num, 0).
is_square_help(Num, S):- Num =:= S * S.
is_square_help(Num, S):-
Num > S * S,
T is S+1,
is_square_help(Num, T).
is_square_help(Num, S):- Num < S * S, fail.
contains(_, []):- fail.
contains(Needle, [Needle|_]).
contains(Needle, [_|Tail]):- contains(Needle, Tail).
nums(0, []).
nums(Num, List) :- length(List, Num), nums_help(Num, List).
nums_help(0, _).
nums_help(Num, List) :-
contains(Num, List),
X is Num - 1, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Rust中实现一个cons列表作为练习.我设法解决了除了这个之外的所有编译器错误:
Compiling list v0.0.1 (file:///home/nate/git/rust/list)
/home/nate/git/rust/list/src/main.rs:18:24: 18:60 error: borrowed value does not live long enough
/home/nate/git/rust/list/src/main.rs:18 List::End => list = &*(box List::Node(x, box List::End)),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/nate/git/rust/list/src/main.rs:16:34: 21:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 16:33...
/home/nate/git/rust/list/src/main.rs:16 fn add(mut list: &List, x: uint) {
/home/nate/git/rust/list/src/main.rs:17 match *list {
/home/nate/git/rust/list/src/main.rs:18 List::End => list = &*(box List::Node(x, box List::End)),
/home/nate/git/rust/list/src/main.rs:19 List::Node(_, ref next_node) => add(&**next_node, x),
/home/nate/git/rust/list/src/main.rs:20 }
/home/nate/git/rust/list/src/main.rs:21 }
/home/nate/git/rust/list/src/main.rs:18:16: 18:60 note: ...but borrowed …Run Code Online (Sandbox Code Playgroud) 我试图在我的C#程序的顶部定义一对类型别名.这是我正在尝试做的一个简短示例:
using System;
using System.Collections.Generic;
namespace Foo {
using TsvEntry = Dictionary<string, string>;
using Tsv = List<TsvEntry>;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用mcs 3.2.8.0编译它时,我收到以下错误消息:
foo.cs(6,19): error CS0246: The type or namespace name `TsvEntry' could not be found. Are you missing an assembly reference?
Run Code Online (Sandbox Code Playgroud)
是否可以using在C#中的其他别名中使用别名,或者我错过了using语句的工作方式?
我正在使用aria2下载动态生成的文件列表。我希望它跳过下载目录中已经存在的文件,但是不这样做,它只是重新下载它们并.1在文件名中添加一个。我尝试使用--check-integrity=true,但这并没有改变行为。有没有办法让它跳过已经存在的文件?