4a7ac70c1a038c3dd737800524030ed614e3dc97
[pycalendar.git] / pycalendar / dtype / geo.py
1 # Copyright
2
3 """Functions for processing geographic position
4
5 As defined in :RFC:`5545`, section 3.8.1.6 (Geographic Position).
6 """
7
8 from . import base as _base
9
10
11 class Geo (_base.DataType):
12     name = 'GEO'
13
14     @classmethod
15     def decode(cls, property, value):
16         """Parse geographic position
17
18         As defined in :RFC:`5545`, section 3.8.1.6 (Geographic
19         Position).
20
21         >>> Geo.decode(property={}, value='37.386013;-122.082932')
22         (37.386013, -122.082932)
23         """
24         geo = tuple(float(x) for x in value.split(';'))
25         if len(geo) != 2:
26             raise ValueError(value)
27         return geo
28
29     @classmethod
30     def encode(cls, property, value):
31         return '{:.6f};{:.6f}'.format(*value)