PythonOzsvaldPyNewbieSeries
From Wiki
This is the support page for Ian Ozsvald's Python Newbies on XP video set. Please feel free to dip-in and extend this information.
Contents |
Using IDLE v1.2 (3rd video)
Generally, you should run IDLE from the start menu. If you want to run it from within the Python interpreter, it is possible, but requires a few lines of code. Tal has provided some code in the forum, if you want to give it a go.
hello.py
def multipleHellos(name, repeats=2):
'''prints Hello name multiple times'''
for i in range(repeats):
print "Hello", name
multipleHellos('ian')
Build a simple file-reader using PyDev (6th video)
Numbers.py
def readNumbers():
f=open('nbrs.txt')
lines = f.readlines()
print lines
f.close()
nbrs = []
for line in lines:
nbrAsStr = line.strip()
nbrAsInt = int(nbrAsStr)
nbrs.append(nbrAsInt)
return nbrs
nbrs = readNumbers()
print nbrs
nbrs.txt
22 33 100 50 20 1 2 3
Unit Tests for Dependable Code (8th video)
Installing nosetests
[Update March 2008] I have recorded a video that shows you how to install Nosetests on Windows (and how to install easy_install) - the videos are applicable to other platforms too: http://showmedo.com/videos/video?name=2100000&fromSeriesID=210
For Windows
First you'll need to download ez_setup.py from http://peak.telecommunity.com/DevCenter/EasyInstall and run:
python ez_setup.py
from the location where you saved 'ez_setup.py'. If 'python.exe' isn't found by Windows then you need to configure your system's Path environment variable - see my 4th video 'Running Python at command line' for instructions. There are notes on this at the peak site as well.
Having run ez_setup.py, now you'll have a new command 'easy_install'. We will use easy_install to install 'nose' which gives us 'nosetests' (which we use to run the unit tests). There are instructions at somethingaboutorange (http://somethingaboutorange.com/mrl/projects/nose/) but all you need to do from the command line is:
easy_install nose
For Ubuntu
On Ubuntu and probably all other Debian disrtibutions it is quick and easy to install nosetests, just run:
sudo apt-get install python-nose python-profiler
After this you'll have nosetests installed. You can confirm this by running 'nosetests' at the command line for any directory, you'll get something like:
C:\temp>nosetests ------------------------ Ran 0 tests in 0.681s OK C:\temp>
Note that if you get a 'not found' error for nosetests then check that your Python path is setup (if you're on Windows) here: http://showmedo.com/videos/video?name=960000&fromSeriesID=96
To further confirm that nosetests is working, write a simple Python file called 'test.py':
def test1():
pass
and run 'nosetests' from that directory and you'll get:
--------------------- Ran 1 test in 0.020s OK
If you have trouble with the above then follow the video-page's link into the forum and ask me a question, I'll do what I can to help.
checkNumbers.py
"checkNumbers.py January 2007"
import math
def isAGoodNumber(stringToTest):
""" Aim: test that stringToTest matches
our criteria of a 'good number', i.e.
it is positive, non-fractional number
Good numbers: 0, 22, 50.0
Bad items: -1, 22.2, 'abc'
"""
isGood = False
try:
result = float(stringToTest)
numberIsNotFractional = math.floor(result) == result
if numberIsNotFractional:
isGood = True
if result < 0:
isGood = False
except ValueError:
pass
return isGood
testCheckNumbers.py
import unittest
import checkNumbers
def testAcceptsPositiveInteger():
assert checkNumbers.isAGoodNumber("22")
def testFailsOnString():
assert checkNumbers.isAGoodNumber("abc") == False
def testFailsOnNegativeInteger():
assert checkNumbers.isAGoodNumber("-1") == False
def testFailsOnFractionalNumber():
assert checkNumbers.isAGoodNumber("22.2") == False
def testAcceptsOnFractionalwholeNumber():
assert checkNumbers.isAGoodNumber("50.0")

