Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/python/slugid/slugid/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 1 kB image not shown  

Quelle  slugid.py   Sprache: Python

 
# Licensed under the Mozilla Public Licence 2.0.
https://www.mozilla.org/en-US/MPL/2.0

import sys
import uuid
import base64


def encode(uuid_):
    """
    Returns the given uuid.UUID object as a 22 character slug. This can be a
    regular v4 slug or a "nice" slug.
    """
    return _convert_bytes_to_slug(uuid_.bytes)


def _convert_bytes_to_slug(bytes_):
    slug = base64.urlsafe_b64encode(bytes_)[:-2]  # Drop '==' padding
    if sys.version_info.major != 2 and isinstance(slug, bytes):
        slug = slug.decode('utf-8')
    return slug


def decode(slug):
    """
    Returns the uuid.UUID object represented by the given v4 or "nice" slug
    """
    if sys.version_info.major != 2 and isinstance(slug, bytes):
        slug = slug.decode('ascii')
    slug = slug + '=='  # base64 padding
    return uuid.UUID(bytes=base64.urlsafe_b64decode(slug))


def v4():
    """
    Returns a randomly generated uuid v4 compliant slug
    """
    return _convert_bytes_to_slug(uuid.uuid4().bytes)


def nice():
    """
    Returns a randomly generated uuid v4 compliant slug which conforms to a set
    of "nice" properties, at the cost of some entropy. Currently this means one
    extra fixed bit (the first bit of the uuid is set to 0) which guarantees the
    slug will begin with [A-Za-f]. For example such slugs don't require special
    handling when used as command line parameters (whereas non-nice slugs may
    start with `-` which can confuse command line tools).

    Potentially other "nice" properties may be added in future to further
    restrict the range of potential uuids that may be generated.
    """
    rawBytes = bytearray(uuid.uuid4().bytes)
    rawBytes[0] = rawBytes[0] & 0x7f  # Ensure slug starts with [A-Za-f]
    return _convert_bytes_to_slug(rawBytes)

Messung V0.5
C=87 H=99 G=93

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.