utils Module
Classes
| CountUpDownLatch |
CountUpDownLatch provides a thread safe implementation of Up Down latch |
Functions
clamp
Limit a value to a given range
This is equivalent to smallest <= n <= largest.
clamp(n, smallest, largest)
Parameters
- n
- smallest
- largest
Examples
>>> clamp(0, 1, 100)
1
>>> clamp(42, 2, 128)
42
>>> clamp(1024, 1, 32)
32
commonprefix
Find common directory for all paths
Python's os.path.commonprefix will not return a valid directory path in
some cases, so we wrote this convenience method.
commonprefix(paths)
Parameters
- paths
Examples
>>> # os.path.commonprefix returns '/disk1/foo'
>>> commonprefix(['/disk1/foobar', '/disk1/foobaz'])
'/disk1'
>>> commonprefix(['a/b/c', 'a/b/d', 'a/c/d'])
'a'
>>> commonprefix(['a/b/c', 'd/e/f', 'g/h/i'])
''
ensure_writable
ensure_writable(b)
Parameters
- b
read_block
Read a block of bytes from a file
read_block(f, offset, length, delimiter=None)
Parameters
- fn
- <xref:<xref:file object>>
Required
a file object that supports seek, tell and read.
- using the delimiter= keyword argument we ensure that the read
- <xref:If>
Required
- at or before the delimiter boundaries that follow the location
- <xref:stops>
Required
- + length. For ADL
- <xref:offset>
Required
- no delimiter is found and the data
- <xref:if>
Required
- is > 4MB an exception is raised
- <xref:requested>
Required
- a single record cannot
- <xref:since>
Required
- 4MB and be guaranteed to land contiguously in ADL.
- <xref:exceed>
Required
- bytestring returned WILL include the
- <xref:The>
Required
- delimiter string.
- <xref:terminating>
Required
Examples
>>> from io import BytesIO
>>> f = BytesIO(b'Alice, 100\nBob, 200\nCharlie, 300')
>>> read_block(f, 0, 13)
b'Alice, 100\nBo'
>>> read_block(f, 0, 13, delimiter=b'\n')
b'Alice, 100\n'
>>> read_block(f, 10, 10, delimiter=b'\n')
b'\nCharlie, 300'
>>> f = BytesIO(bytearray(2**22))
>>> read_block(f,0,2**22, delimiter=b'\n')
IndexError: No delimiter found within max record size of 4MB.
Transfer without specifying a delimiter (as binary) instead.
tokenize
Deterministic token
>>> tokenize('Hello') == tokenize('Hello')
True
tokenize(*args, **kwargs)
write_stdout
Write bytes or strings to standard output
write_stdout(data)
Parameters
- data
Feedback
Submit and view feedback for