Beispiel #1
0
 def __init__(self, name, category, **kwargs):
     if category not in madxcategories:
         raise ValueError("Not a valid MADX element type")
     self.name = str(name)
     self.category = str(category)
     self.length = 0.0  #for bookeeping only
     self['name'] = self.name
     self['category'] = self.category
     self._keysextra = []
     for key, value in kwargs.iteritems():
         if type(value) == tuple and category != 'multipole':
             #use a tuple for (value,units)
             self[key] = (_Decimal(str(value[0])), value[1])
         elif type(value) == tuple and category == 'multipole':
             self[key] = value
         elif _IsFloat(value):
             #just a number
             self[key] = _Decimal(str(value))
         else:
             #must be a string
             self[key] = '"' + value + '"'
         self._keysextra.append(str(key))  #order preserving
     if 'l' in self:
         if type(self['l']) == tuple:
             ll = self['l'][0]
         else:
             ll = self['l']
         self.length += float(ll)
Beispiel #2
0
def _create_decimal(value):
    """Create a decimal from the passed value. This is a decimal that
       has 6 decimal places and is clamped between 0 <= value < 1 quadrillion

       Args:
            value: Value to convert to Decimal
       Returns:
            Decimal: Decimal with value
    """
    from decimal import Decimal as _Decimal

    try:
        d = _Decimal("%.6f" % value, _getcontext())
    except:
        value = _Decimal(value, _getcontext())
        d = _Decimal("%.6f" % value, _getcontext())

    if d < 0:
        from Acquire.Accounting import TransactionError
        raise TransactionError(
            "You cannot create a transaction with a negative value (%s)" %
            (value))

    elif d >= 1000000000000000:
        from Acquire.Accounting import TransactionError
        raise TransactionError(
            "You cannot create a transaction with a value greater than "
            "1 quadrillion! (%s)" % (value))

    return d
Beispiel #3
0
    def atX(self, x):
        indent = "      AtX:"

        if not self.crossesX(x):
            # raise ValueError
            return None
        if self.xy_min.x == x:
            return self.xy_min
        if self.xy_max.x == x:
            return self.xy_max

        msg = ""
        msg += "%s finding point at x=%f\n" % (indent, x)

        dx = self.dxAtX(x)
        if self.xmin + dx != x:
            raise ValueError("%s != %s" % (self.xmin + dx, x))
        msg += "%s FINAL X: %f\n" % (indent, self.xmin + dx)

        pdx = (dx) / self.xrange
        msg += "%s %s x=%f (%f %%)\n" % (indent, self, x, pdx * 100)

        yest = self.ymin + (self.yrange * pdz)
        zest = self.zmin + (self.zrange * pdx)

        dz = self.dzAtX(x)
        z = self.zmin + _Decimal(dz)

        msg += "%s dz = %f\n" % (indent, dz)
        msg += "%s FINAL Z: %s\n" % (indent, z)

        dy = self.dyAtX(x)
        y = self.ymin + _Decimal(dy)

        msg += "%s dy = %s\n" % (indent, dy)
        msg += "%s FINAL Y: %s\n" % (indent, y)

        def errbar(y, z):
            return 100 - (100 / x * y)

        if abs(errbar(zest, z)) > 0.0001 or abs(errbar(yest, y)) > 0.0001:
            msg += "%s estimates: z=%s y=%s" % (indent, zest, yest)
            print("%s" % (msg, ))
        if abs(errbar(zest, z)) > 0.0001:
            print("%s z error is %s pct" % (indent, errbar(zest, z)))

        if abs(errbar(yest, y)) > 0.0001:
            print("%s y error is %s pct" % (indent, errbar(yest, y)))

        if not _inside(x, self.xmin, self.xmax):
            raise ValueError("%s is not in range %s..%s" %
                             (x, self.xmin, self.xmax))
        if not _inside(y, self.ymin, self.ymax):
            raise ValueError("%s is not in range %s..%s" %
                             (x, self.ymin, self.ymax))
        if not _inside(z, self.zmin, self.zmax):
            raise ValueError("%s is not in range %s..%s" %
                             (x, self.zmin, self.zmax))
        return Point(x, y, z)
Beispiel #4
0
    def test_index(self):
        class RomTestIndexedModel(Model):
            attr = Text(index=True)
            attr2 = Text(index=True)
            attr3 = Integer(index=True)
            attr4 = Float(index=True)
            attr5 = Decimal(index=True)

        x = RomTestIndexedModel(
            attr="hello world", attr2="how are you doing?", attr3=7, attr4=4.5, attr5=_Decimal("2.643")
        )
        x.save()
        RomTestIndexedModel(attr="world", attr3=100, attr4=-1000, attr5=_Decimal("2.643")).save()

        self.assertEqual(RomTestIndexedModel.query.filter(attr="hello").count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr2="how").filter(attr2="are").count(), 1)
        self.assertEqual(
            RomTestIndexedModel.query.filter(attr="hello").filter(attr2="how").filter(attr2="are").count(), 1
        )
        self.assertEqual(
            RomTestIndexedModel.query.filter(attr="hello", noattr="bad")
            .filter(attr2="how")
            .filter(attr2="are")
            .count(),
            0,
        )
        self.assertEqual(RomTestIndexedModel.query.filter(attr="hello", attr3=(None, None)).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr="hello", attr3=(None, 10)).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr="hello", attr3=(None, 10)).execute()[0].id, 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr="hello", attr3=(5, None)).count(), 1)
        self.assertEqual(
            RomTestIndexedModel.query.filter(attr="hello", attr3=(5, 10), attr4=(4, 5), attr5=(2.5, 2.7)).count(), 1
        )
        first = RomTestIndexedModel.query.filter(attr="hello", attr3=(5, 10), attr4=(4, 5), attr5=(2.5, 2.7)).first()
        self.assertTrue(first)
        self.assertTrue(first is x)
        self.assertEqual(
            RomTestIndexedModel.query.filter(attr="hello", attr3=(10, 20), attr4=(4, 5), attr5=(2.5, 2.7)).count(), 0
        )
        self.assertEqual(RomTestIndexedModel.query.filter(attr3=100).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr="world", attr5=_Decimal("2.643")).count(), 2)

        results = RomTestIndexedModel.query.filter(attr="world").order_by("attr4").execute()
        self.assertEqual([x.id for x in results], [2, 1])

        for i in range(50):
            RomTestIndexedModel(attr3=i)
        session.commit()
        session.rollback()

        self.assertEqual(len(RomTestIndexedModel.get_by(attr3=(10, 25))), 16)
        self.assertEqual(len(RomTestIndexedModel.get_by(attr3=(10, 25), _limit=(0, 5))), 5)

        key = RomTestIndexedModel.query.filter(attr="hello").filter(attr2="how").filter(attr2="are").cached_result(30)
        conn = connect(None)
        self.assertTrue(conn.ttl(key) <= 30)
        self.assertEqual(conn.zcard(key), 1)
        conn.delete(key)
Beispiel #5
0
def clean(val, quant=None):
    try:
        val = _Decimal(val)
    except:
        raise
    val = val.normalize()
    if quant:
        val = val.quantize(_Decimal(quant))
    return val
Beispiel #6
0
def clean(val, quant=None):
    try:
        val = _Decimal(val)
    except:
        raise
    val = val.normalize()
    if quant:
        val = val.quantize(_Decimal(quant))
    return val
Beispiel #7
0
    def atX(self, x):
        indent = "      AtX:"

        if not self.crossesX(x):
            # raise ValueError
            return None
        if self.xy_min.x == x:
            return self.xy_min
        if self.xy_max.x == x:
            return self.xy_max

        msg = ""
        msg += "%s finding point at x=%f\n" % (indent, x)

        dx = self.dxAtX(x)
        if self.xmin + dx != x:
            raise ValueError("%s != %s" % (self.xmin + dx, x))
        msg += "%s FINAL X: %f\n" % (indent, self.xmin + dx)

        pdx = (dx) / self.xrange
        msg += "%s %s x=%f (%f %%)\n" % (indent, self, x, pdx*100)

        yest = self.ymin + (self.yrange * pdz)
        zest = self.zmin + (self.zrange * pdx)

        dz = self.dzAtX(x)
        z = self.zmin + _Decimal(dz)

        msg += "%s dz = %f\n" % (indent, dz)
        msg += "%s FINAL Z: %s\n" % (indent, z)

        dy = self.dyAtX(x)
        y = self.ymin + _Decimal(dy)

        msg += "%s dy = %s\n" % (indent, dy)
        msg += "%s FINAL Y: %s\n" % (indent, y)

        def errbar(y, z):
            return 100 - (100 / x * y)

        if abs(errbar(zest, z)) > 0.0001 or abs(errbar(yest, y)) > 0.0001:
            msg += "%s estimates: z=%s y=%s" % (indent, zest, yest)
            print("%s" % (msg,))
        if abs(errbar(zest, z)) > 0.0001:
            print("%s z error is %s pct" % (indent, errbar(zest, z)))

        if abs(errbar(yest, y)) > 0.0001:
            print("%s y error is %s pct" % (indent, errbar(yest, y)))

        if not _inside(x, self.xmin, self.xmax):
            raise ValueError("%s is not in range %s..%s" % (x, self.xmin, self.xmax))
        if not _inside(y, self.ymin, self.ymax):
            raise ValueError("%s is not in range %s..%s" % (x, self.ymin, self.ymax))
        if not _inside(z, self.zmin, self.zmax):
            raise ValueError("%s is not in range %s..%s" % (x, self.zmin, self.zmax))
        return Point(x, y, z)
Beispiel #8
0
 def validate(self, value):
     from decimal import Decimal as _Decimal
     from decimal import InvalidOperation
     val = super(Decimal, self).validate(value)
     if val is None:
         return
     try:
         return _Decimal(repr(val)) if isinstance(val, float) else _Decimal(val)
     except InvalidOperation:
         raise ValidationError("{0} '{1}' can't be coerced to decimal".format(self.column_name, val))
Beispiel #9
0
 def validate(self, value):
     from decimal import Decimal as _Decimal
     from decimal import InvalidOperation
     val = super(Decimal, self).validate(value)
     if val is None:
         return
     try:
         return _Decimal(repr(val)) if isinstance(val, float) else _Decimal(val)
     except InvalidOperation:
         raise ValidationError("{0} '{1}' can't be coerced to decimal".format(self.column_name, val))
Beispiel #10
0
def _inside(val, l, r):
    val = Decimal(val)
    if l < r:
        minimum = l
        maximum = r
    else:
        minimum = r
        maximum = l
    if val < minimum - _Decimal(0.01) or val > maximum + _Decimal(0.01):
        return False
    return True
Beispiel #11
0
 def middle(self):
     x = self.xmin + (self.xmax - self.xmin) / _Decimal(2.0)
     x = Decimal(x, quant=self.quant)
     y = self.ymin + (self.ymax - self.ymin) / _Decimal(2.0)
     y = Decimal(y, quant=self.quant)
     try:
         z = self.zmin + (self.zmax - self.zmin) / _Decimal(2.0)
         z = Decimal(z, quant=self.quant)
         return XY(x, y, z)
     except:
         return XY(x, y)
Beispiel #12
0
 def middle(self):
     x = self.xmin + (self.xmax - self.xmin) / _Decimal(2.0)
     x = Decimal(x, quant=self.quant)
     y = self.ymin + (self.ymax - self.ymin) / _Decimal(2.0)
     y = Decimal(y, quant=self.quant)
     try:
         z = self.zmin + (self.zmax - self.zmin) / _Decimal(2.0)
         z = Decimal(z, quant=self.quant)
         return XY(x,y,z)
     except:
         return XY(x,y)
Beispiel #13
0
def _inside(val, l, r):
    val = Decimal(val)
    if l < r:
        minimum = l
        maximum = r
    else:
        minimum = r
        maximum = l
    if val < minimum - _Decimal(0.01) or val > maximum + _Decimal(0.01):
        return False
    return True
Beispiel #14
0
    def __str__(self):
        fmt = "XYZ("
        if int(self.x) == _Decimal(self.x):
            fmt += "%d,"
        else:
            fmt += "%s,"
        if int(self.y) == _Decimal(self.y):
            fmt += "%d,"
        else:
            fmt += "%s,"
        if int(self.z) == _Decimal(self.z):
            fmt += "%d)"
        else:
            fmt += "%s)"

        return fmt % (self.x, self.y, self.z)
Beispiel #15
0
def _d(val):
    '''Return a _Decimal object.'''

    if isinstance(val, _Decimal):
        return val
    elif type(val) in (types.IntType, types.LongType):
        return _Decimal(val)
    elif isinstance(val, types.StringType):
        d = _Decimal(val)
        return d.normalize()
    elif isinstance(val, types.FloatType) or hasattr(val, '__float__'):
        s = '%.18e' % float(val)
        d = _Decimal(s)
        return d.normalize()
    else:
        return None
Beispiel #16
0
    def __str__(self):
        fmt = "XYZ("
        if int(self.x) == _Decimal(self.x):
            fmt += "%d,"
        else:
            fmt += "%s,"
        if int(self.y) == _Decimal(self.y):
            fmt += "%d,"
        else:
            fmt += "%s,"
        if int(self.z) == _Decimal(self.z):
            fmt += "%d)"
        else:
            fmt += "%s)"

        return fmt % (self.x, self.y, self.z)
Beispiel #17
0
 def serialize(dec):
     if isinstance(dec, str):
         dec = _Decimal(dec)
     assert isinstance(
         dec,
         _Decimal), 'Received not compatible Decimal "{}"'.format(repr(dec))
     return str(dec)
Beispiel #18
0
def _try_decimal(val):
    try:
        val = _Decimal(val)
        val = float(val)
    except (ValueError, _invalidOp):
        pass
    return val
Beispiel #19
0
def _d(val):
    '''Return a _Decimal object.'''

    if isinstance(val, _Decimal):
        return val
    elif type(val) in (int, int):
        return _Decimal(val)
    elif isinstance(val, bytes):
        d = _Decimal(val)
        return d.normalize()
    elif isinstance(val, float) or hasattr(val, '__float__'):
        s = '%.18e' % float(val)
        d = _Decimal(s)
        return d.normalize()
    else:
        return None
def _d(val):
    '''Return a _Decimal object.'''

    if isinstance(val, _Decimal):
        return val
    elif type(val) in (int, int):
        return _Decimal(val)
    elif type(val) == str:
        d = _Decimal(val)
        return d.normalize()
    elif type(val) is float or hasattr(val, '__float__'):
        s = '%.18e' % float(val)
        d = _Decimal(s)
        return d.normalize()
    else:
        return None
def _d(val):
    '''Return a _Decimal object.'''

    if isinstance(val, _Decimal):
        return val
    elif type(val) in (types.IntType, types.LongType):
        return _Decimal(val)
    elif type(val) == types.StringType:
        d = _Decimal(val)
        return d.normalize()
    elif type(val) is types.FloatType or hasattr(val, '__float__'):
        s = '%.18e' % float(val)
        d = _Decimal(s)
        return d.normalize()
    else:
        return None
 def __str__(self):
     if not self:
         return '0'
     s = self.quantize(PLACE8)
     s = '%.8f'%_Decimal(s)
     if '.' in s:
         s = s.rstrip('0').rstrip('.')
     return s
Beispiel #23
0
 def __str__(self):
     if not self:
         return '0'
     s = self.quantize(PLACE8)
     s = '%.8f' % _Decimal(s)
     if '.' in s:
         s = s.rstrip('0').rstrip('.')
     return s
Beispiel #24
0
 def get_grbl_params(self):
     self.enqueue("$#")
     found_bracket = False
     while True:
         response = self._getline()
         if response is True:
             continue
         if response.startswith("["):
             found_bracket = True
             response = response[1:-1]
             if response.startswith("TLO:"):
                 self.grbl_params['TLO'] = _Decimal(response[4:])
             elif response.startswith("PRB:"):
                 tmp, dunno = response[4:].split(':')
                 xyz = tmp.split(',')
                 pos = Position(name=response[:3],
                                x=_Decimal(xyz[0]),
                                y=_Decimal(xyz[1]),
                                z=_Decimal(xyz[2]))
                 self.grbl_params[response[:3]] = {
                     "Pos": pos,
                     "Dunno": dunno
                 }
             else:
                 xyz = response[4:].split(',')
                 pos = Position(name=response[:3],
                                x=_Decimal(xyz[0]),
                                y=_Decimal(xyz[1]),
                                z=_Decimal(xyz[2]))
                 self.grbl_params[response[:3]] = pos
         else:
             self._handle_response(response)
             break
Beispiel #25
0
 def validate(self, value):
     from decimal import Decimal as _Decimal
     from decimal import InvalidOperation
     val = super(Decimal, self).validate(value)
     if val is None: return
     try:
         return _Decimal(val)
     except InvalidOperation:
         raise ValidationError("'{}' can't be coerced to decimal".format(val))
Beispiel #26
0
    def parse_status(self, line):
        if line.startswith("<") and line.endswith(">"):
            (status, line) = line[1:-1].split(",", maxsplit=1)

            (tosser, line) = line.split(":", maxsplit=1)
            mpos = [0, 0, 0]
            (mpos[0], mpos[1], mpos[2], line) = line.split(",", maxsplit=3)
            self.mpos = Position(name="MPos", x=_Decimal(mpos[0]), y=_Decimal(mpos[1]), z=_Decimal(mpos[2]))

            wpos = [0, 0, 0]
            (tosser, line) = line.split(":", maxsplit=1)
            (wpos[0], wpos[1], wpos[2]) = line.split(",", maxsplit=2)
            self.wpos = Position(name="WPos", x=_Decimal(wpos[0]), y=_Decimal(wpos[1]), z=_Decimal(wpos[2]))

            if self.screen.status_updates:
                self.screen.status(status, self.wpos)
            return status
        return None
Beispiel #27
0
 def validate(self, value):
     from decimal import Decimal as _Decimal
     from decimal import InvalidOperation
     val = super(Decimal, self).validate(value)
     if val is None: return
     try:
         return _Decimal(val)
     except InvalidOperation:
         raise ValidationError(
             "'{}' can't be coerced to decimal".format(val))
Beispiel #28
0
    def from_data(data):
        """Return a newly constructed transaction from the passed dictionary
           that has been decoded from json
        """
        transaction = Transaction()

        if (data and len(data) > 0):
            transaction._value = _create_decimal(_Decimal(data["value"]))
            transaction._description = data["description"]

        return transaction
Beispiel #29
0
    def test_index(self):
        class IndexedModel(Model):
            attr = String(index=True)
            attr2 = String(index=True)
            attr3 = Integer(index=True)
            attr4 = Float(index=True)
            attr5 = Decimal(index=True)

        x = IndexedModel(
            attr='hello world',
            attr2='how are you doing?',
            attr3=7,
            attr4=4.5,
            attr5=_Decimal('2.643'),
        )
        x.save()
        IndexedModel(
            attr='world',
            attr3=100,
            attr4=-1000,
            attr5=_Decimal('2.643'),
        ).save()

        self.assertEquals(IndexedModel.query.filter(attr='hello').count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr2='how').filter(attr2='are').count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello').filter(attr2='how').filter(attr2='are').count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello', noattr='bad').filter(attr2='how').filter(attr2='are').count(), 0)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(None, None)).count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(None, 10)).count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(None, 10)).execute()[0].id, 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(5, None)).count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(5, 10), attr4=(4,5), attr5=(2.5, 2.7)).count(), 1)
        first = IndexedModel.query.filter(attr='hello', attr3=(5, 10), attr4=(4,5), attr5=(2.5, 2.7)).first()
        self.assertTrue(first)
        self.assertTrue(first is x)
        self.assertEquals(IndexedModel.query.filter(attr='hello', attr3=(10, 20), attr4=(4,5), attr5=(2.5, 2.7)).count(), 0)
        self.assertEquals(IndexedModel.query.filter(attr3=100).count(), 1)
        self.assertEquals(IndexedModel.query.filter(attr='world', attr5=_Decimal('2.643')).count(), 2)

        results = IndexedModel.query.filter(attr='world').order_by('attr4').execute()
        self.assertEquals([x.id for x in results], [2,1])
Beispiel #30
0
def parse_scan_info(mdict, scan_name):
    """
    Parses the `Scan` portion of the metadata dictionary (on a Quanta this is
    always `"EScan"`) to get values such as dwell time, field width, and pixel
    size

    Parameters
    ----------
    mdict : dict
        A metadata dictionary as returned by :py:meth:`get_quanta_metadata`
    scan_name : str
        The "scan name" read from the root-level ``Beam`` node of the
        metadata dictionary

    Returns
    -------
    mdict : dict
        The same metadata dictionary with some values added under the
        root-level ``nx_meta`` key
    """
    # Values are in SI units, but we want easy to display, so include the
    # exponential factor that will get us from input unit (such as seconds)
    # to output unit (such as μs -- meaning factor = 6)
    to_parse = [
        ([scan_name, 'Dwell'], ['Pixel Dwell Time (μs)'], 6),
        ([scan_name, 'FrameTime'], ['Total Frame Time (s)'], 0),
        ([scan_name, 'HorFieldsize'], ['Horizontal Field Width (μm)'], 6),
        ([scan_name, 'VerFieldsize'], ['Vertical Field Width (μm)'], 6),
        ([scan_name, 'PixelHeight'], ['Pixel Width (nm)'], 9),
        ([scan_name, 'PixelWidth'], ['Pixel Height (nm)'], 9),
    ]

    for m_in, m_out, factor in to_parse:
        val = _try_get_dict_val(mdict, m_in)
        if val != 'not found' and val != '':
            val = _Decimal(val) * _Decimal(str(10**factor))
            _set_nest_dict_val(
                mdict, ['nx_meta'] + m_out,
                float(val) if isinstance(val, _Decimal) else val)

    return mdict
Beispiel #31
0
def _create_decimal(value):
    """Create a decimal from the passed value. This is a decimal that
       has 6 decimal places and is clamped between 0 <= value < 1 quadrillion
    """
    try:
        d = _Decimal("%.6f" % value, _getcontext())
    except:
        value = _Decimal(value, _getcontext())
        d = _Decimal("%.6f" % value, _getcontext())

    if d < 0:
        raise TransactionError(
            "You cannot create a transaction with a negative value (%s)" %
            (value))

    elif d >= 1000000000000000:
        raise TransactionError(
            "You cannot create a transaction with a value greater than "
            "1 quadrillion! (%s)" % (value))

    return d
Beispiel #32
0
def create_decimal(value):
    """Create a decimal from the passed value. This is a decimal that
       has 6 decimal places and is clamped between
       -1 quadrillion < value < 1 quadrillion
    """
    try:
        d = _Decimal("%.6f" % value, get_decimal_context())
    except:
        value = _Decimal(value, get_decimal_context())
        d = _Decimal("%.6f" % value, get_decimal_context())

    if d <= -1000000000000:
        raise AccountError(
            "You cannot create a balance with a value less than "
            "-1 quadrillion! (%s)" % (value))

    elif d >= 1000000000000000:
        raise AccountError(
            "You cannot create a balance with a value greater than "
            "1 quadrillion! (%s)" % (value))

    return d
Beispiel #33
0
 def get_grbl_params(self):
     self.enqueue("$#")
     found_bracket = False
     while True:
         response = self._getline()
         if response is True:
             continue
         if response.startswith("["):
             found_bracket = True
             response = response[1:-1]
             if response.startswith("TLO:"):
                 self.grbl_params["TLO"] = _Decimal(response[4:])
             elif response.startswith("PRB:"):
                 tmp, dunno = response[4:].split(":")
                 xyz = tmp.split(",")
                 pos = Position(name=response[:3], x=_Decimal(xyz[0]), y=_Decimal(xyz[1]), z=_Decimal(xyz[2]))
                 self.grbl_params[response[:3]] = {"Pos": pos, "Dunno": dunno}
             else:
                 xyz = response[4:].split(",")
                 pos = Position(name=response[:3], x=_Decimal(xyz[0]), y=_Decimal(xyz[1]), z=_Decimal(xyz[2]))
                 self.grbl_params[response[:3]] = pos
         else:
             self._handle_response(response)
             break
Beispiel #34
0
def create_decimal(value, default=0):
    """Create a decimal from the passed value. This is a decimal that
       has 6 decimal places and is clamped between
       -1 quadrillion < value < 1 quadrillion

       Args:
            value: Value to convert to Decimal
        Returns:
            Decimal: Decimal version of value

    """

    from decimal import Decimal as _Decimal

    if value is None:
        return _Decimal(0, get_decimal_context())

    try:
        d = _Decimal("%.6f" % value, get_decimal_context())
    except:
        value = _Decimal(value, get_decimal_context())
        d = _Decimal("%.6f" % value, get_decimal_context())

    if d <= -1000000000000:
        from Acquire.Accounting import AccountError
        raise AccountError(
                "You cannot create a balance with a value less than "
                "-1 quadrillion! (%s)" % (value))

    elif d >= 1000000000000000:
        from Acquire.Accounting import AccountError
        raise AccountError(
                "You cannot create a balance with a value greater than "
                "1 quadrillion! (%s)" % (value))

    return d
Beispiel #35
0
def parse_det_info(mdict, det_name):
    """
    Parses the `Detector` portion of the metadata dictionary from the Quanta to
    get values such as brightness, contrast, signal, etc.

    Parameters
    ----------
    mdict : dict
        A metadata dictionary as returned by :py:meth:`get_quanta_metadata`
    det_name : str
        The "detector name" read from the root-level ``Beam`` node of the
        metadata dictionary

    Returns
    -------
    mdict : dict
        The same metadata dictionary with some values added under the
        root-level ``nx_meta`` key
    """
    to_parse = [([det_name, 'Brightness'], ['Detector Brightness Setting']),
                ([det_name, 'Contrast'], ['Detector Contrast Setting']),
                ([det_name, 'EnhancedContrast'],
                 ['Detector Enhanced Contrast '
                  'Setting']), ([det_name, 'Signal'], ['Detector Signal']),
                ([det_name, 'Grid'], ['Detector Grid Voltage (V)']),
                ([det_name, 'Setting'], ['Detector Setting'])]

    for m_in, m_out in to_parse:
        val = _try_get_dict_val(mdict, m_in)
        if val != 'not found':
            try:
                val = _Decimal(val)
                if m_in == [det_name, 'Setting']:
                    # if "Setting" value is numeric, it's just the Grid
                    # voltage so skip it
                    continue
            except (ValueError, _invalidOp):
                pass
            _set_nest_dict_val(
                mdict, ['nx_meta'] + m_out,
                float(val) if isinstance(val, _Decimal) else val)

    _set_nest_dict_val(mdict, ['nx_meta'] + ['Detector Name'], det_name)

    return mdict
    def __new__(cls, py):
        '''New L{NSDecimal}.

           @param py: The decimal value (C{Decimal}, C{float}, C{int},
                      C{str} or L{NSDecimal}).

           @return: New L{NSDecimal} (L{ObjCInstance}).
        '''
        if isinstance(py, NSDecimal):
            return py

        if None in (cls._IMP, cls._SEL):
            cls._SEL = get_selector('decimalNumberWithString:')
            m = libobjc.class_getClassMethod(cls._Class, cls._SEL)
            m = libobjc.method_getImplementation(m)
            cls._IMP = cast(m, CFUNCTYPE(Id_t, Id_t, SEL_t, Id_t))

        py = _Decimal(py)  # from Decimal, float, int, str
        t = NSStr(py.to_eng_string())  # maintains accuracy
        d = cls._IMP(cast(cls._Class, Id_t), cls._SEL, t)
        t.release()  # PYCHOK expected
        self = super(NSDecimal, cls).__new__(cls, d)
        self._pyDec = py
        return self
Beispiel #37
0
    def __init__(self, required=False, default=NULL, unique=False, index=False, keygen=None):
        self._required = required
        self._default = default
        self._unique = unique
        self._index = index
        self._init = False
        self._model = None
        self._attr = None
        self._keygen = None

        if unique:
            if self._allowed != str and self._allowed != unicode:
                raise ColumnError("Unique columns can only be strings")

        numeric = True
        if index and not isinstance(self, ManyToOne):
            if not any(isinstance(i, self._allowed) for i in (0, 0.0, _Decimal('0'))):
                numeric = False
                if self._allowed not in (str, unicode) and not keygen:
                    raise ColumnError("Non-numeric/string indexed columns must provide keygen argument on creation")

        if index:
            self._keygen = keygen if keygen else (
                _numeric_keygen if numeric else _string_keygen)
Beispiel #38
0
    def __init__(self, required=False, default=NULL, unique=False, index=False, keygen=None):
        self._required = required
        self._default = default
        self._unique = unique
        self._index = index
        self._init = False
        self._model = None
        self._attr = None
        self._keygen = None

        if unique:
            if self._allowed != str and self._allowed != unicode:
                raise ColumnError("Unique columns can only be strings")

        numeric = True
        if index and not isinstance(self, ManyToOne):
            if not any(isinstance(i, self._allowed) for i in (0, 0.0, _Decimal('0'))):
                numeric = False
                if self._allowed not in (str, unicode) and not keygen:
                    raise ColumnError("Non-numeric/string indexed columns must provide keygen argument on creation")

        if index:
            self._keygen = keygen if keygen else (
                _numeric_keygen if numeric else _string_keygen)
Beispiel #39
0
    def parse_status(self, line):
        if line.startswith("<") and line.endswith(">"):
            (status, line) = line[1:-1].split(',', maxsplit=1)

            (tosser, line) = line.split(':', maxsplit=1)
            mpos = [0, 0, 0]
            (mpos[0], mpos[1], mpos[2], line) = line.split(',', maxsplit=3)
            self.mpos = Position(name="MPos",
                                 x=_Decimal(mpos[0]),
                                 y=_Decimal(mpos[1]),
                                 z=_Decimal(mpos[2]))

            wpos = [0, 0, 0]
            (tosser, line) = line.split(':', maxsplit=1)
            (wpos[0], wpos[1], wpos[2]) = line.split(',', maxsplit=2)
            self.wpos = Position(name="WPos",
                                 x=_Decimal(wpos[0]),
                                 y=_Decimal(wpos[1]),
                                 z=_Decimal(wpos[2]))

            if self.screen.status_updates:
                self.screen.status(status, self.wpos)
            return status
        return None
Beispiel #40
0
 def __get__(self, instance, owner=None):
     value = super().__get__(instance, owner)
     if owner:
         return _Decimal(super().__get__(instance, owner))
     # else: owner == None implies we are called from the serializer
     return value
Beispiel #41
0
 def __set__(self, instance, value):
     if isinstance(value, float):
         value = str(value)
     x = _Decimal(
         value)  #  noQA - Just for checking if it is a well formed decimal.
     super().__set__(instance, str(value))
Beispiel #42
0
 def parse_value(value):
     try:
         return _Decimal(value)
     except ValueError:
         return None
Beispiel #43
0
 def serialize(dec):
     if isinstance(dec, str):
         dec = _Decimal(dec)
     if not isinstance(dec, _Decimal):
         raise AssertionError('Received not compatible Decimal "{}"'.format(repr(dec)))
     return str(dec)
Beispiel #44
0
 def _from_redis(self, value):
     return _Decimal(value)
Beispiel #45
0
import json
import warnings

import six

from .exceptions import (ORMError, InvalidOperation, ColumnError,
    MissingColumn, InvalidColumnValue, RestrictError)
from .util import (_numeric_keygen, _string_keygen, _many_to_one_keygen,
    _boolean_keygen, dt2ts, ts2dt, t2ts, ts2t, session, _connect)
from django.contrib.gis.geos import Point as GeoPoint
from doordash.driver.routing.route import Route
from rest_framework.utils.encoders import JSONEncoder

NULL = object()
MODELS = {}
_NUMERIC = (0, 0.0, _Decimal('0'), datetime(1970, 1, 1), date(1970, 1, 1), dtime(0, 0, 0))
USE_LUA = True
NO_ACTION_DEFAULT = object()
SKIP_ON_DELETE = object()
ON_DELETE = ('no action', 'restrict', 'cascade')

def _restrict(entity, attr, refs):
    name = entity.__class__.__name__
    raise RestrictError(
        "Cannot delete entity %s with pk %s, %i foreign references from %s.%s exist"%(
            name, getattr(entity, entity._pkey), len(refs), name, attr))

def _on_delete(ent):
    '''
    This function handles all on_delete semantics defined on OneToMany columns.
 def _nth_root(value, n_root):
     root_value = 1 / float(n_root)
     temp = round(_Decimal(value) ** _Decimal(root_value), 3)
     return temp
#!/usr/bin/env python
# coding:utf-8

from decimal import *
from decimal import Decimal as _Decimal

PLACE11 = 1/Decimal(10**11)
PLACE8 = _Decimal(10) ** -8
PLACE6 = _Decimal(10) ** -6
PLACE3 = _Decimal(10) ** -3
PLACE2 = Decimal('0.01')

class Decimal(_Decimal):
    def __str__(self):
        if not self:
            return '0'
        s = self.quantize(PLACE8)
        s = '%.8f'%_Decimal(s)
        if '.' in s:
            s = s.rstrip('0').rstrip('.')
        return s

    def quantize(self, exp, rounding=None, context=None, watchexp=True):
        s = super(Decimal, self).quantize(exp, rounding, context, watchexp)
        return Decimal(s)

    def __truediv__(self, other, context=None):
        s = super(Decimal, self).__truediv__(other, context)
        return Decimal(s)

    __div__ = __truediv__
Beispiel #48
0
    def get_by(cls, conn, **kwargs):
        '''
        This method offers a simple query method for fetching entities of this
        type via attribute numeric ranges (such columns must be ``indexed``),
        or via ``unique`` columns.

        Some examples::

            user = User.get_by(email_address='*****@*****.**')
            # gets up to 25 users created in the last 24 hours
            users = User.get_by(
                created_at=(time.time()-86400, time.time()),
                _limit=(0, 25))

        If you would like to make queries against multiple columns or with
        multiple criteria, look into the Model.query class property.
        '''
        # handle limits and query requirements
        _limit = kwargs.pop('_limit', ())
        if _limit and len(_limit) != 2:
            raise QueryError("Limit must include both 'offset' and 'count' parameters")
        elif _limit and not all(isinstance(x, (int, long)) for x in _limit):
            raise QueryError("Limit arguments bust both be integers")
        if len(kwargs) != 1:
            raise QueryError("We can only fetch object(s) by exactly one attribute, you provided %s"%(len(kwargs),))

        for attr, value in kwargs.iteritems():
            plain_attr = attr.partition(':')[0]
            if isinstance(value, tuple):
                if len(tuple) != 2:
                    raise QueryError("Range queries must include exactly two endpoints")

            # handle numeric index lookups
            if plain_attr in cls._index:
                col = cls._columns[plain_attr]
                if not any(isinstance(i, col._allowed) for i in (0L, 0, 0.0, _Decimal('0'))):
                    if not isinstance(col, ManyToOne):
                        raise QueryError("Cannot query on a non-numeric index")

                if isinstance(value, tuple):
                    qval0, qval1 = value
                else:
                    qval0 = qval1 = value
                if not isinstance(qval0, (int, long, float, _Decimal)) or not isinstance(qval1, (int, long, float, _Decimal)):
                    raise QueryError("Cannot query on an index without a numeric value")

                qval0 = cls._columns[plain_attr]._to_redis(qval0)
                qval1 = cls._columns[plain_attr]._to_redis(qval1)

                ids = conn.zrangebyscore('%s:%s:idx'%(cls.__name__, attr), qval0, qval1, *_limit)
                if ids:
                    return cls.get(ids)
                return []

            # handle unique index lookups
            elif attr == cls._unique:
                if isinstance(value, tuple):
                    raise QueryError("Cannot query a unique index with a range of values")
                qvalue = cls._columns[attr]._to_redis(value)
                id = conn.hget('%s:%s:uidx'%(cls.__name__, attr), qvalue)
                if not id:
                    return None
                return cls.get(id)
Beispiel #49
0
This module contains general utility functions and shared constants used by other TritonScraper modules.

:copyright: (c) 2010 by Christopher Rebert.
:license: MIT, see :file:`LICENSE.txt` for more details.
"""

from itertools import izip_longest as _izip_longest
from decimal import Decimal as _Decimal
from logging import getLogger as _getLogger

from triton_scraper.config import LOGGER_NAME as _LOGGER_NAME

from lxml.etree import XPath

#: Floating-point Not-a-Number (NaN) value.
NaN = _Decimal('NaN')
#: Floating-point infinity value; i.e. float('infinity')
INFINITY = float('infinity')

# Common XPath component
RELATIVE_PREFIX = "descendant-or-self::node()"

# TritonScraper's logger
LOGGER = _getLogger(_LOGGER_NAME)

# From the itertools cookbook: http://docs.python.org/library/itertools.html#recipes
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return _izip_longest(fillvalue=fillvalue, *args)
Beispiel #50
0
 def pythonvalue(self, value):
     return _Decimal(value)
Beispiel #51
0
import re

from lxml import etree
import six

from . import namespaces as ns
from .compat import basestring
from .lib import iso8601
from .lib.iso8601 import UTC, FixedOffset
from .utils import timezone_offset_to_string
from .xsd_types import XSDDate

logger = logging.getLogger(__name__)
NIL = object()

UNBOUNDED = _Decimal('infinity')


class CallStyle(object):
    DOCUMENT = 'document'
    RPC = 'RPC'


class Use(object):
    OPTIONAL = 'optional'
    REQUIRED = 'required'
    PROHIBITED = 'prohibited'


class Inheritance(object):
    RESTRICTION = 'RESTRICTION'
Beispiel #52
0
    def test_index(self):
        class RomTestIndexedModel(Model):
            attr = Text(index=True, keygen=FULL_TEXT)
            attr2 = Text(index=True, keygen=FULL_TEXT)
            attr3 = Integer(index=True)
            attr4 = Float(index=True)
            attr5 = Decimal(index=True)

        x = RomTestIndexedModel(
            attr='hello world',
            attr2='how are you doing?',
            attr3=7,
            attr4=4.5,
            attr5=_Decimal('2.643'),
        )
        x.save()
        RomTestIndexedModel(
            attr='world',
            attr3=100,
            attr4=-1000,
            attr5=_Decimal('2.643'),
        ).save()

        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello').count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr2='how').filter(attr2='are').count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello').filter(attr2='how').filter(attr2='are').count(), 1)
        self.assertRaises(QueryError, lambda: RomTestIndexedModel.query.filter(attr='hello', noattr='bad'))
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(None, None)).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(None, 10)).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(None, 10)).execute()[0].id, 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(5, None)).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(5, 10), attr4=(4,5), attr5=(2.5, 2.7)).count(), 1)
        first = RomTestIndexedModel.query.filter(attr='hello', attr3=(5, 10), attr4=(4,5), attr5=(2.5, 2.7)).first()
        self.assertTrue(first)
        self.assertTrue(first is x)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='hello', attr3=(10, 20), attr4=(4,5), attr5=(2.5, 2.7)).count(), 0)
        self.assertEqual(RomTestIndexedModel.query.filter(attr3=100).count(), 1)
        self.assertEqual(RomTestIndexedModel.query.filter(attr='world', attr5=_Decimal('2.643')).count(), 2)

        results = RomTestIndexedModel.query.filter(attr='world').order_by('attr4').execute()
        self.assertEqual([x.id for x in results], [2,1])

        for i in range(50):
            RomTestIndexedModel(attr3=i)
        session.commit()
        session.rollback()

        self.assertEqual(len(RomTestIndexedModel.get_by(attr3=(10, 25))), 16)
        self.assertEqual(len(RomTestIndexedModel.get_by(attr3=(10, 25), _limit=(0,5))), 5)

        key = RomTestIndexedModel.query.filter(attr='hello').filter(attr2='how').filter(attr2='are').cached_result(30)
        conn = connect(None)
        self.assertTrue(conn.ttl(key) <= 30)
        self.assertEqual(conn.zcard(key), 1)
        conn.delete(key)
        self.assertRaises(QueryError, lambda: RomTestIndexedModel.query.order_by('attr6'))
        if not util.USE_LUA:
            # Only the first call raises the warning, so only bother to call it
            # for our first pass through tests (non-Lua case).
            with warnings.catch_warnings(record=True) as w:
                RomTestIndexedModel.query.order_by('attr')
                self.assertEqual(len(w), 1)
Beispiel #53
0
    def atZ(self, z):
        indent = "      AtZ:"

        if not self.crossesZ(z):
            # raise ValueError
            return None
        if self.xy_min.z == z:
            return self.xy_min
        if self.xy_max.z == z:
            return self.xy_max

        msg = ""
        msg += "%s finding point at z=%f\n" % (indent, z)

        dz = self.dzAtZ(z)
        if self.zmin + dz != z:
            raise ValueError("%s != %s" % (self.zmin + dz, z))
        msg += "%s FINAL Z: %f\n" % (indent, self.zmin + dz)

        pdz = (dz) / self.zrange
        msg += "%s %s z=%f (%f %%)\n" % (indent, self, z, pdz*100)

        # just sanity checking - though as a note, the only time I've seen this
        # differ from our math.$TRIGFNS() version below, it's been enough to
        # flip ths sign of a float, but still the value is basically 0.
        # "Error"s look something like:
        #   lineAtZ: Line(XYZ(163.576,13.477,332.267),XYZ(159.657,11.456,334.322))
        #   crosses 333.998378
        #   lineAtZ: Line(XYZ(159.657,11.456,334.322),XYZ(157.736,9.815,332.248)) crosses 333.998378
        #   lineAtZ: crosser:
        #       Line(XYZ(163.576,13.477,332.267),XYZ(159.657,11.456,334.322))
        #     dzAtZ: dz = 333.998378 - 332.266512 = 1.731866
        #       AtZ: FINAL Z: 333.998378
        #       AtZ: Line(XYZ(163.576,13.477,332.267),XYZ(159.657,11.456,334.322)) z=333.998378 (84.239319 %)
        #       AtZ: estimates: x=160.27509756016764 y=11.77480930658196
        #       AtZ: dx = -3.301207
        #       AtZ: FINAL X: 160.27509756016764
        #       AtZ: x error is 0.0 pct
        #       AtZ: dy = -1.7017770446177103
        #       AtZ: FINAL Y: 11.77480930658196
        #       AtZ: y error is -1.4210854715202004e-14 pct
        #   lineAtZ: point: XYZ(160.275,11.775,333.998)
        #   lineAtZ: crosser: Line(XYZ(159.657,11.456,334.322),XYZ(157.736,9.815,332.248))
        #     dzAtZ: dz = 333.998378 - 334.322400 = -0.324022
        #       AtZ: FINAL Z: 333.998378
        #       AtZ: Line(XYZ(159.657,11.456,334.322),XYZ(157.736,9.815,332.248)) z=333.998378 (15.619049 %)
        #       AtZ: estimates: x=159.3572979708394 y=11.19999308061723
        #       AtZ: dx = -0.300163
        #       AtZ: FINAL X: 159.3572979708394
        #       AtZ: x error is 0.0 pct
        #       AtZ: dy = -0.2564237619295354
        #       AtZ: FINAL Y: 11.19999308061723
        #       AtZ: y error is 0.0 pct
        #   lineAtZ: point: XYZ(159.357,11.200,333.998)
        #   lineAtZ: new line Line(XYZ(160.275,11.775,333.998),XYZ(159.357,11.200,333.998)) (slope 0.6262981947785048)
        # got line Line(XYZ(160.275,11.775,333.998),XYZ(159.357,11.200,333.998)) nf:0 d:0
        #
        # in fact, the only values I've seen other than 0 are:
        # -1.4210854715202004e-14 pct
        #  1.4210854715202004e-14 pct
        #  2.842170943040401e-14 pct
        #
        # To be honest, if every calculation I ever did came within
        # -2e-14..3e-14 *percent* of the final answer (which is obviously just
        # round-off error in a float), I'd be really, really happy about it.

        xest = self.xmin + (self.xrange * pdz)
        yest = self.ymin + (self.yrange * pdz)

        dx = self.dxAtZ(z)
        x = self.xmin + _Decimal(dx)

        msg += "%s dx = %f\n" % (indent, dx)
        msg += "%s FINAL X: %s\n" % (indent, x)

        dy = self.dyAtZ(z)
        y = self.ymin + _Decimal(dy)

        msg += "%s dy = %s\n" % (indent, dy)
        msg += "%s FINAL Y: %s\n" % (indent, y)

        def errbar(x, y):
            return 100 - (100 / x * y)

        if abs(errbar(xest, x)) > 0.0001 or abs(errbar(yest, y)) > 0.0001:
            msg += "%s estimates: x=%s y=%s" % (indent, xest, yest)
            print("%s" % (msg,))
        if abs(errbar(xest, x)) > 0.0001:
            print("%s x error is %s pct" % (indent, errbar(xest, x)))

        if abs(errbar(yest, y)) > 0.0001:
            print("%s y error is %s pct" % (indent, errbar(yest, y)))

        if not _inside(x, self.xmin, self.xmax):
            raise ValueError("%s is not in range %s..%s" % (x, self.xmin, self.xmax))
        if not _inside(y, self.ymin, self.ymax):
            raise ValueError("%s is not in range %s..%s" % (x, self.ymin, self.ymax))
        if not _inside(z, self.zmin, self.zmax):
            raise ValueError("%s is not in range %s..%s" % (x, self.zmin, self.zmax))
        return Point(x, y, z)
Beispiel #54
0
 def pythonvalue(self, value):
     return _Decimal(value)
Beispiel #55
0
def Decimal(value: str, prec=VALUE_PREC):
    vlaue = str(value)
    return _Decimal(value).quantize(_Decimal(prec))
Beispiel #56
0
 def distance(self, point):
     d = self.xy_min.distance(point) + self.xy_max.distance(point)
     d = _Decimal(d) / 2
     return Decimal(d)