Beispiel #1
0
def process_default(out_file, algorithm):
    global CONFIG

    csv_file = open(out_file, 'w', newline='')
    csv_writer = csv.writer(csv_file,
                            delimiter=' ',
                            quotechar='|',
                            quoting=csv.QUOTE_MINIMAL)

    engine = create_engine(CONFIG['mysql'])

    print(f'Processing with {algorithm} algorithm...')
    stores = CONFIG['stores']
    barcodes = CONFIG['barcodes']
    periods = CONFIG['periods']
    iter_cnt = len(stores) * len(barcodes) * len(periods)
    bar = ChargingBar('Waiting...', max=iter_cnt)
    bar.start()

    for store_id in stores:
        for barcode in barcodes:
            bar.message = f'[Store: {store_id}] {str(barcode).ljust(13, " ")}'
            bar.update()

            df_barcode = get_barcode_daily_sales(engine, store_id, barcode)

            for period in periods:
                forecast_from_date = arrow.get(period['date'], 'DD.MM.YYYY')
                forecast_before_date = forecast_from_date.shift(
                    days=period['days'])

                forecast = do_forecast(algorithm, df_barcode,
                                       forecast_from_date,
                                       forecast_before_date)

                csv_writer.writerow([
                    store_id, barcode, period['date'], period['days'], forecast
                ])
                bar.next()

    bar.finish()
    csv_file.close()
    print(f'\nDone. Result was written to {args.output}')
Beispiel #2
0
def main(args):

    from progress.bar import ChargingBar as Bar

    ap = argparse.ArgumentParser()
    ap.add_argument("-o",
                    "--output-file",
                    nargs="?",
                    help="save content as file")
    ap.add_argument("url",
                    nargs="?",
                    help="the url to read from (default to clipboard)")

    ns = ap.parse_args(args)
    url = ns.url or clipboard.string()
    output_file = ns.output_file or url.split("/")[-1]

    try:

        # print('Opening: %s\n' % url)
        u = urlopen(url)

        meta = u.info()
        try:
            file_size = int(meta["Content-Length"])
        except (IndexError, ValueError, TypeError):
            file_size = 0

        # print("Save as: {} ".format(output_file), end="")
        # print("({} bytes)".format(file_size if file_size else "???"))

        with open(output_file, "wb") as f:

            file_size_dl = 0
            block_sz = 8192

            if file_size != 0 and file_size is not None:
                bar = Bar("Downloading", max=100)
            else:
                bar = None

            _n = 0

            while True:
                buf = u.read(block_sz)
                if not buf:
                    break
                file_size_dl += len(buf)
                f.write(buf)

                if bar is not None:
                    n = int(file_size_dl * 100.0 / file_size)

                    if n == _n:
                        continue

                    _n = n

                    now = monotonic()
                    dt = now - bar._ts
                    bar.update_avg(n, dt)
                    bar._ts = now
                    bar.index = n
                    bar.update()

            if bar is not None:
                bar.finish()

    except Exception as e:
        print(e)
        print("Unable to download file: %s" % url)
        return 1

    return 0
Beispiel #3
0
def main(args):

    from progress.bar import ChargingBar as Bar

    ap = argparse.ArgumentParser()
    ap.add_argument('-o',
                    '--output-file',
                    nargs='?',
                    help='save content as file')
    ap.add_argument('url',
                    nargs='?',
                    help='the url to read from (default to clipboard)')

    ns = ap.parse_args(args)
    url = ns.url or clipboard.get()
    output_file = ns.output_file or url.split('/')[-1]

    try:

        #print('Opening: %s\n' % url)
        u = urlopen(url)

        meta = u.info()
        try:
            file_size = int(meta["Content-Length"])
        except (IndexError, ValueError, TypeError):
            file_size = 0

        #print("Save as: {} ".format(output_file), end="")
        #print("({} bytes)".format(file_size if file_size else "???"))

        with open(output_file, 'wb') as f:
            file_size_dl = 0
            block_sz = 8192

            bar = Bar('Downloading', max=100)

            _n = 0

            while True:
                buf = u.read(block_sz)
                if not buf:
                    break
                file_size_dl += len(buf)
                f.write(buf)

                n = int(file_size_dl * 100. / file_size)

                if n == _n:
                    continue

                _n = n
                now = monotonic()
                dt = now - bar._ts
                bar.update_avg(n, dt)
                bar._ts = now
                bar.index = n
                bar.update()

            bar.finish()

    except Exception as e:
        print(e)
        print('Unable to download file: %s' % url)
        return 1

    return 0
Beispiel #4
0
def process_short(out_file, in_file, algorithm):
    global CONFIG

    beg_date = arrow.get('2019-10-01', 'YYYY-MM-DD')
    end_date = arrow.get('2020-01-31', 'YYYY-MM-DD')
    write_stdout(f'Forecast from {beg_date} to {end_date}\n')  # 123 days

    lines_count = 0
    if wc and sed:
        write_stdout(f'Counting {in_file} non-blank lines...  ')
        lines_count = int(wc(sed(r'/^\s*$/d', in_file), '-l'))
        print(lines_count)
    ops_count = lines_count * 123

    out_csv_file = open(out_file, 'w', newline='')
    in_csv_file = open(in_file, 'r', newline='\n')
    csv_writer = csv.writer(out_csv_file,
                            delimiter=' ',
                            quotechar='|',
                            quoting=csv.QUOTE_MINIMAL)
    csv_reader = csv.reader(in_csv_file, delimiter=',')

    engine = create_engine(CONFIG['mysql'])
    print(f'Processing short output with {algorithm} algorithm...')

    bar = None
    if ops_count > 0:
        bar = ChargingBar('Waiting...', max=ops_count)
        bar.start()

    i = 0
    dfs = {}
    for row in csv_reader:
        if row is not None and len(row):
            store_id = int(row[0])
            barcode = int(row[1])

            key = f'{store_id}-{barcode}'
            try:
                df_barcode = dfs[key]
            except KeyError:
                df_barcode = get_barcode_daily_sales(engine, store_id, barcode)
                dfs[key] = df_barcode

            for j, d in enumerate(arrow.Arrow.range('day', beg_date,
                                                    end_date)):
                forecast_from_date = d
                forecast_before_date = forecast_from_date.shift(days=5)

                forecast = do_forecast(algorithm, df_barcode,
                                       forecast_from_date,
                                       forecast_before_date)
                csv_writer.writerow([int(round(forecast))])

                if bar:
                    curr_op = i * 123 + j
                    if curr_op % 5 == 0:
                        bar.message = f'{curr_op} of {ops_count}'
                        bar.update()
                    bar.next()
            i += 1

    bar.message = 'Done'
    bar.update()

    out_csv_file.close()
    in_csv_file.close()
Beispiel #5
0
                print(f'Processing...')
                stores = config['stores']
                barcodes = config['barcodes']
                periods = config['periods']
                iter_cnt = len(stores) * len(barcodes) * len(periods)
                bar = ChargingBar('Waiting...', max=iter_cnt)
                bar.start()

                for s in range(len(stores)):
                    store_id = stores[s]

                    for b in range(len(barcodes)):
                        barcode = barcodes[b]
                        bar.message = f'[Store: {store_id}] {str(barcode).ljust(13, " ")}'
                        bar.update()

                        df_barcode = get_barcode_daily_sales(engine, store_id, barcode)

                        for plot in range(len(periods)):
                            period = periods[plot]
                            today = arrow.get(period['date'], 'DD.MM.YYYY')
                            beg = today.shift(days=1)
                            end = beg.shift(days=period['days'])

                            sales = df_barcode.loc[beg.date():end.date()]
                            sales_sum = sales['quantity'].sum()

                            csv_writer.writerow([
                                store_id,
                                barcode,