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
| import sys
reload(sys) sys.setdefaultencoding("utf8") import torndb import datetime
HOST = "" DB_NAME = "" DB_USER = "" DB_PASSWORD = ""
class TimeRecord(object): def __init__(self): self.id = -1 self.user = "" self.content = "" self.cost = 0 self.create_time = ""
self.xuhao = 0
def setup(self, row): self.id = row["id"] self.user = row["user"] self.content = row["content"] self.cost = row["cost"] self.create_time = row["create_time"]
def __str__(self): return "%(create_time)s %(index)s\t%(content)s\t%(cost)dh" % { "create_time": self.create_time[:11].replace("-", ""), "index": "%d[id:%d]" % (self.xuhao, self.id), "content": self.content, "cost": self.cost }
class DataHelper(object): def __init__(self): self.db = torndb.Connection(HOST, DB_NAME, user=DB_USER, password=DB_PASSWORD)
def save_record(self, timeRecord): sql = "INSERT INTO timecost (user,content,cost) VALUES (%s,%s,%s)" self.db.insert(sql, timeRecord.user, timeRecord.content, timeRecord.cost)
def query_by_id_or_user(self, **kwargs): sql = "SELECT * FROM timecost" if kwargs: sql += " WHERE" for key in kwargs.keys(): if key == "id": sql += " id=%s and" % kwargs["id"] elif key == "user": sql += " user='%s' and" % kwargs["user"] sql = sql[:len(sql) - 3] if sql.find("id") < 0: sql += " order by id desc" return [self.__wrap_record(row) for row in self.db.query(sql)]
def query_week_data(self, last_week=0, user=None): end = last_week * 7 start = end + 7 sql = "SELECT * FROM timecost where create_time>'%s' and create_time<='%s'" % ( self.time_str(start), self.time_str(end)) if user: sql += " and user='%s'" % user sql += " order by id desc" return [self.__wrap_record(row) for row in self.db.query(sql)]
def delete_data(self, id): sql = "DELETE FROM timecost where id=%s" % id self.db.execute(sql)
@staticmethod def time_str(day_before=0): return (datetime.datetime.now() - datetime.timedelta(days=day_before, hours=8)).strftime("%Y-%m-%d %H:%M:%S")
@staticmethod def time_str_add(date_time, hours=8): return (date_time + datetime.timedelta(hours=hours)).strftime( "%Y-%m-%d %H:%M:%S")
def __wrap_record(self, row): record = TimeRecord() record.setup(row) record.create_time = self.time_str_add(record.create_time) return record
useage = """ useage: cost save -u user -content content -cost cost cost query -id id | -week week (-user user)? cost delete -id id """
def value(sign, array): index = -1 for i in range(len(array)): if sign == array[i]: index = i break if index > 0 and index + 1 < len(array): return array[index + 1] return ""
if __name__ == "__main__": try: if "save" in sys.argv: user = value("-user", sys.argv) content = value("-content", sys.argv) cost_str = value("-cost", sys.argv) cost = -1 if cost_str.endswith("d") or cost_str.endswith("D"): cost = int(cost_str[:len(cost_str) - 1]) * 8 elif cost_str.endswith("h") or cost_str.endswith("H"): cost = int(cost_str[:len(cost_str) - 1]) if cost < 0: raise Exception("耗时参数异常") if not user: raise Exception("用户参数异常") if not content: raise Exception("内容参数异常") dataHelper = DataHelper() record = TimeRecord() record.user = user record.content = content record.cost = cost dataHelper.save_record(record) print "保存成功" elif "query" in sys.argv: dataHelper = DataHelper() i = 1 if "-id" in sys.argv: for item in dataHelper.query_by_id_or_user(id=int(value("-id", sys.argv))): item.xuhao = i i += 1 print item elif "-week" in sys.argv: week = value("-week", sys.argv) if not week or not week.isdigit(): week = "0" user = value("-user", sys.argv) for item in dataHelper.query_week_data(last_week=int(week), user=user): item.xuhao = i i += 1 print item elif "-user" in sys.argv: for item in dataHelper.query_by_id_or_user(user=value("-user", sys.argv)): item.xuhao = i i += 1 print item else: raise Exception("查询参数异常") elif "delete" in sys.argv: if "-id" in sys.argv: dataHelper = DataHelper() dataHelper.delete_data(id=int(value("-id", sys.argv))) print "删除 %s 成功" % value("-id", sys.argv) else: print "删除参数异常" else: print useage except Exception, e: print str(e) print useage
|