473,414 Members | 1,663 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,414 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 1517
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:
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...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.