0

I am trying to construct an ETL machine for a test.

class TestETLMachine(object):

    API_URL = 'https://9g9xhayrh5.execute-api.us-west-2.amazonaws.com/test/data'

    @staticmethod
    def get_email_data(cls):
        headers = {'accept': 'application/json'}
        r = requests.get(cls.API_URL, headers=headers)
        email_objects_as_list_of_dicts = json.loads(r.content)['data']
        return email_objects_as_list_of_dicts

    @staticmethod
    def get_distinct_emails(cls):
        email_data = cls.get_email_data()
        print email_data

for get_distinct_emails I want to call TestETLMachine.get_email_data() and have it know I'm referring to this class. This object is a static machine, meaning it does the same thing always, and making instances of it is pointless and seems bad form. When I try to call get_email_data now that I pass cls I can't anymore:

In [9]: TestETLMachine.get_email_data()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-cf48fc1a9c1d> in <module>()
----> 1 TestETLMachine.get_email_data()

TypeError: get_email_data() takes exactly 1 argument (0 given)

How do I call these class methods and use the other class methods in my next class methods? Salamat

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 1
    Unless this is a greatly simplified example, I doubt you need a class at all. `API_URL` is only used in one method, and `cls` is only used to access that variable. Hard-code the url, or make it a parameter to `get_email_data`, and you're just left with two ordinary functions `get_email_data(url='https://...')` and `get_distinct_emails` (which calls `get_email_data`). – chepner Mar 05 '16 at 16:32
  • that was just the start, it was a 5 part test – codyc4321 Mar 05 '16 at 23:55

1 Answers1

7

You are looking for classmethod, not staticmethod. If you decorate a method with @classmethod, it will implicitly receive the class as first parameter.

See also the related question Meaning of @classmethod and @staticmethod for beginner?

Community
  • 1
  • 1
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841