code/docs: fix #6, update docs example

This commit is contained in:
Mark Joshwel 2023-06-16 07:53:41 +00:00
parent b13a9bfd14
commit ec8366be91
2 changed files with 104 additions and 58 deletions

View file

@ -20,8 +20,9 @@ $ surplus 9R3J+R9 Singapore
surplus version 1.1.1 surplus version 1.1.1
Thomson Plaza Thomson Plaza
301 Upper Thomson Road, Bishan 301 Upper Thomson Road, Bishan
Sin Ming, Bishan
574408 574408
Singapore Central, Singapore
``` ```
```python ```python
@ -29,7 +30,7 @@ Singapore
>>> Localcode(code="8RPQ+JW", locality="Singapore").full_length() >>> Localcode(code="8RPQ+JW", locality="Singapore").full_length()
(True, '6PH58RPQ+JW') (True, '6PH58RPQ+JW')
>>> surplus("6PH58RPQ+JW") >>> surplus("6PH58RPQ+JW")
(True, 'Caldecott Stn Exit 4\nToa Payoh Link\n298106\nSingapore') (True, 'Caldecott Stn Exit 4\nToa Payoh Link\n298106\nCentral, Singapore')
``` ```
## Installing ## Installing

View file

@ -131,6 +131,13 @@ def surplus(
(True, <str>) - conversion was successful, str is resultant text (True, <str>) - conversion was successful, str is resultant text
(False, <str>) - conversion failed, str is error message (False, <str>) - conversion failed, str is error message
""" """
def _unique(l: list[str]) -> list[str]:
unique: OrderedDict = OrderedDict()
for line in l:
unique.update({line: None})
return list(unique.keys())
_latlong = handle_query(query=query, debug=debug) _latlong = handle_query(query=query, debug=debug)
if _latlong[0] is False: if _latlong[0] is False:
@ -157,22 +164,26 @@ def surplus(
if debug: if debug:
stderr.write(f"debug: {location=}\n") stderr.write(f"debug: {location=}\n")
text: list[str] = [ text: list[str] = _unique(
(
",".join(
[ [
location["address"].get(detail, "") (
", ".join(
[
d
for d in _unique(
[
location["address"].get(detail, None)
for detail in ( for detail in (
"emergency, historic, military, natural, landuse, place, railway," "emergency, historic, military, natural, landuse, place, railway, "
"man_made, aerialway, boundary, amenity, aeroway, club, craft," "man_made, aerialway, boundary, amenity, aeroway, club, craft, "
"leisure, office, mountain_pass, shop, tourism, bridge, tunnel, waterway" "leisure, office, mountain_pass, shop, tourism, bridge, tunnel, waterway"
).split(", ") ).split(", ")
] ]
) )
).strip(","), if d is not None
# location["address"].get("leisure"), ]
# location["address"].get("shop"), )
# location["address"].get("railway"), ).strip(", "),
( (
location["address"].get("building") location["address"].get("building")
if ( if (
@ -187,34 +198,68 @@ def surplus(
+ (" " + location["address"].get("house_name", "")).strip() + (" " + location["address"].get("house_name", "")).strip()
+ " " + " "
+ location["address"].get("road", "") + location["address"].get("road", "")
+ ( # + (
", " + location["address"].get("suburb", "") # ", " + location["address"].get("suburb", "")
# dont repeat if suburb is mentioned in the road itself # # dont repeat if suburb is mentioned in the road itself
# 'Toa Payoh' in 'Lorong 1A Toa Payoh' # # 'Toa Payoh' in 'Lorong 1A Toa Payoh'
if location["address"].get("suburb", "") # if location["address"].get("suburb", "")
not in location["address"].get("road", "") # not in location["address"].get("road", "")
else "" # else None
) # )
).strip(), ).strip(),
( (
",".join( ", ".join(
[
d
for d in _unique(
[ [
location["address"].get(detail, "") location["address"].get(detail, "")
for detail in ( for detail in (
"residential, neighbourhood, allotments, quarter" "residential, neighbourhood, allotments, quarter, "
"city_district, district, borough, suburb, subdivision, "
"municipality, city, town, village"
).split(", ") ).split(", ")
] ]
) )
if all(
[
d != "",
d not in location["address"].get("road", ""),
d
not in [
location["address"].get(detail, "")
for detail in (
"region, state, state_district, county, "
"state, country, continent"
).split(", ")
],
]
)
]
)
).strip(","), ).strip(","),
location["address"].get("postcode"), location["address"].get("postcode"),
location["address"].get("country"), (
", ".join(
[
d
for d in _unique(
[
location["address"].get(detail, None)
for detail in (
"region, state, state_district, county, "
"state, country, continent"
).split(", ")
] ]
)
if d is not None
]
)
),
]
)
unique: OrderedDict = OrderedDict() return True, "\n".join([d for d in text if ((d != None) and d != "")])
for line in text:
unique.update({line: None})
return True, "\n".join([d for d in unique.keys() if ((d != None) and d != "")])
def parse_query( def parse_query(