Last value of yield statement

S

Shriphani

Hello all,

Let us say I have a function like this:

def efficientFiller(file):
worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list



I want to plainly get the last value the yield statement generates.
How can I go about doing this please?

Regards,
Shriphani Palakodety
 
D

Dustan

Hello all,

Let us say I have a function like this:

def efficientFiller(file):

Note that you are shadowing the built-in variable 'file' here. Better
use 'filename', or something to that effect.
worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list

I want to plainly get the last value the yield statement generates.
How can I go about doing this please?

Regards,
Shriphani Palakodety

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
last_value = new_list[-1]
print last_value

# OR

efficient_filler = efficientFiller("libexo-0.3.pot")
for last_value in efficient_filler: pass
print last_value


The latter assumes that the last value is the only value you want.
 
S

Shriphani

Hello all,
Let us say I have a function like this:
def efficientFiller(file):

Note that you are shadowing the built-in variable 'file' here. Better
use 'filename', or something to that effect.


worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list
I want to plainly get the last value the yield statement generates.
How can I go about doing this please?
Regards,
Shriphani Palakodety

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
last_value = new_list[-1]
print last_value

# OR

efficient_filler = efficientFiller("libexo-0.3.pot")
for last_value in efficient_filler: pass
print last_value

The latter assumes that the last value is the only value you want.

Hello again,

Well the basic trouble is that the yield statement you see there
causes it to print the list over and over again when a string
containing "msgid" is found and the subsequent conditions are
satisfied. I just want the last list the yield statement generates. I
don't want the last element of the list generated. just the last
statement.
Regards,
Shriphani Palakodety
 
A

Amit Khemka

Hello all,
Let us say I have a function like this:
def efficientFiller(file):

Note that you are shadowing the built-in variable 'file' here. Better
use 'filename', or something to that effect.


worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list
I want to plainly get the last value the yield statement generates.
How can I go about doing this please?
Well the basic trouble is that the yield statement you see there
causes it to print the list over and over again when a string
containing "msgid" is found and the subsequent conditions are
satisfied. I just want the last list the yield statement generates. I
don't want the last element of the list generated. just the last
statement.

I may be being stupid but i really fail to understand that why would
you want to use 'yield' in such a scenario ?

Btw, the following line in your code may cause some unexpected
behavior (in case of duplicates):
<code>
number = pot_file_text.index(line)
</code>

You should rather use 'enumerate' .

Cheers,
 
J

John Machin

Hello all,

Let us say I have a function like this:

def efficientFiller(file):
worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list



I want to plainly get the last value the yield statement generates.
How can I go about doing this please?

I don't think that 'efficient' and 'plainly' mean what you think they
mean. However to answer your question:

new_list[-1] if new_list else None

BTW I get the impression that the yield statement yields the whole
pot_file_text list each time, so that new_list will be a list of lists;
is that intentional?
 
S

Shriphani

Hello all,
Let us say I have a function like this:
def efficientFiller(file):
worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list
I want to plainly get the last value the yield statement generates.
How can I go about doing this please?

I don't think that 'efficient' and 'plainly' mean what you think they
mean. However to answer your question:

new_list[-1] if new_list else None

BTW I get the impression that the yield statement yields the whole
pot_file_text list each time, so that new_list will be a list of lists;
is that intentional?

Hello again,
I am sorry for having made that extra post and should have seen that
the solution I wanted was posted here. Anyway thanks a lot

Regards,
Shriphani Palakodety
 
P

Paul Hankin

Hello all,

Let us say I have a function like this:

def efficientFiller(file):
worthless_list = []
pot_file = open(file,'r')
pot_file_text = pot_file.readlines()
for line in pot_file_text:
if line.find("msgid") != -1:
message_id = shlex.split(line)[1]
if message_id in dictionary:
number = pot_file_text.index(line)
corresponding_crap =
dictionary.get(message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_crap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_filler = efficientFiller("libexo-0.3.pot")
new_list = list(efficient_filler)
print new_list

I want to plainly get the last value the yield statement generates.
How can I go about doing this please?

If you want only one value, you should be using return rather than
yield. What do you think your code does - and what is it supposed to
do? If it's not doing what it's supposed to be doing, it's better to
understand why not rather than try to patch up the output.

I'm going to guess that you want to take the lines of a file, and
after every line of the form 'msgid K' insert a line 'msgstr "..."'
when K is in some dictionary you've got. I'll assume you want the new
lines as a generator rather than as a list.

Then, something like (untested)

def lookup_msgids(filename):
"""Yield lines from the given file, with 'msgstr' lines added
after 'msgid' lines when the id is in the id dictionary."""
for line in open(filename, 'r'):
yield line
if line.startswith('msgid'):
msgid = shlex.split(line)[1]
if msgid in dictionary:
yield 'msgstr "%s"\n' % dictionary[msgid]


libexo_pot = list(lookup_msgids('libexo-0.3.pot'))
print libexo_pot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top