Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imported from other scripts. #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jkawamoto
Copy link

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)

@bsergean
Copy link

👍

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants