小编Fre*_*nan的帖子

在字典中递归地查找密钥

我正在尝试编写一个非常简单的函数来递归搜索可能嵌套的(在最极端情况下十层深度)Python字典并返回它从给定键中找到的第一个值.

我无法理解为什么我的代码不适用于嵌套字典.

def _finditem(obj, key):
    if key in obj: return obj[key]
    for k, v in obj.items():
        if isinstance(v,dict):
            _finditem(v, key)

print _finditem({"B":{"A":2}},"A")
Run Code Online (Sandbox Code Playgroud)

它回来了None.

然而,它确实可以用于_finditem({"B":1,"A":2},"A")返回2.

我确定这是一个简单的错误,但我找不到它.我觉得在标准库中已经有可能存在这样的东西了collections,但我也找不到.

python recursion search dictionary

26
推荐指数
3
解决办法
4万
查看次数

如何在Tkinter中更改文本光标颜色?

我有一个深色背景的文本小部件,我看不到光标的位置.有没有办法改变(闪烁)文本光标的颜色?

python text tkinter colors cursor

16
推荐指数
2
解决办法
5436
查看次数

当我每次渲染屏幕时都不调用`graphics :: clear`时,如何阻止Piston使屏幕闪烁?

考虑两个程序,以及它们之间的区别:

$ diff flashes/src/main.rs doesnt_flash/src/main.rs
22,23c22
<
<     let mut i = 0;
---
>     let mut cursor_poses: Vec<(f64, f64)> = Vec::new();
28c27
<             mx = x; my = y;
---
>             cursor_poses.push((x,y));
32,33c31,33
<                     if i == 0 {
<                         graphics::clear([1.0; 4], g);
---
>                     graphics::clear([1.0; 4], g);
>                     for &(x, y) in cursor_poses.iter() {
>                         draw_cursor_pos([x, y], &c, g);
35,36d34
<                     draw_cursor_pos([mx, my], &c, g);
<                     i+=1;
Run Code Online (Sandbox Code Playgroud)

这两个节目的视频演示.

该程序是一个非常基本的绘图程序,只有一个画笔宽度,画笔笔触颜色,画布大小,没有保存等; 哦,要停止绘画,将鼠标移出窗口,因为每次你越过窗户,这都算作绘图;-)

flashes.rse.render_args()除了第一次以外,每次都不会绘制每个像素.doesnt_flash.rs …

rust rust-piston

16
推荐指数
1
解决办法
693
查看次数

python将资源管理器拖放到tkinter条目小部件

我对Python很新.我正在尝试将文件名(包含完整路径)输入到TKinter条目小部件.由于文件名的路径可能很长,我希望能够直接从Windows资源管理器拖放文件.在Perl中我看到了以下内容:

use Tk::DropSite;
.
.
my $mw = new MainWindow;
$top = $mw->Toplevel;
$label_entry = $top->Entry(-width => '45',. -background => 'ivory2')->pack();
$label_entry->DropSite(-dropcommand => \&drop,-droptypes => 'Win32',);
Run Code Online (Sandbox Code Playgroud)

我在Python中使用TKinter可以做些什么吗?

python tkinter

15
推荐指数
3
解决办法
2万
查看次数

为什么使用列表作为字符串格式参数,即使没有%s标识符,也会返回原始字符串?

>>> 'string with no string formatting markers' % ['string']
'string with no string formatting markers'
>>> 'string with no string formatting markers' % ('string',)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)

我希望两种情况都能提高TypeError,但事实并非如此.为什么不?

关于这个主题的Python文档讨论了字符串,元组和字典,但没有说明列表.我对这种行为有点困惑.我已经能够在Python 2.7和3.2中复制它.

python string-formatting

12
推荐指数
1
解决办法
188
查看次数

使用SQLAlchemy更新PostgreSQL数组

我正在尝试使用SQLAlchemy Core中的SQL语句更新PostgreSQL表上的整数数组.我首先尝试使用查询生成器,但无法弄清楚如何做到这一点.我相信Psycopg2,我正在使用的方言,可以自动将数组形成PostgreSQL可以接受的格式.

这是表模式:

CREATE TABLE surveys (
    survey_id serial PRIMARY KEY,
    question_ids_ordered INTEGER[],
    created_at TIMESTAMP NOT NULL DEFAULT now(),
);
Run Code Online (Sandbox Code Playgroud)

和SQLAlchemy声明:

survey_id = 46
question_ids_ordered = [237, 238, 239, 240, 241, 242, 243]

with engine.begin() as conn:
    conn.execute("""UPDATE surveys
                    SET question_ids_ordered = %s
                    WHERE survey_id = %s""",
                    question_ids_ordered, survey_id)
Run Code Online (Sandbox Code Playgroud)

我收到的追溯是:

Traceback (most recent call last):
  File "foo.py", line 16, in <module>
    res = add_question_to_group(current_user, 46, 358, question_ids_ordered, new_group_name="Jellyfish?")
  File "/vagrant/workspace/panel/panel/survey.py", line 80, in add_question_to_group
    question_ids_ordered, survey_id)
  File "/home/vagrant/.virtualenvs/project/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 664, …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy

12
推荐指数
1
解决办法
1万
查看次数

如何将libsodium私钥转换为OpenPGP兼容的私钥包?

libsodium是一个很棒的加密库,GnuPG是一个很棒的密钥管理和签名软件.

GnuPG的最近发布了Ed25519签名密钥的支持,并提交了一份草案,IETF.

我想通过我的Web应用程序使用GnuPG离线使用Sodium生成的密钥.这样就可以了,所以每次签名时我都不必用我的私钥来信任服务器,而且我不需要在我的客户端上使用特殊的软件(即使我必须写它)因为我已经拥有了信任GnuPG.

我怎么能这样做?如何将libsodium私钥转换为OpenPGP兼容的私钥包?

cryptography elliptic-curve openpgp nacl-cryptography

12
推荐指数
1
解决办法
737
查看次数

删除空列表有什么特别之处?

考虑以下...

In [1]: del []

In [2]: del {}
  File "<ipython-input-2-24ce3265f213>", line 1
SyntaxError: can't delete literal

In [3]: del ""
  File "<ipython-input-3-95fcb133aa75>", line 1
SyntaxError: can't delete literal

In [4]: del ["A"]
  File "<ipython-input-5-d41e712d0c77>", line 1
SyntaxError: can't delete literal
Run Code Online (Sandbox Code Playgroud)

有什么特别的[]?我希望这也能提高SyntaxError.为什么不呢?我在Python2和Python3中观察到了这种行为.

python

10
推荐指数
1
解决办法
589
查看次数

如何在nom中匹配CSV样式的引用字符串?

出于此问题的目的,CSV样式引用的字符串是一个字符串,其中:

  1. 字符串以一个字符串开头和结尾".
  2. 字符串中的两个双引号折叠为一个双引号."Alo""ha"Alo"ha.
  3. ""本身就是一个空字符串.
  4. "A""" e"无法解析错误输入,例如.这是一个A",然后是垃圾e".

我尝试了几件事,其中没有一件完全有效.

我得到的最接近,感谢Mozilla IRC上#nom用户粉红色的一些帮助:

use std::error as stderror; /* Avoids needing nightly to compile */

named!(csv_style_string<&str, String>, map_res!(
   terminated!(tag!("\""), not!(peek!(char!('"')))),
   csv_string_to_string
));

fn csv_string_to_string(s: &str) -> Result<String, Box<stderror::Error>> {
   Ok(s.to_string().replace("\"\"", "\""))
}
Run Code Online (Sandbox Code Playgroud)

这不能正确捕获字符串的结尾.

我也试图使用re_match!r#""([^"]|"")*""#,但这总是导致Err::Incomplete(1).

我已经确定Nom 1.0给定CSV示例对于引用的CSV字符串不起作用,因为我正在描述它,但我知道实现不同.

csv rust nom

6
推荐指数
1
解决办法
372
查看次数

下载维基百科页面的完整历史记录

我想下载维基百科上一篇文章的完整修订历史记录,但遇到了障碍。

下载整篇维基百科文章,或者使用Special:Export URL 参数获取其历史片段非常容易:

curl -d "" 'https://en.wikipedia.org/w/index.php?title=Special:Export&pages=Stack_Overflow&limit=1000&offset=1' -o "StackOverflow.xml"
Run Code Online (Sandbox Code Playgroud)

当然,我可以从这里下载整个网站,包括每篇文章的所有版本,但这比我需要的要多 TB 和更多的数据。

有没有预先构建的方法来执行此操作?(看来一定有。)

python wikipedia web-scraping

5
推荐指数
2
解决办法
1357
查看次数