473,224 Members | 1,396 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,224 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 1506
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
3
by: andy.leszczynski | last post by:
Hi, I might understand why this does not work, but I am not convinced it should not - following: def nnn(): print 'inside' yield 1 def nn():
8
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.