我有一个包含excel文档数据的BytesIO对象.我想要使用的库不支持BytesIO,而是需要一个File对象.如何获取BytesIO对象并将其转换为File对象?
我正在为刚接触Python的用户编写一组测试用例.我在测试中注意到的一个问题是可能会出现误报.他们可能已经幸运并碰巧以正确的顺序给出每个元素,但他们确实应该使用有序的结构.
到目前为止,这是我能想出的最佳解决方案.
self.assertTrue(isinstance(result, Sequence) or
isinstance(result, GeneratorType) or
callable(getattr(result, '__reversed__', False)))
Run Code Online (Sandbox Code Playgroud)
但是,我不确定GeneratorType
是否真的有序,或者这个测试是全面的.我觉得应该有更好的方法来测试这个.如何测试结构是否有订单?
我正在尝试以编程方式生成requirements.txt文件.这样我可以将它与第二个.txt文件区分开来.到目前为止,我已经尝试了以下内容,但它们只向控制台输出要求(生成No .txt文件).
到目前为止我已经尝试过了
import pip
pip.main(["freeze",">","requirements.txt"])
Run Code Online (Sandbox Code Playgroud)
和
from subprocess import call
call(["pip","freeze", ">","requirements.txt"])
Run Code Online (Sandbox Code Playgroud)
如果我尝试在终端中手动运行相同的命令,则生成的文件没有问题.有什么建议?
我正试图弄清楚如何使用Alembic获取我的数据库版本.我已经将数据库设置为使用alembic并成功执行了升级和降级.我现在想从我自己的python脚本中获取此版本.
我试图创建一个这样做的功能
def get_current_database_version():
path = os.path.join(os.path.dirname(__file__), os.path.pardir)
alembic_cfg = Config(os.path.join(path, 'alembic.ini'))
current_rev = command.current(alembic_cfg, head_only=True)
return current_rev
Run Code Online (Sandbox Code Playgroud)
这个函数返回了一个 NoSectionError: No section: 'formatters'
然后我去了我的alembic.ini文件,检查它是否有格式化区域.这是我的alembic.ini文件:
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
pyramid_config_file = ../../development.ini
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' …
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
test_string = 'not empty'
if test_string:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
我知道我可以构造一个条件表达式来做到这一点:
return True if test_string else False
Run Code Online (Sandbox Code Playgroud)
但是,当我宁愿返回布尔值时,我不喜欢测试布尔值是真还是假.我怎么才能恢复它的真实性?
我正在编写一个解析文件的库,创建一个表示文件的对象,并允许将对象导出回文件.
我想验证每次更改这些值时都包含所需的标题和列.因此,我试图使用@property装饰器设置验证.
我在@property的python文档中注意到,如果属性名称为'variable',则使用'_variable'.据我所知,前面的单个下划线表示该变量仅供弱内部使用.但是,我认为@property装饰器的意思是任何设置变量的调用都会导致setter函数运行.
_headers = None
required_headers = ['FIELD_DELIM', 'VIDEO_FORMAT', 'FPS']
@property
def headers(self):
return self._headers
@headers.setter
def headers(self, value):
for header in self.required_headers:
if header not in value:
raise Exception
self._headers = value
Run Code Online (Sandbox Code Playgroud)
虽然这段代码有效,但我知道我仍然可以通过执行myObject._headers = value来绕过我的setter.
有没有办法可以确保始终执行验证而不依赖于用户尊重_headers是供内部使用?
这是我作为测试编写的代码片段。我注意到,如果我不将 init 方法定义为类方法,则代码不会运行:
class A(object):
def __init__(self):
self.value = 0
self.add(1)
@classmethod
def add(self, arg):
self.value += arg
class B(A):
@classmethod
def add(self, arg):
self.value += arg * 2
if __name__ == '__main__':
a = A()
b = B()
print a.value
print b.value
Run Code Online (Sandbox Code Playgroud)
这输出:
Traceback (most recent call last):
File "inherit.py", line 17, in <module>
a = A()
File "inherit.py", line 4, in __init__
self.add(1)
File "inherit.py", line 8, in add
self.value += arg
AttributeError: type object 'A' has no …
Run Code Online (Sandbox Code Playgroud) 我正在想办法在 git 中使用分支。当时我在 master 中有不相关的提交,我还没有准备好推送,但我不得不做一个错误修复。我不想创建一个新的 repo 只是为了做一个小错误修复,所以我创建了一个基于 origin/master 的分支
git checkout -b example origin/master
根据我的理解,这会创建一个基于 origin/master 分支的分支,并切换到示例分支。
然后我去做了我的修复,提交并测试了它。
$ git status
On branch example
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)
然后我去把它推给主人。
$ git push origin master
但是我没有在示例分支中推送提交,而是找到了这个
$ git status
On branch example
Your branch and 'origin/master' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" to merge …
Run Code Online (Sandbox Code Playgroud) python ×7
alembic ×1
boolean ×1
git ×1
git-branch ×1
git-push ×1
inheritance ×1
pip ×1
properties ×1
sqlalchemy ×1
string ×1
subprocess ×1
testing ×1
truthiness ×1
validation ×1