Creating Document with random num as ID?
I need to create DTML Documents with a [pseudo]unique [pseudo]random id. I can create the objects okay using manage_clone, and I have a method that generates my "unique" "random" id, but I can't seem to create either: a) create the object and then rename it using the random number, or b) [more desirable] create the object with the random id. Ideas? Thanks.
I guess I'll have to answer it myself.<g> I realized I could make javascript generate a random number as part of the request. This doesn't solve the need for uniqueness but with a large enough range and anticipating a low volume of requests I should be able to devise a workaround. If anyone can tell me how the unique randon keys could/should be generated within Zope, that would be preferable, and I'd learn something.
I need to create DTML Documents with a [pseudo]unique [pseudo]random id. I can create the objects okay using manage_clone, and I have a method that generates my "unique" "random" id, but I can't seem to create either: a) create the object and then rename it using the random number, or b) [more desirable] create the object with the random id.
Ideas? Thanks.
On Sat, 17 Apr 1999, Craig Allen wrote:
I guess I'll have to answer it myself.<g>
I realized I could make javascript generate a random number as part of the request. This doesn't solve the need for uniqueness but with a large enough range and anticipating a low volume of requests I should be able to devise a workaround.
If anyone can tell me how the unique randon keys could/should be generated within Zope, that would be preferable, and I'd learn something.
If the randomness of the keys is not a requirement (ie you just want to generate unique ids, then add a property of type integer in the containing folder and increment it everytime you need a new id. Assuming for instance, thet the integer property is called unique_id, then the following dtml will acvieve what you want: <!--# call "manage_changeProperties(unique_id=unique_id+1)"--> <!--# call "manage_addFolder("Folder"+_.str(unique_id), "Folder"+_.str(unique_id),0,0,REQUEST)"--> The above will add a new folder with id and title(Folder1, Folder2, etc) If you do want to use a random id then you can use an external method: import whrandom def random_id(self): '''random_id(self) returns a random id of type Folder<random number>""" id='Folder'+str(whrandom.randint(1,1000)) while id in self.objectIds(): id='Folder'+str(whrandom.randint(1,1000) return id Pavlos
Craig Related to your question, the following external method is very handy: import whrandom def selection_nr(self,elems,no=15): '''selection_nr(self,no=15) returns a random selection of n elements from list elems without replacement''' if len(elems)<=15: return elems random_list=[] for i in range(no): elem=whrandom.choice(elems) random_list.append(elem) elems.remove(elem) return random_list given a list of objects it will return a random list of length no. So if you have a folder of images or quotes or whatever and you need to return a random selection you can use the following DTML: <!--# in "selection_nr(objectValues(['Folder'],4))" <!--# var title --> <!--#/in--> will select randomly 4 subfolders and display their title. Pavlos
Ooops ... I sent a buggy method. Should be: import whrandom def selection_nr(self,elems,num=15): '''selection_nr(self,elems,num=15) returns a random selection of num elements from list elems without replacement''' if len(elems)<=num: return elems random_list=[] for i in range(num): elem=whrandom.choice(elems) random_list.append(elem) elems.remove(elem) return random_list My apologies Pavlos
participants (2)
-
Craig Allen -
Pavlos Christoforou