有几种方法可以写入stderr:
# Note: this first one does not work in Python 3
print >> sys.stderr, "spam"
sys.stderr.write("spam\n")
os.write(2, b"spam\n")
from __future__ import print_function
print("spam", file=sys.stderr)
Run Code Online (Sandbox Code Playgroud)
这似乎与Python#13 †的禅宗相矛盾,那么这里有什么区别,这种方式有哪些优点或缺点?应该使用哪种方式?
† 应该有一个 - 最好只有一个 - 显而易见的方法.
在问这个问题时,我意识到我对原始字符串知之甚少.对于那些自称是Django训练师的人来说,这很糟糕.
我知道编码是什么,而且我知道u''自从我得到什么是Unicode以来我们独自做了什么.
但到底r''做了什么呢?它会产生什么样的字符串?
And above all, what the heck does ur'' do?
Finally, is there any reliable way to go back from a Unicode string to a simple raw string?
Ah, and by the way, if your system and your text editor charset are set to UTF-8, does u'' actually do anything?
我正在寻找一种优雅的方法,使用一些嵌套的dicts和列表(即javascript样式的对象语法)在dict上使用属性访问来获取数据.
例如:
>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
Run Code Online (Sandbox Code Playgroud)
应该可以通过这种方式访问:
>>> x = dict2obj(d)
>>> x.a
1
>>> x.b.c
2
>>> x.d[1].foo
bar
Run Code Online (Sandbox Code Playgroud)
我认为,如果没有递归,这是不可能的,但是什么是获得dicts的对象样式的好方法?
鉴于可以使用pip和virtualenv安装的Python(2.X)包的名称,有没有办法找到pip可以安装的所有可能版本的列表?现在它是试验和错误.
我正在尝试为第三方库安装一个版本,但最新版本太新了,进行了向后不兼容的更改.所以我想以某种方式列出pip知道的所有版本,以便我可以测试它们.
我一直在做一个项目,但不幸的是,我忘了切换到我的分支,因此一直在做主人
如何将我在这里完成的工作(3个文件)从master转移到我的分支(称为例如branch123)而不需要掌握?
如何获取foo从线程目标返回的值?
from threading import Thread
def foo(bar):
print('hello {}'.format(bar))
return 'foo'
thread = Thread(target=foo, args=('world!',))
thread.start()
return_value = thread.join()
Run Code Online (Sandbox Code Playgroud)
如上所示,"一种显而易见的方法"不起作用:'foo'返回'foo'.
在控制台应用程序中,可以使用ascii微调器,如GUI等待光标,以指示正在完成工作.一个普通的微调器循环遍历这4个字符:'|','/',' - ','\'
什么是其他一些循环动画序列来为控制台应用程序增添趣味?
user-interface console-application throbber ascii-art progress-indicator
Python中无穷大的哈希值具有与pi匹配的数字:
>>> inf = float('inf')
>>> hash(inf)
314159
>>> int(math.pi*1e5)
314159
Run Code Online (Sandbox Code Playgroud)
这仅仅是巧合还是故意的?
在我的Django应用程序中,我需要从referrer中获取主机名request.META.get('HTTP_REFERER')及其协议,以便从以下URL中获取:
我应该得到:
我查看了其他相关的问题,发现了关于urlparse,但是从那以后就没办法了
>>> urlparse(request.META.get('HTTP_REFERER')).hostname
'docs.google.com'
Run Code Online (Sandbox Code Playgroud) 执行时pip install -r requirements.txt,我在安装阶段遇到以下错误matplotlib:
REQUIRED DEPENDENCIES AND EXTENSIONS
numpy: yes [not found. pip may install it below.]
dateutil: yes [dateutil was not found. It is required for date
axis support. pip/easy_install may attempt to
install it after matplotlib.]
tornado: yes [tornado was not found. It is required for the
WebAgg backend. pip/easy_install may attempt to
install it after matplotlib.]
pyparsing: yes [pyparsing was not found. It is required for
mathtext support. pip/easy_install may attempt to
install …Run Code Online (Sandbox Code Playgroud) python ×8
pip ×2
ascii-art ×1
dictionary ×1
django ×1
git ×1
hash ×1
math ×1
matplotlib ×1
object ×1
pi ×1
printing ×1
python-2.7 ×1
python-2.x ×1
rawstring ×1
stderr ×1
throbber ×1
ubuntu ×1
unicode ×1