Package pycocoa :: Module utils
[frames] | no frames]

Module utils

(INTERNAL) Utility functions, constants, etc.


Version: 18.06.10

Functions
 
gcd(a, b)
Calculate the Greatest Common Divisor of a and b.
 
aspect_ratio(width, height)
Compute the smallest, int aspect ratio.
 
bytes2repr(bytestr)
Represent bytes like repr(bytestr).
 
bytes2str(bytestr, dflt=missing())
Convert bytes/unicode to str if needed.
iterator
iterbytes(collection)
iter(callable, sentinel) -> iterator
 
str2bytes(bytestr, dflt=missing())
Convert str to bytes/unicode if needed.
 
clip(bytestr, limit=50)
Clip a string or bytes to the given length limit.
 
flint(f)
Return int for integer float.
 
inst2strepr(inst, strepr, *attrs)
Convert an instance's attributes, maintaining the order.
 
isinstanceOf(inst, *classes, **name_missing)
Check a Python object's class.
 
printf(fmt, *args, **kwds)
Formatted print fmt % args with optional keywords.
 
type2strepr(inst, strepr=<type 'str'>)
Return a Python Type instance as str or repr.
 
z1000str(size, sep='_')
Convert a size value to string with 1_000's seperator.
 
zSIstr(size, B='B')
Convert a size value to string with SI-units.
Variables
  DEFAULT_UNICODE = 'utf-8'
  missing = missing()
Missing keyword argument value.
Function Details

gcd(a, b)

 

Calculate the Greatest Common Divisor of a and b.

Unless b==0, the result will have the same sign as b (so that when b is divided by it, the result comes out positive).

aspect_ratio(width, height)

 

Compute the smallest, int aspect ratio.

Parameters:
  • width - The width (float or int).
  • height - The height (float or int).
Returns:
2-Tuple (width, height) or None.

Example: >>> aspect_ratio(10, 15) (2, 3) >>> aspect_ratio(10.0, 15) (2, 3) >>> aspect_ratio(10, -15) (-2, 3) >>> aspect_ratio(-10, -15) (2, 3) >>> aspect_ratio(10.5, 15) (7, 10) >>> aspect_ratio(0, 15) ()

bytes2repr(bytestr)

 

Represent bytes like repr(bytestr).

Parameters:
  • bytestr - Str or bytes.
Returns:
Representation b'...' (str).

bytes2str(bytestr, dflt=missing())

 

Convert bytes/unicode to str if needed.

Parameters:
  • bytestr - Bytes, str or unicode.
  • dflt - Optional, default return value.
Returns:
Str or dflt.
Raises:
  • TypeError - If neither str nor bytes, but iff no dflt is provided.

iterbytes(collection)

 

iter(callable, sentinel) -> iterator

Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. In the second form, the callable is called until it returns the sentinel.

Returns: iterator

str2bytes(bytestr, dflt=missing())

 

Convert str to bytes/unicode if needed.

Parameters:
  • bytestr - Bytes, str or unicode.
  • dflt - Optional, default return value.
Returns:
Bytes or dflt.
Raises:
  • TypeError - If neither bytes nor str, but iff no dflt is provided.

clip(bytestr, limit=50)

 

Clip a string or bytes to the given length limit.

Parameters:
  • bytestr - Bytes or str.
  • limit - Length limit (int).
Returns:
Bytes or str.

inst2strepr(inst, strepr, *attrs)

 

Convert an instance's attributes, maintaining the order.

Parameters:
  • inst - Instance (any).
  • strepr - Conversion (repr or str).
  • attrs - Instance attribute names (all positional).
Returns:
Instance representation (str).

isinstanceOf(inst, *classes, **name_missing)

 

Check a Python object's class.

Parameters:
  • inst - The instance to check (any).
  • classes - One or several classes (all positional).
  • name - The name of the instance (str).
Returns:
The matching class from classes, None otherwise.
Raises:
  • TypeError - If inst does not match any of the classes, but iff keyword name='...' is provided.

See Also: Function isInstanceOf for checking ObjC instances.

printf(fmt, *args, **kwds)

 

Formatted print fmt % args with optional keywords.

Parameters:
  • fmt - Print-like format (str).
  • args - Optional arguments to include (all positional).
  • argv0 - Optional prefix (str).
  • nl - Number of leading blank lines (int).
  • nt - Number of trailing blank lines (int).

type2strepr(inst, strepr=<type 'str'>)

 

Return a Python Type instance as str or repr.

Parameters:
  • inst - Instance (any).
  • strepr - Conversion (repr or str).
Returns:
Instance representation (str).

z1000str(size, sep='_')

 

Convert a size value to string with 1_000's seperator.

Parameters:
  • size - Value to convert (float or int).
Returns:
"<1or2digits><sep><3digits>..." or "-" if size is negative (str).

zSIstr(size, B='B')

 

Convert a size value to string with SI-units.

Parameters:
  • size - Value to convert (float or int).
Returns:
"<Size> <B><SI>" (str).