From: Dustin Rodrigues Date: Fri, 17 Feb 2023 19:30:29 +0000 (-0500) Subject: gh-101992: update plistlib examples to be runnable (#101994) X-Git-Tag: v3.12.0a6~140 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a1723caabfcdca5d675c4cb04554fb04c7edf601;p=thirdparty%2FPython%2Fcpython.git gh-101992: update plistlib examples to be runnable (#101994) * gh-101992: update plistlib examples to be runnable * Update Doc/library/plistlib.rst --------- Co-authored-by: Terry Jan Reedy --- diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 5ded9661f080..7aad15ec91a0 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -159,6 +159,9 @@ Examples Generating a plist:: + import datetime + import plistlib + pl = dict( aString = "Doodah", aList = ["A", "B", 12, 32.1, [1, 2, 3]], @@ -172,13 +175,19 @@ Generating a plist:: ), someData = b"", someMoreData = b"" * 10, - aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), + aDate = datetime.datetime.now() ) - with open(fileName, 'wb') as fp: - dump(pl, fp) + print(plistlib.dumps(pl).decode()) Parsing a plist:: - with open(fileName, 'rb') as fp: - pl = load(fp) - print(pl["aKey"]) + import plistlib + + plist = b""" + + foo + bar + + """ + pl = plistlib.loads(plist) + print(pl["foo"])