data_hacks icon indicating copy to clipboard operation
data_hacks copied to clipboard

Imported from other scripts.

Open jkawamoto opened this issue 9 years ago • 1 comments

It seems nice that other scripts can import and use data_hacks functions.

This pull request adds __init__.py to make it a package, and adds option classes. This PR also modifies run functions to take an output stream to control where they print output.

We can now print a bar char to stdout from another script:

import random
from data_hacks import bar_chart, BarChartOpt

def data():
    for _ in xrange(10000):
        yield str(random.randint(0, 100))

bar_chart(data(), BarChartOpt())

or store it in a file

import random
from data_hacks import bar_chart, BarChartOpt

def data():
    for _ in xrange(10000):
        yield str(random.randint(0, 100))

with open("result.txt", "w") as fp:
    bar_chart(data(), BarChartOpt(), fp)

jkawamoto avatar Dec 09 '16 21:12 jkawamoto

👍

I've been using a wrapper like this for now but this is not great, I'd much rather just invoke a module function.

def asciiPlot(title, items):

    click.secho('== {} =='.format(title), fg='blue')

    values = ['{} {}'.format(key, value)
              for key, value in items.iteritems()]

    # -p for percent
    # -A to aggregate (required)
    barChart = os.path.join(os.path.dirname(sys.executable), 'bar_chart.py')
    os.system("echo '%s' | %s -A -v -r" % ("\n".join(values), barChart))
    print

bsergean avatar Feb 22 '17 04:02 bsergean