是否有一个已知的日期/时间框架,不再支持python 2.7而支持python 3?
Python至少有六种格式化字符串的方法:
In [1]: world = "Earth"
# method 1a
In [2]: "Hello, %s" % world
Out[2]: 'Hello, Earth'
# method 1b
In [3]: "Hello, %(planet)s" % {"planet": world}
Out[3]: 'Hello, Earth'
# method 2a
In [4]: "Hello, {0}".format(world)
Out[4]: 'Hello, Earth'
# method 2b
In [5]: "Hello, {planet}".format(planet=world)
Out[5]: 'Hello, Earth'
# method 2c
In [6]: f"Hello, {world}"
Out[6]: 'Hello, Earth'
In [7]: from string import Template
# method 3
In [8]: Template("Hello, $planet").substitute(planet=world)
Out[8]: 'Hello, Earth'
Run Code Online (Sandbox Code Playgroud)
不同方法的简要历史:
printf
自Pythons婴儿期以来,风格格式一直存在 …python printf string-formatting backwards-compatibility deprecated
说我打开了一个终端,通过那个终端,我已经cd
去了一个目录.通过另一个终端,我删除该目录并从相同的备份恢复它.当我尝试vim
从第一个终端的文件,在同一目录中,为什么我收到有关陈旧文件句柄的错误?这是什么意思?(另一方面,我发现可以绕过这个问题cd $(pwd)
.)
nosetests --pdb
让我停止错误或失败,但这对我的需求来说已经太迟了.在执行期间单步执行代码可帮助我调试问题所在.
但是,nosetests是有用的,因为它们允许依赖于相对导入的测试(即在包中进行测试).
如何在执行测试之前设置断点?目前我正在使用:
python -m pdb /path/to/my/nosetests testfile.py
Run Code Online (Sandbox Code Playgroud)
这种解决方案是不够的.Nosetests干扰pdb输出,我的键盘控制(例如箭头键)被破坏.
使用import pdb; pdb.set_trace()似乎是一个好主意,但是nosetests阻止了我对pdb控制台的访问.
在pdb
该next
指令不跨过列表内涵,而是通过每个迭代步骤.有没有办法跨过它们,所以调试将在列表理解后的下一行继续?
我不得不求助于列出代码,在下一行设置断点,然后继续执行到下一个断点.这很烦人,我认为必须有更好的方法.
如何使用(r + g + b)/ 3方法将RGB图像(3个通道)转换为灰度图像?我查看了一个示例页面:http://www.imagemagick.org/Usage/color_mods/#grayscale 但是所需的方法:
convert test.png -fx '(r+g+b)/3' gray_fx_average.png
Run Code Online (Sandbox Code Playgroud)
给了我一个错误的结果 - 结果图像仍有3个频道.
您可以通过运行命令来检查:identify -format "%[colorspace] <== %f\n" *.png
.
我一直在搜索这里和一般的子分析器示例,但似乎无法想象这个看似简单的事情.
我有两个var类型,其中一个有约束,所以认为subparser是要走的路.例如-t允许"A"或"B".如果用户通过"A",那么他们还需要指定它是"a1"还是"a2".如果他们只通过"B"则没有.
我可以这样做并让argparse返回我通过什么类型的"A"或者它只是"B"?
下面似乎工作但由于某种原因在subparse后传递任何东西时中断.
例如来自Linux终端
>> python test01.py -t A a1 -v 61
Run Code Online (Sandbox Code Playgroud)
错误......
usage: test01.py a1 [-h]
test01.py a1: error: unrecognized arguments: -v
Run Code Online (Sandbox Code Playgroud)
希望这是有道理的.
代码:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='types of A')
parser.add_argument("-t",
choices = ["A", "B"],
dest = "type",
required=True,
action='store',
help="Some help blah blah")
cam_parser = subparsers.add_parser('a1', help='Default')
cam_parser.set_defaults(which='a1')
cam_parser = subparsers.add_parser('a2', help='parse this instead of default')
cam_parser.set_defaults(which='a2')
parser.add_argument("-v",
nargs = '+',
required=True,
dest = "version",
type=int,
action='store',
help="some version help blah blah")
argument …
Run Code Online (Sandbox Code Playgroud) 我从数据库中获取了一个带有以下变量的日期
{{ i.operation_date }}
Run Code Online (Sandbox Code Playgroud)
我得到了一个像这样的价值
April 1, 2013
Run Code Online (Sandbox Code Playgroud)
我需要在上面添加一年,这样我才能得到
April 1, 2014
Run Code Online (Sandbox Code Playgroud)
请建议,我该怎么做?
是否可以修改默认git提交消息的注释部分?我想为我的用户添加更多"上下文"信息.
# Please enter the commit message for your changes.
# (Comment lines starting with '#' will not be included)
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test.txt
#
Run Code Online (Sandbox Code Playgroud) python ×6
pdb ×2
argparse ×1
coroutine ×1
date ×1
deprecated ×1
git ×1
git-commit ×1
grayscale ×1
imagemagick ×1
linux ×1
nose ×1
printf ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
terminology ×1
testing ×1
unix ×1