Team. Coco
Team. Coco

Reputation: 87

Why is my generator implementation incorrect in Python?

So basically i am writing a program where i need to take in a file that contains uppercase letters, that may or may not be seperated by white space or new lines, and return a generator over the letters in the file.

For example:

Z FM
TM  CC

Would return a generator with an output of "Z","F","M","T","M","C","C"

The function should be a generator, so it will only load in letters at a time instead of the entire file.

Anyways, here is what i have so far. ANyone tell me where i went wrong?

def buildGen:
    with open(filename) as sample:
        for word in sample:
           for ch in word:
               yield ch

Upvotes: 1

Views: 91

Answers (4)

acw1668
acw1668

Reputation: 46678

Assume only uppercase letters will be included in the output and only load in letters at a time instead of the entire file:

def buildGen():
    with open(filename) as sample:
        while True:
            ch = sample.read(1)
            if ch:
                if 'A' <= ch <= 'Z':
                    yield ch
            else:
                break

Upvotes: 0

armak
armak

Reputation: 560

your logic here is wrong, as according to my understanding you are trying to read the file and yield all the letters in the file by calling your buildGen function.

As you are using a with statement it will close the file each time and you always end up with the first line only. Another point is you are opening the file as sample and using as dna that itself will throw an error.

you can achieve the same using this

fp = open('sample.txt')
# generator for the data
data_generator = (c for c in fp.read())
for char in data_generator:
    print char


def read_chars(fp):
    for line in fp

Upvotes: -1

Ami Tavory
Ami Tavory

Reputation: 76297

There are a number of problems with your code:

  1. You're defining a function with the wrong syntax (def buildGen: <- Parenthesis should follow).

  2. Your description of the output implies that you need to address whitespace characters.

The following outputs the output in your example (assuming your file is stuff.txt):

# Note `buildGen` takes now `filename`.
def buildGen(filename): 
    with open(filename) as sample:
        for word in sample:
           for ch in word:
               # Only yield non-whitespace.
               if ch.strip():
                   yield ch

print list(buildGen('stuff.txt'))

Upvotes: 2

Aleksandr Karasev
Aleksandr Karasev

Reputation: 437

You are using wrong indentation and a wrong variable name dna. You need write something like this:

def buildGen:
    with open(filename) as sample:
        for line in sample.read().splitlines():
            for ch in line.split():
                yield ch

Upvotes: 0

Related Questions