[Tutor] Python Error Message
Nirel Leitman
leitman375 at yahoo.com
Wed Jan 27 12:39:09 EST 2021
I renamed the folder degrees and inside this folder contains 2 folders: small and large that consist of data. When I go to properties on the degrees.py it shows this: C:\Users\leitm\Desktop\AI\degrees and not C:\Users\leitm\Desktop\AI\degrees\degrees.py but the path to my data does show: C:\Users\leitm\Desktop\AI\degrees\small.Are there any further suggestions on how I can make this work? Thank you again for your time.
And thank you, I didn't know a directory and a folder were the same thing. Thank you for clearing that for me.
I have also adjusted the degrees.py code as I noticed it was incorrect and not finished. Here is the updated version:
import csvimport sys
from util import Node, StackFrontier, QueueFrontier
# Maps names to a set of corresponding person_idsnames = {}
# Maps person_ids to a dictionary of: name, birth, movies (a set of movie_ids)people = {}
# Maps movie_ids to a dictionary of: title, year, stars (a set of person_ids)movies = {}
def load_data(directory): """ Load data from CSV files into memory. """ # Load people with open(f"{directory}/people.csv", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: people[row["id"]] = { "name": row["name"], "birth": row["birth"], "movies": set() } if row["name"].lower() not in names: names[row["name"].lower()] = {row["id"]} else: names[row["name"].lower()].add(row["id"])
# Load movies with open(f"{directory}/movies.csv", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: movies[row["id"]] = { "title": row["title"], "year": row["year"], "stars": set() }
# Load stars with open(f"{directory}/stars.csv", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: try: people[row["person_id"]]["movies"].add(row["movie_id"]) movies[row["movie_id"]]["stars"].add(row["person_id"]) except KeyError: pass
def main(): if len(sys.argv) > 2: sys.exit("Usage: python degrees.py [directory]") directory = sys.argv[1] if len(sys.argv) == 2 else "large"
# Load data from files into memory print("Loading data...") load_data(directory) print("Data loaded.")
source = person_id_for_name(input("Name: ")) if source is None: sys.exit("Person not found.") target = person_id_for_name(input("Name: ")) if target is None: sys.exit("Person not found.")
path = shortest_path(source, target)
if path is None: print("Not connected.") else: degrees = len(path) print(f"{degrees} degrees of separation.") path = [(None, source)] + path for i in range(degrees): person1 = people[path[i][1]]["name"] person2 = people[path[i + 1][1]]["name"] movie = movies[path[i + 1][0]]["title"] print(f"{i + 1}: {person1} and {person2} starred in {movie}")
def shortest_path(source, target): """ Returns the shortest list of (movie_id, person_id) pairs that connect the source to the target.
If no possible path, returns None. """
explored = set([]) frontier = [source] parents = {} while len(frontier) > 0: person = frontier.pop(0) if person == target: break explored.add(person) for (m, p) in neighbors_for_person(person): if not p in frontier and not p in explored: frontier.append(p) parents[p] = (m, person) if not target in parents: return None path = [] person = target while person != source: m, p = parents[person] path.append((m, person)) person = p path = path[::-1] return path
def person_id_for_name(name): """ Returns the IMDB id for a person's name, resolving ambiguities as needed. """ person_ids = list(names.get(name.lower(), set())) if len(person_ids) == 0: return None elif len(person_ids) > 1: print(f"Which '{name}'?") for person_id in person_ids: person = people[person_id] name = person["name"] birth = person["birth"] print(f"ID: {person_id}, Name: {name}, Birth: {birth}") try: person_id = input("Intended Person ID: ") if person_id in person_ids: return person_id except ValueError: pass return None else: return person_ids[0]
def neighbors_for_person(person_id): """ Returns (movie_id, person_id) pairs for people who starred with a given person. """ movie_ids = people[person_id]["movies"] neighbors = set() for movie_id in movie_ids: for person_id in movies[movie_id]["stars"]: neighbors.add((movie_id, person_id)) return neighbors
if __name__ == "__main__": main()
On Wednesday, January 27, 2021, 02:07:47 AM MST, Alan Gauld via Tutor <tutor at python.org> wrote:
On 27/01/2021 03:36, Nirel Leitman via Tutor wrote:
> Thank you. And yes, degrees.py is also a folder name along with small.
In that case you are trying to get Python to execute a folder, which
won't work.
> I was told that file names should have .py at the end of it so
> that Python can recognize it.
Its so that the OS can recognize it as a python file and call
python when you double click on it. Python is happy to execute
the file even without the .py. But it is only files,
not folders, that need the .py extension.
So you need to rename your folder to degrees.
Then inside the degrees folder you need to store your
degrees.py file.
You also need to create another folder inside degrees
called small. I'm not sure what you store in small,
presumably some kind of data file?
Also given your code defaults to large there should
also be a folder called large with another(bigger?)
data file contained in it?
> I went ahead and changed the folder back to just degrees
> before receiving this email.
OK so just to be clear. Your path to your python file is now
C:\Users\leitm\Desktop\AI\degrees\degrees.py
Is that correct?
And the path to your data is:
C:\Users\leitm\Desktop\AI\degrees\small
Is that correct?
> I will try again tomorrow with the forward slashing to see if that helps at all.
I've no idea if that will make a difference,
its been a while since I used Python on windows.
> Also, how to do I create a directory for the small folder?
Now you have confused me!
A directory and a folder are the same thing.
You would normally just use your file manager tool
to create a folder. (You can of course use the
command line or even do it from python, but for
this purpose using file manager is simpler)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list