OSMNX Looping with geocoder - python-3.x

What would be the best way to loop through a list of addresses using geocoder.geocode if there are some locations that don't exist on the map? How could I skip them so that the loop continues without this exception? Exception: Nominatim geocoder returned no results for query "Beli manastir planina,Bakar,Croatia"
Below is what I have tried.
L = [location1, location2, location3, ..., location n]
KO = []
for l in L:
KO = list(ox.geocoder.geocode(l))
if Exception:
continue
KO.append(KO)
Also I tried this:
try:
KO = []
for l in L:
KO = list(ox.geocoder.geocode(l))
KO.append(KO)
except Exception:
pass
Any help is appreciated.

Nest your for loop and try/except differently. Here's a minimal reproducible example:
import osmnx as ox
ox.config(log_console=True, use_cache=True)
locations= ['Pazinska,Zagreb,Croatia',
'Pulska, Pazin,Croatia',
'Zadarska,Zagreb,Croatia',
'Pazinska,Pula,Croatia']
coords = []
for location in locations:
try:
coords.append(ox.geocoder.geocode(location))
except Exception as e:
print(e)

Related

How to convert a list with to wildcards into a list of lists using python?

I obtained a list of all files in a folder using glob:
lista = glob.glob("*.h5")
The list basically contains files with names like:
abc_000000000_000.h5
abc_000000000_001.h5
abc_000000000_002.h5
......
abc_000000000_011.h5
......
abc_000000001_000.h5
abc_000000001_001.h5
abc_000000001_002.h5
....
abc_000000026_000.h5
abc_000000026_001.h5
....
abc_000000027_000.h5
....
abc_000000027_011.h5
which has a format abc_0*_0*.h5. How do I reshape this into a list of lists? The inner list would be ['abc_000000027_0*.h5'] and the outer list would be the sequence of the 'abc_000000*' i.e first wildcard.
One way to create an input would be:
lista=[]
for i in range(115):
for j in range(14):
item="abc_%0.9d_%0.3d"%(i,j)
lista.append(item)
My attempt: my solution is not nice and ugly.
listb = glob.glob("*_011.h5")
then for each item in listb split and glob again, for example
listc = glob.glob("abc_000000027*.h5")
Given:
ls -1
abc_00000001_1.h5
abc_00000001_2.h5
abc_00000001_3.h5
abc_00000002_1.h5
abc_00000002_2.h5
abc_00000002_3.h5
abc_00000003_1.h5
abc_00000003_2.h5
abc_00000003_3.h5
You can use pathlib, itertools.groupby and natural sorting to achieve this:
from pathlib import Path
from itertools import groupby
import re
p=Path('/tmp/t2')
def _k(s):
s=str(s)
try:
return tuple(map(int, re.search(r'_(\d+)_(\d*)', s).groups()))
except ValueError:
return (0,0)
def k1(s):
return _k(s)
def k2(s):
return _k(s)[0]
result=[]
files=sorted(p.glob('abc_000000*.h5'), key=k1)
for k,g in groupby(files, key=k2):
result.append(list(map(str, g)))
Which could be simplified to:
def _k(p):
try:
return tuple(map(int, p.stem.split('_')[-2:]))
except ValueError:
return (0,0)
files=sorted(p.glob('abc_000000*_*.h5'), key=lambda e: _k(e))
result=[list(map(str, g)) for k,g in groupby(files, key=lambda e: _k(e)[0])]
Result (in either case):
>>> result
[['/tmp/t2/abc_00000001_1.h5', '/tmp/t2/abc_00000001_2.h5', '/tmp/t2/abc_00000001_3.h5'], ['/tmp/t2/abc_00000002_1.h5', '/tmp/t2/abc_00000002_2.h5', '/tmp/t2/abc_00000002_3.h5'], ['/tmp/t2/abc_00000003_1.h5', '/tmp/t2/abc_00000003_2.h5', '/tmp/t2/abc_00000003_3.h5']]
Which easily could be a dict:
>>> {k:list(map(str, g)) for k,g in groupby(files, key=k2)}
{1: ['/tmp/t2/abc_00000001_1.h5', '/tmp/t2/abc_00000001_2.h5', '/tmp/t2/abc_00000001_3.h5'],
2: ['/tmp/t2/abc_00000002_1.h5', '/tmp/t2/abc_00000002_2.h5', '/tmp/t2/abc_00000002_3.h5'],
3: ['/tmp/t2/abc_00000003_1.h5', '/tmp/t2/abc_00000003_2.h5', '/tmp/t2/abc_00000003_3.h5']}

Why I can not extract data from this dictionary though a loop?

I'm extracting from data which is of type dictionary.
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.loads(f.data.decode('utf-8'))
data[0]["geometry"]["coordinates"]
geo = []
n = len(data)
for i in range(n):
geo.append(data[i]["geometry"]["coordinates"])
It returns an error
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-26-52e67ffdcaa6> in <module>
12 n = len(data)
13 for i in range(n):
---> 14 geo.append(data[i]["geometry"]["coordinates"])
KeyError: 'geometry'
This is weird, because, when I only run data[0]["geometry"]["coordinates"], it returns [7.000275, 43.58554] without error.
Could you please elaborate on this issue?
Error is occuring because in few of the response dictionaries you don't habe "geometry" key.
Check before appending to geo list, that "geometry" key exists in response dict.
Try following code.
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/BigData/main/fr-esr-principaux-etablissements-enseignement-superieur.json'
f = http.request('GET', url)
data = json.loads(f.data.decode('utf-8'))
geo = []
n = len(data)
for i in range(n):
if "geometry" in data[i]:
geo.append(data[i]["geometry"]["coordinates"])
print(geo)
I believe the problem is that there are places in your data which do not have a "geography" key. As a preliminary matter, your data structure is not technically a dictionary. It is a 'list' of 'dictionaries'. You can tell that by using the print(type(data)) and print(type(data[0]) commands.
I took your code but added the following lines:
dataStructure = data[0]
print(type(dataStructure))
geo = []
n = len(data)
for i in range(321):
try:
geo.append(data[i]["geometry"]["coordinates"])
except:
print(i)
If you run this, you will see that at index positions 64 and 130, there is no geometry key. You may want to explore those entries specifically and see whether they should be removed from your data or whether you just need to alter the keyword to something else for those lines.

Trouble printing out dictionary.meaning() output using PyDictionary

I'm trying to make a program that will grab a random word from a JSON file and print it and it's definition using PyDictionary. It works occasionally but I think the issue I am having is displaying output from dictionary.meaning(word) when the word has multiple meanings. I get an IndexError when that appears the be the case.
example outputs: expected: tinamidae Noun ['comprising the
tinamous']
unwanted result: unmaterially Error: The Following Error occured: list
index out of range No definition found!
import json
import random
from PyDictionary import PyDictionary
dictionary = PyDictionary()
with open('C:\\Users\\jabes\\Desktop\\words_dictionary.json') as json_file:
words = json.load(json_file)
word = random.choice(list(words.keys()))
print(word)
try:
meanings = dictionary.meaning(word)
if meanings:
for k,v in meanings.items():
print(k, v)
else:
print("No definition found!")
except Exception as error:
print(error)
print("Exiting!")

Exception Handling Error: TypeError: can only concatenate list (not "odict_keys") to list

I was running pytest and using someone else's exception handling library. It supposed to run older version of python, not sure which one. However when I try to run it with python3, it spouted error that I didn't understand, also for some reason I have trouble finding the meaning of the keyword error (odict_keys) in the web.
The following was the result from the pytest. The exception handling inside test_analysis procedure was calling run_with_timeout(timeoutwrapper_analysis,max_seconds_per_call,(),{}) before the error occurred here. Inside run_with_timeout, the error happened when it raised e as an exception:
#pytest.mark.parametrize("inputs,outputs,description", portfolio_test_cases)
def test_analysis(inputs, outputs, description, grader):
"""Test get_portfolio_value() and get_portfolio_stats() return correct values.
Requires test inputs, expected outputs, description, and a grader fixture.
"""
points_earned = 0.0 # initialize points for this test case
try:
# Try to import student code (only once)
if not main_code in globals():
import importlib
# * Import module
mod = importlib.import_module(main_code)
globals()[main_code] = mod
# Unpack test case
start_date_str = inputs['start_date'].split('-')
start_date = datetime.datetime(int(start_date_str[0]),int(start_date_str[1]),int(start_date_str[2]))
end_date_str = inputs['end_date'].split('-')
end_date = datetime.datetime(int(end_date_str[0]),int(end_date_str[1]),int(end_date_str[2]))
symbols = inputs['symbol_allocs'].keys() # e.g.: ['GOOG', 'AAPL', 'GLD', 'XOM']
allocs = inputs['symbol_allocs'].values() # e.g.: [0.2, 0.3, 0.4, 0.1]
start_val = inputs['start_val']
risk_free_rate = inputs.get('risk_free_rate',0.0)
# the wonky unpacking here is so that we only pull out the values we say we'll test.
def timeoutwrapper_analysis():
student_rv = analysis.assess_portfolio(\
sd=start_date, ed=end_date,\
syms=symbols,\
allocs=allocs,\
sv=start_val, rfr=risk_free_rate, sf=252.0, \
gen_plot=False)
return student_rv
# Error happen in the following line:
result = run_with_timeout(timeoutwrapper_analysis,max_seconds_per_call,(),{})
grade_analysis.py:176:
func = .timeoutwrapper_analysis at 0x7f8c458347b8>, timeout_seconds = 5, pos_args = (), keyword_args = {}
def run_with_timeout(func,timeout_seconds,pos_args,keyword_args):
rv_dict = timeout_manager.dict()
p = multiprocessing.Process(target=proc_wrapper,args=(func,rv_dict,pos_args,keyword_args))
p.start()
p.join(timeout_seconds)
if p.is_alive():
p.terminate()
raise TimeoutException("Exceeded time limit!")
if not('output' in rv_dict):
if 'exception' in rv_dict:
e = rv_dict['exception']
e.grading_traceback=None
if 'traceback' in rv_dict:
e.grading_traceback = rv_dict['traceback']
# Error occurred after the following line:
raise e
E TypeError: can only concatenate list (not "odict_keys") to list
grading.py:134: TypeError
Looks the script didn't like
raise e
statement. What is that to do with odict_keys?
regards
two of the inputs of analysis.assess_portfolio, symbols and allocs are in the forms of odic_keys. Apparently, this worked in python 2.7, however when running with python 3, they need to be in the form of list, thus changing the input statement from
symbols = inputs['symbol_allocs'].keys()
allocs = inputs['symbol_allocs'].values()
to
symbols = list(inputs['symbol_allocs'].keys())
allocs = list(inputs['symbol_allocs'].values())
fixed it

Python, removing elements under nested loops from a list

I have written a code to get prime numbers upto a certain limit in a list.
As shown above.
import math
primes = []
for i in range(1, 101):
primes.append(i)
primes.remove(10) # Just removing for sake of experiment
tot = math.sqrt(len(primes))
for j in range(2, math.ceil(tot), 1):
for l in range(0, len(primes)):
k = j**2 + l*j
primes.remove(k)
primes.remove(12) # Just removing for sake of experiment
print(primes)
This code is showing error while when it removes elements from nested loop.
Error is shown above.
Traceback (most recent call last):
File "/root/PycharmProjects/love/love.py", line 13, in <module>
primes.remove(k)
ValueError: list.remove(x): x not in list
Why is this happening as this code was able to remove element which is not under nested loop but was unable to remove element which is being removed under nested loops.
Is there any alternate solution to this problem?
You are iterating over a list while you are editing a list, which is something you should never do! When you iterate the list here:
for l in range(0, len(primes)):
You are actually changing the value of len(primes) when you remove the primes! So this causes the code to act irregularly, as:
In the list comprehension, the original list is left intact, instead a new one is created. (SOURCE)
Instead, you can use list comprehension to achieve the same result!
import math
primes = []
for i in range(1, 101):
primes.append(i)
primeslst = []
def isPrime(number):
for i in range(2,int(number/2)+1):
if number%i == 0:
return True
return False
primes = [p for p in primes if not isPrime(p)]
print(primes)
Hope it helps!

Resources