Python Forum
Infinate Number Generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinate Number Generator
#1
There's probably already something like this in collections, but I wanted to write one, so came up with this.
I found it useful for a test case.
to use, instantiate class NumberGenerator like myctr = NumberGenerator(12, 8).numgen
now each time you call myctr() it returns a number starting at 12, and increments by 8
of course you can do the sane thing with range, if in a loop, this one does not require the loop.
class NumberGenerator:
    def __init__(self, start=0, increment=1):
        self.start = start
        self.increment = increment
        self.current_num = start
    
    def numgen(self):
        retval = self.current_num
        self.current_num += self.increment
        return retval


def testit():
    # test1 create a counter1 counts starting at 15 incrementing by 2
    c1 = NumberGenerator(15, 2).numgen
    for x in range(10):
        print(f'c1: {c1()}')
    print()
    # test2 create another counter2 starts at 25 decrementing by -3
    c2 = NumberGenerator(25, -3).numgen
    for x in range(10):
        print(f'c2: {c2()}')
    
    # test 3 one more time for each counter
    print(f'\nc1 again: {c1()}')
    print(f'c2 again: {c2()}\n')

if __name__ == '__main__':
    testit()
output:
Output:
c1: 15 c1: 17 c1: 19 c1: 21 c1: 23 c1: 25 c1: 27 c1: 29 c1: 31 c1: 33 c2: 25 c2: 22 c2: 19 c2: 16 c2: 13 c2: 10 c2: 7 c2: 4 c2: 1 c2: -2 c1 again: 35 c2 again: -5
Reply
#2
This definitely be done with a built-in
Output:
>>> from itertools import count >>> c3 = count(25, -3).__next__ >>> for x in range(10): ... print(f"c3: {c3()}") ... c3: 25 c3: 22 c3: 19 c3: 16 c3: 13 c3: 10 c3: 7 c3: 4 c3: 1 c3: -2
Reply
#3
How about this:
>>> numgen = lambda start, stop, step: (num for num in range(start, stop, step))
>>> for n in numgen(10, 0, -2):
...     print(n)
... 
10
8
6
4
2
>>> 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Sep-18-2018, 03:53 AM)wavic Wrote: How about this:
Can't you just inline the range() call there? The lambda and comprehension are just indirection wrapping it.
Reply
#5
It's not a comprehension but generator expression. I am using lambda because range requires at least one parameter and I can't pass it just using an expression alone. Like: (n for n in range())
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Comprehensions can be of the list or generator variety (or for that matter sets and dictionaries in modern Python).

Let me show you what I meant:
Output:
>>> for n in range(10, 0, -2): ... print(n) ... 10 8 6 4 2
It produces the same output as yours, minus the comprehension, minus the lambda. I don't understand what benefit there is to including them when you can skip them without any issues.

EDIT: to clarify, the link I have here says that the original docs didn't say "generate comprehension" but calls for people to use the term. I don't want to dig into this particular terminology argument, but I think it's a reasonable term, I think Ned is a good Python authority, and the rest of my points about indirection still stand.
Reply
#7
Yes, this implementation is totally useless because range produces one value at a time. Cool
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Real Random Number Generator woodturner550 21 2,161 Feb-01-2024, 03:19 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020