Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
uuid.UUID Class Reference
Inheritance diagram for uuid.UUID:
Inheritance graph
[legend]
Collaboration diagram for uuid.UUID:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __eq__
def __ne__
def __lt__
def __gt__
def __le__
def __ge__
def __hash__
def __int__
def __repr__
def __setattr__
def __str__
def bytes
def bytes_le
def fields
def time_low
def time_mid
def time_hi_version
def clock_seq_hi_variant
def clock_seq_low
def time
def clock_seq
def node
def hex
def urn
def variant
def version

Public Attributes

 int
 variant
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Detailed Description

Instances of the UUID class represent UUIDs as specified in RFC 4122.
UUID objects are immutable, hashable, and usable as dictionary keys.
Converting a UUID to a string with str() yields something in the form
'12345678-1234-1234-1234-123456789abc'.  The UUID constructor accepts
five possible forms: a similar string of hexadecimal digits, or a tuple
of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
48-bit values respectively) as an argument named 'fields', or a string
of 16 bytes (with all the integer fields in big-endian order) as an
argument named 'bytes', or a string of 16 bytes (with the first three
fields in little-endian order) as an argument named 'bytes_le', or a
single 128-bit integer as an argument named 'int'.

UUIDs have these read-only attributes:

    bytes       the UUID as a 16-byte string (containing the six
                integer fields in big-endian byte order)

    bytes_le    the UUID as a 16-byte string (with time_low, time_mid,
                and time_hi_version in little-endian byte order)

    fields      a tuple of the six integer fields of the UUID,
                which are also available as six individual attributes
                and two derived attributes:

        time_low                the first 32 bits of the UUID
        time_mid                the next 16 bits of the UUID
        time_hi_version         the next 16 bits of the UUID
        clock_seq_hi_variant    the next 8 bits of the UUID
        clock_seq_low           the next 8 bits of the UUID
        node                    the last 48 bits of the UUID

        time                    the 60-bit timestamp
        clock_seq               the 14-bit sequence number

    hex         the UUID as a 32-character hexadecimal string

    int         the UUID as a 128-bit integer

    urn         the UUID as a URN as specified in RFC 4122

    variant     the UUID variant (one of the constants RESERVED_NCS,
                RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)

    version     the UUID version number (1 through 5, meaningful only
                when the variant is RFC_4122)

Definition at line 56 of file uuid.py.


Constructor & Destructor Documentation

def uuid.UUID.__init__ (   self,
  hex = None,
  bytes = None,
  bytes_le = None,
  fields = None,
  int = None,
  version = None 
)

Definition at line 105 of file uuid.py.

00105 
00106                        int=None, version=None):
00107         r"""Create a UUID from either a string of 32 hexadecimal digits,
00108         a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
00109         in little-endian order as the 'bytes_le' argument, a tuple of six
00110         integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
00111         8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
00112         the 'fields' argument, or a single 128-bit integer as the 'int'
00113         argument.  When a string of hex digits is given, curly braces,
00114         hyphens, and a URN prefix are all optional.  For example, these
00115         expressions all yield the same UUID:
00116 
00117         UUID('{12345678-1234-5678-1234-567812345678}')
00118         UUID('12345678123456781234567812345678')
00119         UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
00120         UUID(bytes='\x12\x34\x56\x78'*4)
00121         UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
00122                       '\x12\x34\x56\x78\x12\x34\x56\x78')
00123         UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
00124         UUID(int=0x12345678123456781234567812345678)
00125 
00126         Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
00127         be given.  The 'version' argument is optional; if given, the resulting
00128         UUID will have its variant and version set according to RFC 4122,
00129         overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
00130         """
00131 
00132         if [hex, bytes, bytes_le, fields, int].count(None) != 4:
00133             raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
00134         if hex is not None:
00135             hex = hex.replace('urn:', '').replace('uuid:', '')
00136             hex = hex.strip('{}').replace('-', '')
00137             if len(hex) != 32:
00138                 raise ValueError('badly formed hexadecimal UUID string')
00139             int = int_(hex, 16)
00140         if bytes_le is not None:
00141             if len(bytes_le) != 16:
00142                 raise ValueError('bytes_le is not a 16-char string')
00143             bytes = (bytes_(reversed(bytes_le[0:4])) +
00144                      bytes_(reversed(bytes_le[4:6])) +
00145                      bytes_(reversed(bytes_le[6:8])) +
00146                      bytes_le[8:])
00147         if bytes is not None:
00148             if len(bytes) != 16:
00149                 raise ValueError('bytes is not a 16-char string')
00150             assert isinstance(bytes, bytes_), repr(bytes)
00151             int = int_(('%02x'*16) % tuple(bytes), 16)
00152         if fields is not None:
00153             if len(fields) != 6:
00154                 raise ValueError('fields is not a 6-tuple')
00155             (time_low, time_mid, time_hi_version,
00156              clock_seq_hi_variant, clock_seq_low, node) = fields
00157             if not 0 <= time_low < 1<<32:
00158                 raise ValueError('field 1 out of range (need a 32-bit value)')
00159             if not 0 <= time_mid < 1<<16:
00160                 raise ValueError('field 2 out of range (need a 16-bit value)')
00161             if not 0 <= time_hi_version < 1<<16:
00162                 raise ValueError('field 3 out of range (need a 16-bit value)')
00163             if not 0 <= clock_seq_hi_variant < 1<<8:
00164                 raise ValueError('field 4 out of range (need an 8-bit value)')
00165             if not 0 <= clock_seq_low < 1<<8:
00166                 raise ValueError('field 5 out of range (need an 8-bit value)')
00167             if not 0 <= node < 1<<48:
00168                 raise ValueError('field 6 out of range (need a 48-bit value)')
00169             clock_seq = (clock_seq_hi_variant << 8) | clock_seq_low
00170             int = ((time_low << 96) | (time_mid << 80) |
00171                    (time_hi_version << 64) | (clock_seq << 48) | node)
00172         if int is not None:
00173             if not 0 <= int < 1<<128:
00174                 raise ValueError('int is out of range (need a 128-bit value)')
00175         if version is not None:
00176             if not 1 <= version <= 5:
00177                 raise ValueError('illegal version number')
00178             # Set the variant to RFC 4122.
00179             int &= ~(0xc000 << 48)
00180             int |= 0x8000 << 48
00181             # Set the version number.
00182             int &= ~(0xf000 << 64)
00183             int |= version << 76
00184         self.__dict__['int'] = int

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def uuid.UUID.__eq__ (   self,
  other 
)

Definition at line 185 of file uuid.py.

00185 
00186     def __eq__(self, other):
00187         if isinstance(other, UUID):
00188             return self.int == other.int
00189         return NotImplemented

def uuid.UUID.__ge__ (   self,
  other 
)

Definition at line 213 of file uuid.py.

00213 
00214     def __ge__(self, other):
00215         if isinstance(other, UUID):
00216             return self.int >= other.int
00217         return NotImplemented

def uuid.UUID.__gt__ (   self,
  other 
)

Definition at line 203 of file uuid.py.

00203 
00204     def __gt__(self, other):
00205         if isinstance(other, UUID):
00206             return self.int > other.int
00207         return NotImplemented

def uuid.UUID.__hash__ (   self)

Definition at line 218 of file uuid.py.

00218 
00219     def __hash__(self):
00220         return hash(self.int)

def uuid.UUID.__int__ (   self)

Definition at line 221 of file uuid.py.

00221 
00222     def __int__(self):
00223         return self.int

def uuid.UUID.__le__ (   self,
  other 
)

Definition at line 208 of file uuid.py.

00208 
00209     def __le__(self, other):
00210         if isinstance(other, UUID):
00211             return self.int <= other.int
00212         return NotImplemented

def uuid.UUID.__lt__ (   self,
  other 
)

Definition at line 198 of file uuid.py.

00198 
00199     def __lt__(self, other):
00200         if isinstance(other, UUID):
00201             return self.int < other.int
00202         return NotImplemented

def uuid.UUID.__ne__ (   self,
  other 
)

Definition at line 190 of file uuid.py.

00190 
00191     def __ne__(self, other):
00192         if isinstance(other, UUID):
00193             return self.int != other.int
00194         return NotImplemented

def uuid.UUID.__repr__ (   self)

Definition at line 224 of file uuid.py.

00224 
00225     def __repr__(self):
00226         return 'UUID(%r)' % str(self)

def uuid.UUID.__setattr__ (   self,
  name,
  value 
)

Definition at line 227 of file uuid.py.

00227 
00228     def __setattr__(self, name, value):
00229         raise TypeError('UUID objects are immutable')

def uuid.UUID.__str__ (   self)

Definition at line 230 of file uuid.py.

00230 
00231     def __str__(self):
00232         hex = '%032x' % self.int
00233         return '%s-%s-%s-%s-%s' % (
00234             hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])

def uuid.UUID.bytes (   self)

Definition at line 236 of file uuid.py.

00236 
00237     def bytes(self):
00238         bytes = bytearray()
00239         for shift in range(0, 128, 8):
00240             bytes.insert(0, (self.int >> shift) & 0xff)
00241         return bytes_(bytes)

Here is the caller graph for this function:

def uuid.UUID.bytes_le (   self)

Definition at line 243 of file uuid.py.

00243 
00244     def bytes_le(self):
00245         bytes = self.bytes
00246         return (bytes_(reversed(bytes[0:4])) +
00247                 bytes_(reversed(bytes[4:6])) +
00248                 bytes_(reversed(bytes[6:8])) +
00249                 bytes[8:])

Here is the call graph for this function:

def uuid.UUID.clock_seq (   self)

Definition at line 281 of file uuid.py.

00281 
00282     def clock_seq(self):
00283         return (((self.clock_seq_hi_variant & 0x3f) << 8) |
00284                 self.clock_seq_low)

Here is the call graph for this function:

Definition at line 268 of file uuid.py.

00268 
00269     def clock_seq_hi_variant(self):
00270         return (self.int >> 56) & 0xff

Here is the caller graph for this function:

def uuid.UUID.clock_seq_low (   self)

Definition at line 272 of file uuid.py.

00272 
00273     def clock_seq_low(self):
00274         return (self.int >> 48) & 0xff

Here is the caller graph for this function:

def uuid.UUID.fields (   self)

Definition at line 251 of file uuid.py.

00251 
00252     def fields(self):
00253         return (self.time_low, self.time_mid, self.time_hi_version,
00254                 self.clock_seq_hi_variant, self.clock_seq_low, self.node)

Here is the call graph for this function:

def uuid.UUID.hex (   self)

Definition at line 290 of file uuid.py.

00290 
00291     def hex(self):
00292         return '%032x' % self.int

def uuid.UUID.node (   self)

Definition at line 286 of file uuid.py.

00286 
00287     def node(self):
00288         return self.int & 0xffffffffffff

Here is the caller graph for this function:

def uuid.UUID.time (   self)

Definition at line 276 of file uuid.py.

00276 
00277     def time(self):
00278         return (((self.time_hi_version & 0x0fff) << 48) |
00279                 (self.time_mid << 32) | self.time_low)

Here is the call graph for this function:

def uuid.UUID.time_hi_version (   self)

Definition at line 264 of file uuid.py.

00264 
00265     def time_hi_version(self):
00266         return (self.int >> 64) & 0xffff

Here is the caller graph for this function:

def uuid.UUID.time_low (   self)

Definition at line 256 of file uuid.py.

00256 
00257     def time_low(self):
00258         return self.int >> 96

Here is the caller graph for this function:

def uuid.UUID.time_mid (   self)

Definition at line 260 of file uuid.py.

00260 
00261     def time_mid(self):
00262         return (self.int >> 80) & 0xffff

Here is the caller graph for this function:

def uuid.UUID.urn (   self)

Definition at line 294 of file uuid.py.

00294 
00295     def urn(self):
00296         return 'urn:uuid:' + str(self)

def uuid.UUID.variant (   self)

Definition at line 298 of file uuid.py.

00298 
00299     def variant(self):
00300         if not self.int & (0x8000 << 48):
00301             return RESERVED_NCS
00302         elif not self.int & (0x4000 << 48):
00303             return RFC_4122
00304         elif not self.int & (0x2000 << 48):
00305             return RESERVED_MICROSOFT
00306         else:
00307             return RESERVED_FUTURE

def uuid.UUID.version (   self)

Definition at line 309 of file uuid.py.

00309 
00310     def version(self):
00311         # The version bits are only meaningful for RFC 4122 UUIDs.
00312         if self.variant == RFC_4122:
00313             return int((self.int >> 76) & 0xf)


Member Data Documentation

Definition at line 187 of file uuid.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 311 of file uuid.py.


The documentation for this class was generated from the following file: