473,671 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_fille r = 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 1526
On Oct 10, 5:19 am, Shriphani <shripha...@gma il.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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_fille r = 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_fille r = efficientFiller ("libexo-0.3.pot")
new_list = list(efficient_ filler)
last_value = new_list[-1]
print last_value

# OR

efficient_fille r = efficientFiller ("libexo-0.3.pot")
for last_value in efficient_fille r: 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...@g mail.comwrote:
On Oct 10, 5:19 am, Shriphani <shripha...@gma il.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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_fille r = 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_fille r = efficientFiller ("libexo-0.3.pot")
new_list = list(efficient_ filler)
last_value = new_list[-1]
print last_value

# OR

efficient_fille r = efficientFiller ("libexo-0.3.pot")
for last_value in efficient_fille r: 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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_fille r = 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********@gma il.comwrote:
On Oct 10, 3:34 pm, Dustan <DustanGro...@g mail.comwrote:
On Oct 10, 5:19 am, Shriphani <shripha...@gma il.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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_fille r = 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.i ndex(line)
</code>

You should rather use 'enumerate' .

Cheers,
--
--
Amit Khemka
Oct 10 '07 #5
On Oct 10, 4:05 pm, John Machin <sjmac...@lexic on.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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text
efficient_fille r = 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...@gma il.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.readli nes()
for line in pot_file_text:
if line.find("msgi d") != -1:
message_id = shlex.split(lin e)[1]
if message_id in dictionary:
number = pot_file_text.i ndex(line)
corresponding_c rap =
dictionary.get( message_id)
final_string = 'msgstr' + " " + '"' +
corresponding_c rap + '"' + '\n'
pot_file_text[number+1] = final_string
yield pot_file_text

efficient_fille r = 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(f ilename):
"""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(lin e)[1]
if msgid in dictionary:
yield 'msgstr "%s"\n' % dictionary[msgid]
libexo_pot = list(lookup_msg ids('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
40096
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 fetch last 5 records. Can any one help in this...
3
3519
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
1996
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 interactive interpreter does, but only show the part of the traceback relating to the code sent to exec. For example here is the code I'm using: try: exec code
23
18061
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
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.