Re: [Tutor] most useful ide
On 02/02/2014 09:10 PM, Pierre Dagenais wrote: On 14-02-02 01:16 PM, Kodiak Firesmith wrote: Pycharm is nice for bigger projects (since tou can collapse any section); but it's crazy resource intensive. For Linux Gedit can be made very nice I prefer Geany as it will run my code with a click of the mouse. but geany has terrible language-specific settings (and no interface for them) (and you need to find them first somewhere in your filesystem...) d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traversing lists or getting the element you want.
On 02/02/2014 09:46 PM, Kipton Moravec wrote: I am new to Python, and I do not know how to traverse lists like I traverse arrays in C. This is my first program other than "Hello World". I have a Raspberry Pi and they say Python is the language of choice for that little machine. So I am going to try to learn it. Traversing an array of 'n' float item in C, using index instead of pointer, may translate to: float item; uint i; // 0 <= i < n !!! for (i=0 ; iWorks, but this is not the python way. Python also has a builtin *idiom* to traverse a list (or any other collection or "traversable"): for item in items: # use item I have data in the form of x, y pairs where y = f(x) and is non linear. It comes from a .csv file. In this case x is an integer from 165 to 660 so I have 495 data sets. I need to find the optimal locations of three values of x to piecewise linear estimate the function. So I need to find i, j, k so 165 < i < j < k < 660 and the 4 line segments [(165, f(165)), (i, f(i))], [(i, f(i)), (j, f(j))], [(j, f(j), (k, f(k))], [(k, f(k)), (660, f(660))]. The value I need to minimize is the square of the difference between the line estimate and the real value at each of the 495 points. I can do this in C. To keep it simple to understand I will assume the arrays x[] and y[] and minsum, mini, minj, and mink are global. I have not tested this but this is what I came up with in C. Assuming x[] and y[] are already filled with the right values. int x[495]; double y[495]; max_elements = 495; double minsum; int mini, minj, mink void minimize(int max_elements) { minsum = 9.0; // big big number This is more or less a logical error (but one very very frequent). What if the array is empty? This gives: min(nothing_at_all)=9.0. min(nothing) is undefined, an anomaly or error. You should probably: * refuse an empty array (the caller should not call the func at all) * start with the first item as (temporary) min value (Same for all similar 'reduce' functions.) I understand you're not writing a simple min func here (instead minimising deviations to a hypothetical average) but the thinking pattern is the same. A sane min func may look like --i take the opportuity to show you some python code: def min_nums (nums): n = len(nums) assert n > 0, "are you kidding?" min = nums[0] for i in range(2, n): num = nums[i] if num < min: min = num return min nums = [6,4,5,8,3,1,2,7,8] print(min_nums(nums)) print(min_nums([])) for (i=2; i You could export in a sub-func the detail computation of the minimised element. Then, you could test it apart, and your minimise func would be barely more complicated than the one above (mine). double error(int istart, int iend) { // linear interpolation I can optimize but left // it not optimized for clarity of the function int m; double lin_estimate; double errorsq; errorsq = 0; for (m=istart; m See range() or better "for item in items". { lin_estimate = y[istart] + ((y[iend] – y[istart]) * ((x[m] – x[istart]) / (x[iend] – x[istart]))); errorsq += (lin_estimate – y[m]) * (lin_estimate – y[m]); } return (errorsq); } At the end the three values I want are mini, minj, mink; or x[mini], x[minj], x[mink] So how do I do this (or approach this) in Python? Kip Apart for the traversing idiom ('range' or 'for...in') the code is similar. (It also has '+=' and friends). (According to Guido van Rossum, Python was in fact designed to please C/Unix hackers; apart from the indented syntax and some idioms, it is indeed very similar, including a number of "unguessable" terms.) d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] most useful ide
Thanks for all the responses I was mainly looking for a more user friendly ide in a windows environment with things like command completion etc. Like something a novice might use. I suppose vi is simple and available for windows but I don't suppose there would be command completion even in vim. I don't really use emacs much so not that. I suppose idle will suffice, I just wondered if there was another ide simple like idle but a little more helpful like eclipse, but not eclipse. ha ha. Ok its a poor question ps. I know idle has command completion, but just wondered if there is something with a few more bells and whistles. From: dux...@hotmail.com To: tutor@python.org Date: Sun, 2 Feb 2014 08:25:16 + Subject: [Tutor] most useful ide Hi Are there any recommendations for python ide's currently I am using idle, which seems pretty decent but am open to any suggestions cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] most useful ide
On Mon, Feb 03, 2014 at 10:30:59AM +, Ian D wrote: > Thanks for all the responses > > I was mainly looking for a more user friendly ide in a windows environment > with things like command completion etc. Like something a novice might use. > I suppose vi is simple and available for windows but I don't suppose there > would be command completion even in vim. Vim supports all kinds of different completion: http://robots.thoughtbot.com/vim-you-complete-me I've also used jedi. I used it some time ago when it was new and a bit buggy. It started to annoy me after a while so I disabled it but it might be better now: http://jedi.jedidjah.ch/en/latest/ See also: http://sontek.net/blog/detail/turning-vim-into-a-modern-python-ide Vim has a steep learning curve but in my experience if you can get past the initial fight then it's definitely worth it. Another advantage of Vim for me is that it tends to be installed on all of the remote machines that I use so that I can transparently run it over ssh in the terminal. Similarly on Linux I can use it from the virtual terminals which is very handy when you've screwed your computer! Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] most useful ide
On 03/02/14 10:30, Ian D wrote: I was mainly looking for a more user friendly ide in a windows If you are on windows then look at Pythonwin it's similar to IDLE but much more windows friendly and has some extra tools for working with Windows (eg a COM object inspector) You get it in the winall package or if you download the Activestate Python distro its included (along with much else) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traversing lists or getting the element you want.
Kipton Moravec wrote: > I am new to Python, and I do not know how to traverse lists like I > traverse arrays in C. This is my first program other than "Hello World". > I have a Raspberry Pi and they say Python is the language of choice for > that little machine. So I am going to try to learn it. > > > I have data in the form of x, y pairs where y = f(x) and is non linear. > It comes from a .csv file. > > In this case x is an integer from 165 to 660 so I have 495 data sets. > > I need to find the optimal locations of three values of x to piecewise > linear estimate the function. import itertools def boundaries(n, segments): max_n = n-1 for inner in itertools.combinations(range(n), segments-2): yield (0,) + inner + (max_n,) def minimized(x, y, segments): def total_error(b): return sum(error(x, y, start, end) for start, end in zip(b, b[1:])) return min(boundaries(len(x), segments), key=total_error) def error(x, y, istart, iend): errorsq = 0 for m in range(istart, iend): lin_estimate = y[istart] + ((y[iend] - y[istart]) * ((x[m] - x[istart]) / (x[iend] - x[istart]))) d = lin_estimate - y[m] errorsq += d*d return errorsq if __name__ == "__main__": # generate dataset with N random (x, y) pairs import random N = 100 random.seed(42) # want reproducible result data = [ (random.random()*10, random.random()*100-50.) for _ in range(N)] print minimized([x for x, y in data], [y for x, y in data], 4) As this is mostly number crunching the Python version is probably a lot slower than the C code. I tried to move the inner loop into numpy with import numpy [...] def error(x, y, istart, iend): lin_estimate = y[istart] + ((y[iend] - y[istart]) * ((x[istart:iend] - x[istart]) / (x[iend] - x[istart]))) delta = lin_estimate - y[istart:iend] return (delta*delta).sum() [...] print minimized(numpy.array([x for x, y in data]), numpy.array([y for x, y in data]), 4) but that had no noticeable effect. There may be potential gains for someone willing to put in more work or with better knowledge of numpy. By the way, your method of calculating the error > double error(int istart, int iend) > { > > // linear interpolation I can optimize but left > // it not optimized for clarity of the function > > int m; > double lin_estimate; > double errorsq; > errorsq = 0; > > for (m=istart; m { > lin_estimate = y[istart] + ((y[iend] – y[istart]) * >((x[m] – x[istart]) / (x[iend] – x[istart]))); > > errorsq += (lin_estimate – y[m]) * (lin_estimate – y[m]); > } > return (errorsq); > } (which I adopted) looks odd. With a tried and tested method like "least square fit" which does not require the line (segments) to go through any point of the dataset you should get better fits. PS: Caveat emptor! I did not even plot a graph with the result to check plausibility; there may be embarassing errors. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] most useful ide
On Sun, Feb 2, 2014 at 3:25 AM, Ian D wrote: > Are there any recommendations for python ide's > Since you mentionned windows and lightweight and autocompletion, i'd take a look at ninja-ide (http://ninja-ide.org/). It is relatively lightweight. In my case, I use pycharm for larger projects (vcs integration etc), scribes for a graphical editor for one off scripts, vim and bpython (very nice for exploring - autocompletion and autodoc) from the command line, brython console in the web browser and ipython notebook for matlab type "coding" (exploration). Another suggestion would be sublimetext. It is a text editor, but it can handle multiple tabs and it has a folder browser. You can also run the code (ctrl-b) etc. I've used it, but not in the past 6 months. And you might also want to look at pythonxy, canopy and anaconda for a simpler install experience under windows. Francois --- www.pyptug.org - raspberry-python.blogspot.com - @f_dion ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] My First Post
Hi I am Premanshu. I have recently started learning python programming language and have question. Most of them I have googled out however I still have some which i could not find a satisfactory answer. While searching for a forum i cam across this forum so posting my question. Please guide me to the right place if this is not where i must look for my answer. My question is as below:- while going through a wx python Tutoria to create GUI's I have come across a method "Bind". Basically i created a frame then a file menu in it with a exit button as the content. Further the tutorial explain how to Bind the close method to the Exit button that I created in which it uses a argument EVT_MENU. I would actually want to understand the whole of the Bind method...as in what all arguments it takes and what argument is what in it Thank you very much in advance. Regards, Premanshu ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] My First Post
On 03/02/14 15:54, premanshu basak wrote: I am Premanshu. I have recently started learning python programming language and have question. welcome. We are always happy to try to answer questions although our focus is on the python language and its standard library. If we know about a thitrd party library we may well have a go at that too... while going through a wx python Tutoria to create GUI's I have come across a method "Bind". ... I would actually want to understand the whole of the Bind method... The best place to ask for wxPython help is on the wxPython mailing list. However, some folks here do use wxPython so you may well get an answer. But if you need more depth try the wxPython list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
an example of errors that I obtain is: I build a matrix (SciPy array) using the same data. I search for the maximum and the minimum with the given function and I insert the two values in a tuple that I print. sometimes it print to me this: (0.0, 3.1926676650124463e-14) sometimes it print to me: (nan, nan) Thanks Gabriele 2014-02-02 Alan Gauld : > On 02/02/14 02:11, Gabriele Brambilla wrote: > > sometimes when I try to run a program in Python I obtain some errors. >> > > How are you running the program? > > doubly clicking in a file manager/explorer? > Running from an OS command line? > Importing from the Python >>> prompt? > Or using some kind of IDE? (Which?) > > The answer to your question depends a lot on the way you > run the code. > > A sample error and your OS and Python version will help too. > > > How could I avoid this problem? I think that it is because it "remember" >> some variables that I have used... >> > > Could be. If you import a module then change that module then > reimporting alone is not enough. But if you run the code from the OS that > shouldn't be an issue. So how do you run the code? > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
>an example of errors that I obtain is: I build a matrix (SciPy array) using >the same data. >I search for the maximum and the minimum with the given function and I insert >the two values in a tuple that I print. >sometimes it print to me this: (0.0, 3.1926676650124463e-14) >sometimes it print to me: (nan, nan) > >That looks like in may be a scipy issue rather than core Python. We do have some SciPy users who may be able to help, but it would be easier if they could see the function and calling code you are using. Also you may get better help on the scipy mailing list/web fora. Alan G.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
Gabriele Brambilla wrote: > an example of errors that I obtain is: I build a matrix (SciPy array) > using the same data. > I search for the maximum and the minimum with the given function and I > insert the two values in a tuple that I print. > sometimes it print to me this: (0.0, 3.1926676650124463e-14) > sometimes it print to me: (nan, nan) Are you messing around with byteorder or other lowlevel stuff? Can you provide a small script that shows this behaviour? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
No, i'm not using lowlevel stuff...which part of the script do you want to see? thanks Gabriele 2014-02-03 Peter Otten <__pete...@web.de>: > Gabriele Brambilla wrote: > > > an example of errors that I obtain is: I build a matrix (SciPy array) > > using the same data. > > I search for the maximum and the minimum with the given function and I > > insert the two values in a tuple that I print. > > sometimes it print to me this: (0.0, 3.1926676650124463e-14) > > sometimes it print to me: (nan, nan) > > Are you messing around with byteorder or other lowlevel stuff? > Can you provide a small script that shows this behaviour? > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
Gabriele Brambilla wrote: > No, i'm not using lowlevel stuff...which part of the script do you want > to see? The part that remains when you throw out everything that has no effect on the "strange errors" ;) For example if you have a routine def load_data(filename): ... # do complex processing return some_array that reads a custom file format, and your script still fails when you replace that by def load_data(filename): return numpy.array([42.0]) then post the simplified version. In that spirit apply every simplification you can think of, then make sure that your script still fails, then post. Alternatively, post the whole beast and hope that someone will care enough and find the needle in the haystack. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] strange errors
On Mon, Feb 3, 2014 at 2:17 PM, Gabriele Brambilla wrote: > No, i'm not using lowlevel stuff...which part of the script do you want to > see? Since you asked, what we'd really like to see is a Short, Self Contained, Compilable Example (see http://sscce.org/). That is, an example that is short enough to be read quickly, self contained enough so that we can actually save the code and run it, compilable in that the code will run, at least to the point where you're having a problem (it's fine to have code that won't run, as long as the error that pops up is the error you're actually looking for help with). We know that it can be a lot of work to create an example like that, but if you do there are two things that are likely to happen. First, probably 75% of the time, you will find your problem yourself without ever having to post it to the list. The very act of trimming your code into the piece that demonstrates your problem, will help you find the problem yourself. Second, you are very, very likely to get people to pay attention to your question and provide advice on how to fix your code when you make it easy for them to help. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] My First Post
Just as you might look at a dictionary to find the meaning of a word, you'll look at library documentation to look for the meaning of a method. Most likely, you may need to refer to the documentation of the library in question. I do not know if this is the method you are looking at, but here is something about a "bind" related to wxPython: http://www.wxpython.org/docs/api/wx.EvtHandler-class.html#Bind http://wxpython.org/Phoenix/docs/html/events_overview.html The first link is in the style of a reference manual: very terse, but with technical detail. The second link appears to be user-level documentation, with examples and a description of the event model. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] My First Post
(Sorry if you get this message twice: I think I accidently mis-sent the message to the wrong address, so to make sure, I'm sending to what I think is the right address this time.) --- Hi Premanshu, Just as you might look at a dictionary to find the meaning of a word, you'll look at library documentation to look for the meaning of a method. Most likely, you may need to refer to the documentation of the library in question. I do not know if this is the method you are looking at, but here is something about a "bind" related to wxPython: http://www.wxpython.org/docs/api/wx.EvtHandler-class.html#Bind http://wxpython.org/Phoenix/docs/html/events_overview.html The first link is in the style of a reference manual: very terse, but with technical detail. The second link appears to be user-level documentation, with examples and a description of the event model. Good luck to you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traversing lists or getting the element you want.
Thanks to all that helped on this one. I missed in my reading that you can address an element of a list as y[i]. Going back through "Think Python" book, they have one example of it. And spend 1/4 page out of the 263 pages in the book. They spend much more time on other parts of lists like slices, and methods, etc. For the record, this particular problem had to be linear, and I have to have the endpoint of one segment be the same point as the beginning of the next segment for the particular integer x[]. So I could not do a normal linear regression and least square fit for the line segment. My book also does not have anything about itertools, so I have never heard of that. So I will google it to learn more. Thanks, again, Kip On Mon, 2014-02-03 at 14:35 +0100, Peter Otten wrote: > Kipton Moravec wrote: > > > I am new to Python, and I do not know how to traverse lists like I > > traverse arrays in C. This is my first program other than "Hello World". > > I have a Raspberry Pi and they say Python is the language of choice for > > that little machine. So I am going to try to learn it. > > > > > > I have data in the form of x, y pairs where y = f(x) and is non linear. > > It comes from a .csv file. > > > > In this case x is an integer from 165 to 660 so I have 495 data sets. > > > > I need to find the optimal locations of three values of x to piecewise > > linear estimate the function. > > import itertools > > def boundaries(n, segments): > max_n = n-1 > for inner in itertools.combinations(range(n), segments-2): > yield (0,) + inner + (max_n,) > > def minimized(x, y, segments): > def total_error(b): > return sum(error(x, y, start, end) for start, end in zip(b, b[1:])) > return min(boundaries(len(x), segments), key=total_error) > > def error(x, y, istart, iend): > errorsq = 0 > for m in range(istart, iend): > lin_estimate = y[istart] + ((y[iend] - y[istart]) * >((x[m] - x[istart]) / (x[iend] - x[istart]))) > d = lin_estimate - y[m] > errorsq += d*d > return errorsq > > if __name__ == "__main__": > # generate dataset with N random (x, y) pairs > import random > N = 100 > random.seed(42) # want reproducible result > data = [ > (random.random()*10, random.random()*100-50.) > for _ in range(N)] > > > print minimized([x for x, y in data], > [y for x, y in data], 4) > > > As this is mostly number crunching the Python version is probably a lot > slower than the C code. I tried to move the inner loop into numpy with > import numpy > > [...] > > def error(x, y, istart, iend): > lin_estimate = y[istart] + ((y[iend] - y[istart]) * >((x[istart:iend] - x[istart]) / (x[iend] - x[istart]))) > delta = lin_estimate - y[istart:iend] > return (delta*delta).sum() > > [...] > > print minimized(numpy.array([x for x, y in data]), > numpy.array([y for x, y in data]), 4) > > but that had no noticeable effect. There may be potential gains for someone > willing to put in more work or with better knowledge of numpy. > > > By the way, your method of calculating the error > > > double error(int istart, int iend) > > { > > > > // linear interpolation I can optimize but left > > // it not optimized for clarity of the function > > > > int m; > > double lin_estimate; > > double errorsq; > > errorsq = 0; > > > > for (m=istart; m > { > > lin_estimate = y[istart] + ((y[iend] – y[istart]) * > >((x[m] – x[istart]) / (x[iend] – x[istart]))); > > > > errorsq += (lin_estimate – y[m]) * (lin_estimate – y[m]); > > } > > return (errorsq); > > } > > (which I adopted) looks odd. With a tried and tested method like "least > square fit" which does not require the line (segments) to go through any > point of the dataset you should get better fits. > > PS: Caveat emptor! I did not even plot a graph with the result to check > plausibility; there may be embarassing errors. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor