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. 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] could somebody please explain...
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Whack-a-mole
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] could somebody please explain...
!-Original Message- !From: Danny Yoo [mailto:d...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] could somebody please explain...
!-Original Message- !From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On !Behalf Of Steven D'Aprano !Sent: Tuesday, September 30, 2014 7:34 PM !To: tutor@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@python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] could somebody please explain...
!-Original Message- !From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On !Behalf Of Steven D'Aprano !Sent: Tuesday, September 30, 2014 6:38 PM !To: tutor@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: ! ! OperatorFunctionDunder 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 =
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? 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.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Whack-a-mole
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] VERY basic question
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] VERY basic question
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Beautifulsoup4 question
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? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] VERY basic question
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. '' >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beautifulsoup4 question
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beautifulsoup4 question
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! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beautifulsoup4 question
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: http://media.steampowered.com/apps/440/icons/wading_crate_2.dea09767ade382b0151eb6251d1e5b6deaf8ab75.png)">#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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor