我有文件services.py,其中包含某些类MyCache.MyCache的所有实例都应共享一个"缓存"字段,因此我将其设置为静态.为了初始化缓存,有一个加载它的静态方法.在应用程序的最开始,这个方法只调用一次.
问题是,当我从其他.py文件导入services.py并创建MyCache实例时 - 它打印出该缓存是空的!
如何修复它并使类的所有实例共享"缓存"字段而忽略它们的创建位置?
我不明白为什么会这样.请帮忙 :)
services.py:
class MyCache:
cache = {}
@staticmethod
def initialize():
MyCache.cache = load_from_file()
print(len(MyCache.cache)) # prints 3
def __init__(self):
print(len(MyCache.cache)) # supposed to print 3, because initialize has been called earlier, but prints 0 when called from other_file.py
Run Code Online (Sandbox Code Playgroud)
main.py:
import services
if __name__ == '__main__':
services.MyCache.initialize()
Run Code Online (Sandbox Code Playgroud)
other_file.py:
import services
services.MyCache().foo() # creates instance, prints 0 in MyCache.__init__
Run Code Online (Sandbox Code Playgroud) 我的tomcat 7(在amazon-eu,java 1.7.0_51上托管)无法启动以下异常:
SEVERE: Catalina.start:org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: java.lang.IllegalArgumentException: jmxremote.access (No such file or directory)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:372)
at org.apache.catalina.mbeans.JmxRemoteLifecycleListener.createServer(JmxRemoteLifecycleListener.java:304)
at org.apache.catalina.mbeans.JmxRemoteLifecycleListener.lifecycleEvent(JmxRemoteLifecycleListener.java:258)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:347)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:725)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: java.io.FileNotFoundException: jmxremote.access (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.io.FileInputStream.<init>(FileInputStream.java:96)
at com.sun.jmx.remote.security.MBeanServerFileAccessController.propertiesFromFile(MBeanServerFileAccessController.java:294)
at com.sun.jmx.remote.security.MBeanServerFileAccessController.<init>(MBeanServerFileAccessController.java:133)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:370)
... …Run Code Online (Sandbox Code Playgroud) 下面是简单的html。目的是使左跨度高度为外部 div 高度的 100%,而不是将其文本垂直居中(即“abc”应与“ghi”成为一行)。屏幕截图(chrome,win10)上的结果:样式无效。
“row-eq-height”用于制作相同高度的列,并从这里复制。
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
</style>
</head>
<body>
<div class="container">
<div style="display: table;">
<div class="row row-eq-height">
<div class="col-md-6" style="background-color: lightblue">
<span style="height: 100%; display: inline-block; vertical-align: middle; background-color: lightgreen">abc</span>
</div>
<div class="col-md-6" style="background-color: lightcoral">
def<br/>ghi<br/>jkl
</div>
</div>
</div>
</div>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
我应该如何修复它以使跨度达到高度的 100%?
UPD: SOF 的“运行片段”显示具有 100% 高度但未居中文本的跨度。想知道为什么结果与铬不同。
我的django应用程序中有几种型号.其中一些来自models.Model,some - 来自django-hvad的可翻译模型.我想记录它们上的每个保存/删除/更新操作.我知道管理员操作的标准django记录器,但它们太简短且不冗长以满足我的需求.
一般来说,实现此目的的一种常见方法是使用这些操作定义超类并从中扩展每个模型.这不是我的情况,因为我的一些模型是可翻译的,有些则不是.
第二种方式是方面/装饰器.我想,python/django必须有类似的东西,但我不知道到底是什么:)
请为我提供最合适的登录方式.谢谢!