annotate python/example/iterable.py @ 922:74dc16f074be default tip

ignore ff merges
author Jeff Hammel <k0scist@gmail.com>
date Thu, 03 Oct 2024 05:36:19 -0700
parents e10d85ee0be3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
587
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 illustration of a class iterable
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # XXX does not work!!@ TODO!!! XXX #
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 class MyIterable(object):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def __init__(self, max):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 self.items = list(range(max))
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def __iter__(self):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 return self
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def next(self):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 for i in self.items:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 yield i
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def main(args=sys.argv[1:]):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 usage = '%prog [options]'
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 parser = argparse.ArgumentParser(usage=usage, description=__doc__)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 myiter = MyIterable(10)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 for i in myiter:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 print ('Hi {}'.format(i))
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 main()