Пример #1
0
def modo_teste():
    n = confirm("Number of allocs: ")
    start = perf_counter()
    t = cpp.multi_first(heap.blocks_used, n, 1, heap.n_rows * heap.n_blocks)
    tempo = perf_counter() - start
    d = t - n
    print(f"Bem sucedidas: {n - d}",
          f"Mal sucedidas: {d}",
          f"Tempo Gasto: {tempo}",
          sep='\n')
Пример #2
0
def title_files(files, verbose = False, confirm = False):
    ''' changes titles of given mp3 files '''
    for f in files:
        a = EasyID3(f)
        if is_untitled(a["title"]):
            m = re.search('^(\d+)(/\d*)?$', a["tracknumber"][0])
            if m:
                tn = int(m.group(1))
                if(confirm or core.confirm("change title for %s?" % f)):
                    a["title"] = to_roman(tn)
                    if(verbose):
                        print ("%s => %s" % (f, a["title"]))
                    a.save()
Пример #3
0
def install_ElasticSearch(host=None):
    # TODO:
    # change bind addresses in config:
    # network.host: ["localhost", "10.254.239.2"]
    tty = sys.stdout.isatty()
    url = 'https://artifacts.elastic.co/downloads/' \
          'elasticsearch/elasticsearch-6.0.1.deb'
    # sha = 'fa92bf63d32712dc166ab7c21e460cb6b424d4b8ae839a0fe0c8ee6167b981c' \
    #       'e53902f51088cbdbc9ae8fc0e31e621e3dfa878de0b88e11f7a23aea13e6d6fa3'  # noqa
    if apt.installed('elasticsearch', host=host):
        if host:
            print(f'ElasticSearch is already installed on {host}.')
        else:
            print(f'ElasticSearch is already installed on this machine.')
    elif not tty or confirm(f'Install ElasticSearch on {host}?'):
        if host:
            print(f'Installing ElasticSearch on {host}...')
            apt.install_from_url(url, host)
            # raise NotImplementedError('another host')
        else:
            print(f'Installing ElasticSearch locally...')

            f = '/tmp/es.deb'
            if not os.path.isfile(f):
                urllib.request.urlretrieve(url, f)
            else:
                file_size = os.path.getsize(f)
                print(f'{f} exists ({file_size})')
                # TODO: install deb file

                # cache = apt.Cache()
                # if cache['package-name'].is_installed:
                if apt.installed('elasticsearch', host=host):
                    print(f"installed on {host}")
                else:
                    print("NO it's NOT installed")
                    apt.install(f)
Пример #4
0
def main():
    n = confirm("Number of allocs: ")

    start = perf_counter()
    # cpp.multi_first(heap.blocks_used.ctypes.data, n, 1, heap.n_rows * heap.n_blocks)
    for _ in range(n):
        heap.c_first(5)
    print(f"[C-Python Loop] {perf_counter() - start}s")

    heap.blocks_used[:][:] = 0

    start = perf_counter()
    cpp.multi_first(heap.blocks_used, n, 1, heap.n_rows * heap.n_blocks)
    print(f"[C] {perf_counter() - start}s")
    print(heap.blocks_used.sum())

    heap.blocks_used[:][:] = 0

    start = perf_counter()
    for _ in range(n):
        new(5)
    print(f"[Python] {perf_counter() - start}s")

    print(heap.blocks_used.sum())
Пример #5
0
class Heap:
    n_rows = confirm("Number of rows: ")
    n_blocks = confirm("Number of columns: ")
    blocks_used = np.zeros((n_rows, n_blocks), dtype=bool)
    b = range(n_rows * n_blocks)

    def __init__(self):
        np.set_printoptions(linewidth=400)

    def first(self, obj, force_size=None):
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / block_size)

        used = Heap.blocks_used.flat
        for k in (i for i in self.b if not used[i]):
            ind = np.unravel_index((range(k, k + blocks)),
                                   (Heap.n_rows, Heap.n_blocks))
            if (self.blocks_used[ind] == False).all():
                self.blocks_used[ind] = True
                """
                for i in range(blocks - 1, -1, -1):
                    # Converts flatten index to matrix index
                    # x, y = self.flat_to_2D(k + i, self.n_buckets)
                    x, y = np.unravel_index(k + i, (Heap.n_rows, Heap.n_buckets))
                    self.buckets_used[y][x] = 1
                """
                return

        raise BadAlloc("Not enough space")

    def c_first(self, obj, force_size=None):
        blocks = force_size
        if not force_size or not isinstance(force_size, int):
            blocks = math.ceil(sys.getsizeof(obj) / block_size)

        count = cpp.first(Heap.blocks_used, blocks,
                          Heap.n_rows * Heap.n_blocks)
        if count != blocks:
            raise BadAlloc("Not enough space")

    def show(self):
        fig, ax = plt.subplots()
        im = ax.imshow(Heap.blocks_used)
        ax.set_title("Allocated Memory")
        fig.tight_layout()
        plt.show()

    def print(self):
        x = np.where(self.blocks_used == 1, "|x|", '| |')
        if self.n_blocks > 100 or self.n_rows > 100:
            print(x)
        else:
            for row in x:
                print(
                    str(row).replace("'", '').replace('[',
                                                      '').replace(']', ''))
        print()

    def flat_to_2D(self, index, columns):
        return index % columns, index // columns
Пример #6
0
import math
import sys

import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go

from core import BadAlloc, Byte, colors, confirm

print("Alocação de Memória".center(70, '-'))
rows = confirm("Number of rows: ")
columns = confirm("Number of columns: ")


class Bucket:
    # TODO: Change size so it won't be const static, so best fit and first fit can be differentiated
    size = 50

    def __init__(self):
        self._data = Byte()
        self.type = "void"
        self.id = 0

    def __repr__(self):
        return f"{hex(id(self))}"

    def __iter__(self):
        yield self.data.value

    @property
    def data(self):
Пример #7
0
def callback_inline(call):
    try:
        telegram = call.message.chat.id
        message_id = call.message.message_id
        # try:
        #     user_time[telegram].get('last_time')
        # except Exception as e:
        #     user_time.update({telegram: {'last_time': time.time(), 'first_time': True}})
        #     print(e)
        # if user_time[telegram].get('last_time') < (time.time() - 1) or user_time[telegram].get('first_time'):
        if call.message:
            user_time.update(
                {telegram: {
                    'last_time': time.time(),
                    'first_time': False
                }})
            # main menu
            print(call.data)
            if call.data[:7] == "start: ":
                if call.data[7:] == 'order_water':
                    core.select_good(bot, telegram, 'water', 2)
                elif call.data[7:] == 'order':
                    order(bot, telegram)
                elif call.data[7:] == 'basket':
                    basket(bot, telegram, message_id)
                elif call.data[7:] == 'subscription':
                    subscription(bot, telegram)
                elif call.data[7:] == 'shop':
                    shop(bot, telegram)
                elif call.data[7:] == 'news':
                    news(bot, telegram)
                elif call.data[7:] == 'instruction':
                    instruction(bot, telegram)

            elif call.data[:8] == "basket: ":
                if call.data[8:] == 'replay_order':
                    replay_order(bot, telegram)

            elif call.data[:13] == "order_water: ":
                if call.data[13:] == 'minus':
                    min_plu(bot, telegram, 'water', '-', call.id, 2)
                elif call.data[13:] == 'select':
                    other_goods(bot, telegram)
                elif call.data[13:] == 'plus':
                    min_plu(bot, telegram, 'water', '+', call.id, 2)

            elif call.data[:7] == "goods: ":
                if call.data[7:] == 'pompa':
                    select_good(bot, telegram, 'pompa', 3)
                elif call.data[7:] == 'pompaEL':
                    select_good(bot, telegram, 'pompaEL', 4)
                elif call.data[7:] == 'culer':
                    select_good(bot, telegram, 'culer', 5)
                elif call.data[7:] == 'skip':
                    write_adress(bot, telegram, 'other_goods')

            elif call.data[:13] == "order_pompa: ":
                if call.data[13:] == 'minus':
                    min_plu(bot, telegram, 'pompa', '-', call.id, 3)
                elif call.data[13:] == 'select':
                    # other_goods(bot, telegram)
                    write_adress(bot, telegram, 'back_to_pompa')
                elif call.data[13:] == 'plus':
                    min_plu(bot, telegram, 'pompa', '+', call.id, 3)

            elif call.data[:15] == "order_pompaEL: ":
                if call.data[15:] == 'minus':
                    min_plu(bot, telegram, 'pompaEL', '-', call.id, 4)
                elif call.data[15:] == 'select':
                    # other_goods(bot, telegram)
                    write_adress(bot, telegram, 'back_to_pompaEL')
                elif call.data[15:] == 'plus':
                    min_plu(bot, telegram, 'pompaEL', '+', call.id, 4)

            elif call.data[:13] == "order_culer: ":
                if call.data[13:] == 'minus':
                    min_plu(bot, telegram, 'culer', '-', call.id, 5)
                elif call.data[13:] == 'select':
                    # other_goods(bot, telegram)
                    write_adress(bot, telegram, 'back_to_culer')
                elif call.data[13:] == 'plus':
                    min_plu(bot, telegram, 'culer', '+', call.id, 5)

            elif call.data[:6] == "subs: ":
                if call.data[6:] == "1_month":
                    take_sub(bot, telegram, "1_month", 14)
                elif call.data[6:] == "3_months":
                    take_sub(bot, telegram, "3_months", 14)
                elif call.data[6:] == "6_months":
                    take_sub(bot, telegram, "6_months", 14)
                elif call.data[6:] == "1_year":
                    take_sub(bot, telegram, "1_year", 14)
                elif call.data[6:] == "minus":
                    min_plu_of_sub(bot, telegram, "-", call.id)
                elif call.data[6:] == "plus":
                    min_plu_of_sub(bot, telegram, "+", call.id)
                elif call.data[6:] == "change_adress":
                    write_adress(bot, telegram, 'confirm_sub')
                elif call.data[6:] == "select":
                    write_adress(bot, telegram, 'back_to_sub')
                elif call.data[6:] == "confirm_sub":
                    confirm_sub(bot, telegram)

            elif call.data[:6] == "shop: ":
                if call.data[6:] == 'water':
                    select_good(bot, telegram, 'water', 18)
                elif call.data[6:] == 'pompa':
                    select_good(bot, telegram, 'pompa', 19)
                elif call.data[6:] == 'pompaEL':
                    select_good(bot, telegram, 'pompaEL', 20)
                elif call.data[6:] == 'culer':
                    select_good(bot, telegram, 'culer', 21)

            elif call.data[:14] == "shop_pompaEL: ":
                if call.data[14:] == 'minus':
                    min_plu(bot, telegram, 'pompaEL', '-', call.id, 20)
                elif call.data[14:] == 'select':
                    # other_goods(bot, telegram)
                    # add_n_skip(bot, telegram)
                    write_adress(bot, telegram, 'back_to_shop')
                elif call.data[14:] == 'plus':
                    min_plu(bot, telegram, 'pompaEL', '+', call.id, 20)

            elif call.data[:12] == "shop_pompa: ":
                if call.data[12:] == 'minus':
                    min_plu(bot, telegram, 'pompa', '-', call.id, 19)
                elif call.data[12:] == 'select':
                    # other_goods(bot, telegram)

                    # add_n_skip(bot, telegram)
                    write_adress(bot, telegram, 'back_to_shop')
                elif call.data[12:] == 'plus':
                    min_plu(bot, telegram, 'pompa', '+', call.id, 19)

            elif call.data[:12] == "shop_culer: ":
                if call.data[12:] == 'minus':
                    min_plu(bot, telegram, 'culer', '-', call.id, 21)
                elif call.data[12:] == 'select':
                    # other_goods(bot, telegram)

                    # add_n_skip(bot, telegram)
                    write_adress(bot, telegram, 'back_to_shop')
                elif call.data[12:] == 'plus':
                    min_plu(bot, telegram, 'culer', '+', call.id, 21)

            elif call.data[:12] == "shop_water: ":
                if call.data[12:] == 'minus':
                    min_plu(bot, telegram, 'water', '-', call.id, 18)
                elif call.data[12:] == 'select':
                    # other_goods(bot, telegram)

                    # add_n_skip(bot, telegram)
                    write_adress(bot, telegram, 'back_to_shop')
                elif call.data[12:] == 'plus':
                    min_plu(bot, telegram, 'water', '+', call.id, 18)

            elif call.data[:6] == "back: ":
                if call.data[6:] == 'back_to_main_menu_del':
                    back_to_main_menu_del(bot, telegram)
                if call.data[6:] == 'back_to_main_menu':
                    back_to_main_menu_del(bot, telegram)
                if call.data[6:] == 'other_goods':
                    other_goods(bot, telegram)
                if call.data[6:] == 'back_to_pompa':
                    select_good(bot, telegram, 'pompa', 3)
                if call.data[6:] == 'back_to_pompaEL':
                    select_good(bot, telegram, 'pompaEL', 4)
                if call.data[6:] == 'back_to_culer':
                    select_good(bot, telegram, 'culer', 5)
                if call.data[6:] == 'back_to_confirm':
                    confirm_menu(bot, telegram)
                if call.data[6:] == 'back_to_abonem':
                    subscription(bot, telegram)
                if call.data[6:] == 'confirm_sub':
                    show_confirm_sub_water(bot, telegram)
                if call.data[6:] == 'back_to_shop':
                    shop(bot, telegram)
                if call.data[6:] == 'add_n_skip':
                    add_n_skip(bot, telegram)
                if call.data[6:] == 'back_to_sub':
                    show_sub_water(bot, telegram)
                elif call.data[6:] == 'order_pompaEL' or call.data[6:] == 'order_pompa' or \
                                call.data[6:] == 'order_culer':
                    other_goods(bot, telegram)
                elif call.data[6:] == 'shop_water' or call.data[6:] == 'shop_pompa' or \
                                call.data[6:] == 'shop_pompaEL' or call.data[6:] == 'shop_culer':
                    shop(bot, telegram)

            elif call.data[:9] == "confirm: ":
                if call.data[9:] == "confirm":
                    confirm(bot, telegram, call.id)
                elif call.data[9:] == 'change_adress':
                    change_adress(bot, telegram, "show_confirm", 0)

            elif call.data[:10] == "location: ":
                if call.data[10:16] == 'street':
                    print('before error', call.data)
                    set_street(bot, telegram)

            elif call.data[:9] == "payment: ":
                if call.data[9:] == "pay_menu":
                    pay_menu(bot, telegram)
                elif call.data[9:] == "yandex":
                    yandex_payment(bot, telegram)
                elif call.data[9:] == "cash":
                    cash_payment(bot, telegram)
                elif call.data[9:] == 'wish':
                    leave_wishes(bot, telegram)
                elif call.data[9:] == 'skip':
                    skip(bot, telegram)

            elif call.data[:13] == "instruction: ":
                if call.data[13:] == "write_to_us":
                    write_to_us(bot, telegram)
                if call.data[13:] == "about_company":
                    show_contacts(bot, telegram)

            elif call.data[:6] == "news: ":
                if call.data[6:] == "add":
                    add_news(bot, telegram)

    except Exception as e:
        print(e)
Пример #8
0
def callback_inline(call):
    try:
        telegram = call.message.chat.id
        message_id = call.message.message_id
        try:
            user_time[telegram].get('last_time')
        except Exception as e:
            user_time.update(
                {telegram: {
                    'last_time': time.time(),
                    'first_time': True
                }})
            print(e)
        if user_time[telegram].get('last_time') < (
                time.time() - 1) or user_time[telegram].get('first_time'):
            if call.message:
                user_time.update({
                    telegram: {
                        'last_time': time.time(),
                        'first_time': False
                    }
                })
                # main menu
                if call.data[:7] == "start: ":
                    if call.data[7:] == 'order_water':
                        order_water(bot, telegram)
                    elif call.data[7:] == 'basket':
                        basket(bot, telegram, message_id)
                    elif call.data[7:] == 'admin_panel':
                        admin_panel(bot, telegram)
                    elif call.data[7:] == 'instruction':
                        instruction(bot, telegram)

                if call.data[:6] == 'back: ':
                    if call.data[6:] == 'to_packages':
                        core.want_to_economize(bot, telegram)
                    elif call.data[6:] == 'to_show_pack_bottle':
                        core.show_pack_bottle(bot, telegram)
                    elif call.data[6:] == "back_to_main_menu":
                        # back_to_main_menu(bot, telegram, message_id, call.id)
                        print(1)
                        core.back_to_main_menu_del(bot, telegram)
                    # back_to_main_menu_with_del button
                    elif call.data[6:] == "back_to_main_menu_del":
                        core.back_to_main_menu_del(bot, telegram)
                    elif call.data[6:] == "pompa_ques":
                        core.to_order_bottles(bot, telegram)
                    elif call.data[6:] == "back_to_show_bottles":
                        core.show_selected_bottles(bot, telegram)
                # order menu
                elif call.data[:7] == "order: ":
                    if call.data[7:] == 'minus':
                        core.minus_of_bottle(bot, telegram, call.id)
                    elif call.data[7:] == 'select':
                        core.to_order_bottles(bot, telegram)
                    elif call.data[7:] == 'plus':
                        core.plus_of_bottle(bot, telegram)
                    elif call.data[7:] == 'input':
                        core.cin_other_water(bot, telegram, message_id,
                                             call.id)
                # confirm menu
                elif call.data[:9] == "confirm: ":
                    if call.data[9:] == 'confirm':
                        core.confirm(bot, telegram, call.id)
                    if call.data[9:] == 'change_adress':
                        core.change_adress(bot, telegram, "show_confirm", 0)
                    if call.data[9:] == 'want_to_economize':
                        core.want_to_economize(bot, telegram)
                # basket menu cancel_the_order
                elif call.data[:8] == "basket: ":
                    if call.data[8:] == 'replay_order':
                        core.replay_order(bot, telegram)
                    elif call.data[8:] == 'cancel_the_order':
                        cancel_the_order(bot, telegram, call.id)

                elif call.data[:8] == "replay: ":
                    if call.data[8:] == 'confirm':
                        core.confirm(bot, telegram, call.id)
                    elif call.data[8:] == 'skip':
                        # core.skip_comment(bot, telegram, call.id)
                        core.back_to_main_menu_del(bot, telegram)
                # basket menu
                elif call.data[:10] == "location: ":
                    if call.data[10:16] == 'street':
                        print('before error', call.data)
                        core.set_street(bot, telegram)
                        # core.set_street(bot, telegram, str(call.data[17:]))

                    elif call.data[10:] == 'back_to_order':
                        core.show_selected_bottles(bot, telegram)
                        # core.to_order_bottles(bot, telegram, call.id)
                    elif call.data[
                            10:] == 'back_to_confirm_package' or call.data[
                                10:] == 'confirm_pack':
                        print('here')
                        core.show_confirm_pack_bottle(bot, telegram, False)
                    elif call.data[10:] == "show_confirm":
                        core.confirm_menu(bot, telegram)
                    elif call.data[10:] == "show_pompa":
                        core.show_pompa_confirm(bot, telegram)
                    elif call.data[10:] == "order_package":
                        core.show_pack_bottle(bot, telegram)
                # instruction menu
                elif call.data[:13] == "instruction: ":
                    if call.data[13:] == 'about_water':
                        core.about_water(bot, telegram)

                    elif call.data[13:] == 'about_company':
                        core.about_company(bot, telegram)

            # instruction menu

                elif call.data[:7] == "admin: ":
                    if call.data[7:] == 'orders':
                        show_orders(bot, telegram, message_id, call.id)

                    elif call.data[7:] == 'confirm_delivery':
                        confirm_delivery(bot, telegram, message_id, call.id)
                # pompa menu
                elif call.data[:7] == "pompa: ":
                    if call.data[7:] == 'order_pompa':
                        order_pompa(bot, telegram)

                    if call.data[7:] == 'no_need':
                        no_need_pompa(bot, telegram, message_id, call.id)

                    if call.data[7:] == 'back_to_water':
                        core.show_selected_bottles(bot, telegram)

                    if call.data[7:] == 'minus':
                        minus_of_pompa(bot, telegram, call.id)

                    elif call.data[7:] == 'select':
                        to_order_pompa(bot, telegram)

                    elif call.data[7:] == 'plus':
                        plus_of_pompa(bot, telegram)

                    elif call.data[7:] == 'input':
                        order_pompa(bot, telegram)

                elif call.data[:9] == "package: ":
                    if call.data[9:] == '3_months':
                        pack_three_months(bot, telegram)
                    if call.data[9:] == '6_months':
                        pack_six_months(bot, telegram)
                    if call.data[9:] == '1_year':
                        pack_1_year(bot, telegram)
                    if call.data[9:] == 'back_to_confirm_order':
                        core.confirm_menu(bot, telegram, False)
                    if call.data[9:] == 'to_order_package':
                        core.to_order_package(bot, telegram)
                    if call.data[9:] == 'minus_of_pack_bottle':
                        core.minus_of_pack_bottle(bot, telegram, call.id)
                    if call.data[9:] == 'plus_of_pack_bottle':
                        core.plus_of_pack_bottle(bot, telegram)
                    if call.data[9:] == 'change_the_quantity':
                        core.change_the_quantity(bot, telegram)

                elif call.data[:17] == "confirm_package: ":
                    if call.data[17:] == "change_adress_pack":
                        # core.change_adress_pack(bot, telegram)
                        core.change_adress(bot, telegram, "confirm_pack", 26)

                    elif call.data[17:] == "confirm_pack_add_to_db":
                        core.confirm_pack_add_to_db(bot, telegram, call.id)

                elif call.data[:9] == "bottles: ":
                    if call.data[9:] == "1":
                        core.set_bottles(bot, telegram, 1)
                    elif call.data[9:] == "2":
                        core.set_bottles(bot, telegram, 2)
                    elif call.data[9:] == "3":
                        core.set_bottles(bot, telegram, 3)
                    elif call.data[9:] == "4":
                        core.set_bottles(bot, telegram, 4)
                    elif call.data[9:] == "5":
                        core.set_bottles(bot, telegram, 5)
                    elif call.data[9:] == "economize":
                        core.economize(bot, telegram)
                    elif call.data[9:] == "back_to_confirm_order":
                        core.order_water(bot, telegram)

                elif call.data[:7] == "pomps: ":
                    if call.data[7:] == "1":
                        core.set_pomps(bot, telegram, 1)
                    elif call.data[7:] == "2":
                        core.set_pomps(bot, telegram, 2)
                    elif call.data[7:] == "3":
                        core.set_pomps(bot, telegram, 3)

                elif call.data[:9] == "payment: ":
                    if call.data[9:] == "pay_menu":
                        core.pay_menu(bot, telegram)
                    elif call.data[9:] == "yandex":
                        core.yandex_payment(bot, telegram)
                    elif call.data[9:] == "cash":
                        core.cash_payment(bot, telegram)
                    elif call.data[9:] == 'wish':
                        core.leave_wishes(bot, telegram)
                    elif call.data[9:] == 'skip':
                        core.skip(bot, telegram)

    except Exception as e:
        print(e)