Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Add ranking mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed May 7, 2021
1 parent 608a91e commit 4c3c681
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ invoice_dir: data/invoices
import_dir: imports

# Output
dist: build
invoice_file: invoices.pdf
match_dir: build/matches
rank_dir: build/rankings
63 changes: 54 additions & 9 deletions lib/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def match(self, year, quarter) -> None:

# Filter & merge matched invoices
invoices = build_path(self.config['invoice_dir'], '*.pdf')
self.export_pdf(matches, invoices)
self.export_invoices(matches, invoices)

# Write results to CSV files
self.export_csv(matches, self.config['match_dir'])
self.export_matches(matches, self.config['match_dir'])


def match_payments(self, payments, orders, infos) -> list:
Expand Down Expand Up @@ -131,9 +131,7 @@ def match_infos(self, order, infos) -> list:
return []


# PDF tasks

def export_pdf(self, matches, invoice_list) -> None:
def export_invoices(self, matches, invoice_list) -> None:
# Prepare invoice data
invoices = {basename(invoice).split('-')[2][:-4]: invoice for invoice in invoice_list}

Expand Down Expand Up @@ -165,11 +163,58 @@ def export_pdf(self, matches, invoice_list) -> None:
merger.write(invoice_file)


def export_csv(self, raw, base_dir):
csv_data = dedupe(raw)

for code, data in group_data(csv_data).items():
def export_matches(self, matches, base_dir):
for code, data in group_data(matches).items():
# Assign CSV file path & create directory if necessary
csv_file = join(base_dir, code, code + '.csv')
create_path(csv_file)

# Write matches to CSV file
DataFrame(data).to_csv(csv_file, index=False)


def rank(self, year, quarter) -> None:
# Select order files to be analyzed
order_files = build_path(self.config['order_dir'], year=year, quarter=quarter)

# Fetch their content
orders = load_json(order_files)

data = {}

# Sum up number of sold articles
for order in orders:
for isbn, quantity in order['Bestellung'].items():
if isbn not in data:
data[isbn] = quantity

else:
data[isbn] = data[isbn] + quantity

ranking = []

for isbn, quantity in data.items():
item = {}

item['ISBN'] = isbn
item['Anzahl'] = quantity

ranking.append(item)

# Sort sold articles by quantity & in descending order
ranking.sort(key=itemgetter('Anzahl'), reverse=True)

# Generate unique filename
file_name = basename(order_files[0])[:-5] + '_' + basename(order_files[-1])[:-5] + '_' + str(sum(data.values()))

# Write ranking to CSV file
self.export_csv(ranking, file_name)


def export_csv(self, ranking, identifier) -> None:
# Assign CSV file path & create directory if necessary
csv_file = join(self.config['rank_dir'], identifier + '.csv')
create_path(csv_file)

# Write CSV file
DataFrame(ranking).to_csv(csv_file, index=False)
6 changes: 3 additions & 3 deletions lib/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ def build_path(

# No year => all files
if year is None:
return glob(join(base_path, regex))
return sorted(glob(join(base_path, regex)))

# Generate months if quarter was given
if quarter is not None and 1 <= int(quarter) <= 4:
months = [month + 3 * (int(quarter) - 1) for month in [1, 2, 3]]

# Year, but no months => given year
if months is None:
return glob(join(base_path, str(year) + '-' + regex))
return sorted(glob(join(base_path, str(year) + '-' + regex)))

# Year & months => given months for given year
return [join(base_path, '-'.join([str(year), str(month).zfill(2) + '.json'])) for month in months]
return sorted([join(base_path, '-'.join([str(year), str(month).zfill(2) + '.json'])) for month in months])


def create_path(file_path) -> None:
Expand Down
23 changes: 23 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,28 @@ def match(
click.echo(' done!')


@click.option('-c', '--config', help='Path to configuration file.')
@click.option('-y', '--year', help='Year.')
@click.option('-q', '--quarter', help='Quarter.')
@cli.command()
def rank(
config: str,
year: int = None,
quarter: int = None,
):
"""Ranks sold books"""

# Load config
config = load_config(config)

# Initialize object
handler = Inspector(config)

# Load & match data sources
click.echo('Ranking data ..', nl=False)
handler.rank(year, quarter)
click.echo(' done!')


if __name__ == '__main__':
cli()

0 comments on commit 4c3c681

Please sign in to comment.