我想使用python的StreamHandler日志处理程序.我试过的是,
import logging
import sys
mylogger = logging.getLogger("mylogger")
h1 = logging.StreamHandler(stream=sys.stdout)
h1.setLevel(logging.DEBUG)
mylogger.addHandler(h1)
# now trying to log with the created logger
mylogger.debug("abcd") # <no output>
mylogger.info("abcd") # <no output>
mylogger.warn("abcd") # abcd
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?或者做错了什么?为什么INFO和DEBUG级别日志不会出现在STDOUT上?
我想以递归方式从特定目录中删除文件.所以,我用过
find . -wholename "*.txt" -delete
Run Code Online (Sandbox Code Playgroud)
我们也可以使用删除文件
rm -rf *.txt
Run Code Online (Sandbox Code Playgroud)
使用rm和删除文件有什么区别find?
我正在使用mod_wsgi在Apache2上部署我的web.py应用程序.
这是我的virt_host文件,
WSGIPythonPath /home/ubuntu/plotwatt/libplotwatt:/home/ubuntu/plotwatt/pwstage/src
<VirtualHost *:20108>
ServerAdmin gslabrails.dev.plotwatt.com
DocumentRoot /var/www
WSGIScriptAlias / /var/www/currentcost/server.py
WSGIDaemonProcess currentcost user=ubuntu group=ubuntu processes=5 threads=3
WSGIProcessGroup currentcost
WSGIApplicationGroup %{GLOBAL}
AddType text/html .py
<Directory /var/www/currentcost/>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/currentcost_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/currentcost_access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
我在WSGIPythonPath中给了我名为redisStage的lib的路径.但是,它似乎不适合我.我做错了配置吗?我不能把WSGIPythonPath指令放在VitualHost指令中.究竟是什么原因?
这是我从其中一个Web服务收到的示例字符串,
body=%7B%22type%22%3A%22change%22%2C%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ftriggers%2F4100%22%2C%22environment%22%3A%7B%22feed%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%22%2C%22title%22%3A%22Current+Cost+Bridge%22%2C%22description%22%3Anull%2C%22id%22%3A36133%7D%2C%22threshold_value%22%3Anull%2C%22timestamp%22%3A%222012-01-05T09%3A27%3A01Z%22%2C%22triggering_datastream%22%3A%7B%22url%22%3A%22http%3A%2F%2Fapi.pachube.com%2Fv2%2Ffeeds%2F36133%2Fdatastreams%2F1%22%2C%22value%22%3A%7B%22value%22%3A%22523%22%2C%22max_value%22%3A1269.0%2C%22min_value%22%3A0.0%7D%2C%22id%22%3A%221%22%2C%22units%22%3A%7B%22symbol%22%3A%22W%22%2C%22type%22%3A%22derivedUnits%22%2C%22label%22%3A%22watts%22%7D%7D%2C%22id%22%3A4100%7D
Run Code Online (Sandbox Code Playgroud)
这是代码,
class Feeds():
def GET(self):
print "Get request is accepted."
return render.index(None)
def POST(self):
print "Post request is accepted."
print (web.data())
Run Code Online (Sandbox Code Playgroud)
现在,当该Web服务发布上述给定数据时,我将如何将其转换为可读格式?然后,我需要将其转换为JSON对象并进一步使用.那么,我将如何转换它?
当我尝试这段代码时,
json_data = json.loads(web.data())
print json_data['body']
return render.index(json_data['body'])
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误,
enter code Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 237, in process
return self.handle()
File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 228, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 409, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.6/dist-packages/web/application.py", line 385, in handle_class
return tocall(*args)
File "/home/ubuntu/pachubeConsumer/src/controllers/feeds.py", line 17, in POST …Run Code Online (Sandbox Code Playgroud) 我有很多目录,其中有python文件.所有都是普通的python文件.我没有使用任何类型的框架.我想从一个中心位置测试这些py文件.我应该只发出一个命令,并且应该调用每个目录中的所有*_test.py文件.那么,有没有可用于我的要求的现成工具或框架?
我正在寻找PyUnit来测试普通的py文件.并考虑编写一个shell脚本,它将使用正则表达式匹配文件名来调用所有这些*_test.py文件.
任何人都可以建议任何其他方法 随时欢迎你.
谢谢.
我经历了" 在Python中记录未捕获的异常 ".而且,我试过这个:
import web
import sys
app = web.application((
'/test', 'test'), globals())
def test_func(e_type, value, traceback):
print "Handled exception here.."
class test:
def GET(self):
a = 1/0
if __name__ == "__main__":
sys.excepthook = test_func
app.run()
Run Code Online (Sandbox Code Playgroud)
在这里,您可以轻松查看是否有GET /test请求,我故意提出ZerDivisionError.正如我已经覆盖sys.excepthook,我希望方法test_func执行ZeroDivisionError.
然而,这段代码并没有按照预期工作.我观察到,当我尝试覆盖excepthook独立代码(不在web-app中)时,它工作正常.正确调用新方法(overriden).
知道为什么这种不同的行为?
如果我尝试将列表附加到自身会发生什么?
# Let's say empty list is created.
some_list = []
# Now, append it with self
some_list.append(some_list)
# Output shows [[...]] on iPython console.
Run Code Online (Sandbox Code Playgroud)
这是什么意思?会some_list成为递归列表还是什么?引用计数会发生什么some_list?垃圾收集者如何对待这个?什么时候some_list会被垃圾收集?
我有类似这样的dict对象,
topo = {
'name' : 'm0',
'children' : [{
'name' : 'm1',
'children' : []
}, {
'name' : 'm2',
'children' : []
}, {
'name' : 'm3',
'children' : []
}]
}
Run Code Online (Sandbox Code Playgroud)
现在,我想再插入一个dict对象,比如说,
{
'name' : 'ABC',
'children' : []
}
Run Code Online (Sandbox Code Playgroud)
作为在m2的儿童阵列中名为"m2"的dict的孩子.
你能建议我该怎么办?
我应该采用单独的数据结构实现吗?
我试图用RJS替换DOM中的div.这是我试过的代码,控制器有这个方法:
def change
render :update do |page|
page.replace(:test_id, :partial => "input",:locals =>{ :type => 'text', :name => 'user[user][contactinfo][city]', :val => "", :size => '244', :placeholder_text => 'Yes it is working...'})
end
end
Run Code Online (Sandbox Code Playgroud)
该视图包含:
<div id = "test_id"></div>
<%= link_to "AJAX", "/poc/change", :remote => true %>
Run Code Online (Sandbox Code Playgroud)
现在我想div id="test_id"用部分提到的替换.
我得到的输出是:
try {
Element.replace("test_id", "<input type=\"text\" id=\"user[user][contactinfo][city]\" name=\"user[user][contactinfo][city]\" value=\"\" placeholder=\"Yes it is working...\" style=\"width:244px; height:33px; border:0; color:#646464; background:url(/images/form_textfield_244.png) 0 5px no-repeat; padding:12px 5px 0 5px; margin:0 0 10px 0;\" />\n");
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试这段代码,
#include <stdio.h>
main(){
int a[2],i;
a[5] = 12;
for(i=0;i<10;i++){
printf("%d\n", a[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给了我输出:
1053988144
32767
0
3
0
12
-1267323827
32716
0
0
Run Code Online (Sandbox Code Playgroud)
为什么[5]可以访问?不应该通过RunTime错误吗?
我有一个非常具体的要求.有三个或四个衬垫解决方案.我需要一个衬垫解决方案.
假设我有一个像这样的数组:
[["Andorra", "Andorra"], ["United Arab Emirates", "United Arab Emirates"],
["Afghanistan", "Afghanistan"], ["Antigua & Barbuda", "Antigua & Barbuda"],
["Anguilla", "Anguilla"], ["Albania", "Albania"], ["Armenia", "Armenia"],
["Angola", "Angola"]]
Run Code Online (Sandbox Code Playgroud)
我希望["安圭拉","安圭拉"]作为我的第一个元素,而其他的则按降序排列.
有没有办法实现这个目标?(如果可能,一个班轮)
我有一个字符串,让我们说"MDP-A-17_MDP-A-23.3".我想在此基础上串分裂"-","_"和".".
输出将是一个列表:
["MDP", "A", "17", "MDP", "A", "23", "3"]
Run Code Online (Sandbox Code Playgroud) python ×9
ajax ×1
apache2 ×1
arrays ×1
c ×1
delete-file ×1
dictionary ×1
http-post ×1
json ×1
logging ×1
mod-wsgi ×1
python-2.7 ×1
pythonpath ×1
regex ×1
rjs ×1
rm ×1
ruby ×1
split ×1
sys ×1
unit-testing ×1
unix ×1
web-services ×1
web.py ×1