有没有其他人在PuTTy中遇到过这个错误?
例如,按下向上键现在产生:
>>> ^[[A
Run Code Online (Sandbox Code Playgroud)
谷歌不允许你搜索特殊字符,所以我很难找到这个问题的现有版本.
编辑:
所以这发生在我粘贴一些unicode之后(我认为,翻译时已经设置为utf-8).
我已经在"更改设置"对话框中检查了与我的其他打开的PuTTy窗口不同的设置,但设置看起来是相同的.
EDIT2:
这是通过重启PuTTy来解决的,但仍然令人讨厌.
EDIT3:
如何复制完全相同的bug:
python2.7(注意:错误没有出现在2.6中)import codecs所以我有一些相当巨大的.gz文件 - 我们在解压缩时每个都说10到20 GB.
我需要循环遍历它们的每一行,所以我使用标准:
import gzip
f = gzip.open(path+myFile, 'r')
for line in f.readlines():
#(yadda yadda)
f.close()
Run Code Online (Sandbox Code Playgroud)
但是,这两个open()和close()命令都占用了AGES,占用了98%的内存和CPU.以至于程序退出并打印Killed到终端.也许是将整个提取的文件加载到内存中?
我现在正在使用类似的东西:
from subprocess import call
f = open(path+'myfile.txt', 'w')
call(['gunzip', '-c', path+myfile], stdout=f)
#do some looping through the file
f.close()
#then delete extracted file
Run Code Online (Sandbox Code Playgroud)
这有效.但是有更干净的方式吗?
所以我们有一个页面:
<span id='container'>
<a href='#' id='first'>First Link</a>
<a href='#' id='second'>Second Link</a>
</span>
Run Code Online (Sandbox Code Playgroud)
并想要添加一些点击事件:
first.addEventListener('click', function(){alert('sup!');})
Run Code Online (Sandbox Code Playgroud)
奇迹般有效!但是,当您将第二个参数设为外部函数时:
function message_me(m_text){
alert(m_text)
}
second.addEventListener('click', message_me('shazam'))
Run Code Online (Sandbox Code Playgroud)
它立即调用该函数.我怎么能阻止这个?很烦人!
这是一个现场演示:http://jsfiddle.net/ey7pB/1/
所以我使用locals()来获取函数中的一些参数.很好地工作:
def my_function(a, b):
print locals().values()
>>> my_function(1,2)
[1, 2]
Run Code Online (Sandbox Code Playgroud)
标准的东西.但现在让我们介绍一下列表理解:
def my_function(a, b):
print [x for x in locals().values()]
>>> my_function(1,2)
[[...], 1, 2]
Run Code Online (Sandbox Code Playgroud)
EHH?为什么要插入自引用?
python list-comprehension python-2.6 locals python-internals
所以我知道Python字符串是不可变的,但我有一个字符串:
c['date'] = "20110104"
Run Code Online (Sandbox Code Playgroud)
我想转换成哪个
c['date'] = "2011-01-04"
Run Code Online (Sandbox Code Playgroud)
我的代码:
c['date'] = c['date'][0:4] + "-" + c['date'][4:6] + "-" + c['date'][6:]
Run Code Online (Sandbox Code Playgroud)
似乎有点费解,不是吗?最好将它保存为单独的变量,然后再做同样的事情吗?或者基本上没有区别?
这是我的pylinter设置:
{
// When versbose is 'true', various messages will be written to the console.
// values: true or false
"verbose": false,
// The full path to the Python executable you want to
// run Pylint with or simply use 'python'.
"python_bin": "/usr/bin/python2.7",
// The following paths will be added Pylint's Python path
"python_path": [],
// Optionally set the working directory
"working_dir": null,
// Full path to the lint.py module in the pylint package
"pylint_path": null,
// Optional full path …Run Code Online (Sandbox Code Playgroud) 我有一个巨大的图表数据集 - 让我们说它是这样的,但在更大的层面上:
1 -> 2
3 -> 4
Run Code Online (Sandbox Code Playgroud)
1,2,3,4是节点,箭头是有向边.假设它们都在一个图形对象中:
import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([1,2,3,4])
G.add_edge(1,2)
G.add_edge(3,4)
Run Code Online (Sandbox Code Playgroud)
给定一个像这样的对象,它在图形中有两个迷你图形,我们如何拉出每个迷你图形?我觉得必须有这个词吗?我的最终结果如下:
for mini_graph in G:
print mini_graph.nodes()
...
[1,2]
[3,4]
Run Code Online (Sandbox Code Playgroud) 所以我正在测试创建一个chrome扩展.我理解,使用Manifest v2,你不能在popup.html中使用javascript.所以,我已经将javascript移动到一个单独的文件popup.js.
我试图在弹出窗口中设置一个简单的按钮来调用hello world警报,但它根本不起作用.
此外,Chrome的Inspect Element调试器不会显示任何错误.
popup.html
<html>
<head>
<title>Test</title>
<script language='javascript' src='popup.js'></script>
</head>
<body>
<form name='testForm'>
<input type='button' id='alertButton' value='click me'>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
popup.js
function myAlert(){
alert('hello world')
}
window.onload = function(){
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('alertButton').addEventListener('onclick', myAlert);
});
}
Run Code Online (Sandbox Code Playgroud)
的manifest.json
{
"manifest_version": 2,
"name": "Test",
"description": "Test Extension",
"version": "1.0",
"icons": {
"48": "icon.png"
},
"permissions": [
"http://*/*",
"https://*/*"
],
"browser_action": {
"default_title": "This is a test",
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我们有一个清单:
myList = [1, "two"]
Run Code Online (Sandbox Code Playgroud)
并希望打印出来,通常我会使用类似的东西:
"{0} and {1}".format(*myList)
Run Code Online (Sandbox Code Playgroud)
但你也可以这样做:
" and ".join(myList)
Run Code Online (Sandbox Code Playgroud)
但不幸的是:
>>> " and ".join(myList)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
Run Code Online (Sandbox Code Playgroud)
为什么不自动将收到的列表自动转换为字符串?
你何时不需要它将它们转换成字符串?是否有一些我丢失的小边缘情况?
所以我在mongoDB中有一个超级简单的数据库,有一些集合:
> show collections
Aggregates <-- count: 92
Users <-- count: 68222
Pages <-- count: 1728288847, about 1.1TB
system.indexes
Run Code Online (Sandbox Code Playgroud)
该Aggregates集合是集合的Pages集合,每个文档如下所示:
> db.Aggregates.findOne()
{
"_id" : ObjectId("50f237126ba71610eab3aaa5"),
"daily_total_pages" : 16929799,
"day" : 21,
"month" : 9,
"year" : 2011
}
Run Code Online (Sandbox Code Playgroud)
非常简单.但是,让我们daily page loads一起添加所有92天来尝试获取总页面加载:
>>> def get_total():
... start = datetime.now()
... print sum([x['daily_total_pages'] for x in c.Aggregates.find()])
... end = datetime.now()
... print (end-start).seconds
...
>>> get_total()
1728288847
43
Run Code Online (Sandbox Code Playgroud)
43秒?!??!??!?!
那92个总结果很小!我不妨将它们存储在一个文本文件中,这很疯狂.
还是他们很小?根据mongo,它们在磁盘上有多大?
> db.Aggregates.stats()
{ …Run Code Online (Sandbox Code Playgroud) python ×7
javascript ×2
arrow-keys ×1
database ×1
gunzip ×1
gzip ×1
locals ×1
mongodb ×1
networkx ×1
optimization ×1
putty ×1
pylint ×1
pymongo ×1
python-2.6 ×1
string ×1
sublimetext3 ×1
subprocess ×1