Python and NanoID - using NanoID with python
What is NanoID ?
NanoID is a tiny, secure URL-friendly unique string ID generator. It uses cryptographically strong random APIs that guarantees proper distribution of symbols. It is also compact in size. It uses a larger alphabet than the standard UUID (A-Za-z0-9_-), and has a similar number of unique IDs in just 21 symbols instead of 36.
NanoID installation
pip install nanoid
from nanoid import generate
generateId = generate()
print(f"Generated ID: {generateId}")
To check the safety of your ID length in ID collision probability calculator
If you want to reduce ID length (which will increase collisions probability), we can pass the length as an argument.
from nanoid import generate
generateId = generate(size=10)
print(f"Generated ID: {generateId}")
Custom Alphabet or Length
If you want to change the ID's alphabet or length we can use the internal generate module by passing a character list and size.
from nanoid import generate
charactersToUse= '0123456789abcdef'
outputSize = 12
generateId = generate(charactersToUse, outputSize)
print(f"Generated ID: {generateId}")
Note
Symbols -,.() are not encoded in the URL. If used at the end of a link they could be identified as a punctuation symbol.
Other Supported Programming Languages
- C#
- Clojure and ClojureScript
- Crystal
- Dart
- Go
- Elixir
- Haskell
- Java
- JavaScript
- Nim
- PHP
- Ruby
- Rust
- Swift and more ...
更多推荐

所有评论(0)