小编Ale*_*lex的帖子

如何定义两个位置参数的互斥组?

我想用argparse以下两种方式来制作一些代码:

./tester.py all
./tester.py name someprocess
Run Code Online (Sandbox Code Playgroud)

即任何all一个指定OR或name一些额外的字符串.

我试图实现如下:

import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('all', action='store_true', \
        help = "Stops all processes")
group.add_argument('name', \
        help = "Stops the named process")

print parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误

ValueError: mutually exclusive arguments must be optional
Run Code Online (Sandbox Code Playgroud)

知道怎么做对吗?在这种情况下,我也想避免使用子解析器.

python python-2.7 argparse

14
推荐指数
2
解决办法
8249
查看次数

Android logcat错误:"E/CliptrayUtils:hideClipTrayIfNeeded()TextView聚焦!! hideClipTray()"

我是新手,对Android没有经验,试图用Android Studio 1.4在Ubuntu 14.04中开发应用程序.我使用两个活动,这些活动,布局等都没有使用'ClipTray'.但是,在logcat我看到以下条目:

E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
Run Code Online (Sandbox Code Playgroud)

谷歌并没有真正帮助这里(即使只是短语,也没有运气找到任何东西hideClipTrayIfNeeded().

虽然这被列为错误,但应用程序似乎没有问题!但我不喜欢有不确定的错误!

我还在我的日志文件中随机看到这些行:

D/CliptrayUtils: setInputTypeforClipTray(): 0
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议.

另外:我刚刚意识到这个错误可能只发生在应用程序在实际手机上运行并关闭显示屏时.这可能是导致此错误的原因吗?然后它与实际的应用程序无关,我会很高兴...

android android-studio

14
推荐指数
1
解决办法
3755
查看次数

如何在selenium中使用chrome webdriver在python中下载文件?

根据这里这里的帖子,我试图在selenium中使用chrome webdriver来下载文件.这是迄今为止的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("profile.default_content_settings.popups", 0)
chrome_options.add_experimental_option("download.prompt_for_download", "false")
chrome_options.add_experimental_option("download.default_directory", "/tmp")

driver = webdriver.Chrome(chrome_options=chrome_options)
Run Code Online (Sandbox Code Playgroud)

但仅这一点会导致以下错误:

WebDriverException: Message: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: download.default_directory
  (Driver info: chromedriver=2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3),platform=Linux 4.10.0-37-generic x86_64)
Run Code Online (Sandbox Code Playgroud)

那么如何解决这个问题呢?我必须使用这种"能力"的东西吗?如果是这样,怎么样?

python selenium google-chrome

14
推荐指数
3
解决办法
3万
查看次数

如何在python中的类中处理asyncore,而不阻塞任何东西?

我需要创建一个可以接收和存储SMTP消息的类,即电子邮件.为此,我asyncore根据此处发布的示例使用.但是,asyncore.loop()阻塞所以我不能在代码中做任何其他事情.

所以我想到了使用线程.这是一个示例代码,显示了我的想法:

class MyServer(smtpd.SMTPServer):
    # derive from the python server class

    def process_message(..):
        # overwrite a smtpd.SMTPServer method to be able to handle the received messages
        ...
        self.list_emails.append(this_email)

    def get_number_received_emails(self):
        """Return the current number of stored emails"""
        return len(self.list_emails)


    def start_receiving(self):
        """Start the actual server to listen on port 25"""

        self.thread =   threading.Thread(target=asyncore.loop)
        self.thread.start()     

    def stop(self):
        """Stop listening now to port 25"""
        # close the SMTPserver from itself
        self.close()
        self.thread.join()
Run Code Online (Sandbox Code Playgroud)

我希望你能得到这张照片.该类MyServer应该能够以非阻塞的方式开始和停止侦听端口25,能够在收听(或不收听)时查询消息.该start …

python multithreading smtp asyncore

13
推荐指数
1
解决办法
9009
查看次数

如何在python-selenium中使用browsermob?

我想在使用selenium进行GUI测试时使用browsermob来监控网络连接.我在这里,这里这里找到了一些信息和文档,但是如何真正使用它绝对不清楚.

在文档中,它读到:

server = Server("path/to/browsermob-proxy")
Run Code Online (Sandbox Code Playgroud)

但那条路是什么?哪里可以找到它?

我也明白了

java -jar browsermob.jar --port 9090
Run Code Online (Sandbox Code Playgroud)

但是没有解释这个jar文件是什么,如果它是browsermob安装的一部分,或者是不相关的东西.

如果有人能提供关于如何使用browsermob的COMPLETE和WORKING示例,以及我需要安装的所有内容,我将不胜感激......

python selenium browsermob

13
推荐指数
3
解决办法
6190
查看次数

如何通过__getattr__将参数传递给函数

请考虑以下代码示例(python 2.7):

class Parent:
    def __init__(self, child):
        self.child = child

    def __getattr__(self, attr):
        print("Calling __getattr__: "+attr)
        if hasattr(self.child, attr):
            return getattr(self.child, attr)
        else:
            raise AttributeError(attr)

class Child:
    def make_statement(self, age=10):
        print("I am an instance of Child with age "+str(age))

kid = Child()
person = Parent(kid)

kid.make_statement(5)
person.make_statement(20)
Run Code Online (Sandbox Code Playgroud)

可以证明,该函数调用person.make_statement(20)调用Child.make_statement通过函数Parent__getattr__功能.在__getattr__函数中,我可以在调用子实例中的相应函数之前打印出属性.到目前为止这么清楚.

但是这个呼叫的论点是如何person.make_statement(20)通过的__getattr__呢?我怎么能在我的__getattr__功能中打印出数字'20' ?

python inheritance arguments function getattr

12
推荐指数
1
解决办法
5760
查看次数

如何ConfigParse文件保持相同键的多个值?

我需要能够使用它ConfigParser来读取同一个键的多个值.示例配置文件:

[test]
foo = value1
foo = value2
xxx = yyy
Run Code Online (Sandbox Code Playgroud)

使用"标准"使用时ConfigParser,将有一个foo具有该值的键value2.但我需要解析器读入两个值.

重复键上输入后,我创建了以下示例代码:

from collections import OrderedDict
from ConfigParser import RawConfigParser

class OrderedMultisetDict(OrderedDict):
    def __setitem__(self, key, value):

        try:
            item = self.__getitem__(key)
        except KeyError:
            super(OrderedMultisetDict, self).__setitem__(key, value)
            return

        print "item: ", item, value
        if isinstance(value, list):
            item.extend(value)
        else:
            item.append(value)
        super(OrderedMultisetDict, self).__setitem__(key, item)


config = RawConfigParser(dict_type = OrderedDict)
config.read(["test.cfg"])
print config.get("test",  "foo")
print config.get("test",  "xxx")

config2 = RawConfigParser(dict_type = OrderedMultisetDict)
config2.read(["test.cfg"])
print …
Run Code Online (Sandbox Code Playgroud)

python configparser python-2.7

12
推荐指数
3
解决办法
1万
查看次数

如何在Windows上使用pip install修复错误"预期的版本规范..."

在Windows 7计算机上,我使用以下命令从本地目录安装软件包:

pip install addons/pnc_tests --upgrade --extra-index-url=http://some_server/simple
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

C:\Users\alex\PNC\tas\ENV\Scripts\pip-script.py run on 07/16/14 07:50:47

Exception:
Traceback (most recent call last):
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\basecommand.py", line 122, in main
    status = self.run(options, args)
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\commands\install.py", line 258, in run
    InstallRequirement.from_line(name, None))
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\req.py", line 173, in from_line
    return cls(req, comes_from, url=url, prereleases=prereleases)
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\req.py", line 71, in __init__
    req = pkg_resources.Requirement.parse(req)
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\_vendor\pkg_resources.py", line 2667, in parse
    reqs = list(parse_requirements(s))
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\_vendor\pkg_resources.py", line 2605, in parse_requirements
    line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")
  File "C:\Users\alex\PNC\tas\ENV\lib\site-packages\pip\_vendor\pkg_resources.py", …
Run Code Online (Sandbox Code Playgroud)

python windows

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

"客户端无法连接到D-BUS守护程序"是什么意思?

我正在使用一个非常复杂的设置来测试各种非公共网页.我用它jenkins来运行图像中的python-selenium测试docker.这样,我完全独立于jenkins环境,可以创建自己的环境.在这种环境中,我安装了以下软件:

  • Ubuntu 16.04.3
  • Firefox:Mozilla Firefox 57.0.1
  • geckodriver:0.18.0
  • nosetests:1.3.7
  • 硒:3.8.0

在运行大多数成功的测试时,我在geckodriver.log输出消息中看到了

(firefox:55): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
/usr/bin/dbus-launch terminated abnormally without any error message
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 这条消息是什么意思?
  • 这可能表明有时测试失败的原因吗?
  • 如果是这样,如何解决?

python selenium jenkins

12
推荐指数
1
解决办法
7300
查看次数

如何录制在码头工作者中无头跑的硒测试?

我正在使用无头firefox在docker中运行python-selenium测试.

在这些测试期间,我能够使用selenium方法制作屏幕截图截图 - 但是我可以在整个测试过程中使用"视频"记录虚拟显示的东西(几个测试脚本有多种测试方法,许多webdrivers启动和停止).

那么我该如何录制完整的测试会话?

附录:我找到了一个能够准确描述我需要的网页:这里.不幸的是,当我尝试录制时出现错误.这是我正在做的命令:

xvfb-run --listen-tcp --server-num 44 --auth-file /tmp/xvfb.auth -s "-ac -screen 0 1920x1080x24" python seltest.py &
ffmpeg -f x11grab -video_size 1920x1080 -i 127.0.0.1:44 -codec:v libx264 -r 12 /tmp/behat_1.mp4
Run Code Online (Sandbox Code Playgroud)

并且错误是(对于第二个命令):

[x11grab @ 0x1d289c0] Cannot open display 127.0.0.1:44, error 1.
127.0.0.1:44: Input/output error
Run Code Online (Sandbox Code Playgroud)

python selenium ffmpeg xvfb pyvirtualdisplay

12
推荐指数
1
解决办法
1374
查看次数