[Tutor] conventions for establishing and saving default values for variables
Hi I was wondering if anyone could give me some insight as to the best way to get and save variables from a user the first time a script is opened. For example if the script prompts something like "What is the path to the folder?" and the result is held in a variable called thePath, what is the best way to have that variable saved for all subsequent uses of the script by the same user. Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to numerically sort strings that start with numbers?
theList = ["21 trewuuioi","3zxc","134445"] print sorted(theList) Hi, the result of the sorted list above doesn't print in the order I want. Is there a straight forward way of getting python to print ['3zxc','21 trewuuioi','134445'] rather than ['134445', '21 trewuuioi', '3zxc']? Any help would be greatly appreciated Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] find and replace relative to an nearby search string in a file
Hi, I am trying to do a find and replace in a text file (a nuke script). Here are a couple excerpts from the file that demonstrate what I want to do. I am always looking for the line " name Write1" as my starting point. In the first example below, I want to replace the path, which is 2 lines above it. I have made a basic script to do that and it works fine. The problem I am having is when the number of lines between " name Write1" and the path above it is not 1, my script breaks. I'm not sure how to say in python "when you find the line " name Write1", go back line by line until you find a line that begins with " file /Volumes/" and then assign a new variable to the path on that line". At the very bottom of this post I have included what I have so far (The script which works but breaks). Any help would be greatly appreciated. Pete Write { file /Volumes/raid0/Z353_002_comp_v27.%04d.cin file_type cin name Write1 xpos 13762 ypos -364 } Write { file /Volumes/raid0/Z353_002_comp_v04.%04d.exr colorspace linear raw true file_type exr name Write1 selected true xpos -487 ypos -155 } # This is the basic script import re theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk',"r") theNukeScriptText = theFile.read() theFile.close() #p = re.compile('.+_comp_v\d\d.%04d.cin\n file_type cin\n name Write1') p = re.compile(r'.+_comp_v\d\d.%04d.cin\n.+\n name Write1') m = p.finditer(theNukeScriptText) the3Lines = p.findall(theNukeScriptText) the3LinesString = ''.join(the3Lines) theOldSeq = the3LinesString.split('\n')[0] print str(the3LinesString.split('\n')[0]) theNewSeq = 'file /Volumes/raid0/Z353_002_comp_v27.%04d.cin' theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk', "w") theFile.write(theNukeScriptText.replace(theOldSeq,theNewSeq)) theFile.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] open linux file browser from nuke python script
Hi, I am trying to get a python script to open up a file browser window with the location of a folder. I am using kubuntu 10.04. The string that gets created works fine if I paste it into a shell eg: 'kde-open path/to/the/sequence', but it doesn't seem to want to run from within my interpreter using either subprocess.Popen or os.system For the first argument I have tried 'kde-open', 'konqueror', and a few others that all work fine in the shell but not in my interpreter. Here is what my script looks like: import nuke import subprocess def showFolder(): subprocess.Popen(['kde-open', 'path/to/the/sequence']) showFolder() ### I must to run it from a specific interpreter (Nuke, the compositing software) Anyone have any idea what I might be doing wrong? Can anyone suggest a workaround? Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] getting the last file in a folder (reliably)
Hi I have some code which works nine times out of ten. Maybe some could help me make this a little more robust. I have a bunch of files in a folder with a strict versioning based naming convention, where I want to open the highest version number of a nuke script (not necessarily the one with the newest modification date). So in the example folder contents listed below I want to open "233_bb0072_comp_comp2k_v05.nk" (fourth from the bottom). Every once in a while my python script ends up opening 233_bb0072_comp_comp2k_v04.nk (the next to last correct one). The script I am using is shown below. Can anyone explain why it doesn't always work? 233_bb0072_comp_comp2k_v01.nk 233_bb0072_comp_comp2k_v01.nk~ 233_bb0072_comp_comp2k_v02.nk 233_bb0072_comp_comp2k_v03.nk 233_bb0072_comp_comp2k_v03.nk~ 233_bb0072_comp_comp2k_v04.nk 233_bb0072_comp_comp2k_v04.nk~ 233_bb0072_comp_comp2k_v05.nk 233_bb0072_comp_comp2k_v05.autosave 233_bb0072_comp_comp2k_v05.nk~ 233_bb0072_comp_comp2k_v06.nk ### import os def openNewestCompCommandLine(): theDirectory = "/path/to/my/comps/" theFilesInTheFolder = os.listdir(theDirectory) for aFile in theFilesInTheFolder: if "~" not in aFile: if "autosave" not in aFile: theNukeFileName = aFile theFullPath = theDirectory + theNukeFileName os.system("nuke " + theFullPath) if __name__ == '__main__': openNewestCompCommandLine() for aFile in theFilesInTheFolder: print aFile Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to temporarily disable a function
Hi I was wondering if there is a way to disable a function. Hi have a GUI grid snapping function that I use in a program called Nuke (the film compositing software) Here is the function (which loads when Nuke loads): ### def theAutoplaceSnap(): try: nuke.thisNode().autoplace() n = nuke.allNodes(); for i in n: nuke.autoplaceSnap(i) except: pass nuke.addOnUserCreate(theAutoplaceSnap) ### I have many functions which get loaded, but this particular one needs to be disabled when I am viewing another compositors script in the gui. I have a python script editor in Nuke in which I can run code if need be to run code on the fly. Help -- Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] return, why do I need it?
Hi I have been writing python code for a while now and I never return anything within any of my functions, I just (eg.) print stuff or make directories or update a log or what have you. When I look at other people's code they are always returning in their functions and I was wondering if someone could give me an example of when I would absolutely have to return something. The thing I don't like about returning is that when I unindent a function and try to run the code to inspect parts of it for debugging I always have to alter the code so as not to get the "return not inside a function error", so I will change the word "return" to "print" and in many cases that's the way I leave it. Anyone have any thoughts on this? Thanks Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] commands.getoutput equivalent in subprocess
Hello I print a lot of values at work using grep and need to be constantly opening a shell window to do this (rather than staying within my main program which is Nuke by the Foundry). Is there a simple equilavent to commands.getoutput that is more up to date (I would assume within the subprocess module)? It sounds like the commands module will be phased out soonish. I am using Python 2.6 Here is the kind of thing I would like to do in subprocess import commands output = commands.getoutput("echo Hello World!") print output Thanks Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] commands.getoutput equivalent in subprocess
Ok thanks a lot. Pete On Mon, Mar 19, 2012 at 10:01 PM, Peter Otten <__pete...@web.de> wrote: >> Pete O'Connell wrote: > >> Hi, I am using Python 2.6 I can't use Python 3 in this particular > situation. > > [Please reply to the list, not in private mail.] > > You can continue to use commands.getoutput() in 2.6 and 2.7, and once you > are ready to make the jump to 3.x replace it with subprocess.getoutput(). > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] list comprehension, testing for multiple conditions
Hi I am trying to parse a text file and create a list of all the lines that don't include: "vn", "vt" or are empty. I want to make this as fast as possible because I will be parsing many files each containing thousands of lines. I though I would give list comprehensions a try. The last 3 lines of the code below have three list comprehensions that I would like to combine into 1 but I am not sure how to do that. Any tips would be greatly appreciated pete #start fileName = '/usr/home/poconnell/Desktop/objCube.obj' theFileOpened = open(fileName,'r') theTextAsList = theFileOpened.readlines() theTextAsListStripped = [] for aLine in theTextAsList: theTextAsListStripped.append(aLine.strip("\n")) theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""] #end and here is the simple file I am parsing as a test: #start## ## OBJ file generated by Nuke ## # vertex list - offset=0 v 0.00 0.00 1.00 v 1.00 0.00 1.00 v 0.00 1.00 1.00 v 1.00 1.00 1.00 v 1.00 0.00 0.00 v 0.00 0.00 0.00 v 1.00 1.00 0.00 v 0.00 1.00 0.00 v 1.00 0.00 1.00 v 1.00 0.00 0.00 v 1.00 1.00 1.00 v 1.00 1.00 0.00 v 0.00 0.00 0.00 v 0.00 0.00 1.00 v 0.00 1.00 0.00 v 0.00 1.00 1.00 v 0.00 1.00 1.00 v 1.00 1.00 1.00 v 0.00 1.00 0.00 v 1.00 1.00 0.00 v 0.00 0.00 0.00 v 1.00 0.00 0.00 v 0.00 0.00 1.00 v 1.00 0.00 1.00 # point texture coordinates - offset=0 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 vt 0.00 0.00 vt 1.00 0.00 vt 0.00 1.00 vt 1.00 1.00 # vertex normals - offset=0 vn 0.00 0.00 1.00 vn 0.00 0.00 1.00 vn 0.00 0.00 1.00 vn 0.00 0.00 1.00 vn 0.00 0.00 -1.00 vn 0.00 0.00 -1.00 vn 0.00 0.00 -1.00 vn 0.00 0.00 -1.00 vn 1.00 0.00 0.00 vn 1.00 0.00 0.00 vn 1.00 0.00 0.00 vn 1.00 0.00 0.00 vn -1.00 0.00 0.00 vn -1.00 0.00 0.00 vn -1.00 0.00 0.00 vn -1.00 0.00 0.00 vn 0.00 1.00 0.00 vn 0.00 1.00 0.00 vn 0.00 1.00 0.00 vn 0.00 1.00 0.00 vn 0.00 -1.00 0.00 vn 0.00 -1.00 0.00 vn 0.00 -1.00 0.00 vn 0.00 -1.00 0.00 f 1/1/1 2/2/2 4/4/3 3/3/4 f 5/5/5 6/6/6 8/8/7 7/7/8 f 9/9/9 10/10/10 12/12/11 11/11/12 f 13/13/13 14/14/14 16/16/15 15/15/16 f 17/17/17 18/18/18 20/20/19 19/19/20 f 21/21/21 22/22/22 24/24/23 23/23/24 # end of file ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
What a great mailing list! Thanks for all the responses. I have a few questions, though, first in regards to Puneeth's code. He writes to use: >theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" >not in x and "vt" not in x and x!= ""] It works but what I don't understand about this line is why the ands are nor ors ("or" doesn't work even though I would have expected it to) I am sure I will have a few more questions over the next couple days as I work my way through the responses. Thanks Pete On Wed, Aug 22, 2012 at 6:23 PM, Puneeth Chaganti wrote: > On Wed, Aug 22, 2012 at 11:35 AM, Pete O'Connell > wrote: >> Hi I am trying to parse a text file and create a list of all the lines >> that don't include: "vn", "vt" or are empty. I want to make this as >> fast as possible because I will be parsing many files each containing >> thousands of lines. I though I would give list comprehensions a try. >> The last 3 lines of the code below have three list comprehensions that >> I would like to combine into 1 but I am not sure how to do that. >> Any tips would be greatly appreciated >> >> pete >> >> #start >> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >> theFileOpened = open(fileName,'r') >> theTextAsList = theFileOpened.readlines() >> >> theTextAsListStripped = [] >> for aLine in theTextAsList: >> >> theTextAsListStripped.append(aLine.strip("\n")) >> >> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""] > > Something like this should work : > > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped > if "vn" not in x and "vt" not in x and x!= ""] > > HTH, > Puneeth -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
Thanks Peter. This looks like what I need: with open(fileName) as lines: wanted = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] Cheers And in response to Allan's suggestion. I can see using a generator in a situation where the if statements were more numerous and complex. I am sure that will come in handy. Thanks On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__pete...@web.de> wrote: > Pete O'Connell wrote: > >> Hi I am trying to parse a text file and create a list of all the lines >> that don't include: "vn", "vt" or are empty. I want to make this as >> fast as possible because I will be parsing many files each containing >> thousands of lines. I though I would give list comprehensions a try. >> The last 3 lines of the code below have three list comprehensions that >> I would like to combine into 1 but I am not sure how to do that. >> Any tips would be greatly appreciated >> >> pete >> >> #start >> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >> theFileOpened = open(fileName,'r') >> theTextAsList = theFileOpened.readlines() > > If you have a file with 1,000,000 lines you have now a list of 1,000,000 > strings of which perhaps 1,000 match your criteria. You are squandering > memory. Rule of thumb: never use readlines(), iterate over the file > directly. > >> theTextAsListStripped = [] >> for aLine in theTextAsList: >> >> theTextAsListStripped.append(aLine.strip("\n")) >> >> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != >> ""] > > I think that should be > > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x != > ""] > > You can combine the three if clauses or add them all to one list-comp: > > with open(filename) as lines: > wanted = [line.strip("\n") for line in lines > if "vn" not in line and "vt" not in line and line != "\n"] > > > You can even have multiple if clauses in one list-comp (but that is rarely > used): > > with open(filename) as lines: > wanted = [line.strip("\n") for line > if "vn" not in line > if "vt" not in x > if line != "\n"] > > While your problem is simple enough to combine all filters into one list- > comp some problems are not. You can then prevent the intermediate lists from > materializing by using generator expressions. The result minimizes memory > consumption, too, and should be (almost) as fast. For example: > > with open(filename) as lines: > # use gen-exps to remove empty and whitespace-only lines > stripped = (line.strip() for line in lines) > nonempty = (line for line in stripped if line) > > wanted = [line for line in nonempty > if "vt" not in line and "vn" not in line] > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
Hi. The next step for me to parse the file as I want to is to change lines that look like this: f 21/21/21 22/22/22 24/24/23 23/23/24 into lines that look like this: f 21 22 23 24 Below is my terribly slow loop for doing this. Any suggestions about how to make this code more efficient would be greatly appreciated fileName = '/usr/home/poconnell/Desktop/objCube.obj' with open(fileName) as lines: theGoodLines = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] for i in range(len(theGoodLines)): if theGoodLines[i][0] == "f": aGoodLineAsList = theGoodLines[i].split(" ") theGoodLines[i] = aGoodLineAsList[0] + " " + aGoodLineAsList[1].split("/")[-1] + " " + aGoodLineAsList[2].split("/")[-1] + " " + aGoodLineAsList[3].split("/")[-1] + " " + aGoodLineAsList[4].split("/")[-1] for anItem in theGoodLines: print anItem ## Thanks! Pete On Wed, Aug 22, 2012 at 9:59 PM, Pete O'Connell wrote: > Thanks Peter. This looks like what I need: > > with open(fileName) as lines: > wanted = [line.strip("\n") for line in lines if "vn" not in line > and "vt" not in line and line != "\n"] > > Cheers > > And in response to Allan's suggestion. I can see using a generator in > a situation where the if statements were more numerous and complex. I > am sure that will come in handy. > > Thanks > > > On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__pete...@web.de> wrote: >> Pete O'Connell wrote: >> >>> Hi I am trying to parse a text file and create a list of all the lines >>> that don't include: "vn", "vt" or are empty. I want to make this as >>> fast as possible because I will be parsing many files each containing >>> thousands of lines. I though I would give list comprehensions a try. >>> The last 3 lines of the code below have three list comprehensions that >>> I would like to combine into 1 but I am not sure how to do that. >>> Any tips would be greatly appreciated >>> >>> pete >>> >>> #start >>> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >>> theFileOpened = open(fileName,'r') >>> theTextAsList = theFileOpened.readlines() >> >> If you have a file with 1,000,000 lines you have now a list of 1,000,000 >> strings of which perhaps 1,000 match your criteria. You are squandering >> memory. Rule of thumb: never use readlines(), iterate over the file >> directly. >> >>> theTextAsListStripped = [] >>> for aLine in theTextAsList: >>> >>> theTextAsListStripped.append(aLine.strip("\n")) >>> >>> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >>> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != >>> ""] >> >> I think that should be >> >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x != >> ""] >> >> You can combine the three if clauses or add them all to one list-comp: >> >> with open(filename) as lines: >> wanted = [line.strip("\n") for line in lines >> if "vn" not in line and "vt" not in line and line != "\n"] >> >> >> You can even have multiple if clauses in one list-comp (but that is rarely >> used): >> >> with open(filename) as lines: >> wanted = [line.strip("\n") for line >> if "vn" not in line >> if "vt" not in x >> if line != "\n"] >> >> While your problem is simple enough to combine all filters into one list- >> comp some problems are not. You can then prevent the intermediate lists from >> materializing by using generator expressions. The result minimizes memory >> consumption, too, and should be (almost) as fast. For example: >> >> with open(filename) as lines: >> # use gen-exps to remove empty and whitespace-only lines >> stripped = (line.strip() for line in lines) >> nonempty = (line for line in stripped if line) >> >> wanted = [line for line in nonempty >> if "vt" not in line and "vn" not in line] >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > > -- > - -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
Thanks eryksun, that is a very clear example. Cheers pete On Wed, Aug 22, 2012 at 11:16 PM, eryksun wrote: > On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__pete...@web.de> wrote: >> >> wanted = [line.strip("\n") for line in lines >> if "vn" not in line and "vt" not in line and line != "\n"] > > Here's an equivalent expression with the negation factored out: > > not ("vn" in line or "vt" in line or line == "\n") > > http://en.wikipedia.org/wiki/De_Morgan%27s_laws > > If you have a lot of tests all using the same operator (e.g. "in"), > you can use "any" (OR) or "all" (AND) with a generator expression: > > vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"] > wanted = [line for line in lines if not any(v in line for v in vals)] > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano wrote: > On 22/08/12 20:28, Pete O'Connell wrote: >> >> Hi. The next step for me to parse the file as I want to is to change >> lines that look like this: >> f 21/21/21 22/22/22 24/24/23 23/23/24 >> into lines that look like this: >> f 21 22 23 24 > > > In English, what is the rule you are applying here? My guess is: > > "Given three numbers separated by slashes, ignore the first two numbers > and keep the third." > > E.g. "17/25/97" => 97. > > Am I close? Hi Steve, yes that is correct > > > >> Below is my terribly slow loop for doing this. Any suggestions about >> how to make this code more efficient would be greatly appreciated > > > What makes you say it is "terribly slow"? Perhaps it is as fast as it > could be under the circumstances. (Maybe it takes a long time because > you have a lot of data, not because it is slow.) OK maybe I am wrong about it being slow (I thought for loops were slower than lis comprehensions). But I do know I need it to be as fast as possible if I need to run it on a thousand files each with hundreds of thousands of lines > > The first lesson of programming is not to be too concerned about speed > until your program is correct. > > Like most such guidelines, this is not entirely true -- you don't want > to write code which is unnecessarily slow. But the question you should > be asking is, "is it fast enough?" rather than "is it fast?". > > Also, the sad truth is that Python tends to be slower than some other > languages. (It's also faster than some other languages too.) But the > general process is: > > 1) write something that works correctly; > > 2) if it is too slow, try to speed it up in Python; > > 3) if that's still too slow, try using something like cython or PyPy > > 4) if all else fails, now that you have a working prototype, re-write > it again in C, Java, Lisp or Haskell. > > Once they see how much more work is involved in writing fast C code, > most people decide that "fast enough" is fast enough :) OK I will keep it as is and see if I can live with it. Thanks Pete > > > >> with open(fileName) as lines: >> theGoodLines = [line.strip("\n") for line in lines if "vn" not in >> line and "vt" not in line and line != "\n"] > > > I prefer to write code in chains of filters. > > with open(fileName) as lines: > # get rid of leading and trailing whitespace, including newlines > lines = (line.strip() for line in lines) > # ignore blanks > lines = (line in lines if line) > # ignore lines containing "vn" or "vt" > theGoodLines = [line in lines if not ("vn" in line or "vt" in line)] > > Note that only the last step is a list comprehension using [ ], the others > are generator expressions using ( ) instead. > > Will the above be faster than your version? I have no idea. But I think it > is more readable and understandable. Some people might disagree. > > > >> for i in range(len(theGoodLines)): >> if theGoodLines[i][0] == "f": >> aGoodLineAsList = theGoodLines[i].split(" ") >> theGoodLines[i] = aGoodLineAsList[0] + " " + >> aGoodLineAsList[1].split("/")[-1] + " " + >> aGoodLineAsList[2].split("/")[-1] + " " + >> aGoodLineAsList[3].split("/")[-1] + " " + >> aGoodLineAsList[4].split("/")[-1] > > > > Start with a helper function: > > def extract_last_item(term): > """Extract the item from a term like a/b/c""" > return term.split("/")[-1] > > > for i, line in enumerate(theGoodLines): > if line[0] == "f": > terms = line.split() > theGoodLines[i] = " ".join([extract_last_item(t) for t in terms]) > > > > See how you go with that. > > > > -- > Steven > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped > if "vn" not in x > if "vt" not in x > if x!= ""] > > It's slightly more verbose but it makes the rules more explicit, IMHO. I agree, it seems easier to read when written on multiple lines. I'll do it that way, Thanks Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
Ok thanks for the advice everyone. Cheers Pete On Thu, Aug 23, 2012 at 10:58 AM, Jerry Hill wrote: > On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell > wrote: >> OK maybe I am wrong about it being slow (I thought for loops were >> slower than lis comprehensions). But I do know I need it to be as fast >> as possible if I need to run it on a thousand files each with hundreds >> of thousands of lines > > You're quite likely wrong about that. The overall runtime of your > code is very likely to end up dominated by I/O with the hard drive, > not the microsecond differences in how you process each line. The way > to know for sure is to write code that is easy to read and does the > correct thing. Then run the code an see if it's too slow for your > purposes. If it is, profile the code and see where it's spending all > of its time. Only then can you actually optimize the right places. > > Once you've identified where your code needs to be faster, you're > likely to get more mileage out of a clever algorithm change than a > micro-optimization to your filtering. Or maybe you'll discover that it > really is worthwhile to optimize your filtering, because it's inside a > tight loop, and that's where all of your time is spent. There are a > lot of tricks you can use to slightly speed up your python code, but > none of them are worth doing until you know for sure that you're > optimizing in the right place. And if it turns out that the code's > runtime is fine as first written, you don't have to waste your time > doing lots of work for little gain. > > -- > Jerry > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehension, testing for multiple conditions
Hi, I have tried to simplify things and am running into a bit of trouble. What i am really trying to do is: Keep all the lines starting with "v " and then delete those lines whose modulus 5 don't equal zero I have written it like this which seems to take a really long time (a couple of minutes when iteration over a folder with 200 files to parse) # with open(theFilePath) as lines: #keep only the lines beginning with "v " (this works) theGoodLines = [line.strip("\n") for line in lines if "v " == line[0:2]] theLinesAsListSubset = [] for i in range(len(theGoodLines)): nuke.tprint(i) if i%5 != 0: continue elif i%5 == 0: theLinesAsListSubset.append(theGoodLines[i]) I think it would be better to include the modulud test within the original list comprehension but I am not sure how to access the index of "line": #something like this is a sketch of what I mean (I know it's wrong) theGoodLines = [line.strip("\n") for line in lines if "v " == line[0:2] and line.getIndex() % 5 == 0] Do I need enumerate for this maybe? Thanks Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] reducing a list evenly when deleting elements by index
Hi, I have a bezier line with 20 points on it and I am trying to reduce this to a line with 4 points, keeping the first and last points and 2 evenly spaced points in between like so: ** becomes: *. . . .* These points are removable only by index, so if I remove point 2, point 3 then becomes point 2 in the next iteration, so I end up having a spacing like this which is not what I want: *. . . .* Here is the code that I have so far. I hope it makes sense: ## for aNode in nuke.allNodes(): if aNode.shown(): theRotoNode = aNode curveKnob = theRotoNode['curves'] rootLayer = curveKnob.rootLayer theNumberOfPointToReduceTo = 5 for aShape in rootLayer: theOriginalShape = aShape thePointList = [] for aPoint in aShape: thePointList.append(aPoint) if len(thePointList) > 5: theNumberOfPoints = len(thePointList) keepEvery = float(theNumberOfPoints)/theNumberOfPointToReduceTo theIndicesToKeep = [] theIndicesToKeep.append(0) theIndicesToKeep.append(1) for i in range(theNumberOfPoints)[1:-1]: if i*keepEvery < theNumberOfPoints: theIndicesToKeep.append(int(round(i*keepEvery))) theIndicesToKeep.append(theNumberOfPoints-2) theIndicesToKeep.append(theNumberOfPoints-1) thePointsToRemove = tuple(set(range(theNumberOfPoints)) - set(theIndicesToKeep)) #everything is good up to here, the code below doesn't work properly and I'm kinda stuck counter = -1 for aPointIndex in thePointsToRemove: counter+=1 aPointIndex = aPointIndex-(counter) print aPointIndex aShape.remove(aPointIndex) Any advice would be greatly appreciated. Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
Ah of course. Thanks very much Oscar! Pete On Mon, Sep 17, 2012 at 1:29 PM, Oscar Benjamin wrote: > On 17 September 2012 02:15, Pete O'Connell wrote: > >> Hi, I have a bezier line with 20 points on it and I am trying to reduce >> this to a line with 4 points, keeping the first and last points and 2 >> evenly spaced points in between like so: >> ** >> becomes: >> *. . . .* >> >> These points are removable only by index, so if I remove point 2, >> point 3 then becomes point 2 in the next iteration, so I end up having a >> spacing like this which is not what I want: >> *. . . .* > > > But removing item 19 doesn't affect the position of item 2. The trick is > to start the loop at the end of the list and work back to the beginning. > > Oscar > -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
> 1) You have a Bezier curver consisting of 20 control points and would like > to > approximate it with a simpler cubic Bezier curve consisting of only 4 > points. > I would call this "approximating a Bezier with a lower order Bezier". > > Hi Oscar. Thanks for your reply. This one above is the one that I am trying to do. It is true that choosing arbitrary points evenly selected from the original points is not ideal, it is good enough for my purposes in most cases. When I have a bit more time I am going to try to implement the Ramer–Douglas–Peucker algorithm to improve the accuracy of my curve simplification: http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm Thanks Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
Thanks Stephen. That looks like nice clean code too! Cheers Pete On Tue, Sep 18, 2012 at 9:59 AM, Stephen Haywood < step...@averagesecurityguy.info> wrote: > Someone has already tried. https://github.com/sebleier/RDP > > > On Mon, Sep 17, 2012 at 5:50 PM, Pete O'Connell > wrote: > >> When I have a bit more time I am going to try to implement the >> Ramer–Douglas–Peucker algorithm to improve the accuracy of my curve >> simplification: >> >> http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm >> >> >> > -- > Stephen Haywood > Information Security Consultant > CISSP, GPEN, OSCP > T: @averagesecguy > W: averagesecurityguy.info > > -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to load python code only after program startup?
Hi I use a compositing program called Nuke which loads my custom modules on start up. So if I have an error in my python code somewhere, Nuke won't open and it throws a typical error which is easy enough to fix. The problem I am running into is that when others on my network are using an older version of Nuke, some of my code causes their older version to not open. For example, recently I started using gnuplot.py for graphical feed back which the older version of Nuke doesn't like. So my question is: What is the best way to wrap all my custom code so that it isn't read on startup, but rather only after I invoke a "loadMyCustomModules.py" module. Help please:)! Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor