Skip to main content

This is a tool that automatically generates short IDs

Project description

Slim-ID: A Lightweight ID Generation Library for Python

下の方に日本語の説明があります

Overview

  • This is a tool that automatically generates short IDs.
  • It has a mechanism to avoid collisions with already generated IDs.

Features

  • Specify the desired length for generated IDs (default is 5 characters)
  • Automatically increases the length if the generated ID conflicts with existing IDs
  • Specify the alphabet to use for the ID (default is URL-safe Base64, but hexadecimal can also be specified)
  • Uses cryptographically secure random number generation internally, providing a degree of protection against attackers predicting the random number from timestamps, etc.
  • Requires a user-defined function to determine if a generated ID conflicts with existing IDs. This function is expected to handle dictionary lookups or database queries, for example.

Usage Example

import sys
import slim_id

id_dic = {}

def exists(arg_id):
    return (arg_id in id_dic)

# Generate an ID using Slim-ID
s_id = slim_id.gen(exists)
id_dic[s_id] = True
print(s_id)

# Generate an ID using Slim-ID with custom parameters
s_id = slim_id.gen(
    exists,  # Function to determine if an ID exists in the database
    length = 7,  # Base length (automatically increases if a collision occurs)
    ab = "16",  # Alphabet type (base64url... URL-safe Base64, 16... hexadecimal)
)
id_dic[s_id] = True
print(s_id)

Custom Alphabet for Generating Slim-ID

Users can now specify a custom character set for generating IDs, allowing for tailored ID generation to meet specific needs. When using a custom alphabet, provide a list of characters to be used.

import sys
import slim_id

id_dic = {}

def exists(arg_id):
    return (arg_id in id_dic)

# Generate a basic Slim-ID
s_id = slim_id.gen(exists)
id_dic[s_id] = True
print(s_id)

# Generate a Slim-ID using a custom alphabet
s_id = slim_id.gen(
    exists,  # Function to check if the ID already exists in the database
    length = 7,  # Base length (will increase automatically if a collision occurs)
    ab = list("0123456789"),  # Custom alphabet
)
id_dic[s_id] = True
print(s_id)

With this feature, IDs are generated using the specified custom alphabet set. For example, in the code above, the generated IDs will consist only of numbers.

Here is the English translation of your document:

Usage from the Command Line

  • You can use the following command in the console:

  • If no argument is provided, the default length will be 30.

slim_id
  • You can specify the length as an argument. (In the example below, the result will be 10 characters long)
slim_id 10

概要

  • 短いIDを自動生成するツールです
  • 生成済みIDとの衝突を回避する仕組みを持っています

特徴

  • 生成されるIDの長さを指定可能(デフォルトは5文字)
  • 生成されたIDが既存のIDと衝突する場合、長さが自動的に増える
  • IDに使用するアルファベットを指定可能(デフォルトはURLセーフなBase64だが、16進数も指定できる)
  • 内部で暗号学的に安全な乱数生成を使用しており、タイムスタンプなどから乱数を推定する攻撃者に対してある程度の保護を提供
  • 生成されたIDが既存のIDと衝突しているかどうかを判断する関数をユーザーが定義する必要がある。この関数は、辞書の引当やDBの引き当てなどを処理することが想定されている。
import sys
import slim_id

id_dic = {}

def exists(arg_id):
    return (arg_id in id_dic)

# Slim-IDを使用してIDを生成
s_id = slim_id.gen(exists)
id_dic[s_id] = True
print(s_id)

# カスタムパラメータを使用してSlim-IDでIDを生成
s_id = slim_id.gen(
    exists,  # IDがデータベースに存在するかどうかを判断する関数
    length = 7,  # 基本長(衝突が発生した場合、自動的に長くなります)
    ab = "16",  # アルファベットの種類(base64url... URLセーフな64進数、16... 16進数)
)
id_dic[s_id] = True
print(s_id)

カスタムアルファベット指定

ユーザーが任意の文字セットを指定してIDを生成できるようになりました。これにより、特定のニーズに応じたID生成が可能になります。 (カスタム指定の場合は、アルファベットの文字のリストを指定します。)

import sys
import slim_id

id_dic = {}

def exists(arg_id):
    return (arg_id in id_dic)

# 基本的なSlim-IDの生成
s_id = slim_id.gen(exists)
id_dic[s_id] = True
print(s_id)

# カスタムアルファベットを使用してSlim-IDでIDを生成
s_id = slim_id.gen(
    exists,  # IDがデータベースに存在するかどうかを判断する関数
    length = 7,  # 基本長(衝突が発生した場合、自動的に長くなります)
    ab = list("0123456789"),  # カスタムアルファベットの種類
)
id_dic[s_id] = True
print(s_id)

これにより、指定されたカスタムアルファベットのセットからIDが生成されます。例えば、上記の例では数字のみを使用してIDが生成されます。

コマンドラインからの利用

  • コンソールで下記コマンドで利用できます

  • 引数を省略した場合、長さは30になります。

slim_id
  • 引数で長さを指定できます。 (下記の場合は10桁になる)
slim_id 10

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slim-id-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slim_id-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file slim-id-0.1.0.tar.gz.

File metadata

  • Download URL: slim-id-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for slim-id-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f25f8f46673f8a8e5b4f75d63ec775afa9b373c963ba8cfe8086dad14f0e97d
MD5 104a869efad81c026eb0966b017c4bbd
BLAKE2b-256 04d51480761f1328332dcc6aeee63978e0580e5c361f4bc10ebcb577f6197aa5

See more details on using hashes here.

File details

Details for the file slim_id-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slim_id-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for slim_id-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d208463cf4af56e028d05c5776de7ce69e0e029ed4762eb0d92f7ea04aa0734
MD5 ce1f7843da3b2a1fa54115e2adf481b8
BLAKE2b-256 f297863825a247024804ffeb517d7c6431e898443071123b2ae1571a79993f27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page