pushgp.push.interpreter¶
- class pushgp.push.interpreter.Push[source]¶
Bases: collections.defaultdict
The Push interpreter.
The different stack types are available as dictionary keys and values. For example:
>>> p = Push() >>> p['exec'] = [1, 2] >>> p Push: {'exec': [1, 2]}
Each stack is implemented as a List, so any method that work on lists will work on the stacks. For example, to pop an item from a stack use the .pop() method.
>>> p = Push() >>> p['exec'] = [1, 2] >>> p['exec'].pop() 2 >>> p Push: {'exec': [1]}
As you can see, the top of stack is the last item in the list.
To execute the exec stack, call execute() on an instance.
>>> p = Push() >>> p['exec'] = [1, 2] >>> p.execute() >>> p Push: {'int': [2, 1], 'exec': []}
To push an item to the exec stack and then execute it, just call the initiated object on any number of items.
>>> Push()(1, 2) Push: {'exec': [], 'int': [2, 1]}
You can even chane these calls together.
>>> Push()(1)(2) Push: {'exec': [], 'int': [1, 2]}
- __call__(*items)[source]¶
Pushes a list of items onto the exec stack and executes them. Uses the ._execute() method to run through the exec stack.
- __dict__ = dict_proxy({'__module__': 'pushgp.push.interpreter', '__str__': <function __str__ at 0x32e19b0>, 'execute': <function execute at 0x32e1b18>, '__dict__': <attribute '__dict__' of 'Push' objects>, 'stack_for_item': <function stack_for_item at 0x32e1aa0>, '__repr__': <function __repr__ at 0x32e1a28>, '__call__': <function __call__ at 0x32e1b90>, '__weakref__': <attribute '__weakref__' of 'Push' objects>, '__doc__': "\n The Push interpreter.\n\n The different stack types are available as dictionary keys and values.\n For example:\n\n >>> p = Push()\n >>> p['exec'] = [1, 2]\n >>> p\n Push: {'exec': [1, 2]}\n\n Each stack is implemented as a List, so any method that work on lists will\n work on the stacks. For example, to pop an item from a stack use the\n ``.pop()`` method.\n\n >>> p = Push()\n >>> p['exec'] = [1, 2]\n >>> p['exec'].pop()\n 2\n >>> p\n Push: {'exec': [1]}\n\n As you can see, the top of stack is the last item in the list.\n\n To execute the ``exec`` stack, call :py:meth:`~.execute` on an instance.\n\n >>> p = Push()\n >>> p['exec'] = [1, 2]\n >>> p.execute()\n >>> p\n Push: {'int': [2, 1], 'exec': []}\n\n To push an item to the ``exec`` stack and then execute it, just call\n the initiated object on any number of items.\n\n >>> Push()(1, 2)\n Push: {'exec': [], 'int': [2, 1]}\n\n You can even chane these calls together.\n\n >>> Push()(1)(2)\n Push: {'exec': [], 'int': [1, 2]}\n ", '__init__': <function __init__ at 0x32e1938>})¶
- __module__ = 'pushgp.push.interpreter'¶
- execute()[source]¶
Pops each item off the exec stack until it is empty, using the .pop() method.
If the item is in instance in the stack_types attribute then it will be pushed to the corresponding stack.
Else if it a callable is will be called with the Push object and should return a modified Push object.
Else it will raise a TypeError