3

I have a python code roughly like this:

class SomeClass():

    def get_date(self):
        date = # read the data from the database in YYYY-MM-DD HH:MM:SS format
        return date

now, I will use the returned value in an API query, which accepts the input as a unix timestamp. Actually I can give the returned date to another function to have it converted to a timestamp, but I thought it would be a good opportunity to learn about function cascading / chaining, which I've always wondered about in Python.

Now, what I basically want to achieve is something like this:

  • If I just execute SomeClass.get_date(), I want to have the date in the same format that has been returned from the database, which is YYYY-MM-DD HH:MM:SS
  • If I execute SomeClass.get_date().as_timestamp(), I should get it as timestamp.
  • If not possible, I would settle for having it like SomeClass.get_date().as_datetime() and SomeClass.get_date().as_timestamp()
  • I might use the second function (.as_timestamp()) for more than one primary function (there are multiple datetime columns that I may need to be converted into timestamp).

I've looked into some examples of function cascading. They are mostly saying that key to this is returning the self, but I could not find any info about how to implement it in the way I need to.

I basically need the return value to be fed into the second function (I guess), but not sure if it is possible, and don't know how to do it. Any ideas?

2
  • 1
    get_date is going to have to return some object that has the rest of the methods you want to use. You could have the __str__ of the Date object use the default representation, then have each of the other methods return other specific formatting. Commented Aug 8, 2018 at 13:32
  • 1
    The type of cascade you are referring to typically involves methods that mutate the object, and involve method calls that aren't really related to one another. In Python, such methods usually return None to emphasize that the object is being mutated (if the method doesn't return anything, why else you be calling it?). What you want to do is simply invoke a method on the return value of another method without using a temporary variable to store an intermediate value. Commented Aug 8, 2018 at 13:37

1 Answer 1

2

What you're looking for looks more like this:

import time
import calendar

class DateClass():

  def get_date(self):
    self = # read the data from the database in YYYY-MM-DD HH:MM:SS format
    return self

  def as_timestamp(self):
    mysql_time = time.strptime(self, '%Y-%m-%d %H:%M:%S')
    self = calendar.timegm(mysql_time)
    return self

d = DateClass()
print(d.get_date())
print(d.get_date().as_timestamp())
print(d)
d.get_date()
print(d)

The first output would be the MySQL datetime, and the second would be the Unix timestamp, the third would also be the unix timestamp and the fourth would be the datetime. get_date() and as_timestamp() mutate the instance d when called on it.

In this configuration, as_timestamp() requires get_date(), which seems to be in line with your question, but you could change that by assigning self from the database in as_timestamp() like this:

def as_timestamp(self):
  self = # read the data from the database in YYYY-MM-DD HH:MM:SS format
  mysql_time = time.strptime(self, '%Y-%m-%d %H:%M:%S')
  self = calendar.timegm(mysql_time)
  return self

which will make it possible to call as_timestamp() independent of get_date(). You can also then add d.as_timestamp() to mutate any iteration of d so that d is a timestamp.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, exactly what I was looking for! I've always thought that function chaining facilities some method that involves taking previous function's output and operating on that. It's relieving to see that it is actually a lot simpler than that :)
...also known as Fluent Interface

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.