Yes it needs to be short. This will be an invitation string most likely 
written on a chalk board for kids.

Also there was an error in my initial post : I forgot to increment size += 
1 inside the while loop. Noticed right after posting.

thanks

On Monday, September 10, 2012 1:27:37 AM UTC+2, george wrote:
>
> Do you need this identifier to be so short? If that's not a problem, why 
> don't you use something tried and that's proven that will succeed: GUIDS 
> (or UUIDs).
>
> http://en.wikipedia.org/wiki/Globally_unique_identifier
>
> http://docs.python.org/library/uuid.html
>
> On Sun, Sep 9, 2012 at 8:10 PM, Anton Popovine 
> <anton.p...@gmail.com<javascript:>
> > wrote:
>
>> Hi everyone! First time posting to this group!
>>
>> I have a model Class where each row has a unique alphanumeric ID string 
>> like 'EX39H'.
>>
>> This is the function I use to generate the code.
>>
>> def gen_class_code():
>>   import string
>>   import random
>>   size = 5
>>   max_size = 16
>>    attempts = 1000
>>   chars = string.ascii_uppercase + string.digits 
>>
>>   # lets try <attempts> times to generate unique id, else increase code 
>> length
>>   res = ""
>>   while (size <= max_size):
>>     for i in range(attempts):
>>       temp = ''.join(random.choice(chars) for i in range(size)) 
>>       if not Class.objects.filter(code=temp).exists():
>>         res = temp
>>         break
>>     if res : break
>>
>>   return res 
>>
>> How much would you set the attempts variable to and why?
>>
>> Thanks in advance
>> Anton
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msg/django-users/-/-JYyShU_ciUJ.
>> To post to this group, send email to django...@googlegroups.com<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> django-users...@googlegroups.com <javascript:>.
>> For more options, visit this group at 
>> http://groups.google.com/group/django-users?hl=en.
>>
>
>
>
> -- 
> George R. C. Silva
>
> Desenvolvimento em GIS
> http://geoprocessamento.net
> http://blog.geoprocessamento.net
>  
>  

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/sXE0uvWcK2IJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to