我正在尝试编写一个非常简单的函数来递归搜索可能嵌套的(在最极端情况下十层深度)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
,但我也找不到.
我有一个深色背景的文本小部件,我看不到光标的位置.有没有办法改变(闪烁)文本光标的颜色?
考虑两个程序,以及它们之间的区别:
$ 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.rs
e.render_args()
除了第一次以外,每次都不会绘制每个像素.doesnt_flash.rs …
我对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可以做些什么吗?
>>> '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中复制它.
我正在尝试使用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) libsodium是一个很棒的加密库,GnuPG是一个很棒的密钥管理和签名软件.
GnuPG的最近发布了Ed25519签名密钥的支持,并提交了一份草案,IETF.
我想通过我的Web应用程序使用GnuPG离线使用Sodium生成的密钥.这样就可以了,所以每次签名时我都不必用我的私钥来信任服务器,而且我不需要在我的客户端上使用特殊的软件(即使我必须写它)因为我已经拥有了信任GnuPG.
我怎么能这样做?如何将libsodium私钥转换为OpenPGP兼容的私钥包?
考虑以下...
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中观察到了这种行为.
出于此问题的目的,CSV样式引用的字符串是一个字符串,其中:
"
."Alo""ha"
→ Alo"ha
."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)
.
我想下载维基百科上一篇文章的完整修订历史记录,但遇到了障碍。
下载整篇维基百科文章,或者使用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 ×7
rust ×2
tkinter ×2
colors ×1
cryptography ×1
csv ×1
cursor ×1
dictionary ×1
nom ×1
openpgp ×1
postgresql ×1
recursion ×1
rust-piston ×1
search ×1
sqlalchemy ×1
text ×1
web-scraping ×1
wikipedia ×1