class PredictionManager:
    def __init__(self):
        self.filesaver = FileSaver()

    def predict_and_save(self, ts, alt, long, lat, file_name):
        # request prediction of flight
        prediction = self.make_request(ts, HOURS_AHEAD_TO_PREDICT, alt, long, lat)
        # save prediction to file.
        self.filesaver.save_file(file_name, prediction.content)

    @staticmethod
    def gen_filename(file_str_prefix):
        file_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        file_name = '{0}_{1}.json'.format(file_str_prefix, file_time)
        return file_name

    def make_request(self, start_time: datetime, hours_ahead: int, current_alt, current_long, current_lat):
        params = {
            "launch_latitude": current_lat,
            "launch_longitude": current_long % 360,  # convert from [-180,180] to [0,360]
            "launch_altitude": current_alt - 1,
            "launch_datetime": start_time.astimezone().isoformat() + "Z",
            "ascent_rate": 0.8,
            "float_altitude": current_alt,
            "stop_datetime": (start_time + timedelta(hours=hours_ahead)).astimezone().isoformat(),
            "profile": "float_profile"

        }
        return requests.get(BASE_URL, params=params)
Example #2
0
async def download(torrent_file: str, download_location: str):
    """
    Download coroutine to start a download by accepting a torrent file and download location
    :param torrent_file: torrent file to be downloaded
    :param download_location: location to download it to
    """
    torrent = Torrent(torrent_file)

    torrent_writer = FileSaver(download_location, torrent)
    session = DownloadSession(
        torrent, torrent_writer.get_received_pieces_queue())  # FILESAVER

    done_pieces = 0

    while done_pieces < torrent.number_of_pieces:
        await torrent.get_peers()
        peers_info = list(set(torrent.peers))

        seen_peers = set()
        peers = [Peer(session, host, port) for host, port in peers_info]
        seen_peers.update([str(p) for p in peers])

        print('[Peers]: {} {}'.format(len(seen_peers), seen_peers))
        for peer in peers:
            peer.inflight_requests = 0

        print("STARTING")
        await (asyncio.gather(*[peer.download() for peer in peers]))

        print("received", len(session.received_pieces))

        print("progress", len(session.pieces_in_progress))
        pprint(session.pieces_in_progress)

        print("resetting session.pieces_in_progress")
        session.pieces_in_progress = {}

        print("alive peers")
        peers = [peer for peer in peers if peer.have_pieces is not None]

        print("bitfields")
        pprint([(peer, peer.have_pieces) for peer in peers])

        done_pieces = len(session.received_pieces)
        print("Done pieces:", done_pieces)
        print("RESTARTING")

    return True
Example #3
0
async def download(t_file : str, download_loc : str, loop=None):
    '''Entry point for client, initializes `Peers` and `DownloadSession` according to 
    configureation present in `t_file` which is the .torrent file and saves the 
    downloaded file to `download_loc` directory'''

    torrent = Torrent(t_file)
    LOG.info('Torrent : {}'.format(torrent))

    torrent_writer = FileSaver(download_loc, torrent) 
    session = DownloadSession(torrent, torrent_writer.get_received_blocks_queue())

    tracker = Tracker(torrent) # implement Tracker class
    peer_info = await tracker.get_peers()

    seen_peers = set()
    peers = [Peer(session, host, port) for host, port in peer_info]
    seen_peers.update([str(p) for p in peers])
    LOG.info('Peers : {}'.format(seen_peers))

    asyncio.gather([peer.download() for peer in peers])
	def __init__(self):
		super(SymbolsFloater, self).__init__()
		# The list containing info on pages and symbols
		self.pages = []

		self.signal_handlers = SignalHandlers(self)
		self.file_saver = FileSaver(self.pages)

		self.initializeWindow()
		self.initializeClipboard()

		self.file_saver.loadSymbolsFile()
		self.restorePages()
Example #5
0
def parse():
    car_parser = CarParser()
    url = input(
        'Введите URL с сайта auto.ria.com из раздела новые автомобили:')
    url = url.strip()
    html = car_parser.get_html(url)
    if html.status_code != 200:
        print('Error')
        return

    pages_count = car_parser.get_pages_count(html.text)
    soup = BeautifulSoup(html.text, 'html.parser')
    mark = car_parser.get_mark(soup)
    saver = FileSaver(mark)
    for page in range(1, pages_count + 1):
        print(f'Парсинг страницы {page} из {pages_count}...')
        html = car_parser.get_html(url, params={'page': page})
        soup = BeautifulSoup(html.text, 'html.parser')
        for new_car in car_parser.get_content(soup):
            saver.save(new_car, mark)

    print(f'Файл записан')
Example #6
0
async def download(torrent_file, download_path):
    torrent_info = TorrentInfo.from_file(torrent_file,
                                         download_dir=download_path)
    download_info: DownloadInfo = torrent_info.download_info
    tracker = Tracker(torrent_info)

    files = [
        download_info.files[0],
    ]  # download first file

    received_pieces_queue = asyncio.Queue()
    download_info.select_files(files)
    file_writer = FileSaver(torrent_info, files[0], received_pieces_queue)

    peers_info = await tracker.request_peers()

    sessions = list()
    for peer in peers_info:
        session = DownloadSession(torrent_info, received_pieces_queue, peer)
        sessions.append(session)

    await asyncio.gather(*[session.download() for session in sessions])
Example #7
0
 def saveFile(self, text):
     self._isTextUnsave = False
     fileSaver = FileSaver(self._currentFileLocation, text)
     fileSaver.save()
class SymbolsFloater(object):
	"""The class for main Floater window"""
	def __init__(self):
		super(SymbolsFloater, self).__init__()
		# The list containing info on pages and symbols
		self.pages = []

		self.signal_handlers = SignalHandlers(self)
		self.file_saver = FileSaver(self.pages)

		self.initializeWindow()
		self.initializeClipboard()

		self.file_saver.loadSymbolsFile()
		self.restorePages()

	def initializeClipboard(self):
		"""
		Initializes the ClipboardHandler class to handle clipboard manipulations.
		:return:
		"""
		self.clipboard = ClipboardHandler()

	def restorePages(self):
		"""
		Restores the pages in the window according to the `pages` list
		:return:
		"""
		for n, page in enumerate(self.pages):
			self.addPage(page['page_name'], mode= "restore", page_index=n)
			for sym in page['symbols']:
				self.addSymbolButton(sym, mode="restore")

	def initializeWindow(self):
		"""
		Initialization of window layout and connection of signals.
		:return:
		"""
		self.main_window = Win(topmost=True, resizable=False, title="Symbols Floater",
							   initial_position="center", focusable=False, suppress_X=True)
		# self.main_window.resize(300, 150)
		main_box = self.main_window.addBox(parent=self.main_window, orientation="vertical")
		button_row = self.main_window.addBox(parent=main_box, orientation="horizontal")
		self.main_window.addButton(lambda *args: self.main_window.hide(), label="X", parent=button_row)
		self.main_window.addButton(self.openAddPageDialog, label="Add Page", parent=button_row)
		self.main_window.addButton(self.openAddSymbolDialog, label="Add Symbols", parent=button_row)
		self.main_window_nb = self.main_window.addNotebook(parent=main_box)
		self.main_window_nb.connect("page_reordered", self.signal_handlers.tabReorder)

	def getCurrentPage(self):
		"""

		:return: The current page number
		"""
		return self.main_window_nb.get_current_page()

	def openAddSymbolDialog(self, widget):
		"""
		Opens the dialog for adding symbols.
		:param widget:
		:return:
		"""
		def ok_button_handle(widget, get_text_func):
			text = get_text_func()
			if text:
				for sym in text:
					self.addSymbolButton(sym)
				self.file_saver.saveSymbols()

		if self.main_window_nb.get_n_pages():
			dialog_window = TextDialog(ok_func=ok_button_handle, parent_window=self.main_window,
								title="Enter symbols to add")
		else:
			error_message = "Cannot create symbols without any pages!"
			small_message = "Please, create a page first!"
			dialog_window = ErrorMessageDialog(dialog_text=error_message, dialog_text_secondary=small_message,
											   dialog_title="Error!", parent=self.main_window)

	def openAddPageDialog(self, widget):
		"""
		Opens the dialog for adding pages.
		:param widget:
		:return:
		"""
		def ok_button_handle(widget, get_text_func):
			text = get_text_func()
			if text:
				self.addPage(text)
				self.file_saver.saveSymbols()

		dialog_window = TextDialog(ok_func=ok_button_handle, parent_window=self.main_window,
								title="Enter name for the new page")

	def openRenameDialog(self, widget, page_widget):
		"""
		Opens the dialog for page renaming
		:param widget:
		:param page_widget: the main widget of the page
		:return:
		"""
		def ok_button_handle(widget, get_text_func):
			text = get_text_func()
			if text:
				print(page_widget)
				self.renamePage(page_num=self.main_window_nb.page_num(page_widget), new_name=text)
				self.file_saver.saveSymbols()

		dialog_window = TextDialog(ok_func=ok_button_handle, parent_window=self.main_window,
								title="Enter new name for this page")

	def renamePage(self, page_num, new_name):
		"""
		Renames the dialog.
		:param page_num: Index of the page to rename
		:param new_name: A new name for the page
		:return:
		"""
		# Set it in the pages list
		self.pages[page_num]['page_name'] = new_name
		# Set the actual page label
		self.pages[page_num]['label_widget'].set_text(new_name)

	def addPage(self, page_label, mode=None, page_index=None):
		"""
		Add a page
		:param page_label: A text written in the label of the new page
		:param mode: if None, it simply adds a new page at the last position and updates the `pages` list.
		If 'restore', it doesn't create new entry in `pages`, only appends GTK-specific stuff there.
		This should be used when the page name and symbols are already present in the `pages` list, e.g. loaded from file.
		:param page_index: Used with `mode` set to 'restore', shows which page should be added to notebook.
		:return:
		"""
		# Remove @@ from page name, because it's in save file syntax
		page_label = page_label.replace("@@", "")

		page_grid = self.main_window.addBox(orientation="horizontal", spacing=3)
		page_widget, label_widget = self.main_window.addNotebookPage(notebook=self.main_window_nb,
																	page_widget=page_grid,
																	label_title=page_label, has_close_button=True,
																	close_func=self.signal_handlers.tabClose,
																	tab_double_click_func=self.openRenameDialog,
																	tab_double_click_func_args=(page_grid,)
																	)

		# Makes a tab draggable, so you could change order of tabs.
		self.main_window_nb.set_tab_reorderable(page_grid, True)

		if not mode:
			# Initialize a new page in `pages` list
			self.pages += [dict(page_name=page_label, symbols=list(), page_widget=page_grid, label_widget=label_widget)]
		elif mode == "restore":
			# Name and symbols already exist in `pages`. Add GTK specific stuff to a page with index `page_index`
			self.pages[page_index]["page_widget"] = page_grid
			self.pages[page_index]["label_widget"] = label_widget

		# Show all elements. It's kinda refreshing.
		self.main_window_nb.show_all()

		# Go to the newly created page. -1 means the last page, it is created in the end anyway
		# Had to put it after show_all() because it won't switch to a page with invisible child widgets
		self.main_window_nb.set_current_page(-1)

	def addSymbolButton(self, symbol, mode=None):
		"""
		Adds a symbol to a page.
		:param symbol: a symbol to add
		:param mode: If "restore", it doesn't add the symbol to `pages` list, only creates the button.
		This should be used when the symbols are already present in the `pages` list, e.g. loaded from file.
		:return:
		"""
		# get the grid widget on the current page
		page_grid = self.main_window_nb.get_nth_page(self.getCurrentPage())

		if not mode == "restore":
			# Add a symbol to list of symbols for a page
			self.pages[self.getCurrentPage()]['symbols'].append(symbol)

		# Add the button
		button = self.main_window.addButton(self.copySymbolToClipboard, label=symbol,
								parent=page_grid, args=(symbol,), min_size=(40, 40))
		# Make the button draggable with LMB, and thus - movable.
		button.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [], Gdk.DragAction.MOVE)

		# Show all elements. It's kinda refreshing.
		self.main_window_nb.show_all()

	def copySymbolToClipboard(self, widget, symbol):
		"""
		Copies a given string to clipboard
		:param widget:
		:param symbol: string to copy
		:return:
		"""
		self.clipboard.copy_text(text=symbol)

	def HelloWorld(self, *args):
		print("Hello, World!")
		print("args:", args)
Example #9
0
    def test_file_saving(self):
        Fs = FileSaver()
        res = Fs.save_file("test.json", b"abc")

        path = pl.Path("data_dump/test.json")
        self.assertIsFile(path)
 def save(self, py_object):
     time_path = datetime.datetime.now().strftime("%y_%m_%d/%H_%M_%S")
     fs = FileSaver(os.path.join(self._folder_path, time_path), self._file_path, self._file_extension,
                    self._save_method)
     return fs.save(py_object)
 def __init__(self):
     self.filesaver = FileSaver()
     self.pm = PredictionManager()
     logging.debug("Initialised periodic prediction saver")
     self.pp = PredictionPlotter()
 def save(self, py_object):
     time_path = datetime.datetime.now().strftime("%y_%m_%d/%H_%M_%S")
     fs = FileSaver(os.path.join(self._folder_path,
                                 time_path), self._file_path,
                    self._file_extension, self._save_method)
     return fs.save(py_object)
    parser.add_argument('-S', help="The strategy to be used")
    parser.add_argument('input_file',
                        help="The input file containing the SAT problem")

    # Parse the arguments
    arguments = parser.parse_args()

    if not arguments.input_file:
        raise Exception("Invalid input file")

utils.lambda_value = 0.45
utils.beta_value = 0.5

split_method = SplitMethod.DEFAULT
if arguments.S == "2":
    split_method = SplitMethod.MOM
elif arguments.S == "3":
    split_method = SplitMethod.TK

formula = Formula(Operation.AND)
formula.add_elements_from_file(arguments.input_file)

sat_solver = SatSolver(formula, split_method)
solved, values = sat_solver.solve()

output_file_name = f'{arguments.input_file}.out'
file_saver = FileSaver()
if solved:
    file_saver.save_values_in_dimacs(values, output_file_name)
else:
    file_saver.save_values_in_dimacs({}, output_file_name)
 def __init__(self):
     self.filesaver = FileSaver()