Saturday, December 28, 2013

Python's Standard Library

     Have you ever heard someone say Python has batteries included? Have you wondered what the heck that means? Well, I can explain.
     Screw creating your own modules all the time, Python has standard ones built in! These standard modules let you find files, tell/count time, and much more. Remember everything we used with the import function? All of those are modules. The standard ones are the ones we never created in a different program! Should there be something you want to do that isn't a standard module, you should be able to find a download for them, hopefully for free (if you do that, be very careful, as it could be a virus or have viruses attached). So, what standard modules have we been using? Well, up until this point, you never knew you were doing the things in this article all along. The standard modules we know about so far are time and random.
     We already talked about time and random and how to use them, but there's a little more. With time.sleep(), you can make the program do nothing for a certain amount of time. There's a faster way to do it, though.

from time import sleep

     When you type that, it will make things a little faster to type, like this.

sleep(5)

     We do sleep(5) instead of time.sleep(5). We can do that because we imported sleep from time.
     There's also another thing to talk about with random. We all know about random.randint, the function we used in the fortune teller program, the multiplication one, and so on. We all know how random.randint works, but there's something else: random.random().
     random.random() will give you a random number between 0 and 1. Try it!

>>> import random
>>> print(random.random())
0.19406666194760636

     If you want a number that isn't between 0 and 1, you can multiply the answer. Let's try getting a number between 0 and 10.

>>> print(random.random() * 10)
8.2903170046567

     If you don't want a super specific float, you can use the int() function or the round() function, like this.

>>> print(int(random.random() * 10))
8

     The int function took our number and turned it into an integer!
     In this article, you learned about importing from a module, and random.random(). I challenge you to go to one of the old programs and change it with these methods!

Saturday, December 14, 2013

Namespaces

     There's more about modules. This topic is pretty complicated, it's on namespaces. What are they?
     Well, let's say you were in this school. We'll just simply call it School A. There is one person in your class (Class A) named Gerry, but in another class (Class B), someone else is named Gerry. Each class is its own namespace, and the students are inside of it.
     So, if you were to say "Gerry got glasses" in Class A, everyone would assume you mean the Gerry in that class' namespace. You would have to say something like "The other Gerry" for the class to know it's not the Gerry it has. If the principal calls Gerry to come to her office over the loudspeaker, she would get everyone with that name. She'd have to be specific, like saying the last name (i.e. Gerry Vargas) or saying which class the Gerry she wants is from. To make things easier, she could just go to the classroom and tell Gerry in person. The principal has to be specific which namespace has the Gerry she wants.
     So, let's move on to importing namespaces. Let's say a class from another school (Class C) has to teach in one of School A's portables for a while. If someone from Class C is named Harry, but no one from School A is, and the principal calls for Harry, no one will come. She'd have to hook up the loudspeaker to the portables, and to do that, we have to import the namespace (Class C). We could simply do this:

from ClassC import Harry

    Or, if we want to make things faster, we could just do

from ClassC import *
   
     Ta-da! * will automatically import everything in the namespace we chose (ClassC), and now the principal can call anyone from that class whenever she wants. She can just do callToOffice(Harry) or call someone else. Something like that, at least.
     I know, my explanation is a little unclear. We should be able to give my examples a nice shine later on.

Wednesday, December 4, 2013

Modules

     Alright, first, let's say happy birthday to my friend, Aarin Panell! We'll do a birthday theme for this article.
     So, we've been talking about collecting things together for a while. Lists, objects, etc. This should be the last term on that, and then we should start doing the really fun stuff! So, the last collectible programming term we're gonna be talking about is something called a module.
     I'm sure we've gotten errors and all those, but have you ever read them? I've been able to understand what the red text says fairly well. And in EVERY error box, you will see this:

File "C:\Python33\Geometry.py", line 22, in <module>

     See? It says module right there. Line 22, in <module>? Well, we can probably take a guess at what it is. All the programs we've done are simply modules. They can be used in a bigger program. So, a module is a piece of something! If you're still a little confused, take Lego blocks for example. Each little piece represents one of the simple programs we've written during our time at Programmer's Peak. You take the blocks, and use them to build a bigger construction. This construction is an entire program, and each piece is a simple module.
     Modules are little programs that are used as pieces for big programs. Did we get that cleared up? So, why don't we try it? Let's create a new program, a simple little one. To make things easier, we might want to have the same file name. I saved mine as OurModule.py. This is where we get into Aarin's birthday theme. He said he wanted something with presents, so let's do that!



presentPossibilities=['Candy bar', 'Dragon', 'Can of beans', 'Skateboard', 'Hammer', 'Paintball gun']
chooser=0
number=5


     Ha. Getting a dragon for your birthday would be the best thing ever (or not, depending on how dangerous it is), and a can of beans would be the worst. Well, guess what? We are going to make another program and shove this module in it!
     So, how do we do that? Well, don't we understand some functions? They are normal English words. And the function we need is something we've used for quite a while now, to trigger random things and sleeping time. That function is import!
     Let's open up a new window and do this:

import OurModule, time, random

print("Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.")
thing=input()
print("And the present is...")
time.sleep(2)
print("A", presentPossibilities[chooser], "!")

     Well, what're you waiting for? Let's run it!

Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.

And the present is...
Traceback (most recent call last):
  File "C:/Python33/AarinParty.py", line 7, in <module>
    print("A", presentPossibilities[chooser], "!")
NameError: name 'presentPossibilities' is not defined

     That was a disappointment.
     Why'd this happen? Well, computers are dumb. You have to be this specific to make them work. Well, more specific. We never told our big program that presentPossibilities and chooser are part of Module.py! We'll have to fix it, like this:

print("A", OurModule.presentPossibilities[OurModule.chooser], "!")

     Yay! Run the program and you should get this:

Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.

And the present is...
A Candy bar !

     It works! Wait... Try running the program more than once...

Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.

And the present is...
A Candy bar !

Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.

And the present is...
A Candy bar !

Hello. Welcome to Aarin Panell's basic birthday present simulator! Press enter to continue.

And the present is...
A Candy bar !

     Here's a problem! It doesn't stop doing a candy bar! That's my challenge. Try to debug Aarin's birthday party! Here's a tip: this is very similar to the program in the

A Quick Tip - The Randint of a List

article. It may point you in the right direction!
P.S. Don't be surprised if the modules article keeps going on. I may not have completely covered the topic yet.