Exemplo n.º 1
0
    def get_orderbook_to_publish(self):
        orderbook = self.orderbook
        fancy_orderbook = {'bids': defaultdict(Decimal), 'asks': defaultdict(Decimal)}

        for bid in orderbook['bids']:
            fancy_orderbook['bids'][[bid]['price']] += Decimal(bid['volume'])

        for ask in orderbook['asks']:
            fancy_orderbook['asks'][ask['price']] += Decimal(ask['volume'])

        sorted_bid_keys = sorted(
            fancy_orderbook['bids'].keys(),
            key=lambda (k): float(k),
            reverse=True,
        )

        sorted_ask_keys = sorted(
            fancy_orderbook['asks'].keys(),
            key=lambda (k): float(k),
        )

        bids = [[k, str(fancy_orderbook['bids'][k]), ''] for k in sorted_bid_keys]

        asks = [[k, str(fancy_orderbook['asks'][k]), ''] for k in sorted_ask_keys]

        return {
            'bids': bids,
            'asks': asks,
        }
Exemplo n.º 2
0
def round_limits(minimum, maximum):
    """
    Rounds a pair of minimum and maximum values to form reasonable "round"
    values suitable for use as axis minimum and maximum values.

    Values are rounded "out": up for maximum and down for minimum, and "off":
    to one higher than the first significant digit shared by both.

    See unit tests for examples.
    """
    min_bits = minimum.normalize().as_tuple()
    max_bits = maximum.normalize().as_tuple()

    max_digits = max(
        len(min_bits.digits) + min_bits.exponent,
        len(max_bits.digits) + max_bits.exponent)

    # Whole number rounding
    if max_digits > 0:
        multiplier = Decimal('10')**(max_digits - 1)

        min_fraction = (minimum /
                        multiplier).to_integral_value(rounding=ROUND_FLOOR)
        max_fraction = (maximum /
                        multiplier).to_integral_value(rounding=ROUND_CEILING)

        return (min_fraction * multiplier, max_fraction * multiplier)

    max_exponent = max(min_bits.exponent, max_bits.exponent)

    # Fractional rounding
    q = Decimal('10')**(max_exponent + 1)

    return (minimum.quantize(q, rounding=ROUND_FLOOR).normalize(),
            maximum.quantize(q, rounding=ROUND_CEILING).normalize())
Exemplo n.º 3
0
def _decimalize(v, q=None):
    # If already a decimal, just return itself
    if type(v) == Decimal:
        return v

    # If tuple/list passed, bulk-convert
    elif isinstance(v, (tuple, list)):
        return type(v)(decimalize(x, q) for x in v)

    # If PDFObjRef passed, resolve it
    elif isinstance(v, PDFObjRef):
        return decimalize(resolve_all(v), q)

    # Convert int-like
    elif isinstance(v, numbers.Integral):
        return Decimal(int(v))

    # Convert float-like
    elif isinstance(v, numbers.Real):
        if q != None:
            return Decimal(repr(v)).quantize(Decimal(repr(q)),
                                             rounding=ROUND_HALF_UP)
        else:
            return Decimal(repr(v))
    else:
        raise ValueError("Cannot convert {0} to Decimal.".format(v))
Exemplo n.º 4
0
def get_ul_mag(ec, zp=DEFAULT_ZP, sig=DEFAULT_UL_SIGMA):
    dec = Decimal(str(ec))
    dzp = Decimal(str(zp))
    dsig = Decimal(str(sig))
    mag = str(dzp - (D25 * (dsig * dec).log10()))
    emag = str(dec)
    return mag, emag
Exemplo n.º 5
0
def compute_precision(recommendations, test_file):
    """
    Computes recommendation precision based on a tsv test file.
    """
    ## Computing precision
    # Organizing data
    user_item = {}
    with open(test_file) as test_file:
        for line in test_file:
            u, i, v = line.strip().split(' ')
            u, i = int(u), int(i)
            # TODO: accept float =/
            v = 1
            if u in user_item:
                user_item[u].add(i)
            else:
                user_item[u] = set([i])

    # Computing
    total_users = Decimal(len(recommendations.keys()))
    for at in range(1, PRECISION_AT + 1):
        mean = 0
        for u in recommendations.keys():
            relevants = user_item[u]
            retrieved = recommendations[u][:at]
            precision = len(relevants & set(retrieved)) / Decimal(
                len(retrieved))
            mean += precision

        print 'Average Precision @%s: %s' % (at, (mean / total_users))
Exemplo n.º 6
0
    def _clean_quantity(self, quantity):
        """Clean quantity value before it is added to entry.
        """
        value = quantity.get(QUANTITY.VALUE, '').strip()
        error = quantity.get(QUANTITY.E_VALUE, '').strip()
        unit = quantity.get(QUANTITY.U_VALUE, '').strip()
        kind = quantity.get(QUANTITY.KIND, '').strip()

        if not value:
            return False

        if is_number(value):
            value = '%g' % Decimal(value)
        if error:
            error = '%g' % Decimal(error)

        if value:
            quantity[QUANTITY.VALUE] = value
        if error:
            quantity[QUANTITY.E_VALUE] = error
        if unit:
            quantity[QUANTITY.U_VALUE] = unit
        if kind:
            quantity[QUANTITY.KIND] = kind

        return True
Exemplo n.º 7
0
    def __init__(self, session=None, configuration=None):
        super(KrakenBTCEURExchange, self).__init__(session, configuration)

        # Immutable properties and endpoints.
        self.name = u'KRAKEN_BTC_EUR'
        self.friendly_name = u'Kraken BTC-EUR'
        self.base_url = 'https://api.kraken.com/0'
        self.price_decimal_precision = 1
        self.currency = u'EUR'
        self.volume_currency = u'BTC'
        self.bid_string = 'buy'
        self.ask_string = 'sell'

        self.orderbook_depth = 100000

        # Configurables with defaults.
        self.market_order_fee = Decimal('0.0014')
        self.limit_order_fee = Decimal('0.0004')
        self.fee = self.market_order_fee
        self.fiat_balance_tolerance = Money('0.0001', 'EUR')
        self.volume_balance_tolerance = Money('0.00000001', 'BTC')
        self.min_order_size = Money('0.002', 'BTC')
        self.max_tick_speed = 5
        self.use_cached_orderbook = False

        # TODO: Unclear if these should be configurable or not.
        self.withdrawal_fee = Money('0.001', 'BTC')
        self.btc_credit_limit = Money('0', 'BTC')

        if configuration:
            self.configure(configuration)
Exemplo n.º 8
0
    def test_stdev(self):
        with self.assertRaises(NullComputationError):
            self.table.columns['one'].stdev()

        self.assertAlmostEqual(
            self.table.columns['two'].stdev().quantize(Decimal('0.01')),
            Decimal('0.69'))
Exemplo n.º 9
0
    def __init__(self, db, harness, strategy_configuration):
        super(DynamicMarketMaking, self).__init__(db, harness)

        self.logger = logging.getLogger(__name__)

        handler = logging.handlers.RotatingFileHandler(
            'dynamic_market_making.' +
            datetime.now().strftime("%Y%m%d-%H%M%S") + '.log')
        formatter = logging.Formatter(
            '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)
        self.logger.setLevel(logging.DEBUG)

        self.logger.debug("--Strategy Init--")

        self.volat = TS()
        self.midpoints = LocalMoneyTS(cur='EUR')

        # Configurable properties with defaults.
        self.spread = Decimal(
            '0.01'
        )  # how much spread should we start with around orderbook midpoint for ask/bid
        self.base_volume = Money('0.005',
                                 'BTC')  # volume to stake with (constant)

        self.exchange = None

        self.spread_adjust_coef = Decimal('1')
        self.spread_coef_on_loss = Decimal('2')
        self.base_volume_adjust_coef = Decimal('1')

        self.configure(strategy_configuration)
Exemplo n.º 10
0
    def __init__(self, session=None, configuration=None):
        super(CoinbaseBTCUSDExchange, self).__init__(session)

        self.name = u'COINBASE_BTC_USD'
        self.friendly_name = u'Coinbase BTC-USD'
        self.base_url = 'https://api.gdax.com'
        self.currency = 'USD'
        self.bid_string = 'buy'
        self.ask_string = 'sell'
        self.product_id = 'BTC-USD'
        self.price_decimal_precision = 2

        # Configurables with defaults.
        self.market_order_fee = Decimal(
            '0.0022')  # Updated by Gareth on 2016-9-20
        self.limit_order_fee = Decimal('0.001')
        self.fee = self.market_order_fee
        self.fiat_balance_tolerance = Money('0.0001', 'USD')
        self.volume_balance_tolerance = Money('0.00000001', 'BTC')
        self.max_tick_speed = 1
        self.min_order_size = Money('0.001', 'BTC')
        self.use_cached_orderbook = False

        if configuration:
            self.configure(configuration)
Exemplo n.º 11
0
def ExpMovingAverage(values, timestamps, window_time=300):
    assert len(values) == len(timestamps)
    ema = []
    values = list(values)
    values.reverse()
    timestamps = list(timestamps)
    timestamps.reverse()
    for i in range(len(timestamps)):
        noted_timestamp = timestamps[i]
        relevant_values = []
        relevant_timestamps = []
        for j in range(len(timestamps)):

            index = j + i
            if index > len(timestamps) - 1:
                break
            elif noted_timestamp - timestamps[index] < window_time:
                relevant_values.append(values[index])
                relevant_timestamps.append(noted_timestamp - timestamps[index])
            else:
                break

        weights = np.exp([(Decimal(1) / Decimal(1 + ts))
                          for ts in relevant_timestamps])
        weights = [w / sum(weights) for w in weights]

        weighted_value = 0
        for i in range(len(relevant_values)):
            rv = relevant_values[i]
            weight = weights[i]
            weighted_value += rv * weight
        ema.append(weighted_value)
    ema.reverse()
    return ema
Exemplo n.º 12
0
 def search_customers(self):
     """
     list all the customers and his dues
     :return: list of dictionary
     """
     try:
         with Transaction().start(DBNAME, 1):
             party_list = self.Party.search([('categories', '=', 'Customer')
                                             ])
             data = []
             for member in party_list:
                 invoice_list = self.Invoice.search([
                     ('party', '=', member.id), ('state', '=', 'draft')
                 ])
                 customer = {}
                 customer['code'] = member.pan
                 customer['name'] = member.name
                 due = Decimal(0)
                 for invoice in invoice_list:
                     due += invoice.total_amount
                 customer['total'] = due.to_eng()
                 data.append(customer)
             return data
     except Exception:
         if settings.level == 10:
             logger.exception('raised exception')
         return False, 'Failed crediting'
Exemplo n.º 13
0
    def get_bitstamp_volume_between_timestamps(self, start, end):
        volume = Decimal('0.0')
        last_processed_timestamp = start

        while last_processed_timestamp < end:
            request_url = self.trades_base_url + str(last_processed_timestamp)

            req = requests.get(request_url)

            if len(req.text) < 2:
                logger.info("Bad response from bitcoincharts")
                break

            lines = req.text.split('\n')

            for line in lines:
                tokens = line.split(',')

                trade_volume = float(tokens[2].strip())
                trade_timestamp = int(tokens[0])

                last_processed_timestamp = trade_timestamp

                if not last_processed_timestamp < end:
                    break
                else:
                    volume = volume + Decimal(trade_volume)

            # this catches the case that it was a single-line response
            # but that one line is a trade shortly before our 'end'
            if len(lines) <= 1:
                break

        return volume
Exemplo n.º 14
0
    def test_bins_nulls(self):
        rows = []

        for i in range(0, 100):
            rows.append([Decimal(i) / Decimal('100')])

        rows.append([None])

        new_table = Table(rows, self.column_names,
                          self.column_types).bins('number')

        self.assertColumnNames(new_table, ['number', 'Count'])
        self.assertColumnTypes(new_table, [Text, Number])

        self.assertSequenceEqual(new_table.rows[0], [
            u'[0' + get_decimal_symbol() + u'0 - 0' + get_decimal_symbol() +
            u'1)', 10
        ])
        self.assertSequenceEqual(new_table.rows[3], [
            u'[0' + get_decimal_symbol() + u'3 - 0' + get_decimal_symbol() +
            u'4)', 10
        ])
        self.assertSequenceEqual(new_table.rows[9], [
            u'[0' + get_decimal_symbol() + u'9 - 1' + get_decimal_symbol() +
            u'0]', 10
        ])
        self.assertSequenceEqual(new_table.rows[10], [None, 1])
Exemplo n.º 15
0
 def _parse_cost(self, cost):
     pattern = re.compile(r'[$]?(?P<amount>[\d\,]*[.]+[\d]*)')
     try:
         match = re.match(pattern, cost).groupdict()
         return Decimal(match['amount'].replace(',', ''))
     except AttributeError:
         return Decimal('0.00')
Exemplo n.º 16
0
    def test_combine_works(self):
        conf_file = {
            'platform': {
                'audit': True
            },
            'strategy': {
                'spread': Decimal('0.10')
            },
        }

        command_line = {
            'platform': {
                'sentry': True
            },
            'strategy': {
                'tick_sleep': None
            },
        }

        output = config_helper.combine_file_and_command_line_config(
            conf_file,
            command_line,
        )

        assert conf_file['platform']['audit'] is True
        assert conf_file['platform']['sentry'] is True
        assert conf_file['strategy']['spread'] == Decimal('0.10')
        assert conf_file['strategy']['tick_sleep'] is None
Exemplo n.º 17
0
    def cast(self, d):
        """
        Cast a single value to a :class:`decimal.Decimal`.

        :returns:
            :class:`decimal.Decimal` or :code:`None`.
        """
        if isinstance(d, Decimal) or d is None:
            return d
        elif type(d) is int:
            return Decimal(d)
        elif type(d) is float:
            return Decimal(repr(d))
        elif isinstance(d, six.string_types):
            d = d.strip()
            d = d.strip('%')

            for symbol in CURRENCY_SYMBOLS:
                d = d.strip(symbol)

            if d.lower() in self.null_values:
                return None
        else:
            raise CastError('Can not parse value "%s" as Decimal.' % d)

        try:
            return parse_decimal(d, self.locale)
        except:
            pass

        raise CastError('Can not parse value "%s" as Decimal.' % d)
Exemplo n.º 18
0
    def _extract_travel_data(self, addr1, addr2):
        res = self._map.distance_matrix(addr1, addr2)
        try:
            status = res.get('rows')[0].get('elements')[0].get('status')
            if status.lower() == 'not_found':
                return None, None

            res = res.get('rows')[0].get('elements')[0]
            miles = self._convert_km_to_miles(
                res.get('distance').get('text').replace(',', ''))
            time = res.get('duration').get('text')
            minutes = 0

            if 'hour' in time:
                hour = int(time.split(' ')[0])
                minutes = hour * 60
                minutes += int(time.split(' ')[2])
            else:
                minutes += int(time.split(' ')[0])

            miles = Decimal(miles)
            minutes = Decimal(minutes)
            return miles, minutes
        except TypeError:
            pass
        except AttributeError:
            pass
        return None, None
Exemplo n.º 19
0
    def test_variance(self):
        with self.assertRaises(NullComputationError):
            self.table.columns['one'].variance()

        self.assertEqual(
            self.table.columns['two'].variance().quantize(Decimal('0.01')),
            Decimal('0.47'))
Exemplo n.º 20
0
def errorPDF(obs, pred):
    # err_var = 25000.0
    # pdf_min = 0.0
    exp_arg = np.array([Decimal(i) for i in -0.5 / err_var * (obs - pred)**2])
    pdf = Decimal(1.0 / np.sqrt(2 * err_var * np.pi)) * np.exp(exp_arg)
    pdf[pdf < pdf_min] = Decimal(pdf_min)
    return pdf
Exemplo n.º 21
0
def create_disks(n, rnd):
    disks = []
    for i in xrange(n):
        c = (Decimal(rnd.randint(MIN_DISK_BW * 1000, MAX_DISK_BW * 1000)) /
             1000).quantize(Decimal('.0001'))
        disks.append(Disk(i, c))
    return disks
Exemplo n.º 22
0
    def test_pearson_correlation(self):
        rows = ((-1, 0, 'a'), (0, 0, 'b'), (1, 3, 'c'))

        table = Table(rows, self.columns)

        self.assertEqual(table.pearson_correlation('one', 'one'), Decimal('1'))
        self.assertAlmostEqual(table.pearson_correlation('one', 'two'),
                               Decimal('3').sqrt() * Decimal('0.5'))
Exemplo n.º 23
0
    def test_row_names_non_string(self):
        table = Table(self.rows, self.columns, row_names='one')

        self.assertSequenceEqual(
            table.row_names, [Decimal('1'), Decimal('2'), None])
        self.assertSequenceEqual(table.rows[Decimal('1')], (1, 4, 'a'))
        self.assertSequenceEqual(table.rows[Decimal('2')], (2, 3, 'b'))
        self.assertSequenceEqual(table.rows[None], (None, 2, u'👍'))
Exemplo n.º 24
0
def handle_float_decimal_combinations(value, arg, operation):
    if isinstance(value, float) and isinstance(arg, Decimal):
        logger.warning('Unsafe operation: {0!r} {1} {2!r}.'.format(value, operation, arg))
        value = Decimal(str(value))
    if isinstance(value, Decimal) and isinstance(arg, float):
        logger.warning('Unsafe operation: {0!r} {1} {2!r}.'.format(value, operation, arg))
        arg = Decimal(str(arg))
    return value, arg
def handle_numbers(number, number_op):
    number = valid_numeric(number)
    number_op = valid_numeric(number_op)

    if type(number) != type(number_op):
        logger.warning('Unsafe operation: {0} {1}.'.format(number, number_op))

    return Decimal(number), Decimal(number_op)
Exemplo n.º 26
0
 def start(self):
     self.redis = yield util.setup_redis()
     self.setup_mysql()
     self.heartbeat_key = 'trades_auditor_heartbeat'
     self.looping_call = LoopingCall(self.audit).start(1800)
     self.accuracy_bottom = Decimal('0.99')
     self.accuracy_top = Decimal('1.01')
     self.slacker = Slacker('#notifications', 'Auditor')
Exemplo n.º 27
0
    def test_items(self):
        table = Table(self.rows, self.columns, row_names='three')

        self.assertSequenceEqual(table.columns['one'].items(), [
            ('a', Decimal('1')),
            ('b', Decimal('2')),
            ('c', None)
        ])
Exemplo n.º 28
0
    def test_dict(self):
        table = Table(self.rows, self.columns, row_names='three')

        self.assertDictEqual(table.columns['one'].dict(), {
            'a': Decimal('1'),
            'b': Decimal('2'),
            'c': None
        })
Exemplo n.º 29
0
    def test_values_sorted(self):
        rows = ((2, 2, 'a'), (None, 3, 'b'), (1, 4, 'c'))

        table = Table(rows, self.column_names, self.column_types)

        self.assertSequenceEqual(
            table.columns['one'].values_sorted(),
            [Decimal('1'), Decimal('2'), None])
Exemplo n.º 30
0
def control_block_probability(clique, total=Decimal(78), p=Decimal('0.66666666666666666666666666666666666666666666666666666')):
    #how often does the clique, which is a subset of the total, control > p of the block?
    a=Decimal(0)
    rounds=Decimal(500000)
    for i in range(rounds):
        a+=control_block_trial(total, clique, p)
    #print("a: " +str(a))
    return(Decimal(a)/rounds)
Exemplo n.º 31
0
def calculate_camp_concentration(r, t):

    t = Decimal(str(t))
    r = Decimal(str(r))

    n = Constants.N_OF_RELEASED_MOLECULES
    a = Decimal.exp(-((r ** Decimal(2)) / (Decimal(4) * Constants.DIFFUSION_CONSTANT * t)))
    b = Decimal.exp(-(t / Constants.TAU))
    c = Decimal(4 * Decimal(math.pi) * t * Constants.DIFFUSION_CONSTANT) ** (Decimal(2) / Decimal(3))

    return (n * a * b) / c
Exemplo n.º 32
0
Arquivo: srt.py Projeto: yp2/gkonap
    def decompose_time_conversion(self, time, movie_fps=None):
        """
        Zwraca sekundy w Decimal
        """
        _re_time = re.compile("(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2}),(?P<msec>\d{3})")
        _re_time = _re_time.match(time)
        _re_time = _re_time.groupdict()
        _t_hour = float(_re_time["hour"])
        _t_min = float(_re_time["min"])
        _t_sec = float(_re_time["sec"])
        _t_msec = int(_re_time["msec"]) * 0.001
        conv_time = (_t_hour * 3600) + (_t_min * 60) + _t_sec + _t_msec
        conv_time = Decimal(str(conv_time))

        return conv_time.quantize(Decimal("1.000"), rounding=ROUND_DOWN)
Exemplo n.º 33
0
        def percentiler(data, percent):
            if not data:
                return None

            i = Decimal(len(data) - 1) * percent

            if i % 1 == 0:
                return data[int(i)]

            f = i.quantize('0.0')
            c = f + 1 

            d0 = data[int(f)] * Decimal(c - i)
            d1 = data[int(c)] * Decimal(i - f)
            
            return d0 + d1
Exemplo n.º 34
0
Arquivo: tmpl.py Projeto: yp2/gkonap
    def decompose_time_conversion(self, time, movie_fps=None):
        if type(time) is list:
            #jako czas końcowy dla ostaniego napisu użyjemy jego początku
            #następnie przy zwracaniu dodamy określony czas
            time = time[1]
            #dodajemy do końcowgo czasu
            _plus_t_stop = 2
        else:
            #nic nie dodajmy
            _plus_t_stop = 0

        _re_time = re.compile(r"(?P<hour>\d{1,2}):(?P<min>\d{2}):(?P<sec>\d{2}):")
        _re_time = _re_time.match(time)
        _re_time = _re_time.groupdict()
        _t_hour = float(_re_time['hour'])
        _t_min = float(_re_time['min'])
        _t_sec = float(_re_time['sec'])
        conv_time = (_t_hour * 3600) + (_t_min * 60) + _t_sec + _plus_t_stop
        conv_time = Decimal(str(conv_time))

        return conv_time.quantize(Decimal('1.000'), rounding=ROUND_DOWN)
Exemplo n.º 35
0
def set_pd_mag_from_counts(photodict,
                           c,
                           ec='',
                           lec='',
                           uec='',
                           zp=DEFAULT_ZP,
                           sig=DEFAULT_UL_SIGMA):
    with localcontext() as ctx:
        if lec == '' or uec == '':
            lec = ec
            uec = ec
        prec = max(
            get_sig_digits(str(c)),
            get_sig_digits(str(lec)), get_sig_digits(str(uec)))
        ctx.prec = prec
        dlec = Decimal(str(lec))
        duec = Decimal(str(uec))
        dc = Decimal(str(c))
        dzp = Decimal(str(zp))
        dsig = Decimal(str(sig))
        photodict[PHOTOMETRY.ZERO_POINT] = str(zp)
        if float(c) < DEFAULT_UL_SIGMA * float(uec):
            photodict[PHOTOMETRY.UPPER_LIMIT] = True
            photodict[PHOTOMETRY.UPPER_LIMIT_SIGMA] = str(sig)
            photodict[PHOTOMETRY.MAGNITUDE] = str(dzp - (D25 * (dsig * duec
                                                                ).log10()))
            dnec = Decimal('10.0')**(
                (dzp - Decimal(photodict[PHOTOMETRY.MAGNITUDE])) / D25)
            photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                (dnec + duec).log10() - dnec.log10()))
        else:
            photodict[PHOTOMETRY.MAGNITUDE] = str(dzp - D25 * dc.log10())
            photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                (dc + duec).log10() - dc.log10()))
            photodict[PHOTOMETRY.E_LOWER_MAGNITUDE] = str(D25 * (
                dc.log10() - (dc - dlec).log10()))
Exemplo n.º 36
0
def print_bars(self, label_column_name='group', value_column_name='Count', domain=None, width=120, output=sys.stdout, printable=False):
    """
    Print a text-based bar chart based on this table.

    :param label_column_name:
        The column containing the label values. Defaults to :code:`group`, which
        is the default output of :meth:`.Table.pivot` or :meth:`.Table.bins`.
    :param value_column_name:
        The column containing the bar values. Defaults to :code:`Count`, which
        is the default output of :meth:`.Table.pivot` or :meth:`.Table.bins`.
    :param domain:
        A 2-tuple containing the minimum and maximum values for the chart's
        x-axis. The domain must be large enough to contain all values in
        the column.
    :param width:
        The width, in characters, to use for the bar chart. Defaults to
        :code:`120`.
    :param output:
        A file-like object to print to. Defaults to :code:`sys.stdout`.
    :param printable:
        If true, only printable characters will be outputed.
    """
    y_label = label_column_name
    label_column = self._columns[label_column_name]

    # if not isinstance(label_column.data_type, Text):
    #     raise ValueError('Only Text data is supported for bar chart labels.')

    x_label = value_column_name
    value_column = self._columns[value_column_name]

    if not isinstance(value_column.data_type, Number):
        raise DataTypeError('Only Number data is supported for bar chart values.')

    output = output
    width = width

    # Format numbers
    decimal_places = utils.max_precision(value_column)
    value_formatter = utils.make_number_formatter(decimal_places)

    formatted_labels = []

    for label in label_column:
        formatted_labels.append(six.text_type(label))

    formatted_values = []

    for value in value_column:
        formatted_values.append(format_decimal(
            value,
            format=value_formatter,
            locale=utils.LC_NUMERIC
        ))

    max_label_width = max(max([len(l) for l in formatted_labels]), len(y_label))
    max_value_width = max(max([len(v) for v in formatted_values]), len(x_label))

    plot_width = width - (max_label_width + max_value_width + 2)

    min_value = Min(value_column_name).run(self)
    max_value = Max(value_column_name).run(self)

    # Calculate dimensions
    if domain:
        x_min = Decimal(domain[0])
        x_max = Decimal(domain[1])

        if min_value < x_min or max_value > x_max:
            raise ValueError('Column contains values outside specified domain')
    else:
        x_min, x_max = utils.round_limits(min_value, max_value)

    # All positive
    if x_min >= 0:
        x_min = Decimal('0')
        plot_negative_width = 0
        zero_line = 0
        plot_positive_width = plot_width - 1
    # All negative
    elif x_max <= 0:
        x_max = Decimal('0')
        plot_negative_width = plot_width - 1
        zero_line = plot_width - 1
        plot_positive_width = 0
    # Mixed signs
    else:
        spread = x_max - x_min
        negative_portion = (x_min.copy_abs() / spread)

        # Subtract one for zero line
        plot_negative_width = int(((plot_width - 1) * negative_portion).to_integral_value())
        zero_line = plot_negative_width
        plot_positive_width = plot_width - (plot_negative_width + 1)

    def project(value):
        if value >= 0:
            return plot_negative_width + int((plot_positive_width * (value / x_max)).to_integral_value())
        else:
            return plot_negative_width - int((plot_negative_width * (value / x_min)).to_integral_value())

    # Calculate ticks
    ticks = OrderedDict()

    # First tick
    ticks[0] = x_min
    ticks[plot_width - 1] = x_max

    tick_fractions = [Decimal('0.25'), Decimal('0.5'), Decimal('0.75')]

    # All positive
    if x_min >= 0:
        for fraction in tick_fractions:
            value = x_max * fraction
            ticks[project(value)] = value
    # All negative
    elif x_max <= 0:
        for fraction in tick_fractions:
            value = x_min * fraction
            ticks[project(value)] = value
    # Mixed signs
    else:
        # Zero tick
        ticks[zero_line] = Decimal('0')

        # Halfway between min and 0
        value = x_min * Decimal('0.5')
        ticks[project(value)] = value

        # Halfway between 0 and max
        value = x_max * Decimal('0.5')
        ticks[project(value)] = value

    decimal_places = utils.max_precision(ticks.values())
    tick_formatter = utils.make_number_formatter(decimal_places)

    ticks_formatted = OrderedDict()

    for k, v in ticks.items():
        ticks_formatted[k] = format_decimal(
            v,
            format=tick_formatter,
            locale=utils.LC_NUMERIC
        )

    def write(line):
        output.write(line + '\n')

    # Chart top
    top_line = u'%s %s' % (y_label.ljust(max_label_width), x_label.rjust(max_value_width))
    write(top_line)

    if printable:
        bar_mark = utils.PRINTABLE_BAR_MARK
        zero_mark = utils.PRINTABLE_ZERO_MARK
    else:
        bar_mark = utils.BAR_MARK
        zero_mark = utils.ZERO_MARK

    # Bars
    for i, label in enumerate(formatted_labels):
        value = value_column[i]

        if value == 0:
            bar_width = 0
        elif value > 0:
            bar_width = project(value) - plot_negative_width
        elif value < 0:
            bar_width = plot_negative_width - project(value)

        label_text = label.ljust(max_label_width)
        value_text = formatted_values[i].rjust(max_value_width)

        bar = bar_mark * bar_width

        if value >= 0:
            gap = (u' ' * plot_negative_width)

            # All positive
            if x_min <= 0:
                bar = gap + zero_mark + bar
            else:
                bar = bar + gap + zero_mark
        else:
            bar = u' ' * (plot_negative_width - bar_width) + bar

            # All negative or mixed signs
            if x_max > value:
                bar = bar + zero_mark

        bar = bar.ljust(plot_width)

        write('%s %s %s' % (label_text, value_text, bar))

    # Axis & ticks
    axis = utils.HORIZONTAL_LINE * plot_width
    tick_text = u' ' * width

    for i, (tick, label) in enumerate(ticks_formatted.items()):
        # First tick
        if tick == 0:
            offset = 0
        # Last tick
        elif tick == plot_width - 1:
            offset = -(len(label) - 1)
        else:
            offset = int(-(len(label) / 2))

        pos = (width - plot_width) + tick + offset

        # Don't print intermediate ticks that would overlap
        if tick != 0 and tick != plot_width - 1:
            if tick_text[pos - 1:pos + len(label) + 1] != ' ' * (len(label) + 2):
                continue

        tick_text = tick_text[:pos] + label + tick_text[pos + len(label):]
        axis = axis[:tick] + utils.TICK_MARK + axis[tick + 1:]

    write(axis.rjust(width))
    write(tick_text)
Exemplo n.º 37
0
def print_bars(table, label_column_name, value_column_name, domain=None, width=120, output=sys.stdout):
    """
    Print a bar chart representation of two columns.
    """
    y_label = label_column_name
    label_column = table.columns[label_column_name]

    # if not isinstance(label_column.data_type, Text):
    #     raise ValueError('Only Text data is supported for bar chart labels.')

    x_label = value_column_name
    value_column = table.columns[value_column_name]

    if not isinstance(value_column.data_type, Number):
        raise DataTypeError('Only Number data is supported for bar chart values.')

    output = output
    width = width

    # Format numbers
    decimal_places = max_precision(value_column)
    value_formatter = make_number_formatter(decimal_places)

    formatted_labels = []

    for label in label_column:
        formatted_labels.append(six.text_type(label))

    formatted_values = []

    for value in value_column:
        formatted_values.append(format_decimal(value, format=value_formatter))

    max_label_width = max(max([len(l) for l in formatted_labels]), len(y_label))
    max_value_width = max(max([len(v) for v in formatted_values]), len(x_label))

    plot_width = width - (max_label_width + max_value_width + 2)

    min_value = Min(value_column_name).run(table)
    max_value = Max(value_column_name).run(table)

    # Calculate dimensions
    if domain:
        x_min = Decimal(domain[0])
        x_max = Decimal(domain[1])

        if min_value < x_min or max_value > x_max:
            raise ValueError('Column contains values outside specified domain')
    else:
        x_min, x_max = round_limits(min_value, max_value)

    # All positive
    if x_min >= 0:
        x_min = Decimal('0')
        plot_negative_width = 0
        zero_line = 0
        plot_positive_width = plot_width - 1
    # All negative
    elif x_max <= 0:
        x_max = Decimal('0')
        plot_negative_width = plot_width - 1
        zero_line = plot_width - 1
        plot_positive_width = 0
    # Mixed signs
    else:
        spread = x_max - x_min
        negative_portion = (x_min.copy_abs() / spread)

        # Subtract one for zero line
        plot_negative_width = int(((plot_width - 1) * negative_portion).to_integral_value())
        zero_line = plot_negative_width
        plot_positive_width = plot_width - (plot_negative_width + 1)

    def project(value):
        if value >= 0:
            return plot_negative_width + int((plot_positive_width * (value / x_max)).to_integral_value())
        else:
            return plot_negative_width - int((plot_negative_width * (value / x_min)).to_integral_value())

    # Calculate ticks
    ticks = OrderedDict()

    # First tick
    ticks[0] = x_min
    ticks[plot_width - 1] = x_max

    tick_fractions = [Decimal('0.25'), Decimal('0.5'), Decimal('0.75')]

    # All positive
    if x_min >= 0:
        for fraction in tick_fractions:
            value = x_max * fraction
            ticks[project(value)] = value
    # All negative
    elif x_max <= 0:
        for fraction in tick_fractions:
            value = x_min * fraction
            ticks[project(value)] = value
    # Mixed signs
    else:
        # Zero tick
        ticks[zero_line] = Decimal('0')

        # Halfway between min and 0
        value = x_min * Decimal('0.5')
        ticks[project(value)] = value

        # Halfway between 0 and max
        value = x_max * Decimal('0.5')
        ticks[project(value)] = value

    decimal_places = max_precision(ticks.values())
    tick_formatter = make_number_formatter(decimal_places)

    ticks_formatted = OrderedDict()

    for k, v in ticks.items():
        ticks_formatted[k] = format_decimal(v, format=tick_formatter)

    def write(line):
        output.write(line + '\n')

    # Chart top
    top_line = u'%s %s' % (y_label.ljust(max_label_width), x_label.rjust(max_value_width))
    write(top_line)

    # Bars
    for i, label in enumerate(formatted_labels):
        value = value_column[i]

        if value == 0:
            bar_width = 0
        elif value > 0:
            bar_width = project(value) - plot_negative_width
        elif value < 0:
            bar_width = plot_negative_width - project(value)

        label_text = label.ljust(max_label_width)
        value_text = formatted_values[i].rjust(max_value_width)
        bar = BAR_MARK * bar_width

        if value >= 0:
            gap = (u' ' * plot_negative_width)

            # All positive
            if x_min <= 0:
                bar = gap + ZERO_MARK + bar
            else:
                bar = bar + gap + ZERO_MARK
        else:
            bar = u' ' * (plot_negative_width - bar_width) + bar

            # All negative or mixed signs
            if x_max > value:
                bar = bar + ZERO_MARK

        bar = bar.ljust(plot_width)

        write('%s %s %s' % (label_text, value_text, bar))

    # Axis & ticks
    axis = HORIZONTAL_LINE * plot_width
    tick_text = u' ' * width

    for i, (tick, label) in enumerate(ticks_formatted.items()):
        # First tick
        if tick == 0:
            offset = 0
        # Last tick
        elif tick == plot_width - 1:
            offset = -(len(label) - 1)
        else:
            offset = int(-(len(label) / 2))

        pos = (width - plot_width) + tick + offset

        # Don't print intermediate ticks that would overlap
        if tick != 0 and tick != plot_width - 1:
            if tick_text[pos - 1:pos + len(label) + 1] != ' ' * (len(label) + 2):
                continue

        tick_text = tick_text[:pos] + label + tick_text[pos + len(label):]
        axis = axis[:tick] + TICK_MARK + axis[tick + 1:]

    write(axis.rjust(width))
    write(tick_text)
Exemplo n.º 38
0
def convert_time_to_uuid(time_arg, lowest_val=True, randomize=False):
    """
    Converts a timestamp to a type 1 :class:`uuid.UUID`.

    This is to assist with getting a time slice of columns or creating
    columns when column names are ``TimeUUIDType``. Note that this is done
    automatically in most cases if name packing and value packing are
    enabled.

    Also, be careful not to rely on this when specifying a discrete
    set of columns to fetch, as the non-timestamp portions of the
    UUID will be generated randomly. This problem does not matter
    with slice arguments, however, as the non-timestamp portions
    can be set to their lowest or highest possible values.

    :param time_arg:
      The time to use for the timestamp portion of the UUID.
      Expected inputs to this would either be a :class:`decimal` object or
      a timestamp with a precision of at most 100 nanoseconds.
      Sub-second precision should be below the decimal place.
    :type time_arg: :class:`decimal` or timestamp

    :param lowest_val:
      Whether the UUID produced should be the lowest possible value
      UUID with the same timestamp as time_arg or the highest possible
      value.
    :type lowest_val: bool

    :param randomize:
      Whether the clock and node bits of the UUID should be randomly
      generated.  The `lowest_val` argument will be ignored if this
      is true.
    :type randomize: bool

    :rtype: :class:`uuid.UUID`

    """
    if isinstance(time_arg, uuid.UUID):
        return time_arg
    if isinstance(time_arg, float):
        time_arg = Decimal.from_float(time_arg)

    ns_100 = int(time_arg * DECIMAL_1E7)

    # 0x01b21dd213814000 is the number of 100-ns intervals between the
    # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
    timestamp = ns_100 + 0x01b21dd213814000L

    time_low = timestamp & 0xffffffffL
    time_mid = (timestamp >> 32L) & 0xffffL
    time_hi_version = (timestamp >> 48L) & 0x0fffL

    if randomize:
        rand_bits = random.getrandbits(8 + 8 + 48)
        clock_seq_low = rand_bits & 0xffL  # 8 bits, no offset
        clock_seq_hi_variant = (rand_bits & 0xff00L) / 0x100  # 8 bits, 8 offset
        node = (rand_bits & 0xffffffffffff0000L) / 0x10000L  # 48 bits, 16 offset
    else:
        # In the event of a timestamp tie, Cassandra compares the two
        # byte arrays directly. This is a *signed* comparison of each byte
        # in the two arrays.  So, we have to make each byte -128 or +127 for
        # this to work correctly.
        #
        # For the clock_seq_hi_variant, we don't get to pick the two most
        # significant bits (they're always 01), so we are dealing with a
        # positive byte range for this particular byte.
        if lowest_val:
            # Make the lowest value UUID with the same timestamp
            clock_seq_low = 0x80L
            clock_seq_hi_variant = 0 & 0x3fL  # The two most significant bits
                                              # will be 0 and 1, no matter what
            node = 0x808080808080L  # 48 bits
        else:  # pragma: nocover
            # Make the highest value UUID with the same timestamp
            clock_seq_low = 0x7fL
            clock_seq_hi_variant = 0x3fL  # The two most significant bits will
                                          # 0 and 1, no matter what
            node = 0x7f7f7f7f7f7fL  # 48 bits
    return uuid.UUID(fields=(time_low, time_mid, time_hi_version,
                        clock_seq_hi_variant, clock_seq_low, node), version=1)
Exemplo n.º 39
0
Arquivo: mpl2.py Projeto: yp2/gkonap
 def decompose_time_conversion(self, time):
     #format czasowy w postaci [234] gdzie 23 to sec a 4 do dziesiąte
     # części sek.
     conv_time = float(time) / 10
     conv_time = Decimal(str(conv_time))
     return conv_time.quantize(Decimal('1.000'), rounding=ROUND_DOWN,)