Example #1
0
def week_forecast(weat_db, width):
    color_flag = True   # this will eventually get set in a config file
    #  color_flag = False   # this will eventually get set in a config file
    COLORS = utils.get_colors(color_flag=color_flag)
    #  width = utils.get_terminal_width()
    height = utils.get_terminal_height()
    #  there has to be a more elegant way to do the following:
    format_tups = temp_return_format()
    format_list = list(x[0] for x in format_tups['body'])
    labels = []
    #  labels = list(x[1] for x in format_tups['body'])
    for lab in format_tups['body']:
        if lab[2] == 1:
            labels.append(lab[1])
        elif lab[2] > 1:
            temp = []
            temp.append(lab[1])
            for i in range(lab[2] - 1):
                temp.append('')
            labels.extend(temp)
        else:
            raise ValueError("formatter returned negative value")
    box_width = get_box_width(width - utils.max_len(labels))
    #  box_height = get_box_height(format_list, height)
    box_height = get_box_height(format_tups['body'], height)
    formatted_days = format_day_list(weat_db, box_width,
                                     box_height, format_list,
                                     COLORS)
    formatted_header = format_header(None, utils.max_len(labels), box_width,
                                     COLORS)
    results = join_days(formatted_days, formatted_header, labels, width,
                        box_width, box_height, COLORS)
    return results
Example #2
0
def grid_forecast(weat_db, width):
    # first, initialize all the vars
    color_flag = True   # this will eventually get set in a config file
    num_days = len(weat_db)
    #  width = utils.get_terminal_width()
    height = utils.get_terminal_height()
    COLORS = utils.get_colors(color_flag=color_flag)
    max_cols, box_width = get_box_size(len(weat_db), width)
    cols = min(max_cols, width//box_width)
    rows = math.ceil(num_days/cols)
    while rows * 9 > height:    # truncate if too long:
        rows -= 1
    res = []
    for c in range(rows):
        res.append([])

    # now we build the thing
    day_nums = min(rows * max_cols, len(weat_db))
    for day_index in range(day_nums):
        # if the start of a row the whole list gets popped on there...
        if len(res[day_index//cols]) == 0:
            res[day_index//cols] = add_box_lines(
                new_new_forecast_day_format(weat_db[day_index], box_width,
                                            weat_db[0]['high']['fahrenheit'],
                                            weat_db[0]['low']['fahrenheit'],
                                            COLORS),
                day_index, day_nums, cols)
        # or we have to add line by line
        else:
            temp = add_box_lines(new_new_forecast_day_format(
                weat_db[day_index], box_width,
                weat_db[0]['high']['fahrenheit'],
                weat_db[0]['low']['fahrenheit'],
                COLORS),
                day_index, day_nums, cols)
            for lin in range(len(temp)):
                res[day_index//cols][lin] += temp[lin]

    # return the result
    # but first flatten, and add padding:
    padding = ' ' * ((width - len(res[0][0])) // 2)
    res2 = []
    for c in res:
        for lin in c:
            res2.append(padding + lin)
    return res2
Example #3
0
def day_temps_formatter(temps, times):
    import sys
    import datetime as dt
    home_dir = '/usr/self/weather/'
    if home_dir not in sys.path:
        sys.path.append(home_dir)
    import utils.utilities as utils
    import printers.colorfuncs as cf
    width = utils.get_terminal_width()

    # make the passed-in objects
    col_width = min(5, width // 25)
    col_height = 21
    COLOR = utils.get_colors()

    def date_func(zed):
        if isinstance(zed, dt.datetime) or isinstance(zed, dt.time):
            temp = zed
        else:
            temp = dt.datetime.fromtimestamp(int(zed))
        return temp.strftime('%H:%M')

    def scale_min(x):
        return int(x - (x % 10))
        #  return int(x - (x % 5))

    def scale_max(x):
        #  return int(x + (10 - (x % 5)))
        return int(x + (10 - (x % 10)))

    def height_func(mn, mx, col_height):
        scales = [1, 2.5, 5, 10, 20]
        ind = 0
        while ind < len(scales) and (mx - mn) / scales[ind] > (col_height - 1):
            ind += 1
        return min(mx - mn // scales[ind - 1], col_height)

    funcs = {}
    funcs['above'] = lambda x, y: "{}{}{}".format(' ', '+' * (y - 2), ' ')
    funcs['equal'] = lambda x, y: str(x)[:y]
    funcs['below'] = lambda x, y: "{}{}{}".format(' ', '-' * (y - 2), ' ')
    funcs['color_func'] = cf.bar_temp_color
    funcs['label_func'] = date_func
    funcs['label_color_func'] = cf.new_alternating_bg
    funcs['label_test_func'] = lambda x: x % 2 == 0
    funcs['label_formatter'] = label_formatter
    funcs['scale_format'] = lambda x: str(round(x, 1))
    funcs['scale_max'] = scale_max
    funcs['scale_min'] = scale_min
    funcs['height_func'] = height_func

    # clean the input lists
    zepochs = list(map(lambda x: dt.datetime.fromtimestamp(int(x)), times))
    #  cleaned = clean_by_hours(opened[target_keys[0]], zepochs)
    cleaned = clean_by_hours(temps, zepochs)
    full = list(z[0] for z in cleaned)
    epochs = list(z[1] for z in cleaned)

    # call the wrapped func
    return newer_cols_formatter(full, epochs, None, funcs, COLOR,
                                col_height=col_height,
                                col_width=col_width)