def load_data(path='data/'): data_file_lst = os.listdir(path); attrlist = []; # 采集数据点 attrType = {}; # 每个数据点包含的类型 for data_file in data_file_lst: position = data_file.split('.')[0]; print position; for line in open(path+data_file): if line==None or line=='' or line=='\n': continue; posattr = {}; apList = line.split('$'); for ap in apList: if ap!=None and ap!='' and ap!='\n': ap_attr_list = ap.split('|'); posattr[ap_attr_list[1]] = -1*string.atoi(ap_attr_list[3]); posattr['position']=position; attrType[ap_attr_list[1]] = True; attrlist.append(posattr); posdata = []; # 去掉重复数据点 d = {}; # 记录数据点重复次数 count = len(attrlist); # 数据总数 print count; for attr in attrlist: onedata = []; for ap in attrType: try: onedata.append(attr[ap]); except: onedata.append(0); onedata.append(attr['position']); # 过滤掉重复数据 k = ','.join([str(i) for i in onedata]); d.setdefault(k, 0) d[k]+=1 if d[k]==1: posdata.append(onedata); attrlist = attrType.keys(); dataset = [ p[0:-1] for p in posdata]; labels = [p[-1:] for p in posdata]; dl=[]; # 封装到数据库 for dp in posdata : objd = {}; for i in range(len(attrlist)): objd[attrlist[i]] = dp[i] objd['position'] = dp[-1:][0]; k = ','.join([str(i) for i in dp]); objd['repeat'] = d[k]; objd['rate'] = d[k]*1.0/count; dl.append(objd); db = DBConnector(); db.save('locating', dl); return dataset, labels, attrlist;
class Count(object): def __init__(self): self.db = DBConnector("127.0.0.1", 3306, "jihyun", "jihyun", "test") def find(self, identifier, kind): with self.db.connect() as cursor: cursor.execute( """ SELECT * FROM nmf WHERE identifier = %s AND kind = %s """, (identifier, kind)) return cursor.fetchall() def upsert(self, identifier, kind): data = self.find(identifier, kind) if len(data) > 0: return update(identifier, kind) else: return insert(identifier, kind) def update(self, identifier, kind): with self.db.connect() as cursor: return cursor.execute( """ UPDATE FROM nmf WHERE identifier = %s AND kind = %s """, (identifier, kind)) == 1 def insert(self, identifier, kind): with self.db.connect() as cursor: x = cursor.execute( """ INSERT INTO nmf (identifier, kind, count, `updated-time`) VALUES (%s, %s, 1, now()) """, (identifier, kind)) return x.commit()
def weight(user_id): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor(MySQLdb.cursors.DictCursor) as cursor: cursor.execute(""" SELECT weight FROM healthcan WHERE user_id = %s ORDER BY `id` ASC; """, (user_id,)) con.commit() recodes = cursor.fetchall() return recodes
def find_ingredient(id): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # 対応する名前を返す cursor.execute( """ SELECT ingredient_name FROM table_ingredients WHERE ingredient_id = %s ;""", (id, )) con.commit() recode = cursor.fetchall() name = recode return name
def kind(user_id, kind): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute(""" SELECT id FROM table_wishList WHERE user_id = %s and kind = %s ORDER BY `quantity` ASC; """, (user_id,kind,)) con.commit() recodes = cursor.fetchall() w_list = [wishList.find(recode[0]) for recode in recodes] return w_list
def serect_by_gender(gender): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( """ SELECT member_id FROM table_member WHERE `gender` = %s; """, (gender, )) con.commit() recodes = cursor.fetchall() lm = [member.find(recode[0]) for recode in recodes] return lm
def _index(user_id): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute(""" SELECT id FROM healthcan WHERE user_id = %s; """, (user_id,)) con.commit() recodes = cursor.fetchall() ids = [recode[0] for recode in recodes] return ids
def find_idtoname(cid): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor( MySQLdb.cursors.DictCursor) as cursor: # 対応するidをリストで返す cursor.execute( """ SELECT cooking_name FROM table_cookings WHERE cooking_id = %s ;""", (cid, )) con.commit() recode = cursor.fetchall() cooking_name = recode[0] return cooking_name
def delete(self): if self.attr["id"] == None: return None with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # データの削除(DELETE) cursor.execute( """ DELETE FROM table_innerList WHERE id = %s; """, (self.attr["id"], )) con.commit() return self.attr["id"]
def test_snapshot_methods(): """Make an example issue tree, store it as a snapshot - then recreate it from database with DBConnector.make_tree(), and compare with original""" pg = DBConnector(**config['postgres']) # make example issues epic1 = IssueNode("EPIC-1") feat1 = IssueNode("FEAT-1") feat2 = IssueNode("FEAT-2") story2 = IssueNode("STORY-2") story3 = IssueNode("STORY-3") story5 = IssueNode("STORY-5") bug3 = IssueNode("BUG-3") bug4 = IssueNode("BUG-4") acc = IssueNode("ACC-1") change = IssueNode("CHR-4") # make tree, with root 'epic1' story2.add_child(bug3) story2.add_child(acc) story3.add_child(bug4) story5.add_child(change) feat1.add_child(story2) feat1.add_child(story5) feat2.add_child(story3) epic1.add_child(feat1) epic1.add_child(feat2) # store this tree as new snapshot new_snapshot_id = pg.put_snapshot(epic1, str(datetime.now())) # restore from db restored_epic1 = pg.pull_snapshot(new_snapshot_id) # TO-DO: override IssueNode.__eq__() so we can do # assert epic1 == restored_epic1 - for now, can # compare visually print(epic1) print("\n") print(restored_epic1)
def findMenuLog(uid): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( """ SELECT cooking_id FROM table_logs WHERE user_id = %s ORDER BY access_log_id DESC;""", (uid, )) results = cursor.fetchall() if (len(results) == 0): return 'null' # import pdb # pdb.set_trace() return results
def find_cooking_url(c_name): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # 対応するidをリストで返す cursor.execute( """ SELECT cooking_img_url FROM table_cookings WHERE cooking_name = %s ;""", (c_name, )) con.commit() recode = cursor.fetchall() curl = recode[0] curl2 = curl[0] return curl2
def _index(member_id): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # 対応するidをリストで返す cursor.execute( """ SELECT member_id FROM table_member WHERE member_id = %s; """, (member_id, )) con.commit() recodes = cursor.fetchall() ids = [recode[0] for recode in recodes] return ids
def serect_by_name(name): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: likename = '%' + name + '%' cursor.execute( """ SELECT member_id FROM table_member WHERE `name` LIKE %s; """, (likename, )) con.commit() recodes = cursor.fetchall() lm = [member.find(recode[0]) for recode in recodes] return lm
def favorite_update(_favorite_id, _id): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # データの保存(UPDATE) cursor.execute( """ UPDATE table_user SET favorite = %s WHERE id = %s; """, (_favorite_id, _id)) con.commit() return _id
def _db_save_insert(self): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( """ INSERT INTO table_user (email, password) VALUES (%s, %s); """, (self.attr["email"], self.attr["password"])) cursor.execute("SELECT last_insert_id();") results = cursor.fetchone() self.attr["id"] = results[0] con.commit() return self.attr["id"]
def post_cooking(uid, cid): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # データの追加 now = datetime.datetime.now() ymd = now.year * 100 + now.month + now.day cursor.execute( """ INSERT INTO table_logs (user_id,cooking_id,ymd) VALUES (%s,%s,%s); """, ( uid, cid, ymd, )) con.commit()
def get(self): if not self.current_user: self.redirect("/signin") return # サインインユーザーの取得 _id = tornado.escape.xhtml_escape(self.current_user) _signedInUser = user.find(int(_id)) with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor(MySQLdb.cursors.DictCursor) as cursor: cursor.execute(""" SELECT * FROM table_cashbook ORDER BY id; """) results = cursor.fetchall() if (len(results) == 0): print(None) return None cash_attrs = [ "id", "user_id", "ym", # "data", # "summary", # "detail", # "income", # "expenses", # "amount", # "last_updated" ] response = {} self.write("[".encode()) for data in results: for attr in cash_attrs: if(attr == "data"): response["%s" % attr] = str(data["%s" % attr]) else: response["%s" % attr] = data["%s" % attr] self.set_header("Content-Type", "application/json; charset=UTF-8") self.write(json.dumps(response)) if(results[-1] != data): self.write(','.encode()) self.write("]".encode())
def migrate(): with DBConnector(dbName=None) as con, con.cursor() as cursor: # データベース生成 cursor.execute('CREATE DATABASE IF NOT EXISTS db_%s;' % project.name()) # 生成したデータベースに移動 cursor.execute('USE db_%s;' % project.name()) # テーブル初期化(DROP) cursor.execute('DROP TABLE IF EXISTS table_fridge;') # テーブル初期化(CREATE) cursor.execute(""" CREATE TABLE `table_fridge` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `famiry_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`) ); """) con.commit()
def ym(user_id, ym): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( """ SELECT id FROM table_cashbook WHERE user_id = %s and ym = %s ORDER BY `date` ASC; """, ( user_id, ym, )) con.commit() recodes = cursor.fetchall() cb_list = [cashbook.find(recode[0]) for recode in recodes] return cb_list
def find_cooking_name(c_name): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # 対応するidをリストで返す cursor.execute( """ SELECT cooking_name FROM table_cookings WHERE cooking_name = %s ;""", (c_name, )) con.commit() recode = cursor.fetchall() if len(recode) == 0: return recode else: cname = recode[0] cname2 = cname[0] return cname2
def _db_save_update(self): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # データの保存(UPDATE) cursor.execute(""" UPDATE table_user SET name = %s, password = %s WHERE id = %s; """, (self.attr["name"], self.attr["password"], self.attr["id"])) con.commit() return self.attr["id"]
def find(id): with DBConnector(dbName='db_%s' % project.name()) as con, \ con.cursor(MySQLdb.cursors.DictCursor) as cursor: cursor.execute(""" SELECT * FROM table_fridge WHERE id = %s; """, (id,)) results = cursor.fetchall() if (len(results) == 0): return None data = results[0] f = fridge() f.attr["id"] = data["id"] f.attr["famiry_id"] = data["famiry_id"] return f
def find_ingredient_id(cooking_id): # ingredient_idのみを返す with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # 対応するidをリストで返す cursor.execute( """ SELECT ingredient_id FROM table_amounts WHERE cooking_id = %s; """, (cooking_id, )) con.commit() recodes = cursor.fetchall() # import pdb # pdb.set_trace() i_id = [recode[0] for recode in recodes] return i_id
def find_datetime(user_id, ym): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( ''' select * from table_cashbook where user_id = %s and ym = %s order by `date` asc; ''', ( user_id, ym, )) con.commit() recodes = cursor.fetchall() cb_list = [cashbook.find(recode[0]) for recode in recodes] return cb_list
def _db_save_insert(self): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute(""" INSERT INTO table_fridge (famiry_id) VALUES (%s);""", (self.attr["famiry_id"],)) cursor.execute("SELECT last_insert_id();") results = cursor.fetchone() self.attr["id"] = results[0] con.commit() return self.attr["id"]
def _db_save_update(self): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: # データの保存(UPDATE) cursor.execute(""" UPDATE table_fridge SET famiry_id = %s WHERE id = %s; """, ( self.attr["famiry_id"], self.attr["id"] )) con.commit() return self.attr["id"]
def find_nametoingredient_id(word): with DBConnector(dbName='db_%s' % project.name()) as con, con.cursor() as cursor: cursor.execute( """ SELECT ingredient_id FROM table_ingredients WHERE ingredient_name = %s; """, (word, )) results = cursor.fetchall() # import pdb # pdb.set_trace() if (len(results) == 0): return None recode = results[0] i_id = recode[0] return i_id
def migrate(): # データベースへの接続とカーソルの生成 with DBConnector(dbName=None) as con, con.cursor() as cursor: # データベース生成 cursor.execute('CREATE DATABASE IF NOT EXISTS db_%s;' % project.name()) # 生成したデータベースに移動 cursor.execute('USE db_%s;' % project.name()) # テーブル初期化(DROP) cursor.execute('DROP TABLE IF EXISTS table_recipes;') # テーブル初期化(CREATE) cursor.execute(""" CREATE TABLE `table_recipes` ( `recipe_id` int(100) unsigned NOT NULL AUTO_INCREMENT, `recipe_description` varchar(255) NOT NULL , PRIMARY KEY (`recipe_id`) ); """) # データの追加 cursor.execute(""" INSERT INTO table_recipes (recipe_description) VALUES ('パスタを茹でる'),('キャベツを切る'),('玉ねぎ、人参をみじん切りにする') ,('キャベツを茹でる'),('肉だねをつくる'),('キャベツで巻く') ,('コンソメスープで茹でる'),('大根を輪切りにする'),('鍋にお湯を沸かし酒少々入れる') ,('大根を鍋に入れる'),('酒、砂糖、醤油、みりんを入れ煮る'),('バターを泡だて器で混ぜ、、砂糖を入れる') ,('卵黄を加えてぐるぐると手早く回す'),('ゴムベラで粉っぽさがなくなるまで混ぜる'),('冷蔵庫で30分寝かせる') ,('焼く'),('香りのたつものから炒める'),('食材を全て入れしばらくしたら水をいれて沸かす') ,('豚肉を炒める'),('他の食材を入れる'),('キャベツがしなしなになるまで炒める') ,('ネギをみじん切りにする'),('小鍋に油を熱しネギを炒める'),('他の材料を加え煮立たせる') ,('じゃがいもと人参は乱切り玉ねぎはくし切り、白滝は適当'),('中華鍋に油を小さじ2入れて、強火で肉色変わるまで炒める'),('煮汁の材料を入れ沸騰したら灰汁を取り白滝を入れ中火でのまま落し蓋をし、煮汁がなくなるまで、煮詰める。') ,('ある程度煮詰めたところで混ぜ、再度落し蓋して煮詰める。'),('煮汁がなくなったら火を止めて、10分間程蒸らす。'),('完成!') ,('鳥もも肉から脂肪、皮を取り筋を切る。'),('肉の両面に塩、黒こしょうをふり、金属製のバットに乗せて20分室温で置く。'),('にんにくは薄切りに,キャベツはひと口大の角切りにします。レモンはくし型に切ります') ,('フライパンにオリーブ油とにんにくを入れて中火にかけ,にんにくが泡立ってきたら弱火にし、フライパンを傾けて横に寄せ、にんにくがきつね色になるまで揚げ、きつね色になったら取り出します。'),('同じフライパンで鶏もも肉を焼きます。弱火のまま、皮目から入れて10分以上、じっくり焼きます。フタはしません。ときどき菜箸などで肉を押さえ、皮をフライパンに密着させてやります。'),('弱火で10分焼いたら中火にします。中火で皮にカリカリとした焼き目をつけます。この時、キッチンペーパーでフライパンの余分な油を拭き取ります。') ,('肉の皮目を下にしてまな板に置き、包丁でひと口大に切ります。キャベツとミニトマトを添えた皿に皮を上にして盛り付け、刻みパセリ、フライドガーリックを散らし、レモンを添えたら皮カリカリチキンステーキの完成です'),('薄力粉、ホットケーキミックス、水を合わせてよくこねて生地を作ります') ,('ラップを敷き生地をおきラップをかけて挟み上から綿棒で生地を伸ばします'),('玉ねぎはスライスに、他の材料は食べやすい大きさに切り揃え皿に入れ500wのレンジで40秒チンします'),('生地をレンジに入れ500wのレンジで一分チンした後生地にケチャップを塗り材料を乗せチーズを乗せます') ,('ホイルにオリーブオイルを塗り生地をおいてオーブントースターで5分焼いたら完成です。'),('クッキングミキサーに玉ねぎとトマトを入れ、3秒×3~5回のパルス運転でみじん切りにする。'),('それに合挽肉と調味料を加えよく混ぜ、ふたをして500wのレンジで2分x3-4回加熱する') ,('お皿にごはんに盛り付け、中央に[2]のタコミートをのせて完成'),('玉ねぎを薄く千切りにする'),('耐熱容器にすべて投入し、ふんわりラップをしたら、レンジ500w7分加熱') ,('よく混ぜてとろみがついたら完成'),('豆腐はサイの目にきっておく。'),('にんにくとしょうがはみじん切りにしておく。') ,('フライパンにサラダ油を少し入れて、にんにくとしょうがを炒める。(中火)'),('ひき肉と豆板醤を追加して、色が変わるまで炒める。(中火)'),('調味料を追加して一煮立ちさせる。(中火)') ,('豆腐を入れて3分煮込む。(中火)'),('水溶き片栗粉でトロみをつけて、仕上げにごま油を回し入れる。'),('丼にごはんを盛って、その上にかけてネギを散らして完成') ,('玉ねぎ、ピーマンはみじん切り。ウインナーは小口切りにする'),('フライパンにサラダ油を少しひき、1の野菜等を炒め、玉ねぎが透き通ってきたら塩コショウ、ケチャップ、みりんを加えよく炒める'),('ケチャップがワントーン暗くなってきたら、ご飯を加え炒め、白いところがなくなるまでしっかり切るように炒める') ,('ボールに卵、マヨネーズ、砂糖、塩コショウを加えたらよく混ぜる'),('温めたフライパンに冷たいバターわ加え、素早くまんべんなくバターをひき、そこに溶いた卵を加える。'),('ガスなら外から内側へIHなら内側から外に向かって卵を手早く混ぜながら卵液を全体に広げ先ほどのケチャップライスの上にのせる') ,('のせない場合は、焼いた卵の真ん中にアーモンド型にご飯をのせオムレツを作るようにしながらご飯を半分包んだところで皿に盛る') """) con.commit()
def find_by_name(name): with DBConnector(dbName='db_%s' % project.name()) as con, \ con.cursor(MySQLdb.cursors.DictCursor) as cursor: cursor.execute(""" SELECT * FROM table_user WHERE name = %s; """, (name,)) results = cursor.fetchall() if (len(results) == 0): return None data = results[0] u = user() u.attr["id"] = data["id"] u.attr["name"] = data["name"] u.attr["password"] = data["password"] return u
def migrate(): with DBConnector(dbName=None) as con, con.cursor() as cursor: cursor.execute('CREATE DATABASE IF NOT EXISTS db_%s;' % project.name()) cursor.execute('USE db_%s;' % project.name()) cursor.execute('DROP TABLE IF EXISTS user;') cursor.execute(""" CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(256) NOT NULL DEFAULT '', `name` varchar(256) DEFAULT NULL, `password` varchar(256) DEFAULT NULL, `last_updated` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `OUTER_KEY` (`email`), KEY `KEY_INDEX` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """) con.commit()
def __init__(self, project, options): from log import LogManager from report import ReportManager from dev import DeviceManager from cli import CliManager from pressure import PressureManager from db import DBConnector self.logManager = LogManager(options.outputdir) self.logManager.setCmdLogLevel(project.stdLogLevel) self.logManager.setFileLogLevel(project.fileLogLevel) self.reportManager = ReportManager(options.outputdir) self.deviceManager = DeviceManager(project.devices) self.cliManager = CliManager(project.clis) if project.db is not None: self.dbConnector = DBConnector(project.db).createConnect() self.pressureManager = PressureManager(project.isPressure, project.repetition)
import commands import re import dropbox import pHash import os import tempfile import datetime import json import EXIF from PIL import Image import StringIO import dateutil.parser from db import DBConnector conn = DBConnector() highest_group = conn.get_highest_group() IMAGE_WIDTH = 350 def save_and_resize_image(user_name,file_name,local_name): img = Image.open(local_name) width, height = img.size ratio = float(height)/width rez = img.resize((IMAGE_WIDTH,(int(IMAGE_WIDTH*ratio))), Image.ANTIALIAS) new_file_name = "media/thumbnails/" + str(user_name) + file_name.split("/")[-1] rez.save(new_file_name,"JPEG") while(True): users = [x for x in conn.get_all_users()]
import dropbox import os import tempfile import json from db import DBConnector conn = DBConnector() images = [x for x in conn.get_images_all()] print images
def __init__(self): self.db = DBConnector("127.0.0.1", 3306, "jihyun", "jihyun", "test")