Created
December 29, 2015 04:58
-
-
Save mmulich/f6536e734c1172542a1e 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
# -*- coding: utf-8 -*- | |
import os | |
import json | |
here = os.path.abspath(os.path.dirname(__file__)) | |
DEFAULT_VENDING_SIZE = (12, 4,) # 4 trays by 12 spaces | |
# class NoDataFound(Exception): | |
# """Raised when there is no pre-existing data.""" | |
# def provision_vending_machine(data_filepath): | |
# """\ | |
# Provision the vending machine's slots from a persistent datastore. | |
# """ | |
# # Create an empty dataset when the data | |
# if not os.path.exists(data_filepath): | |
# raise NoDataFound() | |
# with open(data_filepath, 'r') as f: | |
# data = json.loads(f.read()) | |
# return data | |
def provision_empty_vending_slots(size=DEFAULT_VENDING_SIZE): | |
"""\ | |
Provision a vending machine's data structures and fill it with empty data. | |
""" | |
x, y = size | |
return [[None] * x] * y | |
# def main(): | |
# # Swap out whatever datastore / persistence layer you'd like. | |
# data_filename = 'vending.data' | |
# data_filepath = os.path.join(here, data_filename) | |
# # Provision the vending slots from the existing data source. | |
# try: | |
# slots = provision_vending_slots(data_filepath) | |
# except NoDataFound: | |
# slots = provision_empty_vending_slots() | |
# fill_vending_machine(slots) | |
# # Write the data out so that it can be reused on next run. | |
# write_vending_slots(data_filepath, slots) | |
# # Return 0, because it let's the terminal know no errors were encountered. | |
# return 0 | |
from pprint import pprint as pp | |
def get_item_counts(machine, vending_ids): | |
items = {} | |
for tray in machine: | |
for slot in tray: | |
items.setdefault(slot, 0) | |
items[slot] = items[slot] + 1 | |
labeled_items = {} | |
for key, value in items.items(): | |
labeled_items[vending_ids[key]] = value | |
return labeled_items | |
def find_item_positon(machine, item_id): | |
for x, tray in enumerate(machine): | |
for y, slot in enumerate(tray): | |
if slot == item_id: # found! | |
return x, y | |
def main(): | |
machine = provision_empty_vending_slots() | |
vending_idents = {None: 'empty', 1: 'cookies', 2: 'candies'} | |
machine[0] = [1, None] * 6 | |
machine[1] = [2] * 12 | |
pp(machine) | |
print('-' * 40) | |
item_count = get_item_counts(machine, vending_idents) | |
pp(item_count) | |
print('-' * 40) | |
user_selection = 2 | |
position = find_item_positon(machine, user_selection) | |
print(position) | |
x, y = position | |
print(machine[x][y]) | |
print('-' * 40) | |
# pp(machine) | |
# When you run the script from the commandline, | |
# this if statement will evaluate to True. | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment