How yield catches StopIteration exception?

Why in the example function terminates: def func(iterable): while True: val = next(iterable) yield val but if I take off yield statement function will raise StopIteration exception? EDIT: Sorry for misleading you guys. I know what generators are and how to use them. Of course when I said function terminates I didn't mean eager evaluation of function. I just implied

yield如何捕获StopIteration异常?

为什么在示例函数中终止: def func(iterable): while True: val = next(iterable) yield val 但是如果我脱掉yield语句函数会引发StopIteration异常? 编辑:抱歉误导你们。 我知道发生器是什么以及如何使用它们。 当然,当我说功能终止时,我并不意味着对功能的渴望评估。 我只是暗示,当我使用函数来产生发电机时: gen = func(iterable) 在func的情况下,它工作并返回相同的生成器,但在func2的

Difference between Python's Generators and Iterators

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful. iterator is a more general concept: any object whose class has a next method ( __next__ in Python 3) and an __iter__ method that does return self . Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield e

Python的生成器和迭代器之间的区别

迭代器和生成器之间有什么区别? 当你使用每个案例时的一些例子会很有帮助。 iterator是更一般的概念:任何对象的类具有next方法( __next__在Python 3)和__iter__ ,做方法return self 。 每个生成器都是一个迭代器,但反之亦然。 通过调用具有一个或多个yield表达式(在Python 2.5及更早版本中为yield语句)的函数来构建生成器,并且该对象符合上一个iterator的定义。 当你需要一个具有复杂的状态维护行为的类,或者

How to loop through all but the last item of a list?

I would like to loop through a list checking each item against the one following it. Is there a way I can loop through all but the last item using for x in y? I would prefer to do it without using indexes if I can. Note freespace answered my actual question, which is why I accepted the answer, but SilentGhost answered the question I should have asked. Apologies for the confusion. for x

如何遍历列表中的最后一项而不是所有项目?

我想通过一个列表来检查每个项目和它后面的项目。 有没有一种方法可以循环遍历所有内容,但是最后一项使用for x in y? 如果可以的话,我宁愿不使用索引。 注意 freespace回答了我的实际问题,这就是为什么我接受了答案,但SilentGhost回答了我应该问的问题。 抱歉的混淆。 for x in y[:-1] 如果y是一个生成器,那么以上将不起作用。 将序列项目与以下内容进行比较的最简单方法: for i, j in zip(a, a[1:]): # co

python irc bot ping answer

I'm trying to write a simple IRC bot in Python. My bot would connect to a server, join a channel and then print to the console the messages received by the bot. I did it, but the server always disconnects me after a while because of ping timeout. I tried to use some libraries (python-irc and oyoyo) and they both ALLEGEDLY handle automatically the ping-pong messages with the server. But I

python irc bot ping的答案

我正在尝试用Python编写一个简单的IRC bot。 我的机器人将连接到服务器,加入一个频道,然后将机器人收到的消息打印到控制台。 我做到了,但由于ping超时,服务器在一段时间后总是断开连接。 我试图使用一些库(python-irc和oyoyo),它们都会自动处理与服务器的乒乓消息。 但我尝试过使用irc.abjects.net和irc.criten.net,并且这两个库都失败了。 然后我根据这个脚本创建了一个脚本:http://hawkee.com/snippet/9725/。

"NOTICE AUTH" notifications when connecting to IRC server

As a learning exercise, I'm writing a Python program to connect to a channel on an IRC network, so I can output messages in the channel to stdout. I'm using asynchat and manually sending the protocol messages, rather than using something like Twisted or existing bot code from the net - again, it's a more useful learning experience that way. I can send JOIN and USER commands quite h

连接到IRC服务器时发出“NOTICE AUTH”通知

作为一种学习练习,我正在编写一个Python程序来连接到IRC网络上的通道,因此我可以将通道中的消息输出到标准输出。 我使用asynchat并手动发送协议消息,而不是使用类似网络中的扭曲或现有的bot代码 - 再次,这是一种更有用的学习体验。 我可以非常高兴地发送JOIN和USER命令,并且可以根据需要PING / PONG。 但是,我注意到当打开一个套接字到6667端口时,我会收到一些消息: NOTICE AUTH :*** Looking up your hostname... N

server password on login

I'm trying to create a simple IRC bot in Python using Twisted. I've come pretty far but have encountered a problem when I need to provide the password to enter the server. I know how to implement a password for joining a specific channel but I have no clue how to provide a username and password upon joining the IRC server itself. The reason I need to provide a password is that I want

登录时的服务器密码

我正在尝试使用Twisted在Python中创建一个简单的IRC bot。 我来得相当远,但当我需要提供输入服务器的密码时遇到了问题。 我知道如何实现加入特定频道的密码,但我不知道如何在加入IRC服务器时提供用户名和密码。 我需要提供密码的原因是我希望机器人能够保持跳动(ZNC)。 (不好意思的压痕) 这是我到目前为止尝试过的 import re import urllib2 import random import time import sys from HTMLParser import HTMLPa

Check if user is 'voiced' or 'op' in IRC channel using twisted

I'm writing an IRC bot using twisted python, and some actions should only be available to channel operators. How do I determine the 'user level' of a user in a channel using twisteds IRCClient? One way to discover if a user has voice or op is to look at their name in a names listing. See How to use Twisted to get an IRC channel's user list for details on how to do that. Other

使用双绞线检查用户是否在IRC频道中“浊音”或“操作”

我正在使用扭曲的Python编写一个IRC bot,并且一些操作应该仅供渠道操作员使用。 如何使用扭曲的IRCClient来确定用户在通道中的“用户级别”? 一种发现用户是否有语音或操作的方式是在名单中查看他们的名字。 请参阅如何使用Twisted获取IRC频道的用户列表以获取有关如何执行此操作的详细信息。 使用WHO或WHOIS的其他解决方案也会采取类似的形式。 如果您的机器人有权这样做,您还可以使用频道访问列表。

Python/Twisted IRC bot logging problem

Hey. I only recently started using Python, and my friend suggested using Twisted as a means to create this IRC bot, as it will be a lot simpler. Here is the code I have so far (which is heavily based off logbot.py haha) from twisted.internet import reactor, protocol from twisted.words.protocols import irc from twisted.python import log import sys, time class MessageLogger: def __init__(se

Python / Twisted IRC bot日志记录问题

嘿。 我最近才开始使用Python,而我的朋友建议使用Twisted作为创建此IRC bot的手段,因为它会简单得多。 这是我到目前为止的代码(这是基于logbot.py哈哈) from twisted.internet import reactor, protocol from twisted.words.protocols import irc from twisted.python import log import sys, time class MessageLogger: def __init__(self, file): self.file = file def log(self, message): t

'METHODNAME' as Client method versus irc

Looking at twisted.words.protocols.irc.IRCClient, it seems to me like there are some strangely redundant methods. For instance, there is a method 'privmsg' but also a method 'irc_PRIVMSG' As another example consider 'join' and 'irc_JOIN' What I want to know is why the redundancy, those are just two examples of many. Are the two different types used in differe

'METHODNAME'作为客户端方法与irc

查看twisted.words.protocols.irc.IRCClient,在我看来,似乎有一些奇怪的冗余方法。 例如,有一种方法'privmsg',但也有一种方法'irc_PRIVMSG' 作为另一个例子,考虑'join'和'irc_JOIN' 我想知道的是为什么冗余,这些只是许多的两个例子。 两种不同的类型在不同的环境中使用吗? 我们应该使用一种类型而不是另一种? 您正处在正确的轨道上,关于在不同情况下使用的两种不同类型的方法

What's the best Django search app?

I'm building a Django project that needs search functionality, and until there's a django.contrib.search , I have to choose a search app. So, which is the best? By "best" I mean... easy to install / set up has a Django- or at least Python-friendly API can perform reasonably complex searches Here are some apps I've heard of, please suggest others if you know of any:

什么是最好的Django搜索应用程序?

我正在构建一个需要搜索功能的Django项目,直到有一个django.contrib.search ,我必须选择一个搜索应用程序。 那么,哪个最好? 我所说的“最好”是...... 易于安装/设置 有一个Django或至少Python友好的API 可以执行相当复杂的搜索 以下是我听说过的一些应用程序,如果您知道任何其他应用程序,请告诉其他人: djangosearch Django的狮身人面像 我还想避免使用第三方搜索引擎(如Google SiteSearch),因为我想索