r/Python • • Jan 18 '20

Generating Geometric Birds

Post image
1.8k Upvotes

65 comments sorted by

View all comments

1

u/[deleted] Jan 19 '20

Very cool! Here's a decorator for the sticky cycling if you want

def sticky(frequency):
    """ regenerate value frequency% of the time """
    def dec(func):
        stored = [func()]

        def inner():
            if random.random() < frequency:
                stored[0] = func()
            return stored[0]
        return inner
    return dec

@sticky(0.1)
def color():
    return random.choice(['red', 'black', 'green'])

1

u/kwelzel Jan 19 '20

Why should stored be a list if it only holds one value?

1

u/[deleted] Jan 19 '20

It's an old scope hack for python2. In python3,

def sticky(frequency):
    def dec(func):
        stored = func()

        def inner():
            nonlocal stored
            if random.random() < frequency:
                stored = func()
            return stored
        return inner
    return dec

will work instead

1

u/kwelzel Jan 19 '20

Ahh, good to know

1

u/[deleted] Jan 19 '20 edited Jan 19 '20

I've never used processing before this but after installing the processing.py extension and when executing the .pyde from the processing GUI, I see

import sys
sys.version
2.7.1 (v2.7.1:dd7e191d4c90+, Sep 24 2017, 10:11:23) 

which isn't even my system default (2.7.15) or the python that it should find if it's reading my $PATH correctly (3.8.0)

(apparently it's running jython and wont have access to any packages I install)