Package qubx :: Module util_types
[hide private]
[frames] | no frames]

Module util_types

source code

General-purpose types and utilities.

Copyright 2007-2014 Research Foundation State University of New York This file is part of QUB Express.

QUB Express is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

QUB Express is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License, named LICENSE.txt, in the QUB Express program directory. If not, see <http://www.gnu.org/licenses/>.

Classes [hide private]
  Event
Usage:
  WeakEvent
Usage:
  WeakCall
Like WeakEvent, except there's at most one listener, and it can return a value.
  Reffer
Holds references to keep objects alive.
  DeathRattle
Prints to stdout when it is finally freed, presumably at the same time as its owner.
  Anon
An object whose fields are the constructor keyword arguments.
  JsonAnon
  Product
  OverridingDict
"Overrides" the base dict so keys are looked up first here, then if not found, in base dict.
  ToolRegistry
A collection of tools or actions, grouped by category.
Functions [hide private]
 
printer(*xx)
Prints and returns its argument(s).
source code
 
breduce(func, seq)
like builtin reduce(), but recursing by splitting in half.
source code
 
memoize(func, lbl='')
Returns wrapped func that caches return values for specific args.
source code
 
memoize_last_n(func, n=1, lbl='')
Returns wrapped func that caches most recent n return values for specific args.
source code
 
memoize_heapq(func, max_weight=1000000, weigh=<__builtin__.function object>, lbl='')
Returns wrapped func that caches return values for specific args.
source code
 
heapq_swim(heap, k) source code
 
heapq_sink(heap, k) source code
 
heapq_remove(heap, index)
Remove item from heap
source code
 
bind(callback, *args, **kw)
Returns a function f which, when called, discards its own args and returns callback(*args, **kw).
source code
 
bind_with_args(callback, *args, **kw)
Like bind, but it appends the callback's positional args after the bound positional args; returns callback(*(args+cb_args), **kw).
source code
 
bind_with_args_before(callback, *args, **kw)
Like bind, but it prepends the callback's positional args before the bound positional args; returns callback(*(args+cb_args), **kw).
source code
 
HSVtoRGB(h, s, v)
Returns (r,g,b) color coordinates corresponding to the hue, saturation and value.
source code
 
RGBtoHSV(r, g, b)
Returns the (hue, saturation, value) color coordinates corresponding to the RGB color.
source code
 
RGBtoCMY(r, g, b) source code
 
RGBtoCMYK(r, g, b) source code
 
ScaleToColor(x, saturation=1.0)
Returns r,g,b corresponding to x in [-1..1].
source code
 
WITHALPHA(tup) source code
 
NOALPHA(tup) source code
 
SETALPHA(tup, a) source code
 
ADDALPHA(tup, a) source code
 
INVERT_COLOR(tup) source code
 
COLOR_CLASS(c) source code
 
rdact() source code
 
path_splits(s)
Returns the list of all path components '/a/b/c' -> ['/', 'a', 'b', 'c']
source code
 
add_ext_if_none(path, ext)
Returns path unmodified if it already has an extension, otherwise appends ext.
source code
 
scrolled_int(val, offset, fine=False, coarse=False, lo=None, hi=None)
Returns val, increased or decreased proportional to offset.
source code
 
scrolled_float(val, offset, fine=False, coarse=False, lo=None, hi=None)
Returns val scrolled according to offset direction and modifiers.
source code
 
scrolled_digits(val, offset, fine=False, coarse=False, lo=None, hi=None)
Returns val scrolled according to offset direction and modifiers; by default moves in increments of 10**(k-1), where 10**k <= val < 10**(k+1) -- the second most significant digit.
source code
 
nested_break(*args, **kwds)
from Mark Dickinson (via stackoverflow.com): a more legible, flexible way to break out of nested loops, e.g.
source code
 
transpose_arrays(arrays)
Takes as input an array of arrays, returns them transposed (so output[j][i] == arrays[i][j]).
source code
 
SafeName(nm) source code
 
union_lists(a, b)
return the union of two lists
source code
 
union(*lists) source code
 
iunion(genexp) source code
Variables [hide private]
  ShowDropped = False
  UNSET_VALUE = 86738.82583
  true = True
  false = False
  COLOR = defaultdict(<function <lambda> at 0x7fd137b1e1b8>, {0:...
  MAX_SAFENAME_LEN = 32
  valid_identifier = re.compile(r'^[A-Za-z_][A-Za-z_0-9]*$')
  __package__ = 'qubx'
  e = 2.71828182846
  pi = 3.14159265359
  rdbk = 'Expired.'
  rdbreak = 'supporting'
  rdname = 'qubfast.txt'
Function Details [hide private]

printer(*xx)

source code 

Prints and returns its argument(s). Handy for testing callbacks.

breduce(func, seq)

source code 

like builtin reduce(), but recursing by splitting in half. Doesn't seem to make a difference.

memoize(func, lbl='')

source code 

Returns wrapped func that caches return values for specific args. All args must be immutable (e.g. tuples not lists).

Example:

>>> def factorial(n):
>>>     if n > 1:
>>>        return n * fast_fac(n-1)
>>>     else:
>>>        return 1
>>> fast_fac = memoize(factorial)

memoize_last_n(func, n=1, lbl='')

source code 

Returns wrapped func that caches most recent n return values for specific args. All args must be immutable (e.g. tuples not lists).

memoize_heapq(func, max_weight=1000000, weigh=<__builtin__.function object>, lbl='')

source code 

Returns wrapped func that caches return values for specific args. All args must be immutable (e.g. tuples not lists).

Keeps most recent k results such that sum(weigh(result) for result in results[:k]) <= max_weight.

Example:

>>> def factorial(n):
>>>     if n > 1:
>>>        return n * fast_fac(n-1)
>>>     else:
>>>        return 1
>>> fast_fac = memoize(factorial)

bind(callback, *args, **kw)

source code 

Returns a function f which, when called, discards its own args and returns callback(*args, **kw).

Lambda can be useful to share one message handler among many sources, but there's a limitation:

>>> for i in xrange(N):
        chk[i].OnToggle += self__ref(lambda chk: self.__onToggle(i))

Here, the lambda binds not the value of i, but the stack location whose contents are repeatedly changed by the for loop. So all the lambdas are effectively doing self.__onToggle(N-1).

Instead:

>>> for i in xrange(N):
        chk[i].OnToggle += self.__ref(bind(self.__onToggle, i))

This creates a unique stack frame for your unique i, so it does what you expect.

HSVtoRGB(h, s, v)

source code 

Returns (r,g,b) color coordinates corresponding to the hue, saturation and value. All coordinates are between 0 and 1.

RGBtoHSV(r, g, b)

source code 

Returns the (hue, saturation, value) color coordinates corresponding to the RGB color. All coordinates are between 0 and 1.

ScaleToColor(x, saturation=1.0)

source code 

Returns r,g,b corresponding to x in [-1..1]. The scale moves smoothly around the color wheel from pink (-) to red (+), with brightness proportional to abs(x).

scrolled_int(val, offset, fine=False, coarse=False, lo=None, hi=None)

source code 

Returns val, increased or decreased proportional to offset.

Parameters:
  • val - integer value
  • offset - integer distance as supplied by gtk scroll-event
  • fine - True to use offset/10.0
  • coarse - True to use offset*10.0
  • lo - minimum output value
  • hi - maximum output value

scrolled_float(val, offset, fine=False, coarse=False, lo=None, hi=None)

source code 

Returns val scrolled according to offset direction and modifiers.

Parameters:
  • val - floating-point value
  • offset - integer distance as supplied by gtk scroll-event
  • fine - True to use offset/10.0
  • coarse - True to use offset*10.0
  • lo - minimum output value
  • hi - maximum output value

scrolled_digits(val, offset, fine=False, coarse=False, lo=None, hi=None)

source code 

Returns val scrolled according to offset direction and modifiers; by default moves in increments of 10**(k-1), where 10**k <= val < 10**(k+1) -- the second most significant digit.

Parameters:
  • val - floating-point value
  • offset - integer distance as supplied by gtk scroll-event
  • fine - True to move in increments of 10**(k-2) -- the third most significant digit
  • coarse - True to move in increments of 10**k -- the most significant digit
  • lo - minimum output value
  • hi - maximum output value

nested_break(*args, **kwds)

source code 
from Mark Dickinson (via stackoverflow.com): a more legible, flexible way to break out of nested loops, e.g.
with nested_break() as mylabel:
    while True:
        print "current state"
        while True:
            ok = raw_input("Is this ok? (y/n)")
            if ok == "y" or ok == "Y": raise mylabel
            if ok == "n" or ok == "N": break
        print "more processing"

Decorators:
  • @contextmanager

Variables Details [hide private]

COLOR

Value:
defaultdict(<function <lambda> at 0x7fd137b1e1b8>, {0: (0, 0, 0), 1: (\
1, 0, 0), 2: (0, 0, 1), 3: (0, 1, 0), 4: (0, 1, 1), 5: (1, 1, 0), 6: (\
1, 0, 1), 7: (0.5, 0.0, 0.0), 8: (0.525, 0.32025, 0.013125000000000012\
), 9: (0.44550000000000006, 0.55, 0.027500000000000028), 10: (0.149499\
99999999994, 0.575, 0.04312499999999997), 11: (0.059999999999999984, 0\
.6, 0.2759999999999999), 12: (0.078125, 0.625, 0.625), 13: (0.09750000\
000000002, 0.3185, 0.65), 14: (0.22949999999999962, 0.1181250000000000\
4, 0.675), 15: (0.5879999999999999, 0.13999999999999996, 0.7), 16: (0.\
...