Python是一种"以空格分隔"的语言.然而,使用分号被允许的.例如,以下工作但不赞成:
print("Hello!");
print("This is valid");
Run Code Online (Sandbox Code Playgroud)
我已经使用python好几年了,而且我用过分号的唯一一次是用python生成一次性命令行脚本:
python -c "import inspect, mymodule; print(inspect.getfile(mymodule))"
Run Code Online (Sandbox Code Playgroud)
或在SO的评论中添加代码(即"你应该尝试import os; print os.path.join(a,b)")
我在这个答案中也注意到一个类似的问题,分号也可以用来制作一个行if块,如
if x < y < z: print(x); print(y); print(z)
Run Code Online (Sandbox Code Playgroud)
这对我给出的两个用法示例(命令行脚本和注释)很方便.
以上示例用于以段落形式传递代码或制作简短的代码段,但不是我在生产代码库中所期望的.
这是我的问题:在python中,是否有理由在生产代码中使用分号?我想他们仅仅因为我引用的原因而被添加到语言中,但是Guido总是有可能考虑到更为宏伟的计划.没有意见; 我正在寻找分号有用的现有代码中的示例,或者来自python docs或Guido关于分号使用的某种语句.
从文档中的示例subprocess.run()来看,似乎不应该有任何输出
subprocess.run(["ls", "-l"]) # doesn't capture output
Run Code Online (Sandbox Code Playgroud)
但是,当我在python shell中尝试它时,列表被打印出来.我想知道这是否是默认行为以及如何抑制输出run().
我在Sublime Text中为自定义语言创建了一个.tmLanuage文件.一切都运作良好,除了我似乎无法自动评论工作.我似乎无法在Sublime Text文档或Google上找到有关如何执行此操作的任何内容,但也许这是因为我没有使用正确的关键字.
让我解释一下我的意思.假设我有以下C代码:
int i = 1;
i += 2;
Run Code Online (Sandbox Code Playgroud)
如果我在Sublime Text中突出显示并按下ctrl+/,则会更改为
// int i = 1;
// i += 2;
Run Code Online (Sandbox Code Playgroud)
同样,对于Python代码:
i = 1
i += 2
Run Code Online (Sandbox Code Playgroud)
会成为
# i = 1
# i += 2
Run Code Online (Sandbox Code Playgroud)
显然,Sublime Text必须知道语言语法才能选择正确的注释字符,这就是为什么我假设我需要在我的.tmLanguage文件中添加一些内容才能使其工作.我查看了Sublime Text附带的C.tmLanguage和Python.tmLanguage文件,没有任何内容跳出来作为执行此自动注释的代码.
我有什么要添加到我的.tmLanguage文件以在Sublime Text中启用此功能?或者,是否有一些其他文件我必须添加/修改才能启用此功能?
我有一个带有索引的pandas DataFrame,我想自然排序.Natsort似乎不起作用.在构建DataFrame之前对索引进行排序似乎没有帮助,因为我对DataFrame的操作似乎搞乱了进程中的排序.关于如何自然地采用指数的任何想法?
from natsort import natsorted
import pandas as pd
# An unsorted list of strings
a = ['0hr', '128hr', '72hr', '48hr', '96hr']
# Sorted incorrectly
b = sorted(a)
# Naturally Sorted
c = natsorted(a)
# Use a as the index for a DataFrame
df = pd.DataFrame(index=a)
# Sorted Incorrectly
df2 = df.sort()
# Natsort doesn't seem to work
df3 = natsorted(df)
print(a)
print(b)
print(c)
print(df.index)
print(df2.index)
print(df3.index)
Run Code Online (Sandbox Code Playgroud) 假设我具有Numpydoc样式中记录的以下函数,并且使用Sphinx autofunction指令自动生成文档:
def foo(x, y, _hidden_argument=None):
"""
Foo a bar.
Parameters
----------
x: str
The first argument to foo.
y: str
The second argument to foo.
Returns
-------
The barred foo.
"""
if _hidden_argument:
_end_users_shouldnt_call_this_function(x, y)
return x + y
Run Code Online (Sandbox Code Playgroud)
我不想将隐藏的参数作为我公共API的一部分进行宣传,但它会显示在我自动生成的文档中.有没有办法告诉Sphinx忽略一个函数的特定参数,或者(甚至更好)让它自动忽略带有前导下划线的参数?
我在企业环境中使用conan,其中操作系统相当旧并且具有旧版本glibc(2.11)。因此,许多预构建的二进制文件conan.io最终无法在我的环境中运行。然而,conan他不知道这一点,并且很乐意下载它们并将其安装在我的系统上,从而导致链接时错误。
我发现,如果我从源代码构建,我就可以让库正常工作。
我期望的行为如下:
conan install安装库(例如它不在我的缓存中)然后conan将从源代码构建并将其放置在我的缓存中,然后使用它。conan install,conan查找缓存的库并使用它,而无需从源重建。conan install作为自动构建脚本的一部分进行调用,因此我希望不必根据是否是第一次安装库来修改调用(但修改配置文件就可以了)。我在实践中很难获得这种行为。以下是我遇到的挑战:
conan install --build=thelibrarythen ,每次调用时conan都会从源代码重建该库,即使它已经存在于我的缓存中。conan install --build=thelibraryconan install --build=missing,那么我可以conan通过设置一些没有与之关联的预构建二进制文件的构建选项来欺骗构建库。
这是我正在寻找的内容(我假设存在但无法找到):
conanfile.txt我可以在我的(或其他一些配置文件)中放置一些设置,告诉conan我忽略给定库的预构建二进制文件,而是从源代码构建,但使用缓存版本(如果可用)。
这可能吗conan?
前几天我做了一个有趣的观察.我用不同的方式来获得一个对象,每个的速度"感实性"试验,我发现not是多少速度比bool.
>>> bool([5, 6, 7])
True
>>> bool([])
False
>>> not not [5, 6, 7]
True
>>> not not []
False
>>> import timeit
>>> from numpy import mean
>>> mean(timeit.repeat('bool(a)', 'a = [5, 6, 7]', repeat=10))
0.19072036743164061
>>> mean(timeit.repeat('bool(a)', 'a = []', repeat=10))
0.18562331199645996
>>> mean(timeit.repeat('not not a', 'a = [5, 6, 7]', repeat=10))
0.072056698799133304
>>> mean(timeit.repeat('not not a', 'a = []', repeat=10))
0.073475956916809082
>>> mean(timeit.repeat('not a', 'a = [5, 6, 7]', repeat=10)) …Run Code Online (Sandbox Code Playgroud) 我尝试在Ubuntu服务器16.04上的Django + Supervisor + NGINX中部署我的网站.
这是我的.conf(主管):
[program:sitepro]
command = /home/user/sitepro/bin/gunicorn sitepro.wsgi:application --bind mywebsite.fr:8002
user = user
autostart = true
autorestart = true
Run Code Online (Sandbox Code Playgroud)
我的NGINX配置文件:
server {
listen 80;
server_name .mywebsite.fr;
charset utf-8;
root /home/user/sitepro/site/sitepro;
access_log /home/user/sitepro/site/logs/nginx/access.log;
error_log /home/user/sitepro/site/logs/nginx/error.log;
location /static {
alias /home/user/sitepro/site/static;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8002;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在项目的根目录上启动gunicorn时,一切顺利:
(sitepro) user@mybps:~/sitepro/site$ gunicorn sitepro.wsgi:application --bind mywebsite.fr:8002
[2017-11-01 16:09:37 +0000] [1920] [INFO] Starting gunicorn 19.7.1
[2017-11-01 16:09:37 +0000] [1920] [INFO] Listening at: …Run Code Online (Sandbox Code Playgroud) It seems that the Python C API is not consistent with the const correctness of character arrays. For example, PyImport_ImportFrozenModule accepts a char*, whereas PyImport_ImportModule accepts a const char*.
The implication of all this is that in my C++ application that I am writing with an embedded Python interpreter, I sometimes have to cast the string literal that I pass to a Python API call as just a char* (as opposed to const char*), and sometimes I …
使用locale具有unicode输入的库时,我遇到了奇怪的行为.以下是最低工作示例:
>>> x = '\U0010fefd'
>>> ord(x)
1113853
>>> ord('\U0010fefd') == 0X10fefd
True
>>> ord(x) <= 0X10ffff
True
>>> import locale
>>> locale.strxfrm(x)
'\U0010fefd'
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.strxfrm(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: character U+110000 is not in range [U+0000; U+10ffff]
Run Code Online (Sandbox Code Playgroud)
我在Python 3.3,3.4和3.5上看过这个.我在Python 2.7上没有出错.
据我所知,我的unicode输入在适当的unicode范围内,因此strxfrm在使用'en_US.UTF-8'时,某些内部的东西似乎正在将输入移出范围.
我正在运行Mac OS X,这种行为可能与http://bugs.python.org/issue23195有关......但我认为这个bug只会表现为不正确的结果,而不是引发异常.我无法在我的SLES 11机器上复制,其他人确认它们无法在Ubuntu,Centos或Windows上复制.在评论中听到其他操作系统可能是有益的.
有人可以解释一下这里可能发生的事情吗?
python ×8
c++ ×2
python-3.x ×2
conan ×1
django ×1
locale ×1
natsort ×1
nginx ×1
pandas ×1
python-2.7 ×1
python-c-api ×1
sorting ×1
sublimetext ×1
sublimetext2 ×1
sublimetext3 ×1
subprocess ×1
unicode ×1