convert usage.rst to valid doctest

pull/445/head
Nicco Kunzmann 2022-10-11 10:03:49 +01:00
rodzic 9c2d097d8b
commit fb51485ed2
1 zmienionych plików z 15 dodań i 14 usunięć

Wyświetl plik

@ -79,8 +79,8 @@ you do it like this. The calendar is a component::
>>> cal['summary'] = 'Python meeting about calendaring' >>> cal['summary'] = 'Python meeting about calendaring'
>>> for k,v in cal.items(): >>> for k,v in cal.items():
... k,v ... k,v
(u'DTSTART', '20050404T080000') ('DTSTART', '20050404T080000')
(u'SUMMARY', 'Python meeting about calendaring') ('SUMMARY', 'Python meeting about calendaring')
NOTE: the recommended way to add components to the calendar is to use NOTE: the recommended way to add components to the calendar is to use
create the subcomponent and add it via Calendar.add! The example above adds a create the subcomponent and add it via Calendar.add! The example above adds a
@ -90,7 +90,7 @@ string, but not a vText component.
You can generate a string for a file with the to_ical() method:: You can generate a string for a file with the to_ical() method::
>>> cal.to_ical() >>> cal.to_ical()
'BEGIN:VCALENDAR\r\nDTSTART:20050404T080000\r\nSUMMARY:Python meeting about calendaring\r\nEND:VCALENDAR\r\n' b'BEGIN:VCALENDAR\r\nDTSTART:20050404T080000\r\nSUMMARY:Python meeting about calendaring\r\nEND:VCALENDAR\r\n'
The rendered view is easier to read:: The rendered view is easier to read::
@ -102,13 +102,13 @@ The rendered view is easier to read::
So, let's define a function so we can easily display to_ical() output:: So, let's define a function so we can easily display to_ical() output::
>>> def display(cal): >>> def display(cal):
... return cal.to_ical().decode("utf-8").replace(b'\r\n', b'\n').strip() ... return cal.to_ical().decode("utf-8").replace('\r\n', '\n').strip()
You can set multiple properties like this:: You can set multiple properties like this::
>>> cal = Calendar() >>> cal = Calendar()
>>> cal['attendee'] = ['MAILTO:maxm@mxm.dk','MAILTO:test@example.com'] >>> cal['attendee'] = ['MAILTO:maxm@mxm.dk','MAILTO:test@example.com']
>>> print display(cal) >>> print(display(cal))
BEGIN:VCALENDAR BEGIN:VCALENDAR
ATTENDEE:MAILTO:maxm@mxm.dk ATTENDEE:MAILTO:maxm@mxm.dk
ATTENDEE:MAILTO:test@example.com ATTENDEE:MAILTO:test@example.com
@ -122,7 +122,7 @@ added. Here is an example::
>>> cal = Calendar() >>> cal = Calendar()
>>> cal.add('attendee', 'MAILTO:maxm@mxm.dk') >>> cal.add('attendee', 'MAILTO:maxm@mxm.dk')
>>> cal.add('attendee', 'MAILTO:test@example.com') >>> cal.add('attendee', 'MAILTO:test@example.com')
>>> print display(cal) >>> print(display(cal))
BEGIN:VCALENDAR BEGIN:VCALENDAR
ATTENDEE:MAILTO:maxm@mxm.dk ATTENDEE:MAILTO:maxm@mxm.dk
ATTENDEE:MAILTO:test@example.com ATTENDEE:MAILTO:test@example.com
@ -148,7 +148,7 @@ component::
And then appending it to a "parent":: And then appending it to a "parent"::
>>> cal.add_component(event) >>> cal.add_component(event)
>>> print display(cal) >>> print(display(cal))
BEGIN:VCALENDAR BEGIN:VCALENDAR
ATTENDEE:MAILTO:maxm@mxm.dk ATTENDEE:MAILTO:maxm@mxm.dk
ATTENDEE:MAILTO:test@example.com ATTENDEE:MAILTO:test@example.com
@ -161,7 +161,7 @@ And then appending it to a "parent"::
Subcomponents are appended to the subcomponents property on the component:: Subcomponents are appended to the subcomponents property on the component::
>>> cal.subcomponents >>> cal.subcomponents
[VEVENT({'DTSTART': '20050404T080000', 'UID': '42'})] [VEVENT({'UID': '42', 'DTSTART': '20050404T080000'})]
Value types Value types
@ -184,7 +184,7 @@ type defined in the spec::
>>> from datetime import datetime >>> from datetime import datetime
>>> cal.add('dtstart', datetime(2005,4,4,8,0,0)) >>> cal.add('dtstart', datetime(2005,4,4,8,0,0))
>>> cal['dtstart'].to_ical() >>> cal['dtstart'].to_ical()
'20050404T080000' b'20050404T080000'
If that doesn't work satisfactorily for some reason, you can also do it If that doesn't work satisfactorily for some reason, you can also do it
manually. manually.
@ -197,7 +197,7 @@ So if you want to do it manually::
>>> from icalendar import vDatetime >>> from icalendar import vDatetime
>>> now = datetime(2005,4,4,8,0,0) >>> now = datetime(2005,4,4,8,0,0)
>>> vDatetime(now).to_ical() >>> vDatetime(now).to_ical()
'20050404T080000' b'20050404T080000'
So the drill is to initialise the object with a python built in type, So the drill is to initialise the object with a python built in type,
and then call the "to_ical()" method on the object. That will return an and then call the "to_ical()" method on the object. That will return an
@ -220,7 +220,7 @@ value directly::
>>> cal = Calendar() >>> cal = Calendar()
>>> cal.add('dtstart', datetime(2005,4,4,8,0,0)) >>> cal.add('dtstart', datetime(2005,4,4,8,0,0))
>>> cal['dtstart'].to_ical() >>> cal['dtstart'].to_ical()
'20050404T080000' b'20050404T080000'
>>> cal.decoded('dtstart') >>> cal.decoded('dtstart')
datetime.datetime(2005, 4, 4, 8, 0) datetime.datetime(2005, 4, 4, 8, 0)
@ -232,12 +232,13 @@ Property parameters are automatically added, depending on the input value. For
example, for date/time related properties, the value type and timezone example, for date/time related properties, the value type and timezone
identifier (if applicable) are automatically added here:: identifier (if applicable) are automatically added here::
>>> import pytz
>>> event = Event() >>> event = Event()
>>> event.add('dtstart', datetime(2010, 10, 10, 10, 0, 0, >>> event.add('dtstart', datetime(2010, 10, 10, 10, 0, 0,
... tzinfo=pytz.timezone("Europe/Vienna"))) ... tzinfo=pytz.timezone("Europe/Vienna")))
>>> lines = event.to_ical().splitlines() >>> lines = event.to_ical().splitlines()
>>> self.assertTrue( >>> assert (
... b"DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20101010T100000" ... b"DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20101010T100000"
... in lines) ... in lines)
@ -249,7 +250,7 @@ dictionary to the add method like so::
>>> event.add('X-TEST-PROP', 'tryout.', >>> event.add('X-TEST-PROP', 'tryout.',
... parameters={'prop1':'val1', 'prop2':'val2'}) ... parameters={'prop1':'val1', 'prop2':'val2'})
>>> lines = event.to_ical().splitlines() >>> lines = event.to_ical().splitlines()
>>> self.assertTrue(b"X-TEST-PROP;PROP1=val1;PROP2=val2:tryout." in lines) >>> assert b"X-TEST-PROP;PROP1=val1;PROP2=val2:tryout." in lines
Example Example
@ -270,7 +271,6 @@ Some properties are required to be compliant::
We need at least one subcomponent for a calendar to be compliant:: We need at least one subcomponent for a calendar to be compliant::
>>> import pytz
>>> event = Event() >>> event = Event()
>>> event.add('summary', 'Python meeting about calendaring') >>> event.add('summary', 'Python meeting about calendaring')
>>> event.add('dtstart', datetime(2005,4,4,8,0,0,tzinfo=pytz.utc)) >>> event.add('dtstart', datetime(2005,4,4,8,0,0,tzinfo=pytz.utc))
@ -314,6 +314,7 @@ Write to disk::
>>> directory = tempfile.mkdtemp() >>> directory = tempfile.mkdtemp()
>>> f = open(os.path.join(directory, 'example.ics'), 'wb') >>> f = open(os.path.join(directory, 'example.ics'), 'wb')
>>> f.write(cal.to_ical()) >>> f.write(cal.to_ical())
570
>>> f.close() >>> f.close()