471,337 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Last value of yield statement

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

Oct 10 '07 #1
6 1431
On Oct 10, 5:19 am, Shriphani <shripha...@gmail.comwrote:
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.

Oct 10 '07 #2
On Oct 10, 3:34 pm, Dustan <DustanGro...@gmail.comwrote:
On Oct 10, 5:19 am, Shriphani <shripha...@gmail.comwrote:
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

Oct 10 '07 #3
On 10/10/2007 8:19 PM, Shriphani wrote:
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?
Oct 10 '07 #4
On 10/10/07, Shriphani <sh********@gmail.comwrote:
On Oct 10, 3:34 pm, Dustan <DustanGro...@gmail.comwrote:
On Oct 10, 5:19 am, Shriphani <shripha...@gmail.comwrote:
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,
--
--
Amit Khemka
Oct 10 '07 #5
On Oct 10, 4:05 pm, John Machin <sjmac...@lexicon.netwrote:
On 10/10/2007 8:19 PM, Shriphani wrote:
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

Oct 10 '07 #6
On Oct 10, 11:19 am, Shriphani <shripha...@gmail.comwrote:
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

--
Paul Hankin

Oct 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

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.