Re: Python Newbie
piterrr.dolin...@gmail.com wrote: > You see, Javascript, for one, behaves the same way as Python (no variable > declaration) but JS has curly braces and you know the variable you have > just used is limited in scope to the code within the { }. With Python, you > have to search the whole file. I don't know if you know javascript, but I think your experience with it is limited, as you listed two of of the many common pitfalls of javascript: javascript has optional variable declaration, but if you don't declare a variable (such as a local variable in a function) it binds itself in the global scope; in c/c++/c#/java... curly braces define a new scope, in javascript no. if you do in javascript function foo() { if (some condition) { a = 'something'; } alert(a); } the moment you call the foo() function (and some condition is true), a global variable "a" is created. (this time you *really* need to search not the whole .js file, but the whole project, as there is no "file-level" globals in javascript) if you instead use the 'var' keyword to define explicitly a local variable function foo() { if (some condition) { var a = 'something'; } alert(a); } while you don't create a global variable, the "a" variable is still accessible outside the "if" braces. this is the reason most javascript linters (most notably jslint) force the first statement of every function to be the definition of all function variables. by this point of view, python and javascript have the same philosophy, but at least python doesn't allow implicit global variable definition -- ZeD -- http://mail.python.org/mailman/listinfo/python-list
Re: encoding error in python 27
Hala Gamal wrote: > thank you :)it worked well for small file but when i enter big file,, i > obtain this error: "Traceback (most recent call last): > File "D:\Python27\yarab (4).py", line 46, in > writer.add_document(**doc) > File "build\bdist.win32\egg\whoosh\filedb\filewriting.py", line 369, in > add_document > items = field.index(value) > File "build\bdist.win32\egg\whoosh\fields.py", line 466, in index > return [(txt, 1, 1.0, '') for txt in self._tiers(num)] > File "build\bdist.win32\egg\whoosh\fields.py", line 454, in _tiers > yield self.to_text(num, shift=shift) > File "build\bdist.win32\egg\whoosh\fields.py", line 487, in to_text > return self._to_text(self.prepare_number(x), shift=shift, > File "build\bdist.win32\egg\whoosh\fields.py", line 476, in > prepare_number > x = self.type(x) > UnicodeEncodeError: 'decimal' codec can't encode characters in position > 0-4: invalid decimal Unicode string" i don't know realy where is the > problem? On Friday, February 22, 2013 4:55:22 PM UTC+2, Hala Gamal wrote: >> my code works well with english file but when i use text file >> encodede"utf-8" "my file contain some arabic letters" it doesn't work. I guess that one of the fields you require to be NUMERIC contains non-digit characters. Replace the line >> writer.add_document(**doc) with something similar to try: writer.add_document(**doc) except UnicodeEncodeError: print "Skipping malformed line", repr(i) This will allow you to inspect the lines your script cannot handle and if they are indeed "malformed" as I am guessing you can fix your input data. i is a terrible name for a line in a file, btw. Also, you should avoid readlines() which reads the whole file into memory and instead iterate over the file object directly: with codecs.open("tt.txt", encoding='utf-8-sig') as textfile: for line in textfile: # no readlines(), can handle # text files of arbitrary size ... -- http://mail.python.org/mailman/listinfo/python-list
Re: PyQT app accessible over network?
On Sat, Feb 23, 2013 at 10:37 AM, Michael Torrie wrote: > On 02/22/2013 02:49 PM, Monte Milanuk wrote: >> Web2py does seem pretty attractive in that it seems to come with a lot >> of functionality rolled in already. It seems to be pretty easy to >> deploy... since this would be more of a case where the volunteer match >> directors are not necessarily computer gurus, and something that can >> literally run from a USB stick on nearly any computer has its benefits. >> I've seen some examples (I think) of twitter-bootstrap in some other >> demos of flask, and it looked reasonably attractive without being too >> over the top. web2py's DAL seems fairly straight-forward too. Looks >> like I may have to get more fluent in CSS & javascript, though... > > If you just use web2py to implement the database calls and business > logic, and to implement a simple, clean API (RPC really) for the clients > to talk to, then you can still use your non-web UI tools like PyQt. But > as an added bonus you can do a web interface as well. You'll have > flexibility either way. A client is a client, whether it's web-bases > and running on the same server, or a remote app using RPC over HTTP. > > I think all web-based apps should expose a web service (an API). that > way you have flexibility to do a variety of front-ends. Normal web > browser, mobile browser, a standalone app (think android or iphone). > > As far as doing client/server stuff with just a database engine, unless > you have tight control over the environment end to end, from a security > pov, it's not a good idea to expose the database engine itself to the > internet. Better to put a restricted web services API in front of it > that handles all the authorization needs (access-control) on the > detailed level that you require. > -- > http://mail.python.org/mailman/listinfo/python-list Michael Torrie: Have seen a few PyWt examples in alpha if that's what you describing… But there would still be more implementation overhead then just using e.g.: SQLFORM(db.table_name) to create a CRUD form. I don't see any disadvantage of using web2py for everything; unless we're talking decentralised infrastructure in which case a queuing mechanism would likely be better; and have each client implement a server as well. (thus still no use-case for Qt). Also SQLite has a number of excellent features, namely 2 file deployments. So it's very portable. Otherwise for postgres or mysql you'd probably need to package in your own silent installer (which admittedly isn't overly difficult; but is quite involved)… On Sat, Feb 23, 2013 at 8:49 AM, Monte Milanuk wrote: > Looks like I may have to get more fluent in > CSS & javascript, though... Understanding how `style` attributes work, how to use FireBug (or Chrome Dev Tools); and finding a good javascript widget library (e.g.: from Twitter Bootstrap) should be more than enough for your project. In fact; it's been enough for almost all my projects! (though now I'm moving to AngularJS will need to get more involved on the js front :P) -- http://mail.python.org/mailman/listinfo/python-list
webbrowser.open("./documentation/help.html")-- No Go in Windows
I created an html help page for my Python 2.7.3 application and put it in a documentation folder. I used webbrowser.open() to fetch the page. On linux -- KDE specifically, the command opens the local file on my default browser with no issues. However, on Windows 7, it opens Internet Explorer, which doesn't even search the local folder, but goes straight to the web and does a Google search, returning nothing but useless noise. My default browser on Windows is Chrome, so my intention is getting undermined right from the start. How do I get a local html file to open properly from Python in Windows? -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Feb 24, 2013 1:21 AM, "llanitedave" wrote: > > I created an html help page for my Python 2.7.3 application and put it in a documentation folder. I used webbrowser.open() to fetch the page. > > On linux -- KDE specifically, the command opens the local file on my default browser with no issues. However, on Windows 7, it opens Internet Explorer, which doesn't even search the local folder, but goes straight to the web and does a Google search, returning nothing but useless noise. > > My default browser on Windows is Chrome, so my intention is getting undermined right from the start. > > How do I get a local html file to open properly from Python in Windows? Please provide the exact code snippet that you're using. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
Rather than using a relative path, try using webbrowser.open('{}/documentation/help.html'.format(os.path.dirname(__file__))). On Sun, Feb 24, 2013 at 1:17 AM, llanitedave wrote: > I created an html help page for my Python 2.7.3 application and put it in a > documentation folder. I used webbrowser.open() to fetch the page. > > On linux -- KDE specifically, the command opens the local file on my default > browser with no issues. However, on Windows 7, it opens Internet Explorer, > which doesn't even search the local folder, but goes straight to the web and > does a Google search, returning nothing but useless noise. > > My default browser on Windows is Chrome, so my intention is getting > undermined right from the start. > > How do I get a local html file to open properly from Python in Windows? > -- > http://mail.python.org/mailman/listinfo/python-list -- Demian Brecht http://demianbrecht.github.com -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Feb 24, 2013 1:21 AM, "llanitedave" wrote: > > I created an html help page for my Python 2.7.3 application and put it in a documentation folder. I used webbrowser.open() to fetch the page. > > On linux -- KDE specifically, the command opens the local file on my default browser with no issues. However, on Windows 7, it opens Internet Explorer, which doesn't even search the local folder, but goes straight to the web and does a Google search, returning nothing but useless noise. > > My default browser on Windows is Chrome, so my intention is getting undermined right from the start. > > How do I get a local html file to open properly from Python in Windows? Sounds like this might be your problem: http://bugs.python.org/issue8936 The fix would seem to be ensuring that the URL you pass includes the scheme (in your case, "file:"). Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] iterable.__unpack__ method
On Mon, Feb 25, 2013 at 1:16 AM, Steven D'Aprano wrote: > On 24/02/13 23:59, Chris Angelico wrote: >> >> On Sun, Feb 24, 2013 at 10:25 PM, Larry Hastings >> wrote: >>> >>> Or >>> >>> command, subcommand = next(iterargs), next(iterargs) >> >> >> Err is there a language guarantee of the order of evaluation in a >> tuple, or is this just a "CPython happens to evaluate independent >> expressions left-to-right"? This is totally broken if the next() calls >> could be done in either order. > > It's a language guarantee. > > http://docs.python.org/2/reference/expressions.html#evaluation-order Ah, so it is. My bad, sorry! In that case, sure, this works. It still violates DRY though, naming the iterable twice and relying on the reader noticing that that means "take two off this one". But that's a much weaker concern. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24/02/2013 04:20, Larry Hudson wrote: On 02/23/2013 03:46 PM, piterrr.dolin...@gmail.com wrote: Hi all, ... I have discovered today there is no do...while type loop. [Sigh] No biggie. This is easily simulated with: while True: ... if : break Less easily simulated is the lack of a switch/case structure. This has to be done with a less convenient extended if/elif/.../else structure. Or a dict, there are umpteen recipes showing how to do this. Peter -=- Larry -=- -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
Re: PyQT app accessible over network?
> My concern is that using postgres or mysql for this would be akin to > using a sledgehammer to swat a fly, I wouldn't use MySQL for anything that requires anything else than "select". And PostgreSQL has extremely spartanic resource requirements in the default configuration. It runs on Linux on hardware where (the most recent) Windows alone wouldn't run. > My other reason for wanting one 'central' app is that there are > various functions (setting up the tournament, closing registration, > editing scores, finalizing results) that I really *don't* want the > satellite/client apps to be able to do. Easy, you simply restrict access rights to the corresponding tables for the individual users. Any halfway decent database application framework will allow to configure the application correspondingly for each user. Sincerely, Wolfgang -- http://mail.python.org/mailman/listinfo/python-list
Re: PyQT app accessible over network?
> As far as doing client/server stuff with just a database engine, > unless you have tight control over the environment end to end, from a > security pov, it's not a good idea to expose the database engine > itself to the internet. Better to put a restricted web services API > in front of it that handles all the authorization needs > (access-control) on the detailed level that you require. Excuse me but that's bullshit. PostgreSQL is definitely more secure than any self-made RPC protocol with a self-made "web" server on top of SQLite that re-invents what PostgreSQL provides "out of the box" and much more efficient that http could ever do it. Experience with security of PostgreSQL servers exposed to "the internet" has been capitalised for much more than a decade now. You won't get anywhere close to that level of security (and reliability) with your private selfmade webnonsense anytime soon. And if there's anything that all those scriptkiddies know their way with it's http servers. Sincerely, Wolfgang -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] iterable.__unpack__ method
On 2013-02-25 01:19, Chris Angelico wrote: > >>> command, subcommand = next(iterargs), next(iterargs) > >> > >> > >> Err is there a language guarantee of the order of evaluation > >> in a tuple, or is this just a "CPython happens to evaluate > >> independent expressions left-to-right"? This is totally broken > >> if the next() calls could be done in either order. > > > > It's a language guarantee. > > > > http://docs.python.org/2/reference/expressions.html#evaluation-order > > Ah, so it is. My bad, sorry! In that case, sure, this works. It > still violates DRY though, naming the iterable twice and relying on > the reader noticing that that means "take two off this one". But > that's a much weaker concern. Your DRY/readability concern might then be addressed by writing it as from itertools import islice # ... command, subcommand = islice(iterargs, 2) (sorry if this was already addressed in the python-ideas@ thread, since I'm not subscribed there and it looks like discussion migrated to python-list@). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] iterable.__unpack__ method
On Mon, Feb 25, 2013 at 1:44 AM, Tim Chase wrote: > On 2013-02-25 01:19, Chris Angelico wrote: >> >>> command, subcommand = next(iterargs), next(iterargs) >> >> >> >> >> >> Err is there a language guarantee of the order of evaluation >> >> in a tuple, or is this just a "CPython happens to evaluate >> >> independent expressions left-to-right"? This is totally broken >> >> if the next() calls could be done in either order. >> > >> > It's a language guarantee. >> > >> > http://docs.python.org/2/reference/expressions.html#evaluation-order >> >> Ah, so it is. My bad, sorry! In that case, sure, this works. It >> still violates DRY though, naming the iterable twice and relying on >> the reader noticing that that means "take two off this one". But >> that's a much weaker concern. > > Your DRY/readability concern might then be addressed by writing it as > > from itertools import islice > # ... > command, subcommand = islice(iterargs, 2) > > (sorry if this was already addressed in the python-ideas@ thread, > since I'm not subscribed there and it looks like discussion migrated > to python-list@). Blargh, it didn't migrate, I just posted to the wrong list courtesy of a typo. Sorry. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: PyQT app accessible over network?
On Mon, Feb 25, 2013 at 1:31 AM, Wolfgang Keller wrote: >> As far as doing client/server stuff with just a database engine, >> unless you have tight control over the environment end to end, from a >> security pov, it's not a good idea to expose the database engine >> itself to the internet. Better to put a restricted web services API >> in front of it that handles all the authorization needs >> (access-control) on the detailed level that you require. > > Excuse me but that's bullshit. I don't use the term but I absolutely agree with the sentiment. Of course, if you're assuming a MySQL setup, then yes, exposing the database engine directly would have risks. But I grew up with DB2, and there were MANY ways in which you could control exactly what people could do (views and stored procedures being the two easiest/most commonly used) - to the extent that one of the recommended organizational structures was to have the end-user login actually *be* the database connection credentials, and to have your fancy app just connect remotely. There's a guarantee that someone who logs in as a non-administrator cannot access administrative functionality. PostgreSQL has all those same features, packaged up in an open source system; MySQL has a philosophical structure of "user logs in to app, but app logs in to database as superuser regardless of user login". ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
Hi guys, Question. Have this code intX = 32 # decl + init int var intX_asString = None # decl + init with NULL string var intX_asString = intX.__str__ ()# convert int to string What are these ugly underscores for? _str___ Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 2:46 AM, wrote: > Hi guys, > > Question. Have this code > > intX = 32 # decl + init int var > intX_asString = None # decl + init with NULL string var > > intX_asString = intX.__str__ ()# convert int to string > > What are these ugly underscores for? _str___ Normally you don't need them. Write it this way: intX_asString = str(intX) The "dunder" methods ("d"ouble "under"score, leading and trailing), also called "magic methods", are the implementations of various special features. For instance, indexing foo[1] is implemented using the __getitem__ method. Here's a list: http://docs.python.org/3.3/reference/datamodel.html#special-method-names You'll seldom, if ever, call these methods directly. By the way, when you're asking a completely new question, it usually helps to do so as a brand new thread (not a reply) and with a new subject line. Otherwise, you risk people losing the new question among the discussion of the old. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassable types
Terry Reedy udel.edu> writes: > > On 2/22/2013 4:35 AM, Wolfgang Maier wrote: > > Dear all, > > I am wondering what the rules are that determine whether a built-in type is > > subclassable or not. > > As examples, why can you base your classes on int or set, > > but not on bool or range? > > Also: can you use introspection to find out whether a type is valid as a > > base type? > > You are not the first to ask about this. I opened a doc issue. > http://bugs.python.org/issue17279 > Thanks, Terry, using the __flags__ & (1 << 10) trick from Daniel, I did something similar to what you just added to the doc issue. The complete list of types classes that *cannot* be subclassed is: of these range, memoryview, slice and bool are the only ones in builtins. Just thought it might be interesting. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , Chris Angelico wrote: > The "dunder" methods ("d"ouble "under"score, leading and trailing), > also called "magic methods", are the implementations of various > special features. For instance, indexing foo[1] is implemented using > the __getitem__ method. Here's a list: > > http://docs.python.org/3.3/reference/datamodel.html#special-method-names > > You'll seldom, if ever, call these methods directly. On the other hand, once you get into building your own classes, you will often be *writing* them. The most common are __str__(), __repr__(), and __unicode__(), and of course, __init__(). A quick look over my current project shows 471 classes, and I've defined: 1 __del__ 1 __getattr__ 1 __iter__ 1 __new__ 2 __cmp__ 2 __len__ 3 __ne__ 4 __contains__ 9 __eq__ 14 __str__ 38 __unicode__ 62 __repr__ 140 __init__ Not to mention the boilerplate: if __name__ == '__main__": which shows up all over the place. -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On 02/24/2013 07:46 AM, piterrr.dolin...@gmail.com wrote:> Hi guys, Question. Have this code intX = 32 # decl + init int var intX_asString = None # decl + init with NULL string var intX_asString = intX.__str__ ()# convert int to string What are these ugly underscores for? _str___ This is a good example of why you shouldn't program language X in language Y. For starters, `intX.__str__` should be written as `str(intX)`; For middlers, intX_asString is probably not necessary (is it being printed? then do a `print intX`, or a `print "size left on disk: %d" % intX`, etc. For finishers, why the System Hungarian Notation? intLength1 = 5 # feet intLength2 = 13 # centimeters . . . intLength1 + intLength2 # oops! vs ftLength1 = 5 cmLength2 = 13 . . . ftLength1 + cmLength2 # hey, that's wrong! better throw in a conversion -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassable types
> The complete list of types classes that *cannot* be subclassed is: > > > > > > > > > > > > > > > > > > of these range, memoryview, slice and bool are the only ones in builtins. I am sorry, but again I forgot to say which Python version I'm referring to: I used Python 3.3 :) Having such a list in the docs somewhere, is certainly a good idea. Cheers, Wolfgang -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 22/02/2013 21:37, piterrr.dolin...@gmail.com wrote: if (some statement):# short form rather than if (some statement == true):# long form What all those ugly brackets are for? Peter -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On 02/24/2013 09:23 AM, Ethan Furman wrote: > On 02/24/2013 07:46 AM, piterrr.dolin...@gmail.com wrote:> Hi guys, >> >> Question. Have this code >> >> intX = 32 # decl + init int var >> intX_asString = None # decl + init with NULL string var >> >> intX_asString = intX.__str__ ()# convert int to string >> >> What are these ugly underscores for? _str___ > > This is a good example of why you shouldn't program language X in language Y. > > For starters, `intX.__str__` should be written as `str(intX)`; > > For middlers, intX_asString is probably not necessary (is it being printed? > then > do a `print intX`, or a `print "size left on disk: %d" % intX`, etc. > > For finishers, why the System Hungarian Notation? I think he's maintaining existing code. It's unfortunate that his first exposure to python is code written by someone else in such a poor style, and in a way that definitely isn't pythonic. No wonder he's struggling to like python! Though I'm sure since his recent experience has been exclusively in C# that he probably uses hungarian notation as a matter of course. A hard habit to break! Is this a good time to introduce him to duck typing? Probably not. Another way to explain the double underscore methods is that they are how things like operator overloading is performed. Want to make a class that you can use the [index] notation on instances? Define the __get_attr__() method. And to define a class that you can then use instances with the + operator? Define the __add__() method. This is a good introduction: http://getpython3.com/diveintopython3/special-method-names.html -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter / gui
On Saturday, February 23, 2013 02:50:43 PM Rex Macey wrote: > Here is one general and one specific question about creating GUIs using > tkinter from a newbie. I have created a class in which to hold some data. > I want to create a GUI to get the data from the user and store it in the > object. Browsing the web I see that a lot of examples on GUIs have the > forms put into classes. I'm not clear why. Why is that? Second, I've > created a form with a bunch of widgets on it, one of which is a listbox. > This is done with a script, not a class. I've defined a method that > responds to a Button Release event. I know this works because I can print > the value selected in the listbox while within the method. However, I want > the value of the listbox after I've closed the form. How do I get that? > Thanks. There are a few ways to approach this. without getting to far in depth here, you can do something like this. This is not technically correct but a simple concept of how to. make a class class Klbox lbox = '' def __init__(self): self.FmkLbox() def FmkLbox(self): mkLbox = Listbox(masterTK) ## create the listbox in TK config.mkLbox( stuff, morestuff, ## config it however you want selectmode = ???) ## you may have to import your class Klbox, here #depending where you are in your program then relate Klbox.lbox = (?? something selected from listbox) # from somewhere else in your program you can import Klbox x = Klbox.lbox print x, Or, you can also learn how to use the Tkinter StringVar Class, available in Tkinter manual but you will need to relate the StringVar to an attribute as I described above so you can get it from another module or class in your program. jd inqvista.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 07:46:07 -0800, piterrr.dolinski wrote: > Hi guys, > > Question. Have this code > > intX = 32 # decl + init int var > intX_asString = None # decl + init with NULL string var > > intX_asString = intX.__str__ ()# convert int to string > > What are these ugly underscores for? > _str___ To demonstrate that the person who wrote this code was not a good Python programmer. I hope it wasn't you :-) This person obviously had a very basic, and confused, understanding of Python. And, quite frankly, was probably not a very good programmer of *any* language: - poor use of Hungarian notation for variable names; - pointless pre-declaration of values; - redundant comments that don't explain anything. If that code came from the code-base you are maintaining, no wonder you don't think much of Python! That looks like something I would expect to see at the DailyWTF. http://thedailywtf.com/ The above code is better written as: x = 32 x_asString = str(x) Double-underscore methods are used for operator overloading and customizing certain operations, e.g. __add__ overloads the + operator. You might define a class with a __str__ method to customize converting the object to a string: # Toy example. class MyObject: def __str__(self): return "This is my object" But you would not call the __str__ method directly. (I won't say "never", because there are rare, advanced, uses for calling double-underscore methods directly.) You would use a public interface, such as the str() function. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
"The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
Hi! The subject is a segment of a sentence which I copied from Python's official homepage. In whole, it reads: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays while waiting for a network connection to be set up. This means that it is difficult to build an interactive Web client using these functions without using threads." Here is my issue: I am testing my web site (which I have some problems with), and comparing its response speed to a couple of other sites, such as msn.com and python.org. Now, it is very strange that python.org responds always under 0.5 s while my site takes about 20 s to respond! And MSN about 60 seconds! So it does not seem "arbitrary" to me. And I can figure out the mechanisms behind the long delays. What I am wondering is if there's another way for me to go about. I didn't really find any solutions with "threads" as suggested by python's document base. Any suggestions? This is really important because technicians at my web hotel do not use proper tools and my site has been nearly dysfunctional since their latest upgrade and I want to make a case with proper statistics. Thank you in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 10:37 AM, Dennis Lee Bieber wrote: > Decided to look up the VAX/VMS scheme... > > """ > If you know the condition code for a message, you can use F$MESSAGE to > translate the code to its associated message. For example: > $ WRITE SYS$OUTPUT F$MESSAGE(%X0001) > %SYSTEM-S-NORMAL, normal successful completion > """ > > VMS used a status of "1" for normal success (which implies that all > the odd integers were success/info messages, even integers would be > warning/error/fatal. > > http://h71000.www7.hp.com/doc/73final/documentation/pdf/ovms_msg_ref_al.pdf It's interesting to note that Windows NT sort of descends from VMS. I guess the end result was an unholy blend of VMS and CP/M. -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On 02/24/2013 12:29 PM, Michael Torrie wrote: > I think he's maintaining existing code. It's unfortunate that his first > exposure to python is code written by someone else in such a poor style, > and in a way that definitely isn't pythonic. No wonder he's struggling > to like python! Though I'm sure since his recent experience has been > exclusively in C# that he probably uses hungarian notation as a matter > of course. A hard habit to break! Is this a good time to introduce him > to duck typing? Probably not. > > Another way to explain the double underscore methods is that they are > how things like operator overloading is performed. Want to make a class > that you can use the [index] notation on instances? Define the > __get_attr__() method. And to define a class that you can then use small correction: it should be __getattr__() -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ Admiration for a quality or an art can be so strong that it deters us from striving to possess it. Friedrich Nietzsche -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , Michael Torrie wrote: > It's interesting to note that Windows NT sort of descends from VMS. More than "sort of". Dave Cutler was the chief architect of both. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 2013-02-23, Chris Angelico wrote: > It's worth noting, though, that there are self-perpetuating aspects to > it. I can happily distribute a .py file to a Linux audience, because > many Linux distros come with a Python already installed, or at very > least can grab one easily via the package manager. Are there any popular, mainstream Linux distros that don't come with Python installed by default? RedHat has had Python installed as part of the base system since day 1, since both the installer and some of the system admin stuff was written in Python. I always thought RPM also originally written in Python, but can't find any references. In any case, yum is written in Python, so I doubt there are any RPM-based distros that don't have Python as part of a base install. Python is required by Gentoo, since the package management tools are written (at least partially) in Python. In theory, it might be possible to do an install tha doesn't include Python by using a different package-management system, but in practice Python is always there on Gentoo systems. All of the Debian systems I've seen had Python installed, but I'm not sure how "required" it is. AFAICT, Python is installed as part of all Ubuntu installations as well. -- Grant Edwards grant.b.edwardsYow! Did you move a lot of at KOREAN STEAK KNIVES this gmail.comtrip, Dingy? -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On Sun, 24 Feb 2013 13:09:56 -0500, Mitya Sirenef wrote: > On 02/24/2013 12:29 PM, Michael Torrie wrote: > > Another way to explain the double underscore methods is that they are > > how things like operator overloading is performed. Want to make a > > class that you can use the [index] notation on instances? Define the > > __get_attr__() method. And to define a class that you can then use > > small correction: it should be __getattr__() Correct correction: it should be __getitem__ to override instance[item] access. __getattr__ is for overriding instance.attribute. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On 02/24/2013 09:29 AM, Michael Torrie wrote: On 02/24/2013 09:23 AM, Ethan Furman wrote: On 02/24/2013 07:46 AM, piterrr.dolin...@gmail.com wrote:> Hi guys, Question. Have this code intX = 32 # decl + init int var intX_asString = None # decl + init with NULL string var intX_asString = intX.__str__ ()# convert int to string What are these ugly underscores for? _str___ This is a good example of why you shouldn't program language X in language Y. For starters, `intX.__str__` should be written as `str(intX)`; For middlers, intX_asString is probably not necessary (is it being printed? then do a `print intX`, or a `print "size left on disk: %d" % intX`, etc. For finishers, why the System Hungarian Notation? I think he's maintaining existing code. It's unfortunate that his first exposure to python is code written by someone else in such a poor style, and in a way that definitely isn't pythonic. No wonder he's struggling to like python! On the bright side, if this is one of his 2000 line scripts, he should be able to get it down to at least half that once he has a good feel for Python and re-writes it. Another way to explain the double underscore methods is that they are how things like operator overloading is performed. Want to make a class that you can use the [index] notation on instances? Define the __get_attr__() method. Actually, it's the __getitem__ method. -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
On 02/24/2013 01:23 PM, Steven D'Aprano wrote: On Sun, 24 Feb 2013 13:09:56 -0500, Mitya Sirenef wrote: > >> On 02/24/2013 12:29 PM, Michael Torrie wrote: > >> > Another way to explain the double underscore methods is that they are >> > how things like operator overloading is performed. Want to make a >> > class that you can use the [index] notation on instances? Define the >> > __get_attr__() method. And to define a class that you can then use >> >> small correction: it should be __getattr__() > > > Correct correction: it should be __getitem__ to override instance[item] > access. __getattr__ is for overriding instance.attribute. > > Oh, yes.. I guess it pays to read more than a single sentence when making corrections! -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ Each religion, by the help of more or less myth, which it takes more or less seriously, proposes some method of fortifying the human soul and enabling it to make its peace with its destiny. George Santayana -- http://mail.python.org/mailman/listinfo/python-list
Re: intX.__str__() ??
In article , Ethan Furman wrote: > On the bright side, if this is one of his 2000 line scripts, he should be > able to get it down > to at least half that once he has a good feel for Python and re-writes it. I hope he doesn't get paid by the line :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
On Sun, Feb 24, 2013 at 10:48 AM, 7segment <7segm...@live.com> wrote: > Hi! > > The subject is a segment of a sentence which I copied from Python's > official homepage. In whole, it reads: > > "The urlopen() and urlretrieve() functions can cause arbitrarily long > delays while waiting for a network connection to be set up. This means > that it is difficult to build an interactive Web client using these > functions without using threads." I believe what this is warning about is that if a network connection is not immediately available when the functions are called, they will block while waiting for one rather than returning control to the calling function. If used in an interactive client without threads, this would cause the application to appear to "hang" while urllib waits for a response. I don't think it's meant to suggest that urllib is responsible for the delays; that sounds like a problem with your network configuration. -- http://mail.python.org/mailman/listinfo/python-list
Re: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
On 2013-02-24 18:55, Ian Kelly wrote: On Sun, Feb 24, 2013 at 10:48 AM, 7segment <7segm...@live.com> wrote: Hi! The subject is a segment of a sentence which I copied from Python's official homepage. In whole, it reads: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays while waiting for a network connection to be set up. This means that it is difficult to build an interactive Web client using these functions without using threads." I believe what this is warning about is that if a network connection is not immediately available when the functions are called, they will block while waiting for one rather than returning control to the calling function. If used in an interactive client without threads, this would cause the application to appear to "hang" while urllib waits for a response. I don't think it's meant to suggest that urllib is responsible for the delays; that sounds like a problem with your network configuration. When in doubt, check some other way, such as with a browser. -- http://mail.python.org/mailman/listinfo/python-list
Re: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
On Sun, 24 Feb 2013 11:55:09 -0700, Ian Kelly wrote: > On Sun, Feb 24, 2013 at 10:48 AM, 7segment <7segm...@live.com> wrote: >> Hi! >> >> The subject is a segment of a sentence which I copied from Python's >> official homepage. In whole, it reads: >> >> "The urlopen() and urlretrieve() functions can cause arbitrarily long >> delays while waiting for a network connection to be set up. This means >> that it is difficult to build an interactive Web client using these >> functions without using threads." > > I believe what this is warning about is that if a network connection is > not immediately available when the functions are called, they will block > while waiting for one rather than returning control to the calling > function. If used in an interactive client without threads, this would > cause the application to appear to "hang" while urllib waits for a > response. I don't think it's meant to suggest that urllib is > responsible for the delays; that sounds like a problem with your network > configuration. Thanks. I store all the data (response times) in a database. The figures I cited are always nearly the same. For example MSN has response times between 60-61 seconds consistently, my site 21-35 seconds, but python.org only about 0.3 seconds. the calls to the servers happen during the same minute. And there's nothing wrong with my web connection at home. -- http://mail.python.org/mailman/listinfo/python-list
Re: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
On Sun, 24 Feb 2013 19:04:28 +, MRAB wrote: > On 2013-02-24 18:55, Ian Kelly wrote: >> On Sun, Feb 24, 2013 at 10:48 AM, 7segment <7segm...@live.com> wrote: >>> Hi! >>> >>> The subject is a segment of a sentence which I copied from Python's >>> official homepage. In whole, it reads: >>> >>> "The urlopen() and urlretrieve() functions can cause arbitrarily long >>> delays while waiting for a network connection to be set up. This means >>> that it is difficult to build an interactive Web client using these >>> functions without using threads." >> >> I believe what this is warning about is that if a network connection is >> not immediately available when the functions are called, they will >> block while waiting for one rather than returning control to the >> calling function. If used in an interactive client without threads, >> this would cause the application to appear to "hang" while urllib waits >> for a response. I don't think it's meant to suggest that urllib is >> responsible for the delays; that sounds like a problem with your >> network configuration. >> > When in doubt, check some other way, such as with a browser. Thank you Ian. Browser is not a good idea, because I need this tool to work automatically. I don't have time to check and compare the response times manually and put them into the database. -- http://mail.python.org/mailman/listinfo/python-list
Re: Correct handling of case in unicode and regexps
On 23 fév, 15:26, Devin Jeanpierre wrote: > Hi folks, > > I'm pretty unsure of myself when it comes to unicode. As I understand > it, you're generally supposed to compare things in a case insensitive > manner by case folding, right? So instead of a.lower() == b.lower() > (the ASCII way), you do a.casefold() == b.casefold() > > However, I'm struggling to figure out how regular expressions should > treat case. Python's re module doesn't "work properly" to my > understanding, because: > > >>> a = 'ss' > >>> b = 'ß' > >>> a.casefold() == b.casefold() > True > >>> re.match(re.escape(a), b, re.UNICODE | re.IGNORECASE) > >>> # oh dear! > > In addition, it seems improbable that this ever _could_ work. Because > if it did work like that, then what would the value be of > re.match('s', 'ß', re.UNICODE | re.IGNORECASE).end() ? 0.5? > > I'd really like to hear the thoughts of people more experienced with > unicode. What is the ideal correct behavior here? Or do I > misunderstand things? - I'm just wondering if there is a real issue here. After all, this is only a question of conventions. Unicode has some conventions, re modules may (has to) use some conventions too. It seems to me, the safest way is to preprocess the text, which has to be examinated. Proposed case study: How should be ss/ß/SS/ẞ interpreted? 'Richard-Strauss-Straße' 'Richard-Strauss-Strasse' 'RICHARD-STRAUSS-STRASSE' 'RICHARD-STRAUSS-STRAẞE' There is more or less the same situation with sorting. Unicode can not do all and it may be mandatory to preprocess the "input". Eg. This fct I wrote once for the fun. It sorts French words (without unicodedata and locale). >>> import libfrancais >>> z = ['oeuf', 'œuf', 'od', 'of'] >>> zo = libfrancais.sortedfr(z) >>> zo ['od', 'oeuf', 'œuf', 'of'] jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
> To demonstrate that the person who wrote this code was not a good Python > > programmer. I hope it wasn't you :-) This person obviously had a very > > basic, and confused, understanding of Python. > > > > And, quite frankly, was probably not a very good programmer of *any* > > language: > > > > - poor use of Hungarian notation for variable names; > > - pointless pre-declaration of values; > > - redundant comments that don't explain anything. > > > > If that code came from the code-base you are maintaining, no wonder you > > don't think much of Python! That looks like something I would expect to > > see at the DailyWTF. Hi. Steve, I don't know where you have been over the past couple of days but it is widely known (if the thread title is any indication) that I am indeed very new to Python, but not new to programming in general. To give a bit of background where I found __str__, I am using a Python IDE called PyScripter. Its Intellisense is full of methods starting and ending with "__", hence the question. Regarding Hungarian notation, I don't use it in any other language but Python and JS. Call it poor, but it helps me to remember what type a variable is. The redundant comments serve the same purpose. As for "pointless predeclaration", it helps me know where in the code I first started using the variable, so I know there are no references to it before then. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
> > if (some statement):# short form > > > > rather than > > > > if (some statement == true):# long form > > > What all those ugly brackets are for? > Mark, Back in the day when C was king, or take many newer long established languages (C#, Java), the use of () has been widespread and mandated by the compilers. I have never heard anyone moan about the requirement to use parentheses. Now come Python in which parens are optional, and all of a sudden they are considered bad and apparently widely abandoned. Do you really not see that code with parens is much more pleasing visually? I could understand someone's reluctance to use parens if they are very new to programming and Pythons is their first language. But my impression here is that most group contributors are long-time programmers and have long used () where they are required. Again, I'm really surprised the community as a whole ignores the programming "heritage" and dumps the parens in a heartbeat. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 02:40 PM, piterrr.dolin...@gmail.com wrote: if (some statement): # short form >>> >>> rather than >>> >>> if (some statement == true): # long form >> >> >> What all those ugly brackets are for? >> > > Mark, > > Back in the day when C was king, or take many newer long established > languages (C#, Java), the use of () has been widespread and mandated > by the compilers. I have never heard anyone moan about the requirement > to use parentheses. Now come Python in which parens are optional, and > all of a sudden they are considered bad and apparently widely > abandoned. Do you really not see that code with parens is much more > pleasing visually? I could understand someone's reluctance to use > parens if they are very new to programming and Pythons is their first > language. But my impression here is that most group contributors are > long-time programmers and have long used () where they are required. > Again, I'm really surprised the community as a whole ignores the > programming "heritage" and dumps the parens in a heartbeat. > > Peter When I write in English, I write: If it rains, I'll get an umbrella. I do not write: If (it rains), I'll get an umbrella. The second example isn't any clearer. The only reason you like unneeded parens is that you're used to them. I've never heard of anyone missing this "feature" after a month or two of using Python. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ The world is a perpetual caricature of itself; at every moment it is the mockery and the contradiction of what it is pretending to be. George Santayana -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 5:19 AM, Grant Edwards wrote: > On 2013-02-23, Chris Angelico wrote: > >> It's worth noting, though, that there are self-perpetuating aspects to >> it. I can happily distribute a .py file to a Linux audience, because >> many Linux distros come with a Python already installed, or at very >> least can grab one easily via the package manager. > > Are there any popular, mainstream Linux distros that don't come with > Python installed by default? Probably all the main desktop distros come with _some_ Python (but not necessarily a recent one... RHEL with 2.4?!). I used "many" rather than "all" in case there's one somewhere that doesn't, but even then, Python will be an easily-added feature. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: > On Feb 24, 2013 1:21 AM, "llanitedave" wrote: > > > > > > I created an html help page for my Python 2.7.3 application and put it in a > > documentation folder. I used webbrowser.open() to fetch the page. > > > > > > On linux -- KDE specifically, the command opens the local file on my > > default browser with no issues. However, on Windows 7, it opens Internet > > Explorer, which doesn't even search the local folder, but goes straight to > > the web and does a Google search, returning nothing but useless noise. > > > > > > > My default browser on Windows is Chrome, so my intention is getting > > undermined right from the start. > > > > > > How do I get a local html file to open properly from Python in Windows? > > Sounds like this might be your problem: > > http://bugs.python.org/issue8936 > > The fix would seem to be ensuring that the URL you pass includes the scheme > (in your case, "file:"). > > Cheers, > > Chris Holy Toledo! That's a two-year-old bug spanning two versions of the language! BTW, Chris, the snippet I showed in the title essentially WAS the exact code. It's a method with that single line called from a wxPython Help menu. I can't really put an absolute pathname into the argument, because the application is going to be distributed to a variety of computers at my workplace, and there's no assurance that it will go into (or remain in)a particular folder. I was trying to avoid using the wx.html.HtmlWindow feature of wxPython, because it doesn't handle CSS and styles. My help page is the portal to a multi-page users guide with a style sheet to render all the content consistently. Plus, I couldn't get the wx.html.HtmlWindow to open relative paths either -- it gave me "URL Malformed" messages even in KDE, when webbrowser.open("filepath") was working for the exact same path. But that's something to take up on the wxPython list, I guess. This to me illustrates the downside of the Python philosophy of "There should be only one obvious way to do things". If that one obvious way has a fatal bug, you're pretty much SOL. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 20:40:05 +0100, wrote: > if (some statement): # short form > > rather than > > if (some statement == true): # long form What all those ugly brackets are for? Mark, Back in the day when C was king, or take many newer long established languages (C#, Java), the use of () has been widespread and mandated by the compilers. I have never heard anyone moan about the requirement to use parentheses. You've never heard me then. I ... "strongly dislike" having to parse visual elements which I consider superfluous and implicit. Does the English language have a proverb like "not being able to see the forest for the trees"? To me, a C source looks like all brackets. Can't see the code for all the brackets. Now come Python in which parens are optional, and all of a sudden they are considered bad and apparently widely abandoned. Do you really not see that code with parens is much more pleasing visually? I guess one can get just as religious about the brackets as one can about the whitespace. if ( condition ) { action } vs if condition: action In time estimated, I'd say I can read and understand Python code about 20% faster than any of these brackety languages, even compared to languages I worked a with couple of years longer. That's a lot of effort saved. Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 2013-02-24 19:40, piterrr.dolin...@gmail.com wrote: if (some statement):# short form rather than if (some statement == true):# long form What all those ugly brackets are for? Mark, Back in the day when C was king, or take many newer long established languages (C#, Java), the use of () has been widespread and mandated by the compilers. I have never heard anyone moan about the requirement to use parentheses. Now come Python in which parens are optional, and all of a sudden they are considered bad and apparently widely abandoned. Do you really not see that code with parens is much more pleasing visually? I could understand someone's reluctance to use parens if they are very new to programming and Pythons is their first language. But my impression here is that most group contributors are long-time programmers and have long used () where they are required. Again, I'm really surprised the community as a whole ignores the programming "heritage" and dumps the parens in a heartbeat. Some languages require parentheses, others don't. C does. C++, Java and C# are descended from, or influenced by, C. Algol didn't (doesn't?). Pascal, Modula-2, Oberon, Ada, and others don't. Parentheses are used where required, but not used where they're not required, in order to reduce visual clutter. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 7:34 AM, MRAB wrote: > Some languages require parentheses, others don't. > > C does. C++, Java and C# are descended from, or influenced by, C. > > Algol didn't (doesn't?). Pascal, Modula-2, Oberon, Ada, and others > don't. > > Parentheses are used where required, but not used where they're not > required, in order to reduce visual clutter. And just to muddy the waters, parens are used in Python when the condition goes over a line break: if (condition1 and condition2 and condition3): ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24/02/2013 19:40, piterrr.dolin...@gmail.com wrote: if (some statement):# short form rather than if (some statement == true):# long form What all those ugly brackets are for? Mark, Back in the day when C was king, or take many newer long established languages (C#, Java), the use of () has been widespread and mandated by the compilers. I have never heard anyone moan about the requirement to use parentheses. Now come Python in which parens are optional, and all of a sudden they are considered bad and apparently widely abandoned. Do you really not see that code with parens is much more pleasing visually? I could understand someone's reluctance to use parens if they are very new to programming and Pythons is their first language. But my impression here is that most group contributors are long-time programmers and have long used () where they are required. Again, I'm really surprised the community as a whole ignores the programming "heritage" and dumps the parens in a hea rtbeat. Peter Your words "the use of () has been widespread and mandated by the compilers" and "have long used () where they are required". As they are neither mandated nor required in Python it just wastes the time of anybody reading code as they have to parse something that offers nothing except visual noise. As for being "visually pleasing" that's simply laughable. I want to be able to read code, not hang it in an art gallery. -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 11:40 AM, piterrr.dolin...@gmail.com wrote: Back in the day when C was king, or take many newer long established languages (C#, Java), the use of () has been widespread and mandated by the compilers. I have never heard anyone moan about the requirement to use parentheses. Now come Python in which parens are optional, and all of a sudden they are considered bad and apparently widely abandoned. Do you really not see that code with parens is much more pleasing visually? I could understand someone's reluctance to use parens if they are very new to programming and Pythons is their first language. But my impression here is that most group contributors are long-time programmers and have long used () where they are required. Again, I'm really surprised the community as a whole ignores the programming "heritage" and dumps the parens in a heartbeat. Python will also allow you to have ';' at the end of your lines. It does nothing for you, but perhaps you also find that "visually pleasing"? I find () to be four extra keystrokes, not visually pleasing, and needed only to override order of operations. One of the things I love about Python is its ability to get out of the way and let me work: - no variable declarations, just use 'em - no type declarations, just use 'em - no need to remember what's an object and what's not -- everything is an object - no need to cast to bool as everything has a truthy/falsey (something vs nothing) value From a different email you said PyScripter was showing you all the dunder methods? You might want to try one of the others. -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On 24/02/2013 20:28, llanitedave wrote: On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: On Feb 24, 2013 1:21 AM, "llanitedave" wrote: I created an html help page for my Python 2.7.3 application and put it in a documentation folder. I used webbrowser.open() to fetch the page. On linux -- KDE specifically, the command opens the local file on my default browser with no issues. However, on Windows 7, it opens Internet Explorer, which doesn't even search the local folder, but goes straight to the web and does a Google search, returning nothing but useless noise. My default browser on Windows is Chrome, so my intention is getting undermined right from the start. How do I get a local html file to open properly from Python in Windows? Sounds like this might be your problem: http://bugs.python.org/issue8936 The fix would seem to be ensuring that the URL you pass includes the scheme (in your case, "file:"). Cheers, Chris Holy Toledo! That's a two-year-old bug spanning two versions of the language! Only two years is nothing. Pay your money, take your choice :) This to me illustrates the downside of the Python philosophy of "There should be only one obvious way to do things". If that one obvious way has a fatal bug, you're pretty much SOL. Misquoted as always. I guess that some day someone will quote it correctly. -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman wrote: > One of the things I love about Python is its ability to get out of the way > and let me work: > > - no variable declarations, just use 'em > - no type declarations, just use 'em > - no need to remember what's an object and what's not -- everything is an > object > - no need to cast to bool as everything has a truthy/falsey (something vs > nothing) value Variable declarations can go either way; Python requires you to name all globals that you mutate, and to be careful when working with nested functions. With declared variables, you name all locals, and can enforce scoping and destructors without language features like 'with'. Both options are viable. I absolutely agree with your third point. Treat 'em all as objects! But of *course*, Java is "more object oriented" than Python. Everyone knows that. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Sun, Feb 24, 2013 at 12:28 PM, llanitedave wrote: > On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: >> On Feb 24, 2013 1:21 AM, "llanitedave" wrote: >> > I created an html help page for my Python 2.7.3 application and put it in >> > a documentation folder. I used webbrowser.open() to fetch the page. >> > On linux -- KDE specifically, the command opens the local file on my >> > default browser with no issues. However, on Windows 7, it opens Internet >> > Explorer, which doesn't even search the local folder, but goes straight to >> > the web and does a Google search, returning nothing but useless noise. >> > My default browser on Windows is Chrome, so my intention is getting >> > undermined right from the start. >> > How do I get a local html file to open properly from Python in Windows? >> >> Sounds like this might be your problem: >> http://bugs.python.org/issue8936 >> >> The fix would seem to be ensuring that the URL you pass includes the scheme >> (in your case, "file:"). > > Holy Toledo! That's a two-year-old bug spanning two versions of the language! > > BTW, Chris, the snippet I showed in the title essentially WAS the exact code. Sorry, my bad. This is why I dislike messages that put critical info *only* in the subject line; I tend not to reread the subject line once I've opened the message. > It's a method with that single line called from a wxPython Help menu. I > can't really put an absolute pathname into the argument, because the > application is going to be distributed to a variety of computers at my > workplace, and there's no assurance that it will go into (or remain in)a > particular folder. As Demian demonstrated, you can simply compute the absolute path from the relative path at runtime; although I would probably toss an abspath() call in for good measure (http://docs.python.org/2/library/os.path.html#os.path.abspath ). > This to me illustrates the downside of the Python philosophy of "There should > be only one obvious way to do things". If that one obvious way has a fatal > bug, you're pretty much SOL. On the other hand, you don't have to investigate which of N APIs is the "fixed"/"correct" one (Which PHP MySQL function is safe from SQL injection again?), and you only have wait for 1 fix instead of N. But yes, some of Python's included batteries are due for some recharging. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Sunday, February 24, 2013 12:48:40 PM UTC-8, Chris Rebert wrote: > On Sun, Feb 24, 2013 at 12:28 PM, llanitedave wrote: > > > On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: > > >> On Feb 24, 2013 1:21 AM, "llanitedave" wrote: > > >> > I created an html help page for my Python 2.7.3 application and put it > >> > in a documentation folder. I used webbrowser.open() to fetch the page. > > >> > On linux -- KDE specifically, the command opens the local file on my > >> > default browser with no issues. However, on Windows 7, it opens > >> > Internet Explorer, which doesn't even search the local folder, but goes > >> > straight to the web and does a Google search, returning nothing but > >> > useless noise. > > >> > My default browser on Windows is Chrome, so my intention is getting > >> > undermined right from the start. > > >> > How do I get a local html file to open properly from Python in Windows? > > >> > > >> Sounds like this might be your problem: > > >> http://bugs.python.org/issue8936 > > >> > > >> The fix would seem to be ensuring that the URL you pass includes the > >> scheme (in your case, "file:"). > > > > > > Holy Toledo! That's a two-year-old bug spanning two versions of the > > language! > > > > > > BTW, Chris, the snippet I showed in the title essentially WAS the exact > > code. > > > > Sorry, my bad. This is why I dislike messages that put critical info > > *only* in the subject line; I tend not to reread the subject line once > > I've opened the message. > Nah, my bad. I didn't realize that the title was the only place I'd put the actual command. I don't like it when other people do that either. > > > > It's a method with that single line called from a wxPython Help menu. I > > can't really put an absolute pathname into the argument, because the > > application is going to be distributed to a variety of computers at my > > workplace, and there's no assurance that it will go into (or remain in)a > > particular folder. > > > > As Demian demonstrated, you can simply compute the absolute path from > > the relative path at runtime; although I would probably toss an > > abspath() call in for good measure > > (http://docs.python.org/2/library/os.path.html#os.path.abspath ). > > OK, I'm going to have to study that one a bit. It looks like a new concept for my feeble brain. > > > This to me illustrates the downside of the Python philosophy of "There > > should be only one obvious way to do things". If that one obvious way has > > a fatal bug, you're pretty much SOL. > > > > On the other hand, you don't have to investigate which of N APIs is > > the "fixed"/"correct" one (Which PHP MySQL function is safe from SQL > > injection again?), and you only have wait for 1 fix instead of N. But > > yes, some of Python's included batteries are due for some recharging. > > > > Cheers, > > Chris You're right. It's one thing to have a persistent bug, it's another thing to offer the function in the documentation as if the bug doesn't exist. The bug report from October 2010 indicated that someone was working on a fix at that time. The fact that it's still not fixed implies that it might be something that's really hard to pin down. In a case like that, it's probably better to simply withdraw the feature, or tag it as "Non-windows only" -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , Chris Angelico wrote: > On Mon, Feb 25, 2013 at 7:34 AM, MRAB wrote: > > Some languages require parentheses, others don't. > > > > C does. C++, Java and C# are descended from, or influenced by, C. > > > > Algol didn't (doesn't?). Pascal, Modula-2, Oberon, Ada, and others > > don't. > > > > Parentheses are used where required, but not used where they're not > > required, in order to reduce visual clutter. > > And just to muddy the waters, parens are used in Python when the > condition goes over a line break: > > if (condition1 > and condition2 > and condition3): > > ChrisA That could also be written: if condition1 \ and condition2 \ and condition3: but as a practical matter, I would write it in the parens style, if for no other reason than because emacs does a better job of auto-indenting it that way :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Sunday, February 24, 2013 12:50:02 PM UTC-8, Mark Lawrence wrote: > On 24/02/2013 20:28, llanitedave wrote: > > > On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: > > >> On Feb 24, 2013 1:21 AM, "llanitedave" wrote: > > >> > > >>> > > >> > > >>> I created an html help page for my Python 2.7.3 application and put it in > >>> a documentation folder. I used webbrowser.open() to fetch the page. > > >> > > >>> > > >> > > >>> On linux -- KDE specifically, the command opens the local file on my > >>> default browser with no issues. However, on Windows 7, it opens Internet > >>> Explorer, which doesn't even search the local folder, but goes straight > >>> to the web and does a Google search, returning nothing but useless noise. > > >> > > >> > > >>> > > >> > > >>> My default browser on Windows is Chrome, so my intention is getting > >>> undermined right from the start. > > >> > > >>> > > >> > > >>> How do I get a local html file to open properly from Python in Windows? > > >> > > >> Sounds like this might be your problem: > > >> > > >> http://bugs.python.org/issue8936 > > >> > > >> The fix would seem to be ensuring that the URL you pass includes the > >> scheme (in your case, "file:"). > > >> > > >> Cheers, > > >> > > >> Chris > > > > > > Holy Toledo! That's a two-year-old bug spanning two versions of the > > language! > > > > Only two years is nothing. Pay your money, take your choice :) > > > > > This to me illustrates the downside of the Python philosophy of "There > > should be only one obvious way to do things". If that one obvious way has > > a fatal bug, you're pretty much SOL. > > > > Misquoted as always. I guess that some day someone will quote it correctly. > > > > -- > > Cheers. > > > > Mark Lawrence I think the correct quote is "You pays your money, and you takes your chances". ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , Chris Angelico wrote: > > no need to remember what's an object and what's not -- everything is an > > object Well, not quite everything. If I write: if foo: do_this() and_this() the code block making up the body of the "if" statement is not an object. In some languages, it is. -- http://mail.python.org/mailman/listinfo/python-list
Re: "The urlopen() and urlretrieve() functions can cause arbitrarily long delays"
Am 24.02.2013 20:27 schrieb 7segment: When in doubt, check some other way, such as with a browser. Thank you Ian. Browser is not a good idea, because I need this tool to work automatically. I don't have time to check and compare the response times manually and put them into the database. Of course not for the long term. But you could check e.g. if MSN needs 60 s as well from the browser. If not, there could be something else wrong. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24 February 2013 19:29, wrote: > Hi. Steve, I don't know where you have been over the past couple of days > but it is widely known (if the thread title is any indication) that I am > indeed very new to Python, but not new to programming in general. > > To give a bit of background where I found __str__, I am using a Python IDE > called PyScripter. Its Intellisense is full of methods starting and ending > with "__", hence the question. > > Regarding Hungarian notation, I don't use it in any other language but > Python and JS. Call it poor, but it helps me to remember what type a > variable is. If you can't remember what type a variable is, you're doing something incorrectly. > The redundant comments serve the same purpose. To help you remember the type of the variable? > intX = 32 # decl + init int var How is it not obvious that "intX" is an integer *without* the comment? > X = 32 How is it not obvious that X is an integer? > intX_asString = None # decl + init with NULL string var How is it not obvious that intX_asString is an integer (it starts with "int", duh"... oh wait) The comment says it's the NULL string, so it's "". F*ck. It's None. Why? No idea. > intX_asString = intX.__str__ ()# convert int to string Wait. So why'd you write the previous line? Just write: > X_as_string = str(X) 'Cause "str" *always* returns a string. So it's a string. How is that not obvious? But then, what's the context? "X" is a *useless* name. Why are you converting X to a string? I have no idea. The problem with the code isn't that you could be overwriting "X". The problem is that your code is contradictory, pretends it's C, has useless names and doesn't try to be readable. > As for "pointless predeclaration", it helps me know where in the code I > first started using the variable, so I know there are no references to it > before then. > Why? Why can't you overwrite old variables? Why can't a variable change type? If your functions are so large that you're likely to lose track of what's defined, you have a different problem indeed. For example: def floatA_floatB_floatC_to_tupleR(floatA, floatB, floatC): # decl with floatA, floatB, floatC parameters floatD = None # decl + init with NULL float var floatD = ((floatB ** 2) - (4 * floatA * floatC)) # set to B² - 4AC floatD = floatD ** 0.5 # set to √floatD floatR1 = None # decl + init with NULL float var floatR1 = (((- floatB) + floatD) / (2 * floatA)) # set to (-B+D)/(2A) floatR2 = None # decl + init with NULL float var floatR2 = (((- floatB) - floatD) / (2 * floatA)) # set to (-B-D)/(2A) return (floatR1, floatR2) Versus def solve_quadratic(a, b, c): """Solve a quadratic equation of the form ax² + bx + c = 0 The result will be a tuple of the two results; the results can be equal if the determinant is 0. This supports imaginary results for if the determinant is negative.""" # The method used is the quadratic equation: # http://en.wikipedia.org/wiki/Quadratic_equation # b² - 4ac determinant = b**2 - 4*a*c # ±√(b² - 4ac) sqrt_determinant = determinant ** 0.5 squareroots = sqrt_determinant, -sqrt_determinant # -b ± √(b² - 4ac) fraction_tops = [(-b + d) for d in squareroots] results = [top/(2*a) for top in fraction_tops] return results Which is easier to read? Reading through it you don't just suddenly forget what the type of "determinant" is (which must be a number because it's a determinant) or "results" (which is a container since it's plural). The names tell you. The useful comments such as "The method used is..." and "±√(b² - 4ac)" give you context, too, which is a lot more than can be said of "floatA_floatB_floatC_to_tupleR". For that, I tried to emulate what I saw in your code. I'm not a good programmer. But because of that the code I write makes sense, so I can understand it. Tell the reader what they want to know, not what they see. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24 February 2013 20:48, Roy Smith wrote: > In article , > Chris Angelico wrote: > > > On Mon, Feb 25, 2013 at 7:34 AM, MRAB > wrote: > > > Some languages require parentheses, others don't. > > > > > > C does. C++, Java and C# are descended from, or influenced by, C. > > > > > > Algol didn't (doesn't?). Pascal, Modula-2, Oberon, Ada, and others > > > don't. > > > > > > Parentheses are used where required, but not used where they're not > > > required, in order to reduce visual clutter. > > > > And just to muddy the waters, parens are used in Python when the > > condition goes over a line break: > > > > if (condition1 > > and condition2 > > and condition3): > > > > ChrisA > > That could also be written: > > if condition1 \ >and condition2 \ >and condition3: > > but as a practical matter, I would write it in the parens style, if for > no other reason than because emacs does a better job of auto-indenting > it that way :-) > Pah, condition1 = long_condition_expression_1 condition2 = long_condition_expression_2 condition3 = long_condition_expression_3 if condition1 and condition2 and condition3: STUFF No multiline needed. If you have *many* conditions, then: supercondition = all( condition1, condition2, condition3, condition4, condition5, condition6, condition7, condition8, condition9 ) # or equiv. if supercondition: STUFF Reason: Indentation should be *really simple*. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 8:35 AM, Joshua Landau wrote: > def solve_quadratic(a, b, c): > """Solve a quadratic equation of the form ax² + bx + c = 0 > > The result will be a tuple of the two results; the results can be equal if > the determinant is 0. > This supports imaginary results for if the determinant is negative.""" > ... > results = [top/(2*a) for top in fraction_tops] Yeah, I think we know which one is the more readable... Just to nit-pick a little though, that returns a list when its docstring says it'll return a tuple :) Other than that (which is probably better solved by changing the docs than the code), the only change I'd make would be to ditch the fraction_tops temporary (and to throw out some of the comments that serve only to reexpress the code that immediately follows them, though for a demo they're entirely appropriate). Even in a language with mandatory declarations, the code would look pretty similar: # Assume that the declaration 'complex' permits a float - otherwise you need a Pike-style piped declaration eg "float|complex" # Details elided for brevity, keep the docstring and comments from the above version list(complex) solve_quadratic(float a, float b, float c): float determinant = b**2 - 4*a*c complex sqrt_determinant = determinant ** 0.5 tuple(complex) squareroots = sqrt_determinant, -sqrt_determinant return [(-b + d)/(2*a) for top in squareroots] Variable names seldom if ever need to identify their types, if by "type" you mean what the language sees as a type. There are times when it's useful to adorn a variable name with a unit, perhaps (length_ft and height_m shouldn't be multiplied together), or some other special marker (a "tainted" flag on all data that's come from user input, and which therefore must not be executed or interpolated into SQL or anything), but this is a much higher level of abstraction. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On 2013-02-24 20:28, llanitedave wrote: On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: [snip] Sounds like this might be your problem: http://bugs.python.org/issue8936 The fix would seem to be ensuring that the URL you pass includes the scheme (in your case, "file:"). Holy Toledo! That's a two-year-old bug spanning two versions of the language! BTW, Chris, the snippet I showed in the title essentially WAS the exact code. It's a method with that single line called from a wxPython Help menu. I can't really put an absolute pathname into the argument, because the application is going to be distributed to a variety of computers at my workplace, and there's no assurance that it will go into (or remain in)a particular folder. I was trying to avoid using the wx.html.HtmlWindow feature of wxPython, because it doesn't handle CSS and styles. My help page is the portal to a multi-page users guide with a style sheet to render all the content consistently. Plus, I couldn't get the wx.html.HtmlWindow to open relative paths either -- it gave me "URL Malformed" messages even in KDE, when webbrowser.open("filepath") was working for the exact same path. But that's something to take up on the wxPython list, I guess. This to me illustrates the downside of the Python philosophy of "There should be only one obvious way to do things". If that one obvious way has a fatal bug, you're pretty much SOL. I've had a brief look at webbrowser.py. It's looking for the browsers in the paths listed in the PATH environment variable. On my PC at least, the paths to the other browsers, such as "C:\Program Files\Mozilla Firefox" for Firefox, aren't listed there, hence the only one it can find is Internet Explorer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 04:44 PM, Chris Angelico wrote: On Mon, Feb 25, 2013 at 8:08 AM, Roy Smith wrote: >> In article , >> Chris Angelico wrote: >> no need to remember what's an object and what's not -- everything is an object > > Careful on the citations - Ethan Furman said that, I just quoted him. > >> Well, not quite everything. If I write: >> >> if foo: >> do_this() >> and_this() >> >> the code block making up the body of the "if" statement is not an >> object. In some languages, it is. > > Maybe, but the code of an entire function *is*. Granted, it's not an > object that can be built up manually (at least, not that I know of), > and it offers only limited functionality (dis.dis, but not a lot > else), so really it could be seen as just an implementation detail of > the function object itself. But it's still an object. > > ChrisA But if block doesn't have to be inside a function, right? It needs to be inside a module, but then again everything is inside a module, but it wouldn't be very object-oriented if the module was the only object in Python :-). -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ The press, the machine, the railway, the telegraph are premises whose thousand-year conclusion no one has yet dared to draw. Friedrich Nietzsche -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 12:58 PM, Chris Angelico wrote: On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman wrote: - no variable declarations, just use 'em Variable declarations can go either way; Python requires you to name all globals that you mutate I'm not sure what you mean -- example? -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
Josh, Not thank you for your malicious post. I think you are missing the point here. My source code was just a dummy to offer context for the question I wanted to ask. Further down the line, if I ever feel I don't need to pseudo-declare variables I will stop doing it. But for the moment I am trying to imitate familiar ground. My code as written has no syntax errors, so what's the problem? It is highly unlikely you will ever read any of my Python code - no need to get excited over a few of my lines. And you don't need to answer questions which were not posed, thank you. I wanted Python to register what type of variable I'm after. So I init my vars accordingly, int might be 0, float 0.0 and string with null, err... None. In practice, I wouldn't define an intX_asString var, I would do "str (num)" every time a string representation is needed, provided it isn't a loop, as in that context the expression would probably negatively impact performance in an interpreted language. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 10:46 AM, piterrr.dolin...@gmail.com wrote: Hi guys, Question. Have this code intX = 32 # decl + init int var intX_asString = None # decl + init with NULL string var None is not a str, and it's not a "NULL string var" Perhaps what you want is intX_asString = "" > I am using a Python IDE called PyScripter. Its Intellisense is full > of methods starting and ending with "__", hence the question. I'm surprised; I'd expect the Intellisense to filter those out by default, since people seldom should call them. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, Feb 24, 2013 at 5:43 PM, wrote: > Josh, > > Not thank you for your malicious post. > I think you are missing the point here. > > My source code was just a dummy to offer context for the question I wanted > to ask. Further down the line, if I ever feel I don't need to > pseudo-declare variables I will stop doing it. But for the moment I am > trying to imitate familiar ground. > > My code as written has no syntax errors, so what's the problem? It is > highly unlikely you will ever read any of my Python code - no need to get > excited over a few of my lines. > > And you don't need to answer questions which were not posed, thank you. > > I wanted Python to register what type of variable I'm after. So I init my > vars accordingly, int might be 0, float 0.0 and string with null, err... > None. > In a language that defines Names that are bound to objects, there can't be a 'type' inferred. In C or similar, when you delcair the variable you are setting aside the memory to hold something of that type. This is compile time typing. That isn't how python works, so naming something an int will never make it an int. intMe = 'Joel' is totally valid in python. Sticking to ideas like this will hinder understanding of how python works. I suggest taking two hours to study the python documentation at python.org. I don't speak Chinese, but I know that I can't just use a dictionary of English to Chinese and use the same syntax. It won't be Chinese. Get over your prejudice and learn the new language or don't, but trying to shoe horn python into the concepts of another language won't help you understand python, it will produce ugly, messy, unsupportable code. > > In practice, I wouldn't define an intX_asString var, I would do "str > (num)" every time a string representation is needed, provided it isn't a > loop, as in that context the expression would probably negatively impact > performance in an interpreted language. > > Peter > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Motorola RAZR HD
Motorola RAZR HD http://www.youtube.com/watch?v=u28AjcMah0o -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24 February 2013 22:43, wrote: > Josh, > > Not thank you for your malicious post. > Be careful, us programmers do *eventually* catch on to who is a troll, and if you say things like that we may eventually mark you off as just to hostile. I *honestly* meant no malice or insult. If you can't take my word, you can point out what I said that was otherwise. (Then again, you'll have about a week before we really start to notice :P) > I think you are missing the point here. > > My source code was just a dummy to offer context for the question I wanted > to ask. Further down the line, if I ever feel I don't need to > pseudo-declare variables I will stop doing it. But for the moment I am > trying to imitate familiar ground. > > My code as written has no syntax errors, so what's the problem? It is > highly unlikely you will ever read any of my Python code - no need to get > excited over a few of my lines. > You said "Any comments on this before I quit my job?". I commented on how I think you should approach Python in order to appreciate its virtues rather than get stuck in its differences. Again, I am no good programmer, but I think these methods will help you. > And you don't need to answer questions which were not posed, thank you. > Nor do I need to answer questions which were posed. > I wanted Python to register what type of variable I'm after. So I init my > vars accordingly, int might be 0, float 0.0 and string with null, err... > None. > You seem to think that a "null" version of a type is the falsy version. Then: int -> 0 float -> 0. tuple -> () list -> [] And then (*dun dun duuun!*): str -> "" (NOT None, which is a different type) Other people have commented on whether this is a good idea (it's not), so I'll suggest you read those, too. In practice, I wouldn't define an intX_asString var, I would do "str (num)" > every time a string representation is needed, provided it isn't a loop, as > in that context the expression would probably negatively impact performance > in an interpreted language. PS: Guess what str(None) is. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24 February 2013 22:08, Chris Angelico wrote: > On Mon, Feb 25, 2013 at 8:35 AM, Joshua Landau > wrote: > > def solve_quadratic(a, b, c): > > """Solve a quadratic equation of the form ax² + bx + c = 0 > > > > The result will be a tuple of the two results; the results can be equal > if > > the determinant is 0. > > This supports imaginary results for if the determinant is negative.""" > > ... > > results = [top/(2*a) for top in fraction_tops] > > Yeah, I think we know which one is the more readable... Just to > nit-pick a little though, that returns a list when its docstring says > it'll return a tuple :) > Good catch. > Other than that (which is probably better solved by changing the docs > than the code), the only change I'd make would be to ditch the > fraction_tops temporary (and to throw out some of the comments that > serve only to reexpress the code that immediately follows them, though > for a demo they're entirely appropriate). > I knew someone would critique it. It's an exaggerated demo for foo's sake. Heck, who even uses a function like that (or uses unicode in comments :P)? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 24 February 2013 23:18, Oscar Benjamin wrote: > On 24 February 2013 21:35, Joshua Landau > wrote: > > > > determinant = b**2 - 4*a*c > > It's called the discriminant. A determinant is something altogether > different. *cries at own idiocy* Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
> Most of what gets hung in art galleries these days is far less > visually pleasing than well-written code. +1 QOTW -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
>> intX = 32 # decl + init int var > How is it not obvious that "intX" is an integer *without* the comment? Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary. Regarding my use of None to mean NULL, point taken to use "" instead. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 9:33 AM, Ethan Furman wrote: > On 02/24/2013 12:58 PM, Chris Angelico wrote: >> >> On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman wrote: >>> >>> >>>- no variable declarations, just use 'em >> >> >> Variable declarations can go either way; Python requires you to name >> all globals that you mutate > > > I'm not sure what you mean -- example? Whoops, said the wrong thing. All globals that you assign to. >>> a=1 >>> b=[] >>> def foo(x): y=x+1 global a a+=x b.append(y) >>> foo(2) >>> a 3 >>> b [3] Python requires that you name 'a' in a global statement; C would require a declaration for 'y' to make it local. PHP, meanwhile, would require declarations for both a and b. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 10:38 AM, wrote: > >>> intX = 32 # decl + init int var >> How is it not obvious that "intX" is an integer *without* the comment? > > Indeed the assignment is enough to deduce "intX" is an int. The comment is > there to let me know it is unlikely intX appears earlier in the code. Please, > let me do things my way until I find reasons to the contrary. > > Regarding my use of None to mean NULL, point taken to use "" instead. It's worth noting that None does make a good rendition of "null-as-opposed-to-blank", for instance when you're fetching data from an VARCHAR field in an SQL database. You'd use a string for anything that isn't NULL, and None for the others. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
For the record, I completely misread and misunderstood the question. I should stop posting that late at night :P On Sun, Feb 24, 2013 at 1:25 AM, Demian Brecht wrote: > Rather than using a relative path, try using > webbrowser.open('{}/documentation/help.html'.format(os.path.dirname(__file__))). > > On Sun, Feb 24, 2013 at 1:17 AM, llanitedave wrote: >> I created an html help page for my Python 2.7.3 application and put it in a >> documentation folder. I used webbrowser.open() to fetch the page. >> >> On linux -- KDE specifically, the command opens the local file on my default >> browser with no issues. However, on Windows 7, it opens Internet Explorer, >> which doesn't even search the local folder, but goes straight to the web and >> does a Google search, returning nothing but useless noise. >> >> My default browser on Windows is Chrome, so my intention is getting >> undermined right from the start. >> >> How do I get a local html file to open properly from Python in Windows? >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > > -- > Demian Brecht > http://demianbrecht.github.com -- Demian Brecht http://demianbrecht.github.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 03:38 PM, piterrr.dolin...@gmail.com wrote: intX = 32 # decl + init int var How is it not obvious that "intX" is an integer *without* the comment? Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary. Of course you can, but wouldn't you rather find reasons to the contrary by us telling you, instead of tripping over something yourself? For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain X as an integer: --> intX = 32 --> intX = intX / 3.0 --> intX 10.66 -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
using urllib on a more complex site
I'm trying to write a simple script to scrape http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day in order to send myself an email every day of the 99c movie of the day. However, using a simple command like (in Python 3.0): urllib.request.urlopen('http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read() I don't get the all the source I need, its just the navigation buttons. Now I assume they are using some CSS/javascript witchcraft to load all the useful data later, so my question is how do I make urllib "wait" and grab that data as well? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
> For example (I believe it's already been mentioned) "declaring" intX with > some integer value does *nothing* to maintain > > X as an integer: > > --> intX = 32 > > --> intX = intX / 3.0 > > --> intX > > 10.66 > Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: using urllib on a more complex site
On Sunday, February 24, 2013, Adam W. wrote: > I'm trying to write a simple script to scrape > http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day > > in order to send myself an email every day of the 99c movie of the day. > > However, using a simple command like (in Python 3.0): > urllib.request.urlopen(' > http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read( > ) > > I don't get the all the source I need, its just the navigation buttons. > Now I assume they are using some CSS/javascript witchcraft to load all the > useful data later, so my question is how do I make urllib "wait" and grab > that data as well? > urllib isn't a web browser. It just requests the single (in this case, HTML) file from the given URL. It does not parse the HTML (indeed, it doesn't care what kind of file you're dealing with); therefore, it obviously does not retrieve the other resources linked within the document (CSS, JS, images, etc.) nor does it run any JavaScript. So, there's nothing to "wait" for; urllib is already doing everything it was designed to do. Your best bet is to open the page in a web browser yourself and use the developer tools/inspectors to watch what XHR requests the page's scripts are making, find the one(s) that have the data you care about, and then make those requests instead via urllib (or the `requests` 3rd-party lib, or whatever). If the URL(s) vary, reverse-engineering the scheme used to generate them will also be required. Alternatively, you could use something like Selenium, which let's you drive an actual full web browser (e.g. Firefox) from Python. Cheers, Chris -- Cheers, Chris -- http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: using urllib on a more complex site
On 02/24/2013 07:02 PM, Adam W. wrote: I'm trying to write a simple script to scrape http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day in order to send myself an email every day of the 99c movie of the day. However, using a simple command like (in Python 3.0): urllib.request.urlopen('http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read() I don't get the all the source I need, its just the navigation buttons. Now I assume they are using some CSS/javascript witchcraft to load all the useful data later, so my question is how do I make urllib "wait" and grab that data as well? The CSS and the jpegs, and many other aspects of a web "page" are loaded explicitly, by the browser, when parsing the tags of the page you downloaded. There is no sooner or later. The website won't send the other files until you request them. For example, that site at the moment has one image (prob. jpeg) highlighted, http://images2.vudu.com/poster2/179186-m"; alt="Sex and the City: The Movie (Theatrical)"> if you want to look at that jpeg, you need to download the file url specified by the src attribute of that img element. Or perhaps you can just look at the 'alt' attribute, which is mainly there for browsers who don't happen to do graphics, for example, the ones for the blind. Naturally, there may be dozens of images on the page, and there's no guarantee that the website author is trying to make it easy for you. Why not check if there's a defined api for extracting the information you want? Check the site, or send a message to the webmaster. No guarantee that tomorrow, the information won't be buried in some javascript fragment. Again, if you want to see that, you might need to write a javascript interpreter. it could use any algorithm at all to build webpage information, and the encoding could change day by day, or hour by hour. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 25 February 2013 00:08, wrote: > > For example (I believe it's already been mentioned) "declaring" intX > with some integer value does *nothing* to maintain > > > > X as an integer: > > > > --> intX = 32 > > > > --> intX = intX / 3.0 > > > > --> intX > > > > 10.66 > > > > Yes I did see that it is possible to redefine the type of a variable. But > I don't think I would ever do this intentionally; need to be really careful > with Python. Not necessarily. Python duck types. If you don't know what that means, Google's got a ton on it. Take a look at my really bad quadratic equation solver. It supports integer input, float input and complex input. It will output a list of two floats or complex numbers. That's a use for having one variable have different types. You'll find thousands of parallels in real, working code. Hence, you don't really need to be careful. You'd probably benefit if you stopped thinking of supporting type-changing as "dangerous" and started thinking of it as "useful". -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 25/02/2013 00:08, piterrr.dolin...@gmail.com wrote: For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain X as an integer: --> intX = 32 --> intX = intX / 3.0 --> intX 10.66 Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python. Peter Yes this is a big downside with Python. Sadly it means us poor Python programmers have to waste a lot of time and effort testing our code, unlike those who use statically typed languages which work perfectly once they've been compiled. -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
yield expression
Hi folks, When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time. Thanks ! code snippet: def counter(start_at=0): count = start_at while True: val = (yield count) if val is not None: count = val else: print 'val is None' count += 1 -- http://mail.python.org/mailman/listinfo/python-list
yield expression
Hi folks, When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time. Thanks ! code snippet: def counter(start_at=0): count = start_at while True: val = (yield count) if val is not None: count = val else: print 'val is None' count += 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 04:08 PM, piterrr.dolin...@gmail.com wrote: For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain X as an integer: --> intX = 32 --> intX = intX / 3.0 --> intX 10.66 Yes I did see that it is possible to redefine the type of a variable. And that right there is one of the key aspects of Python: there are no variables, only objects, and objects' types cannot be changed. objects can be labeled with names, but the names are more like sticky notes, and can be peeled off and stuck on some other object. -- ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 16:08:01 -0500, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> > no need to remember what's an object and what's not -- everything is >> > an object > > Well, not quite everything. If I write: > > if foo: >do_this() >and_this() > > the code block making up the body of the "if" statement is not an > object. In some languages, it is. In Python, that code block isn't any *thing*. It's merely a small part of the enclosing code block, which *is* an object. When we say "everything is an object" in Python, we're talking about values, not arbitrary language constructs. The "*3" bit of "y = x*3" is not a value, a for-loop is not a value, and the delay you experience when you call time.sleep(30) is not a value, so none of these things are objects. This is not to reduce the importance of these things as programming concepts, but they aren't the kind of things we mean when we say everything is an object. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , Ethan Furman wrote: > On 02/24/2013 03:38 PM, piterrr.dolin...@gmail.com wrote: > > > >>> intX = 32 # decl + init int var > >> How is it not obvious that "intX" is an integer *without* the comment? > > > > Indeed the assignment is enough to deduce "intX" is an int. The comment is > > there to let me know it is unlikely intX appears earlier in the code. > > Please, let me do things my way until I find reasons to the contrary. > > Of course you can, but wouldn't you rather find reasons to the contrary by us > telling you, instead of tripping > over something yourself? > > For example (I believe it's already been mentioned) "declaring" intX with > some integer value does *nothing* to maintain > X as an integer: > > --> intX = 32 > --> intX = intX / 3.0 > --> intX > 10.66 I could imagine a getattr-based implementation of DBC (Design By Contract) which does use the variable name to enforce type. Unclear if this is a Good Thing, a Bad Thing, or a just plain Crazy Thing. In any cae, it would be a neat (if somewhat advanced) exercise for somebody interested in enforcing types and looking to explore some of the more arcane corners of Python. class DBC_Example: # Ad-libbing this, code not tested def __setattr__(self, name, value): if name.startswith('int'): assert isinstance(value, int) self.__dict__[name] = value -- http://mail.python.org/mailman/listinfo/python-list
Re: yield expression
On 25 February 2013 00:39, Ziliang Chen wrote: > Hi folks, > When I am trying to understand "yield" expression in Python2.6, I did the > following coding. I have difficulty understanding why "val" will be "None" ? > What's happening under the hood? It seems to me very time the counter resumes > to execute, it will assign "count" to "val", so "val" should NOT be "None" > all the time. > > Thanks ! > > code snippet: > > def counter(start_at=0): > count = start_at > while True: > val = (yield count) > if val is not None: > count = val > else: > print 'val is None' > count += 1 The value of the yield expression is usually None. yield only returns a value if the caller of a generator function sends one with the send method (this is not commonly used). The send method supplies a value to return from the yield expression and then returns the value yielded by the next yield expression. For example: >>> g = counter() >>> next(g) # Need to call next() once to suspend at the first yield call 0 >>> g.send('value for count') # Now we can send a value for yield to return 'value for count' Oscar -- http://mail.python.org/mailman/listinfo/python-list
yield expression
Hi folks, When I am trying to understand "yield" expression in Python2.6, I did the following coding. I have difficulty understanding why "val" will be "None" ? What's happening under the hood? It seems to me very time the counter resumes to execute, it will assign "count" to "val", so "val" should NOT be "None" all the time. Thanks ! code snippet: def counter(start_at=0): count = start_at while True: val = (yield count) if val is not None: count = val else: print 'val is None' count += 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 25 February 2013 00:08, wrote: Chris Angelico wrote: >> For example (I believe it's already been mentioned) "declaring" intX with >> some integer value does *nothing* to maintain >> >> X as an integer: >> >> --> intX = 32 >> >> --> intX = intX / 3.0 >> >> --> intX >> >> 10.66 >> > > Yes I did see that it is possible to redefine the type of a variable. But I > don't think I would ever do this intentionally; need to be really careful > with Python. You do need to be careful around types in Python (as in all languages). It took some time for me to understand how to use types in Python. After a while I came to realise that not knowing exactly the type of a particular object is not as much of a problem as I initially thought. Once you understand how to use this ambiguity to your advantage it becomes possible to write very flexible code that can be reused without ambiguity in situations that you haven't yet anticipated. The key mental hurdle, I think, is to realise that instead of relying on compilation errors to spot (a small subset of) your programming errors, you are relying on runtime exceptions. Python still gives errors when you use an object in a way that is inconsistent with its type; you just don't see those errors at compile time. The trickier cases are ones where two types are very similar and can be used similarly in most, but not all, situations. An example of this would be the one that Chris has highlighted where an object that you expected to be an int is actually a float. I find that I need to be careful when using division on quantities that I expected to be integers (true in all languages) and careful about the notation used in a numeric literal. Once you get used to it, you will find it easy to see that the '.0' that Chris appended was deliberate in order to control the type of the resulting object. Oscar -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
In article , piterrr.dolin...@gmail.com wrote: > Yes I did see that it is possible to redefine the type of a variable. But I > don't think I would ever do this intentionally One does not need language features to protect themselves against things they do intentionally. They need language features to protect themselves against things they do by accident. Different languages protect you from different things. Compare, for example, C++ and Python. C++ protects you against accidentally passing an int where you were supposed to pass a float. Well, no, with automatic type promotion, that's a bad example. But it does prevent you from passing an IntThing where you were supposed to pass a FloatThing (assuming IntThing is not a subclass of FloatThing, and a few other details). But, Python protects you from dereferencing a null pointer, or double-freeing a pointer. There's just no way to even write those concepts in Python. You pays your money and you takes your chances. Pick which type of protection you feel is more important and use the language which gives you that. > need to be really careful with Python. You need to be really careful with all programming languages. You just need to be careful about different things. -- http://mail.python.org/mailman/listinfo/python-list
Re: yield expression
On Monday, February 25, 2013 8:51:28 AM UTC+8, Oscar Benjamin wrote: > On 25 February 2013 00:39, Ziliang Chen wrote: > > > Hi folks, > > > When I am trying to understand "yield" expression in Python2.6, I did the > > following coding. I have difficulty understanding why "val" will be "None" > > ? What's happening under the hood? It seems to me very time the counter > > resumes to execute, it will assign "count" to "val", so "val" should NOT be > > "None" all the time. > > > > > > Thanks ! > > > > > > code snippet: > > > > > > def counter(start_at=0): > > > count = start_at > > > while True: > > > val = (yield count) > > > if val is not None: > > > count = val > > > else: > > > print 'val is None' > > > count += 1 > > > > The value of the yield expression is usually None. yield only returns > > a value if the caller of a generator function sends one with the send > > method (this is not commonly used). The send method supplies a value > > to return from the yield expression and then returns the value yielded > > by the next yield expression. For example: > > > > >>> g = counter() > > >>> next(g) # Need to call next() once to suspend at the first yield call > > 0 > > >>> g.send('value for count') # Now we can send a value for yield to return > > 'value for count' > > > > > > Oscar Thanks Oscar ! I am cleared. Only when "send" is used to feed "yield" a new value, the "yield" expression has none "None", otherwise, "yield" expression has "None" value. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 11:40:05 -0800, piterrr.dolinski wrote: >> > if (some statement): # short form >> > >> > rather than >> > >> > if (some statement == true): # long form >> >> >> What all those ugly brackets are for? >> >> > Mark, > > Back in the day when C was king, or take many newer long established > languages (C#, Java), Python is older than either C# or Java. Why have those languages paid no attention to the innovations of Python, instead of copying the misfeatures of C? Pascal and Algol and Fortran are older than C. Why did C introduce unnecessary brackets when these older languages did not need them? > the use of () has been widespread and mandated by > the compilers. I have never heard anyone moan about the requirement to > use parentheses. You have not been paying attention. In many ways, C has been a curse on programming. It has trained large numbers of coders to expect and *demand* poor syntax. > Now come Python in which parens are optional, and all > of a sudden they are considered bad and apparently widely abandoned. Do > you really not see that code with parens is much more pleasing visually? That's funny. Perhaps you should be programming in Lisp. > I could understand someone's reluctance to use parens if they are very > new to programming and Pythons is their first language. But my > impression here is that most group contributors are long-time > programmers and have long used () where they are required. Again, I'm > really surprised the community as a whole ignores the programming > "heritage" and dumps the parens in a heartbeat. (Because they are unnecessary) (visual noise) (that don't add anything) (useful) (to the reader's understanding) (of the code). -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: webbrowser.open("./documentation/help.html")-- No Go in Windows
On Sunday, February 24, 2013 2:15:10 PM UTC-8, MRAB wrote: > On 2013-02-24 20:28, llanitedave wrote: > > > On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote: > > [snip] > > >> Sounds like this might be your problem: > > >> > > >> http://bugs.python.org/issue8936 > > >> > > >> The fix would seem to be ensuring that the URL you pass includes > > >> the scheme (in your case, "file:"). > > >> > > > Holy Toledo! That's a two-year-old bug spanning two versions of the > > > language! > > > > > > BTW, Chris, the snippet I showed in the title essentially WAS the > > > exact code. It's a method with that single line called from a > > > wxPython Help menu. I can't really put an absolute pathname into the > > > argument, because the application is going to be distributed to a > > > variety of computers at my workplace, and there's no assurance that > > > it will go into (or remain in)a particular folder. > > > > > > I was trying to avoid using the wx.html.HtmlWindow feature of > > > wxPython, because it doesn't handle CSS and styles. My help page is > > > the portal to a multi-page users guide with a style sheet to render > > > all the content consistently. > > > > > > Plus, I couldn't get the wx.html.HtmlWindow to open relative paths > > > either -- it gave me "URL Malformed" messages even in KDE, when > > > webbrowser.open("filepath") was working for the exact same path. But > > > that's something to take up on the wxPython list, I guess. > > > > > > This to me illustrates the downside of the Python philosophy of > > > "There should be only one obvious way to do things". If that one > > > obvious way has a fatal bug, you're pretty much SOL. > > > > > I've had a brief look at webbrowser.py. It's looking for the browsers in > > the paths listed in the PATH environment variable. > > > > On my PC at least, the paths to the other browsers, such as "C:\Program > > Files\Mozilla Firefox" for Firefox, aren't listed there, hence the only > > one it can find is Internet Explorer. Well, it's still very odd, because when I use wxPython's wx.html.HtmlWindow to click a web link, it DOES use the default browser, which is Chrome on my PC. It's just using the webbrowser.open() function that goes to IE. Until then, I'd been suspecting that wx.html.HtmlWindow was using webbrowser.open() under the hood. I guess not. But wx.html.HtmlWindow doesn't work on relative paths, it seems (in neither Linux NOR Windows), so I'm not able to find a substitute as of yet. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 16:08:06 -0800, piterrr.dolinski wrote: >> For example (I believe it's already been mentioned) "declaring" intX >> with some integer value does *nothing* to maintain >> >> X as an integer: >> >> --> intX = 32 >> >> --> intX = intX / 3.0 >> >> --> intX >> >> 10.66 >> >> > Yes I did see that it is possible to redefine the type of a variable. Variables do not have types in Python. Reset your thinking. Python is a dynamic language with name bindings and strongly-typed objects, not a static language with strongly-typed variables. If you don't understand the difference, ask. But so long as you make the wrong assumptions about the language, you will have a bad time. You will find programming much easier, and more pleasant, if you learn the semantics and idioms of the language you are using, instead of trying to treat every language as the same. > But I don't think I would ever do this intentionally; need to be really > careful with Python. Not at all. The only difference is whether you get a compiler error or a runtime error. Instead of: 10 Write code. 20 Compile. 30 If compiler error, GO TO 10. 40 REM code compiles, but it still needs to be tested 50 Test code. 60 If error, GO TO 10. 70 Deploy. we have: 10 Write code. 20 Test code. 30 If error, GO TO 10. 40 Deploy. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Sun, 24 Feb 2013 17:40:54 -0500, Mitya Sirenef wrote: > But if block doesn't have to be inside a function, right? It needs to be > inside a module, but then again everything is inside a module, but it > wouldn't be very object-oriented if the module was the only object in > Python :-). Python doesn't have code blocks as distinct values. I suppose you could fake it using compile() and eval() by hand, but it wouldn't work very well. Ruby-style code blocks have been requested for many years. GvR has given his support to this *in principle*, but it depends on somebody thinking up decent, unambiguous syntax that works with the rest of Python. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: using urllib on a more complex site
On Sunday, February 24, 2013 7:30:00 PM UTC-5, Dave Angel wrote: > On 02/24/2013 07:02 PM, Adam W. wrote: > > > I'm trying to write a simple script to scrape > > http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day > > > > > > in order to send myself an email every day of the 99c movie of the day. > > > > > > However, using a simple command like (in Python 3.0): > > > urllib.request.urlopen('http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read() > > > > > > I don't get the all the source I need, its just the navigation buttons. > > Now I assume they are using some CSS/javascript witchcraft to load all the > > useful data later, so my question is how do I make urllib "wait" and grab > > that data as well? > > > > > > > The CSS and the jpegs, and many other aspects of a web "page" are loaded > > explicitly, by the browser, when parsing the tags of the page you > > downloaded. There is no sooner or later. The website won't send the > > other files until you request them. > > > > For example, that site at the moment has one image (prob. jpeg) > > highlighted, > > > > http://images2.vudu.com/poster2/179186-m"; > > alt="Sex and the City: The Movie (Theatrical)"> > > > > if you want to look at that jpeg, you need to download the file url > > specified by the src attribute of that img element. > > > > Or perhaps you can just look at the 'alt' attribute, which is mainly > > there for browsers who don't happen to do graphics, for example, the > > ones for the blind. > > > > Naturally, there may be dozens of images on the page, and there's no > > guarantee that the website author is trying to make it easy for you. > > Why not check if there's a defined api for extracting the information > > you want? Check the site, or send a message to the webmaster. > > > > No guarantee that tomorrow, the information won't be buried in some > > javascript fragment. Again, if you want to see that, you might need to > > write a javascript interpreter. it could use any algorithm at all to > > build webpage information, and the encoding could change day by day, or > > hour by hour. > > > > -- > > DaveA The problem is, the image url you found is not returned in the data urllib grabs. To be clear, I was aware of what urllib is supposed to do (ie not download image data when loading a page), I've used it before many times, just never had to jump through hoops to get at the content I needed. I'll look into figuring out how to find XHR requests in Chrome, I didn't know what they called that after the fact loading, so now my searching will be more productive. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 11:45 AM, Oscar Benjamin wrote: > On 25 February 2013 00:08, wrote: > Chris Angelico wrote: >>> For example (I believe it's already been mentioned) "declaring" intX with >>> some integer value does *nothing* to maintain >>> >>> X as an integer: >>> >>> --> intX = 32 >>> >>> --> intX = intX / 3.0 >>> >>> --> intX >>> >>> 10.66 >>> >> >> Yes I did see that it is possible to redefine the type of a variable. But I >> don't think I would ever do this intentionally; need to be really careful >> with Python. > The trickier cases are ones where two types are very similar and can > be used similarly in most, but not all, situations. An example of this > would be the one that Chris has highlighted where an object that you > expected to be an int is actually a float. I find that I need to be > careful when using division on quantities that I expected to be > integers (true in all languages) and careful about the notation used > in a numeric literal. Once you get used to it, you will find it easy > to see that the '.0' that Chris appended was deliberate in order to > control the type of the resulting object. Once again, Ethan gets the short end of the citations stick... 'twarn't me wrote that, he did. Not that it's at all contrary to my views, and I might well have said it if he hadn't, but credit should go his direction :) Note though that in Python 3, you don't need the explicit .0 to force it to float (and __future__ can bring that to Python 2 too). 32/3 -> 10.6, int/int->float. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On Mon, Feb 25, 2013 at 12:04 PM, Steven D'Aprano wrote: > Not at all. The only difference is whether you get a compiler error or a > runtime error. Instead of: > > 10 Write code. > 20 Compile. > 30 If compiler error, GO TO 10. > 40 REM code compiles, but it still needs to be tested > 50 Test code. > 60 If error, GO TO 10. > 70 Deploy. > > we have: > > 10 Write code. > 20 Test code. > 30 If error, GO TO 10. > 40 Deploy. The advantage of compile-time errors is that you don't have to wait for *that branch* to be executed. So they're hugely advantageous when it comes to the "obvious problems" like syntactic errors... and looky here, Python does exactly that :) The only difference between "static" and "dynamic" is how much is done at each phase. If your program has no inputs or side effects, the compiler could theoretically convert it into a single static output statement. Voila! All your run-time errors have become compile-time errors. Conversely, you could compile your application from source just before running it. Voila! Everything's a run-time error, even the omission of a semicolon in C. Okay, both of those are pretty stupid examples, but there's really no fundamental difference. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: using urllib on a more complex site
On Sunday, February 24, 2013 7:27:54 PM UTC-5, Chris Rebert wrote: > On Sunday, February 24, 2013, Adam W. wrote: > I'm trying to write a simple script to scrape > http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day > > > > > in order to send myself an email every day of the 99c movie of the day. > > > > However, using a simple command like (in Python 3.0): > > urllib.request.urlopen('http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read() > > > > > I don't get the all the source I need, its just the navigation buttons. Now > I assume they are using some CSS/javascript witchcraft to load all the useful > data later, so my question is how do I make urllib "wait" and grab that data > as well? > > > > > > urllib isn't a web browser. It just requests the single (in this case, HTML) > file from the given URL. It does not parse the HTML (indeed, it doesn't care > what kind of file you're dealing with); therefore, it obviously does not > retrieve the other resources linked within the document (CSS, JS, images, > etc.) nor does it run any JavaScript. So, there's nothing to "wait" for; > urllib is already doing everything it was designed to do. > > > > Your best bet is to open the page in a web browser yourself and use the > developer tools/inspectors to watch what XHR requests the page's scripts are > making, find the one(s) that have the data you care about, and then make > those requests instead via urllib (or the `requests` 3rd-party lib, or > whatever). If the URL(s) vary, reverse-engineering the scheme used to > generate them will also be required. > > > > Alternatively, you could use something like Selenium, which let's you drive > an actual full web browser (e.g. Firefox) from Python. > > > Cheers, > Chris > > > -- > Cheers, > Chris > -- > http://rebertia.com Huzzah! Found it: http://apicache.vudu.com/api2/claimedAppId/myvudu/format/application*2Fjson/callback/DirectorSequentialCallback/_type/contentSearch/count/30/dimensionality/any/followup/ratingsSummaries/followup/totalCount/offset/0/tag/99centOfTheDay/type/program/type/season/type/episode/type/bundle Thanks for the tip about XHR's -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
On 02/24/2013 03:40 PM, Mitya Sirenef wrote: > But if block doesn't have to be inside a function, right? It needs > to be inside a module, but then again everything is inside a module, but > it wouldn't be very object-oriented if the module was the only object in > Python :-). A module indeed fits into the OOP paradigm. It's called a singleton and I love the fact that I can define and use them in python without wrapping them in tons of boilerplate class and factory code. -- http://mail.python.org/mailman/listinfo/python-list