I'm mainly a C# developer, but I'm currently working on a project in Python. How can I represent the equivalent of an Enum in Python? Enums have been added to Python 3.4 as described in PEP 435. It has also been backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4 on pypi. For more advanced Enum techniques try the aenum library (2.7, 3.3+, same author as enum34 . Code is not perfectl
我主要是一名C#开发人员,但我目前正在使用Python开发一个项目。 我如何表示Python中Enum的等价物? 如PEP 435中所述,Enum已被添加到Python 3.4中。它也被反向移植到pypi上的3.3,3.2,3.1,2.7,2.6,2.5和2.4。 对于更高级的枚举技术尝试aenum库(2.7,3.3+,同一作者的enum34 ,代码不PY2和PY3之间的完美兼容,比如你需要__order__在Python 2)。 要使用enum34 ,请执行$ pip install enum34 要使用aenum ,请执行$ pi
I've been hearing a lot about the PyPy project. They claim it is 6.3 times faster than the CPython interpreter on their site. Whenever we talk about dynamic languages like Python, speed is one of the top issues. To solve this, they say PyPy is 6.3 times faster. The second issue is parallelism, the infamous Global Interpreter Lock (GIL). For this, PyPy says it can give GIL-less Python.
我一直听到很多关于PyPy项目的消息。 他们声称它比他们网站上的CPython解释器快6.3倍。 每当我们谈论像Python这样的动态语言时,速度就是最重要的问题之一。 为了解决这个问题,他们说PyPy的速度提高了6.3倍。 第二个问题是并行,即臭名昭着的全球口译员锁(GIL)。 为此,PyPy说它可以给GIL少的Python。 如果PyPy能够解决这些重大挑战,那么它的弱点在哪些方面会阻止更广泛的采用? 也就是说,什么阻止像我这样的典型
Is there a portable way to get the current user's username in Python (ie, one that works under both Linux and Windows, at least). It would work like os.getuid : >>> os.getuid() 42 >>> os.getusername() 'slartibartfast' I googled around and was surprised not to find a definitive answer (although perhaps I was just googling poorly). The pwd module provides a relatively easy
是否有一种可移植的方式来获取当前用户的用户名(例如,至少在Linux和Windows下都可以使用)。 它会像os.getuid一样os.getuid : >>> os.getuid() 42 >>> os.getusername() 'slartibartfast' 我搜索了一下,很惊讶没有找到一个明确的答案(虽然也许我只是谷歌搜索很差)。 pwd模块提供了一种相对简单的方式来实现这个目标,比如Linux,但它不在Windows上。 某些搜索结果表明,在某些情况下(例如,作为Wi
Is it possible to have static class variables or methods in python? What syntax is required to do this? Variables declared inside the class definition, but not inside a method are class or static variables: >>> class MyClass: ... i = 3 ... >>> MyClass.i 3 As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i var
是否有可能在Python中有静态类变量或方法? 需要什么语法才能做到这一点? 在类定义中声明的变量,但不在方法内的变量是类或静态变量: >>> class MyClass: ... i = 3 ... >>> MyClass.i 3 正如@millerdev指出的那样,这会创建一个类级别的i变量,但这与任何实例级别的i变量不同,因此您可以 >>> m = MyClass() >>> m.i = 4 >>> MyClass.i, m.i >>> (3, 4)
The question is in the title. I'd like to do it in python. What I'd like to do in this example in c: #include <stdio.h> int main() { int i; for (i=0; i<10; i++) printf("."); return 0; } Output: .......... In Python: >>> for i in xrange(0,10): print '.' . . . . . . . . . . >>> for i in xrange(0,10): print '.', . . . . . . . . . . In Python p
问题在于标题。 我想用python来做。 我在这个例子中想要做的是在c: #include <stdio.h> int main() { int i; for (i=0; i<10; i++) printf("."); return 0; } 输出: .......... 在Python中: >>> for i in xrange(0,10): print '.' . . . . . . . . . . >>> for i in xrange(0,10): print '.', . . . . . . . . . . 在Python print会添加一个n或一个空格,我该如何避免这种
I know how to run a command using cmd = subprocess.Popen and then subprocess.communicate. Most of the time I use a string tokenized with shlex.split as 'argv' argument for Popen. Example with "ls -l": import subprocess import shlex print subprocess.Popen(shlex.split(r'ls -l'), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0] Howev
我知道如何使用cmd = subprocess.Popen然后执行subprocess.communicate来运行命令。 大多数情况下,我使用shlex.split标记的字符串作为Popen的'argv'参数。 “ls -l”的例子: import subprocess import shlex print subprocess.Popen(shlex.split(r'ls -l'), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0] 但是,管道似乎不起作用...例如,以下示例返回注释: imp
我如何引用Python中的空对象? In Python, the 'null' object is the singleton None . The best way to check things for "Noneness" is to use the identity operator, is : if foo is None: ... It's not called null as in other languages, but None . There is always only one instance of this object, so you can check for equivalence with x is None (identity comparison) instead o
我如何引用Python中的空对象? 在Python中,'null'对象是单例None 。 检查东西“Noneness”最好的办法是使用运营商的身份, is : if foo is None: ... 它不叫空在其他语言,但None 。 总是只有一个这个对象的实例,所以你可以检查与x is None等价性x is None (身份比较)而不是x == None ,如果你愿意的话。 在Python中,为了表示缺少值,可以对对象使用None值(types.NoneType.None),对于字符串可以使用“”
There are way too many overlapping timezones in pytz's common list. Has anyone pared this down? For example there are 5 or 6 duplicate Canadian timezones and 15 or so duplicate US time zones. China is 1 timezone no DST, but has 5 entries.. By duplicate I mean GMT offset and DST are exactly the same. Getting rid of the timezones in pytz.country_timezones['us'] gets rid of the dupl
Pytz的共同列表中有太多重叠的时区。 有没有人削减了这个? 例如,有5或6个加拿大时区和15个美国时区。 中国是一个时区没有DST,但有5个条目。重复的我的意思是GMT偏移和DST完全相同。 摆脱pytz.country_timezones ['us']中的时区可以消除重复的美国时区,因为它只包含America / *区域,而不包含基本的US / *区域。 这对其他国家不起作用。 我不认为这是个好主意。 在澳大利亚,墨尔本和悉尼通常共享相同的时区
Are there any tools to do a URL compare in Python? For example, if I have http://google.com and google.com/ I'd like to know that they are likely to be the same site. If I were to construct a rule manually, I might Uppercase it, then strip off the http:// portion, and drop anything after the last alpha-numeric character.. But I can see failures of this, as I'm sure you can as well.
有什么工具可以在Python中进行URL比较吗? 例如,如果我有http://google.com和google.com/我想知道它们可能是同一个网站。 如果我要手动创建一个规则,我可能会将它大写,然后去掉http://部分,并在最后一个字母数字字符后面放下任何东西。但是我可以看到这个失败,因为我确定你也可以。 有没有这样的图书馆? 你会怎么做? 这是我的头顶: def canonical_url(u): u = u.lower() if u.startswith("http://"):
This is my first questions here, so I hope it will be done correctly ;) I've been assigned the task to give a web interface to some "home made" python script. This script is used to check some web sites/applications availability, via curl commands. A very important aspect of this script is that it gives its results in real-time, writing line by line to the standard output. By
这是我的第一个问题,所以我希望它能正确完成;) 我已经被分配了任务来给一些“自制的”python脚本提供一个web界面。 该脚本用于通过curl命令检查一些网站/应用程序的可用性。 这个脚本的一个非常重要的方面是它实时给出结果,逐行写入标准输出。 通过为该脚本提供Web界面,主要目标是可以从任何地方轻松使用该脚本,例如通过智能手机。 所以Web界面必须非常基本,并且“无插件”工作。 我的问题是,我认为或发现在网络上的