Re: Is it possible to get image size before/without downloading?
In the head of an HTTP response, most servers will specify a Content-Length that is the number of bytes in the body of the response. Normally, when using the GET method, the header is returned with the body following. It is possible to make a HEAD request to the server that will only return header information that will hopefully tell you the file size. If you want to know the actual dimensions of the image, I don't know of anything in HTTP that will tell you. You will probably just have to download the image to find that out. Relevant HTTP specs below if you care. http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html The above is true regardless of language. In python it appears there an httplib module. I would call request using the method head. http://docs.python.org/lib/httpconnection-objects.html [EMAIL PROTECTED] wrote: Hi there: a bit of a left-field question, I think. I'm writing a program that analyses image files downloaded with a basic crawler, and it's slow, mainly because I only want to analyse files within a certain size range, and I'm having to download all the files on the page, open them, get their size, and then only analyse the ones that are in that size range. Is there a way (in python, of course!) to get the size of images before or without downloading them? I've checked around, and I can't seem to find anything promising... Anybody got any clues? Cheers, Al. -- http://mail.python.org/mailman/listinfo/python-list
Nested function scope problem
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work either. But, changing the variable in the containing scope is the whole purpose of this helper function. I'm new to python, so there is probably some solution I haven't encountered yet. Could you please suggest a nice clean solution? The offending code is below. Thanks. def breakLine(s): Break a string into a list of words and symbols. def addTok(): if len(tok) 0: ls.append(tok) tok = '' ls = [] tok = '' splitters = '?()|:~,' whitespace = ' \t\n\r' for c in s: if c in splitters: addTok() ls.append(c) elif c in whitespace: addTok() else: tok = tok + c addTok() return ls #some tests to make sure it works print breakLine('carolina(Prada):cat(X,Y)') print breakLine('trouble :bird (X ) cat ( Y )') print breakLine('?trouble') -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested function scope problem
Thank you for your corrections to the previous code. Your regex solution is definitely much cleaner. Referring to your other suggestions, is the advantage of using a list of chars instead of adding to a string just a bow to big-O complexity, or are there other considerations? First I had tried appending to the string, but it seems they are immutable. It seems that using a list for a string isn't a very clear way to represent a mutable string. Although I gladly accept that using a regex is the best solution to this problem, I am still interested in knowing how to access the variables in a containing function. It seems that there should be some keyword akin to global that would expose them, or some other method. I have read that python uses nested scopes (or at least was planning to in 2.2), so I wonder what I am missing. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested function scope problem
I just did some timings, and found that using a list instead of a string for tok is significantly slower (it takes 1.5x longer). Using a regex is slightly faster for long strings, and slightly slower for short ones. So, regex wins in both berevity and speed! -- http://mail.python.org/mailman/listinfo/python-list
list of polynomial functions
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am attempting to make a list of polynomial functions such that poly[0](3) = 1, poly[1](3) = 3, poly[2](3) = 9, etc. Could someone point me in the right direction? Thanks. def make_polys(n): Make a list of polynomial functions up to order n. p = lambda x: 1 polys = [p] for i in range(n): polys.append(lambda x: polys[i](x)*x) return polys # construct a vector of polynomials polys = make_polys(5) # print for p in polys: print p(3) -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
You could try SciTE. It has syntax highlighting for almost every language I have heard of plus some, and seems to work pretty well. It has some issues with fonts, and on some computers is unstable (it crashes in linux, and may have issues with multiprocessor machines). I would also like to know if someone has made a good text editor that works in a variety of languages and overcomes some of SciTE's limitations. John Salerno wrote: I know there's a request for a good IDE at least once a week on the ng, but hopefully this question is a little different. I'm looking for suggestions for a good cross-platform text editor (which the features for coding, such as syntax highlighting, etc.) but not a full IDE with all the fancy jazz (GUI developer, UML diagrams, etc.). Ideally, it would be something I could even put on a flash drive and move from computer to computer, but this isn't necessary. Just something I can immediately use in either Windows or Linux (or Mac, if necessary). Based on another thread, I tried out Scite, but no matter what I do it doesn't seem to remember the window size and position, or any options I choose (like showing line numbers). It seems to always reset itself each time I open it. And naturally there are Emacs and Vim, but I just don't know if I need to invest *that* much time into learning one of them (probably Vim, since I hear it's lighter and faster). I've tried a few others, like TextPad and Crimson, and right now I use UltraEdit, which I love actually, except for minor issues here and there. But it'd be nice to make the move, as much as possible, to free, open-source, cross-platform software. Thanks for any suggestions, and again I'm sorry if this feels like the same question as usual (it's just that in my case, I'm not looking for something like SPE, Komodo, Eric3, etc. right now). -- http://mail.python.org/mailman/listinfo/python-list
Re: list of polynomial functions
The `i` is the problem. It's not evaluated when the lambda *definition* is executed but when the lambda function is called. And then `i` is always == `n`. You have to explicitly bind it as default value in the lambda definition: polys.append(lambda x, i=i: polys[i](x)*x) Thank you for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: list of polynomial functions
I'm curious why the very first attempt to call p(3) doesn't bomb out with the NameError that polys wasn't defined before it even got to the point of attempting to call it. In the first call, the 0th lambda function is evaluated, and it was defined as the constant function 1. The functions after that each refer to the previous polynomial, so they mess up. The first doesn't so it's fine. I'm new to this, as evidenced by my original posting, so I may be missing something. I hope I helped, Josiah -- http://mail.python.org/mailman/listinfo/python-list
Existance of of variable
Hello. I am very new to Python, and have been unable to figure out how to check if a variable exists or not. In the following code I have made a kludge that works, but I think that it would be clearer to check if closest exists and not have to initialize it in the first place. How is that check done? The following code finds the closest place to a position and rejects places that are too far away. dist = 1e9 closest = -1 for n,p in galaxy.places.iteritems(): dif = p.pos - pos len = dif.len() if len dist and len 10.0/self.zoom: dist = len closest = p if closest != -1: self.sel = [closest.name] I also have a few other questions to tack on if you don't mind. I am setting dist to 1e9, because that is a larger value than any of the places in the galaxy will be far away. Is there a better way to initialize dist so that it is impossible for this to fail? For example, would setting dist to infinity work, and how is that done? Extending my existance checking question, how does one check what type a variable has? Thanks for your help! -- Josiah -- http://mail.python.org/mailman/listinfo/python-list