Skip to content

Instantly share code, notes, and snippets.

@ben432rew
Last active November 12, 2015 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben432rew/e3269a186de057afcd0c to your computer and use it in GitHub Desktop.
Save ben432rew/e3269a186de057afcd0c to your computer and use it in GitHub Desktop.
suggestions for ninja gold. it's pretty good as is. just some tips :)

use the right tools

Don't use a try/except clause for checking for a key/value pair and providing a default. Python has .get builtin for this purpose:

# no
		try: 
			session['activities_arr']
		except:
			session['activities_arr'] = []

# yes
    activities_array = session.get('activities_arr', [])

Also use the .format method for formating strings:

# no 
'You earned ' + str(farm_capital) + ' from the farm! ' + date
# yes
'You earned {} from the farm! {}'.format(farm_capital, date)

write clean, object-oriented, DRY code

You're repeating yourself a lot in your server.py. If you ever see yourself repeating the same code more than once, stop and see if there's a different way you can do it without repeating yourself. You also have a many-branched if/else. If you ever start to have more than a few branches in an if/else, see if you can do some kind of mapping instead. Your function is also over 50 lines long, which should be a red flag. Functions should only do One Thing.

Also, comment your code more, may seem unnecessary/annoying but get in the habit, you'll appreciate it later when you start building large projects because you won't be able to remember why you wrote old code.

Don't just copy paste this code into your repo. Read it carefully, and see how it is different from your code, and how it addresses each concern mentioned above. If you don't understand anything, even if it seems like a little thing, please ask me about it.

def generate_number(action):
    """
    Returns a random number between 10 and 20.  If the action is a 'casino',
    the number may be negative instead
    """
    random_number = random.randrange(10, 20)
    if action == 'casino' and random.randint(0, 1):
        return -1 * random_number
    else:
        return random_number


def build_message(action, capital):
    """ Build message for activities_arr """
    date = datetime.now().strftime('(%Y/%m/%d %r)')
    earned_lost = 'earned' if capital > 0 else 'lost'
    if action == 'casino' and capital > 0:
        optional_message = 'BALL OUT! '
    elif capital < 0:
        optional_message = 'HANG YOUR HEAD IN SHAME! '
    else:
        optional_message = ''
    return 'You {} {} from the {}! {} {}'.format(earned_lost,
                                                 capital,
                                                 action,
                                                 optional_message,
                                                 date)


@app.route('/')
def init_root():
    if not session.get('gold'):
        session['gold'] = 0
    return render_template('index.html')


@app.route('/money_machine', methods=['POST'])
def process():
    """ Calculate new gold amount, add message to message array """
    activities_array = session.get('activities_arr', [])
    action = request.form.get('action')

    capital = generate_number(action)
    message = build_message(action, capital)
    activities_array.insert(0, message)

    session['gold'] += capital
    session['activities_arr'] = activities_array
    return redirect('/')

Couple minor style things:

Use soft tabs, set your sublime text default to be soft

(10, 20) not (10,20)

When you have functions that aren't class methods, put two lines between them:

# no
    return render_template('index.html')

@app.route('/money_machine', methods=['POST'])

# yes

    return render_template('index.html')


@app.route('/money_machine', methods=['POST'])

Also, do you have GitGutter installed on your sublime text yet?

@erikasland
Copy link

if action == 'casino' and random.randint(0, 1):

This is confusing to me.
Its condition is if action is equal to casino and is 0 or 1 return negative. What role does random.randint play?

Would it make more sense to check it like...
if action == 'casino' and random <= 15:

Are you using the randint() function to produce a true or false boolean (1 = true, 0 = false)? If so, why?

@erikasland
Copy link

I am a bit unfamiliar with .format(). If you could explain it a bit better to me, that would be wonderful.
My current understanding is that .format() inserts its parameters into the curly braces.

So if I were to do something like...

descriptor = "stinks"
action = "put it down"

"The dog {}. Can we please just {}!?".format(descriptor, action)

It would return a string like...

"The dog stinks. Can we please just put it down!?"

@erikasland
Copy link

How is the .get() function working?

Is it like...

If activities array is present, grab that value. If it does not exist, create it.

@erikasland
Copy link

I don't have a Git Gutter yet. What is it?

@ben432rew
Copy link
Author

the integer 1 is truthy, and the integer 0 is falsey, so you're right about me using that. randint gives a random number given a range. I gave it a range of 0 to 1, so it will randomly pick 0 or 1. So basically half the time (if the action is casino) it will change random_number into a negative number.

@ben432rew
Copy link
Author

Yes you're right about how .format works

as to .get:

y = x.get('happy', 5) is the same as:

try:
    y = x['happy']
except KeyError:
    y = 5

So get takes 2 arguments, the first is the key to the dictionary, and the second is a default value in case the dictionary doesn't have that key. Further reading on format and get

The python docs are your new home for the next couple decades. Get cozy

GitGutter

@ben432rew
Copy link
Author

ps if you used if action == 'casino' and random <= 15: as your conditional, anytime someone got less than 156points on the casino they would have a negative, but if they got more than 15 they'd get a positive number. So if you want them to be winning larger numbers and losing smaller numbers, sure, it just depends on what you want the user experience to be but keeping with what was there already it would be a different play, you know?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment