Gribouillis 1,391 Programming Explorer Team Colleague

Because the * is a multiplier which must apply to something. You can look for ^[^/]*/10$ , which means begining of string, any number of non slash characters, a slash, a 10 and the end of the string.

Gribouillis 1,391 Programming Explorer Team Colleague

No, the / is not special in python's regex syntax. The \ is special, so if you want to match a single backslash, you must write re.compile(r"\\") . The role of the r is to avoid the interpretation of special characters by the python compiler in strings. For example the literal string "\n" has a single character (a newline), but the string r"\n" has 2 characters, a backslash and a 'n' (and no newline). When passed to re.compile , it is then interpreted as a newline. You don't need to think too much about this: if you put the 'r' when you use re.compile, it will usually do what you're expecting.

Gribouillis 1,391 Programming Explorer Team Colleague

I was more successful with a slightly modified version. I modified the query string. Also, it's a good habit to always use strings prefixed by 'r' when you pass a literal string to re.compile.

#format the raw_input string for searching
raw_string = re.compile(r' ') #search for a space in string
searchstring = raw_string.sub('+', title) #replace with +
print searchstring

#find the film page url
url = "http://www.imdb.com/find?s=all&q=" + searchstring
print url
source = fetchsource(url)
soup = BeautifulSoup(source)
filmlink = soup.find('a', href=re.compile(r"/title/tt[0-9]*/"))
print filmlink

For your other question, what happens when you call re.compile ? The regular expression specified by the argument string is interpreted to create a finite automaton (a collection of nodes and transition rules between these nodes). This automaton is a machine to look for the regular expression in a string. The whole thing is hidden in a "regular expression object" with a nice interface from the point of vue of the client code.

Gribouillis 1,391 Programming Explorer Team Colleague

I'd suggest

file_in =  open("data/%s.txt" % data)
file_list = []
for line in file_in:
    item = line.split(",")[5]
    file_list.append(item)

oops, somebody gave the solution before I did :)

Gribouillis 1,391 Programming Explorer Team Colleague

You can use the subprocess module

import subprocess
child = subprocess.Popen("myprog.py")

see the documentation of this module. There are other ways to call your program;

child = subprocess.Popen("python myprog.py", shell=True)

for example.

Gribouillis 1,391 Programming Explorer Team Colleague

Your loop while 1 runs forever. You must exit the loop when a.readline() returns an empty line (the string "" ). This test must be done before you call the method strip() . You can exit the loop with a break statement.

Gribouillis 1,391 Programming Explorer Team Colleague

The line YES += 1 should have the same indentation as the line b.write(line +'\n') above. Same remark for the line NO += 1 . Also I don't know how you indent your code, but use only space characters, not tab characters.

Gribouillis 1,391 Programming Explorer Team Colleague

It could be an indentation problem, but since you don't wrap your code in code tags as paulthom says above, it's difficult to see what's wrong.

Gribouillis 1,391 Programming Explorer Team Colleague

The call to main() in line 45 should go at the end of the program, otherwise, the game starts before the interpreter reads the win_combo function. Also you should rewrite entirely this function with loops. It should take no more than 20 lines.

Gribouillis 1,391 Programming Explorer Team Colleague

If you want to access the correct answer, you should iterate over the dictionary items instead of the values

def vocabtest():
    for a, r in words.items(): # <-- the answer is a
        ...
SoulMazer commented: Very helpful and follows through with posts. +1
Gribouillis 1,391 Programming Explorer Team Colleague

I'm afraid I can't help you with pygame, but perhaps other members can help you :)

Gribouillis 1,391 Programming Explorer Team Colleague

Your problem is called "input validation". Each time that you ask some input to the user, there is a high probability that the user enters unexpected input and your program must handle this (= validate input). Here you could use a test like this

answer = raw_input("> ")
            answer = answer.strip() # <--- remove leading and trailing white space
            if answer not in words:
                pass # <-- Put here what you want to do when answer is not in words
            elif words[answer] == r: #Error
                print("Correct!")
                count = 100000

However, I don't know what you want to do if the user enters an answer which is not in the dictionary.

Gribouillis 1,391 Programming Explorer Team Colleague

You can learn about exceptions here http://www.diveintopython.org/file_handling/index.html and here http://docs.python.org/tutorial/errors.html#exceptions. In the above code, I'm using the line

column = "abcdefg".index(move)

This expression returns the position of move in the string "abcdefg". For example if move is 'a', it returns 0, if move is 'b', it returns 1, etc. Now suppose that move is 'z'. The call to index will fail, and it does this by raising an 'exception'. Here the type of this exception is ValueError. If we do nothing, the program exits and prints a 'traceback' like this

>>> column = "abcdefg".index('z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

What we can do is 'catch' the exception with a try...except statement like this

try:
    column = "abcdefg".index(move)
except ValueError:
   print("We didn't compute column, but we caught the exception")

The statements in the except part are executed if one of the statements in the try part failed with a ValueError exception. So the program won't crash on this error and we can decide what we want to do when 'move' is not in the string "abcdefg".

Gribouillis 1,391 Programming Explorer Team Colleague

Here is how you could write a main loop for your game. Add this to the above post

def main():
    print("Welcome to Connect Four")
    players = "OX"
    playerIndex = 0
    while True:
        # The game's mainloop, we iterate over the players
        make_board()
        playerIndex = 1 - playerIndex # alternatively 1 or 0
        player = players[playerIndex] # This is 'O' or 'X'
        move = raw_input("You are %s. Enter a lettered row to make a move! " % player)
        while True:
            # Inner loop: we iterate until valid input
            move = move.strip() # remove trailing white space
            try:
                if move == 'q':
                    # player entered 'q' to exit
                    raise SystemExit
                column = "abcdefg".index(move)
                row = find_row(column)
                break # we exit inner loop
            except ValueError:
                move = raw_input("Invalid input, try again: ")
        pmove(player, row, column)

def find_row(column):
    for i in range(7):
        if rows[i][column] == ' ':
            return i
    raise ValueError

def pmove(player, row, column):
    rows[row][column] = player

main()

There is still a lot to do. You must check if a player wins and take appropriate action.

Gribouillis 1,391 Programming Explorer Team Colleague

Instead of 7 variables row1, row2, etc, you could have one variable rows, which is an array of rows

rows = [ [' ' for i in range(7)] for j in range(7)]

template = " %s |" * 7
def make_board():
    for i in range(6, -1, -1):
        print(("%d |" %(i+1)) + (template % tuple(rows[i])))
    print("-" * 31)
    print("  |" + (template % tuple("abcdefg")))

Now your rows are rows[0], rows[1], ..., rows[6] . This makes it easier to write loops.

Gribouillis 1,391 Programming Explorer Team Colleague

I quote the python 3.0 documentation: "If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread." So I think you should start your class like this

class UpdateLabels(Thread):#activates in main...did not work?
    def __init__(self):
        Thread.__init__(self)
        global XPOS
        global YPOS

Also, I don't understand your method _initialize . I don't think you need it.

Gribouillis 1,391 Programming Explorer Team Colleague

No, I meant that you could write

x = ((a**2 + b**2)**0.5)/2
    if repetition in (2, 5, 8, 11):
        turtle.forward((length/4)-5)
        turtle.right(degree)
        turtle.forward(x)
        turtle.right(degree)
        turtle.forward(x)
        turtle.left(degree+degree)
        etc...

This avoids computing repeatidly the same expression.

Gribouillis 1,391 Programming Explorer Team Colleague

Also you should compute only once ((a**2+b**2)**0.5)/2 and store the value in a variable.

Gribouillis 1,391 Programming Explorer Team Colleague

You should try if repetition in (2, 5, 8, 11):

Gribouillis 1,391 Programming Explorer Team Colleague

You can change addbook like this

def addbook(ISBN, author, title, stock):
    if ISBN in booklist:
	booklist[ISBN][2] += stock
    else:
	booklist[ISBN] = [author, title, stock]
Gribouillis 1,391 Programming Explorer Team Colleague

You should rewrite find_text as

def find_text(element):
    if element.text:
        yield element
    for subelement in element:
        for txt in find_text(subelement):
            yield txt

If you wrote it like this, I can't see how it could fail.

Gribouillis 1,391 Programming Explorer Team Colleague

Yes because with your new code, the for loop is executed only if the element has a text, which is not what you want

Gribouillis 1,391 Programming Explorer Team Colleague

You changed the indentation of for subelement in element: .

Gribouillis 1,391 Programming Explorer Team Colleague

I agree with Sillyboy, sum is not a syntax, it's a function which applies to a sequence. Here I think that the expression that you put in the sum is the value of your investment, so you can simply drop the sum. However I think your program is confusing because you say that the principal is invested each year.

Gribouillis 1,391 Programming Explorer Team Colleague

No the translation is done inside the function. Outside of the function, I only build the translation table which has nothing to do with the text.

Gribouillis 1,391 Programming Explorer Team Colleague

Here is a function which is 10 times faster on my computer

from string import maketrans
fromst = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
tost = ("a" * 26) + ("A"*26) + ("0"*10)
assert (len(fromst) == 62) and (len(tost) == len(fromst))
table = maketrans(fromst, tost)
def count_char3(text):
    text = text.translate(table)
    low = text.count("a")
    upp = text.count("A")
    dig = text.count("0")
    spa = text.count(" ")
    return upp, low, dig, spa

However this algorithm should work only with 8 bits characters. Also in count_char2, you should replace c.isspace() by c == " ", because isspace matches other chars than the space character.

Gribouillis 1,391 Programming Explorer Team Colleague

I would like to learn regular expressions. I have no Idea what it is (I always see people here using). Can someone explain a litle and suggest good tutorial?

Didn't mean to deviate the thread, so keep your first focus on the thread, then you can answer my question

You could start with dive into python http://diveintopython.org/regular_expressions/index.html.

Gribouillis 1,391 Programming Explorer Team Colleague

Here is another one (faster ?)

#!/usr/bin/env python
import re

def main():
    regexes = [ re.compile(x) for x in
         (r"[^A-Z]+", r"[^a-z]+", r"[^0-9]+", r"[^\ ]+")]
    filename = "test.txt"
    content = open(filename).read()
    counts = [len(s) for s in (r.sub("", content) for r in regexes)]
    print("""There are
%d uppercase letters
%d lowercase letters
%d digits
%d space characters
in file '%s'""" % (tuple(counts) + (filename,)))

main()
sneekula commented: nice +6
Gribouillis 1,391 Programming Explorer Team Colleague

I think it's suspicious to have column names with spaces. I doubt it's compatible with the SQL syntax. You should try to replace the spaces by underscores.

Gribouillis 1,391 Programming Explorer Team Colleague

I Haven't catched you, can you elaborate?

You have a TypeError: str object is not callable because the syntax "mystring"(myargs) is invalid.

Gribouillis 1,391 Programming Explorer Team Colleague

There should be something here CHAR, ? CHAR)" (t_name, t_date between the string and the opening parenthesis :)

Gribouillis 1,391 Programming Explorer Team Colleague

I suggest this if you want to see the pdf file

import webbrowser
webbrowser.open("http://fetac.ie/MODULES/D20120.pdf")
Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague

I don't think you really need a tutorial. Start like this

>>> from wx.lib.mixins import listctrl
>>> help(listctrl)

Note that the help for the class TextEditMixin shows you how to use the mixin class. You should try each mixin class in the same way, and then try to derive from more than one mixin class. A mixin class is just a class which groups additional methods which you want to use optionally in a class (or several classes). So think about it as a collection of methods. One way to use it is to write a class which one main parent class and one or more mixin classes.

Gribouillis 1,391 Programming Explorer Team Colleague

Run this example

listA = range(5)
def get_list():
    return listA
listB = get_list()
listC = list(listA)
listA.append("hello")
for x in (listA, listB, listC):
    print(x)

listA and listB are 2 names for the same list object. listC is another list object.

Gribouillis 1,391 Programming Explorer Team Colleague

It's not a cast, it's a copy. The problem here is that your subsequent calls to htmlparser.feed add items to the list, while you're looping over the list items. This is already bad. Now suppose that a page contains a link to itself: you enter an infinite loop. When I copy the list, I iterate the items of the list as it was when I made the copy, so that the loop is necessarily finite.

Gribouillis 1,391 Programming Explorer Team Colleague

I think you should copy the list like this

class MSULinkExtractor(htmllib.HTMLParser):
    ...
    def get_links(self):
        print '\tMSULinkExtractor.get_links has been called'
        return list(self.links)
    ...
Gribouillis 1,391 Programming Explorer Team Colleague

You could try print(set(img_I)) to see all the colors contained in your image.

Gribouillis 1,391 Programming Explorer Team Colleague

The pydoc module (used by the function 'help') finds all definitions in a module and produces text or html documentation. I read that it does this by importing the module first and then using objects introspection. If you look at the code of pydoc.py in your standard library, you should find tools to extract the api from the module. This could be a good starting point, with a lot of work already done.

Gribouillis 1,391 Programming Explorer Team Colleague

Python is a high level object oriented programming language created at the beginning of the 90's bu Guido Van Rossum. Python allows for fast software development, which is the main reason of its success. The official site of the language is www.python.org.

Gribouillis 1,391 Programming Explorer Team Colleague

If your block is only one statement, then you can do something like this:

if number == guess: print("yep it's identical")

It's true, but G Van Rossum regrets this, so I wouldn't say it's good python code.

vegaseat commented: Good point! +11
Gribouillis 1,391 Programming Explorer Team Colleague

Why not use braces to separate blocks ? because you're not writing C but python code and this use of braces doesn't belong to the syntax of python. Indentation is the only way to express the block structure in python. Braces only appear in the definition of literal dictionary objects.

Gribouillis 1,391 Programming Explorer Team Colleague

No, it's probably because your editor writes 4 spaces when you hit the tab key. Read your pyhon file as a string, and print it's repr, you'll see if it actually contains tab characters.

Gribouillis 1,391 Programming Explorer Team Colleague

It's very simple, some idioms in a python program introduce a *block* of statements. The block of statements must be non empty and indented. They are introduced by lines ending with a : . These idioms are

# class definitions
class myClass(object):
    # block of statements (indented)

# function definitions
def myFunction(*args, **kwd):
    # block of statement (indented)

# if statements
if myCondition:
    # block of statements
elif condition:
    # block of statements
else:
    # block of statements

# while loops
while condition:
    # block of satements

# for loops
for x, y in coordinates:
    # block of statements

# try .. except clauses
try:
    # block of statements
except myException:
    # block of statements
finally:
    # block of statements

# with statements (advanced)
with expression as variable:
    # block of statements

Roughly speaking, each time you end a line with a colon (outside a literal dictionary definition), you must write an indented block after this line. If you need an empty block, put the pass statement as the only statement of the block.
Blocks can be nested of course:

while condition1:
    print("hello") # indented
    while inner_condition:
        print("hi") #inner block indented twice

The recommended and most widely used way to indent is to put 4 spaces for each indentation, but you're free to put any number of spaces you want (I like 2 spaces). However the indentation must be coherent in the same file. You cannot indent with 4 spaces at the beginning of the file and …

Gribouillis 1,391 Programming Explorer Team Colleague

I think you could write your own function like this

from random import random
from math import sqrt

def triangular(a, c, b):
  c = float(c)
  t = (c-a)/(b-a)
  y = sqrt(random())
  d = a if random() < t else b
  return d + (c-d) * y

if __name__ == "__main__":
  for i in range(100):
    print(triangular(1, 3, 4))

Explanation: I start from the density function in the wikipedia page http://en.wikipedia.org/wiki/Triangular_distribution. Obviously, if a random variable Z has this distribution, the probability that it's between a and c is t=(c-a)/(b-a). So I first choose if the variable must be between a and c or between c and b by comparing a uniformly random number in [0,1] to this value. Then I choose a first Y which is the square root of a uniformly random number between 0 and 1. Such a variable has a linear density function f(t) = 2 t in (0, 1]. This is easy to compute. Then I rescale this variable so that it falls between [a, c] or [c, b].
There is some work if you want to convince yourself that it works, but I'm rather good at math, so it should :)

Gribouillis 1,391 Programming Explorer Team Colleague

Hello again and merry christmas ! In fact I don't use decorators so much. The decorators I use most often are the builtin decorators @property , @staticmethod and @classmethod . I discovered decorators in the attached slides of pycon 2005, which don't seem to be available anymore at python.org. Decorators are useful mainly as a way of 'typing' functions, when you want to apply the same transformation to a collection of functions or methods. For example you want to define the concept of 'a function which checks its arguments' or 'a function which traces its calls' . You should check the python decorator library http://wiki.python.org/moin/PythonDecoratorLibrary. There is also PEP 318 which introduces decorators. I also found a nice @consumer decorator in pep 342.

sneekula commented: Thanks for the good info! +6
Gribouillis 1,391 Programming Explorer Team Colleague

No, in python methods, there is no implicit argument (like C++'s this for example)

Gribouillis 1,391 Programming Explorer Team Colleague

put from classA import classA in classB.py instead of import classA

Gribouillis 1,391 Programming Explorer Team Colleague

You could try to match the url only if it's followed by a white space character or the end of the string, with a lookahead assertion like this r"\b%s(?=\s|$)" % url . Alternatively, you could try to match the url if it's not followed by a slash like this r"\b%s(?![/])" % url

Gribouillis 1,391 Programming Explorer Team Colleague

Did you try re.compile(r"\b%s\b" % url) ? Or probably better re.compile(r"\b%s\b" % re.escape(url) ?