Printing n elements per line in a list
If have a list from 1 to 100, what's the easiest, most elegant way to print them out, so that there are only n elements per line. So if n=5, the printed list would look like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 etc. My search through the previous posts yields methods to print all the values of the list on a single line, but that's not what I want. I feel like there is an easy, pretty way to do this. I think it's possible to hack it up using while loops and some ugly slicing, but hopefully I'm missing something -- http://mail.python.org/mailman/listinfo/python-list
Integrating Python with Fortran
Hi all, I'm currently working on a large, legacy Fortran application. I would like to start new development in Python (as it is mainly I/O related). In order to do so, however, the whole project needs to be able to compile in Fortran. I'm aware of resources like the F2Py Interface generator, but this only lets me access the Fortran modules I need in Python. I'm wondering if there's a way to generate the .o files from Python (maybe using py2exe?) and then link the .o file with the rest of the Fortran project using something like gcc. I realize that all of this is highly dependent on the libraries I use, etc, but I'm just looking for general strategies to attack the problem or someone to tell me that this is impossible. -- http://mail.python.org/mailman/listinfo/python-list
Avoiding if..elsif statements
I have a program where based on a specific value from a dictionary, I call a different function. Currently, I've implemented a bunch of if..elsif statements to do this, but it's gotten to be over 30 right now and has gotten rather tedious. Is there a more efficient way to do this? Code: value = self.dictionary.get(keyword)[0] if value == "something": somethingClass.func() elsif value == "somethingElse": somethingElseClass.func() elsif value == "anotherthing": anotherthingClass.func() elsif value == "yetanotherthing": yetanotherthingClass.func() Is it possible to store these function calls in a dictionary so that I could just call the dictionary value? -- http://mail.python.org/mailman/listinfo/python-list
Re: Avoiding if..elsif statements
the missing () was the trick! However, I'm passing in a few variables, so I can't just take it out-though every single function would be passing the same variables. so something.func() is actually something.func(string, list) How would I modify it to include them? Sorry I didn't include them the first time, I was trying to simplify it to make it easier...oops! Fredrik Lundh wrote: > "unexpected" <[EMAIL PROTECTED]> wrote: > > > I have a program where based on a specific value from a dictionary, I > > call a different function. Currently, I've implemented a bunch of > > if..elsif statements to do this, but it's gotten to be over 30 right > > now and has gotten rather tedious. Is there a more efficient way to do > > this? > > > > Code: > > > > value = self.dictionary.get(keyword)[0] > > > > if value == "something": > > somethingClass.func() > > elsif value == "somethingElse": > > somethingElseClass.func() > > elsif value == "anotherthing": > > anotherthingClass.func() > > elsif value == "yetanotherthing": > > yetanotherthingClass.func() > > > > Is it possible to store these function calls in a dictionary so that I > > could just call the dictionary value? > > but of course (did you try it?). here's an outline: > > dispatch = { > "something": somethingClass.func, # note: no () here > "somethingElse": somethingElseClass.func, > "anotherthing": anotherthingClass.func, > "yetanotherthing": yetanotherthingClass.func, > } > > ... > > dispatch[value]() # note: do the call here! > > or, a bit more robust: > > try: > func = dispatch[value] > except KeyError: > print "- no handler for", value > else: > func() > > tweak as necessary. > > -- http://mail.python.org/mailman/listinfo/python-list
Strange import behavior
Hey guys, I'm having problems importing a file into my python app. Everytime I try to define the object specified by this file, i.e, test = Test(), It raises an ImportError exception: ImportError: cannot import name Test. I've declared it as: from test import Test (and I've also tried from test import *) As luck would have it, if I try creating a newfile (say test2) and then declare the same class as: from test2 import Test() It works fine! I tried doing this with the first couple of files, but then another error crops up for another file giving me this ImportError...so I'd end up having to re-copy a bunch of files over again. I feel like there may be some sort of dependency screw up or whatever..Is there a command that's like "Python, chill, let's start from scratch and build this thing"? -- http://mail.python.org/mailman/listinfo/python-list
Python regular expression question!
I'm trying to do a whole word pattern match for the term 'MULTX-' Currently, my regular expression syntax is: re.search(('^')+(keyword+'\\b') where keyword comes from a list of terms. ('MULTX-' is in this list, and hence a keyword). My regular expression works for a variety of different keywords except for 'MULTX-'. It does work for MULTX, however, so I'm thinking that the '-' sign is delimited as a word boundary. Is there any way to get Python to override this word boundary? I've tried using raw strings, but the syntax is painful. My attempts were: re.search(('^')+("r"+keyword+'\b') re.search(('^')+("r'"+keyword+'\b') and then tried the even simpler: re.search(('^')+("r'"+keyword) re.search(('^')+("r''"+keyword) and all of those failed for everything. Any suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python regular expression question!
> \b matches the beginning/end of a word (characters a-zA-Z_0-9). > So that regex will match e.g. MULTX-FOO but not MULTX-. > So is there a way to get \b to include - ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python regular expression question!
Sweet! Thanks so much! -- http://mail.python.org/mailman/listinfo/python-list