-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsession-01-part-03.py
More file actions
165 lines (142 loc) · 4.91 KB
/
Copy pathsession-01-part-03.py
File metadata and controls
165 lines (142 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import csv
import os
import shutil
import subprocess
import tempfile
FILE_PATH = "movies.csv"
INCOMPLETE_FILE_PATH = "movies_incomplete.csv"
def ensure_dataset_file(path, repo_id, repo_filename="movies.csv"):
if os.path.exists(path):
return True
try:
subprocess.run(
[
"hf",
"download",
repo_id,
repo_filename,
"--repo-type",
"dataset",
"--local-dir",
".",
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
return False
# If the local target filename differs, keep both files by copying.
if path != repo_filename and os.path.exists(repo_filename):
try:
shutil.copyfile(repo_filename, path)
except Exception:
return False
return os.path.exists(path)
def ensure_incomplete_movies_file(path):
if os.path.exists(path):
return True
try:
# Download to a temporary directory first so we never overwrite root movies.csv.
with tempfile.TemporaryDirectory() as temp_dir:
subprocess.run(
[
"hf",
"download",
"Birkbeck/movies_incomplete",
"movies.csv",
"--repo-type",
"dataset",
"--local-dir",
temp_dir,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
source_path = os.path.join(temp_dir, "movies.csv")
if not os.path.exists(source_path):
return False
shutil.copyfile(source_path, path)
except Exception:
return False
return os.path.exists(path)
if not ensure_dataset_file(FILE_PATH, "Birkbeck/movies") and os.path.exists("movies_complete.csv"):
FILE_PATH = "movies_complete.csv"
if not os.path.exists(FILE_PATH):
raise SystemExit(
"movies.csv not found. Run: "
"hf download Birkbeck/movies movies.csv --repo-type dataset --local-dir ."
)
# Task 1: Print header only
with open(FILE_PATH, "r") as file:
reader = csv.reader(file)
header = next(reader)
print("Task 1 - Header:")
print(header)
# Task 2: Print first 5 rows only (including header)
with open(FILE_PATH, "r") as file:
reader = csv.reader(file)
print("\nTask 2 - First 5 rows:")
for i, row in enumerate(reader):
if i == 5:
break
print(row)
# Task 3: Find first movie where genres contains Action and stop
with open(FILE_PATH, "r") as file:
reader = csv.reader(file)
next(reader) # skip header
print("\nTask 3 - First Action movie:")
for row in reader:
if "Action" in row[4]:
print(row)
break
print("\nTask 4 - Notes:")
print("Benefit: Built-in, simple, and lightweight for line-by-line processing.")
print("Limitation: All values are strings, so numeric conversion and validation are manual.")
print("\nTask 5 - Complexity answers:")
print("Header only: time O(1), space O(1)")
print("First 5 rows only: time O(1), space O(1)")
print("First Action search: time O(n), space O(1)")
# Exercise 2: Find missing record in incomplete dataset
if ensure_incomplete_movies_file(INCOMPLETE_FILE_PATH):
with open(INCOMPLETE_FILE_PATH, "r") as file:
reader = csv.reader(file)
header = next(reader)
print("\nExercise 2 - Missing record location:")
found_missing = False
for row_number, row in enumerate(reader, start=2):
for col_number, value in enumerate(row, start=1):
if value == "":
print(
f"Missing cell at row {row_number}, column {col_number} ({header[col_number - 1]})"
)
found_missing = True
break
if found_missing:
break
if not found_missing:
print("No missing cell found.")
else:
print("\nExercise 2 - movies_incomplete.csv not available.")
print("Run: hf download Birkbeck/movies_incomplete movies.csv --repo-type dataset --local-dir tmp_incomplete")
print("Then copy/rename tmp_incomplete/movies.csv to movies_incomplete.csv")
# Mini project: average rating_imdb
with open(FILE_PATH, "r") as file:
reader = csv.reader(file)
next(reader) # skip header
total = 0.0
count = 0
for row in reader:
try:
rating = float(row[5])
except (ValueError, IndexError):
continue
total += rating
count += 1
print("\nMini Project - Average rating_imdb:")
if count > 0:
print(total / count)
else:
print("No valid rating values found.")
print("Mini project complexity: time O(n), space O(1)")