Skip to content

Commit f1b0441

Browse files
committed
rework timezone example. closes irmen#27
1 parent 528a91a commit f1b0441

File tree

5 files changed

+49
-112
lines changed

5 files changed

+49
-112
lines changed

Pyro5/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]).
55
"""
66

7-
__version__ = "5.10"
7+
__version__ = "5.11"
88
__author__ = "Irmen de Jong"
99

1010

docs/source/changelog.rst

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Change Log
33
**********
44

5+
**Pyro 5.11**
6+
7+
- reworked the timezones example. (it didn't work as intended)
8+
- httpgateway message data bytearray type fix
9+
- fixed ipv6 error in filetransfer example
10+
11+
12+
513
**Pyro 5.10**
614

715
- finally ported over the unit test suite from Pyro4

examples/timezones/Readme.txt

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
This example exercises the support for pytz/dateutil timezones in the serializers.
2-
You'll see that this is quite problematic as the tz info is not really a standard
3-
extension, or some serializers may fail to serialize a datetime object at all.
4-
5-
The only solution is to convert back to a datetime without tz info (so perhaps
6-
normalize them all to UTC) before transferring, or avoiding datetime objects entirely...
7-
8-
91
PREREQUISITES:
102
install the 'pytz', 'python-dateutil' and 'pendulum' libraries
3+
4+
This example shows how datetime and timezones could be handled.
5+
The default serpent serializer will serialize them as a string in ISO date/time format.
6+
You will have to either parse the string yourself,
7+
or perhaps use a custom serializer/deserializer (not shown in this example).

examples/timezones/client.py

+27-97
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,35 @@
1+
import pendulum
12
import datetime
2-
import traceback
3-
from Pyro5.api import Proxy, config
3+
from Pyro5.api import Proxy
44

55

66
uri = input("What is the server uri? ").strip()
77
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
88
print("local time without timezone: ", datetime.datetime.now().strftime(fmt))
99

1010

11-
def test():
12-
with Proxy(uri) as serv:
13-
print("\nFIRST: no timezone")
14-
try:
15-
date1 = serv.echo(datetime.datetime.now())
16-
print("Got from server:", date1)
17-
print("{0}\n {1} ({2})".format(date1, repr(date1), type(date1)))
18-
if isinstance(date1, datetime.datetime):
19-
if hasattr(date1, "tzinfo"):
20-
print(" tzinfo =", date1.tzinfo)
21-
else:
22-
print(" no tzinfo attribute")
23-
except Exception:
24-
print("ERROR!")
25-
traceback.print_exc()
26-
27-
print("\nSECOND: PyTz timezones")
28-
try:
29-
date1 = serv.pytz()
30-
print("Got from server:", date1)
31-
if isinstance(date1, datetime.datetime):
32-
assert isinstance(date1.tzinfo, datetime.tzinfo)
33-
print("{0}\n {1} ({2})\n {3}".format(date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt)))
34-
date2 = serv.echo(date1)
35-
print("{0}\n {1} ({2})\n {3}".format(date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt)))
36-
assert date1 == date2
37-
except Exception:
38-
print("ERROR!")
39-
traceback.print_exc()
40-
41-
print("\nTHIRD: DateUtil timezones")
42-
try:
43-
date1 = serv.dateutil()
44-
print("Got from server:", date1)
45-
if isinstance(date1, datetime.datetime):
46-
assert isinstance(date1.tzinfo, datetime.tzinfo)
47-
print("{0}\n {1} ({2})\n {3}".format(date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt)))
48-
date2 = serv.echo(date1)
49-
print("{0}\n {1} ({2})\n {3}".format(date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt)))
50-
assert date1 == date2
51-
except Exception:
52-
print("ERROR!")
53-
traceback.print_exc()
54-
55-
print("\nFOURTH: Pendulum timezones")
56-
try:
57-
date1 = serv.pendulum()
58-
print("Got from server:", date1)
59-
if isinstance(date1, datetime.datetime):
60-
assert isinstance(date1.tzinfo, datetime.tzinfo)
61-
print("{0}\n {1} ({2})\n {3}".format(date1, date1.tzinfo, type(date1.tzinfo), date1.strftime(fmt)))
62-
date2 = serv.echo(date1)
63-
print("{0}\n {1} ({2})\n {3}".format(date2, date2.tzinfo, type(date2.tzinfo), date2.strftime(fmt)))
64-
assert date1 == date2
65-
except Exception:
66-
print("ERROR!")
67-
traceback.print_exc()
68-
69-
70-
# serpent.
71-
print("\n******* serpent *******")
72-
config.SERIALIZER = "serpent"
73-
try:
74-
test()
75-
except Exception:
76-
import traceback
77-
traceback.print_exc()
78-
79-
# json.
80-
print("\n******* json *******")
81-
config.SERIALIZER = "json"
82-
try:
83-
test()
84-
except Exception:
85-
import traceback
86-
traceback.print_exc()
87-
88-
# msgpack.
89-
print("\n******* msgpack *******")
90-
config.SERIALIZER = "msgpack"
91-
try:
92-
test()
93-
except Exception:
94-
import traceback
95-
traceback.print_exc()
96-
97-
# marshal.
98-
print("\n******* marshal *******")
99-
config.SERIALIZER = "marshal"
100-
try:
101-
test()
102-
except Exception:
103-
import traceback
104-
traceback.print_exc()
105-
11+
with Proxy(uri) as serv:
12+
print("\n1. no timezone")
13+
datestr = serv.echo(datetime.datetime.now())
14+
print("Got from server:", repr(datestr))
15+
dt = pendulum.parse(datestr)
16+
print(" parsed:", repr(dt))
17+
18+
print("\n2. PyTz timezones")
19+
datestr = serv.pytz()
20+
print("Got from server:", repr(datestr))
21+
dt = pendulum.parse(datestr)
22+
print(" parsed:", repr(dt))
23+
24+
print("\n3. DateUtil timezones")
25+
datestr = serv.dateutil()
26+
print("Got from server:", repr(datestr))
27+
dt = pendulum.parse(datestr)
28+
print(" parsed:", repr(dt))
29+
30+
print("\n4. Pendulum timezones")
31+
datestr = serv.pendulum()
32+
print("Got from server:", repr(datestr))
33+
dt = pendulum.parse(datestr)
34+
print(" parsed:", repr(dt))
35+
print()

examples/timezones/server.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@
1111
@expose
1212
class Server(object):
1313
def echo(self, date):
14-
print("ECHO:")
15-
print(" [raw] ", repr(date))
16-
if hasattr(date, "isoformat"):
17-
print(" [iso] ", date.isoformat())
14+
print("RETURNING:", repr(date))
1815
return date
1916

2017
def pytz(self):
2118
tz_nl = pytz.timezone("Europe/Amsterdam")
22-
return tz_nl.localize(datetime.datetime.now())
19+
result = tz_nl.localize(datetime.datetime.now())
20+
print("RETURNING:", repr(result))
21+
return result
2322

2423
def dateutil(self):
2524
tz_nl = dateutil.tz.gettz("Europe/Amsterdam")
26-
return datetime.datetime.now(tz_nl)
25+
result = datetime.datetime.now(tz_nl)
26+
print("RETURNING:", repr(result))
27+
return result
2728

2829
def pendulum(self):
2930
tz_nl = pendulum.now("Europe/Amsterdam")
31+
print("RETURNING:", repr(tz_nl))
3032
return tz_nl
3133

3234

0 commit comments

Comments
 (0)