geolucidate.functions – Functions

geolucidate.functions.get_replacements(string, sub_function=<function func at 0x103a62c80>)[source]

Return a dict whose keys are instances of re.MatchObject and whose values are the corresponding replacements. Use get_replacements() when the replacement cannot be performed through ordinary string substitution by re.sub(), as in replace().

>>> get_replacements("4630 NORTH 5705 WEST 58147N/07720W")
... 
{<_sre.SRE_Match object at ...>: u'<a href="..." title="...">4630 NORTH 5705 WEST</a>', <_sre.SRE_Match object at ...>: u'<a href="..." title="...">58147N/07720W</a>'}
>>> string = "4630 NORTH 5705 WEST 58147N/07720W"
>>> replacements = get_replacements(string)
>>> offset = 0
>>> from UserString import MutableString
>>> out = MutableString(string)
>>> for (match, link) in replacements.iteritems():
...     start = match.start() + offset
...     end = match.end() + offset
...     out[start:end] = link
...     offset += (len(link) - len(match.group()))
>>> out == replace(string)
True
geolucidate.functions.replace(string, sub_function=<function func at 0x10344e2a8>)[source]

Replace detected coordinates with a map link, using the given substitution function.

The substitution function will be passed a MapLink instance, and should return a string which will be substituted by re.sub() in place of the detected coordinates.

>>> replace("58147N/07720W")
u'<a href="http://maps.google.com/maps?q=58.235278%2C-77.333333+%2858147N%2F07720W%29&ll=58.235278%2C-77.333333&t=h" title="58147N/07720W (58.235278, -77.333333)">58147N/07720W</a>'
>>> replace("5814N/07720W", google_maps_link('satellite'))
u'<a href="http://maps.google.com/maps?q=58.233%2C-77.333+%285814N%2F07720W%29&ll=58.233%2C-77.333&t=k" title="5814N/07720W (58.233, -77.333)">5814N/07720W</a>'
>>> from geolucidate.links.bing import bing_maps_link
>>> replace("58N/077W", bing_maps_link('map'))
u'<a href="http://bing.com/maps/default.aspx?style=r&cp=58%7E-77&sp=Point.58_-77_58N%2F077W&v=2" title="58N/077W (58, -77)">58N/077W</a>'

Previous topic

geolucidate – Imports

Next topic

geolucidate.parser – Regular expression for parsing coordinates

This Page