装饰的功能@staticmethod和装饰的功能有什么区别@classmethod?
如果一个函数没有抛出预期的异常,那么如何编写一个单元测试失败?
如何了解安装给定Python模块的源文件的位置?Windows上的方法与Linux上的方法不同吗?
我正在努力寻找datetime模块的来源,但我也对更一般的答案感兴趣.
我需要知道Python中的变量是字符串还是字典.以下代码有什么问题吗?
if type(x) == type(str()):
do_something_with_a_string(x)
elif type(x) == type(dict()):
do_somethting_with_a_dict(x)
else:
raise ValueError
Run Code Online (Sandbox Code Playgroud)
更新:我接受了avisser的回答(但如果有人解释为什么isinstance更喜欢,我会改变主意type(x) is).
但由于nakedfanatic提醒我,这是经常清洁剂使用的字典(作为case语句)比如果/ elif的/其他系列.
让我详细说明我的用例.如果变量是一个字符串,我需要将它放在一个列表中.如果它是一个字典,我需要一个唯一值的列表.这是我想出的:
def value_list(x):
cases = {str: lambda t: [t],
dict: lambda t: list(set(t.values()))}
try:
return cases[type(x)](x)
except KeyError:
return None
Run Code Online (Sandbox Code Playgroud)
如果isinstance是首选,你会怎么写这个value_list()功能?
如果我执行以下操作:
import subprocess
from cStringIO import StringIO
subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one\ntwo\nthree\nfour\nfive\nsix\n')).communicate()[0]
Run Code Online (Sandbox Code Playgroud)
我明白了:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 533, in __init__
(p2cread, p2cwrite,
File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: 'cStringIO.StringI' object has no attribute 'fileno'
Run Code Online (Sandbox Code Playgroud)
显然,一个cStringIO.StringIO对象不能足够接近文件duck以适应subprocess.Popen.我该如何解决这个问题?
我正在尝试在使用OpenSSL 1.0.0的Ubuntu 10.04 LTS上构建一些代码.当我运行make时,它使用"-lssl"选项调用g ++.来源包括:
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/des.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
Run Code Online (Sandbox Code Playgroud)
我跑了:
$ sudo apt-get install openssl
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Run Code Online (Sandbox Code Playgroud)
但我想openssl包不包含库.我在make上遇到这些错误:
foo.cpp:21:25: error: openssl/bio.h: No such file or directory
foo.cpp:22:28: error: openssl/buffer.h: No such file or directory
foo.cpp:23:25: error: openssl/des.h: No such file or directory
foo.cpp:24:25: error: openssl/evp.h: …Run Code Online (Sandbox Code Playgroud) 如何在C++中实现以下(Python伪代码)?
if argv[1].startswith('--foo='):
foo_value = int(argv[1][len('--foo='):])
Run Code Online (Sandbox Code Playgroud)
(例如,如果argv[1]是--foo=98,则foo_value是98.)
更新:我对调查Boost犹豫不决,因为我只是想对一个简单的小命令行工具进行一个非常小的改动(我宁愿不必学习如何链接并使用Boost作为一个小的更改).
从包含名为bar(包含一个或多个文件)的目录和名为baz(还包含一个或多个文件)的目录的目录中运行以下代码.确保没有名为的目录foo.
import shutil
shutil.copytree('bar', 'foo')
shutil.copytree('baz', 'foo')
Run Code Online (Sandbox Code Playgroud)
它将失败:
$ python copytree_test.py
Traceback (most recent call last):
File "copytree_test.py", line 5, in <module>
shutil.copytree('baz', 'foo')
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/shutil.py", line 110, in copytree
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.py", line 172, in makedirs
OSError: [Errno 17] File exists: 'foo'
Run Code Online (Sandbox Code Playgroud)
我希望它的工作方式与我输入的方式相同:
$ mkdir foo
$ cp bar/* foo/
$ cp baz/* foo/
Run Code Online (Sandbox Code Playgroud)
我是否需要使用shutil.copy()将每个文件复制baz到foo?(之后我已经将'bar'的内容复制到'foo'中shutil.copytree()?)或者是否有更简单/更好的方法?
python ×8
c++ ×2
installation ×2
copytree ×1
exception ×1
match ×1
methods ×1
module ×1
oop ×1
openssl ×1
parsing ×1
regex ×1
search ×1
shutil ×1
startswith ×1
stdin ×1
string ×1
subprocess ×1
substring ×1
typechecking ×1
types ×1
ubuntu ×1
unit-testing ×1