If anyone can help me to get a value from a json response. I intend to get only the first incidence of id where the type is CLIENTREF.
I am reading a list to get some values to do a GET and check if the response is other than null "".
This is the JSON structure:
This is my code so far
def checkParent(list):
for item in list:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
jsonpath_expression = parse("$..identifier[*]")
# $..identifier[?(#.type == 'CLIENTREF')].id
parentId = []
for match in jsonpath_expression.find(r):
# print(f"match id: {match.value}")
thisdict = match.value
if thisdict["type"] == "CLIENTREF":
# print(thisdict["id"])
parentId.append(thisdict["id"])
elif thisdict["id"] == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)
print(f "match id: {match.value}") I get this, What I need is the first id related to the type: CLIENTREF
The error that appears on my console is this:
Traceback (most recent call last):
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 114, in <module>
checkParent(r_mheu_trade)
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 106, in checkParent
if thisdict["type"] == "CLIENTREF":
KeyError: 'type'
With the help of pyzer I found a solution to my problem. This is how the code looks like:
# Request to get all UltimateParent
def checkParent(murex):
for item in murex:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
clientref = r[0]["riskUltimateParent"]["identifier"][1]
if clientref == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
else:
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)
Related
I have a list (named Responses) which contains some dictionary with the following key-value pair:
Responses = [
{'Id':100,'ResponseText1':'aaaa','ResponseText2':'yyyy'},
{'Id':101,'ResponseText1':'bbbb','ResponseText2': 'zzzz'},
{'Id':103,'ResponseText1':'cccc'},
{'Id':104,'ResponseText2': True},
]
As you can see here Id is a common field for all dictionary, but ResponseText1 and ResponseText2 are unavailable for some dictionary.
I want to separate and detect all the data to push them into my database. My python code:
for j in Responses:
print(j['Id'])
if j['ResponseText1']:
print(j['ResponseText1'])
else:
print("No ResponseText1 is present")
if j['ResponseText2']:
print(j['ResponseText2'])
else:
print("No ResponseText2 is present")
But It says errors like:
Traceback (most recent call last):
File "<string>", line 14, in <module>
KeyError: 'ResponseText2'
Please suggest how can I fix this?
Use in or .get('key', default), not ['key'] access to prevent KeyErrors
example of both
for j in Responses:
print(j['Id'])
text1 = j.get('ResponseText1')
if text1 is not None:
print(text1)
else:
print("No ResponseText1 is present")
if 'ResponseText2' in j:
print(j['ResponseText2'])
else:
print("No ResponseText2 is present")
Im trying to load the data from a list in python where, I save the data in a list:
class Region(object):
def __init__(self, cities: list[CitiySalah], label: str):
self.cities = cities
self.label = label
def toMap(self) -> dict:
map = self.__dict__
r = [{}]
for x in self.cities:
r.append(x.toMap())
map["cities"] = r
return map
and the one that loads the data is:
def startLoading() -> list[Region]:
.......
for select in selects:
if select.has_attr("name") and select['name'] == "ville":
groups = select.find_all('optgroup')
for group in groups:
lable = group['label']
allR = {"lable": lable}
cities = [CitiySalah]
for option in group:
try:
# the city
if(option.has_attr('value')):
value = option['value']
city = str(option).split('<')[1].split('>')[1]
id = str(option).split('<')[1].split(
'?ville=')[1].split('"')[0]
dataUrl = url+"?ville="+id
data = MySalah(getSalahHour(dataUrl))
R = CitiySalah(argu={"value": value,
"city": city,
"dataUrl": dataUrl,
"id": id, }, data=data)
# print(R.toMap())
cities.append(R)
except:
pass
# allR['cities'] = cities
res.append(Region(label=lable, cities=cities))
return res
and when I'm trying to call the function by:
def getDataForDatabase():
listR = [{}]
data = startLoading()
for x in data:
listR.append(x.toMap())
return listR
I get this error
Traceback (most recent call last):
File "/home/nimr/ServerApps/ScrappingProject/Salah/Functions.py", line 108, in <module>
print(getDataForDatabase())
File "/home/nimr/ServerApps/ScrappingProject/Salah/Functions.py", line 104, in getDataForDatabase
listR.append(x.toMap())
TypeError: toMap() missing 1 required positional argument: 'self'
I have tried, I'm still new in python as I'm from c++ family, and I got stuck here,
I need to save the data in a list and convert them into Map, so I can save them in the database (NoSQL).
and as for the rest of models they are working correct, I don't know why I'm getting this error.
I'm getting the following error when executing the following script:
Error Type: <type 'exceptions.TypeError'>
Error Contents: 'NoneType' object is not iterable
Traceback (most recent call last):
File "addon.py", line 75, in <module>
plugin.run()
File "xbmcswift2/plugin.py", line 332, in run
items = self._dispatch(self.request.path)
File "/plugin.py", line 306, in _dispatch
listitems = view_func(**items)
File "/addon.py", line 42, in all_episodes
items = thisiscriminal.compile_playable_podcast(playable_podcast)
File "/lib/thisiscriminal.py", line 121, in compile_playable_podcast
for podcast in playable_podcast:
TypeError: 'NoneType' object is not iterable
The code in question is as follows, any advice would be greatly appreciated as I have no idea what I'm doing wrong:
def get_playable_podcast(soup):
"""
#param: parsed html page
"""
r = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
data = json.loads(r.read().decode('utf-8'))
for post in data['posts']:
print post['title']
print post['episodeNumber']
print post['audioSource']
print post['image']['medium']
subjects = []
item = {
'title': post['title'],
'audioSource': post['audioSource'],
'episodeNumber': post['episodeNumber'],
'medium': post['image']['medium']
}
subjects.append(item)
print subjects
def compile_playable_podcast(playable_podcast):
"""
#para: list containing dict of key/values pairs for playable podcasts
"""
items = []
for podcast in playable_podcast:
items.append({
post['title']: podcast['title']['episodeNumber'],
post['audioSource']: podcast['audioSource'],
post['image']['medium']: podcast['medium'],
'is_playable': True,})
return items
I assume your script does something alike to the following,
podcast = get_playable_podcast(soup)
compiled = compile_playable_podcast(podcast)
The problem is that get_playable_podcast has no return statement. In such a case, Python defaults to returning None - which you then pass into compile_playable_podcast. Since None is not iterable, compile_playable_podcast rightfully raises a TypeError.
Now, the solution is of course to return the podcast list you're building in get_playable_podcast, like so,
def get_playable_podcast(soup):
"""
#param: parsed html page
"""
r = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
data = json.loads(r.read().decode('utf-8'))
subjects = []
for post in data['posts']:
print post['title']
print post['episodeNumber']
print post['audioSource']
print post['image']['medium']
item = {
'title': post['title'],
'audioSource': post['audioSource'],
'episodeNumber': post['episodeNumber'],
'medium': post['image']['medium']
}
subjects.append(item)
print subjects
return subjects
Beside this, it may be worthwhile to carefully check your script for unused parameters and/or duplicate code.
I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:
def isLookingAround(lst):
res = []
body = []
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))
if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'
Error:
Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable
I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.
To check data is empty. you can use below code
if data:
l.append(data)
In order to handle the NoneType exception, you can surround the statement within try-except blocks.
For example, you can do something like,
try:
labels.append(detectors.isLookingAround(back_Data))
except TypeError:
# Do something. Like,
return
It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.
You can re-check that, and if you're unable to resolve it, post a new question, I guess.
I'm trying to follow this guide: https://medium.com/#sarahnadia/how-to-code-a-simple-twitter-bot-for-complete-beginners-36e37231e67d#.k2cljjf0m
This is the error message:
MacBook-Pro-2:heroku_ebooks-master Rupert$ heroku run worker
Running worker on ⬢ desolate-brushlands-56729... up, run.6788
Traceback (most recent call last):
File "ebooks.py", line 79, in <module>
source_tweets_iter, max_id = grab_tweets(api,max_id)
File "ebooks.py", line 51, in grab_tweets
max_id = user_tweets[len(user_tweets)-1].id-1
IndexError: list index out of range
This is the code:
def grab_tweets(api, max_id=None):
source_tweets=[]
user_tweets = api.GetUserTimeline(screen_name=user, count=200, max_id=max_id, include_rts=True, trim_user=True, exclude_replies=True)
max_id = user_tweets[len(user_tweets)-1].id-1
for tweet in user_tweets:
tweet.text = filter_tweet(tweet)
if len(tweet.text) != 0:
source_tweets.append(tweet.text)
return source_tweets, max_id
if __name__=="__main__":
order = ORDER
if DEBUG==False:
guess = random.choice(range(ODDS))
else:
guess = 0
if guess == 0:
if STATIC_TEST==True:
file = TEST_SOURCE
print ">>> Generating from {0}".format(file)
string_list = open(file).readlines()
for item in string_list:
source_tweets = item.split(",")
else:
source_tweets = []
for handle in SOURCE_ACCOUNTS:
user=handle
api=connect()
max_id=None
for x in range(17)[1:]:
source_tweets_iter, max_id = grab_tweets(api,max_id)
source_tweets += source_tweets_iter
The traceback tells us the error is here:
max_id = user_tweets[len(user_tweets)-1].id-1
which will be out of range if user_tweets is an empty list. You can check to make sure it isn't before trying to access its value.
if len(user_tweets) > 0:
max_id = user_tweets[-1].id-1
Note the use of user_tweets[-1]. Using a negative index counts backward from the end, so the last element of a list is always at [-1].