Dictionary in Python 3.6 - python-3.x

I have question. I try to get info from function and pack result into dictionary like this:
import requests
class CommonUtils():
def get_data_list(self, url, listType):
c_list = requests.get(url)
data = {"json": c_list.json(), "text": c_list.text}
return data.get(listType)
I debugged function and I know, that requests retrieves info from passed url.
And when I try to return data, I get error with message: "AttributeError: 'dict' object has no attribute 'json'"
What I do wrong?

Related

AttributeError: 'bytes' object has no attribute 'hexdigest'

I wrote the following code but the problem is that I recieved an error (AttributeError: 'bytes' object has no attribute 'hexdigest')
the error syntax doesn't work
import requests
import hashlib
def request_api_data (query_char):
url = 'https://api.pwnedpasswords.com/range/'+ query_char
res = requests.get(url)
if res.status_code != 200:
print('it is an error')
#raise RuntimeError(f'Error fetching: {res.status_code}, check api and try again')
return res
request_api_data('123')
def pwned_api_check(password):
sha1password= hashlib.sha1(password.encode('utf-8').hexdigest().upper())
print (sha1password)
#return sha1password
pwned_api_check('123')
Why does this error occur and how do I fix it??
You need to add a parenthesis after hashlib.sha1(password.encode('utf-8'), so hexdigest().upper() is called on it.
The following code works for me:
hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
I was taking the same class as you and got the same error. The parenthesis are in the wrong place.
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()

Fetch Keys from Response Dictionary

Sending Client request to get data from API.
request =client.get_income('ET',incomeType='PNL',startTime=1611287550000)
The API returns the following data:
[{"symbol":"ET","incomeType":"R","income":"-2.4","time":1611287909000,"info":"50538270"},{"symbol":"ET","incomeType":"R","income":"1.68","time":1611287909000,"info":"50538271"}]
It's a dictionary inside the list. When I try to access the items through a for loop or any of the following methods, It returns the OBJECT.
Methods I tried:
for item in request:
print(item['income'])
returns this : <model.income.Income object at 0x000000684EFB4580>
print(request[0]['income']
ERROR: TypeError: 'Income' object is not subscriptable
None of them works.
I have fixed it myself.
request =client.get_income('ET',incomeType='PNL',startTime=1611287550000)
for x in range(len(request)):
print(request[x].__dict__['income'])
request = [{"symbol":"ET","incomeType":"R","income":"-2.4","time":1611287909000,"info":"50538270"},{"symbol":"ET","incomeType":"R","income":"1.68","time":1611287909000,"info":"50538271"}]
for item in request:
print(item['income'])
it's working fine on my pc, I'm using python 3.9
-2.4
1.68
The data structure you have is a JSON. Use Pandas to parse it and access it elements:
import pandas as pd
request = [{"symbol":"ET","incomeType":"R","income":"-2.4","time":1611287909000,"info":"50538270"},
{"symbol":"ET","incomeType":"R","income":"1.68","time":1611287909000,"info":"50538271"}]
df = pd.DataFrame(request)
print(df)
print(f'Incomes are: {df["income"].tolist()}')
Output:

how to pass element tree to other function TypeError: 'ElementTree' object is not iterable - - -

I am trying to return the entire XML chunk to the controller in order to make them downloaded as an XML file.
from xml.etree import ElementTree as ET
#api.model
def _xml_download(self):
root = ET.Element("Test A")
doc = ET.Element("Test B")
root.append(doc)
return ET.ElementTree(root)
And here is my controller:
class TestXMl(http.Controller):
#http.route('/xml/download/report', type='http', auth="user")
#serialize_exception
def sale_xml_download(self, model, id, filename=None, **kw):
filename = 'Test%s.xml'%('Test')
records= http.request.env['sale.order'].search([('some_attribute', '=', True)])
if records:
xml_record = http.request.env['sale.order']._xml_download()
else:
xml_record = 'Test'
filename = 'Test%s.xml'%('Test')
headers = [
('Content-Type', 'application/xml'),
('Content-Disposition', content_disposition(filename)),
('charset', 'utf-8'),
]
return request.make_response(
xml_record, headers=headers, cookies=None)
But I xml_record is not getting the element tree. It is saying TypeError: 'ElementTree' object is not iterable - - - In this case how we suppose to pass the elementTree. Looking for useful response, thank you in advance
_xml_download method returns an xml.etree.ElementTree.ElementTree object which is not iterable.
You can find in the documentation of the method request.make_response:
param basestring data: response body
Try to return ET.tostring(root)
Note that passing data as a list of strings should work

how to access the individual elements of a python <class 'requests.models.Response'> class

My code accesses a light sensor via a python request:
address = 'https://api.particle.io/v1/devices/my_device_id/analogvalue'
headers = {'Authorization':'Bearer {0}'.format(access_token)}
vals = requests.get(address, headers=headers)
The code returns the following values:
{"cmd":"VarReturn","name":"analogvalue","result":171,"coreInfo":{"last_app":"","last_heard":"2019-06-13T21:55:57.387Z","connected":true,"last_handshake_at":"2019-06-13T20:51:02.691Z","deviceID":"my_device_id","product_id":6}}
Python tells me that this is a 'requests.models.Response' class and not a dictionary like I thought.
When I try to access the 'result' value, I get error messages. Here are the various ways I have tried along with their error messages.
print(vals[2])
TypeError: 'Response' object does not support indexing
print(vals['result'])
TypeError: 'Response' object is not subscriptable
print(vals[2].json())
TypeError: 'Response' object does not support indexing
print(vals['result'].json())
TypeError: 'Response' object is not subscriptable
I got the last two approaches (.json) from a answer here on stack overflow.
Can anyone tell me how to access this result value or am I going to be forced to use regular expression?
EDIT: With help from Sebastien D I added the following and was able to get the result I was looking for.
import json
new_vals = json.loads(vals.content)
print(new_vals['result'])
Just do :
import json
### your code ###
json.loads(vals.content)

Exception Handling Python TypeError: 'NoneType' object is not callable

I am scraping a list of urls with the same html format. Here is my scraper
import requests, bs4, csv, json
from pprint import pprint
with open('playerslist.json') as data_file:
data = json.load(data_file)
for i in data['player']:
name = i['player_name']
url = 'https://www.capfriendly.com/players/'+name
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text, 'lxml')
table = soup.find(id="cont_x")
with open(name+".csv", "w", newline='') as team_data:
def parse_td(td):
filtered_data = [tag.text for tag in td.find_all('span', recursive=False)
if 'q' not in tag.attrs['class']]
return filtered_data[0] if filtered_data else td.text;
for tr in table('tr', class_=['column_head', 'odd', 'even']):
row = [parse_td(td) for td in tr('td')]
writer = csv.writer(team_data)
writer.writerow(row)
The problem is that some of the url's (https://www.capfriendly.com/players/'+name) pages no longer exist. Which means that when I try to scrape them, I get the following error
for tr in table('tr', class_=['column_head', 'odd', 'even']):
TypeError: 'NoneType' object is not callable
I could make sure that my list of urls are all valid but I have 10000 urls to go through. That is why I am looking for a way to handle the exception, that way if a page no longer exists, it is skipped and the following url gets scraped.
Also, if there is a more efficient way to store the data, please let me know.
Exception handling in python is done with try...except statement.
try:
for tr in table('tr', class_=['column_head', 'odd', 'even']):
row = [parse_td(td) for td in tr('td')]
writer = csv.writer(team_data)
writer.writerow(row)
except TypeError:
pass
This will ignore the TypeError exception raised when table is None because the url doesn't exist anymore.
You can also check table before the loop, but the try... except approach is considered more 'pythonic', since exception handling isn't that much slower than checks in most cases (and for other subjective reasons of course).
I used the following successfully (Python v. 3.9):
try:
<my_code>
except AttributeError:
<my_code>

Resources