Exemplo n.º 1
0
    def match_query(self, query):
        '''Given a search query, return a tuple containing a regex match and
        trigger object that matches the given query.  If no match can be found,
        return a tuple of (None, None).'''

        sink = LifoQueue()

        while not self.triggers.empty():
            trigger = self.triggers.get()
            match = trigger.pattern.match(query)

            if match:
                break

            else:
                sink.put(trigger)
                trigger = None

        while not sink.empty():
            self.triggers.put(sink.get())

        if trigger:
            self.triggers.put(trigger)
            return (match, trigger)

        return (None, None)
Exemplo n.º 2
0
class AbstractMessageSender(metaclass=ABCMeta):
    #delegate this to a different class
    def __init__(self):
        self.message_q = LifoQueue()
        
    def send_message(self, message, sock):
        self.add_message(message)
        sock.sendall(message.message)

    def add_message(self, message):
        self.message_q.put(message)
def inorder_walk(a_root_node: BSTreeNode):
    node_stack = LifoQueue()
    current_item = a_root_node
    while True:
        while current_item:
            node_stack.put(current_item)
            current_item = current_item.left_child
        if node_stack.empty():
            break
        tmp_item = node_stack.get()
        yield tmp_item

        current_item = tmp_item.right_child
Exemplo n.º 4
0
def get_friends(inlist, output_path, subpath):
    ##########
    # task assignment
    ##########

    global lock
    lock = Lock()
    global q
    q = LifoQueue()  # screen_name queue
    global friend_list
    friend_list = dict()

    with open("./twitter_api_keys.pydict", "r") as f:
        keys = eval(f.read())

    # initiate task for crawler()
    for input in inlist:
        friend_list[input] = set()
        q.put({input_type: input, "count": 5000, "cursor": -1})

    for key in keys:
        t = Thread(target=uid_crawler, kwargs={"key": key, "name": keys.index(key), "rest_url": "friends/ids"})
        # t.daemon = True
        # time.sleep(2)
        t.start()

    q.join()

    # # RUN THIS AFTER FINISHED.
    try:
        mkdir("{}/{}".format(output_path, subpath))
    except OSError:
        pass

    print("writing to disk.", end=".", flush=True)
    if subpath == 0:
        for key, vals in list(friend_list.items()):
            print(".", end="", flush=True)
            with BZ2File("{}/{}/{}.bz2".format(output_path, subpath, key), "w") as f:
                f.writelines(map(lambda item: (str(item) + "\n").encode("utf-8"), vals))
    else:
        for key, vals in list(friend_list.items()):
            print(".", end="", flush=True)
            with ZipFile("{}/{}/{}.zip".format(output_path, subpath, str(key)[:3]), "a") as zf:
                zf.writestr(str(key), "\n".join([str(item) for item in vals]).encode("utf-8"), ZIP_LZMA)

    print("Done. Waiting remaining threads to quit", end=".", flush=True)
    while activeCount() > 1:
        print(activeCount(), end=".", flush=True)
        time.sleep(2)
    return friend_list
Exemplo n.º 5
0
class ThreadedNormalWorker(object):
    def __init__(self, print_errors=False):
        self.print_errors = print_errors
        self.queue = LifoQueue()

    def get_url_bulk(self):
        normals = Normals.objects(access_success=False)
        for i in normals:
            self.queue.put(item=i)

    def grab_from_queue(self):
        while not self.queue.empty():
            url = self.queue.get()
            normals_finder = NormalsSpider(url=url.url,
                                           print_errors=self.print_errors)
            normals_finder.update_normals_data()
            print(url.url)
            self.queue.task_done()

    def start(self, n_threads):
        self.get_url_bulk()
        for i in range(n_threads):
            thread = Thread(target=self.grab_from_queue())
            thread.start()
        self.queue.join()
Exemplo n.º 6
0
 def __init__(self, name, start_url, list_of_urls, number_of_threads,
              delayed_request=False, max_allowed_error=10):
     super().__init__(name, start_url, number_of_threads,
                      delay_request=delayed_request,
                      max_err=max_allowed_error)
     self.url_list = list_of_urls
     self.task_queue = LifoQueue()
Exemplo n.º 7
0
    def __init__(self, size, **kwargs):
        if not isinstance(size, int):
            raise TypeError("Pool 'size' arg must be an integer")

        if not size > 0:
            raise ValueError("Pool 'size' arg must be greater than zero")

        logger.debug(
            "Initializing connection pool with %d connections", size)

        self._lock = threading.Lock()
        self._queue = LifoQueue(maxsize=size)
        self._thread_connections = threading.local()

        connection_kwargs = kwargs
        connection_kwargs['autoconnect'] = False

        for i in xrange(size):
            connection = Connection(**connection_kwargs)
            self._queue.put(connection)

        # The first connection is made immediately so that trivial
        # mistakes like unresolvable host names are raised immediately.
        # Subsequent connections are connected lazily.
        with self.connection():
            pass
Exemplo n.º 8
0
    def __init__(self):
        QMainWindow.__init__(self)
        rec = QApplication.desktop().screenGeometry()
        self.w = rec.width()
        self.h = rec.height()
        self.kv = config.get('Settings', 'keys').split(' ')
        self.ki = 0
        self.keys = self.kv[self.ki]
        self.rect = QRectF(0, 0, self.w, self.h)
        self.shortcuts = []
        self.rects = LifoQueue()
        self.query = ''

        self.resize(self.w, self.h)
        self.setStyleSheet("background:rgba(0,0,0,%s)" % config.getfloat('Settings', 'background_opacity'))
        self.setAttribute(Qt.WA_TranslucentBackground);
        self.setWindowFlags(Qt.FramelessWindowHint);
        view = QGraphicsView(self)
        scene = QGraphicsScene()
        scene.setSceneRect(0, 0, self.w, self.h)
        view.setScene(scene)
        view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff);
        view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff);

        self.setCentralWidget(view)
        self.scene = scene
        self.drawLines()
        self.esc = QShortcut(QKeySequence('Esc'), self)
        self.esc.activated.connect(lambda: move(*config.get('Settings', 'mouse_home_coords').split(' ')))
        self.back = QShortcut(QKeySequence('Backspace'), self)
        self.back.activated.connect(self.goBack)
Exemplo n.º 9
0
 def __init__(self, conf, connection_class, **connection_kwargs):
     self.pid = os.getpid()
     self.conf = {"max": 0, "min": 1, "timeout": 0.1, "idle_timeout": 60, "max_lifetime": 30 * 60}
     if conf:
         self.conf.update(conf)
     self.connection_class = connection_class
     self.connection_kwargs = connection_kwargs
     self.pool = LifoQueue(self.conf["max"])
     self.diet()
Exemplo n.º 10
0
def breadth_first_search(start, goal):
    visited = [[False for x in range(500)] for y in range(500)]
    dad = [[None for x in range(500)] for y in range(500)]

    queue = Queue()
    visited[start.first()][start.second()] = True
    dad[start.first()][start.second()] = start
    current_node = start
    neighbours_ = neighbours(current_node)

    if not goal.first() - 10 <= current_node.first() <= goal.first() + 10 \
            or not \
            goal.second() - 10 <= current_node.second() <= goal.second() + 10:
        for i in range(neighbours_.__len__()):
            if visited[neighbours_[i].first()][neighbours_[i].second()] \
                    is False:
                queue.put(neighbours_[i])
                visited[neighbours_[i].first()][neighbours_[i].second()] \
                    = True
                dad[neighbours_[i].first()][neighbours_[i].second()] \
                    = current_node

    while (not goal.first() - 10 <= current_node.first() <= goal.first() + 10
            or not goal.second() - 10 <= current_node.second()
            <= goal.second() + 10) and not queue.empty():
        current_node = queue.get_nowait()
        neighbours_ = neighbours(current_node)

        for i in range(neighbours_.__len__()):
            if visited[neighbours_[i].first()][neighbours_[i].second()] \
                    is False:
                queue.put(neighbours_[i])
                visited[neighbours_[i].first()][neighbours_[i].second()] \
                    = True
                dad[neighbours_[i].first()][neighbours_[i].second()] \
                    = current_node

    path = LifoQueue()
    while not current_node.first() == start.first() \
            or not current_node.second() == start.second():
        path.put(current_node)
        current_node = dad[current_node.first()][current_node.second()]

    return path
Exemplo n.º 11
0
class QueryQueue:
    def __init__(self):
        self.queue = LifoQueue()
        self.comm_sender = CommSender()
        th = threading.Thread(target=self.send_require)
        th.start()

    def put(self, item):
        self.queue.put(item)

    def send_require(self):
        while True:
            time.sleep(1)
            c = ConnInfo.objects.all()[0]
            q = QueryInfo.objects.all()[0]
            r = RoomInfo.objects.all()[0]
            # if is logout or unconnected, only flush queue
            if c.is_log == "False" or c.is_conn == "False":
                while not self.queue.empty():
                    self.queue.get()
                continue

            # else get last item and flush queue
            if not self.queue.empty():
                query = self.queue.get()
                while not self.queue.empty():
                    self.queue.get()
                #
                m = ModeInfo.objects.all()[0]
                s = SensorInfo.objects.all()[0]
                ss = SettingInfo.objects.all()[0]
                if m.mode == 'cold' and ss.target_temp > s.current_temp:
                    query = 'standby'
                elif m.mode == 'hot' and ss.target_temp < s.current_temp:
                    query = 'standby'
                #
                q.query_speed = query
                q.save()
                r = self.comm_sender.send_msg(data={'type': 'require', 'source': r.room_number, 'speed': query})
                # if query is standby, we should change to standby immediately
                if query == 'standby' and r.json()['ack_nak'] == 'ACK':
                    q.current_speed = 'standby'
                    q.query_speed = 'None'
                    q.save()
Exemplo n.º 12
0
    def __init__(self, columns=2, rows=2, allowed_paths=[Path.up, Path.right, Path.down, Path.left], verbose=False):
        self._columns = columns
        self._rows = rows
        self._allowed_paths = allowed_paths
        self._verbose = verbose

        self._pos_x = 0
        self._pos_y = 0
        self._move_history = LifoQueue()
        self._last_move = None

        self._create_grid_matrix()
Exemplo n.º 13
0
def benchmark_iterator(components: List[str], max_builds: int) -> Iterator:
    for component, category in showfast_iterator(components=components):
        curr_metric, curr_release = None, None
        queue = LifoQueue(maxsize=max_builds)

        for benchmark in get_benchmarks(component, category):
            if not benchmark['hidden']:
                release = parse_release(benchmark['build'])

                if curr_metric != benchmark['metric']:
                    curr_metric, curr_release = benchmark['metric'], release
                    queue.queue.clear()

                if release != curr_release:
                    curr_release = release
                    queue.queue.clear()

                if queue.full():
                    yield benchmark
                else:
                    queue.put(benchmark)
    def __init__(self):
        super().__init__()
        self._handling_lock = Lock()
        self._teardown_callback_stack = LifoQueue()  # we execute callbacks in the reverse order that they were added
        self._logger = log.get_logger(__name__)
        self._handled_exceptions = Queue()

        # Set up a handler to be called when process receives SIGTERM.
        # Note: this will raise if called on a non-main thread, but we should NOT work around that here. (That could
        # prevent the teardown handler from ever being registered!) Calling code should be organized so that this
        # singleton is only ever initialized on the main thread.
        signal.signal(signal.SIGTERM, self._application_teardown_signal_handler)
        signal.signal(signal.SIGINT, self._application_teardown_signal_handler)
Exemplo n.º 15
0
    def __init__(self, name, start_url, list_of_url, number_of_threads,
                 max_err=10, delay_request=False):

        # Constructor for BaseCrawler
        """
        Crawler for the websites of type 0.
        :param list_of_url: List of URLs to start with.
        """
        super().__init__(name, start_url, number_of_threads, max_err,
                         delay_request)

        # Initialize data members
        self.task_queue = LifoQueue()
        self.url_list = list_of_url
Exemplo n.º 16
0
Arquivo: hapod.py Projeto: pymor/pymor
class LifoExecutor:

    def __init__(self, executor, max_workers=None):
        self.executor = executor
        self.max_workers = max_workers or executor._max_workers
        self.queue = LifoQueue()
        self.loop = asyncio.get_event_loop()
        self.sem = asyncio.Semaphore(self.max_workers)

    def submit(self, f, *args):
        future = self.loop.create_future()
        self.queue.put((future, f, args))
        self.loop.create_task(self.run_task())
        return future

    async def run_task(self):
        await self.sem.acquire()
        future, f, args = self.queue.get()
        executor_future = self.loop.run_in_executor(self.executor, f, *args)
        executor_future.add_done_callback(lambda f, ff=future: self.done_callback(future, f))

    def done_callback(self, future, executor_future):
        self.sem.release()
        future.set_result(executor_future.result())
Exemplo n.º 17
0
    def __init__(self, socket, maxrequests=1024):
        """
        :param socket:  connection to the server.  The socket will be closed by
            the marshall on shutdown.

        :param maxrequests: sets an upper limit on the number of requests that
            can be sent to the server without receiving a response.  Requests
            made beyond the limit will not be dropped but will instead wait for
            an earlier request to finish.  Maximum possible value is 65535.
        :type maxrequests: unsigned 16bit integer (0 <= maxtag <= 65535)
        """
        # the maximum length of a packet, including headers, that can be sent
        # by the marshall.  Should be set after receiving a version response
        self.max_message_size = 0xffffffff

        self._socket = socket

        # queue of ``(type, request, on_error, on_success, sequential)`` tuples
        # for passing requests to the send thread.  Adding false to the queue
        # will cause the send loop to exit
        self._send_queue = Queue()

        # queue of booleans used as a counter for the number of remaining
        # responses and stop the receive loop blocking on recv if no message is
        # expected
        # True indicates that a request has been sent and to expect a reply
        # False indicates that the receive loop should quit immediately
        self._recv_queue = Queue()

        # stack of available transaction tags that can be assigned to new
        # requests.
        # tags are used to identify the request that a response corresponds to.
        self._tags = LifoQueue(maxsize=maxrequests)
        for tag in range(1, maxrequests):
            self._tags.put(tag)

        # map from transaction tags (uint16) to completion callbacks
        self._callbacks = {}

        # responses to callbacks without tags are dispatched in the same order
        # the as the requests were submitted
        self._sequential_callbacks = Queue()

        self._send_thread = Thread(target=self._send_loop, daemon=True)
        self._send_thread.start()

        self._recv_thread = Thread(target=self._recv_loop, daemon=True)
        self._recv_thread.start()
Exemplo n.º 18
0
    def __init__(self, name, start_url, list_of_url, number_of_threads,
                 delay_request=False, max_allowed_errors=3):
        """

        :param name: As usual
        :param start_url: As usual
        :param list_of_url: As usual
        :param number_of_threads: As usual
        :param delay_request: As usual
        :param max_allowed_errors: As usual
        """
        super().__init__(name, start_url, number_of_threads=number_of_threads,
                         delay_request=delay_request,
                         max_err=max_allowed_errors)
        self.url_list = list_of_url
        self.task_queue = LifoQueue()
Exemplo n.º 19
0
    def __init__(self, subscriptions, publications):
        # Create queues for communication with the rlpy world
        self.observations_queue = LifoQueue(1)  # ROS updates constantly, keep only the last observation
        self.actions_queue = Queue()

        # Create the list of subscriptions so that the states can be
        # constructed from observations
        self.subscriptions = []
        self.last_state = []
        
        for subscription in subscriptions:
            self.last_state.append(0.0)
            sub = {}
            
            sub['index'] = len(self.subscriptions)  # Index in the state vector
            sub['subscriber'] = rospy.topics.Subscriber(
                subscription['path'],
                subscription['type'],
                self.subscription_callback,
                sub
            )
            sub['f'] = subscription.get('f', lambda x: x)
            
            self.subscriptions.append(sub)

        # Create the list of publications so that actions can be mapped to
        # publications
        self.publications = []
        self.actions = []
        
        for publication in publications:
            pub = {}
            
            pub['type'] = publication['type']
            pub['publisher'] = rospy.topics.Publisher(
                publication['path'],
                publication['type'],
                queue_size=10
            )

            # Add an action descriptor so that actions can be taken
            for value in publication['values']:
                self.actions.append((pub, value))
                
            self.publications.append(pub)
Exemplo n.º 20
0
    def __init__(self, com, port, maxlength = 100):

        self.maxlength = maxlength
        self._ring_buffer = deque([], maxlength)
        self._in_queue = LifoQueue()
        self._out_queue = Queue()

        self.com = com

        self.port = port
        # First, connect to the stream port
        self.com.connect(port,
                         self._in_queue,
                         self._out_queue,
                         self._ring_buffer,
                         self.on_data)

        self.lock = Lock() # lock for concurrent access to self.subscribers
        self.subscribers = []
Exemplo n.º 21
0
    def __init__(self, min_size, max_size=None):
        if not isinstance(min_size, int):
            raise TypeError('min_size should be int.')

        if max_size and not isinstance(max_size, int):
            raise TypeError('max_size should be int.')

        if min_size < 0:
            raise ValueError('min_size should be greater than 0.')

        if max_size and max_size < min_size:
            raise ValueError('max_size should be greater or equal to min_size.')

        self.__min_size = min_size
        self.__max_size = max_size or -1
        self.__size = 0
        self.__size_lock = threading.Lock()
        self.__closed = False
        self.__pool = LifoQueue()
    def __init__(self):
        super().__init__()
        self._handling_lock = Lock()
        self._teardown_callback_stack = LifoQueue()  # we execute callbacks in the reverse order that they were added
        self._logger = log.get_logger(__name__)
        self._handled_exceptions = Queue()
        self._teardown_callback_raised_exception = False

        # Set up handlers to be called when the application process receives certain signals.
        # Note: this will raise if called on a non-main thread, but we should NOT work around that here. (That could
        # prevent the teardown handler from ever being registered!) Calling code should be organized so that this
        # singleton is only ever initialized on the main thread.
        signal.signal(signal.SIGTERM, self._application_teardown_signal_handler)
        signal.signal(signal.SIGINT, self._application_teardown_signal_handler)
        try:
            signal.signal(self.SIGINFO, self._application_info_dump_signal_handler)
        except ValueError:
            self._logger.warning('Failed at registering signal handler for SIGINFO. This is expected if ClusterRunner'
                                 'is running on Windows.')
Exemplo n.º 23
0
 def __init__(self):
     data_dir = player_get_data_dir()
     self.db_path = os.path.join(data_dir, "similarity.db")
     self.gaia_db_path = os.path.join(data_dir, "gaia.db")
     self.db_queue = PriorityQueue()
     self._db_wrapper = DatabaseWrapper()
     self._db_wrapper.daemon = True
     self._db_wrapper.set_path(self.db_path)
     self._db_wrapper.set_queue(self.db_queue)
     self._db_wrapper.start()
     self.create_db()
     self.network = LastFMNetwork(api_key=API_KEY)
     self.cache_time = 90
     if GAIA:
         self.gaia_queue = LifoQueue()
         self.gaia_analyser = GaiaAnalysis(
             self.gaia_db_path, self.gaia_queue)
         self.gaia_analyser.daemon = True
         self.gaia_analyser.start()
Exemplo n.º 24
0
Arquivo: http.py Projeto: acidtv/Ducky
    def index(self, conf):

        session = requests.session()
        urls = LifoQueue()

        allowed_domains = conf['allowed_domains'].split(',')
        start = conf['url']
        ignore = re.compile(conf['ignore'])

        found = set([start])
        urls.put(start)

        while not urls.empty():
            url = urls.get()

            r = session.get(url)

            for link in BeautifulSoup(r.content, 'lxml').find_all('a'):
                link_href = link.get('href')

                if not link_href:
                    continue

                if link_href.startswith('/'):
                    link_href = urljoin(url, link_href)

                parsed = urlparse(link_href)

                if parsed.hostname not in allowed_domains:
                    continue

                if conf['ignore'] and ignore.match(link_href):
                    continue

                if link_href not in found:
                    found.add(link_href)
                    urls.put(link_href)

            file = MemoryFile(r.content)
            file.url = url
            file.mimetype = 'text/html'
            file.size = 0
            file.modified = None

            yield file
Exemplo n.º 25
0
 def __init__(self, parent, function, arguments, *, globals_=None, interfaces=()):
     self.parent = parent
     if globals_ is None:
         globals_ = {}
     if parent:
         self.stack = parent.stack
         self.blocks = parent.blocks
         self.frames = parent.frames
         self.globals = parent.globals
         self.interfaces = parent.interfaces
     else:
         self.stack = LifoQueue()
         self.blocks = LifoQueue()
         self.frames = LifoQueue()
         self.globals = globals_
         self.interfaces = interfaces
     self.local_variables = {}
     self.function = function
     self.arguments = arguments
     self.index = 0
Exemplo n.º 26
0
    def load_triggers(self):
        '''Read from all the trigger definition files listed in config value
        TRIGGER_PATHS, and add them all to a LIFO queue.'''

        self.fallback = Trigger(url=app.config['DEFAULT_SEARCH'], pattern='')
        self.triggers = LifoQueue()
        count = 0

        for filepath in app.config['TRIGGER_PATHS']:
            if not path.isfile(filepath):
                app.logger.warning('trigger path %s not found', filepath)
                continue

            with open(filepath) as f:
                triggers = json.loads(f.read())

            if not isinstance(triggers, dict):
                app.logger.warning('trigger path %s has invalid structure',
                                   filepath)
                continue

            for pattern, value in triggers.items():
                if isinstance(value, basestring):
                    trigger = Trigger(url=value, pattern=pattern)

                elif isinstance(value, dict):
                    trigger = Trigger(data=value, pattern=pattern)

                else:
                    # don't know what to do with anything else atm
                    continue

                self.triggers.put(trigger)
                count += 1

        app.logger.info('loaded %d triggers', count)
Exemplo n.º 27
0
class VideoAnalyzer:
    def __init__(self, config):
        self.q = LifoQueue()
        self.config = config
        self.photo_url = config["camera_settings"]["photo_url"]
        self.shape_detector = Detector(config)
        self.last_frame_data = None

    def load_video(self):
        while True:
            img_resp = requests.get(url=self.photo_url)
            im_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
            img = cv2.imdecode(im_arr, -1)
            # newX, newY = img.shape[1], img.shape[0]
            # img = cv2.resize(img, (int(newX), int(newY)))
            # cv2.imshow("AndroidCam", img)
            self.analyze(img)
            if cv2.waitKey(1) == 27:
                break

    def start_image_getting(self):
        t_imget = threading.Thread(target=self.image_get_thread, args=[self.q])
        t_imget.start()

    def image_get_thread(self, q: LifoQueue):
        while True:
            img_resp = requests.get(url=self.photo_url)
            if q.full():
                q.get()
            q.put(img_resp)

    def load_photo_once(self, path=None):
        if path is None:
            img_resp = requests.get(url=self.photo_url)
            # img_resp = self.q.get()
            im_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
            img = cv2.imdecode(im_arr, -1)
        else:
            img = cv2.imread(path)
        return self.analyze(img)

    def load_photo(self, path=None):
        if path is None:
            # img_resp = requests.get(url=self.photo_url)
            img_resp = self.q.get()
            im_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
            img = cv2.imdecode(im_arr, -1)
        else:
            img = cv2.imread(path)

        return self.analyze(img)

    def analyze(self, frame):
        # sd = ShapeDetector()

        try:
            frame_data = self.shape_detector.analyze_image(
                frame, self.config["camera_settings"]["resize"])
            # print("board: " + str(frame_data[0]) + "\nmarker: " + str(frame_data[1]) + "\ntriangle: " + str(frame_data[2]))
            self.last_frame_data = frame_data
            return frame_data
        except ValueError as e:
            print("IMAGE ERROR")
            print(e)
            return self.last_frame_data
Exemplo n.º 28
0
#Implementation of stack

#Method 1
from queue import LifoQueue
maxsize=4
stack=LifoQueue()
i=1
#push
while stack.qsize()<maxsize:
	stack.put(i)
	i+=1
print("Size:",stack.qsize())

#pop
while stack.qsize()>0:
	print(stack.get())
print("Size:",stack.qsize())

#Method 2
from collections import deque 

stack = deque() 

#push
stack.append('a') 
stack.append(2) 
print('Initial stack:',stack) 

#pop
print(stack.pop()) 
print(stack.pop()) 
Exemplo n.º 29
0
#Python Program to demostrate stack implementation
#Using queue module

from queue import LifoQueue

#Initializing a stack
stack = LifoQueue(maxsize=3)

# qsize() show the number of element in the stack
print(stack.qsize())

#push() function to push the elements to the stack
stack.put('a')
stack.put('b')
stack.put('c')

print(stack.qsize())
print(stack.full())
print(stack.empty())

#POP  from stack
print(stack.get())
print(stack.get())

print(stack.full())

stack.put('e')
print(stack.full())
print(stack.qsize())

stack.get()
 def __init__(self, manager, session):
     super().__init__()
     self.manager = manager
     self.api = YandexMusicApi(session)
     self.requests_queue = LifoQueue()
Exemplo n.º 31
0
    def pos_order_un_recur(root):
        """

        stack_1: left right parent
        stack_2: parent right left
        need O(1) extra space
        :param root:
        :return:
        """
        if root is None:
            return
        print('pos-ordered without recur: ', end='')
        stack_1 = LifoQueue()
        stack_2 = LifoQueue()
        stack_1.put(root)
        while not stack_1.empty():
            root = stack_1.get()
            stack_2.put(root)
            if root.left is not None:
                stack_1.put(root.left)
            if root.right is not None:
                stack_1.put(root.right)

        while not stack_2.empty():
            print(stack_2.get().data, end=' ')
Exemplo n.º 32
0
class ImapConnectionPool(object):
    def __init__(
        self,
        account,
        host,
        port,
        username,
        password=None,
        ssl=True,
        timeout=DEFAULT_TIMEOUT,
        max_connections=DEFAULT_CONNECTIONS,
        max_attempts=DEFAULT_ATTEMPTS,
    ):
        self.account = account
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.ssl = ssl
        self.timeout = timeout
        self.max_attempts = max_attempts

        self.pool = LifoQueue()

        # Push/start all the connections
        for _ in range(max_connections):
            self.pool.put(ImapConnectionWrapper(self))

    def log(self, method, message):
        func = getattr(logger, method)
        func(f'[Account: {self.account}]: {message}')

    @contextmanager
    def get_connection(self, selected_folder=None):
        if not self.password:
            self.password = get_password('account', self.host, self.username)

        if not self.password:
            raise ConnectionSettingsError(
                self.account,
                'Missing IMAP password! Please re-enter your password in settings.',
            )

        connection = self.pool.get()
        self.log('debug',
                 f'Got connection from pool: {self.pool.qsize()} (-1)')

        try:
            if selected_folder:
                connection.set_selected_folder(selected_folder)

            yield connection

        finally:
            try:
                if selected_folder:
                    connection.unset_selected_folder()
            except Exception:
                self.log('warning', 'Failed to unselect folder!')

            self.pool.put(connection)
            self.log('debug',
                     f'Returned connection to pool: {self.pool.qsize()} (+1)')
Exemplo n.º 33
0
class CameraService(Service):
    """
    Starts the OpenCV capturing and puts the frame in order in the FIFO queue
    """
    def __init__(self, service_name=None, cam_id=0):
        super().__init__(service_name)

        _queue_size = 20
        self._queue = LifoQueue(_queue_size)

        # self._queue_size = 20
        # self._queue = Queue(self._queue_size)

        self.cam_id = cam_id
        self._capture = None
        self._open_capture()
        self._stop_event = threading.Event()
        self._do_capture = False

    def _open_capture(self):
        if isar.PLATFORM == "Windows":
            self._capture = cv2.VideoCapture(self.cam_id, cv2.CAP_DSHOW)
            width = 1920
            height = 1080
            self._capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
            self._capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

        elif isar.PLATFORM == "Linux":
            self._capture = cv2.VideoCapture(self.cam_id, cv2.CAP_V4L2)
            self._capture.set(cv2.CAP_PROP_FOURCC,
                              cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
            width = 1920
            height = 1080
            self._capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
            self._capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

        else:  # Darwin
            self._capture = cv2.VideoCapture(self.cam_id)

            # TODO: possibly for later
            # width = 1920
            # height = 1080
            # self._capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
            # self._capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
            # self.capture.set(cv2.CAP_PROP_FPS, 24)

        if not self._capture.isOpened():
            message = "Could not open camera {}".format(self.cam_id)
            raise Exception(message)

    def start(self):
        t = threading.Thread(name="CameraThread", target=self._start_capture)
        t.daemon = True
        t.start()

    def _start_capture(self):
        """
        Read OpenCV frames and put them in the Queue.
        It blocks if the queue is full.
        """
        if not self._capture.isOpened():
            self._open_capture()

        frame_number = -1
        while not self._stop_event.is_set():
            if not self._do_capture:
                continue

            if self._queue.full():
                # logger.warning("Camera _queue is full! continue.")
                continue

            # time.sleep(0.05)

            ret, frame = self._capture.read()
            if ret:
                frame_number += 1
                camera_frame = CameraFrame(frame, frame_number)
                self._queue.put(camera_frame)
            else:
                logger.error("Capture was unsuccessful.")

    def stop(self):
        """
        Stop capturing
        :return:
        """
        self._stop_event.set()

        for i in range(100):
            try:
                self._queue.get_nowait()
            except:
                pass

        for i in range(100):
            try:
                self._queue.put_nowait(isar.POISON_PILL)
            except:
                pass

        # TODO: this hangs on stop! why? I don't know
        # self._capture.release()

    def release_capture(self):
        self._capture.release()

    def start_capture(self):
        self._do_capture = True

    def stop_capture(self):
        self._do_capture = False

    def get_frame(self, flipped_x=False, flipped_y=False):
        """
        Return the frame from FIFO queue
        It blocks if the queue is empty.
        :return:
        """
        if not self._do_capture:
            raise RuntimeError(
                "_do_capture is False. Have you forgotten to call start_capture() first?"
            )

        if self._queue.empty():
            # logger.warning("Camera _queue is empty! Return None.")
            return None

        camera_frame = self._queue.get()
        if flipped_x:
            camera_frame.flip(0)

        if flipped_y:
            camera_frame.flip(1)

        return camera_frame

    def get_camera_capture_size(self):
        if self._capture:
            width = int(self._capture.get(cv2.CAP_PROP_FRAME_WIDTH))  # float
            height = int(self._capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
            return width, height
        else:
            return None
Exemplo n.º 34
0
class ScreenManagement(ScreenManager):
    pass

# Config.set('kivy','window_icon','path/to/icon.ico')

game = Builder.load_file("uno.kv")

class MainGame(App):
    def build(self):
        return game

MainGame().run()
#SOCKET PART
client = socket.socket()
client.connect(('127.0.0.1', 8443))
message = LifoQueue()
game_status = None
client.send(username.encode('utf-8'))
player_id = client.recv(2048).decode('utf-8')
def send_to_server():
    while True:
        while message.not_empty:
            send_message = {
                'username' : username,
                'action' : message.get()
            }
            client.send(json.dumps(send_message).encode('utf-8'))
            time.sleep(1)
        if tanda:
            break
Exemplo n.º 35
0
        if (home not in n) and (totalDist + streetDist < runDistance):
            route.put(n)
            print(
                "-------------------------route: ------------------------------"
            )
            print(route.qsize())
            '''temp = route
            i=0
            while i < temp.qsize():
                print(temp.get())
                i = i+1'''
            adjacent.pop(0)
            totalDist += streetDist
            router(current, adjacent, runDistance)
        elif (home in n) and (totalDist + streetDist == runDistance):
            route.put(n)
            allRoutes.put(route)
            route.get()


if __name__ == "__main__":
    app.run(debug=False)
    home = (40.006066, -83.009263)
    route = LifoQueue()
    allRoutes = LifoQueue()
    totalDist = 0
    streetDist = 1
    runDistance = 4
    adjStreet = sweep(home, 0.00005)
    router(adjStreet)
Exemplo n.º 36
0
 def __init__(self):
     self.stack = Stack()
     self.top = None
Exemplo n.º 37
0
class Sudoku:
    def __init__(self, data):
        # 数据初始化(二维的object数组)
        self.value = np.array([[0] * 9] * 9, dtype=object)  # 数独的值,包括未解决和已解决的
        self.new_points = Queue()  # 先进先出,新解(已解决值)的坐标
        self.recorder = LifoQueue()  # 先进后出,回溯器
        self.guess_times = 0  # 猜测次数

        # 九宫格的基准列表
        self.base_points = [[0, 0], [0, 3], [0, 6], [3, 0], [3, 3], [3, 6],
                            [6, 0], [6, 3], [6, 6]]

        # 整理数据
        _data = np.array(data).reshape(9, -1)
        for r in range(0, 9):
            for c in range(0, 9):
                if _data[r, c]:  # if not Zero
                    # numpy default is int32, convert to int
                    self.value[r, c] = int(_data[r, c])
                    # 新的确认的值添加到列表中,以便遍历
                    self.new_points.put((r, c))
                    # logger.debug(f'init: answer={self.value[r, c]} at {(r, c)}')
                else:  # if Zero, guess no. is 1-9
                    self.value[r, c] = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    # 剔除数字
    def _cut_num(self, point):
        r, c = point
        val = self.value[r, c]

        # 行
        for i, item in enumerate(self.value[r]):
            if isinstance(item, list):
                if item.count(val):
                    item.remove(val)

                    # 判断移除后,是否剩下一个元素
                    if len(item) == 1:
                        self.new_points.put((r, i))  # 添加坐标到“已解决”列表
                        # logger.debug(f'only one in row: answer={self.value[r, i]} at {(r, i)}')
                        self.value[r, i] = item[0]

        # 列
        for i, item in enumerate(self.value[:, c]):
            if isinstance(item, list):
                if item.count(val):
                    item.remove(val)

                    # 判断移除后,是否剩下一个元素
                    if len(item) == 1:
                        self.new_points.put((i, c))
                        # logger.debug(f'only one in col: answer={self.value[i, c]} at {(i, c)}')
                        self.value[i, c] = item[0]

        # 所在九宫格(3x3的数组)
        b_r, b_c = map(lambda x: x // 3 * 3, point)  # 九宫格基准点
        for m_r, row in enumerate(self.value[b_r:b_r + 3, b_c:b_c + 3]):
            for m_c, item in enumerate(row):
                if isinstance(item, list):
                    if item.count(val):
                        item.remove(val)

                        # 判断移除后,是否剩下一个元素
                        if len(item) == 1:
                            r = b_r + m_r
                            c = b_c + m_c
                            self.new_points.put((r, c))
                            # logger.debug(f'only one in block: answer={self.value[r, c]} at {(r, c)}')
                            self.value[r, c] = item[0]

    # 同一行、列或九宫格中, List里,可能性只有一个的情况
    def _check_one_possbile(self):
        # 同一行只有一个数字的情况
        for r in range(0, 9):
            # 只取出是这一行是List的格子
            values = list(filter(lambda x: isinstance(x, list), self.value[r]))

            for c, item in enumerate(self.value[r]):
                if isinstance(item, list):
                    for value in item:
                        if sum(map(lambda x: x.count(value), values)) == 1:
                            self.value[r, c] = value
                            self.new_points.put((r, c))
                            # logger.debug(f'list val is only one in row: answer={self.value[r, c]} at {(r, c)}')
                            return True

        # 同一列只有一个数字的情况
        for c in range(0, 9):
            values = list(
                filter(lambda x: isinstance(x, list), self.value[:, c]))

            for r, item in enumerate(self.value[:, c]):
                if isinstance(item, list):
                    for value in item:
                        if sum(map(lambda x: x.count(value), values)) == 1:
                            self.value[r, c] = value
                            self.new_points.put((r, c))
                            # logger.debug(f'list val is only one in col: answer={self.value[r, c]} at {(r, c)}')
                            return True

        # 九宫格内的单元格只有一个数字的情况
        for r, c in self.base_points:
            # reshape: 3x3 改为1维数组
            values = list(
                filter(lambda x: isinstance(x, list),
                       self.value[r:r + 3, c:c + 3].reshape(1, -1)[0]))

            for m_r, row in enumerate(self.value[r:r + 3, c:c + 3]):
                for m_c, item in enumerate(row):
                    if isinstance(item, list):
                        for value in item:
                            if sum(map(lambda x: x.count(value), values)) == 1:
                                self.value[r + m_r, c + m_c] = value
                                self.new_points.put((r + m_r, c + m_c))
                                return True

    # 同一个九宫格内数字在同一行或同一列处理(同行列隐性排除)
    def _check_same_num(self):
        for b_r, b_c in self.base_points:
            block = self.value[b_r:b_r + 3, b_c:b_c + 3]

            # 判断数字1~9在该九宫格的分布情况
            _data = block.reshape(1, -1)[0]
            for i in range(1, 10):
                result = map(
                    lambda x: 0 if not isinstance(x[1], list) else x[0] + 1
                    if x[1].count(i) else 0, enumerate(_data))
                result = list(filter(lambda x: x > 0, result))
                r_count = len(result)

                if r_count in [2, 3]:
                    # 2或3个元素才有可能同一行或同一列
                    rows = list(map(lambda x: (x - 1) // 3, result))
                    cols = list(map(lambda x: (x - 1) % 3, result))

                    if len(set(rows)) == 1:
                        # 同一行,去掉其他行的数字
                        result = list(map(lambda x: b_c + (x - 1) % 3, result))
                        row = b_r + rows[0]

                        for col in range(0, 9):
                            if not col in result:
                                item = self.value[row, col]
                                if isinstance(item, list):
                                    if item.count(i):
                                        item.remove(i)

                                        # 判断移除后,是否剩下一个元素
                                        if len(item) == 1:
                                            self.new_points.put((row, col))
                                            self.value[row, col] = item[0]
                                            return True

                    elif len(set(cols)) == 1:
                        # 同一列
                        result = list(map(lambda x: b_r + (x - 1) // 3,
                                          result))
                        col = b_c + cols[0]

                        for row in range(0, 9):
                            if not row in result:
                                item = self.value[row, col]
                                if isinstance(item, list):
                                    if item.count(i):
                                        item.remove(i)

                                        # 判断移除后,是否剩下一个元素
                                        if len(item) == 1:
                                            self.new_points.put((row, col))
                                            self.value[row, col] = item[0]
                                            return True

    # 排除法解题
    def sudo_exclude(self):
        is_run_same = True
        is_run_one = True

        while is_run_same:
            while is_run_one:
                # 剔除数字
                while not self.new_points.empty():
                    point = self.new_points.get()  # 先进先出
                    self._cut_num(point)

                # 检查List里值为单个数字的情况,如有新answer则加入new_points Queue,立即_cut_num
                is_run_one = self._check_one_possbile()

            # 检查同行或列的情况
            is_run_same = self._check_same_num()
            is_run_one = True

    # 得到有多少个确定的数字
    def get_num_count(self):
        return sum(
            map(lambda x: 1 if isinstance(x, int) else 0,
                self.value.reshape(1, -1)[0]))

    # 评分,找到最佳的猜测坐标
    def get_best_point(self):
        best_score = 0
        best_point = (0, 0)

        for r, row in enumerate(self.value):
            for c, item in enumerate(row):
                point_score = self._get_point_score((r, c))
                if best_score < point_score:
                    best_score = point_score
                    best_point = (r, c)
        return best_point

    # 计算某坐标的评分
    def _get_point_score(self, point):
        # 评分标准 (10-候选个数) + 同行确定数字个数 + 同列确定数字个数
        r, c = point
        item = self.value[r, c]

        if isinstance(item, list):
            score = 10 - len(item)
            score += sum(
                map(lambda x: 1 if isinstance(x, int) else 0, self.value[r]))
            score += sum(
                map(lambda x: 1
                    if isinstance(x, int) else 0, self.value[:, c]))
            return score
        else:
            return 0

    # 验证有没错误
    def check_value(self):
        # 行
        r = 0
        for row in self.value:
            nums = []
            lists = []
            for item in row:
                (lists if isinstance(item, list) else nums).append(item)
            if len(set(nums)) != len(nums):
                return False  # 数字要不重复
            if len(list(filter(lambda x: len(x) == 0, lists))):
                return False  # 候选列表不能为空集
            r += 1

        # 列
        for c in range(0, 9):
            nums = []
            lists = []
            col = self.value[:, c]

            for item in col:
                (lists if isinstance(item, list) else nums).append(item)
            if len(set(nums)) != len(nums):
                return False  # 数字要不重复
            if len(list(filter(lambda x: len(x) == 0, lists))):
                return False  # 候选列表不能为空集

        # 九宫格
        for b_r, b_c in self.base_points:
            nums = []
            lists = []
            block = self.value[b_r:b_r + 3, b_c:b_c + 3].reshape(1, -1)[0]

            for item in block:
                (lists if isinstance(item, list) else nums).append(item)
            if len(set(nums)) != len(nums):
                logger.debug(f'verify failed. dup in block {b_r, b_c}')
                return False  # 数字要不重复
            if len(list(filter(lambda x: len(x) == 0, lists))):
                return False  # 候选列表不能为空集
        return True

    # 猜测记录
    def record_guess(self, point, index=0):
        # 记录
        recorder = Recorder()
        recorder.point = point
        recorder.point_index = index
        # recorder.value = self.value.copy() #numpy的copy不行
        recorder.value = copy.deepcopy(self.value)
        self.recorder.put(recorder)
        self.guess_times += 1  # 记录猜测次数

        # 新一轮的排除处理
        item = self.value[point]
        # assume only 1 in this point
        self.value[point] = item[index]
        self.new_points.put(point)
        self.sudo_exclude()

    # 回溯,需要先进后出
    def recall(self):
        while True:
            if self.recorder.empty():
                raise Exception('Sudo is wrong, no answer!')
            else:
                recorder = self.recorder.get()
                point = recorder.point
                index = recorder.point_index + 1
                item = recorder.value[point]

                # 判断索引是否超出范围
                # if not exceed,则再回溯一次
                if index < len(item):
                    break
        self.value = recorder.value
        self.record_guess(point, index)

    # Main function 解题
    def sudo_solve(self):
        self.sudo_exclude()

        # 检查有没错误的,有错误的则回溯;没错误却未解开题目,则再猜测
        while True:
            if self.check_value():
                fixed_answer = self.get_num_count()
                if fixed_answer == 81:
                    break
                else:
                    # 获取最佳猜测点
                    point = self.get_best_point()

                    # 记录并处理
                    self.record_guess(point)
            else:
                # 出错,则回溯,尝试下一个猜测
                self.recall()
Exemplo n.º 38
0
    def tputs(out, str_, *args):
        """Print the given terminal capabilities
        :param out: Output stream
         :type out: _io._TextIOBase
        :param str_: the capability to output
         :type str_: str
        :param args: optional parameters
        :raises IOError: if an error occurs
        """
        index = 0
        length = len(str_)
        ifte = IFTE_NONE
        do_exec = True
        stack = LifoQueue()
        params = list(args)  # unfreeze args
        while index < length:
            ch = str_[index]
            index += 1
            if ch == '\\':
                ch = str_[index]
                index += 1
                if '0' <= ch <= '9':
                    raise Exception('Unsupported operation %s' % ch)  # TODO: define UnsupportedOperationException
                else:
                    if ch == 'e' or ch == 'E':  # or ch.lower() == 'e', it's not important
                        if do_exec:
                            out.write(unichr(27))
                    elif ch == 'n':
                        out.write('\n')
                    elif ch == 'r':
                        if do_exec:
                            out.write('\r')
                    elif ch == 't':
                        if do_exec:
                            out.write('\t')
                    elif ch == 'b':
                        if do_exec:
                            out.write('\b')
                    elif ch == 'f':
                        if do_exec:
                            out.write('\f')
                    elif ch == 's':
                        if do_exec:
                            out.write(' ')
                    elif ch == ':' or ch == '^' or ch == '\\':
                        if do_exec:
                            out.write(ch)
                    else:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
            elif ch == '^':
                ch = str_[index]
                index += 1
                if do_exec:
                    out.write(unichr(ord(ch) - ord('@')))
            elif ch == '%':
                ch = str_[index]
                index += 1
                if ch == '%':
                    if do_exec:
                        out.write('%')
                elif ch == 'p':
                    ch = str_[index]
                    index += 1
                    if do_exec:
                        stack.put(params[ord(ch) - ord('1')])
                elif ch == 'P':
                    ch = str_[index]
                    index += 1
                    if 'a' <= ch <= 'z':
                        if do_exec:
                            Curses.__dv[ord(ch) - ord('a')] = stack.get()
                    elif 'A' <= ch <= 'Z':
                        if do_exec:
                            Curses.__sv[ord(ch) - ord('A')] = stack.get()
                    else:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                elif ch == 'g':
                    ch = str_[index]
                    index += 1
                    if 'a' <= ch <= 'z':
                        if do_exec:
                            stack.put(Curses.__dv[ord(ch) - ord('a')])
                    elif 'A' <= ch <= 'Z':
                        if do_exec:
                            stack.put(Curses.__sv[ord(ch) - ord('A')])
                    else:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                elif ch == '\'':
                    ch = str_[index]
                    index += 1
                    if do_exec:
                        stack.put(ord(ch))
                    ch = str_[index]
                    index += 1
                    if do_exec:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                elif ch == '{':
                    start = index
                    while str_[index] != '}':
                        index += 1
                    if do_exec:
                        v = int(str_[start:index-1])
                        stack.put(v)
                elif ch == 'l':
                    if do_exec:
                        stack.put(len(text_type(stack.get())))
                elif ch == '+':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 + v2)
                elif ch == '-':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 - v2)
                elif ch == '*':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 * v2)
                elif ch == '/':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 / v2)
                elif ch == 'm':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 % v2)
                elif ch == '&':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 & v2)
                elif ch == '|':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 | v2)
                elif ch == '^':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 ^ v2)
                elif ch == '=':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 == v2)
                elif ch == '>':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 > v2)
                elif ch == '<':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 < v2)
                elif ch == 'A':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 != 0 and v2 != 0)
                elif ch == 'O':
                    if do_exec:
                        v2 = int(stack.get())
                        v1 = int(stack.get())
                        stack.put(v1 != 0 or v2 != 0)
                elif ch == '!':
                    if do_exec:
                        v1 = int(stack.get())
                        stack.put(v1 == 0)
                elif ch == '~':
                    if do_exec:
                        v1 = int(stack.get())
                        stack.put(~v1)
                elif ch == '?':
                    if ifte != IFTE_NONE:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                    else:
                        ifte = IFTE_IF
                elif ch == 't':
                    if ifte != IFTE_IF and ifte != IFTE_ELSE:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                    else:
                        ifte = IFTE_THEN
                    do_exec = int(stack.get()) != 0
                elif ch == 'e':
                    if ifte != IFTE_THEN:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                    else:
                        ifte = IFTE_ELSE
                    do_exec = not do_exec
                elif ch == ';':
                    if ifte == IFTE_NONE or ifte == IFTE_IF:
                        raise Exception('Illegal argument %s at ind %d' % (ch, index))
                    else:
                        ifte = IFTE_NONE
                    do_exec = True
                elif ch == 'i':
                    if len(params) >= 1:
                        params[0] = int(params[0]) + 1
                    if len(params) >= 2:
                        params[1] = int(params[1]) + 1
                elif ch == 'd':
                    out.write(text_type(int(stack.get())))
                else:
                    raise Exception('Illegal argument %s at ind %d' % (ch, index))

            elif ch == '$':
                if str_[index] == '<':
                    while str_[index] != '>':
                        index += 1
                else:
                    if do_exec:
                        out.write(ch)
            else:
                if do_exec:
                    out.write(ch)
Exemplo n.º 39
0
class MainApp(ttk.Frame):
    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.queue = LifoQueue()
        self.grid(column = 0, row = 0)
        self.dirbutton = ttk.Button(self, text='開啟資料夾', command=self.askdirectory)
        self.dirbutton.grid(column = 0, row = 0, sticky = (tkinter.W, tkinter.E))
        ttk.Label(self, text="路徑:").grid(column = 1, row = 0, sticky = (tkinter.W, tkinter.E))
        self.dirinput = ttk.Entry(self, width=60)
        self.dirinput.grid(column = 2, row = 0, sticky = (tkinter.W,tkinter.E))
        self.runbutton = ttk.Button(self, text="執行", command = self.analysisrun)
        self.runbutton.grid(column = 0, row = 1, sticky = (tkinter.W, tkinter.E))
        self.actionlabel = ttk.Label(self)
        self.actionlabel.grid(column = 1, row = 1, sticky=(tkinter.W, tkinter.E))
        self.filelabel = ttk.Label(self)
        self.filelabel.grid(column = 2, row = 1, sticky=(tkinter.W, tkinter.E))
        self.outputarea = scrolledtext.ScrolledText(self, height = 15, width = 80)
        self.outputarea.grid(column = 0, row = 2, columnspan = 3)
        self.maindic = {}
        self.filedic = {}

    def analysisrun(self):
        maindir = self.dirinput.get()
        resultdirpath = pathlib.Path(maindir + "\\result")
        if not resultdirpath.is_dir():
            resultdirpath.mkdir()
        self.setactionlabel("讀取檔案中....")
        beforefileread = time.time()
        self.update_idletasks()
        self.fill_dic_with_refresh(maindir)
        afterfileread = time.time()
        #write crude data
        self.setactionlabel("尋找breast cancer:")
        self.update()
        for k in self.maindic.keys():
            self.update()
            invfname = maindir + "\\result" + "\\" + k + "inv.json"
            cisfname = maindir+ "\\result" + "\\" + k + "cis.json"
            invasive_raw = []
            cis_raw = []
            for item in self.maindic[k]:
                if not item[0].isdigit():
                    continue
                else:
                    self.update()
                    self.setfilelabel(item)
                    self.outputarea.see(tkinter.END)
                    currentreport = reportreader.read_report_text(self.filedic[item])
                    currentclass = breastanalysis.breast_classify(currentreport)
                    ihcresult = breastanalysis.find_erprher(currentreport)
                    if not currentclass:
                        continue
                    if currentclass[0] == breastmacro.BREAST and currentclass[1] == breastmacro.INVASIVE_CARCINOMA:
                        invdic = {}
                        if ihcresult[breastmacro.ER]:
                            invdic[breastmacro.ER] = list(ihcresult[breastmacro.ER])
                        else:
                            invdic[breastmacro.ER] = None
                        if ihcresult[breastmacro.PR]:
                            invdic[breastmacro.PR] = list(ihcresult[breastmacro.PR])
                        else:
                            invdic[breastmacro.PR] = None
                        if ihcresult[breastmacro.AR]:
                            invdic[breastmacro.AR] = list(ihcresult[breastmacro.AR])
                        else:
                            invdic[breastmacro.AR] = None
                        invdic[breastmacro.HERTWO] = ihcresult[breastmacro.HERTWO]
                        invdic[reportreader.ORGANNAME] = breastmacro.BREAST
                        invdic[breastmacro.BREASTCLASS] = breastmacro.INVASIVE_CARCINOMA
                        invdic[reportreader.DIAGNOSIS] = currentclass[2]
                        invdic[reportreader.PATHOLOGYNO] = item
                        invasive_raw.append(invdic)
                        outputstr = "找到乳癌: {0}, cell type: {5}, ER: {1}, PR: {2}, AR: {3}, Her-2: {4}\n".format(item,
                                    invdic[breastmacro.ER], invdic[breastmacro.PR], invdic[breastmacro.AR], invdic[breastmacro.HERTWO],
                                    currentclass[2])
                        self.tooutput(outputstr)
                        self.update()
                    if currentclass[0] == breastmacro.BREAST and currentclass[1] == breastmacro.CIS:
                        cisdic = {}
                        if ihcresult[breastmacro.ER]:
                            cisdic[breastmacro.ER] = list(ihcresult[breastmacro.ER])
                        else:
                            cisdic[breastmacro.ER] = None
                        if ihcresult[breastmacro.PR]:
                            cisdic[breastmacro.PR] = list(ihcresult[breastmacro.PR])
                        else:
                            cisdic[breastmacro.PR] = None
                        cisdic[reportreader.ORGANNAME] = breastmacro.BREAST
                        cisdic[breastmacro.BREASTCLASS] = breastmacro.CIS
                        cisdic[reportreader.DIAGNOSIS] = currentclass[2]
                        cisdic[reportreader.PATHOLOGYNO] = item
                        cis_raw.append(cisdic)
                        self.update()
            with open(invfname, encoding="utf-8", mode="w") as f:
                json.dump(invasive_raw, f)
            with open(cisfname, encoding="utf-8", mode="w") as f:
                json.dump(cis_raw, f)
        self.update()
        afteranalysis = time.time()
        self.setactionlabel("統計結果中")
        self.update_idletasks()
        listrawinv = list(resultdirpath.glob("*inv.json"))
        csvpath = maindir + "\\result\\result.csv"
        with open(csvpath, mode = "w", encoding = "utf-8", newline="") as csvfile:
            spamwriter = csv.writer(csvfile)
            spamwriter.writerow(["月份", "total invasive", "ER positive",
                "PR positive", "AR positive", "HER2 score 0", "HER2 score 1+",
                "HER2 score 2+", "HER2 score 3+"])
            for item in listrawinv:
                itemcsv = maindir + "\\result\\" + item.name[:5] + "invlist.csv"
                currentmonth = item.name[:5]
                with item.open(mode = "r", encoding="utf-8") as f:
                    itemdata = json.load(f)
        #print(item)
                with open(itemcsv, mode = "w", encoding = "utf-8", newline="") as itemcsvfile:
                    secondspamwriter = csv.writer(itemcsvfile)
                    for data in itemdata:
                        secondspamwriter.writerow([data[reportreader.PATHOLOGYNO], data[reportreader.DIAGNOSIS],
                        "ER:{0}".format(data[breastmacro.ER]), "PR:{0}".format(data[breastmacro.PR]), "AR:{0}".format(data[breastmacro.AR]),
                        "Her-2:{}".format(data[breastmacro.HERTWO])])
                ihcdata = take_monthlist_data_inv(itemdata)
                spamwriter.writerow([currentmonth] + list(ihcdata))
                resultrow = ["total invasive", "ER positive",
                    "PR positive", "AR positive", "HER2 score 0", "HER2 score 1+",
                    "HER2 score 2+", "HER2 score 3+"]
                outputstr = "Month: {0}".format(currentmonth)
                for (k,v) in enumerate(resultrow):
                    try:
                        outputstr = outputstr + " {0}:{1}\n".format(v, ihcdata[k])
                    except IndexError:
                        continue
                self.tooutput(outputstr)
                self.update_idletasks()
                self.outputarea.see(tkinter.END)
        outputstr = "讀檔案總共花:{0}秒,分析總共花: {1}秒".format(afterfileread-beforefileread, afteranalysis-afterfileread)
        self.tooutput(outputstr)
        self.update_idletasks()
        self.outputarea.see(tkinter.END)


    def askdirectory(self):
        self.dirinput.delete(0, "end")
        self.dirinput.insert(0, filedialog.askdirectory())

    def setfilelabel(self, txt):
        self.filelabel.config(text = txt)

    def setactionlabel(self, txt):
        self.actionlabel.config(text = txt)

    def tooutput(self, txt):
        self.outputarea.insert(tkinter.INSERT, txt)

    def refresh(self):
        self.update()
        print("refresh")
        if not self.queue.empty():
            txt = self.queue.qsize()
            print(txt)
            self.setfilelabel(txt)
            self.update()
        self.after(500, self.refresh)

    def fill_dic_with_refresh(self, maindir):
        t = Thread(target = self.fill_dic, args=[maindir])
        t.start()
        while True:
            txt = self.queue.get()
            self.setfilelabel(txt)
            self.update()
            self.update_idletasks()
            if not t.is_alive():
                break

    def fill_dic(self, maindir):
        self.maindic, self.filedic = readlist.make_dic_from_folder(maindir, self.queue)
Exemplo n.º 40
0
 def __init__(self, config):
     self.q = LifoQueue()
     self.config = config
     self.photo_url = config["camera_settings"]["photo_url"]
     self.shape_detector = Detector(config)
     self.last_frame_data = None
Exemplo n.º 41
0
 def image_get_thread(self, q: LifoQueue):
     while True:
         img_resp = requests.get(url=self.photo_url)
         if q.full():
             q.get()
         q.put(img_resp)
Exemplo n.º 42
0
# print(q.get_nowait())  #queue.Empty

# Queue(3)  =>  指定队列长度, 元素个数只能是3个;
q2 = Queue(3)
q2.put(1000)
q2.put(2000)
q2.put(3000)
# q2.put(4000)  #阻塞

# q2.put_nowait(6000)  报错

# (2) LifoQueue
# """先进后出,后进先出(栈的特点)"""
from queue import LifoQueue
lq = LifoQueue()
lq.put(100)
lq.put(101)
lq.put(102)

print(lq.get())  #102
print(lq.get())  #101
print(lq.get())  #100
print('-----------------1')

# (3) PriorityQueue
# """按照优先级顺序进行排序存放(默认从小到大)"""
# """在一个优先级队列中,要放同一类型的数据,不能混合使用"""
from queue import PriorityQueue
pq = PriorityQueue()
Exemplo n.º 43
0
    def val_rpn(self, tokens):
        if not tokens:
            return 0

        q = LifoQueue()

        for i in tokens:
            for i in ['+', '-', '*', '/']:
                o2 = q.get()
                o1 = q.get()

                if i == '+':
                    q.put(o1 + o2)
                elif i == '-':
                    q.put(o1 - o2)
                elif i == '*':
                    q.put(o1 * o2)
                elif i == '/':
                    q.put(-(-o1 // o2) if o1 * o2 < 0 else o1 // o2)
                else:
                    q.put(int(i))

            return q.get()
Exemplo n.º 44
0
import sys
import random
import numpy as np
from queue import LifoQueue

# from playsound import playsound as ps

bst_fst_sucStack_h1 = LifoQueue()
bst_fst_sucStack_h2 = LifoQueue()
bst_fst_sucStack_h3 = LifoQueue()

arg_err_msg = 'Sorry, your input puzzle wasn\'t formatted correcty. It should be a permutation of the form: 1 2 3 4 5 6 7 8 _'

mhtn = {
    1: [0, 1, 2, 1, 2, 3, 2, 3, 4],
    2: [1, 0, 1, 2, 1, 2, 2, 2, 3],
    3: [2, 1, 0, 3, 2, 1, 4, 3, 2],
    4: [1, 2, 3, 0, 1, 2, 1, 2, 3],
    5: [2, 1, 2, 1, 0, 1, 2, 1, 2],
    6: [3, 2, 1, 2, 1, 0, 3, 2, 1],
    7: [2, 3, 4, 1, 2, 3, 0, 1, 2],
    8: [3, 2, 3, 2, 1, 2, 1, 0, 1],
    9: [4, 3, 2, 3, 2, 1, 2, 1, 0]
}

goal = [1, 2, 3, 4, 5, 6, 7, 8, 9]

move_dict = {'U': -3, 'R': 1, 'D': 3, 'L': -1}


def get_successors_with_memory(puzz, move_options, blank, heuristic):
Exemplo n.º 45
0
class Stream():

    def __init__(self, com, port, maxlength = 100):

        self.maxlength = maxlength
        self._ring_buffer = deque([], maxlength)
        self._in_queue = LifoQueue()
        self._out_queue = Queue()

        self.com = com

        self.port = port
        # First, connect to the stream port
        self.com.connect(port,
                         self._in_queue,
                         self._out_queue,
                         self._ring_buffer,
                         self.on_data)

        self.lock = Lock() # lock for concurrent access to self.subscribers
        self.subscribers = []

    def last(self, n = None):
        """ Returns the latest or the n latest data records read
        from the data stream.

        If called without parameter, returns a single record, as a
        Python object. If no record has been received yet, returns None.

        If called with a parameter n, returns the n latest ones or less
        if less records have been received (in older to most recent order).

        Returns None only if no data has been ever published on the stream.
        """

        # No data yet?
        if not self._ring_buffer:
            return None

        if not n:
            res = self._ring_buffer[0]
        else:
            n = min(n, self.maxlength)
            res = list(self._ring_buffer)[:n] #TODO: optimize that! currently, the whole queue is copied :-/
            res.reverse()

        return res

    def get(self, timeout = None):
        """ Blocks until a record is available from the data stream, and returns it.

        If records are already available, returns the most recent one.

        :param timeout: (default: None) If timeout is a positive number, it
        blocks at most timeout seconds and raises the Empty exception if no
        item was available within that time. If timeout is None, block
        indefinitely.

        """
        # in_queue is a LIFO: get() returns the last inserted element,
        # ie the most recent record.
        res = self._in_queue.get(True, timeout)

        # Clear the queue: we do not need to stack older result.
        pymorselogger.debug("Clearing stream incoming queue")
        with self._in_queue.mutex:
            self._in_queue.queue = []

        # Note that we may loose one records between the get() and the clear()
        # if the other thread reacquires the mutex. It is however not a big
        # issue to loose a record here.

        return res

    def on_data(self, data):
        # Since this method can takes some time (invoking all callback
        # on the new incoming data), we copy the list of callbacks
        # to release quickly the lock on self.subscribers
        with self.lock:
            cbs = self.subscribers[:]

        for cb in cbs:
            cb(data)

    def subscribe(self, cb):
        with self.lock:
            self.subscribers.append(cb)

    def unsubscribe(self, cb):
        with self.lock:
            self.subscribers.remove(cb)

    def publish(self, msg):
        self._out_queue.put(msg)
class ShipComputer:
    def __init__(self, initial_memory, inputs=None):
        self.opcodes = {}
        self.memory = [int(i) for i in initial_memory]
        self.instruction_pointer = 0
        if (isinstance(inputs, int)):
            inputs = [inputs]
        self.inputs = SimpleQueue()
        if inputs != None:
            for input in inputs:
                self.inputs.put(input)
        self.output = LifoQueue()
        self.relative_base = 0
        self.status = Status.IDLE

        self.addOpCode(
            1, 'add', lambda memory, params:
            (params[2], memory[params[0]] + memory[params[1]], None), 3)
        self.addOpCode(
            2, 'mul', lambda memory, params:
            (params[2], memory[params[0]] * memory[params[1]], None), 3)
        self.addOpCode(3, 'input', self.get_input, 1)  # SimpleQueue.get blocks
        self.addOpCode(
            4, 'output', lambda memory, params:
            (None, self.output.put(memory[params[0]]), None), 1)
        self.addOpCode(
            5, 'jump-if-true', lambda memory, params:
            (None, None, None
             if memory[params[0]] == 0 else memory[params[1]]), 2, 0)
        self.addOpCode(
            6, 'jump-if-false', lambda memory, params:
            (None, None, None
             if memory[params[0]] != 0 else memory[params[1]]), 2, 0)
        self.addOpCode(
            7, 'less-than', lambda memory, params:
            (params[2], 1
             if memory[params[0]] < memory[params[1]] else 0, None), 3)
        self.addOpCode(
            8, 'equals', lambda memory, params:
            (params[2], 1
             if memory[params[0]] == memory[params[1]] else 0, None), 3)
        self.addOpCode(9, 'rel_base', self.update_rel_base, 1)
        self.addOpCode(98, 'seti', lambda memory, params:
                       (params[1], params[0], None), 2)
        self.addOpCode(99, 'halt', lambda memory, params: (None, None, None),
                       0)

    def update_rel_base(self, memory, params):
        self.relative_base += memory[params[0]]
        return (None, None, None)

    def get_memory(self):
        return self.memory

    def get_input(self, memory, params):
        if self.inputs.qsize() == 0:
            self.status = Status.WAITING_FOR_INPUT
            return (None, None, self.instruction_pointer)
        input = self.inputs.get()  # SimpleQueue.get blocks
        self.status = Status.RUNNING
        return (params[0], input, None)

    def get_output(self):
        return self.output.get()

    def get_all_outputs(self):
        outputs = []
        while (not self.output.empty()):
            outputs.append(self.get_output())
        outputs.reverse()  # LIFO -> FIFO queue
        return outputs

    def put_input(self, value):
        return self.inputs.put(value)

    def addOpCode(self,
                  opcode,
                  name,
                  run_function,
                  parmLength,
                  ip_offset=None):
        self.opcodes[opcode] = self.createIntCode(name, run_function,
                                                  parmLength, ip_offset)

    def execute_until_blocked(self):
        while self.tick():
            pass

    def tick(self):
        self.status = Status.RUNNING
        instruction = self.memory[self.instruction_pointer]
        opcode = instruction % 100
        param_modes = [(instruction // 100) % 10, (instruction // 1000) % 10,
                       (instruction // 10000) % 10]
        if opcode in self.opcodes.keys():
            curr_inst = self.opcodes[opcode]
            parm_length = curr_inst.parameterLength
            parm_addresses = []
            for i in range(0, curr_inst.parameterLength):
                memory_address = self.instruction_pointer + 1 + i
                if param_modes[
                        i] == 0:  # position, not immediate, so dereference
                    memory_address = self.memory[memory_address]
                if param_modes[i] == 2:  # relative
                    memory_address = self.relative_base + self.memory[
                        memory_address]
                parm_addresses.append(memory_address)
            curr_inst.run(parm_addresses)
            if opcode == 99:  # halt
                self.status = Status.FINISHED
                return False
        else:
            sys.stderr.write("Error - IP opcode" + str(opcode))
        return self.status == Status.RUNNING

    def execute(self):
        try:
            while True:
                next(self.execute_concurrent())
        except StopIteration:
            return

    def execute_concurrent(self, concurrent_mode=False):
        while True:
            instruction = self.memory[self.instruction_pointer]
            opcode = instruction % 100
            param_modes = [(instruction // 100) % 10,
                           (instruction // 1000) % 10,
                           (instruction // 10000) % 10]
            if opcode in self.opcodes.keys():
                curr_inst = self.opcodes[opcode]
                parm_length = curr_inst.parameterLength
                parm_addresses = []
                for i in range(0, curr_inst.parameterLength):
                    memory_address = self.instruction_pointer + 1 + i
                    if param_modes[
                            i] == 0:  # position, not immediate, so dereference
                        memory_address = self.memory[memory_address]
                    if param_modes[i] == 2:  # relative
                        memory_address = self.relative_base + self.memory[
                            memory_address]
                    parm_addresses.append(memory_address)
                curr_inst.run(parm_addresses)
                if opcode == 99:  # halt
                    return False
                if opcode == 4 and concurrent_mode:  #output
                    yield self.get_output()
            else:
                sys.stderr.write("Error - IP opcode" + str(opcode))

    def createIntCode(self,
                      name,
                      run_function,
                      parameterLength,
                      ip_offset=None):
        return ShipComputer.IntCode(self, name, run_function, parameterLength,
                                    ip_offset)

    class IntCode:
        def __init__(self,
                     outer,
                     name,
                     run_function,
                     parameterLength,
                     ip_offset=None):
            self.computer = outer
            self.name = name
            self.run_function = run_function
            self.parameterLength = parameterLength
            self.ip_offset = ip_offset if ip_offset != None else parameterLength + 1

        def run(self, params):
            for address in params[:-1]:
                if address >= len(self.computer.memory):
                    self.computer.memory += [0] * (10 + address -
                                                   len(self.computer.memory))
            output, result, new_ip = self.run_function(self.computer.memory,
                                                       params)
            if (output != None):
                if output >= len(self.computer.memory):
                    self.computer.memory += [0] * (10 + output -
                                                   len(self.computer.memory))
                self.computer.memory[output] = result
            if (new_ip != None):
                self.computer.instruction_pointer = new_ip
            else:
                self.computer.instruction_pointer += self.parameterLength + 1
Exemplo n.º 47
0
    def pnl_lifo(self):
        """
        使用后进先出法配对成交记录
        """
        X = dict(
            zip(
                self.target.code,
                [{'buy': LifoQueue(), 'sell': LifoQueue()}
                 for i in range(len(self.target.code))]
            )
        )
        pair_table = []
        for _, data in self.target.history_table_min.iterrows():
            while True:

                if data.direction in[1, 2, -2]:
                    if data.direction in [1, 2]:
                        X[data.code]['buy'].append(
                            (data.datetime,
                             data.amount,
                             data.price,
                             data.direction)
                        )
                    elif data.direction in [-2]:
                        X[data.code]['sell'].append(
                            (data.datetime,
                             data.amount,
                             data.price,
                             data.direction)
                        )
                    break
                elif data.direction in[-1, 3, -3]:

                    rawoffset = 'buy' if data.direction in [-1, -3] else 'sell'

                    l = X[data.code][rawoffset].get()
                    if abs(l[1]) > abs(data.amount):
                        """
                        if raw> new_close:
                        """
                        temp = (l[0], l[1] + data.amount, l[2])
                        X[data.code][rawoffset].put_nowait(temp)
                        if data.amount < 0:
                            pair_table.append(
                                [
                                    data.code,
                                    data.datetime,
                                    l[0],
                                    abs(data.amount),
                                    data.price,
                                    l[2],
                                    rawoffset
                                ]
                            )
                            break
                        else:
                            pair_table.append(
                                [
                                    data.code,
                                    l[0],
                                    data.datetime,
                                    abs(data.amount),
                                    l[2],
                                    data.price,
                                    rawoffset
                                ]
                            )
                            break

                    elif abs(l[1]) < abs(data.amount):
                        data.amount = data.amount + l[1]

                        if data.amount < 0:
                            pair_table.append(
                                [
                                    data.code,
                                    data.datetime,
                                    l[0],
                                    l[1],
                                    data.price,
                                    l[2],
                                    rawoffset
                                ]
                            )
                        else:
                            pair_table.append(
                                [
                                    data.code,
                                    l[0],
                                    data.datetime,
                                    l[1],
                                    l[2],
                                    data.price,
                                    rawoffset
                                ]
                            )
                    else:
                        if data.amount < 0:
                            pair_table.append(
                                [
                                    data.code,
                                    data.datetime,
                                    l[0],
                                    abs(data.amount),
                                    data.price,
                                    l[2],
                                    rawoffset
                                ]
                            )
                            break
                        else:
                            pair_table.append(
                                [
                                    data.code,
                                    l[0],
                                    data.datetime,
                                    abs(data.amount),
                                    l[2],
                                    data.price,
                                    rawoffset
                                ]
                            )
                            break

        pair_title = [
            'code',
            'sell_date',
            'buy_date',
            'amount',
            'sell_price',
            'buy_price',
            'rawdirection'
        ]
        pnl = pd.DataFrame(pair_table, columns=pair_title)

        pnl = pnl.assign(
            unit=pnl.code.apply(lambda x: self.market_preset.get_unit(x)),
            pnl_ratio=(pnl.sell_price / pnl.buy_price) - 1,
            sell_date=pd.to_datetime(pnl.sell_date),
            buy_date=pd.to_datetime(pnl.buy_date)
        )
        pnl = pnl.assign(
            pnl_money=(pnl.sell_price - pnl.buy_price) * pnl.amount * pnl.unit,
            hold_gap=abs(pnl.sell_date - pnl.buy_date),
            if_buyopen=pnl.rawdirection == 'buy'
        )
        pnl = pnl.assign(
            openprice=pnl.if_buyopen.apply(lambda pnl: 1 if pnl else 0) *
            pnl.buy_price +
            pnl.if_buyopen.apply(lambda pnl: 0 if pnl else 1) * pnl.sell_price,
            opendate=pnl.if_buyopen.apply(lambda pnl: 1 if pnl else 0) *
            pnl.buy_date.map(str) +
            pnl.if_buyopen.apply(lambda pnl: 0 if pnl else 1) *
            pnl.sell_date.map(str),
            closeprice=pnl.if_buyopen.apply(lambda pnl: 0 if pnl else 1) *
            pnl.buy_price +
            pnl.if_buyopen.apply(lambda pnl: 1 if pnl else 0) * pnl.sell_price,
            closedate=pnl.if_buyopen.apply(lambda pnl: 0 if pnl else 1) *
            pnl.buy_date.map(str) +
            pnl.if_buyopen.apply(lambda pnl: 1 if pnl else 0) *
            pnl.sell_date.map(str)
        )
        return pnl.set_index('code')
Exemplo n.º 48
0
class CrawlerType2(BaseCrawler):
    def __init__(self, name, start_url, list_of_urls, number_of_threads,
                 delayed_request=False, max_allowed_error=10):
        super().__init__(name, start_url, number_of_threads,
                         delay_request=delayed_request,
                         max_err=max_allowed_error)
        self.url_list = list_of_urls
        self.task_queue = LifoQueue()

    def run(self):
        """
        Function to be called by subclasses to start crawler
        """
        while True:
            # Crawl cycle starts
            print_util.print_info(
                'Starting crawl with {0}'.format(
                    self.name
                ),
                Colors.BLACK
            )
            # Add URLs to task queue
            for url in self.url_list:
                self.task_queue.put(
                    {
                        'type': 0,
                        'url': url,
                        'n_errors': 0
                    }
                )
            # Start all threads
            threads = []
            for n in range(1, self.number_of_threads + 1):
                temp_thread = Thread(
                    target=self.threader,
                    args=(n,)
                )
                threads.append(temp_thread)
                temp_thread.start()
            # Wait for threads to finish
            for temp_thread in threads:
                temp_thread.join()
                # Crawl cycle ends

    def threader(self, thread_id):
        """
        Worker function
        :param thread_id: Ass usual
        """
        while not self.task_queue.empty():

            task = self.task_queue.get()
            if task['n_errors'] >= self.max_allowed_errors:
                print_util.print_warning(
                    '{0} --> Too many errors in task {1}. Skipping.'.format(
                        thread_id,
                        task
                    )
                )
                continue

            print_util.print_info(
                '{0} --> New task : {1}'.format(
                    thread_id,
                    task
                )
            )

            try:
                if task['type'] == 0:
                    self.get_artists(
                        thread_id,
                        task['url']
                    )
                elif task['type'] == 1:
                    self.get_artist(
                        thread_id,
                        task['url'],
                        task['artist']
                    )
                elif task['type'] == 2:
                    self.get_songs_from_page(
                        thread_id,
                        task['url'],
                        task['artist']
                    )
                elif task['type'] == 3:
                    self.get_song(
                        thread_id,
                        task['url'],
                        task['song'],
                        task['artist']
                    )
                print_util.print_info(
                    '{0} --> Task complete : {1}'.format(
                        thread_id,
                        task
                    ),
                    Colors.GREEN
                )
            except Exception as e:
                print_util.print_error(
                    '{0} --> Error : {1}'.format(
                        thread_id,
                        e
                    )
                )
                task['n_errors'] += 1
                self.task_queue.put(task)

    def get_artists(self, thread_id, url):

        """
        Method to get artists from a URL
        :param thread_id: As usual
        :param url: As usual
        """
        complete_url = self.start_url + url
        raw_html = open_request(complete_url, delayed=self.delay_request)

        artists_with_url = self.get_artist_with_url(raw_html)

        for artist_url, artist in artists_with_url:
            self.task_queue.put(
                {
                    'type': 1,
                    'url': artist_url,
                    'artist': artist,
                    'n_errors': 0
                }
            )

    def get_artist(self, thread_id, url, artist):
        """
        Get songs for artist from URL in two parts:
            1. Get songs from first page (:param url)
            2. Add all other pages to task queue
        :param thread_id:
        :param url:
        :param artist:
        """
        complete_url = self.start_url + url
        raw_html = open_request(complete_url, delayed=self.delay_request)

        pages = self.get_pages_for_artist(raw_html)

        # Add all songs from current page
        for song_url, song in self.get_songs(raw_html):
            self.task_queue.put(
                {
                    'type': 3,
                    'url': song_url,
                    'song': song,
                    'artist': artist,
                    'n_errors': 0
                }
            )

        # Add rest of pages in task queue
        for page in pages[1:]:
            self.task_queue.put(
                {
                    'type': 2,
                    'url': page,
                    'artist': artist,
                    'n_errors': 0
                }
            )

    def get_songs_from_page(self, thread_id, url, artist):
        """
        Get songs from other pages of artist
        :param thread_id: As usual
        :param url: As usual
        :param artist: As usual
        """
        complete_url = self.start_url + url
        raw_html = open_request(complete_url, delayed=self.delay_request)

        for song_url, song in self.get_songs(raw_html):
            self.task_queue.put(
                {
                    'type': 3,
                    'url': song_url,
                    'song': song,
                    'artist': artist,
                    'n_errors': 0
                }
            )

    def get_song(self, thread_id, url, song, artist):
        """
        Get song from a URL
        :param thread_id: As usual
        :param url: As usual
        :param song: As usual
        :param artist: Artist of song
        """
        if db_operations.exists_song(self.start_url, url):
            print_util.print_warning(
                '{0} --> Song {1} already exists. Skipping.'.format(
                    thread_id,
                    song
                )
            )
        complete_url = self.start_url + url
        raw_html = open_request(complete_url, delayed=self.delay_request)

        album, lyrics, lyricist, additional_artists = self.get_song_details(
            raw_html
        )  # Note: additional_artists are artist(s) featured in the song

        db_operations.save(
            song,
            url,
            album,
            url,
            self.start_url,
            lyrics,
            additional_artists + [artist, ],
            [artist, ],
            lyricist
        )

    def get_song_details(self, raw_html):
        """
        User overrides this method to get details about a song
        :param raw_html: HTML code of web page
        :return: Song details
        """
        return (
            'album',
            'lyrics',
            [
                'lyricist1',
                'lyricist2'
            ],
            [
                'additional_artist1',
                'additional_artist2',
            ]
        )

    def get_artist_with_url(self, raw_html):
        """
        User overrides this method to get all artists with URL from a web page
        :param raw_html: HTML code of web page
        :return: Artists with URLs
        """
        return [
            ('url1', 'artist1'),
            ('url2', 'artist2')
        ]

    def get_pages_for_artist(self, raw_html):
        """
        Get a list of pages for an artist from given HTML code
        :param raw_html: HTML code of web page
        :return: List of URLs
        """
        return [
            'url1',
            'url2'
        ]

    def get_songs(self, raw_html):
        """
        User overrides this function to get songs with URL from page's HTML
        :param raw_html: HTML code for web page
        :return: Songs with URLs
        """
        return [
            ('url1', 'song1'),
            ('url2', 'song2')
        ]
Exemplo n.º 49
0
    start_url = sys.argv[1]
    max_levels = sys.argv[2]
    keyword = sys.argv[3]
    search_type = sys.argv[4]

    # use lock to visited links so only one thread can update at a time
    visited_lock = threading.Lock()
    unvisited_lock = threading.Lock()
    # make visited links a hashed set so there are not duplicates
    # a bloom filter may improve performance with less memory
    visited_links = set()
    # create a queue of unvisited links added by threads as they scrape
    single_path = False
    if int(search_type) == 0:
        # DFS
        unvisited_links = LifoQueue()
    elif int(search_type) == 1:
        # BFS
        unvisited_links = Queue()
    elif int(search_type) == 2:
        # DFS
        NUM_THREADS = 1
        unvisited_links = LifoQueue()
        single_path = True

    threads = list()

    first_link = dict()
    first_link['url'] = start_url
    first_link['parent_url'] = None
    first_link['level'] = 0
Exemplo n.º 50
0
class Breakout(Game):
    def __init__(self):
        Game.__init__(self, 'Breakout', c.screen_width, c.screen_height,
                      os.path.abspath(c.background_image), c.frame_rate)
        self.reset_effect = None
        self.effect_start_time = None
        self.score = 0
        self.lives = c.initial_lives
        self.start_level = False
        self.paddle = None
        self.bricks = None
        self.ball = None
        self.menu_buttons = []
        self.is_game_running = False
        self.create_objects()
        self.points_per_brick = 1
        self.fo = open(os.path.abspath("../score.txt"), "w")

        self.q = LifoQueue()
        self.t = Thread(target=enqueue_output, args=(sys.stdin, self.q))
        self.t.daemon = True  # thread dies with the program
        self.t.start()

        self.padhit = False
        self.wallhit = False

    def tricky_life(self):
        if self.score < c.row_count * c.screen_width // (c.brick_width +
                                                         1) // 4:
            if self.lives > 1:
                self.lives -= 1
        elif self.score > 3 * c.row_count * c.screen_width // (c.brick_width +
                                                               1) // 4:
            self.lives += 1

    def change_paddle(self, rate):
        if rate is not 0:
            self.paddle.bounds.inflate_ip(-self.paddle.width // rate, 0)
        else:
            self.paddle.bounds.inflate_ip(self.paddle.width, 0)

    def slow_paddle(self, indicator):
        if indicator > 0:
            self.paddle.offset = self.paddle.offset * 3 // 4
        else:
            self.paddle.offset = c.paddle_speed

    def set_points_per_brick(self, points):
        self.points_per_brick = points

    def change_ball_speed(self, dy):
        self.ball.speed = (self.ball.speed[0], int(self.ball.speed[1] * dy))

    def create_menu(self):

        self.is_game_running = True
        self.start_level = True

    def create_objects(self):
        self.create_bricks()
        self.create_labels()
        self.create_menu()
        self.create_paddle()
        self.create_ball(c.screen_width // 2)

    def create_labels(self):
        self.score_label = TextObject(c.score_offset, c.status_offset_y,
                                      lambda: 'SCORE: {0}'.format(self.score),
                                      c.text_color, c.font_name, c.font_size)
        self.objects.append(self.score_label)
        self.lives_label = TextObject(c.lives_offset, c.status_offset_y,
                                      lambda: 'LIVES: {0}'.format(self.lives),
                                      c.text_color, c.font_name, c.font_size)
        self.objects.append(self.lives_label)

    def create_ball(self, x):
        speed = (4 if random.random() > 0.5 else -4, c.ball_speed)
        self.ball = Ball(x, c.screen_height // 2, c.ball_radius, c.ball_color,
                         speed)
        self.objects.append(self.ball)

    def create_paddle(self):
        paddle = Paddle((c.screen_width - c.paddle_width) // 2,
                        c.screen_height - c.paddle_height * 2, c.paddle_width,
                        c.paddle_height, c.paddle_color, c.paddle_speed)
        self.keydown_handlers[pygame.K_LEFT].append(paddle.handle)
        self.keydown_handlers[pygame.K_RIGHT].append(paddle.handle)
        self.keyup_handlers[pygame.K_LEFT].append(paddle.handle)
        self.keyup_handlers[pygame.K_RIGHT].append(paddle.handle)
        self.paddle = paddle
        self.objects.append(self.paddle)

    def create_bricks(self):
        w = c.brick_width
        h = c.brick_height
        brick_count = c.screen_width // (w + 1)
        offset_x = (c.screen_width - brick_count * (w + 1)) // 2

        bricks = []
        effects = random.sample([i for i in range(c.row_count * brick_count)],
                                24)
        random.shuffle(effects)
        for row in range(c.row_count):
            for col in range(brick_count):
                index = row * brick_count + col
                effect = None
                brick_color = c.brick_color
                if index in effects:
                    brick_color, start_effect_func, reset_effect_func = list(
                        special_effects.values())[effects.index(index) // 4]
                    effect = start_effect_func, reset_effect_func

                brick = Brick(offset_x + col * (w + 1),
                              c.offset_y + row * (h + 1), w, h, brick_color,
                              effect)
                bricks.append(brick)
                self.objects.append(brick)
        self.bricks = bricks

    def handle_ball_collisions(self):
        # Hit paddle
        s = self.ball.speed
        edge = intersect(self.paddle, self.ball)
        if edge != 0:
            speedy = int(math.copysign(s[1], -1)) if edge // 3 == 1 else s[1]
            speedx = -s[0] if edge % 3 != 0 or (edge // 3 == 1 and (
                self.paddle.moving_left or self.paddle.moving_right)) else s[0]
            self.ball.speed = (speedx, speedy)

        # Hit floor
        if self.ball.top >= c.screen_height:
            x = self.ball.centerx
            self.lives -= 1
            if self.reset_effect is not None:
                self.reset_effect(self)
                self.reset_effect = None
            if self.lives <= 0:
                self.game_over = True
            else:
                self.objects.remove(self.ball)  # Delete old ball
                self.create_ball(x)

        # Hit brick
        for brick in self.bricks:
            edge = intersect(brick, self.ball)
            if not edge:
                continue

            self.bricks.remove(brick)
            self.objects.remove(brick)
            self.score += self.points_per_brick

            self.ball.speed = (-s[0] if edge % 3 != 0 and
                               ((edge % 3) - 1.5) * s[0] < 0 else s[0],
                               -s[1] if edge // 3 != 0 and
                               (edge // 3 - 1.5) * s[1] < 0 else s[1])

            if brick.special_effect is not None:
                # Reset previous effect if any
                if self.reset_effect is not None:
                    self.reset_effect(self)

                # Trigger special effect
                self.effect_start_time = datetime.now()
                brick.special_effect[0](self)
                # Set current reset effect function
                self.reset_effect = brick.special_effect[1]
        # Hit ceiling
        if self.ball.top <= 0:
            self.ball.speed = (s[0], -s[1] if s[1] < 0 else s[1])

        # Hit wall
        if self.ball.left <= 0:
            self.ball.speed = (-s[0] if s[0] < 0 else s[0], s[1])

        if self.ball.right >= c.screen_width:
            self.ball.speed = (-s[0] if s[0] > 0 else s[0], s[1])

    def update(self):

        if not self.is_game_running:
            return

        if self.start_level:
            self.start_level = False
            self.show_message('GET READY!', centralized=True)

        if not self.bricks:
            print("{0} {1}".format(self.score, self.lives))
            self.is_game_running = False
            self.game_over = True
            return

        # Reset special effect if needed
        if self.reset_effect:
            if datetime.now() - self.effect_start_time >= timedelta(
                    seconds=c.effect_duration):
                self.reset_effect(self)
                self.reset_effect = None

        self.handle_ball_collisions()

        # Return location information to agent
        infor = " ".join((str(self.ball.centerx), str(self.ball.centery),
                          str(self.paddle.centerx), str(self.paddle.width),
                          str(self.lives), str(self.score)))
        for brick in self.bricks:
            infor += " " + str(brick.centerx)
            infor += " " + str(brick.centery)
            infor += " " + str(color2index(brick.color))
        print(infor)

        sys.stdout.flush()

        # Pre-Update paddle
        self.paddle.moving_left = 0
        self.paddle.moving_right = 0

        try:
            line = self.q.get_nowait()  # or q.get(timeout=.1)
        except Empty:
            line = []

        for i in range(len(line)):
            if line[i] == "R":
                self.paddle.moving_left = 0
                self.paddle.moving_right = 1
            elif line[i] == "L":
                self.paddle.moving_left = 1
                self.paddle.moving_right = 0

        if self.paddle.moving_left:
            dx = -(min(self.paddle.offset, self.paddle.left))
            if dx == 0:
                self.paddle.moving_left = 0
        elif self.paddle.moving_right:
            dx = min(self.paddle.offset, c.screen_width - self.paddle.right)
            if dx == 0:
                self.paddle.moving_right = 0
        else:
            dx = 0

        #Update paddle and ball(truncated)
        speedx, speedy = self.objects[-1].speed[0], self.objects[-1].speed[1]
        n_step = max(abs(speedx), abs(speedy))
        n_start = min(abs(speedx), abs(speedy))
        tmp_ball = copy.deepcopy(self.ball)
        tmp_paddle = copy.deepcopy(self.paddle)
        end = False
        hit = False
        wall = False
        for i in range(1, n_step + 1):
            if end:
                break
            tmp_ball = copy.deepcopy(self.ball)
            tmp_paddle = copy.deepcopy(self.paddle)
            if math.copysign(i * abs(speedy) // n_step, speedy) == 0:
                continue
            tmp_ball.move(math.copysign(i * abs(speedx) // n_step, speedx),
                          math.copysign(i * abs(speedy) // n_step, speedy))
            tmp_paddle.move(math.copysign(i * abs(dx) // n_step, dx), 0)
            if tmp_ball.top >= c.screen_height or\
                    tmp_ball.top <= 0:
                end = True
            if tmp_ball.left <= 0 or\
                    tmp_ball.right >= c.screen_width:
                if self.wallhit == True:
                    continue
                end = True
                wall = True
            for brick in self.bricks:
                if intersect(brick, tmp_ball) != 0:
                    end = True
                    break
            if intersect(tmp_paddle, tmp_ball) != 0:
                if self.padhit == True:
                    continue
                end = True
                self.padhit = True
                hit = True
        self.padhit = hit
        self.wallhit = wall
        self.ball = copy.deepcopy(tmp_ball)
        self.objects[-1] = self.ball
        self.paddle = copy.deepcopy(tmp_paddle)
        self.objects[-2] = self.paddle
        super().update()

        if self.game_over:
            self.fo.write("{0} {1}\n".format(self.score, self.lives))
            self.fo.close()

            return

    def show_message(self,
                     text,
                     color=colors.WHITE,
                     font_name='Arial',
                     font_size=20,
                     centralized=False):
        message = TextObject(c.screen_width // 2, c.screen_height // 2,
                             lambda: text, color, font_name, font_size)
        self.draw()
        message.draw(self.surface, centralized)
        pygame.display.update()
        time.sleep(c.message_duration)
Exemplo n.º 51
0
from queue import LifoQueue

pilha = LifoQueue()
keys = {'(': ')', '{': '}', '[': ']'}
strings = input("Entre com um conjunto de ({[]}): ")
controle = 0
for j in strings:
    if j == '(' or j == '{' or j == '[':
        pilha.put(keys[j])
    else:
        if pilha.empty() == True or j != pilha.get():
            controle = 1
            break
if controle == 0 and pilha.empty() == True:
    print("Caracteres validos!")
else:
    print("Caracteres invalidos!")
Exemplo n.º 52
0
from tkinter import *
from tkinter import ttk
import time
import controller.client_controller as controller
from queue import LifoQueue
queue = LifoQueue()


class QuizWindow:
    root = None
    frame = None
    display_data = []
    your_info = None
    opp_info = None

    def set_root(self):
        # Create the window itself
        self.root = Tk()

        # Create title for the window
        self.root.title('Quiz Wars')

        # Set window size
        self.root.geometry('350x200')

    def welcome_window(self):
        # Create frame inside window (root) to hold all widgets
        self.frame = ttk.Frame(self.root, padding="10 10 10 10")

        # Create grid layout inside the frame
        self.frame.grid(column=0, row=0, sticky=(N, W, E, S))
Exemplo n.º 53
0
    def pnl_lifo(self):
        """
        使用后进先出法配对成交记录
        """
        X = dict(
            zip(self.target.code,
                [LifoQueue() for i in range(len(self.target.code))]))
        pair_table = []
        for _, data in self.target.history_table.iterrows():
            while True:
                if X[data.code].qsize() == 0:
                    X[data.code].put((data.datetime, data.amount, data.price))
                    break
                else:
                    l = X[data.code].get()
                    if (l[1] * data.amount) < 0:
                        # 原有多仓/ 平仓 或者原有空仓/平仓

                        if abs(l[1]) > abs(data.amount):
                            temp = (l[0], l[1] + data.amount, l[2])
                            X[data.code].put_nowait(temp)
                            if data.amount < 0:
                                pair_table.append([
                                    data.code, data.datetime, l[0],
                                    abs(data.amount), data.price, l[2]
                                ])
                                break
                            else:
                                pair_table.append([
                                    data.code, l[0], data.datetime,
                                    abs(data.amount), data.price, l[2]
                                ])
                                break

                        elif abs(l[1]) < abs(data.amount):
                            data.amount = data.amount + l[1]

                            if data.amount < 0:
                                pair_table.append([
                                    data.code, data.datetime, l[0], l[1],
                                    data.price, l[2]
                                ])
                            else:
                                pair_table.append([
                                    data.code, l[0], data.datetime, l[1],
                                    data.price, l[2]
                                ])
                        else:
                            if data.amount < 0:
                                pair_table.append([
                                    data.code, data.datetime, l[0],
                                    abs(data.amount), data.price, l[2]
                                ])
                                break
                            else:
                                pair_table.append([
                                    data.code, l[0], data.datetime,
                                    abs(data.amount), data.price, l[2]
                                ])
                                break

                    else:
                        X[data.code].put_nowait(l)
                        X[data.code].put_nowait(
                            (data.datetime, data.amount, data.price))
                        break

        pair_title = [
            'code', 'sell_date', 'buy_date', 'amount', 'sell_price',
            'buy_price'
        ]
        pnl = pd.DataFrame(pair_table, columns=pair_title).set_index('code')
        pnl = pnl.assign(pnl_ratio=(pnl.sell_price / pnl.buy_price) - 1,
                         sell_date=pd.to_datetime(pnl.sell_date),
                         buy_date=pd.to_datetime(pnl.buy_date))
        pnl = pnl.assign(pnl_money=pnl.pnl_ratio * pnl.amount,
                         hold_gap=abs(pnl.sell_date - pnl.buy_date))
        return pnl
Exemplo n.º 54
0
from queue import LifoQueue
T = int(input())
for i in range(T):
    N = int(input())
    stack = LifoQueue()
    for j in range(N):
        s = input()
        if s.startswith('place'):
            l = s.split(' ')
            x = l[1]
            print(x)
            stack.put(x)
        elif stack.qsize():
            print("remove: ", end=' ')
            print(stack.get(), end= ' ')
        else:
            print("-1", end=' ')
    print()
Exemplo n.º 55
0
#implemented using list
stack = []
stack.append(100)
stack.append(200)
print(stack.pop())

#using collections.deque
from collections import deque
stack = deque()
stack.append(200)
stack.append("opop")
print(stack)
print(stack.pop())

#using Queue
from queue import LifoQueue
stack = LifoQueue(maxsize=3)
print(stack.qsize())
stack.put(30)
stack.put(80)
print(stack.get())
print(stack.qsize())
class DepthFirstSearchSampler(Sampler):
    r"""An implementation of node sampling by depth first search. The starting node
    is selected randomly and neighbors are added to the last in first out queue 
    by shuffling them randomly.

    Args:
        number_of_nodes (int): Number of nodes. Default is 100.
        seed (int): Random seed. Default is 42.
    """
    def __init__(self, number_of_nodes: int = 100, seed: int = 42):
        self.number_of_nodes = number_of_nodes
        self.seed = seed
        self._set_seed()

    def _create_seed_set(self, graph, start_node):
        """
        Creating a visited node set and a traversal path list.
        """
        self._queue = LifoQueue()
        if start_node is not None:
            if start_node >= 0 and start_node < self.backend.get_number_of_nodes(
                    graph):
                self._queue.put(start_node)
            else:
                raise ValueError("Starting node index is out of range.")
        else:
            start_node = random.choice(
                range(self.backend.get_number_of_nodes(graph)))
            self._queue.put(start_node)
        self._nodes = set()
        self._path = []

    def _extract_edges(self):
        """
        Extracting edges from the depth first search tree.
        """
        self._edges = [[self._path[i], self._path[i + 1]]
                       for i in range(len(self._path) - 1)]

    def sample(self,
               graph: Union[NXGraph, NKGraph],
               start_node: int = None) -> Union[NXGraph, NKGraph]:
        """
        Sampling a graph with randomized depth first search.

        Arg types:
            * **graph** *(NetworkX or NetworKit graph)* - The graph to be sampled from.
            * **start_node** *(int, optional)* - The start node.

        Return types:
            * **new_graph** *(NetworkX or NetworKit graph)* - The graph of sampled nodes.
        """
        self._deploy_backend(graph)
        self._check_number_of_nodes(graph)
        self._create_seed_set(graph, start_node)
        while len(self._nodes) < self.number_of_nodes:
            source = self._queue.get()
            if source not in self._nodes:
                neighbors = self.backend.get_neighbors(graph, source)
                random.shuffle(neighbors)
                for neighbor in neighbors:
                    self._queue.put(neighbor)
                self._nodes.add(source)
                self._path.append(source)
        self._extract_edges()
        new_graph = self.backend.graph_from_edgelist(self._edges)
        new_graph = self.backend.get_subgraph(new_graph, self._nodes)
        return new_graph
Exemplo n.º 57
0
class ConnectionPool(object):
    def __init__(self, conf, connection_class, **connection_kwargs):
        self.pid = os.getpid()
        self.conf = {"max": 0, "min": 1, "timeout": 0.1, "idle_timeout": 60, "max_lifetime": 30 * 60}
        if conf:
            self.conf.update(conf)
        self.connection_class = connection_class
        self.connection_kwargs = connection_kwargs
        self.pool = LifoQueue(self.conf["max"])
        self.diet()

    def _checkpid(self):
        if self.pid != os.getpid():
            logger.info("This pool is created by other process.")
            self.dispose()
            self.__init__(self.conf, self.connection_class, **self.connection_kwargs)

    def dispose(self):
        self.clear()

    def clear(self):
        while True:
            try:
                conn = self.pool.get(block=False)
                if conn:
                    self.abandon(conn)
            except Empty:
                break

    def diet(self):
        for i in range(self.conf["min"]):
            try:
                self.pool.put(None, block=False)
            except Full:
                break

    def acquire(self):
        conn = None
        try:
            conn = self.pool.get(block=True, timeout=self.conf["timeout"])
        except Empty:
            logger.warning("No idle connection, create one more.")
        if conn:
            now = time.time()
            idle_time = now - conn.access_time
            life_time = now - conn.open_time
            if (idle_time > self.conf["idle_timeout"]) or (life_time > self.conf["max_lifetime"]):
                logger.debug("Discard obsolete connection %s. idle: %d, life: %d" % (conn, idle_time, life_time))
                conn.close()
                conn = None
        return conn or self.connection_class(**self.connection_kwargs)

    def release(self, conn):
        try:
            conn.touch()
            self.pool.put(conn, block=False)
        except Full:
            logger.warning("The pool is full, discard connection %s." % conn)
            conn.close()

    def abandon(self, conn):
        conn.close()
        self.clear()
        self.diet()

    def connection(self):
        self._checkpid()
        return ConnectionGuard(self)
Exemplo n.º 58
0
def dfs_search(initial_state):
    from queue import LifoQueue
    """DFS search"""
    init_time = time.time()
    process = psutil.Process(os.getpid())
    i = 0
    fringe = LifoQueue()
    fringe.put(initial_state.config)
    n = initial_state.n
    g = Graph()
    root = Node(initial_state.config, type="dfs")
    g.add_node(root)
    while not fringe.empty():
        state = fringe.get()
        node = Node(state, "dfs")
        # visited.append(state)
        node_g = g.get_node(node.key)
        # check if node is in fringe or visited
        if node_g:
            node_g.update_visited()
            node_g.infringe = False
        else:
            g.add_node(node)
            node_g = g.get_node(node.key)
            node_g.update_visited()
            node_g.infringe = False

        state = PuzzleState(state, n)
        reached = test_goal(state)
        if reached:
            final_time = time.time()
            delta = final_time - init_time
            ram_usage = process.memory_info().rss * 1e-6
            print("Optimal Found")
            writeOutput(state)
            print("FINSHED")
            path, path_cost = g.get_path(node.key)
            nodes_expanded = i
            max_depth = g.get_max_depth()
            sol = "path_to_goal: %s \n" % path + \
                  "cost_of_path: %s \n" % path_cost + \
                  "nodes_expanded: %i \n" % nodes_expanded + \
                  "search_depth: %i \n" % path_cost + \
                  "max_search_depth: %i \n" % max_depth + \
                  "running_time: %f \n" % delta + \
                  "max_ram_usage: %f \n" % ram_usage
            print(sol)
            with open("output.txt", "wb") as file:
                file.write(sol.encode("utf-8"))
            return path, path_cost, nodes_expanded, max_depth

        elif state is not None:
            expand = state.expand()
            # EXPAND STATES
            keys_reversed = [*expand.keys()][::-1]
            for exp in keys_reversed:
                # print(exp)
                child_node = Node(expand[exp].config, type="dfs")
                ingraph = g.get_node(child_node.key)
                if ingraph:
                    node_visit = ingraph.visited
                    infringe = ingraph.infringe
                else:
                    # print("ADDING EXPANDED NODE")
                    g.add_node(child_node)
                    node_visit = False
                    infringe = False

                if not node_visit and not infringe:
                    fringe.put(expand[exp].config)
                    child_node.infringe = True
                    g.add_child(node_g.key, exp, child_node.key)
            # quit()
        i += 1
Exemplo n.º 59
0
#4. task_done()/join(): join就是在等多线程所有task_done结束

str="abcdefghijk"
import random
#Queue
q=Queue(10)
for i in range(10):
	q.put(random.choice(str))
print("size=",q.qsize())
while not q.empty():
	print(q.get())
	q.task_done()

#Lifo Queue
print("-"*10,"lifo_queue","-"*10)
lifoq=LifoQueue(10)
for i in range(10):
	lifoq.put_nowait(random.choice(str))
while not lifoq.empty():
	print(lifoq.get_nowait())
	lifoq.task_done()

#Priority Queue
print("-"*10,"priority queue","-"*10)
pq=PriorityQueue(10)
for i in range(10):
	pq.put_nowait(random.choice(str))
while not pq.empty():
	print(pq.get_nowait())
	pq.task_done()
Exemplo n.º 60
0
def quick_sort(arr):
    """
    模拟栈操作实现非递归的快速排序
    :param arr:
    :return:
    """
    if len(arr) < 2:
        return arr
    qu = LifoQueue()

    qu.put(len(arr) - 1)
    qu.put(0)
    while not qu.empty():
        left = qu.get()
        right = qu.get()
        index = part(arr, left, right)
        if left < index - 1:
            qu.put(index - 1)
            qu.put(left)
        if right > index + 1:
            qu.put(right)
            qu.put(index + 1)