Skip to content

Commit

Permalink
Reorganize main function to clean up code
Browse files Browse the repository at this point in the history
This commit should be replaced when merging with master after pushing.

https://twitter.com/WesleyBaugh/status/626966439862255616
  • Loading branch information
bwbaugh committed Jul 31, 2015
1 parent 1b2f1d7 commit a56822d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions rotal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@ def main():
Intended to replace `sort | uniq -c` when the input is a stream.
"""
stdin = click.get_text_stream('stdin')
counter = collections.Counter()
for line in stdin:
click.clear()
line = line.rstrip('\n')
counter[line] += 1
click.echo(format_output(counter=counter))
for line in click.get_text_stream('stdin'):
increment_counter(line=line.rstrip('\n'), counter=counter)
output(
formatted_output=format_output(
# Ensure the order is consistent. Alphabetical order also
# more closely matches the behavior or `sort | uniq -c`.
ordered_counts=sorted(counter.iteritems()),
)
)


def format_output(counter):
return '\n'.join(get_output_lines(counter=counter))
def increment_counter(line, counter):
counter[line] += 1


def get_output_lines(counter):
return [
'\t'.join(unicode(x) for x in item)
# Ensure the order is consistent. Alphabetical order also
# more closely matches the behavior or `sort | uniq -c`.
for item in sorted(counter.iteritems())
]
def format_output(ordered_counts):
return '\n'.join(
'\t'.join(unicode(x) for x in item) for item in ordered_counts
)


def output(formatted_output):
click.clear()
click.echo(formatted_output)

0 comments on commit a56822d

Please sign in to comment.