小编ale*_*cxe的帖子

为什么我们在Command设计模式中需要一个"接收器"类

我正在学习命令设计模式.据我所知,总是与命令模式相关的四个术语是命令,接收器,调用者和客户端.

具体的命令类有一个execute()方法,调用者有几个命令.调用者决定何时调用execute()命令的方法.

execute()方法被调用时,它会调用接收器的方法.然后,接收器完成工作.

我不明白为什么我们需要接收器类?我们可以在内部工作execute(),似乎接收器类是多余的.

预先感谢.

oop design-patterns

7
推荐指数
3
解决办法
2795
查看次数

XLWT多种款式

这一直困扰着我目前的项目.我正在尝试使用XLWT将样式写入excel表,见下文:

sheet.write(rowi,coli,value,stylesheet.bold,stylesheet.bordered)
Run Code Online (Sandbox Code Playgroud)

我遇到了这个错误:

TypeError:write()最多需要5个参数(给定6个)

有没有想过如何解决这个问题,以便为某个单元添加多种样式?可以在这里做一个清单吗?

python excel xlwt

7
推荐指数
1
解决办法
2万
查看次数

用于循环项目拆包

有一次,在看了Mike Muller的性能优化教程(我认为这个)之后,一个想法开始存在于我的脑海中:如果性能很重要,最小化通过索引访问循环中的项目,例如,如果您需要x[1]在循环中多次访问for x in l-为变量赋值x[1]并在循环中重用它.

现在我有了这个合成的例子:

import timeit

SEQUENCE = zip(range(1000), range(1, 1001))

def no_unpacking():
    return [item[0] + item[1] for item in SEQUENCE]


def unpacking():    
    return [a + b for a, b in SEQUENCE]


print timeit.Timer('no_unpacking()', 'from __main__ import no_unpacking').timeit(10000)
print timeit.Timer('unpacking()', 'from __main__ import unpacking').timeit(10000)
Run Code Online (Sandbox Code Playgroud)

unpacking()no_unpacking()函数返回相同的结果.实现方式不同:unpacking()将项目解包到循环中ab循环中; no_unpacking()通过索引获取值.

对于python27,它显示:

1.25280499458
0.946601867676
Run Code Online (Sandbox Code Playgroud)

换言之,unpacking()表现优于no_unpacking()约25%.

问题是:

  • 为什么通过索引访问会显着减慢事情(即使在这个简单的情况下)?

奖金问题:

  • 我也试过这个pypy- …

python performance for-loop iterable-unpacking

7
推荐指数
1
解决办法
344
查看次数

如何使用grunt和量角器进行单e2e测试

我假设这是可能的,实际上非常简单,但我是咕噜咕噜和量角器的新手,我无法在网上找到答案(也许我使用了错误的搜索条件).

我在文件中有以下e2e测试test/e2e/Recipients.js:

describe('Recipients Tab', function() {

    beforeEach(function () {
        browser.get('#/recipients');
    });

    it('should have no e-mail list', function () {
        expect(element(by.css('accordion')).isPresent()).toBe(false);
    });
});
Run Code Online (Sandbox Code Playgroud)

目前,我这样做:

grunt e2e
Run Code Online (Sandbox Code Playgroud)

我的量角器配置文件:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    capabilities: {
        'browserName': 'chrome'
    },
    specs: ['../e2e/**/*.js'],
    baseUrl : 'http://localhost:8080/spr',

    jasmineNodeOpts: {
        showColors: true // Use colors in the command line report.
    }
};
Run Code Online (Sandbox Code Playgroud)

当然这会运行我所有的测试,但是当我正在开发一个特定的测试时,我不想运行整个测试.我想运行这个文件.

我怎样才能做到这一点?有旗帜还是什么?

谢谢

javascript end-to-end angularjs gruntjs protractor

7
推荐指数
2
解决办法
9349
查看次数

使用promises配置多个功能

这是带有量角器主题的Set firefox配置文件的后续内容.

根据setFirefoxProfilehowto,可以设置一个firefox配置文件,其中包含一个特殊的"helper"js代码,该代码使用firefox-profileq库来动态编写 firefox配置文件.

这对我有用,multiCapabilities直到我尝试使用多个浏览器并配置:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',

    multiCapabilities: [
        {
            browserName: 'chrome',
            specs: [
                'footer.disabledCookies.spec.js'
            ],
            chromeOptions: {
                prefs: {
                    'profile.default_content_settings.cookies': 2
                }
            }
        },

        ...
        // other capabilities here
        ...

        helper.getFirefoxProfile()    
     },

     ...
}
Run Code Online (Sandbox Code Playgroud)

使用此设置我收到一个错误(这里是完整的回溯):

规格模式与任何文件都不匹配.

据我所知,这意味着使用firefox配置文件的设置缺少specs密钥.换句话说,它找不到任何运行的测试.

我试图在帮助器本身内部包含specscapabilities字典中,但错误仍然存​​在.

如果使用multiCapabilities?如何修复错误并设置firefox配置文件?


作为一种解决方法,我创建了一个单独的量角器配置文件,只配置了firefox(使用capabilities)并设置grunt …

javascript firefox selenium angularjs protractor

7
推荐指数
1
解决办法
1748
查看次数

使用BeautifulSoup删除第一个子节点

import os
from bs4 import BeautifulSoup
do = dir_with_original_files = 'C:\FOLDER'
dm = dir_with_modified_files = 'C:\FOLDER'
for root, dirs, files in os.walk(do):
    for f in files:
        print f.title()
        if f.endswith('~'): #you don't want to process backups
            continue
        original_file = os.path.join(root, f)
        mf = f.split('.')
        mf = ''.join(mf[:-1])+'_mod.'+mf[-1] # you can keep the same name 
                                             # if you omit the last two lines.
                                             # They are in separate directories
                                             # anyway. In that case, mf = f
        modified_file = os.path.join(dm, mf) …
Run Code Online (Sandbox Code Playgroud)

html python parsing beautifulsoup html-parsing

7
推荐指数
1
解决办法
4663
查看次数

在 Django 项目中放置外部 python 脚本的位置?

有人应该在哪里为她的 Django 项目放置任何外部 python 脚本?更合适的位置是什么(如果有的话)?她应该在主 Django 项目中创建一个文件夹并将其放在那里并将其添加到 python 路径中还是有更好的方法来处理这个问题?使用外部脚本的原因是不要使用可以在脚本文件中更好地组织并且可以为多个视图提供服务的代码来重载视图。

python django django-views

7
推荐指数
1
解决办法
9092
查看次数

在程序运行期间查看生成列表

故事:

Nina Zakharenko的PyCon谈论 Python内存管理期间,她解释了Python 中分代垃圾收集的工作方式,注意到:

Python维护一个程序运行时创建的每个对象的列表.实际上,它使3:

  • generation 0
  • generation 1
  • generation 2

问题:

为了更好地理解Python中的内存管理以及调试内存泄漏的目的,如何在程序运行期间观察/观察在所有3代列表中添加和删除的对象

我查看了gc模块,但没有找到获取当前生成列表值的相关方法.

python memory debugging garbage-collection memory-management

7
推荐指数
1
解决办法
431
查看次数

断言数组减少为true

在其中一个测试中,我们需要声明存在3个元素中的一个.目前我们正在使用protractor.promise.all()Array.reduce():

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

protractor.promise.all([
    title.isPresent(),
    summary.isPresent(),
    description.isPresent()
]).then(function (arrExists) {
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来解决它与Jasmine没有明确解决承诺?我们需要一个自定义匹配器,还是可以通过内置匹配器解决?

javascript arrays testing jasmine protractor

7
推荐指数
1
解决办法
247
查看次数

使用BeautifulSoup CSS Selector获取文本

示例HTML

<h2 id="name">
    ABC
    <span class="numbers">123</span>
    <span class="lower">abc</span>
</h2>
Run Code Online (Sandbox Code Playgroud)

我可以用以下内容获得数字:

soup.select('#name > span.numbers')[0].text
Run Code Online (Sandbox Code Playgroud)

如何ABC使用BeautifulSoup和select函数获取文本?

在这种情况下怎么样?

<div id="name">
    <div id="numbers">123</div> 
    ABC
</div>
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup css-selectors html-parsing python-2.7

7
推荐指数
1
解决办法
5744
查看次数