def __init__(self, dest=[]): Handler.__init__(self, dest=dest) self.last_stamp = 0.0 self.parsers = {} self.available_parsers = { "stdtime": self._parse_std_time, "spec1": self._parse_spec1_time, "vsftpd": self._parse_vsftpd_time }
def main(): args_handler = Handler() ip = args_handler.parsing_ip() tgt.send('1.1.1.1') scan = Scanner(tgt) scan.scan_ports()
def main(): ops = ['list', 'info', 'create', 'get', 'update', 'create'] def msg(name=None): return 'help message' parser = argparse.ArgumentParser(usage=msg()) parser.add_argument('command', type=str, help='lots of help') parser.add_argument('-f', '--file-name', type=str, help='gist file name', default=None) parser.add_argument('-d', '--description', type=str, help='description of the gist', default=None) parser.add_argument('-u', '--user', help='user help', default=None) args = parser.parse_args() try: args.command = args.command.lower() ops.index(args.command) except: didyoumean = difflib.get_close_matches(args.command, ops, 1) if didyoumean: print 'Did you mean: ' + didyoumean.pop() + '?' sys.exit() h = Handler() print args h.handle(args)
class Daemon(object): def __init__(self, debug=False): self.input = [] self.running = False self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.log = logger.Logger('server', debug=debug) self.handler = Handler(self) self._notify = pynotify.Notification('', '') self._state = 'running' def connect(self, arg=None): try: self.log.info('try connect server to port %s', cst.PORT) self.socket.bind((socket.gethostname(), cst.PORT)) self.log.info('server started') self.socket.listen(3) self.running = True self.input = [self.socket] self.handler.start() th = threading.Thread(target=self.start) th.start() self.notify('server started') return '' except socket.error, msg: self.log.warning('connection failed: %s', str(msg)) return msg
def __init__(self, config): """ Create a new instance of the ArchiveHandler class """ # Initialize Handler Handler.__init__(self, config) # Create Archive Logger self.archive = logging.getLogger('autmon')
def read(self): line = self.file.readline() while line: self.count += 1 Handler.push(self, {'type': 'line', 'raw': line.strip(), 'source': self.name }) line = self.file.readline() reactor.callLater(1, self.read)
def push(self, data): if (data.has_key('timestamp')): print("%s - %s : %s" % (data['timestamp'], data['source'], data['raw'])) else: print("unknown - %s : %s" % (data['source'], data['raw'])) Handler.push(self, data)
def __init__(self, source_name, filename, dest=[]): Handler.__init__(self, dest=dest) self.info("Initializing filereader at %s" % (filename)) self.name = source_name self.count = 0 self.last_report = time.time() self.file = open(filename) self.read() self.report()
def run(self): while True: try: conn, addr = self.ssock.accept() self.logger.debug("Client {} connected".format(utils.addr_to_ident(addr))) hdl = Handler(conn, addr) hdl.start() except KeyboardInterrupt: self.logger.critical("Interrupted by user. Exiting.") self.ssock.shutdown(socket.SHUT_RDWR) self.ssock.close() sys.exit(0)
def __init__(self, source_name, path, dest=[]): Handler.__init__(self, dest=dest) self.childrens = [] self.name = source_name for f in os.listdir(path): if os.path.isfile("%s/%s" % (path, f)): self.childrens.append( FileReader(f, "%s/%s" % (path, f), dest = dest)) else: self.childrens.append( DirReader(f, "%s/%s" % (path, f), dest))
def push(self, data): match = data['raw'].find("ERROR")>=0 if not self.match and match: self.info("Detected ERROR") data['type'] = 'event' data['raw'] = "@type:\"line\" AND @timestamp:[\"%s\" *] - %s" % ( data['timestamp'], data['raw']) Handler.push(self, data) self.match = match
def __init__(self, filter, startpage, maxpages): self.filter = filter self.startpage = startpage self.maxpages = maxpages filter = Filter(self.filter, self.startpage, self.maxpages) self.search_url = filter.create_filter_url(self.startpage) handler = Handler(self.search_url) self.search_results = handler.init() if self.search_results["pages"] < startpage: raise Exception("Error: startpage exceeds available pages [pages=" + str(self.search_results["pages"]) + "]") if self.search_results["pages"] < startpage + maxpages: self.maxpages = self.search_results["pages"]
def __init__(self): '''Init game.''' Handler.__init__(self) self.turns = 0 '''Holds the number of turns that have passed. Ticks up every time Game.input is called.''' self.actor = None '''Default actor to pass input to.''' if self.script != None: s = self.script.read() self.script.close() self.script = s self.scoreList = {}
def push(self, data): self.debug("Parsing time in data %s" % (data)) try: parser = self.parsers.get(data['source']) if not parser: parser = self.parsers[data['source']] = self._autodetect( data['raw']) stamp = parser(data['raw']) stamp = self._format_time(self._adjust_time(stamp)) data['timestamp'] = stamp except Exception as e: self.warning("Failed to parse time in %s" % (data['raw'])) Handler.push(self, data)
class SocketHandler(threading.Thread): ''' Takes a socket request and delegates requests/responses to the base handler. ''' def __init__(self, request): self.request = request self.handler = Handler() self.running = False threading.Thread.__init__(self) def run(self): self.sockfile = self.request[0].makefile() self.running = True while self.running: json = self.get_input() response = self.handler.handle(json, self) self.request[0].send("%s\r\n\r\n"%response) self.request[0].close() def get_input(self): json = "" for line in self.sockfile: if line.startswith('\r\n'): break json += line return json.rstrip('\r\n')
def __init__(self, command_kwargs, retcodes=None, chain=None): """ Salt job constructor @param command_kwargs - Kwarg dictionary to send to an APIClient @param retcodes - Set of acceptable process return codes. If none provided, only retcode 0 will be acceptable @param chain - Optional salt job to link this job to in sequence """ # Kwargs passed to the salt api client self.kwargs = command_kwargs # Acceptable return codes self.goodcodes = retcodes or set([0]) # Next in a sequence of SaltJob's self.chain = chain # Job id created by publish self.jid = None # Minions identified by publish self.minions = None # Minions that have finished so far self.finished_minions = set() # Response self.ret = {} # Events collected from salt self.events = {} # Handler - raises exceptions and output self.handler = Handler()
def __init__(self): # init Query super(Client, self).__init__() self.token = None self.server_info = None self.headers = {} self.config = Config().get_data() self.authentication = None data = 'data' in self.config.keys() and self.config['data'] or None if data: self.hostname = 'hostname' in data.keys() and \ data['hostname'] or '' self.sf_apikey = 'apikey' in data.keys() and \ data['apikey'] or '' self.username = '******' in data.keys() and \ data['username'] or '' self.password = '******' in data.keys() and \ data['password'] or '' self.impersonation_username = \ 'impersonation_username' in data.keys() and \ data['impersonation_username'] or '' self.token_life_time = 'token_life_time' in data.keys() and \ data['token_life_time'] or 0 self.handler = Handler(self.config) self.authentication = Authentication( hostname=self.hostname, sf_apikey=self.sf_apikey, username=self.username, password=self.password, impersonation_username=self.impersonation_username)
def handle(self): #得到log logging.config.fileConfig("log.conf") log=logging.getLogger("simpleExample") #初始化处理 cf=ConfigParser.ConfigParser() cf.read('xb.ini') root_data=cf.get("path","data_path") addr=int(cf.get("host","addr")) log.info("ROOT_DATA is %s"%root_data) h=Handler(self.request,self.client_address,threading.current_thread(),log,root_data,addr) log.info("{} New Client!".format(self.client_address)) #主处理程序 h.handle()
def load(self, data, overwrite=True): """Takes the string data for the on-disk format and loads it into the object. See dump() for a description of the on-disk format.""" for line in filter(None, data.split('\n')): parts = line.split('\t') parts = list(parts) + [''] * max(3 - len(parts), 0) # pad to length 3 with '' path, name, args = parts[:3] path = path.decode('string-escape') args = filter(None, args.split(',')) posargs, kwargs = [], {} for arg in args: if '=' in arg: k, v = arg.split('=', 1) kwargs[k.strip()] = v.strip() else: posargs.append(arg.strip()) if name == 'none' or not name: handler = None else: handler = Handler.from_name(name)(self, path, *posargs, **kwargs) self.add_file(path, handler, overwrite=overwrite)
def __init__(self): # self.id = random.uniform(0, 999999999) Rectangle.__init__(self) self.handler = Handler() self.magnetos = Magnetos() self.offset = Point() self.pivot = Point() self.selected = False self.resizing = False self.direction = NONE self.control = AUTOMATIC self.z = 0 self.hints = False from ui.canvas import Canvas self.canvas = Canvas() self.handler.is_testing = self.canvas.is_testing self.dash = [] self.thickness = 1.0 self.fill_style = COLOR self.fill_color = Color(0.25, 0.25, 0.25, 0.25) self.stroke_color = Color(0.25, 0.25, 0.25, 1.0) self.gradient = Gradient()
def __init__(self): """ MultiJob constructor """ self._jobs = {} self.client = salt.client.LocalClient(mopts=MASTER_OPTIONS) self.handler = Handler()
def start(req): form = util.FieldStorage(req) directory = 'YOUR_SAVE_FILE_DIRECTORY' api_key = 'YOUR_API_KEY' username = form.getfirst('user') if not username: raise InputDataIncorrect('Specify the username with the \'user\' GET attribute.') handling = Handler(directory, api_key, username, 50) handling.add_to_watched(form.getfirst('watched')) handling.add_video(form.getfirst('add_video')) handling.save() req.write(handling.build_html()) if not form.getfirst('watched'): handling.update_videos() req.write('Refresh the page to see new videos.')
def __init__(self): """ MultiJob constructor """ self._jobs = {} self.client = APIClient(opts=MASTER_OPTIONS) self.handler = Handler()
def __init__(self,env): Handler.__init__(self, env) self._ut = Utility() self._conf = self._ut._conf self._db = self._ut._db self._jtheme = self._ut._jtheme self._jtheme_mobile = self._ut._jtheme_mobile self._jcache = self._ut._jcache self._errors = [] self._messages = [] self._news = [] self._page = {} self._site = {} self._ = self._ut._ self._site['title'] = self._conf.SITE_TITLE self._lang = self._conf.DEFAULT_LANG self._template_add = {} self._is_mobile = self.get_is_mobile()
def __init__(self, debug=False): self.input = [] self.running = False self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.log = logger.Logger('server', debug=debug) self.handler = Handler(self) self._notify = pynotify.Notification('', '') self._state = 'running'
def post(self): playlist = models.Playlist.getPlaylistFromRequest(self.request) if not playlist: return self.redirect("/?" + Handler.warning("Playlist not found")) delSnippets = [int(v) for v in self.request.get_all('snippetIndex') if 0 <= int(v) < len(playlist.snippets)] playlist.snippets = [s for i, s in enumerate(playlist.snippets) if i not in delSnippets] playlist.put() self.redirect("/view/?" + playlist.keyForLink())
def __init__(self, config=None): """ Create a new instance of the MySQLHandler class """ # Initialize Handler Handler.__init__(self, config) # Initialize Options self.hostname = self.config['hostname'] self.port = int(self.config['port']) self.username = self.config['username'] self.password = self.config['password'] self.database = self.config['database'] self.table = self.config['table'] self.col_time = self.config['col_time'] self.col_metric = self.config['col_metric'] self.col_value = self.config['col_value'] # Connect self._connect()
def __init__(self, config=None): """ Create a new instance of the MySQLHandler class """ # Initialize Handler Handler.__init__(self, config) # Initialize Options self.hostname = self.config["hostname"] self.port = int(self.config["port"]) self.username = self.config["username"] self.password = self.config["password"] self.database = self.config["database"] self.table = self.config["table"] self.col_time = self.config["col_time"] self.col_metric = self.config["col_metric"] self.col_value = self.config["col_value"] # Connect self._connect()
def __init__(self, config=None): """ Create a new instance of the AutmonHandler class """ # Initialize Handler Handler.__init__(self, config) # Initialize Data self.socket = None # Initialize Options self.proto = self.config.get('proto', 'tcp').lower().strip() if self.config.get('compress', 'False').strip() == 'True': self.compress = True else: self.compress = False try: self.host = (self.config['host'][0], int(self.config['host'][1])) except Exception, e: self.log.error("AutmonHandler: Host parameter is invaild. %s", e)
def post(self): playlist = models.Playlist.getPlaylistFromRequest(self.request) if not playlist: return self.redirect("/?" + Handler.warning("Playlist not found")) try: playlist.snippets = [models.Snippet(**{ k: int(self.request.get(key + '_' + k)) if isinstance(getattr(models.Snippet, k), ndb.IntegerProperty) else self.request.get(key + '_' + k) for k in models.Snippet._properties}) for key in self.request.get_all('key')] except: print sys.exc_info()[0] self.redirect("/view/?" + playlist.keyForLink() + '&' + Handler.warning("There was a problem applying edits")) req_fields = models.Snippet._properties.keys() req_fields.remove('notes') if any(not v for v in [getattr(snpt, n) for snpt in playlist.snippets for n in req_fields]): return self.redirect("/view/?" + playlist.keyForLink() + '&' + Handler.warning("Required fields were missing")) playlist.put() return self.redirect("/view/?" + playlist.keyForLink() + '&' + Handler.status("Edits applied to playlist"))
@author: wanjia """ from libZotero import zotero from urllib import urlopen import datetime from handler import Handler import os from wikiparser import WikiParser import hashlib #https://api.zotero.org/users/3949286/items?page=3&key=yrQKEJNQsAKekW9GOgGVzCBG # handler = Handler() wikiparser = WikiParser() zlib = zotero.Library('user', '3949286', '<null>', 'yrQKEJNQsAKekW9GOgGVzCBG') print zlib # retrieve the first five top-level items. #items = zlib.fetchItemsTop({'limit': 6, 'content': 'json,bib,coins'}) #for item in items: # print 'Item Type: %s | Key: %s | Title: %s ' % (item.itemType,item.itemKey, item.title) term = "Blinded_experiment" url = "https://en.wikipedia.org/wiki/" + term req = urlopen(url) #.readlines() filename = os.getcwd() + "/urlcontent.html" file = open(filename, "w") file.write(req.read())
class Coindesk: def __init__(self): self.__version__ = "0.3" self.header = HEADER self.handler = Handler() options = Options() options.headless = True self.browser = webdriver.Firefox(options=options, service_log_path=os.path.devnull) self.assets_keys = [ 'title', 'author', 'published', 'updated', 'tags', 'link', 'text' ] def __del__(self): self.browser.quit() def _download(self, link, clicks=None): """ Gets webpage source code by acting like a real person - clicking and scrolling for "another set of articles" element. """ # Open headless browser with specified link self.browser.get(link) if clicks: # Sets up actions fro browser action = webdriver.ActionChains(self.browser) while clicks != 0: # Waits 5 seconds while all elements are loaded time.sleep(5) # Gets hight of the loaded window last_height = self.browser.execute_script( "return document.body.scrollHeight") try: # Finds "more articles" element #element = self.browser.find_element_by_xpath('//div[@class="cta-content"]') element = self.browser.find_element_by_xpath( '//div[@class="cta-story-stack"]') except: return None # Finds position of the element - top left corner in the window pos = element.location['y'] - element.size['height'] # Scrolls window to that position self.browser.execute_script(f"window.scrollTo(0, {pos});") #action = webdriver.ActionChains(self.browser) # Moves cursor over the element action.move_to_element(element).perform() # Waits 1 second just like a real user time.sleep(1) try: # Clicks element to load next batch of articles element.click() #self.browser.find_element_by_xpath('//div[@class="cta-content"]').click() # Counds down clicks number clicks -= 1 except: return None return self.browser.page_source def front_articles(self, link, clicks=None): content = self._download(link, clicks=clicks) if not content: return None tree = html.fromstring(content) headings = tree.xpath( '//section[@class="list-body"]//h4[@class="heading"]/text()') authors = tree.xpath('//span[@class="credit"]/a/text()') times = tree.xpath( '//section[@class="list-body"]//time[@class="time"]/text()') links = tree.xpath( '//section[@class="list-body"]//div[@class="text-content"]/a[2]/@href' ) links = ['https://www.coindesk.com' + i for i in links] tags = tree.xpath( '//section[@class="list-body"]//div[@class="text-content"]/a[1]/span/text()' ) return list(zip(headings, authors, times, links, tags)) def article(self, link): content = self._download(link) tree = html.fromstring(content) # They change this element often!!! times = tree.xpath('//div[@class="timestamps"]/time/text()') if not times: times = tree.xpath('//div[@class="datetime"]/time/text()') if not times: times = tree.xpath( '//div[@class="article-hero-datetime"]/time/text()') # Need to redo this string position in list is fixed - it will break-up fast!!! try: published = times[0] published = parser.parse(published) if len(times) == 2: updated = times[1].strip('Updated ') updated = parser.parse(updated) elif len(times) == 3: updated = times[2] updated = parser.parse(updated) else: updated = times[0] updated = parser.parse(updated) except: print(f"Error: No date at: {link}") published = "" updated = "" tf = TextFormatter() text = tf.format(content, link) return [published, updated, text] def aggregator(self, link, limit=50): """ Makes sure that number of articles are downloaded that are in the limit. """ clicks = 0 articles = self.front_articles(link, clicks=clicks) assets, items = self.back_articles(articles) while items < limit: clicks += 1 articles = self.front_articles(link, clicks=clicks) if not articles: return None, None assets, items = self.back_articles(articles) return assets, items def back_articles(self, articles): assets = {} counter = 0 for item in articles: article = {} article[self.assets_keys[0]] = item[0] id = self.handler.check_article(article) if not id: article[self.assets_keys[1]] = item[1] article[self.assets_keys[5]] = item[3] article[self.assets_keys[4]] = item[4] article_components = self.article(item[3]) article[self.assets_keys[2]] = article_components[0] article[self.assets_keys[3]] = article_components[1] article[self.assets_keys[6]] = article_components[2] id = self.handler.insert_article(article) print(f"{id}: {article[self.assets_keys[0]]}.") else: article = self.get_article(id) assets[id] = article counter += 1 return assets, counter def get_article(self, id): items = self.handler.get_article(id) if items: article = {} article[self.assets_keys[0]] = items[0] article[self.assets_keys[1]] = items[1] article[self.assets_keys[2]] = items[2] article[self.assets_keys[3]] = items[3] article[self.assets_keys[4]] = items[4] article[self.assets_keys[5]] = items[5] article[self.assets_keys[6]] = items[6] return article def loop_text_handler(self, link): """ Function for handling article text parsing. """ content = self._download(link) tf = TextFormatter() return tf.format(content, link) def main(self, limit=50, links=None): """ Main function for looping through subpages of coindesk. """ if not links: links = [BASE_URL + i for i in PAGES] for link in links: print(f"Getting articles in {link}.") _, _ = self.aggregator(link, limit=limit) def reparse_article_texts(self, forced=False): """ This is a utility function for formatting old articles text into new text with paras. Do not need to to be run again. """ ids = self.handler.get_ids() for id in ids: content = self.handler.get_content_by_id(id) if not '\n' in content or forced: link = self.handler.get_link_by_id(id) text = self.loop_text_handler(link) self.handler.update_content_by_id(id, text) print(f"Article ID: {id} updated.") time.sleep(5) else: continue
def partitioning(G, k): A = nx.adjacency_matrix(G) # Alpah cut M = Handler.alphaCut(A, 1) # eigen calculation eigenvalues, eigenvectors = np.linalg.eig(M) # define K partitionSize = k # tempEigenValues = np.absolute(eigenvalues) tempEigenValues = eigenvalues idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j)))**2 if (total > 0): z[i] = z[i] / (total**(1 / 2)) # z = np.matrix.transpose(z) # find k means paritions kmeans = KMeans(n_clusters=partitionSize, random_state=0).fit(z) lables = kmeans.labels_ array = Handler.indexArray(G.nodes(), partitionSize, lables) np.savetxt('test_1.txt', array, fmt='%r') print(lables) # New partition array partitionArray = [] # get each laplacian matrix for k in array: # sort = tempEigenvalues if (len(k) > 1): print(k) H = G.subgraph(k) r = nx.connected_components(H) for i in r: partitionArray.append(i) else: partitionArray.append(k) matrix, edgecut1 = Handler.conectivityMatrix(partitionArray, G) print(edgecut1) edgecut2 = 0 q = queue.Queue() partitionQueue = queue.Queue() partitionQueue.put(partitionArray) q.put(matrix) alpha = Handler.alphaCut(matrix, 0) partitionCount = 1 part = [] part.append(partitionArray) while (partitionCount != partitionSize): if (q.empty() is False): matrix = q.get() if (matrix.shape[0] > 1): alpha = Handler.alphaCut(matrix, 0) eigenvalues, eigenvectors = np.linalg.eig(alpha) tempEigenValues = np.absolute(eigenvalues) idx = tempEigenValues.argsort()[:2][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:, idx] z = eigenVectors # normalize the matrix for i in range(0, eigenVectors.shape[0]): total = 0 for j in range(0, eigenVectors.shape[1]): total += abs(eigenVectors.item((i, j)))**2 if (total > 0): z[i] = z[i] / (total**(1 / 2)) # norm = np.linalg.norm(z,axis=1,ord=2) # print(norm) # z = z.astype(np.float)/norm[:,None] # print(z) # find k means paritions kmeans = KMeans(n_clusters=2, random_state=0).fit(z) w = 0 p1, p2 = [], [] partition = partitionQueue.get() for p in kmeans.labels_: if (p == 0): p1.append(partition[w]) else: p2.append(partition[w]) w += 1 put1, tempedge1 = Handler.conectivityMatrix(p1, G) put2, tempedge2 = Handler.conectivityMatrix(p2, G) edgecut2 += tempedge1 + tempedge2 part.pop(0) if (len(p1) >= len(p2)): partitionQueue.put(p2) partitionQueue.put(p1) q.put(put2) q.put(put1) part.append(p2) part.append(p1) else: partitionQueue.put(p1) partitionQueue.put(p2) q.put(put1) q.put(put2) part.append(p1) part.append(p2) partitionCount += 1 partition = [] for p in part: partTemp = [] for par in p: for part in par: partTemp.append(part) partition.append(partTemp) return partition
G.add_edge('c','f',weight=0.2) G.add_edge('f','d',weight=0.7) G.add_edge('e','f',weight=0.8) G.add_edge('e','d',weight=0.6) G.add_edge('f','g',weight=1) G.add_edge('g','h',weight=0.1) G.add_edge('e','h',weight=1) """ #print(nx.adjacency_matrix(G,nodelist = ['c','e']).todense()) A = nx.adjacency_matrix(G) #Alpah cut M = Handler.alphaCut(A,1) #eigen calculation eigenvalues, eigenvectors = np.linalg.eig(M) #define K partitionSize=4 tempEigenValues = np.absolute(eigenvalues) idx = tempEigenValues.argsort()[:partitionSize][::] eigenValues = tempEigenValues[idx] eigenVectors = eigenvectors[:,idx] z = eigenVectors #normalize the matrix
def ping(self): h = Handler(self) h.ping()
nickname2 = input("Player2 please enter your nickname: ") if len(nickname2) == 0: continue else: break player1 = Player(nickname1, 50000) player2 = Player(nickname2, 50000) print('\n') count = 0 turn = randint(1, 2) print("Player" + str(turn) + " you should start.") while count != 3: count += 1 if turn == 1: var1 = Handler(player1).menu(1) var2 = Handler(player2).menu(2) result = Attack(player1, player2).start() if result == 'win': player1.successful_attacks += 1 turn = 1 else: turn = 2 elif turn == 2: var2 = Handler(player2).menu(2) var1 = Handler(player1).menu(1) result = Attack(player2, player1).start() if result == 'win': player2.successful_attacks += 1 turn = 2
def test_success(self, create_task_request_fixture: dict, handler: Handler, mock_controller: Controller): mock_controller.create_task = MagicMock(return_value=("id", True)) result = handler.create_task(None, create_task_request_fixture) assert 200 == result.get('statusCode') mock_controller.create_task.assert_called_once_with(None, CreateTaskRequest( mutation=TaskMutationRequest(content='some content')))
def handler(mock_controller): return Handler(mock_controller)
def handler(start, end, url, file): h = Handler(start, end, url, file) h.run() return h
class Object(Rectangle): """This class represents the parent of all draweable objects""" def __init__(self): #self.id = random.uniform(0, 999999999) Rectangle.__init__(self) self.handler = Handler() self.magnetos = Magnetos() self.offset = Point() self.pivot = Point() self.selected = False self.resizing = False self.direction = NONE self.control = AUTOMATIC self.z = 0 self.hints = False from ui.canvas import Canvas self.canvas = Canvas() self.handler.is_testing = self.canvas.is_testing self.dash = [] self.thickness = 1.0 self.fill_style = COLOR self.fill_color = Color(0.25, 0.25, 0.25, 0.25) self.stroke_color = Color(0.25, 0.25, 0.25, 1.0) self.gradient = Gradient() def get_properties(self): return Rectangle.get_properties(self) + [ "z", "fill_style", "fill_color", "stroke_color", "gradient" ] def post(self): pass def delete(self): self.canvas.document.pages[0].children.remove(self) def set_fill_style(self, fill_style): self.fill_style = fill_style #self.set_property("fill_style", fill_style) if fill_style == COLOR: self.set_fill_color() elif fill_style == GRADIENT: self.set_gradient() def set_gradient(self, gradient=Gradient()): #ToDo: by name and from Canvas! self.fill_style = GRADIENT self.gradient = gradient def set_fill_color(self, color=Color()): self.fill_style = COLOR self.fill_color = color def set_stroke_color(self, color=Color()): self.stroke_color = color def draw_hints(self, context): extent = 25.0 context.save() context.new_path() context.rectangle(self.x - extent, self.y - extent, extent, extent) context.set_source_rgba(130 / 255.0, 130 / 255.0, 250 / 255.0, 0.25) context.fill_preserve() context.set_line_width(1) context.set_source_rgb(130 / 255.0, 130 / 255.0, 250 / 255.0) context.stroke() context = pangocairo.CairoContext(context) layout = pangocairo.CairoContext.create_layout(context) if platform.system() == 'Windows': fontname = 'Sans' else: fontname = 'Ubuntu' text = str(int(self.z)) length = len(text) if length > 3: size = 6 text = "..." + text[length - 1:4] elif length > 2: size = 8 elif length > 1: size = 10 else: size = 12 description = '%s Bold %d' % (fontname, size) font = pango.FontDescription(description) layout.set_justify(True) layout.set_font_description(font) layout.set_text(text) context.set_source_rgb(0, 0, 0) width, height = layout.get_size() width /= pango.SCALE height /= pango.SCALE context.move_to(self.x - (extent + width) / 2, self.y - (extent + height) / 2) context.show_layout(layout) context.set_antialias(cairo.ANTIALIAS_DEFAULT) context.restore() def draw(self, context): if self.hints: self.draw_hints(context) ###context.save() if self.selected: self.handler.x = self.x self.handler.y = self.y self.handler.width = self.width self.handler.height = self.height self.post() self.handler.draw(context) if self.magnetos.magnetized: self.magnetos.draw(context) ###context.restore() def at_position(self, x, y): if not len(self.handler.control): return False return (x >= (self.x - self.handler.control[0].size / 2.0)) and\ (x <= (self.x + self.width + self.handler.control[0].size / 2.0)) and\ (y >= (self.y - self.handler.control[0].size / 2.0)) and\ (y <= (self.y + self.height + self.handler.control[0].size / 2.0)) def in_region(self, x, y, width, height): if width < 0: x += width width *= -1 if height < 0: y += height height *= -1 return (x + width) > self.x and (y + height) > self.y and\ x < (self.x + self.width) and y < (self.y + self.height) def in_selection(self, selection): return self.in_region(selection.x, selection.y, selection.width, selection.height) def transform(self, x, y): pass def get_cursor(self, direction): if direction == NORTHWEST: cursor = gtk.gdk.TOP_LEFT_CORNER elif direction == NORTH: cursor = gtk.gdk.TOP_SIDE elif direction == NORTHEAST: cursor = gtk.gdk.TOP_RIGHT_CORNER elif direction == WEST: cursor = gtk.gdk.LEFT_SIDE elif direction == EAST: cursor = gtk.gdk.RIGHT_SIDE elif direction == SOUTHWEST: cursor = gtk.gdk.BOTTOM_LEFT_CORNER elif direction == SOUTH: cursor = gtk.gdk.BOTTOM_SIDE elif direction == SOUTHEAST: cursor = gtk.gdk.BOTTOM_RIGHT_CORNER elif direction >= ANONIMOUS: cursor = gtk.gdk.CROSSHAIR else: cursor = gtk.gdk.ARROW return gtk.gdk.Cursor(cursor) def resize(self, x, y): direction = self.direction position = Position() position.x = self.x position.y = self.y size = Size() size.width = self.width size.height = self.height side = get_side(direction) if side is not VERTICAL: size.width = x - self.pivot.x if size.width < 0: position.x = x else: position.x = self.pivot.x if side is not HORIZONTAL: size.height = y - self.pivot.y if size.height < 0: position.y = y else: position.y = self.pivot.y self.set_position(position) self.set_size(size) def press(self, x, y): pass
def serve(): if request.method == "GET": return "OK" hdl = Handler(request.form) return hdl.handle()
def test_missing_body(self, create_task_request_fixture: dict, handler: Handler, mock_controller: Controller): create_task_request_fixture['body'] = '{}' mock_controller.create_task = MagicMock(return_value=(None, False)) result = handler.create_task(None, create_task_request_fixture) assert 400 == result.get('statusCode')
def size(self): if self.data: return len(self.data) else: return None class GoogleRoot: def join(self, query): return GoogleSearch(query) class GoogleSearch(HtmlNode): r_links = re.compile(r'<a\sclass=l\s[^>]*href="([^"]+)"[^>]*>(.*?)</a>', re.IGNORECASE | re.DOTALL) def __init__(self, query): self.url = 'http://www.google.com/search?q=' + query def format(self, text, index): text = text.replace('<b>', '').replace('</b>', '') text = HtmlNode.format(self, text, index) return '%d. %s' % (index, text) if __name__ == '__main__': root = Root() handler = Handler('/home/arigo/mnt', ObjectFs(root)) handler.loop_forever()
def test_success(self, get_task_request_fixture: dict, handler: Handler, mock_controller: Controller, stub_task: Task): mock_controller.get_task = MagicMock(return_value=(stub_task, True)) result = handler.get_task(None, get_task_request_fixture) assert 200 == result.get('statusCode') mock_controller.get_task.assert_called_once_with(None, GetTaskRequest(id='myid'))
def __init__(self): self.cookies = hangups.get_auth_stdin("./token.txt", True) self.client = hangups.Client(self.cookies) self.handler = Handler()
def test_without_query_param(self, get_task_request_fixture: dict, handler: Handler, mock_controller: Controller, stub_task: Task): del get_task_request_fixture['queryStringParameters']['id'] mock_controller.get_task = MagicMock(return_value=(stub_task, True)) result = handler.get_task(None, get_task_request_fixture) assert 400 == result.get('statusCode')
import numpy as np from handler import Handler import networkx as nx G = nx.read_graphml("../../Osmnx_large_trips_graph_75000.graphml") data = 'trips' with open("alpha_1_6.txt") as f: datas = f.read() # print(data.split(']')[0]) partitionArray = [] nodeCounter = 0 for k in range(0, 6): partition_data = datas.split('\n')[k].replace('{','').replace('}', '').replace(']', '').replace('[', '').split(', ') tempartition = [] for i in partition_data: for z in i.split('+'): nodeCounter+=1 tempartition.append(int(z.replace('\'',''))) partitionArray.append(tempartition) np.savetxt('test.txt', partitionArray, fmt='%r') adjecencyMatrix , pickMatrix = Handler.adjecencyMatrix(partitionArray, G, data) sum = 0 for x in range(len(pickMatrix)): sum += pickMatrix[x][x] print("6") print(pickMatrix) print(sum,(9980283-sum)*100/9980283)
def test_controller_fail(self, get_task_request_fixture: dict, handler: Handler, mock_controller: Controller): mock_controller.get_task = MagicMock(return_value=(None, False)) result = handler.get_task(None, get_task_request_fixture) assert 404 == result.get('statusCode')
if keystr == KeyBinds.pomodoro.value: handler.btn_pomodoro_clicked_cb(None) elif keystr == KeyBinds.short_break.value: handler.btn_short_break_clicked_cb(None) elif keystr == KeyBinds.long_break.value: handler.btn_long_break_clicked_cb(None) elif keystr == KeyBinds.timer.value: if handler.watch and handler.watch.is_alive(): handler.btn_stop_clicked_cb(None) else: handler.btn_start_clicked_cb(None) elif keystr == KeyBinds.reset.value: handler.btn_reset_clicked_cb(None) elif keystr == KeyBinds.quit.value: handler.window_destroy_cb(None) if __name__ == "__main__": handler = Handler builder = Gtk.Builder() builder.add_from_file("./pomodoro.glade") window = builder.get_object("window") lbl_time = builder.get_object("lbl_time") handler = Handler(lbl_time) builder.connect_signals(handler) window.show_all() Keybinder.init() for keystr in KeyBinds: Keybinder.bind(keystr.value, callback) Gtk.main()
def test_success(self, update_task_request_fixture: dict, handler: Handler, mock_controller: Controller): mock_controller.update_task = MagicMock(return_value=True) result = handler.update_task(None, update_task_request_fixture) assert 204 == result.get('statusCode') mock_controller.update_task.assert_called_once_with(None, UpdateTaskRequest(id='myid', mutation=TaskMutationRequest(content='some content')))
#!/usr/bin/env python3 """ Main applikation for website """ import os import re from flask import Flask, render_template, request, session, redirect, url_for from handler import Handler app = Flask(__name__) app.secret_key = re.sub(r"[^a-z\d]", "", os.path.realpath(__file__)) handler = Handler() @app.route("/") def main(): """ Main route """ handler.read_session(session) return render_template("index.html", people=handler.get_people()) @app.route("/company", methods=["POST", "GET"]) def company(): """ Company route """ if request.method == "POST": handler.read_session(session) handler.add_employee(request.form) handler.write_session(session) return render_template("company.html")
def test_id_not_in_body(self, update_task_request_fixture: dict, handler: Handler, mock_controller: Controller): update_task_request_fixture['body'] = '{"content":"something"}' mock_controller.update_task = MagicMock(return_value=True) result = handler.update_task(None, update_task_request_fixture) assert 400 == result.get('statusCode')
from aiohttp import web import os from handler import Handler import logging if __name__ == '__main__': app = web.Application() # Handler automatically registers its internal routes to the app handler = Handler(app) app.on_cleanup.append(Handler.cleanup) logging.basicConfig(level=logging.WARN) web.run_app(app, host='0.0.0.0', port=os.getenv('SERVICE_PORT', 5000))
root_logger.addHandler(fh) return root_logger def parse_args(): import argparse parser = argparse.ArgumentParser() parser.add_argument("--config_file", "-cf", help="config file", required=True) parser.add_argument("--logfile", "-log", help="log dir", default="logs/log.log") parser.add_argument("--num_process", "-p", help="multi process num", default=-1) return parser.parse_args() if __name__ == '__main__': args = parse_args() logger = setup_logger(args.logfile) logger.info("log file {} created!".format(args.logfile)) handler = Handler(args.config_file, args.num_process) logger.info("handler created! start processing...") handler.start() logger.info("done.")
import sys
async def processRequest(request: Request) -> Any: Logger.debug(f"worker: processRequest() [method:{request.method}]") if request.method == "dump": result = { "pid": getpid(), "players": [], "handlers": [] } for playerId, player in players.items(): playerDump = { "id": playerId } # type: Dict[str, Any] if player.audio: playerDump["audioTrack"] = { "id": player.audio.id, "kind": player.audio.kind, "readyState": player.audio.readyState } if player.video: playerDump["videoTrack"] = { "id": player.video.id, "kind": player.video.kind, "readyState": player.video.readyState } result["players"].append(playerDump) # type: ignore for handler in handlers.values(): result["handlers"].append(handler.dump()) # type: ignore return result elif request.method == "createPlayer": internal = request.internal playerId = internal["playerId"] data = request.data player = MediaPlayer( data["file"], data["format"] if "format" in data else None, data["options"] if "options" in data else None ) # store the player in the map players[playerId] = player result = {} if player.audio: result["audioTrackId"] = player.audio.id if player.video: result["videoTrackId"] = player.video.id return result elif request.method == "getRtpCapabilities": pc = RTCPeerConnection() pc.addTransceiver("audio", "sendonly") pc.addTransceiver("video", "sendonly") offer = await pc.createOffer() await pc.close() return offer.sdp elif request.method == "createHandler": internal = request.internal handlerId = internal["handlerId"] data = request.data # use RTCConfiguration if given jsonRtcConfiguration = data.get("rtcConfiguration") rtcConfiguration = None if jsonRtcConfiguration and "iceServers" in jsonRtcConfiguration: iceServers = [] for entry in jsonRtcConfiguration["iceServers"]: iceServer = RTCIceServer( urls=entry.get("urls"), username=entry.get("username"), credential=entry.get("credential"), credentialType=entry.get("credentialType") ) iceServers.append(iceServer) rtcConfiguration = RTCConfiguration(iceServers) handler = Handler( handlerId, channel, loop, getTrack, addRemoteTrack, getRemoteTrack, rtcConfiguration ) handlers[handlerId] = handler return else: internal = request.internal handler = handlers.get(internal["handlerId"]) if handler is None: raise Exception("hander not found") return await handler.processRequest(request)
class MainMenu: def __init__(self): directories = { "English": "data/lang/English/", "German": "data/lang/German/", "Polish": "data/lang/Polish/" } self.handler = Handler(directories) self.initializeMenu() def initializeMenu(self): while True: sg.theme("DarkTeal2") print("init") layout = [ [sg.Text("number of iterations: ", font=("courier", 20))] ,[sg.Input()], [sg.Text("learning rate: ", font=("courier", 20))] ,[sg.Slider(range=(0.0, 1.0), orientation='h', resolution=.1, size=(40, 20), default_value=0.5)], [sg.Button("Train"),sg.Button("Graph"), sg.Button("Cancel")]] window = sg.Window("Single-Layer Network", layout) event, values = window.read() if event in (None, "Cancel"): # if user closes window or clicks cancel break if event in (None, "Graph") and values[0] != "": # if user closes window or clicks cancel numberOfIterations = int(values[0]) self.handler.graphAccuracy(numberOfIterations, values[1]) if event in (None, "Train") and values[0] != "": numberOfIterations = int(values[0]) self.train(numberOfIterations, values[1]) break def train(self, numberOfIterations, learningRate): self.handler.trainNetwork(numberOfIterations, learningRate) self.test() def test(self): testDirectories = { "English": "data/lang.test/English/", "German": "data/lang.test/German/", "Polish": "data/lang.test/Polish/" } accuracy = self.handler.classifyVectorsFromDirectories(testDirectories) print(accuracy) result = "" textToTest = "Enter text to test" while True: sg.theme("DarkTeal2") print("init") layout = [ [sg.Text("Accuracy: ", font=("courier", 20))] ,[sg.Text(accuracy, font=("courier", 20))], [sg.Multiline(default_text=textToTest, font=("courier", 20), size=(100,20))], [sg.Text("Language: ", font=("courier", 20))] ,[sg.Text(result, font=("courier", 20))], [sg.Button("Run"), sg.Button("Cancel")]] window = sg.Window("Single-Layer Network", layout) event, values = window.read() if event in (None, "Cancel"): # if user closes window or clicks cancel break if event in (None, "Run") and values[0] != "": textToTest = values[0] parser = Parser() result = self.handler.classifySingleVector(parser.createSimpleVector(textToTest))
def test_success(self, delete_task_request_fixture: dict, handler: Handler, mock_controller: Controller): mock_controller.delete_task = MagicMock(return_value=True) result = handler.delete_task(None, delete_task_request_fixture) assert 204 == result.get('statusCode') mock_controller.delete_task.assert_called_once_with(None, DeleteTaskRequest(id='myid'))
def process(self, root): message = '' update = {} self.root = root # prepare for output vcdfile = join(self.root, self.vcd) tclfile = join(self.root, self.tcl) self.prepareFile(tclfile) self.createFile(tclfile, '# ') fp = open(tclfile, 'a') fp.write('transcript off\n') fp.write('\n') fp.write('set root ' + self.root + '\n') if self.stage != 'rtl': fp.write('set lib_top ' + self.lib_top + '\n') fp.write('\n') lib = '{}_{}_lib'.format(self.design, self.stage) fp.write('vlib {}\n'.format(lib)) # library resource # todo if self.stage != 'rtl': for libsrc in self.lib: if self.lang == 'verilog': fp.write( 'vlog $lib_top/{} -work {} +time_mode_zero\n'.format( libsrc, lib)) elif self.lang == 'vhdl': fp.write('vcom $lib_top/{} -work {}\n'.format(libsrc, lib)) else: raise HandlerError('Unknown HDL {}.'.format(self.lang)) # sources fp.write('\n# sources\n') for src in self.src: srcfile = join(self.root, src) if not isfile(srcfile): print( 'Warning: Source file could not be found on disk!\n -> {}'. format(realpath(src))) if src[-2:] == '.v': fp.write('vlog $root/{} -work {} +time_mode_zero\n'.format( src, lib)) elif src[-4:] == '.vhd': fp.write('vcom $root/{} -work {}\n'.format(src, lib)) # tbench tbfile = join(self.root, self.tb) if not isfile(tbfile): print( 'Warning: Testbench file could not be found on disk!\n -> {}'. format(tbfile)) if self.tb[-2:] == '.v': fp.write('vlog $root/{} -work {}\n'.format(self.tb, lib)) elif self.tb[-4:] == '.vhd': fp.write('vcom $root/{} -work {}\n'.format(self.tb, lib)) # print (Handler.getTime(self.cpath), self.tclk, self.nclk, Handler.getTime(self.prec)) if self.tclk * Handler.getTime(self.prec) < Handler.getTime( self.cpath): print( 'Warning: Simulation clock violates critical path of the design.' ) ack = input('Set simulation:tclk to critical path? [yes/no] ') if ack == 'yes': self.tclk = int( Handler.getTime(self.cpath) / Handler.getTime(self.prec)) update['simulation:tclk'] = self.tclk print('Info: Simulation clock will be updated.') else: print('Info: Critical path will be violated.') period = self.nclk * self.tclk rstdelay = period initdelay = period start = rstdelay + initdelay finish = start + self.nframes * period if self.rstdelay != rstdelay: self.rstdelay = rstdelay update['test:rstdelay'] = self.rstdelay if self.initdelay != initdelay: self.initdelay = initdelay update['test:initdelay'] = self.initdelay if self.start != start: self.start = start update['test:start'] = self.start if self.finish != finish: self.finish = finish update['test:finish'] = self.finish # run and finish fp.write('\n# simulation parameters\n') fp.write('vsim {}.{}'.format(lib, self.tbmodule)) fp.write('\\\n -t {}'.format(self.prec)) fp.write('\\\n -gTCLK={}'.format( self.tclk)) # see tbgen, these generics have fixed names fp.write('\\\n -gPERIOD={}'.format( period)) # see tbgen, these generics have fixed names fp.write('\\\n -gN_FRAMES={}'.format( self.nframes)) # see tbgen, these generics have fixed names if self.rst: fp.write('\\\n -gRST_DELAY={}'.format( self.rstdelay)) # see tbgen, these generics have fixed names fp.write('\\\n -gINIT_DELAY={}'.format( self.initdelay)) # see tbgen, these generics have fixed names if not self.novopt_off: fp.write('\\\n -novopt') if self.noglitch: fp.write('\\\n -noglitch') if self.stage not in ['rtl', 'zero']: if self.sdfnoerror: fp.write('\\\n -sdfnoerror') fp.write('\\\n -sdf{} {}=$root/{}'.format(self.sdfcorner, self.sdfstrip, self.sdf)) fp.write('\n') fp.write('\n') fp.write('onerror {resume}\n') fp.write('quietly WaveActivateNextPane {} 0\n') fp.write('TreeUpdate [SetDefaultTree]\n') fp.write('update\n') fp.write('\n# simulation output\n') fp.write('vcd file $root/{}\n'.format(self.vcd)) fp.write('vcd add {}\n'.format(self.tgt)) fp.write('vcd add {}\n'.format(self.tr)) fp.write('vcd add {}/*\n'.format(self.sdfstrip)) fp.write('\n# run and exit\n') fp.write('run {}\n'.format(self.run)) fp.write('quit -sim\n') fp.write('quit -f\n') # end file fp.close() message = 'Script created at: ' + tclfile if self.exe: raise NotImplementedError('Take care of the pipe later') # print('Info: Running QuestaSim found at: {}.'.format(QuestaSim.SOURCE)) # x = check_output('source {}; vsim -c -64 -do {}'.format( # QuestaSim.SOURCE, self.tcl).split()).decode().strip() # message = x return message, update
def test_id_not_in_body(self, delete_task_request_fixture: dict, handler: Handler, mock_controller: Controller): delete_task_request_fixture['body'] = '{}' mock_controller.delete_task = MagicMock(return_value=False) result = handler.delete_task(None, delete_task_request_fixture) assert 400 == result.get('statusCode')