How to create a new unique name for the context?

What is the best way to create a name for some temporary context that is guaranteed to be unique (a context with this name should not exist in the system)?

+6
source share
5 answers

I can suggest a function that I used here :

Clear[unique]; unique[sym_] := ToExpression[ ToString[Unique[sym]] <> StringReplace[StringJoin[ToString /@ Date[]], "." :> ""]]; 

You can replace ToExpression with StringJoin[...,"`"] to adapt it to your needs.

+7
source

The following expression will generate a context name that is guaranteed not to conflict with any loaded context:

 First@Contexts [] //. c_ /; MemberQ[Contexts[], c] :> "Context"~~ToString[RandomInteger[1000000]]~~"`" 

It does not attempt to account for contexts that are not yet loaded. As written, this expression can be used up to 1,000,000 times before the names expire. Adjust the fixed line ("Context") and the number of names (1,000,000) to suit your taste.

Update

As @Leonid notes in a comment, empty contexts will not be listed in Contexts[] . Therefore, strictly speaking, it is possible that this expression can return the name of an existing empty context.

UUID

For all practical purposes, creating a name from a number randomly selected from a sufficiently large range will work, for example.

 "Context"~~ToString[RandomInteger[2^128]]~~"`" 

In a similar vein, you can use the UUID . UUIDs are commonly used as identifiers, which phenomenally can be unique in all network nodes:

 Needs["JLink`"] LoadJavaClass["java.util.UUID"] "Context"~~ StringReplace[ JavaBlock@java `util`UUID`randomUUID[]@toString[], "-" -> ""]~~ "`" 
+9
source

Another option would be to look at all the initial contexts (before the first reverse lookup), find their string length, and then generate a string (possibly random, but this is optional), which is at least one character longer than the rest, This is guaranteed to be unique, and theoretical the possibility of a collision does not even exist, as in some other solutions.

 sl = (StringSplit[#, "`"][[1]] & /@ Contexts[] // StringLength // Max ) Out[349]= 30 In[353]:= "c" ~~ ToString[10^sl] ~~ "`" Out[353]= "c1000000000000000000000000000000`" 

The disadvantage of this method would be that the context names become longer after each repeated use of this method. ;-) If this is a problem, we could create a unique name based on the set of the longest context names using the Cantor diagonal procedure.

+2
source

Is Unique perhaps what you are looking for?

0
source

This is really an example illustrating Alexey’s answer to Sjrdd’s answer / question. From the new kernel on my machine, the following code

 Begin["myContext3`"]; Unique["myContext"] 

The "myContext3" is inferior. So, obviously, Unique (the first thing I thought) is not working.

By the way, I would just add a comment to Sjoerd's answer, but I don’t know how to include the accent character used to indicate the inline context. Does anyone know how to do this?

0
source

Source: https://habr.com/ru/post/893781/


All Articles