Created
June 26, 2012 11:44
-
-
Save Lysander/2995324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # Copyright (C) 2012 Christian Hausknecht <> | |
| # | |
| # This program is free software: you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation, either version 3 of the License, or | |
| # (at your option) any later version. | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| # GNU General Public License for more details. | |
| # You should have received a copy of the GNU General Public License | |
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| # | |
| ''' | |
| .. module: calc.py | |
| Simple script that calculates the sum of machine values per week. | |
| Input file looks like this: | |
| M010 # 50.0 # 23/12 # | |
| M140 # 12.5 # 23/12 # | |
| M010 # 37.5 # 23/12 # | |
| M160 # 75.0 # 24/12 # | |
| ... | |
| The column represents 1) the machine ID 2) a value 3) week of a year | |
| .. moduleauthor:: Christian Hausknecht <[email protected]> | |
| ''' | |
| import csv | |
| import argparse | |
| from pprint import pprint | |
| from collections import defaultdict | |
| from functools import partial | |
| from operator import itemgetter | |
| def load_csv(filename): | |
| with open(filename) as f: | |
| reader = csv.reader(f, delimiter="#") | |
| for row in reader: | |
| if row: | |
| yield list(col.strip() for col in row if col.strip()) | |
| def calc_sums(rows): | |
| weeks = set() | |
| machines = set() | |
| sums = defaultdict(partial(defaultdict, int)) | |
| for row in rows: | |
| machine, value, week = row | |
| sums[machine][week] += float(value) | |
| machines.add(machine) | |
| weeks.add(week) | |
| return machines, weeks, sums | |
| def create_table(machines, weeks, sums): | |
| table = list() | |
| for week in sorted(weeks): | |
| row = [week] | |
| for machine in sorted(machines): | |
| row.append(sums[machine][week]) | |
| table.append(row) | |
| return table | |
| def calc_total(table): | |
| cols = [itemgetter(index) for index, _ in enumerate(table[0][1:], 1)] | |
| return [sum(col(row) for row in table) for col in cols] | |
| def dumps(table, total): | |
| lines = list() | |
| for row in table: | |
| lines.append("{} # {}".format(row[0], " # ".join(map(str, row[1:])))) | |
| lines.append("Gesamt # {}".format(" # ".join(map(str, total)))) | |
| return "\n".join(lines) | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Calculation of machine " | |
| "usages.") | |
| parser.add_argument("filename", metavar="FILE", help='CSV input file') | |
| parser.add_argument("-o", metavar="OUTPUT", help='output') | |
| args = parser.parse_args() | |
| machines, weeks, sums = calc_sums(load_csv(args.filename)) | |
| table = create_table(machines, weeks, sums) | |
| total = calc_total(table) | |
| if args.o: | |
| with open(args.o, "w") as f: | |
| f.write(dumps(table, total)) | |
| else: | |
| print(dumps(table, total)) | |
| if __name__ == "__main__": | |
| main() |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| M010 # 50.0 # 23/12 # | |
| M140 # 12.5 # 23/12 # | |
| M010 # 37.5 # 23/12 # | |
| M160 # 75.0 # 24/12 # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment