##// END OF EJS Templates
Merge pull request #1893 from minrk/compositeerr...
Merge pull request #1893 from minrk/compositeerr Update Parallel Magics and Exception Display Based on feedback from @fperez, a few small changes to parallel exception handling and magics: Exception changes: * apply_requests trigger showtraceback machinery, so apply errors are as pretty as execute ones * InteractiveShell.showtraceback handles RemoteErrors, so it only draws the remote traceback, rather than the unhelpful local one. Magics changes: * removed parallelmagic extension * creating a Client *implies* activate of a lazily-evaluated directview on all engines * can activate Magics on multiple views with different suffixes: ```python eall = rc.activate('all', 'all') e0 = rc.activate(0, '0') %pxall a=5 %px0 print a ``` * add %pxconfig magic for changing default block/targets for a collection of magics * add targets arg to %%px cell magic * %result renamed to %pxresult for consistency (%result kept for bw compat) * %pxresult now only draws most recent result, but accepts all the output-formatting args of %%px * add --out arg to %%px for storing the AsyncResult object in the user_ns * changed %px to not be verbose by default, and added verbosity control to %pxconfig.

File last commit:

r5390:c82649ea
r7503:60e66298 merge
Show More
PhysicalQInteractive.py
90 lines | 3.3 KiB | text/x-python | PythonLexer
/ IPython / deathrow / PhysicalQInteractive.py
# -*- coding: utf-8 -*-
"""Modify the PhysicalQuantities class for more convenient interactive use.
Also redefine some math functions to operate on PhysQties with no need for
special method syntax. This just means moving them out to the global
namespace.
This module should always be loaded *after* math or Numeric, so it can
overwrite math functions with the versions that handle units.
Authors
-------
- Fernando Perez <Fernando.Perez@berkeley.edu>
"""
#*****************************************************************************
# Copyright (C) 2008-2011 The IPython Development Team
# Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
from Scientific.Physics.PhysicalQuantities import PhysicalQuantity
# This code can be set up to work with Numeric or with math for providing the
# mathematical functions. Uncomment the one you prefer to use below.
# If you use math, sin(x) won't work for x an array, only float or PhysQty
import math
# If you use Numeric, sin(x) works for x a float, PhysQty an array.
#import Numeric as math
class PhysicalQuantityFunction:
"""Generic function wrapper for PhysicalQuantity instances.
Calls functions from either the math library or the instance's methods as
required. Allows using sin(theta) or sqrt(v**2) syntax irrespective of
whether theta is a pure number or a PhysicalQuantity.
This is *slow*. It's meant for convenient interactive use, not for
speed."""
def __init__(self,name):
self.name = name
def __call__(self,x):
if isinstance(x,PhysicalQuantity):
return PhysicalQuantity.__dict__[self.name](x)
else:
return math.__dict__[self.name](x)
class PhysicalQuantityInteractive(PhysicalQuantity):
"""Physical quantity with units - modified for Interactive use.
Basically, the __str__ and __repr__ methods have been swapped for more
convenient interactive use. Powers are shown as ^ instead of ** and only 4
significant figures are shown.
Also adds the following aliases for commonly used methods:
b = PhysicalQuantity.inBaseUnits
u = PhysicalQuantity.inUnitsOf
These are useful when doing a lot of interactive calculations.
"""
# shorthands for the most useful unit conversions
b = PhysicalQuantity.inBaseUnits # so you can just type x.b to get base units
u = PhysicalQuantity.inUnitsOf
# This can be done, but it can get dangerous when coupled with IPython's
# auto-calling. Everything ends up shown in baseunits and things like x*2
# get automatically converted to k(*2), which doesn't work.
# Probably not a good idea in general...
#__call__ = b
def __str__(self):
return PhysicalQuantity.__repr__(self)
def __repr__(self):
value = '%.4G' % self.value
units = self.unit.name().replace('**','^')
return value + ' ' + units
# implement the methods defined in PhysicalQuantity as PhysicalQuantityFunctions
sin = PhysicalQuantityFunction('sin')
cos = PhysicalQuantityFunction('cos')
tan = PhysicalQuantityFunction('tan')
sqrt = PhysicalQuantityFunction('sqrt')