Module gwy :: Class Inventory
[hide private]
[frames] | no frames]

Class Inventory

source code

Ordered item inventory, indexed by both name and position.

Inventory is a uniform container that offers both hash table and array (sorted or unsorted) interfaces. Both types of read access are fast, operations that modify it may be slower. Inventory can also maintain a notion of default item.

Inventory can be used both as an actual container for some data, or just wrap a static array with a the same interface so the actual storage is opaque to inventory user. The former kind of inventories can be created with Inventory.new() or Inventory.new_filled(); constant inventory is created with Inventory.new_from_array(). Contantess of an inventory can be tested with Inventory.is_const().

Possible operations with data items stored in an inventory are specified upon inventory creation with InventoryItemType structure. Not all fields are mandatory, with items allowing more operations the inventory is more capable too. For example, if items offer a method to make copies, Inventory.new_item() can be used to directly create new items in the inventory (this capability can be tested with Inventory.can_make_copies()).

Item can have `traits', that is data that can be obtained generically. They are similar to GObject properties. Actually, if items are objects, they should simply map object properties to traits. But it is possible to define traits for simple structures too.

Instance Methods [hide private]
 
__init__(itype)
Creates a new inventory.
source code
 
get_n_items()
Returns the number of items in an inventory.
source code
 
can_make_copies()
Returns whether an inventory can create new items itself.
source code
 
get_item_position(name)
Finds position of an item in an inventory.
source code
 
foreach(function, user_data)
Calls a function on each item of an inventory, in order.
source code
 
find(predicate, user_data)
Finds an inventory item using user-specified predicate function.
source code
 
set_default_item_name(name)
Sets the default of an inventory.
source code
 
get_default_item_name()
Returns the name of the default item of an inventory.
source code
 
item_updated(name)
Notifies inventory an item was updated.
source code
 
nth_item_updated(n)
Notifies inventory item on given position was updated.
source code
 
restore_order()
Assures an inventory is sorted.
source code
 
forget_order()
Forces an inventory to be unsorted.
source code
 
delete_item(name)
Deletes an item from an inventory.
source code
 
delete_nth_item(n)
Deletes an item on given position from an inventory.
source code
 
store_new()
Creates a new GtkTreeModel wrapper around a Inventory.
source code
 
get_item(name)
Looks up an item in an inventory.
source code
 
get_item_or_default(name)
Looks up an item in an inventory, eventually falling back to default.
source code
 
get_nth_item(n)
Returns item on given position in an inventory.
source code
 
get_default_item()
Returns the default item of an inventory.
source code
 
insert_item(object)
Inserts an item into an inventory.
source code
 
insert_nth_item(object, n)
Inserts an item to an explicit position in an inventory.
source code
 
rename_item(name, newname)
Renames an inventory item.
source code
 
new_item(name, newname)
Creates a new item as a copy of existing one and inserts it to inventory.
source code
Method Details [hide private]

__init__(itype)
(Constructor)

source code 

Creates a new inventory.

Parameters:
  • itype - Type of items the inventory will contain. (const-InventoryItemType*)
Returns:
The newly created inventory. (Inventory)

get_n_items()

source code 

Returns the number of items in an inventory.

Returns:
The number of items. (int)

can_make_copies()

source code 

Returns whether an inventory can create new items itself.

The prerequistie is that item type is a serializable object. It enables functions like Inventory.new_item().

Returns:
True if inventory can create new items itself. (bool)

get_item_position(name)

source code 

Finds position of an item in an inventory.

Parameters:
  • name - Item name. (string)
Returns:
Item position, or (guint)-1 if there is no such item. (int)

foreach(function, user_data)

source code 

Calls a function on each item of an inventory, in order.

function's first argument is item position (transformed with GUINT_TO_POINTER()), second is item pointer, and the last is user_data.

Parameters:
  • function - A function to call on each item. It must not modify inventory. (GHFunc)
  • user_data - Data passed to function. (gpointer)

find(predicate, user_data)

source code 

Finds an inventory item using user-specified predicate function.

predicate is called for each item in inventory (in order) until it returns True. Its arguments are the same as in Inventory.foreach().

Parameters:
  • predicate - A function testing some item property. It must not modify inventory. (GHRFunc)
  • user_data - Data passed to predicate. (gpointer)
Returns:
The item for which predicate returned True. If there is no such item in the inventory, None is returned. (gpointer)

set_default_item_name(name)

source code 

Sets the default of an inventory.

Item name must already exist in the inventory.

Parameters:
  • name - Item name, pass None to unset default item. (string)

get_default_item_name()

source code 

Returns the name of the default item of an inventory.

Returns:
The default item name, None if no default name is set. Item of this name may or may not exist in the inventory. (string)

item_updated(name)

source code 

Notifies inventory an item was updated.

This function makes sense primarily for non-object items, as object items can notify inventory via signals.

Parameters:
  • name - Item name. (string)

nth_item_updated(n)

source code 

Notifies inventory item on given position was updated.

This function makes sense primarily for non-object items, as object items can provide watchable_signal.

Parameters:
  • n - Item position. (int)

forget_order()

source code 

Forces an inventory to be unsorted.

Item positions don't change, but future Inventory.insert_item() won't try to insert items in order.

delete_item(name)

source code 

Deletes an item from an inventory.

Parameters:
  • name - Name of item to delete. (string)
Returns:
True if item was deleted. (bool)

delete_nth_item(n)

source code 

Deletes an item on given position from an inventory.

Parameters:
  • n - Position of item to delete. (int)
Returns:
True if item was deleted. (bool)

store_new()

source code 

Creates a new GtkTreeModel wrapper around a Inventory.

Returns:
The newly created inventory store. (InventoryStore)

get_item(name)

source code 

Looks up an item in an inventory.

Parameters:
  • name - Item name. (string)
Returns:
Item called name, or None if there is no such item. (gobject.GObject)

get_item_or_default(name)

source code 

Looks up an item in an inventory, eventually falling back to default.

The lookup order is: item of requested name, default item (if set), any inventory item, None (can happen only when inventory is empty).

Parameters:
  • name - Item name. (string)
Returns:
Item called name, or default item. (gobject.GObject)

get_nth_item(n)

source code 

Returns item on given position in an inventory.

Parameters:
  • n - Item position. It must be between zero and the number of items in inventory, inclusive. If it is equal to the number of items, None is returned. In other words, inventory behaves like a None-terminated array, you can simply iterate over it until Inventory.get_nth_item() returns None. (int)
Returns:
Item at given position. (gobject.GObject)

get_default_item()

source code 

Returns the default item of an inventory.

Returns:
The default item. If there is no default item, None is returned. (gobject.GObject)

insert_item(object)

source code 

Inserts an item into an inventory.

Item of the same name must not exist yet.

If the inventory is sorted, item is inserted to keep order. If the inventory is unsorted, item is simply added to the end.

Parameters:
  • object - (gobject.GObject)

insert_nth_item(object, n)

source code 

Inserts an item to an explicit position in an inventory.

Item of the same name must not exist yet.

Parameters:
  • object - (gobject.GObject)
  • n - Position to insert item to. (int)

rename_item(name, newname)

source code 

Renames an inventory item.

If an item of name newname is already present in inventory, the rename will fail.

Parameters:
  • name - Name of item to rename. (string)
  • newname - New name of item. (string)

new_item(name, newname)

source code 

Creates a new item as a copy of existing one and inserts it to inventory.

The newly created item can be called differently than newname if that already exists.

Parameters:
  • name - Name of item to duplicate, may be None to use default item (the same happens when name does not exist). (string)
  • newname - Name of new item, it must not exist yet. It may be None, the new name is based on name then. (string)
Returns:
The newly added item. (gobject.GObject)