Last value of yield statement

Discussion in 'Python' started by Shriphani, Oct 10, 2007.

  1. Shriphani

    Shriphani Guest

    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
     
    Shriphani, Oct 10, 2007
    #1
    1. Advertising

  2. Shriphani

    Dustan Guest

    On Oct 10, 5:19 am, Shriphani <> wrote:
    > 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.
     
    Dustan, Oct 10, 2007
    #2
    1. Advertising

  3. Shriphani

    Shriphani Guest

    On Oct 10, 3:34 pm, Dustan <> wrote:
    > On Oct 10, 5:19 am, Shriphani <> wrote:
    >
    > > 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
     
    Shriphani, Oct 10, 2007
    #3
  4. Shriphani

    Amit Khemka Guest

    On 10/10/07, Shriphani <> wrote:
    > On Oct 10, 3:34 pm, Dustan <> wrote:
    > > On Oct 10, 5:19 am, Shriphani <> wrote:
    > >
    > > > 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
     
    Amit Khemka, Oct 10, 2007
    #4
  5. Shriphani

    John Machin Guest

    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?
     
    John Machin, Oct 10, 2007
    #5
  6. Shriphani

    Shriphani Guest

    On Oct 10, 4:05 pm, John Machin <> wrote:
    > 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
     
    Shriphani, Oct 10, 2007
    #6
  7. Shriphani

    Paul Hankin Guest

    On Oct 10, 11:19 am, 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?


    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
     
    Paul Hankin, Oct 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    2
    Views:
    272
    Frank
    Jul 29, 2005
  2. Replies:
    1
    Views:
    331
    Gabriel Genellina
    Apr 22, 2008
  3. bthumber
    Replies:
    5
    Views:
    426
    Alexey Smirnov
    Jan 29, 2009
  4. Markus
    Replies:
    1
    Views:
    208
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    285
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page