Common Types

Helpers for creating standardized Type instances. as defined by http://epics-pvdata.sourceforge.net/alpha/normativeTypes/normativeTypes.html .

Automatic Value unwrapping

Automatic transformation can be performed. between Value and more convenient types.

Transformation may be performed at the following points:

Controlling (Un)wrapping

Client p4p.client.thread.Context accepts an argument nt= which may be None to sure some reasonable defaults. False disables wrapping, and always works with Value. nt= may also be passed a dictionary keyed by top level structure IDs mapped to callables returning objects conforming to WrapperInterface.

The unwrap argument is legacy which functions like nt= but mapping to plain functions instead of wrapper objects.

from p4p.client.thread import Context
ctxt=Context('pva', nt=False) # disable (un)wrap.  All methods use Value

Server p4p.server.thread.SharedPV accepts an argument nt= which is an instance of an object conforming to WrapperInterface.

from p4p.server.thread import SharedPV
from p4p.nt import NTScalar
pv1 = SharedPV() # pv1.open() expects a Value
pv2 = SharedPV(nt=NTScalar('d'))
pv2.open(4.2) # NTScalar automatically wraps this float into a Value

Conforming objects include NTScalar, NTNDArray, and others listed below.

p4p.nt.defaultNT()[source]

Returns a copy of the default NT helper mappings.

Since

3.1.0

NT wrap/unwrap interface

class p4p.nt.WrapperInterface
Since

3.1.0

classmethod buildtype()

Returns a Type based on some helper specific conditions.

Return type

Type

__init__()

Each time the type ID of a Channel changes, a new wrapper will be instantiated if available.

unwrap(Value) → object

Called with a Value and may return an arbitrary object.

Called by both clients and servers. eg. during p4p.client.thread.Context.get() and p4p.server.thread.SharedPV.current().

wrap(object) → Value

Called with an arbitrary object which it should try to translate into a Value.

Called by servers. eg. during p4p.server.thread.SharedPV.post().

assign(Value, object)

Called to update a Value based on an arbitrary object.

Called by clients. eg. during p4p.client.thread.Context.put(), where the get= argument effects the state of the Value passed in.

API Reference

class p4p.nt.NTScalar(valtype='d', **kws)[source]

Describes a single scalar or array of scalar values and associated meta-data

>>> stype = NTScalar('d') # scalar double
>>> V = stype.wrap(4.2)
>>> assert isinstance(V, Value)
>>> stype = NTScalar.buildType('ad') # vector double
>>> V = Value(stype, {'value': [4.2, 4.3]})

The result of wrap() is an augmented value object combining ntwrappercommon and a python value type (str, int, float, numpy.ndarray).

Agumented values have some additional attributes including:

  • .timestamp - The update timestamp is a float representing seconds since 1 jan 1970 UTC.

  • .raw_stamp - A tuple of (seconds, nanoseconds)

  • .severity - An integer in the range [0, 3]

  • .raw - The complete underlying Value

static buildType(valtype, extra=[], display=False, control=False, valueAlarm=False)[source]

Build a Type

Parameters
  • valtype (str) – A type code to be used with the ‘value’ field. See Type definitions

  • extra (list) – A list of tuples describing additional non-standard fields

  • display (bool) – Include optional fields for display meta-data

  • control (bool) – Include optional fields for control meta-data

  • valueAlarm (bool) – Include optional fields for alarm level meta-data

Returns

A Type

wrap(value, timestamp=None)[source]

Pack python value into Value

Accepts dict to explicitly initialize fields by name. Any other type is assigned to the ‘value’ field.

assign(V, py)[source]

Store python value in Value

classmethod unwrap(value)[source]

Unpack a Value into an augmented python type (selected from the ‘value’ field)

class p4p.nt.NTNDArray(**kws)[source]

Representation of an N-dimensional array with meta-data

Translates into ntndarray

static buildType(extra=[])[source]

Build type

wrap(value)[source]

Wrap numpy.ndarray as Value

assign(V, py)[source]

Store python value in Value

classmethod unwrap(value)[source]

Unwrap Value as NTNDArray

class p4p.nt.NTTable(columns=[], extra=[])[source]

A generic table

>>> table = NTTable.buildType(columns=[
    ('columnA', 'ai'),
    ('columnB', 'as'),
])
static buildType(columns=[], extra=[])[source]

Build a table

Parameters
  • columns (list) – List of column names and types. eg [(‘colA’, ‘d’)]

  • extra (list) – A list of tuples describing additional non-standard fields

Returns

A Type

wrap(values)[source]

Pack an iterable of dict into a Value

>>> T=NTTable([('A', 'ai'), ('B', 'as')])
>>> V = T.wrap([
    {'A':42, 'B':'one'},
    {'A':43, 'B':'two'},
])
static unwrap(value)[source]

Iterate an NTTable

Returns

An iterator yielding an OrderedDict for each column

class p4p.nt.NTURI(args)[source]
static buildType(args)[source]

Build NTURI

Parameters

args (list) – A list of tuples of query argument name and PVD type code.

>>> I = NTURI([
    ('arg_a', 'I'),
    ('arg_two', 's'),
])
wrap(path, args=(), kws={}, scheme='', authority='')[source]

Wrap argument values (tuple/list with optional dict) into Value

Parameters
  • path (str) – The PV name to which this call is made

  • args (tuple) – Ordered arguments

  • kws (dict) – Keyword arguments

Return type

Value

class p4p.nt.NTMultiChannel[source]

Describes a structure holding the equivalent of a number of NTScalar

static buildType(valtype, extra=[])[source]

Build a Type

Parameters
  • valtype (str) – A type code to be used with the ‘value’ field. Must be an array

  • extra (list) – A list of tuples describing additional non-standard fields

Returns

A Type

class p4p.nt.scalar.ntfloat(x=0, /)[source]

Augmented float with additional attributes

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.

class p4p.nt.scalar.ntint[source]

Augmented integer with additional attributes

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.

class p4p.nt.scalar.ntstr[source]

Augmented string with additional attributes

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.

class p4p.nt.scalar.ntnumericarray[source]

Augmented numpy.ndarray with additional attributes

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.

class p4p.nt.scalar.ntstringarray(iterable=(), /)[source]

Augmented list of strings with additional attributes

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.

class p4p.nt.ndarray.ntndarray[source]

Augmented numpy.ndarray with additional attributes

  • .attrib - dictionary

  • .severity

  • .status

  • .timestamp - Seconds since 1 Jan 1970 UTC as a float

  • .raw_stamp - A tuple (seconds, nanoseconds)

  • .raw - The underlying p4p.Value.