I am working on a project for a python class, where we have to create a vin number look up tool. This code below works so far for country and year made. However I am having an issue with how I am indexing the user input.
Sometimes a country is the first two characters, and other times the country code is only the first character. I do not how to get around this problem, I tried using an if else kind of method while iterating through the dictionary, but it did not work.
class vinfo():
def __init__(self):
""" Get user input """
vin = input("Enter Vin: ")
""" Stupidity check """
unaccepted_chars = [
"-","/",".",","," ","?",
"^","$","*","(",")","[",
"]","{","}","#","!","_",
"+","'","=","|","#","<",
">","`","~","&"
]
for char in vin:
if char in unaccepted_chars:
vin = vin.replace(char, "")
else:
print("", end = "")
""" Length check and index """
if len(vin) == 17:
self.country_filt1 = vin[0]
self.country_filt2 = vin[0:2]
self.engine_filt = vin[7]
self.year_filt = vin[9]
self.manufact_filt = vin[1:3]
self.manufact_filt1 = vin[1:4]
else:
print("You must've entered something really stupid.\n(must be 17 characters, letters are uppercase)")
""" Vin Code Data """
# each manufacturer seems to have multiple vins for different car styles
# and for different countries
manufact_codes = {
"1G1":"Chevy",
"":"",
}
year_codes = {
"M":"2021","L":"2020","K":"2019",
"J":"2018","H":"2017","G":"2016",
"F":"2015","E":"2014","D":"2013",
"C":"2012","B":"2011","A":"2010",
"9":"2009","8":"2008","7":"2007",
"6":"2006","5":"2005","4":"2004",
"3":"2003","2":"2002","1":"2001",
"Y":"2000","X":"1999","W":"1998",
"V":"1997","T":"1996","S":"1995",
"R":"1994","P":"1993","N":"1992",
"M":"1991","L":"1990","K":"1989",
"J":"1988","H":"1987","G":"1986",
"F":"1985","E":"1984","D":"1983",
"C":"1982","B":"1981","A":"1980"
}
country_codes = {
"1": "USA",
"4": "USA",
"5": "USA",
"7F": "USA",
"3X": "Mexico",
"37": "Mexico",
"3A": "Canada",
"3W": "Canada",
"W": "Germany",
"SA": "United Kingdom",
"SM": "United Kingdom",
"J": "Japan",
"KL": "Korea",
"KR": "Korea"
}
engine_codes = {
}
""" Define the vehicles attributes using the find() function below """
self.year = self.find(self.year_filt, year_codes)[0]
# self.country = self.find(self.country_filt, country_codes)[0]
# self.manufact = self.find(self.manufact_filt, manufact_codes)
self.engine = self.find(self.engine_filt, engine_codes)
""" Find country (different lengths of country codes) """
for key, value in country_codes.items():
if key == self.country_filt1:
country = value
elif key == self.country_filt2:
country = value
else:
country = "Unsupported code"
""" Same for manufacturer """
for key, value in manufact_codes.items():
if key == self.manufact_filt:
manufact = value
elif key == self.manufact_filt1:
manufact = value
else:
manufact = "Unsupported code"
self.info = print(f"Year: {self.year}\nManufacturer: {manufact}\nCountry: {country}\nEngine: {self.engine}")
""" search through the dictionaries """
def find(self, filt, dict_of_codes):
try:
info = [value for key, value in dict_of_codes.items() if key == filt]
except:
info = "Unsupported"
if len(info) > 1:
info += " (Could be any of these)"
return info
Related
Issue: The for loop for this function is not iterating the over all elements. Its stopping at 1. I used some diagnostic print statements to count the number of loops and its stopping at 1. I have reviewed the indentiation and the loop but cannot seem to find the issue.
def process_data(data):
"""Analyzes the data, looking for maximums.
Returns a list of lines that summarize the information.
"""
loop_count = 0
year_by_sales = dict()
max_revenue = {"revenue": 0}
# ----------->This is where the Loop Issue Exists <-----
for item in data:
item_price = locale.atof(item["price"].strip("$"))
item_revenue = item["total_sales"] * item_price
if item["car"]["car_year"] not in year_by_sales.keys():
year_by_sales[item["car"]["car_year"]] = item["total_sales"]
loop_count += 1
if item_revenue > max_revenue["revenue"]:
item["revenue"] = item_revenue
max_revenue = item
most_sold_model = item['car']['car_model']
highest_total_sales = item["total_sales"]
else:
year_by_sales[item["car"]["car_year"]] += item["total_sales"]
loop_count +=1
most_popular_year = max(year_by_sales, key=year_by_sales.get)
summary = [
"The {} generated the most revenue: ${}".format(
format_car(max_revenue["car"]), max_revenue["revenue"]
),
f"The {most_sold_model} had the most sales: {highest_total_sales}",
f"The most popular year was {most_popular_year} with {highest_total_sales} sales.",
]
print(loop_count)
print(year_by_sales)
return summary
Input Data
[{
"id": 1,
"car": {
"car_make": "Ford",
"car_model": "Club Wagon",
"car_year": 1997
},
"price": "$5179.39",
"total_sales": 446
},
{
"id": 2,
"car": {
"car_make": "Acura",
"car_model": "TL",
"car_year": 2005
},
"price": "$14558.19",
"total_sales": 589
},
{
"id": 3,
"car": {
"car_make": "Volkswagen",
"car_model": "Jetta",
"car_year": 2009
},
"price": "$14879.11",
"total_sales": 825
}]
The entire codebase for this script is https://replit.com/join/dkuzpdujne-terry-brooksjr
Actually problem is that your return statement is inside the for loop so you return after the first iteration itself,
it should run just fine if you move it outside something like below:
def process_data(data):
"""Analyzes the data, looking for maximums.
Returns a list of lines that summarize the information.
"""
loop_count = 0
year_by_sales = dict()
max_revenue = {"revenue": 0}
# ----------->This is where the Loop Issue Exists <-----
for item in data:
item_price = locale.atof(item["price"].strip("$"))
item_revenue = item["total_sales"] * item_price
if item["car"]["car_year"] not in year_by_sales.keys():
year_by_sales[item["car"]["car_year"]] = item["total_sales"]
loop_count += 1
if item_revenue > max_revenue["revenue"]:
item["revenue"] = item_revenue
max_revenue = item
most_sold_model = item['car']['car_model']
highest_total_sales = item["total_sales"]
else:
year_by_sales[item["car"]["car_year"]] += item["total_sales"]
loop_count +=1
most_popular_year = max(year_by_sales, key=year_by_sales.get)
summary = "1"
print(loop_count)
print(year_by_sales)
return summary # move this out of for loop
how can i change the code, to make it more functional (change the for loop for the same result only to make it shorter in 2 sentences)?
def get_language_from_text():
"""
Simply return an array with the predicted languages.
"""
resultSet = []
inputdata = request.data
#print(inputdata)
inputdataparsed = json.loads(request.data)
array_of_sentences = inputdataparsed['inputdata']
for obj_in_array in array_of_sentences:
obj_in_array_tmp = obj_in_array
sentence = obj_in_array['TEXT']
obj_type = obj_in_array['TYPE']
obj_lang = obj_in_array['LANGUAGE']
prediction = detect(sentence)
result_to_safe = {"TEXT":sentence,
"TYPE": obj_type,
"LANGUAGE":obj_lang,
"PREDICTED_LANGUAGE": prediction}
resultSet.append(result_to_safe)
break
print(resultSet)
Your code is fine, it could use a bit of cleaning but that's okay.
You can shorten your loop to:
def make_dict(sentence_dict):
return {
"TEXT": sentence_dict["TEXT"],
"TYPE": sentence_dict["TYPE"],
"LANGUAGE": sentence_dict["LANGUAGE"],
"PREDICTED_LANGUAGE": detect(sentence_dict["TEXT"])
}
result_set = [ make_dict(sentence_dict) for sentence_dict in array_of_sentences ]
You can make this more functional by mapping make_dict over array_of_sentences as follows:
result_set = list(map(make_dict, array_of_sentences))
I have tried using import pandas as pd and am able to produce a csv but it is not how I want it to be. I want it to be saved/printed across the columns rather than down as rows (so job title as the header for column 'A' and all the results listed in the rows etc...
This is my code, what do I need to change to flip the results to appear the other way with all my results.
from requests_html import HTMLSession
import re
import pandas as pd
url = 'https://company.onefootball.com/jobs/#jobs-wrap'
departmentcategories = {
"android": "Software Development",
"social media": "Marketing",
"content ": "Marketing",
"sales": "Sales",
}
languagecategories = {
" and ": "English",
" und ": "German",
}
experiencecategories = {
"senior": "Mid Senior Level",
"Junior": "Entry Level",
}
s = HTMLSession()
r = s.get(url)
r.html.render(sleep=1)
jobs = r.html.xpath('//*[#id="jobs-wrap"]', first=True)
def get_department_categories(department):
depcats = []
for k, v in departmentcategories.items():
if re.search(k, department, re.IGNORECASE):
depcats.append(v)
return depcats
def get_language_categories(language):
langcats = []
for k, v in languagecategories.items():
if re.search(k, language, re.IGNORECASE):
langcats.append(v)
return langcats
def get_experience_categories(experience):
expcats = []
for k, v in experiencecategories.items():
if re.search(k, experience, re.IGNORECASE):
expcats.append(v)
return expcats
for item in jobs.absolute_links:
r = s.get(item)
job_title = r.html.find('h1.headline', first=True).text
city = r.html.find('p.h6', first=True).text
if city == ('Berlin, Germany'):
city = 'Berlin'
country = r.html.find('p.h6', first=True).text
if country == ('Berlin, Germany'):
country = 'Germany'
#Section for the department, languages, and experience level
department = r.html.find('div.job-content--parsed', first=True).text
department_cats = get_department_categories(department)
language = r.html.xpath('//*[#id="job"]/div[1]/div[2]', first=True).text
language_cats = get_language_categories(language)
experience = r.html.find('div.job-content--parsed', first=True).text
experience_cats = get_experience_categories(experience)
joblist = [job_title, city, country, "OneFootball", ", ".join(department_cats), ", ".join(experience_cats), ", ".join(language_cats), "Sport", item]
df = pd.DataFrame(joblist)
print(df.head())
df.to_csv('newtest.csv')
I'm currently working with a JSON file that has a number of theater locations stored in it as well as the code that corresponds to each location.
What I'm currently doing is, if a user enters in a location, I return the respective code.
What I would like to do instead is, I want to split the input string and search for the respective keywords in my JSON file and return the appropriate location matches and their codes.
This is my code so far:
queryname = input("Enter the Venue Name:")
def query():
for d in data['BookMyShow']['arrVenues']:
if d['VenueName'] == queryname:
yield d['VenueCode']
f1 = query()
for f in f1:
print(f)
I'm going to change my input() statement into a input.split() so I can store the individual keywords. However I'm not sure how to change my function so as to search for each of these individual keywords and return the relevant strings.
So, an example would be something like this:
Enter the Venue Name:
Carnival
My file contains 3 instances of Carnival (Carnival Bangalore, Carnival Mumbai and Carnival Delhi)
The output I want it to return is this:
Carnival Bangalore A000
Carnival Mumbai A001
Carnival Delhi A002
This is what a snippet of my data file looks like:
{
"BookMyShow": {
"arrVenues": [
{
"VenueCode": "AAAD",
"VenueName": "Khinvasara Cineplex (Apsara Cinema): Aurangabad",
"VenueSeq": "9999"
},
{
"VenueCode": "AACM",
"VenueName": "Anjana Chitra Mandir: Raniganj",
"VenueSeq": "9999"
},
{
"VenueCode": "AAME",
"VenueName": "Shree Ram Cinema: Deesa",
"VenueSeq": "9999"
},
{
"VenueCode": "AASH",
"VenueName": "Adarsh Chitra Mandir: Bhandara",
"VenueSeq": "9999"
},
{
"VenueCode": "ABCC",
"VenueName": "Abhay Cinema: Chandrapur",
"VenueSeq": "9999"
}]
}
}
You can add another loop and use the in operator:
def query():
keywords = queryname.split(' ')
for d in data['BookMyShow']['arrVenues']:
for keyword in keywords:
if(keyword in d['VenueName']):
yield(d) # you should yield the whole venue because you want to print both the venue code and name later on
break # this is so that we don't yield it twice when multiple keywords match
To print out the venues in the format you showed, you can do this:
f1 = query()
for f in f1:
print(f['VenueName'], f['VenueCode'])
For case-insensitive-ness, just convert both sides to lowercase/uppercase:
if keyword.lower() in d['VenueName'].lower():
If you want to only match venues that contain all keywords use this:
def query():
keywords = queryname.split(' ')
for d in data['BookMyShow']['arrVenues']:
reject = False
for keyword in keywords:
if not (keyword.lower() in d['VenueName'].lower()):
reject = True
break
if not reject:
yield d
I have this JSON file.
{
"reviewers":[
{
"user":{
"name":"keyname",
"emailAddress":"John#email",
"id":3821,
"displayName":"John Doe",
"active":true,
"slug":"jslug",
"type":"NORMAL",
"link":{
"url":"/users/John",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":true
},
{
"user":{
"name":"keyname2",
"emailAddress":"Harry#email",
"id":6306,
"displayName":"Harry Smith",
"active":true,
"slug":"slug2",
"link":{
"type":"NORMAL",
"url":"/users/Harry",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":false
}
],
}
Initially, I was using a snippet of code that would go through and grab the full names of the reviewers.
def get_reviewers(json):
reviewers = ""
for key in json["reviewers"]:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which would return "John Doe, Harry Smith". However, now I'm trying to get it so that the script will return a (A) next to the name of the user if their tag equals true "approved"=true.
So for example the code above would get the names, then see that John's approved tag is true and Harry's is false, then return "John Doe(A), Harry Smith". I'm just not sure where to even begin to do this. Can anyone point me in the right direction?
This is what I've been trying so far but obviously it isn't working as I'd like it to.
def get_reviewers(stash_json):
reviewers = ""
for key in stash_json["reviewers"]:
if stash_json["reviewers"][0]["approved"] == true:
reviewers += key["user"]["displayName"] + "(A)" + ", "
else:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which outputs Jason Healy(A), Joan Reyes(A)
This is what my stash_json outputs when put through pprint.
You probably want something along the lines of this:
def get_reviewers(stash_json):
reviewers = ""
for item in stash_json["reviewers"]:
if item["approved"]:
reviewers += item["user"]["displayName"] + "(A)" + ", "
else:
reviewers += item["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
I think part of your confusion comes from the fact that "reviewers" is a list of dict elements, and each dict element has a key-value approved, but also a key "user" which value itself is another dict.
Read the JSON file carefully, and for debugging purposes, use plenty of
print(...)
print(type(...)) # whether something is a dict, list, str, bool etc
or
from pprint import pprint # pretty printing
pprint(...)
This looks like a good place to use join and list comprehension:
def get_reviewers(stash_json):
return ", ".join([item['user']['displayName'] + ('(A)' if item['approved'] else '') for item in stash_json['reviewers']])