source
stringclasses
1 value
version
stringclasses
1 value
module
stringclasses
43 values
function
stringclasses
307 values
input
stringlengths
3
496
expected
stringlengths
0
40.5k
signature
stringclasses
0 values
cpython
cfcd524
uuid
__module__
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' # make a UUID from a 16-byte string
null
cpython
cfcd524
uuid
__module__
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f') # get the Nil UUID
null
cpython
cfcd524
uuid
__module__
>>> uuid.NIL
UUID('00000000-0000-0000-0000-000000000000') # get the Max UUID
null
cpython
cfcd524
uuid
__module__
>>> uuid.MAX
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> p = Point(11, y=22) # instantiate with positional args or keywords
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> p[0] + p[1] # indexable like a plain tuple
33
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> x, y = p # unpack like a regular tuple
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> x, y
(11, 22)
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> p.x + p.y # fields also accessible by name
33
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> d = p._asdict() # convert to a dictionary
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> d['x']
11
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
null
cpython
cfcd524
collections
OrderedDict.namedtuple
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
null
cpython
cfcd524
tempfile
__module__
>>> tempfile.mkstemp()
(4, '/tmp/tmptpu9nin8')
null
cpython
cfcd524
tempfile
__module__
>>> tempfile.mkdtemp(suffix=b'')
b'/tmp/tmppbi8f0hy' This module also provides some data items to the user: TMP_MAX - maximum number of names that will be tried before giving up. tempdir - If this is set to a string before the first use of any routine from this module, it will be considered as another candidate location to store temporary files.
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s = FoldedCase('hello world')
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s == 'Hello World'
True
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> 'Hello World' == s
True
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s != 'Hello World'
False
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s.index('O')
4
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s.split('O')
['hell', ' w', 'rld']
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
['alpha', 'Beta', 'GAMMA'] Sequence membership is straightforward.
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> "Hello World" in [s]
True
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s in ["Hello World"]
True You may test for set inclusion, but candidate and elements must both be folded.
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> FoldedCase("Hello World") in {s}
True
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> s in {FoldedCase("Hello World")}
True String inclusion works as long as the FoldedCase object is on the right.
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> "hello" in FoldedCase("Hello World")
True But not if the FoldedCase object is on the left:
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> FoldedCase('hello') in 'Hello World'
False In that case, use in_:
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> FoldedCase('hello').in_('Hello World')
True
null
cpython
cfcd524
importlib.metadata._text
FoldedCase
>>> FoldedCase('hello') > FoldedCase('Hello')
False
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> class MyClass: ... calls = 0 ... ... @method_cache ... def method(self, value): ... self.calls += 1 ... return value
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a = MyClass()
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.method(3)
3
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> for x in range(75): ... res = a.method(x)
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.calls
75 Note that the apparent behavior will be exactly like that of lru_cache except that the cache is stored on each instance, so values in one instance will not flush values from another, and when an instance is deleted, so are the cached values for that instance.
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> b = MyClass()
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> for x in range(35): ... res = b.method(x)
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> b.calls
35
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.method(0)
0
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.calls
75 Note that if method had been decorated with ``functools.lru_cache()``, a.calls would have been 76 (due to the cached value of 0 having been flushed by the 'b' instance). Clear the cache with ``.cache_clear()``
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.method.cache_clear()
Same for a method that hasn't yet been called.
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> c = MyClass()
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> c.method.cache_clear()
Another cache wrapper may be supplied:
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> cache = functools.lru_cache(maxsize=2)
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache)
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a = MyClass()
null
cpython
cfcd524
importlib.metadata._functools
method_cache
>>> a.method2()
3 Caution - do not subsequently wrap the method with another decorator, such as ``@property``, which changes the semantics of the function. See also http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/ for another implementation and additional justification.
null
cpython
cfcd524
importlib.metadata._functools
pass_none
>>> print_text = pass_none(print)
null
cpython
cfcd524
importlib.metadata._functools
pass_none
>>> print_text('text')
text
null
cpython
cfcd524
importlib.metadata._functools
pass_none
>>> print_text(None)
null
cpython
cfcd524
importlib.metadata._collections
FreezableDefaultDict
>>> dd = FreezableDefaultDict(list)
null
cpython
cfcd524
importlib.metadata._collections
FreezableDefaultDict
>>> dd[0].append('1')
null
cpython
cfcd524
importlib.metadata._collections
FreezableDefaultDict
>>> dd.freeze()
null
cpython
cfcd524
importlib.metadata._collections
FreezableDefaultDict
>>> dd[1]
[]
null
cpython
cfcd524
importlib.metadata._collections
FreezableDefaultDict
>>> len(dd)
1
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = (1, 2, 3)
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj))
[1, 2, 3] If *obj* is not iterable, return a one-item iterable containing *obj*::
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = 1
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj))
[1] If *obj* is ``None``, return an empty iterable:
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = None
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(None))
[] By default, binary and text strings are not considered iterable::
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = 'foo'
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj))
['foo'] If *base_type* is set, objects for which ``isinstance(obj, base_type)`` returns ``True`` won't be considered iterable.
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = {'a': 1}
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj)) # Iterate over the dict's keys
['a']
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj, base_type=dict)) # Treat dicts as a unit
[{'a': 1}] Set *base_type* to ``None`` to avoid any special handling and treat objects Python considers iterable as iterable:
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> obj = 'foo'
null
cpython
cfcd524
importlib.metadata._itertools
always_iterable
>>> list(always_iterable(obj, base_type=None))
['f', 'o', 'o']
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> iterable = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3']
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> s = bucket(iterable, key=lambda x: x[0]) # Bucket by 1st character
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> sorted(list(s)) # Get the keys
['a', 'b', 'c']
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> a_iterable = s['a']
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> next(a_iterable)
'a1'
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> next(a_iterable)
'a2'
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> list(s['b'])
['b1', 'b2', 'b3'] The original iterable will be advanced and its items will be cached until they are used by the child iterables. This may require significant storage. By default, attempting to select a bucket to which no items belong will exhaust the iterable and cache all values. If you specify a *validator* function, selected buckets will instead be checked against it.
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> from itertools import count
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> it = count(1, 2) # Infinite sequence of odd numbers
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> key = lambda x: x % 10 # Bucket by last digit
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> validator = lambda x: x in {1, 3, 5, 7, 9} # Odd digits only
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> s = bucket(it, key=key, validator=validator)
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> 2 in s
False
null
cpython
cfcd524
importlib.metadata._itertools
bucket
>>> list(s[2])
[]
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> for item in Sectioned.read(Sectioned._sample): ... print(item)
Pair(name='sec1', value='# comments ignored') Pair(name='sec1', value='a = 1') Pair(name='sec1', value='b = 2') Pair(name='sec2', value='a = 2')
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> res = Sectioned.section_pairs(Sectioned._sample)
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item = next(res)
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item.name
'sec1'
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item.value
Pair(name='a', value='1')
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item = next(res)
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item.value
Pair(name='b', value='2')
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item = next(res)
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item.name
'sec2'
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> item.value
Pair(name='a', value='2')
null
cpython
cfcd524
importlib.metadata
Sectioned
>>> list(res)
[]
null
cpython
cfcd524
importlib.metadata
EntryPoint
>>> ep = EntryPoint( ... name=None, group=None, value='package.module:attr [extra1, extra2]')
null
cpython
cfcd524
importlib.metadata
EntryPoint
>>> ep.module
'package.module'
null
cpython
cfcd524
importlib.metadata
EntryPoint
>>> ep.attr
'attr'
null
cpython
cfcd524
importlib.metadata
EntryPoint
>>> ep.extras
['extra1', 'extra2'] If the value package or module are not valid identifiers, a ValueError is raised on access.
null
cpython
cfcd524
importlib.metadata
EntryPoint
>>> EntryPoint(name=None, group=None, value='invalid-name').module
Traceback (most recent call last): ... ValueError: ('Invalid object reference...invalid-name...
null