473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Generators can only yield ints?

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)

>>l = letters()
l.next()
Traceback (most recent call last):
File "<pyshell#225>", line 1, in <module>
l.next()
File "<pyshell#223>", line 5, in letters
yield chr(a)
TypeError: an integer is required
>>>

Any way to get around this?
Aug 22 '08 #1
5 1121
defn noob:
Any way to get around this?
Your code is wrong, this is one of the correct versions:

from itertools import izip

def letters():
lower = xrange(ord('a'), ord('z')+1)
upper = xrange(ord('A'), ord('Z')+1)
for lc, uc in izip(lower, upper):
yield chr(lc)
yield chr(uc)

print list(letters())

There are other ways to do the same thing.

Bye,
bearophile
Aug 22 '08 #2
On Fri, Aug 22, 2008 at 6:44 PM, defn noob <ci**********@yahoo.sewrote:
def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)
TypeError: an integer is required
No. The problem is that "chr function" receives "int"
"a" and "B" are generators of "int" items.

What exactly you intent with that code?

Maybe:
#<code>
def letters():
for i in xrange(ord('a'), ord('z')+1):
yield chr(i)
for i in xrange(ord('A'), ord('Z')+1):
yield chr(i)

for letter in letters():
print letter
#</code>

Regards
Aug 22 '08 #3
On Fri, 22 Aug 2008 15:44:15 -0700 (PDT), defn noob wrote:
def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)

>>>l = letters()
l.next()

Traceback (most recent call last):
File "<pyshell#225>", line 1, in <module>
l.next()
File "<pyshell#223>", line 5, in letters
yield chr(a)
TypeError: an integer is required
>>>>
The error you're seeing is a result of passing
non-integer to chr() function, it has nothing to do
with generators:
>>chr([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>>
I have simplified your code a bit:
-------------
import string
def letters():
a, B = (string.letters,) * 2
for i in zip(a, B):
yield i[0]
yield i[1]

l = letters()

print l.next()
print l.next()
print l.next()
--------------

and the output:

$ python genlet.py
a
a
b
$

Is that what you tried to achieve?

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Aug 22 '08 #4
On Fri, 22 Aug 2008 15:44:15 -0700, defn noob wrote:
def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)

>>>l = letters()
l.next()

Traceback (most recent call last):
File "<pyshell#225>", line 1, in <module>
l.next()
File "<pyshell#223>", line 5, in letters
yield chr(a)
TypeError: an integer is required
>>>>

Any way to get around this?
Yes, write code that isn't buggy :)

Generators can return anything you want. Your problem is that you're
passing an xrange object to chr() instead of an int. Try this:

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
for t in zip(a, B):
yield chr(t[0])
yield chr(t[1])

But (arguably) a better way is:

def letters():
from string import ascii_letters as letters
for a,b in zip(letters[0:26], letters[26:]):
yield a
yield b
Note that it is important to use ascii_letters rather than letters,
because in some locales the number of uppercase and lowercase letters
differ.
--
Steven
Aug 23 '08 #5
Lie
On Aug 23, 5:44*am, defn noob <circularf...@yahoo.sewrote:
def letters():
* * * * a = xrange(ord('a'), ord('z')+1)
* * * * B = xrange(ord('A'), ord('Z')+1)
* * * * while True:
* * * * * * * * yield chr(a)
* * * * * * * * yield chr(B)
>l = letters()
l.next()

Traceback (most recent call last):
* File "<pyshell#225>", line 1, in <module>
* * l.next()
* File "<pyshell#223>", line 5, in letters
* * yield chr(a)
TypeError: an integer is required

Any way to get around this?
The most direct translation on what you've done, with corrections, is
either this:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
while True:
yield a
yield B
>>l = letters()
l.next()
xrange(97, 123)

or this:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
while True:
yield [chr(char) for char in a]
yield [chr(char) for char in B]
>>l = letters()
l.next()
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

but my psychic ability guessed that you actually wanted this instead:
def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
a = [chr(char) for char in a]
B = [chr(char) for char in B]
index = 0
while index < 26:
yield a[index]
yield B[index]
index += 1
>>l = letters()
l.next()
'a'

or possibly in a more pythonic style, using for:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
for lower, upper in zip(a, B):
yield chr(lower)
yield chr(upper)
>>l = letters()
l.next()
'a'

paralelly, my psychic ability also tells me that you might prefer this
instead:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
for lower, upper in zip(a, B):
yield chr(lower), chr(upper)
>>l = letters()
l.next()
('a', 'A')
Aug 23 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
8
by: Timothy Fitz | last post by:
It seems to me that in python, generators are not truly coroutines. I do not understand why. What I see is that generators are used almost exclusively for generation of lists just-in-time. Side...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
7
by: Ben Sizer | last post by:
A simple question - can anybody give a short example of how these work and what they are good for? I've read PEP 342 and the associated bit in the What's New section and it's still all Greek to me....
13
by: Martin Sand Christensen | last post by:
Hi! First a bit of context. Yesterday I spent a lot of time debugging the following method in a rather slim database abstraction layer we've developed: ,---- | def selectColumn(self,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.