Esempio n. 1
0
def calculatebmi(listpr):
    c = Context()
    for p in listpr:
        for timeattr in p.getalltimeattribute():
            if timeattr.getattribute("HEIGHT") is not None and timeattr.getattribute("WEIGHT") is not None:
                bmi = c.divide(timeattr.getattribute("HEIGHT"), c.power(timeattr.getattribute("WEIGHT"), Decimal("2")))
                timeattr.setattribute("BMI", bmi)
 def test_timepoint(self):
     """Assert timepoints are saved from the schedule correctly
     as Decimals and ordered by appt_datetime.
     """
     context = Context(prec=2)
     self.helper.consent_and_put_on_schedule()
     self.assertEqual(
         [obj.timepoint for obj in Appointment.objects.all().order_by('appt_datetime')],
         [context.create_decimal(n) for n in range(0, 4)])
Esempio n. 3
0
 def float_to_decimal(self, f):
     "Convert a floating point number to a Decimal with no loss of information"
     n, d = f.as_integer_ratio()
     numerator, denominator = Decimal(n), Decimal(d)
     ctx = Context(prec=60)
     result = ctx.divide(numerator, denominator)
     while ctx.flags[Inexact]:
         ctx.flags[Inexact] = False
         ctx.prec *= 2
         result = ctx.divide(numerator, denominator)
     return result
Esempio n. 4
0
def bench_telco(loops, filename):
    getcontext().rounding = ROUND_DOWN
    rates = list(map(Decimal, ('0.0013', '0.00894')))
    twodig = Decimal('0.01')
    Banker = Context(rounding=ROUND_HALF_EVEN)
    basictax = Decimal("0.0675")
    disttax = Decimal("0.0341")

    with open(filename, "rb") as infil:
        data = infil.read()

    infil = io.BytesIO(data)
    outfil = six.StringIO()

    start = perf.perf_counter()
    for _ in range(loops):
        infil.seek(0)

        sumT = Decimal("0")   # sum of total prices
        sumB = Decimal("0")   # sum of basic tax
        sumD = Decimal("0")   # sum of 'distance' tax

        for i in xrange(5000):
            datum = infil.read(8)
            if datum == '':
                break
            n, =  unpack('>Q', datum)

            calltype = n & 1
            r = rates[calltype]

            p = Banker.quantize(r * n, twodig)

            b = p * basictax
            b = b.quantize(twodig)
            sumB += b

            t = p + b

            if calltype:
                d = p * disttax
                d = d.quantize(twodig)
                sumD += d
                t += d

            sumT += t
            print(t, file=outfil)

        outfil.seek(0)
        outfil.truncate()

    return perf.perf_counter() - start
Esempio n. 5
0
def float_to_decimal(f):
    """Convert a floating point number to a Decimal with no loss of information.
        See: http://docs.python.org/release/2.6.7/library/decimal.html#decimal-faq
    """
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result
Esempio n. 6
0
def float_to_decimal(f):
    "Convert a floating point number to a Decimal with no loss of information"
    if f in _special_floats:
        return _special_floats[f]
    elif math.isnan(f):
        return Decimal('NaN')
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result
Esempio n. 7
0
def float_to_decimal(f):
    """
    Convert a floating point number to a Decimal with
    no loss of information. Intended for Python 2.6 where
    casting float to Decimal does not work.
    """
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result
Esempio n. 8
0
def solve():
    mod, cnt, num1, num2 = 10 ** 9, 2, 1, 1
    x, y = (
        Decimal(1) / Decimal(5).sqrt(),
        (Decimal(1) + Decimal(5).sqrt()) / Decimal(2)
    )
    _context = Context()
    while True:
        num1, num2 = num2, (num1 + num2) % mod
        _set = set(str(num1))
        if '0' not in _set and len(_set) == 9:
            _tmp = x.log10() + cnt * y.log10()
            _num = int(_context.power(10, _tmp - int(_tmp) + 8))
            _set = set(str(_num))
            if '0' not in _set and len(_set) == 9:
                return cnt
        cnt += 1
Esempio n. 9
0
def round_to_nearest(num, roundto):
    """ rounds num to the closest multiple of roundto. e.g., 
    round_to_nearest(8,5) => 10
    round_to_nearest(6,5) => 5
    int -> int -> int
    """
    assert num > 0 and roundto > 0 # for now
    ceil_ctx = Context()
    ceil_ctx.rounding = ROUND_CEILING
    
    num = Decimal(num)
    roundto = Decimal(roundto)
    modval = num % roundto
    if modval > roundto / 2:
        return (num/roundto).quantize(1, rounding=ROUND_CEILING) * roundto
    else:
        return (num/roundto).quantize(1, rounding=ROUND_FLOOR) * roundto
Esempio n. 10
0
def query_naivebayes(model={}, text='', min_word_size=4, k=1):
	from decimal import Decimal, Context
	from math import log
	from re import sub as replace

	# cria contexto para o built-in Decimal
	context = Context()
	
	# processa texto para de ter apenas palavras significativas
	if type(text).__name__ == 'str':
		print('Processando texto, stemming, removendo stopwords ....\n\n')
		words = text_processing( replace(r'([^a-zA-Z ]+)', r'', text),
				min_word_size=min_word_size)
	else:
		words = text
	
	#return model

	# cria vetor de probabilidades 
	prob_topic = list()
	for topic in model.keys():
		
		#print("Calculando topico: %s" % topic)
		
		prob = context.ln(model[topic]['P_topic'])
		words_of_topic = model[topic]['P_wk'].keys()
		#print("ANTES %s" % words)
		for wk in words:
			#print("PROBLEMA %s %f %f" % (wk, model[topic]['P_wk'][wk], log(model[topic]['P_wk'][wk])))
			if not wk in words_of_topic:
				prob += context.ln( model[topic]['P_wk']['espuria'] )
			else:
				prob += context.ln( model[topic]['P_wk'][wk] )
		prob_topic.append( {topic: prob} )
	
	sorted_probs = sorted(prob_topic, key=lambda k: tuple(k.values())[0], 
							reverse=True)
	
	return sorted_probs[0:k]
Esempio n. 11
0
def get_completed_prices(kth_percentile=None):
	categories = {
		'sportcoat': 3002,
		'dress_shirt': 57991,
		'casual_shirt': 57990,
		'tie': 15662,
		'sweater': 11484,
		'coats_and_jacket': 57988,
		'pant': 57989,
		'jeans': 11483,
		'suit': 3001,
		'vest': 15691,
		'sleepwear_and_robe': 11510,
		'swimwear': 15690,
		'dress_shoes': 53120
	}

	prices = {}
	for key, category_id in categories.items():
		request_completed(category_id=category_id)

		num_sold = f_api.response.dict()['paginationOutput']['totalEntries']
		values_list = return_list_of_prices(f_api)
		list_len = len(values_list)
		list_mean = sum(values_list) / list_len

		prices[key] = {
			'average_price': list_mean,
			'values_list': values_list,
			'num_sold': num_sold
		}
		if kth_percentile is not None:
			from decimal import Decimal, Context
			kth_decimal = Decimal(kth_percentile) / 100
			index = Decimal(Decimal(list_len) * kth_decimal, Context(rounding='ROUND_UP'))
			sorted_values = sorted(values_list)
			kth_dist_value = sorted_values[int(index)]
			prices[key]['percentile'] = {
				'percentile_value': kth_percentile,
				'kth_distribution': kth_dist_value}

	return prices
Esempio n. 12
0
    def to_xdr_amount(value: Union[str, Decimal]) -> int:
        """Converts an amount to the appropriate value to send over the network
        as a part of an XDR object.

        Each asset amount is encoded as a signed 64-bit integer in the XDR
        structures. An asset amount unit (that which is seen by end users) is
        scaled down by a factor of ten million (10,000,000) to arrive at the
        native 64-bit integer representation. For example, the integer amount
        value 25,123,456 equals 2.5123456 units of the asset. This scaling
        allows for seven decimal places of precision in human-friendly amount
        units.

        This static method correctly multiplies the value by the scaling factor
        in order to come to the integer value used in XDR structures.

        See `Stellar's documentation on Asset Precision
        <https://www.stellar.org/developers/guides/concepts/assets.html#amount-precision-and-representation>`_
        for more information.

        :param value: The amount to convert to an integer for XDR
            serialization.

        """
        if not (isinstance(value, str) or isinstance(value, Decimal)):
            raise TypeError(
                f"Value of type '{value}' must be of type {str} or {Decimal}, but got {type(value)}."
            )
        # throw exception if value * ONE has decimal places (it can't be represented as int64)
        try:
            amount = int((Decimal(value) * Operation._ONE).to_integral_exact(
                context=Context(traps=[Inexact])))
        except decimal.Inexact:
            raise ValueError(
                f"Value of '{value}' must have at most 7 digits after the decimal."
            )

        if amount < 0 or amount > 9223372036854775807:
            raise ValueError(
                f"Value of '{value}' must represent a positive number "
                "and the max valid value is 922337203685.4775807.")

        return amount
Esempio n. 13
0
 def testQueryGets(self):
     context = Context(prec=10, rounding=ROUND_HALF_EVEN)
     self.query_request.set_consistency(Consistency.EVENTUAL).set_statement(
         query_statement).set_prepared_statement(
             self.prepare_result_select).set_limit(3).set_max_read_kb(
                 2).set_max_write_kb(3).set_max_memory_consumption(
                     5).set_math_context(context)
     self.assertIsNone(self.query_request.get_compartment())
     self.assertTrue(self.query_request.is_done())
     self.assertEqual(self.query_request.get_limit(), 3)
     self.assertEqual(self.query_request.get_max_read_kb(), 2)
     self.assertEqual(self.query_request.get_max_write_kb(), 3)
     self.assertEqual(self.query_request.get_max_memory_consumption(), 5)
     self.assertEqual(self.query_request.get_math_context(), context)
     self.assertEqual(self.query_request.get_consistency(),
                      Consistency.EVENTUAL)
     self.assertEqual(self.query_request.get_statement(), query_statement)
     self.assertEqual(self.query_request.get_prepared_statement(),
                      self.prepare_result_select.get_prepared_statement())
     self.assertEqual(self.query_request.get_timeout(), timeout)
Esempio n. 14
0
def is_perfect_square(square, *, complex=False):
    """
    Things I like about this:
    * cmath.sqrt is really neat!
    * float.is_integer() is descriptive
    * complex.real and complex.imag are well named
    * Decimal(<number>).sqrt(<context>) is handy
    Things that I don't like:
    * logic is branchy: we handle complex numbers iff complex=True
    * special case stuff for high precision floats of non-complex input only
      means that complex/real behaviour is different again...
    *
    """
    if complex:
        root = cmath.sqrt(square)
        return root.real.is_integer() and root.imag.is_integer()
    if square < 0:
        return False
    high_precision_context = Context(prec=len(str(square)) * 2)
    return 0 == Decimal(square).sqrt(high_precision_context) % 1
Esempio n. 15
0
    def countPercentValueForGroup(self, row, rounding=0):
        column = CLaboratoryCalculatorTableModel.ciPercentCount
        rows = self.getGroupRows(row)
        for row in rows:
            rowGroupData = forceInt(
                self.data(
                    self.index(row,
                               CLaboratoryCalculatorTableModel.ciGroupSumm)))
            if rowGroupData:
                rowData = forceInt(
                    self.data(
                        self.index(
                            row,
                            CLaboratoryCalculatorTableModel.ciGlobalCount)))
                value = (100.0 * rowData) / rowGroupData

                decimalValue = Decimal(unicode(value))
                context = Context(prec=rounding + len(str(int(value))))
                value = float(decimalValue.normalize(context) / 1)

                self.setData(self.index(row, column), QtCore.QVariant(value))
Esempio n. 16
0
def cova(patch_size, grp, align):  ##############################
    T = grp[-1].t[1] - grp[0].t[0]  ################### time span
    _p = patch_size  ################################### shortcut
    Np = ceil(T / _p)  ############################# nof patches
    p0 = grp[0].t[0]  ######################## cova left endpoint
    if align: p0 -= Np * _p - T  ###################### right align

    def GPI(t):
        return floor((t - p0) / _p)  #### get patch index

    nof_guards = [1] * Np  ############# number of guards per patch
    for g in grp:  ##############################################
        if g.n == 1: continue  ##################################
        ### Here we use the next_minus method to handle the case
        # vhen t's right endpoint is on a patch boundary. ######
        j = [
            GPI(g.t[0]),  ######################################
            GPI(g.t[1].next_minus(Context(prec=4)))
        ]  #########<
        for i in range(j[0], j[1] + 1):  ########################
            nof_guards[i] = 2  ##################################
    return nof_guards, p0  ######################################
Esempio n. 17
0
def sqrt_continued_fraction_period(
        number: int,
        *,
        members_count_start: int = 250,
        members_count_step: int = 50,
        precision_start: int = 500,
        precision_step: int = 250,
        precision_stop: int = 2_501) -> Sequence[int]:
    try:
        return memoized_sqrt_continued_fractions_periods[number]
    except KeyError:
        for members_count in count(members_count_start, members_count_step):
            for precision in range(precision_start, precision_stop,
                                   precision_step):
                context = Context(prec=precision)
                setcontext(context)
                sequence = list(
                    islice(sqrt_continued_fraction(number), 1, members_count))
                cycle = find_cycle(sequence)
                if cycle is not None:
                    memoized_sqrt_continued_fractions_periods[number] = cycle
                    return cycle
Esempio n. 18
0
def test_sign_built_vote_tx(tx_builder, feeder_wallet, account_addrs,
                            wei_value, sign_data):
    feeder, validator = account_addrs
    pub_key, signature = sign_data

    txb = tx_builder("soju-0012", 45, 0)
    txb.append_votemsg(
        exchange_rate=Decimal("8000.0").quantize(
            wei_value,
            context=Context(prec=30),
        ),
        denom="ukrw",
        feeder=f"{feeder}",
        validator=f"{validator}",
        salt="1234",
    )

    result = txb.sign(feeder_wallet)

    expected = signed_message(feeder, validator, pub_key, signature)

    assert result == expected
Esempio n. 19
0
def time_for_cookies(C, F, X):
    from decimal import Decimal
    from decimal import setcontext
    from decimal import Context
    setcontext(Context(prec=10))
    time_taken = Decimal(0.0)

    current_cookies = Decimal(0.0)
    CPS = Decimal(2.0)
    wait_time = Decimal(0.0)
    buy_time = Decimal(0.0)

    if C > X > current_cookies:
        time_taken = X / CPS
        current_cookies = X
        return time_taken

    while current_cookies < X:

        if current_cookies >= C:
            wait_time = (X - current_cookies) / CPS
            buy_time = X / (CPS + F)

            if wait_time > buy_time:
                current_cookies = current_cookies - C
                CPS = CPS + F

            elif wait_time <= buy_time:
                time_taken = time_taken + (X - current_cookies) / CPS
                current_cookies = X

        elif current_cookies < C:
            wait_time = (C - current_cookies) / CPS
            current_cookies = C
            time_taken = time_taken + wait_time

    return time_taken
Esempio n. 20
0
 def _update_price(self, stock, price):
     symbol = stock.symbol.replace(".", "-")
     if stock.exchange == Stock.Exchange.TSX:
         symbol = symbol + ".TO"
     else:
         self.stderr.write(
             "Symbol: {}. We don't handle this exchange yet: {}".format(
                 stock.symbol, stock.exchange))
         return
     self.stdout.write("Looking up symbol {}".format(symbol))
     new_price = round(
         Decimal(
             yfinance.Ticker(symbol).info["regularMarketPrice"],
             context=Context(prec=120),
         ),
         4,
     )
     if new_price != price.price:
         self.stdout.write("Updating price from {} to {}".format(
             price.price, new_price))
         price.price = new_price
         price.save()
     else:
         self.stdout.write("No update required")
Esempio n. 21
0
 def __init__(self, precision=2):
     self.empty()
     self._context = Context(prec=precision)
Esempio n. 22
0
    def __new__(cls, *args, **kwargs):
        def replace_microsecond(new_value):
            if has_microsecond_argument:
                lst = list(args)
                lst[DATETIME_CONSTRUCTOR_MICROSECOND_ARGUMENT_INDEX] = new_value
                return tuple(lst)
            else:
                kwargs[TIMESTAMP_MICROSECOND_FIELD] = new_value
                return args

        precision = None
        fractional_precision = None
        fractional_seconds = None
        datetime_microseconds = None
        has_microsecond_argument = len(
            args) > DATETIME_CONSTRUCTOR_MICROSECOND_ARGUMENT_INDEX
        if has_microsecond_argument:
            datetime_microseconds = args[
                DATETIME_CONSTRUCTOR_MICROSECOND_ARGUMENT_INDEX]
        elif TIMESTAMP_MICROSECOND_FIELD in kwargs:
            datetime_microseconds = kwargs.get(TIMESTAMP_MICROSECOND_FIELD)
        if TIMESTAMP_PRECISION_FIELD in kwargs:
            precision = kwargs.get(TIMESTAMP_PRECISION_FIELD)
            # Make sure we mask this before we construct the datetime.
            del kwargs[TIMESTAMP_PRECISION_FIELD]
        if TIMESTAMP_FRACTION_PRECISION_FIELD in kwargs:
            fractional_precision = kwargs.get(
                TIMESTAMP_FRACTION_PRECISION_FIELD)
            if fractional_precision is not None and not (
                    0 <= fractional_precision <= MICROSECOND_PRECISION):
                raise ValueError(
                    'Cannot construct a Timestamp with fractional precision of %d digits, '
                    'which is out of the supported range of [0, %d].' %
                    (fractional_precision, MICROSECOND_PRECISION))
            # Make sure we mask this before we construct the datetime.
            del kwargs[TIMESTAMP_FRACTION_PRECISION_FIELD]
        if TIMESTAMP_FRACTIONAL_SECONDS_FIELD in kwargs:
            fractional_seconds = kwargs.get(TIMESTAMP_FRACTIONAL_SECONDS_FIELD)
            if fractional_seconds is not None:
                if not (0 <= fractional_seconds < 1):
                    raise ValueError(
                        'Cannot construct a Timestamp with fractional seconds of %s, '
                        'which is out of the supported range of [0, 1).' %
                        str(fractional_seconds))
            # Make sure we mask this before we construct the datetime.
            del kwargs[TIMESTAMP_FRACTIONAL_SECONDS_FIELD]

        if fractional_seconds is not None and (fractional_precision is not None
                                               or datetime_microseconds
                                               is not None):
            raise ValueError(
                'fractional_seconds cannot be specified '
                'when fractional_precision or microseconds are not None.')

        if fractional_precision is not None and datetime_microseconds is None:
            raise ValueError(
                'datetime_microseconds cannot be None while fractional_precision is not None.'
            )

        if fractional_precision == 0 and datetime_microseconds != 0:
            raise ValueError(
                'datetime_microseconds cannot be non-zero while fractional_precision is 0.'
            )

        if fractional_seconds is not None:
            fractional_seconds_exponent = fractional_seconds.as_tuple(
            ).exponent
            if fractional_seconds == DECIMAL_ZERO and fractional_seconds_exponent > 0:
                # Zero with a positive exponent is just zero. Set the exponent to zero so fractional_precision is
                # calculated correctly.
                fractional_seconds_exponent = 0
                fractional_seconds = DECIMAL_ZERO
            fractional_precision = min(-fractional_seconds_exponent,
                                       MICROSECOND_PRECISION)
            # Scale to microseconds and truncate to an integer.
            args = replace_microsecond(
                int(fractional_seconds *
                    BASE_TEN_MICROSECOND_PRECISION_EXPONENTIATION))
        elif datetime_microseconds is not None:
            if fractional_precision is None:
                fractional_precision = MICROSECOND_PRECISION
            if fractional_precision == 0:
                # As previously verified, datetime_microseconds must be zero in this case.
                fractional_seconds = DECIMAL_ZERO
            else:
                try:
                    fractional_seconds = Decimal(datetime_microseconds).scaleb(-MICROSECOND_PRECISION)\
                        .quantize(PRECISION_LIMIT_LOOKUP[fractional_precision], context=Context(traps=[Inexact]))
                except Inexact:
                    raise ValueError(
                        'microsecond value %d cannot be expressed exactly in %d digits.'
                        % (datetime_microseconds, fractional_precision))
        else:
            assert datetime_microseconds is None
            # The datetime constructor requires a non-None microsecond argument.
            args = replace_microsecond(0)
            fractional_precision = 0
            fractional_seconds = DECIMAL_ZERO

        instance = super(Timestamp, cls).__new__(cls, *args, **kwargs)
        setattr(instance, TIMESTAMP_PRECISION_FIELD, precision)
        setattr(instance, TIMESTAMP_FRACTION_PRECISION_FIELD,
                fractional_precision)
        setattr(instance, TIMESTAMP_FRACTIONAL_SECONDS_FIELD,
                fractional_seconds)

        return instance
Esempio n. 23
0
# IN THE SOFTWARE.
#
"""
Some utility functions to deal with mapping Amazon DynamoDB types to
Python types and vice-versa.
"""
import base64
from decimal import (Decimal, DecimalException, Context,
                     Clamped, Overflow, Inexact, Underflow, Rounded)
from collections import Mapping
from boto.dynamodb.exceptions import DynamoDBNumberError
from boto.compat import filter, map, six, long_type


DYNAMODB_CONTEXT = Context(
    Emin=-128, Emax=126, rounding=None, prec=38,
    traps=[Clamped, Overflow, Inexact, Rounded, Underflow])


# python2.6 cannot convert floats directly to
# Decimals.  This is taken from:
# http://docs.python.org/release/2.6.7/library/decimal.html#decimal-faq
def float_to_decimal(f):
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = DYNAMODB_CONTEXT
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
Esempio n. 24
0
def tahara_features(classify="dropout", is_classify="dropout", is_80=False):
    u"""田原アルゴリズムに基づいて特徴語を抽出し、ユーザーベクトルを作成する

        Args:
            calassify:適用するユーザーのクラス
            is_classify:どのユーザークラスらしさでベクトルを作成するか
            is_80:上位80語制限
    """

    import pymongo
    from decimal import Decimal,Context
    import math
    from function_common import get_word_point

    context=Context()
    count=0

    if classify == "dropout":
        profiles=load_from_mongo(config.m_dropout.db,config.m_dropout.profile_coll)
    elif classify=="normal":
        profiles=load_from_mongo(config.m_normal.db,config.m_normal.profile_coll)
    elif classify=="dropout_test":
        profiles=load_from_mongo(config.m_dropout_test.db,config.m_dropout_test.profile_coll)
        print len(profiles)
    elif classify=="normal_test":
        profiles=load_from_mongo(config.m_normal_test.db,config.m_normal_test.profile_coll)
        print len(profiles)
    else:
        print "unknown classify ",classify
        return

    if is_classify =="dropout":
            dropouts=load_from_mongo(config.m_dropout.db,config.m_dropout.tahara_coll,True)
            names=[]
            for d in dropouts.sort("rate",pymongo.DESCENDING):
                count+=1
                names.append(d["_id"])
                if count >=1000:
                    break
            if(is_80==True):
                print "name,dropout,{0}".format(
                ",".join([str(n) for n in names if get_word_point(n) > 0]))
            else:
                print "name,dropout,{0}".format(",".join([str(n) for n in names]))

    for profile in profiles:
        if classify == "dropout" or classify == "normal":
            wui=load_from_mongo("db","users_of_tehara",return_cursor=False,criteria={'_id':profile['_id']})
        elif classify == "dropout_test" or classify == "normal_test":
            wui=load_from_mongo("db","users_of_tehara_test",return_cursor=False,criteria={'_id':profile['_id']})
        else:
            print "unknown classify ",classify
            return

        if (wui is None) or (len(wui)==0):
            continue
        words= wui[0][u'words']
        wui_wci=0
        w2ui=0
        w2ci=0

        if is_classify =="dropout":
            dropouts=load_from_mongo(config.m_dropout.db,config.m_dropout.tahara_coll,True)
        else:
            dropouts=load_from_mongo(config.m_normal.db,config.m_normal.tahara_coll,True)

        if classify=="dropout":
            tweets=load_from_mongo(config.m_dropout.db,config.m_dropout.tweets_coll,criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            if len(tweets)==0:
                tweets=load_from_mongo(config.m_dropout.db,"tweets_of_dropout?",criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            tmp_sim_dropouts=[]
            tmp_sim_dropouts.append(profile["_id"])
            tmp_sim_dropouts.append(-1)
        elif classify == "normal":
            tweets=load_from_mongo(config.m_normal.db,config.m_normal.tweets_coll,criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            tmp_sim_dropouts=[]
            tmp_sim_dropouts.append(profile["_id"])
            tmp_sim_dropouts.append(1)
        elif classify == "dropout_test":
            tweets=load_from_mongo(config.m_dropout_test.db,config.m_dropout_test.tweets_coll,criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            if len(tweets)==0:
                tweets=load_from_mongo(config.m_dropout_test.db,"tweets_of_dropout?",criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            tmp_sim_dropouts=[]
            tmp_sim_dropouts.append(profile["_id"])
            tmp_sim_dropouts.append(-1)
        elif classify == "normal_test":
            tweets=load_from_mongo(config.m_normal_test.db,config.m_normal_test.tweets_coll,criteria={'$and':[{"user.screen_name":profile["_id"]},{'text':{'$regex':'^[^RT]'}},{'created_at':{'$gte':profile["date_of_start"],'$lte':profile["date_of_dropout"]}}]})
            tmp_sim_dropouts=[]
            tmp_sim_dropouts.append(profile["_id"])
            tmp_sim_dropouts.append(-1)

        # 実際につぶやきがあった日数をカウント
        day_counter=set()
        for tweet in tweets:
            y=extract_time(tweet["created_at"],"Y")
            m=extract_time(tweet["created_at"],"M")
            d=extract_time(tweet["created_at"],"D")
            days=y,":",m,":",d
            day_counter.add(days)
        total_days=len(day_counter)

        if total_days == 0:
            print "Error! ",profile["_id"],"has 0 tweets."
            continue

        tmp_sim_dropouts=[]
        count=0

        for d in dropouts.sort("rate",pymongo.DESCENDING):

            count+=1
            word=[w for w in words if w["name"] == d["_id"]]
            if len(word)==0:
                word=[{"count":0,"name":d["_id"],"days":0}]
            else:
                pass

            cnt=Decimal(word[0]["count"])
            rate=Decimal(d["rate"])
            days=Decimal(word[0]["days"])
            d_point=Decimal(get_word_point(d["_id"]))

            if d_point == 0:
                if is_classify=="dropout":
                    if count > 1000:
                        if is_80==True:
                            break
                        else:
                            pass
                    else:
                        if is_80==True:
                            continue
                        else:
                            pass
            td=Decimal(total_days)

            if is_classify=="dropout":
                tmp_wui_wci=Decimal(days/td)
                wui_wci+=tmp_wui_wci
                tmp_w2ui=context.power(cnt,2)
                w2ui+=tmp_w2ui
                tmp_w2ci=context.power(rate,2)
                w2ci+=tmp_w2ci

            else:
                tmp_wui_wci=Decimal(cnt*rate)
                wui_wci+=(cnt*rate)
                tmp_w2ui=Decimal(math.pow((Decimal(cnt)),2))
                w2ui+=math.pow((cnt),2)
                tmp_w2ci=Decimal(math.pow(Decimal(rate),2))
                w2ci+=math.pow(rate,2)

            if count >= 1000:
                break

            if tmp_wui_wci == 0 or tmp_w2ci == 0 or tmp_w2ui == 0 or cnt ==0:
                tmp_sim_dropouts.append(0)
            else:
                tmp_sim_dropout=tmp_wui_wci
                tmp_sim_dropouts.append(float(tmp_sim_dropout))

        print profile["_id"],",True,{0}".format(
            ",".join([str(t) for t in tmp_sim_dropouts])
        )
def round_places(input_value, prec=4):
    from decimal import Decimal, ROUND_HALF_EVEN, Context
    ctx = Context(prec=prec, rounding=ROUND_HALF_EVEN)
    return float(ctx.create_decimal(input_value))
Esempio n. 26
0
def floatformat(text, arg=-1):
    """
    Displays a float to a specified number of decimal places.

    If called without an argument, it displays the floating point number with
    one decimal place -- but only if there's a decimal place to be displayed:

    * num1 = 34.23234
    * num2 = 34.00000
    * num3 = 34.26000
    * {{ num1|floatformat }} displays "34.2"
    * {{ num2|floatformat }} displays "34"
    * {{ num3|floatformat }} displays "34.3"

    If arg is positive, it will always display exactly arg number of decimal
    places:

    * {{ num1|floatformat:3 }} displays "34.232"
    * {{ num2|floatformat:3 }} displays "34.000"
    * {{ num3|floatformat:3 }} displays "34.260"

    If arg is negative, it will display arg number of decimal places -- but
    only if there are places to be displayed:

    * {{ num1|floatformat:"-3" }} displays "34.232"
    * {{ num2|floatformat:"-3" }} displays "34"
    * {{ num3|floatformat:"-3" }} displays "34.260"

    If the input float is infinity or NaN, the (platform-dependent) string
    representation of that value will be displayed.
    """

    try:
        input_val = repr(text)
        d = Decimal(input_val)
    except InvalidOperation:
        try:
            d = Decimal(str(float(text)))
        except (ValueError, InvalidOperation, TypeError):
            return ''
    try:
        p = int(arg)
    except ValueError:
        return input_val

    try:
        m = int(d) - d
    except (ValueError, OverflowError, InvalidOperation):
        return input_val

    if not m and p < 0:
        return mark_safe(formats.number_format('%d' % (int(d)), 0))

    if p == 0:
        exp = Decimal(1)
    else:
        exp = Decimal('1.0') / (Decimal(10)**abs(p))
    # Set the precision high enough to avoid an exception (#15789).
    tupl = d.as_tuple()
    units = len(tupl[1])
    units += -tupl[2] if m else tupl[2]
    prec = abs(p) + units + 1

    # Avoid conversion to scientific notation by accessing `sign`, `digits`,
    # and `exponent` from Decimal.as_tuple() directly.
    sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP,
                                        Context(prec=prec)).as_tuple()
    digits = [str(digit) for digit in reversed(digits)]
    while len(digits) <= abs(exponent):
        digits.append('0')
    digits.insert(-exponent, '.')
    if sign:
        digits.append('-')
    number = ''.join(reversed(digits))
    return mark_safe(formats.number_format(number, abs(p)))
from .schema import extract_record_type, extract_logical_type, parse_schema
from ._schema_common import SCHEMA_DEFS
from ._read_common import (SchemaResolutionError, MAGIC, SYNC_SIZE,
                           HEADER_SCHEMA, missing_codec_lib)
from ._timezone import epoch
from .const import (MCS_PER_HOUR, MCS_PER_MINUTE, MCS_PER_SECOND, MLS_PER_HOUR,
                    MLS_PER_MINUTE, MLS_PER_SECOND, DAYS_SHIFT)

MASK = 0xFF
AVRO_TYPES = {
    'boolean', 'bytes', 'double', 'float', 'int', 'long', 'null', 'string',
    'fixed', 'enum', 'record', 'error', 'array', 'map', 'union', 'request',
    'error_union'
}

decimal_context = Context()


def match_types(writer_type, reader_type):
    if isinstance(writer_type, list) or isinstance(reader_type, list):
        return True
    if isinstance(writer_type, dict) or isinstance(reader_type, dict):
        return match_schemas(writer_type, reader_type)
    if writer_type == reader_type:
        return True
    # promotion cases
    elif writer_type == 'int' and reader_type in ['long', 'float', 'double']:
        return True
    elif writer_type == 'long' and reader_type in ['float', 'double']:
        return True
    elif writer_type == 'float' and reader_type == 'double':
Esempio n. 28
0
import math

# Decimal precision
getcontext().prec = 64
getcontext().Emax = 33
getcontext().Emin = -33
dec_con = getcontext()

# Decimal constants
dec_zero = dec_con.create_decimal('0E-16')
dec_one = dec_con.create_decimal('1.0000000000000000')
dec_eps = dec_con.create_decimal('1E-16')
dec_qua = dec_con.create_decimal('1E-8')

# Reward decimal context
rew_con = Context(prec=64, Emax=33, Emin=-33)
rew_quant = rew_con.create_decimal('0E-32')

# Debug flag
debug = True

# logger
class Logger(object):
    logger = logging.getLogger('Cryptotrader')

    @staticmethod
    def __init__(name, output_dir=None):
        """
        Initialise the logger
        """
        # Logger.logger = logging.getLogger(name)
Esempio n. 29
0
import ast
from decimal import Decimal, Context
import decimal

def binomial(n, k):
    if not(type(n) is int and type(k) is int):
        raise ValueError('expected int')
    res = 1
    for i in range(n-k+1,n+1):
        res *= i
    for i in range(1,k+1):
        res /= i
    return res

context = Context()

stack = [];
display_format = 'd'

while 1:
    input_str = input("")
    try:
        if input_str == "quit":
            break
        if input_str == "":
            pass
        elif input_str == "%" or input_str == "mod":
            stack.append(stack.pop() % stack.pop())
        elif input_str == "+":
            stack.append(stack.pop() + stack.pop())
Esempio n. 30
0
def validDecimal_12_4(d):
    """validate that d is dec(12,4)"""
    FOURPLACES = Decimal(10)**-4
    d.quantize(FOURPLACES, context=Context(traps=[Inexact]))
    if d < Decimal("1000000000000"):
        return True
Esempio n. 31
0
 u'hello',
 u'x' * 5000,
 'x' * 9000,
 123,
 -123,
 123.12,
 -123.12,
 10**20,
 10**38 - 1,
 -10**38 + 1,
 datetime.datetime(2011, 2, 3, 10, 11, 12, 3000),
 Decimal('1234.567'),
 Decimal('1234000'),
 Decimal('9' * 38),
 Decimal('0.' + '9' * 38),
 Decimal('-' + ('9' * 38), Context(prec=38)),
 Decimal('1E10'),
 Decimal('1E-10'),
 Decimal('0.{0}1'.format('0' * 37)),
 None,
 'hello',
 '',
 pytds.Binary(b''),
 pytds.Binary(b'\x00\x01\x02'),
 pytds.Binary(b'x' * 9000),
 2**63 - 1,
 False,
 True,
 uuid.uuid4(),
 u'Iñtërnâtiônàlizætiøn1',
 u'\U0001d6fc',
Esempio n. 32
0
def gauss_seidel(A, b, eps):
    global n
    setcontext(Context(traps=[DivisionByZero, Underflow, Overflow]))
    # Verifica se a matriz converge

    A_modified = sassenfeld(A)

    if A_modified is None:
        print('Convergence test failed')
        exit(0)

    n = len(A_modified)

    # Número de iterações máximas
    iter_max = 10000
    # Vetor inicial de x
    x = []
    # Vetor para os novos valores de x
    v = []

    # Inicializa com zero os vetores de x inicial e x atualizado do mesmo tamanho do vetor b
    for i in range(n):
        x.append(Decimal(0.0))
        v.append(Decimal(0.0))

    # Percorre todas as linhas da matriz
    for i in range(n):
        r = Decimal(1.0) / A_modified[i][i]

        # percorre todas colunas da matriz
        for j in range(n):
            if i != j:
                # Divide o valor dos elementos que não fazem parte da diagonal da
                # matriz pelo o valor da diagonal correspondente aquela linha
                A_modified[i][j] = A_modified[i][j] * r

        # Divide o elemento do vetor resposta pelo valor da diagonal da matriz
        # correspondente aquela linha
        b[i] *= r

    # Quantidade de iterações
    k = 0

    while True:
        # Inicializa a váriavel que recebe o calculo do critério de parada
        d = 0
        # Incrementa o número de iterações
        k += 1

        # Percorre todas as linhas da matriz
        for i in range(n):
            # Inicializa a soma como 0
            soma = 0

            # Percorre todas as colunas da matriz
            for j in range(n):
                # Se não for um elemento da diagonal principal soma o valor
                # do elemento multiplicado ao valor de x daquela linha a variavel soma
                if i != j:
                    # Se for a primeira linha da matriz, x0
                    if i == 0:
                        soma += A_modified[i][j] * x[j]
                    # Caso contrário, utiliza os valores de x já calculado
                    else:
                        soma += A_modified[i][j] * v[j]

            # Calcula o valor de x mais atualizado com relação aquela linha
            # e o valor de y da linha
            v[i] = b[i] - soma

        # Recebe o valor do critério de parada
        d = teste_parada(x, v)

        # A_modifiedtualiza o vetor x para os valores de x calculados na iteração
        for i in range(0, n):
            x[i] = v[i]

        # Verifica se d é menor ou igual que o eps
        if d <= eps:
            return x
        # Verifica se k é maior que o número máximo de iterações
        elif k >= iter_max:
            break
Esempio n. 33
0
def floatformat(text, arg=-1):
    """
    Display a float to a specified number of decimal places.

    If called without an argument, display the floating point number with one
    decimal place -- but only if there's a decimal place to be displayed:

    * num1 = 34.23234
    * num2 = 34.00000
    * num3 = 34.26000
    * {{ num1|floatformat }} displays "34.2"
    * {{ num2|floatformat }} displays "34"
    * {{ num3|floatformat }} displays "34.3"

    If arg is positive, always display exactly arg number of decimal places:

    * {{ num1|floatformat:3 }} displays "34.232"
    * {{ num2|floatformat:3 }} displays "34.000"
    * {{ num3|floatformat:3 }} displays "34.260"

    If arg is negative, display arg number of decimal places -- but only if
    there are places to be displayed:

    * {{ num1|floatformat:"-3" }} displays "34.232"
    * {{ num2|floatformat:"-3" }} displays "34"
    * {{ num3|floatformat:"-3" }} displays "34.260"

    If arg has the 'g' suffix, force the result to be grouped by the
    THOUSAND_SEPARATOR for the active locale. When the active locale is
    en (English):

    * {{ 6666.6666|floatformat:"2g" }} displays "6,666.67"
    * {{ 10000|floatformat:"g" }} displays "10,000"

    If the input float is infinity or NaN, display the string representation
    of that value.
    """
    force_grouping = False
    if isinstance(arg, str) and arg.endswith('g'):
        force_grouping = True
        arg = arg[:-1] or -1
    try:
        input_val = repr(text)
        d = Decimal(input_val)
    except InvalidOperation:
        try:
            d = Decimal(str(float(text)))
        except (ValueError, InvalidOperation, TypeError):
            return ''
    try:
        p = int(arg)
    except ValueError:
        return input_val

    try:
        m = int(d) - d
    except (ValueError, OverflowError, InvalidOperation):
        return input_val

    if not m and p < 0:
        return mark_safe(
            formats.number_format('%d' % (int(d)),
                                  0,
                                  force_grouping=force_grouping), )

    exp = Decimal(1).scaleb(-abs(p))
    # Set the precision high enough to avoid an exception (#15789).
    tupl = d.as_tuple()
    units = len(tupl[1])
    units += -tupl[2] if m else tupl[2]
    prec = abs(p) + units + 1

    # Avoid conversion to scientific notation by accessing `sign`, `digits`,
    # and `exponent` from Decimal.as_tuple() directly.
    rounded_d = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec))
    sign, digits, exponent = rounded_d.as_tuple()
    digits = [str(digit) for digit in reversed(digits)]
    while len(digits) <= abs(exponent):
        digits.append('0')
    digits.insert(-exponent, '.')
    if sign and rounded_d:
        digits.append('-')
    number = ''.join(reversed(digits))
    return mark_safe(
        formats.number_format(number, abs(p), force_grouping=force_grouping), )
Esempio n. 34
0
    
    #attempt to Approximate ln(x) for a given value    
    try:
        value_x = (value - D("1.0"))/(value + D("1.0"))
        for item in xrange(1,k,2):
            next_value = value_x**item/item
            first_value += next_value
            
        return 2*(first_value)
    
    #Raise TypeError if input is not a number   
    except TypeError:
        print 'Please enter an integer or a float value'

if __name__ == "__main__":
    with localcontext(Context(prec=67)):
        for arg in xrange(7,28,10):
            print "ln(%s) = %s " %\
            (arg, maclaurin_ln(arg,D("10000")))
    
#######################################################################

#FT  "C:\Maclaurin_lndecimal1.py"
#ln(7) = 1.945910149055313305105352743443179729637084729581861188459390149937
#ln(17) = 2.833213344056216080249534617873126535588203012585744787297237737878
#ln(27) = 3.295836866004329074185735710767577113942471673468248355204083000896
##########################################################################################

Version : Python 3.2

#from decimal import Decimal as D
Esempio n. 35
0
class Utils:
    TYPE_STRING = 'S'
    TYPE_NUMBER = 'N'
    TYPE_BINARY = 'B'
    TYPE_STRING_SET = 'SS'
    TYPE_NUMBER_SET = 'NS'
    TYPE_BINARY_SET = 'BS'
    TYPE_NULL = 'NULL'
    TYPE_BOOLEAN = 'BOOL'
    TYPE_MAP = 'M'
    TYPE_LIST = 'L'
    ALL_TYPES_WHERE_VALUE_DO_NOT_NEED_MODIFICATIONS = [TYPE_STRING, TYPE_BOOLEAN]

    DECIMAL_DYNAMODB_CONTEXT = Context(prec=38, rounding=decimal.ROUND_HALF_EVEN, Emin=-128, Emax=126,
                                       capitals=1, clamp=0, flags=[], traps=[])
    # 38 is the maximum numbers of Decimals numbers that DynamoDB can support.

    from boto3.dynamodb.types import TypeSerializer, TypeDeserializer
    # The serializer and deserializer can be static, even in a lambda,
    # since their classes will never change according to the user.
    _serializer = None
    _deserializer = None

    @classmethod
    def serializer(cls) -> TypeSerializer:
        if cls._serializer is None:
            cls._serializer = cls.TypeSerializer()
        return cls._serializer
    
    @classmethod
    def deserializer(cls) -> TypeDeserializer:
        if cls._deserializer is None:
            cls._deserializer = cls.TypeDeserializer()
        return cls._deserializer

    # todo: deprecate this function that was a complication because i was using the decimal module
    @staticmethod
    def float_serializer(python_object):
        if isinstance(python_object, float):
            # Okay, this is a weird one. The decimal module will crash if it is passed a float
            # with less than two decimal numbers, like float(0.5) which has only one. The decimal
            # module is used when using the Utils.TypeSerializer accessible via the serializer property.
            # Yet, the module has no issues with number with more than 20 decimals, and we want to keep
            # this infos, so we will not round the object. Instead, with the "%.f" % python_object we will
            # get the number of decimal numbers on the float (returned in the form of a string, for real,
            # and if this number is under two, we will turn the float into a float with two decimal numbers by
            # using "%.2f" % python_object, which will then be able to be passed to the Decimal modules without errors.
            # PS : Obviously, instead of doing this, a number like 0.5 cannot be converted to an int ;)
            if ("%.f" % python_object) < "2":
                python_object = "%.2f" % python_object

            return Decimal(python_object)
        elif isinstance(python_object, list):
            for i, item in enumerate(python_object):
                python_object[i] = Utils.float_serializer(python_object=item)
            return python_object
        elif isinstance(python_object, dict):
            for key, value in python_object.items():
                python_object[key] = Utils.float_serializer(python_object=value)
            return python_object
        else:
            return python_object

    def decimal_deserializer(self, python_object):
        from decimal import Decimal
        if isinstance(python_object, Decimal):
            if ("%.f" % python_object) > "0":
                python_object = float(python_object)
            else:
                python_object = int(python_object)
            return python_object
        elif isinstance(python_object, list):
            for i, item in enumerate(python_object):
                python_object[i] = Utils.decimal_deserializer(python_object=item)
            return python_object
        elif isinstance(python_object, dict):
            for key, value in python_object.items():
                python_object[key] = Utils.decimal_deserializer(python_object=value)
            return python_object
        else:
            return python_object

    @staticmethod
    def python_to_dynamodb(python_object: Any):
        """
            None                                    {'NULL': True}
            True/False                              {'BOOL': True/False}
            int/Decimal                             {'N': str(value)}
            string                                  {'S': string}
            Binary/bytearray/bytes (py3 only)       {'B': bytes}
            set([int/Decimal])                      {'NS': [str(value)]}
            set([string])                           {'SS': [string])
            set([Binary/bytearray/bytes])           {'BS': [bytes]}
            list                                    {'L': list}
            dict                                    {'M': dict}
        :param python_object:
        :return:
        """

        object_type = type(python_object)
        if object_type in [int, float]:
            return {Utils.TYPE_NUMBER: Utils._python_to_decimal(python_number=python_object)}
        elif object_type == list:
            for i, item in enumerate(python_object):
                python_object[i] = Utils.python_to_dynamodb(python_object=item)
            return {Utils.TYPE_LIST: python_object}
        elif object_type == dict:
            for key, item in python_object.items():
                python_object[key] = Utils.python_to_dynamodb(python_object=item)
            return {Utils.TYPE_MAP: python_object}
        elif object_type == bool:
            return {Utils.TYPE_BOOLEAN: python_object}
        elif object_type == str:
            return {Utils.TYPE_STRING: python_object}
        elif object_type == type(None):
            return {Utils.TYPE_NULL: True}
        elif object_type == bytes:
            return {Utils.TYPE_BINARY: python_object}
        else:
            return python_object

    @staticmethod
    def dynamodb_to_python(dynamodb_object: Any):
        if isinstance(dynamodb_object, Decimal):
            return Utils._decimal_to_python(decimal_number=dynamodb_object)
        elif isinstance(dynamodb_object, list):
            for i, item in enumerate(dynamodb_object):
                dynamodb_object[i] = Utils.dynamodb_to_python(dynamodb_object=item)
            return dynamodb_object
        elif isinstance(dynamodb_object, dict):
            if len(dynamodb_object) == 1:
                # If the length of the Dict is only one, it might be a DynamoDB object, with its key
                # as its variable type. For example : {'N': '1'}  And yes, this also apply to lists and maps.

                first_key = list(dynamodb_object.keys())[0]
                first_item = dynamodb_object[first_key]

                # First do we thing, is check if the value is a Decimal value (which happens often).
                if isinstance(first_item, Decimal):
                    return Utils._decimal_to_python(decimal_number=first_item)

                # Then the order of the elif statement is based on what we personally use the most at Inoft.
                # For example, we always use the numbers, string, list and dictionaries (maps) but always
                # never binary data or sets. In the default library, the list and dict were at the end of the loop.
                elif first_key == Utils.TYPE_NUMBER:
                    return Utils._dynamodb_number_to_python(number_string=first_item)
                elif first_key == Utils.TYPE_STRING:
                    return first_item
                elif first_key in Utils.ALL_TYPES_WHERE_VALUE_DO_NOT_NEED_MODIFICATIONS:
                    return first_item
                elif first_key == Utils.TYPE_MAP:
                    return dict([(key, Utils.dynamodb_to_python(element)) for key, element in first_item.items()])
                elif first_key == Utils.TYPE_LIST:
                    return [Utils.dynamodb_to_python(element) for element in first_item]
                elif first_key == Utils.TYPE_NULL:
                    return None
                elif first_key == Utils.TYPE_BINARY:
                    return Utils._dynamodb_binary_to_python(binary_data=first_item)
                elif first_key == Utils.TYPE_NUMBER_SET:
                    return set(map(Utils.DECIMAL_DYNAMODB_CONTEXT.create_decimal, first_item))
                elif first_key == Utils.TYPE_STRING_SET:
                    return set(first_item)
                elif first_key == Utils.TYPE_BINARY_SET:
                    from boto3.dynamodb.types import Binary
                    return set(map(Utils._dynamodb_binary_to_python, first_item))

            # If the dict was a classic dict, with its first key not in the keys used by DynamoDB
            keys_to_pop: List[str] = list()
            for key, item in dynamodb_object.items():
                item_value = Utils.dynamodb_to_python(dynamodb_object=item)
                if item_value is not None:
                    dynamodb_object[key] = item_value
                else:
                    keys_to_pop.append(key)

            for key in keys_to_pop:
                dynamodb_object.pop(key)

            return dynamodb_object
        else:
            return dynamodb_object

    @staticmethod
    def dynamodb_to_python_higher_level(dynamodb_object: Any):
        if isinstance(dynamodb_object, Decimal):
            return Utils._decimal_to_python(decimal_number=dynamodb_object)
        elif isinstance(dynamodb_object, list):
            for i, item in enumerate(dynamodb_object):
                dynamodb_object[i] = Utils.dynamodb_to_python_higher_level(dynamodb_object=item)
            return dynamodb_object
        elif isinstance(dynamodb_object, dict):
            # If the dict was a classic dict, with its first key not in the keys used by DynamoDB
            for key, item in dynamodb_object.items():
                dynamodb_object[key] = Utils.dynamodb_to_python_higher_level(dynamodb_object=item)
            return dynamodb_object
        return dynamodb_object

    @staticmethod
    def _decimal_to_python(decimal_number: Decimal) -> float or int:
        decimal_float = decimal_number.__float__()
        return decimal_float if decimal_float.is_integer() is not True else int(decimal_float)

    @staticmethod
    def _python_to_decimal(python_number: int or float) -> Decimal:
        return Utils.DECIMAL_DYNAMODB_CONTEXT.create_decimal(python_number)

    @staticmethod
    def _dynamodb_number_to_python(number_string: str):
        float_number = float(number_string)
        if float_number.is_integer():
            return int(float_number)
        else:
            return float_number

    @staticmethod
    def _dynamodb_binary_to_python(binary_data):
        from boto3.dynamodb.types import Binary
        return Binary(value=binary_data)

    @staticmethod
    def python_type_to_dynamodb_type(python_type):
        """
        {'NULL': True}                          None
        {'BOOL': True/False}                    True/False
        {'N': str(value)}                       Decimal(str(value))
        {'S': string}                           string
        {'B': bytes}                            Binary(bytes)
        {'NS': [str(value)]}                    set([Decimal(str(value))])
        {'SS': [string]}                        set([string])
        {'BS': [bytes]}                         set([bytes])
        {'L': list}                             list
        {'M': dict}                             dict
        """
        if python_type == type(None):
            return "NULL"
        elif python_type == bool:
            return "BOOL"
        elif python_type in [Decimal, int, float]:
            return "N"
        elif python_type == str:
            return "S"
        elif python_type == bytes:
            return "B"
        elif python_type == list:
            return "L"
        elif python_type == dict:
            return "M"
Esempio n. 36
0
from taurus.engine.runtime.dynamodb.definitions import (
    InstanceDataHourlyDynamoDBDefinition, MetricDynamoDBDefinition,
    MetricDataDynamoDBDefinition, MetricTweetsDynamoDBDefinition)

from htmengine import htmengineerrno, utils
from htmengine.runtime.anomaly_service import AnomalyService

from taurus.engine import logging_support

g_log = taurus_logging.getExtendedLogger(__name__)

# FIXME Hack around boto bug #2413
# See https://github.com/boto/boto/issues/2413
FIXED_DYNAMODB_CONTEXT = Context(
    Emin=-128,
    Emax=126,
    rounding=None,
    prec=5,  # Reduce precision for overall reduction in payload size
    traps=[Clamped, Overflow, Underflow])


def convertDefineModelResultToMetricItem(modelCommandResult):
    """ Convert "defineModel" Model Command Result to MetricItem suitable for
  publishing to dynamodb.

  :param dict modelCommandResult:  See model_command_result_amqp_message.json
    for schema.
  :returns: MetricItem instance suitable for publishing to the `taurus.metric`
    dynamodb table.
  :rtype: Instance of namedtuple implemented in
    `MetricDynamoDBDefinition().Item()` constructor
  """
Esempio n. 37
0
import csv
import gc
import json
from datetime import datetime, date
from decimal import Decimal, Context, setcontext
from os import path, environ
from multiprocessing import Pool, cpu_count

import click
from spectrify.utils.timestamps import iso8601_to_nanos, iso8601_to_days_since_epoch
from spectrify.utils.parquet import Writer
from spectrify.utils.s3 import S3GZipCSVReader

# Redshift allows up to 38 bits of decimal/numeric precision. Set the Python
# decimal context accordingly
redshift_context = Context(prec=38)
setcontext(redshift_context)

# This determines the number of rows in each row group of the Parquet file.
# Larger row group means better compression.
# Larger row group also means more memory required for write.
# Assuming a row size of 2KB, this will process approx. 500MB of data per group.
# Actual memory usage will be some multiple of that, since multiple copies
# are required in memory for processing.
SPECTRIFY_ROWS_PER_GROUP = environ.get('SPECTRIFY_ROWS_PER_GROUP') or 250000

# These are the values Redshift uses for true/false in its CSVs
POSTGRES_TRUE_VAL = 't'
POSTGRES_FALSE_VAL = 'f'

Esempio n. 38
0
@cache
def web_lookup(url):
    return urllib.urlopen(url).read()
'''

print("-----分离临时上下文-----")
print("保存旧的,创建新的")
old_context = getcontext().copy()
print(old_context)
getcontext().prec = 50
print(Decimal(355) / Decimal(113))
setcontext(old_context)

print("-----分离临时上下文,更好的方法-----")
print("译注:示例代码在使用标准库decimal,这个库已经实现好了localcontext")
with localcontext(Context(prec=50)):
    print(Decimal(355) / Decimal(113))

print("-----如何打开关闭文件-----")
file = 'test.txt'
file_name = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + \
            '\\' + 'docs' + '\\' + 'txt' + '\\' + file
f = open(file_name)
try:
    data = f.read()
    print(data)
finally:
    f.close()

print("-----如何打开关闭文件,更好的方法-----")
with open(file_name) as f:
Esempio n. 39
0
from xrpl.constants import XRPLException

ONE_DROP: Final[Decimal] = Decimal("0.000001")
"""Indivisible unit of XRP"""

MAX_XRP: Final[Decimal] = Decimal(10 ** 11)
"""100 billion decimal XRP"""

MAX_DROPS: Final[Decimal] = Decimal(10 ** 17)
"""Maximum possible drops of XRP"""

# Drops should be an integer string. MAY have (positive) exponent.
# See also: https://xrpl.org/currency-formats.html#string-numbers
_DROPS_REGEX: Final[str] = r"[1-9][0-9Ee-]{0,17}|0"

_DROPS_CONTEXT: Final[Context] = Context(prec=18, Emin=0, Emax=18)


def xrp_to_drops(xrp: Union[int, float, Decimal]) -> str:
    """
    Convert a numeric XRP amount to drops of XRP.

    Args:
        xrp: Numeric representation of whole XRP

    Returns:
        Equivalent amount in drops of XRP

    Raises:
        TypeError: if ``xrp`` is given as a string
        XRPRangeException: if the given amount of XRP is invalid
Esempio n. 40
0
#
# copyright: 2020-2021, Frederico Martins
# author: Frederico Martins <http://github.com/fscm>
# license: SPDX-License-Identifier: MIT

"""Tests for the Lek representation."""

from decimal import Context
from pytest import raises
from multicurrency import Currency, Lek
from multicurrency import (
    CurrencyMismatchException,
    CurrencyTypeException)


CONTEXT = Context(prec=28, rounding='ROUND_HALF_EVEN').copy()


def test_lek():
    """test_lek."""
    amount = CONTEXT.create_decimal(1) / CONTEXT.create_decimal(7)
    lek = Lek(amount=amount)
    decimal = CONTEXT.create_decimal(amount)
    assert lek.amount == decimal
    assert lek.numeric_code == '008'
    assert lek.alpha_code == 'ALL'
    assert lek.decimal_places == 2
    assert lek.decimal_sign == ','
    assert lek.grouping_sign == '\u202F'
    assert not lek.international
    assert lek.symbol == 'Lek'
Esempio n. 41
0
def learn_naivebayes_text(texts=[], min_word_size=4):
	from datetime import datetime as date
	from decimal import Decimal, Context 
	
	# cria contexto para o built-in Decimal
	context = Context()

	if type(texts).__name__ == 'list':
		print('Preparando dados')
		proc_texts = prepare_reuters_data(texts, 
						min_word_size=min_word_size)
	else:
		proc_texts = texts
	
	print('\n\nCalculo das probabilidades\n')
	
	# cria vocabulario e ja conta ocorrencias de palavas
	# por texto
	vocabulary = set()
	freq_words = []
	index = 0 
	for text in proc_texts['texts']:
		
		# contagem das palavras
		freq_words.append({})
		for word in text:
			if not word in freq_words[index]:
				freq_words[index][word] = 0
			freq_words[index][word] += 1
		
		# criacao do vocabulario
		vocabulary.update(text)
		index += 1
	
	# ja adiciona um para evitar a prob ser zero
	vocabulary_size = len(vocabulary)
	vocab_freq = {}
	for word in vocabulary:
		vocab_freq[word] = 1
	
	# calculando P_topicj e P_wk__topicj
	topics = {}
	total_texts = len(proc_texts['texts'])
	for topic, text_indices in proc_texts['topics'].items():
		
		print("\t" + date.now().strftime('[%Y-%m-%d %H:%M:%S]')
			+ " topico: %s" % topic)

		topics[topic] = {
			'P_topic': Decimal( len(text_indices) / total_texts),
			'P_wk': vocab_freq
		}
		
		# itera os textos do topico em questao
		total_words = 0
		for text in text_indices:
			for word, freq in freq_words[text].items():
				topics[topic]['P_wk'][word] += freq
			total_words += len(freq_words[text])
		
		# frequencia das palavras para o topico em questao
		# ja contadas, e acrescidas de 1, para evitar o zero
		# agora calcula a prob real da palavra dado o topico
		for word, freq in topics[topic]['P_wk'].items():
			topics[topic]['P_wk'][word] = context.divide(
			Decimal(freq), Decimal(total_words + vocabulary_size))
		
		# crio uma palavra nao existente como um marcador para 
		# palavras nao vistas
		topics[topic]['P_wk']['espuria'] = context.divide(
			Decimal(1), Decimal(total_words + vocabulary_size))

	print('\n\nTopicos processados: %d' % len(topics.keys()))
	print('Tamanho do vocabulario: %d' % vocabulary_size)
	
	return topics
Esempio n. 42
0
        return True
    if (n < 2 or n % 2 == 0):
        return False
    if (n < 9):
        return True
    if (n % 3 == 0):
        return False
    r = int(n**0.5)
    f = 5
    while (f <= r):
        if (n % f == 0):
            return False
        if (n % (f + 2) == 0):
            return False
        f += 6
    return True


setcontext(Context(prec=300))
xe = Decimal(1).exp()
xe_s = str(xe)

print(xe_s)

pos = 0
while (pos < 290):
    nr = int(xe_s[pos + 2:pos + 10])
    pos += 1
    if (is_prime(nr)):
        print(str(nr) + ":" + str(pos))
def arredondar_precisao_4(numero):
    return numero.quantize(
        Context(prec=4, rounding=ROUND_FLOOR).create_decimal(numero))