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,64 +164,102 @@ 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( (
[ ", ".join(
location["address"].get(detail, "") [
for detail in ( d
"emergency, historic, military, natural, landuse, place, railway," for d in _unique(
"man_made, aerialway, boundary, amenity, aeroway, club, craft," [
"leisure, office, mountain_pass, shop, tourism, bridge, tunnel, waterway" location["address"].get(detail, None)
).split(", ") for detail in (
] "emergency, historic, military, natural, landuse, place, railway, "
) "man_made, aerialway, boundary, amenity, aeroway, club, craft, "
).strip(","), "leisure, office, mountain_pass, shop, tourism, bridge, tunnel, waterway"
# location["address"].get("leisure"), ).split(", ")
# location["address"].get("shop"), ]
# location["address"].get("railway"), )
( if d is not None
location["address"].get("building") ]
if ( )
).strip(", "),
(
location["address"].get("building") location["address"].get("building")
!= location["address"].get("house_number") if (
) location["address"].get("building")
else None != location["address"].get("house_number")
), )
location["address"].get("highway"), else None
( ),
location["address"].get("house_number", "") location["address"].get("highway"),
+ (" " + location["address"].get("house_name", "")).strip() (
+ " " location["address"].get("house_number", "")
+ location["address"].get("road", "") + (" " + location["address"].get("house_name", "")).strip()
+ ( + " "
", " + location["address"].get("suburb", "") + location["address"].get("road", "")
# dont repeat if suburb is mentioned in the road itself # + (
# 'Toa Payoh' in 'Lorong 1A Toa Payoh' # ", " + location["address"].get("suburb", "")
if location["address"].get("suburb", "") # # dont repeat if suburb is mentioned in the road itself
not in location["address"].get("road", "") # # 'Toa Payoh' in 'Lorong 1A Toa Payoh'
else "" # if location["address"].get("suburb", "")
) # not in location["address"].get("road", "")
).strip(), # else None
( # )
",".join( ).strip(),
[ (
location["address"].get(detail, "") ", ".join(
for detail in ( [
"residential, neighbourhood, allotments, quarter" d
).split(", ") for d in _unique(
] [
) location["address"].get(detail, "")
).strip(","), for detail in (
location["address"].get("postcode"), "residential, neighbourhood, allotments, quarter, "
location["address"].get("country"), "city_district, district, borough, suburb, subdivision, "
] "municipality, city, town, village"
).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(","),
location["address"].get("postcode"),
(
", ".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(