playing with iteration

Ok, so today started with, I want to break some list comprehensions, but turned into, 'What is this bit of joy?'

A stackOverflow post:
https://stackoverflow.com/questions/19151/build-a-basic-python-iterator

Totally seeing what I can make this do, cause... why not!
UPDATE!  I locked up my computer trying to do this:
DO NOT DO THIS.  lol....  here's what locked me up: (see yellow highlighted code)
update: After much oh shitness,  I got the Itertooly fixed. I don't get why there isn't something built in to stop infinite loops from crashing Unix.  Once a running program eats up so much memory,  It seems like it should just stop whats running that's eating it...
Update: 5-18-17 My good CPU is now crashing without any reason... I fired it up and opened up a few things to start working, and BOOP..... frozen... nothing.
Picture of bootup I caught on camera:




update: 5-23-18,  currently trying to find what ulimit to set to stop my alienware from crashing.  Every time I fire it up now there are 6 million + files being cleaned.  Sometimes it works without a blip,  then today, fired it up, opened my terminal, atom, firefox, and BOOP! NO CPU FOR YOU!
Hard-reset...  Looking for answers on my laptop.  Grrr.....  I'm pretty sure that bit of code is some kind of accidental python fork bomb.
My laptops ulimits has a 0 core dump, meaning it won't allow those,  Now I'm going to fire up the alienware and see what it's limits are....  I did lock the laptop up with that trash of code down there, but it has not since locked up like the alienware linux.


My alienware's ulimit -a:
https://access.redhat.com/solutions/61334  Everyone wants to tell me how to unlimit it, but not what to change to limit it... frustrating....





look I made a reverse suffix string!

from picture class modification:

def __next__(self):
        if self.count == len(self.stop):
            raise StopIteration
        else:
            result = self.start
            length = len(self.stop) - 1
            if self.count < length:
                character = self.stop[self.count]
                self.start = character + self.start
                self.count += 1
            else:
                c = self.stop[-1]
                result = c + self.start
                self.count += 1
            return result



fixed itertooly:
# not exactly fixed.. given the wrong pair of strings this can infinite loop too:
fixing it like the above modification
 class IterTooly(object):

    def __init__(self, start, stop):
        self.start = start
        self.stop = stop
        self.count = 0


    def __iter__(self):
        return self

    def __next__(self):
        if self.count == len(self.stop):
# was -->  if self.start > self.stop:  
            raise StopIteration
        else:
            result = self.start
            length = len(self.stop) - 1
            character = self.stop[length - self.count]
            self.start = character + self.start
            self.count += 1
            return result









# after a little browsing, list comprehensions should be used to make a list
# using it in any other fashion may be foolish and unPythonic... Lets break it.

def make_list_comp(something):
    return [x for x in something]

def make_list_explicit(something):
    alist = []
    for x in something:
        alist.append(x)
    return alist

class IterToolx(object):
    def __init__(self, start, stop):
        self.start = start
        self.stop = stop
    def __iter__(self):
        return self

    def __next__(self):
        if self.start > self.stop:
            raise StopIteration
        else:
            self.start += 1
            #yield breaks it.
            result = self.start - 1
            return result

class IterTooly(object):
    def __init__(self, start, stop):
        self.start = start
        self.stop = stop



    def __iter__(self):
        return self

    def __next__(self):
        if self.start > self.stop:
            raise StopIteration
        else:
            result = self.start
            self.start += 'a'
            #yield breaks it (from a different run-did not lock up computer)
            return result


#test 1
"""
a = make_list_comp('a string')
b = make_list_explicit('a string')
print(a)
print(b)
"""
#test 2
"""
#a = make_list_comp(345678) #int object not iterable
b = make_list_explicit(345678) #int object not iterable
print(a)
print(b)
"""
#test 3
"""
spam = str(1234567)
a = make_list_comp(spam)
b = make_list_explicit(spam)
print(a)
print(b)
"""
#test 4
"""
spam = IterToolx(1, 15)
eggs = IterToolx(1, 15)
a = make_list_comp(spam)
b = make_list_explicit(spam) # empty if same Itertoolx object is passed to both
print(a) # printed list of 1-15
print(b) # printed empty list
print(spam) #<__main__.IterToolx object ....>
"""
#test 5
spam = IterToolx(1, 15)
fail = IterTooly('boo', 'hoo')
#print(fail)

#eggs = IterToolx(1, 15)
# How do I find out what in this locked me up, if it locks up the computer???
#maybe it's like 'timeit' screwing with me all over again...
a = make_list_comp(fail) # think this is what locked me up.

b = make_list_explicit(spam) #worked
print(a)
print(b)






Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame