Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, February 9, 2010

Python - A Real Beginners Guide

PYTHON

I. Intro
II. My First Program
III. Variables, Numbers and Strings
IV. String Manipulation
V. Operators
VI. Arrays/Lists
VII. Loops and Conditionals
a. The IF/ELIF/ELSE Statements
b. The WHILE Statement
VIII Bye bye! Good luck!

+---------------------------------------+
I. Intro
+---------------------------------------+
PYTHON! According to Python.org, this is what Python is...:

"Python is a dynamic object-oriented programming language that can be
used for many kinds of software development. It offers strong support
for integration with other languages and tools, comes with extensive
standard libraries, and can be learned in a few days. Many Python
programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code"

More simply however, Python is an easy-to-read, highly compatible,
oft-used programming language that is powerful and quick. It is often
compared to languages such as Perl, Ruby, Java, etc

ABOUT THIS GUIDE:
- This guide is intended for COMPLETE beginners to programming
languages, and is suggested to most as a first language, as it is as
mentioned before, an easy-to-read and simple language :)

REQUIREMENTS FOR THIS GUIDE:
- Having the latest version of Python installed on your computer. The
installers can be found at http://www.python.org/download/


+---------------------------------------+
II. My First Program
+---------------------------------------+
FOR the first program, we will be creating a small program that writes
"Hello World!" on screen.

Here is the code:
CODE :
__________________________________________________________________________
>>>print("Hello World!")
__________________________________________________________________________

BREAKDOWN:
print() - the typical function (functions will be covered more later
on) to write sentences and variables to the screen.

NOTE: It should also be noted that when using the print function, you
must remember that when you try print multiple things, e.g.
>>>print("Lol", "and", "hi"), a space will immediately be placed
between each part.

NOTE2: Instead of using print(), we also can type the string, number,
or variable and press enter in Python Shell to print the value of it
(remember that you will have to wrap strings in quotation marks if you
decide to use this method).


+---------------------------------------+
III. Variables, Numbers and Strings
+---------------------------------------+
VARIABLES are ways of holding information inside a word, to be able to
call the information back later in a program. The way to assign a value is..:
CODE :
__________________________________________________________________________
>>>#This is a comment line... Comment lines in Python are always
>>>#Preceded by a #
>>>#A variable can either hold a string (words) or a number
>>>varName = "variable"
>>>varName1 = 2009
__________________________________________________________________________

Variables can be changed later in the program. They can also hold a
formula or function. In addition, you can assign the same values to
several variables at once. For examples...

CODE :
__________________________________________________________________________
>>>#This variable holds a total of 25
>>>varFormula = 5*5
>>>
>>>#These variables both hold a value of 45
>>>varX = varY = 40+5
>>>
>>>#This variable holds a function that finds out the length of a
>>>#string or other value
>>>varLength = len("Hello World!")
>>>
>>>#The len() function does not work with numbers!
>>>#Using print(varLength) will output the length of "Hello World!"
>>>#which is 12.
>>>
>>>#Remember that when using the len() function, it counts every
>>>#character, including the space, and not just letters.
__________________________________________________________________________

+---------------------------------------+
IV. Word Indexing
+---------------------------------------+
BEING able to control strings is a vital part of programming. We
already know a couple of basic functions that allow us to manipulate or use strings, i.e. print() and len(). Another useful feature of Python is word indexing: being able to pick out certain letters in strings.

Here is an example of how to use word indexing:

CODE :
__________________________________________________________________________
>>>Hello = "Hello World!" #Establishes a variable...
>>>
>>>Hello[0] #Writes the first letter of the variable
"H"
>>>
>>>Hello[1:] #Writes all letters after the first letter
"ello World!"
>>>
>>>Hello[:5] #Writes all letters up to the sixth letter
"Hello"
>>>
>>>Hello[3:7] #Writes letters between the third and eighth letters
"lo W"
>>>
>>>Hello[3:-1] #Writes letters between position 3 and -1
"lo World"
__________________________________________________________________________

Yes, strangely enough, the first letter is indexed as [0]... Here is a
little table to illustrate index positions.
CODE :
__________________________________________________________________________
+---+---+---+---+
| A | B | C | D | = String
+---+---+---+---+
| 0 | 1 | 2 | 3 | = Positive indices
+---+---+---+---+
|-3 |-2 |-1 | ? | = Negative indices
+---+---+---+---+
__________________________________________________________________________

As you will see, there is absolutely no way of selecting a whole string using negative numbers... Of course, there are other ways of doing that.



+---------------------------------------+
V. Operators
+---------------------------------------+
OPERATORS are VERY important in Python... And sound much more
complicated than they really are. Operators are simply mathematical
symbols that do stuff for programming languages. Here is the table of
operators and how they work:
CODE :
__________________________________________________________________________
MATHEMATICAL OPERATORS - These produce a value
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|+..............|Addition.......|a + 5 = 10.............|
+---------------+---------------+-----------------------+
|-..............|Subtraction....|a - 5 = 0..............|
+---------------+---------------+-----------------------+
|*..............|Multiplication.|a * a = 25.............|
|**.............|Powers.........|a **3 = 25*25*25 = 125.|
+---------------+---------------+-----------------------+
|/..............|Division.......|a / a = 1..............|
|//.............|Rounds to floor|a // 0.3 = 16..........|
|%..............|Gives remainder|a % 2 = 1..............|
+---------------+---------------+-----------------------+

ASSIGNMENT OPERATORS - These give values to a variable
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|=..............|Assigns a value|a = 5..................|
+---------------+---------------+-----------------------+
|-=.............|Subtraction....|a-=10 is the same as...|
|...............|assigner.......|a = a - 10.............|
+---------------+---------------+-----------------------+
|*=.............|Multiplication.|a *= 10 is the same as.|
|...............|assigner.......|a = a * 10.............|
+---------------+---------------+-----------------------+
|**=............|Power assigner.|a **= 2 is the same as.|
|...............|...............|a = a ** 2.............|
+---------------+---------------+-----------------------+
|/=.............|Division.......|a /= 10................|
|...............|assigner.......|a = a / 10.............|
+---------------+---------------+-----------------------+
etc...etc...etc...etc...etc...etc...etc...etc...etc...etc

COMPARISON OPERATORS - These evaluate the truth of a statement
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|==.............|Is equal to....|a == 5.........TRUE....|
+---------------+---------------+-----------------------+
|!=.............|Not equal to...|a != 5.........FALSE...|
+---------------+---------------+-----------------------+
|>..............|More than......|a > a..........FALSE...|
|<..............|Less than......|a < 10.........TRUE....|
+---------------+---------------+-----------------------+
|>=.............|More than......|a >= a.........TRUE....|
|...............|or equal to....|a >= 6.........FALSE...|
+---------------+---------------+-----------------------+

BOOLEAN OPERATORS - These are used to link COMPARISON OPERATORS
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|and............|Both expression|a==5 and a>1...........|
|...............|must be true...|Evaluates to true......|
+---------------+---------------+-----------------------+
|or.............|Either expressi|a>6 or a<3.............|
|...............|on must be true|Evaluates to false.....|
+---------------+---------------+-----------------------+
|in.............|Checks if value|arr=["lol", "rofl"]....|
|...............|is in an array.|"lol" in arr...TRUE....|
+---------------+---------------+-----------------------+
__________________________________________________________________________

These are the main operators that you will need when programming in
Python.


+---------------------------------------+
VI. Arrays/Lists
+---------------------------------------+
ARRAYS/lists can be indexed exactly like words, but can store multiple
strings, numbers and variables. Also, you can append arrays: changing
them as you see fit. Here is the correct way of starting an array and changing it.
[/code]
>>>#Starts an array, 5 "elements" long
>>>arr = ["H", "e", "l", "l", "o"]
>>>
>>>arr[0] #Writes the first element in the array
"H"
>>>
>>>#Here are 3 functions that can change lists
>>>#These will be explained at the end of this chapter
>>>
>>>arr.append("!")
>>>arr # <- Prints the array
["H", "e", "l", "l", "o", "!"]
>>>
>>>arr.insert(1, "a")
>>>arr
["H", "a", "l", "l", "o", "!"]
>>>
>>>arr.extend(["Wo", "rld", "!"])
>>>arr
["H", "a", "l", "l", "o", "!", "Wo", "rld", "!"]
>>>
>>>arr.remove("!")
>>>arr
["H", "a", "l", "l", "o", "Wo", "rld", "!"]
[/code]
BREAKDOWN:
arrName.append(value):
........arrName.........- the array to be changed.
........append(value)...- the function to be used. Only takes one
................argument though. I.e, you can not add two elements to
................the array.

arrName.insert(pos,value):
........arrName.........- the array to be changed.
........insert(pos,v...)- function adds an element at the indicated
................position.

arrName.extend([value1, value2...])
........arrName.........- the array to be changed.
........extend([val...])- function adds multiple elements onto the
................end of a list. The elements to be added must be
................inside square brackets. This function only takes
................one argument - i.e. a list ([]).

arrName.remove(value):
........arrName.........- the array to be changed.
........remove(value)...- removes the first instance of the value
................inputted.

As mentioned before, arrays can be indexed the same way as words. Python also allows you to pick a specific letter/range of letters out
words in an array...

>>>arr = ["Hello", "World", "!"]
>>>arr[0][3:] #Picks first word, letters between 3 to end
"lo"

Also, a useful function for both single strings and arrays:

>>>arr = ["Hello, "World", "!"]
>>>arr.index("Hello") #Displays pos of value in array
0
>>>word = "Hello!"
>>>word.index("H") #Displays pos of first occurrence in array
0

Arrays can also be changed by putting them into formulas, e.g.

CODE :
__________________________________________________________________________
>>>arr = ["Hello", "World", "!"]
>>>arr = arr + [":P"]
>>>arr
["Hello", "World", "!", ":P"]
>>>
>>>arr = [90, 91] * 2
>>>arr
[90, 91, 90, 91]
__________________________________________________________________________

In conclusion to this section, arrays are a very much needed tool of a
serious programmer...


+---------------------------------------+
VII. Loops and Conditionals
+---------------------------------------+
+---------------------------------------+
VII.a. The IF/ELSE/ELIF Statements
+---------------------------------------+
IFS, elifs and elses help us develop a sense of control to our
programs... Without these, programs would be, in one word, pretty useless... *(...?)*

In this section, we will also create a whole program, that will
eventually be able let the user of the program input a number, and have the program count down from their number to 0.
Here is a summary of each term, and how they work, IF, ELIF, and ELSE,
including the syntax.

CODE :
__________________________________________________________________________
#For this part of the program, we need to make sure that the input
#is no more than 9, and no less than 1
#----------------------------------------------------IF---------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
__________________________________________________________________________

So far, the program lets the user input a number to the program and the program checks if that number is between 1 to 9, and if it is, the
program prints "Countdown initializing!". Unfortunately, if the number
is more than 9, or less than 1, nothing happens... That is BAD. So we
need to sort this out. Introducing ELIF...
CODE :
__________________________________________________________________________
#----------------------------------------------------ELIF------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")
__________________________________________________________________________

Ah, great! Now our program tells the user off for entering a number too high... But what if the input is less than 1? Well, seeing as we have covered 1-9, and anything above 9, we can now introduce "else"...
CODE :
__________________________________________________________________________
#----------------------------------------------------ELSE------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#Sets up a number array,
>>> #so that we can check if the
>>> #input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")
...else:
... print("Your number is a bit too low!")
__________________________________________________________________________

WONDERFUL! Our program now works. However, it is a bit plain, let us go on to WHILE.

+---------------------------------------+
VII.b. The WHILE Statement
+---------------------------------------+
SO, our program up to now is pretty good. We still need to make the
countdown though. And on top of that, we need the input to repeat, in
case the number that the user enters a number that is not between
1-9... This is where the WHILE statement comes into play. The WHILE
statement will repeat and repeat til the condition that makes it run
changes.

Here is the modified script, using the WHILE loop.
CODE :
__________________________________________________________________________
#----------------------------------------------------WHILE-----
>>>numArray = [1,2,3,4,5,6,7,8,9] #Sets up number array
>>>
>>>case = 0
>>>#^ This variable will allow us to switch from
>>>#one loop to the next... Read on
>>>
>>>while case == 0: ###Checks if case == 0 and then runs script
... a = int(input("Enter a number 1-9: "))
... if a in numArray: #checks if input is in numArray
... print("Countdown initializing...!")
... case += 1 #Tells case to increase by 1;
...
... else:
... pass
>>>#"pass" tells the program to do nothing
>>>#And go to the beginning of the loop again
>>>
>>>#The above loop will repeat til the user enters a valid number
>>>#then case will change to 1 and run the below script
>>>
>>>
>>>
>>>while case == 1: #Checks case == 1, i.e, it checks if the
>>> #number in the first loop is valid
... if a == 0:
... case +=1 #Passes the program to the next part
... else:
... print(a,"more loop(/s) til the bang!")
... a -= 1 #Tells the program to take 1 away from input
>>>
>>>#The above loop continues to repeat til a == 0, when it finally
>>>#carries you to the last part of the program...:
>>>
>>>if case == 2:
>>> print("BANG!")
__________________________________________________________________________

The above program can be broken down into four simple parts:
1. a preparation of the variables for the program to handle

2. loop one: this loop makes the user enter a number between
one to nine, and then goes to loop two, carrying across the
"a" integer input variable.

3. loop two: this loop counts down starting from "a", which
the user specified. When the loop turns variable "a" to 0, the
program changes case to 2, and sends it across to the final
part.

4. This part simply ends the program, printing 'BANG!' to the
screen.


+---------------------------------------+
VIII. Bye Bye! Good Luck!
+---------------------------------------+
SO, yeah, that's the very very very basics of Python. And I can only
hope that this all didn't sound like a complete load of donkey doodar
to you.

If this does not help you at all, many apologies. Here is a site that
might help you if you wanna take it slower:
http://docs.python.org/3.1/tutorial/ :)
Best of luck to all of you :)

BYE BYE!! <3
Code2004 // Connor

Post-script: please remember that ">>>" and "..." mean that this was written in Python Shell. DO NOT use them in a script, because it wont work xD!

Web Interaction Using Python

Introduction


In a number of the HTS programming missions you are asked to interact with the site from a program that you have written, as opposed to using a webbrowser. There are plenty of other applications for web interaction, however. I have written a few python scripts to download various data from websites (e.g. http://python.pastebin.com/f268e6319 )

I will cover two ways of getting data from a website (and in fact, sending data too). If there are any problems with the article, leave a comment.

All examples have been written in Python 2.6. There are quite a few differences between 2.6 and 3.0, but the only ones that should apply in the code snippets in this article involve the print function.

In Python 2.6 a simple hello world is this:
CODE :
__________________________________________________________________________
print "Hello World"
__________________________________________________________________________

In Python 3.0 it looks like this:
CODE :
__________________________________________________________________________
print("Hello World")
__________________________________________________________________________

It's a good idea, and I will switch to 3.0 when it is finally worn in, but for the moment I'm sticking with 2.6.
If there are problems with any of the code running as 3.0, try using the 2to3 script (It came preinstalled with Xubuntu for me.. not sure about on windows etc).

Anyway, now that's all covered, on with the article.

The Url Libraries

First of all we will start with a tutorial on the URL libraries. These are urllib and urllib2.

Let's immediately get started with some code.
CODE :
__________________________________________________________________________
import urllib2
url = "http://example.com"
website = urllib2.urlopen(url)
print website.read()
__________________________________________________________________________

Pretty simple code really, and for a lot of things it's all you need to know. It fetches the website "http://example.com" and stores the data as an instance on which we use the read() function to return the data retrieved from the site. Here are the functions:
instance.read() This returns the data retrieved from the site.
instance.info() This returns the HTTP message from the server, it has a lot of useful information in it including cookie info and server type.
instance.geturl() Returns the URL that was requested - seems pointless but we'll cover it in a second and you'll see why there is a point.
instance.getcode() Returns the HTTP status code. (e.g. 404, 200)

It's worth messing around with those a bit, rather than just taking my word for what they do.
I'll now just show a use of the geturl() function:
CODE :
__________________________________________________________________________
import urllib2
url = "http://google.com" # After google, try 'http://example.com'
website = urllib2.urlopen(url)
if url == website.geturl():
print "Website not redirected."
else:
print "Website redirected you."
__________________________________________________________________________

Why you'd want to do that, I don't know, but there's bound to be a use for it sometime. But that is one application of the geturl() function anyway.

Let's do a HTTP POST request now. They're pretty easy really, but can look a little complicated, so don't worry.
Before you look at the code, you might want to set up a server (or get some webspace) so you can test this out. A little PHP script like below will do the trick:
CODE :
__________________________________________________________________________
echo $_POST['test'];
?>
__________________________________________________________________________

And before anyone says anything about XSS - get lost - it's a testpage that will be up for 10 minutes on a server that noone cares about. But if you really are that bothered, you can use strip_tags() around that. (I say this because I can tell there'll be someone who will try and pipe up a clever comment).

Now then, we'll be introducing a new module for this (though it isn't strictly necessary, it's the best way I reckon). I will import the single function as we don't need any other functions from the module.

Okay, let's go:
CODE :
__________________________________________________________________________
import urllib2
from urllib import urlencode # new module and function

url = "http://localhost/test.php"
data = {'test':'lolwut'}
# you can add as much info as you want to this dictionary
# "test" is the label for the data, so that PHP script above
# should display "lolwut".

encoded_data = urlencode(data)
# remember that this is from that imported module, normally you'd
# use this: urllib.urlencode(data) if you used a normal import.

website = urllib2.urlopen(url, encoded_data)
print website.read() # That was pretty easy, right?
__________________________________________________________________________

Pretty straightforward, right?
Let's go onto HTTP Basic Authentication. This is more tricky. Here's the skeleton code for opening more advanced things, including HTTP authentication.
CODE :
__________________________________________________________________________
import urllib2

url = "http://example.com"

openerDirective1 = ...
openerDirective2 = ...

opener = urllib2.build_opener(openerDirective1, openerDirective2)

urllib2.install_opener(opener)

website = urllib2.urlopen(url)
__________________________________________________________________________

Okay, that's a lot more complicated. Note the "openerDirective"s. They are basically a way of adding headers to the urlopen requests.
You can have numerous opener directives, or just the one. You build them into an opener using the build_opener() function then install it, using install_opener(). After that, you can request a site and it will include the headers that you have specified.

Let's look at creating a HTTP Basic Authentication header.

CODE :
__________________________________________________________________________
authDirective = urllib2.HTTPBasicAuthHandler()
realm = "Webmail"
url = "http://example.com/webmail/"
username = "leethaxxer"
password = "letmein"
authDirective.add_password(realm, url, username, password)
__________________________________________________________________________

Then, we just build the opener and install it like we did in the skeleton code. Here:
CODE :
__________________________________________________________________________
opener = urllib2.build_opener(authDirective)
urllib2.install_opener(opener)
__________________________________________________________________________

I plan to write another article soon about cookies in Python, both as part of CGI and as part of requests with Urllib2.
Now I will move onto sockets and raw HTTP requests, and include cookies in that.

Socket Programming in Python

Socket programming is a really useful thing to learn - it's a must really, especially if you want to learn about security.

Again, we'll get some code out there straight away:
CODE :
__________________________________________________________________________
import socket
s = socket.socket()

host = "www.example.com"
port = 80
addr = (host, port)

s.connect(addr)
s.send("Something to send..")
print s.recv(1024)
# 1024 is the buffer size, you don't need to worry about it
# much right now.

s.close()
__________________________________________________________________________

There we are. We've created a socket, connected to "www.example.com" on port 80 then sent "Something to send.." and received something back, which has been printed out. Then we closed the socket, which isn't strictly necessary - but good practice.

Here's some better stuff to send, however:
CODE :
__________________________________________________________________________
GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n
__________________________________________________________________________

That's a simple HTTP GET request, asking for "index.html".
Here's a post request:
CODE :
__________________________________________________________________________
POST /index.php HTTP/1.1\r\n
Host: www.example.com\r\n
Content-Length: 11\r\n
\r\n
hello=world\r\n
__________________________________________________________________________

Now let's add a cookie to a HTTP GET:
CODE :
__________________________________________________________________________
GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n
Set-Cookie: hello=world\r\n
__________________________________________________________________________

There are other socket modes that can be set, this article is a very basic introduction. I would recommend reading this article if you want to learn more: http://www.amk.ca/python/howto/sockets/

Conclusion

Hopefully this article will help you begin to interact with the Internet using Python. It's just the beginning and I will work on follow-up articles. Good luck and thanks for reading.
dotty.

Writing Your Own Python Modules

Extending the Python Language with your own Modules


One of the best bits about Python is the extensive catalogue of modules that are around, and free, to extend the power and functionality of the language. Sometimes, however, it is necessary to create your own - perhaps because a particular feature isn't available in the standard build, perhaps a new technology has arrived that you wish to write an API for, or perhaps you are writing a large application where the breaking up of code is necessary for it to remain flexible and unbloated. Whatever your reasoning, this article serves as a basic introduction to module creation in Python. It covers both procedural and Object Orientated programming, but will not go into detailed specifics of either. However, it will give enough information for you to be able to begin developing your own "add-ons" to the Python language.

I'm using Python 2.6 so there will be slight differences if you are using another version.

Brief Overview of Modules in Python


Modules in Python are imported using the "import" statement.
CODE :
__________________________________________________________________________
import re
__________________________________________________________________________
That, for example, imports "re" - the regex module - into the document. The functions and classes belonging to "re" can be accessed like so:
CODE :
__________________________________________________________________________
import re
re.function(....)
__________________________________________________________________________

Modules on my system (Linux Xubuntu) are stored in /usr/lib/python2.6/
It'll be different on Windows - obviously - and may be on Mac and other Linux distros. You'll have to do some searching. On a *nix system, using this command should help you locate them:
CODE :
__________________________________________________________________________
locate urllib2
__________________________________________________________________________

However you don't have to stick all your modules in there, if the python interpretter can't find your module there, it'll look in the current working directory.
Modules have to end with .py too.

Your First Module


First I'm going to create two files: "firstmod.py" - which will contain the actual module - and "test.py" - with which we will test it (this could also be replaced by the Python console).

CODE :
__________________________________________________________________________
# firstmod.py

def doSomething(a,b):
c = a + b + 5
return c
__________________________________________________________________________

CODE :
__________________________________________________________________________
# test.py
import firstmod
print firstmod.doSomething(5,6)
__________________________________________________________________________

When we execute test.py, we get 16.
The code is pretty self explanatory. Now I'm going to do something a little different.
CODE :
__________________________________________________________________________
#test.py
from firstmod import doSomething
print doSomething(4,9)
__________________________________________________________________________

This time, instead of importing the entire module (which, admittedly, in this case is still only the one function), we are just importing that doSomething function. However, there's another difference. We're not accessing that function through the module itself but instead as we would any other function that we have created. That can be useful if you only want to access the one function, and don't want to have to type out a long module name each time you want to use it.
Another way of using the "from ... import ..." statement is with the wildcard '*'. Now you should know that that means everything, and that's exactly what it does. Import everything. In most cases you shouldn't do that, but there are cases when it is useful - especially when doing GUI programming, should you want to.
CODE :
__________________________________________________________________________
#test.py
from firstmod import *
print doSomething(2,6)
__________________________________________________________________________
See? Pretty simple.

[h3]Initialisation[/h3]
You may, for some reason, want to execute a few lines of code as soon as your module is imported. What for, I don't know, but perhaps there's an application. To do this, well.. just write that code in there.
CODE :
__________________________________________________________________________
# firstmod.py
def doSomething(a,b):
c = a + b + 5
return c

print "Initialised."
a = 5
__________________________________________________________________________

Okay, so I've made it print out "Initialised" and have set the variable "a" to 5. Now what does this look like when I import it?
I'll show you the output from the Python console.
CODE :
__________________________________________________________________________
>>> import firstmod
Initialised.
>>> print firstmod.a
5
>>> print a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
__________________________________________________________________________

That's pretty important to note. When you set a global variable in a module as I did above, you can access that variable as you would a function.
As expected, it works the same as functions when specifically imported:
CODE :
__________________________________________________________________________
>>> from firstmod import *
Initialised.
>>> print a
5
__________________________________________________________________________

However, here's a little quirk:
CODE :
__________________________________________________________________________
>>> from firstmod import doSomething
Initialised.
>>> print a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
>>> print firstmod.a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'firstmod' is not defined
__________________________________________________________________________

Hopefully those output snippets will help you understand how things work, I think that they explain better than I could, anyway.

[h3]Documentation[/h3]
Presumably at some point in your Python programming lives you will have used the help function. What you may not know is that the documentation for each module is in fact built in to the module.
Here's some code that will explain how it works.
CODE :
__________________________________________________________________________
# firstmod.py
"""This is an introduction to the firstmod module. It tells the user how to use the module as a whole, may give code snippets and generally give an idea of what the module does and how it does it. This can, and nearly always does, run over multiple lines."""
def doSomething(a,b):
"""This explains what this particular module does, so for example, in this circumstance I might say:
doSomething returns the sum of the two arguments with 5 added on."""
c = a + b + 5
return c
__________________________________________________________________________

Now try importing your module in the Python console, and typing "help(firstmod)".

[h3]Object Orientated[/h3]
Object Orientated programming is a tricky subject and can take a while to grasp completely. It's something that I like, but am wary of, and would advise anyone to read a lot about it before just jumping in.
I think the best way to teach this bit would be to actually create a working module - so that's what we'll do. We're going to write a 'toggle' variable: a variable with two specific positions and a function that switches between them. The best example is "True" or "False". Yep, they already have the boolean variable. Yep, you probably will never use this (though I've actually used it occasionally). Yep, it's pretty simple and a good learning script. So let's get started.

You can look at the full, finished code here: http://pastebin.com/m64220b4b
We won't be writing that much but feel free to add to the module if you want.

First of all I'm going to create a single file, "toggle.py". I will use the Python console for importing the module, etc.
Inside that, we'll have the class "toggle". Let's write some code.
CODE :
__________________________________________________________________________
# toggle.py
"""Toggle variable module"""
class toggle(object):
"""Toggle object with 2 possible values"""
def __init__(self, first, second):
"""This is executed as soon as the class is loaded"""
self.statements = first,second
self.position = 0
__________________________________________________________________________

Right, I'm going to stop there and explain a little. From now on, however, we'll be adding code to that class (not function), unless I state otherwise.
If we were to import this code and use it, we'd do it like this:
CODE :
__________________________________________________________________________
>>> import toggle
>>> t = toggle.toggle("option1","option2")
__________________________________________________________________________

The self in self.statements and self.position refers to the class itself. I won't explain it in detail because that'd involve basically explaining OOP, which would take another article. The self.statements variable is a tuple. It is assigned the variables passed to the __init__ function.

Okay, let's give this some functionality - we're going to make the object return a value when printed. Try this:
CODE :
__________________________________________________________________________
import toggle
t = toggle.toggle()
print t
__________________________________________________________________________

Now wouldn't it be nicer if it returned the current value then, rather than some weird value? Python is great in the way it lets you manipulate the language like that, here's how we'd do it:
CODE :
__________________________________________________________________________
# remember, we're adding a new function to the class here
def __str__(self):
"""Returns the current value"""
return str(self.statements[self.position])
__________________________________________________________________________

Now try the previous code and see what you get. Great, eh?
Currently though, you can't change the value. I'm going to introduce a toggle() function now.

CODE :
__________________________________________________________________________
def toggle(self):
"""Switch value, returns new value"""
if self.position == 0:
self.position = 1
else:
self.position = 0
return self.statements[self.position]
__________________________________________________________________________

Pretty self explanatory really. Let's try using it now:
CODE :
__________________________________________________________________________
>>> import toggle
>>> t = toggle.toggle("option1","option2")
>>> print t
option1
>>> t.toggle()
'option2'
>>> print t
option2
__________________________________________________________________________

[h3]Conclusion[/h3]
There we have it. That's your first basic module. There's a lot more you can do too and I encourage you to read up more on the subject. Writing modules is great fun, especially if you know Perl or another scripting language, and can port some modules over.
If you want to try out your skills writing modules, why not consider writing an IRC bot module? Or a noughts and crosses game, perhaps? There are plenty of applications.

Here's an old module I wrote for writing IRC bots. It isn't complete and it is probably pretty poorly coded in parts - I haven't looked at it for a while now. I'm not saying that it's perfect at all, but why don't you fix some problems it has? It definitely needs some regexs in there - message parsing has been done by hand :/
http://pastebin.com/m4e121ebf

Good luck, thanks for reading and have fun!
If there are any errors in the article, please leave a comment (or PM me if the comments system is down). Feel free to leave your opinions. :)
~thedotmaster

+++

Share |

"make something then You never be lost"

wibiya widget