try:
...
except ValueError as e:
print(e)
Tuesday, April 3, 2012
Python: How to print the ValueError Error Message
Python Tkinter: How to set the window size without using canvas
Reference: http://stackoverflow.com/q/9996599/1276534
Credit: George, Bryan Oakley
Here is the code on how to set the window size without using canvas, it is great if you just starting, or do not want to use canvas to do this. You can specify your dimension in your frame, and then use pack_progate(0) flag to tell tkinter to use your size.
Reference: stackoverflow 1, stackoverflow 2
Credit: George, Bryan Oakley
Here is the code on how to set the window size without using canvas, it is great if you just starting, or do not want to use canvas to do this. You can specify your dimension in your frame, and then use pack_progate(0) flag to tell tkinter to use your size.
import tkinter as tk root = tk.Tk() frame = tk.Frame(root, width=400, height=400) frame.pack_propagate(0) # set the flag to use the size frame.pack() # remember to pack it or else it will not be pack textBox = tk.Label(frame, text="(x,y): ") textBox.pack() root.mainloop()Note: If your frame is not big enough to how the items, it will expand to fit your items). And the pack_progate(0) is a flag, it does not replace pack() method, you still have to call it or otherwise it will not appear. Hope this help.
Reference: stackoverflow 1, stackoverflow 2
Labels:
Python,
stackoverflow goodies,
tkinter,
window size
Monday, April 2, 2012
Python: [] vs. {} vs. ()
Reference: http://stackoverflow.com/q/4407873/1276534
Authors: Zolomon, Greg Hewgill, Andrew Jaffe
It is a good note post to make sure we remember what is [] vs. {} vs. ()
Authors: Zolomon, Greg Hewgill, Andrew Jaffe
It is a good note post to make sure we remember what is [] vs. {} vs. ()
() - tuple
A tuple is a sequence of items that can't be changed (immutable).[] - list
A list is a sequence of items that can be changed (mutable).{} - dictionary or set
A dictionary is a list of key-value pairs, with unique keys (mutable). From Python 2.7/3.1,{} can also represent a set of unique values (mutable).Python: Regular Expression 101 Example Code
Reference: http://stackoverflow.com/q/9980381/1276534
Authors: Rajeev, George
In computer science theory class, we learned about regular expression. But it is unclear what exactly can it do at first, today I would like to introduce data validation as an example that uses the concept of regular expression. Python itself, like other language I assume (heard), has an implementation of regular expression. It comes standard from python too, see: http://docs.python.org/library/re.html
For example, you would like to ask the user for a telephone number, in the format of: 917-222-1234, if it is not in the format of XXX-XXX-XXXX, it will ask the user again until it is store. Let's take a look at the sample code.
Authors: Rajeev, George
In computer science theory class, we learned about regular expression. But it is unclear what exactly can it do at first, today I would like to introduce data validation as an example that uses the concept of regular expression. Python itself, like other language I assume (heard), has an implementation of regular expression. It comes standard from python too, see: http://docs.python.org/library/re.html
For example, you would like to ask the user for a telephone number, in the format of: 917-222-1234, if it is not in the format of XXX-XXX-XXXX, it will ask the user again until it is store. Let's take a look at the sample code.
import re
while True:
# Get the user's input into the string
myString = input('Enter your telephone number: ')
# Matching it with the regular expresssion
# isGoodTelephone will return True if it matches
isGoodTelephone = re.match('^[0-9]{3}-[0-9]{3}-[0-9]{4}$', myString)
if (isGoodTelephone):
print('Great! Got your phone number into the system')
print('Entry:', myString)
else:
print('Not in the correct format. Ex: xxx-xxx-xxxx')
print()
Output of the csci133rep1.py: Enter your telephone number: 917-123-1234 Great! Got your phone number into the system Entry: 917-123-1234 Enter your telephone number: 9171231234 Not in the correct format. Ex: xxx-xxx-xxxxActually the basic of the regular expression is not too hard to learn, take a look at the bottom and you will able to figure out how to use it with no problem. Didn't need to put too much comment to make it understandable. Although there are much more ways to use it than just the telephone.
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
^ # mark the start of the telephone string
[0-9] # any one of the 0123456789
{3} # match it exactly three times, no less
- # a hyphen symbol
[0-9] # any one of the number between 0 and 9
{3} # exactly three copies
- # another hyphen symbol
[0-0] # any one of the number 0-9
{4} # four times
$ # mark the end of the telephone string
Sunday, April 1, 2012
Python: Calculate Grade Point Average In a 4.0 Scale
This is a simple program that take a list of grade, and then convert it to a 4.0 GPA scale with 1 decimal point, finally calculate the grade point average.
# Take the list of grade as input, assume list is not empty
def convertGrade(myGrades):
myResult = [] # List that store the new grade
for grade in myGrades:
gpa = (grade / 20) -1
# Depending on how many deciaml you want
gpa = round(gpa, 1)
myResult.append(gpa)
return myResult
# The list of grades, can be more than 5 if you want to
grades = [88.3, 93.6, 50.2, 70.2, 80.5]
convertedGrades = convertGrade(grades)
print(convertedGrades)
total = 0
# If you want the average of them
for grade in convertedGrades:
total += grade # add each grade into the total
average = total / len(convertedGrades)
print('Average GPA is:', average)
Output:[3.4, 3.7, 1.5, 2.5, 3.0] Average GPA is: 2.82
Saturday, March 31, 2012
Python: Get the most frequent elements from list when there is more than one
Reference: stackoverflow
Authors: james_kansas, Niklas B.
The question is: When you are given a list that is unsorted, how do you get the most frequent appeared element, in particular, when there is more than one.
Authors: james_kansas, Niklas B.
The question is: When you are given a list that is unsorted, how do you get the most frequent appeared element, in particular, when there is more than one.
from collections import Counter
def myFunction(myDict):
myMax = 0 # Keep track of the max frequence
myResult = [] # A list for return
for key in myDict:
# Finding out the max frequence
if myDict[key] >= myMax:
if myDict[key] == myMax:
myMax = myDict[key]
myResult.append(key)
# Case when it is greater than, we will delete and append
else:
myMax = myDict[key]
del myResult[:]
myResult.append(key)
return myResult
foo = ['1', '1', '5', '2', '1', '6', '7', '10', '2', '2']
print('The list:', foo)
myCount = Counter(foo)
print(myCount)
print(myFunction(myCount))
OutputThe list: ['1', '1', '5', '2', '1', '6', '7', '10', '2', '2']
Counter({'1': 3, '2': 3, '10': 1, '5': 1, '7': 1, '6': 1})
['1', '2']
More Reading: http://stackoverflow.com/questions/1518522/python-most-common-element-in-a-list
csci133allCombination.py
Reference: http://stackoverflow.com/q/9961077/1276534
Authors: PePe, Li-aung Yip
Overview: Example of this nested while loop is a bad idea, and noting the while loop in python require resetting the loop counter to be 0. And for usage of getting the combination of everything, use either nested for loop with range() function, or itertools, which takes list and return every possible combination from each element in the list you give as argument. And also, xrange() is replaced by range() at 3.0.
In C++, although it might not be recommended, we can write a nested while loop and the following code will work. Because of unknown reason.
Authors: PePe, Li-aung Yip
Overview: Example of this nested while loop is a bad idea, and noting the while loop in python require resetting the loop counter to be 0. And for usage of getting the combination of everything, use either nested for loop with range() function, or itertools, which takes list and return every possible combination from each element in the list you give as argument. And also, xrange() is replaced by range() at 3.0.
In C++, although it might not be recommended, we can write a nested while loop and the following code will work. Because of unknown reason.
a = 0
b = 0
c = 0
while a <= 5:
while b <=3:
while c <= 8:
print(a , b , c)
c += 1
b += 1
a += 1
Output is the following0 0 0 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8Answer Because we need to remember to reset the loop's counter, a, b, c respectively on each iteration. But this method is kind of funky.
a = 0
b = 0
c = 0
while a <= 5:
while b <=3:
while c <= 8:
print(a , b , c)
c += 1
b += 1
c = 0 # reset
a += 1
b = 0 # reset
c = 0 # reset
I think most python programmer would prefer using the for loop over the range() function. It is interesting also to note and learn that, xrange() is the range() function, if you are using python 2.x. From 3.0 on, use range instead. :) for a in range(5+1): # Note xrange(n) produces 0,1,2...(n-1) and does not include n.
for b in range (3+1):
for c in range (8+1):
print(a, b, c)
But then wait... from the Li-aung Yip, there is a better way. Check out this solution which involve using itertools.product() import itertools
for a, b, c in itertools.product(range(5+1), range(3+1), range(8+1)):
print a,b,cFor even more reading: Dan Goodger's "Code Like a Pythonista: Idiomatic Python" Thought: I think 2 second way resemble C++ the most to me, I don't know if I want to use while loop even in C++. But it is great to learn another function from the itertools, the itertools.product(). And nice to see the use for for a, b, c. I think it is powerful, but never use it in my code yet, should practice using it. import itertools
colors = ['red', 'green', 'blue']
vehicles = ['car', 'train', 'ship', 'boat']
numbers = [1, 2, 3, 4, 5]
"""Pints out all the possible combination of number of color vehicles"""
for color, vehicle, number in itertools.product(colors, vehicles, numbers):
print(number, color, vehicle)
More reference: http://docs.python.org/library/itertools.html
Wednesday, March 28, 2012
csci133c7.py
or known as csci133cleanup.py
In this tutorial we will write a program that clean up the string, it is one of the most classic program. Almost every student will be given a novel text file or input text file and ask them to do something on the data. So the first thing is to "open and load" the text file, and get the English letters into a new string. This tutorial looks long, because I included the full source code of every single program, but in fact it is only minor changes. Read on!
Answer: Because we need this mechanism to separate possible words, for example, here is a string Doctor--John. If we did not append a space, we will get 'DoctorJohn' in one word. When we want every single word in the file, we want to separate them instead of keeping them as the same one.
The last version we want to search it, we want to look up our dictionary we just created. Take a look at the last couple of lines.
Keyboard interrupt hot key: Control + C
In this tutorial we will write a program that clean up the string, it is one of the most classic program. Almost every student will be given a novel text file or input text file and ask them to do something on the data. So the first thing is to "open and load" the text file, and get the English letters into a new string. This tutorial looks long, because I included the full source code of every single program, but in fact it is only minor changes. Read on!
# Version 1 of csci133cleanup.py
# Full implementation of cleanup
wordList = [] # Create a list to store our words
abc = 'abcdefghijklmnopqrstuvwxyz'
with open('novel.text') as book:
for line in book:
cleanline = ''
for character in line.tolower():
if character in abc:
cleanline += character
else:
# Important! We have append a space!
cleanline += ' '
for word in cleanline.split():
if word not in wordList:
wordList.append(word)
The first version we are only cleaning up the string text, so there are nothing too special about it. But notice, on line 18, we appended a space to it. Why? Take a moment to think about it, or try to clean 'Doctor--John' on a piece of paper. Answer: Because we need this mechanism to separate possible words, for example, here is a string Doctor--John. If we did not append a space, we will get 'DoctorJohn' in one word. When we want every single word in the file, we want to separate them instead of keeping them as the same one.
# without space append: Doctor--John, result in DoctorJohn # with space append: Doctor--John, result in Doctor John (YES!)Of course this is not without its problem, for example, we will be left with a lot of 's', so we will want to check if it is already in the list or not. (See line 16), if they are in the list, we might not want to append it again. *Depend on your need, maybe you can add a line number to it. See the next example.
# Version 2 of csci133cleanup.py
# Insert the line numbers into the dictionary
wordList = {} # Create a dictionary to store them
abc = 'abcdefghijklmnopqrstuvwxyz'
with open('novel.text') as book:
for line in book:
lineNumber = 1 # Starting at line 1
cleanline = ''
for character in line.tolower():
if character in abc:
cleanline += character
else:
# Important! We have append a space!
cleanline += ' '
for word in cleanline.split():
if word in wordList:
wordList[word].append(lineNumber)
else:
# Store the value as a list that contain 1 item
wordList[word] = [lineNumber]
lineNumber += 1
Take a moment to read and compare the code. The very first line is different. We are using a dictionary instead of list. Because when we want to check if the item is in the dictionary already or not, we want to use its build in function, instead of going them one by one. And the other difference is, we are now appending the line number into a list of them. There is an interesting part to it, See line 21.wordList[word] = [lineNumber]Notice, we can not use wordList[word] = lineNumber. Because we are creating the first value for the dictionary's key. We instead will create this value as a list that contain one integer. I actually did not aware of this when I was learning python, I keep running into error, because I only used a single interger. And when I try to append to this single integer, it does not work.
The last version we want to search it, we want to look up our dictionary we just created. Take a look at the last couple of lines.
# Version 3 of csci133cleanup.py
# This version include part 1 - 3
wordList = {}
abc = 'abcdefghijklmnopqrstuvwxyz'
with open('novel.text') as book:
lineNumber = 1
for line in book:
cleanline = ''
for character in line.tolower():
if character in abc:
cleanline += character
else:
cleanline += ' '
for word in cleanline.split():
if word in wordList:
# do something, such as append line number
wordList[word].append(lineNumber)
else:
wordList[word] = [lineNumber]
lineNumber += 1
while True:
word = input('Enter a word here: ' )
if word in wordList:
print('Found on lines:, wordList[word])
else:
print('Not found.')
wordList = {'apple':[2, 25, 55, 100], 'banana':[5, 10, 36, 90]' ...}
This is the first time we see a while statement in python, the structure of the while loop is simple. while (condition is true), it will execute all the code within it once, and then check if the condition is true, if it is true, do it again, if it is not, it will exist and go to the next statement. See we have 'True' as the condition, that means this loop will run forever, until we kill it with keyboard interrupt. Keyboard interrupt hot key: Control + C
Tuesday, March 27, 2012
csci133ifelif.py
Reference: http://stackoverflow.com/questions/7052393/python-elif-or-new-if
Today when I am reading on the python exercises, I came across one of the exercise program it uses elif (in chapter 10). For a second I am not sure what does it mean because it is called differently. But when I read closely to the source file. It looks like it is trying to replaces some of the other if else statements. Finally I look it up online, I found out it is a little bit more than just if else loops.
For example: If you want to check if a string is English word or not, you would want to check if "isalpha()" or not, and then you start to clean up the letters. So that way, your loop will exist as soon as it knows it contain non-letter characters.
Today when I am reading on the python exercises, I came across one of the exercise program it uses elif (in chapter 10). For a second I am not sure what does it mean because it is called differently. But when I read closely to the source file. It looks like it is trying to replaces some of the other if else statements. Finally I look it up online, I found out it is a little bit more than just if else loops.
def foo(var):
# Check if var is 5
if var == 5:
var = 6
elif var == 6:
var = 8
else:
var = 10
return var
def bar(var):
if var == 5:
var = 6
if var == 6:
var = 8
if var not in (5, 6):
var = 10
return var
print foo(5) # 6
print bar(5) # 8
You can see the exam of foo(5), if the val is 5. Then the rest of them are treated as (else) loop. The elif is a nested else if loop. It is good (maybe) if you want a cleaner looking program, because you don't have the nested else if loops, the indent level is smaller, and faster compare to a sequence of if, if, if statements, because you are not checking explicitly for every single if statement. Note: always try to put the most common condition on the top, so things can check off the 'list of conditions' faster. For example: If you want to check if a string is English word or not, you would want to check if "isalpha()" or not, and then you start to clean up the letters. So that way, your loop will exist as soon as it knows it contain non-letter characters.
Friday, March 23, 2012
csci133class.py
Tkinter module provide many data types, such as Frames, Labels, and Buttons. They equipped with their own sub routines, get for Entry, some of them is standard, like pack, and after. But wouldn't it be nice if we can do their to all the data type (class) we create?
For our ice cream store, we want to create memebership account (Object).
And when we want to create an instance of the iceCreamMemeber, an individual object of this new type, we will use the class name as it is a function.
Different from C++, python's class instance does not have member variable pre say. They have attributes, and you do it with the syntax of.
But if we try to print(member1), something weird will happen, try it. It actually let us learn another fact about how python work.
*Important* : self vs. Deck
For our ice cream store, we want to create memebership account (Object).
standardMember = Account('George Chan')
standardMember.deposit(100)
Or, for a worker timesheet program, we can create worker that contain other datatype object)class iceCreamMember:
def __init__(self, name, age):
self.myName = name
self.myAge = age
def getAge(self):
return self.myAge
def getName(self):
return self.myName
member1 = iceCreamMember('George', 23)
member2 = iceCreamMember('Gerry', 29)
print(member1.getAge())
print(member2.getName())
In python, when we want to create a new datatype (new class), we would use a class statement. The keyword class tells python that it is a new datatype we are creating, notice it is a good style to always name your class with a upper case letter.And when we want to create an instance of the iceCreamMemeber, an individual object of this new type, we will use the class name as it is a function.
member1 = iceCreamMember('George', 23)
This create a new iceCreamMember object, and member1 refers to it. Remember the constructor is called when we create an instance of the object in C++? In python, the __init__ function will be called right away. def __init__(self, name, age):Look at this line, the __init__ function takes 3 parameters, but look at the line #20, we only actually passing 2 to it. Why? Because the first argument is always set to refer to the new instance we just created. (Self, itself). You can name it anything, but the first one is always point to the object itself, so it makes senss to use 'self'. The order of ('George', 23) is important, because name gets 'George', as a string, and age gets 23, as an integer.
Different from C++, python's class instance does not have member variable pre say. They have attributes, and you do it with the syntax of.
self.myName = name self.myAge = ageTo create a function for our userdefined class, we just use def like we always do, with the difference of giving it a (self) parameter. Notice, when we call the function .getName(), we don't have to give it anything. Since the self argument is generated automatically.
But if we try to print(member1), something weird will happen, try it. It actually let us learn another fact about how python work.
>>> <__main__.iceCreamMember object at 0x0000000002EE1E48>The reason why we get the memory address like output, it is because we have yet to "teach" python how we want to print this. python is computer, and that's what the iceCreamMemeber to python is at this moment, let's add another method to our class.
def __str__(self):
return self.myName + ', and age ' + str(self.myAge)
So when we ask python to print, it will know what to print.*Important* : self vs. Deck
class goldMemeber(iceCreamMemeber):
def __init__(self, discount):
iceCreamMemeber.__init__(self)
self.myDiscount = discount
def __str__(self):
return "{0}% of discount for member".format(self.myDiscount)
We can understand this new class as: GoldMemeber is a kind of iceCreamMemeber. And the GoldMemeber inherits all the function the iceCreamMemeber has. So the getName, getAge function, will be provided to the GoldMemeber automically.iceCreamMemeber.__init__(self)*Important*: We must use the explicitly passing method to to pass the instead. If we use self.__init__(), we will be instead calling the goldMember.__init__ function. Which is the function we are trying to define right now at this moment.
Subscribe to:
Posts (Atom)