예제 #1
0
def date(ctx, year, month, day):
    """
    Defines a date value
    """
    return _date(conversions.to_integer(year, ctx),
                 conversions.to_integer(month, ctx),
                 conversions.to_integer(day, ctx))
예제 #2
0
def word_slice(ctx, text, start, stop=0, by_spaces=False):
    """
    Extracts a substring spanning from start up to but not-including stop
    """
    text = conversions.to_string(text, ctx)
    start = conversions.to_integer(start, ctx)
    stop = conversions.to_integer(stop, ctx)
    by_spaces = conversions.to_boolean(by_spaces, ctx)

    if start == 0:
        raise ValueError("Start word cannot be zero")
    elif start > 0:
        start -= 1  # convert to a zero-based offset

    if stop == 0:  # zero is treated as no end
        stop = None
    elif stop > 0:
        stop -= 1  # convert to a zero-based offset

    words = __get_words(text, by_spaces)

    selection = operator.getitem(words, slice(start, stop))

    # re-combine selected words with a single space
    return ' '.join(selection)
예제 #3
0
def time(ctx, hours, minutes, seconds):
    """
    Defines a time value
    """
    return _time(conversions.to_integer(hours, ctx),
                 conversions.to_integer(minutes, ctx),
                 conversions.to_integer(seconds, ctx))
예제 #4
0
def randbetween(ctx, bottom, top):
    """
    Returns a random integer number between the numbers you specify
    """
    bottom = conversions.to_integer(bottom, ctx)
    top = conversions.to_integer(top, ctx)
    return random.randint(bottom, top)
예제 #5
0
def word_slice(ctx, text, start, stop=0, by_spaces=False):
    """
    Extracts a substring spanning from start up to but not-including stop
    """
    text = conversions.to_string(text, ctx)
    start = conversions.to_integer(start, ctx)
    stop = conversions.to_integer(stop, ctx)
    by_spaces = conversions.to_boolean(by_spaces, ctx)

    if start == 0:
        raise ValueError("Start word cannot be zero")
    elif start > 0:
        start -= 1  # convert to a zero-based offset

    if stop == 0:  # zero is treated as no end
        stop = None
    elif stop > 0:
        stop -= 1  # convert to a zero-based offset

    words = __get_words(text, by_spaces)

    selection = operator.getitem(words, slice(start, stop))

    # re-combine selected words with a single space
    return " ".join(selection)
예제 #6
0
def randbetween(ctx, bottom, top):
    """
    Returns a random integer number between the numbers you specify
    """
    bottom = conversions.to_integer(bottom, ctx)
    top = conversions.to_integer(top, ctx)
    return random.randint(bottom, top)
예제 #7
0
def rept(ctx, text, number_times):
    """
    Repeats text a given number of times
    """
    if number_times < 0:
        raise ValueError("Number of times can't be negative")
    return conversions.to_string(text, ctx) * conversions.to_integer(number_times, ctx)
예제 #8
0
def rept(ctx, text, number_times):
    """
    Repeats text a given number of times
    """
    if number_times < 0:
        raise ValueError("Number of times can't be negative")
    return conversions.to_string(text, ctx) * conversions.to_integer(number_times, ctx)
예제 #9
0
def _int(ctx, number):
    """
    Rounds a number down to the nearest integer
    """
    return conversions.to_integer(
        conversions.to_decimal(number, ctx).to_integral_value(ROUND_FLOOR),
        ctx)
예제 #10
0
def left(ctx, text, num_chars):
    """
    Returns the first characters in a text string
    """
    num_chars = conversions.to_integer(num_chars, ctx)
    if num_chars < 0:
        raise ValueError("Number of chars can't be negative")
    return conversions.to_string(text, ctx)[0:num_chars]
예제 #11
0
def _round(ctx, number, num_digits):
    """
    Rounds a number to a specified number of digits
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_HALF_UP)
예제 #12
0
def roundup(ctx, number, num_digits):
    """
    Rounds a number up, away from zero
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_UP)
예제 #13
0
def rounddown(ctx, number, num_digits):
    """
    Rounds a number down, toward zero
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_DOWN)
예제 #14
0
def rounddown(ctx, number, num_digits):
    """
    Rounds a number down, toward zero
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_DOWN)
예제 #15
0
def _round(ctx, number, num_digits):
    """
    Rounds a number to a specified number of digits
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_HALF_UP)
예제 #16
0
def roundup(ctx, number, num_digits):
    """
    Rounds a number up, away from zero
    """
    number = conversions.to_decimal(number, ctx)
    num_digits = conversions.to_integer(num_digits, ctx)

    return decimal_round(number, num_digits, ROUND_UP)
예제 #17
0
def left(ctx, text, num_chars):
    """
    Returns the first characters in a text string
    """
    num_chars = conversions.to_integer(num_chars, ctx)
    if num_chars < 0:
        raise ValueError("Number of chars can't be negative")
    return conversions.to_string(text, ctx)[0:num_chars]
예제 #18
0
def right(ctx, text, num_chars):
    """
    Returns the last characters in a text string
    """
    num_chars = conversions.to_integer(num_chars, ctx)
    if num_chars < 0:
        raise ValueError("Number of chars can't be negative")
    elif num_chars == 0:
        return ''
    else:
        return conversions.to_string(text, ctx)[-num_chars:]
예제 #19
0
def right(ctx, text, num_chars):
    """
    Returns the last characters in a text string
    """
    num_chars = conversions.to_integer(num_chars, ctx)
    if num_chars < 0:
        raise ValueError("Number of chars can't be negative")
    elif num_chars == 0:
        return ''
    else:
        return conversions.to_string(text, ctx)[-num_chars:]
예제 #20
0
def field(ctx, text, index, delimiter=' '):
    """
    Reference a field in string separated by a delimiter
    """
    splits = text.split(delimiter)

    # remove our delimiters and whitespace
    splits = [f for f in splits if f != delimiter and len(f.strip()) > 0]

    index = conversions.to_integer(index, ctx)
    if index < 1:
        raise ValueError('Field index cannot be less than 1')

    if index <= len(splits):
        return splits[index - 1]
    else:
        return ''
예제 #21
0
def field(ctx, text, index, delimiter=" "):
    """
    Reference a field in string separated by a delimiter
    """
    splits = text.split(delimiter)

    # remove our delimiters and whitespace
    splits = [f for f in splits if f != delimiter and len(f.strip()) > 0]

    index = conversions.to_integer(index, ctx)
    if index < 1:
        raise ValueError("Field index cannot be less than 1")

    if index <= len(splits):
        return splits[index - 1]
    else:
        return ""
예제 #22
0
def regex_group(ctx, text, pattern, group_num):
    """
    Tries to match the text with the given pattern and returns the value of matching group
    """
    text = conversions.to_string(text, ctx)
    pattern = conversions.to_string(pattern, ctx)
    group_num = conversions.to_integer(group_num, ctx)

    expression = regex.compile(
        pattern, regex.UNICODE | regex.IGNORECASE | regex.MULTILINE | regex.V0)
    match = expression.search(text)

    if not match:
        return ""

    if group_num < 0 or group_num > len(match.groups()):
        raise ValueError("No such matching group %d" % group_num)

    return match.group(group_num)
예제 #23
0
def trunc(ctx, number):
    """
    Truncates a number to an integer by removing the fractional part of the number
    """
    return conversions.to_integer(
        conversions.to_decimal(number, ctx).to_integral_value(ROUND_DOWN), ctx)
예제 #24
0
def edate(ctx, date, months):
    """
    Moves a date by the given number of months
    """
    return conversions.to_date_or_datetime(date, ctx) + relativedelta(months=conversions.to_integer(months, ctx))
예제 #25
0
def trunc(ctx, number):
    """
    Truncates a number to an integer by removing the fractional part of the number
    """
    return conversions.to_integer(conversions.to_decimal(number, ctx).to_integral_value(ROUND_DOWN), ctx)
예제 #26
0
def date(ctx, year, month, day):
    """
    Defines a date value
    """
    return _date(conversions.to_integer(year, ctx), conversions.to_integer(month, ctx), conversions.to_integer(day, ctx))
예제 #27
0
def word(ctx, text, number, by_spaces=False):
    """
    Extracts the nth word from the given text string
    """
    return word_slice(ctx, text, number, conversions.to_integer(number, ctx) + 1, by_spaces)
예제 #28
0
def edate(ctx, date, months):
    """
    Moves a date by the given number of months
    """
    return conversions.to_date_or_datetime(
        date, ctx) + relativedelta(months=conversions.to_integer(months, ctx))
예제 #29
0
def char(ctx, number):
    """
    Returns the character specified by a number
    """
    return chr(conversions.to_integer(number, ctx))
예제 #30
0
def char(ctx, number):
    """
    Returns the character specified by a number
    """
    return chr(conversions.to_integer(number, ctx))
예제 #31
0
def _int(ctx, number):
    """
    Rounds a number down to the nearest integer
    """
    return conversions.to_integer(conversions.to_decimal(number, ctx).to_integral_value(ROUND_FLOOR), ctx)
예제 #32
0
def word(ctx, text, number, by_spaces=False):
    """
    Extracts the nth word from the given text string
    """
    return word_slice(ctx, text, number,
                      conversions.to_integer(number, ctx) + 1, by_spaces)
예제 #33
0
def time(ctx, hours, minutes, seconds):
    """
    Defines a time value
    """
    return _time(conversions.to_integer(hours, ctx), conversions.to_integer(minutes, ctx), conversions.to_integer(seconds, ctx))