Tim Hochberg <[EMAIL PROTECTED]> writes:
> > is fast, but if a is aliased, Python can't do the optimization, so
> Is this really true? After the first time through the loop, 'a' won't
> be aliased any more since strings are immutable.
Hmm, ok, there was some previous discussion of this problem a
Paul Rubin wrote:
> "Tuvas" <[EMAIL PROTECTED]> writes:
>
>>I've actually done the tests on this one, it's actually faster to use
>>the += than a list, odd as it may sound.
>
>
> Frederik explained the reason; there's an optimization in Python 2.4
> that I'd forgotten about, for that specific c
"Tuvas" <[EMAIL PROTECTED]> writes:
> I've actually done the tests on this one, it's actually faster to use
> the += than a list, odd as it may sound.
Frederik explained the reason; there's an optimization in Python 2.4
that I'd forgotten about, for that specific case. It's not in earlier
versio
On Tue, 07 Mar 2006 22:32:04 +0100, Fredrik Lundh wrote:
>> Python lists have a special efficiency hack so that ret.append doesn't
>> copy the whole list around, but rather, allocates space in bigger
>> chunks so that appending usually takes constant time.
>
> in 2.4 and later, += on strings does
I've actually done the tests on this one, it's actually faster to use
the += than a list, odd as it may sound. I ran into this one a while
back. The best way to do it is to build an array from scratch, fill the
array, and then join it, but I didn't have time to do it that way...
--
http://mail.py
Paul Rubin wrote:
> The usual Python idiom for building up a string in approx linear time
> is:
>
>def cstring(n):
> ret = []
> while (something):
> ret.append(generate_another_character())
> return ''.join(ret)
>
> Python lists have a special efficiency hack so that
"Tuvas" <[EMAIL PROTECTED]> writes:
> from os import urandom
> def cstring(bytes):
> ret=''
> while(len(ret) c=os.urandom(1)
> if c>'0' and c<'z':
> ret=ret+c
> return ret
>
> That should do it, though I bet there might be a more efficient way.
One efficie
I will admit though, I have the same question as Paul, why do you want
a random string of numbers, letters, and symbols? But, you asked for
it, so, that'll do.
--
http://mail.python.org/mailman/listinfo/python-list
Tuvas wrote:
> from os import urandom
> def cstring(bytes):
> ret=''
> while(len(ret) c=os.urandom(1)
> if c>'0' and c<'z':
> ret=ret+c
> return ret
>
> That should do it, though I bet there might be a more efficient way. I
> don't know if that's the set of
Gervasio Bernal <[EMAIL PROTECTED]> writes:
> How can I generate a random string containing digits, symbols and
> letters? I will use this random string for the key of a cryptographic
> algorithm.
Generally if this is a string that some human user has to type in,
it's preferable to select some wor
from os import urandom
def cstring(bytes):
ret=''
while(len(ret)'0' and c<'z':
ret=ret+c
return ret
That should do it, though I bet there might be a more efficient way. I
don't know if that's the set of characters you want to use, but... If
you want a better answer, you'd h
Bryan Olson wrote:
> Tuvas wrote:
>
>>Ahh, you are correct, that is a large bug... How about this one?
>
>
> Much better. Do note the comments from Emile van Sebille and Paul
> Rubin. There are some minor style and efficiency points, but it
> looks reasonable.
>
> Incidentally, as of Python 2.4
Thanks for the function Paul, it works alot nicer than the one I had in
my program... Now, with all of this knowledge, I'm going to be brave
and try out everything with AES. It seems to be working alright, I'll
debug this more on my own than I did with my RSA code, which turned out
to be full of bu
Tuvas wrote:
[...]
> As to the s2num(text), well, that looks really neat. Is there an easy
> way to do the reverse of that? Thanks!
Easy? Well, you be the judge:
def num2string(n):
""" Pass a non-negative int or long n.
Returns a string with bytes holding the big-endian base
Wow, that would have been nice to know... Oh well, I've already got the
function, might as well use it... I'm starting to learn alot more of
the standard libraries that exist for alot of the little functions. It
seems like every project I have I build a misc.py file that contains
several small, but
Tuvas wrote:
> Ahh, you are correct, that is a large bug... How about this one?
Much better. Do note the comments from Emile van Sebille and Paul
Rubin. There are some minor style and efficiency points, but it
looks reasonable.
Incidentally, as of Python 2.4, the standard library offers
random.Sy
"Tuvas" <[EMAIL PROTECTED]> writes:
> Wait, I now see that there is a native base 2 log in python, so I will
> just do that rather than my adhoc way. The reason for adding one is to
> make sure there isn't any problems if the log is, for instance, 2.2. It
> will always round up. It's better to have
Wait, I now see that there is a native base 2 log in python, so I will
just do that rather than my adhoc way. The reason for adding one is to
make sure there isn't any problems if the log is, for instance, 2.2. It
will always round up. It's better to have to try twice to make sure the
number can ha
Paul Rubin wrote:
> My favorite way to convert strings to numbers is with binascii:
>
> from binascii import hexlify
> def s2num(text):
>return int(hexlify(text), 16)
Neat. I use the empty string as a binary representation of zero,
which you can accommodate with:
def s2num(text):
ret
"Tuvas" <[EMAIL PROTECTED]> writes:
> def s2num(text):
> if(len(text)==1):
> return ord(text)
> else:
> return ord(text[0])+256*s2num(text[1:])
My favorite way to convert strings to numbers is with binascii:
from binascii import hexlify
def s2num(text):
return int(hexl
Ahh, you are correct, that is a large bug... How about this one?
def s2num(text):
if(len(text)==1):
return ord(text)
else:
return ord(text[0])+256*s2num(text[1:])
def cran_rand(min,max):
range=int(log(abs(max-min))/log(2))+1
num=max+1
if range%8==0:
cra
Tuvas wrote:
> Okay, I'm working on devoloping a simple, cryptographically secure
> number, from a range of numbers (As one might do for finding large
> numbers, to test if they are prime). My function looks like this:
>
> def cran_rand(min,max):
> if(min>max):
> x=max
> max=mi
Good idea about the max and min values. Yes, urandom is os.urandom.
s2num('blah') will convert the phrase blah to ascii, and treat them as
if they were a big function.
Anyone else whose still interested, I found another small bug, but it
was in the modular (Again). It won't do much, but...
I did
> def cran_rand(min,max):
You're shadowing built-ins here. Not a problem, but something I'd generally
avoid.
>if(min>max):
>x=max
>max=min
>min=x
If the args were a,b you could say:
maxarg,minarg = min(a,b),max(a,b)
>range=round(log(max-min)/log(256))
more
Okay, I'm working on devoloping a simple, cryptographically secure
number, from a range of numbers (As one might do for finding large
numbers, to test if they are prime). My function looks like this:
def cran_rand(min,max):
if(min>max):
x=max
max=min
min=x
range=rou
25 matches
Mail list logo