-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
1172 lines (1014 loc) · 40.6 KB
/
utils.py
File metadata and controls
1172 lines (1014 loc) · 40.6 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import random
from datetime import datetime
# File paths
RESOURCES_FILE = "data/resources.json"
PROJECTS_FILE = "data/projects.json"
USER_PROGRESS_FILE = "data/user_progress.json"
# Ensure data directory exists
os.makedirs("data", exist_ok=True)
def load_resources():
"""Load resources from JSON file or create if not exists"""
if not os.path.exists(RESOURCES_FILE):
# Create default resources structure
resources = generate_default_resources()
save_resources(resources)
try:
with open(RESOURCES_FILE, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading resources: {e}")
return []
def save_resources(resources):
"""Save resources to JSON file"""
try:
with open(RESOURCES_FILE, 'w') as f:
json.dump(resources, f, indent=4)
except Exception as e:
print(f"Error saving resources: {e}")
def load_projects():
"""Load projects from JSON file or create if not exists"""
if not os.path.exists(PROJECTS_FILE):
# Create default projects structure
projects = generate_default_projects()
save_projects(projects)
try:
with open(PROJECTS_FILE, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading projects: {e}")
return []
def save_projects(projects):
"""Save projects to JSON file"""
try:
with open(PROJECTS_FILE, 'w') as f:
json.dump(projects, f, indent=4)
except Exception as e:
print(f"Error saving projects: {e}")
def load_user_progress(username):
"""Load user progress from JSON file"""
if not os.path.exists(USER_PROGRESS_FILE):
with open(USER_PROGRESS_FILE, 'w') as f:
json.dump({}, f, indent=4)
try:
with open(USER_PROGRESS_FILE, 'r') as f:
progress_data = json.load(f)
if username in progress_data:
user_data = progress_data[username]
import streamlit as st
st.session_state.resources_completed = user_data.get('completed_resources', [])
st.session_state.current_level = user_data.get('current_level', 'beginner')
except Exception as e:
print(f"Error loading user progress: {e}")
def save_user_progress(username, completed_resources, current_level):
"""Save user progress to JSON file"""
try:
if os.path.exists(USER_PROGRESS_FILE):
with open(USER_PROGRESS_FILE, 'r') as f:
progress_data = json.load(f)
else:
progress_data = {}
progress_data[username] = {
'completed_resources': completed_resources,
'current_level': current_level,
'last_updated': datetime.now().isoformat()
}
with open(USER_PROGRESS_FILE, 'w') as f:
json.dump(progress_data, f, indent=4)
except Exception as e:
print(f"Error saving user progress: {e}")
def get_recommendations(level, completed_resources):
"""Get personalized recommendations based on user's progress"""
resources = load_resources()
# Filter resources by level and not completed
available_resources = [r for r in resources if r['level'] == level and r['id'] not in completed_resources]
# If user has completed more than 80% of current level, suggest some from next level
if level != "advanced":
completed_level_resources = [r for r in resources if r['level'] == level and r['id'] in completed_resources]
all_level_resources = [r for r in resources if r['level'] == level]
if len(all_level_resources) > 0 and len(completed_level_resources) / len(all_level_resources) >= 0.8:
next_level = "intermediate" if level == "beginner" else "advanced"
next_level_resources = [r for r in resources if r['level'] == next_level and r['id'] not in completed_resources]
available_resources.extend(next_level_resources[:2])
# Sort by priority (if present) and randomize a bit to provide variety
random.shuffle(available_resources)
return available_resources
def generate_default_resources():
"""Generate default resource dataset"""
return [
# Beginner resources
{
"id": "b1",
"title": "Official Python Documentation - Getting Started",
"type": "Documentation",
"description": "The official Python documentation's beginner guide",
"url": "https://docs.python.org/3/tutorial/index.html",
"level": "beginner",
"tags": ["syntax", "basics", "installation"]
},
{
"id": "b2",
"title": "Python for Everybody (py4e)",
"type": "Free Course",
"description": "A complete beginner-friendly Python course by Dr. Charles Severance",
"url": "https://www.py4e.com/",
"level": "beginner",
"tags": ["basics", "programming", "fundamentals"]
},
{
"id": "b3",
"title": "W3Schools Python Tutorial",
"type": "Tutorial",
"description": "Interactive Python tutorial with exercises and examples",
"url": "https://www.w3schools.com/python/",
"level": "beginner",
"tags": ["syntax", "examples", "interactive"]
},
{
"id": "b4",
"title": "Automate the Boring Stuff with Python",
"type": "Free Book",
"description": "Practical programming for total beginners by Al Sweigart",
"url": "https://automatetheboringstuff.com/",
"level": "beginner",
"tags": ["automation", "practical", "scripts"]
},
{
"id": "b5",
"title": "Real Python - Python Basics",
"type": "Tutorial",
"description": "Python basics with practical examples and explanations",
"url": "https://realpython.com/python-basics/",
"level": "beginner",
"tags": ["basics", "examples", "fundamentals"]
},
{
"id": "b6",
"title": "Codecademy - Learn Python",
"type": "Interactive Course",
"description": "Interactive Python course with coding exercises",
"url": "https://www.codecademy.com/learn/learn-python-3",
"level": "beginner",
"tags": ["interactive", "basics", "syntax"]
},
{
"id": "b7",
"title": "freeCodeCamp - Python Beginner Course",
"type": "Video Course",
"description": "Full Python course for beginners on YouTube",
"url": "https://www.youtube.com/watch?v=rfscVS0vtbw",
"level": "beginner",
"tags": ["video", "comprehensive", "tutorial"]
},
{
"id": "b8",
"title": "Programiz Python Tutorial",
"type": "Tutorial",
"description": "Step by step Python tutorial with examples",
"url": "https://www.programiz.com/python-programming",
"level": "beginner",
"tags": ["tutorial", "examples", "basics"]
},
# Intermediate resources
{
"id": "i1",
"title": "Python Design Patterns",
"type": "Tutorial",
"description": "Implementation of design patterns in Python",
"url": "https://refactoring.guru/design-patterns/python",
"level": "intermediate",
"tags": ["design patterns", "OOP", "architecture"]
},
{
"id": "i2",
"title": "Intermediate Python",
"type": "Free Book",
"description": "A free book for intermediate Python programmers",
"url": "https://book.pythontips.com/en/latest/",
"level": "intermediate",
"tags": ["advanced concepts", "tips", "tricks"]
},
{
"id": "i3",
"title": "Fluent Python",
"type": "Book Excerpts",
"description": "Clear, concise, and effective programming with Python",
"url": "https://github.com/fluentpython",
"level": "intermediate",
"tags": ["idiomatic", "effective", "pythonic"]
},
{
"id": "i4",
"title": "Real Python - Intermediate Topics",
"type": "Tutorial Collection",
"description": "A collection of intermediate-level Python tutorials",
"url": "https://realpython.com/tutorials/intermediate/",
"level": "intermediate",
"tags": ["diverse topics", "practical", "tutorials"]
},
{
"id": "i5",
"title": "Python Testing with pytest",
"type": "Tutorial",
"description": "Learn about testing Python applications with pytest",
"url": "https://realpython.com/pytest-python-testing/",
"level": "intermediate",
"tags": ["testing", "pytest", "quality"]
},
{
"id": "i6",
"title": "Python Tricks: The Book",
"type": "Book Samples",
"description": "A buffet of awesome Python features and tricks",
"url": "https://realpython.com/products/python-tricks-book/",
"level": "intermediate",
"tags": ["tricks", "features", "techniques"]
},
{
"id": "i7",
"title": "Python Data Science Handbook",
"type": "Free Book",
"description": "Python data science libraries and techniques",
"url": "https://jakevdp.github.io/PythonDataScienceHandbook/",
"level": "intermediate",
"tags": ["data science", "numpy", "pandas", "matplotlib"]
},
{
"id": "i8",
"title": "Python Concurrency",
"type": "Tutorial",
"description": "Understanding concurrency in Python with threading, multiprocessing, and asyncio",
"url": "https://realpython.com/python-concurrency/",
"level": "intermediate",
"tags": ["concurrency", "threading", "asyncio"]
},
# Advanced resources
{
"id": "a1",
"title": "Python 3 Module of the Week",
"type": "Documentation",
"description": "Deep dive into Python's standard library modules",
"url": "https://pymotw.com/3/",
"level": "advanced",
"tags": ["standard library", "modules", "reference"]
},
{
"id": "a2",
"title": "Python Cookbook",
"type": "Book Excerpts",
"description": "Recipes for mastering Python 3",
"url": "https://github.com/dabeaz/python-cookbook",
"level": "advanced",
"tags": ["recipes", "techniques", "advanced patterns"]
},
{
"id": "a3",
"title": "CPython Internals",
"type": "Tutorial",
"description": "Understanding the internals of CPython",
"url": "https://realpython.com/cpython-source-code-guide/",
"level": "advanced",
"tags": ["cpython", "internals", "c-api"]
},
{
"id": "a4",
"title": "Full Stack Python",
"type": "Documentation",
"description": "Full stack Python web development",
"url": "https://www.fullstackpython.com/",
"level": "advanced",
"tags": ["web development", "deployment", "frameworks"]
},
{
"id": "a5",
"title": "Python Decorators",
"type": "Tutorial",
"description": "Deep dive into Python decorators",
"url": "https://realpython.com/primer-on-python-decorators/",
"level": "advanced",
"tags": ["decorators", "metaprogramming", "functions"]
},
{
"id": "a6",
"title": "Building Machine Learning Systems with Python",
"type": "Tutorial Series",
"description": "Advanced machine learning with Python",
"url": "https://github.com/luispedro/BuildingMachineLearningSystemsWithPython",
"level": "advanced",
"tags": ["machine learning", "algorithms", "data science"]
},
{
"id": "a7",
"title": "Python Performance Optimization",
"type": "Tutorial",
"description": "Advanced techniques for optimizing Python code",
"url": "https://pythonspeed.com/",
"level": "advanced",
"tags": ["performance", "optimization", "profiling"]
},
{
"id": "a8",
"title": "Making Python Programs Blazingly Fast",
"type": "Video",
"description": "Advanced techniques for high-performance Python",
"url": "https://www.youtube.com/watch?v=o9wePFI0XkE",
"level": "advanced",
"tags": ["optimization", "performance", "cython"]
}
]
def generate_default_projects():
"""Generate default projects dataset"""
return [
# Beginner projects
{
"id": "pb1",
"title": "Number Guessing Game",
"description": "Create a simple number guessing game where the computer generates a random number and the user tries to guess it.",
"level": "beginner",
"difficulty": 1,
"skills": ["Basic syntax", "Input/Output", "Random module", "Conditionals"],
"details": "Build a console-based number guessing game where the program generates a random number between 1 and 100. The player gets feedback after each guess whether their guess was too high or too low, until they correctly guess the number.",
"starter_code": """import random
# Generate a random number between 1 and 100
target_number = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
# Your code here
# Hint: Use a while loop to keep asking for guesses
# Use input() to get the user's guess
# Convert the input to an integer using int()
# Compare the guess with target_number
# Provide appropriate feedback
"""
},
{
"id": "pb2",
"title": "To-Do List Application",
"description": "Build a simple command-line to-do list where users can add, view, and delete tasks.",
"level": "beginner",
"difficulty": 2,
"skills": ["Lists", "Functions", "File I/O", "User input"],
"details": "Create a command-line application that allows users to manage their to-do list. Implement functions to add tasks, view all tasks, mark tasks as completed, delete tasks, and save the tasks to a file for persistence.",
"starter_code": """# To-Do List Application
todos = []
def show_menu():
print("\\n==== TO-DO LIST MENU ====")
print("1. Add task")
print("2. View tasks")
print("3. Mark task as completed")
print("4. Delete task")
print("5. Save and exit")
choice = input("Enter your choice (1-5): ")
return choice
def add_task():
# Your code here
pass
def view_tasks():
# Your code here
pass
def complete_task():
# Your code here
pass
def delete_task():
# Your code here
pass
def save_tasks():
# Your code here
pass
def load_tasks():
# Your code here
pass
# Main program
load_tasks() # Load any existing tasks
while True:
user_choice = show_menu()
# Implement the menu logic
# Your code here
"""
},
{
"id": "pb3",
"title": "Simple Calculator",
"description": "Create a basic calculator that can perform addition, subtraction, multiplication, and division.",
"level": "beginner",
"difficulty": 1,
"skills": ["Functions", "User input", "Conditionals", "Error handling"],
"details": "Build a command-line calculator that prompts the user for two numbers and an operation, then displays the result. Include error handling for invalid inputs and division by zero.",
"starter_code": """# Simple Calculator
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
# Your code here
pass
def divide(x, y):
# Your code here - remember to handle division by zero
pass
# Main calculator loop
while True:
print("\\nOptions:")
print("Enter 'add' for addition")
print("Enter 'subtract' for subtraction")
print("Enter 'multiply' for multiplication")
print("Enter 'divide' for division")
print("Enter 'quit' to end the program")
user_input = input(": ")
if user_input == "quit":
break
# Your code here to get the numbers and call the appropriate function
"""
},
{
"id": "pb4",
"title": "Password Generator",
"description": "Create a program that generates random passwords with varying complexity.",
"level": "beginner",
"difficulty": 2,
"skills": ["Random module", "Strings", "Functions", "User input"],
"details": "Build a password generator that creates random passwords based on user-specified criteria such as length and inclusion of uppercase letters, lowercase letters, numbers, and special characters.",
"starter_code": """import random
import string
def generate_password(length, use_uppercase, use_numbers, use_special):
# Define character sets
lowercase_chars = string.ascii_lowercase
uppercase_chars = string.ascii_uppercase
number_chars = string.digits
special_chars = string.punctuation
# Create a pool of characters based on selected options
# Your code here
# Generate and return the password
# Your code here
# Main program
print("Welcome to Password Generator!")
# Get user preferences
# Your code here
# Generate and display password
# Your code here
"""
},
# Intermediate projects
{
"id": "pi1",
"title": "Weather App",
"description": "Build a command-line application that fetches and displays weather data for a given location.",
"level": "intermediate",
"difficulty": 3,
"skills": ["APIs", "JSON", "Error handling", "Data formatting"],
"details": "Create a weather application that takes a city name or ZIP code as input, fetches weather data from a free API, and displays current conditions, temperature, and forecast in a user-friendly format.",
"starter_code": """import requests
import json
def get_weather(location):
# Use a free weather API like OpenWeatherMap
# You'll need to use the requests library to fetch data
# Parse the JSON response and extract relevant information
# Return formatted weather data
pass
def display_weather(weather_data):
# Format and display the weather information in a user-friendly way
pass
def main():
print("Weather Information App")
location = input("Enter city name or ZIP code: ")
try:
weather_data = get_weather(location)
display_weather(weather_data)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
"""
},
{
"id": "pi2",
"title": "Web Scraper",
"description": "Build a web scraper that extracts specific information from websites.",
"level": "intermediate",
"difficulty": 3,
"skills": ["Web scraping", "BeautifulSoup/Requests", "HTML/CSS basics", "Data extraction"],
"details": "Create a program that scrapes information from a selected website (e.g., news headlines, product prices, or weather forecasts). The scraper should extract specific data elements, format them, and save them to a file or display them in a structured way.",
"starter_code": """import requests
from bs4 import BeautifulSoup
def scrape_website(url, target_element, target_class=None):
# Fetch the web page
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Find the target elements
# Your code here
# Extract and return the desired information
# Your code here
else:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
return None
def main():
# Example: Scraping news headlines
print("Web Scraper")
url = input("Enter the URL to scrape: ")
element = input("Enter the HTML element to target (e.g., div, h2, p): ")
class_name = input("Enter the class name (optional): ")
results = scrape_website(url, element, class_name)
# Process and display the results
# Your code here
if __name__ == "__main__":
main()
"""
},
{
"id": "pi3",
"title": "Personal Finance Tracker",
"description": "Create an application to track personal expenses and income.",
"level": "intermediate",
"difficulty": 3,
"skills": ["File I/O", "Data structures", "Data visualization", "OOP"],
"details": "Build a personal finance tracker that allows users to record expenses and income, categorize transactions, and generate reports. The application should store transaction history in a file and provide summary statistics and basic visualizations of spending patterns.",
"starter_code": """import json
import os
from datetime import datetime
import matplotlib.pyplot as plt
class Transaction:
def __init__(self, amount, category, description, date=None, transaction_type="expense"):
# Initialize transaction attributes
# Your code here
pass
def __str__(self):
# Return a string representation of the transaction
# Your code here
pass
class FinanceTracker:
def __init__(self, filename="finances.json"):
# Initialize the tracker
# Your code here
pass
def add_transaction(self, transaction):
# Add a transaction to the list
# Your code here
pass
def get_balance(self):
# Calculate and return current balance
# Your code here
pass
def get_summary_by_category(self):
# Summarize transactions by category
# Your code here
pass
def save_to_file(self):
# Save transactions to file
# Your code here
pass
def load_from_file(self):
# Load transactions from file
# Your code here
pass
def generate_report(self):
# Generate a report of finances
# Your code here
pass
def visualize_expenses(self):
# Create visualizations of spending patterns
# Your code here
pass
# Main application
def main():
tracker = FinanceTracker()
tracker.load_from_file()
# Implement the user interface
# Your code here
if __name__ == "__main__":
main()
"""
},
{
"id": "pi4",
"title": "URL Shortener",
"description": "Create a URL shortening service like bit.ly.",
"level": "intermediate",
"difficulty": 4,
"skills": ["Web development", "Databases", "Flask/Django", "Unique ID generation"],
"details": "Build a basic URL shortening service that takes long URLs and creates shorter, more manageable links. The system should store the mapping between short and long URLs and redirect users who visit the short URL to the original long URL.",
"starter_code": """from flask import Flask, request, redirect, render_template, url_for
import string
import random
import json
import os
app = Flask(__name__)
# In a real application, you would use a database
# For simplicity, we'll use a JSON file
URL_DB_FILE = "urls.json"
def load_urls():
if os.path.exists(URL_DB_FILE):
with open(URL_DB_FILE, 'r') as f:
return json.load(f)
return {}
def save_urls(urls):
with open(URL_DB_FILE, 'w') as f:
json.dump(urls, f)
def generate_short_code(length=6):
# Generate a random short code
# Your code here
pass
@app.route('/')
def index():
# Render the home page with a form for URL submission
# Your code here
pass
@app.route('/shorten', methods=['POST'])
def shorten():
# Get the URL from the form
# Generate a short code
# Save the mapping
# Return the shortened URL
# Your code here
pass
@app.route('/<short_code>')
def redirect_to_url(short_code):
# Look up the short code and redirect to the original URL
# Your code here
pass
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
"""
},
# Advanced projects
{
"id": "pa1",
"title": "Machine Learning Image Classifier",
"description": "Build an image classification system using machine learning.",
"level": "advanced",
"difficulty": 5,
"skills": ["Machine learning", "TensorFlow/PyTorch", "Neural networks", "Image processing"],
"details": "Create an image classification system that can identify objects in images. Use a pre-trained model like ResNet or VGG16 and fine-tune it for your specific classification task. Implement a user interface that allows users to upload images and receive predictions.",
"starter_code": """import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import numpy as np
def load_model():
# Load a pre-trained model
model = MobileNetV2(weights='imagenet')
return model
def preprocess_image(img_path):
# Load and preprocess an image
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
def predict(model, img):
# Make a prediction
preds = model.predict(img)
results = decode_predictions(preds, top=5)[0]
return results
def main():
# Load the model
model = load_model()
# Get image path from user
img_path = input("Enter path to image: ")
# Preprocess the image
preprocessed_img = preprocess_image(img_path)
# Make predictions
results = predict(model, preprocessed_img)
# Display results
print("Top predictions:")
for i, (imagenet_id, label, score) in enumerate(results):
print(f"{i+1}: {label} ({score:.2f})")
if __name__ == "__main__":
main()
"""
},
{
"id": "pa2",
"title": "Natural Language Processing Chatbot",
"description": "Build a chatbot that can understand and respond to natural language inputs.",
"level": "advanced",
"difficulty": 5,
"skills": ["NLP", "NLTK/spaCy", "Text processing", "Dialog management"],
"details": "Develop a chatbot that can understand user inputs using natural language processing techniques. The chatbot should be able to extract intent and entities from user messages, maintain context across multiple turns of conversation, and generate appropriate responses.",
"starter_code": """import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import string
import random
import json
# Download necessary NLTK data
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
class Chatbot:
def __init__(self, knowledge_base_file="knowledge_base.json"):
# Initialize the chatbot with a knowledge base
self.lemmatizer = WordNetLemmatizer()
self.stop_words = set(stopwords.words('english'))
self.load_knowledge_base(knowledge_base_file)
self.context = {}
def load_knowledge_base(self, file_path):
# Load the knowledge base from a JSON file
try:
with open(file_path, 'r') as f:
self.knowledge_base = json.load(f)
except FileNotFoundError:
# Create a basic knowledge base if file not found
self.knowledge_base = {
"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Hey", "How are you", "Greetings"],
"responses": ["Hello!", "Hi there!", "Hey! How can I help you today?"]
},
{
"tag": "goodbye",
"patterns": ["Bye", "See you", "Goodbye", "I'm leaving"],
"responses": ["Goodbye!", "See you later!", "Have a nice day!"]
},
{
"tag": "thanks",
"patterns": ["Thanks", "Thank you", "That's helpful"],
"responses": ["You're welcome!", "Happy to help!", "Anytime!"]
}
]
}
self.save_knowledge_base(file_path)
def save_knowledge_base(self, file_path):
# Save the knowledge base to a file
with open(file_path, 'w') as f:
json.dump(self.knowledge_base, f, indent=4)
def preprocess(self, text):
# Tokenize, remove punctuation, lemmatize, and remove stop words
# Your code here
pass
def extract_intent(self, text):
# Extract the intent from user input
# Your code here
pass
def generate_response(self, intent):
# Generate a response based on the intent
# Your code here
pass
def process_input(self, user_input):
# Process user input and generate a response
# Your code here
pass
def main():
chatbot = Chatbot()
print("Chatbot: Hello! How can I help you today? (type 'quit' to exit)")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
print("Chatbot: Goodbye!")
break
response = chatbot.process_input(user_input)
print(f"Chatbot: {response}")
if __name__ == "__main__":
main()
"""
},
{
"id": "pa3",
"title": "Web Application with Django",
"description": "Build a full-featured web application using Django.",
"level": "advanced",
"difficulty": 4,
"skills": ["Django", "Databases", "Web development", "Authentication"],
"details": "Create a complete web application with Django. Include user authentication, database models for storing application data, forms for user input, and views for displaying information. Implement a responsive frontend using Django templates and CSS.",
"starter_code": """# This is a simplified guide for a Django project
# In a real implementation, you'd need multiple files in different directories
# 1. Create a new Django project:
# django-admin startproject myproject
# 2. Create a new app:
# cd myproject
# python manage.py startapp myapp
# 3. Define models in myapp/models.py:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
def __str__(self):
return self.user.username
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
# 4. Create forms in myapp/forms.py:
from django import forms
from .models import Profile, Item
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['bio', 'location', 'birth_date']
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ['name', 'description']
# 5. Define views in myapp/views.py:
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Profile, Item
from .forms import ProfileForm, ItemForm
def home(request):
items = Item.objects.all().order_by('-created_at')
return render(request, 'myapp/home.html', {'items': items})
@login_required
def profile(request):
profile = get_object_or_404(Profile, user=request.user)
if request.method == 'POST':
form = ProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = ProfileForm(instance=profile)
return render(request, 'myapp/profile.html', {'form': form})
@login_required
def add_item(request):
if request.method == 'POST':
form = ItemForm(request.POST)
if form.is_valid():
item = form.save(commit=False)
item.created_by = request.user
item.save()
return redirect('home')
else:
form = ItemForm()
return render(request, 'myapp/add_item.html', {'form': form})
# 6. Configure URLs in myapp/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('profile/', views.profile, name='profile'),
path('add-item/', views.add_item, name='add_item'),
]
# 7. Update project URLs in myproject/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
# 8. Create templates in myapp/templates/myapp/
# (home.html, profile.html, add_item.html)
# 9. Configure settings in myproject/settings.py
# 10. Run migrations:
# python manage.py makemigrations
# python manage.py migrate
# 11. Create a superuser:
# python manage.py createsuperuser