bear_hug.bear_utilities module

A collection of random stuff for bear_hug that wouldn’t fit into other submodules.

Includes a series of useful functions and all bear_hug exception classes.

bear_hug.bear_utilities.shapes_equal(a, b)

Tests if two nested lists are of the same shape

Parameters:
  • a – list
  • b – list
Returns:

True if lists are indeed of the same shape, False otherwise

bear_hug.bear_utilities.copy_shape(l, value=None)

Takes a nested list and returns the new list of the same shape, completely filled with the same value.

May cause bugs when the value is mutable (for example, a list) because it fills the returned list with (pointers to) the same element, not independent copies. Since in practice this function is used to create colors for a widget with known chars, or otherwise to mess around with chars/colors data (which are normally replaced entirely, not edited), it is left for the callers to make sure their values are OK.

Parameters:
  • l – initial list
  • value – value to fill the list with
bear_hug.bear_utilities.slice_nested(l, slice_pos, slice_size)

Slice the nested list

Parameters:
  • l – a nested list
  • slice_pos – a 2-tuple (x, y) of slice start
  • slice_size – a 2-tuple (width, height) of slice size
bear_hug.bear_utilities.rotate_list(l)

Take a nested list of (x, y) dimensions, return an (y, x) list.

Parameters:l – a 2-nested list
bear_hug.bear_utilities.rectangles_collide(pos1, size1, pos2, size2)

Return True if the rectangles collide

Rectangles are supplied in [x,y], [xsize, ysize] form with the left corner and size. Assumes positions and sizes to be sorted

Parameters:
  • pos1 – top left corner of the first rectangle, as (x, y) 2-tuple
  • size1 – size of the first rectangle, as (width, height) 2-tuple
  • pos2 – top left corner of the second rectangle, as (x, y) 2-tuple
  • size2 – size of the second rectangle, as (width, height) 2-tuple
bear_hug.bear_utilities.has_values(l)

Returns True if a 2-nested list contains at least one truthy value.

Parameters:l – a nested list
Returns:
bear_hug.bear_utilities.blit(l1, l2, x, y)

Blits l2 to l1 at a given pos, overwriting the original values.

This method does not actually affect l1; instead, it copies it to a new variable, sets whatever values need to be set, and returns the modified copy.

Parameters:
  • l1 – A 2-nested list.
  • l2 – A 2-nested list.
  • y (x,) – A top left corner of l2 relative to l1.
Returns:

bear_hug.bear_utilities.generate_box(size, line_width='single')

Generate a chars list for a box bounded by pseudographic lines.

Uses CP437 chars 0xB3-0xDA translated to Unicode points (see `here<http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT>`_ for the translation table)

Parameters:
  • size – an (xsize, ysize) tuple
  • line_width – str. Either ‘single’ or ‘double’
Returns:

a nested list of chars.

class bear_hug.bear_utilities.Singleton

Bases: type

A Singleton metaclass for EntityTracker

exception bear_hug.bear_utilities.BearException

Bases: Exception

A base class for all bear_hug exceptions

exception bear_hug.bear_utilities.BearLoopException

Bases: bear_hug.bear_utilities.BearException

Something wrong with the loop or event system.

exception bear_hug.bear_utilities.BearLayoutException

Bases: bear_hug.bear_utilities.BearException

Something wrong with adding/drawing/removing a Widget on a Layout

exception bear_hug.bear_utilities.BearECSException

Bases: bear_hug.bear_utilities.BearException

Something wrong with Entity-Component System.

exception bear_hug.bear_utilities.BearSoundException

Bases: bear_hug.bear_utilities.BearException

Something wrong with the sound.

exception bear_hug.bear_utilities.BearJSONException

Bases: bear_hug.bear_utilities.BearException

Something wrong with JSON (de)serialization of widgets or entities.

exception bear_hug.bear_utilities.BearResourceException

Bases: bear_hug.bear_utilities.BearException

Something wrong with loading ASCII assets