#StackBounty: #python #python-3.x #decorator Pass a decorator's function into Python RQ

Bounty: 50

How do I pass a decorator’s function into a job?

I have a decorator that would run a job using the function.

@job
def queueFunction(passedFunction, *args, **kwargs):
    # Do some stuff
    passedFunction(*args, **kwargs)

def myDecorator(async=True):
    def wrapper(function):
        def wrappedFunc(*args, **kwargs):
            data = DEFAULT_DATA
            if async:
                queueFunction.delay(function, *args, **kwargs)
            else:
                data = queueFunction(function, *args, **kwargs)
            return data
        return wrappedFunc
    return wrapper

I get an error when trying to use it.

Can't pickle <function Model.passedFunction at 0x7f410ad4a048>: it's not the same object as modelInstance.models.Model.passedFunction

Using Python 3.4


Get this bounty!!!

Example of Decorator Pattern – LowerCaseInputStream

Create a LowerCaseInputStream class as follows

public class LowerCaseInputStream extends FilterInputStream 
{

    public LowerCaseInputStream(InputStream in) {
        super(in);
    }
    
    @Override
    public int read() throws IOException
    {
        int c = super.read();
        if(c==-1)
            return c;
        else
            return Character.toLowerCase(c);
    }
    @Override
    public int read(byte b[], int offset, int len) throws IOException
    {
        int result = super.read(b, offset, len);
        for (int i = offset; i < offset+result; i++) 
        {
            b[i] = (byte)Character.toLowerCase((char)b[i]);
        }      
        return result;
    }
    
}

Use LowerCaseInputStream as follows

public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("D:\Yogesh.txt");
        BufferedInputStream bufin = new BufferedInputStream(fis);
        InputStream in = new LowerCaseInputStream(bufin);
        int c;
        try
        {
            while((c=in.read())!=-1)
            {
                System.out.print((char)c);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
            
    }

Find it really interesting? You can read more about Decorator pattern in Head First Design Pattern book.