def __init__(self): self.scales = [(_("Hour"),hours(1),MinuteLocator(interval=10)), (_("Half-hour"),minutes(30),MinuteLocator(interval=5)), (_("10-Minute"),minutes(10),MinuteLocator(interval=2)), (_("5-Minute"),minutes(5),MinuteLocator(interval=1)), (_("2-Minute"),minutes(2),SecondLocator(interval=20)), (_("Minute"),minutes(1),SecondLocator(interval=10)), (_("30-Seconds"),minutes(0.5),SecondLocator(interval=5)), (_("12-Seconds"),minutes(0.2),SecondLocator(interval=2)) ] self.cur = 0 self.pos = None
def time_truncate(dictionary, start_time, end_time): """ take a dictionary and reduce it to the desired interval. We do this rather than integrate it into time_to_tweets to keep time_to_tweets from becoming more complicated; this doesn't add any extra time, and requires only slightly more memory. It could be abstracted even more and called in time_to_tweets, but this lets us keep the logic separate and easier to debug. """ # We need to preprocess times into our desired mdates format. zero_time = list(dictionary.keys())[0] start_time = start_time.split(':') start_time = mdate.hours(float(start_time[0])) + \ mdate.minutes(float(start_time[1])) start_time = start_time + zero_time end_time = end_time.split(':') end_time = mdate.hours(float(end_time[0])) + \ mdate.minutes(float(end_time[1])) end_time = zero_time + end_time # we need a new dictionary. It costs a bit of memory. new_dict = OrderedDict({}) for time in dictionary: if time < start_time: continue elif time > start_time and time < end_time: new_dict[time] = dictionary[time] elif time > end_time: break else: break return new_dict
def time_to_tweets(filename, search_list, interval=15): """ This determines how to bucket the times for a line plot, and returns a dictionary. """ fi = open(filename, 'r', encoding='utf_8') time_dictionary = OrderedDict({}) interval_time = mdate.minutes(interval) # this is set to zero because of the way initialization works later curr_time = 0 for line in nonblank_lines(fi): jdata = json.loads(line) for term in search_list: if term in jdata['text'].lower(): timestamp = datetime.datetime.strptime(jdata['created_at'], '%a %b %d \ %H:%M:%S %z %Y') timename = mdate.date2num(timestamp) # initializes the dictionary. This is true only once, but we do # it here, rather than earlier, since it lets us initialize it # for the first timename (which we won't know until we've # started searching the JSON file). if time_dictionary == {}: time_dictionary[timename] = dict(search_list) time_dictionary[timename][term] += 1 curr_time = timename # this conditional should NOT be an elif! if timename < curr_time + interval_time: time_dictionary[curr_time][term] += 1 elif timename > curr_time + interval_time: curr_time = timename time_dictionary[curr_time] = dict(search_list) time_dictionary[curr_time][term] += 1 fi.close() return time_dictionary