Python, regex and html: match final tag on line

I'm confused about python greedy/not-greedy characters. "Given multi-line html, return the final tag on each line." I would think this would be correct: re.findall('<.*?>$', html, re.MULTILINE) I'm irked because I expected a list of single tags like: "</html>", "<ul>", "</td>". My O'Reilly's Pocket Reference says that *? wil "match 0

Python,正则表达式和html:匹配最后一个标签

我对python贪婪/不贪婪的字符感到困惑。 “给定多行html,返回每行的最终标签。” 我会认为这是正确的: re.findall('<.*?>$', html, re.MULTILINE) 我很厌烦,因为我期望的单个标签列表如下: "</html>", "<ul>", "</td>". 我的O'Reilly的袖珍参考文献说*? 将“匹配0次或更多次,但尽可能少”。 那么为什么我会得到'贪婪的'匹配,即在一些(但不是全部)匹配中有多个标签? 你的

Iterating over a Django QuerySet while deleting objects in the same QuerySet

I am wondering what is the best way to iterate over a Django QuerySet while deleting objects within the Queryset? For example, say you have a log table with entries at specific times, and you wanted to archive them so that there is no more than 1 entry every 5 minutes. I know this may be wrong, but this is kind of what I am going for: toarchive = Log.objects.all().order_by("-date") start = toa

在删除同一个QuerySet中的对象时迭代Django QuerySet

我想知道什么是在Queryset中删除对象时迭代Django QuerySet的最佳方式? 例如,假设您在特定时间有一个包含条目的日志表,并且您想对其进行存档,以便每5分钟不超过1个条目。 我知道这可能是错误的,但这是我所要做的: toarchive = Log.objects.all().order_by("-date") start = toarchive[0].date interval = start - datetime.timedelta(minutes=5) for entry in toarchive[1:]: if entry.date > interval:

Python string interpolation implementation

[EDIT 00]: I've edited several times the post and now even the title, please read below. I just learned about the format string method, and its use with dictionaries, like the ones provided by vars() , locals() and globals() , example: name = 'Ismael' print 'My name is {name}.'.format(**vars()) But I want to do: name = 'Ismael' print 'My name is {name}.' # Similar to ruby So I came up w

Python字符串插值实现

[编辑00]:我已经编辑了好几次帖子,现在甚至是标题,请在下面阅读。 我刚刚了解了格式字符串方法,以及它与字典的用法,比如由vars() , locals()和globals() ,例如: name = 'Ismael' print 'My name is {name}.'.format(**vars()) 但我想这样做: name = 'Ismael' print 'My name is {name}.' # Similar to ruby 所以我想出了这个: def mprint(string='', dictionary=globals()): print string.format(**dictiona

Randomizing integer behavior

After seeing this question, I started wondering: is it possible to write a class that behaves like a random integer? I managed to find some overridable methods with dir() : class RandomInt(int): def __add__(self, other): return randint(1, 100) + other def __mul__(self, other): return randint(1, 100) * other def __div__(self, other): return randint(1, 100)

随机化整数行为

看到这个问题后,我开始怀疑:是否可以编写一个类似随机整数的类? 我设法用dir()找到一些可覆盖的方法: class RandomInt(int): def __add__(self, other): return randint(1, 100) + other def __mul__(self, other): return randint(1, 100) * other def __div__(self, other): return randint(1, 100) / other def __sub__(self, other): return randint(1, 100) - ot

Sphinx's .. include:: directive and "duplicate label" warnings

I'm trying to use Sphinx's .. include:: directive to include docs from one file in another file, to avoid duplicating the source text of the docs. The section I'm including is in configuration.rst (it's part of the reference docs for the config settings) and it contains some labels for cross-referencing each config setting: .. start_config-authorization .. _ckan.auth.anon_creat

Sphinx的include ::指令和“重复标签”警告

我试图使用Sphinx的.. include::指令将文档从一个文件包含在另一个文件中,以避免重复文档的源文本。 我包含的部分在configuration.rst (它是配置设置的参考文档的一部分),它包含一些用于交叉引用每个配置设置的标签: .. start_config-authorization .. _ckan.auth.anon_create_dataset: ckan.auth.anon_create_dataset ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example:: ckan.auth.anon_create_dataset = False Default valu

Hashing same character multiple times

I'm doing a programming challenge and I'm going crazy with one of the challenges. In the challenge, I need to compute the MD5 of a string. The string is given in the following form: n[c] : Where n is a number and c is a character. For example: b3[a2[c]] => baccaccacc Everything went ok until I was given the following string: 1[2[3[4[5[6[7[8[9[10[11[12[13[a]]]]]]]]]]]]] This

散列相同的字符多次

我正在做一个编程挑战,我要面对其中一项挑战而疯狂。 在挑战中,我需要计算一个字符串的MD5。 该字符串以下面的形式给出: n[c] :其中n是一个数字, c是一个字符。 例如: b3[a2[c]] => baccaccacc 一切都很顺利,直到我得到以下字符串: 1[2[3[4[5[6[7[8[9[10[11[12[13[a]]]]]]]]]]]]] 这个字符串变成了一个6227020800 a的字符串。 这个字符串大于6GB,因此在实际时间内计算几乎是不可能的。 所以,这是我的

Python join: why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" Than this: my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" Is there a specific reason it is like this? It's because any iterable can be joined, not just lists, but the result and the "joiner" are always str

Python连接:为什么它是string.join(list)而不是list.join(string)?

这一直困扰着我。 看起来这样会更好: my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" 比这个: my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" 有没有特定的原因是这样的? 这是因为任何迭代器都可以被连接,而不仅仅是列表,但结果和“连接器”总是字符串。 例如: import urllib2 print 'n############n'.join( urllib2.urlopen('http://data.

python run test with app installed from pip

For some of the python apps, if I install them manually, I can run python setup.py test inside the app folder to carry out test scripts. But if I install them through pip, there's only an .egg file in dist-packages, so how should I run their test? I did the following in a virtualenv and was able to run the tests. $ pip install pyelasticsearch $ pip install nose virtualenv scripttest mock

从pip安装的应用程序运行python测试

对于一些python应用程序,如果我手动安装它们,我可以在app文件夹内运行python setup.py test来执行测试脚本。 但是,如果我通过pip安装它们,dist-packages中只有一个.egg文件,那么我应该如何运行它们的测试? 我在virtualenv中做了以下操作,并能够运行测试。 $ pip install pyelasticsearch $ pip install nose virtualenv scripttest mock $ cdsitepackages #virtualenvwrapper shortcut to go to the site-packages dir

ABSOLUTE with IF statement ? Python 2.5

I have been using 'dis' module in order to re-write some compiled script (.pyc). I understand the difference between JUMP_FORWARD and JUMP_ABSOLUTE. To my knowledge an IF statement will be closed by a JUMP_FORWARD: >>> def f(): if a: print '' >>> from dis import dis >>> dis(f) 2 0 LOAD_GLOBAL 0 (a)

使用IF语句的ABSOLUTE? Python 2.5

我一直在使用'dis'模块来重新编写一些编译脚本(.pyc)。 我了解JUMP_FORWARD和JUMP_ABSOLUTE之间的区别。 据我所知,IF声明将由JUMP_FORWARD关闭: >>> def f(): if a: print '' >>> from dis import dis >>> dis(f) 2 0 LOAD_GLOBAL 0 (a) 3 JUMP_IF_FALSE 9 (to 15) 6 POP_TOP

Does 'finally' always execute in Python?

For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed? For example, let's say I return while in an except block: try: 1/0 except ZeroDivisionError: return finally: print("Does this code run?") Or maybe I re-raise an Exception : try: 1/0 except ZeroDivisionError: raise finally: print("What about this code?")

“终于”总是用Python执行吗?

对于Python中任何可能的try-finally块,它是否可以保证finally块总是会被执行? 例如,假设我在一个except块中返回: try: 1/0 except ZeroDivisionError: return finally: print("Does this code run?") 或者,也许我重新提出一个Exception : try: 1/0 except ZeroDivisionError: raise finally: print("What about this code?") 个人测试表明, finally会为上述示例执行,但我想象还有其他场景