def create_random_mix(): """ Generate a random mix """ available_drinks = default_database().get_drinks() available_components = [] for drink in available_drinks: if not isinstance(drink, Mix): available_components.append(drink) nr_of_components = random.randint(2, 4) components = [ random.choice(available_components) for _ in range(0, nr_of_components) ] mix = Mix() mix.name = "CNOC" for comp in components: mix.insert_drink(comp) if comp.alc_perc < 7: mix.insert_drink(comp) return mix
def get_random_mix(self): drinks = default_database().get_drinks() mix = self._random.choice(drinks) while not isinstance(mix, Mix): mix = self._random.choice(drinks) return mix
def run_server(): """ Run the default quartjes server. """ from quartjes.controllers.stock_exchange import StockExchange from quartjes.controllers.stock_exchange2 import StockExchange2 from quartjes.controllers.database import default_database from quartjes.controllers.random_mixer import run_random_mixer from quartjes.controllers.mix_discounter import run_mix_discounter server = ServerConnector(default_port) print("Using stock exchange version %i" % stock_exchange_version) if stock_exchange_version == 1: exchange = StockExchange() server.register_service(exchange, "stock_exchange") else: exchange = StockExchange2() server.register_service(exchange, "stock_exchange") server.register_service(default_database(), "database") run_random_mixer() run_mix_discounter() server.start() print("Server started on port %i" % default_port) print("Press enter to stop") raw_input() print("Stopping server") server.stop() exchange.stop()
def find_existing_mixes(): """ Search the database for already existing random mixes. Searches for drinks using the default name given to random mixes. Adds them to the list of active mixes and if the number exceeds the maximum, some mixes are removed. """ global random_mixes current_drinks = default_database().get_drinks() for drink in current_drinks: if isinstance(drink, Mix): if drink.name.startswith(mix_name_prefix): random_mixes.append(drink) while len(random_mixes) > max_random_mixes: remove = random_mixes.pop(0) try: default_database().remove(remove) except KeyError: pass # Drink might have been removed by admin
def _run_test_server(): from quartjes.connector.server import ServerConnector from quartjes.controllers.database import default_database server = ServerConnector(test_port) server.register_service(default_database(), "database") server.start() import time while True: time.sleep(100)
def add_new_mix(): """ Create a random mix and add it to the database. If the maximum number of mixes is already present, the oldest is removed. """ global random_counter global random_mixes mix = create_random_mix() mix.name = mix_name_format % (random_counter) random_counter += 1 if random_counter > max_random_mixes * 2: random_counter = 1 if len(random_mixes) > max_random_mixes: remove = random_mixes.pop(0) try: default_database().remove(remove) except KeyError: pass # Drink might have been removed by admin random_mixes.append(mix) default_database().add(mix)
def create_random_mix(): """ Generate a random mix """ available_drinks = default_database().get_drinks() available_components = [] for drink in available_drinks: if not isinstance(drink, Mix): available_components.append(drink) nr_of_components = random.randint(2, 4) components = [random.choice(available_components) for _ in range(0, nr_of_components)] mix = Mix() mix.name = "CNOC" for comp in components: mix.insert_drink(comp) if comp.alc_perc < 7: mix.insert_drink(comp) return mix
in reversed(drink.price_history)] return data def order_by_relative_price_change(drinks, time_window): """ Create a list of drinks ordered by their relative price change. Parameters ---------- drinks Drinks to analyze. time_window Number of history samples to go back. """ diff_data = calculate_price_difference(drinks) price_data = [(drink, price_data[time_window if len(price_data) > time_window else len(price_data)-1][1]) for drink, price_data in diff_data.items()] price_data.sort(cmp=lambda x, y: cmp(x[1], y[1])) return price_data if __name__ == '__main__': simulate(60*60) #default_database()._dump_drinks() test_data = order_by_relative_price_change(default_database().get_drinks(), 10) import pprint pprint.pprint(test_data)