From crk at godblessthe.us Wed Oct 1 00:54:42 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Tue, 30 Sep 2014 15:54:42 -0700 Subject: [Tutor] could somebody please explain... Message-ID: <01eb01cfdd01$86034220$9209c660$@us> I don't understand the multiplicity of some tools. Namely, why is there a 'a+b', operator.add(a,b), operator.__add__(a,b), operator.iadd(a,b), operator.__iadd__(a,b) and their related operators? Also, I found something that I can't get my mind around. It is part of the time/date protocols. I've not seen it anywhere else. Datetime(year=blah, blah, blah).date/time() datetime(2013,3,6).date() #returns. datetime.date(2013,3,6) datetime(2013,3,6).time() #returns. datetime.time(0,0) This is one of the weirder things I've run across. Is this allowed/needed in other functions/classes, or is it a datetime thing only? Please spare my mind:<)) Clayton -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 1 02:58:16 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Oct 2014 01:58:16 +0100 Subject: [Tutor] could somebody please explain... In-Reply-To: <01eb01cfdd01$86034220$9209c660$@us> References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: On 30/09/14 23:54, Clayton Kirkwood wrote: > I don't understand the multiplicity of some tools. Namely, why is there a > 'a+b', operator.add(a,b), operator.__add__(a,b), operator.iadd(a,b), > operator.__iadd__(a,b) and their related operators? The operator module is there largely to allow you to pass operations to functions that take functions as parameters. For example map() which applies a function to a collection. total = map(operator.add, [1,2,3,4,5,6]) is the same result as total = sum([1,2,3,4,5,6]) You could do the same thing with a lambda: total = map(lambda a,b: a+b, [1,2,3,4,5,6]) But using operator.add makes it easier to read (and less error prone). The operator module has functions representing most of the builtin operations in Python. I'm not really sure why it implements the dunder methods (eg operatotor.__add__()) corresponding to the operations though. I'm sure somebody else can provide a good explanation... > Also, I found something that I can't get my mind around. It is part of the > time/date protocols. I've not seen it anywhere else. > > Datetime(year=blah, blah, blah).date/time() A Datetime object is an instance of a combined date and time. the date and time methods return just the date or time parts of that combined object. > This is one of the weirder things I've run across. Is this allowed/needed in > other functions/classes, or is it a datetime thing only? The nearest I can think of elsewhere is the complex number case where you can get the real/imaginary values from a complex number >>> n = (1+6j) >>> n.imag 6.0 >>> n.real 1.0 But similar concepts apply in most classes where attributes, methods or properties are used to return significant aspects of the object. An Address might have methods to return the city or street or house number for example. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Wed Oct 1 03:38:14 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 1 Oct 2014 11:38:14 +1000 Subject: [Tutor] could somebody please explain... In-Reply-To: <01eb01cfdd01$86034220$9209c660$@us> References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: <20141001013814.GW19757@ando.pearwood.info> On Tue, Sep 30, 2014 at 03:54:42PM -0700, Clayton Kirkwood wrote: > I don't understand the multiplicity of some tools. Namely, why is there a > 'a+b', operator.add(a,b), operator.__add__(a,b), operator.iadd(a,b), > operator.__iadd__(a,b) and their related operators? The + operator is the public interface, but the implementation that makes + work are the special methods __add__ and __radd__ . When you write in your code: result = a + b how does Python know what to do with a and b? In principle, Python could hard-code into the language a handful of types that the interpreter knows how to add: int, float, str, list, etc. But that is not easily extended when a new type is supported, and Python supports "operator overloading" where any custom class can define "a + b" to do whatever the class developer wants. So when the Python interpreter executes a + b, when it does is look for a special "dunder" (Double UNDERscore) method on a, __add__, or a special dunder method __radd__ ("right add") on b, and calls that. Actually the rules are a bit more complicated than that, which I'm happy to explain if you would like, but for simplicity let's ignore __radd__ and just say that when Python sees "a + b" what actually gets called is a.__add__(b). So when you create a new class and want it to support the + operator, you write a __add__ method: class Spam: def __add__(self, other): ... and now Python knows how to add your Spam instances together. Sometimes it is useful to treat the + operator as a function, e.g. so that you can pass it to another function like reduce. But operators aren't values, you can't pass them to functions. This doesn't work: py> reduce(+, [1, 2, 3, 4]) File "", line 1 reduce(+, [1, 2, 3, 4]) ^ SyntaxError: invalid syntax But you can wrap the operator in a function using lambda: py> reduce(lambda a, b: a+b, [1, 2, 3, 4]) 10 but a more efficient way is to use the pre-made functions in the operator module: py> import operator py> reduce(operator.add, [1, 2, 3, 4]) 10 So for every operator + - * / ** etc. there is a corresponding function version in the operator module, add(), sub() etc. [ Aside: you might not know about reduce(). It takes a function f, and a list [a, b, c, d, ...] and calls the function with the first two values: result = f(a, b) then takes that result and repeatedly calls the function again with the next value from the list: result = f(result, c) result = f(result, d) ... until there are no more values left, then returns the final result. These days, now that Python has a sum() function, reduce() doesn't get used very often. ] So for each operator that Python knows about, there is the operator itself, a function version, and one or two special dunder methods: Operator Function Dunder methods ========== ============== ===================== + operator.add __add__ __radd__ - operator.sub __sub__ __rsub__ * operator.mul __mul__ __rmul__ ** operator.pow __pow__ __rpow__ == operator.eq __eq__ != operator.ne __ne__ etc. Then there are the special "augmented assignment" operators, so that Python can support writing: x += 1 y -= x etc. Again, the syntax used is a combined operator-assignment += and that ends up calling a special dunder method, __iadd__. And again, there are special function versions in the operator module. In summary: (1) When you want to add two values, use a + b. (2) When you want a function that adds two values, use operator.add. (3) When you want to write a class that supports addition, give it the two special dunder methods __add__ and __radd__. (4) You almost never should call __add__ yourself. -- Steven From steve at pearwood.info Wed Oct 1 04:16:56 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 1 Oct 2014 12:16:56 +1000 Subject: [Tutor] could somebody please explain... In-Reply-To: References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: <20141001021656.GX19757@ando.pearwood.info> On Wed, Oct 01, 2014 at 01:58:16AM +0100, Alan Gauld wrote: > On 30/09/14 23:54, Clayton Kirkwood wrote: > >I don't understand the multiplicity of some tools. Namely, why is there a > >'a+b', operator.add(a,b), operator.__add__(a,b), operator.iadd(a,b), > >operator.__iadd__(a,b) and their related operators? > > The operator module is there largely to allow you to pass > operations to functions that take functions as parameters. > For example map() which applies a function to a collection. > > total = map(operator.add, [1,2,3,4,5,6]) > > is the same result as > > total = sum([1,2,3,4,5,6]) No, you're thinking of reduce(), not map(). reduce() takes a function and applies it all of the items, pairwise, gradually reducing the result down to a single value. map() takes a function and applies it to each of the items individually, returning a new list. [...] > The operator module has functions representing most > of the builtin operations in Python. > > I'm not really sure why it implements the dunder methods > (eg operatotor.__add__()) corresponding to the operations though. > I'm sure somebody else can provide a good explanation... They're just aliases. According to the documentation, the "official" versions are operator.__add__, etc. with the underscore-less versions just given for convenience. But in practice everybody uses the non-underscore versions. My advice is to ignore the operator.__add__ and similar double-underscore versions. They're longer to type, don't add any additional value, and in fact are actually misleading since they don't directly call the dunder methods. -- Steven From steve at pearwood.info Wed Oct 1 04:33:57 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 1 Oct 2014 12:33:57 +1000 Subject: [Tutor] could somebody please explain... In-Reply-To: <01eb01cfdd01$86034220$9209c660$@us> References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: <20141001023357.GY19757@ando.pearwood.info> On Tue, Sep 30, 2014 at 03:54:42PM -0700, Clayton Kirkwood wrote: > Also, I found something that I can't get my mind around. It is part of the > time/date protocols. I've not seen it anywhere else. > > Datetime(year=blah, blah, blah).date/time() > > datetime(2013,3,6).date() #returns. > datetime.date(2013,3,6) > > datetime(2013,3,6).time() #returns. > datetime.time(0,0) > > This is one of the weirder things I've run across. Is this allowed/needed in > other functions/classes, or is it a datetime thing only? I'm afraid I have no clue what part of this you consider weird. Is it that the date() and time() methods don't take an argument? That's quite common: py> "Hello".upper() 'Hello' Or is it that the result of calling date() or time() methods isn't the same type of thing as what you started with? Again, that's very common: py> {1: 'a', 2: 'b'}.keys() # Start with a dict, returns a list. [1, 2] Start with a datetime object. The date() method returns the date part alone, so it returns a date object. The time() method returns the time part alone, so it returns a time object. Or maybe you're weirded out by the leading "datetime" in the name. That's unfortunate, but not weird. The datetime module contains at least three classes. When you print the class, they show the module name. It is unfortunate that the module name happens to have the same name as one of those classes: py> datetime py> datetime.date py> datetime.time py> datetime.datetime So when you see something like this: py> d = datetime.datetime(2000, 5, 22, 11, 5, 27) py> d datetime.datetime(2000, 5, 22, 11, 5, 27) the "datetime." means the module, and the "datetime(...)" means the class with its various arguments. Is this common? Sadly, there are quite a few modules where the main function or class in the module has the same, or very similar, name: dis.dis bisect.bisect decimal.Decimal fractions.Fraction etc. (P.S. it is better to raise each independent question in a separate email.) -- Steven From dyoo at hashcollision.org Wed Oct 1 09:11:07 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 1 Oct 2014 00:11:07 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: <01eb01cfdd01$86034220$9209c660$@us> References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: > Also, I found something that I can?t get my mind around. It is part of the > time/date protocols. I?ve not seen it anywhere else. > > Datetime(year=blah, blah, blah).date/time() > > datetime(2013,3,6).date() #returns? > datetime.date(2013,3,6) > > datetime(2013,3,6).time() #returns? > datetime.time(0,0) > > This is one of the weirder things I?ve run across. Is this allowed/needed in > other functions/classes, or is it a datetime thing only? Can you say more about what you expect? It may help to be very explicit, even if it seems silly. The problem with talking with experienced tutors and programmers is that our perspective has warped slightly from extended exposure. :P So we may need a bit of hinting to tell what you're referring to by weirdness. The datetime library, if I recall correctly, combines two things: the date part, and the time part, each which are otherwise treated separately. It's a composite object. https://docs.python.org/2/library/datetime.html#datetime-objects When we construct a datetime.datetime, at the very least we need to provide its year, month, and day, but the other "time" components of it are optional. That's what the documentation is trying to say when it wraps the arguments in braces here: https://docs.python.org/2/library/datetime.html#datetime.datetime If you don't provide the time-related arguments, I think it assumes that those components are zeroed out. From alan.gauld at btinternet.com Wed Oct 1 12:05:27 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Oct 2014 11:05:27 +0100 Subject: [Tutor] could somebody please explain... In-Reply-To: <20141001021656.GX19757@ando.pearwood.info> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001021656.GX19757@ando.pearwood.info> Message-ID: On 01/10/14 03:16, Steven D'Aprano wrote: >> For example map() which applies a function to a collection. >> >> total = map(operator.add, [1,2,3,4,5,6]) >> >> is the same result as >> >> total = sum([1,2,3,4,5,6]) > > No, you're thinking of reduce(), not map(). Oops, you're quite right. Apologies. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From Ben.Smith at arnoldkeqms.com Wed Oct 1 17:59:36 2014 From: Ben.Smith at arnoldkeqms.com (Ben Smith) Date: Wed, 1 Oct 2014 15:59:36 +0000 Subject: [Tutor] Whack-a-mole Message-ID: <89409F4193210C4EB6B405E88013078230CA429D@QUIMBY.CSCo.Org.UK> Hi, Can anyone help explain why you can keep hitting the Mole even when hittable should be False? from tkinter import * root = Tk() #root.state('zoomed') sec = 0 points=0 pic=PhotoImage(file='Dirt.gif') pic2=PhotoImage(file='Mole.gif') hittable=False def HIT(): if hittable==True: global points;points+=1 butty.configure(image=pic) labby.configure(text=points) def tick(): global sec, hittable sec += 1 if sec == 3: hittable=True butty.configure(image=pic2) if sec==6: hittable=False butty.configure(image=pic) time['text'] = sec time.after(1000, tick) time = Label(root) time.pack() labby = Label(root, text="POINTS");labby.pack() Button(root, text='Start', command=tick).pack() butty=Button(root, image=pic, command=HIT);butty.pack() root.mainloop() This email and any attachments sent with it are intended only for the named recipient. If you are not that person please contact us immediately through our website and delete this message from your computer. You should not disclose the content nor take, retain or distribute any copies. No responsibility is accepted by AKS, United Learning or any associated entity for the contents of e-mails unconnected with their business. No responsibility is accepted for any loss or damage caused due to any virus attached to this email. AKS is part of United Learning, comprising: UCST (Registered in England No: 2780748. Charity No. 1016538) and ULT (Registered in England No. 4439859. An Exempt Charity). Companies limited by guarantee. -------------- next part -------------- An HTML attachment was scrubbed... URL: From crk at godblessthe.us Wed Oct 1 18:07:02 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Wed, 1 Oct 2014 09:07:02 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: References: <01eb01cfdd01$86034220$9209c660$@us> Message-ID: <001b01cfdd91$bd1b8ed0$3752ac70$@us> !-----Original Message----- !From: Danny Yoo [mailto:dyoo at hashcollision.org] !Sent: Wednesday, October 01, 2014 12:11 AM !To: Clayton Kirkwood !Cc: Python Tutor Mailing List !Subject: Re: [Tutor] could somebody please explain... ! !> Also, I found something that I can?t get my mind around. It is part of !> the time/date protocols. I?ve not seen it anywhere else. !> !> Datetime(year=blah, blah, blah).date/time() !> !> datetime(2013,3,6).date() #returns? !> datetime.date(2013,3,6) !> !> datetime(2013,3,6).time() #returns? !> datetime.time(0,0) !> !> This is one of the weirder things I?ve run across. Is this !> allowed/needed in other functions/classes, or is it a datetime thing !only? ! ! !Can you say more about what you expect? It may help to be very !explicit, even if it seems silly. The problem with talking with !experienced tutors and programmers is that our perspective has warped !slightly from extended exposure. :P So we may need a bit of hinting to !tell what you're referring to by weirdness. ! Sure, the interest is regarding the '2013,3,6' in the first datetime. I've not seen something in the first set of parenthesis before. Is the first one a class or a function, how can you tell without looking at its internals or some documentation? ! !The datetime library, if I recall correctly, combines two things: the !date part, and the time part, each which are otherwise treated !separately. It's a composite object. ! ! https://docs.python.org/2/library/datetime.html#datetime-objects ! !When we construct a datetime.datetime, at the very least we need to !provide its year, month, and day, but the other "time" components of it !are optional. That's what the documentation is trying to say when it !wraps the arguments in braces here: ! ! https://docs.python.org/2/library/datetime.html#datetime.datetime ! !If you don't provide the time-related arguments, I think it assumes that !those components are zeroed out. Yes, but apparently you can also specify the specific handle in any order like a dict. Somewhat. Clayton Kirkwood From crk at godblessthe.us Wed Oct 1 18:10:13 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Wed, 1 Oct 2014 09:10:13 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: <20141001023357.GY19757@ando.pearwood.info> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001023357.GY19757@ando.pearwood.info> Message-ID: <001c01cfdd92$30badcb0$92309610$@us> !-----Original Message----- !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On !Behalf Of Steven D'Aprano !Sent: Tuesday, September 30, 2014 7:34 PM !To: tutor at python.org !Subject: Re: [Tutor] could somebody please explain... ! !On Tue, Sep 30, 2014 at 03:54:42PM -0700, Clayton Kirkwood wrote: ! !> Also, I found something that I can't get my mind around. It is part of !> the time/date protocols. I've not seen it anywhere else. !> !> Datetime(year=blah, blah, blah).date/time() !> !> datetime(2013,3,6).date() #returns. !> datetime.date(2013,3,6) !> !> datetime(2013,3,6).time() #returns. !> datetime.time(0,0) !> !> This is one of the weirder things I've run across. Is this !> allowed/needed in other functions/classes, or is it a datetime thing !only? ! !I'm afraid I have no clue what part of this you consider weird. Is it !that the date() and time() methods don't take an argument? That's quite !common: ! !py> "Hello".upper() !'Hello' ! ! !Or is it that the result of calling date() or time() methods isn't the !same type of thing as what you started with? Again, that's very common: ! !py> {1: 'a', 2: 'b'}.keys() # Start with a dict, returns a list. ![1, 2] ! ! !Start with a datetime object. The date() method returns the date part !alone, so it returns a date object. The time() method returns the time !part alone, so it returns a time object. ! !Or maybe you're weirded out by the leading "datetime" in the name. !That's unfortunate, but not weird. The datetime module contains at least !three classes. When you print the class, they show the module name. It !is unfortunate that the module name happens to have the same name as one !of those classes: ! !py> datetime ! !py> datetime.date ! !py> datetime.time ! !py> datetime.datetime ! ! ! !So when you see something like this: ! !py> d = datetime.datetime(2000, 5, 22, 11, 5, 27) d !datetime.datetime(2000, 5, 22, 11, 5, 27) ! !the "datetime." means the module, and the "datetime(...)" means the !class with its various arguments. ! !Is this common? Sadly, there are quite a few modules where the main !function or class in the module has the same, or very similar, name: ! !dis.dis !bisect.bisect !decimal.Decimal !fractions.Fraction ! !etc. ! ! !(P.S. it is better to raise each independent question in a separate !email.) ! The part in question is the date components in the parentheses of the first datetime. Clayton Kirkwood ! ! !-- !Steven !_______________________________________________ !Tutor maillist - Tutor at python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor From crk at godblessthe.us Wed Oct 1 18:43:29 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Wed, 1 Oct 2014 09:43:29 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: <20141001013814.GW19757@ando.pearwood.info> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001013814.GW19757@ando.pearwood.info> Message-ID: <003201cfdd96$d4a17e20$7de47a60$@us> !-----Original Message----- !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On !Behalf Of Steven D'Aprano !Sent: Tuesday, September 30, 2014 6:38 PM !To: tutor at python.org !Subject: Re: [Tutor] could somebody please explain... ! !On Tue, Sep 30, 2014 at 03:54:42PM -0700, Clayton Kirkwood wrote: ! !> I don't understand the multiplicity of some tools. Namely, why is !> there a 'a+b', operator.add(a,b), operator.__add__(a,b), !> operator.iadd(a,b), !> operator.__iadd__(a,b) and their related operators? ! !The + operator is the public interface, but the implementation that !makes + work are the special methods __add__ and __radd__ . ! !When you write in your code: ! ! result = a + b ! !how does Python know what to do with a and b? In principle, Python could !hard-code into the language a handful of types that the interpreter !knows how to add: int, float, str, list, etc. But that is not easily !extended when a new type is supported, and Python supports "operator !overloading" where any custom class can define "a + b" to do whatever !the class developer wants. ! !So when the Python interpreter executes a + b, when it does is look for !a special "dunder" (Double UNDERscore) method on a, __add__, or a !special dunder method __radd__ ("right add") on b, and calls that. ! !Actually the rules are a bit more complicated than that, which I'm happy !to explain if you would like, but for simplicity let's ignore __radd__ !and just say that when Python sees "a + b" what actually gets called is !a.__add__(b). ! !So when you create a new class and want it to support the + operator, !you write a __add__ method: ! !class Spam: ! def __add__(self, other): ! ... ! ! !and now Python knows how to add your Spam instances together. ! !Sometimes it is useful to treat the + operator as a function, e.g. so !that you can pass it to another function like reduce. But operators !aren't values, you can't pass them to functions. This doesn't work: ! !py> reduce(+, [1, 2, 3, 4]) ! File "", line 1 ! reduce(+, [1, 2, 3, 4]) ! ^ !SyntaxError: invalid syntax ! ! !But you can wrap the operator in a function using lambda: ! !py> reduce(lambda a, b: a+b, [1, 2, 3, 4]) !10 ! ! !but a more efficient way is to use the pre-made functions in the !operator module: ! !py> import operator !py> reduce(operator.add, [1, 2, 3, 4]) !10 ! ! !So for every operator + - * / ** etc. there is a corresponding function !version in the operator module, add(), sub() etc. ! ! ![ Aside: you might not know about reduce(). It takes a function f, and a !list [a, b, c, d, ...] and calls the function with the first two values: ! ! result = f(a, b) ! !then takes that result and repeatedly calls the function again with the !next value from the list: ! ! result = f(result, c) ! result = f(result, d) ! ... ! !until there are no more values left, then returns the final result. !These days, now that Python has a sum() function, reduce() doesn't get !used very often. ] ! !So for each operator that Python knows about, there is the operator !itself, a function version, and one or two special dunder methods: ! ! Operator Function Dunder methods ! ========== ============== ===================== ! + operator.add __add__ __radd__ ! - operator.sub __sub__ __rsub__ ! * operator.mul __mul__ __rmul__ ! ** operator.pow __pow__ __rpow__ ! == operator.eq __eq__ ! != operator.ne __ne__ ! !etc. ! !Then there are the special "augmented assignment" operators, so that !Python can support writing: ! ! x += 1 ! y -= x ! !etc. Again, the syntax used is a combined operator-assignment += and !that ends up calling a special dunder method, __iadd__. And again, there !are special function versions in the operator module. ! ! !In summary: ! !(1) When you want to add two values, use a + b. ! !(2) When you want a function that adds two values, use operator.add. ! !(3) When you want to write a class that supports addition, give it ! the two special dunder methods __add__ and __radd__. ! !(4) You almost never should call __add__ yourself. ! In an effort to learn and teach, I present a simple program which measures the time it takes to the various add functions with the appending results: # program to test time and count options import datetime,operator, sys from datetime import time, date, datetime date = datetime.now() dayofweek = date.strftime("%a, %b") print("Today is", dayofweek, date.day, "at ", date.time()) start = 0 count_max=int(input("give me a number")) start_time = datetime.now() print( start_time ) while start < count_max: start=start + 1 end_time = datetime.now() print( "s=s+1 time difference is:", (end_time - start_time) ) start=0 start_time = datetime.now() while( start < count_max ): start += 1 end_time = datetime.now() print( "the += time difference is:", (end_time - start_time) ) start_time = datetime.now() start = 0 while( start < count_max ): start = operator.add( start, 1) end_time = datetime.now() print( "the operator.add time difference is:", (end_time - start_time) ) start_time = datetime.now() start=0 while( start < count_max ): start = operator.iadd( start, 1) end_time = datetime.now() print( "the operator.iadd time difference is:", (end_time - start_time) ) start_time = datetime.now() start=0 while( start < count_max ): start = operator.__add__(start,1) end_time = datetime.now() print( "the operator.__add__ time difference is:", (end_time - start_time) ) start_time = datetime.now() start=0 while( start < count_max ): start = operator.__iadd__(start,1) end_time = datetime.now() print( "the operator.__iadd__ time difference is:", (end_time - start_time) ) As can be seen below, there is a definite pattern: s=s+1 and s+=1, are faster. There is some variability within the two schemes, but typically, the iadds are slowest. Today is Wed, Oct 1 at 09:19:05.671235 give me a number22222222 2014-10-01 09:19:18.485235 s=s+1 time difference is: 0:00:09.438000 the += time difference is: 0:00:09.072000 the operator.add time difference is: 0:00:17.172000 the operator.iadd time difference is: 0:00:17.325000 the operator.__add__ time difference is: 0:00:17.248000 the operator.__iadd__ time difference is: 0:00:17.673000 Today is Wed, Oct 1 at 09:35:06.525235 give me a number10000000 2014-10-01 09:35:17.915235 s=s+1 time difference is: 0:00:04.412000 the += time difference is: 0:00:04.343000 the operator.add time difference is: 0:00:07.499000 the operator.iadd time difference is: 0:00:07.734000 the operator.__add__ time difference is: 0:00:07.494000 the operator.__iadd__ time difference is: 0:00:07.906000 Today is Wed, Oct 1 at 09:39:07.830235 give me a number100000 2014-10-01 09:39:14.445235 s=s+1 time difference is: 0:00:00.042000 the += time difference is: 0:00:00.041000 the operator.add time difference is: 0:00:00.084000 the operator.iadd time difference is: 0:00:00.077000 the operator.__add__ time difference is: 0:00:00.076000 the operator.__iadd__ time difference is: 0:00:00.080000 Process finished with exit code 0 ! ! Clayton !-- !Steven !_______________________________________________ !Tutor maillist - Tutor at python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor From dyoo at hashcollision.org Wed Oct 1 19:21:49 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 1 Oct 2014 10:21:49 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: <001b01cfdd91$bd1b8ed0$3752ac70$@us> References: <01eb01cfdd01$86034220$9209c660$@us> <001b01cfdd91$bd1b8ed0$3752ac70$@us> Message-ID: > !> Also, I found something that I can?t get my mind around. It is part of > !> the time/date protocols. I?ve not seen it anywhere else. > !> > !> Datetime(year=blah, blah, blah).date/time() > !> > !> datetime(2013,3,6).date() #returns? > !> datetime.date(2013,3,6) > !> > !> datetime(2013,3,6).time() #returns? > !> datetime.time(0,0) > !> > !> This is one of the weirder things I?ve run across. Is this > !> allowed/needed in other functions/classes, or is it a datetime thing > !only? > ! > ! > !Can you say more about what you expect? It may help to be very > !explicit, even if it seems silly. The problem with talking with > !experienced tutors and programmers is that our perspective has warped > !slightly from extended exposure. :P So we may need a bit of hinting to > !tell what you're referring to by weirdness. > ! > > Sure, the interest is regarding the '2013,3,6' in the first datetime. I've not seen something in the first set of parenthesis before. Is the first one a class or a function, how can you tell without looking at its internals or some documentation? Hi Clayton, I will assume that, at the very beginning of your program, you've done: from datetime import datetime or something equivalent to this. The expression: datetime(2013,3,6).date() can be rewritten as two separate pieces: t = datetime(2013, 3, 6) t.date() with the difference that, in the original expression, the result of the `datetime(2013,3,6)` is not given an explicit variable name, but is directly used as part of a larger expression. You will have seen this before. For example, in the function: ############################################# def c2f(c): """Returns conversion from celsius to fahrenheit.""" return (c * 9/5) + 32 ############################################# the mathematical expression: (c * 9/5) + 32 has two parts to it. We could have rewritten the c2f() function as this: ############################################# def c2f(c): """Returns conversion from celsius to fahrenheit.""" t = c * 9/5 f = t + 32 return f ############################################# where we store the value of each expression with a variable name. Similar meaning, but more verbose. Sometimes we don't need to name every value because otherwise the code is pedantic. But sometimes names help make large expressions easier to understand. Good taste is the judge. One of the key things about expressions is that they "compose": they act like lego in the sense that you can plug them into each other with very few restrictions. So this expression composition is what's happening in: datetime(2013,3,6).date() [More technical note: grammatically, the expression above is an "attribute reference", as defined in: https://docs.python.org/2/reference/expressions.html#attribute-references. The left hand side of an attribute reference expression can itself be a "primary" sub-expression as defined by the grammar.] As for the return value of datetime(2013,3,6): whether it returns an object or something else, you have to trust the documentation. In Python, object construction uses the same syntax as a function call. This is different than from a few other languages, where object construction has a distinct syntax. One of the advantages of having a uniform syntax is that it's easier to later swap out the object construction with something more sophisticated, such as: memoization, or pooling, or other managed work. The disadvantage is that it's harder to see from the source code alone where allocations occur. The documentation of: https://docs.python.org/2/library/datetime.html#datetime.datetime tells us that the return value of: datetime(2013,3,6) is an instance of the datetime class in the datetime module. (It's a bit unfortunate that the class name and the module name use the same name, so as to encourage confusion.) From alan.gauld at btinternet.com Wed Oct 1 23:44:32 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Oct 2014 22:44:32 +0100 Subject: [Tutor] Whack-a-mole In-Reply-To: <89409F4193210C4EB6B405E88013078230CA429D@QUIMBY.CSCo.Org.UK> References: <89409F4193210C4EB6B405E88013078230CA429D@QUIMBY.CSCo.Org.UK> Message-ID: On 01/10/14 16:59, Ben Smith wrote: > Hi, Can anyone help explain why you can keep hitting the Mole even when hittable should be False? I can't. I can hit it 3 seconds after hitting Start then it turns un-hittable and the secs counter keeps counting but nothing else responds until you close the window. The only changes I made were to replace the images with text (I didn't have your gif files...) So if you make those changes and it works for you then it must be something about the way you are using the images... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From stefan.sthilaire at gmail.com Wed Oct 1 23:34:49 2014 From: stefan.sthilaire at gmail.com (Stefan St-Hilaire) Date: Wed, 01 Oct 2014 17:34:49 -0400 Subject: [Tutor] VERY basic question Message-ID: <542C7379.1000702@gmail.com> Hello, I am just starting out with Python and ran into a problem day one. I am doing this statement: input("\n\nPress the enter key to exit.") I get the following error: >>> input("\n\nPress the enter key to exit.") Press the enter key to exit. Traceback (most recent call last): File "", line 1, in File "", line 0 ^ SyntaxError: unexpected EOF while parsing I am using Linux (Shell) and PyCharm and get the same result when I run the command. I know this is stupid but any help would be appreciated. Thanks, Stefan From alan.gauld at btinternet.com Thu Oct 2 00:06:15 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Oct 2014 23:06:15 +0100 Subject: [Tutor] VERY basic question In-Reply-To: <542C7379.1000702@gmail.com> References: <542C7379.1000702@gmail.com> Message-ID: On 01/10/14 22:34, Stefan St-Hilaire wrote: > >>> input("\n\nPress the enter key to exit.") > > > Press the enter key to exit. > Traceback (most recent call last): > File "", line 1, in > File "", line 0 > > ^ > SyntaxError: unexpected EOF while parsing > > > I am using Linux (Shell) and PyCharm and get the same result when I run > the command. I know this is stupid but any help would be appreciated. Can you tell us more about how you run this in the Linux shell? You start Python by typing 'python' at a bash shell? You get the >>> prompt appearing? You type input("\n\nPress the enter key to exit.") at the >>> prompt? You get the Press the enter key to exit. prompt? What do you actually hit then - which specific key(s)? You then see the error message? Is that right? I can't reproduce the exact error message you are seeing, that's why I'm asking for the details... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From juan0christian at gmail.com Thu Oct 2 00:56:58 2014 From: juan0christian at gmail.com (Juan Christian) Date: Wed, 1 Oct 2014 19:56:58 -0300 Subject: [Tutor] Beautifulsoup4 question Message-ID: I have this part of the code - full_item_list = self._soup.find_all('li', {'data-app': '440'}) - that gives me this: Very long output (~ 211 lines): http://pastebin.com/WLTtgVZz Now I need to filter this RAW data, what I need is to convert this data to something like a list of dicts in Python, so that I can do, let's say... for item in data: item['data-name'] > returns > 'Mann Co. Supply Crate' item['data-p-bptf'] > returns > '0.01 ref' item['image'] > returns > 'URL_TO_IMG' item['data-original-id'] > returns > '2713101947' and so on... It would be a list of dicts, each item in the list would be one "
  • item already parsed/filtered", and inside each list item I'd have a dict with these info. Is there something in bs4 that does that, or maybe a different module? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Oct 2 01:16:36 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Oct 2014 01:16:36 +0200 Subject: [Tutor] VERY basic question References: <542C7379.1000702@gmail.com> Message-ID: Stefan St-Hilaire wrote: > Hello, I am just starting out with Python and ran into a problem > day one. I am doing this statement: > > input("\n\nPress the enter key to exit.") > > I get the following error: > > >>> input("\n\nPress the enter key to exit.") > > > Press the enter key to exit. > Traceback (most recent call last): > File "", line 1, in > File "", line 0 > > ^ > SyntaxError: unexpected EOF while parsing > > > I am using Linux (Shell) and PyCharm and get the same result when I run > the command. I know this is stupid but any help would be appreciated. You may be using Python 2 to run a code example written in Python 3. In Python 2 the string entered in input() was evaluated as a Python expression, and an empty string is a syntax error as you can verify with eval(): >>> eval("") Traceback (most recent call last): File "", line 1, in File "", line 0 ^ SyntaxError: unexpected EOF while parsing To get the string as entered by the user you had to use raw_input() instead of input(): >>> raw_input("\n\nPress the enter key to exit.") Press the enter key to exit. '' >>> From dyoo at hashcollision.org Thu Oct 2 01:02:51 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 1 Oct 2014 16:02:51 -0700 Subject: [Tutor] Beautifulsoup4 question In-Reply-To: References: Message-ID: Hi Juan, What you should be getting back from the call to find_all() should already be dictionary-like. Although they *print* like HTML, they're really soups. So you should already be able to do: ############################################# full_item_list = self._soup.find_all('li', {'data-app': '440'}) for item in full_item_list: print item['data-name'] ############################################# That is to say that what you're getting back from self._soup.find_all() is definitely not raw: it's parsed, it's soupy, and you can continue to deal with it structurally. From juan0christian at gmail.com Thu Oct 2 02:37:58 2014 From: juan0christian at gmail.com (Juan Christian) Date: Wed, 1 Oct 2014 21:37:58 -0300 Subject: [Tutor] Beautifulsoup4 question In-Reply-To: References: Message-ID: On Wed, Oct 1, 2014 at 8:02 PM, Danny Yoo wrote: > Hi Juan, > > What you should be getting back from the call to find_all() should > already be dictionary-like. Although they *print* like HTML, they're > really soups. > > So you should already be able to do: > > ############################################# > full_item_list = self._soup.find_all('li', {'data-app': '440'}) > > for item in full_item_list: > print item['data-name'] > ############################################# > > That is to say that what you're getting back from > self._soup.find_all() is definitely not raw: it's parsed, it's soupy, > and you can continue to deal with it structurally. > OH MY GOD! Super fail, hahaha. Thanks, bs4 is incredible. I thought they were RAW html data. Thank you! -------------- next part -------------- An HTML attachment was scrubbed... URL: From juan0christian at gmail.com Thu Oct 2 03:44:18 2014 From: juan0christian at gmail.com (Juan Christian) Date: Wed, 1 Oct 2014 22:44:18 -0300 Subject: [Tutor] Beautifulsoup4 question In-Reply-To: References: Message-ID: On Wed, Oct 1, 2014 at 9:37 PM, Juan Christian wrote: > OH MY GOD! Super fail, hahaha. > > Thanks, bs4 is incredible. I thought they were RAW html data. Thank you! > Not everything is that easy, hahaha. So, I can get everything I want, but this part:
  • #86
    WHATEVER
  • I need this, the image link: background-image:url(NEED_THIS_LINK) I need the image link, I have this in all "
  • items", how can I get that? Sometimes it has a .PNG in the end and sometimes it's a '94x94' in the end, as in here: http://cdn.steamcommunity.com/economy/image/iRulfx1JB6hWyBlnfvJwHzFXb85ZOQnoggbKfZoUOLhAEJKERFVBuvYRBZlYkiyRKVA0ilcmCeyEDc1vmwQTvkAThpJsU1Kx92AKl0faKM86RyzaVSQWs9RQlyrSVHW5FkTa0gJUB7nzWlSTA9l91jsSItxWdxDgyRLNew==/94x94 Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 2 13:56:31 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 2 Oct 2014 12:56:31 +0100 (BST) Subject: [Tutor] VERY basic question Message-ID: <1346078.12213.1412250991277.JavaMail.defaultUser@defaultHost> I've been using Python3 for a while now so forgot how Python 2 handled input errors. You could use Python 2 but you'd need to replace input() with raw_input(). But for learning I'd advise you to stick with Python3, just don't delete python2 from your PC since several Linux tools rely on it. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos >----Original message---- >From : stefan.sthilaire at gmail.com >Date : 02/10/2014 - 00:30 (BST) >To : alan.gauld at btinternet.com >Subject : Re: [Tutor] VERY basic question > >You're a saint. I installed the latest version...3.4 and it executes >with no errors. > >Thanks Again. > > >Stefan > > >On 10/01/2014 06:25 PM, ALAN GAULD wrote: >>> Hi Alan, yes I just type python at the shell to get into interactive >>> mode. Yes all I do is hit the enter key and I get this response. Nothing >>> else. I am following a book to learn python and this is one of the first >>> exercises. The Hello World, followed by a prompt to hit enter to exit. >>> >> ok, its because you are using Python2 interpreter. >> >> Start python using python3 and see what happens. >> >> Alan g > > From crushed26 at gmail.com Thu Oct 2 17:41:49 2014 From: crushed26 at gmail.com (Bo Morris) Date: Thu, 2 Oct 2014 11:41:49 -0400 Subject: [Tutor] printing all text that begins with "25" Message-ID: Hello all, hope everyone is doing well. When I run the linux command "hamachi list" i get something along the lines of the following output 087-888-279 Pandora 25.x.x.xxx alias: not set 096-779-867 AM1LaptopBD-PC 25.x.x.xxx alias: not set 097-552-220 OWS-Desktop 1 25.0.0.0 alias: not set 099-213-641 DESKTOP 25.0.0.0 alias: not set I am trying to write a python script that will run the above command and only print out the IP's that begin with 25. How do I strip out all other text except for the IP's that begin with "25?" Would it be best to send to a file first, then read the contents of the file? Would I need to use regex? I know how to run the above command in python. I also know how to send the output to a file and read the file; however I cannot figure out how to strip all text out except for the IPs that begin with "25." Thanks PS...Danny, still working on the "root" problem we discussed in previous emails. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Oct 2 18:36:16 2014 From: davea at davea.name (Dave Angel) Date: Thu, 2 Oct 2014 12:36:16 -0400 (EDT) Subject: [Tutor] printing all text that begins with "25" References: Message-ID: Bo Morris Wrote in message: (Thanks for starting a new thread when asking a new question. But please use text mode in your emails, not html.) For the first version, write it as a filter, and pipe the two commands together in the shell. So all you have to do is read a line from stdin, parse it, and conditionally write it to std. You don't provide a spec, just a short sample of data. So I'll have to guess that the leading whitespace is irrelevant and the first two fields cannot contain whitespace, and that each contain at least one non-whitespace character. Further that the fields are delimited by whitespace. So, use lstrip to get rid of leading junk, and split to split the line into fields. Then subscript into the resulting list to get the appropriate field. And use startswith to check the desired 3 character string. Notice that you don't just want to use "25", or you might accept an ip like 251.12.3.6 That still leaves you to deal with reporting invalid files, such as those with short or blank lines. -- DaveA From david at graniteweb.com Thu Oct 2 18:33:36 2014 From: david at graniteweb.com (David Rock) Date: Thu, 2 Oct 2014 11:33:36 -0500 Subject: [Tutor] printing all text that begins with "25" In-Reply-To: References: Message-ID: <20141002163336.GH3591@wdfs> * Bo Morris [2014-10-02 11:41]: > Hello all, hope everyone is doing well. > > When I run the linux command "hamachi list" i get something along the lines > of the following output > > 087-888-279 Pandora 25.x.x.xxx alias: not set > 096-779-867 AM1LaptopBD-PC 25.x.x.xxx alias: not set > 097-552-220 OWS-Desktop 1 25.0.0.0 alias: not set > 099-213-641 DESKTOP 25.0.0.0 alias: not set > > I am trying to write a python script that will run the above command and > only print out the IP's that begin with 25. How do I strip out all other > text except for the IP's that begin with "25?" There are a few assumptions that need to be made, for starters. Is the format always the same (ie, is the IP address always in column 3 separated by whitespace)? Looking at the line with "OWS-Desktop 1", the answer is no. That complicates things a bit. If it was true, you could use the string split method to get column 3. Maybe the fields are separated by a tab? A regex may be possible, but you will have similar issues to using split. I'm also assuming it's possible for there to be IP addresses that do not start with 25. Are you looking to isolate those? It's not necessary to write out to a file first. You can get the output from commands and work on it directly. Another approach would be to change the command you are running. I've never heard of hamachi list before; does it have any commandline options to display only IP addresses? -- David Rock david at graniteweb.com From davea at davea.name Thu Oct 2 19:35:20 2014 From: davea at davea.name (Dave Angel) Date: Thu, 02 Oct 2014 13:35:20 -0400 Subject: [Tutor] print date and skip any repeats In-Reply-To: References: <20140917093505.GA9051@cskk.homeip.net> Message-ID: <542D8CD8.5040002@davea.name> On 09/24/2014 05:19 AM, questions anon wrote: > Ok, I am continuing to get stuck. I think I was asking the wrong question > so I have posted the entire script (see below). > What I really want to do is find the daily maximum for a dataset (e.g. > Temperature) that is contained in monthly netcdf files where the data are > separated by hour. > The major steps are: > open monthly netcdf files and use the timestamp to extract the hourly data > for the first day. > Append the data for each hour of that day to a list, concatenate, find max > and plot > then loop through and do the following day. > > I can do some of the steps separately but I run into trouble in working out > how to loop through each hour and separate the data into each day and then > repeat all the steps for the following day. > > Any feedback will be greatly appreciated! > > This is NOT the whole program. You don't seem to define ncvariablename, Dataset, np, Basemap, plt, and probably others. > > oneday=[] > all_the_dates=[] > onedateperday=[] > > > #open folders and open relevant netcdf files that are monthly files > containing hourly data across a region > for (path, dirs, files) in os.walk(MainFolder): > for ncfile in files: If there are more than one file, or more than one directory, then there are no guarantees that this will give you the files in the order you are likely to want. You may need to do some sorting to make it work out. But after further study, you seem to assume you'll read everything in from all the files, and then process it. Have you studied how big that might get, and whether you'll have enough memory to make it viable? > if ncfile.endswith(ncvariablename+'.nc'): > print "dealing with ncfiles:", path+ncfile > ncfile=os.path.join(path,ncfile) > ncfile=Dataset(ncfile, 'r+', 'NETCDF4') > variable=ncfile.variables[ncvariablename][:,:,:] > TIME=ncfile.variables['time'][:] Since you've given ncfile MANY different meanings, this close() will not close the file. Variables are cheap, use lots of them, and give them good names. In this particular case, maybe using a with statement would make sense. > ncfile.close() > > #combine variable and time so I can make calculations based > on hours or days > for v, time in zip((variable[:,:,:]),(TIME[:])): > > cdftime=utime('seconds since 1970-01-01 00:00:00') > ncfiletime=cdftime.num2date(time) > timestr=str(ncfiletime) > utc_dt = dt.strptime(timestr, '%Y-%m-%d %H:%M:%S') > au_tz = pytz.timezone('Australia/Sydney') > local_dt = > utc_dt.replace(tzinfo=pytz.utc).astimezone(au_tz) > strp_local=local_dt.strftime('%Y-%m-%d_%H') #strips > time down to date and hour > local_date=local_dt.strftime('%Y-%m-%d') #strips time > down to just date > > all_the_dates.append(local_date) > > #make a list that strips down the dates to only have one date per day > (rather than one for each hour) > onedateperday = sorted ( list (set (all_the_dates))) > > #loop through each day and combine data (v) where the hours occur on the > same day > for days in onedateperday: > if strp_local.startswith(days): > oneday.append(v) > And just where does v come from? You're no longer in the loop that says "for v, time in..." > big_array=np.ma.dstack(oneday) #concatenate data > v_DailyMax=big_array.max(axis=2) # find max > > #then go on to plot v_DailyMax for each day > map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33, > llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i') > map.drawcoastlines() > map.drawstates() > map.readshapefile(shapefile1, 'REGIONS') > x,y=map(*np.meshgrid(LON,LAT)) > plottitle=ncvariablename+'v_DailyMax'+days > cmap=plt.cm.jet > CS = map.contourf(x,y,v_DailyMax, 15, cmap=cmap) > l,b,w,h =0.1,0.1,0.8,0.8 > cax = plt.axes([l+w+0.025, b, 0.025, h]) > plt.colorbar(CS,cax=cax, drawedges=True) > plt.savefig((os.path.join(OutputFolder, plottitle+'.png'))) > plt.show() > plt.close() > > It looks to me like you've jumbled up a bunch of different code fragments. Have you learned about writing functions yet? Or generators? By making everything global, you're masking a lot of mistakes you've made. For example, v has a value, but just the data from the last record of the last file. I think you need to back off and design this, without worrying much at first about how to code it. It's generally a bad idea to try to collect all data before processing it and winnowing it down. If you know something about the filenaming (or directory naming), and can assume that all data from one day will be processed before encountering the next day, you can greatly simplify things. Likewise if one hour is complete before the next begins. Is there one file per day? Is there one record per hour and are they in order? If some such assumptions can be made, then you might be able to factor the problem down to a set of less grandiose functions. Whoever wrote Dataset() has produced some data from a single file that you can work with. What does that data look like, and how much of it are you really needing to save past the opening of the next one? Sometimes when the data is large and not sorted, it pays off to make two or more passes through it. In the extreme, you might take one pass to make a sorted list of all the hours involved in all the files. Then for each of those hours, you'd take an additional pass looking for data related to that particular hour. Of course if you're talking years, you'll be opening each file many thousands of times. So if you know ANYTHING about the sorting of the data, you can make that more efficient. I can't help you at all with numpy (presumably what np stands for), or the plotting stuff. -- DaveA From crushed26 at gmail.com Thu Oct 2 22:20:43 2014 From: crushed26 at gmail.com (Crush) Date: Thu, 2 Oct 2014 16:20:43 -0400 Subject: [Tutor] printing all text that begins with 25" In-Reply-To: References: Message-ID: <07182624-2758-4E48-B846-6AEF788AF581@gmail.com> Yes the format is always the same and the IPs will always be in the 3rd collumn; although, the amount of whitspace that seperates column 2 and 3 may be different depending on how long the name is in column 2. Also all of the IPs will begin with a "25," so there would be no fear of having to deal with other IP addresses that start with anything else. Hamachi is VPN software and unfortunately, there is no command line argument that allows one to isolate the IPs. Bo From dantheman5457 at gmail.com Thu Oct 2 17:47:44 2014 From: dantheman5457 at gmail.com (John Doe) Date: Thu, 2 Oct 2014 11:47:44 -0400 Subject: [Tutor] Iterating Lines in File and Export Results Message-ID: Hello List, I am in need of your assistance. I have a text file with random words in it. I want to write all the lines to a new file. Additionally, I am using Python 2.7 on Ubuntu 12.04: Here is my code: def loop_extract(): with open('words.txt', 'r') as f: for lines in f: #print lines (I confirmed that each line is successfully printed) with open('export.txt', 'w') as outf: outf.write(lines) #outf.write(lines) #outf.write('{}\n'.format(lines)) #outf.write('{}\n'.format(line for line in lines)) For some reason, the second file only contains the last line from the original file -- I have tried multiple variations (.read, .readlines, .writelines, other examples preceded by comment from above and many more) and tried to use the module, fileinput, but I still get the same results. I do understand there is another way to copy the file over, but to provide additional background information on my purpose -- I want to read a file and save successful regex matches to a file; exporting specific data. There doesn't appear to be anything wrong with my expression as it prints the expected results without failure. I then decided to just write the export function by itself in its basic form, per the code above, which the same behavior occurred; only copying the last line. I've googled for hours and, unfortunately, at loss. Thank you in advance for your help! From dantheman5457 at gmail.com Thu Oct 2 18:58:54 2014 From: dantheman5457 at gmail.com (John Doe) Date: Thu, 2 Oct 2014 12:58:54 -0400 Subject: [Tutor] printing all text that begins with "25" In-Reply-To: <20141002163336.GH3591@wdfs> References: <20141002163336.GH3591@wdfs> Message-ID: Hello, If you want to accomplish what you are looking for within linux (perhaps a bash script, instead?): $ hamachi list | grep -oP '25\.\d+\.\d+\.\d+' 25.0.0.0 25.255.255.255 For your python script, you want to group your regex: reg = re.compile(r'(25\.\d+\.\d+\.\d+)', re.MULTILINE) So when you call group(1) or group(0), it'll grab just the addresses. On Thu, Oct 2, 2014 at 12:33 PM, David Rock wrote: > * Bo Morris [2014-10-02 11:41]: >> Hello all, hope everyone is doing well. >> >> When I run the linux command "hamachi list" i get something along the lines >> of the following output >> >> 087-888-279 Pandora 25.x.x.xxx alias: not set >> 096-779-867 AM1LaptopBD-PC 25.x.x.xxx alias: not set >> 097-552-220 OWS-Desktop 1 25.0.0.0 alias: not set >> 099-213-641 DESKTOP 25.0.0.0 alias: not set >> >> I am trying to write a python script that will run the above command and >> only print out the IP's that begin with 25. How do I strip out all other >> text except for the IP's that begin with "25?" > > There are a few assumptions that need to be made, for starters. > > Is the format always the same (ie, is the IP address always in column 3 > separated by whitespace)? Looking at the line with "OWS-Desktop 1", the > answer is no. That complicates things a bit. If it was true, you could > use the string split method to get column 3. Maybe the fields are > separated by a tab? > > A regex may be possible, but you will have similar issues to using > split. > > I'm also assuming it's possible for there to be IP addresses that do not > start with 25. Are you looking to isolate those? > > It's not necessary to write out to a file first. You can get the output > from commands and work on it directly. > > Another approach would be to change the command you are running. I've > never heard of hamachi list before; does it have any commandline options > to display only IP addresses? > > -- > David Rock > david at graniteweb.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From sebastian at fuentelibre.org Thu Oct 2 18:50:59 2014 From: sebastian at fuentelibre.org (Sebastian Silva) Date: Thu, 02 Oct 2014 11:50:59 -0500 Subject: [Tutor] printing all text that begins with "25" In-Reply-To: <20141002163336.GH3591@wdfs> References: <20141002163336.GH3591@wdfs> Message-ID: <1412268659.26956.2@smtp.gmail.com> El jue, 2 de oct 2014 a las 11:33 AM, David Rock escribi?: > > A regex may be possible, but you will have similar issues to using > split. In my humble experience, a regex is the way to go: import re ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', s ) you will get a list of IP addresses and can filter from there which ones start with "25." -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Oct 3 00:04:00 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 02 Oct 2014 23:04:00 +0100 Subject: [Tutor] Iterating Lines in File and Export Results In-Reply-To: References: Message-ID: On 02/10/14 16:47, John Doe wrote: > def loop_extract(): > with open('words.txt', 'r') as f: > for lines in f: > #print lines (I confirmed that each line is successfully printed) > with open('export.txt', 'w') as outf: This opens and closes the file for each iteration of the inner loop. You need this outside the loop beside the other with statement. > outf.write(lines) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri Oct 3 00:08:13 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Oct 2014 00:08:13 +0200 Subject: [Tutor] Iterating Lines in File and Export Results References: Message-ID: John Doe wrote: > Hello List, > I am in need of your assistance. I have a text file with random words > in it. I want to write all the lines to a new file. Additionally, I am > using Python 2.7 on Ubuntu 12.04: > > Here is my code: > > def loop_extract(): > with open('words.txt', 'r') as f: > for lines in f: The name `lines` is misleading, you are reading one line at a time. > #print lines (I confirmed that each line is successfully > #printed) > with open('export.txt', 'w') as outf: > outf.write(lines) > #outf.write(lines) > #outf.write('{}\n'.format(lines)) > #outf.write('{}\n'.format(line for line in lines)) > > > For some reason, the second file only contains the last line from the > original file -- I have tried multiple variations (.read, .readlines, > .writelines, other examples preceded by comment from above and many > more) and tried to use the module, fileinput, but I still get the same > results. Every time the line > with open('export.txt', 'w') as outf: is executed the file "export.txt" is truncated: https://docs.python.org/dev/library/functions.html#open To avoid the loss of data open the file once, outside the loop: with open("words.txt") as infile, open("export.txt", "w") as outfile: for line in infile: outfile.write(line) > I do understand there is another way to copy the file over, but to > provide additional background information on my purpose -- I want to > read a file and save successful regex matches to a file; exporting > specific data. There doesn't appear to be anything wrong with my > expression as it prints the expected results without failure. I then > decided to just write the export function by itself in its basic form, > per the code above, which the same behavior occurred; That is a good approach! Reduce the code until only the source of the problem is left. > only copying the > last line. I've googled for hours and, unfortunately, at loss. I do that too, but not "for hours" ;) > I want to read a file and save successful regex matches to a file; > exporting specific data. An experienced user of Python might approach this scenario with a generator: def process_lines(infile): for line in infile: line = process(line) # your line processing if meets_condition(line): # your filter condition yield line with open("words.txt") as infile: with open("export.txt", "w") as outfile: outfile.writelines( process_lines(infile)) From alan.gauld at btinternet.com Fri Oct 3 00:06:06 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 02 Oct 2014 23:06:06 +0100 Subject: [Tutor] printing all text that begins with "25" In-Reply-To: References: Message-ID: On 02/10/14 16:41, Bo Morris wrote: > of the following output > > 087-888-279 Pandora 25.x.x.xxx alias: not > set > > 096-779-867 AM1LaptopBD-PC 25.x.x.xxx alias: not set > > > 097-552-220 OWS-Desktop 1 25.0.0.0 alias: not set > > 099-213-641 DESKTOP 25.0.0.0 alias: not > set > > I am trying to write a python script that will run the above command and > only print out the IP's that begin with 25. How do I strip out all other > text except for the IP's that begin with "25?" Use split() to get the 'columns' in a list then use strip() to get rid of whitespace. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From martin at linux-ip.net Fri Oct 3 01:01:50 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 2 Oct 2014 16:01:50 -0700 Subject: [Tutor] printing all text that begins with "25" In-Reply-To: References: Message-ID: Hi Bo, > I am trying to write a python script that will run the above > command and only print out the IP's that begin with 25. How do I > strip out all other text except for the IP's that begin with "25?" I liked the suggestion by John Doe earlier that this is a pretty good case for 'grep', but perhaps you want to do more than simply see the results on the terminal. So, you seem to want to be able to 'grep' for IPs that match a particular prefix, 25.0.0.0/8. Do you, perchance work for org-name: DINSA, Ministry of Defence [0] or are you using 25.0.0.0/8 in a private network. If the latter, are you sure you don't want to use one of the RFC 1918 networks? > Would it be best to send to a file first, then read the contents > of the file? Would I need to use regex? Others have addressed some of this. > I know how to run the above command in python. I also know how to > send the output to a file and read the file; however I cannot > figure out how to strip all text out except for the IPs that begin > with "25." Ok, so I can't help but observe that you are working with IP-oriented data. While you can perform tests like: ipstr.startswith('25') # -- yep, '250', '251', '253', '254', also or similar tests by writing a regular expression and using one of the heavier tools (re.findall, re.compile, re.match), I think that merely helps you locate the text that you think is the IP address. If you are asking is the IP within the 25.0.0.0/8 prefix, then you probably want to use the ipaddr (Python 2.x from PyPI) or ipaddress (Python 3.x stdlib) module to validate the IP and make sure that the IP is in a prefix of interest. I made one liberal change to the format of your data--I made it tab-separated. If it is not tab-separated, then you can see which line would probably need to have your regex line-splitter. The below, is more general than finding every IP that starts with '25.', because now you can "ipaddr-grep" for what you want. #! /usr/bin/python from __future__ import print_function import sys try: # -- Python2.x import ipaddr as ipaddress except ImportError: # -- Python3.x import ipaddress separator = '\t' def ipaddr_grep(prefix, fin): for line in fin: line = line.strip() if not line or line.startswith('#'): continue parts = line.strip().split(separator) # -- tab separated ip = ipaddress.IPv4Address(parts[2]) if ip in prefix: yield(line) def ipaddr_grep_main(prefix, fnames): prefix = ipaddress.IPv4Network(prefix) while fnames: fname = fnames.pop() with open(fname) as fin: for line in ipaddr_grep(prefix, fin): print(line) if __name__ == '__main__': ipaddr_grep_main(sys.argv[1], sys.argv[2:]) I happen to be the sort of person who always wants to point out the IP-related tools available in Python hence my reply to your post. Happy trails and good luck, -Martin [0] https://apps.db.ripe.net/search/query.html?searchtext=25.0.0.0/8&source=RIPE#resultsAnchor -- Martin A. Brown http://linux-ip.net/ From dantheman5457 at gmail.com Fri Oct 3 00:51:12 2014 From: dantheman5457 at gmail.com (John Doe) Date: Thu, 2 Oct 2014 18:51:12 -0400 Subject: [Tutor] Iterating Lines in File and Export Results In-Reply-To: References: Message-ID: Alan, Peter, et al: Thank you all very much! Staring at this problem for hours was driving me crazy and I am very appreciative for your guys' time in looking into my silly error -- I have thoroughly reviewed both the responses and it makes perfect sense (*sigh of relief*). On Thu, Oct 2, 2014 at 6:08 PM, Peter Otten <__peter__ at web.de> wrote: > John Doe wrote: > >> Hello List, >> I am in need of your assistance. I have a text file with random words >> in it. I want to write all the lines to a new file. Additionally, I am >> using Python 2.7 on Ubuntu 12.04: >> >> Here is my code: >> >> def loop_extract(): >> with open('words.txt', 'r') as f: >> for lines in f: > > The name `lines` is misleading, you are reading one line at a time. > >> #print lines (I confirmed that each line is successfully >> #printed) >> with open('export.txt', 'w') as outf: >> outf.write(lines) >> #outf.write(lines) >> #outf.write('{}\n'.format(lines)) >> #outf.write('{}\n'.format(line for line in lines)) >> >> >> For some reason, the second file only contains the last line from the >> original file -- I have tried multiple variations (.read, .readlines, >> .writelines, other examples preceded by comment from above and many >> more) and tried to use the module, fileinput, but I still get the same >> results. > > Every time the line > >> with open('export.txt', 'w') as outf: > > is executed the file "export.txt" is truncated: > > https://docs.python.org/dev/library/functions.html#open > > To avoid the loss of data open the file once, outside the loop: > > with open("words.txt") as infile, open("export.txt", "w") as outfile: > for line in infile: > outfile.write(line) > > >> I do understand there is another way to copy the file over, but to >> provide additional background information on my purpose -- I want to >> read a file and save successful regex matches to a file; exporting >> specific data. There doesn't appear to be anything wrong with my >> expression as it prints the expected results without failure. I then >> decided to just write the export function by itself in its basic form, >> per the code above, which the same behavior occurred; > > That is a good approach! Reduce the code until only the source of the > problem is left. > >> only copying the >> last line. I've googled for hours and, unfortunately, at loss. > > I do that too, but not "for hours" ;) > >> I want to read a file and save successful regex matches to a file; >> exporting specific data. > > An experienced user of Python might approach this scenario with a generator: > > def process_lines(infile): > for line in infile: > line = process(line) # your line processing > if meets_condition(line): # your filter condition > yield line > > with open("words.txt") as infile: > with open("export.txt", "w") as outfile: > outfile.writelines( > process_lines(infile)) > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Fri Oct 3 13:41:26 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Oct 2014 21:41:26 +1000 Subject: [Tutor] could somebody please explain... In-Reply-To: <003201cfdd96$d4a17e20$7de47a60$@us> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001013814.GW19757@ando.pearwood.info> <003201cfdd96$d4a17e20$7de47a60$@us> Message-ID: <20141003114126.GC19757@ando.pearwood.info> On Wed, Oct 01, 2014 at 09:43:29AM -0700, Clayton Kirkwood wrote: > In an effort to learn and teach, I present a simple program which measures > the time it takes to the various add functions with the appending results: Well done for making the effort! Now I'm going to tell you all the things you've done wrong! Sorry. But seriously, I am very pleased to see you making the effort to develop this on your own, but *accurately* timing fast-running code on modern computers is very tricky. The problem is, when you run some code, it isn't the only program running! The operating system is running, and these days all computers are multi-tasking, which means that anything up to hundreds of other programs could be running at the same time. At any one instant, most of them will be idle, doing nothing, but there's no way to be sure. Furthermore, there are now complexities with CPU caches. Running a bit of code will be much slower the first time, since it is not in the CPU cache. If the code it too big, it won't fit in the cache. The end result is that when you time how long a piece of code takes to run, there will always be two components: - the actually time taken for your code to run; - random "noise" caused by CPU cache effects, other processes running, the operating system, your anti-virus suddenly starting a scan in the middle of the run, etc. The noise can be quite considerable, possibly a few seconds. Now obviously if your code took ten minutes to run, then a few seconds either way is no big deal. But imagine that your timing test says that it took 2 seconds. That could mean: - 0.001 seconds for your code, and 1.999 seconds worth of noise; - 1.999 seconds for your code, and 0.001 seconds worth of noise; - or anything in between. That measurement is clearly quite useless. Does this mean that timing Python code is impossible? No, not really, but you have to do it carefully. The best way is to use Python's "timeit" module, which is carefully crafted to be as accurate as possible. First I'll show some results with timeit, then come back for a second post where I explain what you can do to be (nearly) as accurate. I'm going to compare four different ways of adding two numbers: (1) Using the + operator (2) Using operator.add (3) Using operator.__add__ (4) Using a hand-written function, made with lambda Here's the plus operator: from the command shell, I tell Python to use the timeit module to time some code. I give it some setup code to initialise two variables, then I time adding them together: [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" "x + y" 10000000 loops, best of 3: 0.0971 usec per loop [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" "x + y" 10000000 loops, best of 3: 0.0963 usec per loop So timeit measures how long it takes to run "x + y" ten million times. It does that three times, and picks the fastest of the three. The fastest will have the least amount of noise. I ran it twice, and the two results are fairly close: 0.0971 microseconds, and 0.0963 microseconds. [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "import operator" "operator.add(x, y)" 1000000 loops, best of 3: 0.369 usec per loop [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "import operator" "operator.add(x, y)" 1000000 loops, best of 3: 0.317 usec per loop This time I use operator.add, and get a speed of about 0.3 microseconds. So operator.add is about three times slower than the + operator. [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "import operator" "operator.__add__(x, y)" 1000000 loops, best of 3: 0.296 usec per loop [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "import operator" "operator.__add__(x, y)" 1000000 loops, best of 3: 0.383 usec per loop This time I use operator.__add__, and get about the same result as operator.add. You can see the variability in the results: 0.296 to 0.383 microsecond, that's a variation of about 30%. [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "add = lambda a,b: a+b" "add(x, y)" 1000000 loops, best of 3: 0.296 usec per loop [steve at ando ~]$ python3.3 -m timeit -s "x = 1; y = 2" -s "add = lambda a,b: a+b" "add(x, y)" 1000000 loops, best of 3: 0.325 usec per loop Finally, I try it with a hand-made function using lambda, and I get about the same 0.3 microseconds again, with considerable variability. Of course, the results you get on your computer may be completely different. More to follow... -- Steven From steve at pearwood.info Fri Oct 3 15:21:15 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Oct 2014 23:21:15 +1000 Subject: [Tutor] could somebody please explain... In-Reply-To: <003201cfdd96$d4a17e20$7de47a60$@us> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001013814.GW19757@ando.pearwood.info> <003201cfdd96$d4a17e20$7de47a60$@us> Message-ID: <20141003132115.GD19757@ando.pearwood.info> On Wed, Oct 01, 2014 at 09:43:29AM -0700, Clayton Kirkwood wrote: > # program to test time and count options > > import datetime,operator, sys > from datetime import time, date, datetime > date = datetime.now() > dayofweek = date.strftime("%a, %b") > print("Today is", dayofweek, date.day, "at ", date.time()) > > start = 0 > count_max=int(input("give me a number")) > start_time = datetime.now() > > print( start_time ) > while start < count_max: > start=start + 1 > > end_time = datetime.now() > print( "s=s+1 time difference is:", (end_time - start_time) ) The first problem you have here is that you are not actually timing how long it takes to add "start + 1". You're actually timing eight things: - lookup the value of start; - lookup the value of count_max; - check whether the first is less than the second; - decide whether to loop, or exit the loop; - if we're still inside the loop, lookup start again; - add 1 to it; - store the result in start; - jump back to the top of the loop. So the results you get don't tell you much about the speed of start+1. Analogy: you want to know how long it takes you to drive to work in the morning. So you wake up, eat breakfast, brush your teeth, start the stopwatch, have a shower, get dressed, get in the car, drive to the gas station, fill up, buy a newspaper, and drive the rest of the way to work, and finally stop the stopwatch. The time you get is neither accurate as "driving time", nor "total time it takes to get to work" time. Ideally, we want to do as little extra work as possible inside the timing loop, so we can get a figure as close as possible to the time actually taken by + as we can. The second problem is that you are using datetime.now() as your clock. That's not a high-precision clock. It might be only be accurate to a second, or a millisecond. It certainly isn't accurate enough to measure a single addition: py> from datetime import datetime py> x = 1 py> t = datetime.now(); x + 1; datetime.now() - t 2 datetime.timedelta(0, 0, 85) This tells me that it supposedly took 85 microseconds to add two numbers, but as I showed before with timeit, the real figure is closer to 0.09 microseconds. That's a lot of noise! About 85000% noise! Unfortunately, it is tricky to know which clock to use. On Windows, time.clock() used to be the best one; on Linux, time.time() was the best. Starting in Python 3.3, there are a bunch more accurate clocks in the time module. But if you use the timeit module, it already picks the best clock for the job. But if in doubt, time.time() will normally be acceptable. https://docs.python.org/3/library/time.html https://docs.python.org/3/library/timeit.html -- Steven From crk at godblessthe.us Fri Oct 3 17:38:46 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Fri, 3 Oct 2014 08:38:46 -0700 Subject: [Tutor] could somebody please explain... In-Reply-To: <20141003132115.GD19757@ando.pearwood.info> References: <01eb01cfdd01$86034220$9209c660$@us> <20141001013814.GW19757@ando.pearwood.info> <003201cfdd96$d4a17e20$7de47a60$@us> <20141003132115.GD19757@ando.pearwood.info> Message-ID: <033501cfdf20$1f8178c0$5e846a40$@us> Steven, I don't disagree with most of your analysis, I didn't know of other timing routines, and all of the superfluous stuff adds up. However, for a simple test, the route that I took was adequate I think. Yes I timed the whole wakeup to get to work, but the important element is that whatever I timed, was accurate between runs. And that is all that was import: to see the relative times.I also ran the complete program multiple times and found the test to be relatively consistent. I appreciate your notice of timeit(), I'll have to look into that, thanks. Thanks for taking the time to review and comment. Clayton !-----Original Message----- !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On !Behalf Of Steven D'Aprano !Sent: Friday, October 03, 2014 6:21 AM !To: tutor at python.org !Subject: Re: [Tutor] could somebody please explain... ! !On Wed, Oct 01, 2014 at 09:43:29AM -0700, Clayton Kirkwood wrote: ! !> # program to test time and count options !> !> import datetime,operator, sys !> from datetime import time, date, datetime date = datetime.now() !> dayofweek = date.strftime("%a, %b") print("Today is", dayofweek, !> date.day, "at ", date.time()) !> !> start = 0 !> count_max=int(input("give me a number")) start_time = datetime.now() !> !> print( start_time ) !> while start < count_max: !> start=start + 1 !> !> end_time = datetime.now() !> print( "s=s+1 time difference is:", (end_time - start_time) ) ! ! !The first problem you have here is that you are not actually timing how !long it takes to add "start + 1". !You're actually timing eight things: ! !- lookup the value of start; !- lookup the value of count_max; !- check whether the first is less than the second; !- decide whether to loop, or exit the loop; !- if we're still inside the loop, lookup start again; !- add 1 to it; !- store the result in start; !- jump back to the top of the loop. ! ! !So the results you get don't tell you much about the speed of start+1. ! !Analogy: you want to know how long it takes you to drive to work in the !morning. So you wake up, eat breakfast, brush your teeth, start the !stopwatch, have a shower, get dressed, get in the car, drive to the gas !station, fill up, buy a newspaper, and drive the rest of the way to !work, and finally stop the stopwatch. The time you get is neither !accurate as "driving time", nor "total time it takes to get to work" !time. ! !Ideally, we want to do as little extra work as possible inside the !timing loop, so we can get a figure as close as possible to the time !actually taken by + as we can. ! !The second problem is that you are using datetime.now() as your clock. !That's not a high-precision clock. It might be only be accurate to a !second, or a millisecond. It certainly isn't accurate enough to measure !a single addition: ! !py> from datetime import datetime !py> x = 1 !py> t = datetime.now(); x + 1; datetime.now() - t !2 !datetime.timedelta(0, 0, 85) ! ! !This tells me that it supposedly took 85 microseconds to add two !numbers, but as I showed before with timeit, the real figure is closer !to 0.09 microseconds. That's a lot of noise! About 85000% noise! ! !Unfortunately, it is tricky to know which clock to use. On Windows, !time.clock() used to be the best one; on Linux, time.time() was the !best. Starting in Python 3.3, there are a bunch more accurate clocks in !the time module. But if you use the timeit module, it already picks the !best clock for the job. But if in doubt, time.time() will normally be !acceptable. ! !https://docs.python.org/3/library/time.html ! !https://docs.python.org/3/library/timeit.html ! ! ! !-- !Steven !_______________________________________________ !Tutor maillist - Tutor at python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor From azzairob at gmail.com Fri Oct 3 23:27:31 2014 From: azzairob at gmail.com (Rob Ward) Date: Fri, 3 Oct 2014 14:27:31 -0700 Subject: [Tutor] pygame module Message-ID: i downloaded the 3.4 version of python but there is no matching binary file for pygame ive tried every 1.9.1 file and still cant import pygame would an older version of python work rob -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Sat Oct 4 03:04:36 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 3 Oct 2014 18:04:36 -0700 Subject: [Tutor] pygame module In-Reply-To: References: Message-ID: On Fri, Oct 3, 2014 at 2:27 PM, Rob Ward wrote: > i downloaded the 3.4 version of python but there is no matching binary file > for pygame ive tried every 1.9.1 file and still cant import pygame would an > older version of python work You might have better results contacting the Pygame community for this question, as you're asking an installation question on a third-party library. http://pygame.org/wiki/info According to their FAQ, Pygame 1.9.2 should support Python 3: http://www.pygame.org/wiki/FrequentlyAskedQuestions#Does Pygame work with Python 3? From robertvstepp at gmail.com Sat Oct 4 02:46:02 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 3 Oct 2014 19:46:02 -0500 Subject: [Tutor] pygame module In-Reply-To: References: Message-ID: On Fri, Oct 3, 2014 at 4:27 PM, Rob Ward wrote: > i downloaded the 3.4 version of python but there is no matching binary file > for pygame ive tried every 1.9.1 file and still cant import pygame would an > older version of python work > If you have windows try: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame -- boB From spauldingfamily at twc.com Mon Oct 6 00:40:39 2014 From: spauldingfamily at twc.com (Mike Spaulding) Date: Sun, 5 Oct 2014 18:40:39 -0400 Subject: [Tutor] New at Python Message-ID: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total . Use no variables other than n , k , and total . Hi, I am really a rookie at Python and I am just learning some of the language and some of the functions etc. I am using Pearson's Programming lab and the accompanying online book. The biggest problem that I am having is trying to figure out how to start off on problems such as the one above. I have been out of school for over 40 years and I am struggling, needless to say. I know this problem will be laughably easy for most of you, but do you have any suggestions as to how I need to look at a problem and to "identify" what exactly it is asking for, and how I can pick out the elements that I need to start with? Any and all help will be greatly appreciated. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From amonroe at columbus.rr.com Mon Oct 6 02:10:13 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Sun, 5 Oct 2014 20:10:13 -0400 Subject: [Tutor] New at Python In-Reply-To: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> References: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> Message-ID: <1474428057.20141005201013@columbus.rr.com> > while loop to compute > the sum of the cubes of the first n counting numbers > do you have any suggestions as to how I need to look at a problem > and to ?identify? what exactly it is asking for, Can you do it on paper? If I gave you "5" as a starting point, could you write down on your paper 1 cubed plus 2 cubed plus 3 cubed plus 4 cubed plus 5 cubed and arrive at a correct sum? Alan From alan.gauld at btinternet.com Mon Oct 6 02:14:23 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Oct 2014 01:14:23 +0100 Subject: [Tutor] New at Python In-Reply-To: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> References: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> Message-ID: On 05/10/14 23:40, Mike Spaulding wrote: > Given that n refers to a positive int use a while loop to compute the sum of > the cubes of the first n counting numbers, and associate this value with > total . Use no variables other than n , k , and total . > lab and the accompanying online book. The biggest problem that I am having > is trying to figure out how to start off on problems such as the one above. You need to break the problem into sub problems until you wind up with things you can do. For example in the above exercise: 1) can you associate a variable called n with an integer? 2) can you use a while loop to iterate a number of times? 3) can you calculate the cube of a number? 4) can you total two numbers and store the result? 5) can you display the final total? Assuming you can do each of those bits you need to organize them into a structure to form your program. You need to store a value in n and use a while loop to loop that many times. You need a counter to store the number of loops. For each time through the loop you need to calculate the cube of the counter. You then need to add that result to the running total. When the loop ends you need to report the total. Its the same with most beginner exercises, you divide it into hunks and solve each chunk. Then assemble the bits to solve the whole. Once the problems get a lot bigger you need to apply some more advanced techniques which we call analysis and design. But for beginner stuff the divide and conquer approach works just fine. Does that help? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Oct 6 02:15:27 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 5 Oct 2014 17:15:27 -0700 Subject: [Tutor] New at Python In-Reply-To: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> References: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> Message-ID: On Sun, Oct 5, 2014 at 3:40 PM, Mike Spaulding wrote: > Given that n refers to a positive int use a while loop to compute the sum of > the cubes of the first n counting numbers, and associate this value with > total . Use no variables other than n , k , and total . > > Hi, I am really a rookie at Python and I am just learning some of the > language and some of the functions etc. I am using Pearson?s Programming > lab and the accompanying online book. The biggest problem that I am having > is trying to figure out how to start off on problems such as the one above. Hi Mike, If you can point out previous exercises that you've done, that may help us point out similarities between the things you've done before, and this particular problem. (The material you're pointing out is not freely available, so I can not search for it not see what problems preceded the one you're working on now.) Generally, a problem solving strategy such as: http://en.wikipedia.org/wiki/How_to_Solve_It should be helpful in breaking this down. R. Alan Monroe suggests that you try it "by hand" first. This is a good approach to start: you need to _understand_ what's being asked before trying to instruct a computer by hand. Do you understand the problem statement? What is the "input", and what is the expected "output"? Let's get that cleared up before talking about the code. Good luck! From steve at pearwood.info Mon Oct 6 04:08:48 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 6 Oct 2014 13:08:48 +1100 Subject: [Tutor] New at Python In-Reply-To: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> References: <007301cfe0ed$63d8d760$2b8a8620$@twc.com> Message-ID: <20141006020848.GM19757@ando.pearwood.info> On Sun, Oct 05, 2014 at 06:40:39PM -0400, Mike Spaulding wrote: > Given that n refers to a positive int use a while loop to compute the sum of > the cubes of the first n counting numbers, and associate this value with > total . Use no > 3> variables other than n , k , and total . Here is the thought process I would use to convert this into code. Actually writing code happens quite late in the process, first I try to understand the problem, and then and only then do I write code. We want to "compute the sum of ...", associating that value with total. So write it out in English (or whatever your native language is): total = sum of ... What do the ... represent? The cubes of the first n counting numbers. Do you remember the definition of "counting numbers"? If not, google for it. Understanding the problem, and the language used in the problem, is critical to solving the problem. https://duckduckgo.com/html/?q=counting+numbers The counting numbers are simply 1, 2, 3, 4, 5, ... which goes on forever, but fortunately for this problem we only want "the first n" of them. If you're familiar with mathematics, that's obvious, but what if you're not? Let's reason inductively. If we wanted the first *three* numbers, we would have: 1, 2, 3. If we wanted the first *four* numbers, we would have: 1, 2, 3, 4. If we wanted the first *ten* numbers, we would have: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. I'm seeing a pattern here. So if you use a pronumeral "n" to represent the, er, number of numbers we want, we'll have something like this: 1, 2, 3, 4, ... n. and we want the *cubes* of those numbers: 1**3, 2**3, 3**3, 4**3, ... n**3. and they go into our sum: total = sum of 1**3, 2**3, 3**3, 4**3, ... n**3 We're told to solve this with a while loop, using variables n, k and total. Let's ignore the part about the while loop for now, and concentrate on the variables. n and total are obviously the input and the output: n is the number we're working on, total is the number we calculate. What's k? That gives us a strong hint that we need a third variable. What could it be? Since we can only add two numbers at a time (a + b) and we know that total is going to be one of them (total + ???) and n is *not* going to be the other one, that gives us a clue that k will be the other one: k = 1 total = total + k**3 k = 2 total = total + k**3 etc. Let's start with a simpler problem: ignore the requirement to cube the numbers, and let's just sum the natural numbers themselves: total = sum of 1, 2, 3, 4, ... n We can write that as: total = 0 # We start with nothing. total = total + 1 # Add the first number. total = total + 2 # Add the second number. total = total + 3 # Add the third number. total = total + 4 # I'm starting to see a pattern here. and so on. Whenever you see repeated code like that, you should think of a loop. A for loop is actually the right solution here, but the question insists you use a while loop, so let's start with a little bit of almost-but-not-quite Python code. We use k as the loop variable, it will start at the first number we want to add, and increase by one each time through the loop: total = 0 k = 1 # The number we're going to add to the total, starting at 1. while there are still numbers to be added: total = total + k k = k + 1 # Go on to the next number to be added. There's a shorter way to add an increment to a variable: total = 0 k = 1 while there are still numbers to be added: total += k k += 1 Obviously "while there are still numbers to be added" is not Python code. In Python, we need "while condition". What is the condition that decides whether there are still numbers to be added or not? Let's take a concrete example: n = 4. We're going to loop with: k = 1 is used. k = 2 is used. k = 3 is used. k = 4 is used. <-- n = 4 k = 5 is NOT used. So our condition is that k is less than or equal to n. So now we can insert that condition into the Python code: total = 0 k = 1 while k <= n: total += k k += 1 Don't forget that we actually want to sum the cubes! total = 0 k = 1 while k <= n: total += k**3 k += 1 And we're done here and can move on to the next exercise. Or, we can continue on this one and do it *right*. Earlier, I said that a for loop was the right solution for this, rather than a while loop. Using a for loop simplifies the management of the k variable: total = 0 for k in (1, 2, 3, 4, ..., n) # not quite legal Python! total = total + k**3 To get the values (1, 2, 3, 4, ..., n) we use the built-in Python function "range", which returns a sequence of numbers, given an optional starting value, a stopping value, and an optional stepsize or stride: range(6) -> 0, 1, 2, 3, 4, 5 range(1, 6) -> 1, 2, 3, 4, 5 range(1, 6, 2) -> 1, 3, 5 So in this case, we want to start at 1, and end with n *included*, so our stopping value is n+1, and the step is just the default 1: total = 0 for k in range(1, n+1): total = total + k**3 That can be simplified using a generator expression and the built-in sum() function: total = sum(k**3 for k in range(1, n+1)) only you probably haven't learned about them yet. But you can probably see the similarity between the explicit for-loop and the generator expression: for k in range(1, n+1): k**3 vs. (k**3 for k in range(1, n+1)) The order is slightly different, but I hope you can see the similarity. Can we improve this solution? Here it is again: total = sum(k**3 for k in range(1, n+1)) Yes, we can improve it, thanks to mathematics! Can we find a simple equation that gives us the sum of the first n cubes? Googling for "sum of first n cubes" gets me to here: https://en.wikipedia.org/wiki/Squared_triangular_number which simplifies my code to: total = sum(k for k in range(1, n+1))**2 I can simplify that even more, since there is a marvelous equation that gives the sum of the first n counting numbers. These two pages will give you a good feel for how to think like a mathematician and find patterns, which is an excellent skill for programmers too: http://www.comfsm.fm/~dleeling/math/cogapp/sumofn.html http://proofsfromthebook.com/2013/01/29/first-n-positive-integers/ Using that formula gives us: total = (n*(n+1)//2)**2 # sum of the first n cubes. which requires no other variables, no loops, and only four maths operations: one each addition, multiplication, division and power. For large values of n it will be *much* faster than summing them by hand. The only downside is that it isn't *obvious* that it is the sum of the first n cubes, but that's why # comments are available. -- Steven From crk at godblessthe.us Mon Oct 6 04:19:36 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Sun, 5 Oct 2014 19:19:36 -0700 Subject: [Tutor] grave confusion Message-ID: <04db01cfe10b$fb217f00$f1647d00$@us> Here's my problem; my code snippet reads a file(presumably an _io.readline, I'll question this later), with the file.readline(). The output shows individual characters being read and printed out followed by the "here" being printed. Also, see below. Source data file: html class="yui3-js-enabled" id="yui_3_10_3_4_1412203661632_7637">
    Code snippet: import re, os for line_in in file.readline(): print( line_in ) print("here") continue Output: < here ! here - here - here here s here a here v here e Why the individual characters and not the full line? Also, it is very unclear to me which readline I am using. I have imported os. I am using pycharm IDE and supposedly it will show me where readline is coming from by right clicking and choosing find usages. However, it brings up three readlines which are place holders. It doesn't bring up the os.readline. I am getting really tired of trying to figure out which function is being used! So, what is happening that I am missing? TIA, Clayton -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Oct 6 12:48:19 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Oct 2014 11:48:19 +0100 Subject: [Tutor] grave confusion In-Reply-To: <04db01cfe10b$fb217f00$f1647d00$@us> References: <04db01cfe10b$fb217f00$f1647d00$@us> Message-ID: On 06/10/14 03:19, Clayton Kirkwood wrote: > Here's my problem; my code snippet reads a file(presumably an _io.readline, > I'll question this later), with the file.readline(). Nope, it reads a line from a file. That's quite a different thing. > The output shows > individual characters being read and printed out followed by the "here" > being printed. Also, see below. Because a for loop iterates over a string character by character And because the print 'here' is inside the loop. I'm not sure if thats deliberate or not. > for line_in in file.readline(): > print( line_in ) > print("here") > continue You don't need the continue since the loop will continue from the end of the block automatically. Thats what loops do. > Why the individual characters and not the full line? Because you are iterating over the line which is a collection of characters. So the for loop breaks it down to each character. > Also, it is very unclear to me which readline I am using. We can't be certain since you only show a snippet which does not include you opening the file. But based on the output you are using a file readline and in Python 3 a text file is an instance of class _io.TextIOWrapper But you don't need to know that, its just a "file-like object". > I have imported os. I am using pycharm IDE and supposedly it > will show me where readline is coming from ... it brings up three > readlines which are place holders. It doesn't bring up the os.readline. I don't think there is an os.readline. You don't need the IDE to tell you where things are coming from just look at your code. What is file? Where does it come from? Thats the readline() you are using. If you type help(file) it will give you the documentation of file including its superclasses if any. > getting really tired of trying to figure out which function is being used! Mostly you don't need to. Use them. If in doubt use help(). In this case help(file.readline) > So, what is happening that I am missing? Mostly the fact that readline() reads a line() not the entire file. That would be readlines() But nowadays you rarely need readlines(() either. You should just iterate over the file. So your code should be: for line in file: print(line) print('help') -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon Oct 6 13:23:27 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Oct 2014 12:23:27 +0100 Subject: [Tutor] grave confusion In-Reply-To: References: <04db01cfe10b$fb217f00$f1647d00$@us> Message-ID: On 06/10/14 11:48, Alan Gauld wrote: > Mostly the fact that readline() reads a line() ... Oops, Got a bit over enamoured with the parentheses there. Should just be plain 'line' of course. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon Oct 6 18:43:41 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Oct 2014 17:43:41 +0100 Subject: [Tutor] Sqliute Row row factory Message-ID: I just discovered the SQLite Row row_factory which as the docs say: ----- If returning a tuple doesn?t suffice and you want name-based access to columns, you should consider setting row_factory to the highly-optimized sqlite3.Row type. Row provides both index-based and case-insensitive name-based access to columns with almost no memory overhead. ----- Now my question is: Does this exist in the DBAPI or is it SQLite specific? In other words, if I use this feature will it screw me up if I try to convert from SQLite to Oracle or Postgres later? Anyone know? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Oct 6 19:46:53 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 6 Oct 2014 10:46:53 -0700 Subject: [Tutor] grave confusion In-Reply-To: <04db01cfe10b$fb217f00$f1647d00$@us> References: <04db01cfe10b$fb217f00$f1647d00$@us> Message-ID: Alan has pointed out that your loop here: for line_in in file.readline(): ... has a much different meaning that the one you intend. It means: "for every character in the first line of the file: ..." The reason is because "file.readline()" returns a line of your file as a string. A string is a sequence of characters. Loops work on sequences of things, so in the loop above, it will walk over the sequence of characters. That being said, if you're trying to parse HTML with regular expressions, you probably want to reconsider. Instead, you might want to look into a dedicated parser for that task such as Beautiful Soup. http://www.crummy.com/software/BeautifulSoup/ The problem with a regular expressions approach is that it's easy to code up a fragile, broken solution. See: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags From dyoo at hashcollision.org Mon Oct 6 20:25:41 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 6 Oct 2014 11:25:41 -0700 Subject: [Tutor] Fwd: grave confusion In-Reply-To: References: <04db01cfe10b$fb217f00$f1647d00$@us> <058601cfe191$5ae52d30$10af8790$@us> Message-ID: ---------- Forwarded message ---------- From: Danny Yoo Date: Mon, Oct 6, 2014 at 11:23 AM Subject: Re: [Tutor] grave confusion To: Clayton Kirkwood > Well the guide certainly doesn't suggest that the read is one character at a time, it implies one line at a time. However, it's hard to argue against the one character because that is what the output is looking at. I thought it would read one line in per iteration. Why didn't they call it readchar()? I think you might still be confused. Here, let's look at it again from a different angle. #################### for line in file.readline(): ... #################### I'm going to make a small change to this: #################### seq = file.readline() for line in seq: ... #################### I've introduced a temporary variable. This should preserve the rough meaning by just giving a name to the thing we're walking across. One more change: #################### seq = file.readline() for thing in seq: ... #################### Again, meaning preserving, if we change every instance of "line" in "..." with "thing". But "thing" is a poor name for this as well. What's its type? If seq is a string, then thing has to be a character. Let's change the code one more time. ###################### seq = file.readline() for ch in seq: ... ####################### Contrast this with: ####################### for line in file.readlines(): ... ####################### This has a *totally* different meaning. The "delta" is a single character in terms of the physical source code, but in terms of what the program means, high impact. From lists at mostrom.pp.se Tue Oct 7 00:08:03 2014 From: lists at mostrom.pp.se (=?UTF-8?Q?Jan_Erik_Mostr=C3=B6m?=) Date: Tue, 7 Oct 2014 00:08:03 +0200 Subject: [Tutor] Scaling photos Message-ID: I want to write a small script that scales photos. I want the scaled photos to keep all meta data available in the originals. I also want to keep this python only and easily installed on linux and OS X machines. What library would you people recommend for doing this? - jem From dyoo at hashcollision.org Tue Oct 7 00:16:23 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 6 Oct 2014 15:16:23 -0700 Subject: [Tutor] Scaling photos In-Reply-To: References: Message-ID: On Mon, Oct 6, 2014 at 3:08 PM, Jan Erik Mostr?m wrote: > I want to write a small script that scales photos. I want the scaled > photos to keep all meta data available in the originals. I also want > to keep this python only and easily installed on linux and OS X > machines. > > What library would you people recommend for doing this? Hi Jan, Python Imaging Library (PIL) is the first thing that comes to mind. http://www.pythonware.com/products/pil/ Good luck! From alan.gauld at btinternet.com Tue Oct 7 03:10:57 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Oct 2014 02:10:57 +0100 Subject: [Tutor] Scaling photos In-Reply-To: References: Message-ID: On 06/10/14 23:08, Jan Erik Mostr?m wrote: > I want to write a small script that scales photos. I want the scaled > photos to keep all meta data available in the originals. I also want > to keep this python only and easily installed on linux and OS X > machines. > > What library would you people recommend for doing this? Pillow Its the Python 3 version of PIL but works with 2.7 too I think. Most PIL tutorials will work for Pillow too. Also consider ImageMagick. Its usually available in the package system for Linux and can be downloaded for Macs too. It has a Python library. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From crk at godblessthe.us Tue Oct 7 01:55:02 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Mon, 6 Oct 2014 16:55:02 -0700 Subject: [Tutor] search/match file position q Message-ID: <05e101cfe1c0$f2adf190$d809d4b0$@us> Howdy I haven't been able to find an definitive answer. I am looking through a file(stream:<), for several matching strings. Match definitively starts at the beginning of the stream. As I search, or match, do I start over at the beginning of the stream for each match or do I start at the end of the last search/match? Apparently the file object knows where it is in a stream, but it is unclear whether line_location = readline() keeps track of that location. I see advantages to both protocols, but overall, I would prefer that the next search starts at the end of the last start TIA, Clayton -------------- next part -------------- An HTML attachment was scrubbed... URL: From pughpl at me.com Tue Oct 7 00:42:34 2014 From: pughpl at me.com (Phillip Pugh) Date: Mon, 06 Oct 2014 17:42:34 -0500 Subject: [Tutor] Suggestions Please Message-ID: I am trying to decide if Python is the right toolset for me. I do a lot of data analytics. Over the years I have used a lot of SQL and VBA, but the data sources are getting bigger. I am thinking Python may be what I need to use, but I am in the early stages of getting to know Python. Can you point me to a really good, intuitive resource for getting up to speed with data. Something that would help with the following. I have one text file that is 500,000 + records.. I need to read the file, move "structured" data around and then write it to a new file. The txt file has several data elements and is 300 characters per line. I am only interested in the first two fields. The first data element is 19 characters. The second data element is 6 characters. I want to rearrange the data by moving the 6 characters data in front of the 19 characters data and then write the 25 character data to a new file. I have spent some time digging for the correct resource, However being new to Python and the syntax for the language makes it slow going. I would like to see if I can speed up the learning curve. Hopefully can get some results quickly then I will know how deep I want to go into Python. Thanks in advance Phillip From davea at davea.name Tue Oct 7 03:56:49 2014 From: davea at davea.name (Dave Angel) Date: Mon, 6 Oct 2014 21:56:49 -0400 (EDT) Subject: [Tutor] search/match file position q References: <05e101cfe1c0$f2adf190$d809d4b0$@us> Message-ID: "Clayton Kirkwood" Wrote in message: > > haven?t been able to find an definitive answer You should make your question clearer. Are you asking what your code does, or what you should like it to do? Either way, you should start with some sample code, and refer to that in your questions. As it is, we can't tell whether you're searching a line at a time, whether you're using regex, index, or what. And please use text message, not html. -- DaveA From davea at davea.name Tue Oct 7 04:03:12 2014 From: davea at davea.name (Dave Angel) Date: Mon, 6 Oct 2014 22:03:12 -0400 (EDT) Subject: [Tutor] Suggestions Please References: Message-ID: Phillip Pugh Wrote in message: > I am trying to decide if Python is the right toolset for me. I do a lot of data analytics. Over the years I have used a lot of SQL and VBA, but the data sources are getting bigger. I am thinking Python may be what I need to use, but I am in the early stages of getting to know Python. Can you point me to a really good, intuitive resource for getting up to speed with data. Something that would help with the following. > > I have one text file that is 500,000 + records.. I need to read the file, move "structured" data around and then write it to a new file. The txt file has several data elements and is 300 characters per line. I am only interested in the first two fields. The first data element is 19 characters. The second data element is 6 characters. I want to rearrange the data by moving the 6 characters data in front of the 19 characters data and then write the 25 character data to a new file. > > I have spent some time digging for the correct resource, However being new to Python and the syntax for the language makes it slow going. I would like to see if I can speed up the learning curve. Hopefully can get some results quickly then I will know how deep I want to go into Python. > Open the two files, using the 'with' statement. Using a for loop, iterate over the input file. For each line, use substring operators to fetch each field. Then write second + first. Maybe 6 lines. If you can get most of this, show it to us, and tell us where you're stuck. -- DaveA From martin at linux-ip.net Tue Oct 7 06:17:15 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 6 Oct 2014 21:17:15 -0700 Subject: [Tutor] Suggestions Please In-Reply-To: References: Message-ID: Greetings Phillip, > I am trying to decide if Python is the right toolset for me. It might be. That depends on you and also the environment in which you operate. > I do a lot of data analytics. Over the years I have used a lot of > SQL and VBA, but the data sources are getting bigger. Data sets have a very annoying habit of growing. > I am thinking Python may be what I need to use, but I am in the > early stages of getting to know Python. Can you point me to a > really good, intuitive resource for getting up to speed with data. > Something that would help with the following. Ooof. So, the killer in your question is the word 'intuitive'. Intuitive for whom? That depends entirely on you. There are a variety of tutorials, and I point out one below, but I do not know if it will be intuitive for you. Something that is intuitive for one person is opaque to the next. > I have one text file that is 500,000 + records.. There's always somebody who has dealt with bigger files. 500k records (at only 300 chars per line)? I'd read that straight into memory and do something with it. Given recent CPUs and amounts of available RAM, the biggest cost I see here is disk (and possibly algorithm). > I need to read the file, move "structured" data around and then > write it to a new file. The txt file has several data elements and > is 300 characters per line. I am only interested in the first two > fields. The first data element is 19 characters. The second data > element is 6 characters. I want to rearrange the data by moving > the 6 characters data in front of the 19 characters data and then > write the 25 character data to a new file. Dave Angel gave you some suggestions on how to do start. I'll make an attempt at translating his English into a Python block for you. Specifically, in Python, he suggested something like this: with open('yourfile.txt') as f: for line in f: first, second = line[:19], line[19:19+6] print second, first # Python-2.x #print(second, first) # Python-3.x Try the above. Does that do what you expected? If not, then have a look at substring operations that you can do once you have open()'d up a file and have read a line of data into a variable, "line" above, which is of type string: https://docs.python.org/2/library/stdtypes.html#string-methods If you can control the serialized format of data on disk, that might help open up a much richer set of tools for you. Plain text has the benefit and liability that it is amazingly flexible. If you are accustomed to performing data analytics with SQL and VBA, then here are some tools to examine. For people accustomed to working with data analytics in R, the Python pandas toolkit is a great fit: http://pandas.pydata.org/pandas-docs/stable/tutorials.html http://pandas.pydata.org/ This sounds much more like strict text-handling than like data analytics, though. Some of us may be able to help you more if you have a specific known-format you deal with regularly. For example, Python has modules for handling JSON and CSV (or TSV) out of the box: https://docs.python.org/2/library/json.html https://docs.python.org/2/library/csv.html Given that many SQL implementations (e.g. MySQL, Postgres, Oracle, SQLite) can produce outputs in CSV format, you may find generating exported data from your SQL engine of choice and then importing using the csv library is easier than parsing a fixed format. Why did you quote the word "structured"? It almost seems that you do not like your (peculiar) fixed-width field format. If you have a follow-up question, please post a small relevant snippet of the Python, the data and explain what you expected. Anyway, good luck--I have found Python is a fantastic tool, readable and it grew with my sophistication. I still reach for SQL for many reasons, but I like the flexibility, richness and tools that Python offers me. -Martin P.S. The Python online documentation is pretty good.....though the language changed very little between Python-2.x and Python-3.x, there are a few stumbling blocks, so run 'python -V' to make sure you are reading the correct documentation: https://docs.python.org/2/ # -- Python-2.7.x https://docs.python.org/3/ # -- Python-3.4.x -- Martin A. Brown http://linux-ip.net/ From crk at godblessthe.us Tue Oct 7 07:58:23 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Mon, 6 Oct 2014 22:58:23 -0700 Subject: [Tutor] search/match file position q In-Reply-To: References: <05e101cfe1c0$f2adf190$d809d4b0$@us> Message-ID: <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> I was trying to keep it generic. Wrapped data file: SWKS23.27', line_in) #scan to SWKS"> in data #line, stock should be SWKS low_52 = re.search('.+wk52:low.+([\d\.]+)<', line_in) #want to pick up from #SWKS">, low_52 should be 23.27 I am trying to figure out if each re.match starts scanning at the beginning of the same line over and over or does each scan start at the end of the last match. It appears to start over?? This is stock: <_sre.SRE_Match object; span=(0, 47), match=' '> This is low_52: <_sre.SRE_Match object; span=(0, 502875), match=' If necessary, how do I pick up and move forward to the point right after the previous match? File.tell() and file.__sizeof__(), don't seem to play a useful role. TIA, Clayton From alan.gauld at btinternet.com Tue Oct 7 11:39:08 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Oct 2014 10:39:08 +0100 Subject: [Tutor] Suggestions Please In-Reply-To: References: Message-ID: On 06/10/14 23:42, Phillip Pugh wrote: > I am trying to decide if Python is the right toolset for me. > I do a lot of data analytics. It can almost certainly do what you want but there may be other tools that do it better. However, data analytics is quite vague. It depends on what kind of data and what kind of analysis. > Can you point me to a really good, intuitive resource intuitive depends on the student. But we also need to know what kind of data. Is it stored in flat files? in a SQL database(which one?) In a NoSQL database(which one?) Python can handle all of those but the tutorials involved will all be different. If you want a general introduction with some SQL database specifics you can try my tutorial(see sig). Whether you find it intuitive is another matter. > I have one text file that is 500,000 + records.. Thats not very big in modern computing terms. You could probably just read that straight into memory. > I need to read the file, What kind of file? A database file such as Foxpro? or Access? or a CSV export? Or something else? > move "structured" data around and then write it to a new file. What is structured about it? Fixed column width? Fixed relative position? Binary format? > The txt file has several data elements and is > 300 characters per line. > I am only interested in the first two fields. > The first data element is 19 characters. > The second data element is 6 characters. There are two ways in Python to extract 'columns' from a file. If you know the separators you can use either the csv module(best) or string.split() to create a list of fields. If its a fixed length record (with potentially no seperator) you can use string slicing. In your case that would be field1 = string[:19]; field2 = string[19:25] > I want to rearrange the data by moving the 6 characters data > in front of the 19 characters data Do you need a separator? > and then write the 25 character data to a new file. the reading and writing of the files is straightforward, any tutorial will show you that. > I have spent some time digging for the correct resource, > However being new to Python and the syntax for the language > makes it slow going. I would like to see if I can speed up > the learning curve. So far it sounds like you don't need any of the high powered data analysis tools like R or Pandas, you are just doing basic data extraction and manipulation. For that standard Python should be fine and most tutorials include all you need. If you look at mine the most relevant topics from the contents are: The raw materials - variables & data types Looping - basic loops in Python Branching - basic selection in python Handling Files - files Handling Text - text and possibly Working with Databases - using SQL in Python You probably should read the CSV module documentation too. I suspect it will do a lot of what you want. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Oct 7 11:47:00 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Oct 2014 10:47:00 +0100 Subject: [Tutor] search/match file position q In-Reply-To: <05e101cfe1c0$f2adf190$d809d4b0$@us> References: <05e101cfe1c0$f2adf190$d809d4b0$@us> Message-ID: On 07/10/14 00:55, Clayton Kirkwood wrote: > I haven't been able to find an definitive answer. There isn't one. Each technique has its place. It depends on what you are doing and why. And to answer that you need to ask a definitive question. > I am looking through a file(stream:<), for several matching strings. Using what? regex? Or string search methods? Or character by character? or a parser? > Match definitively starts at the beginning of the stream. Sounds like regex except regex doesn't work on streams it works on strings. You could read the string(s) from a stream but that's not what regex does. > As I search, or match, do I start over at the beginning of the stream > for each match or do I start at the end of the last > search/match? Whichever fits your design best. Usually using regex I just use findall() and let Python do the work. > Apparently the file object knows where it is in a stream, but > it is unclear whether line_location = readline() keeps track of that > location. All read operations move the cursor forward and the file keeps track of it. But that's all irrelevant for match() and search() they don't care about the file. > I see advantages to both protocols, but overall, I would prefer > that the next search starts at the end of the last start If that's what you want then that's what you should do. Now what is preventing you? Do you have code where you try to do it? If so we might see the problem. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Tue Oct 7 12:49:39 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Oct 2014 12:49:39 +0200 Subject: [Tutor] search/match file position q References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> Message-ID: Clayton Kirkwood wrote: > I was trying to keep it generic. > Wrapped data file: > data-model="name:DatumModel;id:null;" data-tmpl=""> data-ylk="cat:portfolio;cpos:1" > href="http://finance.yahoo.com/q?s=SWKS" > data-rapid_p="18">SWKS class="col-fiftytwo_week_low cell-raw:23.270000"> class="wrapper " > data-model="name:DatumModel;id:SWKS:qsi:wk52:low;" > data-tmpl="change:yfin.datum">23.27 class="col-prev_close cell-raw:58.049999"> class="wrapper " data-model="name:DatumMo Doesn't Yahoo make the data available as CSV? That would be the way to go then. Anyway, regular expressions are definitely the wrong tool here, and reading the file one line at a time only makes it worse. > import re, os > line_in = file.readline() # read in humongous html line > stock = re.search('\s*', line_in) > #scan to SWKS"> in data #line, stock should be SWKS > low_52 = re.search('.+wk52:low.+([\d\.]+)<', line_in) #want to > pick up from #SWKS">, low_52 should be 23.27 > > I am trying to figure out if each re.match starts scanning at the > beginning of the same line over and over or does each scan start at the > end of the last match. It appears to start over?? > > This is stock: > <_sre.SRE_Match object; span=(0, 47), match=' data-row-symbol="SWKS">'> This is low_52: > <_sre.SRE_Match object; span=(0, 502875), match=' data-row-symbol="SWKS"> > If necessary, how do I pick up and move forward to the point right after > the previous match? File.tell() and file.__sizeof__(), don't seem to play > a useful role. You should try BeautifulSoup. Let's play: >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup("""SWKS23.27""") >>> soup.find("tr") SWKS23.27 >>> tr = soup.find("tr") >>> tr["data-row-symbol"] 'SWKS' >>> tr.find_all("span") [SWKS, 23.27] >>> span = tr.find_all("span")[1] >>> span["data-model"] 'name:DatumModel;id:SWKS:qsi:wk52:low;' >>> span.text '23.27' Note that normally soup would hold the complete html and you'd need a few more iterations to get to the element of interest. From francois.dion at gmail.com Tue Oct 7 13:02:23 2014 From: francois.dion at gmail.com (Francois Dion) Date: Tue, 7 Oct 2014 07:02:23 -0400 Subject: [Tutor] Suggestions Please In-Reply-To: References: Message-ID: <2E58A5F4-12DB-4E57-A4E7-20895B028A41@gmail.com> El Oct 6, 2014, a las 6:42 PM, Phillip Pugh escribi?: > I am trying to decide if Python is the right toolset for me. I do a lot of data analytics. Over the years I have used a lot of SQL and VBA, but the data sources are getting bigger. I am thinking Python may be what I need to use, but I am in the early stages of getting to know Python. Can you point me to a really good, intuitive resource for getting up to speed with data. If you are doing data analytics, particularly iterative stuff, get to know ipython notebook and pandas. http://nbviewer.ipython.org/github/twiecki/financial-analysis-python-tutorial/blob/master/1.%20Pandas%20Basics.ipynb To have an overall feel for ipython notebook and python in general for matlab style notebooks: https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks Francois -- raspberry-python.blogspot.com - www.pyptug.org - @f_dion From ljetibo at gmail.com Tue Oct 7 13:30:58 2014 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Tue, 7 Oct 2014 13:30:58 +0200 Subject: [Tutor] Suggestions Please (Martin A. Brown) Message-ID: Hey, Most of your answers you already got. I'm just willing to share a short story of mine considering big data and python. I've done computer vision on images ~15Mb big each. There's 6 filters of which each can reach 7TB in total size. I ran detection algorithms per 2 filters at a time that totalled around 14TB. I've also had to read a lot of "side-data" in from SQL db's. I chose python for easy of writing and it's force-to-write-pretty-or-die syntax. I also chose it because it's more easily portable anywhere than any other languages are (some of them dependant even on the 3rd number of compiler version code!) I did all per-pixel operations with wrapped C code. That means I only did loops from 0 to width/height of the image with minimal calculations in C because the numerics there is just inherently faster than loops in python. With the new Python >3 versions a lot has been improved in that area and you no longer have to think about the implementation differences behind things like zip/izip, range/xrange, read/realine/readlines in example. Output files were easily killing 10GB in size. I used various generator expressions and iterators, that are heavily supported and encouraged in python>3 unlike python<3, and I have never run into space problems. And I have never had issues with RAM (swapping comes to mind). A short clarifications: generators are functions that behave like iterators, that means, for a generator reading a file you only allocate the memory the size of that 1 single line but are still able to preform calculations on them. These expressions are hardly ever longer than a line of code. And even better is that today you rarely have to write them yourself even, they've been written for you and hidden in the underbelly of python implementations. Combine the amazing compatibility python offers with other programs (how it's relatively easy to wrap and call outside programs in different languages, execute them and catch their return) and how easy it is to communicate with the OS in question (hardly any worries if you use python functions and not subprocess module) and how clean that code looks (compared i.e. if you ever had to pass a function as an object in C, or writing C documentation) and you're fairly well armed to combat anything. I've had my code run on Win, Ubuntu, Suse, I've run it on clusters (hybrids/IBMs) and I've never ever regretted that I chose to write my code in python. Now that we hopefully have the motivation question out of the way. There's only one way to learn anything. Set aside some free time, start coding, if you like it it's good for you. If you don't move on. One thing you will just have to learn when doing python, is how to learn really fast. All these things have pretty much been pre-written for you, but you have to get the info of where (numpy/scipy/swig in my case) and you're going to have to keep track of modules, after all that's where python really shines. I recommend books: "Learning python the hard way" and "Dive into python". also as a disclaimer, I've ran over a lot of things and some I've really bludgeoned in short don't hold any of the half-assed explanations it against me. > To: Phillip Pugh > Cc: Python Tutor Mailing List > Subject: Re: [Tutor] Suggestions Please > Message-ID: > Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII > >I am trying to decide if Python is the right toolset for me. I do a lot of data >analytics. Over the years I have used a lot of SQL and VBA, but the data >sources are getting bigger. I am thinking Python may be what I need to >use, but I am in the early stages of getting to know Python. Can you point >me to a really good, intuitive resource for getting up to speed with data. >Something that would help with the following. > >I have one text file that is 500,000 + records.. I need to read the file, move >"structured" data around and then write it to a new file. The txt file has >several data elements and is 300 characters per line. I am only interested >in the first two fields. The first data element is 19 characters. The second >data element is 6 characters. I want to rearrange the data by moving the 6 >characters data in front of the 19 characters data and then write the 25 >character data to a new file. > >I have spent some time digging for the correct resource, However being >new to Python and the syntax for the language makes it slow going. I >would like to see if I can speed up the learning curve. Hopefully can get >some results quickly then I will know how deep I want to go into Python. > >Thanks in advance > >Phillip From davea at davea.name Tue Oct 7 14:39:41 2014 From: davea at davea.name (Dave Angel) Date: Tue, 7 Oct 2014 08:39:41 -0400 (EDT) Subject: [Tutor] search/match file position q References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> Message-ID: "Clayton Kirkwood" Wrote in message: > I was trying to keep it generic. > Wrapped data file: > SWKS23.27', line_in) #scan to SWKS"> in data #line, stock should be SWKS > low_52 = re.search('.+wk52:low.+([\d\.]+)<', line_in) #want to pick up from #SWKS">, low_52 should be 23.27 > > I am trying to figure out if each re.match starts scanning at the beginning of the same line over and over or does each scan start at the end of the last match. It appears to start over?? > > This is stock: > <_sre.SRE_Match object; span=(0, 47), match=' '> > This is low_52: > <_sre.SRE_Match object; span=(0, 502875), match=' > If necessary, how do I pick up and move forward to the point right after the previous match? File.tell() and file.__sizeof__(), don't seem to play a useful role. > The best solution is ANYTHING but html scraping. If the website offers an api like csf, use it. Html is too prone to changing at the whim of the developers. If you must use html, get beautiful soup. Regex can mess up suddenly even if the developers don't change anything. Regex should only be used on html if you're the one generating the website, and you coordinate it to be parseable. If regex were the best solution you could read the following example pasted from the online docs. re.findall searches a string, not a file, so file position is irrelevant. The numbers below can be used to subscript your string, either for saving the results or for removing the part already searched. Something like line_in = line_in[span [0] +span [1]: ] Ref: https://docs.python.org/3.4/howto/regex.html findall() has to create the entire list before it can be returned as the result. The finditer() method returns a sequence of match object instances as an iterator: >>> >>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...') >>> iterator >>> for match in iterator: ... print(match.span()) ... (0, 2) (22, 24) (29, 31) -- DaveA From dillonrw at comcast.net Tue Oct 7 15:40:24 2014 From: dillonrw at comcast.net (Richard Dillon) Date: Tue, 7 Oct 2014 06:40:24 -0700 Subject: [Tutor] format command help Message-ID: <6B65AD97-1216-43D2-B84B-7E64C103BC7B@comcast.net> I create column headings using \t print('base1\tbase2\theight\tarea') and I would like the numbers to align with the headings. I think that I need to use format instead of doing this: print(A,' ',B,' ',C,' ',int(area1)) print(D,' ',E,' ',F,' ',int(area2)) but I don't know how. I've looked at code examples and I can't figure out how. Thanks From alan.gauld at btinternet.com Tue Oct 7 17:45:38 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Oct 2014 16:45:38 +0100 Subject: [Tutor] format command help In-Reply-To: <6B65AD97-1216-43D2-B84B-7E64C103BC7B@comcast.net> References: <6B65AD97-1216-43D2-B84B-7E64C103BC7B@comcast.net> Message-ID: On 07/10/14 14:40, Richard Dillon wrote: > I create column headings using \t > > print('base1\tbase2\theight\tarea') > > and I would like the numbers to align with the headings. > I think that I need to use format instead of doing this: Yes, using tabs is a bit hit and miss depemding on the length of the fields. If they are all similar sizes its OK but otherwise it gets messy. > print(A,' ',B,' ',C,' ',int(area1)) > print(D,' ',E,' ',F,' ',int(area2)) > > but I don't know how. At its simplest you just use braces like this: fmtString = "{}\t{}\t{}\t{}" print(fmtString.format('base1','base2',height','area')) And that gives you your header line then print(fmtString.format(A,B,C,int(area1)) gives you your first line and so on. By using a constant fmtString you avoid any risk of mixing things up. But formatting gives you more options. You can specify width and justification. By using a fixed width you are more likely to get the alignment consistent than by using tabs. Here is an example: fmtString="{:20}{:20}{:10}{:6}" Then your prints stay the same but the formatting will now use the fixed lengths you gave rather than rely on tabs. You can specify which end of the space you want the data by using the < and > justification characters. So to pull the integer to the left hand siode of its column use: fmtString="{:20}{:20}{:10}{:<6}" You can specify the type too but often you don't need to since Python will do conversion to string for you. There's lots more. Read the documentation on format and play with different format strings in the interpreter. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From crk at godblessthe.us Tue Oct 7 17:47:37 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Tue, 7 Oct 2014 08:47:37 -0700 Subject: [Tutor] search/match file position q In-Reply-To: References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> Message-ID: <06aa01cfe246$08799e90$196cdbb0$@us> !-----Original Message----- !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On !Behalf Of Peter Otten !Sent: Tuesday, October 07, 2014 3:50 AM !To: tutor at python.org !Subject: Re: [Tutor] search/match file position q ! !Clayton Kirkwood wrote: ! !> I was trying to keep it generic. !> Wrapped data file: !> data-model="name:DatumModel;id:null;" data- !tmpl=""> data-ylk="cat:portfolio;cpos:1" !> href="http://finance.yahoo.com/q?s=SWKS" !> data-rapid_p="18">SWKS class="col-fiftytwo_week_low cell- !raw:23.270000"> class="wrapper " !> data-model="name:DatumModel;id:SWKS:qsi:wk52:low;" !> data-tmpl="change:yfin.datum">23.27 class="col-prev_close cell-raw:58.049999"> class="wrapper " data-model="name:DatumMo ! !Doesn't Yahoo make the data available as CSV? That would be the way to !go then. Yes, Yahoo has a few columns that are csv, but I have maybe 15 fields that aren't provided. Besides, what fun would that be, I try to find tasks that allow me to expand my knowledge"<))) ! !Anyway, regular expressions are definitely the wrong tool here, and !reading the file one line at a time only makes it worse. Why is it making it only worse? I don't think a char by char would be helpful, the line happens to be very long, and I don't have a way of peeking around the corner to the next line so to speak. If I broke it into shorter strings, it would be much more onerous to jump over the end of the current to potentially many next strings. ! !> import re, os !> line_in = file.readline() ! # read in humongous html line !> stock = re.search('\s*', !line_in) !> #scan to SWKS"> in data ! #line, stock !should be SWKS !> low_52 = re.search('.+wk52:low.+([\d\.]+)<', line_in) !#want to !> pick up from ! #SWKS">, !low_52 should be 23.27 !> !> I am trying to figure out if each re.match starts scanning at the !> beginning of the same line over and over or does each scan start at !> the end of the last match. It appears to start over?? !> !> This is stock: !> <_sre.SRE_Match object; span=(0, 47), match=' data-row-symbol="SWKS">'> This is low_52: !> <_sre.SRE_Match object; span=(0, 502875), match=' ! data-row-symbol="SWKS"> !> If necessary, how do I pick up and move forward to the point right !> after the previous match? File.tell() and file.__sizeof__(), don't !> seem to play a useful role. ! !You should try BeautifulSoup. Let's play: ! !>>> from bs4 import BeautifulSoup !>>> soup = BeautifulSoup(""">> class="col-symbol !txt">SWKS23.27""") !>>> soup.find("tr") !SWKS23.27 !>>> tr = soup.find("tr") !>>> tr["data-row-symbol"] !'SWKS' !>>> tr.find_all("span") ![SWKS, 23.27] !>>> span = tr.find_all("span")[1] !>>> span["data-model"] !'name:DatumModel;id:SWKS:qsi:wk52:low;' !>>> span.text !'23.27' So, what makes regex wrong for this job? question still remains: does the search start at the beginning of the line each time or does it step forward from the last search? I will check out beautiful soup as suggested in a subsequent mail; I'd still like to finish this process:<}} Clayton From __peter__ at web.de Tue Oct 7 20:10:11 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Oct 2014 20:10:11 +0200 Subject: [Tutor] search/match file position q References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> Message-ID: Clayton Kirkwood wrote: > > > !-----Original Message----- > !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On > !Behalf Of Peter Otten > !Sent: Tuesday, October 07, 2014 3:50 AM > !To: tutor at python.org > !Subject: Re: [Tutor] search/match file position q > ! > !Clayton Kirkwood wrote: > ! > !> I was trying to keep it generic. > !> Wrapped data file: > !> !> data-model="name:DatumModel;id:null;" data- > !tmpl=""> !> data-ylk="cat:portfolio;cpos:1" > !> href="http://finance.yahoo.com/q?s=SWKS" > !> data-rapid_p="18">SWKS !> class="col-fiftytwo_week_low cell- > !raw:23.270000"> !> class="wrapper " > !> data-model="name:DatumModel;id:SWKS:qsi:wk52:low;" > !> data-tmpl="change:yfin.datum">23.27 !> class="col-prev_close cell-raw:58.049999"> !> class="wrapper " data-model="name:DatumMo > ! > !Doesn't Yahoo make the data available as CSV? That would be the way to > !go then. > > > Yes, Yahoo has a few columns that are csv, but I have maybe 15 fields that > aren't provided. Besides, what fun would that be, I try to find tasks that > allow me to expand my knowledge"<))) > > ! > !Anyway, regular expressions are definitely the wrong tool here, and > !reading the file one line at a time only makes it worse. > > > Why is it making it only worse? I don't think a char by char would be > helpful, the line happens to be very long, and I don't have a way of > peeking around the corner to the next line so to speak. If I broke it into > shorter strings, it would be much more onerous to jump over the end of the > current to potentially many next strings. I meant you should slurp in the whole file instead of reading it line after line. That way you'd at least have a chance to find elements that spread over more than one line like Example > !> import re, os > !> line_in = file.readline() > ! # read in humongous html line > !> stock = re.search('\s*', > !line_in) > !> #scan to SWKS"> in data > ! #line, stock > !should be SWKS > !> low_52 = re.search('.+wk52:low.+([\d\.]+)<', line_in) > !#want to > !> pick up from > ! #SWKS">, > !low_52 should be 23.27 > !> > !> I am trying to figure out if each re.match starts scanning at the > !> beginning of the same line over and over or does each scan start at > !> the end of the last match. It appears to start over?? > !> > !> This is stock: > !> <_sre.SRE_Match object; span=(0, 47), match=' !> data-row-symbol="SWKS">'> This is low_52: > !> <_sre.SRE_Match object; span=(0, 502875), match=' > ! !> data-row-symbol="SWKS"> > !> If necessary, how do I pick up and move forward to the point right > !> after the previous match? File.tell() and file.__sizeof__(), don't > !> seem to play a useful role. > ! > !You should try BeautifulSoup. Let's play: > ! > !>>> from bs4 import BeautifulSoup > !>>> soup = BeautifulSoup(""" !>>> span.text > !'23.27' > > > So, what makes regex wrong for this job? A regex doesn't understand the structure of an html document. For example you need to keep track of the nesting level manually to find the cells of the inner of two nested tables. > question still remains: does the > search start at the beginning of the line each time or does it step > forward from the last search? re.search() doesn't keep track of prior searches; whatever string you feed it (in your case a line cut out of an html document) is searched. > I will check out beautiful soup as suggested > in a subsequent mail; I'd still like to finish this process:<}} Do you say that when someone points out that you are eating your shoe? From dyoo at hashcollision.org Tue Oct 7 20:13:32 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 7 Oct 2014 11:13:32 -0700 Subject: [Tutor] search/match file position q In-Reply-To: <06aa01cfe246$08799e90$196cdbb0$@us> References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> Message-ID: > So, what makes regex wrong for this job? question still remains: does the > search start at the beginning of the line each time or does it step forward > from the last search? I will check out beautiful soup as suggested in a > subsequent mail; I'd still like to finish this process:<}} Mathematically, regular expressions can capture a certain class of text called the "regular languages". Regular languages have a few characteristics. As a concrete example of a limitation: you can't write a pattern that properly does parentheses matching with a regular expression alone. This isn't a challenge to your machismo: it's a matter of mathematics! For the precise details on the impossibility proof, you'd need to take a CS theory class, and in particular, learn about the "pumping lemma for regular expressions". Sipser's "Introduction to the Theory of Computation" has a good presentation. This is one reason why CS theory matters: it can tell you when some approach is not a good idea. :P HTML is not a regular language: it has nested substructure. The same problem about matching balanced parentheses is essentially that of matching start and end tags. So that's the objections from the purely mathematical point of view. This is not to say that regular expressions are useless: they work well for breaking down HTML into a sequence of tokens. If you only care about processing individual tokens at a time, regexes might be appropriate. They're just not the best tool for everything. From a practical point of view: HTML parsing libraries such as Beautiful Soup are nicer to work with than plain regular expressions. From juan0christian at gmail.com Tue Oct 7 21:08:33 2014 From: juan0christian at gmail.com (Juan Christian) Date: Tue, 7 Oct 2014 16:08:33 -0300 Subject: [Tutor] Using xml.etree ElementTree Message-ID: I have this XML scheme: http://steamcommunity.com/profiles/76561198084537782?xml=1 My code: self._xml = ElementTree.fromstring(requests.get(url + '?xml=1').content) print(self._xml.tag) > returns > profile It's working, the thing is that I don't know how to "navigate" inside the XML, I read the doc but everything there regarding key-value is on for-loops, I need something more direct, like: XML.steamID64 XML.steamID XML.onlineState XML.vacBanned ... Something like that, where I provide the key () and get the value (value). Is that possible? -------------- next part -------------- An HTML attachment was scrubbed... URL: From crk at godblessthe.us Tue Oct 7 21:40:54 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Tue, 7 Oct 2014 12:40:54 -0700 Subject: [Tutor] search/match file position q In-Reply-To: References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> Message-ID: <007201cfe266$9c5261e0$d4f725a0$@us> !> !> So, what makes regex wrong for this job? ! !A regex doesn't understand the structure of an html document. For !example !you need to keep track of the nesting level manually to find the cells !of !the inner of two nested tables. ! !> question still remains: does the !> search start at the beginning of the line each time or does it step !> forward from the last search? ! !re.search() doesn't keep track of prior searches; whatever string you !feed !it (in your case a line cut out of an html document) is searched. ! So, you are saying that each regex starts at the beginning of the long line? Is there a way to start the next search at the end of the last one? !> I will check out beautiful soup as suggested !> in a subsequent mail; I'd still like to finish this process:<}} ! !Do you say that when someone points out that you are eating your shoe? Depends on the flavor of the shoe:<))) Clayton From crk at godblessthe.us Tue Oct 7 21:53:19 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Tue, 7 Oct 2014 12:53:19 -0700 Subject: [Tutor] search/match file position q In-Reply-To: References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> Message-ID: <007c01cfe268$58b189a0$0a149ce0$@us> !-----Original Message----- !From: Danny Yoo [mailto:dyoo at hashcollision.org] !Sent: Tuesday, October 07, 2014 11:14 AM !To: Clayton Kirkwood !Cc: Python Tutor Mailing List !Subject: Re: [Tutor] search/match file position q ! !> So, what makes regex wrong for this job? question still remains: does !> the search start at the beginning of the line each time or does it !> step forward from the last search? I will check out beautiful soup as !> suggested in a subsequent mail; I'd still like to finish this !> process:<}} ! ! !Mathematically, regular expressions can capture a certain class of text !called the "regular languages". Regular languages have a few !characteristics. As a concrete example of a limitation: you can't write !a pattern that properly does parentheses matching with a regular !expression alone. ! !This isn't a challenge to your machismo: it's a matter of mathematics! ! For the precise details on the impossibility proof, you'd need to take !a CS theory class, and in particular, learn about the "pumping lemma for !regular expressions". Sipser's "Introduction to the Theory of !Computation" has a good presentation. This is one reason why CS theory !matters: it can tell you when some approach is not a good idea. !:P ! !HTML is not a regular language: it has nested substructure. The same !problem about matching balanced parentheses is essentially that of !matching start and end tags. ! !So that's the objections from the purely mathematical point of view. !This is not to say that regular expressions are useless: they work well !for breaking down HTML into a sequence of tokens. If you only care !about processing individual tokens at a time, regexes might be !appropriate. They're just not the best tool for everything. From a !practical point of view: HTML parsing libraries such as Beautiful Soup !are nicer to work with than plain regular expressions. In this case, I was able to determine which line I was interested in because it had a specific marker. From that point, I knew specific markers to look for for each desired field. I thought the desired parenthesis couple was assigned to the variable at the beginning of the match line. I thought that regex's Were meant to skip over unwanted detritus and grab only the desired match with in the parentheses. Wrong? TIA, Clayton From juan0christian at gmail.com Wed Oct 8 00:03:32 2014 From: juan0christian at gmail.com (Juan Christian) Date: Tue, 7 Oct 2014 19:03:32 -0300 Subject: [Tutor] Using xml.etree ElementTree In-Reply-To: References: Message-ID: On Tue, Oct 7, 2014 at 4:08 PM, Juan Christian wrote: > I have this XML scheme: > http://steamcommunity.com/profiles/76561198084537782?xml=1 > > My code: > > self._xml = ElementTree.fromstring(requests.get(url + '?xml=1').content) > print(self._xml.tag) > returns > profile > > It's working, the thing is that I don't know how to "navigate" inside the > XML, I read the doc but everything there regarding key-value is on > for-loops, I need something more direct, like: > > XML.steamID64 > XML.steamID > XML.onlineState > XML.vacBanned > ... > > Something like that, where I provide the key () and get the value > (value). Is that possible? > So.. I found that I can do it with 'self._xml[index].text', but that's not a good and 'visible' way to do things. Explicit is better than implicit, and this XML has some data that might or might not be there, so I can't trust in the index. -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Wed Oct 8 00:05:07 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Tue, 7 Oct 2014 15:05:07 -0700 Subject: [Tutor] search/match file position q In-Reply-To: <007201cfe266$9c5261e0$d4f725a0$@us> References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> <007201cfe266$9c5261e0$d4f725a0$@us> Message-ID: Good afternoon Clayton, > !A regex doesn't understand the structure of an html document. For > !example > !you need to keep track of the nesting level manually to find the cells > !of > !the inner of two nested tables. > ! > !> question still remains: does the > !> search start at the beginning of the line each time or does it step > !> forward from the last search? > ! > !re.search() doesn't keep track of prior searches; whatever string you > !feed > !it (in your case a line cut out of an html document) is searched. > ! > > So, you are saying that each regex starts at the beginning of the > long line? Is there a way to start the next search at the end of > the last one? Well, it depends on how you are using the re module (if you really want to do that). Have a look at: https://docs.python.org/2/library/re.html#re.RegexObject.search But...I'll add my voice to the admonition against using regex here. Consider the following events that could happen in the future after you have labored over your program and are able to get it to work, based on today's HTML. 1. Somebody inserts a line-break in the middle of the element you were searching for with regex. 2. A week from now, somebody runs 'tidy' on the HTML or changes or removes the the line endings. 3. Somebody adds an HTML comment which causes your regex to match. These are the first three reasons that occur to me for why regex is the wrong tool for the job here, given that you know precisely the format of the data. It is HTML. The good thing is that there are other tools for processing HTML. Anyway, if you want to use regexes, nobody can stop you, so see below, a bit of nonsense text which you can search for 2 distinct instances of the string "ei" [0]. > !> I will check out beautiful soup as suggested > !> in a subsequent mail; I'd still like to finish this process:<}} > !Do you say that when someone points out that you are eating your shoe? > Depends on the flavor of the shoe:<))) Root beer float. -Martin [0] If you really, really want to use regex, here's an example of how to keep track of where you last sought, and how to search from that place in the string. from __future__ import print_function import re def main(): s = 'Wo lattenzaun aneinander erhaltenen vorpfeifen grasgarten.' pattern = re.compile('ei', re.IGNORECASE) matched = pattern.search(s,0) while matched: endpos = matched.end() print(matched.group(0), matched.start(), matched.end()) matched = pattern.search(s, endpos) -- Martin A. Brown http://linux-ip.net/ From dyoo at hashcollision.org Wed Oct 8 00:50:46 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 7 Oct 2014 15:50:46 -0700 Subject: [Tutor] Using xml.etree ElementTree In-Reply-To: References: Message-ID: >> It's working, the thing is that I don't know how to "navigate" inside the >> XML, I read the doc but everything there regarding key-value is on >> for-loops, I need something more direct, like: Hi Juan, I think you're looking for: http://effbot.org/zone/element.htm#searching-for-subelements where you should be able to do something like: sml._xml.findtext('steamID64') to get the text "76561198084537782". Let's check: ################################################################### >>> import xml.etree.ElementTree >>> import urllib >>> f = urllib.urlopen("http://steamcommunity.com/profiles/76561198084537782?xml=1") >>> tree = xml.etree.ElementTree.ElementTree(file=f) >>> tree.findtext('steamID64') '76561198084537782' ################################################################### Yup, this looks relatively straightforward. Let us know if you hit any snags. From alan.gauld at btinternet.com Wed Oct 8 01:18:32 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 08 Oct 2014 00:18:32 +0100 Subject: [Tutor] search/match file position q In-Reply-To: <007c01cfe268$58b189a0$0a149ce0$@us> References: <05e101cfe1c0$f2adf190$d809d4b0$@us> <060a01cfe1f3$b52ea7f0$1f8bf7d0$@us> <06aa01cfe246$08799e90$196cdbb0$@us> <007c01cfe268$58b189a0$0a149ce0$@us> Message-ID: On 07/10/14 20:53, Clayton Kirkwood wrote: > In this case, I was able to determine which line I > was interested in because it had a > specific marker. > From that point, I knew specific markers to look > for for each desired field. > I thought the desired parenthesis couple was assigned > to the variable at the beginning of the match line. I'm not quite sure what you mean by that. > I thought that regex's Were meant to skip over > unwanted detritus and grab only the desired > match with in the parentheses. Not quite. They don't "skip over" unwanted detritus. The detritus is either part of the matched pattern or it's not. regex find defined patterns they don't find undefined patterns. The skipping over happens as a side effect of looking for the pattern, it's not a design goal of itself. And the reason regex are often described as a weapon of last resort is because it's infernally difficult to define exactly the right pattern for anything non-trivial. And especially in things like HTML where it may even be impossible to get it right. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From juan0christian at gmail.com Wed Oct 8 02:02:45 2014 From: juan0christian at gmail.com (Juan Christian) Date: Tue, 7 Oct 2014 21:02:45 -0300 Subject: [Tutor] Using xml.etree ElementTree In-Reply-To: References: Message-ID: On Tue, Oct 7, 2014 at 7:50 PM, Danny Yoo wrote: > Hi Juan, > > I think you're looking for: > > http://effbot.org/zone/element.htm#searching-for-subelements > > where you should be able to do something like: > > sml._xml.findtext('steamID64') > > to get the text "76561198084537782". > > > Let's check: > > ################################################################### > >>> import xml.etree.ElementTree > >>> import urllib > >>> f = urllib.urlopen(" > http://steamcommunity.com/profiles/76561198084537782?xml=1") > >>> tree = xml.etree.ElementTree.ElementTree(file=f) > >>> tree.findtext('steamID64') > '76561198084537782' > ################################################################### > > > Yup, this looks relatively straightforward. Let us know if you hit any > snags. > Exactly what I needed, thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at mostrom.pp.se Wed Oct 8 07:47:13 2014 From: lists at mostrom.pp.se (=?UTF-8?Q?Jan_Erik_Mostr=C3=B6m?=) Date: Wed, 8 Oct 2014 07:47:13 +0200 Subject: [Tutor] Scaling photos In-Reply-To: References: Message-ID: Thanks for the suggestions On Tue, Oct 7, 2014 at 3:10 AM, Alan Gauld wrote: > On 06/10/14 23:08, Jan Erik Mostr?m wrote: >> >> I want to write a small script that scales photos. I want the scaled >> photos to keep all meta data available in the originals. I also want >> to keep this python only and easily installed on linux and OS X >> machines. >> >> What library would you people recommend for doing this? > > > Pillow > Its the Python 3 version of PIL but works with 2.7 too I think. > Most PIL tutorials will work for Pillow too. > > Also consider ImageMagick. Its usually available in the package system for > Linux and can be downloaded for Macs too. It has a Python library. > > HTH > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Wed Oct 8 08:49:21 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 08 Oct 2014 07:49:21 +0100 Subject: [Tutor] Suggestions Please In-Reply-To: References: Message-ID: <5434DE71.8020009@btinternet.com> On 08/10/14 02:02, Phillip Pugh wrote: > with open("InputTest.txt","r") as f: > with open("Outputt.txt", "w") as fw: > for line in f: > first,second = line[:32], line[32:37] > > if first.isspace()== False: > fw.write (second.strip()+ first.strip()+"\n") > > f.close() > fw.close() > > You don't need the close()s the with construct does that for you automatically. Alan G. From pughpl at me.com Wed Oct 8 03:02:09 2014 From: pughpl at me.com (Phillip Pugh) Date: Tue, 07 Oct 2014 20:02:09 -0500 Subject: [Tutor] Suggestions Please In-Reply-To: References: Message-ID: Thank you All!! I am impressed with the support. It was very helpful and timely. I was able to put together a script to do what I wanted. I know now that I wont be wasting time learning Python. As with any language, it is about understanding the syntax. As I mentioned before, I want to make sure I am focusing my time on something useful and this was a big help. Here is what I came up with (with your help) . I expect there is a more efficient way to do it, but hey... it was my first try with data. And FYI, I work with over one hundred data sources, I wanted to test on something small. Phillip with open("InputTest.txt","r") as f: with open("Outputt.txt", "w") as fw: for line in f: first,second = line[:32], line[32:37] if first.isspace()== False: fw.write (second.strip()+ first.strip()+"\n") f.close() fw.close() On Oct 7, 2014, at 4:39 AM, Alan Gauld wrote: > On 06/10/14 23:42, Phillip Pugh wrote: >> I am trying to decide if Python is the right toolset for me. > > I do a lot of data analytics. > > It can almost certainly do what you want but there may be other > tools that do it better. However, data analytics is quite vague. > It depends on what kind of data and what kind of analysis. > > > Can you point me to a really good, intuitive resource > > intuitive depends on the student. > But we also need to know what kind of data. > Is it stored in flat files? > in a SQL database(which one?) > In a NoSQL database(which one?) > Python can handle all of those but the tutorials involved > will all be different. > > If you want a general introduction with some SQL database > specifics you can try my tutorial(see sig). Whether you > find it intuitive is another matter. > >> I have one text file that is 500,000 + records.. > > Thats not very big in modern computing terms. > You could probably just read that straight into memory. > >> I need to read the file, > > What kind of file? A database file such as Foxpro? > or Access? or a CSV export? Or something else? > >> move "structured" data around and then write it to a new file. > > What is structured about it? Fixed column width? > Fixed relative position? Binary format? > >> The txt file has several data elements and is > > 300 characters per line. > >> I am only interested in the first two fields. > > The first data element is 19 characters. > > The second data element is 6 characters. > > There are two ways in Python to extract 'columns' from a file. > > If you know the separators you can use either the csv module(best) > or string.split() to create a list of fields. > > If its a fixed length record (with potentially no seperator) > you can use string slicing. In your case that would be > field1 = string[:19]; field2 = string[19:25] > >> I want to rearrange the data by moving the 6 characters data > > in front of the 19 characters data > > Do you need a separator? > >> and then write the 25 character data to a new file. > > the reading and writing of the files is straightforward, any tutorial will show you that. > >> I have spent some time digging for the correct resource, > > However being new to Python and the syntax for the language > > makes it slow going. I would like to see if I can speed up > > the learning curve. > > So far it sounds like you don't need any of the high powered data analysis tools like R or Pandas, you are just doing basic data extraction and manipulation. For that standard Python should > be fine and most tutorials include all you need. > > If you look at mine the most relevant topics from the contents > are: > The raw materials - variables & data types > Looping - basic loops in Python > Branching - basic selection in python > Handling Files - files > Handling Text - text > > and possibly > Working with Databases - using SQL in Python > > You probably should read the CSV module documentation too. > I suspect it will do a lot of what you want. > > HTH > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From davea at davea.name Wed Oct 8 10:38:18 2014 From: davea at davea.name (Dave Angel) Date: Wed, 8 Oct 2014 04:38:18 -0400 (EDT) Subject: [Tutor] Suggestions Please References: Message-ID: Phillip Pugh Wrote in message: > Thank you All!! > > I am impressed with the support. It was very helpful and timely. I was able to put together a script to do what I wanted. I know now that I wont be wasting time learning Python. As with any language, it is about understanding the syntax. As I mentioned before, I want to make sure I am focusing my time on something useful and this was a big help. > > Here is what I came up with (with your help) . I expect there is a more efficient way to do it, but hey... it was my first try with data. And FYI, I work with over one hundred data sources, I wanted to test on something small. > > Phillip > > > with open("InputTest.txt","r") as f: > with open("Outputt.txt", "w") as fw: > for line in f: > first,second = line[:32], line[32:37] > > if first.isspace()== False: > fw.write (second.strip()+ first.strip()+"\n") > > f.close() > fw.close() > > > Some comments, first the minor stuff. No need to close f or fw; the with statements already took care of it before you got to those lines. And the if statement would normally be spelled if not first.isspace (): But the important thing is that by stripping those two strings, you're almost certainly damaging the output data. Unless you know some specific reason why you can get away with it. Also, please don't top-post. > > On Oct 7, 2014, at 4:39 AM, Alan Gauld wrote: > >> On 06/10/14 23:42, Phillip Pugh wrote: >>> I am trying to decide if Python is the right toolset for me. >> > I do a lot of data analytics. > >> >>> move "structured" data around and then write it to a new file. >> >> What is structured about it? Fixed column width? >> Fixed relative position? Binary format? >> >>> The txt file has several data elements and is >> > 300 characters per line. >> >>> I am only interested in the first two fields. >> > The first data element is 19 characters. >> > The second data element is 6 characters. >> >> >>> I want to rearrange the data by moving the 6 characters data >> > in front of the 19 characters data >> >> Do you need a separator? >> >>> and then write the 25 character data to a new file. -- DaveA From robertvstepp at gmail.com Wed Oct 8 16:56:02 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 Oct 2014 09:56:02 -0500 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? Message-ID: About two years ago I wrote my most ambitious program to date, a hodge-podge collection of proprietary scripting, perl and shell files that collectively total about 20k lines of code. Amazingly it actually works and has saved my colleagues and I much time and effort. At the time I created this mess, I was playing "guess the correct proprietary syntax to do something" and "hunt and peck perl" games and squeezing this programming work into brief snippets of time away from what I am actually paid to do. I did not give much thought to design at the time and knew I would regret it later, which is now today! So now in my current few snippets of time I wish to redesign this program from scratch and make it much, ... , much easier to maintain the code and update the data tables, which change from time to time. And now that I have some version of python available on all of our current Solaris 10 systems (python versions 2.4.4 and 2.6.4), it seems like a fine time to (finally!) do some serious python learning. Right now I have separated my data into their own files. Previously I had integrated the data with my source code files (Horrors!). Currently, a snippet from one of these data files is: NUMBER_FX:ONE; DATA_SOURCE:Timmerman; RELEASE_DATE:(11-2012); SERIAL_ROI:Chiasm; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; SERIAL_ROI:Optic_Nerve_R; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; SERIAL_ROI:Optic_Nerve_L; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; [...] PARALLEL_ROI:Lungs_Bilateral; CRITICAL_VOLUME_CC:1500.0; CRITICAL_VOLUME_DOSE_MAX_GY:7.0; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; PARALLEL_ROI:Lungs_Bilateral; CRITICAL_VOLUME_CC:1000.0; CRITICAL_VOLUME_DOSE_MAX_GY:7.6; V8GY:< 37.0%; V20GY: ; MAX_MEAN_DOSE: ; PARALLEL_ROI:Liver; CRITICAL_VOLUME_CC:700.0; CRITICAL_VOLUME_DOSE_MAX_GY:11.0; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; PARALLEL_ROI:Renal_Cortex_Bilateral; CRITICAL_VOLUME_CC:200.0; CRITICAL_VOLUME_DOSE_MAX_GY:9.5; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; [EOF] I just noticed that copying from my data file into my Google email resulted in all extra spaces being condensed into a single space. I do not know why this has just happened. Note that there are no tab characters. The [...] indicates omitted lines of serial tissue data and [EOF] just notes the end-of-file. I am far from ready to write any code at this point. I am trying to organize my data files, so that they will be easy to use by the programs that will process the data and also to be easily updated every time these data values get improved upon. For the latter, I envision writing a second program to enable anyone to update the data tables when we are given new values. But until that second program gets written, the data files would have to be opened and edited manually, which is why I have labels included in all-caps ending in a colon. This is so the editor will know what he is editing. So, basically the actual data fields fall between ":" and ";" . String representations of numbers will need to get converted to floats by the program. Some fields containing numbers are of a form like "< 0.2 cc" . These will get copied as is into a GUI display, while the "0.2" will be used in a computation and/or comparison. Also notice that in each data file there are two distinct groupings of records--one for serial tissue (SERIAL_ROI:) and one for parallel tissue (PARALLEL_ROI). The fields used are different for each grouping. Also, notice that some fields will have no values, but in other data files they will have values. And finally the header line at the top of the file identifies for what number of fractions (FX) the data is to be used for as well as the source of the data and date that the data was released by that source. Finally the questions! Will I easily be able to use python to parse this data as currently structured, or do I need to restructure this? I am not at the point where I am aware of what possibilities python offers to handle these data files. Also, my efforts to search the 'net did not turn up anything that really clicked for me as the way to go. I could not seem to come up with a search string that would bring up what I was really interested in: What are the best practices for organizing plain text data? Thanks! -- boB From joel.goldstick at gmail.com Wed Oct 8 17:02:42 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 8 Oct 2014 11:02:42 -0400 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: On Wed, Oct 8, 2014 at 10:56 AM, boB Stepp wrote: > About two years ago I wrote my most ambitious program to date, a > hodge-podge collection of proprietary scripting, perl and shell files > that collectively total about 20k lines of code. Amazingly it actually > works and has saved my colleagues and I much time and effort. At the > time I created this mess, I was playing "guess the correct proprietary > syntax to do something" and "hunt and peck perl" games and squeezing > this programming work into brief snippets of time away from what I am > actually paid to do. I did not give much thought to design at the time > and knew I would regret it later, which is now today! So now in my > current few snippets of time I wish to redesign this program from > scratch and make it much, ... , much easier to maintain the code and > update the data tables, which change from time to time. And now that I > have some version of python available on all of our current Solaris 10 > systems (python versions 2.4.4 and 2.6.4), it seems like a fine time > to (finally!) do some serious python learning. > > Right now I have separated my data into their own files. Previously I > had integrated the data with my source code files (Horrors!). > Currently, a snippet from one of these data files is: > > NUMBER_FX:ONE; DATA_SOURCE:Timmerman; RELEASE_DATE:(11-2012); > > SERIAL_ROI:Chiasm; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; > MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; > SERIAL_ROI:Optic_Nerve_R; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; > MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; > SERIAL_ROI:Optic_Nerve_L; TEST_VOLUME:< 0.2 cc; VOLUME_MAX_GY:8.0; > MAX_PT_DOSE_GY:10.0; MAX_MEAN_DOSE: ; > > [...] > > PARALLEL_ROI:Lungs_Bilateral; CRITICAL_VOLUME_CC:1500.0; > CRITICAL_VOLUME_DOSE_MAX_GY:7.0; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; > PARALLEL_ROI:Lungs_Bilateral; CRITICAL_VOLUME_CC:1000.0; > CRITICAL_VOLUME_DOSE_MAX_GY:7.6; V8GY:< 37.0%; V20GY: ; MAX_MEAN_DOSE: > ; > PARALLEL_ROI:Liver; CRITICAL_VOLUME_CC:700.0; > CRITICAL_VOLUME_DOSE_MAX_GY:11.0; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; > PARALLEL_ROI:Renal_Cortex_Bilateral; CRITICAL_VOLUME_CC:200.0; > CRITICAL_VOLUME_DOSE_MAX_GY:9.5; V8GY: ; V20GY: ; MAX_MEAN_DOSE: ; > [EOF] > > I just noticed that copying from my data file into my Google email > resulted in all extra spaces being condensed into a single space. I do > not know why this has just happened. Note that there are no tab > characters. The [...] indicates omitted lines of serial tissue data > and [EOF] just notes the end-of-file. > > I am far from ready to write any code at this point. I am trying to > organize my data files, so that they will be easy to use by the > programs that will process the data and also to be easily updated > every time these data values get improved upon. For the latter, I > envision writing a second program to enable anyone to update the data > tables when we are given new values. But until that second program > gets written, the data files would have to be opened and edited > manually, which is why I have labels included in all-caps ending in a > colon. This is so the editor will know what he is editing. So, > basically the actual data fields fall between ":" and ";" . String > representations of numbers will need to get converted to floats by the > program. Some fields containing numbers are of a form like "< 0.2 cc" > . These will get copied as is into a GUI display, while the "0.2" will > be used in a computation and/or comparison. Also notice that in each > data file there are two distinct groupings of records--one for serial > tissue (SERIAL_ROI:) and one for parallel tissue (PARALLEL_ROI). The > fields used are different for each grouping. Also, notice that some > fields will have no values, but in other data files they will have > values. And finally the header line at the top of the file identifies > for what number of fractions (FX) the data is to be used for as well > as the source of the data and date that the data was released by that > source. > > Finally the questions! Will I easily be able to use python to parse > this data as currently structured, or do I need to restructure this? I > am not at the point where I am aware of what possibilities python > offers to handle these data files. Also, my efforts to search the 'net > did not turn up anything that really clicked for me as the way to go. > I could not seem to come up with a search string that would bring up > what I was really interested in: What are the best practices for > organizing plain text data? > > Thanks! > > -- > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor It looks like you have csv like data. Except you have a semicolon as a separator. Look at the csv module. That should work for you -- Joel Goldstick http://joelgoldstick.com From robertvstepp at gmail.com Wed Oct 8 17:47:36 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 Oct 2014 10:47:36 -0500 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: On Wed, Oct 8, 2014 at 10:02 AM, Joel Goldstick wrote: [...] > It looks like you have csv like data. Except you have a semicolon as > a separator. Look at the csv module. That should work for you > Joel, will the labels (like SERIAL_ROI:) cause me difficulties? I will need to strip these off to get to the actual data. But when I implement a data editor later, these labels will be needed (I think.). I just now have located the documentation for python 2.4.4. It does not seem to be as friendly or easy to read as for the current version documentation. But I will persevere... -- boB From alan.gauld at btinternet.com Wed Oct 8 18:27:23 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 08 Oct 2014 17:27:23 +0100 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: On 08/10/14 16:47, boB Stepp wrote: >> It looks like you have csv like data. Except you have a semicolon as >> a separator. Look at the csv module. That should work for you >> > Joel, will the labels (like SERIAL_ROI:) cause me difficulties? I will > need to strip these off to get to the actual data. But when I > implement a data editor later, these labels will be needed (I think.). You will get the name:value fields as strings. You can then use string.split(':') to separate them and then either lose the label or convert them to a dictionary. If its not too big a task you could even convert the data structure to JSON which is quite a close match to what you have now and the json module will help you read/write to them. > I just now have located the documentation for python 2.4.4. It does > not seem to be as friendly or easy to read as for the current version > documentation. Shouldn't be that much different. What kind of anomalies are you seeing? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From aakazemaa at gmail.com Wed Oct 8 18:51:52 2014 From: aakazemaa at gmail.com (Kazem Ardekanian) Date: Wed, 8 Oct 2014 09:51:52 -0700 Subject: [Tutor] Welcome to the "Tutor" mailing list In-Reply-To: References: Message-ID: Hi, I was reading IDLE tutorial. The following document dose not exist. http://www.python.org/idle/doc/ It was referenced from https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/ Thanks, Kazem On Wed, Oct 8, 2014 at 9:48 AM, wrote: > Welcome to the Tutor at python.org mailing list! This list is for folks > who want to ask (and/or answer) questions from folks who wish to learn > how to program with Python. Feel free to ask even the most basic of > questions -- that's what the list is for! > > For best results when asking a question on this list: - Try to write > some code to solve your problem - Show the code you have written - > Describe what the code does and what you want it to do - If the code > generates an error, copy and paste the entire error message, including > the traceback, into your email. - Tell us what OS and Python version > you are using. > > - Don't ask us to do your homework. - Don't assume we know what you > are talking about. If you are having trouble with a third-party > library, include a link to the library home page. > > When replying to a posting: - Use Reply All to reply to the entire > list - Don't top post - put your reply after the text to which you are > replying > > For all posts: - Format your email as plain text, not HTML > > > To post to this list, send your message to: > > tutor at python.org > > General information about the mailing list is at: > > https://mail.python.org/mailman/listinfo/tutor > > If you ever want to unsubscribe or change your options (eg, switch to > or from digest mode, change your password, etc.), visit your > subscription page at: > > https://mail.python.org/mailman/options/tutor/aakazemaa%40gmail.com > > You can also make such adjustments via email by sending a message to: > > Tutor-request at python.org > > with the word `help' in the subject or body (don't include the > quotes), and you will get back a message with instructions. > > You must know your password to change your options (including changing > the password, itself) or to unsubscribe without confirmation. It is: > > 123zak > > Normally, Mailman will remind you of your python.org mailing list > passwords once every month, although you can disable this if you > prefer. This reminder will also include instructions on how to > unsubscribe or change your account options. There is also a button on > your options page that will email your current password to you. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From robertvstepp at gmail.com Wed Oct 8 19:51:08 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 Oct 2014 12:51:08 -0500 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: On Wed, Oct 8, 2014 at 11:27 AM, Alan Gauld wrote: > On 08/10/14 16:47, boB Stepp wrote: > >>> It looks like you have csv like data. Except you have a semicolon as >>> a separator. Look at the csv module. That should work for you >>> >> Joel, will the labels (like SERIAL_ROI:) cause me difficulties? I will >> need to strip these off to get to the actual data. But when I >> implement a data editor later, these labels will be needed (I think.). > > > You will get the name:value fields as strings. > You can then use string.split(':') to separate them and > then either lose the label or convert them to a dictionary. Ah! Thanks. That should work well. > If its not too big a task you could even convert the data > structure to JSON which is quite a close match to what you > have now and the json module will help you read/write > to them. I am not seeing JSON listed among python's standard libraries for version 2.4.4. Is this something that has to be independently installed? >> I just now have located the documentation for python 2.4.4. It does >> not seem to be as friendly or easy to read as for the current version >> documentation. > > > Shouldn't be that much different. > What kind of anomalies are you seeing? Compare the current library index, https://docs.python.org/2/library/index.html, with the 2.4.4 one, https://docs.python.org/release/2.4.4/lib/lib.html . On the former I was able to immediately find the csv module with a quick scroll, but in the latter I found myself using my browser's find function to locate the link. The other improvement that the newer presentation has that the older does not, is an actual brief example along with the description in the module contents. Perhaps these are minor points... -- boB From martin at linux-ip.net Wed Oct 8 21:19:11 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Wed, 8 Oct 2014 12:19:11 -0700 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: Good afternoon, >> If its not too big a task you could even convert the data >> structure to JSON which is quite a close match to what you >> have now and the json module will help you read/write >> to them. I would agree with the JSON recommendation (until your data set grows to more than 10GB in size). Also, if you are operating with JSON, you can add elements and delete elements and the serialization bits don't care. This makes it more dynamic than a typical csv/tsv format. The application still has to know about the data type, of course. > I am not seeing JSON listed among python's standard libraries for > version 2.4.4. Is this something that has to be independently > installed? Yes. The json module was 'only' added in Python 2.6. If you wanted to operate on JSON in Python 2.4, you had to use something called simplejson [0]. According to this post [0], the standard library json module is a(n older) release of the simplejson module. Anyway, when I found myself stuck in Python-2.4 land (on the stock Python shipped with CentOS-5.x, for example), I often saw and wrote snippets like this [1]: try: # -- if Python-2.6+, it's in STDLIB import json except ImportError: # -- Python-2.4, simplejson import simplejson as json And, then the rest of the program can operate just the same, regardless of which one you used. Good luck, -Martin [0] https://pypi.python.org/pypi/simplejson/ [1] http://stackoverflow.com/questions/712791/what-are-the-differences-between-json-and-simplejson-python-modules -- Martin A. Brown http://linux-ip.net/ From danny.yoo at gmail.com Wed Oct 8 21:16:47 2014 From: danny.yoo at gmail.com (Danny Yoo) Date: Wed, 8 Oct 2014 12:16:47 -0700 Subject: [Tutor] Welcome to the "Tutor" mailing list In-Reply-To: References: Message-ID: > I was reading IDLE tutorial. > The following document dose not exist. > http://www.python.org/idle/doc/ > > It was referenced from https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/ > Ok. I'll correct the broken link as soon as I have time. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Oct 8 22:45:46 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 8 Oct 2014 13:45:46 -0700 Subject: [Tutor] Welcome to the "Tutor" mailing list In-Reply-To: References: Message-ID: On Wed, Oct 8, 2014 at 12:16 PM, Danny Yoo wrote: > >> I was reading IDLE tutorial. >> The following document dose not exist. >> http://www.python.org/idle/doc/ >> >> It was referenced from >> https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/ >> > > Ok. I'll correct the broken link as soon as I have time. Thanks. Link should be corrected now to: https://docs.python.org/2/library/idle.html. From robertvstepp at gmail.com Wed Oct 8 23:07:39 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 Oct 2014 16:07:39 -0500 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: On Wed, Oct 8, 2014 at 2:19 PM, Martin A. Brown wrote: > > Good afternoon, > >>> If its not too big a task you could even convert the data >>> structure to JSON which is quite a close match to what you >>> have now and the json module will help you read/write >>> to them. Looking at some examples of JSON-structured data, I see that I formatted my data very similarly, as Alan noted. Purely a lucky coincidence! > > I would agree with the JSON recommendation (until your data set grows to > more than 10GB in size). Also, if you are operating with JSON, you can add > elements and delete elements and the serialization bits don't care. This > makes it more dynamic than a typical csv/tsv format. The application still > has to know about the data type, of course. > My data files are rather small and should remain so, about 30 to 40 lines at the most. Definitely forever less than 100 lines for each file. This is because there are only so many human organs in the vicinity of our radiation treatments that we might want to track dose for. >> I am not seeing JSON listed among python's standard libraries for version >> 2.4.4. Is this something that has to be independently installed? > > > Yes. The json module was 'only' added in Python 2.6. If you wanted to > operate on JSON in Python 2.4, you had to use something called simplejson > [0]. According to this post [0], the standard library json module is a(n > older) release of the simplejson module. > I looked at [0] and it states that it is backwards compatible to version 2.5, which is one iteration higher than my lowest installed python version at work. They provide a link to an older simplejson that is from the python 2.2 era, but state that this should be done only as a last resort. > Anyway, when I found myself stuck in Python-2.4 land (on the stock Python > shipped with CentOS-5.x, for example), I often saw and wrote snippets like > this [1]: > > try: # -- if Python-2.6+, it's in STDLIB > import json > except ImportError: # -- Python-2.4, simplejson > import simplejson as json > > And, then the rest of the program can operate just the same, regardless of > which one you used. > Is there a typo on [0]? Will it in fact work for python 2.4? > > [0] https://pypi.python.org/pypi/simplejson/ > [1] > http://stackoverflow.com/questions/712791/what-are-the-differences-between-json-and-simplejson-python-modules > -- boB From martin at linux-ip.net Wed Oct 8 23:53:07 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Wed, 8 Oct 2014 14:53:07 -0700 Subject: [Tutor] How best to structure a plain text data file for use in program(s) and later updating with new data? In-Reply-To: References: Message-ID: Good afternoon again, > I looked at [0] and it states that it is backwards compatible to > version 2.5, which is one iteration higher than my lowest installed > python version at work. They provide a link to an older simplejson > that is from the python 2.2 era, but state that this should be done > only as a last resort. > Is there a typo on [0]? Will it in fact work for python 2.4? Probably not. I don't recall what version of simplejson I used with Python-2.4. Note: If they provide a version that will work back to Python-2.2 and the next option is something that only works with Python-2.5+, then the obvious choice is to use the old version of software. The developers of simplejson (and most developers) will probably not want to support very old releases of their software, which is why they suggest using this as a last resort. If you can install or use a newer Python, then maybe that's a better option for you. If you can not do so, then take this older version of simplejson. You are at that place of last resort to which the simplejson authors allude. How do you like it at that resort? Would I want to go on vacation there? -Martin -- Martin A. Brown http://linux-ip.net/ From felisha.lawrence at gmail.com Thu Oct 9 01:58:38 2014 From: felisha.lawrence at gmail.com (Felisha Lawrence) Date: Wed, 8 Oct 2014 19:58:38 -0400 Subject: [Tutor] Renaming Files in Directory Message-ID: Hello, I have the following program import os path = '/Users/felishalawrence/testswps/vol1' for file in os.listdir(path): newFile = path+file[:file.rindex("v")]+"v20" print newFile and I want to output the results of the 'newFile' variable into the directory specified by the 'path' variable. There are current files in this directory, but I would like tho replace them with different names. Can anyone point me in the right direction? Thanks, Felisha Lawrence -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawrence at gmail.com (email) -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Oct 9 11:36:18 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Oct 2014 11:36:18 +0200 Subject: [Tutor] Renaming Files in Directory References: Message-ID: Felisha Lawrence wrote: > I have the following program > import os > > path = '/Users/felishalawrence/testswps/vol1' > for file in os.listdir(path): > newFile = path+file[:file.rindex("v")]+"v20" > > print newFile > and I want to output the results of the 'newFile' variable into the > directory specified by the 'path' variable. There are current files in > this directory, but I would like tho replace them with different names. > Can anyone point me in the right direction? So you can no longer defer that dreadful task? ;) To rename a file you need its old and its new name, both preferrably with their complete path, for example for old_name in os.listdir(path): old_file = os.path.join(path, old_name) new_name = old_name[:old_name.rindex("v")] + "v20" new_file = os.path.join(path, new_name) os.rename(old_file, new_file) If there are only files containing a "v" in your /Users/felishalawrence/testswps/vol1 folder the above should already work. Here are a few ideas to make it more robust (or rather less brittle): Verify that old_name contains a "v" using the `in` operator Verify that old_file is a file using os.path.isfile() Verify that new_file doesn't exist with os.path.exists() From alan.gauld at btinternet.com Thu Oct 9 13:02:34 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 09 Oct 2014 12:02:34 +0100 Subject: [Tutor] Renaming Files in Directory In-Reply-To: References: Message-ID: On 09/10/14 00:58, Felisha Lawrence wrote: > Hello, > I have the following program > > > import os > > path = '/Users/felishalawrence/testswps/vol1' > for file in os.listdir(path): > newFile = path+file[:file.rindex("v")]+"v20" > > print newFile > > and I want to output the results of the 'newFile' variable into the > directory specified by the 'path' variable. You want the os.rename function. Also you should use os.path.join() to create the path rather than string addition. It will ensure the correct separators are used for the OS. You might also want to look at glob.glob() rather than listdir to get a listing of files matching a wildcard pattern. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From fomcl at yahoo.com Thu Oct 9 20:32:01 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 9 Oct 2014 18:32:01 +0000 (UTC) Subject: [Tutor] alternative Python 2.6 install? Message-ID: <1529317176.81747.1412879521526.JavaMail.yahoo@jws10743.mail.gq1.yahoo.com> Hi, I need to install Python 2.6 on my Debian system to check some code.*) What is the easiest way to do this? Simply "sudo apt-get install python2.6"? I know I can also compile it and then do make altinstall, but I prefer apt-get. I am kinda paranoid that I might accidentally change my system Python version. Thank you! Regards, Albert-Jan *) albertjan at debian:~$ uname -a Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux albertjan at debian:~$ python -c "import sys; print sys.version_info" sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From wbecerra1 at gmail.com Thu Oct 9 20:38:02 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Thu, 9 Oct 2014 20:38:02 +0200 Subject: [Tutor] (no subject) Message-ID: I'm new to programming. Started reading the book 'How to think like a computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter 3.4 Math functions. When I write the following code: import maths; decibel = math.log10 (17.0); angle = 1.5; height = math.sin(angle); print height; I get the following error: Traceback (most recent call last): File "C:/Python27/test", line 1, in import maths; ImportError: No module named maths I don't know what I'm doing wrong? >From what I've read the maths module is supposed to come with the python installation package. I'm using a windows 8 operating system python 2.7.8 please help? -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Fri Oct 10 02:03:23 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 9 Oct 2014 20:03:23 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On Oct 9, 2014 8:00 PM, "William Becerra" wrote: > > I'm new to programming. Started reading the book 'How to think like a computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter 3.4 Math functions. > > When I write the following code: > > import maths; import math You added s > decibel = math.log10 (17.0); > angle = 1.5; > height = math.sin(angle); > print height; > > I get the following error: > > Traceback (most recent call last): > File "C:/Python27/test", line 1, in > import maths; > ImportError: No module named maths > > I don't know what I'm doing wrong? > From what I've read the maths module is supposed to come with the python installation package. > I'm using a windows 8 operating system > python 2.7.8 > please help? > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Fri Oct 10 02:13:51 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 9 Oct 2014 17:13:51 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: Hi there and welcome! > import maths; > decibel = math.log10 (17.0); > angle = 1.5; > height = math.sin(angle); > print height; > > Traceback (most recent call last): > File "C:/Python27/test", line 1, in > import maths; > ImportError: No module named maths Oops! It's a nice error report, though! Python tried to locate a module called 'maths' and was not able to find it. What happens if you try: import math N.B. You say 'import maths'--assume that this import succeeded. A few lines later, there's a line 'math.log10(17.0)' which seems to be trying to use something from a module called 'math' not 'maths'. > I don't know what I'm doing wrong? Computers are so picky. > From what I've read the maths module is supposed to come with the python > installation package. The 'math' library is a standard library module for quite awhile now. Here's a possibly useful online link, which describes that module: https://docs.python.org/2/library/math.html This is just more documentation support, in addition to the book you are reading. > I'm using a windows 8 operating system > python 2.7.8 > please help? One other issue I might point out. The semicolon at the end of the line (statement) is a feature of other programming languages with which you may be familiar (C, Java, Perl), but it is not necessary and, in fact, discouraged in Python. So, rid yourself of the semicolons and enjoy the benefits of a trivially cleaner syntax. Enjoy! -Martin P.S. Thanks for your clear question and letting us know your OS and Python version, as well. -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Fri Oct 10 02:36:25 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Oct 2014 01:36:25 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 09/10/14 19:38, William Becerra wrote: > import maths; Python, like most languages speaks American English so its math not maths. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Fri Oct 10 05:57:24 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 9 Oct 2014 22:57:24 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! Message-ID: I am hoping to save other people the grief I just worked through. I wanted to run both Python 2 and 3 on my windows PC, and, after googling this topic found that with Python 3.3 or later one could easily do both. So I merrily installed Python 3.4.2 first and then Python 2.7.8. A Python 3 program that had been working fine suddenly stopped working. After working down to a test portion of code that isolated the culprit I realized my Python 3 program was being interpreted by Python 2. I soon found that installing Python 2 first and then 3 enabled both to happily coexist. If there was a mention about the order of installation anywhere during my searches, I missed it. Anyway, I hope that my experience helps some other newbie who wants to play around with both major versions. -- boB From wbecerra1 at gmail.com Fri Oct 10 05:55:38 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Fri, 10 Oct 2014 05:55:38 +0200 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: It is working now. Thank you everyone. It was very helpfull. On Fri, Oct 10, 2014 at 2:36 AM, Alan Gauld wrote: > On 09/10/14 19:38, William Becerra wrote: > > import maths; >> > > Python, like most languages speaks American English > so its math not maths. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ljetibo at gmail.com Fri Oct 10 09:24:41 2014 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Fri, 10 Oct 2014 09:24:41 +0200 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! (boB Stepp) Message-ID: In the lieu of the same thing. I did an install of both python 2.6 and 3.3 on Win7. The key was to start using virtualenv (http://docs.python-guide.org/en/latest/dev/virtualenvs/) and set it up to the python install of my choice with: virtualenv -p /usr/bin/python2.7 venv although virtualenv has somewhat mixed reviews, some say it's good some don't really like it, I find that it makes handling of python versions really easy and I do recommend it. Upsides are: clean enviroment every time, you get to use pip even in Win. Downsides: sometimes is a bugger to install. You want to isolate each virtualenv by not using local packages, that means for each virtual env you want to install all the packages from scratch, but some packages don't really want to install easily in virtualenv (matplotlib I'm looking at you!). The --relocatable flag to can be a real nightmare, don't know if they fixed it as of recently.... As far as it goes without virtualenv, it's best to structure your code so that it's terminal friendly even in Win, and then start each with C:\python2.6\python program.py or whatever your full path may be. > ------------------------------ > > Message: 7 > Date: Thu, 9 Oct 2014 22:57:24 -0500 > From: boB Stepp > To: tutor > Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows > 7 Pro 64-bit: Install Python 2.7 FIRST! > Message-ID: > > Content-Type: text/plain; charset=UTF-8 > > I am hoping to save other people the grief I just worked through. I > wanted to run both Python 2 and 3 on my windows PC, and, after > googling this topic found that with Python 3.3 or later one could > easily do both. So I merrily installed Python 3.4.2 first and then > Python 2.7.8. A Python 3 program that had been working fine suddenly > stopped working. After working down to a test portion of code that > isolated the culprit I realized my Python 3 program was being > interpreted by Python 2. I soon found that installing Python 2 first > and then 3 enabled both to happily coexist. If there was a mention > about the order of installation anywhere during my searches, I missed > it. Anyway, I hope that my experience helps some other newbie who > wants to play around with both major versions. > > -- > boB > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 128, Issue 22 > ************************************** From wolfgang.maier at biologie.uni-freiburg.de Fri Oct 10 09:05:35 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Fri, 10 Oct 2014 09:05:35 +0200 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: Message-ID: <5437853F.5050309@biologie.uni-freiburg.de> On 10/10/2014 05:57 AM, boB Stepp wrote: > I am hoping to save other people the grief I just worked through. I > wanted to run both Python 2 and 3 on my windows PC, and, after > googling this topic found that with Python 3.3 or later one could > easily do both. So I merrily installed Python 3.4.2 first and then > Python 2.7.8. A Python 3 program that had been working fine suddenly > stopped working. After working down to a test portion of code that > isolated the culprit I realized my Python 3 program was being > interpreted by Python 2. It would help if you could share details about how you tried to run the Python 3 program (command line call, double-click, Python launcher for Windows, ..). > I soon found that installing Python 2 first > and then 3 enabled both to happily coexist. If there was a mention > about the order of installation anywhere during my searches, I missed > it. Anyway, I hope that my experience helps some other newbie who > wants to play around with both major versions. > From henry at sa-hk.com Fri Oct 10 10:32:09 2014 From: henry at sa-hk.com (Henry) Date: Fri, 10 Oct 2014 16:32:09 +0800 Subject: [Tutor] Tutor Digest, Vol 128, Issue 22 In-Reply-To: References: Message-ID: Hi I am new to programming. After I created two text files(the text file is most consist of numbers), its class is "", how can I compare this class with two text files? Please give me a hint which area I should look under? Set? List? Thanks Henry On Fri, Oct 10, 2014 at 12:04 PM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Renaming Files in Directory (Alan Gauld) > 2. alternative Python 2.6 install? (Albert-Jan Roskam) > 3. (no subject) (William Becerra) > 4. Re: (no subject) (Joel Goldstick) > 5. Re: (no subject) (Martin A. Brown) > 6. Re: (no subject) (Alan Gauld) > 7. Installing both Python 2.7 and Python 3.4 on Windows 7 Pro > 64-bit: Install Python 2.7 FIRST! (boB Stepp) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 09 Oct 2014 12:02:34 +0100 > From: Alan Gauld > To: tutor at python.org > Subject: Re: [Tutor] Renaming Files in Directory > Message-ID: > Content-Type: text/plain; charset=windows-1252; format=flowed > > On 09/10/14 00:58, Felisha Lawrence wrote: > > Hello, > > I have the following program > > > > > > import os > > > > path = '/Users/felishalawrence/testswps/vol1' > > for file in os.listdir(path): > > newFile = path+file[:file.rindex("v")]+"v20" > > > > print newFile > > > > and I want to output the results of the 'newFile' variable into the > > directory specified by the 'path' variable. > > You want the os.rename function. > > Also you should use os.path.join() to create the path rather than string > addition. It will ensure the correct separators are used > for the OS. > > You might also want to look at glob.glob() rather than listdir > to get a listing of files matching a wildcard pattern. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > > ------------------------------ > > Message: 2 > Date: Thu, 9 Oct 2014 18:32:01 +0000 (UTC) > From: Albert-Jan Roskam > To: Python Tutor Mailing List > Subject: [Tutor] alternative Python 2.6 install? > Message-ID: > < > 1529317176.81747.1412879521526.JavaMail.yahoo at jws10743.mail.gq1.yahoo.com> > > Content-Type: text/plain; charset=UTF-8 > > Hi, > > > I need to install Python 2.6 on my Debian system to check some code.*) > What is the easiest way to do this? Simply "sudo apt-get install > python2.6"? I know I can also compile it and then do make altinstall, but I > prefer apt-get. I am kinda paranoid that I might accidentally change my > system Python version. > > > > Thank you! > > > > Regards, > > Albert-Jan > > > *) > > albertjan at debian:~$ uname -a > Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux > albertjan at debian:~$ python -c "import sys; print sys.version_info" > sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > All right, but apart from the sanitation, the medicine, education, wine, > public order, irrigation, roads, a > > fresh water system, and public health, what have the Romans ever done for > us? > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > ------------------------------ > > Message: 3 > Date: Thu, 9 Oct 2014 20:38:02 +0200 > From: William Becerra > To: tutor at python.org > Subject: [Tutor] (no subject) > Message-ID: > Tiax3CcaQQ at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > I'm new to programming. Started reading the book 'How to think like a > computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter > 3.4 Math functions. > > When I write the following code: > > import maths; > decibel = math.log10 (17.0); > angle = 1.5; > height = math.sin(angle); > print height; > > I get the following error: > > Traceback (most recent call last): > File "C:/Python27/test", line 1, in > import maths; > ImportError: No module named maths > > I don't know what I'm doing wrong? > >From what I've read the maths module is supposed to come with the python > installation package. > I'm using a windows 8 operating system > python 2.7.8 > please help? > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20141009/5670d430/attachment-0001.html > > > > ------------------------------ > > Message: 4 > Date: Thu, 9 Oct 2014 20:03:23 -0400 > From: Joel Goldstick > Cc: tutor at python.org > Subject: Re: [Tutor] (no subject) > Message-ID: > NrkYw6uNtAYkfttot+Ot3x1taF3oa5ew0kEWYDSPw at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > On Oct 9, 2014 8:00 PM, "William Becerra" wrote: > > > > I'm new to programming. Started reading the book 'How to think like a > computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter > 3.4 Math functions. > > > > When I write the following code: > > > > import maths; > import math > > You added s > > > decibel = math.log10 (17.0); > > angle = 1.5; > > height = math.sin(angle); > > print height; > > > > I get the following error: > > > > Traceback (most recent call last): > > File "C:/Python27/test", line 1, in > > import maths; > > ImportError: No module named maths > > > > I don't know what I'm doing wrong? > > From what I've read the maths module is supposed to come with the python > installation package. > > I'm using a windows 8 operating system > > python 2.7.8 > > please help? > > > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20141009/787fbf3e/attachment-0001.html > > > > ------------------------------ > > Message: 5 > Date: Thu, 9 Oct 2014 17:13:51 -0700 > From: "Martin A. Brown" > To: William Becerra > Cc: tutor at python.org > Subject: Re: [Tutor] (no subject) > Message-ID: > Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed > > > Hi there and welcome! > > > import maths; > > decibel = math.log10 (17.0); > > angle = 1.5; > > height = math.sin(angle); > > print height; > > > > Traceback (most recent call last): > > File "C:/Python27/test", line 1, in > > import maths; > > ImportError: No module named maths > > Oops! It's a nice error report, though! Python tried to locate a > module called 'maths' and was not able to find it. > > What happens if you try: > > import math > > N.B. You say 'import maths'--assume that this import succeeded. A > few lines later, there's a line 'math.log10(17.0)' which seems to be > trying to use something from a module called 'math' not 'maths'. > > > I don't know what I'm doing wrong? > > Computers are so picky. > > > From what I've read the maths module is supposed to come with the python > > installation package. > > The 'math' library is a standard library module for quite awhile > now. Here's a possibly useful online link, which describes that > module: > > https://docs.python.org/2/library/math.html > > This is just more documentation support, in addition to the book you > are reading. > > > I'm using a windows 8 operating system > > python 2.7.8 > > please help? > > One other issue I might point out. The semicolon at the end of the > line (statement) is a feature of other programming languages with > which you may be familiar (C, Java, Perl), but it is not necessary > and, in fact, discouraged in Python. > > So, rid yourself of the semicolons and enjoy the benefits of a > trivially cleaner syntax. > > Enjoy! > > -Martin > > P.S. Thanks for your clear question and letting us know your OS and > Python version, as well. > > -- > Martin A. Brown > http://linux-ip.net/ > > > ------------------------------ > > Message: 6 > Date: Fri, 10 Oct 2014 01:36:25 +0100 > From: Alan Gauld > To: tutor at python.org > Subject: Re: [Tutor] (no subject) > Message-ID: > Content-Type: text/plain; charset=windows-1252; format=flowed > > On 09/10/14 19:38, William Becerra wrote: > > > import maths; > > Python, like most languages speaks American English > so its math not maths. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > > ------------------------------ > > Message: 7 > Date: Thu, 9 Oct 2014 22:57:24 -0500 > From: boB Stepp > To: tutor > Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows > 7 Pro 64-bit: Install Python 2.7 FIRST! > Message-ID: > < > CANDiX9LG2y7XNGLsruqjNtY7GZYN5VWEm-E2sha1o0LLqB2Xsw at mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > I am hoping to save other people the grief I just worked through. I > wanted to run both Python 2 and 3 on my windows PC, and, after > googling this topic found that with Python 3.3 or later one could > easily do both. So I merrily installed Python 3.4.2 first and then > Python 2.7.8. A Python 3 program that had been working fine suddenly > stopped working. After working down to a test portion of code that > isolated the culprit I realized my Python 3 program was being > interpreted by Python 2. I soon found that installing Python 2 first > and then 3 enabled both to happily coexist. If there was a mention > about the order of installation anywhere during my searches, I missed > it. Anyway, I hope that my experience helps some other newbie who > wants to play around with both major versions. > > -- > boB > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 128, Issue 22 > ************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Oct 10 11:55:35 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Oct 2014 10:55:35 +0100 Subject: [Tutor] Tutor Digest, Vol 128, Issue 22 In-Reply-To: References: Message-ID: On 10/10/14 09:32, Henry wrote: > I am new to programming. Welcome to tutor. Please set a meaningful subject line in your messages. It helps people doing a search and will attract interested readers so you get more chance of help. > After I created two text files(the text file is most consist of > numbers), its class is "", how can I compare > this class with two text files? I'm not really sure what you mean. How did you "create" the text files? On the hard disk using a text editor? Or by opening the files in python using open()? The _io.TextWrapper is just the class that python uses to model text files, you should hardly ever need to know anything about it. You should never need to be comparing it to actual files - I'm not even sure what that would mean... You can compare two text file objects, but that's rarely meaningful. Or you can compare the contents of two text files, which is more common. For the latter there is the difflib module to help. > Please give me a hint which area I should look under? Set? List? You need to explain in more detail what you are trying to do. It might be good to give short example data, say a couple of lines from each file and what you expect the output to look like. Also any code you have written, because it shows us the way you are tackling the problem. Finally always include the fill text of any errors, plus a note of the python version and OS you are using too. > On Fri, Oct 10, 2014 at 12:04 PM, > wrote: > > Send Tutor mailing list submissions to > tutor at python.org And do not send the full text of the digest. We've all seen the messages already and some people pay by the byte so it costs them money. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From fomcl at yahoo.com Fri Oct 10 13:08:49 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 10 Oct 2014 11:08:49 +0000 (UTC) Subject: [Tutor] alternative Python 2.6 install? In-Reply-To: <1529317176.81747.1412879521526.JavaMail.yahoo@jws10743.mail.gq1.yahoo.com> References: <1529317176.81747.1412879521526.JavaMail.yahoo@jws10743.mail.gq1.yahoo.com> Message-ID: <770191121.141799.1412939329664.JavaMail.yahoo@jws10762.mail.gq1.yahoo.com> ----- Original Message ----- > From: Albert-Jan Roskam > To: Python Tutor Mailing List > Cc: > Sent: Thursday, October 9, 2014 8:32 PM > Subject: [Tutor] alternative Python 2.6 install? > > Hi, > > > I need to install Python 2.6 on my Debian system to check some code.*) What is > the easiest way to do this? Simply "sudo apt-get install python2.6"? I > know I can also compile it and then do make altinstall, but I prefer apt-get. I > am kinda paranoid that I might accidentally change my system Python version. Ok, I decided to take the altinstall route anyway. I am now a getting message 'failed to find necessary bits to build these modules', even though I did install a whole bunch of packages (zlib1g is the first one that causes problems). Does anyone know how to solve this? I don't understand why e.g. 'zlib' still is missing while I clearly installed it! Alternatively: which Ubuntu (or Debian) version was the last version to use Python 2.6? I could simply install it in VirtualBox... Lame, but I am in kind of a hurry. This is what I did (outcommented lines are what won't work, or commands that are pointless at this point): sudo apt-get install libsqlite3-dev libbz2-dev libgdbm-dev libncurses5-dev tk-dev zlib1g-dev wget https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz tar -zxvf Python-2.6.8.tgz cd Python-2.6.8/ ./configure --prefix=/usr/local make # [a] see 'failed stuff' below #sudo make altinstall #mkvirtualenv -p /usr/local/bin/python2.6 python26 # ImportError: No module named zlib #workon python26 #pip install mysql-python # [a] Failed stuff Failed to find the necessary bits to build these modules: _bsddb _curses _curses_panel _hashlib _sqlite3 _ssl bsddb185 bz2 dbm dl gdbm imageop linuxaudiodev ossaudiodev readline sunaudiodev zlib To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: crypt nis running build_scripts creating build/scripts-2.6 copying and adjusting /home/albertjan/Downloads/Python-2.6.8/Tools/scripts/pydoc -> build/scripts-2.6 copying and adjusting /home/albertjan/Downloads/Python-2.6.8/Tools/scripts/idle -> build/scripts-2.6 copying and adjusting /home/albertjan/Downloads/Python-2.6.8/Tools/scripts/2to3 -> build/scripts-2.6 copying and adjusting /home/albertjan/Downloads/Python-2.6.8/Lib/smtpd.py -> build/scripts-2.6 changing mode of build/scripts-2.6/pydoc from 644 to 755 changing mode of build/scripts-2.6/idle from 644 to 755 changing mode of build/scripts-2.6/2to3 from 644 to 755 changing mode of build/scripts-2.6/smtpd.py from 644 to 755 thanks in advance! Albert-Jan From fomcl at yahoo.com Fri Oct 10 14:36:40 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 10 Oct 2014 12:36:40 +0000 (UTC) Subject: [Tutor] alternative Python 2.6 install? In-Reply-To: <770191121.141799.1412939329664.JavaMail.yahoo@jws10762.mail.gq1.yahoo.com> References: <770191121.141799.1412939329664.JavaMail.yahoo@jws10762.mail.gq1.yahoo.com> Message-ID: <1652645073.149046.1412944600267.JavaMail.yahoo@jws10781.mail.gq1.yahoo.com> ----- Original Message ----- > From: Albert-Jan Roskam > To: Albert-Jan Roskam ; Python Tutor Mailing List > Cc: > Sent: Friday, October 10, 2014 1:08 PM > Subject: Re: [Tutor] alternative Python 2.6 install? > > > > ----- Original Message ----- > >> From: Albert-Jan Roskam >> To: Python Tutor Mailing List >> Cc: >> Sent: Thursday, October 9, 2014 8:32 PM >> Subject: [Tutor] alternative Python 2.6 install? >> >> Hi, >> >> >> I need to install Python 2.6 on my Debian system to check some code.*) What > is >> the easiest way to do this? Simply "sudo apt-get install > python2.6"? I >> know I can also compile it and then do make altinstall, but I prefer > apt-get. I >> am kinda paranoid that I might accidentally change my system Python > version. > > > Ok, I decided to take the altinstall route anyway. I am now a getting message > 'failed to find necessary bits to build these modules', even though I > did install a whole bunch of packages (zlib1g is the first one that causes > problems). Does anyone know how to solve this? I don't understand why e.g. > 'zlib' still is missing while I clearly installed it! > > Alternatively: which Ubuntu (or Debian) version was the last version to use > Python 2.6? I could simply install it in VirtualBox... Lame, but I am in kind of > a hurry. > > > This is what I did (outcommented lines are what won't work, or commands that > are pointless at this point): > > sudo apt-get install libsqlite3-dev libbz2-dev libgdbm-dev libncurses5-dev > tk-dev zlib1g-dev > wget https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz > tar -zxvf Python-2.6.8.tgz > cd Python-2.6.8/ > ./configure --prefix=/usr/local > make # [a] see 'failed stuff' below > #sudo make altinstall > #mkvirtualenv -p /usr/local/bin/python2.6 python26 # ImportError: No module > named zlib > #workon python26 > #pip install mysql-python The first of the symlinks solved the zlib problem. The there was a problem with _sha256... ln -s /lib/x86_64-linux-gnu/libz.so /lib/libz.so ln -s /lib/x86_64-linux-gnu/libssl.so /lib/libssl.so ln -s /lib/x86_64-linux-gnu/libcrypt.so /lib/libcrypt.so ln -s /lib/x86_64-linux-gnu/libcrypto.so /lib/libcrypto.so ln -s /lib/x86_64-linux-gnu/libbz2.so /lib/libbz2.so ln -s /lib/x86_64-linux-gnu/libgdbm.so /lib/libgdbm.so ln -s /lib/x86_64-linux-gnu/libcurses.so /lib/libcurses.so ln -s /lib/x86_64-linux-gnu/libsqlite3.so /lib/libsqlite3.so OK, I gave up. I used Vagrant to install a Ubuntu 10.0.4 LTS virtualbox (which has Python 2.6 by default). Now I can SSH to that machine and do whatever I want. Sheesh. I am *still* very much interested to hear what might have gone wrong, though. Albert-Jan From robertvstepp at gmail.com Fri Oct 10 14:43:47 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 10 Oct 2014 07:43:47 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: <5437853F.5050309@biologie.uni-freiburg.de> References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Fri, Oct 10, 2014 at 2:05 AM, Wolfgang Maier wrote: > On 10/10/2014 05:57 AM, boB Stepp wrote: >> >> I am hoping to save other people the grief I just worked through. I >> wanted to run both Python 2 and 3 on my windows PC, and, after >> googling this topic found that with Python 3.3 or later one could >> easily do both. So I merrily installed Python 3.4.2 first and then >> Python 2.7.8. A Python 3 program that had been working fine suddenly >> stopped working. After working down to a test portion of code that >> isolated the culprit I realized my Python 3 program was being >> interpreted by Python 2. > > > It would help if you could share details about how you tried to run the > Python 3 program (command line call, double-click, Python launcher for > Windows, ..). > Initially, I ran the program in question by double-clicking on its icon, which was my normal way of running it. This is when I realized I had a problem. The program would run without error, but give erroneous print and input statement results. I next put Python 3 into my path variable, but the same error persisted. From the command line I found that if I ran it normally (With Python 3 still in the path like it was on my previous 3.4.1 installation.), meaning python program_name.py it behaved the same way as double-clicking on the icon. However, if I ran it as py -3 program_name.py then all was well. Apparently Python 2.7.8 became the system default installation if I installed it last, but after uninstalling both and installing Python 3.4.2 last, it became the default. Or, at least when double-clicked on it ran normally. I have since added shebang lines to my programs specifying Python 3. I hope I have not forgotten any relevant details! -- boB From niyanaxx95 at gmail.com Fri Oct 10 17:46:09 2014 From: niyanaxx95 at gmail.com (niyanaxx95 at gmail.com) Date: Fri, 10 Oct 2014 15:46:09 +0000 Subject: [Tutor] =?utf-8?q?Tile_Code_Program?= Message-ID: <5437ffaf.632c8c0a.8d0f.6e95@mx.google.com> This is the prompt: Write an algorithm / pseudocode that: ? Reads in a two integers; the width and length of a room. i. The valid range for the width (x-axis) of the room is between 100 and 1000 pixels. ii. The valid range for the length (y-axis) of the room is between 100 and 900 pixels. ? Validate the inputs, prompting the user to try again if they enter an invalid integer. ? Compute the number of tiles in each row (the number of columns) ? Compute the number of tiles in each column (the number of rows) ? Print the number of columns ? Print the number of rows ? Print the total number of tiles needed ? Print the gap at the end of each row ? Print the gap at the end of each column ? Print the pattern of tiles centered in a graphics window My Code: #This is a program to compute tiles # Constants # from graphics import GraphicsWindow TILESIZE = 20 roomWidth = 100.0 roomLength = 90.0 while roomWidth < 100 or roomWidth > 1000: roomWidth = float(input("Please enter a room width between 100 and 1000: ")) if roomWidth >= 100 or roomWidth <= 1000: print("Invalid entry") while roomLength < 100 or roomLength > 900: roomLength = float(input("Please enter a room length between 100 and 900: ")) if roomLength >= 100 or roomLength <= 900: print("Invalid entry.") win = GraphicsWindow(roomWidth, roomLength) canvas = win.canvas() #Calculate the number of pairs of tiles # the number of pairs = interger part of (total width - tile width) / (2 * tile width) numberOfPairsWidth = int((roomWidth - TILESIZE) // (2 * TILESIZE)) numberOfPairsLength = int((roomLength - TILESIZE) // (2 * TILESIZE)) #Calculate the number of columns and rows numberOfCol = int(1 + (2 * numberOfPairsWidth)) numberOfRow = int(1 + (2 * numberOfPairsLength)) #Calculate the gap # the gap = (the total width - the number of tiles * tile width / 2 gapCol = (roomWidth - numberOfCol * TILESIZE) / 2 gapRow = (roomLength - numberOfRow * TILESIZE) / 2 # Draw Tiles for i in range(numberOfCol) : if i % 2 == 0 : for j in range(numberOfRow) : if j % 2 == 0 : canvas.setColor("black") else : canvas.setColor("yellow") canvas.drawRect(gapCol + i * TILESIZE, gapRow + j * TILESIZE, TILESIZE, TILESIZE) else : for j in range(numberOfRow) : if j % 2 == 0 : canvas.setColor("yellow") else : canvas.setColor("black") canvas.drawRect(gapCol + i * TILESIZE, gapRow + j * TILESIZE, TILESIZE, TILESIZE) # Print results print("The number of columns is: ", numberOfCol) print("The number of rows is: ", numberOfRow) print("The total number of tiles needed is: ", numberOfCol * numberOfRow) print("The gap at the end of each rows is: ", gapRow) print("The gap at the end of each column is: ", gapCol) The output: Please enter a room length between 100 and 900: 200 Invalid entry. The number of columns is: 5 The number of rows is: 9 The total number of tiles needed is: 45 The gap at the end of each rows is: 10.0 The gap at the end of each column is: 0.0 Sent from Windows Mail -------------- next part -------------- An HTML attachment was scrubbed... URL: From twitarch at gmail.com Fri Oct 10 19:47:03 2014 From: twitarch at gmail.com (Z) Date: Fri, 10 Oct 2014 13:47:03 -0400 Subject: [Tutor] Tutorials for experimental psycology Message-ID: I am familiar with basics of programming, and wanted to learn how to code psychology experiments in python. Are there standard libraries/tutorials and/or GUI-based software? I especially want to be able to assign probability distribution functions to variables. I would eventually be working with looking times, reaction times, and integrating these different measures to measure behavior. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Fri Oct 10 22:57:18 2014 From: davea at davea.name (Dave Angel) Date: Fri, 10 Oct 2014 16:57:18 -0400 (EDT) Subject: [Tutor] Tile Code Program References: <5437ffaf.632c8c0a.8d0f.6e95@mx.google.com> Message-ID: Wrote in message: > > (Please use text mail to post here) >>>>>>>> roomWidth = 100.0. #BUGBUG roomLength = 90.0 while roomWidth < 100 or roomWidth > 1000: roomWidth = float(input("Please enter a room width between 100 and 1000: ")) if roomWidth >= 100 or roomWidth <= 1000: print("Invalid entry") while roomLength < 100 or roomLength > 900: roomLength = float(input("Please enter a room length between 100 and 900: ")) if roomLength >= 100 or roomLength <= 900: print("Invalid entry.") >>>>>>>> a room length of 100 causes the first while to be skipped entirely. Both the if statements are inside out. You need the same conditional as you used for the while loops. -- DaveA From dyoo at hashcollision.org Sat Oct 11 00:03:25 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 10 Oct 2014 15:03:25 -0700 Subject: [Tutor] Tile Code Program In-Reply-To: <5437ffaf.632c8c0a.8d0f.6e95@mx.google.com> References: <5437ffaf.632c8c0a.8d0f.6e95@mx.google.com> Message-ID: On Fri, Oct 10, 2014 at 8:46 AM, wrote: > This is the prompt: Write an algorithm / pseudocode that: > ? Reads in a two integers; the width and length of a room. [code cut] Can you talk a little bit about what problem you're having, if anything? Or are you asking for a code review? It's unclear, so more context would be helpful. From alan.gauld at btinternet.com Sat Oct 11 00:41:36 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Oct 2014 23:41:36 +0100 Subject: [Tutor] Tutorials for experimental psycology In-Reply-To: References: Message-ID: On 10/10/14 18:47, Z wrote: > I am familiar with basics of programming, and wanted to learn how to > code psychology experiments in python. Are there standard > libraries/tutorials and/or GUI-based software? Yes, but a lot depends on your specific requirements. You should probably research SciPy/Scikit and in particular NumPy, Pandas and IPython. But I'm no expert on their details and their use is a bit out of scope for this mailing list. But there are dedicated fora for most of the SciP{y components. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Sat Oct 11 00:45:05 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 10 Oct 2014 15:45:05 -0700 Subject: [Tutor] Tutorials for experimental psycology In-Reply-To: References: Message-ID: On Fri, Oct 10, 2014 at 3:41 PM, Alan Gauld wrote: > On 10/10/14 18:47, Z wrote: >> >> I am familiar with basics of programming, and wanted to learn how to >> code psychology experiments in python. Are there standard >> libraries/tutorials and/or GUI-based software? > > > Yes, but a lot depends on your specific requirements. > > You should probably research SciPy/Scikit and in particular > NumPy, Pandas and IPython. > > But I'm no expert on their details and their use is > a bit out of scope for this mailing list. But there are > dedicated fora for most of the SciP{y components. Agreed; it's a bit out of scope for Python-Tutor. A web search for [experimental psychology python] does hit a few intriguing leads: http://www.psychopy.org/ but I suspect none of us here have direct experience with this or other related libraries. From robertvstepp at gmail.com Sat Oct 11 06:34:35 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 10 Oct 2014 23:34:35 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Fri, Oct 10, 2014 at 7:43 AM, boB Stepp wrote: > On Fri, Oct 10, 2014 at 2:05 AM, Wolfgang Maier > wrote: >> On 10/10/2014 05:57 AM, boB Stepp wrote: [...] >> >> It would help if you could share details about how you tried to run the >> Python 3 program (command line call, double-click, Python launcher for >> Windows, ..). >> > Initially, I ran the program in question by double-clicking on its > icon, which was my normal way of running it. This is when I realized I > had a problem. The program would run without error, but give erroneous > print and input statement results. I next put Python 3 into my path > variable, but the same error persisted. From the command line I found > that if I ran it normally (With Python 3 still in the path like it was > on my previous 3.4.1 installation.), meaning > > python program_name.py > > it behaved the same way as double-clicking on the icon. However, if I ran it as > > py -3 program_name.py > > then all was well. > > Apparently Python 2.7.8 became the system default installation if I > installed it last, but after uninstalling both and installing Python > 3.4.2 last, it became the default. Or, at least when double-clicked on > it ran normally. > > I have since added shebang lines to my programs specifying Python 3. > > I hope I have not forgotten any relevant details! > Apparently I must have forgotten relevant details because I have concluded tonight that I have not solved anything. Despite thinking that I had made Python 3.4.2 my default Python, I am now certain that Python 2.7.8 is, in fact, my current default Python. So switching the order of installation did not work and whatever I did last night in my presumably sleepy or lame state was not what I thought I was doing. If I double-click on a file or icon to run a program, it runs under Python 2.7.8. If I run it from the command line with py program_name.py, then it runs under Python 2.7.8. This is with shebang lines removed from all programs. So I apologize for what I wrote earlier. I can live with 2.7.8 being the default Python, but if I wanted to make 3.4.2 the default, how would I go about doing it? boB From robertvstepp at gmail.com Sat Oct 11 07:24:49 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 11 Oct 2014 00:24:49 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Fri, Oct 10, 2014 at 11:54 PM, Zachary Ware wrote: > On Fri, Oct 10, 2014 at 11:34 PM, boB Stepp wrote: >> I can live with 2.7.8 being the default Python, but if I wanted to >> make 3.4.2 the default, how would I go about doing it? > > Check the output of "ftype Python.File", it should be: > > C:\>ftype Python.File > Python.File="C:\Windows\py.exe" "%1" %* > This checked out sat. > If it's not, make it so :). Then configure the Python Launcher > (py.exe) as described here: > https://docs.python.org/3/using/windows.html#customization > > (You'll probably want your py.ini to look like this: > > [defaults] > python=3 > Cannot locate either of the mentioned py.ini files. I did a search for these on my PC and came up empty. I am going to try to create my own py.ini file and place it in C:\WINDOWS and see what happens. -- boB From robertvstepp at gmail.com Sat Oct 11 07:30:03 2014 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 11 Oct 2014 00:30:03 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Sat, Oct 11, 2014 at 12:24 AM, boB Stepp wrote: > On Fri, Oct 10, 2014 at 11:54 PM, Zachary Ware > wrote: >> On Fri, Oct 10, 2014 at 11:34 PM, boB Stepp wrote: >>> I can live with 2.7.8 being the default Python, but if I wanted to >>> make 3.4.2 the default, how would I go about doing it? >> >> Check the output of "ftype Python.File", it should be: >> >> C:\>ftype Python.File >> Python.File="C:\Windows\py.exe" "%1" %* >> > > This checked out sat. > >> If it's not, make it so :). Then configure the Python Launcher >> (py.exe) as described here: >> https://docs.python.org/3/using/windows.html#customization >> >> (You'll probably want your py.ini to look like this: >> >> [defaults] >> python=3 >> > Cannot locate either of the mentioned py.ini files. I did a search for > these on my PC and came up empty. I am going to try to create my own > py.ini file and place it in C:\WINDOWS and see what happens. > This did the trick! Many thanks, Zach! -- boB From zachary.ware+pytut at gmail.com Sat Oct 11 07:37:15 2014 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Sat, 11 Oct 2014 00:37:15 -0500 Subject: [Tutor] Fwd: Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Fri, Oct 10, 2014 at 11:34 PM, boB Stepp wrote: > I can live with 2.7.8 being the default Python, but if I wanted to > make 3.4.2 the default, how would I go about doing it? Check the output of "ftype Python.File", it should be: C:\>ftype Python.File Python.File="C:\Windows\py.exe" "%1" %* If it's not, make it so :). Then configure the Python Launcher (py.exe) as described here: https://docs.python.org/3/using/windows.html#customization (You'll probably want your py.ini to look like this: [defaults] python=3 ) Hope this helps, -- Zach From zachary.ware+pytut at gmail.com Sat Oct 11 07:39:57 2014 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Sat, 11 Oct 2014 00:39:57 -0500 Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! In-Reply-To: References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: On Sat, Oct 11, 2014 at 12:30 AM, boB Stepp wrote: > On Sat, Oct 11, 2014 at 12:24 AM, boB Stepp wrote: >> Cannot locate either of the mentioned py.ini files. I did a search for >> these on my PC and came up empty. I am going to try to create my own >> py.ini file and place it in C:\WINDOWS and see what happens. Correct, those files don't exist by default; they only exist if you want to change the launcher's defaults. > This did the trick! Many thanks, Zach! Glad I could help :) -- Zach From wbecerra1 at gmail.com Sat Oct 11 10:44:18 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Sat, 11 Oct 2014 10:44:18 +0200 Subject: [Tutor] keyword colors disappear Message-ID: Hey, I'm new to programming. Only have about 2 weeks of experience. Using Python 2.7.8 and running Windows 8 I'm having the following problem. I open Python shell press file, new file and write my code(any code) then all the Python keywords appear in their different *colors*, for example print appears in orange, strings in green numbers in blue, etc. Now here is the problem, after I press F5 and i run my code. Then i try go back to my code and add some more code or change the code. Now all ll the colors of the Python keywords are gone. Everything appears in normal black and white text. Note: weather the code is correct or there is any errors I still have the same problem. What i want to know is. Is there something i can do to keep the colors of the keywords? because it makes it easier for me to keep track of my code. Hope what i asked is clear. Thank You -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Oct 11 14:14:32 2014 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Oct 2014 14:14:32 +0200 Subject: [Tutor] keyword colors disappear References: Message-ID: William Becerra wrote: > Hey, I'm new to programming. > Only have about 2 weeks of experience. > Using Python 2.7.8 and running Windows 8 > I'm having the following problem. > > I open Python shell press file, new file and write my code(any code) > then all the Python keywords appear in their different *colors*, for > example print appears in orange, strings in green numbers in blue, etc. >From your description I conclude that you are using IDLE. Is that correct? (If you are not sure look into the help menu, there should be an "About IDLE" entry) > Now here is the problem, after I press F5 and i run my code. Then i try go > back to my code and add some more code or change the code. Now all ll the > colors of the Python keywords are gone. Everything appears in normal > black and white text. > Note: weather the code is correct or there is any errors I still have the > same problem. > > What i want to know is. > Is there something i can do to keep the colors of the keywords? because it > makes it easier for me to keep track of my code. > > Hope what i asked is clear. What is the suffix of the saved file? It should be '.py'. If the file name ends with with something else (on my linux machine even '.PY' doesn't work) IDLE may mistake the code for arbitrary text. From davea at davea.name Sat Oct 11 15:35:12 2014 From: davea at davea.name (Dave Angel) Date: Sat, 11 Oct 2014 09:35:12 -0400 (EDT) Subject: [Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST! References: <5437853F.5050309@biologie.uni-freiburg.de> Message-ID: boB Stepp Wrote in message: > > I can live with 2.7.8 being the default Python, but if I wanted to > make 3.4.2 the default, how would I go about doing it? > I haven't used Windows in a long while. When I did, I used assoc.exe and ftype.exe to set my python to run py.exe. That program is installed by pthon 3.3 and later. In turn, it interprets the shebang line, launching whatever version needed. However somebody corrected me, saying that per-user mappings in HKEY_CURRENT_USER\Software\Classes can override the ones set with ftype and assoc. -- DaveA From juan0christian at gmail.com Sun Oct 12 04:29:13 2014 From: juan0christian at gmail.com (Juan Christian) Date: Sat, 11 Oct 2014 23:29:13 -0300 Subject: [Tutor] How to use custom protocol with requests? Message-ID: I need to call this URL: steam://friends/add/ If I put it in my browser and press enter it works as expected, now I need to do this on my code, I don't need to retrieve anything, I just need it to be "executed", is there a way to do that in Python with requests or any other lib? Python 3.4.1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Sun Oct 12 04:47:36 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 11 Oct 2014 19:47:36 -0700 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: On Sat, Oct 11, 2014 at 7:29 PM, Juan Christian wrote: > I need to call this URL: steam://friends/add/ > > If I put it in my browser and press enter it works as expected, now I need > to do this on my code, I don't need to retrieve anything, I just need it to > be "executed", is there a way to do that in Python with requests or any > other lib? This may be more difficult to do; it's unclear whether or not this is exposed in the web API that Valve provides. Unlike the other methods described in: https://developer.valvesoftware.com/wiki/Steam_Web_API which are all query-based, you're asking for something that _mutates_ a user. This functionality would probably not be exposed as a RESTful web-based API: imagine the kind of havok that a malicious user could do if they could add arbitrary friends on any other person. So I would expect it to require some sort of user authentication, at the very least. Searching... I see no such documentation from web searches on this. You'll probably need to ask the Steam folks on this one. From juan0christian at gmail.com Sun Oct 12 04:55:44 2014 From: juan0christian at gmail.com (Juan Christian) Date: Sat, 11 Oct 2014 23:55:44 -0300 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: On Sat, Oct 11, 2014 at 11:53 PM, Danny Yoo wrote: > >> I need to call this URL: steam://friends/add/ > >> > >> If I put it in my browser and press enter it works as expected, now I > need > >> to do this on my code, I don't need to retrieve anything, I just need > it to > >> be "executed", is there a way to do that in Python with requests or any > >> other lib? > > Thinking about this a bit more. If your browser is already doing the > "right thing", maybe the webbrowser module is enough to get this > behavior: > > https://docs.python.org/2/library/webbrowser.html > > webbrowser should invoke the browser on your behalf. Maybe your web > browser is set up to dispatch this to the steam application? Hard to > tell without testing this myself, but I'm not in an environment that > lets me try this out at the moment. > YES, the browser redirects it to the Steam software installed on the PC! -------------- next part -------------- An HTML attachment was scrubbed... URL: From juan0christian at gmail.com Sun Oct 12 04:56:42 2014 From: juan0christian at gmail.com (Juan Christian) Date: Sat, 11 Oct 2014 23:56:42 -0300 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: On Sat, Oct 11, 2014 at 11:47 PM, Danny Yoo wrote: > This may be more difficult to do; it's unclear whether or not this is > exposed in the web API that Valve provides. > > Unlike the other methods described in: > > https://developer.valvesoftware.com/wiki/Steam_Web_API > > which are all query-based, you're asking for something that _mutates_ > a user. This functionality would probably not be exposed as a RESTful > web-based API: imagine the kind of havok that a malicious user could > do if they could add arbitrary friends on any other person. So I > would expect it to require some sort of user authentication, at the > very least. Searching... I see no such documentation from web > searches on this. You'll probably need to ask the Steam folks on this > one. > Indeed the person HAS TO BE logged in on Steam, it already exists in sites like backpack.tf, you have a +ADD button in all profiles there, I just want to mimic this action on my script when needed. -------------- next part -------------- An HTML attachment was scrubbed... URL: From juan0christian at gmail.com Sun Oct 12 04:58:58 2014 From: juan0christian at gmail.com (Juan Christian) Date: Sat, 11 Oct 2014 23:58:58 -0300 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: Sorry for triple post, but yes, webbrowser worked 100%. Exactly what I needed! Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Sun Oct 12 05:17:28 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 11 Oct 2014 20:17:28 -0700 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: On Sat, Oct 11, 2014 at 7:58 PM, Juan Christian wrote: > Sorry for triple post, but yes, webbrowser worked 100%. Exactly what I > needed! Huh. Wow. That actually worked? :P --- Frankly speaking though, this sounds like a horrible XSRF-style attack in waiting, if I understand what has just happened. (http://en.wikipedia.org/wiki/Cross-site_request_forgery) Usually, requests to do mutation operations are protected so that, in order to make the request, you have to have some knowledge in the request that's specific to the user, and not public knowledge. The URL you've described is missing this basic information, an "XSRF token" as its commonly known (though I would have assumed it would be called an "anti-XSRF" token, but oh well.) I'm not sure how your web browser is handling the 'steam://' URL class, but I would very much hope that, in the interface between the browser and your Steam client, it's doing something to mitigate what looks like an XSRF exploit. From dyoo at hashcollision.org Sun Oct 12 04:53:11 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 11 Oct 2014 19:53:11 -0700 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: >> I need to call this URL: steam://friends/add/ >> >> If I put it in my browser and press enter it works as expected, now I need >> to do this on my code, I don't need to retrieve anything, I just need it to >> be "executed", is there a way to do that in Python with requests or any >> other lib? Thinking about this a bit more. If your browser is already doing the "right thing", maybe the webbrowser module is enough to get this behavior: https://docs.python.org/2/library/webbrowser.html webbrowser should invoke the browser on your behalf. Maybe your web browser is set up to dispatch this to the steam application? Hard to tell without testing this myself, but I'm not in an environment that lets me try this out at the moment. From wbecerra1 at gmail.com Sun Oct 12 09:41:40 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Sun, 12 Oct 2014 09:41:40 +0200 Subject: [Tutor] Infinite Recursion Message-ID: Hey, I'm new to programming. Using python 2.7.8 and running windows8 OS I'm reading 'How to think like a computer scientist, learning with Python' I'm in chapter 4 sub-chapter 4.11 Infinite recursion According to the book if I write def recurse(): recurse() I should get the following error File"", line2, in recurse ( 98 repetitions omittted) File "", line 2, in recurse RuntimeError: Maximum recursion depth exceeded. I don't get that error, instead the Python shell prints out two blank lines. >From what i understand if i don't get the error the infinite recursion is not been tried by the shell. Am I missing anything in the code? and If anything is wrong. How can I write a easy Infinite recursion to help me grasp the concept? Thank You. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Oct 12 11:03:52 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 12 Oct 2014 20:03:52 +1100 Subject: [Tutor] Infinite Recursion In-Reply-To: References: Message-ID: <20141012090352.GE23419@ando.pearwood.info> On Sun, Oct 12, 2014 at 09:41:40AM +0200, William Becerra wrote: > Hey, I'm new to programming. > Using python 2.7.8 and running windows8 OS > I'm reading 'How to think like a computer scientist, learning with Python' > I'm in chapter 4 sub-chapter 4.11 Infinite recursion > > According to the book if I write > def recurse(): > recurse() > I should get the following error > File"", line2, in recurse > ( 98 repetitions omittted) > File "", line 2, in recurse > RuntimeError: Maximum recursion depth exceeded. > > I don't get that error, instead the Python shell prints out two blank lines. *Two* blank lines? I can't reproduce that. Are you perhaps using IPython? Or IDLE? Either way, I expect that you have forgotten the brackets (parentheses) on the *inner* call to `recurse`. If I type this instead: def recurse(): recurse recurse() # Call the function. notice that there are no parens on the inner call, so what happens is that when I enter "recurse()", Python executes the function. Inside the body of the function, Python grabs a reference to the "recurse" function, but *does not call it*. Because it's not called, the recursion stops immediately. It may help you understand the difference to play around with these functions. Copy and paste them into the shell: def recurse1(): # Actually a lie, not really recursing at all. print "Look up the name 'recurse1' and see if it exists:" recurse1 print "Done!" def recurse2(): print "Look up the name 'recurse2' and call it:" recurse2() print "Done!" -- Steven From fomcl at yahoo.com Sun Oct 12 14:35:48 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 12 Oct 2014 12:35:48 +0000 (UTC) Subject: [Tutor] what is the easiest way to install different Python versions? Message-ID: <1798207457.287910.1413117348399.JavaMail.yahoo@jws10769.mail.gq1.yahoo.com> Hi, (sorry for cross-posting, sort of) A few days ago I needed to check whether some Python code ran with Python 2.6. What is the easiest way to install another Python version along side the default Python version? My own computer is Debian Linux 64 bit, but a platform-independent solution would be best. Possible solutions that I am aware of -make altinstall *). This is what I tried (see below), but not all modules could be built. I gave up because I was in a hurry -Pythonbrew. This project is dead -Deadsnakes -Anaconda -Tox? I only know this is as a cross-version/implementation test runner -Vagrant. This is what I eventually did, and this was very simple. I ran Ubuntu 10.0.4 LTS, which uses Python 2.6, and used Vagrant SSH to run and check my code in Python 2.6 (and I replaced a dict comprehension with a list comprehension, for example) - ... What is the recommended way? I don't expect/hope that I'd ever need something lower than Python 2.5 Thank you. Albert-Jan *) Make altinstall sudo apt-get install libsqlite3-dev libbz2-dev libgdbm-dev libncurses5-dev tk-dev zlib1g-dev wget https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz tar -zxvf Python-2.6.8.tgz cd Python-2.6.8/ ./configure --prefix=/usr/local make # see 'failed stuff' below sudo make altinstall mkvirtualenv -p /usr/local/bin/python2.6 python26 # ImportError: No module named zlib # Failed stuff Failed to find the necessary bits to build these modules: _bsddb _curses _curses_panel _hashlib _sqlite3 _ssl bsddb185 bz2 dbm dl gdbm imageop linuxaudiodev ossaudiodev readline sunaudiodev zlib ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From joel.goldstick at gmail.com Sun Oct 12 15:15:28 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 12 Oct 2014 09:15:28 -0400 Subject: [Tutor] what is the easiest way to install different Python versions? In-Reply-To: <1798207457.287910.1413117348399.JavaMail.yahoo@jws10769.mail.gq1.yahoo.com> References: <1798207457.287910.1413117348399.JavaMail.yahoo@jws10769.mail.gq1.yahoo.com> Message-ID: On Sun, Oct 12, 2014 at 8:35 AM, Albert-Jan Roskam wrote: > Hi, > > (sorry for cross-posting, sort of) > > A few days ago I needed to check whether some Python code ran with Python 2.6. What is the easiest way to install another Python version along side the default Python version? My own computer is Debian Linux 64 bit, but a platform-independent solution would be best. > > Possible solutions that I am aware of > > -make altinstall *). This is what I tried (see below), but not all modules could be built. I gave up because I was in a hurry > -Pythonbrew. This project is dead > -Deadsnakes > -Anaconda > -Tox? I only know this is as a cross-version/implementation test runner > -Vagrant. This is what I eventually did, and this was very simple. I ran Ubuntu 10.0.4 LTS, which uses Python 2.6, and used Vagrant SSH to run and check my code in Python 2.6 (and I replaced a dict comprehension with a list comprehension, for example) > - ... > > What is the recommended way? I don't expect/hope that I'd ever need something lower than Python 2.5 > > Using virtualenvwrapper is easy and isolates your environment http://virtualenvwrapper.readthedocs.org/en/latest/index.html > > Thank you. > > Albert-Jan > > > > > *) Make altinstall > sudo apt-get install libsqlite3-dev libbz2-dev libgdbm-dev libncurses5-dev tk-dev zlib1g-dev > wget https://www.python.org/ftp/python/2.6.8/Python-2.6.8.tgz > tar -zxvf Python-2.6.8.tgz > cd Python-2.6.8/ > ./configure --prefix=/usr/local > make # see 'failed stuff' below > sudo make altinstall > mkvirtualenv -p /usr/local/bin/python2.6 python26 # ImportError: No module named zlib > > > # Failed stuff > Failed to find the necessary bits to build these modules: > _bsddb _curses _curses_panel > _hashlib _sqlite3 _ssl > bsddb185 bz2 dbm > dl gdbm imageop > linuxaudiodev ossaudiodev readline > sunaudiodev zlib > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a > > fresh water system, and public health, what have the Romans ever done for us? > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com From fomcl at yahoo.com Sun Oct 12 15:18:47 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 12 Oct 2014 13:18:47 +0000 (UTC) Subject: [Tutor] what is the easiest way to install different Python versions? In-Reply-To: References: Message-ID: <998879688.288775.1413119927126.JavaMail.yahoo@jws10753.mail.gq1.yahoo.com> Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ----- Original Message ----- > From: Joel Goldstick > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Sunday, October 12, 2014 3:15 PM > Subject: Re: [Tutor] what is the easiest way to install different Python versions? > > On Sun, Oct 12, 2014 at 8:35 AM, Albert-Jan Roskam > wrote: >> Hi, >> >> (sorry for cross-posting, sort of) >> >> A few days ago I needed to check whether some Python code ran with Python > 2.6. What is the easiest way to install another Python version along side the > default Python version? My own computer is Debian Linux 64 bit, but a > platform-independent solution would be best. >> >> Possible solutions that I am aware of >> >> -make altinstall *). This is what I tried (see below), but not all modules > could be built. I gave up because I was in a hurry >> -Pythonbrew. This project is dead >> -Deadsnakes >> -Anaconda >> -Tox? I only know this is as a cross-version/implementation test runner >> -Vagrant. This is what I eventually did, and this was very simple. I ran > Ubuntu 10.0.4 LTS, which uses Python 2.6, and used Vagrant SSH to run and check > my code in Python 2.6 (and I replaced a dict comprehension with a list > comprehension, for example) >> - ... >> >> What is the recommended way? I don't expect/hope that I'd ever need > something lower than Python 2.5 >> >> > > Using virtualenvwrapper is easy and isolates your environment > > http://virtualenvwrapper.readthedocs.org/en/latest/index.html But then it's still required that Python 2.6 is installed on my system, right? This seems to confirm this: http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv (I like the --python or -p flag of virtualenv and virtualenvwrapper though) From juan0christian at gmail.com Sun Oct 12 15:26:12 2014 From: juan0christian at gmail.com (Juan Christian) Date: Sun, 12 Oct 2014 10:26:12 -0300 Subject: [Tutor] How to use custom protocol with requests? In-Reply-To: References: Message-ID: On Sun, Oct 12, 2014 at 12:17 AM, Danny Yoo wrote: > Huh. Wow. That actually worked? > > :P > > --- > > Frankly speaking though, this sounds like a horrible XSRF-style attack > in waiting, if I understand what has just happened. > (http://en.wikipedia.org/wiki/Cross-site_request_forgery) > > Usually, requests to do mutation operations are protected so that, in > order to make the request, you have to have some knowledge in the > request that's specific to the user, and not public knowledge. The > URL you've described is missing this basic information, an "XSRF > token" as its commonly known (though I would have assumed it would be > called an "anti-XSRF" token, but oh well.) > > I'm not sure how your web browser is handling the 'steam://' URL > class, but I would very much hope that, in the interface between the > browser and your Steam client, it's doing something to mitigate what > looks like an XSRF exploit. > Well, the person needs to be logged in the browser (maybe cookies are set for that), when I trigger that in the browser it automatically opens the Steam software installed in the computer and add the person. I don't know if it's a flaw, but it's very useful for what I'm doing. If you go to ANY profile on Steam (after logged in), let's say ' http://steamcommunity.com/profiles/', you can add the person, that simple. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Sun Oct 12 15:24:08 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 12 Oct 2014 13:24:08 +0000 (UTC) Subject: [Tutor] what is the easiest way to install different Python versions? In-Reply-To: <1798207457.287910.1413117348399.JavaMail.yahoo@jws10769.mail.gq1.yahoo.com> References: <1798207457.287910.1413117348399.JavaMail.yahoo@jws10769.mail.gq1.yahoo.com> Message-ID: <1081777067.291567.1413120248648.JavaMail.yahoo@jws10795.mail.gq1.yahoo.com> ----- Original Message ----- > From: Albert-Jan Roskam > To: Python Mailing List > Cc: > Sent: Sunday, October 12, 2014 2:35 PM > Subject: [Tutor] what is the easiest way to install different Python versions? > > Hi, > > (sorry for cross-posting, sort of) Oops, now I see that I posted it to Python Tutor again. Sorry. I intended to send this to the main Python list as this question is perhaps a bit out of scope for Tutor. From wbecerra1 at gmail.com Sun Oct 12 16:38:54 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Sun, 12 Oct 2014 16:38:54 +0200 Subject: [Tutor] Return Statement error Message-ID: Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS I was making an application to see if I understand how the return statement works I want my application to compare x and y and return either 1, -1 or 0. I'm using IDLE Here is my code: print"Please write a value for x" x = raw_input() print "Please write a value for y" y = raw_input() if x > y: return 1 elif x < y: return -1 elif x == y: return 0 else: return "Please insert a Valid character" When I press F5 to run my code a new window called syntax error appears The window says the following There's an error in your program: ***'return' outside function (C:/Users/William/Desktop/Python Files/Function compare.py, line6) What am I doing Wrong? I noticed that if i substitute all the return keywords with print the code runs correctly. However I want to use return as I am trying to learn how it works. Thank You. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Sun Oct 12 19:02:55 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 12 Oct 2014 13:02:55 -0400 Subject: [Tutor] Return Statement error In-Reply-To: References: Message-ID: On Sun, Oct 12, 2014 at 10:38 AM, William Becerra wrote: > Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS > I was making an application to see if I understand how the return statement > works > I want my application to compare x and y and return either 1, -1 or 0. > I'm using IDLE > > Here is my code: > print"Please write a value for x" > x = raw_input() > print "Please write a value for y" > y = raw_input() > if x > y: > return 1 > elif x < y: > return -1 > elif x == y: > return 0 > else: > return "Please insert a Valid character" > > > When I press F5 to run my code a new window called syntax error appears > The window says the following > There's an error in your program: > ***'return' outside function (C:/Users/William/Desktop/Python Files/Function > compare.py, line6) > > What am I doing Wrong? return is only allowed inside a function -- not in main line code You can wrap your code into a function and then just call it to see what it returns > I noticed that if i substitute all the return keywords with print the code > runs correctly. > However I want to use return as I am trying to learn how it works. > Thank You. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From danny.yoo at gmail.com Sun Oct 12 20:24:43 2014 From: danny.yoo at gmail.com (Danny Yoo) Date: Sun, 12 Oct 2014 11:24:43 -0700 Subject: [Tutor] Return Statement error In-Reply-To: References: Message-ID: On Oct 12, 2014 9:52 AM, "William Becerra" wrote: > > Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS > I was making an application to see if I understand how the return statement works > I want my application to compare x and y and return either 1, -1 or 0. > I'm using IDLE The return statement is intimately tied to functions: look for tutorial content about functions. For example: http://www.greenteapress.com/thinkpython/html/thinkpython004.html If you have questions, please feel free to ask. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Oct 13 01:01:53 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 13 Oct 2014 10:01:53 +1100 Subject: [Tutor] Return Statement error In-Reply-To: References: Message-ID: <20141012230153.GI23419@ando.pearwood.info> On Sun, Oct 12, 2014 at 04:38:54PM +0200, William Becerra wrote: > Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS > I was making an application to see if I understand how the return statement > works The `return` statement can only be used inside a function. That means you have to start off with a `def` line, and indent your code. Have you learned about functions yet? If not, perhaps you might prefer to forget about `return` until you do. Otherwise, if you take the code you wrote, indent it, and put a function declaration at the top, you should be able to use `return` successfully: def compare(): print "Please write a value for x" x = raw_input() print "Please write a value for y" y = raw_input() if x > y: return 1 elif x < y: return -1 elif x == y: return 0 else: return "this will never happen" Then, once you have defined your function, you can call it: result = compare() # don't forget the parentheses () print "And the result is", result -- Steven From wbecerra1 at gmail.com Mon Oct 13 07:48:53 2014 From: wbecerra1 at gmail.com (William Becerra) Date: Mon, 13 Oct 2014 07:48:53 +0200 Subject: [Tutor] Return Statement error In-Reply-To: <20141012230153.GI23419@ando.pearwood.info> References: <20141012230153.GI23419@ando.pearwood.info> Message-ID: I am familiar with funtions, i didn't realize i had to write the return statement inside a function...Thank you all..that was very helpful On 13 Oct 2014 01:03, "Steven D'Aprano" wrote: > On Sun, Oct 12, 2014 at 04:38:54PM +0200, William Becerra wrote: > > Hello, I'm new to programming. I'm using Python 2.7.8 and Windows 8 OS > > I was making an application to see if I understand how the return > statement > > works > > The `return` statement can only be used inside a function. That means > you have to start off with a `def` line, and indent your code. > > Have you learned about functions yet? If not, perhaps you might prefer > to forget about `return` until you do. Otherwise, if you take the code > you wrote, indent it, and put a function declaration at the top, you > should be able to use `return` successfully: > > def compare(): > print "Please write a value for x" > x = raw_input() > print "Please write a value for y" > y = raw_input() > if x > y: > return 1 > elif x < y: > return -1 > elif x == y: > return 0 > else: > return "this will never happen" > > > Then, once you have defined your function, you can call it: > > result = compare() # don't forget the parentheses () > print "And the result is", result > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From crusier at gmail.com Mon Oct 13 09:54:59 2014 From: crusier at gmail.com (Crusier) Date: Mon, 13 Oct 2014 15:54:59 +0800 Subject: [Tutor] Compare two text files Message-ID: Hi Alan, Attached are the two text files (stocklist.txt & stocklist1.txt) which I want to do a comparison with the content of the file, Basically, I want to see if there are any new numbers added to the file. Please comment on the sequence of the file: 1. First, Open up the File No. 1 and put the string into a list. 2. Second, Open the File No. 2 and put the string into a list. 3. Use difflib to compare This is some of the code I had written. #Remove ".HK" from the stock list def remove_HK(): f = open('stock_list.txt', "r") output = open('stock_lista.txt', "w") output.write(f.read().replace(".hk","").replace(".HK","")) f.close() output.close() remove_HK() My thinking is first remove ".HK" so I could compare in as int to another text file. Thank you Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- 1728.HK 1033.HK 2393.HK 0968.HK 3378.HK 3049.HK 1661.HK 8269.HK 3393.HK 0151.HK 0303.HK 0345.HK 0913.HK 0220.HK 0696.HK 0570.HK 3886.HK 2283.HK 3382.HK 0882.HK 1065.HK 0826.HK 3823.HK 1613.HK 1228.HK 2382.HK 1089.HK 0981.HK 0598.HK 1099.HK 0361.HK 1177.HK 0750.HK 0444.HK 0460.HK 2877.HK 2313.HK 0152.HK 0747.HK 2607.HK 0563.HK 2727.HK 0205.HK 8047.HK 1004.HK 2010.HK 8201.HK 1345.HK 2328.HK 1515.HK 8311.HK 0402.HK 1323.HK 8180.HK 0553.HK 1618.HK 0231.HK 2186.HK 1108.HK 8058.HK 8237.HK 1212.HK 0381.HK 6136.HK 1638.HK 3336.HK 0419.HK 2211.HK 0923.HK 0438.HK 0091.HK 0167.HK 1886.HK 1071.HK 0336.HK 2811.HK 6823.HK 8292.HK 0911.HK 0566.HK 1367.HK 2208.HK 0283.HK 0530.HK 0175.HK 3800.HK 0451.HK 0500.HK 0038.HK 8123.HK 8018.HK 3360.HK 0729.HK 1856.HK 1808.HK 1330.HK 0895.HK 1072.HK 2880.HK 3898.HK 0080.HK 0867.HK 0471.HK 2722.HK 1060.HK 1313.HK 1333.HK 0728.HK 2198.HK 2380.HK 0572.HK 1185.HK 0085.HK 0217.HK 0370.HK 0031.HK 1196.HK 2623.HK 0476.HK 1375.HK 0996.HK 2324.HK 3188.HK 1848.HK 6828.HK 8321.HK 0285.HK 0154.HK 2357.HK 0232.HK 0161.HK 1803.HK 0899.HK 2020.HK 1131.HK -------------- next part -------------- 0471.HK 3800.HK 0728.HK 1033.HK 1099.HK 2357.HK 0566.HK 2328.HK 0232.HK 0729.HK 2208.HK 0598.HK 2186.HK 0231.HK 0175.HK 0981.HK 0285.HK 0460.HK 0553.HK 2382.HK 0031.HK 0747.HK 3188.HK 1071.HK 3382.HK 3823.HK 3898.HK 0451.HK 2727.HK 0968.HK 0750.HK 1680.HK 6136.HK 1072.HK 6823.HK 1177.HK 2020.HK 0419.HK 6828.HK 1060.HK 8047.HK 0867.HK 0336.HK 1848.HK 1856.HK 1313.HK 2607.HK 3886.HK 8292.HK 1618.HK 0572.HK 2211.HK 3336.HK 2313.HK 0220.HK 1323.HK 1638.HK 1185.HK 1004.HK 1808.HK 8321.HK 0205.HK 2623.HK 2393.HK 0161.HK 1613.HK 0855.HK 8201.HK 0882.HK 1212.HK 0696.HK 1375.HK 0091.HK 0038.HK 0911.HK 3360.HK 0085.HK 1333.HK 0152.HK 1522.HK 0570.HK 0938.HK 1330.HK 2880.HK 3049.HK 0546.HK 2198.HK 1108.HK 8237.HK 2380.HK 0996.HK 0402.HK 0036.HK 0732.HK 0444.HK 0895.HK 3393.HK 1345.HK 0476.HK 1369.HK 1131.HK 1228.HK 0154.HK 0548.HK 8123.HK 0899.HK 0718.HK 2322.HK 0926.HK 1661.HK 1089.HK 0811.HK 0433.HK 83188.HK 0303.HK 1728.HK 0260.HK 0107.HK 2348.HK 1599.HK 1065.HK 8311.HK 8018.HK 0530.HK 8207.HK 0440.HK 1308.HK 0564.HK 0568.HK From davea at davea.name Mon Oct 13 11:30:53 2014 From: davea at davea.name (Dave Angel) Date: Mon, 13 Oct 2014 05:30:53 -0400 (EDT) Subject: [Tutor] Compare two text files References: Message-ID: Crusier Wrote in message: > Attached are the two text files (stocklist.txt & stocklist1.txt) which I want to do a comparison with the content of the file, Basically, I want to see if there are any new numbers added to the file. > Please comment on the sequence of the file: 1. First, Open up the File No. 1 and put the string into a list. 2. Second, Open the File No. 2 and put the string into a list. 3. Use difflib to compare I don't see what the included code had to do with the problem, since difflib doesn?t care about numbers. It compares sequences (like lists) of strings. So you have a couple of files to read in. Normally you might use readlines, but there don't seem to be any newlines in the files. So you'll need split, or something similar. Come back when you've made an attempt at the problem, and ask a question about your code. Otherwise you're just asking others to do your homework for you. That's no way to learn. And please post in plain text. Html messages can cause lots of problems. -- DaveA From steve at pearwood.info Mon Oct 13 11:32:16 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 13 Oct 2014 20:32:16 +1100 Subject: [Tutor] Compare two text files In-Reply-To: References: Message-ID: <20141013093216.GJ23419@ando.pearwood.info> On Mon, Oct 13, 2014 at 03:54:59PM +0800, Crusier wrote: > Hi Alan, > > Attached are the two text files (stocklist.txt & stocklist1.txt) which I > want to do a comparison with the content of the file, Basically, I want to > see if there are any new numbers added to the file. > > Please comment on the sequence of the file: > 1. First, Open up the File No. 1 and put the string into a list. > 2. Second, Open the File No. 2 and put the string into a list. > 3. Use difflib to compare Sounds like a good plan to me. Something like this would work: py> import difflib py> a = '1728.HK 1033.HK 2393.HK 0968.HK 3378.HK'.split() py> b = '1728.HK 1036.HK 2393.HK 0968.HK 2784.HK 3378.HK'.split() py> print '\n'.join(difflib.unified_diff(a, b)) --- +++ @@ -1,5 +1,6 @@ 1728.HK -1033.HK +1036.HK 2393.HK 0968.HK +2784.HK 3378.HK Obviously you don't type in the strings '1728.HK...', you read them from the files you wish to compare. -- Steven From ofir_l at walla.com Mon Oct 13 12:40:45 2014 From: ofir_l at walla.com (=?UTF-8?Q?=D7=90=D7=95=D7=A4=D7=99=D7=A8=20=D7=9C=D7=99=D7=A8=D7=95=D7=9F?=) Date: Mon, 13 Oct 2014 13:40:45 +0300 Subject: [Tutor] =?utf-8?q?Help_with_guess_my_number_game?= Message-ID: <1413196845.709000-19643650-15260@walla.com> An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Mon Oct 13 22:06:26 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 13 Oct 2014 13:06:26 -0700 Subject: [Tutor] Help with guess my number game In-Reply-To: <1413196845.709000-19643650-15260@walla.com> References: <1413196845.709000-19643650-15260@walla.com> Message-ID: > > if guess != the_number: > > print ("you failed, the number was", the_number) > > elif guess==the_number: > > print("You guessed it! The number was", the_number) > > print("And it only took you", tries, "tries!\n") This block of code appears to be applied for every iteration through your loop. Is that your intention? From dyoo at hashcollision.org Mon Oct 13 22:43:10 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 13 Oct 2014 13:43:10 -0700 Subject: [Tutor] Fwd: Re: Help with guess my number game In-Reply-To: <1413231639.190000-93791214-29830@walla.com> References: <1413231639.190000-93791214-29830@walla.com> Message-ID: ---------- Forwarded message ---------- From: ????? ????? Date: Mon, Oct 13, 2014 at 1:20 PM Subject: Re: Re: [Tutor] Help with guess my number game To: Danny Yoo Hi Danny Thanks for your response, I think it is part of the problem, but my intention was to end the first loop only after 5 tries. I don't understand why the code go to the print section after only two gusses and not five. and it also reveal the_number before the game end. Again thank for your help. Ofir ________________________________ ????: Danny Yoo, ????: Re: [Tutor] Help with guess my number game > > if guess != the_number: > > print ("you failed, the number was", the_number) > > elif guess==the_number: > > print("You guessed it! The number was", the_number) > > print("And it only took you", tries, "tries!n") This block of code appears to be applied for every iteration through your loop. Is that your intention? ________________________________ Walla! Mail - Get your free unlimited mail today From alan.gauld at btinternet.com Tue Oct 14 01:39:00 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Oct 2014 00:39:00 +0100 Subject: [Tutor] Help with guess my number game In-Reply-To: <1413196845.709000-19643650-15260@walla.com> References: <1413196845.709000-19643650-15260@walla.com> Message-ID: On 13/10/14 11:40, ????? ????? wrote: > # set the initial values > > the_number = random.randint(1, 100) > guess = int(input("Take a guess: ")) > tries = 1 > > # guessing loop > while guess != the_number: > if guess > the_number: > print("Lower...") > else: > print("Higher...") > guess = int(input("Take a guess: ")) > > tries += 1 > if tries > 5: > break so far so good.... almost... > > if guess != the_number: > print ("you failed, the number was", the_number) This is still inside the loop. You want to remove the indentation so this only happens after you exit the loop. Otherwise you tell the user the answer before they guess it (or have 5 goes) and it doesn't work right if the first guess is correct... > input("\n\nPress the enter key to exit.") You need the if/else to look like this. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From ofir_l at walla.com Tue Oct 14 18:23:33 2014 From: ofir_l at walla.com (=?UTF-8?Q?=D7=90=D7=95=D7=A4=D7=99=D7=A8=20=D7=9C=D7=99=D7=A8=D7=95=D7=9F?=) Date: Tue, 14 Oct 2014 19:23:33 +0300 Subject: [Tutor] =?utf-8?q?Tutor_Digest=2C_Vol_128=2C_Issue_33?= Message-ID: <1413303813.070000-70028564-17186@walla.com> An HTML attachment was scrubbed... URL: From crk at godblessthe.us Thu Oct 16 00:27:30 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Wed, 15 Oct 2014 15:27:30 -0700 Subject: [Tutor] well I finally got finished with this part of my exercise Message-ID: <007901cfe8c7$35fd6a90$a1f83fb0$@us> No, it hasn't taken the whole time, but it did take longer than I wanted, but I did progress as seen below. 1st pass - get the idea down: #program to capture streaming quotes from yahoo import re out_file = open("output", mode='w') in_file = open('Yahoo!_Finance_Portfolios.htm', encoding= "utf-8" ) if not (in_file.readline().startswith("")): #first line isn't an HTML exit(44) line_in, line_no = ' ', 0 for line_in in in_file: print( line_in[:100], flush = True ) # check out the line for debug. [:100] cuz charset error on long string line_no += 1 cap_end = 0 print(line_no, '\"', line_in[:120], '\"', flush = True) while True: pattern = re.compile(r'.+?([\d\.]+?)<') a = pattern.search( line_in, stock_end ) low_52 = a.group(1) low_end = a.end() pattern = re.compile(r'.+?prevclose:.+?>([\d\.\,]+?)<') a = pattern.search( line_in, low_end ) prev = a.group(1) prev_end = a.end() pattern = re.compile(r'.+?mkt:open.+?>([\d\.\,]+?)<') a = pattern.search( line_in, prev_end ) mkt_open = a.group(1) mkt_end = a.end() pattern = re.compile(r'.+?mkt:low.+?>([\d\.\,]+?)<' ) a = pattern.search(line_in, mkt_end) mkt_low = a.group(1) mkt_end = a.end() pattern = re.compile(r'.+?mkt:lsttrdtime.+?>(.+?)<' ) a = pattern.search( line_in, mkt_end ) last_time = a.group(1) last_end = a.end() pattern = re.compile(r'.+?tquote:value.+?>([\d\.\,]+?)<' ) a = pattern.search( line_in, last_end ) stock_price = a.group(1) stock_end = a.end() pattern =re.compile(r'.+?mkt:chg.+?>([\-\+][\d\.\,]+?)<' ) a = pattern.search(line_in, stock_end ) mkt_change = a.group(1) mkt_end = a.end() pattern = re.compile(r'.+?mkt:pctchg.+?>([\+\-][\d\.\,]+?%)<' ) a = pattern.search( line_in, mkt_end ) pct_change = a.group(1) pct_end = a.end() pattern = re.compile(r'.+?mkt:high.+?>([\d\.\,]+?)<' ) a = pattern.search( line_in, pct_end ) mkt_high = a.group(1) mkt_end = a.end() pattern = re.compile(r'.+?wk52:high.+?>([\d\.\,]+?)<' ) a = pattern.search( line_in, mkt_end ) high_52 = a.group(1) high_end = a.end() pattern = re.compile(r'.+?mkt:vol.+?>([\d\,]+?)<' ) a = pattern.search( line_in, high_end ) vol = a.group(1) vol_end = a.end() pattern = re.compile(r'.+?mkt.avgVol.+?>([\d\,]+?)<' ) a = pattern.search( line_in, vol_end ) vol_avg = a.group(1) vol_end = a.end() pattern = re.compile(r'.+?dividend_per_share.+?datum">([\d\.\,-]+?)<' ) a = pattern.search( line_in, vol_end ) div_share = a.group(1) div_end = a.end() pattern = re.compile(r'.+?dividend_yield.+?>([\d\.-]+?)<' ) a = pattern.search(line_in, div_end ) div_yield = a.group(1) div_end = a.end() pattern = re.compile(r".+?market_cap.+?datum.?>([\d\.\,-]+?[A-Z])<") a = pattern.search(line_in, div_end ) cap = a.group(1) cap_end = a.end() print( stock, low_52, prev, mkt_open, mkt_low, last_time, stock_price, mkt_change, pct_change, mkt_high, high_52, vol, vol_avg, div_share, div_yield, cap, flush=True ) out_file.closed 2nd pass: #program to capture streaming quotes from yahoo import re out_file = open("output", mode='w') in_file = open('Yahoo!_Finance_Portfolios.htm', encoding= "utf-8" ) if not (in_file.readline().startswith("")): #first line isn't an HTML exit(44) line_in = ' ' for line_in in in_file: # print( line_in[:100], flush = True ) # check out the line for debug. [:100] cuz charset error on long string string_end = 0 # print(line_no, '\"', line_in[:120], '\"', flush = True) #debug, flush needed cuz otherwise confused output while True: pattern = re.compile(r'.+?([\d\.]+?)<') match = pattern.search( line_in, string_end ) low_52 = match.group(1) string_end = match.end() pattern = re.compile(r'.+?prevclose:.+?>([\d\.\,]+?)<') match = pattern.search( line_in, string_end ) prev = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:open.+?>([\d\.\,]+?)<') match = pattern.search( line_in, string_end ) mkt_open = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:low.+?>([\d\.\,]+?)<' ) match = pattern.search(line_in, string_end) mkt_low = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:lsttrdtime.+?>(.+?)<' ) maatch = pattern.search( line_in, string_end ) last_time = match.group(1) string_end = match.end() pattern = re.compile(r'.+?tquote:value.+?>([\d\.\,]+?)<' ) match = pattern.search( line_in, string_end ) stock_price = match.group(1) string_end = match.end() pattern =re.compile(r'.+?mkt:chg.+?>([\-\+][\d\.\,]+?)<' ) match = pattern.search(line_in, string_end ) mkt_change = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:pctchg.+?>([\+\-][\d\.\,]+?%)<' ) match = pattern.search( line_in, string_end ) pct_change = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:high.+?>([\d\.\,]+?)<' ) match = pattern.search( line_in, string_end ) mkt_high = match.group(1) string_end = match.end() pattern = re.compile(r'.+?wk52:high.+?>([\d\.\,]+?)<' ) match = pattern.search( line_in, string_end ) high_52 = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt:vol.+?>([\d\,]+?)<' ) match = pattern.search( line_in, string_end ) vol = match.group(1) string_end = match.end() pattern = re.compile(r'.+?mkt.avgVol.+?>([\d\,]+?)<' ) match = pattern.search( line_in, string_end ) vol_avg = match.group(1) string_end = match.end() pattern = re.compile(r'.+?dividend_per_share.+?datum">([\d\.\,-]+?)<' ) match = pattern.search( line_in, string_end ) div_share = match.group(1) string_end = match.end() pattern = re.compile(r'.+?dividend_yield.+?>([\d\.-]+?)<' ) match = pattern.search(line_in, string_end ) div_yield = match.group(1) string_end = match.end() pattern = re.compile(r".+?market_cap.+?datum.?>([\d\.\,-]+?[A-Z])<") match = pattern.search(line_in, string_end ) cap = match.group(1) string_end = match.end() # we'll use string_end at the beginning of loop print( stock, low_52, prev, mkt_open, mkt_low, last_time, stock_price, mkt_change, pct_change, mkt_high, high_52, vol, vol_avg, div_share, div_yield, cap, flush=True ) out_file.closed # always do the right thing final pass: #program to capture streaming quotes from yahoo import re def match_string( pattern, string_end ): pattern = re.compile( pattern ) match = pattern.search( line_in, string_end ) return(match.group(1), match.end()) out_file = open("output", mode='w') in_file = open('Yahoo!_Finance_Portfolios.htm', encoding= "utf-8" ) if not (in_file.readline().startswith("")): #first line isn't an HTML exit(44) line_in = ' ' for line_in in in_file: # print( line_in[:100], flush = True ) # check out the line for debug. [:100] cuz charset error on long string string_end = 0 # print(line_no, '\"', line_in[:120], '\"', flush = True) #debug, flush needed cuz otherwise confused output while True: pattern = re.compile(r'.+?([\d\.]+?)<') # match = pattern.search( line_in, string_end ) # low_52 = match.group(1) # string_end = match.end() (low_52, string_end) = match_string( r'.+?wk52:low.+?>([\d\.]+?)<', string_end) (prev, string_end) = match_string(r'.+?prevclose:.+?>([\d\.\,]+?)<', string_end) (mkt_open, string_end) = match_string(r'.+?mkt:open.+?>([\d\.\,]+?)<', string_end) (mkt_low, string_end) = match_string(r'.+?mkt:low.+?>([\d\.\,]+?)<', string_end) (last_time, string_end) = match_string(r'.+?mkt:lsttrdtime.+?>(.+?)<', string_end) (stock_price, string_end) = match_string(r'.+?tquote:value.+?>([\d\.\,]+?)<', string_end) (mkt_change, string_end) = match_string(r'.+?mkt:chg.+?>([\-\+][\d\.\,]+?)<', string_end) (pct_change, string_end) = match_string(r'.+?mkt:pctchg.+?>([\+\-][\d\.\,]+?%)<', string_end) (mkt_high, string_end) = match_string(r'.+?mkt:high.+?>([\d\.\,]+?)<', string_end ) (high_52, string_end) = match_string(r'.+?wk52:high.+?>([\d\.\,]+?)<', string_end) (vol, string_end) = match_string(r'.+?mkt:vol.+?>([\d\,]+?)<', string_end) (vol_avg, string_end) = match_string(r'.+?mkt.avgVol.+?>([\d\,]+?)<', string_end) (div_share, string_end) = match_string(r'.+?dividend_per_share.+?datum">([\d\.\,-]+?)<', string_end ) (div_yield, string_end) = match_string(r'.+?dividend_yield.+?>([\d\.-]+?)<', string_end ) (cap, string_end) = match_string(r".+?market_cap.+?datum.?>([\d\.\,-]+?[A-Z])<", string_end) print( stock, low_52, prev, mkt_open, mkt_low, last_time, stock_price, mkt_change, pct_change, mkt_high, high_52, vol, vol_avg, div_share, div_yield, cap, flush=True ) out_file.close() # always do the right thing So, I learned quite a bit about matching and found that .+? is very useful: it is the question mark. Swallows as little as possible. Learned something about a simple function and returns. All put out the same thing: EKSO 0.75 1.38 1.39 1.26 03:57pm EST 1.3000 -0.0800 -5.80% 1.49 8.22 660,578 596,448 - - 102.05M SWKS 23.27 52.23 50.13 44.06 04:00pm EST 47.01 -5.22 -9.99% 50.13 59.25 12,383,347 3,448,690 0.44 0.80 8.91B EROC 2.79 3.01 2.98 2.79 04:00pm EST 2.95 -0.06 -1.99% 3.05 7.88 716,374 608,405 0.60 18.80 464.19M RKUS 8.65 12.54 12.36 11.78 04:04pm EST 11.84 -0.70 -5.58% 12.39 18.58 1,243,329 1,345,410 - - 979.58M FES 2.93 3.18 3.14 3.07 04:00pm EST 3.08 -0.10 -3.14% 3.15 5.75 15,514 43,111 - - 67.12M WLB 13.38 35.60 35.41 34.37 04:00pm EST 34.45 -1.15 -3.23% 36.33 45.19 213,917 189,145 0.00 0.00 585.17M DANG 7.69 11.00 10.67 10.33 04:02pm EST 10.33 -0.67 -6.09% 11.02 19.05 1,767,816 2,327,650 - - 833.54M UEPS 7.03 11.77 11.72 11.26 04:00pm EST 11.29 -0.48 -4.08% 11.82 14.24 212,876 431,726 - - 535.52M GY 15.01 15.46 15.37 15.12 04:02pm EST 15.17 -0.29 -1.88% 15.53 19.77 339,871 505,269 0.00 0.00 890.48M NR 10.43 11.00 10.89 10.59 04:05pm EST 10.89 -0.11 -1.00% 11.06 13.64 1,005,357 740,434 - - 911.13M PQ 3.64 4.54 4.50 4.32 04:02pm EST 4.50 -0.04 -0.88% 4.65 7.82 893,841 799,475 - - 297.42M PLG 0.76 0.7873 0.7899 0.7823 04:00pm EST 0.7942 +0.0069 +0.88% 0.8143 1.37 159,580 161,960 - - 440.8642M CALX 7.12 8.93 8.85 8.27 04:05pm EST 8.31 -0.62 -6.94% 8.85 13.36 371,015 295,832 - - 423.1M SWC 10.42 14.08 13.93 13.82 04:03pm EST 13.85 -0.23 -1.63% 14.44 19.42 1,531,009 1,828,360 - - 1.66B BBG 16.06 17.26 16.84 16.06 04:04pm EST 16.10 -1.16 -6.72% 17.21 30.69 1,993,888 1,333,430 - - 773.22M IAU 11.51 11.84 11.85 11.80 04:00pm EST 11.85 +0.01 +0.08% 11.86 13.46 6,511,530 2,368,960 - 0.00 1.62B IDTI 9.18 14.04 13.74 12.62 04:00pm EST 12.685 -1.355 -9.65% 13.84 17.32 8,615,394 3,095,850 - - 1.887B GRH 0.8 1.30 1.16 1.16 04:00pm EST 1.1650 -0.1350 -10.38% 1.25 3.62 131,138 631,260 - - 42.93M ORBC 5.4 5.85 5.82 5.75 04:00pm EST 5.80 -0.05 -0.85% 5.97 8.21 148,377 174,626 - - 320.47M PTX 1.68 8.09 8.04 7.89 04:00pm EST 8.20 +0.11 +1.36% 8.45 9.56 890,188 425,780 0.00 0.00 311.95M AMAT 16.4 20.87 20.55 19.50 04:15pm EST 19.83 -1.04 -4.98% 20.59 23.46 33,135,379 12,219,800 0.40 2.00 24.16B ^DJI 0 1,928.21 1,925.63 1,906.05 04:28pm EST 1,906.13 -22.08 -1.15% 1,936.98 2,019.26 757,485,255 27,888,833 0.92 1.59 121.77M ADEP 6.85 7.34 7.29 6.85 04:00pm EST 6.89 -0.45 -6.13% 7.29 21.9 146,339 230,198 - - 90.02M CIMT 5.38 5.51 5.50 5.50 03:59pm EST 5.50 -0.01 -0.18% 5.54 10.75 45,323 79,577 0.00 0.00 59.24M DDD 39.09 40.86 40.31 39.09 04:00pm EST 39.14 -1.72 -4.21% 40.92 97.28 3,237,974 3,035,590 0.11 0.60 4.3B IRBT 28.9 31.82 31.65 31.42 04:00pm EST 32.00 +0.18 +0.57% 32.32 48.36 892,183 661,142 - - 945.7M ONVO 5.12 6.00 5.86 5.53 04:00pm EST 5.56 -0.44 -7.33% 5.98 13.65 962,106 1,057,330 - - 436.34M VJET 11.9 12.84 12.55 11.90 04:00pm EST 12.09 -0.75 -5.84% 12.87 70 408,697 453,477 - - 221.28M XONE 16 18.21 18.11 17.81 04:00pm EST 19.78 +1.57 +8.62% 20.75 70.25 941,813 577,675 - - 285.66M AXAS 2.81 4.13 4.09 3.79 04:00pm EST 3.84 -0.29 -7.02% 4.13 6.45 4,336,860 2,590,600 - - 400.34M CMLP 19.01 19.92 19.79 19.01 04:03pm EST 19.03 -0.89 -4.47% 19.79 24.94 1,212,528 614,751 1.64 7.70 3.58B CORR 6.11 7.33 7.27 7.19 04:02pm EST 7.20 -0.13 -1.77% 7.39 8.54 103,846 132,263 0.50 7.70 10.58B FET 24.32 28.50 28.34 27.58 04:03pm EST 27.66 -0.84 -2.95% 28.51 37.03 343,356 423,854 - - 2.56B GDP 10.61 11.22 11.20 10.62 04:02pm EST 10.83 -0.39 -3.48% 11.82 30.52 2,935,299 1,686,260 - - 481.14M LNCGY 6.63 7.30 6.95 6.63 03:59pm EST 6.7499 -0.5501 -7.54% 7.00 17.25 123,277 38,138 - - 396.8334M NAMG 0.0251 0.0272 0.0251 0.0251 01:20pm EST 0.0261 -0.0011 -4.04% 0.0299 1.3 14,610 92,791 - - 1.6408M NRG 26.3 31.04 31.16 30.08 04:02pm EST 30.14 -0.90 -2.90% 31.38 38.09 5,711,982 4,186,300 0.56 1.80 10.18B QTWW 2.99 3.34 3.27 3.22 04:00pm EST 3.51 +0.17 +5.09% 3.60 11.25 248,630 284,014 - - 81.77M SDLP 26.69 29.37 29.01 28.19 04:05pm EST 29.98 +0.61 +2.08% 30.42 36.07 817,735 315,097 2.17 7.50 2.51B SDRL 22.01 24.11 23.54 22.01 04:05pm EST 22.83 -1.28 -5.31% 23.64 47.29 15,010,230 6,057,380 4.00 16.10 10.7B SLCA 24.28 48.95 47.50 44.00 04:03pm EST 45.62 -3.33 -6.80% 48.60 73.43 6,377,504 1,900,830 0.50 1.00 2.46B TRP 42.21 47.77 47.71 47.19 04:04pm EST 47.72 -0.05 -0.10% 48.77 58.4 3,454,796 1,501,470 1.73 3.40 33.79B TSL 9.55 10.09 9.97 9.55 04:05pm EST 9.92 -0.17 -1.68% 10.41 18.77 7,281,208 5,037,110 - - 811.15M UGA 49.9 50.35 50.38 49.90 04:00pm EST 50.25 -0.10 -0.20% 50.56 64.27 20,892 23,802 - 0.00 1.84B WDGJF 10.14 10.92 10.749 10.53 12:19pm EST 10.533 -0.387 -3.54% 10.749 13.93 2,983 2,040 - - 3.842B WPRT 5.7 6.23 6.23 5.70 04:00pm EST 6.05 -0.18 -2.89% 6.28 25.1 1,692,057 812,189 - - 381.98M FCSMF 0.24 0.39 0.41 0.40 03:43pm EST 0.40 +0.01 +3.09% 0.42 0.65 43,300 99,049 - - 42.83M GTI 3.31 3.58 3.52 3.47 04:04pm EST 3.72 +0.14 +3.91% 3.75 13.01 2,016,591 1,878,670 - - 506.43M MCP 1.14 1.49 1.49 1.36 04:03pm EST 1.38 -0.11 -7.38% 1.52 7.24 4,485,912 5,543,760 - - 337.79M NGPHF 0.53 0.774 0.75 0.745 03:59pm EST 0.7460 -0.0280 -3.62% 0.77 1.32 35,188 144,531 - - 36.689M TAS 0.55 0.605 0.60 0.55 04:00pm EST 0.5680 -0.0370 -6.12% 0.6099 1.89 286,580 95,643 - - 36.5892M PALL 67.6 77.35 76.27 75.85 04:00pm EST 76.34 -1.01 -1.31% 76.45 88.42 38,892 57,737 - - 69.2618M SPPP 8.35 8.92 8.84 8.77 04:00pm EST 8.80 -0.12 -1.35% 8.86 10.34 83,538 130,674 - - 15.7B ANV 2.29 2.69 2.64 2.51 04:02pm EST 2.5100 -0.1800 -6.69% 2.76 6.7 2,492,399 2,747,350 - - 261.87M GDX 20.11 21.22 21.13 20.67 04:00pm EST 20.77 -0.45 -2.12% 21.61 28.03 52,019,404 33,993,100 0.11 0.20 18.88B IAG 2.22 2.41 2.40 2.31 04:02pm EST 2.32 -0.09 -3.73% 2.47 5.59 8,204,914 6,909,500 0.00 0.00 874.41M PZG 0.68 0.75 0.75 0.70 04:01pm EST 0.70 -0.05 -6.53% 0.75 1.48 444,793 487,271 - - 112.91M SBGL 4.39 8.68 8.66 8.39 04:02pm EST 8.47 -0.21 -2.42% 8.95 11.29 1,031,828 762,377 0.28 3.30 1.9B SIL 9.72 10.30 10.19 9.96 04:00pm EST 10.14 -0.16 -1.55% 10.53 15.28 533,074 235,908 0.24 - 6.95B SVM 1.41 1.55 1.52 1.52 04:01pm EST 1.58 +0.03 +1.94% 1.59 3.6 806,644 860,260 0.02 1.20 270M AFOP 11.11 11.71 11.66 11.11 04:00pm EST 11.22 -0.49 -4.18% 11.75 22.6 267,475 482,178 0.15 1.30 208.87M AMD 2.71 2.95 2.95 2.71 04:00pm EST 2.72 -0.23 -7.80% 2.95 4.8 41,013,929 30,318,300 - - 2.08B AMKR 4.42 8.18 8.09 7.18 04:00pm EST 7.18 -1.00 -12.22% 8.20 12.27 2,336,091 1,478,200 - - 1.7B ASX 4.44 6.20 6.16 5.90 04:02pm EST 5.95 -0.25 -4.03% 6.16 6.89 1,765,849 732,037 0.15 2.40 9.12B EBIX 11.13 13.42 13.38 13.14 04:00pm EST 13.30 -0.12 -0.89% 13.56 17.95 328,446 448,305 0.30 2.30 508.69M GTAT 0.75 1.29 1.00 0.76 04:00pm EST 0.81 -0.48 -37.21% 1.09 20.54 70,540,435 22,427,900 - - 111.41M HIMX 5.7 9.52 9.28 8.85 04:00pm EST 9.09 -0.43 -4.52% 9.45 16.15 5,445,526 5,650,400 0.27 2.90 1.55B INTC 22.82 33.62 32.63 30.50 04:00pm EST 31.91 -1.71 -5.09% 32.86 35.56 80,910,985 31,394,900 0.90 2.70 157.99B INVN 15.2 19.09 18.85 17.66 04:00pm EST 18.74 -0.35 -1.83% 19.21 26.78 3,113,288 3,267,400 - - 1.67B INVT 1.22 1.31 1.32 1.28 03:58pm EST 1.3000 -0.0100 -0.76% 1.40 14.98 76,880 163,928 0.00 0.00 31.32M MU 16.17 30.64 29.75 27.59 04:15pm EST 27.79 -2.85 -9.30% 29.87 34.85 85,676,857 24,427,600 0.00 0.00 29.68B PCRFY 9.32 11.40 11.33 11.10 03:58pm EST 11.13 -0.27 -2.35% 11.33 13.14 89,081 196,168 - - 25.73B RFMD 4.5 10.34 10.09 9.30 04:00pm EST 9.62 -0.72 -6.96% 10.23 12.98 23,946,024 11,723,600 - - 2.77B STM 6.93 7.39 7.20 6.93 04:00pm EST 6.96 -0.43 -5.82% 7.21 10 2,775,384 775,592 0.34 4.50 6.22B CXDC 4.51 5.16 5.18 5.06 04:00pm EST 5.21 +0.05 +0.97% 5.25 13.24 100,879 353,680 - - 258.36M FRM 6.57 7.16 7.10 6.86 04:02pm EST 6.90 -0.26 -3.63% 7.18 12.7 243,022 200,508 0.00 0.00 259.7M JBLU 6.77 10.68 10.59 10.04 04:00pm EST 10.06 -0.62 -5.81% 10.84 12.83 13,438,950 7,708,150 - - 2.94B FTR 4.28 5.93 5.97 5.87 04:15pm EST 5.90 -0.03 -0.51% 6.01 7.24 7,605,322 10,940,100 0.40 6.50 5.91B SSW 19.13 19.72 19.62 19.13 04:02pm EST 19.45 -0.27 -1.37% 19.86 24.48 335,848 163,789 1.38 6.70 1.84B WIN 7.18 10.33 10.36 10.09 04:00pm EST 10.11 -0.22 -2.08% 10.415 13.3 6,950,101 8,084,240 1.00 9.40 6.096B ACRX 5.22 6.07 6.02 5.80 04:00pm EST 6.06 -0.01 -0.16% 6.32 13.64 509,193 1,145,230 - - 262.86M ADHD 5.88 6.53 6.41 5.95 04:00pm EST 6.06 -0.47 -7.20% 6.50 25.44 447,337 385,751 - - 82.79M AFFX 6.25 7.21 7.15 7.10 04:00pm EST 7.10 -0.11 -1.53% 7.37 9.8 541,914 549,582 - - 521.01M CRY 7.22 10.39 10.30 10.24 04:01pm EST 10.30 -0.09 -0.87% 10.61 12.14 84,549 114,891 0.12 1.10 287.63M DRTX 8.6 23.60 23.56 23.51 04:00pm EST 23.56 -0.04 -0.17% 23.85 24.33 741,953 571,268 - - 630.87M GALE 1.66 1.81 1.78 1.69 04:00pm EST 1.71 -0.10 -5.52% 1.83 7.77 1,887,999 1,904,160 - - 202.13M HZNP 3.65 12.24 12.16 11.66 04:00pm EST 11.97 -0.27 -2.21% 12.44 18.3 1,876,080 2,035,960 - - 894.98M INO 6.52 10.09 10.23 9.92 04:00pm EST 10.04 -0.05 -0.50% 10.42 15.8 822,957 1,428,130 - - 606.4M KERX 8.61 15.82 15.85 14.92 04:00pm EST 14.98 -0.84 -5.31% 16.21 18.48 2,889,907 2,435,450 - - 1.38B KLH.V 0.65 1.33 1.31 1.24 03:59pm EST 1.28 -0.05 -3.76% 1.32 2.55 230,485 514,989 - - 100M SBOTF 0.6 1.196 1.165 1.10 03:57pm EST 1.1310 -0.0648 -5.42% 1.18 2.36 306,955 590,542 - - 88.357M MBII 1.85 2.29 2.22 1.85 04:00pm EST 1.99 -0.30 -13.10% 2.25 20 571,556 261,028 - - 48.55M NBIX 8.57 15.94 15.85 15.71 04:00pm EST 15.93 -0.01 -0.06% 16.32 20.29 651,053 527,702 0.00 0.00 1.21B NEO 2.71 4.69 4.63 4.53 04:00pm EST 4.79 +0.10 +2.13% 4.93 6.1 161,449 532,298 - - 240.33M NKTR 8.87 12.67 12.58 12.55 04:00pm EST 12.65 -0.02 -0.16% 13.00 15.34 1,984,022 1,252,300 - - 1.61B NVAX 2.68 4.41 4.33 4.33 04:00pm EST 4.40 -0.01 -0.23% 4.59 6.95 4,341,420 2,963,340 - - 1.05B OPK 7.32 8.27 8.22 8.05 04:01pm EST 8.16 -0.11 -1.33% 8.33 12.95 2,310,886 2,344,180 - - 3.5B PGNX 3.1 4.55 4.51 4.41 04:00pm EST 4.4900 -0.0600 -1.32% 4.72 7.45 712,642 1,115,430 - - 312.3M POZN 5.35 8.01 7.98 7.91 04:00pm EST 7.95 -0.06 -0.75% 8.18 9.9 253,740 258,711 0.00 0.00 250.65M RDNT 1.5 7.64 7.63 7.40 04:00pm EST 7.60 -0.04 -0.46% 8.00 8.46 406,024 453,480 - - 324.32M SPPI 6.36 7.35 7.29 7.21 04:00pm EST 7.33 -0.02 -0.27% 7.51 10.32 1,000,626 882,048 0.00 0.00 473.58M THCZ 0.015 0.016 0.016 0.015 03:50pm EST 0.0180 +0.0024 +15.38% 0.018 0.018 399,453 0 - - 3.349M XOMA 3.42 3.72 3.76 3.65 04:00pm EST 3.70 -0.02 -0.54% 3.92 9.57 1,598,839 1,283,950 - - 396.18M I'm pretty happy with the outcome, but as I said, way to long. Next step is to play with http calls. Clayton -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmferras at estudiantes.uci.cu Thu Oct 16 17:21:38 2014 From: cmferras at estudiantes.uci.cu (C@rlos) Date: Thu, 16 Oct 2014 11:21:38 -0400 (CDT) Subject: [Tutor] Convert Qstring to string in windows In-Reply-To: <1549164247.13980291.1413472329833.JavaMail.zimbra@estudiantes.uci.cu> Message-ID: <969535236.13984943.1413472898089.JavaMail.zimbra@estudiantes.uci.cu> I have been tryed to convert a Qstring text to string on python, in linux that work fine but in windows when qstring contine ?,?,?,?,? the converted text is not correct, contine extranger characters, this qstring text is an url from qdialogtext. in linux i do for this way: pythonstringtext=qstringtext.text().toUtf8.data() and it return a python string correctly. i need some help plese... .....C at rlos III Escuela Internacional de Invierno en la UCI del 17 al 28 de febrero del 2014. Ver www.uci.cu -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu Oct 16 20:14:22 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 16 Oct 2014 11:14:22 -0700 Subject: [Tutor] Convert Qstring to string in windows In-Reply-To: <969535236.13984943.1413472898089.JavaMail.zimbra@estudiantes.uci.cu> References: <1549164247.13980291.1413472329833.JavaMail.zimbra@estudiantes.uci.cu> <969535236.13984943.1413472898089.JavaMail.zimbra@estudiantes.uci.cu> Message-ID: On Thu, Oct 16, 2014 at 8:21 AM, C at rlos wrote: > > I have been tryed to convert a Qstring text to string on python, in linux > that work fine but in windows when qstring contine ?,?,?,?,? the converted > text is not correct, contine extranger characters, > this qstring text is an url from qdialogtext. > > in linux i do for this way: > pythonstringtext=qstringtext.text().toUtf8.data() > and it return a python string correctly. Hi Carlos, This seems like a question that's very specific to Qt: you may want to ask on a Qt-Python mailing list. Tutor is intended for beginner programmers, and the question you're asking seems a bit specialized for the intended audience. In absence of this information, I have to make a few guesses. My best guesses so far are that you're working with Qt, which provides its own Unicode string class: http://qt-project.org/doc/qt-4.8/qstring.html Are you using PyQt 4 or PyQt 5, or something else entirely? Are you using Python 2 or Python 3? According to the PyQt5 documentation, it automatically handles the string conversion: http://pyqt.sourceforge.net/Docs/PyQt5/gotchas.html#python-strings-qt-strings-and-unicode and according to the PyQt 4 documentation, it also handles the conversion automatically for you: http://pyqt.sourceforge.net/Docs/PyQt4/python_v3.html#qstring and because the mapping is done by the library, you should not have to be doing anything on your own end to convert Qstrings to Python strings. Yeah, I am not sure what you are doing yet, because the documentation says that it handles conversions for you. The fact that you're doing this manually suggests that you might be doing something unusual. We need more information. But I think you may get better help on a Qt-specific mailing list; I suspect very few of us here have Qt experience. From fomcl at yahoo.com Thu Oct 16 23:08:45 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 16 Oct 2014 14:08:45 -0700 Subject: [Tutor] Convert Qstring to string in windows Message-ID: <1413493725.22928.BPMail_high_carrier@web163805.mail.gq1.yahoo.com> ----------------------------- On Thu, Oct 16, 2014 10:59 PM CEST Alan Gauld wrote: >On 16/10/14 19:14, Danny Yoo wrote: > >> need more information. But I think you may get better help on a >> Qt-specific mailing list; I suspect very few of us here have Qt >> experience. > >There are at least 2 Python Qt mailing lists and also two for >Side which is Nokia's public domain fork of Qt. That's probably >worth a look too. > >Definitely in the minority interest camp on the tutor list. > I am not 100 % sure but I think you can use str() on a qstring. In other words, it has its own __str__ method. From Pete.Wilson at atmel.com Fri Oct 17 01:35:02 2014 From: Pete.Wilson at atmel.com (Wilson, Pete) Date: Thu, 16 Oct 2014 23:35:02 +0000 Subject: [Tutor] Registering callbacks and .DLL Message-ID: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> I'm having problems registering a call-back with a .DLL. Maybe someone has some advice on registering call-backs from .dlls. I'm using Windows 7 and Python 2.6 (32-bits). The .DLL was written in C++ is working with C++ apps calling it. I tried the methods in section 15.17.1.17 with the qsort() and CFUNCTYPE, but it is not working. My code and the .dll are attached. Pete -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: py_cb.7z Type: application/octet-stream Size: 955723 bytes Desc: py_cb.7z URL: From alan.gauld at btinternet.com Fri Oct 17 02:24:07 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Oct 2014 01:24:07 +0100 Subject: [Tutor] Registering callbacks and .DLL In-Reply-To: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> References: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> Message-ID: On 17/10/14 00:35, Wilson, Pete wrote: > I?m having problems registering a call-back with a .DLL. Maybe someone > has some advice on registering call-backs from .dlls. > > I?m using Windows 7 and Python 2.6 (32-bits). The .DLL was written in > C++ is working with C++ apps calling it. OK As I understand it, You are trying to register a Python function as a call back on a C++ function in a DLL is that correct? Do you have any code? I don't see any attachment. Many lists/servers strip out binary attachments as potential viruses so it may not have made it through. Code is best included inline as text provided its not too long, or alternatively, in a pastebin. > I tried the methods in section 15.17.1.17 with the qsort() and > CFUNCTYPE, but it is not working. My code and the .dll are attached. Section 15.17.1.17 of what? Not the tutorial, not the library reference and not the language reference. So what? This question may be a tad too complex for the tutor list which is targeted to those learning Python, you probably want the main list for this. But you are going to have to provide some code and a more detailed description of what you are trying to do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From Pete.Wilson at atmel.com Fri Oct 17 18:02:36 2014 From: Pete.Wilson at atmel.com (Wilson, Pete) Date: Fri, 17 Oct 2014 16:02:36 +0000 Subject: [Tutor] Registering callbacks and .DLL In-Reply-To: References: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> Message-ID: <1A99A1517680884DAE440F5341907717872BA414@DVRMBX02.corp.atmel.com> Hi AG. I guess the attachment py_cb.7z was stripped. The statement below is correct, I am trying to register a Python function as the Callback (Confirmation) For a C++ function in the .dll. When I register the call-back with prod_bat_vol_read_request I should get a machine readable 'token' from send_serial_data.tx_data. I send this token to the battery tester to start it. When the test is done, my_conf_func should be triggered. The code is in line below. Section 15.17.1.17 is in the 2.6/library docs see the URL in the comments below. I tried several different ways without success. Thanks for your advice. Pete #********************************************************************************* ''' bat_read_15_17_1_17 this program is attempting ot register a call back function the ProductionTest.dll using the techniques outlined in section 15.17.1.17 of the python docs https://docs.python.org/2.6/library/ctypes.html#module-ctypes ''' '''open and register SendSerialData this part looks like it's working ''' from ctypes import * pt_dll = pt_dll = cdll.LoadLibrary("c:/py_stuff/ProductionTest.dll") reg_send_serial_data = pt_dll.RegSendSerialData class SendSerialData_t(Structure): _fields_ = [("tx_data", c_void_p), ("size", c_uint8)] send_serial_data = SendSerialData_t() try: reg_send_serial_data(send_serial_data) except: print "reg_send_serial_data fail" print send_serial_data.tx_data print send_serial_data.size '''set up ProdBatVolRequest and register my_conf_func as the call back this is NOT working. ''' prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest #MY_FUNC = CFUNCTYPE(None, POINTER(c_uint16),POINTER(c_uint8)) #MY_FUNC = CFUNCTYPE(None, c_uint16,c_uint8) MY_FUNC = CFUNCTYPE(c_uint16,c_uint8) #MY_FUNC = WINFUNCTYPE(c_uint16,c_uint8) #MY_FUNC = WINFUNCTYPE(None, POINTER(c_uint16),POINTER(c_uint8)) def my_conf_func(bat_vol, status): print "my_conf_func", bat_vol, status #return 0 conf_func = MY_FUNC(my_conf_func) print "breakpoint" #prod_bat_vol_read_request(conf_func) #prod_bat_vol_read_request(my_conf_func) #prod_bat_vol_read_request(POINTER(my_conf_func)) #************************************************************************** > -----Original Message----- > From: Tutor [mailto:tutor-bounces+pete.wilson=atmel.com at python.org] On > Behalf Of Alan Gauld > Sent: Thursday, October 16, 2014 5:24 PM > To: tutor at python.org > Subject: Re: [Tutor] Registering callbacks and .DLL > > On 17/10/14 00:35, Wilson, Pete wrote: > > I'm having problems registering a call-back with a .DLL. Maybe > someone > > has some advice on registering call-backs from .dlls. > > > > I'm using Windows 7 and Python 2.6 (32-bits). The .DLL was written in > > C++ is working with C++ apps calling it. > > OK As I understand it, You are trying to register a Python function as > a call back on a C++ function in a DLL is that correct? > > Do you have any code? I don't see any attachment. Many lists/servers > strip out binary attachments as potential viruses so it may not have > made it through. Code is best included inline as text provided its not > too long, or alternatively, in a pastebin. > > > I tried the methods in section 15.17.1.17 with the qsort() and > > CFUNCTYPE, but it is not working. My code and the .dll are attached. > > Section 15.17.1.17 of what? Not the tutorial, not the library reference > and not the language reference. So what? > > This question may be a tad too complex for the tutor list which is > targeted to those learning Python, you probably want the main list for > this. But you are going to have to provide some code and a more > detailed description of what you are trying to do. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From eryksun at gmail.com Sat Oct 18 05:53:11 2014 From: eryksun at gmail.com (eryksun) Date: Fri, 17 Oct 2014 22:53:11 -0500 Subject: [Tutor] Registering callbacks and .DLL In-Reply-To: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> References: <1A99A1517680884DAE440F5341907717872B88ED@DVRMBX02.corp.atmel.com> Message-ID: On Thu, Oct 16, 2014 at 6:35 PM, Wilson, Pete wrote: > > The .DLL was written in C++ is working with C++ apps calling it. ctypes doesn't support the platform C++ ABI (I don't think the VC++ ABI is even stable), classes, STL containers, or exceptions [*]. It isn't "cpptypes". To work with ctypes, a C++ library needs an extern "C" interface. The library you're using probably qualifies, but try a simple test program in C before jumping into ctypes. [*] On Windows ctypes has limited support for Structured Exception Handling (SEH), a Microsoft extension of ANSI C. Inadvertently it also handles any exception raised by Win32 RaiseException, such as VC++ exceptions. The code for VC++ exceptions is 0xE06D7363, i.e. "\xE0" "msc". ctypes isn't looking for this code and doesn't delve deeper to get the C++ exception type, so the OSError it raises is almost useless. Pretend this doesn't exist. SEH support is only implemented for the few cases ctypes handles explicitly such as access violations. > I tried the methods in section 15.17.1.17 with the qsort() and CFUNCTYPE, > but it is not working. My code and the .dll are attached. > > from ctypes import * > > pt_dll = cdll.LoadLibrary("c:/py_stuff/ProductionTest.dll") You can use CDLL instead. It's fewer keystrokes. from ctypes import * pt_dll = CDLL("c:/py_stuff/ProductionTest.dll") If the functions use the stdcall convention, substitute WinDLL for CDLL. If there's a mix of calling conventions you can simply load the library twice, once as CDLL and again as WinDLL. They'll each have the same _handle attribute. You can also define prototypes manually via CFUNCTYPE and WINFUNCTYPE. Then instantiate them with a 2-tuple (name_or_ordinal, library), e.g. libc = CDLL('msvcr100') atoi_t = CFUNCTYPE(c_int, c_char_p) atoi = atoi_t(('atoi', libc)) >>> atoi(b'42') 42 FYI, 64-bit Windows has a single calling convention, so if you switch to 64-bit Python you don't have to worry about cdecl vs stdcall. http://en.wikipedia.org/wiki/X86_calling_conventions > reg_send_serial_data = pt_dll.RegSendSerialData > > class SendSerialData_t(Structure): > _fields_ = [("tx_data", c_void_p), > ("size", c_uint8)] > > send_serial_data = SendSerialData_t() SendSerialData_t is a function pointer type, not a data structure. Here are the C prototypes from the attached PDF: typedef void (*SendSerialData_t) (uint8_t *tx_data, uint8_t size); void RegSendSerialData(SendSerialData_t SendSerialData); A SenedSerialData_t function takes two parameters (uint8_t *, uint8_t) and returns nothing (void). ctypes declarations: SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8) reg_send_serial_data = pt_dll.RegSendSerialData reg_send_serial_data.argtypes = [SendSerialData_t] reg_send_serial_data.restype = None The first argument to CFUNCTYPE is the return type. Use None for void. Next define the Python callback. def send_serial_data(tx_data, size): # testing print tx_data, size print tx_data[:size] cb_send_serial_data = SendSerialData_t(send_serial_data) Finally, register the callback with the library: reg_send_serial_data(cb_send_serial_data) It's vital that you keep a reference to cb_send_serial_data (as a global, an instance attribute, in a container, etc). This prevents the callback from being deallocated while it's possible the library can call it. Otherwise at best you'll get an access violation (or segfault on POSIX systems), but probably a less obvious error. Next your test code sets up ProdBatVolRequest, which is prototyped as follows: typedef void (*BatVolReadRequest_cb)(uint16_t bat_vol, uint8_t status); typedef struct { BatVolReadRequest_cb BatVolReadConf; } BatVolReadRequest_t; void ProdBatVolReadRequest(BatVolReadRequest_t BatVolReadParam); ProdBatVolReadRequest is passed a struct by value that consists of a single function pointer. You can skip defining this struct and just pass the function pointer. It's the same ABI for x86 and x64. BatVolReadRequest_t = CFUNCTYPE(None, c_uint16, c_uint8) prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest prod_bat_vol_read_request.argtypes = [BatVolReadRequest_t] prod_bat_vol_read_request.restype = None def bat_vol_read(bat_vol, status): # testing print bat_vol, status cb_bat_vol_read = BatVolReadRequest_t(bat_vol_read) prod_bat_vol_read_request(cb_bat_vol_read) Remember to keep a reference to cb_bat_vol_read. HTH From eryksun at gmail.com Sat Oct 18 06:02:36 2014 From: eryksun at gmail.com (eryksun) Date: Fri, 17 Oct 2014 23:02:36 -0500 Subject: [Tutor] Convert Qstring to string in windows In-Reply-To: <969535236.13984943.1413472898089.JavaMail.zimbra@estudiantes.uci.cu> References: <1549164247.13980291.1413472329833.JavaMail.zimbra@estudiantes.uci.cu> <969535236.13984943.1413472898089.JavaMail.zimbra@estudiantes.uci.cu> Message-ID: On Thu, Oct 16, 2014 at 10:21 AM, C at rlos wrote: > in linux i do for this way: > pythonstringtext=qstringtext.text().toUtf8.data() > and it return a python string correctly. pythonstringtext is a byte string that has to be decoded as UTF-8. Here's the 'mojibake' result when it gets decoded as UTF-16LE or Windows codepages 850 and 1252: >>> s = u'?????' >>> b = s.encode('utf-8') >>> print b.decode('utf-16le') ????? >>> print b.decode('850') ?????????? >>> print b.decode('1252') ?????????? The native character size in the Windows kernel is 2 bytes, so UTF-16 is the path of least resistance for in-memory strings used with GUI controls and file/registry paths. UTF-8 is preferred for text data in files and network communication. From grgoffe at yahoo.com Sat Oct 18 20:36:43 2014 From: grgoffe at yahoo.com (George R Goffe) Date: Sat, 18 Oct 2014 11:36:43 -0700 Subject: [Tutor] A question about using stdin/out/err vs named files Message-ID: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Hi, When you run a python program, it appears that stdin, stdout, and stderr are opened automatically. I've been trying to find out how you tell if there's data in stdin (like when you pipe data to a python program) rather than in a named input file. It seems like most/all the Unix/Linux commands are able to figure this out. Do you know how Python programs do this or might do this? MANY thanks for any/all help/hints/tips/suggestions, George... From ben+python at benfinney.id.au Sun Oct 19 02:46:13 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 19 Oct 2014 11:46:13 +1100 Subject: [Tutor] A question about using stdin/out/err vs named files References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: <85tx30nay2.fsf@benfinney.id.au> George R Goffe writes: > When you run a python program, it appears that stdin, stdout, and > stderr are opened automatically. That's true of any program on a POSIX-compliant operating system. > I've been trying to find out how you tell if there's data in stdin > (like when you pipe data to a python program) rather than in a named > input file. What does ?there's data in [a stream]? mean here, and how is it distinct from there being ?data in ? a named input file?? The advantage of the standard POSIX streams is that processes can treat them as very nearly normal files. I don't doubt you have a distinction you want to detect, but can you be clearer about what that distinction is? > It seems like most/all the Unix/Linux commands are able to figure this > out. Do you know how Python programs do this or might do this? Hmm. The standard input stream is a separate object from any named input file you might otherwise open. That's a trivially obvious difference to detect, so I guess you must not mean that. Perhaps you mean ?is the ?stdin? stream connected to an interactive terminal??. That's quite a different question from what you seem to be asking, so I don't know. -- \ ?The best is the enemy of the good.? ?Voltaire, _Dictionnaire | `\ Philosophique_ | _o__) | Ben Finney From wolfrage8765 at gmail.com Sun Oct 19 02:52:12 2014 From: wolfrage8765 at gmail.com (wolfrage8765 at gmail.com) Date: Sat, 18 Oct 2014 20:52:12 -0400 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: Are you planning to pipe data to a python program? If so please specify and you will get more complete answers. Specifically I am thinking you want information pertaining to subprocess in the standard library. https://docs.python.org/3/library/subprocess.html On Sat, Oct 18, 2014 at 2:36 PM, George R Goffe wrote: > Hi, > > When you run a python program, it appears that stdin, stdout, and stderr are opened automatically. > > I've been trying to find out how you tell if there's data in stdin (like when you pipe data to a python program) rather > than in a named input file. It seems like most/all the Unix/Linux > commands are able to figure this out. Do you know how Python programs do this or might do this? > > MANY thanks for any/all help/hints/tips/suggestions, > > George... > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Sun Oct 19 05:04:59 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Oct 2014 14:04:59 +1100 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: <20141019030459.GI18740@ando.pearwood.info> On Sat, Oct 18, 2014 at 11:36:43AM -0700, George R Goffe wrote: > Hi, > > When you run a python program, it appears that stdin, stdout, and > stderr are opened automatically. > > I've been trying to find out how you tell if there's data in stdin > (like when you pipe data to a python program) rather than in a named > input file. It seems like most/all the Unix/Linux commands are able to > figure this out. Do you know how Python programs do this or might do > this? Hmmm, good question. I've never actually done this before, but I think the right way is to just try reading from stdin and see if anything is there. Let's try it, using Python 2.7: # stdin_demo.py import sys c = sys.stdin.read(1) if c: text = c + sys.stdin.read() # Slurp everything. print "Read text from stdin:" print text else: print "Nothing in stdin." Alas, that doesn't do what I expected. It works if I pipe something into stdin: [steve at ando ~]$ echo "Some data here" | python2.7 stdin_demo.py Read text from stdin: Some data here but if stdin is empty, the call to read() blocks, waiting for input. I have to manually type something (or nothing, as the case may be) then type Ctrl-D to force end-of-file: [steve at ando ~]$ python2.7 stdin_demo.py # enter something, then Ctrl-D hello world Read text from stdin: hello world [steve at ando ~]$ python2.7 stdin_demo.py # immediately Ctrl-D Nothing in stdin. Here's my second attempt: # stdin_demo.py import sys if sys.stdin.isatty(): print "Ignoring stdin." else: c = sys.stdin.read(1) if c: text = c + sys.stdin.read() # Slurp everything. print "Read text from stdin:" print text else: print "Nothing in stdin." This version seems to do what I think you want: [steve at ando ~]$ python2.7 stdin_demo.py Ignoring stdin. [steve at ando ~]$ echo "Some data here" | python2.7 stdin_demo.py Read text from stdin: Some data here [steve at ando ~]$ echo -n "" | python2.7 stdin_demo.py Nothing in stdin. I haven't tried this under Windows, and I'm not sure if it is the "correct" way to do it under POSIX systems like Unix, Linux or Mac, but it seems to work. Oh, in case it wasn't obvious... it's probably not a good idea to slurb everything as I do above, but I leave processing stdin line-by-line as an exercise. -- Steven From __peter__ at web.de Sun Oct 19 10:05:15 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 19 Oct 2014 10:05:15 +0200 Subject: [Tutor] A question about using stdin/out/err vs named files References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: George R Goffe wrote: > When you run a python program, it appears that stdin, stdout, and stderr > are opened automatically. > > I've been trying to find out how you tell if there's data in stdin (like > when you pipe data to a python program) rather than in a named input file. > It seems like most/all the Unix/Linux commands are able to figure this > out. Do you know how Python programs do this or might do this? I don't think there is a way to guess that. Instead there is an optional commandline argument; if that is missing or '-' the script assumes stdin as the default. With argparse you spell it like this: $ cat upper.py #!/usr/bin/env python3 import argparse import sys parser = argparse.ArgumentParser(description="Convert input to uppercase") parser.add_argument( "input", type=argparse.FileType("r"), default=sys.stdin, nargs="?", help="Input file or '-' for stdin. Default: stdin.") for line in parser.parse_args().input: print(line.upper(), end="") $ ./upper.py Hello HELLO $ ./upper.py upper.py #!/USR/BIN/ENV PYTHON3 IMPORT ARGPARSE IMPORT SYS PARSER = ARGPARSE.ARGUMENTPARSER(DESCRIPTION="CONVERT INPUT TO STDIN") PARSER.ADD_ARGUMENT( "INPUT", TYPE=ARGPARSE.FILETYPE("R"), DEFAULT=SYS.STDIN, NARGS="?", HELP="INPUT FILE OR '-' FOR STDIN. DEFAULT: STDIN.") FOR LINE IN PARSER.PARSE_ARGS().INPUT: PRINT(LINE.UPPER(), END="") There is also the fileinput module. From alan.gauld at btinternet.com Sun Oct 19 10:28:01 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 19 Oct 2014 09:28:01 +0100 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: On 18/10/14 19:36, George R Goffe wrote: > When you run a python program, it appears that stdin, stdout, and stderr are opened automatically. correct. > I've been trying to find out how you tell if there's data in stdin Same way you tell if there's data in any file/stream - you read from it. In the old days when I programmed in C there were a pair of calls often used for this: getc() and ungetc(). You read a character with getc() then pout it back with ungetc(). Unfortunately they don't exist in Python stdlib. But, there is a getch()/ungetch() in the msvcrt for Windows, so you could use that. The curses module on linux has an ungetch() but no getch() - which seems bizarre... Steven has posted a solution using read(1) but that blocks so you need to use the isatty() method which all seems a bit messy and doesn't work with tty input. On further investigation the curses.screen object has a getch() method, but its not clear how the curses.ungetch() relates to that (there's no help() documentation) and I've run out of time to experiment. But if you need a linux solution that works with any kind of stdin that might be worth exploring. Sorry, not a great answer but hopefully its of some use. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jarod_v6 at libero.it Sun Oct 19 13:32:27 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Sun, 19 Oct 2014 13:32:27 +0200 (CEST) Subject: [Tutor] python draw christman tree using loops Message-ID: <12201833.2224411413718347686.JavaMail.httpd@webmail-35.iol.local> Dear All, or improve my understanding o python I would like to learn how to draw simple figures using loops. I start from this code in java: import java.util.Scanner; public class Eserc2_8 { public static void main(String args[]) { Scanner s = new Scanner(System.in); // Altezza del triangolo int altezza = s.nextInt(); for (int riga = 1; riga <= altezza; riga++) { int i = 0; while (i References: <12201833.2224411413718347686.JavaMail.httpd@webmail-35.iol.local> Message-ID: On Sun, Oct 19, 2014 at 7:32 AM, jarod_v6 at libero.it wrote: > > Dear All, > or improve my understanding o python I would like to learn how to draw simple > figures using loops. > I start from this code in java: > import java.util.Scanner; > > public class Eserc2_8 { > public static void main(String args[]) { > Scanner s = new Scanner(System.in); > // Altezza del triangolo > int altezza = s.nextInt(); > > for (int riga = 1; riga <= altezza; riga++) { > int i = 0; > while (i System.out.print(" "); > i += 1; > } > i = 0; > while (i< (riga*2)-1) { > System.out.print("*"); > i += 1; > } > System.out.println(""); > } > } > } > > and I want to transfer to python loops: > > for i in range(10): > #space > for t in range(6-i): > print "" > for item in range(i+1): > print "." > > > But I ha not succed. So someone could you please explain how can work' > thanks a lot > bw > what is your output? What errors (print complete traceback) You probably want print " ", and print ".", so that there is no added newline. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com From david at graniteweb.com Sun Oct 19 15:10:53 2014 From: david at graniteweb.com (David Rock) Date: Sun, 19 Oct 2014 08:10:53 -0500 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: <20141019131053.GB1276@wdfs> * Peter Otten <__peter__ at web.de> [2014-10-19 10:05]: > George R Goffe wrote: > > > When you run a python program, it appears that stdin, stdout, and stderr > > are opened automatically. > > > > I've been trying to find out how you tell if there's data in stdin (like > > when you pipe data to a python program) rather than in a named input file. > > It seems like most/all the Unix/Linux commands are able to figure this > > out. Do you know how Python programs do this or might do this? > > There is also the fileinput module. I use fileinput all the time. "This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed." It gives a fairly clean way to just "do the Right Thing" whether you are feeding files, or reading from stdin. -- David Rock david at graniteweb.com From grgoffe at yahoo.com Sun Oct 19 14:32:16 2014 From: grgoffe at yahoo.com (George R Goffe) Date: Sun, 19 Oct 2014 05:32:16 -0700 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: References: Message-ID: <1413721936.75294.YahooMailNeo@web310204.mail.ne1.yahoo.com> Hi, Wow. Lots of feedback. REALLY GOOD FEEDBACK! This was my first question to this list. Let me clarify my question. I want to use tst.py as follows: tst.py input-file output-file OR cat data-file | tst.py - output-file OR cat data-file | tst.py output-file tst.py input-file output-file works well tst.py - output-file works well The question boils down to "tst.py output-file"... which is a "parsing the args" question which you "guys" have caused me to think more clearly about. If there's just 1 arg, consider it an output-file and read from stdin and write to output-file ONLY if output-file does not exist. Thank you all for your very helpful and informative responses. Regards, George... From joel.goldstick at gmail.com Sun Oct 19 15:43:26 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 19 Oct 2014 09:43:26 -0400 Subject: [Tutor] python draw christman tree using loops In-Reply-To: <235009006.1195201413725265309.JavaMail.defaultUser@defaultHost> References: <235009006.1195201413725265309.JavaMail.defaultUser@defaultHost> Message-ID: On Sun, Oct 19, 2014 at 9:27 AM, jarod_v6 at libero.it wrote: > thanks for the help: > In [60]: for i in range(10): > ....: for t in range(6-i): > ....: print " " > ....: for item in range(i+1): > ....: print "*" > ....: > > * > > * > > * > > * > > * > > * > > * > * > > * > * > > * > * > > * > * > > * > * > > * > * > * > > * > * > * > > * > * > * > > * > * > * > > * > * > * > * > > * > * > * > * > > * > * > * > * > > * > * > * > * > * > > * > * > * > * > * > > * > * > * > * > * > * > > this i the output.. it is not a tree.. > > >>----Messaggio originale---- >>Da: joel.goldstick at gmail.com >>Data: 19/10/2014 14.10 >>A: "jarod_v6 at libero.it" >>Cc: , "tutor at python.org" >>Ogg: Re: [Tutor] python draw christman tree using loops >> >>On Sun, Oct 19, 2014 at 7:32 AM, jarod_v6 at libero.it > wrote: >>> >>> Dear All, >>> or improve my understanding o python I would like to learn how to draw > simple >>> figures using loops. >>> I start from this code in java: >>> import java.util.Scanner; >>> >>> public class Eserc2_8 { >>> public static void main(String args[]) { >>> Scanner s = new Scanner(System.in); >>> // Altezza del triangolo >>> int altezza = s.nextInt(); >>> >>> for (int riga = 1; riga <= altezza; riga++) { >>> int i = 0; >>> while (i>> System.out.print(" "); >>> i += 1; >>> } >>> i = 0; >>> while (i< (riga*2)-1) { >>> System.out.print("*"); >>> i += 1; >>> } >>> System.out.println(""); >>> } >>> } >>> } >>> >>> and I want to transfer to python loops: >>> >>> for i in range(10): >>> #space >>> for t in range(6-i): >>> print "" >>> for item in range(i+1): >>> print "." >>> >>> >>> But I ha not succed. So someone could you please explain how can work' >>> thanks a lot >>> bw >>> >>what is your output? What errors (print complete traceback) >> >>You probably want print " ", and print ".", so that there is no added > newline. >>> In python 2.x you can add a comma to the print statement so that it will not generate a newline. That way your *s will print across. After you loop use plain print to go to next line. Also, reply to the list, not to me. >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >> >> >> >>-- >>Joel Goldstick >>http://joelgoldstick.com >> > > -- Joel Goldstick http://joelgoldstick.com From dyoo at hashcollision.org Sun Oct 19 21:00:14 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 19 Oct 2014 12:00:14 -0700 Subject: [Tutor] python draw christman tree using loops In-Reply-To: <12201833.2224411413718347686.JavaMail.httpd@webmail-35.iol.local> References: <12201833.2224411413718347686.JavaMail.httpd@webmail-35.iol.local> Message-ID: In Java, System.out represents the standard output device. In Python, there's a similar value in sys.stdout. https://docs.python.org/2/library/sys.html#sys.stdout In your Python program, you should be able to say: import sys at the beginning of your program, and then use: sys.stdout.write(" ") and: sys.stdout.write("*") with the expected results. From alruheili at berkeley.edu Sun Oct 19 16:27:25 2014 From: alruheili at berkeley.edu (AMNA MOHAMMED ALRUHEILI) Date: Sun, 19 Oct 2014 07:27:25 -0700 Subject: [Tutor] Need help to convert pdf to excel Message-ID: Hell, My name is Amna and I am totally new to python world with zero experience in programming. I am facing the challenge of converting data from pdf to excel. The data within pdf is numbers separated by space not within a table. I need a help to figure out a code that help me to convert these pdf to excel. How can I do that. Thank you, Amna -------------- next part -------------- An HTML attachment was scrubbed... URL: From hanzer at riseup.net Sun Oct 19 18:38:43 2014 From: hanzer at riseup.net (Adam Jensen) Date: Sun, 19 Oct 2014 12:38:43 -0400 Subject: [Tutor] A question about using stdin/out/err vs named files In-Reply-To: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> References: <1413657403.99078.YahooMailNeo@web310209.mail.ne1.yahoo.com> Message-ID: <5443E913.9030607@riseup.net> On 10/18/2014 02:36 PM, George R Goffe wrote: > Hi, > > When you run a python program, it appears that stdin, stdout, and stderr are opened automatically. > > I've been trying to find out how you tell if there's data in stdin (like when you pipe data to a python program) rather > than in a named input file. It seems like most/all the Unix/Linux > commands are able to figure this out. Do you know how Python programs do this or might do this? > > MANY thanks for any/all help/hints/tips/suggestions, > > George... Command line argument parsing aside, perhaps something like this would be useful: script.py ------------------------------------------- #!/usr/bin/env python3 import os, stat mode = os.fstat(0).st_mode if stat.S_ISFIFO(mode): print("stdin is a pipe") elif stat.S_ISREG(mode): print("stdin is a redirected file") elif stat.S_ISCHR(mode): print("stdin is terminal") else: print("stdin is weird") ------------------------------------------- $ ./script.py stdin is terminal $ cat script.py | ./script.py stdin is a pipe $ ./script.py < script.py stdin is a redirected file From dyoo at hashcollision.org Sun Oct 19 23:59:49 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 19 Oct 2014 14:59:49 -0700 Subject: [Tutor] Need help to convert pdf to excel In-Reply-To: References: Message-ID: On Sun, Oct 19, 2014 at 7:27 AM, AMNA MOHAMMED ALRUHEILI wrote: > My name is Amna and I am totally new to python world with zero experience in > programming. I am facing the challenge of converting data from pdf to excel. > The data within pdf is numbers separated by space not within a table. > I need a help to figure out a code that help me to convert these pdf to > excel. Can you get the original data in a format that is not PDF? PDF is a human-centric published format: its concerns are typographic. It is not intended primarily for computers to extract data. Certainly you can extract data from it, but it won't be structured in a way that makes it particularly easy. That being said: you might try an approach that gets text from a PDF: http://stackoverflow.com/questions/25665/python-module-for-converting-pdf-to-text and then write some processing program that takes that text and inserts into an excel spreadsheet. There are several libraries that other programmers have written to write Excel documents from Python. For example: https://xlsxwriter.readthedocs.org/ As a side comment: this task actually does require a significant bit of programming knowledge. I believe it would be an unfair and unreasonable request to ask a complete programming beginner to do this without some basic programming training. I would strongly recommend taking an introductory programming class first, before trying to tackle your PDF->Excel problem head on. From crk at godblessthe.us Mon Oct 20 00:26:44 2014 From: crk at godblessthe.us (Clayton Kirkwood) Date: Sun, 19 Oct 2014 15:26:44 -0700 Subject: [Tutor] what am I not understanding? Message-ID: <050a01cfebeb$c3d4d160$4b7e7420$@us> raw_table = (''' a: Ask y: Dividend Yield b: Bid d: Dividend per Share b2: Ask (Realtime) r1: Dividend Pay Date b3: Bid (Realtime) q: Ex-Dividend Date p: Previous Close o: Open''') key_name = raw_table.rstrip('\t') print(key_name) a: Ask y: Dividend Yield b: Bid d: Dividend per Share b2: Ask (Realtime) r1: Dividend Pay Date b3: Bid (Realtime) q: Ex-Dividend Date p: Previous Close o: Open #why is the tab not being removed? key_name = raw_table.rstrip('\n') print(key_name) a: Ask y: Dividend Yield b: Bid d: Dividend per Share b2: Ask (Realtime) r1: Dividend Pay Date b3: Bid (Realtime) q: Ex-Dividend Date p: Previous Close o: Open # why is the \n not being removed? key_name = raw_table.split('\t') print(key_name) ['\na: Ask', 'y: Dividend Yield\nb: Bid', 'd: Dividend per Share\nb2: Ask (Realtime)', 'r1: Dividend Pay Date\nb3: Bid (Realtime)'. #great the tab is being removed but now I have to remove the \n but it is no longer a string key_name = raw_table.split('\n') print(key_name) ['', 'a: Ask\ty: Dividend Yield', 'b: Bid\td: Dividend per Share', 'b2: Ask (Realtime)\tr1: Dividend Pay Date', 'b3: Bid (Realtime)\tq: Ex-Dividend Date'. #great, the \n is being "removed" but now I have to remove the \t, but it is no longer a string key_name = raw_table.split('\t\n') print(key_name) ['\na: Ask\ty: Dividend Yield\nb: Bid\td: Dividend per Share\nb2: Ask (Realtime)\tr1: Dividend Pay Date\nb3: Bid (Realtime)\tq: Ex-Dividend Date\n. #why isn't the \t and \n not both "removed" key_name = raw_table.rstrip('[\t\n]') a: Ask y: Dividend Yield b: Bid d: Dividend per Share b2: Ask (Realtime) r1: Dividend Pay Date b3: Bid (Realtime) q: Ex-Dividend Date p: Previous Close o: Open #why aren't both the \t and \n being removed? (tried with and without the square brackets) I am trying to get to where I can create a dict using the ':' separator Thanks Clayton -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Oct 20 02:03:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Oct 2014 01:03:12 +0100 Subject: [Tutor] what am I not understanding? In-Reply-To: <050a01cfebeb$c3d4d160$4b7e7420$@us> References: <050a01cfebeb$c3d4d160$4b7e7420$@us> Message-ID: On 19/10/14 23:26, Clayton Kirkwood wrote: > raw_table = (''' > a: Ask y: Dividend Yield > b: Bid d: Dividend per Share > b2: Ask (Realtime) r1: Dividend Pay Date ... > o: Open???) > > key_name = raw_table.rstrip('\t') rstrip() strips characters from the *right* hand side. When it finds a character that is not a strippable character(the n of Open in this case) it stops. If you want to replace all tabs with spaces or nulls then you need to use string.replace() (or sub for regex) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon Oct 20 03:16:23 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Oct 2014 12:16:23 +1100 Subject: [Tutor] what am I not understanding? In-Reply-To: <050a01cfebeb$c3d4d160$4b7e7420$@us> References: <050a01cfebeb$c3d4d160$4b7e7420$@us> Message-ID: <20141020011623.GM18740@ando.pearwood.info> On Sun, Oct 19, 2014 at 03:26:44PM -0700, Clayton Kirkwood wrote: > raw_table = (''' > a: Ask y: Dividend Yield [...] > o: Open''') > key_name = raw_table.rstrip('\t') > print(key_name) [...] > #why is the tab not being removed? How do you know that the raw_table contains tabs rather than spaces? As far as I can see, it contains spaces, although that might just be an artifact of copying and pasting text into your email. You think you have a problem with tabs not being removed, but it seems to me that your *actual* problem is that you're not really sure whether there are tabs in the string in the first place! Just because you press the TAB key on your keyboard doesn't mean that a tab is inserted. Your system might be confirgured to insert a number of spaces instead. Your editor might automatically convert the tab into spaces. Who knows? Instead of typing TAB, the best way to get tabs into a Python string is to use the \t (backslash-t) escape code. And the best way to see that they are inside a string is to print the repr() of the string: py> raw = " some text " py> print raw # Looks like a leading tab, trailing tab is invisible. some text py> print repr(raw) # But repr() reveals the truth, no tabs here! ' some text ' py> raw = "\tsome text\t" # Actually are tabs. py> print raw some text py> print repr(raw) '\tsome text\t' So the first thing for you to do is to satisfy yourself that what you think are tabs actually are tabs. Then, the second thing is to understand what the rstrip() method actually does. It only removes characters from the right hand end of the string, not everywhere in the string: py> raw = "--some-text----" py> print raw.rstrip("-") --some-text (There is also a lstrip() to do only the left hand end of the string, and a strip() method to do both.) "Right hand end" doesn't mean the right hand end of *each line*, only of the entire string: py> raw = """---some-text---- ... --more--text--- ... -and-even-more--text-----""" py> print raw.rstrip("-") ---some-text---- --more--text--- -and-even-more--text If you want to strip something from the end of each line, first you have to break the string up into individual lines, strip each one, then put them back together again: py> lines = raw.split('\n') py> lines = [line.rstrip('-') for line in lines] py> print '\n'.join(lines) ---some-text --more--text -and-even-more--text To remove from the middle of the string, use the replace() method: py> print raw.replace("-", "") sometext moretext andevenmoretext > key_name = raw_table.split('\t') > print(key_name) [...] > #great the tab is being removed but now I > have to remove the \n but it is no longer a string Right. The split() method isn't a "remove" method, that's why it's called "split" and not "remove". It splits the string into pieces, and returns a list of substrings. You can always join the pieces back together, if you want, or find a better way to remove things. > key_name = raw_table.split('\t\n') > print(key_name) [...] > #why isn't the \t and \n not both "removed" Because you didn't tell Python to remove \t and \n separately. You told it to split the string everywhere it sees the pair of characters \t\n. The argument to split is not a list of individual characters to split on, but an exact substring to split on. If it doesn't match that substring exactly, it won't split and you'll only get one piece: py> print raw ---some-text---- --more--text--- -and-even-more--text----- py> raw.split('ex') ['---some-t', 't----\n--more--t', 't---\n-and-even-more--t', 't-----'] py> raw.split('xe') ['---some-text----\n--more--text---\n-and-even-more--text-----'] > I am trying to get to where I can create a dict using the ':' separator It takes about three steps (excluding print statements, which I have only included so you can see the individual steps in action): # Step 1: split the string into "key:value" substrings. py> raw = "a : something, b: another thing, c:yet a third thing" py> items = [s.strip() for s in raw.split(',')] py> print items ['a : something', 'b: another thing', 'c:yet a third thing'] # Step 2: split each substring into a separate (key, value) tuple, # cleaning up any whitespace around them. py> items = [s.split(':') for s in items] py> print items [['a ', ' something'], ['b', ' another thing'], ['c', 'yet a third thing']] py> items = [(key.strip(), value.strip()) for key,value in items] py> print items [('a', 'something'), ('b', 'another thing'), ('c', 'yet a third thing')] # Step 3: convert into a dict. py> d = dict(items) py> print d {'a': 'something', 'c': 'yet a third thing', 'b': 'another thing'} -- Steven From __peter__ at web.de Mon Oct 20 10:30:47 2014 From: __peter__ at web.de (Peter Otten) Date: Mon, 20 Oct 2014 10:30:47 +0200 Subject: [Tutor] what am I not understanding? References: <050a01cfebeb$c3d4d160$4b7e7420$@us> Message-ID: Clayton Kirkwood wrote: > raw_table = (''' > a: Ask y: Dividend Yield > b: Bid d: Dividend per Share > b2: Ask (Realtime) r1: Dividend Pay Date > b3: Bid (Realtime) q: Ex-Dividend Date > p: Previous Close > o: Open''') > o: Open #why aren't both the \t and \n > being removed? (tried with and without the square brackets) > I am trying to get to where I can create a dict using the ':' separator You never state your actual problem with sufficient precision, so I'm going to assume: - Create a Python dict from some text - Key-value pairs are separated by tabs or newlines - Keys are separated from values by a colon - Remaining whitespace surrounding keys and values needs to be stripped So >>> raw_table = '''\ ... a: Ask\t y: Dividend Yield ... b: Bid\td: Dividend per Share ... b2: Ask (Realtime)\t r1: Dividend Pay Date ... b3: Bid (Realtime)\tq: Ex-Dividend Date ... p: Previous Close ... o: Open''' I used \t as tabs, but literal tabs will work, too. The backslash in the first line is to avoid the empty first line. alternatively remove it and other leading/trailing whitespace with raw_table = raw_table.strip() >>> pairs = [pair for line in raw_table.splitlines() for pair in line.split("\t")] >>> pairs ['a: Ask', ' y: Dividend Yield', 'b: Bid', 'd: Dividend per Share', 'b2: Ask (Realtime)', ' r1: Dividend Pay Date', 'b3: Bid (Realtime)', 'q: Ex-Dividend Date', 'p: Previous Close', 'o: Open'] >>> pairs = [pair.partition(":")[::2] for pair in pairs] pair.split(":") instead of partition()[::2] would work, too. >>> pairs [('a', ' Ask'), (' y', ' Dividend Yield'), ('b', ' Bid'), ('d', ' Dividend per Share'), ('b2', ' Ask (Realtime)'), (' r1', ' Dividend Pay Date'), ('b3', ' Bid (Realtime)'), ('q', ' Ex-Dividend Date'), ('p', ' Previous Close'), ('o', ' Open')] >>> d = {k.strip(): v.strip() for k, v in pairs} >>> d {'b2': 'Ask (Realtime)', 'q': 'Ex-Dividend Date', 'b3': 'Bid (Realtime)', 'r1': 'Dividend Pay Date', 'd': 'Dividend per Share', 'y': 'Dividend Yield', 'p': 'Previous Close', 'b': 'Bid', 'a': 'Ask', 'o': 'Open'} From taylor.ruzgys at hotmail.com Mon Oct 20 04:56:57 2014 From: taylor.ruzgys at hotmail.com (Taylor Ruzgys) Date: Sun, 19 Oct 2014 22:56:57 -0400 Subject: [Tutor] Python Help Message-ID: Hi, I was wondering if you could help me with an assignment that I'm doing involving Python? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Oct 20 12:31:02 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Oct 2014 11:31:02 +0100 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On 20/10/14 03:56, Taylor Ruzgys wrote: > Hi, I was wondering if you could help me with an assignment that I'm > doing involving Python? Yes, we can help you do it. We won't do it for you. You need to tell us what the assignment is, how you have tried to solve it, including any code you've written. Explain where and how you are stuck. Include the full text of any error messages. Tell us the Python version and OS you use. Tell us if you are using any non standard libraries/modules -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From crushed26 at gmail.com Mon Oct 20 14:34:34 2014 From: crushed26 at gmail.com (Bo Morris) Date: Mon, 20 Oct 2014 08:34:34 -0400 Subject: [Tutor] Insert time into email Message-ID: hello all, hope everyone is doing well. The below code works, however I am going back and trying to enter the time and date and I cant quite figure out how to do this without breaking the code. #!/usr/bin/python import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import time strFrom = "HourlyReport.com" #strTo = "engineering at oneconnxt.com" #strTo = "mmedley at onemediacorpinc.com" strTo = "bo at onemediacorpinc.com" l = ['3102EHD-01108.png', '3102DHD-01109.png','3102EHD-01082.png', '3102DHD-01033.png', '3102EHD-01302.png', '3102DHD-01149.png', '3102EHD-01125.png', '3102DHD-01144.png', '3102EHD-01105.png'] t = time.strftime("%H:%M:%S") d = time.strftime("%d/%m/%Y") msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'Test Hourly Report' msgRoot['From'] = strFrom msgRoot['To'] = strTo msgRoot.preamble = 'This is a multi-part message in MIME format.' msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) msgText = MIMEText('
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    TIMEDATE
    ', 'html') msgAlternative.attach(msgText) for image in l: with open(image, 'rb') as fh: msgImage = MIMEImage(fh.read()) msgImage.add_header('Content-ID', '<{0}>'.format(image)) msgRoot.attach(msgImage) try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(strFrom, strTo, msgRoot.as_string()) print "Successfully sent email" except smtplib.SMTPException: print "Error: unable to send email" I need to enter the time and date in the html where "TIME" and "DATE" are. I imagine I can do this by adding "cid: t" and "cid:d" which just refers back to t = "time.strftime("%H:%M:%S")" "d = time.strftime("%d/%m/%Y")"? Thanks in advance for any help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Mon Oct 20 18:02:14 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 20 Oct 2014 18:02:14 +0200 Subject: [Tutor] Insert time into email In-Reply-To: References: Message-ID: On Mon, Oct 20, 2014 at 2:34 PM, Bo Morris wrote: > hello all, hope everyone is doing well. > > The below code works, however I am going back and trying to enter the time > and date and I cant quite figure out how to do this without breaking the > code. > > #!/usr/bin/python > > import smtplib > from email.MIMEMultipart import MIMEMultipart > from email.MIMEText import MIMEText > from email.MIMEImage import MIMEImage > import time > > strFrom = "HourlyReport.com" PS. You may want to use a real e-mail address here. Or, at the very least, something that looks like one. > #strTo = "engineering at oneconnxt.com" > #strTo = "mmedley at onemediacorpinc.com" > strTo = "bo at onemediacorpinc.com" > > l = ['3102EHD-01108.png', '3102DHD-01109.png','3102EHD-01082.png', > '3102DHD-01033.png', '3102EHD-01302.png', '3102DHD-01149.png', > '3102EHD-01125.png', '3102DHD-01144.png', '3102EHD-01105.png'] > > t = time.strftime("%H:%M:%S") > d = time.strftime("%d/%m/%Y") > > msgRoot = MIMEMultipart('related') > msgRoot['Subject'] = 'Test Hourly Report' > msgRoot['From'] = strFrom > msgRoot['To'] = strTo > msgRoot.preamble = 'This is a multi-part message in MIME format.' > > msgAlternative = MIMEMultipart('alternative') > msgRoot.attach(msgAlternative) > > msgText = MIMEText('This is the alternative plain text message.') > msgAlternative.attach(msgText) > > msgText = MIMEText('
    src="cid:3102EHD-01108.png" width="400" > height="300">
    TIMEDATE
    src="cid:3102DHD-01109.png" width="400" > height="300">
    TIMEDATE
    cellspacing="15" border="1">
    width="400" > height="300">
    TIMEDATE
    src="cid:3102DHD-01033.png" width="400" > height="300">
    TIMEDATE
    cellspacing="15" border="1">
    width="400" > height="300">
    TIMEDATE
    src="cid:3102DHD-01149.png" width="400" > height="300">
    TIMEDATE
    cellspacing="15" border="1">
    width="400" > height="300">
    TIMEDATE
    src="cid:3102DHD-01144.png" width="400" > height="300">
    TIMEDATE
    cellspacing="15" border="1">
    width="400" > height="300">
    TIMEDATE
    ', > 'html') > msgAlternative.attach(msgText) > > for image in l: > with open(image, 'rb') as fh: > msgImage = MIMEImage(fh.read()) > msgImage.add_header('Content-ID', '<{0}>'.format(image)) > msgRoot.attach(msgImage) > > > try: > smtpObj = smtplib.SMTP('localhost') > smtpObj.sendmail(strFrom, strTo, msgRoot.as_string()) > print "Successfully sent email" > except smtplib.SMTPException: > print "Error: unable to send email" > > I need to enter the time and date in the html where "TIME" and "DATE" are. I > imagine I can do this by adding "cid: t" and "cid:d" which just refers back > to t = "time.strftime("%H:%M:%S")" "d = time.strftime("%d/%m/%Y")"? No, not really. cid: is for images. You want to insert actual text. Use string formatting. '?snip?{time}{date}?snip?'.format(time=t, date=d) This is assuming every date is the same. If it isn?t, you?d have to use different identifiers and values. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From juan0christian at gmail.com Mon Oct 20 19:04:56 2014 From: juan0christian at gmail.com (Juan Christian) Date: Mon, 20 Oct 2014 15:04:56 -0200 Subject: [Tutor] Python sqlite3 issue Message-ID: I have this code (http://pastebin.com/Q21vQdHZ): import sqlite3 db = sqlite3.connect('db.sqlite') def create_db(): db.execute(''' CREATE TABLE TOPICS( ID INT PRIMARY KEY NOT NULL, URL VARCHAR NOT NULL, AUTHOR VARCHAR NOT NULL, MESSAGE VARCHAR NOT NULL ); ''') def insert_db(_id, url, author, message): db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({}, {}, {}, {})".format(_id, url, author, message)) db.commit() def get_db(_id): cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE ID = {}".format(_id)) return cursor.fetchone() if __name__ == '__main__': create_db() insert_db(12, 'abc.com', 'a', 'b') get_db(12) db.close() And when I try to execute it I get: First time executing: Traceback (most recent call last): File ".\sql.py", line 29, in insert_db(12, 'abc.com', 'a', 'b') File ".\sql.py", line 18, in insert_db db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({}, {}, {}, {})".format(_id, url, author, message) ) sqlite3.OperationalError: no such column: abc.com Second time executing: Traceback (most recent call last): File ".\sql.py", line 28, in create_db() File ".\sql.py", line 14, in create_db ''') sqlite3.OperationalError: table TOPICS already exists What's the problem? It's just a test script just to learn sqlite3 with python. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Mon Oct 20 19:32:12 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 20 Oct 2014 10:32:12 -0700 Subject: [Tutor] Python sqlite3 issue In-Reply-To: References: Message-ID: Hi Juan, On Mon, Oct 20, 2014 at 10:04 AM, Juan Christian wrote: > I have this code (http://pastebin.com/Q21vQdHZ): > > import sqlite3 [code cut] > def insert_db(_id, url, author, message): > db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES ({}, > {}, {}, {})".format(_id, url, author, message)) [code cut] Ah. Do not use normal string formatting when you're dealing with SQL. In web programming, we can introduce script injection problems when we use naive string concatenation. Unsurprisingly, the same class of injection attack can be done in SQL when we use naive string concatenation to build SQL statements. See: http://xkcd.com/327/ I'm being serious! :P Look for the concept of "prepared statements" or "parameter substitution" or "placeholder" in the sqllite3 Python bindings: it should let you safely construct the statement templates. In the documentation here: https://docs.python.org/2/library/sqlite3.html search for the word "placeholder", and you should see the relevant material. From kwpolska at gmail.com Mon Oct 20 19:38:59 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 20 Oct 2014 19:38:59 +0200 Subject: [Tutor] Insert time into email In-Reply-To: <323D4157-6573-4BC8-9F65-78534C334152@gmail.com> References: <323D4157-6573-4BC8-9F65-78534C334152@gmail.com> Message-ID: Forwarding to list ? please use Reply All? next time. On Mon, Oct 20, 2014 at 6:28 PM, Crush wrote: > Where does the ."format(time=t,date=d)" go? > > I would assume something like > > msgTime = MIMEText() > msgTime.add_header('not sure what to put here?' '