Re: [Tutor] feedback on writing pipelines in python
On Wed, 21 Mar 2012, Abhishek Pratap wrote: Hi Guys I am in the process of perl to python transition for good. Welcome! 1. stitch pipelines : I want python to act as a glue allowing me to run various linux shell based programs. If needed wait for a program to finish and then move on, logs if required You'll want to take a look at the subprocess module. One thing you will notice is that Perl has a lot more terse syntax when it comes to commands line integration. That being said, Python is still fully capable. You may want to check out an Oreilly book called Python for the system administrator by Noah Gift. It doesn't tell you much about each tool but it exposes you to a ton of them. HTH, Wayne___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] opening a Qt .py file from within another python app.
On 22/03/12 03:47, ken brockman wrote: None of which had gotten the Qt window to open. > Yet when I run it directly, on it's own, it open's > and the Qt window is displayed. My guess is that there is an if __name__... clause at the bottom with some code that doesn't get executed when you import it. That clause has some initialisation code that starts the main window up. Try opening the file in an editor and seeing what is happening there. You will either need to move that into a function then call the function after importing, or replicate it in your top level file. Having said that the os.system technique should have worked regardless, but it won't let you do any more integration. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Opening a Qt .py file from within another python app
M On 22/03/12 03:47, ken brockman wrote: >> None of which had gotten the Qt window to open. >> Yet when I run it directly, on it's own, it open's >> and the Qt window is displayed. >My guess is that there is an if __name__... clause >at the bottom with some code that doesn't get >executed when you import it. That clause has some >initialisation code that starts the main window up. >Try opening the file in an editor and seeing what is >happening there. You will either need to move that >into a function then call the function after importing, >or replicate it in your top level file. >Having said that the os.system technique should have worked regardless, but it >won't let you do any more integration. >-- Alan G Thanks much Alan for you time and input. I had managed to get it to run, after reading that the if __name__=__main__ at the bottom of the Qt file is what makes it fire up if run directly. I had figured that if I were to copy those last few lines after that statement from the bottom of the Qt file into my main file, that it may just work. And, after affixing the name of the Qt file, which i had import into my main app, to the beginning of one or two calls from that bit, it did run. Only problem was, that it was all it did. It open and displayed the window, but the rest of the file didn't run. I'm guessing it may have to do with something in the body of those few lines i had copied. Either that or something in the qt file its self, waiting for a connection or some such. Since i am fairly new to both python and Qt and hardly up to speed in either I will just have to prod along and try my limited best to work it out.. Curious how the os.system technique hadn't worked though? From what you said it should have done the trick. Oh well, thank you once again for your help. Have a good day. Ken PS Another odd bit, was on the python docs page. It had said that using import File_name, without the .py would import it, but not run it. Seems a glaring oversight not to have mentioned, what would have made it run. Sure would have made my life a lot easier anyway.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening a Qt .py file from within another python app
On 22/03/12 09:57, ken brockman wrote: PS Another odd bit, was on the python docs page. It had said that using import File_name, without the .py would import it, but not run it. Seems a glaring oversight not to have mentioned, what would have made it run. Actually it does run it when you import, just not the bit after the if __name__ = "__main__". The point of modules is that you don't usually want them to run as a program, you import them so as to get access to the functions within them. You want to control when they are called from your own code. So although Python imports do run the file, normally all that happens is that a few classes and functions get defined and, possibly, a few global variables get initialized. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Library of Module for Analyzing Answer Cards
Hi All, I work in in academic testing environment and we employ expensive machines to scan answer sheets (the ones where you blacken the letter of the correct multiple choice answer). Anyway, I was thinking if there was a way we could use regular old scanners to scan the sheets than analyze the images to score the tests. Do you know any modules or libraries in python that can be helpful in that? Also, what approach if any would you employ to tackle this project? thanks a million, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening a Qt .py file from within another python app
On 22/03/12 09:57, ken brockman wrote: >> PS Another odd bit, was on the python docs page. It had said that using >> import File_name, without the .py would import it, but not run it. Seems >> a glaring oversight not to have mentioned, what would have made it run. >Actually it does run it when you import, just not the bit after the >if __name__ = "__main__". >The point of modules is that you don't usually want them to run as a program, >you import them so as to get access to the >functions within them. You want to >control when they are called from your own code. >-- Alan G Thank you once more Alan, for taking the time to try to enlighten me. I had gone back to the python site, to try to get a better understanding of what it had said, with your insight in mind. But I think i am more befuddled now then i had been prior, If that is humanly possible. Below an excerpt from the python site, (2.7) but i think it is still applicable. 6.1.1. Executing modules as scripts When you run a Python module with python fibo.py the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module: if __name__ == "__main__": import sys fib(int(sys.argv[1])) you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file: $ python fibo.py 50 If the module is imported, the code is not run: Please bare with me, as I had said, I am a total novice. So if I understand this, which Is highly unlikely the, If __name__==__main__, is what makes the module run as a stand alone script? Which shouldn't effect it's usability as a module which is imported? So why was it, that I couldn't get it to run without including that bit in my main python program. And I am assuming the line I had mistook to mean the module wouldn't run, means instead that the "name ==main won't run, which once again leads to the question, then why would i need to include it to make it function?? Do you see my point. What am i missing here? Some vital piece of the puzzle seems to be eluding me. Ken___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening a Qt .py file from within another python app
Alan, no need to respond to that last missive of mine. I no sooner had hit the send key and reopened the apps in question then the answer hit me like a small mallet in the back of my head. Obviously, what makes the additional code at the bottom of the second app, run it, is that it must replicate the code that would be needed to call it from the main app. Jeez, my bad, as the kids say. Have a good one. Ken PS maybe time to find a new hobby. I hear that knitting is very relaxing and no heavy mental lifting. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening a Qt .py file from within another python app
> python fibo.py > the code in the module will be executed, just as if you imported it, but > with the __name__ set to "__main__". That means that by adding this code at > the end of your module: > > if __name__ == "__main__": > import sys > fib(int(sys.argv[1])) > you can make the file usable as a script as well as an importable module, > because the code that parses the command line only runs if the module is > executed as the “main” file: > > $ python fibo.py 50 > > If the module is imported, the code is not run: > > > Please bare with me, as I had said, I am a total novice. So if I understand > this, which Is highly unlikely the, If __name__==__main__, is what makes > the module run as a stand alone script? Which shouldn't effect > it's usability as a module which is imported? So why was it, that I > couldn't get it to run without including that bit in my main python > program. And I am assuming the line I had mistook to mean the module > wouldn't run, means instead that the "name ==main won't run, which > once again leads to the question, then why would i need to include it to > make it function?? > Do you see my point. What am i missing here? Some vital piece of the puzzle > seems to be eluding me. No everything in a module is run when imported. Usually modules are just a class and function definitions so nothing actually happens. Anything you put in a module that is not in a function or class will run. If you put a print statement in the module that is not in a function or class, it will print the statement every time you import the module. That is why the convention is to use the following code to only automatically run the module when running the script. if __name__ == '__main__': # this is only true if you are running the script directly # by doing something like python myscript.py run_something_here() Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] weird error in my python program : merge sort
I am imlpementing a merge sort algo for clarity purposes but my program is giving me weird answers. Sometimes it is able to sort and other times it does funky things. Help appreciated from random import * from numpy import * nums = [random.randint(100) for num in range(4)] #nums = [3,7,2,10] def merge_sort(nums, message='None'): #print "%s : num of elements in the list %d" % (message,len(nums)) print '[merge_sort] %s : %s' % ( message, nums) if len(nums) <= 1: return nums middle = len(nums)/2 print '[merge_sort] Mid point is %d' % middle left = nums[:middle] right = nums[middle:] merge_sort(left,'left') merge_sort(right,'right') print '[merge_sort] Calling merge on left: %s right : %s' % (left,right) result = merge(left,right) print '[merge_sort] %s' % result return result def merge(left,right): result = [] i,j = 0,0 print '[merge] left %s, right %s' % (left, right) while i < len(left) and j < len(right): print '[merge]Comparing left %d to right %d' % (left[i],right[j]) if left[i] <= right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 print '[merge]pushing to result',result result.extend(left[i:]) result.extend(right[j:]) print '[merge] return',result return result merge_sort(nums,'start') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] weird error in my python program : merge sort : resolved
I was not updating the list from the recursive call. > merge_sort(left,'left') > merge_sort(right,'right') left = merge_sort(left,'left') right = merge_sort(right,'right') -A -Abhi On Thu, Mar 22, 2012 at 1:40 PM, Abhishek Pratap wrote: > I am imlpementing a merge sort algo for clarity purposes but my > program is giving me weird answers. Sometimes it is able to sort and > other times it does funky things. Help appreciated > > > from random import * > from numpy import * > > nums = [random.randint(100) for num in range(4)] > #nums = [3,7,2,10] > > def merge_sort(nums, message='None'): > #print "%s : num of elements in the list %d" % (message,len(nums)) > print '[merge_sort] %s : %s' % ( message, nums) > > if len(nums) <= 1: > return nums > > middle = len(nums)/2 > print '[merge_sort] Mid point is %d' % middle > left = nums[:middle] > right = nums[middle:] > > merge_sort(left,'left') > merge_sort(right,'right') > print '[merge_sort] Calling merge on left: %s right : %s' % (left,right) > result = merge(left,right) > print '[merge_sort] %s' % result > return result > > > def merge(left,right): > result = [] > i,j = 0,0 > > print '[merge] left %s, right %s' % (left, right) > > while i < len(left) and j < len(right): > print '[merge]Comparing left %d to right %d' % (left[i],right[j]) > if left[i] <= right[j]: > result.append(left[i]) > i += 1 > else: > result.append(right[j]) > j += 1 > > print '[merge]pushing to result',result > > result.extend(left[i:]) > result.extend(right[j:]) > print '[merge] return',result > return result > > > merge_sort(nums,'start') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] python sorting
i want to sort the list formed by two variable taken as raw_input().i have written following code; a=raw_input() b=raw_input() c=a+b list=c.split() how i can sort the list formed___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening a Qt .py file from within another python app
On 22/03/12 19:59, ken brockman wrote: PS maybe time to find a new hobby. I hear that knitting is very relaxing and no heavy mental lifting. Nah, I tried that once. programming is much easier! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python sorting
On 03/22/2012 06:01 PM, Sukhpreet Sdhu wrote: i want to sort the list formed by two variable taken as raw_input().i have written following code; a=raw_input() b=raw_input() c=a+b list=c.split() how i can sort the list formed Do a search on python.org for list + sort. First match I got: http://wiki.python.org/moin/HowTo/Sorting -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] python arthematics
i want to write a program that reads simple arithematic epressions and calculates the result. for example input "1*3+2" should generate "5'" as result___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python arthematics
>i want to write a program that reads simple arithematic epressions and >calculates the result. >for example input "1*3+2" should generate "5'" as result So what is stopping you? Snark aside, this can get really complicated unless you make some well defined rules. This becomes difficult especially when you think about whether you want to support order of operations. Should “1+3*2” generate “7” or “8”? Do you want to support longer expressions or just 2 operations? Personally, if I were learning the language I would make it do 1 operation. Then after that I could consider how to make it do more operations. Turning it into a true arithmetic calculator might be a bit much to start with. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python arthematics
What have you tried? What so u intend to achieve? There are math functions in python already; so, are you planning to use the functions in the math class for your program or you want to write yours separately? The later would be a little disturbing in my candid opinion. Regards. Sent from my BlackBerry wireless device from MTN -Original Message- From: Sukhpreet Sdhu Sender: tutor-bounces+delegbede=dudupay@python.org Date: Fri, 23 Mar 2012 06:14:41 To: tutor@python.org Reply-To: Sukhpreet Sdhu Subject: [Tutor] python arthematics ___ 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] python arthematics
On 22/03/2012 22:14, Sukhpreet Sdhu wrote: i want to write a program that reads simple arithematic epressions and calculates the result. for example input "1*3+2" should generate "5'" as result ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Take a look at the operator module as it should give you some ideas. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python arthematics
On 22/03/12 22:14, Sukhpreet Sdhu wrote: i want to write a program that reads simple arithematic expressions and calculates the result. OK, there are many ways to do this but they pretty much fall into three categories: 1) read the string and exec() it - easy but very risky from a security point of view. 2) Write a general text parser to break the input string into operands and operators and combine the results. 3) build a state machine that looks for valid input (this will be easier if you opt for reverse polish notation BTW) You can do No 1 real quick just for fun, but don't try that in a real world program. So that leaves 2 and 3. If you understand the concepts (or research them on wikipedia) you can have at it, write some code and lets see what you get to. Hint: Start with a limited subset - only 2 operands and one operator allowed, say. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python arthematics
Please use ReplyAll when responding to posts, Thanks. i have tried like this way but i want to read all integers and operators in one string. how can i do that. > > >As described below. > > >expr = raw_input("Type an expression: ") > > >Now you have the expression as a string in expr. >You just need to analyze it to identify the various components (this is >called parsing). > > >One simplistic approach to get you started is to treat white-space and the >operators >as separators between operands. You can then scan the string to extract a list >of >operands and a list of operators. So " 3 + 4 * 6" yields: > > >operators = ["+", "*"] >operands = ["3", "4", "6"] > > >Since we are expecting the operands to be integers we can go further > > >operands = [int(n) for n in operands] > > >For the operators you need to do a lookup table and maybe map them >to the operators module operations... > > >You can then start matching them and performing the operations just like you >did > >in your code with the values read separately. eg. You can apply each operator >in >turn to the first pair of operands in the list: > > >3 + 4 -> 7 >7 * 6 -> 42 > >That doesn't take any account of the normal rules of precedence and >thats when things start to get much more complex. Also it doesn't allow >for parenthesised sub expressions etc. This is actually quite tricky to get >right, >but a very good learning exercise! (But much harder than you probably >thought it would be!) > > >HTH > > > > > From: Alan Gauld >To: tutor@python.org >Sent: Thursday, 22 March 2012 8:14 PM >Subject: Re: [Tutor] python arthematics > >On 22/03/12 22:14, Sukhpreet Sdhu wrote: >> i want to write a program that reads simple arithematic expressions and >> calculates the result. > >OK, there are many ways to do this but they pretty much >fall into three categories: > >1) read the string and exec() it - easy but very > risky from a security point of view. > >2) Write a general text parser to break the input > string into operands and operators and combine > the results. > >3) build a state machine that looks for valid input > (this will be easier if you opt for reverse polish > notation BTW) > >You can do No 1 real quick just for fun, but don't try >that in a real world program. > >So that leaves 2 and 3. If you understand the concepts >(or research them on wikipedia) you can have at it, >write some code and lets see what you get to. >Hint: Start with a limited subset - only 2 operands and >one operator allowed, say. > >HTH >-- Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ > >___ >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] Library of Module for Analyzing Answer Cards
On 3/22/2012 2:45 PM, Khalid Al-Ghamdi wrote: Hi All, I work in in academic testing environment and we employ expensive machines to scan answer sheets (the ones where you blacken the letter of the correct multiple choice answer). Anyway, I was thinking if there was a way we could use regular old scanners to scan the sheets than analyze the images to score the tests. This is not a Python solution - but www.cardiff-*teleform*.com/ offers a product called Teleform that does exactly what you want. I used it a while ago for a project where we scanned over 100,000 copies of 4 different forms. Worked like a charm. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Library of Module for Analyzing Answer Cards better link
On 3/22/2012 2:45 PM, Khalid Al-Ghamdi wrote: Hi All, I work in in academic testing environment and we employ expensive machines to scan answer sheets (the ones where you blacken the letter of the correct multiple choice answer). Anyway, I was thinking if there was a way we could use regular old scanners to scan the sheets than analyze the images to score the tests. This is not a Python solution - but http://www.cardiff.com/products/teleform/index.html offers a product called Teleform that does exactly what you want. I used it a while ago for a project where we scanned over 100,000 copies of 4 different forms. Worked like a charm. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor