Re: [Tutor] Explanation of this lambda CORRECTION

2007-02-21 Thread Bob Gailer
Bob Gailer wrote: I just noticed missing ) in the last 2 statements. Fixed below. Johan Geldenhuys wrote: Hi all,   I found this function that converts a integer into a 8 bit binary string.   Would somebody care to explain what is happening in this process?

Re: [Tutor] Explanation of this lambda

2007-02-21 Thread Bob Gailer
Johan Geldenhuys wrote: Hi all,   I found this function that converts a integer into a 8 bit binary string.   Would somebody care to explain what is happening in this process?       def intToBin(self, x, count=8): """ Parameters: `x`: integer     Returns a 8 bit

Re: [Tutor] storing text in databases

2007-02-21 Thread Bob Gailer
Chae M wrote: > Is it a reasonable idea to store text (a few hundred words each > record) in a database? I'm learning to use the SQLite database > that comes with Python, and it seems like a good idea in terms > of being able to search for notes, etc. My specific questions: > > - Can formatting o

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Dick Moores
At 09:30 PM 2/21/2007, Dick Moores wrote: >At 06:45 PM 2/21/2007, Terry Carroll wrote: > >I'm glad to have learned your cool Boolean trick ( sign = >['','-'][n<0] ), but I think instead I'll try to make > > >sign = '-' if n<0 else '' > >a habit. Here's some timing info: retiming my computeBin

[Tutor] Explanation of this lambda

2007-02-21 Thread Johan Geldenhuys
Hi all, I found this function that converts a integer into a 8 bit binary string. Would somebody care to explain what is happening in this process? def intToBin(self, x, count=8): """ Parameters: `x`: integer Returns a 8 bit binary string of x """

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Dick Moores
At 06:45 PM 2/21/2007, Terry Carroll wrote: I'm glad to have learned your cool Boolean trick ( sign = ['','-'][n<0] ), but I think instead I'll try to make >sign = '-' if n<0 else '' a habit. My thanks to everyone for the feedback about my function, and with my question about sign = ['',

[Tutor] small personal server

2007-02-21 Thread Kirk Bailey
I recall how much stirr the python web server program created when I copied it from a book and posted it to the list. Well, if you want something composed in C++ for placing in your notebook or desktop, try this: http://smallsrv.com/index.htm -- Salute! -Kirk Bailey T

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Brian van den Broek
Dick Moores said unto the world upon 02/21/2007 08:08 PM: > At 05:17 PM 2/21/2007, Terry Carroll wrote: >> I like the approach of mapping hex or octal digits posted by Alan and Bob, >> but, not thinking of that, this would be my straightforward approach: >> >> def computeBin(n): >> """conve

[Tutor] storing text in databases

2007-02-21 Thread Chae M
Is it a reasonable idea to store text (a few hundred words each record) in a database? I'm learning to use the SQLite database that comes with Python, and it seems like a good idea in terms of being able to search for notes, etc. My specific questions: - Can formatting of text be stored or is it

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Terry Carroll
Taking your points out of order On Thu, 22 Feb 2007, John Fouhy wrote: > Hmm, and it may not be faster either: That doesn't surprise me. It must have all the logic of the if-else construct, and additionally must build a list. > It's more compact, but less clear (IMO), so I'm not a big fan

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Jerry Hill
On 2/21/07, Dick Moores <[EMAIL PROTECTED]> wrote: > But there's syntax(?) there I've never seen before. "['','-'][n<0]". > I see it works: > > >>> n = -6 > >>> ['','-'][n<0] > '-' > >>> n = 98 > >>> ['','-'][n<0] > '' > > What's this called? I'd like to look it up. ['', '-'] is a list with tw

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Terry Carroll
On Wed, 21 Feb 2007, Dick Moores wrote: > But there's syntax(?) there I've never seen before. "['','-'][n<0]". > I see it works: > > >>> n = -6 > >>> ['','-'][n<0] > '-' > >>> n = 98 > >>> ['','-'][n<0] > '' > > What's this called? I'd like to look it up. It's nothing special; it's just a

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread John Fouhy
On 22/02/07, Dick Moores <[EMAIL PROTECTED]> wrote: > But there's syntax(?) there I've never seen before. "['','-'][n<0]". > I see it works: > > >>> n = -6 > >>> ['','-'][n<0] > '-' > >>> n = 98 > >>> ['','-'][n<0] > '' > > What's this called? I'd like to look it up. It's taking advantage of t

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Dick Moores
At 05:17 PM 2/21/2007, Terry Carroll wrote: >On Tue, 20 Feb 2007, Dick Moores wrote: > > > I was surprised to be unable to find a function in Python for > > converting ints from base10 to base2. Is there one? > > > > I wrote one, but have I reinvented the wheel again? (Even if I have, > > it was an

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Terry Carroll
On Tue, 20 Feb 2007, Dick Moores wrote: > I was surprised to be unable to find a function in Python for > converting ints from base10 to base2. Is there one? > > I wrote one, but have I reinvented the wheel again? (Even if I have, > it was an interesting exercise for me.) I like the approach o

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Bob Gailer
David Perlman wrote: > d'oh, I fell for the "reply-all" trick. :) > > Here's the code in question: > > Apparently there are built in functions hex() and oct() to generate > hexadecimal and octal digit strings of numbers, but there's no > corresponding bin(). Kind of a bizarre oversight, if yo

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread ALAN GAULD
>>> a & 128 will be 128 if the high bit in a is set, 0 if it is not. >>> >> >> Only for an 8 bit integer! Most integers nowadays are >> 32 bit so you need to use 0x8000 to get the msb! > Right, but if you read the thread it is about byte values read from a > file so presumably he is tal

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread Kent Johnson
Alan Gauld wrote: > "Kent Johnson" <[EMAIL PROTECTED]> wrote > >> This is also a good application of bitwise operations. >> >> a & 128 will be 128 if the high bit in a is set, 0 if it is not. >> > > Only for an 8 bit integer! Most integers nowadays are > 32 bit so you need to use 0x80

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread shawn bright
Hey thanks for all the help guys, i am at least pulling some values that make sense. i feel more like i am on my way. thanks again shawn On 2/21/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > "Kent Johnson" <[EMAIL PROTECTED]> wrote > > This is also a good application of bitwise operations. > > >

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote > This is also a good application of bitwise operations. > > a & 128 will be 128 if the high bit in a is set, 0 if it is not. Only for an 8 bit integer! Most integers nowadays are 32 bit so you need to use 0x8000 to get the msb! Alan G.

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread Alan Gauld
"Dick Moores" <[EMAIL PROTECTED]> wrote >I was surprised to be unable to find a function in Python for > converting ints from base10 to base2. Is there one? The sidebar in my Using the OS topicv includes a function to do this. It uses a simpler algorithm: def bin(n): digits = {'0':'000','1':'

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread Alan Gauld
"shawn bright" <[EMAIL PROTECTED]> wrote > but how do i read the individual bits of a byte > how do i know what the msb of a is ? See my reply to Johan in the thread Struct the solution... Basically you can use a bitwise and with a mask containing only the bit you are interested in. Johan wa

Re: [Tutor] It lives...

2007-02-21 Thread Alan Gauld
Hi Kirk, We remember you :-) "Kirk Z Bailey" <[EMAIL PROTECTED]> wrote > And this is good, because the new edition of python does not like my > windows wiki very well. Seems when it reads the page in, it creates > the > data in a list- with everything in one cell, not one line per cell. > Hmmm.

Re: [Tutor] Struct the solution for Hex translation

2007-02-21 Thread Alan Gauld
"Johan Geldenhuys" <[EMAIL PROTECTED]> wrote > On the struct module, How can I het the binary 1's > and 0's of the Hex value? Let say I want to get the > 8 bit value of '\xe2', can I use struct to convert > that into binary code No, struct converts your data into a string of bytes and provid

Re: [Tutor] stumped again adding bytes

2007-02-21 Thread Kent Johnson
shawn bright wrote: > oh, sorry, i meant how to get the 0x0A27 out of two bytes > a = 0x27 and b = 0x8A Why is the correct result not 0x8A27 ? Maybe this is what you want: >>> a=0x27 >>> b=0x8a >>> (b & 0x7f) * 256 + a 2599 Kent ___ Tutor maillist

Re: [Tutor] stumped again adding bytes

2007-02-21 Thread Luke Paireepinart
shawn bright wrote: > oh, sorry, i meant how to get the 0x0A27 out of two bytes > a = 0x27 and b = 0x8A I don't see what the number 0x0A27 has to do with bytes 0x27 and 0x8A, but I'll assume you meant 0x0A for b. > > actually, in my script, i am not using the hex values at all, i have > these beca

Re: [Tutor] stumped again adding bytes

2007-02-21 Thread Kent Johnson
shawn bright wrote: > Hey all, thanks for the help yesterday on finding out if an msb is set or not. > > i am now kinda stumped with discovering the value of two bytes together. > > i am making the integers with ord(a) and ord(b) > > how do i put them together ? If this is related to your earli

Re: [Tutor] stumped again adding bytes

2007-02-21 Thread shawn bright
oh, sorry, i meant how to get the 0x0A27 out of two bytes a = 0x27 and b = 0x8A actually, in my script, i am not using the hex values at all, i have these because they are examples in the documentation of a machine i am talking to. i am actually using ord(a) and ord(b) to get digital values of the

Re: [Tutor] stumped again adding bytes

2007-02-21 Thread Luke Paireepinart
shawn bright wrote: > Hey all, thanks for the help yesterday on finding out if an msb is set or not. > > i am now kinda stumped with discovering the value of two bytes together. > > ok, if i have two bytes that together make a number, how do i find that > number? > i know that i do not add them. >

[Tutor] stumped again adding bytes

2007-02-21 Thread shawn bright
Hey all, thanks for the help yesterday on finding out if an msb is set or not. i am now kinda stumped with discovering the value of two bytes together. ok, if i have two bytes that together make a number, how do i find that number? i know that i do not add them. like byte_a = 39 byte_b = 138 to

Re: [Tutor] Question about local scopes (namespaces)

2007-02-21 Thread Kent Johnson
Adam Pridgen wrote: > Sorry for the long email, and thanks in advance. > > In the below example, is the list foo supposed to retain the value > after the function, Bar(), returns? Yes > Is the list foo supposed to reinitialized on each call to Bar(), > meaning len(foo) == 0, and when Bar() retur

[Tutor] Question about local scopes (namespaces)

2007-02-21 Thread Adam Pridgen
Sorry for the long email, and thanks in advance. In the below example, is the list foo supposed to retain the value after the function, Bar(), returns? Is the list foo supposed to reinitialized on each call to Bar(), meaning len(foo) == 0, and when Bar() returns len(foo) (when Bar() is called w/

[Tutor] [Fwd: Re: list problem]

2007-02-21 Thread Luke Paireepinart
Please stop sending me private messages. And as I said before, include the file as an attachment. It does us no good to see the text. We need to see the line endings. And your (or my) e-mail program may mess with these. -Luke --- Begin Message --- Sure. Here's the PAGE source. i already posted the

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread shawn bright
even better, thanks much for this. shawn On 2/21/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > Luke Paireepinart wrote: > > shawn bright wrote: > >> lo there all, > >> > >> i am reading a binary file with open('myfile', 'rb') > >> > >> then i do a read(1) to read one byte. cool so far. > >> > >> b

Re: [Tutor] list problem

2007-02-21 Thread Kent Johnson
> Kirk Bailey wrote: >> ok, here comes some code: >> >> f1=open(pagename,'r') >> page=f1.readlines() >> f1.close() >> >> at the end of which, the data is in page, which is a list. But >> something strange is going on here. all the data is in a single cell! >> it's a one cell list! Say what? It s

Re: [Tutor] summer_v04.py

2007-02-21 Thread Kent Johnson
Christopher Spears wrote: > I've been working on a version of a script I found in > "Programming Python". The helpful users of this forum > gave me some advice to make the code less wordy. Here > is the code: > > #!/usr/bin/python > import string > > def find_longest_line(fileName): > lin

Re: [Tutor] summer_v04.py

2007-02-21 Thread Kent Johnson
Rikard Bosnjakovic wrote: > On 2/21/07, John Fouhy <[EMAIL PROTECTED]> wrote: > >> (because you are using a generator expression, and they were only >> introduced in 2.4) > > List comprehensions were implemented in v2.0. The OP probably does not > use an older version, but as far as I can see, hi

Re: [Tutor] how to read one bit of a byte

2007-02-21 Thread Kent Johnson
Luke Paireepinart wrote: > shawn bright wrote: >> lo there all, >> >> i am reading a binary file with open('myfile', 'rb') >> >> then i do a read(1) to read one byte. cool so far. >> >> but how do i read the individual bits of a byte >> >> i mean if i have a = read(1) >> >> how do i know what the m

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread David Perlman
d'oh, I fell for the "reply-all" trick. :) Here's the code in question: Apparently there are built in functions hex() and oct() to generate hexadecimal and octal digit strings of numbers, but there's no corresponding bin(). Kind of a bizarre oversight, if you ask me. Searching on the inter

Re: [Tutor] list problem

2007-02-21 Thread Michael Lange
On Wed, 21 Feb 2007 21:21:26 +1300 "John Fouhy" <[EMAIL PROTECTED]> wrote: > > Kirk Bailey wrote: > > > ok, here comes some code: > > > > > > f1=open(pagename,'r') > > > page=f1.readlines() > > > f1.close() > > > > > > at the end of which, the data is in page, which is a list. But > > > something

Re: [Tutor] list problem

2007-02-21 Thread John Fouhy
> Kirk Bailey wrote: > > ok, here comes some code: > > > > f1=open(pagename,'r') > > page=f1.readlines() > > f1.close() > > > > at the end of which, the data is in page, which is a list. But > > something strange is going on here. all the data is in a single cell! > > it's a one cell list! Say what