From 94b1d8ba5563b6d50fa37b3d6e7208adf6984ddc Mon Sep 17 00:00:00 2001 From: t510599 Date: Sat, 22 Dec 2018 00:37:45 +0800 Subject: [PATCH] Read database in python directly now --- README.md | 20 ++++++++++ cli/seat.py | 101 ++++++++++++++++++++++++++++++++++++-------------- web/empty.js | 10 ----- web/export.js | 16 -------- 4 files changed, 94 insertions(+), 53 deletions(-) delete mode 100644 web/empty.js delete mode 100644 web/export.js diff --git a/README.md b/README.md index 5b0d45c..84a6f91 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # 210-seat +## Usage +`seats.py [mode] [parameters]` +``` +generate - generate a random seat +switch - switch seats of two students +run - apply requirements +export - export requirements to requirements.txt +print - print the current seat +debug - dry run +``` + +## Dependencies +- cli + - pymongo + +- web +```bash +cd 210-seats/web +npm install +``` \ No newline at end of file diff --git a/cli/seat.py b/cli/seat.py index 38b2c53..ae43903 100644 --- a/cli/seat.py +++ b/cli/seat.py @@ -1,46 +1,65 @@ +# -*- coding: utf-8 -*- from algo import generateSeats, printArr, shiftUp, shiftDown, shiftLeft, shiftRight, shiftLeftDown, shiftLeftUp, shiftRightDown, shiftRightUp +from pymongo import MongoClient from random import choice import sys import json def load(): - f = open('seats.txt','r',encoding="utf-8") - seats = json.load(f) - f.close() + with open('seats.txt','r',encoding="utf-8") as f: + seats = json.load(f) return seats -def parse(seats,requirements): +def save(seats): + with open('seats.txt','w',encoding="utf-8") as n: + n.write(str(seats)) + +def query(): + client = MongoClient('127.0.0.1', 27017) + db = client['210-seats'] + collection = db['users'] + + data = collection.find().sort("no", ) + for user in data: + yield user + +def apply_requirements(seats, requirements): funcs = {"f": shiftUp,"b": shiftDown, "l": shiftLeft, "r": shiftRight, "fr": shiftRightUp, "br": shiftRightDown, "fl": shiftLeftUp, "bl": shiftLeftDown} # f: forward, b: backward, l:left, r: right - name = {"f": "向前","b": "向後", "l": "向左", "r": "向右", "fr": "向右前", "br": "向右後", "fl": "向左前", "bl": "向左後"} + name = {"f": "Forward","b": "Backward", "l": "Left", "r": "Right", "fr": "Right Forward", "br": "Right Backward", "fl": "Left Forward", "bl": "Left Backward"} order = sorted([int(n) for n in requirements.keys()]) luckier = choice(order) - print("幸運兒: " + str(luckier) + "\n") + print("Luckier: " + str(luckier) + "\n") order = order[order.index(luckier):] + order[:order.index(luckier)] for req in order: - no,direction,steps = requirements[str(req)] - no = no - steps = steps - row,col = find(seats,no) + no, direction, steps = requirements[str(req)] + row,col = find(seats, no) if col != None and row != None: if direction in funcs.keys(): - funcs[direction](row,col,steps,seats) + funcs[direction](row, col, steps, seats) print(no, name[direction], steps) printArr(seats) print() return seats -def find(seats,no): +def find(seats, no, raw=False): + if raw: + modifier = 0 + else: + modifier = 1 # start from 0 -> start from 1 + for y in range(len(seats)): if no in seats[y]: col = seats[y].index(no) row = y - return (row+1,col+1) # start from 0 -> start from 1 + return (row + modifier, col + modifier) return (None,None) help_msg = '''Please provide mode! generate - generate a random seat +switch - switch seats of two students run - apply requirements +export - export requirements to requirements.txt print - print the current seat debug - dry run''' @@ -53,30 +72,58 @@ if __name__ == "__main__": if mode == "generate": seats = generateSeats() printArr(seats) - n = open('seats.txt','w',encoding="utf-8") - n.write(str(seats)) - n.close() + save(seats) print("Done!") elif mode == "run" or mode == "debug": seats = load() print("Original:") printArr(seats) - #load requirements + + # load requirements requirements = dict() - r = open('requirements.txt','r',encoding="utf-8") - for line in r.readlines(): - no,direction,steps = line.strip().split(" ") - requirements[no] = (int(no),direction,int(steps)) - parse(seats,requirements) + with open('requirements.txt', 'w', encoding="utf-8") as req_f: + for user in query(): + if user['steps'].strip() != "": + requirements[str(user['no'])] = (user['no'], user['direction'], int(user['steps'])) + req_f.write("{0} {1} {2}\n".format(user['no'], user['direction'], int(user['steps']))) + + apply_requirements(seats, requirements) + print("Result:") printArr(seats) + + # save if mode == "run": - #save - n = open('seats.txt','w',encoding="utf-8") - n.write(str(seats)) - n.close() + with open('seats.txt','w',encoding="utf-8") as n: + n.write(str(seats)) + print("Done!") - elif mode == "show": + elif mode == "switch": + seats = load() + + one = int(sys.argv[2]) + two = int(sys.argv[3]) + pos_one = find(seats, one, True) + pos_two = find(seats, two, True) + seats[pos_one[0]][pos_one[1]], seats[pos_two[0]][pos_two[1]] = seats[pos_two[0]][pos_two[1]], seats[pos_one[0]][pos_one[1]] + + printArr(seats) + save(seats) + + # logging switch + log = open('switch.txt','a',encoding='utf-8') + log.write("{} {}\n".format(str(one), str(two))) + log.close() + elif mode == "export": + requirements = dict() + with open('requirements.txt', 'w', encoding="utf-8") as req_f: + for user in query(): + if user['steps'].strip() != "": + requirements[str(user['no'])] = (user['no'], user['direction'], int(user['steps'])) + req_f.write("{0} {1} {2}\n".format(user['no'], user['direction'], int(user['steps']))) + + print(requirements) + elif mode == "print": printArr(load()) else: print(help_msg) diff --git a/web/empty.js b/web/empty.js deleted file mode 100644 index 70d9882..0000000 --- a/web/empty.js +++ /dev/null @@ -1,10 +0,0 @@ -var users = require('./models/users'); - -users.find({}).sort({ no: 1 }).exec(function(err,data){ - for (set of data) { - if (set.direction == "") { - console.log(set.no) - } - } -}); - diff --git a/web/export.js b/web/export.js deleted file mode 100644 index d44cf9a..0000000 --- a/web/export.js +++ /dev/null @@ -1,16 +0,0 @@ -var fs = require('fs'); -var path = require('path'); -var users = require('./models/users'); - -fs.writeFileSync(path.resolve(__dirname,"../cli/requirements.txt"),""); // empty the file - -users.find({}).sort({ no: 1 }).exec(function(err,data){ - for (set of data) { - console.log(set.no) - if (set.direction != "" && set.steps != "") { - console.log('yes') - fs.appendFileSync(path.resolve(__dirname,"../cli/requirements.txt"),`${set.no} ${set.direction} ${set.steps}\n`); - } - } -}); -