read/ edit line from stdin
Hello, for my project confux (http://confux.sourceforge.net) I want to read a line from stdin. But I don't want the user to type a new line. I want so display a value which the user can edit. For example I want to ask the user for a hostname and I print "localhost", the user modified it to "localserver" and I read it after . What is the fastest way to realize that? I do NOT want to use curses (yet) ;-). mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: read/ edit line from stdin
It's a bit more complicated. The field has a default and a preassigned Value. The default is p.e. 'localhost' and the value is 'www.gentoo.org'. Then localhost is chosen if the value is erased to ''. But i want to make it easy to keep the preassigned value! So if nothing is typed 'www.gentoo.org' should be used. I just thought that extending the commandline input interface would be quite useful. I want a text field that can be edited like common GUI-TextFields on command line... mfg - eth James Stroud wrote: > Clemens Hepper wrote: >> Hello, >> for my project confux (http://confux.sourceforge.net) I want to read >> a line from stdin. >> But I don't want the user to type a new line. I want so display a >> value which the user can edit. >> >> For example I want to ask the user for a hostname and I print >> "localhost", the user modified it to "localserver" and I read it >> after . >> >> What is the fastest way to realize that? I do NOT want to use curses >> (yet) ;-). >> >> mfg >> - eth > > Why don't you just make "localhost" the default and have the user enter > the complete name if they want it different. Seems like this is the way > every CL program I've ever seen works. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Bitwise OR?
To look at the bit-structure i've implemented a little function: def bitstring(number, digits=32): """lsb-->msb""" result = "" for a in xrange(digits): if number & 1: result += '1' else: result += '0' number >>= 1 return result I wonder if there is something like sizeof() for int numbers. mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: Bitwise OR?
Hello, I've written a little (optimized) method to get a bit-string: def bitstringneg(number, digits=32): """optimized for negative numbers""" result = "" for a in xrange(digits): if number & 1: result += '1' else: result += '0' number >>= 1 return result def bitstringpos(number): """optimized for positive numbers""" result = "" while number: if number & 1: result += '1' else: result += '0' number >>= 1 return result def bitstring(number, digits=32): """lsb-->msb""" result = "" if number < 0: return bitstringneg(number, digits) else: return bitstringpos(number) BTW: Is there something like a sizeof() method for int numbers? mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: Bitwise OR?
Adam DePrince wrote: >> BTW: Is there something like a sizeof() method for int numbers? > > import struct > help( strict.calcsize ) Mh, that doesn't do what i want. I'd like to have something like: def size(number): return sizeof(number) > Why one bit at a time? Good question... Here my new approach based on your idea: _digits = { 0:"", 1:"1000", 2:"0100", 3:"1100", 4:"0010", 5:"1010", 6:"0110", 7:"1110", 8:"0001", 9:"1001", 0xa:"0101", 0xb:"1101", 0xc:"0011", 0xd:"1011", 0xe:"0111", 0xf:""} def bitstring2(number): """lsb-->msb""" rlist = list() if number >= 0: while number: rlist.append( _digits[number & 0xF] ) number >>= 4 else: while number != -1: rlist.append( _digits[number & 0xF] ) number >>= 4 return ''.join(rlist) This was faster for positive numbers. For negative numbers yours was faster, but my version handles those numbers different. Your version fails for Large numbers since hex( long ) returns something like "0xFFFL" instead of "0xfff". > Cheers - Adam Cheers :) - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: Bitwise OR?
Okay... pythons build-in methods are quite fast. so is hex(). with about 64 kb memory i can write it with a 16 bit dictionary where the dictionary generation itself is not yet optimized: def genBitList(exp): next = lambda now: [x+'0' for x in now]+[x+'1' for x in now] result = [""] for x in range(exp): result = next(result) return result _digits = genBitList(16) def bitstring2(number): """lsb-->msb""" rlist = list() if number >= 0: while number: rlist.append( _digits[number & 0x] ) number >>= 16 return ''.join(rlist).rstrip('0') else: while number != -1: rlist.append( _digits[number & 0x] ) number >>= 16 return ''.join(rlist).rstrip('1') this is quite fast and in lots of cases faster than the hex()-version. however your method (with my inverses formatting) is very fast, too and without so much memory expense: _nibbles = {"0":"", "1":"0001", "2":"0010", "3":"0011", "4":"0100", "5":"0101", "6":"0110", "7":"0111", "8":"1000", "9":"1001", "a":"1010", "b":"1011", "c":"1100", "d":"1101", "e":"1110", "f":"", "l":"", "-":"", "x":""} from string import maketrans, translate _invert = maketrans('01', '10') def bitstring3( number ): if number > 0: return ''.join( [_nibbles[d] for d in hex( number ).lower()] ).lstrip( '0' ) else: return translate(''.join( [_nibbles[d] for d in hex( ~number ).lower()] ).lstrip( '0' ), _invert) Benchmark: for 0xa: bitstring2: 0.61802315712 bitstring3: 0.91001200676 for 0x: bitstring2: 0.561501026154 bitstring3: 1.11787199974 for 0x: bitstring2: 1.2295820713 bitstring3: 1.61559510231 for 0x: bitstring2: 1.90036797523 bitstring3: 2.2683339119 for -0x: bitstring2: 2.81339716911 bitstring3: 2.74266886711 mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: a problem to solve
Hi, [EMAIL PROTECTED] wrote: > That's one way to do it. I did it that way because I have the > hex patterns memorized. You should be able to generate your numbers like this: number = int('001001000100100', 2) mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between 'is' and '=='
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > Joel Hedlund <[EMAIL PROTECTED]> wrote: > >> Which means that "is" comparisons in general will be faster than == >> comparisons. > > I thought that == automatically compared identify before trying to compare > the values. Or am I thinking of some special case, like strings? Even for strings there is a performance difference: >>> timeit.Timer("'a'=='a'").timeit() 0.26859784126281738 >>> timeit.Timer("'a' is 'a'").timeit() 0.21730494499206543 mfg - eth -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between 'is' and '=='
Dan Sommers wrote: > This does *not* also mean constants and such: > > Python 2.4.2 (#1, Feb 22 2006, 08:02:53) > [GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> a = 123456789 > >>> a == 123456789 > True > >>> a is 123456789 > False It's strange: python seem to cache constants from 0 to 99: for x in xrange(1000): if not eval("%d"%x) is eval("%d"%x): print x for me it printed 100-999. - eth -- http://mail.python.org/mailman/listinfo/python-list