目前我正在学习生成器和列表理解,并且弄乱了剖析器以查看性能增益,并且在这两个中使用两者中的大量素数的总和数量的混乱.
我可以在生成器中看到:1 genexpr作为累积时间方式比列表对应方式短,但第二行是令我感到困惑的.正在做一个我认为是数字检查的电话是素数,但是不应该是另一个:列表理解中的1个模块?
我在个人资料中遗漏了什么吗?
In [8]: cProfile.run('sum((number for number in xrange(9999999) if number % 2 == 0))')
5000004 function calls in 1.111 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
5000001 0.760 0.000 0.760 0.000 <string>:1(<genexpr>)
1 0.000 0.000 1.111 1.111 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.351 0.351 1.111 1.111 {sum}
In [9]: cProfile.run('sum([number for number in xrange(9999999) if number % 2 == 0])')
3 function calls in 1.123 seconds …Run Code Online (Sandbox Code Playgroud) 我正在为更大的系统开发python模块。我做了一个setup.py软件包,用于将其安装在主模块中。它可以正常工作,但是随后我对模块做了一些更改,主要是修改了py文件的名称,并重新组织了一堆类。
然后,我更新了模块的版本,使用pip卸载了旧版本,并使用python setup.py install安装了新版本,当我尝试导入ipython时,发现我得到了已删除的旧模块。
发现它很奇怪,并检查了我的virtualenv lib文件夹,找到了模块的两个版本,以及旧的类结构和新的类结构。两者都可用,因为我在ipython中都导入了它并对其进行了测试。
它不会引起任何问题,因为我可以简单地使用最新版本,但是令人困惑。知道为什么会有这种行为吗?
我正在搞乱局部和全局命名空间,我发现了一些奇怪的行为.如果我这样做......
len(globals().keys())
len(locals().keys())
Run Code Online (Sandbox Code Playgroud)
我得到两个不同的结果,首先我得到344然后我得到346.因此,出于好奇心,我想知道哪些键在我的本地但不在我的全局,所以我这样做.
[key for key in local().keys() if key not in globals().keys()]
Run Code Online (Sandbox Code Playgroud)
而bam,没有,返回一个空列表.
想想也许我的代码有问题我试试这个.
g = [1,2,3,4]
l = [1,2,3,4,5,6]
[key for key in l if key not in g]
Run Code Online (Sandbox Code Playgroud)
并按预期返回 [5,6]
那么,Python无法区分locals()和globals()的键的原因是什么.
它locals() == globals()与副作用有关吗?
非常感谢.
我正在使用 Flask 和 Flask-Restful 进行一个项目,尝试根据用户在我的 Restful API 中的输入获取动态 url。我创建了一个带有 get 方法的类,该类将响应请求,当用户调用另一个资源时,它会激活新资源并创建一个 url。
当我尝试向我的 api 添加新资源时出现问题,该资源调用 Flask 中的函数 setupmethod 来检查应用程序是否处于调试状态以及是否已经发出第一个请求。
if self.debug and self._got_first_request:
raise AssertionError('A setup function was called after the '
'first request was handled. This usually indicates a bug '
'in the application where a module was not imported '
'and decorators or other functionality was called too late.\n'
'To fix this make sure to import all your view modules, '
'database models and everything related at a …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用greate库混合器生成一些夹具来测试Flask应用程序。我已经在Django项目上使用了它,并且运行良好,但是在flask + sqlalchemy上,无论我做什么我都会收到空值。
from mixer.backend.flask import mixer
from models import Users
from myproj import app
mixer.init_app(app)
me.blend(Users)
Run Code Online (Sandbox Code Playgroud)
返回null。
我也尝试直接实例化该应用程序。
from mixer.backend.flask import Mixer
from models import Users
from myproj import app
mixer = Mixer(app=app)
me = mixer.blend(Users)
Run Code Online (Sandbox Code Playgroud)
返回相同的空值。
两种情况都返回一个自动生成的ID。并且保存到数据库,但是所有字段都为Null。
知道为什么吗?
谢谢
我有一个继承的类并覆盖了一个也从基类继承的方法.但问题是中间方法创建了一个异常,我想通过调用第一个声明的方法来绕过它.有没有办法指定忽略第二次调用的mro?
一个例子可能是:
class Base(object):
def __init__(self):
res = "Want this"
print res
class BaseA(Base):
def __init__(self):
res = super(BaseA, self).__init__()
res = "Not this"
print res
class BaseB(BaseA):
def __init__(self):
res = super(BaseB, self).__init()
#At this poing res is "Not this"
#The desire is that it would be "Want this"
print res
Run Code Online (Sandbox Code Playgroud)
非常感谢
PD:像BaseB(Base,BaseA)这样的东西可以工作吗?
python ×6
flask ×2
fixtures ×1
generator ×1
inheritance ×1
namespaces ×1
packaging ×1
profiling ×1
setup.py ×1
super ×1
unit-testing ×1