Last active
October 14, 2018 01:12
-
-
Save cpatdowling/6d793b8687db1468ed677e8f3b693038 to your computer and use it in GitHub Desktop.
Demonstrating a global reference variable in action for a friend
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
import copy | |
import random | |
global market_ref | |
market_ref = {'item_1': 100.0, | |
'item_2': 200.0, | |
'item_3': 100.0} | |
def market_shift(): | |
#updates the global market baseline | |
#check out this random walk on Wall St. | |
for key in market_ref: | |
market_ref[key] += random.uniform(-0.1, 0.1) * market_ref[key] | |
class Planet | |
def __init__(self, params['volatility']): | |
#common attributes | |
self.market = copy.copy(market_ref) | |
self.volatility = params['volatility'] | |
#planet specific attributes | |
self.gravity = 2.0 #this could be a base you modify adaptively or a params object | |
def market_update(self): | |
#this permanently modifies the planet market, but not the global reference baseline above | |
#planet specific market randomization strategy | |
for key in self.market: | |
self.market[key] += random.uniform(-self.volatility, self.volatility) * self.market[key] | |
class Space_Station | |
def __init__(self, params): | |
#common attributes | |
self.market = copy.copy(market_ref) | |
self.volatility = params['volatility'] | |
self.name = random.choice(params['names']) | |
#space station specific attributes | |
self.defense_rating = 200 #this could be a base you modify adaptively or a params object | |
def market_update(self, params) | |
#space station specific randomization strategy | |
#here I can define a completely different update strategy | |
if params['faction'] == "Imperial": | |
#double prices | |
for key in self.market: | |
self.market[key] = 2.0 * self.market[key] | |
else: | |
#randomize but discount | |
for key in self.market: | |
self.market[key] += random.uniform(-self.volatility, self.volatility) * self.market[key] | |
#I could do a higher level abstraction and just define one class as "location" and decide | |
#if its a planet or a space station as an attribute, but this demonstrates the purpose of a | |
#global variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment