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.
Related
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)
I want to call df["ID"] in the dataset_csv function and then call the dataset_csv function using dataset = RawToCSV.dataset_csv(input_path). df["ID"] was defined in the raw_file_processing function.
My code raised TypeError: __init__() missing 1 required positional argument: 'df' error.
import re
import pandas as pd
import os
import numpy as np
input_path = "../input_data"
class RawToCSV:
def __init__(self, path_, df):
self.measurement_df = None
self.cls = None
self.path_ = path_
self.df = df
def raw_file_processing(self, path_):
# Open all the subfolders within path
for root, dirs, files in os.walk(path_):
for file in files:
with open(os.path.join(root, file), "r") as data:
self.df = pd.read_csv(data)
# 'Class' refers to the independent variable
cls_info = self.df.iloc[2]
# Dummy-code the classes
cls = pd.get_dummies(cls_info)
# Create the ID series by concatenating columns 1-3
self.df = self.df.assign(
ID=self.df[['cell_id:cell_id', 'region:region', 'tile_num:tile_num']].apply(
lambda row: '_'.join([str(each) for each in row]), axis=1))
self.df = self.df.drop(columns=['cell_id:cell_id', 'region:region', 'tile_num:tile_num'])
# Obtain measurement info
# Normalize data against blank/empty columns
# log-transform the data
for col in self.df[9:]:
if re.findall(r"Blank|Empty", col):
background = col
else:
line = col.readline()
for dat in line:
norm_data = dat / background
self.measurement_df = np.log2(norm_data)
return self.df["ID"], cls, self.measurement_df
def dataset_csv(self):
"""Col 1: ID
Col 2: class
Col 3-n: measurements"""
ids = self.df["ID"]
id_col = ids.to_frame()
cls_col = self.cls.to_frame()
frames = [id_col, cls_col, self.measurement_df]
dataset_df = pd.concat(frames)
data_csv = dataset_df.to_csv("../input_data/dataset.csv")
return data_csv
raw = RawToCSV(input_path)
three_tuple = raw.raw_file_processing(input_path)
dataset = raw.data_csv()
Traceback:
> --------------------------------------------------------------------------- TypeError Traceback (most recent call
> last) /tmp/ipykernel_136/323215226.py in <module>
> ----> 1 raw = RawToCSV(input_path)
> 2 three_tuple = raw.raw_file_processing(input_path)
>
> TypeError: __init__() missing 1 required positional argument: 'df'
In this part of code:
dataset = RawToCSV.dataset_csv(input_path)
You are using the class itself, however you should first instantiate from the class RawToCSV, like this:
rawToCSV = RawTOCSV(input_path)
dataset = rawToCSV.data_csv()
But still you have another mistake ,too. In the constructor of the class , __init__ you've initiated the self.df with self.df, which the latter one hasn't been defined ,yet.
Therefore in this part of code, you'll get another error (AttributeError: 'RawToCSV' object has no attribute 'df'):
def __init__(self, path_):
self.measurement_df = None
self.cls = None
self.path_ = path_
self.df = self.df # <-----
On this line:
dataset = RawToCSV.dataset_csv(input_path)
you're calling dataset_csv as if it were a static method (calling it on the class not an instance). You are passing in input_path, which I assume is a string. Since you're calling the method as if it were static, it is not invisibly adding the actual self value into the call (you have to have an object to even be sent as self).
This means that your one parameter of dataset_csv, which you named self, is receiving the (string) value of input_path.
The error message is telling you that the string input_path has no member .df because it doesn't.
With the way your class and its methods are currently set up, you'll need your entry point code at the bottom to be something like this:
raw = RawToCSV(input_path)
three_tuple = raw.raw_file_processing(input_path)
dataset = raw.dataset_csv()
Though, you may want to restructure your class and its methods
new to flask, i'm not so sure why I am getting this name error: 'Nontype' object has no attribute value 'name'.
(PLease ignore: "It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
")
Here is what it looks like in the console
File "/Users/thomashunt/projects/ct-platform-api/apis/student_api.py", line 448, in put
return StudentService.student_WorkShadow(submission)
File "/Users/thomashunt/projects/ct-platform-api/services/students.py", line 234, in student_WorkShadow
AddressService.set_address_info(submission.student_detail.location_address)
File "/Users/thomashunt/projects/ct-platform-api/services/addresses.py", line 18, in set_address_info
address_description = address.address_description(country.name)
AttributeError: 'NoneType' object has no attribute 'name'
services/students
#staticmethod
def student_WorkShadow(submission: StudentWorkShadowEdit) -> Person:
repo = PersonData()
advisor = repo.find_by_email(submission.advisor_email)
email = submission.email.lower()
student = repo.find_by_email(email)
if not student:
raise RecordNotFoundException('No Record with this email in the database')
if not advisor:
raise RecordNotFoundException('No Record with this advisor email in the database')
# Forced re-write of Address entered by Student
student.student_detail.location_address = \
AddressService.set_address_info(submission.student_detail.location_address)
submission.set_model(student)
files = StudentService.promote_student_files(advisor, submission.file_ids, student.id)
# Forced re-write of Address entered by Student
repo.save(student, advisor.id)
repo.session.commit()
student_statement = 'student workshadow details updated'
reference_fields = [EventItemReferenceField('updated_workshadowDetails', 'Updated workshadow Details'),
EventItemReferenceField('form_action', 'confidential_updated')]
reference_content = [student_statement]
MessagingActivityService.create_student_event_for_action(student.id, None, student,
True,
ActionTypes.Student.value.InternalNote,
student_statement,
reference_fields,
reference_content, files, None,
None, None, True, True)
StudentService.re_index(student)
return student
API Endpoints
#ns.route('/StudentWorkShadow')
class StudentWorkShadowEndpoint(Resource):
#SecurityService.requires_system
#ns.expect(student_workshadow_model, validate=True)
#ns.marshal_with(student_person_model)
def put(self):
logging.info('student workshadow details updated')
submission = StudentWorkShadowEdit.from_dict(request.json)
return StudentService.student_WorkShadow(submission)
services/address
import logging
from models import Address
from resources import AddressEdit
from utility import GoogleUtility
from .data import CountryData
class AddressService:
#staticmethod
def set_address_info(address: Address):
countries = CountryData()
country = countries.load_country(address.country_code)
if address.suburb is not None and address.state is not None:
address.location_description = address.suburb + ', ' + address.state
address_description = address.address_description(country.name)
maps_result = GoogleUtility.resolve_coords(address_description)
try:
first_result = maps_result[0]
print(first_result)
address.latitude = first_result['geometry']['location']['lat']
address.longitude = first_result['geometry']['location']['lng']
address.raw_location = first_result
address.formatted_address = first_result['formatted_address']
except TypeError:
print(maps_result.error)
logging.error(maps_result.error)
except IndexError:
logging.error('No result for address resolution')
return address
#staticmethod
def has_address_changed(old_address: Address, new_address: AddressEdit):
if not old_address and new_address:
return True
return not (old_address.line_1 == new_address.line_1
and old_address.line_2 == new_address.line_2
and old_address.suburb == new_address.suburb
and old_address.postcode == new_address.postcode
and old_address.country_code == new_address.country_code)
country/data outputs:
import json
from resources import Country
class CountryData:
with open('services/data/countries.json') as json_data:
source = json.load(json_data)
countries = [Country.from_dict(l) for l in source]
def load_country(self, country_code: str):
result = None
for country in self.countries:
if country.country_code == country_code:
result = country
return result
def load_state(self, country_code: str, short_title: str):
result = None
country = self.load_country(country_code)
for state in country.states:
if state.short_title == short_title:
result = state
return result
def list_states(self, country_code: str):
return self.load_country(country_code).states
My suspicion is that the value you pass for country_code does not match against any country.country_code attribute.
My advice is to put a debug print line in the method like this:
class CountryData:
...
def load_country(self, country_code: str):
result = None
for country in self.countries:
if country.country_code == country_code:
result = country
print(result, country.country_code) # this line added
return result
...
Doing this, you should be able to see if result is ever set to a value other than None, and you can observe exactly which country code triggers it. Moreover, this will print all available country codes (one per line). If your country_code is not one of these, that is the problem.
I would like to create a program that converts money from one type of currency to another. So far this is my code:
def read_exchange_rates(exchange_file_name):
#reads file with exchange rates formatted like USD,1. Each line is a 3 letter currency code and a float to convert it to USD.
f=open(exchange_file_name,"r")
answer={}
for line in f:
k, v = line.split(",")
answer[k] = float(v)
return answer
f.close()
pass
class Money:
exchange_rates = read_exchange_rates(rate_file)
#calls previously defined function to read file with exchange rates
def __init__ (self, monamount, code):
self.monamount=monamount
self.code=code
def to(self, othercode):
i = self.monamount/self.exchange_rates[self.code]
j = i*self.exchange_rates[self.othercode]
return othercode+str(j)
It should return the converted amount along with it's currency code (othercode) but instead it returns a KeyError. If I type
a=Money(650,'USD')
b=a.to('GBP')
it should return GBP somenumber. This is the error. Thank you!
Traceback (most recent call last):
File "<pyshell#126>", line 1, in <module>
b=a.to('GBP')
File "<pyshell#124>", line 9, in to
i = self.monamount/self.exchange_rates[self.code]
KeyError: 'USD'
Are you sure your file contains 'USD' key?
Here is a slightly modified and simplified code (so I didn't have to create an exchange file):
def read_exchange_rates():
answer={}
answer['USD'] = 1
answer['GBP'] = 0.76
return answer
class Money:
exchange_rates = read_exchange_rates()
def __init__ (self, monamount, code):
self.monamount = monamount
self.code = code
def to(self, othercode):
i = self.monamount/self.exchange_rates[self.code]
j = i*self.exchange_rates[othercode]
return othercode + str(j)
a = Money(650,'USD')
b = a.to('GBP')
print(b)
This prints out GBP494.0.
I am trying to read key-value pairs from an already existing shelf to create a new class object with a updated field and write that class object to a new shelf.
My class object : SongDetails
This is the procedure which fails:
def updateShelfWithTabBody(shelfFileName, newShelfFileName):
"""this function updates songDetails with
html body i.e. just the part that contains lyrics and
chords in the tab """
#read all songDetails
shelf = shelve.open(shelfFileName)
listOfKeys = shelf.keys()
#create new songDetails object
temporaryShelfObject = SongDetails.SongDetails()
#iterate over list of keys
for key in listOfKeys:
#print "name:"+shelf[key].name
#fill details from temporaryShelfObject
temporaryShelfObject.name = shelf[key].name
temporaryShelfObject.tabHtmlPageContent = shelf[key].tabHtmlPageContent
#add new detail information
htmlPageContent = shelf[key].tabHtmlPageContent
temporaryShelfObject.htmlBodyContent = extractDataFromDocument.fetchTabBody(htmlPageContent)
#write SongDetails back to shelf
writeSongDetails.writeSongDetails(temporaryShelfObject, newShelfFileName)
Definitions for functions used in above code:
def fetchTabBody(page_contents):
soup = BeautifulSoup(page_contents)
HtmlBody = ""
try:
#The lyrics and chords of song are contained in div with id = "cont"
#Note: This assumtption is specific to ultimate-guitar.com
HtmlBody = soup.html.body.find("div",{"id":"cont"})
except:
print "Error: ",sys.exc_info()[0]
return HtmlBody
def writeSongDetails(songDetails, shelfFileName):
shelf = shelve.open(shelfFileName)
songDetails.name = str(songDetails.name).strip(' ')
shelf[songDetails.name] = songDetails
shelf.close()
SongDetails class:
class SongDetails:
name = ""
tabHtmlPageContent = ""
genre = ""
year = ""
artist = ""
chordsAndLyrics = ""
htmlBodyContent = ""
scale = ""
chordsUsed = []
This is the error that I get:
Traceback (most recent call last):
File "/l/nx/user/ndhande/Independent_Study_Project_Git/Crawler/updateSongDetailsShelfWithNewAttributes.py", line 69, in <module>
updateShelfWithTabBody(shelfFileName, newShelfFileName)
File "/l/nx/user/ndhande/Independent_Study_Project_Git/Crawler/updateSongDetailsShelfWithNewAttributes.py", line 38, in updateShelfWithTabBody
writeSongDetails.writeSongDetails(temporaryShelfObject, newShelfFileName)
File "/home/nx/user/ndhande/Independent_Study_Project_Git/Crawler/writeSongDetails.py", line 7, in writeSongDetails
shelf[songDetails.name] = songDetails
File "/usr/lib64/python2.6/shelve.py", line 132, in __setitem__
p.dump(value)
File "/usr/lib64/python2.6/copy_reg.py", line 71, in _reduce_ex
state = base(self)
File "/u/ndhande/.local/lib/python2.6/site-packages/BeautifulSoup.py", line 476, in __unicode__
return str(self).decode(DEFAULT_OUTPUT_ENCODING)
**RuntimeError: maximum recursion depth exceeded**
I couldn't find any reason why I'm getting this error even though there is no explicit recursive call in my code. I have seen this error in other stackoverflow posts, but they did have recursive calls in their case.
str(self) calls __str__ or calls __unicode__ calls str(self).