File: quantity.py

package info (click to toggle)
python-casacore 3.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,488 kB
  • sloc: python: 4,170; cpp: 1,549; makefile: 67
file content (70 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from ._quanta import QuantVec
from ._quanta import Quantity
from ._quanta import from_string, from_dict, from_dict_v


def is_quantity(q):
    """Indicate whether the object is a valid quantity"""
    return isinstance(q, QuantVec) or isinstance(q, Quantity)


# Quantity returns new Quantities, so we need to insert these
# functions into Quantity
def new_get_value(quant, *args):
    val = QuantVec._get_value(quant, *args)
    if len(val) == 1:
        return val[0]
    else:
        return val


QuantVec.get_value = new_get_value


def to_string(quant, fmt="%0.5g"):
    val = quant.get_value()
    if hasattr(val, "__len__"):
        fmt = "[" + ", ".join([fmt % i for i in val]) + "] %s"
        return fmt % quant.get_unit()
    fmt += " %s"
    return fmt % (val, quant.get_unit())


QuantVec.to_string = to_string
Quantity.to_string = to_string
QuantVec.__str__ = to_string
Quantity.__str__ = to_string


# QuantVec.__repr__ = to_string
# Quantity.__repr__ = to_string


def quantity(*args):
    """Create a quantity. This can be from a scalar or vector.

    Example::

      q1 = quantity(1.0, "km/s")
      q2 = quantity("1km/s")
      q1 = quantity([1.0,2.0], "km/s")

    """
    if len(args) == 1:
        if isinstance(args[0], str):
            # use copy constructor to create quantity from string
            return Quantity(from_string(args[0]))
        elif isinstance(args[0], dict):
            if hasattr(args[0]["value"], "__len__"):
                return QuantVec(from_dict_v(args[0]))
            else:
                return Quantity(from_dict(args[0]))
        elif isinstance(args[0], Quantity) or isinstance(args[0], QuantVec):
            return args[0]
        else:
            raise TypeError("Invalid argument type for")
    else:
        if hasattr(args[0], "__len__"):
            return QuantVec(*args)
        else:
            return Quantity(*args)