如何生成一个表示rrule对象的可读的字符串? [英] How to generate a human-readable string that represents a rrule object?
问题描述
您只需提供一个 __str __
方法,每当需要将对象呈现为字符串时,就会调用它。
例如,考虑以下类: p>
class rrule:
def __init__(self):
self.data =
def (self,str):
self.data = str $ b $ def __str__(self):
self.data.startswith(d):
return %s(自我数据[1:])
如果self.data.startswith(m):
返回月份,月份%s%(self.data [1 :])
returnUnknown
使用 __ str __
方法。当你对这个类运行下面的代码时:
pre $ x $ x $ x $ b $ x $ b xyzzy.schedule(m3rd)
print(xyzzy)
xyzzy.schedule(d4:30pm)
print(xyzzy)
$ b
$ p $ 未知
每月,每月的3日
每日,下午4:30
My app allows users define scheduling on objects, and they is stored as rrule. I need to list those objects and show something like "Daily, 4:30 pm". There is something available that "pretty formats" an rrule instance?
You simply have to provide a __str__
method and it will be called whenever something needs to render 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 pretty-prints itself using the __str__
method. When you run the following code against that class:
xyzzy = rrule()
print (xyzzy)
xyzzy.schedule ("m3rd")
print (xyzzy)
xyzzy.schedule ("d4:30pm")
print (xyzzy)
you see the following output:
Unknown
Monthly, 3rd of the month
Daily, 4:30pm
这篇关于如何生成一个表示rrule对象的可读的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!