Gribouillis 1,391 Programming Explorer Team Colleague

The easiest solution is to have both Class A and Button_Manager inherit a common superclass which holds their common methods

class Common(object):
    def data_mod1(self):
        ...

class A(Common):
    ...

class Button_Manager(Common):
    ...
Gribouillis 1,391 Programming Explorer Team Colleague

The error 'TypeError: must be type, not classobj ' usually appears in python 2 when an old style class instance is used in a function where a new style class instance is expected. Aren't you using an old version of python where Tkinter classes are old style classes ? If this is the case, you could try and add 'object' to the superclasses of your Application class.

Gribouillis 1,391 Programming Explorer Team Colleague

Either way it's not pretty, is it? At first I thought that escaping the "[" and "]" as "\[2011\]" would work but it didn't.

Python uses the fnmatch module to convert a shell pattern to a regular expression (the function fnmatch.translate). You could modify this function to treat characters [ and ] differently. In python 2.6.5, the function is

def translate(pat):
    """Translate a shell PATTERN to a regular expression.

    There is no way to quote meta-characters.
    """

    i, n = 0, len(pat)
    res = ''
    while i < n:
        c = pat[i]
        i = i+1
        if c == '*':
            res = res + '.*'
        elif c == '?':
            res = res + '.'
        elif c == '[':
            j = i
            if j < n and pat[j] == '!':
                j = j+1
            if j < n and pat[j] == ']':
                j = j+1
            while j < n and pat[j] != ']':
                j = j+1
            if j >= n:
                res = res + '\\['
            else:
                stuff = pat[i:j].replace('\\','\\\\')
                i = j+1
                if stuff[0] == '!':
                    stuff = '^' + stuff[1:]
                elif stuff[0] == '^':
                    stuff = '\\' + stuff
                res = '%s[%s]' % (res, stuff)
        else:
            res = res + re.escape(c)
    return res + '\Z(?ms)'

It shouldn't be too difficult to create your own simpler translate() function.

Gribouillis 1,391 Programming Explorer Team Colleague

Sorry about the earlier post. I realized what the problem was and I wanted to think about it for a while. Here is the question. How do I "negate" the effect of a "[" or "]" in a file name when I am doing globbing. For example, if my code has the following:

pattern = sys.argv[1]

for filename in glob.glob(pattern):
    print filename

works fine for patterns like "*.avi". But what if I don't want to actually specify a file pattern but instead want to specify a single file? If I want to do something to all avi files I can call

scriptname *.avi

but sometimes I just want to do

scriptname myfile.avi

works fine unless my filename has certain chars like

scriptname "The Jackal [1997].avi"

What do I have to do to the string to get glob to match the real file name?

I found a way by replacing [ with [[] and ] with []]. Here is the code

import glob
import sys
import re
s = sys.argv[1]
s = re.sub("[\[\]]", lambda m: "[%s]" % m.group(0), s)
print glob.glob(s)

""" example on the command line
$ foo.py "The Jackal [1997].avi"
['The Jackal [1997].avi']
"""
Gribouillis 1,391 Programming Explorer Team Colleague
JoshuaBurleson commented: Thanks Dr.G +3
Gribouillis 1,391 Programming Explorer Team Colleague

Gribouillis, your function worked nicely, but it didn't stop the user from entering an incorrect value, it just stopped them from leaving it blank.

I agree, but we're only supposed to help you write your code, not to do all the work for you :)

Gribouillis 1,391 Programming Explorer Team Colleague

Alright, here is a snippet from my script.

def checkInput(prompt):
  result = input(prompt)
  if not result.strip():
    print("Please enter an appropriate value!")
  return result

def convert():
  loop = 1
  while loop == 1:
    deg = checkInput("\nDo you have Celsius, Fahrenheit, or Kelvin? ")

    # Celsius calculations
    if deg == 'c' or deg == 'celsius' or deg == 'Celsius':
      cdeg = checkInput("Convert to: Fahrenheit or Kelvin? ")
      if cdeg == 'f' or cdeg == 'fahrenheit' or cdeg == 'Fahrenheit':
        fcel = getFloat("\nWhat is the degrees in Celsius? ")
        print ("\n{} degrees Celsius is {} degrees Fahrenheit.\n".format(fcel, fcel * 1.8 + 32))
      if cdeg == 'k' or cdeg == 'kelvin' or cdeg == 'Kelvin':
        kcel = getFloat("\nWhat is the degrees in Celsius? ")
        print("\n{} degrees Celsius is {} degrees Kelvin.\n".format(kcel, kcel + 273))

You can add a loop, and a try block to catch all errors, since there are more than one check. When a check fails, you can 'raise ValueError' like this

def checkInput(prompt):
  while True:
    result = input(prompt).strip()
    try:
      if not result:
        raise ValueError
      # here add more checks and raise ValueError if any of them fails
    except ValueError:
      print("Please enter an appropriate value!")
    else:
      return result
Gribouillis 1,391 Programming Explorer Team Colleague

In python 3, after result = input(prompt) , result is a string in the python sense (an instance of the datatype 'str'). Examples of strings are

"" # the empty string
"    " # a string of white space
"3.14159" # a string with the representation of a number
"hello world  " # another string, with white space at the end

result.strip() is the string with white space removed at both ends, so if there was nothing significant in the user's input, it's the empty string, so:

if not result.strip():
  ... # the input was empty, do something

Also notice that even the empty string is not None, so be careful about your loop condition

Gribouillis 1,391 Programming Explorer Team Colleague

I'm still an ameture at programming, so could you please be a bit more specific about the solution. I don't quite know what you mean by 'Unindent'. Thanks for helping me! :)

I mean remove the definitions from the Player class and write them at the beginning of the line.

abders commented: Clear and precise aid. +1
Gribouillis 1,391 Programming Explorer Team Colleague

Unindent the ask_number() and ask_yes_no() definitions and remove the self parameter.

Gribouillis 1,391 Programming Explorer Team Colleague

okay, I understand, I've just yet to use the yield function. Is it essentially the same as return? Also, how would it be possible to be done otherwise?

You can do it otherwise with

while True:
    attempt = raw_input("...")
    etc

but I like to use generators.

Generators are very nice and powerful. There are a few great articles by David Beazley on generators, see here for a starting point http://www.dabeaz.com/generators/

Gribouillis 1,391 Programming Explorer Team Colleague

@Gribouillis how would he give more than one attempt after the value error was raised with yours? I'm just curious, I've never seen it that way before.

Because my generator raw_input[b]s[/b]() asks indefinitely the same question and generates the sequence of user answers.

Gribouillis 1,391 Programming Explorer Team Colleague

Here is another way

def raw_inputs(msg):
    while True:
        yield raw_input(msg)
        

def ask_depth():
    for attempt in raw_inputs("Enter depth in meters:- "):
        try:
            d = int(attempt)
            if d < 0 or d > 50:
                raise ValueError
            return d
        except ValueError:
            print("Invalid value: depth is a number in [0, 50]")

if __name__ == "__main__":
    depth = ask_depth()
    print(depth)

""" my output -->
Enter depth in meters:- sldfsdf
Invalid value: depth is a number in [0, 50]
Enter depth in meters:- sdfsdf
Invalid value: depth is a number in [0, 50]
Enter depth in meters:- -5
Invalid value: depth is a number in [0, 50]
Enter depth in meters:- 57
Invalid value: depth is a number in [0, 50]
Enter depth in meters:- 65
Invalid value: depth is a number in [0, 50]
Enter depth in meters:- 35
35
"""

Use raw_input() in python 2 and input() in python 3

Gribouillis 1,391 Programming Explorer Team Colleague

Actually I'm still stuck I guess I need to take it one step further, but I don't know how..

In reality this is my situation, I've got a dictionary containing dictionaries by position which indicate the distribution of nucleotides, in this case for the combination of
.
So position 0 has a A and a G, hence both are at 0.5
{0: {'A': 0.5, 'C': 0, 'T': 0, 'G': 0.5}, 1: {'A': 0, 'C': 0.5, 'T': 0.5, 'G': 0}, 2: {'A': 0.5, 'C': 0, 'T': 0, 'G': 0.5}, 3: {'A': 0, 'C': 0.5, 'T': 0.5, 'G': 0}}
I can make lists out of the above dictionaries, in such a way:
[[0.5, 0, 0, 0.5], [0, 0.5, 0.5, 0], [0.5, 0, 0, 0.5], [0, 0.5, 0.5, 0]]

and then do the above product() trick. The thing is that I'm interested in obtaining the next combination of nucleotides (A,C,T,G) which complements the above nucleotides as good as possible. this new combination has to be validated according to other criteria. My quest hence is, how do I sequentially validate four-letter words in the order that I would obtain if I used the above function for the nucleotide composition per position?

IE I want to see something like:
CACA first, then CACG etc. untill I reach GTGT which is the least desirable..

All you need to do is to compute the preferred order for each position:

from __future__ import print_function
import itertools

distribution = {0: {'A': 0.5, …
Gribouillis 1,391 Programming Explorer Team Colleague

thank you very much it worked.

just one more thing. now that this works i tried a simple programm

import risar
from turtle import Turtle

t = Turtle()

def square(turtle, l):
    for i in range(4):
        turtle.forward(l)
        turtle.left(90)


for i in range(5):
    square(t, 150)
    t.turn(72)

and now it says that
Turtle object has no attribute 'Turn'

Again, beware that python has a builtin module 'turtle'; import turtle and print turtle.__file__ to see which one you are importing.

Gribouillis 1,391 Programming Explorer Team Colleague

Another module may be hiding your module risar. Add print risar.__file__ after the 'import risar' in module turtle to see if it's importing from the expected file.

Gribouillis 1,391 Programming Explorer Team Colleague

Well, if the error is at line 178, you could perhaps add tests before that line: check that db is a Shelf, check that it has the key 'opened', print the key and whole content of the shelf, etc. Also print a message after line 178 to see if the program reaches this point, etc.

Your program contains many repeated blocks, like line 58-61. Write a function to execute these 3 lines and call this function instead of repeating the lines of code. Also a global db is not a good idea, why not have a member self.db ?

Gribouillis 1,391 Programming Explorer Team Colleague

I don't see how your code can handle several rules with the same 'cutstart', nor where your code stores words for later use. I agree with snippsat: you should write functions to clarify the algorithm. There are other errors, look at this:

>>> cutstart = "hello > world"
>>> if "<" and ">" in cutstart:
...  print "wow"
... 
wow

but cutstart doesn't contain "<".

Gribouillis 1,391 Programming Explorer Team Colleague

If you want to allow "don't", you can replace \w+ with (?:\w|)+ in template_helper(). Experiment with regular expressions, you could install kodos for this, see http://kodos.sourceforge.net/. I think you're asking too much too fast from your program. Think about your program structure and algorithm. Think about a robust way to handle punctuation.

Gribouillis 1,391 Programming Explorer Team Colleague

Also why don't you post the whole code ? It would make things easier.

Gribouillis 1,391 Programming Explorer Team Colleague

Hmmm, that works but when the input is eg. 'my name is James.' the output (for a custom rule) should be 'Hi there James!'. Instead, I get 'Hi there James.!' I'm guessing I should strip the ends of punctuation... doing now...

EDIT: Yes that seems to work...

To include punctuation in the cutstart part, use cutstart = cutstart.replace(".",r"\.")

Gribouillis 1,391 Programming Explorer Team Colleague

Wait, so here's the part of my code where I add the variable to my dictionary.

if match is not None:
                for item in cutstart.split():
                    if item.startswith("<") and item.endswith(">"):
                        worda = item.strip("<>")
                        word1 = match.group(worda)
                        dict[item] = word1

Whereabouts would I put the match.span code?

Well, I understood that you convert input to lowercase, so your code should look like

lower_input = initial_input.lower()
match = regex.search(lower_input)
if match is not None:
    x, y = match.span(worda)
    word1 = initial_input[x:y]

Otherwise it would be better to extract the group names with regex while compiling the template as I wrote above (instead of splitting cutstart, ...)

Gribouillis 1,391 Programming Explorer Team Colleague

Ok I've found a problem. When matching the input to the rule, the input needs to be in lower case so I would just use .lower() however when the variable is used in the response, the letter case of the variable must have been kept in tact. How can I do this?? :/

You could use x, y = match.span("w2") to get the position of the word w2 in the matched string, then retrieve the initial word with word = initial_input[x:y]

Gribouillis 1,391 Programming Explorer Team Colleague

I had a problem but I fixed it so dw about this post ;)

Notice that you can extract the words while sustituting in the template

from functools import partial
from pprint import pprint
import re
spaces_re = re.compile(r"\s+")
template_re = re.compile(r"<\w+>")

def template_helper(words, match):
    word = match.group(0)
    words.append(word[1:-1])
    return "(?P{0}\w+)".format(match.group(0))

def compile_template(template):
    template = spaces_re.sub(r"\s+", template)
    words = list()
    regex = template_re.sub(partial(template_helper, words), template)
    regex = re.compile(regex)
    return regex, words

everything = list()

with open('rules.txt', 'rU') as rules:
    for line in rules:
        cutstart, cutend = line.split("|", 1)
        cutstart, cutend = cutstart.rstrip(), cutend.lstrip()
        regex, words = compile_template(cutstart)
        everything.append((cutstart, cutend, words, regex))

pprint(everything)

""" my output -->
[('i am <state>',
  'How long have you been <state>?\n',
  ['state'],
  <_sre.SRE_Pattern object at 0x7fb012eadcf0>),
 ('you <w1> me',
  'What makes you think I <w1> you?\n',
  ['w1'],
  <_sre.SRE_Pattern object at 0x7fb012eb55e0>),
 ('you <w1> <w2> me',
  'What makes you think I <w1> <w2> you?\n',
  ['w1', 'w2'],
  <_sre.SRE_Pattern object at 0x7fb012eb7730>)]
"""
Gribouillis 1,391 Programming Explorer Team Colleague

You can add some features, for example allow arbitrary white space

spaces = re.compile(r"\s+")
template = "can you <w1> <w2> <w3> something"
template = spaces.sub(r"\s+", template)
# the rest as before

this allows the user to enter

can   you help         me with     something
Gribouillis 1,391 Programming Explorer Team Colleague

Wow, all I had to do was return the function on the exceptions instead of calling it. I don't know why, but that just didn't click. Thank you so much for your reply, it works perfectly now!

A high percentage of errors involving None come from forgotten return :)

JoshuaBurleson commented: sweet and simple +3
Gribouillis 1,391 Programming Explorer Team Colleague

Thanks that's done it. And what about with multiple missing words?

well

>>> template = "can you <w1> <w2> <w3> something"
>>> regex = template_re.sub(template_helper, template)
>>> print regex
can you (?P<w1>\w+) (?P<w2>\w+) (?P<w3>\w+) something
>>> regex = re.compile(regex)
>>> match = regex.match("can you help me with something")
>>> match.group("w2")
'me'
Gribouillis 1,391 Programming Explorer Team Colleague

And, it doesn't work when say, the input is 'i suppose i am hungry' It only matches the exact case of 'i am hungry'. So how could I, in clearer terms, make it change from: input = rule, to: input in rule

Use match = myregex.search(sentence) instead of match = myregex.match(sentence) . Regular expressions take some time to get used to: don't hesitate to explore the re module and exemples in tutorials. What about 'i am Nostradamus' ?

Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that you call kcinput() instead of return kcinput(). Use a loop for such problems instead of recursion

from __future__ import print_function
import sys
if sys.version_info < (3,):
    input = raw_input
# these lines were for python before 3.0

def kcinput():
    for i in range(2):
        try:
            return float(input("\nWhat is the degrees in Celsius? "))
        except Exception:
            if i:
                raise
            else:
                print("You must enter a number!")

if __name__ == "__main__":
    print(kcinput())
Gribouillis 1,391 Programming Explorer Team Colleague

Well that's just it. I have to use a template because I can't predict the sentence I am going to match. And so my code has to be generic; to work for anything. The user is supposed to be able to add their own rules to the text file for me to match with the input.

Then you can try simple regex substitution like this

>>> import re
>>> template_re = re.compile(r"<\w+>")
>>> def template_helper(match):
...  return "(?P{0}\w+)".format(match.group(0))
... 
>>> template = "i am <state>"
>>> regex = template_re.sub(template_helper, template)
>>> print repr(regex)
'i am (?P<state>\\w+)'
>>> regex = re.compile(regex)
>>> match = regex.match("i am hungry")
>>> match.group("state")
'hungry'

This could be a starting point.

Gribouillis 1,391 Programming Explorer Team Colleague

Thanks that works. So if the first line was: i am <something> How can I change the <something> to (?P<something>\w+)? In other words, changing it so I can use the re.compile function?

And, how can I only make it so it performs the print when the match is true? I tried

if state_re.match(input_words) ==True:
    print match.group("state")

but it doesn't print anything regardless...

I think you will have to write the regular expressions by hand: parsing templates to produce valid regular expressions is another problem. The first thing to do is to read the re module documentation (also have a look in python tutorials).
The match() method returns a "match object" when the sentence matches and None if it doesn't, so the good test is if match is not None:... .

Gribouillis 1,391 Programming Explorer Team Colleague

The simplest tool is regular expressions (the re module). For example

>>> import re
>>> state_re = re.compile(r"i am (?P<state>\w+)")
>>> match = state_re.match("i am hungry")
>>> print match.group("state")
hungry

This has serious limitations and you may need a real parser for your grammar rules. With a learning effort, you could use the "natural language toolkit" (the module nltk) which contains tools to parse english sentences. See http://www.nltk.org/

Gribouillis 1,391 Programming Explorer Team Colleague

With your rules, I would suggest a game logic such as

class OutOfCards(Exception):
    # raised by choice() or by deck.draw()
    pass

# initialize ai, player, deck
# the toss() method is supposed to return True or False

try:
    x, y = ai, player
    while True:
        x, y = y, x
        card = x.choice()
        if y.toss(card) or deck.draw() == card:
            x.toss(card)
            x.score += 1
except OutOfCards:
    tally_scores(player, ai)
Gribouillis 1,391 Programming Explorer Team Colleague

I get an IndexError at line 58, meaning that the list self.cards is empty. This happens in a sequence of nested calls of give() and decide(). There should be a for loop somewhere. I don't like the self.decide(self) at line 42 in give(). Perhaps you should describe the intended progress of the game with pseudo code.

Gribouillis 1,391 Programming Explorer Team Colleague

I did not find exact match as round rounds numbers, does not cut. It is simple to write small function though:

>>> def cut(n, decimals):
	return int(n*10**decimals)/10**decimals

>>> cut(7339520/(1024.0*1024), 3)
6.999
>>> cut(7339520/(1024.0*1024), 2)
6.99
>>>

you can use math.trunc() instead of int().

Gribouillis 1,391 Programming Explorer Team Colleague

If it stops working, there must be a traceback or something. Can you post the traceback ? Also your code doesn't show what is db nor its content, perhaps you could attach the shelve file in a zipped form. The 'global db' statements are suspicious. At best, they serve no purpose.

Gribouillis 1,391 Programming Explorer Team Colleague

does it say how in the documentation? I tried that with an Entry(self) and when I tried stating if the variable I had assigned to it == F1 "how ever I hit the F1 button" nothing happened, it just wouldn't accept the input from the key "while writing the program, not user end."

Here is an exemple, adapted from an old forum post, to show you how to bind the F1 key

from functools import partial
from Tkinter import *

def fonction(text, event):
    text.insert(END,'ok ')

def bind(wid):
    wid.bind('<F1>',partial(fonction, wid))

def unbind(wid):
    wid.unbind('<F1>')

root = Tk()
text = Text(root)
text.pack()
b1 = Button(root,text="Bind",command=partial(bind, text))
b2 = Button(root,text="Unbind",command=partial(unbind, text))
b1.pack()
b2.pack()
b1.invoke()
text.focus_set()
root.mainloop()

Also read this http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

Gribouillis 1,391 Programming Explorer Team Colleague

I'm guessing the only way to do this is with some third party modules?

You can use them with the GUI toolkits (eg Tkinter).

Gribouillis 1,391 Programming Explorer Team Colleague

Consider the following class hierarchy

import inspect

class A(object):
    def func(self):
        return "A.func"

class B(A):
    def func(self):
        return "B.func"
    
class C(A):
    def func(self):
        return "C.func"
    
class D(B, C):
    def func(self):
        return "D.func"
    
class E(D):
    def func(self):
        return "E.func"
    
if __name__ == "__main__":
    def printmro(k):
        print "mro({0}): {1}".format(k.__name__,
                ", ".join(klass.__name__ for klass in inspect.getmro(k)))
    for k in (A,B,C,D,E):
        printmro(k)
    x = E()
    print "type(x):", type(x).__name__
    print "x.func():", x.func()
    print "super(D, x).func():", super(D, x).func()

""" my output-->
mro(A): A, object
mro(B): B, A, object
mro(C): C, A, object
mro(D): D, B, C, A, object
mro(E): E, D, B, C, A, object
type(x): E
x.func(): E.func
super(D, x).func(): B.func
"""

The mro of a type is the 'method resolution order'. It means that when looking for the method x.func, since x is a E instance, the interpreter looks for a method func() in each of the classes E, D, B, C, A, object in this order, because this is class E's mro.

Now, since x is also an instance of D, we can write super(D, x).func(). Then the method will be searched in D's mro without the class D itself. Since D's mro is D, B, C, A, object, python looks in the list of classes B, C, A, object, and it finds B.func().

In python 2, super is not very useful because one must explicitely use a reference to the class (Application in your example). In python 3, you can use super() in a method without the class name …

Gribouillis 1,391 Programming Explorer Team Colleague

The body of Evolve_ms(), which is called by Evolve_m1() and Evolve_mv(), uses the global E and not the local E wich is updated in each iteration in F(). A solution is to pass E as a parameter and define Evolve_ms(m, E), Evolve_m1(m, E) and Evolve_mv(m, E).

There may be other errors like this, but I couldn't see them. It would be a good idea to use a class with methods instead of a bunch of global functions and variables.

Gribouillis 1,391 Programming Explorer Team Colleague

Thanks for the prompt response, however I'm having problems installing the xlrd module, could provide more details regarding the "easy_install" command?

I went here: http://pypi.python.org/pypi/xlrd and downloaded the MS Windows Installer, when I ran it it found where my python was installed but when I tried to import it into my script the module wasn't found.

You are probably using the cygwin version of python. To have easy_install, download setuptools as described here
http://pypi.python.org/pypi/setuptools#cygwin-mac-os-x-linux-other (the file to download is near the end of the web page). Once setuptools is installed, you can install xlrd (as well as 15000 other modules in pypi) by running easy_install xlrd on the cygwin command line (when a working internet connection is available).

Gribouillis 1,391 Programming Explorer Team Colleague

You only need to read the spreadsheet file in sys.argv and use the module xlrd

#!/usr/bin/env python
import sys
import xlrd

if __name__ == "__main__":
    assert len(sys.argv) == 2
    spreadsheet = sys.argv[-1]
    wb = xlrd.open_workbook(spreadsheet)
    sh = wb.sheet_by_index(0)
    my_variable = sh.cell_value(0,0) # access a cell

Get xlrd from pypi with the easy_install command.

Gribouillis 1,391 Programming Explorer Team Colleague
x          y            z
0.045     0.45        0.0002
0.004     0.0001      0.0005678

I also suggest a hand made solution

import csv
from math import sqrt
import os

class MaxiAverageCalculator(object):
    def __init__(self):
        self.ncols = 3
        self.nrows = 0
        self.s = [0.0, 0.0, 0.0]
        self.s2 = [0.0, 0.0, 0.0]
        
    def run(self, fullpath):
        with open(fullpath, "rb") as infile:
            reader = csv.reader(infile, delimiter=",")
            self.colnames = list(next(reader)) # skip first line with column names
            assert len(self.colnames) == 3
            for row in reader:
                L = [ float(x) for x in row ]
                assert len(L) == 3
                for i, x in enumerate(L):
                    self.s[i] += x
                    self.s2[i] += x * x
                    self.nrows += 1
        self.avg = [x/self.nrows for x in self.s]
        self.std = [ sqrt((y/self.nrows) - a * a) for a, y in zip(self.avg, self.s2) ]
        print "Results for {0}".format(fullpath)
        for name, a, s in zip(self.colnames, self.avg, self.std):
            print "{0}: avg = {1:.5f}, std = {2:.5f}".format(name, a, s)
        print
            
if __name__ == "__main__":
    path="A:\\yoyo\\heyy\\folder"
    dirList=os.listdir(path)
    for file in dirList:
        fullpath=os.path.join(path,file)
        calc = MaxiAverageCalculator()
        calc.run(fullpath)
Gribouillis 1,391 Programming Explorer Team Colleague

i changed it to CSV files and it prints out an error message that says the local variable 'wb' referenced before assignment

It's probably because the first time I wrote open(name, wb) instead of open(name, "wb") . Try with the new version.

Gribouillis 1,391 Programming Explorer Team Colleague

the script works when i have few excels saved in a folder, but it does work when i run the script on a folder that has more than 100 excels that has more than 10 excels for a patient.......this is the error message it prints out....please explain??

Traceback (most recent call last):
  File "C:\Amanu\Amanu_summer_2011_Scripts\Patient_ID_Columnvalues.py", line 45, in <module>
    main(SOURCE_DIR, OUTPUT_DIR)
  File "C:\Amanu\Amanu_summer_2011_Scripts\Patient_ID_Columnvalues.py", line 42, in main
    create_workbook(dstdir, ID, triples)
  File "C:\Amanu\Amanu_summer_2011_Scripts\Patient_ID_Columnvalues.py", line 36, in create_workbook
    sh.write(row, col, val)
  File "C:\Python26\Lib\site-packages\xlwt\Worksheet.py", line 1003, in write
    self.row(r).write(c, label, style)
  File "C:\Python26\Lib\site-packages\xlwt\Worksheet.py", line 1048, in row
    self.__rows[indx] = self.Row(indx, self)
  File "C:\Python26\Lib\site-packages\xlwt\Row.py", line 40, in __init__
    raise ValueError("row index (%r) not an int in range(65536)" % rowx)
ValueError: row index (65536) not an int in range(65536)

There seems to be a limitation of xlwt: row indexes are stored in a 16 bits integer, therefore a sheet can't have more than 65536 rows. From what I read elsewhere it was also a limitation of excel 2003.

I suggest that you write a csv file instead of an excel file (then perhaps you can convert the csv file to xls with openoffice or excel)

def create_workbook(dstdir, ID, triples):
    name = pjoin(dstdir, ID + ".csv")
    import csv
    with open(name, "wb") as outfile:
        wb = csv.writer(outfile, delimiter = ',')
        wb.writerow("Xvalue Yvalue Zvalue".split())
        wb.writerows(triples)

Another solution would be to write in more than 3 columns. I understand that according to the same limitation of excel 2003, there can be 256 colums, which means 85 patient records. …

Gribouillis 1,391 Programming Explorer Team Colleague

I think the following code should work, if ordering the files for each patient alphabetically gives the correct order

from itertools import groupby
import os
from os.path import join as pjoin
import xlrd, xlwt

SOURCE_DIR = "E:\\IMRT_C\\Preanalysis\\"
OUTPUT_DIR = "E:\\IMRT_C\\Working_files"

def first_eight(string):
    return string[:8]

def files_by_patient(srcdir):
    filenames = sorted(f for f in os.listdir(srcdir) if f.endswith(".xls"))
    for ID, group in groupby(filenames, first_eight):
        yield ID, [ pjoin(srcdir, filename) for filename in group ]

def triples_by_patient(srcdir):
    for ID, files in files_by_patient(srcdir):
        yield ID, value_triples(xlrd.open_workbook(f).sheet_by_name(
                                          u'RawTrackingData') for f in files)

def value_triples(sheet_sequence):
    for sh in sheet_sequence:
        for j in xrange(21, sh.nrows):
            yield tuple(sh.cell_value(j, i) for i in range(3))

def create_workbook(dstdir, ID, triples, sheet_name = u"TrackingData"):
    name = pjoin(dstdir, ID + ".xls")
    wb = xlwt.Workbook()
    try:
        sh = wb.add_sheet(sheet_name)
        for col, val in enumerate("Xvalue Yvalue Zvalue".split()):
            sh.write(0, col, val)
        for row, triple in enumerate(triples, 1):
            for col, val in enumerate(triple):
                sh.write(row, col, val)
    finally:
        wb.save(name)
        
def main(srcdir, dstdir):
    for ID, triples in triples_by_patient(srcdir):
        create_workbook(dstdir, ID, triples)

if __name__ == "__main__":
    main(SOURCE_DIR, OUTPUT_DIR)
Gribouillis 1,391 Programming Explorer Team Colleague

Ok, now since peter_budo closed the other thread with the same question, see my answer here http://www.daniweb.com/software-development/python/threads/377865/1627143#post1627143

Gribouillis 1,391 Programming Explorer Team Colleague

The common idiom is

thelist[:] = (x for x in thelist if not criteria(x))

# for example

thelist[:] = (x for x in thelist if not 'a' in x)
JoshuaBurleson commented: Thank you so much! +1
Gribouillis 1,391 Programming Explorer Team Colleague

As a first hint, look at line 248 for example. Evolve_rho_ss(rho) is called 8 times in the same expression. I assume that all these calls involve arrays of real numbers. Obviously you should call the function only once temp = Evolve_rho_ss(rho) and then use the variable temp 8 times in the expression if this is needed. The call could perhaps even be done before the for loop.

The same problem arises in many of your functions for many calls. So The first thing to do is to remove all the calls which compute exactly the same quantity in each function and store the result of each single call in a local variable. This may speed up your program considerably.

Try to give descriptive names to these local variables (instead of 'temp').

Gribouillis 1,391 Programming Explorer Team Colleague

This is what im facing when I try to run the python interactively from the command prompt:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Shankaranarayanan>cd\

C:\>python
Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
File "<stdin>", line 1
print "hello"
^
SyntaxError: invalid syntax
>>>


is this a problem with the way environment variables are set??
IF so can someone give me the step by step method to set them as i deleted them by mistake!

In principle, you don't need to cd to python's folder to invoke the command 'python' if C:\Python32 is in your PATH variable. Also I don't understand why in first post it was C:\Python27...

Your python statement fails because the 'print' statement became a function in python 3.0. So now, you must write

print("hello")

with parenthesis. I understand that you're a python beginner, so use a tutorial for python 3.x and not python 2.x. There are not so many differences, but 'print' is one of them. Also, please use code tags when you post code in the forum. Read this http://www.daniweb.com/software-development/python/threads/366794 and this http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3