我想创建一个Python日志记录类,它可以作为记录配置的常用方法继承,但是从父级单独控制基类的日志记录级别.这类似于如何在多个模块中使用python日志记录.Vinay Sajip使用LogMixin 的答案非常接近.以下是我稍加修改的版本.
我的大多数类都继承了较小的类.例如:
filename:LogMixin.py
import logging, logging.config
import yaml
class LogMixin(object):
__loggerConfigured = False
@property
def logger(self):
if not self.__loggerConfigured:
with open('log_config.yaml', 'rt') as f:
config = yaml.load(f.read())
logging.config.dictConfig(config)
self.__loggerConfigured = True
name = '.'.join([self.__class__.__name__])
return logging.getLogger(name)
Run Code Online (Sandbox Code Playgroud)
文件名:Base.py
from LogMixin import LogMixin
class Base(LogMixin):
def __init__(self):
self.logger.debug("Debug Base")
def run_base(self):
self.logger.debug("Debug Running Base")
self.logger.info("Info Running Base")
if __name__ == '__main__':
my_base = Base()
my_base.run_base()
Run Code Online (Sandbox Code Playgroud)
filename:Parent.py
from Base import Base
class Parent(Base):
def __init__(self):
self.logger.debug("Debug Parent") …Run Code Online (Sandbox Code Playgroud) 我正在使用Koush的AndroidAsync作为WebSocket客户端.我的代码遵循https://github.com/koush/AndroidAsync中的示例并且有效.(以下复制的例子.)
我需要我的应用程序在启动时打开websocket,但是,我需要处理一些问题:
A)我需要允许用户更改websocket服务器的地址.在这种情况下,我需要关闭现有的websocket(可能已经失败)并打开一个websocket到新服务器.
B)服务器可能已关闭或不可用.在这种情况下,我想将此报告回活动.目前它只是默默地失败.
所以按重要性排序:
如果在某处记录,请告诉我.
以下复制的网站示例代码:
AsyncHttpClient.getDefaultInstance().websocket(get,"my-protocol",new WebSocketConnectCallback(){
@Override
public void onCompleted(Exception ex,WebSocket webSocket){
if(ex!=null){
ex.printStackTrace();
return;
}
webSocket.send("a string");
webSocket.send(new byte[10]);
webSocket.setStringCallback(new StringCallback(){
public void onStringAvailable(String s){
System.out.println("I got a string: "+s);
}
});
webSocket.setDataCallback(new DataCallback(){
public void onDataAvailable(ByteBufferList byteBufferList){
System.out.println("I got some bytes!");
// note that this data has been read
byteBufferList.recycle();
}
});
}
});
Run Code Online (Sandbox Code Playgroud) StackOverflow上似乎有很多解决方案用于将XML转换为Python字典,但它们都没有生成我正在寻找的输出.我有以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
<section1
mystatus:field1="data1"
mystatus:field2="data2" />
<section2
mystatus:lineA="outputA"
mystatus:lineB="outputB" />
</status>
Run Code Online (Sandbox Code Playgroud)
lxml有一个优雅简单的解决方案,用于将XML转换为字典:
def recursive_dict(element):
return element.tag, dict(map(recursive_dict, element)) or element.text
Run Code Online (Sandbox Code Playgroud)
不幸的是,我得到:
('status', {'section2': None, 'section1': None})
Run Code Online (Sandbox Code Playgroud)
代替:
('status', {'section2':
{'field1':'data1','field2':'data2'},
'section1':
{'lineA':'outputA','lineB':'outputB'}
})
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何获得我想要的输出而不会使recursive_dict()函数大大复杂化.
我没有与lxml绑定,只要它给了我xml中的所有信息,我也可以使用不同的字典组织.谢谢!
xmltodict将 XML 转换为 Python 字典。它支持命名空间。我可以按照主页上的示例并成功删除命名空间。但是,我无法从 XML 中删除命名空间并且无法确定原因?这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
<section1
mystatus:field1="data1"
mystatus:field2="data2" />
<section2
mystatus:lineA="outputA"
mystatus:lineB="outputB" />
</status>
Run Code Online (Sandbox Code Playgroud)
并使用:
xmltodict.parse(xml,process_namespaces=True,namespaces={'http://localhost/mystatus':None})
Run Code Online (Sandbox Code Playgroud)
我得到:
OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@http://localhost/mystatus:field1', u'data1'), (u'@http://localhost/mystatus:field2', u'data2')])), (u'section2', OrderedDict([(u'@http://localhost/mystatus:lineA', u'outputA'), (u'@http://localhost/mystatus:lineB', u'outputB')]))]))])
Run Code Online (Sandbox Code Playgroud)
代替:
OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'field1', u'data1'), (u'field2', u'data2')])), (u'section2', OrderedDict([(u'lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])
Run Code Online (Sandbox Code Playgroud)
我是否犯了一些简单的错误,或者我的 XML 是否存在阻止 process_namespace 修改正常工作的问题?