main.py
# coding=utf-8 from wsgiref.simple_server import make_server from urlparse import parse_qs import json import router SERVER_PORT = 8080 API_TOKEN = 'z8675309q' # Starts the server def start_server(): server = make_server('', SERVER_PORT, app) print "serving at port", SERVER_PORT server.serve_forever() # Basic app hook def app(environ, start_response): request_path = environ['PATH_INFO'] query_params = parse_qs(environ['QUERY_STRING']) request_bodysize = int(environ['CONTENT_LENGTH']) request_body = environ['wsgi.input'].read(request_bodysize) json_body = json.loads(request_body) token = json_body.get('token') if token is not None and token != API_TOKEN: start_response('403 FORBIDDEN', [('Content-type', 'application/json')]) return json.dumps({"ok": False, "error": "invalid_token"}) # route the request start_response('200 OK', [('Content-type', 'application/json')]) return json.dumps(router.HandleRouting(request_path, query_params, json_body)) start_server()
# coding=utf-8 import storage def HandleRouting(path, query_parameters, body): # Divert based on path if path == '/api/course.create': return storage.create_course(body['name'], body['credits'], body['type']) if path == '/api/course.addOffering': return storage.create_course_offering(body['course_id'], body['instructor_id'], body['year'], body['term'], body['days'], body['time'], body['capacity']) if path == '/api/course.getOfferings': return storage.get_course_offerings(body['course_id']) if path == '/api/course.enroll': return storage.create_enrollment(body['course_offering_id'], body['student_id']) if path == '/api/student.add': return storage.addStudent(body['name']) if path == '/api/instructor.add': return storage.add_instructor(body['name']) # can't find an associated endpoint return { "ok": False, "error": "unknown_method" }
# coding=utf-8 import mysql.connector # Creates a course in the database def create_course(name, credits, type): connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') cursor = connection.cursor() cursor.execute('INSERT INTO COURSES (name, credits, type) VALUES("' + name + '",' + str(credits) + ',"' + type + '")') course_id = cursor.lastrowid connection.commit() connection.close() return { "ok": True, "course_id": course_id, } def addStudent(name): """Adds a new student by first name and last name. The combination must be unique.""" connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') cursor = connection.cursor() cursor.execute('INSERT INTO students (name) VALUES (%(name)s)', {"name": name}) student_id = cursor.lastrowid connection.commit() connection.close() return { "ok": True, "student_id": student_id, } # Adds a new instructor to the database def add_instructor(name): connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') cursor = connection.cursor() cursor.execute('INSERT INTO instructors (name) VALUES (%(name)s)', {"name": name}) student_id = cursor.lastrowid connection.commit() connection.close() return { "ok": True, "student_id": student_id, } # Adds a new course offering def create_course_offering(course_id, instructor_id, year, term, days, time, capacity): # Validate days is in the allowed set allowed_days = ['M', 'W', 'F', 'MW', 'WF', 'MWF', 'T', 'Tr', 'TTr'] found = False for allowed_days_option in allowed_days: if allowed_days_option == days: found = True break if not found: return { "ok": False, "error": "invalid_days", } # Create the course offering connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') cursor = connection.cursor() cursor.execute('INSERT INTO course_offerings (course_id, instructor_id, year, term, days, time, capacity) VALUES (%s, %s, %s, %s, %s, %s, %s)', (course_id, instructor_id, year, term, days, time, capacity)) course_offering_id = cursor.lastrowid connection.commit() connection.close() return { "ok": True, "course_offering_id": course_offering_id, } def get_course_offerings(course_id): """Gets course offering information for the specified course id""" connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') c1 = connection.cursor(buffered=True) c1.execute('SELECT id, instructor_id, year, term, days, time, capacity FROM course_offerings WHERE course_id=%(course_id)s', {"course_id": course_id}) # Enumerate the retrieved course offerings offerings = [] for (id, instructor_id, year, term, days, time, capacity) in c1: # Get instructor name c2 = connection.cursor() c2.execute('SELECT name FROM instructors WHERE id=%(id)s', {"id": instructor_id}) row = c2.fetchone() if row is not None: instructor_name = row[0] # Determine remaining capacity c3 = connection.cursor() c3.execute('select count(*) as count from enrollments where course_offering_id=%(id)s', {"id": id}) row = c3.fetchone() count = row[0] remaining_capacity = capacity - count offerings.append({ 'course_offering_id': id, 'instructor': instructor_name, 'year': year, 'term': term, 'days': days, 'time': time, 'remaining_capacity': remaining_capacity }) connection.close() # Return results return { "ok": True, "course_offerings": offerings, } def create_enrollment(course_offering_id, student_id): """Enrolls a student to a specified course offering""" connection = mysql.connector.connect(user='root', password='super-secret-password', host='localhost', database='registrar') # First check if there's space in the course offering c1 = connection.cursor() c1.execute('SELECT capacity FROM course_offerings WHERE id=%(id)s', {"id": course_offering_id}) capacity = c1.fetchone()[0] c2 = connection.cursor() c2.execute('SELECT COUNT(*) FROM enrollments WHERE course_offering_id=%(id)s', {"id": course_offering_id}) count = c2.fetchone()[0] if count >= capacity: return { "ok": False, "error": "capacity_reached", } # Enroll the student c3 = connection.cursor() c3.execute('INSERT INTO enrollments (course_offering_id, student_id) VALUES (%s, %s)', (course_offering_id, student_id)) connection.commit() connection.close() return { "ok": True, }
DROP DATABASE registrar; CREATE DATABASE registrar; USE registrar; CREATE TABLE `COURSES` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `credits` int(11) NOT NULL, `type` enum('course','lab','combined') NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name_type_unique` (`name`, `type`), KEY `type` (`type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `course_offerings` ( `id` int NOT NULL AUTO_INCREMENT, `course_id` int NOT NULL, `instructor_id` int NOT NULL, `year` tinyint NOT NULL, `term` enum('summer','spring','fall') NOT NULL, `days` varchar(100) NOT NULL, `time` varchar(100) NOT NULL, `capacity` tinyint NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `students` ( `id` tinyint NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_name_unique` (`id`, `name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `instructors` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `enrollments` ( `id` int NOT NULL AUTO_INCREMENT, `course_offering_id` int(11) NOT NULL, `student_id` int(11) NOT NULL, `student_name` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_student_id_unique` (`id`, `student_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Course Enrollment System (Registrar)
Prompt
You are on a 2-person developer team tasked with writing a rudimentary course enrollment system for a small college. Your solution includes a backend written in Python and uses a MySQL database for permanent data storage.
You have been tasked with reviewing your fellow engineer's latest pull request! Please review the submitted changes for correctness and best practices, all the while ensuring that it fulfills the project requirements listed below. If you’re not a Python expert, no worries; we won’t be looking for comments specific to the sample’s programming language.
This is a lengthy pull request with a wide range of mistakes throughout; We do not require you to enumerate all of the mistakes in order to receive a passing grade, but ensure that you've provided feedback on a few different types of problems. We recommend reading the code completely at least once before beginning to provide feedback. Please spend no more than about an hour reviewing the pull request.
For inspiration, read our engineering blog post about code review best practices.
System Requirements
- Add courses and course offerings
- Link instructors to course offerings
- Add students
- Enroll students in course offerings
- Course offerings have limited capacity
- Students also have limited capacity - no single student can enroll in more than five course offerings per semester
API Usage
The following examples demonstrate how to use the enrollment system's API.
curl localhost:8080/api/student.add -d '{"name": "Yoshi", "token": "z8675309q"}'
curl localhost:8080/api/course.create -d '{"name": "COMP200", "credits": 3, "type": "lab", "token": "z8675309q"}'
curl localhost:8080/api/instructor.add -d '{"name": "Dr. Mario", "token": "z8675309q"}'
curl localhost:8080/api/course.addOffering -d '{"course_id": 1, "instructor_id": 2, "year": 2018, "term": "Spring", "days": "MWF", "time": "9:00", "capacity": 10, "token": "z8675309q"}'
curl localhost:8080/api/course.getOfferings -d '{"course_id": "1", "token": "z8675309q"}'
curl localhost:8080/api/course.enroll -d '{"course_offering_id": "1", "student_id": 1, "token": "z8675309q"}'
相关推荐
c socket pyton socketc socket pyton socket
Pyton核心编程 高清目录 pdf 电子书 高清目录 pdf 电子书
selenium+pyton_google
这是很全面的协同过滤算法,python写的
python 实现简单的学生信息管理,实现增加、删除、修改、查找 功能。代码清晰易懂,适合刚接触python的伙伴。
pyton 创建对象 仅用于考试
pyton2.7.15安装文件64位
pyton ursina 实现简陋我的世界
a byte of pyton(python简明教程 3.0版本) 这是一个入门的教程,该教程绝对值得看看,是入门的不二选择,虽然是英文的,但是很容易看得懂的
使用Pyton3.6.5和Gdal2.2.0编译的库,可以通过Pyhon调用GDAL的相关函数
需要工具: 1. Python 2.3 以上 2. BOA Constructor Python設定檔介面程式設計 這段程式碼主要是把資料儲存到config.txt這個檔案中,我們將資料以 \n[_config_]\n 來作區隔,以便將來將資料讀出來時可以知道所...
pyton的帮助文档python-docs-html
Pyton与C语言的比较研究.pdf
一.常量,变量和标识符 1.标识符 用来表示各种程序成分,命名程序中的一些实体(变量,常量,函数名等) 标识符命名规则: (1)变量名和函数名中的英文字母一般用小写,增加程序的可读性 (2)见名知意(一般...
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
用于连接mysql数据库..这是mysql的2.0.0版本 源代码
非常有意思的python pyside小程序-数字时钟,让编程不无聊!
解开包后,只要用自己的程序替换mysrc.zip就可以使用了。 包里有runpy.exe和runpyw.exe两个文件。其中,runpy.exe是控制台程序,runpyw.exe是非控制台程序。这两个程序分别类似于python.exe和pythonw.exe。...
A bit of Pyton&Machine; Leaning System with Python 两本经典书籍 外加面向对象编程的指南
基于Pyton的Climate Indices库计算不同站点、不同时间尺度的SPI