0

I have the following code which finds all of the tag "depends-on" and joins them and prints them..now I want to grab only the last depends on and print is value..expected output and current out put is shown below.

Current output:-

['348523', '350167', '350169', '348522', '350166', '350168', '350169', '350170', '350428', '350435', '350439', '350446', '350449', '350450', '350459', '350462', '350463', '350466', '350472', '350475', '350810', '350811', '350812', '350870', '350871', '350875', '350876', '350882', '350883', '350884', '350885', '348521']

Expected output:-
['348521','238567']

Code:-

import re
def findexternaldep ():
    #print "Processing dependency for change %s", change
    #change=str(change)
    comments = ''' comments:
    timestamp: 2013-06-12 09:18:36 PDT
    reviewer:
      name: user L
      email: username@com.company.com
      username: username
    message: Patch Set 2:

             Depends-on: 348523 350167 350169
    timestamp: 2013-06-12 09:18:36 PDT
    reviewer:
      name: user L
      email: username@com.company.com
      username: username
    message: Patch Set 2:

             Depends-on: 348522 350166 350168 350169 350170 350428 350435 350439 350446 350449 350450 350459 350462 350463 350466 350472 350475 350810 350811 350812 350870 350871 350875 350876 350882 350883 350884 350885
  comments:
    timestamp: 2013-06-12 10:39:46 PDT
    reviewer:
      name: user L
      email: username@com.company.com
      username: username
    message: Patch Set 2:
             Depends-on: 348521 238567
    '''

    print "COMMENTS"
    print comments
    deps = ' '.join(re.findall(r'(?<=Depends-on:\s)[\d ]+(?=\n)', comments)).split()
    print "DEPS"
    print deps
    #print depgerrit
    return deps

def main ():
    findexternaldep()

if __name__ == '__main__':
    main()
carte blanche
  • 10,796
  • 14
  • 46
  • 65
  • I think this is very close to a duplicate of this http://stackoverflow.com/questions/930397/how-to-get-the-last-element-of-a-list – Josh Jun 18 '13 at 00:38
  • actually no..I want to grab the latest depends-on and prints its values..above will always print the last value in the list whichis not what I want. – carte blanche Jun 18 '13 at 00:41
  • What do you mean by 'latest' in this case? By timestamp? By order in a data structure? It is ambiguous. – dawg Jun 18 '13 at 00:42
  • To get your exact expected output: replace `print deps` with `print [deps[-1]]` – dawg Jun 18 '13 at 00:44
  • You seem to be missing a chunk of code that processes the timestamp. Is that what you're looking for? Can you paste that code in? – aronchick Jun 18 '13 at 00:45
  • @aronchick - nothing is missing... i want to parse the last depends-on: and get is value – carte blanche Jun 18 '13 at 00:52
  • @user2125827 -- OK, but what do you mean by "last"? Clearly not last in the list. Last in terms of the most recent associated timestamp? If that's so, what do you want to do when the most recent entry has multiple depends-on ids? Return them all? – jedwards Jun 18 '13 at 01:04
  • @jedwards - last means last..no need to check for any timestamps...whatever is the last one...just print is values yes return them all..I updated my question – carte blanche Jun 18 '13 at 01:15

4 Answers4

1

Just search backwards in the comments, then take up until the end of line:

print comments.rsplit('Depends-on: ', 1)[1].split('\n', 1)[0]

Or modifying your regular expression to be:

deps = re.findall(r'(?<=Depends-on:\s)[\d ]+(?=\n)', comments)

Then get the last element:

print deps[-1]

And if you wanted them all joined:

all_deps = ' '.join(deps) # similar to before
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

What about using a greedy dot match to skip until the last Depends-on:

m=re.match(r'.*Depends-on:\s*([^\n]*)', comments, re.DOTALL)
if m: print m.group(1)

Output:

348521
perreal
  • 94,503
  • 21
  • 155
  • 181
  • how to take care of a case where m is none...am getting the error deps = m.group(1) AttributeError: 'NoneType' object has no attribute 'group' – carte blanche Jun 18 '13 at 01:06
0

If by latest, you mean the last element of the list, then you can get that by:

deps[-1]

For example:

a = [2,4,6]
e = a[-1]
print(e)    #  Prints "6"

This seems like what you are looking for by your expected output.

However, if you're looking for the largest or smallest of the list elements (when interpreted as integers), you could use something like:

max(map(int, deps))

For example:

a = ['2','4','1','3']
e = max(map(int, a))
print(e)    #  Prints "4"
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • actually no..I want to grab the latest depends-on and prints its values..above will always print the last value in the list whichis not what I want.. – carte blanche Jun 18 '13 at 00:40
  • Then what do you mean by "latest"? – jedwards Jun 18 '13 at 00:43
  • I mean the last "depends-on: " flag and then print its value – carte blanche Jun 18 '13 at 00:52
  • ...actually no..I want to grab the latest ...I mean the last "depends-on: " ...actually no..I want to grab the latest,...I mean the last "depends-on: ". Which is it? How is the latest even different from the last? – dansalmo Jun 18 '13 at 01:00
0

Do you mean you want it to return the last one in the list? With no error checking, wouldn't that just be:

deps[-1]

Otherwise, the issue is that each one is getting found in the regular expression, so they're all being included in the list.

aronchick
  • 6,786
  • 9
  • 48
  • 75