How to get self values in decorators from tasks.py












-1















I need some help to get values from a class inside a decorator, I keep getting stuck.



tasks.py



@lazy_task()
def import_(date_time):
importer = Importer(date_time=date_time)
importer.import_jira()


import_.py



class Importer(object):

RECIPIENTS = 'friday@gmail.com'

def __init__(self, date_time=None):
self.date_time = data_time

def import_jira(self):
pass


decorators.py



def lazy_task():

def decorator(func):
@wraps(func)
def get_lazy_task(self, *args, **kwargs):
func(*args, **kwargs)
print self.RECIPIENTS # Not working
return get_lazy_task

return decorator


Please help me how to get RECIPIENTS in decorator from the file import.py










share|improve this question

























  • Try posting the contents of the entire files for better understanding.

    – Diptangsu Goswami
    Jan 2 at 12:38











  • And don't use keywords for function names or filenames. import is a keyword in python.

    – Diptangsu Goswami
    Jan 2 at 12:52
















-1















I need some help to get values from a class inside a decorator, I keep getting stuck.



tasks.py



@lazy_task()
def import_(date_time):
importer = Importer(date_time=date_time)
importer.import_jira()


import_.py



class Importer(object):

RECIPIENTS = 'friday@gmail.com'

def __init__(self, date_time=None):
self.date_time = data_time

def import_jira(self):
pass


decorators.py



def lazy_task():

def decorator(func):
@wraps(func)
def get_lazy_task(self, *args, **kwargs):
func(*args, **kwargs)
print self.RECIPIENTS # Not working
return get_lazy_task

return decorator


Please help me how to get RECIPIENTS in decorator from the file import.py










share|improve this question

























  • Try posting the contents of the entire files for better understanding.

    – Diptangsu Goswami
    Jan 2 at 12:38











  • And don't use keywords for function names or filenames. import is a keyword in python.

    – Diptangsu Goswami
    Jan 2 at 12:52














-1












-1








-1








I need some help to get values from a class inside a decorator, I keep getting stuck.



tasks.py



@lazy_task()
def import_(date_time):
importer = Importer(date_time=date_time)
importer.import_jira()


import_.py



class Importer(object):

RECIPIENTS = 'friday@gmail.com'

def __init__(self, date_time=None):
self.date_time = data_time

def import_jira(self):
pass


decorators.py



def lazy_task():

def decorator(func):
@wraps(func)
def get_lazy_task(self, *args, **kwargs):
func(*args, **kwargs)
print self.RECIPIENTS # Not working
return get_lazy_task

return decorator


Please help me how to get RECIPIENTS in decorator from the file import.py










share|improve this question
















I need some help to get values from a class inside a decorator, I keep getting stuck.



tasks.py



@lazy_task()
def import_(date_time):
importer = Importer(date_time=date_time)
importer.import_jira()


import_.py



class Importer(object):

RECIPIENTS = 'friday@gmail.com'

def __init__(self, date_time=None):
self.date_time = data_time

def import_jira(self):
pass


decorators.py



def lazy_task():

def decorator(func):
@wraps(func)
def get_lazy_task(self, *args, **kwargs):
func(*args, **kwargs)
print self.RECIPIENTS # Not working
return get_lazy_task

return decorator


Please help me how to get RECIPIENTS in decorator from the file import.py







python-2.7 python-decorators django-celery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 12:56









Diptangsu Goswami

782921




782921










asked Jan 2 at 12:19









Ubaidulla AzeemUbaidulla Azeem

166




166













  • Try posting the contents of the entire files for better understanding.

    – Diptangsu Goswami
    Jan 2 at 12:38











  • And don't use keywords for function names or filenames. import is a keyword in python.

    – Diptangsu Goswami
    Jan 2 at 12:52



















  • Try posting the contents of the entire files for better understanding.

    – Diptangsu Goswami
    Jan 2 at 12:38











  • And don't use keywords for function names or filenames. import is a keyword in python.

    – Diptangsu Goswami
    Jan 2 at 12:52

















Try posting the contents of the entire files for better understanding.

– Diptangsu Goswami
Jan 2 at 12:38





Try posting the contents of the entire files for better understanding.

– Diptangsu Goswami
Jan 2 at 12:38













And don't use keywords for function names or filenames. import is a keyword in python.

– Diptangsu Goswami
Jan 2 at 12:52





And don't use keywords for function names or filenames. import is a keyword in python.

– Diptangsu Goswami
Jan 2 at 12:52












2 Answers
2






active

oldest

votes


















0














In the file import_.py, RECIPIENTS is a static variable of the class Importer. This means, the variable RECIPIENTS needs to be accessed by <class-name>.<var-name>, which is this case is Importer.RECIPIENTS.



I would suggest you import Importer from import_.py and print Importer.RECIPIENTS instead of self.RECIPIENTS.



Add this line to the file decorators.py



from import_ import Importer


and change the print statement from print self.RECIPIENTS to



print Importer.RECIPIENTS


I wrote a simple example to make this work.



foo.py



class Foo:
VALUE = 'value'

def __init__():
pass


bar.py



from functools import wraps
from foo import Foo # importing class Foo from foo.py

def bar():
def decorator(func):
@wraps(func)
def inner():
print Foo.VALUE
return inner
return decorator

bar()(lambda: None)()


On running the file bar.py we get this output.



$ python bar.py
value


If you don't want to add an extra line of import statement at the top of every file that needs to use the RECIPIENTS value, you can directly import and print like this



print __import__('import_').Importer.RECIPIENTS


However, there is no other way of accessing a value from another file without importing it.






share|improve this answer


























  • Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

    – Ubaidulla Azeem
    Jan 2 at 13:25











  • Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

    – Diptangsu Goswami
    Jan 2 at 13:41













  • I have updated my answer, please check if this solves your problem.

    – Diptangsu Goswami
    Jan 2 at 13:48











  • Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

    – bruno desthuilliers
    Jan 2 at 14:12











  • Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

    – Diptangsu Goswami
    Jan 2 at 14:15





















-1














In your import_ function, the Importer instance is a local variable - not a function's argument -, so it can NOT be accessed by the decorator. Period.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006268%2fhow-to-get-self-values-in-decorators-from-tasks-py%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In the file import_.py, RECIPIENTS is a static variable of the class Importer. This means, the variable RECIPIENTS needs to be accessed by <class-name>.<var-name>, which is this case is Importer.RECIPIENTS.



    I would suggest you import Importer from import_.py and print Importer.RECIPIENTS instead of self.RECIPIENTS.



    Add this line to the file decorators.py



    from import_ import Importer


    and change the print statement from print self.RECIPIENTS to



    print Importer.RECIPIENTS


    I wrote a simple example to make this work.



    foo.py



    class Foo:
    VALUE = 'value'

    def __init__():
    pass


    bar.py



    from functools import wraps
    from foo import Foo # importing class Foo from foo.py

    def bar():
    def decorator(func):
    @wraps(func)
    def inner():
    print Foo.VALUE
    return inner
    return decorator

    bar()(lambda: None)()


    On running the file bar.py we get this output.



    $ python bar.py
    value


    If you don't want to add an extra line of import statement at the top of every file that needs to use the RECIPIENTS value, you can directly import and print like this



    print __import__('import_').Importer.RECIPIENTS


    However, there is no other way of accessing a value from another file without importing it.






    share|improve this answer


























    • Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

      – Ubaidulla Azeem
      Jan 2 at 13:25











    • Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

      – Diptangsu Goswami
      Jan 2 at 13:41













    • I have updated my answer, please check if this solves your problem.

      – Diptangsu Goswami
      Jan 2 at 13:48











    • Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

      – bruno desthuilliers
      Jan 2 at 14:12











    • Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

      – Diptangsu Goswami
      Jan 2 at 14:15


















    0














    In the file import_.py, RECIPIENTS is a static variable of the class Importer. This means, the variable RECIPIENTS needs to be accessed by <class-name>.<var-name>, which is this case is Importer.RECIPIENTS.



    I would suggest you import Importer from import_.py and print Importer.RECIPIENTS instead of self.RECIPIENTS.



    Add this line to the file decorators.py



    from import_ import Importer


    and change the print statement from print self.RECIPIENTS to



    print Importer.RECIPIENTS


    I wrote a simple example to make this work.



    foo.py



    class Foo:
    VALUE = 'value'

    def __init__():
    pass


    bar.py



    from functools import wraps
    from foo import Foo # importing class Foo from foo.py

    def bar():
    def decorator(func):
    @wraps(func)
    def inner():
    print Foo.VALUE
    return inner
    return decorator

    bar()(lambda: None)()


    On running the file bar.py we get this output.



    $ python bar.py
    value


    If you don't want to add an extra line of import statement at the top of every file that needs to use the RECIPIENTS value, you can directly import and print like this



    print __import__('import_').Importer.RECIPIENTS


    However, there is no other way of accessing a value from another file without importing it.






    share|improve this answer


























    • Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

      – Ubaidulla Azeem
      Jan 2 at 13:25











    • Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

      – Diptangsu Goswami
      Jan 2 at 13:41













    • I have updated my answer, please check if this solves your problem.

      – Diptangsu Goswami
      Jan 2 at 13:48











    • Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

      – bruno desthuilliers
      Jan 2 at 14:12











    • Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

      – Diptangsu Goswami
      Jan 2 at 14:15
















    0












    0








    0







    In the file import_.py, RECIPIENTS is a static variable of the class Importer. This means, the variable RECIPIENTS needs to be accessed by <class-name>.<var-name>, which is this case is Importer.RECIPIENTS.



    I would suggest you import Importer from import_.py and print Importer.RECIPIENTS instead of self.RECIPIENTS.



    Add this line to the file decorators.py



    from import_ import Importer


    and change the print statement from print self.RECIPIENTS to



    print Importer.RECIPIENTS


    I wrote a simple example to make this work.



    foo.py



    class Foo:
    VALUE = 'value'

    def __init__():
    pass


    bar.py



    from functools import wraps
    from foo import Foo # importing class Foo from foo.py

    def bar():
    def decorator(func):
    @wraps(func)
    def inner():
    print Foo.VALUE
    return inner
    return decorator

    bar()(lambda: None)()


    On running the file bar.py we get this output.



    $ python bar.py
    value


    If you don't want to add an extra line of import statement at the top of every file that needs to use the RECIPIENTS value, you can directly import and print like this



    print __import__('import_').Importer.RECIPIENTS


    However, there is no other way of accessing a value from another file without importing it.






    share|improve this answer















    In the file import_.py, RECIPIENTS is a static variable of the class Importer. This means, the variable RECIPIENTS needs to be accessed by <class-name>.<var-name>, which is this case is Importer.RECIPIENTS.



    I would suggest you import Importer from import_.py and print Importer.RECIPIENTS instead of self.RECIPIENTS.



    Add this line to the file decorators.py



    from import_ import Importer


    and change the print statement from print self.RECIPIENTS to



    print Importer.RECIPIENTS


    I wrote a simple example to make this work.



    foo.py



    class Foo:
    VALUE = 'value'

    def __init__():
    pass


    bar.py



    from functools import wraps
    from foo import Foo # importing class Foo from foo.py

    def bar():
    def decorator(func):
    @wraps(func)
    def inner():
    print Foo.VALUE
    return inner
    return decorator

    bar()(lambda: None)()


    On running the file bar.py we get this output.



    $ python bar.py
    value


    If you don't want to add an extra line of import statement at the top of every file that needs to use the RECIPIENTS value, you can directly import and print like this



    print __import__('import_').Importer.RECIPIENTS


    However, there is no other way of accessing a value from another file without importing it.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 2 at 13:47

























    answered Jan 2 at 12:50









    Diptangsu GoswamiDiptangsu Goswami

    782921




    782921













    • Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

      – Ubaidulla Azeem
      Jan 2 at 13:25











    • Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

      – Diptangsu Goswami
      Jan 2 at 13:41













    • I have updated my answer, please check if this solves your problem.

      – Diptangsu Goswami
      Jan 2 at 13:48











    • Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

      – bruno desthuilliers
      Jan 2 at 14:12











    • Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

      – Diptangsu Goswami
      Jan 2 at 14:15





















    • Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

      – Ubaidulla Azeem
      Jan 2 at 13:25











    • Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

      – Diptangsu Goswami
      Jan 2 at 13:41













    • I have updated my answer, please check if this solves your problem.

      – Diptangsu Goswami
      Jan 2 at 13:48











    • Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

      – bruno desthuilliers
      Jan 2 at 14:12











    • Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

      – Diptangsu Goswami
      Jan 2 at 14:15



















    Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

    – Ubaidulla Azeem
    Jan 2 at 13:25





    Thank you for your answer. But we used the same decorator for multiple tasks.py, So can you please tell us how to get them, RECIPIENTS.

    – Ubaidulla Azeem
    Jan 2 at 13:25













    Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

    – Diptangsu Goswami
    Jan 2 at 13:41







    Simply import the class from the file import_.py in every file that needs to use the variable RECIPIENTS.

    – Diptangsu Goswami
    Jan 2 at 13:41















    I have updated my answer, please check if this solves your problem.

    – Diptangsu Goswami
    Jan 2 at 13:48





    I have updated my answer, please check if this solves your problem.

    – Diptangsu Goswami
    Jan 2 at 13:48













    Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

    – bruno desthuilliers
    Jan 2 at 14:12





    Please check your facts. Python has nothing like "static variables", and class attributes CAN be accessed thru instances (which is how you can call methods on instances FWIW).

    – bruno desthuilliers
    Jan 2 at 14:12













    Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

    – Diptangsu Goswami
    Jan 2 at 14:15







    Please check this question. Static variables can obviously be accessed by instances, but it's not necessary to create instances in order to access them. Please check your facts.

    – Diptangsu Goswami
    Jan 2 at 14:15















    -1














    In your import_ function, the Importer instance is a local variable - not a function's argument -, so it can NOT be accessed by the decorator. Period.






    share|improve this answer




























      -1














      In your import_ function, the Importer instance is a local variable - not a function's argument -, so it can NOT be accessed by the decorator. Period.






      share|improve this answer


























        -1












        -1








        -1







        In your import_ function, the Importer instance is a local variable - not a function's argument -, so it can NOT be accessed by the decorator. Period.






        share|improve this answer













        In your import_ function, the Importer instance is a local variable - not a function's argument -, so it can NOT be accessed by the decorator. Period.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 14:16









        bruno desthuilliersbruno desthuilliers

        51.7k54465




        51.7k54465






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006268%2fhow-to-get-self-values-in-decorators-from-tasks-py%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith