snek5000.magic#

IPython magic extensions#

Usage example in an IPython console:

In [1]: %load_ext snek5000.magic

In [2]: %snek abl
INFO     Reading baseline parameters from /home/avmo/src/snek5000/snek5000/src/abl/abl.par
Created Simul class and default parameters for abl -> Simul, params

In [3]: sim = Simul(params)

Functions

load_ipython_extension(ipython)

Load the extension in IPython.

Classes

SnekMagics(**kwargs)

Magic commands can be loaded in IPython or Jupyter as

class snek5000.magic.SnekMagics(**kwargs: Any)[source]#

Magic commands can be loaded in IPython or Jupyter as

>>> %load_ext fluidsim.magic

Examples

  • Magic command %fluidsim or %snek (alias)

%snek creates the variables params and Simul for a particular solver.

Create default parameters for a solver:

>>> %snek phill

If a variable params already exists, you will be ask if you really want to overwrite it. To skip this question:

>>> %snek phill -f

List all available solvers and initialized simulation:

>>> %snek
  • Other fluidsim magic commands

Quick reference (print this help message):

>>> %fluidsim_help

Delete the objects sim and params:

>>> %fluidsim_reset

Todo

  • Magic command %fluidsim_load

%fluidsim_load creates the variables sim, params and Simul from an existing simulation.

Load existing simulation excluding state_phys files:

>>> %fluidsim_load

Load existing simulation all options: force overwrite, with state_phys files, merging parameters:

>>> %fluidsim_load -f -s -t -m
entrypoint_grp = 'snek5000.solvers'#
snek(line)[source]#
%snek [-f] [solver]
positional arguments:

solver

optional arguments:

-f, –force-overwrite

add_traits(**traits)#

Dynamically add trait attributes to the HasTraits instance.

arg_err(func)#

Print docstring if incorrect arguments were passed

classmethod class_config_rst_doc()#

Generate rST documentation for this class’ config options.

Excludes traits defined on parent classes.

classmethod class_config_section(classes=None)#

Get the config section for this class.

Parameters

classes (list, optional) – The list of other classes in the config file. Used to reduce redundant information.

classmethod class_get_help(inst=None)#

Get the help string for this class in ReST format.

If inst is given, its current trait values will be used in place of class defaults.

classmethod class_get_trait_help(trait, inst=None, helptext=None)#

Get the helptext string for a single trait.

Parameters
  • inst – If given, its current trait values will be used in place of the class default.

  • helptext – If not given, uses the help attribute of the current trait.

classmethod class_own_trait_events(name)#

Get a dict of all event handlers defined on this class, not a parent.

Works like event_handlers, except for excluding traits from parents.

classmethod class_own_traits(**metadata)#

Get a dict of all the traitlets defined on this class, not a parent.

Works like class_traits, except for excluding traits from parents.

classmethod class_print_help(inst=None)#

Get the help string for a single trait and print it.

classmethod class_trait_names(**metadata)#

Get a list of all the names of this class’ traits.

This method is just like the trait_names() method, but is unbound.

classmethod class_traits(**metadata)#

Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.

This method is just like the traits() method, but is unbound.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.

config#

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

Subclasses can declare default classes by overriding the klass attribute

property cross_validation_lock#

A contextmanager for running a block with our cross validation lock set to True.

At the end of the block, the lock’s value is restored to its value prior to entering the block.

default_option(fn, optstr)#

Make an entry in the options_table for fn, with value optstr

fluidsim(line)#
%fluidsim [-f] [solver]
positional arguments:

solver

optional arguments:

-f, –force-overwrite

fluidsim_help(line)#
fluidsim_reset(line)#
%fluidsim_reset
format_latex(strng)#

Format a string for latex inclusion.

has_trait(name)#

Returns True if the object has a trait with the specified name.

hold_trait_notifications()#

Context manager for bundling trait change notifications and cross validation.

Use this when doing multiple trait assignments (init, config), to avoid race conditions in trait notifiers requesting other trait values. All trait notifications will fire after all values have been assigned.

is_defined(varname)#
magics = {'cell': {}, 'line': {'fluidsim': 'fluidsim', 'fluidsim_help': 'fluidsim_help', 'fluidsim_reset': 'fluidsim_reset', 'snek': 'snek'}}#
notify_change(change)#

Notify observers of a change event

observe(handler, names=traitlets.All, type='change')#

Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Parameters
  • handler (callable) – A callable that is called when a trait changes. Its signature should be handler(change), where change is a dictionary. The change dictionary at least holds a ‘type’ key. * type: the type of notification. Other keys may be passed depending on the value of ‘type’. In the case where type is ‘change’, we also have the following keys: * owner : the HasTraits instance * old : the old value of the modified trait attribute * new : the new value of the modified trait attribute * name : the name of the modified trait attribute.

  • names (list, str, All) – If names is All, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.

  • type (str, All (default: 'change')) – The type of notification to filter by. If equal to All, then all notifications are passed to the observe handler.

on_trait_change(handler=None, name=None, remove=False)#

DEPRECATED: Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).

If remove is True and handler is not specified, all change handlers for the specified name are uninstalled.

Parameters
  • handler (callable, None) – A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new), handler(name, old, new), or handler(name, old, new, self).

  • name (list, str, None) – If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.

  • remove (bool) – If False (the default), then install the handler. If True then unintall it.

options_table = None#
parent#

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

Subclasses can declare default classes by overriding the klass attribute

parse_options(arg_str, opt_str, *long_opts, **kw)#

Parse options passed to an argument string.

The interface is similar to that of getopt.getopt(), but it returns a Struct with the options as keys and the stripped argument string still as a string.

arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.

Parameters
  • arg_str (str) – The arguments to parse.

  • opt_str (str) – The options specification.

  • mode (str, default 'string') – If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.

  • list_all (bool, default False) – Put all option values in lists. Normally only options appearing more than once are put in a list.

  • posix (bool, default True) – Whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.

registered = True#
classmethod section_names()#

return section names as a list

set_trait(name, value)#

Forcibly sets trait attribute, including read-only attributes.

setup_instance(**kwargs)#

This is called before self.__init__ is called.

shell = None#
trait_defaults(*names, **metadata)#

Return a trait’s default value or a dictionary of them

Notes

Dynamically generated default values may depend on the current state of the object.

classmethod trait_events(name=None)#

Get a dict of all the event handlers of this class.

Parameters

name (str (default: None)) – The name of a trait of this class. If name is None then all the event handlers of this class will be returned instead.

Return type

The event handlers associated with a trait name, or all event handlers.

trait_has_value(name)#

Returns True if the specified trait has a value.

This will return false even if getattr would return a dynamically generated default value. These default values will be recognized as existing only after they have been generated.

Example

class MyClass(HasTraits):
    i = Int()

mc = MyClass()
assert not mc.trait_has_value("i")
mc.i # generates a default value
assert mc.trait_has_value("i")
trait_metadata(traitname, key, default=None)#

Get metadata values for trait by key.

trait_names(**metadata)#

Get a list of all the names of this class’ traits.

trait_values(**metadata)#

A dict of trait names and their values.

The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.

Return type

A dict of trait names and their values.

Notes

Trait values are retrieved via getattr, any exceptions raised by traits or the operations they may trigger will result in the absence of a trait value in the result dict.

traits(**metadata)#

Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.

unobserve(handler, names=traitlets.All, type='change')#

Remove a trait change handler.

This is used to unregister handlers to trait change notifications.

Parameters
  • handler (callable) – The callable called when a trait attribute changes.

  • names (list, str, All (default: All)) – The names of the traits for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all changes.

  • type (str or All (default: 'change')) – The type of notification to filter by. If All, the specified handler is uninstalled from the list of notifiers corresponding to all types.

unobserve_all(name=traitlets.All)#

Remove trait change handlers of any type for the specified name. If name is not specified, removes all trait notifiers.

update_config(config)#

Update config and load the new values

snek5000.magic.load_ipython_extension(ipython)[source]#

Load the extension in IPython.