众所周知,python __del__方法不应该用于清理重要的东西,因为不能保证调用此方法.另一种方法是使用上下文管理器,如几个线程中所述.
但我不太明白如何重写一个类来使用上下文管理器.详细说明,我有一个简单的(非工作)示例,其中包装器类打开并关闭设备,并且在任何情况下,类的实例都超出其范围(异常等)时将关闭设备.
第一个文件mydevice.py是打开和关闭设备的标准包装类:
class MyWrapper(object):
def __init__(self, device):
self.device = device
def open(self):
self.device.open()
def close(self):
self.device.close()
def __del__(self):
self.close()
Run Code Online (Sandbox Code Playgroud)
这个类由另一个类使用myclass.py:
import mydevice
class MyClass(object):
def __init__(self, device):
# calls open in mydevice
self.mydevice = mydevice.MyWrapper(device)
self.mydevice.open()
def processing(self, value):
if not value:
self.mydevice.close()
else:
something_else()
Run Code Online (Sandbox Code Playgroud)
我的问题:当我mydevice.py用with __enter__和__exit__方法实现上下文管理器时,如何处理这个类myclass.py?我需要做点什么
def __init__(self, device):
with mydevice.MyWrapper(device):
???
Run Code Online (Sandbox Code Playgroud)
但是如何处理呢?也许我忽略了一些重要的事情?或者我可以仅在函数内使用上下文管理器而不是作为类范围内的变量?
这个问题与之前提出的问题有关,但可能不相关.问题是:在使用subparsers时,如何在下面给定(工作)示例的帮助文本中使用换行符?
import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers()
parser_start = subparsers.add_parser('stop')
parser_start.add_argument("file", help = "firstline\nnext line\nlast line")
print parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
我的输出如下:
tester.py stop -h
usage: tester.py stop [-h] file
positional arguments:
file firstline next line last line
optional arguments:
-h, --help show this help message and exit
Run Code Online (Sandbox Code Playgroud)
帮助的预期输出file应该是:
first line
next line
last line
Run Code Online (Sandbox Code Playgroud) 我正在使用远程seleniumwebdriver执行一些测试.但是,在某些时候,我需要下载一个文件并检查其内容.
我使用远程webdriver如下(in python):
PROXY = ...
prefs = {
"profile.default_content_settings.popups":0,
"download.prompt_for_download": "false",
"download.default_directory": os.getcwd(),
}
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("prefs", prefs)
webdriver.DesiredCapabilities.CHROME['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}
driver = webdriver.Remote(
command_executor='http://aaa.bbb.ccc:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
Run Code Online (Sandbox Code Playgroud)
使用"普通"webdriver,我可以在本地计算机上下载文件而不会出现问题.然后我可以使用测试代码来验证下载文件的内容(可以根据测试参数进行更改).它不是对下载本身的测试,但我需要一种方法来验证生成的文件的内容 ......
但是如何使用远程 webdriver 做到这一点?我在任何地方都找不到任何帮助......
我试图chrome在docker图像中安装浏览器
RUN apt-get install chromium-browser
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Package chromium-browser is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'chromium-browser' has no installation candidate
Run Code Online (Sandbox Code Playgroud)
如何正确安装chromium在docker图像中?
以下是关于在Linux中重置串口的示例,我想翻译以下代码段
fd = open(filename, O_WRONLY);
ioctl(fd, USBDEVFS_RESET, 0);
close(fd);
Run Code Online (Sandbox Code Playgroud)
到有效的python代码.这是我到目前为止所尝试的
file_handler = open(self._port, 'w')
fcntl.ioctl(file_handler, termios.USBDEVFS_RESET)
file_handler.close()
Run Code Online (Sandbox Code Playgroud)
以错误结束'module' object has no attribute 'USBDEVFS_RESET'.该termios的文件是不是在这一点上是非常有帮助的,因为它没有列出的可能的性质termios.有关此类属性的示例,另请参见fcntl文档termios.
如何正确地将C代码"转换"为python2.7代码?
我有一个Linux标准头文件,例如
/usr/src/linux-headers-3.2.0-35/include/linux/usbdevice_fs.h
Run Code Online (Sandbox Code Playgroud)
其中包含define如下声明:
#define USBDEVFS_SUBMITURB32 _IOR('U', 10, struct usbdevfs_urb32)
#define USBDEVFS_DISCARDURB _IO('U', 11)
#define USBDEVFS_REAPURB _IOW('U', 12, void *)
Run Code Online (Sandbox Code Playgroud)
'_IOR','_IO'和'_IOW'是什么意思?实际上给了什么价值USBDEVFS_DISCARDURB?
我有以下测试代码
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", default = 0, type=int)
subparsers = parser.add_subparsers(dest = "parser_name")
parser_lan = subparsers.add_parser('car')
parser_lan.add_argument("--boo")
parser_lan.add_argument("--foo")
parser_serial = subparsers.add_parser('bus')
parser_serial.add_argument("--fun")
print parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
它定义了两个子解析器,具有不同的参数集.当我把测试代码称为
tester.py --verbose 3 car --boo 1 --foo 2
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果
Namespace(boo='1', foo='2', parser_name='car', verbose=3)
Run Code Online (Sandbox Code Playgroud)
我想要的是来自每个subparser的值在一个单独的命名空间或dict中,类似于
Namespace(subparseargs={boo:'1', foo:'2'}, parser_name='car', verbose=3)
Run Code Online (Sandbox Code Playgroud)
这样来自每个subparser的参数与主解析器中的参数逻辑分离(如verbose本例所示).
我怎样才能实现这一点,在同一名称空间中使用每个subparser的参数(subparseargs在示例中).
我在jenkins中使用了一些windows批处理命令,其中每个命令都可能失败.为了使每个步骤的jenkins作业失败,这些批处理命令如下所示:
net use m: \\%IP_ADDRESS%\Whatever %PASSWORD% /user:%USERNAME%
if ERRORLEVEL 1 exit 1
mkdir m:\Install
if ERRORLEVEL 1 exit 1
copy /b %LOCAL_TEMP%\%INSTALLER_EXE% m:\Install\%INSTALLER_EXE%
if ERRORLEVEL 1 exit 1
net use m: /D
if ERRORLEVEL 1 exit 1
Run Code Online (Sandbox Code Playgroud)
换句话说:我似乎检查每个批处理命令是否失败,然后如果需要则退出错误代码1.有没有更有意义/方便/更好的方法来实现这一目标?
如果我不检查每一步并退出,jenkins将执行以下批处理命令.
我有同样的问题,如在这里或这里发布了很多次.我定义了一个菜单项,它显示在预览中AndroidStudio:
但是当我在手机上运行应用程序时,图标(png图像)不可见,并且有很多可用空间.但是,此"添加"选项会显示在"选项"菜单中(最右侧;与"Srttings"一起显示).这是我的menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_favourite"
android:icon="@mipmap/ic_add"
android:title="@string/menu_add"
android:showAsAction="always"/>
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
Run Code Online (Sandbox Code Playgroud)
我尝试了我能找到的建议,但没有一个能解决我的问题.我的手机是LG G3.我怎么解决这个问题?
附加信息: onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常复杂的py.testpython-selenium测试设置,我在一个py.test夹具中创建一个Firefox webdriver .以下是我正在做的事情的一些想法:
'driver.py':
class Driver(object):
"""
Driver class with basic wrappers around the selenium webdriver
and other convenience methods.
"""
def __init__(self, config, options):
"""Sets the driver and the config.
"""
self.remote = options.getoption("--remote")
self.headless = not options.getoption("--with-head")
if self.headless:
self.display = Display(visible=0, size=(13660, 7680))
self.display.start()
# Start the selenium webdriver
self.webdriver = fixefox_module.get_driver()
Run Code Online (Sandbox Code Playgroud)
'conftest.py':
@pytest.fixture
def basedriver(config, options):
driver = driver.Driver(config, options)
yield driver
print("Debug 1")
driver.webdriver.quit()
print("Debug 2")
Run Code Online (Sandbox Code Playgroud)
在运行测试时,我只能看到Debug 1打印出来.整个过程此时停止,似乎没有继续进行.整个硒测试都停留在webdriver.quit) …