它是一个Web挖掘脚本.
def printer(q,missing):
while 1:
tmpurl=q.get()
try:
image=urllib2.urlopen(tmpurl).read()
except httplib.HTTPException:
missing.put(tmpurl)
continue
wf=open(tmpurl[-35:]+".jpg","wb")
wf.write(image)
wf.close()
Run Code Online (Sandbox Code Playgroud)
q是一个Queue()由Urls组成的``缺少一个空队列来收集错误提升网址
它由10个线程并行运行.
每次我跑这个,我得到了这个.
File "C:\Python27\lib\socket.py", line 351, in read
data = self._sock.recv(rbufsize)
File "C:\Python27\lib\httplib.py", line 541, in read
return self._read_chunked(amt)
File "C:\Python27\lib\httplib.py", line 592, in _read_chunked
value.append(self._safe_read(amt))
File "C:\Python27\lib\httplib.py", line 649, in _safe_read
raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(5274 bytes read, 2918 more expected)
Run Code Online (Sandbox Code Playgroud)
但我确实使用了except......我尝试过其他类似的东西
httplib.IncompleteRead
urllib2.URLError
Run Code Online (Sandbox Code Playgroud)
甚至,
image=urllib2.urlopen(tmpurl,timeout=999999).read()
Run Code Online (Sandbox Code Playgroud)
但这都不起作用..
我怎么能抓住IncompleteRead和URLError?
我正在使用Python中的字谜程序字典.键是排序字母的元组,值是具有这些字母的可能单词的数组:
wordlist = {
('d', 'g', 'o'): ['dog', 'god'],
('a', 'c', 't'): ['act', 'cat'],
('a', 's', 't'): ['sat', 'tas'],
}
Run Code Online (Sandbox Code Playgroud)
我正在使用正则表达式来过滤列表.因此,r't$'作为过滤器,最终结果应为:
filtered_list = {
('a', 'c', 't'): ['act', 'cat'],
('a', 's', 't'): ['sat'],
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经把它归结为两步了.首先,保留与表达式匹配的所有单词:
tmp = {k: [w for w in v if re.search(r't$', w)] for k, v in wordlist.items()}
Run Code Online (Sandbox Code Playgroud)
这给我留下了空列表:
{
('d', 'g', 'o'): [],
('a', 'c', 't'): ['act', 'cat'],
('a', 's', 't'): ['sat'],
}
Run Code Online (Sandbox Code Playgroud)
然后我需要第二遍来摆脱空列表:
filtered_list = {k: v for k, v in tmp.items() if …Run Code Online (Sandbox Code Playgroud) python dictionary list-comprehension dictionary-comprehension
我最近从ipython0.10切换到ipython0.11.在ipython0.11中,我只看到python调试器参与(即使用%pdb)时的完整回溯的一小段,而在ipython0.10中我会看到完整的回溯.据我所知,pdb命令行无法直接访问完整的回溯 - 您可以使用'u'浏览它,但无法直接看到它.
那么,有没有办法显示完整的追溯?比如配置参数?
或者,更有用的是,有没有办法让ipython只显示被捕获的异常,而不是显示它被捕获的代码中的位置?
编辑:示例:
In [1]: pdb
Automatic pdb calling has been turned ON
In [2]: 1/0
> <ipython-input-2-05c9758a9c21>(1)<module>()
-1 1/0
ipdb> q
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/Users/adam/<ipython-input-2-05c9758a9c21> in <module>()
----> 1 1/0
ZeroDivisionError: integer division or modulo by zero
Run Code Online (Sandbox Code Playgroud)
我想在 q退出pdb 之前看到ZeroDivisionError .
我即将为实时应用编写一个algorthim,它涉及一些高维NLP(非线性编程).
在实现之前,我需要对算法进行计时,以确定它是否适用于实时应用程序,因此我使用Matlab的内置fmincons作为基线.
根据经验表明,matlab算法的速度往往从慢到低于C++对应物,所以我想估计一下这种特殊情况可以带来什么样的性能提升?
由于我的工作主要与实时应用程序有关,因此我很少使用NLP(非线性编程),所以我问我的同事们,他们建议我尝试ipopt作为开始,我google了它的网站,没有针对Matlab的基准测试关于算法细节也没有太多话题(至少在Matlab中,检查它们的算法的细节并不难),所以我基本上对它的准确性/鲁棒性/最优性等几乎一无所知.
所以关于NLP的C++实现的任何帮助都将非常有用,非常感谢提前.
我使用plumbum python库(http://plumbum.readthedocs.org/)作为shell脚本的替代品.
有一个我想要运行的命令,当它失败时它会输出我感兴趣的文件的路径:
$ slow_cmd
Working.... 0%
Working.... 5%
Working... 15%
FAIL. Check log/output.log for details
Run Code Online (Sandbox Code Playgroud)
我想在前台运行程序来检查进度:
from plumbum.cmd import slow_cmd
try:
f = slow_cmd & FG
except Exception, e:
print "Something went wrong."
# Need the error output from f to get the log file :(
Run Code Online (Sandbox Code Playgroud)
当slow_cmd失败时,它抛出异常(我可以捕获).但我无法从异常或f未来对象获取错误输出.
如果我没有slow_cmd在FG上运行,则异常包含所有输出,我可以从那里读取文件.
我正在编写一个将从cron调用的bash脚本.
bash脚本运行一个python命令,通过使用pythons os.isatty函数检测它何时在终端中, 并输出不同的东西,具体取决于它是手动运行还是通过cron运行.这使得调试非常困难,我想这样做,以便它总是假设它不在TTY中.
我希望能够在bash脚本中添加一些内容来欺骗它不在终端中运行的python脚本,因此总是输出相同的东西.
为了确认,我控制了bash脚本,但不想编辑python,因为这是一个打包的应用程序.
有任何想法吗?
我希望这是有道理的.
非常感谢你提前.
如果我在命令下运行,那么python返回了很好的结果..
result_aftermatch= subp.Popen('ls -lrt', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
但同样的方式我要求使用代码从文件greping行如下...
list_of_id=[23,34,56,77,88]
result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
result_lines,result_err= result_aftermatch.communicate()
print result_lines
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了如下错误...
Traceback (most recent call last):
File "test.py", line 144, in <module>
result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
File "/usr/lib/python2.6/subprocess.py", line 573, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
Run Code Online (Sandbox Code Playgroud)
请帮忙.
给定一个字符串,例如'helloyellowellow',解析给定字符串中的所有有效字符串.(例如:[[地狱,你好,黄色],[低,低] ........]
我正在寻找最优化的编写代码的方法.这是我的,但我不确定这是不是最好的方法.
完全披露 - 这是一个面试问题
master = []
# Dictionary for us to look up words
def is_word(inputstr):
#returns True/False
def processstring(fstr,secstr,li):
if is_word(fstr):
li.append(fstr)
if len(secstr) == 0:
if len(li) != 0:
master.append(li)
return
processstring(fstr+secstr[0], secstr[1:len(secstr)],li)
def wrapperprocess(inpstr):
li = []
if len(inpstr) == 0:
return
processstring('',inpstr,li)
wrapperprocess(inpstr[1:len(inpstr)])
wrapperprocess('helloyellowellow')
print master
Run Code Online (Sandbox Code Playgroud) 当比较python中的两个字符串时,它工作正常,当比较一个string对象与一个unicode对象时,它会按预期失败,但是当比较一个string对象与转换后的unicode (unicode --> str)对象时,它会失败
按预期工作:
>>> if 's' is 's': print "Hurrah!"
...
Hurrah!
Run Code Online (Sandbox Code Playgroud)
差不多是啊:
>>> if 's' is u's': print "Hurrah!"
...
Run Code Online (Sandbox Code Playgroud)
没想到:
>>> if 's' is str(u's'): print "Hurrah!"
...
Run Code Online (Sandbox Code Playgroud)
当两个类型属于同一类时,为什么第三个示例不能按预期工作?
>>> type('s')
<type 'str'>
>>> type(str(u's'))
<type 'str'>
Run Code Online (Sandbox Code Playgroud) 我有点问题,我试过谷歌搜索,但它没有任何帮助.
我正在设计一个django应用程序,我想/需要一个名为"property"的字段.这样做的原因是,是的,我想管理,我想如果可能的话,以保持业务术语事物的技术职称.
现在这已经不是问题......直到现在.
我现在需要一个方法,我希望能够用作属性,但是使用令牌会有一些冲突property.
class DataElementConcept(trebleObject):
template = "polls/dataElementConcept.html"
objectClass = models.ForeignKey(ObjectClass,blank=True,null=True)
property = models.ForeignKey(Property,blank=True,null=True)
@property
def registryCascadeItems(self):
return [self.objectClass,self.property]
Run Code Online (Sandbox Code Playgroud)
我明白这个错误是可以理解的.
File "/home/theodore/Github/possum-mdr/polls/models.py", line 381, in DataElementConcept
@property
TypeError: 'ForeignKey' object is not callable
Run Code Online (Sandbox Code Playgroud)
有没有办法让我将此方法视为属性,并保留property属性?
python ×9
bash ×1
built-in ×1
c++ ×1
cron ×1
dictionary ×1
django ×1
httplib ×1
ipdb ×1
ipopt ×1
ipython ×1
matlab ×1
optimization ×1
pdb ×1
plumbum ×1
properties ×1
python-2.7 ×1
tty ×1
unicode ×1
urllib2 ×1