我有一个python脚本必须启动一个shell命令为每个文件在目录中: import os files = os.listdir(".") for f in files: os.execlp("myscript", "myscript", f) 这对第一个文件工作正常,但在“myscript”命令结束后,执行停止,不会回到python脚本。 我能怎么做? 在calling os.execlp()之前,我必须fork() calling os.execlp()吗?
假设这样的代码: class Base: def start(self): pass def stop(self) pass class A(Base): def start(self): … do something for A def stop(self) …. do something for A class B(Base): def start(self): def stop(self): a1 = A(); a2 = A() b1 = B(); b2 = B() all = [a1, b1, b2, a2,…..] 现在我想调用方法开始和停止(也许还有其他)列表中的每个对象。 有没有什么优雅的方式来做这个,除了写一堆函数之外 def start_all(all): for item […]
如何执行assert almost equal与py.test相同的浮动,而不诉诸于像这样的东西: assert x – 0.00001 <= y <= x + 0.00001 更具体地说,知道一个简洁的解决scheme可以快速比较浮点数对, assert (1.32, 2.4) == i_return_tuple_of_two_floats()
在我的Django应用程序中,我有一个身份validation系统。 所以,如果我不login并尝试访问某个个人资料的个人信息,我将被redirect到一个login页面。 现在,我需要为此写一个testing用例。 我得到的浏览器的回应是: GET /myprofile/data/some_id/ HTTP/1.1 302 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 301 0 GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 200 6533 我如何编写我的testing? 这是我迄今为止: self.client.login(user="user", password="passwd") response = self.client.get('/myprofile/data/some_id/') self.assertEqual(response.status,200) self.client.logout() response = self.client.get('/myprofile/data/some_id/') 接下来会发生什么?
有人可以build议我怎么可以在Python中或通过命令行美化JSON? 唯一可以做到的基于在线的JSON美化器是: http : //jsonviewer.stack.hu/ 。 但是,我需要从Python中使用它。 这是我的数据集: { "head": {"vars": [ "address" , "description" ,"listprice" ]} , "results": { "bindings": [ { "address" : { "type":"string", "value" : " Dyne Road, London NW6"}, "description" :{ "type":"string", "value" : "6 bed semi detached house"}, "listprice" : { "type":"string", "value" : "1,150,000"} } , { "address" […]
如何将ConfigParser.items('section')的结果转换为字典来格式化string,如下所示: import ConfigParser config = ConfigParser.ConfigParser() config.read('conf.ini') connection_string = ("dbname='%(dbname)s' user='%(dbuser)s' host='%(host)s' " "password='%(password)s' port='%(port)s'") print connection_string % config.items('db')
这里是我的错误信息的尝试。 我究竟做错了什么? string.decode("ascii", "ignore") UnicodeEncodeError:'ascii'编解码器不能对位置37的字符u'\ xa0'进行编码:序号不在范围内(128) string.encode('utf-8', "ignore") UnicodeDecodeError:'ascii'编解码器无法解码位置37中的字节0xc2:序号不在范围内(128)
如何根据当前使用Python的datefind上个星期一的date? 我想也许我可以使用: datetime.weekday()来做到这一点,但我陷入困境。 我基本上想要find今天的date和星期一的date,以在django中使用: created__range=(start_date, end_date)来构builddate范围查询。
这是一个挑战,拿出最优雅的JavaScript,ruby或其他解决scheme,以相对微不足道的问题。 这个问题是最长的公共子串问题的一个更具体的例子。 我只需要在数组中find最长的公共起始子string。 这大大简化了这个问题。 例如, [interspecies, interstelar, interstate]最长的子串是“inters”。 但是,我不需要在[specifics, terrific]find“定义”。 我已经解决了这个问题,通过在JavaScript中快速编写一个解决scheme,作为我关于shell-like tab-completion ( testing页面 )的答案的一部分。 这个解决scheme略有调整: function common_substring(data) { var i, ch, memo, idx = 0 do { memo = null for (i=0; i < data.length; i++) { ch = data[i].charAt(idx) if (!ch) break if (!memo) memo = ch else if (ch != memo) break […]
在现有的“什么是你最有用的C / C ++片段”的精神 – 线程: 你们是否有简短的单functionPython片段(经常使用),并希望与StackOverlow社区分享? 请保持input较小(可能不超过25行),每个post只能有一个例子。 我将以不时使用的简短片段来计算python项目中的sloc(源代码行): # prints recursive count of lines of python source code from current directory # includes an ignore_list. also prints total sloc import os cur_path = os.getcwd() ignore_set = set(["__init__.py", "count_sourcelines.py"]) loclist = [] for pydir, _, pyfiles in os.walk(cur_path): for pyfile in pyfiles: if pyfile.endswith(".py") and […]