montepy.numbered_object_collection module#
Classes:
|
|
|
A collections of MCNP objects. |
- class montepy.numbered_object_collection.NumberedDataObjectCollection(obj_class, objects=None, problem=None)#
Bases:
NumberedObjectCollection
Methods:
add
(obj)Add the given object to this collection.
append
(obj, **kwargs)Appends the given object to the end of this collection.
append_renumber
(obj[, step])Appends the object, but will renumber the object if collision occurs.
check_number
(number)Checks if the number is already in use, and if so raises an error.
clear
()Removes all objects from this collection.
clone
([starting_number, step])Create a new instance of this collection, with all new independent objects with new numbers.
difference
(*others)Return a new collection with elements from collection, that are not in the others.
difference_update
(*others)Update the new collection removing all elements from others.
discard
(obj)Remove the object from the collection if it is present.
extend
(other_list)Extends this collection with another list.
get
(i[, default])Get
i
if possible, or else returndefault
.intersection
(*others)Return a new collection with all elements in common in collection, and all others.
intersection_update
(*others)Update the collection keeping all elements in common in collection, and all others.
isdisjoint
(other)Test if there are no elements in common between the collection, and other.
issubset
(other)Test whether every element in the collection is in other.
issuperset
(other)Test whether every element in other is in the collection.
items
()Get iterator of the collections (number, object) pairs.
keys
()Get iterator of the collection's numbers.
link_to_problem
(problem)Links the card to the parent problem for this card.
next_number
([step])Get the next available number, based on the maximum number.
pop
([pos])Pop the final items off of the collection
remove
(delete)Removes the given object from the collection.
request_number
([start_num, step])Requests a new available number.
symmetric_difference
(other)Return a new collection with elements in either the collection or the other, but not both.
symmetric_difference_update
(other)Update the collection, keeping only elements found in either collection, but not in both.
union
(*others)Return a new collection with all elements from collection, and all others.
update
(*objs)Add the given objects to this collection.
values
()Get iterator of the collection's objects.
Attributes:
A generator of the numbers being used.
Returns a shallow copy of the internal objects list.
The starting number to use when an object is cloned.
The step size to use to find a valid number during cloning.
- add(obj: Numbered_MCNP_Object)#
Add the given object to this collection.
- Parameters:
obj (Numbered_MCNP_Object) – The object to add.
- Raises:
TypeError – if the object is of the wrong type.
NumberConflictError – if this object’s number is already in use in the collection.
- append(obj, **kwargs)#
Appends the given object to the end of this collection.
- Parameters:
obj (Numbered_MCNP_Object) – the object to add.
**kwargs – extra arguments that are used internally.
- Raises:
NumberConflictError – if this object has a number that is already in use.
- append_renumber(obj, step=1)#
Appends the object, but will renumber the object if collision occurs.
This behaves like append, except if there is a number collision the object will be renumbered to an available number. The number will be incremented by step until an available number is found.
- Parameters:
obj (Numbered_MCNP_Object) – The MCNP object being added to the collection.
step (int) – the incrementing step to use to find a new number.
- Returns:
the number for the object.
- Return type:
int
- check_number(number)#
Checks if the number is already in use, and if so raises an error.
- Parameters:
number (int) – The number to check.
- Raises:
NumberConflictError – if this number is in use.
- clear()#
Removes all objects from this collection.
- clone(starting_number=None, step=None)#
Create a new instance of this collection, with all new independent objects with new numbers.
This relies mostly on
copy.deepcopy
.Notes
If starting_number, or step are not specified
starting_number()
, andstep()
are used as default values.Added in version 0.5.0.
- Parameters:
starting_number (int) – The starting number to request for a new object numbers.
step (int) – the step size to use to find a new valid number.
- Returns:
a cloned copy of this object.
- Return type:
type(self)
- difference(*others: Self)#
Return a new collection with elements from collection, that are not in the others.
collection - other - ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- difference_update(*others: Self)#
Update the new collection removing all elements from others.
collection -= other | ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- discard(obj: Numbered_MCNP_Object)#
Remove the object from the collection if it is present.
Added in version 1.0.0.
- Parameters:
obj (Numbered_MCNP_Object) – the object to remove.
- extend(other_list)#
Extends this collection with another list.
- Parameters:
other_list (list) – the list of objects to add.
- Raises:
NumberConflictError – if these items conflict with existing elements.
- get(i, default=None)#
Get
i
if possible, or else returndefault
.- Parameters:
i (int) – number of the object to get, not it’s location in the internal list
default (object) – value to return if not found
- Return type:
- intersection(*others: Self)#
Return a new collection with all elements in common in collection, and all others.
collection & other & ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- intersection_update(*others: Self)#
Update the collection keeping all elements in common in collection, and all others.
collection &= other & ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- isdisjoint(other: Self)#
Test if there are no elements in common between the collection, and other.
Collections are disjoint if and only if their intersection is the empty set.
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- issubset(other: Self)#
Test whether every element in the collection is in other.
collection <= other
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- issuperset(other: Self)#
Test whether every element in other is in the collection.
collection >= other
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- items() Generator[Tuple[int, Numbered_MCNP_Object], None, None] #
Get iterator of the collections (number, object) pairs.
- Return type:
tuple(int, MCNP_Object)
- keys() Generator[int, None, None] #
Get iterator of the collection’s numbers.
- Return type:
int
- link_to_problem(problem)#
Links the card to the parent problem for this card.
This is done so that cards can find links to other objects.
- Parameters:
problem (MCNP_Problem) – The problem to link this card to.
- next_number(step=1)#
Get the next available number, based on the maximum number.
This works by finding the current maximum number, and then adding the stepsize to it.
- Parameters:
step (int) – how much to increase the last number by
- pop(pos=-1)#
Pop the final items off of the collection
- Parameters:
pos (int) – The index of the element to pop from the internal list.
- Returns:
the final elements
- Return type:
- remove(delete)#
Removes the given object from the collection.
- Parameters:
delete (Numbered_MCNP_Object) – the object to delete
- request_number(start_num=None, step=None)#
Requests a new available number.
This method does not “reserve” this number. Objects should be immediately added to avoid possible collisions caused by shifting numbers of other objects in the collection.
Notes
If starting_number, or step are not specified
starting_number()
, andstep()
are used as default values.Changed in version 0.5.0: In 0.5.0 the default values were changed to reference
starting_number()
andstep()
.- Parameters:
start_num (int) – the starting number to check.
step (int) – the increment to jump by to find new numbers.
- Returns:
an available number
- Return type:
int
- symmetric_difference(other: Self)#
Return a new collection with elements in either the collection or the other, but not both.
collection ^ other
Added in version 1.0.0.
- Parameters:
others (Self) – the other collections to compare to.
other (Self)
- Return type:
Self
- symmetric_difference_update(other: Self)#
Update the collection, keeping only elements found in either collection, but not in both.
collection ^= other
Added in version 1.0.0.
- Parameters:
others (Self) – the other collections to compare to.
other (Self)
- union(*others: Self)#
Return a new collection with all elements from collection, and all others.
collection | other | ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- update(*objs: Self)#
Add the given objects to this collection.
Notes
This is not a thread-safe method.
Changed in version 1.0.0: Changed to be more set like. Accepts multiple arguments. If there is a number conflict, the current object will be kept.
- Parameters:
*objs (list[Numbered_MCNP_Object]) – The objects to add.
- Raises:
TypeError – if the object is of the wrong type.
NumberConflictError – if this object’s number is already in use in the collection.
- values() Generator[Numbered_MCNP_Object, None, None] #
Get iterator of the collection’s objects.
- Return type:
- property numbers#
A generator of the numbers being used.
- Return type:
generator
- property objects#
Returns a shallow copy of the internal objects list.
The list object is a new instance, but the underlying objects are the same.
- Return type:
list
- property starting_number#
The starting number to use when an object is cloned.
- Returns:
the starting number
- Return type:
int
- property step#
The step size to use to find a valid number during cloning.
- Returns:
the step size
- Return type:
int
- class montepy.numbered_object_collection.NumberedObjectCollection(obj_class: type, objects: list = None, problem: MCNP_Problem = None)#
Bases:
ABC
A collections of MCNP objects.
Examples
Accessing Objects#
It quacks like a dict, it acts like a dict, but it’s a list.
The items in the collection are accessible by their number. For instance to get the Cell with a number of 2 you can just say:
>>> import montepy >>> problem = montepy.read_input("tests/inputs/test.imcnp") >>> cell = problem.cells[2] >>> print(cell) CELL: 2, mat: 2, DENS: 8.0 atom/b-cm
You can also add, and delete items like you would in a dictionary normally. Though
append()
andadd()
are the preferred way of adding items. When adding items by key the key given is actually ignored.import montepy problem = montepy.read_input("tests/inputs/test.imcnp") cell = montepy.Cell() cell.number = 25 # this will actually append ignoring the key given problem.cells[3] = cell print(problem.cells[3] is cell) del problem.cells[25] print(cell not in problem.cells)
This shows:
False True
Slicing a Collection#
Unlike dictionaries this collection also supports slices e.g.,
[1:3]
. This will return a newNumberedObjectCollection
with objects that have numbers that fit that slice.for cell in problem.cells[1:3]: print(cell.number)
Which shows
1 2 3
Because MCNP numbered objects start at 1, so do the indices. The slices are effectively 1-based and endpoint-inclusive. This means rather than the normal behavior of [0:5] excluding the index 5, 5 would be included.
Set-Like Operations#
Changed in version 1.0.0: Introduced set-like behavior.
These collections act like sets. The supported operators are:
&
,|
,-
,^
,<
,<=
,>
,>=
,==
. See the set documentation for how these operators function. The set operations are applied to the object numbers. The corresponding objects are then taken to form a new instance of this collection. The if both collections have objects with the same number but different objects, the left-hand-side’s object is taken.cells1 = montepy.Cells() for i in range(5, 10): cell = montepy.Cell() cell.number = i cells1.add(cell) cells2 = montepy.Cells() for i in range(8, 15): cell = montepy.Cell() cell.number = i cells2.add(cell) overlap = cells1 & cells2 # The only overlapping numbers are 8, 9, 10 print({8, 9} == set(overlap.keys()))
This would print:
True
Other set-like functions are:
difference()
,difference_update()
,intersection()
,isdisjoint()
,issubset()
,issuperset()
,symmetric_difference()
,symmetric_difference_update()
,union()
,discard()
, andupdate()
.- type obj_class:
type
- param obj_class:
the class of numbered objects being collected
- type obj_class:
type
- type objects:
list
- param objects:
the list of cells to start with if needed
- type objects:
list
- type problem:
- param problem:
the problem to link this collection to.
- type problem:
MCNP_Problem
Methods:
add
(obj)Add the given object to this collection.
append
(obj, **kwargs)Appends the given object to the end of this collection.
append_renumber
(obj[, step])Appends the object, but will renumber the object if collision occurs.
check_number
(number)Checks if the number is already in use, and if so raises an error.
clear
()Removes all objects from this collection.
clone
([starting_number, step])Create a new instance of this collection, with all new independent objects with new numbers.
difference
(*others)Return a new collection with elements from collection, that are not in the others.
difference_update
(*others)Update the new collection removing all elements from others.
discard
(obj)Remove the object from the collection if it is present.
extend
(other_list)Extends this collection with another list.
get
(i[, default])Get
i
if possible, or else returndefault
.intersection
(*others)Return a new collection with all elements in common in collection, and all others.
intersection_update
(*others)Update the collection keeping all elements in common in collection, and all others.
isdisjoint
(other)Test if there are no elements in common between the collection, and other.
issubset
(other)Test whether every element in the collection is in other.
issuperset
(other)Test whether every element in other is in the collection.
items
()Get iterator of the collections (number, object) pairs.
keys
()Get iterator of the collection's numbers.
link_to_problem
(problem)Links the card to the parent problem for this card.
next_number
([step])Get the next available number, based on the maximum number.
pop
([pos])Pop the final items off of the collection
remove
(delete)Removes the given object from the collection.
request_number
([start_num, step])Requests a new available number.
symmetric_difference
(other)Return a new collection with elements in either the collection or the other, but not both.
symmetric_difference_update
(other)Update the collection, keeping only elements found in either collection, but not in both.
union
(*others)Return a new collection with all elements from collection, and all others.
update
(*objs)Add the given objects to this collection.
values
()Get iterator of the collection's objects.
Attributes:
A generator of the numbers being used.
Returns a shallow copy of the internal objects list.
The starting number to use when an object is cloned.
The step size to use to find a valid number during cloning.
- add(obj: Numbered_MCNP_Object)#
Add the given object to this collection.
- Parameters:
obj (Numbered_MCNP_Object) – The object to add.
- Raises:
TypeError – if the object is of the wrong type.
NumberConflictError – if this object’s number is already in use in the collection.
- append(obj, **kwargs)#
Appends the given object to the end of this collection.
- Parameters:
obj (Numbered_MCNP_Object) – the object to add.
**kwargs – extra arguments that are used internally.
- Raises:
NumberConflictError – if this object has a number that is already in use.
- append_renumber(obj, step=1)#
Appends the object, but will renumber the object if collision occurs.
This behaves like append, except if there is a number collision the object will be renumbered to an available number. The number will be incremented by step until an available number is found.
- Parameters:
obj (Numbered_MCNP_Object) – The MCNP object being added to the collection.
step (int) – the incrementing step to use to find a new number.
- Returns:
the number for the object.
- Return type:
int
- check_number(number)#
Checks if the number is already in use, and if so raises an error.
- Parameters:
number (int) – The number to check.
- Raises:
NumberConflictError – if this number is in use.
- clear()#
Removes all objects from this collection.
- clone(starting_number=None, step=None)#
Create a new instance of this collection, with all new independent objects with new numbers.
This relies mostly on
copy.deepcopy
.Notes
If starting_number, or step are not specified
starting_number()
, andstep()
are used as default values.Added in version 0.5.0.
- Parameters:
starting_number (int) – The starting number to request for a new object numbers.
step (int) – the step size to use to find a new valid number.
- Returns:
a cloned copy of this object.
- Return type:
type(self)
- difference(*others: Self)#
Return a new collection with elements from collection, that are not in the others.
collection - other - ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- difference_update(*others: Self)#
Update the new collection removing all elements from others.
collection -= other | ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- discard(obj: Numbered_MCNP_Object)#
Remove the object from the collection if it is present.
Added in version 1.0.0.
- Parameters:
obj (Numbered_MCNP_Object) – the object to remove.
- extend(other_list)#
Extends this collection with another list.
- Parameters:
other_list (list) – the list of objects to add.
- Raises:
NumberConflictError – if these items conflict with existing elements.
- get(i, default=None)#
Get
i
if possible, or else returndefault
.- Parameters:
i (int) – number of the object to get, not it’s location in the internal list
default (object) – value to return if not found
- Return type:
- intersection(*others: Self)#
Return a new collection with all elements in common in collection, and all others.
collection & other & ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- intersection_update(*others: Self)#
Update the collection keeping all elements in common in collection, and all others.
collection &= other & ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- isdisjoint(other: Self)#
Test if there are no elements in common between the collection, and other.
Collections are disjoint if and only if their intersection is the empty set.
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- issubset(other: Self)#
Test whether every element in the collection is in other.
collection <= other
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- issuperset(other: Self)#
Test whether every element in other is in the collection.
collection >= other
Added in version 1.0.0.
- Parameters:
other (Self) – the set to compare to.
- Return type:
bool
- items() Generator[Tuple[int, Numbered_MCNP_Object], None, None] #
Get iterator of the collections (number, object) pairs.
- Return type:
tuple(int, MCNP_Object)
- keys() Generator[int, None, None] #
Get iterator of the collection’s numbers.
- Return type:
int
- link_to_problem(problem)#
Links the card to the parent problem for this card.
This is done so that cards can find links to other objects.
- Parameters:
problem (MCNP_Problem) – The problem to link this card to.
- next_number(step=1)#
Get the next available number, based on the maximum number.
This works by finding the current maximum number, and then adding the stepsize to it.
- Parameters:
step (int) – how much to increase the last number by
- pop(pos=-1)#
Pop the final items off of the collection
- Parameters:
pos (int) – The index of the element to pop from the internal list.
- Returns:
the final elements
- Return type:
- remove(delete)#
Removes the given object from the collection.
- Parameters:
delete (Numbered_MCNP_Object) – the object to delete
- request_number(start_num=None, step=None)#
Requests a new available number.
This method does not “reserve” this number. Objects should be immediately added to avoid possible collisions caused by shifting numbers of other objects in the collection.
Notes
If starting_number, or step are not specified
starting_number()
, andstep()
are used as default values.Changed in version 0.5.0: In 0.5.0 the default values were changed to reference
starting_number()
andstep()
.- Parameters:
start_num (int) – the starting number to check.
step (int) – the increment to jump by to find new numbers.
- Returns:
an available number
- Return type:
int
- symmetric_difference(other: Self)#
Return a new collection with elements in either the collection or the other, but not both.
collection ^ other
Added in version 1.0.0.
- Parameters:
others (Self) – the other collections to compare to.
other (Self)
- Return type:
Self
- symmetric_difference_update(other: Self)#
Update the collection, keeping only elements found in either collection, but not in both.
collection ^= other
Added in version 1.0.0.
- Parameters:
others (Self) – the other collections to compare to.
other (Self)
- union(*others: Self)#
Return a new collection with all elements from collection, and all others.
collection | other | ...
Added in version 1.0.0.
- Parameters:
*others (Self) – the other collections to compare to.
- Return type:
Self
- update(*objs: Self)#
Add the given objects to this collection.
Notes
This is not a thread-safe method.
Changed in version 1.0.0: Changed to be more set like. Accepts multiple arguments. If there is a number conflict, the current object will be kept.
- Parameters:
*objs (list[Numbered_MCNP_Object]) – The objects to add.
- Raises:
TypeError – if the object is of the wrong type.
NumberConflictError – if this object’s number is already in use in the collection.
- values() Generator[Numbered_MCNP_Object, None, None] #
Get iterator of the collection’s objects.
- Return type:
- property numbers#
A generator of the numbers being used.
- Return type:
generator
- property objects#
Returns a shallow copy of the internal objects list.
The list object is a new instance, but the underlying objects are the same.
- Return type:
list
- property starting_number#
The starting number to use when an object is cloned.
- Returns:
the starting number
- Return type:
int
- property step#
The step size to use to find a valid number during cloning.
- Returns:
the step size
- Return type:
int
- Parameters:
obj_class (type)
objects (list)
problem (montepy.MCNP_Problem)