Ejemplo n.º 1
0
def main(argv):
    (inputfile, outputfile, shapesCount, primitive, method, gif, seed) = parseArguments(argv)
    # Load image
    img = loadImage(inputfile)
    if img is None:
        print "Input file is not an image"
        sys.exit(2)

    # Process image
    random.seed(seed)
    [rendering, shapes] = method(shapesCount, primitive, maxSize=240, randomIterations=500).process(img)

    # Export rendering
    exportFilename = '{}_{}_{:1.0f}'.format(os.path.splitext(os.path.basename(inputfile))[0], shapesCount, Method.rmse(rendering, np.asarray(img, dtype=np.float32)))
    if gif:
        renderings = Method.renderIntermediates(shapes, Method.backgroundImage(img), gif)
        basesha1 = '%032x' % random.getrandbits(128)
        dirpath = tempfile.mkdtemp()
        for (i,render) in enumerate(renderings):
            cv2.imwrite('{}_{:03}.png'.format(dirpath+basesha1, i), render)
        os.system('convert -delay 75 {}_*.png {}.gif'.format(dirpath+basesha1, exportFilename))
        shutil.rmtree(dirpath)
    if not outputfile:
        outputfile = '{}.png'.format(exportFilename)
    cv2.imwrite(outputfile, rendering)

    # Show rendering
    plt.axis('off')
    plt.imshow(cv2.cvtColor(rendering, cv2.COLOR_BGR2RGB))
    plt.show()
Ejemplo n.º 2
0
    def test(self):
        """
        Test the model loaded from model_file on data in data_file
        model_file: pkl file to read from
        data_file:  pcap file to read from
        verbosity:  verbosity
        """
        self.method = Method(api=self.api, send_fn=self.send_to_monitor)
        self.method.load_model(self.model_file)

        self.monitor = Monitor(log_level=self.verbosity,
                               send_fn=self.send_to_method,
                               attack_type=self.attack_type)
        pkts = []
        with open(self.data_file, 'rb') as f:
            pkts = pickle.load(f)
        self.monitor.create_test_data(pkts)

        self.monitor.send()
        start_time = time.time()
        # time out after 10 seconds
        while not self.monitor.completed():
            sleep(.1)
        self.monitor.show_results()

        if self.out_file:
            pickle.dump(self.monitor.test_data, open(self.out_file, 'wb'))
Ejemplo n.º 3
0
 def train(self):
     """
     Train the model on data in data_file and save to model_file
     model_file: pkl file to write to
     data_file:  pcap file to read from
     """
     self.method = Method(api=self.api, send_fn=self.send_to_monitor)
     self.method.train_model(self.model_file, self.data_file,
                             self.featurizer)
Ejemplo n.º 4
0
 def add_main(self):
   main_cls = self.main_cls
   if any(main_cls.mtd_by_name(u"main")): return
   params = [ (u"String[]", u"args") ]
   main = Method(clazz=main_cls, mods=C.PBST, name=u"main", params=params)
   def to_call(mtd): return mtd.name + "();"
   body = '\n'.join(map(to_call, self.harness()))
   main.body = st.to_statements(main, body)
   main_cls.mtds.append(main)
Ejemplo n.º 5
0
    def add_main(self):
        main_cls = self.main_cls
        if any(main_cls.mtd_by_name(u"main")): return
        params = [(u"String[]", u"args")]
        main = Method(clazz=main_cls, mods=C.PBST, name=u"main", params=params)

        def to_call(mtd):
            return mtd.name + "();"

        body = '\n'.join(map(to_call, self.harness()))
        main.body = st.to_statements(main, body)
        main_cls.mtds.append(main)
Ejemplo n.º 6
0
    def render_GET(self, request):
        ret = Method.render_GET(self, request)

        if ret:
            return ret

        entry = self.player.get_playing_entry()

        header = utils.entry_header()
        header.append('time')

        item = utils.render_entry(entry)

        r, shuffle, repeat = self.player.get_playback_state()

        if entry:
            [r, t] = self.player.get_playing_time()
            item.append(t)

        r, volume = self.player.get_volume()
        r, mute = self.player.get_mute()

        ret = {
            'header': header,
            'current': item,
            'repeat': repeat,
            'shuffle': shuffle,
            'playing': self.player.props.playing,
            'volume': volume,
            'mute': mute
        }

        return json.dumps(ret)
Ejemplo n.º 7
0
    def render_GET(self, request):
        ret = Method.render_GET(self, request)

        if ret:
            return ret

        entry = self.player.get_playing_entry()

        header = utils.entry_header()
        header.append('time')

        item = utils.render_entry(entry)

        r, shuffle, repeat = self.player.get_playback_state()

        if entry:
            [r, t] = self.player.get_playing_time()
            item.append(t)

        r, volume = self.player.get_volume()
        r, mute = self.player.get_mute()

        ret = {'header': header, 'current': item, 'repeat': repeat, 'shuffle': shuffle, 'playing': self.player.props.playing, 'volume': volume, 'mute': mute};

        return json.dumps(ret)
Ejemplo n.º 8
0
    def calculate(self):
        '''Функция заполнение таблицы '''
        if __import__('re').findall('\d+? *?\+ *?\d+?',
                                    self.objectiv_func_edit.text()):
            pass
        else:
            print("[SECURITY] F**K YOU!")
            return

        self.result_table.setColumnCount(0)
        self.result_table.setRowCount(0)
        self.except_label.setText('')

        self.x1min = float(self.x1_min_edit.text())
        self.x1max = float(self.x1_max_edit.text())
        self.x2min = float(self.x2_min_edit.text())
        self.x2max = float(self.x2_max_edit.text())
        self.step = float(self.step_combBox.currentText())
        self.function = str(self.objectiv_func_edit.text())

        if self.x1min >= self.x1max or self.x2min >= self.x2max:
            self.except_label.setText(
                'Минимальное значение больше максимального !')
        else:
            self.excpts = []
            # достает существующие ограничения и вносит в список
            for expt in range(0, self.exeption_table.rowCount()):
                if self.exeption_table.item(expt, 0).text():
                    self.excpts.append(
                        self.exeption_table.item(expt, 0).text())

            self.method = Method(self.x1min, self.x2min, self.x1max,
                                 self.x2max, self.step, self.function,
                                 self.excpts)
            bool_matrix = self.method.boolean_matrix
            spis = self.method.spis

            self.result_table.setColumnCount(len(spis[0]))
            self.result_table.setRowCount(len(spis))

            for i in range(len(spis)):
                for j in range(len(spis[i])):
                    self.min(i, j, spis, bool_matrix)

            self.min_label.setText(str(self.method.min_needed()))
Ejemplo n.º 9
0
    def method_perebor(self):
        method = Method(self.x1min, self.x2min, self.x1max, self.x2max,
                        self.step, self.function, self.excpts)
        bool_matrix = method.boolean_matrix
        spis = method.spis

        self.result_table.setColumnCount(len(spis[0]))
        self.result_table.setRowCount(len(spis))

        for i in range(len(spis)):
            for j in range(len(spis[i])):
                self.min(i, j, spis, bool_matrix)

        min_value_of_fuction, x1, x2 = method.min_needed()
        QtWidgets.QMessageBox.information(
            self, "Результат",
            "Минимум функции:\t{0}\n\tx1:\t{1}\n\tx2:\t{2}".format(
                min_value_of_fuction, x1, x2))
Ejemplo n.º 10
0
    def __init__(self, *args):
        """Args can either be a single arg Method, or method_class, method_id, *args
		where *args are passed onto the correct Method constructor"""
        if len(args) == 1 and isinstance(args[0], Method):
            self.method, = args
            method_class, method_id = self.method.method_class, self.method.method_id
        else:
            if len(args) < 2:
                raise TypeError("If no Method given, method_class and method_id must be provided")
            method_class, method_id = args[:2]
            args = args[2:]
            self.method = Method.from_id(method_class, method_id)(*args)
        super(MethodPayload, self).__init__(method_class, method_id)
Ejemplo n.º 11
0
def makeMethods(analysis):
    objs = []
    numMethods = 0
    posMethods = []
    for j in range(
            0, len(analysis)
    ):  #start with something that starts w def, ends when run into other def
        if "def" in analysis[j]:
            numMethods = numMethods + 1
            posMethods.append(j)
    for i in range(0, numMethods):
        innerMethod = []  #strings of things in method
        tempName = ""
        tempArgs = []
        if (i == numMethods - 1):
            end = len(analysis)
        else:
            end = posMethods[i + 1] - 1
        for a in range(posMethods[i], end):
            if a == posMethods[i]:
                tempName = analysis[a][4]
                tempArgs = findArgs(tempName)
            else:
                if ("def" not in analysis[a][4]):
                    innerMethod.append(analysis[a])
        organizedInnerMethod = organizeInnerMethod(tempName, innerMethod)
        appendMeMethod = Method(tempName, tempArgs, organizedInnerMethod)

        appendMeMethod.setMethodsCalled(findMethods(organizedInnerMethod))
        appendMeMethod.setBasicMethodsCalled(
            findBasicMethods(organizedInnerMethod))

        appendMeMethod.setDocs(
            makeDocs(appendMeMethod, tempName, tempArgs, organizedInnerMethod))

        objs.append(appendMeMethod)
    return objs
Ejemplo n.º 12
0
	def parse(db, raw_recipe):
		"""parsing recipe download into recipe structure"""
		(raw_name, raw_ingredients, raw_directions) = raw_recipe
                tokenized_dirs = [ nltk.word_tokenize(d) for d in raw_directions]
                tagged_directions = [ 
                        nltk.pos_tag(d) for d in tokenized_dirs
                ]
		name = raw_name
		ingredients = [Ingredient.parse(db, i) for i in raw_ingredients]

		directions = [
                        Direction.parse(d, ingredients)
                        for d in tagged_directions
                ]
		methods = Method.find_methods(directions)
                tok_text = [
                        word
                        for d in tokenized_dirs
                        for word in d                        
                ]
		tools   = Tool.find_tools(tok_text)
		return Recipe(name, ingredients, tools, methods, directions)
Ejemplo n.º 13
0
    def process(self):
        if self.projects is None:
            self.projects = {}
            for name, module in self.project_args:
                with open(f'{name}.json', 'r') as f:
                    project: Project = jsonpickle.decode(f.read())
                    self.projects[project.name] = project

        for name, project in self.projects.items():
            test_files = TestIndex.iter_test_files(name)
            test_methods: Dict[PyDrillerMethod, Set[CustomCommit]] = {}
            for commit in project.commits:
                for method in commit.modified_methods:
                    if method.filename in test_files:
                        if method in test_methods:
                            test_methods[method].add(commit)
                        else:
                            test_methods[method] = set()
            for method, commits in test_methods.items():
                contributors = {commit.author.email for commit in commits}
                new_method = Method.from_pydriller_method(method.filename, method, len(commits), contributors, project)
                project.methods.append(new_method)
Ejemplo n.º 14
0
 def __init__(self, cursor, comment):
     Method.__init__(self, cursor, comment)
Ejemplo n.º 15
0
 def getChild(self, name, request):
     if name == 'seek':
         return Seek(self)
     else:
         return Method.getChild(self, name, request)
Ejemplo n.º 16
0
    def __init__(self, shell, sources):
        Method.__init__(self)

        self.shell = shell
        self.sources = sources
        self.player = shell.get_player()
Ejemplo n.º 17
0
class Simulator():
    def __init__(self,
                 model_file,
                 data_file,
                 out_file,
                 attack_type,
                 is_training,
                 verbosity,
                 featurizer=None):
        """
        This class will eventually simulate real communication between
        the api, method, and monitor. This behavior only makes sense for
        when the system is "deployed" aka making requests in real time.

        Currently, it serves to stitch together the method and monitor
        (train/test and display) of static data.
        """
        self.model_file = model_file
        self.data_file = data_file
        self.out_file = out_file
        self.attack_type = attack_type
        self.is_training = is_training
        self.verbosity = verbosity
        self.featurizer = featurizer

        # requires sudo to send and receive real packets
        self.api = api.API()

    def train(self):
        """
        Train the model on data in data_file and save to model_file
        model_file: pkl file to write to
        data_file:  pcap file to read from
        """
        self.method = Method(api=self.api, send_fn=self.send_to_monitor)
        self.method.train_model(self.model_file, self.data_file,
                                self.featurizer)

    def test(self):
        """
        Test the model loaded from model_file on data in data_file
        model_file: pkl file to read from
        data_file:  pcap file to read from
        verbosity:  verbosity
        """
        self.method = Method(api=self.api, send_fn=self.send_to_monitor)
        self.method.load_model(self.model_file)

        self.monitor = Monitor(log_level=self.verbosity,
                               send_fn=self.send_to_method,
                               attack_type=self.attack_type)
        pkts = []
        with open(self.data_file, 'rb') as f:
            pkts = pickle.load(f)
        self.monitor.create_test_data(pkts)

        self.monitor.send()
        start_time = time.time()
        # time out after 10 seconds
        while not self.monitor.completed():
            sleep(.1)
        self.monitor.show_results()

        if self.out_file:
            pickle.dump(self.monitor.test_data, open(self.out_file, 'wb'))

    def run(self):
        """
        Run the simulation in train or test mode
        """
        if self.is_training:
            self.train()
        else:
            self.test()

    def send_to_method(self, pkt):
        self.method.handle_pkt(pkt)

    def send_to_monitor(self, pkt):
        self.monitor.handle_pkt(pkt)
Ejemplo n.º 18
0
			"""
			name = driver.find_element_by_class_name(api.nome).text 
			message_content = driver.find_elements_by_class_name(api.chatdados)[-1]
			message = message_content.text.lower()
			if len(message) > 0:
				if (message == "/ping"):
					api.sendResponse(response='PONG')
				elif (message == "/img"):
					api.sendMedia(file=os.path.dirname(os.path.realpath(__file__)) + '/img/Prev.png')
				elif (message == "/doc"):
					api.sendDocument(file=os.path.dirname(os.path.realpath(__file__)) + '/main.py')
		except Exception as error:
			traceback.print_exc()
			pass
		driver.find_element_by_xpath(f"//span[@title='{contactorgroupname}']").click()
	api.waittemp(slp=1)


if __name__ == '__main__':
	options = webdriver.ChromeOptions()
	options.add_argument("user-data-dir={}".format(os.path.join(os.getcwd(), "profile", "wpp")))
	driver = webdriver.Chrome(executable_path='chromedriver', options=options)
	meth = Method(driver)
	meth.waittemp(temp=1)
	driver.get("https://web.whatsapp.com/")
	try:
		while True:
			whatsapp(driver, meth)
	except KeyboardInterrupt:
			print("\nClosing bot ...")
Ejemplo n.º 19
0
def method_init(variables):
    method = Method(variables, '', time(), scriptpath)
    return method
Ejemplo n.º 20
0
    fig.canvas.draw_idle()


if __name__ == '__main__':
    fig, ax = plt.subplots()
    fig.canvas.set_window_title('Лабораторная работа №5')
    ax.set(xlim=(-0.1, 1.1),
           ylim=(0, 0.3),
           xticks=np.arange(0, 1.1, 0.1),
           yticks=np.arange(0, 0.31, 0.025))
    ax_solution, = ax.plot([], [],
                           label=SolutionCauchyProblem.name_tex_notation())
    add_plot(SolutionCauchyProblem, ax_solution, 200)

    plot_dict = dict(
        zip(Method.get_name_methods(),
            [None for _ in range(len(Method.get_name_methods()))]))
    for plot in plot_dict:
        plot_dict[plot], = ax.plot([], [], label=plot)
    add_plots(Method, plot_dict)

    ax.legend(loc='upper left')

    fig.subplots_adjust(bottom=0.2)

    box_axes = fig.add_axes([0.125, 0.05, 0.3, 0.075])
    text_box = TextBox(box_axes, 'N', initial='5')
    text_box.on_submit(submit)

    visible_grid = True
    ax.grid(linestyle='--', visible=visible_grid)
Ejemplo n.º 21
0
def get_all_methods(gifts, log):
  return [sub(gifts, log) for sub in Method.__subclasses__()]
Ejemplo n.º 22
0
 def actions(self, state, player):
     return list(set(Method(player, state[1], state[2], state[3], state[4])))
Ejemplo n.º 23
0
 def getChild(self, name, request):
     if name == 'seek':
         return Seek(self)
     else:
         return Method.getChild(self, name, request)
Ejemplo n.º 24
0
class Main(Ui_Form):
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        Form = QtWidgets.QWidget()
        self.setupUi(Form)
        '''Назначение кнопкам события'''
        self.run_button.clicked.connect(self.calculate)
        self.add_btn.clicked.connect(self.add_exception)
        self.btn_graph.clicked.connect(self.show_graph)
        self.btn_rand.clicked.connect(self.show_rand)
        Form.show()
        sys.exit(app.exec_())

    def show_rand(self):
        monteCarlo = MonteCarlo(self.x1min, self.x1max, self.x2min, self.x2max,
                                10, self.function, self.excpts)
        for i in range(121):
            x1, x2 = monteCarlo.rand()
            monteCarlo.func(x1, x2)
        min_list = monteCarlo.get_min()
        self.min_label.setText(str(min_list[:3]))
        self.label.setText(str(min_list[3]))
        #print(monteCarlo.get_min())

    def show_graph(self):
        xx1 = numpy.arange(self.x1min, self.x1max + self.step, self.step)
        xx2 = numpy.arange(self.x2min, self.x2max + self.step, self.step)
        x1, x2 = numpy.meshgrid(xx1, xx2)

        zgrid = eval(self.function)

        cs = pylab.contour(x1, x2, zgrid)
        #pylab.clabel(cs, colors="black", inline=False)
        pylab.show()

    def add_exception(self):
        '''Добавление исключений в таблицу'''
        self.except_label.setText('')
        left_edit = self.exeption_left_edit.text()
        right_edit = self.exeption_right_edit.text()
        if not left_edit or not right_edit:
            self.except_label.setText(
                'Заполните все поля при добавлении исключений!')
        else:
            kol = self.exeption_table.rowCount()
            self.exeption_table.setRowCount(kol + 1)

            tableItem = QtWidgets.QTableWidgetItem("{0} {1} {2}".format(
                left_edit, self.combBox.currentText(), right_edit))
            self.exeption_table.setItem(0, kol, tableItem)

    def calculate(self):
        '''Функция заполнение таблицы '''
        if __import__('re').findall('\d+? *?\+ *?\d+?',
                                    self.objectiv_func_edit.text()):
            pass
        else:
            print("[SECURITY] F**K YOU!")
            return

        self.result_table.setColumnCount(0)
        self.result_table.setRowCount(0)
        self.except_label.setText('')

        self.x1min = float(self.x1_min_edit.text())
        self.x1max = float(self.x1_max_edit.text())
        self.x2min = float(self.x2_min_edit.text())
        self.x2max = float(self.x2_max_edit.text())
        self.step = float(self.step_combBox.currentText())
        self.function = str(self.objectiv_func_edit.text())

        if self.x1min >= self.x1max or self.x2min >= self.x2max:
            self.except_label.setText(
                'Минимальное значение больше максимального !')
        else:
            self.excpts = []
            # достает существующие ограничения и вносит в список
            for expt in range(0, self.exeption_table.rowCount()):
                if self.exeption_table.item(expt, 0).text():
                    self.excpts.append(
                        self.exeption_table.item(expt, 0).text())

            self.method = Method(self.x1min, self.x2min, self.x1max,
                                 self.x2max, self.step, self.function,
                                 self.excpts)
            bool_matrix = self.method.boolean_matrix
            spis = self.method.spis

            self.result_table.setColumnCount(len(spis[0]))
            self.result_table.setRowCount(len(spis))

            for i in range(len(spis)):
                for j in range(len(spis[i])):
                    self.min(i, j, spis, bool_matrix)

            self.min_label.setText(str(self.method.min_needed()))

    def min(self, i, j, spis, bool_matrix):
        if i == len(spis) - 1 or j == 0:
            tableItem = QtWidgets.QTableWidgetItem("{0}".format(spis[i][j]))
            self.result_table.setItem(i, j, tableItem)
            self.result_table.item(i, j).setForeground(
                QtGui.QColor(140, 100, 150))

        elif bool_matrix[i][j]:
            tableItem = QtWidgets.QTableWidgetItem(str(spis[i][j]))
            self.result_table.setItem(i, j, tableItem)

        else:
            tableItem = QtWidgets.QTableWidgetItem("{0}".format(spis[i][j]))
            self.result_table.setItem(i, j, tableItem)
            self.result_table.item(i, j).setBackground(
                QtGui.QColor(140, 100, 150))
            self.result_table.item(i, j).setForeground(
                QtGui.QColor(255, 255, 255))
Ejemplo n.º 25
0
Archivo: test.py Proyecto: cjx3721/QA
@function_timer
def t2wv(db) :
	return dict((attrib["title"], model[jieba.cut(text)]) for text, attrib in db)
	
if __name__ == "__main__" :
	db = Database("data/zhwiki-extracted/", conditions = [
		Database.cond_length(50), 
		Database.cond_title(lambda t: not t.startswith("Wikipedia:")),
		Database.cond_title(lambda t: not t.startswith("File:")),
		Database.cond_title(lambda t: not t.startswith("Draft:"))
	])
	# 767125 loaded, 451657 filtered, 31997 fails

	
	model = Method.load("dump/lda-model.dump")
	title2topic = t2wv(db)
	pickle.dump(title2topic, open("dump/title2topic.dump", "w"))
	#title2wv = pickle.load(open("dump/title2wv.dump"))
	vectors = []
	id2title = {}
	for i, (t, v) in enumerate(title2topic.items()) :
		vectors.append(v)
		id2title[i] = t
	
	fin = codecs.open("样例数据.txt", "r", "utf-8")
	lines = map(lambda l: l.strip(), fin.readlines())
	fin.close()
	queries = map(lambda l: l[:l.find(u"?")], lines)
	
	import sys
Ejemplo n.º 26
0
 def unpack(cls, data):
     method_class, data = Short.unpack(data)
     method_id, data = Short.unpack(data)
     method_type = Method.from_id(method_class.value, method_id.value)
     method, data = method_type.unpack(data)
     return cls(method), data
Ejemplo n.º 27
0
 def __init__(self, cursor, comment):
     Method.__init__(self, cursor, comment)
Ejemplo n.º 28
0
    def __init__(self, shell, sources):
        Method.__init__(self)

        self.shell = shell
        self.sources = sources
        self.player = shell.get_player()