How to create a human-readable string representing a rrule object?

My application allows users to define the scheduling of objects, and they are saved as rrule. I need to list these objects and show something like "Daily, 4:30 pm". Is there something available that "fairly formats" an rrule instance?

+4
source share
1 answer

You just need to provide the __str__ method, and it will be called whenever something should display your object as a string.

For example, consider the following class:

 class rrule: def __init__ (self): self.data = "" def schedule (self, str): self.data = str def __str__ (self): if self.data.startswith("d"): return "Daily, %s" % (self.data[1:]) if self.data.startswith("m"): return "Monthly, %s of the month" % (self.data[1:]) return "Unknown" 

which prints itself nicely with the __str__ method. When you run the following code for this class:

 xyzzy = rrule() print (xyzzy) xyzzy.schedule ("m3rd") print (xyzzy) xyzzy.schedule ("d4:30pm") print (xyzzy) 

you will see the following output:

 Unknown Monthly, 3rd of the month Daily, 4:30pm 
+1
source

Source: https://habr.com/ru/post/1397157/


All Articles