[Tutor] for loop issue
Adam Bark
adam.jtm30 at gmail.com
Fri Oct 9 11:40:23 CEST 2009
2009/10/9 Stefan Lesicnik <stefan at lsd.co.za>
Hi,
>
> This feels like a strange issue, so i hope I formulate this so its
> understandable.
>
> I have some objects. Each object has associated values. I am looping
> through these objects, working with a value and printing out the
> value.
> The issue in this case is that i need to check if the one value
> superseeds the other, and in that case, not print it out. I think the
> problem is that when you are in the loop, you dont know about the
> other object that you havent processed yet, and when you are
> processing that object, you dont know about the previous one? (not
> 100% sure if this assumption is correct)
>
> eg of my data (im comparing ubuntu -> debian security fixes and the
> data comes from https://edge.launchpad.net/ubuntu/+source/openswan)
> You can see what i have so far here -
> http://people.ubuntu.com/~stefanlsd/synclist.html<http://people.ubuntu.com/%7Estefanlsd/synclist.html>
>
> The Jaunty Jackalope (current stable release)
> Show details 1:2.4.12+dfsg-1.3+lenny2build0.9.04.1 updates, security
> (universe) two days ago
> Show details 1:2.4.12+dfsg-1.3 release (universe) 49 weeks ago
>
> In the above case, the first loop goes through
> 1:2.4.12+dfsg-1.3+lenny2build0.9.04.1. At this point i need it to stop
> without checking the next entry 1:2.4.12+dfsg-1.3 as the former
> superseeds it.
> I understand that I can use a break to exit the loop, but i run a
> whole bunch of things after this check, so i dont want to break out of
> it.
>
> Is this possible what i'm doing? or have i architected the loop wrong
> so i 'should' be able to break out of it.
>
> Thanks in advance!
>
> stefan
>
>
> snippet of code below if its of any use...
>
>
> for app in apps:
> print('app in apps: %s' % app)
> for item in series:
> print('item in series: %s' % item)
> debian_series = item[0]
> debian_fullversion = item[2]
> print('debian_series: %s' % debian_series)
> print('debian_fullversion: %s' % debian_fullversion)
>
> debian_version =
> re.sub('\+?(etch|lenny|sarge|squeeze|woody)[0-9]+$', '',
> debian_fullversion)
> print('debian_version: %s' % debian_version)
>
> main_archive =
> launchpad.distributions['ubuntu'].main_archive
> #TODO - May be able to use a version check in the
> publishedsource to increase speed of checking
> publishedsource =
> main_archive.getPublishedSources(source_name = app,
> status='Published', exact_match=True)
>
> for source in publishedsource:
>
> if source.distro_series.active:
> print source.distro_series.name,
> source.source_package_version
> ubuntu_fullversion = source.source_package_version
> print('ubuntu fullversion before: %s' %
> ubuntu_fullversion)
> ubuntu_version =
> re.sub('build0.[0-9]+.[0-9][0-9].[0-9]+$', '', ubuntu_fullversion)
> #ubuntu_version =
> re.sub('\+?(etch|lenny|sarge|squeeze|woody)[0-9]+$', '',
> ubuntu_version)
> print('ubuntu version after: %s' % ubuntu_version)
> #ubuntu_target = ('%subuntu/+source/%s') %
> (launchpad._root_uri,app)
>
> if debian_fullversion == ubuntu_version:
> continue
>
> if debian_version in ubuntu_fullversion:
> ubuntu_component = source.component_name
> ubuntu_series = source.distro_series.name
> ubuntu_pocket = source.pocket
> print('ubuntu_pocket: %s' % ubuntu_pocket)
> print('debian version: %s' % debian_version)
>
> #If pocket is Security, no need to check further
> if ubuntu_pocket == 'Security' or
> ubuntu_pocket == 'Updates':
> print('Not Found version in: %s.
> Continue' % ubuntu_pocket)
> break
>
> if debian_version == ubuntu_version:
> matchtype = 'Sync Match'
> else:
> matchtype = 'MoM Match'
>
> ubuntu_changelog = '<a
> href=http://changelogs.ubuntu.com/changelogs/pool/' + ubuntu_component
> + '/' + app[:1] + '/' + app + '/' + app + '_' + ubuntu_version +
> '/changelog>Ubuntu Changelog</a>'
> debian_changelog = '<a
> href=http://packages.debian.org/changelogs/pool/' + 'main' + '/' +
> app[:1] + '/' + app + '/' + app + '_' + debian_fullversion +
> '/changelog>Debian Changelog</a>'
>
> print('cve: %s' % cve)
> for cveitem in cve:
> cvestatus, url = cvecheck(cveitem,
> ubuntu_series, ubuntu_version)
> print('cvestatus: "%s"' % cvestatus)
> if cvestatus == 'released' or
> cvestatus == 'dne' or cvestatus == 'not-affected':
> pass
> else:
> writeweb(date, matchtype, app,
> ubuntu_series, ubuntu_fullversion, debian_fullversion,
> ubuntu_changelog, debian_changelog, url, cveitem, cvestatus)
>
I'm not sure I fully understand this but you can put the break statement
anywhere so you can check whether or not you need to continue then do a
bunch of things before finally breaking out of the loop eg.
if "something good" == happened:
# do stuff
# do some more things
break
HTH
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091009/5ea8c755/attachment-0001.htm>
More information about the Tutor
mailing list