Tag: python

Monkey在Python的另一个模块中修补一个类

我正在使用其他人编写的模块。 我想猴子修补模块中定义的类的__init__方法。 我已经find的例子展示了如何做到这一点,都假设我自己会调用这个类(例如Monkey-patch Python类 )。 然而,这种情况并非如此。 在我的情况下,这个类在另一个模块的函数中初始化。 看下面的(大大简化的)例子: thirdpartymodule_a.py class SomeClass(object): def __init__(self): self.a = 42 def show(self): print self.a thirdpartymodule_b.py import thirdpartymodule_a def dosomething(): sc = thirdpartymodule_a.SomeClass() sc.show() mymodule.py import thirdpartymodule_b thirdpartymodule.dosomething() 有没有办法修改SomeClass的__init__方法,以便当从mymodule.py中调用dosomething时,例如打印43而不是42? 理想情况下,我可以包装现有的方法。 我无法更改第三方模块* .py文件,因为其他脚本依赖于现有的function。 我宁愿不必创build自己的模块副本,因为我需要做的更改非常简单。 编辑2013-10-24 我忽略了上面例子中的一个小而重要的细节。 SomeClass是像这样from thirdpartymodule_a import SomeClass : from thirdpartymodule_a import SomeClass 。 要做FJbuild议的补丁,我需要replacethirdpartymodule_b的副本,而不是thirdpartymodule_a 。 例如thirdpartymodule_b.SomeClass.__init__ = new_init […]

如何避免HTTP错误429(太多的请求)的Python

我正在尝试使用Pythonlogin到一个网站,并从几个网页收集信息,我得到以下错误: Traceback (most recent call last): File "extract_test.py", line 43, in <module> response=br.open(v) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open return self._mech_open(url, data, timeout=timeout) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open raise response mechanize._response.httperror_seek_wrapper: HTTP Error 429: Unknown Response Code 我用time.sleep() ,它的工作原理,但它似乎非智能和不可靠的,有没有其他方法来躲避这个错误? 这是我的代码: import mechanize import cookielib import re first=("example.com/page1") second=("example.com/page2") third=("example.com/page3") fourth=("example.com/page4") ## I have seven […]

根据内容过滤string列表

给定列表['a','ab','abc','bac'] ,我想计算一个列表中有'ab'string。 即结果是['ab','abc'] 。 这怎么可以在Python中完成?

Python的列表理解与.NET LINQ

以下简单的LINQ代码 string[] words = { "hello", "wonderful", "linq", "beautiful", "world" }; // Get only short words var shortWords = from word in words where word.Length <= 5 select word; // Print each word out shortWords.Dump(); 可以使用列表理解翻译成python如下。 words = ["hello", "wonderful", "linq", "beautiful", "world"] shortWords = [x for x in words if len(x) <=5] print shortWords […]

django – 将列表转换回查询集

我有一些我想根据计算值sorting的logging。 在这里得到答案…像这样: sorted(Profile.objects.all(), key=lambda p: p.reputation) 在一个像这样的configuration文件类: class Profile(models.Model): … @property def reputation(self): … 不幸的是,通用视图期望一个查询集对象,并且如果我给它一个列表就会抛出一个错误。 有没有办法做到这一点,返回一个查询集 要么… 我可以以某种方式将列表转换为查询集吗? 在django文档中找不到类似的东西。 我希望不要使数据非规范化,但是我想如果必须的话。 更新/回答: 看起来回到查询集的唯一方法是如果你可以把你所有的逻辑到sql查询。 当这是不可能的,(我认为)你需要非规范化的数据

Python:根据索引集从列表中select子集

我有几个列表具有所有相同数量的条目(每个指定对象属性): property_a = [545., 656., 5.4, 33.] property_b = [ 1.2, 1.3, 2.3, 0.3] … 并用相同长度的标志列出 good_objects = [True, False, False, True] (可以很容易地用一个等价的索引列表来代替: good_indices = [0, 3] 生成新列表property_asel , property_bsel ,最简单的方法是什么?它只包含由True条目或索引指示的值。 property_asel = [545., 33.] property_bsel = [ 1.2, 0.3]

检查键是否存在,并使用Python迭代JSON数组

我有一堆来自Facebookpost的JSON数据,如下所示: {"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": "1543", "name": "Honey Pinter"}]}, "type": "status", "id": "id_7"} JSON数据是半结构化的,全部不一样。 以下是我的代码: import json str = '{"from": {"id": "8", "name": "Mary Pinter"}, "message": "How ARE you?", "comments": {"count": 0}, "updated_time": "2012-05-01", "created_time": "2012-05-01", "to": {"data": [{"id": […]

为什么在编译为字节码之前,Python不计算常量数运算?

在下面的代码中,为什么Python不将f2编译为与f1相同的字节码? 有没有理由不? >>> def f1(x): x*100 >>> dis.dis(f1) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 1 (100) 6 BINARY_MULTIPLY 7 POP_TOP 8 LOAD_CONST 0 (None) 11 RETURN_VALUE >>> def f2(x): x*10*10 >>> dis.dis(f2) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 1 (10) 6 BINARY_MULTIPLY 7 LOAD_CONST 1 (10) 10 BINARY_MULTIPLY 11 POP_TOP 12 LOAD_CONST 0 […]

如何在Python中获取UTC时间?

我已经在StackExchange上search了一堆解决scheme,但没有什么是我需要的。 在JavaScript中,我使用以下来计算1970年1月1日以来的UTC时间: function UtcNow() { var now = new Date(); var utc = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds()); return utc; } 什么是等效的Python代码?

我怎样才能创build一个随机数在python密码保护?

我正在做一个Python项目,我想创build一个密码安全的随机数字,我该怎么做? 我已经在网上读到,规则随机数生成的数字不是encryption安全的,并且函数os.urandom(n)返回给我一个string,而不是一个数字。