def attempt_2(): players = get_players() player_regex = '|'.join(players) filter_base = r'(%s) \[INFO\] (%s)(?:\[.*?\]){,1}' % (timestamp_re, player_regex) filter_base += r' (%s)' login_filter = re.compile(filter_base % '|'.join(login_strings)) logoff_filter = re.compile(filter_base % '|'.join(logoff_strings)) shutdown_filter = re.compile(r'(%s) \[INFO\] Stopping the server' % timestamp_re) actions = { 'login': login_filter, 'logoff': logoff_filter, 'shutdown': shutdown_filter, } logged_in = {} playtimes = {} for player in players: playtimes[player] = 0 mclog = MCLogFile() for line in mclog.info_no_convo(): action = None for action in actions: result = actions[action].match(line) if result: break else: continue if action == 'login': player = result.group(2) logging.info('logging in player "%s"' % player) if player in logged_in: logging.warning('player "%s" already logged in' % player) logging.debug(result.group(0)) else: logged_in[result.group(2)] = timestamp2time(result.group(1)) logging.debug(logged_in.keys()) elif action == 'logoff': player = result.group(2) logging.info('logging off player "%s"' % player) if player in logged_in: playtimes[player] += timediff(logged_in[player], timestamp2time(result.group(1))) del logged_in[player] else: logging.warning('player "%s" was never logged in' % player) logging.debug(result.group(0)) logging.debug(logged_in.keys()) elif action == 'shutdown': logging.info('shutting down') shutdown_time = timestamp2time(result.group(1)) for player in logged_in: playtimes[player] += timediff(logged_in[player], shutdown_time) logged_in.clear() logging.debug(logged_in.keys()) for player in playtimes: print '%s: %f' % (player, playtimes[player])
def get_lists(logfile='server.log'): """Return 2 lists of tuples (player, timestamp) of player logins and logouts, and a third list of logout times. """ if not logfile: logfile = 'server.log' players = get_players() player_regex = '|'.join(players) logged_in = [] logged_out = [] shutdown = [] filter_base = r'(%s) \[INFO\] (%s)(?:\[.*?\]){,1}' % (timestamp_re, player_regex) filter_base += r' (%s)' login_filter = re.compile(filter_base % '|'.join(login_strings)) logoff_filter = re.compile(filter_base % '|'.join(logoff_strings)) shutdown_filter = re.compile(r'(%s) \[INFO\] Stopping the server' % timestamp_re) mclog = MCLogFile(logfile) for line in mclog.info_no_convo(): result = login_filter.match(line) if result: logged_in.append(get_stamp(result)) continue result = logoff_filter.match(line) if result: logged_out.append(get_stamp(result)) continue result = shutdown_filter.match(line) if result: shutdown.append(timestamp2time(result.group(1))) return logged_in, logged_out, shutdown
def crea_vento(self): cur = self.db.cur anno = self.anno mese = self.mese ngiorni = self.ngiorni vento = [] for giorno in range(1, ngiorni + 1): day = datetime.datetime(anno, mese, giorno) cmd = """ SELECT vvel, data FROM Raw WHERE data BETWEEN datetime('{giorno}', 'start of day', '-1 days','+19 hours', '+10 minutes') AND datetime('{giorno}', 'start of day', '+19 hours') AND vvel IS NOT NULL """.format(giorno=day) dati = cur.execute(cmd).fetchall() v = ([x[0] for x in dati]) km_tot = sum(v) * 0.6 # 1[m/s] * 10 [minuti] = 0.6 [km] # todo: km_media da capire cosa si intende km_media = statistics.mean(v) * 0.6 # media km all'ora vmax, ora = max(dati) ora = util.timestamp2time(ora) ora = ora.hour + 1 if ora.minute else ora.hour rec = (day, km_tot, km_media, vmax, ora) vento.append(rec) return vento
def plot_candle_chart(df, type=0, pic_name='candle_chart'): # 对数据进行整理 # df.set_index(df['tick_id'], drop=True, inplace=True) # df = df[['open', 'high', 'low', 'close']] # 作图 ll = np.arange(0, len(df), 1) x_times = [] for row in ll: tick_id = df.loc[row, 'tick_id'] x_times.append(timestamp2time(tick_id, precision="minute")) # xticks = df.index[ll] print(x_times) xticks = x_times fig, ax = plt.subplots() candlestick2_ohlc(ax, df['open'].values, df['high'].values, df['low'].values, df['close'].values, width=0.6, colorup='r', colordown='g', alpha=1) #eg:xticks(np.arange(12), calendar.month_name[1:13], rotation=20) plt.xticks(ll, xticks, rotation=60) plt.title(pic_name) plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) # 保存数据 if type == 0: plt.show() elif type == 1: plt.savefig("picture//" + pic_name + ".png") else: plt.savefig("picture//" + pic_name + ".png") plt.show()
def get_stamp(match_object): """Returns a tuple of player, timestamp based on minecraft log timestamp""" player = match_object.group(2) timestamp = timestamp2time(match_object.group(1)) return (player, timestamp)
def crea_pioggia(self, cur): anno = self.anno mese = self.mese ngiorni = calendar.monthrange(anno, mese)[1] mm = [] for giorno in range(1, ngiorni + 1): day = datetime.datetime(anno, mese, giorno) cmd_mm8 = """ SELECT date(data), sum(mm), sum(durata) FROM Orario WHERE data BETWEEN datetime('{giorno}', 'start of day', '-4 hours') AND datetime('{giorno}', 'start of day', '+8 hours') """.format(giorno=day) dati_mm8 = cur.execute(cmd_mm8).fetchall()[0] cmd_mm14 = """ SELECT sum(mm), sum(durata) FROM Orario WHERE data BETWEEN datetime('{giorno}', 'start of day', '+9 hours') AND datetime('{giorno}', 'start of day', '+14 hours') """.format(giorno=day) dati_mm14 = cur.execute(cmd_mm14).fetchall()[0] cmd_mm19 = """ SELECT sum(mm), sum(durata) FROM Orario WHERE data BETWEEN datetime('{giorno}', 'start of day', '+15 hours') AND datetime('{giorno}', 'start of day', '+19 hours') """.format(giorno=day) dati_mm19 = cur.execute(cmd_mm19).fetchall()[0] totale = sum((dati_mm8[1], dati_mm14[0], dati_mm19[0])) durata = sum((dati_mm8[2], dati_mm14[1], dati_mm19[1])) ore, minuti = util.minuti2ore_minuti(durata) durata = datetime.time(ore, minuti).strftime(TIME_SHORT_PF) # todo: max pioggia in un'ora calcolata prendendo dalla tabella oraria il max cmd = """ SELECT data, max(mm) FROM Orario WHERE data BETWEEN datetime('{giorno}', 'start of day', '-4 hours') AND datetime('{giorno}', 'start of day', '+19 hours') """.format(giorno=day) dati = cur.execute(cmd).fetchall()[0] ora_max, mm_max = (util.timestamp2time(dati[0]).hour, dati[1]) if dati[1] else (None, None) rec = [ dati_mm8[0], dati_mm8[1], dati_mm14[0], dati_mm19[0], totale, durata, mm_max, ora_max, ] mm.append(rec) return mm