如何生成一个表示rrule对象的可读的字符串? [英] How to generate a human-readable string that represents a rrule object?

查看:124
本文介绍了如何生成一个表示rrule对象的可读的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户在对象上定义调度,并将它们存储为rrule。我需要列出这些对象,并显示每日,下午4:30。有一些可用的漂亮格式的实例吗?

解决方案

您只需提供一个 __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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆