[英]How to generate a human-readable string that represents a rrule object?
我的應用程序允許用戶定義對象的調度,並將它們存儲為rrule。 我需要列出這些對象並顯示類似“每日,下午4:30”的內容。 有一些東西可以“漂亮地格式化”一個rrule實例嗎?
您只需提供__str__
方法,只要需要將對象呈現為字符串,就會調用它。
例如,請考慮以下類:
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"
使用__str__
方法進行漂亮打印。 當您針對該類運行以下代碼時:
xyzzy = rrule()
print (xyzzy)
xyzzy.schedule ("m3rd")
print (xyzzy)
xyzzy.schedule ("d4:30pm")
print (xyzzy)
你看到以下輸出:
Unknown
Monthly, 3rd of the month
Daily, 4:30pm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.