下面的代码在'ipython qtconsole'中打印'我想要打印',但它不能在普通的IPython中打印.
import logging
import logging.handlers
log = logging.getLogger()
f = logging.Formatter("%(asctime)s - %(module)s. %(funcName)s - %(levelname)s - %(message)s")
fh = logging.handlers.TimedRotatingFileHandler('log.txt', 'W6')
fh.setFormatter(f)
log.addHandler(fh)
log.setLevel(logging.INFO)
log.info('I want this to print')
Run Code Online (Sandbox Code Playgroud)
在'IPython qtconsole'然而我得到了不同的问题,我试图在这里解释(这不是很好,不需要阅读!).
你能告诉我为什么吗?
编辑:我使用Python 2.7
EDIT2:也许我真的只需要添加logging.StreamHandler.
我有一个巨大的特征应用程序,它遇到了enthought特性的限制.使用@on_traits_changed装饰器时主要是性能问题.使用PyQt4(或PyQt5)信号来解决这些问题是非常简单的,如果可以的话:
from traits.api import *
from PyQt4 import QtCore
class Foo(HasTraits, QtCore.QObject):
pass
Run Code Online (Sandbox Code Playgroud)
错误堆栈:
TypeError Traceback (most recent call last)
<ipython-input-3-ecdfa57492f7> in <module>()
2 from PyQt4 import QtCore
3
----> 4 class Foo(HasTraits, QtCore.QObject):
5 pass
C:\Python27\lib\site-packages\traits\has_traits.pyc in __new__(cls, class_name,
bases, class_dict)
427
428 # Finish building the class using the updated class dictionary:
--> 429 klass = type.__new__( cls, class_name, bases, class_dict )
430
431 # Fix up all self referential traits to refer to this class:
TypeError: Error …Run Code Online (Sandbox Code Playgroud) 我有一个函数 fun 需要几个参数 p0,p1,.. 对于每个参数,我给出了一个可能值的列表:
p0_list = ['a','b','c']
p1_list = [5,100]
Run Code Online (Sandbox Code Playgroud)
我现在可以为 p0,p1 的每个组合调用我的函数
for i in itertools.product(*[p0,p1]):
print fun(i)
Run Code Online (Sandbox Code Playgroud)
现在问题来了:如果我已经知道参数 p1 只对 fun 的结果有影响,如果 p0 是 'a' 或 'c' 怎么办?在这种情况下,我需要我的参数组合列表看起来像:
[('a', 5), ('a',100), ('b', 5), ('c',5), ('c', 100)]
Run Code Online (Sandbox Code Playgroud)
所以 ('b', 100) 只是被省略了,因为这将是对乐趣的不必要评估。
我的最终解决方案:
param_lists = [['p0', ['a','b','c']],['p1', [5,100]]]
l = itertools.product(*[x[1] for x in param_lists])
l = [x for x in l if not x[0] == 'b' or x[1]==5]
Run Code Online (Sandbox Code Playgroud)
我将这种方法用于 5 个参数和各种条件,并且效果很好。它也很容易阅读。这段代码的灵感来自 Corley Brigmans 和 nmcleans 的回答。
我刚刚安装了孔雀鱼0.1.10.然后输入Ipython 2.1.0
from guppy import hpy
hp = hpy()
Run Code Online (Sandbox Code Playgroud)
使控制台崩溃,即Windows告诉我,python.exe刚刚崩溃.有没有解决方法?还是一个堆积的替代品?
我确实用Enthought Traits构建了一个应用程序,它使用了太多的内存.我想,问题是由特质通知引起的:
@on_trait_change或使用特殊命名约定(例如_foo_changed())捕获的事件的内存使用情况似乎存在根本差异.我用两个类Foo和FooDecorator做了一个小例子,我假设它表现出完全相同的行为.但他们没有!
from traits.api import *
class Foo(HasTraits):
a = List(Int)
def _a_changed(self):
pass
def _a_items_changed(self):
pass
class FooDecorator(HasTraits):
a = List(Int)
@on_trait_change('a[]')
def bar(self):
pass
if __name__ == '__main__':
n = 100000
c = FooDecorator
a = [c() for i in range(n)]
Run Code Online (Sandbox Code Playgroud)
当使用c = Foo运行此脚本时,Windows任务管理器显示整个python进程的内存使用量为70MB,这对于增加n保持不变.对于c = FooDecorator,python进程使用450MB,增加n更高.
你能告诉我这个行为吗?
编辑:也许我应该改写:为什么有人会选择FooDecorator而不是Foo?
编辑2:我刚刚卸载了python(x,y)2.7.9并安装了最新版本的canopy 4.5.0.现在450MB变成了750MB.
编辑3:编译特征-4.6.0.dev0-py2.7-win-amd64我自己.结果与编辑2中的结果相同.因此,尽管所有合理性,https://github.com/enthought/traits/pull/248/files似乎并不是原因.
我想绘制一条曲线并进行缩放,并有一个插图显示绘图特定部分的缩放。这是我的代码,部分实现了这一点:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
fig, ax = plt.subplots()
axins = inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(.08, 0.35),bbox_transform=axfft.figure.transFigure)
x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.plot(x, y)
x1, x2, y1, y2 = 1, 2, .5, 4.5 # specify the limits
axins.set_xlim(x1, x2) # apply the x-limits
axins.set_ylim(y1, y2) # apply the y-limits
plt.xticks(visible=False)
plt.yticks(visible=False)
mark_inset(ax, axins, loc1=2, loc2=3, fc="none", ec="0.5")
Run Code Online (Sandbox Code Playgroud)
这是它产生的输出:
我的问题:
如何将mark_inset线条放在插图的右角而不是左角?目前的情况是,指标线穿过我的插图,我不希望这样。
我真的不明白,为什么代码
def isIn(char, aStr):
ms = len(aStr)/2
if aStr[ms] == char:
print 'i am here now'
return True
elif char>aStr[ms] and not ms == len(aStr)-1:
aStr = aStr[ms+1:]
elif char <aStr[ms] and not ms == 0:
aStr = aStr[0:ms]
else:
return False
isIn(char, aStr)
print isIn('a', 'ab')
Run Code Online (Sandbox Code Playgroud)
继续返回无.它打印'我现在在这里',但它不会返回True,就像下一行所说的那样.为什么?