Re: [Tutor] How do you turn something into a number?
> I have what I think is a string from socket.recvfrom(...). > I want to turn it into numbers so I tried: > data, address = recvfrom (...stuff...) > s = "" This reads a sequence of bytes from the socket. What those bytes represents only you can know. If they are a sequence of ascii codes then they can be used as a string of characters. If they are binary numbers then they could represent almost anthing and you will have to figure out how to map or convert them to the appropriate representation or format. > number =int(s.join(data[10:13],16)) # turn two bytes of the string > into But this is strange. Starting at the inside it extracts bytes 10,11 and 12 it then passes them to s.join along with the value 16. Thats not a valid call to join(). I therefore assume you intended to have the ')' before the 16 making the 16 a base value to int(). However, using base 16 implies that the three data elements extracted have the ascii values of a hex string of numbers, is this the case? If you actually only want the raw data you read the slice operation is sufficient. If you want a hex string representation of those bytes then you can use the hex() function. > This yaks and says something about improper argument to int. Thats almost certainly the bad parenthesis placement. > I don't understand python's type system at all so any help > here will be greatly appreciated. You could look at the "raw materials" topic in my tutor for a discussion of the various data types in Python. However they are very conventional in form and behaviour. HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching Sorted Lists
> The only list search function I know is List.Index(X), which is > pretty > inefficient I reckon, especially hen the lists are likely to contain > 100's > or 100's of members. Have you tried it? How long is it taking? I'd be very surprised if it there was a serious problem indexing a list of even 1000 members. Premature optimisation is a dangerous trap. If you find you really have a problem then we can start to consider the options! Alan g ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching Sorted Lists
[EMAIL PROTECTED] wrote: > Hi, Can someone tell me if there is a bulit in Binary search function for > python lists ? > > I am currently building lists and sorting them with a comparison function. > The only list search function I know is List.Index(X), which is pretty > inefficient I reckon, especially hen the lists are likely to contain 100's > or 100's of members. Hundreds or thousands of entries are pretty much nothing in computer terms. Unless you have measured that it's a bottleneck in your application, I wouldn't bother finding an alternative to index(). > Is there a search function that uses a compariosn function / binary chop ? > or will I have to implement my own ? It's quite easy to code it yourself if necessary. The Wikipedia has a nice article on it, including sample code which looks very much like Python: http://en.wikipedia.org/wiki/Binary_search Yours, Andrei = Mail address in header catches spam. Real contact info (decode with rot13): [EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur yvfg, fb gurer'f ab arrq gb PP. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] String formatting
Hi, I'm slighty confused with python's string formatting operators. Why is it that this prints as a string: channel, info = server.accept() print "Connection from", info And this doesn't? channel, info = server.accept() print "Connection from %s" % info Also, anyone knows how do I pass arguments to a logger? logger.debug("Connection from:", args) thanks and chrs j. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting
Jorge Louis De Castro a écrit : > Hi, > > I'm slighty confused with python's string formatting operators. > > Why is it that this prints as a string: > > channel, info = server.accept() > print "Connection from", info > > And this doesn't? > > channel, info = server.accept() > print "Connection from %s" % info Well, when using the "%" operator on string always put a tuple or a dictionnary on the RHS : print "Connection from %s" % (info,) Like that, even if info is iterable you'll have the representation of info and not of its first element (if it has only one) and an error if it has more. > > Also, anyone knows how do I pass arguments to a logger? > > logger.debug("Connection from:", args) > > thanks and chrs > j. > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting
Pierre Barbier de Reuille wrote: > Jorge Louis De Castro a écrit : > >>Hi, >> >>I'm slighty confused with python's string formatting operators. >> >>Why is it that this prints as a string: >> >>channel, info = server.accept() >>print "Connection from", info >> >>And this doesn't? >> >>channel, info = server.accept() >>print "Connection from %s" % info What happens when you try this? > Well, when using the "%" operator on string always put a tuple or a > dictionnary on the RHS : > > print "Connection from %s" % (info,) No, you can put a single item on the right without putting it in a tuple: >>> print 'x = %s' % 3 x = 3 >>Also, anyone knows how do I pass arguments to a logger? >> >>logger.debug("Connection from:", args) The first string is a format string, so you could use logger.debug("Connection from: %s", info) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange "snippets" in Learning Python, 2nd ed.
I have the first printing. The snippets are on pp. 159 and 160, and are there to illustrate the loop "else" clause. found = 0 while x and not found: if match(x[0]): # value at front? print 'Ni' found = 1 else: x = x[1:]# slice off front and repeat if not found: print 'not found' while x: # exit when x empty if match(x[0]): print 'Ni' break# exit, go around else x = x[1:] else: print 'Not found'# only here is exhausted x "match()" seems to come out of the blue, and also "Ni". Or have I misunderstood something? Thanks, Dick Moores [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting
Kent Johnson a écrit : > Pierre Barbier de Reuille wrote: > [...] > > >>Well, when using the "%" operator on string always put a tuple or a >>dictionnary on the RHS : >> >>print "Connection from %s" % (info,) > > > No, you can put a single item on the right without putting it in a tuple: > >>> print 'x = %s' % 3 > x = 3 > I never said it does not work, but I say it is Bad Practice (TM). The reason (as I already explained) is, if the variable suddently happen to become a tuple the semantic of your print statement will change ! Just try that if you're not convinced : >>> def foo(x): ... print "x = %s" % x >>> foo(3) >>> foo((3,)) >>> foo((1,2,3)) Now, if you change the function with : >>> def foo(x): ... print "x = %s" % (x,) It will always work as intended ! And when you're debugging this is *very* important ! Pierre -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] os.access vs os.path.isfile
os.access is better/fast that os.path.isfile for checking if exist a file? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.access vs os.path.isfile
> os.access is better/fast that os.path.isfile for checking if exist a file? I don't think we should judge this primarily as a matter of speed, but a matter of functionality. If we do something like: os.access(pathname, os.F_OK) this will tell us if a path name exists. But note that this doesn't necessarily mean that pathname is a regular file: it can be a directory too: ## >>> import os >>> os.access("/usr/share/dict/", os.F_OK) True >>> os.access("/usr/share/dict/words", os.F_OK) True >>> os.access("/foo", os.F_OK) False ## On the other hand, os.path.isfile() checks to see if we're looking at a non-directory file: ## >>> import os.path >>> os.path.isfile("/usr/share/dict/") False >>> os.path.isfile("/usr/share/dict/words") True >>> os.path.isfile("/foo") False ## So they do different things. The intent that you're trying to express is what you should use to choose between the two: what do you really want to check for? Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange "snippets" in Learning Python, 2nd ed.
Dick Moores wrote: > I have the first printing. The snippets are on pp. 159 and 160, and are > there to illustrate the loop "else" clause. > while x: # exit when x empty > if match(x[0]): > print 'Ni' > break# exit, go around else > x = x[1:] > "match()" seems to come out of the blue, and also "Ni". Or have I > misunderstood something? I can't tell you where match() comes from - perhaps it's just a dummy name of a function the purpose of which is left to the imagination of the user. I don't think it's important actually, the point would be that it returns True if some criterium is met and False otherwise. "Ni" comes from a Monty Python movie. The name of the Python language comes from Monty Python, hence the reference to the movie. For more info, google for "knights who say ni". It's quite traditional in the Python world to include Monty Python references in code or even program names. References to idle, eric, spam, dead parrots, shrubberies, cleese, grails, the Spanish inquisition, swallows and just about anything else that seems weird can usually be tracked down to that same source. -- Yours, Andrei = Mail address in header catches spam. Real contact info (decode with rot13): [EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur yvfg, fb gurer'f ab arrq gb PP. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange "snippets" in Learning Python, 2nd ed.
Dick Moores wrote: > I have the first printing. The snippets are on pp. 159 and 160, and are > there to illustrate the loop "else" clause. > > found = 0 > while x and not found: > if match(x[0]): # value at front? > print 'Ni' > found = 1 > else: > x = x[1:]# slice off front and repeat > if not found: > print 'not found' > > > while x: # exit when x empty > if match(x[0]): > print 'Ni' > break# exit, go around else > x = x[1:] > else: > print 'Not found'# only here is exhausted x This is still a pretty strange way to iterate unless the truncated list x is a desirable side effect...it could be for y in x: if match(y): print 'Ni' break else: print 'Not found' Kent > > "match()" seems to come out of the blue, and also "Ni". Or have I > misunderstood something? > > Thanks, > > Dick Moores > [EMAIL PROTECTED] > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String formatting
Ah, thanks for the explanation. I've never been tripped up by that... Kent Pierre Barbier de Reuille wrote: > Kent Johnson a écrit : > >>Pierre Barbier de Reuille wrote: >> > > [...] > >> >>>Well, when using the "%" operator on string always put a tuple or a >>>dictionnary on the RHS : >>> >>>print "Connection from %s" % (info,) >> >> >>No, you can put a single item on the right without putting it in a tuple: >> >>> print 'x = %s' % 3 >>x = 3 >> > > > I never said it does not work, but I say it is Bad Practice (TM). The > reason (as I already explained) is, if the variable suddently happen to > become a tuple the semantic of your print statement will change ! > > Just try that if you're not convinced : > > def foo(x): > > ... print "x = %s" % x > > foo(3) > > foo((3,)) > > foo((1,2,3)) > > > Now, if you change the function with : > > def foo(x): > > ... print "x = %s" % (x,) > > It will always work as intended ! And when you're debugging this is > *very* important ! > > Pierre > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Confused about embedding python in Html
I want to use embedded python in an html page. However, I dont want to force the user to have python installed for the page to work. Is there any way to make the embedded python code be executed by the server? I'm hoping ti use python as an alternative to vbscript and jaava thanks tony ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confused about embedding python in Html
On 8/21/05, Tony Cappellini <[EMAIL PROTECTED]> wrote: > > > I want to use embedded python in an html page. > However, I dont want to force the user to have python installed for the > page to work. > Is there any way to make the embedded python code be executed by the server? > > I'm hoping ti use python as an alternative to vbscript and jaava > > > thanks > > > tony If your web server supports python (i.e. has it installed) then you should be able to run scripts server-side. Check with your host/server. Adam -- PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor