Exemple #1
0
 def __init__(self, inputBoard=None):
     if (inputBoard == None):
         self.pieceLocations = PieceLocations()
         self.board = Board(self.pieceLocations)
         self.history = History()
     else:
         pass  #implement later
Exemple #2
0
 def __init__(self, output=None, date=Dates.empty()):
     self.date = date
     self.budgetData = Configuration().getBudget()
     self.transactions = []
     self.outputFile = output
     self.outFileCount = 0
     self.history = History()
Exemple #3
0
def run():
    logging.basicConfig(filename='log.txt',
                        filemode='a',
                        format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                        datefmt='%d-%b-%y %H:%M:%S',
                        level=logging.DEBUG)
    logging.info('Initiating Run')
    auth_client = Account()
    history = History('BTC-USD')
    sma50 = history.sma(50)
    sma100 = history.sma(100)
    if sma50 > sma100:
        bull_flag = True
    else:
        bull_flag = False
    while True:
        if datetime.now().minute == 0:
            history = History('BTC-USD')
            if bull_flag is False and history.sma(50) > history.sma(100):
                buy = auth_client.buy('BTC-USD')
                logging.info(f'Golden Cross: {buy}')
                bull_flag = True
            if bull_flag is True and history.sma(50) < history.sma(100):
                sell = auth_client.sell('BTC-USD')
                logging.info(f'Death Cross: {sell}')
                bull_flag = False
            else:
                logging.info('No Crossover event')
            time.sleep((60 * 60) - datetime.now().minute * 60 - (datetime.now().microsecond / 1000000))
        else:
            time.sleep((60 * 60) - datetime.now().minute * 60 - (datetime.now().microsecond / 1000000))
Exemple #4
0
 def calcTupleMP(self, indices):
     v = self.v
     globalScore, linearScore, score = 0, 0, 0
     mySumfiyi = np.zeros(self.featureBuilder.size)
     for line in self.splitted[indices[0]:indices[1] + 1]:
         sentence = [w[0] for w in line]
         tags = ["*", "*"] + [w[1] for w in line]
         for (t2, t1, index) in zip(tags[:], tags[1:], range(0, len(sentence))):
             history = History(t2, t1, sentence, index)
             f = self.featureBuilder.getFeatureVector(history, tags[index + 2])
             mySumfiyi[f] += 1
     linearScore = np.inner(mySumfiyi, v)
     weighted_sum = np.zeros(self.featureBuilder.size)
     for line in self.splitted[indices[0]:indices[1] + 1]:
         sentence = [w[0] for w in line]
         tags = ["*", "*"] + [w[1] for w in line]
         for (t2, t1, index) in zip(tags[:], tags[1:], range(0, len(sentence))):
             history = History(t2, t1, sentence, index)
             fs = [self.featureBuilder.getFeatureVector(history, t) for t in self.allTags]
             np_sums = np.array([np.sum(v[x]) for x in fs])
             np_exp_nominators = np.exp(np_sums)
             np_exp_sum = np.sum(np_exp_nominators)
             globalScore = globalScore + np.math.log(np_exp_sum)
             np_ps_of_ytag = np_exp_nominators / np_exp_sum
             np_features_mult_probs = np.zeros(self.featureBuilder.size)
             for i in range(0, len(self.allTags)):
                 np_features_mult_probs[fs[i]] += np_ps_of_ytag[i]
             weighted_sum = weighted_sum + np_features_mult_probs
     return weighted_sum, [linearScore - globalScore]
    def onModuleLoad(self):
        self.curInfo = ''
        self.curSink = None
        self.description = HTML()
        self.sink_list = SinkList()
        self.panel = DockPanel()

        self.loadSinks()
        self.sinkContainer = DockPanel()
        self.sinkContainer.setStyleName("ks-Sink")

        vp = VerticalPanel()
        vp.setWidth("100%")
        vp.add(self.description)
        vp.add(self.sinkContainer)

        self.description.setStyleName("ks-Info")

        self.panel.add(self.sink_list, DockPanel.WEST)
        self.panel.add(vp, DockPanel.CENTER)

        self.panel.setCellVerticalAlignment(self.sink_list,
                                            HasAlignment.ALIGN_TOP)
        self.panel.setCellWidth(vp, "100%")

        History().addHistoryListener(self)
        RootPanel().add(self.panel)

        initToken = History().getToken()
        if len(initToken):
            self.onHistoryChanged(initToken)
        else:
            self.showIntro()
Exemple #6
0
 def viterbiLoop(self):
     allTagsList, bp, k, pi, sentence = self.relevantTagTuples, self.bp, self.k, self.pi, self.sentence
     for tagU, tagV in self.relevantTagTuples:
         if sentence[k - 1] in self.seenWordsToTagsDict:
             if tagV not in self.seenWordsToTagsDict[sentence[k - 1]]:
                 continue
         if sentence[k - 2] in self.seenWordsToTagsDict:
             if tagU not in self.seenWordsToTagsDict[sentence[k - 2]]:
                 continue
         tmpMax = -1
         tmpMaxT = self.tagsNum
         for tagT in self.tags:
             if sentence[k - 3] in self.seenWordsToTagsDict:
                 t2Tags = self.seenWordsToTagsDict[sentence[k - 3]]
                 if tagT not in t2Tags:
                     continue
             history = History(tagT, tagU, sentence, k - 1)
             currTags = self.tags
             if sentence[k-1] in self.seenWordsToTagsDict:
                 currTags = self.seenWordsToTagsDict[sentence[k-1]]
             #mleRes = self.mle.p(history, tagV, self.v)
             mleRes =  self.mle.p_forTags(history,tagV, self.v, currTags)
             tmpRes = pi[k - 1, self.tagsToIdxDict[tagT], self.tagsToIdxDict[tagU]] * mleRes
             if tmpRes > tmpMax:
                 tmpMax, tmpMaxT = tmpRes, tagT
         if tmpMax == -1:
             for tagT in self.tags:
                 history = History(tagT, tagU, sentence, k - 1)
                 mleRes = self.mle.p(history, tagV, self.v)
                 tmpRes = pi[k - 1, self.tagsToIdxDict[tagT], self.tagsToIdxDict[tagU]] * mleRes
                 if tmpRes > tmpMax:
                     tmpMax, tmpMaxT = tmpRes, tagT
         self.pi[k,self.tagsToIdxDict[tagU], self.tagsToIdxDict[tagV]] = tmpMax
         self.bp[k,self.tagsToIdxDict[tagU], self.tagsToIdxDict[tagV]] = self.tagsToIdxDict[tmpMaxT]
Exemple #7
0
    def __init__(self, appState, parent=None, settings=None):
        super(PSSBuilder, self).__init__(parent, modal=1)

        self.state = appState

        self.ui = Ui_PSSEditor()
        self.ui.setupUi(self)

        image = os.path.dirname(os.path.realpath(__file__)) + "/PSS.png"
        self.ui.PSSdiagram.setPixmap(
            QtGui.QPixmap(QtCore.QString.fromUtf8(image)))

        self.history = History()
        self.isAdded = False

        self.pssPreview = MixtureViewer(self.history, self, None)
        self.ui.viewerLayout.addWidget(self.pssPreview)
        if settings != None:
            self.pssPreview.setHighResolution(settings.highResolution)
            self.pssPreview.setLabeling(settings.showLabels)
            self.pssPreview.showAxes(settings.showAxes)
            self.pssPreview.showHelp(settings.showHelp)

        self.files = None
        self.slideFactor = 50
        self.ui.mSpinBox.setValue(1)
        self.ui.nSpinBox.setValue(1)

        if __name__ != '__main__':
            self.ui.saveButton.setHidden(True)

        self.generatePolymer()
        self.pssPreview.update()
Exemple #8
0
    def setJob(job_dict,
               isNew=0,
               path=__guidePath__,
               histoyPath=__historyPath__,
               logPath=__logPath__):
        try:
            Json_evaluation.updateJson(job_dict, __jobFile__, path=path)
            if isNew == 1:
                for x in job_dict.keys():
                    Json_evaluation.writeText(
                        filename=str(x) + ".txt",
                        path=histoyPath,
                        data=Generic.prefixTimeStamp("Job Created!!"))
                log("Adding " + str(job_dict.keys()) + " Job", path=logPath)
            else:
                log("Update made on  " + str(job_dict.keys()) + " Job",
                    path=logPath)
                for x in job_dict.keys():
                    History.addJobHistory(filename=str(x + ".txt"),
                                          path=histoyPath,
                                          data=Generic.prefixTimeStamp(
                                              str("Job has been updated!!")))

            pass
        except Exception as e:
            log("Error_Jobs_setJob@" + str(e), path=logPath)
Exemple #9
0
    def profit_with_tax(self, start, end, p=True, sl_for_risk=False):

        use_bal = self.use_balance
        self.use_balance = True

        bal = self.base_balance
        running_bal = bal

        orig_hist = self.hist_obj
        tax_sum = 0

        if p:
            print("\nStarting backtest for strat", self.strat.name, "on",
                  self.hist_obj.asset_name, "over", start, "-", end,
                  "with base balance", self.base_balance, "and risk of",
                  self.risk)
            print(
                "\n!!!   !!!   !!!   Scholz rule is NOT applied!   !!!   !!!   !!!\n"
            )
            print("                                  balance: {:12.2f}".format(
                bal))

        for i in range(start, end + 1):

            self.hist_obj = History(self.hist_obj.asset_name, i)
            abs_p = self.test(p=False, use_sl_for_risk=sl_for_risk)
            p = (0.75 * abs_p)
            tax_sum += 0.25 * abs_p
            perc_gr = (((running_bal + p) / running_bal) - 1) * 100
            running_bal += p
            if p:
                print(
                    "Profit in {}:\t\t{:12.2f}, balance: {:12.2f}, growth in %: {} %, tax: {:12.2f}, trades: {}"
                    .format(i, p, running_bal,
                            "{:12.2f}".format(perc_gr).zfill(5), 0.25 * abs_p,
                            len(self.closed_trades)))
            self.base_balance = running_bal

        if p:
            print(
                "\nProfit in {}-{}:{:12.2f}, balance: {:12.2f}, % growth: {:12.2f}%\nrisk free:\t\t\t{:12.2f}"
                .format(start, end, self.base_balance - bal, self.base_balance,
                        ((self.base_balance / bal) - 1) * 100,
                        bal * (1.07**(end - start + 1))))
            # risk free is based off of all-weather portfolio with 9% gain/year -> 0.75*9% ~7%/year

        self.base_balance = bal

        if p:
            self.hist_obj = History(self.hist_obj.asset_name, start, end)
            pure_profit = self.test(p=False)
            print("\nPayed tax:\t\t\t{:12.2f}, prof\\tax:{:12.2f}\n".format(
                tax_sum, pure_profit))

        self.use_balance = use_bal
        self.hist_obj = orig_hist

        return running_bal - self.base_balance
Exemple #10
0
def mallShow(id):
    mall = Mall.query.get(id)
    if mall == None:
        return redirect(url_for('index'))
    category = MallCategory.query.get(mall.catid)
    thumbs = get_albums(mall,g.siteDomain)
    albums = get_albums(mall,g.siteDomain,1)
    p1 = get_nv(mall.n1,mall.v1)
    p2 = get_nv(mall.n2,mall.v2)
    p3 = get_nv(mall.n3,mall.v3)
    data = MallData.query.get(id)
    mall.hits += 1
    if g.islogin:
        history = History.query.get(g.user.id)
        if not history:
            history = History()
            history.userid = g.user.id
            history.data = ''
            db.session.add(history)
        if not history.data:
            history.data += str(mall.id)
        elif str(mall.id) not in history.data:
            history.data += ',' + str(mall.id)
        historys = history.data.split(',')
        length = len(historys)-30
        if length > 0:
            historys = historys[length:]
            history.data = ','.join(historys)
        hmalls = Mall.query.filter(Mall.id.in_(historys)).all()
    else:
        hmalls = None
    db.session.commit()
    relprice = 0.00
    if mall.relate_id:
        relids = mall.relate_id.split(',')
        relmalls = Mall.query.filter(Mall.id.in_(relids)).all()
        for v in relmalls:
            if v.proprice != 0.00:
                relprice += float(v.proprice)
            else:
                relprice += float(v.price)
    else:
        relmalls = None
    maylikeids = History.query.filter(History.data.like('%'+str(mall.id)+'%')).all()
    alist = []
    for v in maylikeids:
        tmp = v.data.split(',')
        alist += tmp
    if alist:
        alist = list(set(alist))
        mlikes = Mall.query.filter(Mall.id.in_(alist)).all()
    else:
        mlikes = None
    mlength = 0
    if mlikes :
        mlength = len(mlikes)
    return render_template('mall/mallShow.html',mall=mall,category=category,thumbs=thumbs,albums=albums,p1=p1,p2=p2,p3=p3,moduleid=4,data=data,g=g,hmalls=hmalls,relmalls=relmalls,relprice=relprice,mlikes=mlikes,mlength=mlength)
Exemple #11
0
    def __init__(self, **kwargs):
        super(MainApp, self).__init__(**kwargs)

        self.main_box = BoxLayout(orientation="vertical")
        self.number_grid = GridLayout(cols=3)

        self.operators_grid = BoxLayout(orientation="vertical",
                                        size_hint=(0.2, 1))
        self.main_button_box = BoxLayout()

        self.history = History()
        self.more_functions = MoreFunctions()
Exemple #12
0
 def __init__(self, url, checkInterval):
     self.url = url
     self.checkInterval = checkInterval
     # calculating the number of checks in an hour to initialize the history
     maxNumberOfChecksSaved = int(round(3600.0 / checkInterval))
     self.checksHistory = History(maxNumberOfChecksSaved)
     # the history may be used by other threads, thus we protect it with a lock
     self.historyLock = threading.Lock()
     # interval keeps track of the Interval instance that execute self.check every self.checkInterval
     self.interval = None
     # keeps track of when the monitor was launched
     self.startedMonitoringTime = time.time()
Exemple #13
0
class Data:
    """Houses all the data for the chess game

    consists of 
    a 2D list that maps location to piece 
    a dictionary that maps pieceID to location
    historical data for en passant and castling"""
    def __init__(self, inputBoard=None):
        if (inputBoard == None):
            self.pieceLocations = PieceLocations()
            self.board = Board(self.pieceLocations)
            self.history = History()
        else:
            pass  #implement later

    def isEmpty(self, row, column):
        return self.board.isEmpty(row, column)

    def isWhite(self, row, column):
        return self.board.isWhite(row, column)

    def getSide(self, row, column):
        return self.board.getSide(row, column)

    def isDifferentSides(self, square1, square2):
        return self.board.isDifferentSides(square1, square2)

    def getPiece(self, square):
        return self.board.board[square.row][square.column]

    def destroyPiece(self, square):
        self.getPiece(square).destroy()
        self.board.board[square.row][square.column] = 0

    def movePiece(self, startSquare, endSquare):

        myPiece = self.getPiece(startSquare)
        myPiece.move(endSquare.row, endSquare.column)
        self.board.board[startSquare.row][startSquare.column] = 0
        self.board.board[endSquare.row][endSquare.column] = myPiece

    def getWhiteKing(self):
        return self.pieceLocations.whiteKing

    def getBlackKing(self):
        return self.pieceLocations.blackKing

    def getShallowPieceCopy(self):
        self.pieceLocations.getShallowCopy()

    def getShallowHistoryCopy(self):
        self.history.getShallowCopy()
Exemple #14
0
class WebsiteMonitor:
    def __init__(self, url, checkInterval):
        self.url = url
        self.checkInterval = checkInterval
        # calculating the number of checks in an hour to initialize the history
        maxNumberOfChecksSaved = int(round(3600.0 / checkInterval))
        self.checksHistory = History(maxNumberOfChecksSaved)
        # the history may be used by other threads, thus we protect it with a lock
        self.historyLock = threading.Lock()
        # interval keeps track of the Interval instance that execute self.check every self.checkInterval
        self.interval = None
        # keeps track of when the monitor was launched
        self.startedMonitoringTime = time.time()

    def check(self):
        # HTTP HEAD request to the url of the monitored website
        # measurez
        success = True
        checkTime = time.time()
        try:
            r = requests.head(self.url, timeout=min(self.checkInterval, 5.0))
        except requests.exceptions.RequestException:
            success = False
        responseTime = time.time() - checkTime

        with self.historyLock:
            self.checksHistory.add((
                -1 if not (success) else r.status_code,
                checkTime,
                -1.0 if not (success) else responseTime,
            ))

    def startMonitoring(self):
        self.startedMonitoringTime = time.time()
        self.interval = Interval(self.checkInterval, self.check)

    def stopMonitoring(self):
        if self.interval != None:
            self.interval.cancel()
            self.interval = None

    #####
    # THREAD SAFE GETTER WITH CONDITION
    #####

    def getCheckHistoryForThePast(self, seconds):
        t = time.time()
        with self.historyLock:
            # check[1] = timestamp of the check
            return self.checksHistory.getValuesUntilCondition(
                lambda check: t - check[1] > seconds)
 def testPredicateAll(self):
     historyBuffer=['load smth',
                    'export aa=cc',
                    'history clear']
     
     expected=['load smth','export aa=cc']
     
     readLine = ReadLine()
     readLine.getLineBuffer = MagicMock(return_value=historyBuffer)
     
     history = History( readLine )
     result = history.get( history.passAllPredicate )
     
     self.assertEquals(expected, result)
def main():
    load_param()
    pm = PolicyManager()
    mm = MachineManager()

    # Loop through all machines
    for machine_name in force_machine_test or mm.get_machine_names():

        # Initialize for machine.
        mm.use(machine_name)
        h = History()

        # Deteremine dynamic oracle.
        for t in range(1, T):
            arm_id, prob = mm.oracle(t)
            h.store_oracle(t, arm_id, prob)

        # Loop through all policies
        for policy_name in force_policy_test or pm.get_policy_names():
            x = [0] * len(param_range)
            y = [0] * len(param_range)
            for i in range(num_runs):
                print("Run #{}".format(i))
                for j in range(len(param_range)):
                    param_val = param_range[j]
                    h.reset()
                    pm.use(policy_name, mm.get_num_arms())
                    pm.set_params({
                        "gamma": param_val,
                    })
                    for t in range(1, T):
                        arm_id = pm.get_arm(t)
                        reward = mm.play(t, arm_id)
                        pm.store(t, arm_id, reward)
                        h.store(t, arm_id, reward)
                    x[j] = param_val
                    y[j] += h.get_reward() / num_runs
                    print(".", end="", flush=True)
                print("")
            plt.plot(x, y, "r^-")
            plt.title(policy_name)
            os.makedirs("results/param/", exist_ok=True)
            plt.savefig("results/param/{}_{}.png".format(
                mm.get_name(), policy_name))
            plt.close()

            best_param = 0
            best_reward = 0
            for i in range(len(x)):
                if y[i] > best_reward:
                    best_param = x[i]
                    best_reward = y[i]
            all_params["{}_{}".format(mm.get_name(), policy_name)] = best_param
    save_param()
Exemple #17
0
class CRDTStore:
    def __init__(self,
                 server=None,
                 user_id=0,
                 initial_version=None,
                 initial={}):
        self.state = initial
        self.initial_state = initial
        self.history = History()

        self.user_id = user_id
        self.version = VectorClock(user_id, initial_version)

        self.server = server
        if server is not None:
            self.server.on_recieve += self.on_recieve

        self.on_update = Callback()

    def on_recieve(self, op: Op):
        # merge
        self.merge(op)

    def merge(self, op: Op):
        # TODO: ignore duplicates?

        # update version
        self.version.merge(op.id)

        # apply and store history
        self.state = op.apply(self.state)
        self.history.append(op)

        self.on_update()

    def apply(self, op: Op):
        # merge op
        self.merge(op)

        # send op to clients
        if self.server is not None:
            self.server.send(op)

    def get(self, field: str):
        if field in self.state:
            return self.state[field][1]  # TODO: check if tombstoned

        return None
Exemple #18
0
    def __init__(self, argv):
        # key is instruction address
        # value is number of correct predictions
        self.correct_prediction_dict = defaultdict(lambda: 0)

        self.history_table = [
            History(self.history_size)
            for foo in xrange(2**self.num_bits_for_history)
        ]
        self.predictors = [
            BranchPredictor(self.predictor_size)
            for foo in xrange(2**self.history_size)
        ]

        self.parseCommandLineArgs(argv)
        self.btb = BranchTargetBuffer(self.btb_size)

        hist_file = open(self.hist_file_name)
        guess_int = lambda x: int(x, 0)

        self.hist_file_data = [
            map(guess_int,
                line.strip().split()) for line in hist_file.read().split('\r')
            if line
        ]
        hist_file.close()
Exemple #19
0
def Menu():

    username = input("Enter username: "******"Enter password: "******"Do you want to login or register new user?\nPress 1 to login 2 to register.\n"
    )

    if reg == "1":
        user.login()
    elif reg == "2":
        user.register()

    while True:

        choice = input(
            "What you option you want to use?\n1 = history\n2 = progress\n3 = main\n4 = quit\n"
        )

        if choice == "1":
            History(user.getuser())
        elif choice == "2":
            Checker.check(user.getuser())
        elif choice == "3":
            Stash.Adder(user.getuser())
        elif choice == "4":
            break
        else:
            print("\nEnter a valid nubmer")
Exemple #20
0
 def start(self, getter):
     self.keyboard_input = getter
     self.picture_to_show = self.keyboard_input.get()
     self.picture_storage = PictureStorage()
     self.settings = Settings.Settings()
     self.camera = Camera()
     self.tools = ImageProcessing.ImageProcessing(self.picture_storage, self.settings)
     self.window = Window(self.settings, "Bilder")
     self.main_window = Window(self.settings, "Taschenrechner")
     self.calculator = Calculator.Calculator()
     self.gui = GUI.GUI(self.picture_storage, self.settings)
     self.history = History()
     self.stage = 0
     self.buttons = ButtonGenerator(self.picture_storage)
     self.delete_history = History()
     self._run()
    def __init__(self):
        self.__version__ = '3.1.3'

        logger_format = '%(asctime)s %(message)s'
        logging.basicConfig(format=logger_format, level=logging.INFO,
                            datefmt="%H:%M:%S", filename='./logfile.log', filemode='w')
        logging.getLogger().addHandler(SysLogHandler(facility=SysLogHandler.LOG_DAEMON, address='/dev/log'))

        self._SIGKILL = False
        self.logging = logging
        self.config = DB('./config.json')

        self.network = Network(self)
        self.display = DisplayContent()
        self.detector = Detector()
        self.ultrasound = Ultrasound(self)
        self.temp = Temp()
        self.server = SimpleWebSocketServer('', 8069, WebSocketServer, self)
        self.cook = Cook(self)
        self.energy = Energy(self)
        self.history = History(self)
        self.audio = Audio(self)
        self.users = Users(self)
        self.automations = Automations(self)

        signal.signal(signal.SIGTERM, self.sig_handler)
        signal.signal(signal.SIGINT, self.sig_handler)

        self.log("Boot: v" + self.__version__)
Exemple #22
0
def sanity():
    words = [("t1", "t2"), ("t4", "t3"), ("t3", "t7"), ("t4", "t1"),
             ("t1", "t9"), ("t1", "t1")]
    f104 = F104Builder(words)
    history = History("t1", "t1", ["w2", "w5", "w8", "w2"], 1)
    assert f104.getFeatureVector(history, "t8").size == 0
    assert f104.getFeatureVector(history, "t9").size == 1
Exemple #23
0
    def simulate(self):
        env = self.env
        agent = self.agent

        observation = env.reset()

        history = History()
        sum_reward = 0
        done = False

        while not done:
            action, L1A, L2A, probs = agent.determine_action(observation)
            observation, reward, done, info = env.step(action)
            history.append(observation, L1A, L2A, probs, action, reward)
            sum_reward += reward

        return history, sum_reward
Exemple #24
0
def realData():
    p = MyParser('../train.wtag')
    words = p.getWordsWithTag()
    f100 = F100Builder(words)
    firstSent = [w for (w, t) in p.splitted[0]]
    history=History("t5","t2",firstSent,3)
    assert f100.getFeatureVector(history,"bla").size == 0
    assert f100.getFeatureVector(history,"RB").size == 1
Exemple #25
0
    def __init__(self,
                 server=None,
                 user_id=0,
                 initial_version=None,
                 initial={}):
        self.state = initial
        self.initial_state = initial
        self.history = History()

        self.user_id = user_id
        self.version = VectorClock(user_id, initial_version)

        self.server = server
        if server is not None:
            self.server.on_recieve += self.on_recieve

        self.on_update = Callback()
Exemple #26
0
def sanity():
    words = [("w1", "t1"), ("w1", "t2"), ("w3", "t1"), ("w5", "t2"), ("w3", "t2")]
    f100 = F100Builder(words)
    history = History("t5", "t2", ["w2", "w5", "w8", "w2"], 1)
    vec = f100.getFeatureVector(history, "t2")
    assert vec.size == 1
    vec = f100.getFeatureVector(history, "t1")
    assert vec.size == 0
Exemple #27
0
def realData():
    p = MyParser('../train.wtag')
    words = p.getAllPairTagsCombinations()
    f104 = F104Builder(words)
    firstSent = [w for (w, t) in p.splitted[0]]
    history = History("RB", "VBG", firstSent, 3)
    assert f104.getFeatureVector(history, "bla").size == 0
    assert f104.getFeatureVector(history, "RP").size == 1
Exemple #28
0
 def __init__(self, data):
     """
     Initialize the QuadsData object.
     """
     self.hosts = Hosts(data)
     self.clouds = Clouds(data)
     self.history = History(data)
     self.cloud_history = CloudHistory(data)
Exemple #29
0
 def preprocess(self):
     for line in self.splitted:
         sentence = [w[0] for w in line]
         tags = ["*", "*"] + [w[1] for w in line]
         for (t2, t1, index) in zip(tags[:], tags[1:], range(0, len(sentence))):
             history = History(t2, t1, sentence, index)
             f = self.featureBuilder.getFeatureVector(history, tags[index + 2])
             self.sumfiyi[f] += 1
Exemple #30
0
 def inferenceFirstIteration(self, sentence):
     for tagV in self.tags:
         if sentence[0] in self.seenWordsToTagsDict:
             if tagV not in self.seenWordsToTagsDict[sentence[0]]:
                 continue
         history = History('*', '*', sentence, 0)
         self.pi[1, self.tagsToIdxDict['*'], self.tagsToIdxDict[tagV]] = \
             self.mle.p(history, tagV, self.v)
         self.bp[1, self.tagsToIdxDict['*'], self.tagsToIdxDict[tagV]] = self.tagsToIdxDict['*']
Exemple #31
0
def basicTest():
    parser = MyParser("MLE_db.wtag")
    splitted = parser.splitted
    fb = BasicFeatureVectorBuilder(parser)
    mle = MLE(["t1", "t2", "t3", "t5"], splitted, fb)
    v = np.ones(fb.size)
    history = History("t1", "t2", ["w1", "w2", "w3", "w2"], 2)
    res = mle.p(history, "t3", v)
    print(res)
Exemple #32
0
	def addGuild(self, guild):
		#self.histories[guild.id] = {}
		ch_histories = self.histories[guild.id]
		for channel in guild.channels.itervalues():
			try:
				ch_histories[channel.id] = History(channel)
			except Exception, err:
				print 'failed to build history for:', err, channel.id, ' ', guild.id
				pass
Exemple #33
0
class App:
	def __init__(self,port=35353, debug=False):
		gnome.program_init("Dreadmill UI", "1.0")
		client = gnome.ui.master_client()
		command = os.path.normpath(os.path.join(os.getcwd(), sys.argv[0]))
		client.set_restart_style(gnome.ui.RESTART_IF_RUNNING)
		try: client.set_restart_command([command] + sys.argv[1:])
		except TypeError:
			client.set_restart_command(len(sys.argv), [command] + sys.argv[1:])
		client.connect('die', self.die)
		client.connect('save-yourself',  self.save_state)

		self.dreadmill = Dreadmill(port=port, debug=debug)
		self.history = History(self.dreadmill)
		
		# create window
		self.control_window = ControlWindow(self.dreadmill, self)
		# add app indicator
		self.indicator = Indicator(self.dreadmill, self)
		
		# hook callback for IO
		gobject.io_add_watch(self.dreadmill.get_socket(), gobject.IO_IN, self.prod_dreadmill, None)
		gobject.timeout_add_seconds(3, self.dreadmill.ping)

	def prod_dreadmill(self, source, condition, misc=None):
		self.dreadmill.socket_ready()
		return True

	def show_control_window(self):
		self.control_window.show()

	def main(self):
		gtk.main()

	def get_distance_today(self):
		return self.history.get_distance_today()

	def destroy(self):
		self.history.sync()
		self.history.close()
		gtk.main_quit()

	def die(self, *args):
		self.destroy()

	def save_state(self, *args):
		self.history.sync()
Exemple #34
0
	def __init__(self, parent = None):
		QtGui.QWidget.__init__(self, parent)
		
		self.parent = parent
		
		self.usersScribbleHistory = None

		self.setAttribute(QtCore.Qt.WA_StaticContents)
		self.modified = False
		self.scribbling = False
		self.myPenWidth = 2
		self.myPenColor = QtGui.QColor(0, 170, 255)
		imageSize = QtCore.QSize()
		self.image = QtGui.QImage(imageSize, QtGui.QImage.Format_RGB32)
		self.lastPoint = QtCore.QPoint()
		"""
		self.scrollArea = QtGui.QScrollArea()
		self.scrollArea.setBackgroundRole(QtGui.QPalette.Dark)
		self.scrollArea.setWidget(self.scribbleArea)
		"""		
		self.history = History()
 def __init__(self):
     Cmd.__init__(self)
     if sys.platform.startswith("win"):
         self.__disableColors()
     
     self.__home = os.path.expanduser("~")
     self.__appDir = os.path.dirname(__file__)
     self.__historyFile = os.path.normpath(self.__home + "/.pkt_synth")
     self.__packetGenerator = None
     self.__packetSender = None
     self.__context = {}
     self.__packet_context_difference = set([])
     self.__sender_context_difference = set([])
     self.__listPacketGenerators = self.__findInheritedClasses('AbstractPacket')
     self.__listPacketSenders = self.__findInheritedClasses('AbstractSender')
     self.prompt = self.OKGREEN + ">> " + self.ENDC
     self.__valueParser = VariableValueParser(self.__context)
     
     readlineImpl = ReadLineImpl(self.__historyFile)
     self.__history = History( readlineImpl )
     self.__history.load()
Exemple #36
0
	def __init__(self,port=35353, debug=False):
		gnome.program_init("Dreadmill UI", "1.0")
		client = gnome.ui.master_client()
		command = os.path.normpath(os.path.join(os.getcwd(), sys.argv[0]))
		client.set_restart_style(gnome.ui.RESTART_IF_RUNNING)
		try: client.set_restart_command([command] + sys.argv[1:])
		except TypeError:
			client.set_restart_command(len(sys.argv), [command] + sys.argv[1:])
		client.connect('die', self.die)
		client.connect('save-yourself',  self.save_state)

		self.dreadmill = Dreadmill(port=port, debug=debug)
		self.history = History(self.dreadmill)
		
		# create window
		self.control_window = ControlWindow(self.dreadmill, self)
		# add app indicator
		self.indicator = Indicator(self.dreadmill, self)
		
		# hook callback for IO
		gobject.io_add_watch(self.dreadmill.get_socket(), gobject.IO_IN, self.prod_dreadmill, None)
		gobject.timeout_add_seconds(3, self.dreadmill.ping)
Exemple #37
0
    def __init__(self, url, dom_or_doc, navigator = None, personality = 'xpie61', name="", 
                 target='_blank', parent = None, opener = None, replace = False, screen = None, 
                 width = 800, height = 600, left = 0, top = 0, **kwds):

        self.url = url
        self.doc = w3c.getDOMImplementation(dom_or_doc, **kwds) if isinstance(dom_or_doc, BeautifulSoup.BeautifulSoup) else dom_or_doc
        
        self.doc.window        = self
        self.doc.contentWindow = self
         
        self._navigator = navigator if navigator else Navigator(personality, self)
        self._location  = Location(self)
        self._history   = History(self)
        self._history.update(url, replace)

        #self.doc.location = self._location
        self.doc.location = property(self.getLocation, self.setLocation)

        self._target = target
        self._parent = parent
        self._opener = opener
        self._screen = screen or Screen(width, height, 32)
        self._closed = False
        
        self._personality = personality
        self.__init_personality()

        self.name          = name
        self.defaultStatus = ""
        self.status        = ""
        self._left         = left
        self._top          = top
        self.innerWidth    = width
        self.innerHeight   = height
        self.outerWidth    = width
        self.outerHeight   = height
        self.timers        = []
        self.java          = java()
Exemple #38
0
class Window(PyV8.JSClass):

    class Timer(object):
        def __init__(self, window, code, delay, repeat, lang = 'JavaScript'):
            self.window  = window
            self.code    = code
            self.delay   = float(delay) / 1000
            self.repeat  = repeat
            self.lang    = lang
            self.running = True

        def start(self):
            self.event = sched.enter(self.delay, 1, self.execute, ())
            try:
                sched.run()
            except:
                pass

        def stop(self):
            self.running = False
            if self.event in sched.queue:
                sched.cancel(self.event)

        def execute(self):
            if not self.running:
                return

            log.debug(str(self.code))
            self.code.__call__()
            if self.repeat:
                self.start()
        
    def __init__(self, url, dom_or_doc, navigator = None, personality = 'xpie61', name="", 
                 target='_blank', parent = None, opener = None, replace = False, screen = None, 
                 width = 800, height = 600, left = 0, top = 0, **kwds):

        self.url = url
        self.doc = w3c.getDOMImplementation(dom_or_doc, **kwds) if isinstance(dom_or_doc, BeautifulSoup.BeautifulSoup) else dom_or_doc
        
        self.doc.window        = self
        self.doc.contentWindow = self
         
        self._navigator = navigator if navigator else Navigator(personality, self)
        self._location  = Location(self)
        self._history   = History(self)
        self._history.update(url, replace)

        #self.doc.location = self._location
        self.doc.location = property(self.getLocation, self.setLocation)

        self._target = target
        self._parent = parent
        self._opener = opener
        self._screen = screen or Screen(width, height, 32)
        self._closed = False
        
        self._personality = personality
        self.__init_personality()

        self.name          = name
        self.defaultStatus = ""
        self.status        = ""
        self._left         = left
        self._top          = top
        self.innerWidth    = width
        self.innerHeight   = height
        self.outerWidth    = width
        self.outerHeight   = height
        self.timers        = []
        self.java          = java()

    def __getattr__(self, name):
        if name == 'constructor':
            return PyV8.JSClassConstructor(self.__class__)

        #if name == 'prototype':
        #    return PyV8.JSClassPrototype(self.__class__)

        prop = self.__dict__.setdefault('__properties__', {}).get(name, None)

        if prop and callable(prop[0]):
            return prop[0]()

        raise AttributeError(name)

    @property 
    def closed(self):
        return self._closed

    def close(self):
        self._closed = True

    @property
    def this(self):
        return self

    @property
    def window(self):
        return self

    @property
    def self(self):
        return self

    @property
    def top(self):
        return self

    @property
    def document(self):
        return self.doc

    def _findAll(self, tags):
        return self.doc.doc.find_all(tags, recursive = True)

    @property
    def frames(self):
        """an array of all the frames (including iframes) in the current window"""
        return w3c.HTMLCollection(self.doc, [self.doc.createHTMLElement(self.doc, f) for f in self._findAll(['frame', 'iframe'])])

    @property
    def length(self):
        """the number of frames (including iframes) in a window"""
        return len(self._findAll(['frame', 'iframe']))

    @property
    def history(self):
        """the History object for the window"""
        return self._history

    def getLocation(self):
        """the Location object for the window"""
        return self._location

    def setLocation(self, location):
        self._location.href = location

    location = property(getLocation, setLocation)

    @property
    def navigator(self):
        """the Navigator object for the window"""
        return self._navigator

    @property
    def opener(self):
        """a reference to the window that created the window"""
        return self._opener

    @property
    def pageXOffset(self):
        return 0

    @property
    def pageYOffset(self):
        return 0

    @property
    def parent(self):
        return self._parent

    @property
    def screen(self):
        return self._screen
        
    @property
    def screenLeft(self):
        return self._left

    @property
    def screenTop(self):
        return self._top

    @property
    def screenX(self):
        return self._left

    @property
    def screenY(self):
        return self._top

    def ActiveXObject(self, cls, type = 'name'):
        return _ActiveXObject(self, cls, type = 'name')

    # Window object methods
    #
    # escape        Encodes a string.
    # sizeToContent Sizes the window according to its content.
    # unescape      Unencodes a value that has been encoded in hexadecimal (e.g., a cookie).

    def alert(self, text):
        """
        Display an alert dialog with the specified text.
        Syntax

        window.alert(text) 

        Parameters

        text is a string of the text you want displayed in the alert dialog.
        """
        log.warning('[Window] Alert Text: %s' % (str(text), ))

    def back(self):
        """
        Returns the window to the previous item in the history.
        Syntax

        window.back() 

        Parameters

        None.
        """
        pass

    def blur(self):
        """
        Shifts focus away from the window.
        Syntax

        window.blur() 

        Parameters

        None.
        """
        pass

    def captureEvents(self, eventType):
        """
        Registers the window to capture all events of the specified type.
        Syntax

        window.captureEvents(Event.eventType) 

        Parameters

        eventType is a string
        """
        self.alert("[Captured Event] %s" % (eventType, ))

    def clearInterval(self, intervalID):
        """
        Clears a delay that's been set for a specific function.
        Syntax

        window.clearInterval(intervalID) 

        Parameters

        intervalID is the ID of the specific interval you want to clear.
        """
        self.timers[intervalID].stop()

    def clearTimeout(self, timeoutID):
        """
        Clears the delay set by window.setTimeout().
        Syntax

        window.clearTimeout(timeoutID) 

        Parameters

        timeoutID is the ID of the timeout you wish you clear.
        """
        self.timers[timeoutID].stop()
    
    def confirm(self, text):
        """
        Displays a dialog with a message that the user needs to respond to.
        Syntax

        result = window.confirm(text) 

        Parameters

        text is a string.

        result is a boolean value indicating whether OK or Cancel was selected.
        """
        return True

    def dump(self, text):
        """
        Prints messages to the console.
        Syntax

        window.dump(text) 

        Parameters

        text is a string.
        """
        self.alert(text)

    def focus(self):
        """
        Sets focus on the window.
        Syntax

        window.focus() 

        Parameters

        None.
        """
        pass
    
    def forward(self):
        """
        Moves the window one document forward in the history.
        Syntax

        window.forward() 

        Parameters

        None.
        """
        self._history.forward()

    def GetAttention(self):
        """
        Flashes the application icon to get the user's attention.
        Syntax

        window.GetAttention() 

        Parameters

        None.
        """
        pass

    def getSelection(self):
        """
        Returns the selection (generally text).
        Syntax

        selection = window.getSelection() 

        Parameters

        selection is a selection object.
        """
        return None

    def home(self):
        """
        Returns the window to the home page.
        Syntax

        window.home() 

        Parameters

        None.
        """
        self.open()

    def moveBy(self, deltaX, deltaY):
        """
        Moves the current window by a specified amount.
        Syntax

        window.moveBy(deltaX, deltaY) 

        Parameters

        deltaX is the amount of pixels to move the window horizontally.
        deltaY is the amount of pixels to move the window vertically.
        """
        pass

    def moveTo(self, x, y):
        """
        Moves the window to the specified coordinates.
        Syntax

        window.moveTo(x, y) 

        Parameters

        x is the horizontal coordinate to be moved to.
        y is the vertical coordinate to be moved to.
        """
        pass
    
    def prompt(self, text):
        """
        Returns the text entered by the user in a prompt dialog. 
        """
        return text

    def releaseEvents(self, eventType):
        """
        Releases the window from trapping events of a specific type.
        Syntax

        window.releaseEvents(Event.eventType) 

        Parameters

        eventType is a string
        """
        self.alert("[Released Event] %s" % (eventType, ))

    def resizeBy(self, xDelta, yDelta):
        """
        Resizes the current window by a certain amount.
        Syntax

        window.resizeBy(xDelta, yDelta) 

        Parameters

        xDelta is the number of pixels to grow the window horizontally.
        yDelta is the number of pixels to grow the window vertically.
        """
        pass

    def resizeTo(self, iWidth, iHeight):
        """
        Dynamically resizes window.
        Syntax

        window.resizeTo(iWidth, iHeight) 

        Parameters

        iWidth is an integer representing the new width in pixels.
        iHeight is an integer value representing the new height in pixels.
        """
        pass

    def scroll(self, x, y):
        """
        Scrolls the window to a particular place in the document.
        Syntax

        window.scroll(x-coord, y-coord) 

        Parameters

        x-coord is the pixel along the horizontal axis of the document that
        you want displayed in the upper left.
        y-coord is the pixel along the vertical axis of the document that you
        want displayed in the upper left.
        """
        pass

    def scrollBy(self, xDelta, yDelta):
        """
        Scrolls the document in the window by the given amount.
        Syntax

        window.scrollBy(xDelta, yDelta) 

        Parameters

        xDelta is the amount of pixels to scroll horizontally.

        yDelta is the amount of pixels to scroll vertically.
        """
        pass
    
    def scrollByLines(self, lines):
        """
        Scrolls the document by the given number of lines.
        Syntax

        window.scrollByLines(lines) 

        Parameters

        lines is the number of lines.
        """
        pass

    def scrollByPages(self, pages):
        """
        Scrolls the current document by the specified number of pages.
        Syntax

        window.scrollByPages(pages) 

        Parameters

        pages is the number of pages to scroll.
        """
        pass

    def scrollTo(self, x, y):
        """
        Scrolls to a particular set of coordinates in the document.
        Syntax

        window.scrollTo(x-coord, y-coord) 

        Parameters

        x-coord is the pixel along the horizontal axis of the document that you
        want displayed in the upper left.

        y-coord is the pixel along the vertical axis of the document that you
        want displayed in the upper left.
        """
        pass

    def setCursor(self, s):
        pass

    def setInterval(self, f, delay, lang = 'JavaScript'):
        """
        Set a delay for a specific function.
        Syntax

        ID = window.setInterval("funcName", delay)

        Parameters

        funcName is the name of the function for which you want to set a
        delay.

        delay is the number of milliseconds (thousandths of a second)
        that the function should be delayed.

        ID is the interval ID.
        """
        timer = Window.Timer(self, f, delay, True, lang)
        self.timers.append(timer)
        timer.start()

        return len(self.timers) - 1 

    def setTimeout(self, f, delay, lang = 'JavaScript'):
        """
        Sets a delay for executing a function.
        Syntax

        ID = window.setTimeout("funcName", delay) 

        Parameters

        funcName is the name of the function for which you want to set a
        delay.

        delay is the number of milliseconds (thousandths of a second)
        that the function should be delayed.

        ID is the interval ID.
        """
        timer = Window.Timer(self, f, delay, False, lang)
        self.timers.append(timer)
        timer.start()

        return len(self.timers) - 1

    def stop(self):
        """
        This method stops window loading.
        Syntax

        window.stop() 

        Parameters

        None.
        """
        pass

    # Windows Script Host Run method documentation at
    # http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.85).aspx
    def _Run(self, strCommand, intWindowStyle = 0, bWaitOnReturn = False):
        log.warning("[Windows Script Host Run] Command: \n%s\n", strCommand)
        if not 'http' in strCommand:
            return

        self._doRun(strCommand, 1)

    def _doRun(self, p, stage):
        if not isinstance(p, str):
            return

        try:
            pe = pefile.PE(data = p, fast_load = True)
            return
        except:
            pass

        log.ThugLogging.add_code_snippet(p, 'VBScript', 'Contained_Inside')
        log.warning("[Windows Script Host Run - Stage %d] Code:\n%s" % (stage, p, ))

        while True:
            try:
                index = p.index('"http')
            except ValueError:
                break

            p = p[index + 1:]
            s = p.split('"')
            if len(s) < 2:
                break

            url = s[0]
            log.warning("[Windows Script Host Run - Stage %d] Downloading from URL %s" % (stage, url, ))

            try:
                response, content = self._navigator.fetch(url)
            except:
                continue

            if response.status == 404:
                continue

            md5 = hashlib.md5()
            md5.update(content)
            log.warning("[Windows Script Host Run - Stage %d] Saving file %s" % (stage, md5.hexdigest()))
            p = '"'.join(s[1:])
            
            self._doRun(content, stage + 1)
                
    def _attachEvent(self, sEvent, fpNotify):
        log.debug("[attachEvent] %s %s" % (sEvent, fpNotify, ))
        fpNotify.__call__()

    def _detachEvent(self, sEvent, fpNotify):
        log.debug("[detachEvent] %s %s" % (sEvent, fpNotify, ))

    def _addEventListener(self, type, listener, useCapture = False):
        log.debug("[addEventListener] %s %s %s" % (type, listener, useCapture, ))

    def _removeEventListener(self, type, listener, useCapture = False):
        log.debug("[removeEventListener] %s %s %s" % (type, listener, useCapture, ))

    def _CollectGarbage(self):
        pass

    def __init_personality(self):
        if self._personality.startswith(('xpie', 'w2kie')):
            self.attachEvent    = self._attachEvent
            self.detachEvent    = self._detachEvent
            self.Run            = self._Run
            self.CollectGarbage = self._CollectGarbage

        if self._personality.startswith('firefox'):
            self.addEventListener    = self._addEventListener
            self.removeEventListener = self._removeEventListener

    def eval(self, script):
        return self.evalScript(script)

    @property
    def context(self):
        if not hasattr(self, '_context'):
            self._context = PyV8.JSContext(self)
            with self._context as ctxt:
                thug_js = os.path.join(os.path.dirname(os.path.abspath(__file__)), "thug.js")
                ctxt.eval(open(thug_js, 'r').read())

        return self._context

    def evalScript(self, script, tag = None):
        result = 0
        
        if tag:
            self.doc.current = tag
        else:
            try:
                body = self.doc.body
            except:
                # This code is for when you are desperate :)
                body = self.doc.getElementsByTagName('body')[0]

            if body:
                self.doc.current = body.tag.contents[-1]
            else:
                self.doc.current = self.doc.doc.contents[-1]

        with self.context as ctxt:
            try:
                ast = AST(script)
            except:
                log.debug(traceback.format_exc())
                return result

            if self._personality.startswith(('xpie', 'w2kie')):
                script = script.replace('@cc_on!@', '*/!/*')

            shellcode = Shellcode.Shellcode(ctxt, ast, script)
            result    = shellcode.run()

        return result

    def fireOnloadEvents(self):
        #for tag in self._findAll('script'):
        #   self.evalScript(tag.string, tag = tag)
        for tag in self._findAll('object'):
            classid = tag.get('classid', None)
            id      = tag.get('id', None)
            if not classid or not id:
                continue

            setattr(self, id, _ActiveXObject(self, classid, 'id'))

        index = 0
        tags  = self._findAll('script')
        while index < len(self._findAll('script')):
            tag = self._findAll('script')[index]
            if not tag.string:
                src = tag.get('src', None)
                if src:
                    try:
                        response, js = self._navigator.fetch(src)
                    except:
                        continue

                    if response.status == 404:
                        continue

                    tag.setString(js)
            try:
                self.evalScript(tag.string, tag = tag)
            except:
                log.debug(traceback.format_exc())

            index += 1

        body = self.doc.body
        if body and body.tag.has_attr('onload'):
            self.evalScript(body.tag['onload'], tag = body.tag.contents[-1])

        if hasattr(self, 'onload'):
            self.evalScript(self.onload)

    def Image(self, width = 800, height = 600):
        return self.doc.createElement('img')

    def XMLHttpRequest(self):
        return _ActiveXObject(self, 'microsoft.xmlhttp')

    def open(self, url = None, name = '_blank', specs = '', replace = False):
        if url:
            try:
                response, html = self._navigator.fetch(url)
            except:
                return None

            if response.status == 404:
                return None

            if 'content-type' in response and response['content-type'] in ('application/pdf', ):
                return None

            # Log response here
            kwds = { 'referer' : self.url }
            if 'set-cookie' in response:
                kwds['cookie'] = response['set-cookie']
            if 'last-modified' in response:
                kwds['lastModified'] = response['last-modified']
        else:
            url  = 'about:blank'
            html = ''
            kwds = {}
       
        dom = BeautifulSoup.BeautifulSoup(html, "html5lib")
        
        for spec in specs.split(','):
            spec = [s.strip() for s in spec.split('=')]

            if len(spec) == 2:
                if spec[0] in ['width', 'height', 'left', 'top']:
                    kwds[spec[0]] = int(spec[1])

            if name in ['_blank', '_parent', '_self', '_top']:
                kwds['target'] = name
                name = ''
            else:
                kwds['target'] = '_blank'

        return Window(url, dom, navigator = None, personality = self._personality, 
                        name = name, parent = self, opener = self, replace = replace, **kwds)
class Interpreter(Cmd):
    
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    BRYELLOW = '\033[93m'
    YELLOW = '\033[33m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    INTRO = BRYELLOW + \
'''   __
 <(\033[91mo\033[93m )___
  ( ._> /  
   `---'   
''' + ENDC
    
    def __init__(self):
        Cmd.__init__(self)
        if sys.platform.startswith("win"):
            self.__disableColors()
        
        self.__home = os.path.expanduser("~")
        self.__appDir = os.path.dirname(__file__)
        self.__historyFile = os.path.normpath(self.__home + "/.pkt_synth")
        self.__packetGenerator = None
        self.__packetSender = None
        self.__context = {}
        self.__packet_context_difference = set([])
        self.__sender_context_difference = set([])
        self.__listPacketGenerators = self.__findInheritedClasses('AbstractPacket')
        self.__listPacketSenders = self.__findInheritedClasses('AbstractSender')
        self.prompt = self.OKGREEN + ">> " + self.ENDC
        self.__valueParser = VariableValueParser(self.__context)
        
        readlineImpl = ReadLineImpl(self.__historyFile)
        self.__history = History( readlineImpl )
        self.__history.load()
        
    
    def __disableColors(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.BRYELLOW = ''
        self.YELLOW = ''
        self.FAIL = ''
        self.ENDC = ''
        self.INTRO =\
'''   __
 <(o )___
  ( ._> /  
   `---'   
'''
    
    def __findInheritedClasses(self, baseClass):
        fileExtensions = ('.py')
        definedModules = []
        allClasses = {}

        for fileInCurDir in os.listdir(self.__appDir):
            if fileInCurDir.endswith(fileExtensions):
                filename = fileInCurDir.split('.')[0]
                fp, pathname, description = imp.find_module(filename)
                module = imp.load_module(filename, fp, pathname, description)
                for name, value in inspect.getmembers(module):
                    if inspect.isclass(value):
                        if str(name).find(baseClass) == -1:
                            allClasses[name] = value
        
        for className in allClasses:
            if str(inspect.getmro(allClasses[className])).find(baseClass + ".") != -1:
                definedModules.append(className)
        return definedModules

    def saveHistory(self):
        self.__history.save()

    def cmdloop(self):
        intro = self.INTRO
        
        while(True):
            try:
                Cmd.cmdloop(self, intro)
                break
            except KeyboardInterrupt:
                self.stdout.write("\n")
                break
            except Exception:
                import traceback
                traceback.print_exc()

        self.saveHistory()   
            
    
    def do_quit(self, arg):
        return True
        
    def do_EOF(self, arg):
        return True
    
    def print_possible_load_list(self):
        print "Generators:"
        self.print_generators()
        print "Senders:"
        self.print_senders()       
    
    def print_generators(self):
        for module in self.__listPacketGenerators:
            print "\t" + module
    
    def print_senders(self):
        for module in self.__listPacketSenders:
            print "\t" + module
        
    def print_env(self, arg):
        for var in self.__context.keys():
            print self.BRYELLOW + var + self.ENDC + "=" + repr(self.__context[var])
        
    def do_export(self, arg):
        if '' == arg:
            return self.print_env(arg)
            
        eqPos = arg.find("=")
        if( -1 == eqPos):
            print "Syntax error. Please use as set var=value"
        else:
            self.__context[arg[0:eqPos].strip()] = self.parseVariableValue(arg[eqPos+1:])
    
    def do_send(self, arg):
        if '' == arg:
            arg = "1"
        
        packet = self.__packetGenerator.generatePacket();
            
        for x in range( 0, int(arg)):       
            self.__packetSender.sendPacket(packet)
            print "Sent[" + str(x) + "]:"  + repr(packet);
    
    def do_load(self, arg):
        return self.exceptSafeInvoker(partial(self.load_impl,arg))
    
    def removeItems(self, dict, keys ):
        for key in keys:
            dict.pop(key)
    
    def load_impl(self, arg):
        if arg == '':
            self.print_possible_load_list()
            return
        
        if( str(arg) in self.__listPacketGenerators):
            self.removeItems(self.__context, self.__packet_context_difference)
            contextCopy = self.__context.copy()
            self.loadGenerator(arg)
            self.__packet_context_difference = set(self.__context.keys()) - set(contextCopy.keys())
            return
        
        if( arg in self.__listPacketSenders):
            self.removeItems(self.__context, self.__sender_context_difference)
            contextCopy = self.__context.copy()
            self.loadSender(arg)
            self.__sender_context_difference = set(self.__context.keys()) - set(contextCopy.keys())
            return
        
        print arg + " is not loaded, no such module "
        
    def loadGenerator(self, arg):
        fp = None
        try:
            fp, pathname, description = imp.find_module(arg)
            module = imp.load_module(arg, fp, pathname, description)
            packetGenerator = getattr(module, arg)
            self.__packetGenerator = packetGenerator(self.__context)
            print arg + " loaded"
        except:
            import traceback
            traceback.print_exc()
            print arg + " is not loaded, no such module "
            if fp:
                fp.close()

    def loadSender(self, arg):
        fp = None
        try:
            fp, pathname, description = imp.find_module(arg)
            module = imp.load_module(arg, fp, pathname, description)
            packetSender = getattr(module, arg)
            self.__packetSender = packetSender(self.__context)
            print arg + " loaded "
        except:
            print arg + " is not loaded, no such module "
            if fp:
                fp.close()
                          
    def do_help(self, arg):
        print "\t load [sender,generator] - load available packet sender or available packet type generator"
        print "\t quit - exit application"
        print "\t send [number_of_packets_to_send] - generate and send packet"
        print "\t export [variable = value | {lambda} | {$variable} ] - setting value to a context variable"
        print "\t clobber - reset everything, besides history"
        print "\t history [[script] | [last <number>] | [clear]]"
    
    def do_clobber(self, arg):
        return self.exceptSafeInvoker(partial(self.clobber_impl,arg)) 
      
    def clobber_impl(self, arg):
        self.__packetGenerator = None
        self.__packetSender = None
        self.__context = {}
        
    def do_history(self, arg):
        return self.exceptSafeInvoker(partial(self.history_impl,arg))
    
    def printHistory(self, history):
        for item in history:
            print item
    
    def history_impl(self, arg):
        if arg.startswith("clear"):
            self.__history.clear();
            return
        
        if '' == arg:
            self.printHistory(self.__history.get(self.__history.passAllPredicate))
            return
        
        if arg.startswith('script'):
            self.printHistory(self.__history.get(self.__history.passScript))
            return
                
        if arg.startswith('last'):
            linesNumber = int(arg.replace('last', '').strip(" "))
            self.printHistory(self.__history.getLastN_lines(linesNumber))
            return
        
        print "history syntax error, please type help for more information"
        
    
    def completedefault_impl(self, predicate, all_targets, *ignored):
        if predicate(ignored[1]):
            if ignored[0] == '':
                return all_targets
            else:
                matched = []
                for target in all_targets:
                    if target.lower().startswith(ignored[0].lower()):
                        matched.append(target)
                        
                return matched
        
        return None
    
    def load_cmd_predicate(self, cmd):
        if cmd.startswith('load'):
            return True
        
        return False
    
    def export_cmd_predicate(self, cmd):
        isVariableName_LHS = (cmd.startswith('export') and cmd.find('=') == -1)
        rhsVariablePattern = re.compile(r'=\{\$')
        
        if isVariableName_LHS or (re.search(rhsVariablePattern, cmd ) is not None):
            return True
        
        return False
    
    def history_cmd_predicate(self, cmd):
        return cmd.startswith("history")
        
    def completedefault(self, *ignored):
        completation_variants =\
            self.completedefault_impl( self.load_cmd_predicate, 
                                       self.__listPacketGenerators + self.__listPacketSenders,
                                       *ignored )
        if completation_variants is not None:
            return completation_variants
        
        completation_variants =\
            self.completedefault_impl( self.export_cmd_predicate, 
                                       self.__context.keys(),
                                       *ignored )
        if completation_variants is not None:
            return completation_variants
        
        completation_variants =\
            self.completedefault_impl( self.history_cmd_predicate, 
                                       ['last', 'script', 'clear'],
                                       *ignored )
        if completation_variants is not None:
            return completation_variants
        
        return []
            
    def exceptSafeInvoker(self, functor):
        try:
            return functor()
        except IOError as e:
            print "I/O error({0}): {1}".format(e.errno, e.strerror)
        except ValueError:
            print "Could not convert data to an integer."
        except KeyboardInterrupt:
            return True
        except:
            import traceback
            traceback.print_exc()
        
        return None 
    
    def parseVariableValue(self, value):
        return self.__valueParser.parseVariableValue(value)    
Exemple #40
0
    def __init__(self,
                 _input,  # input capture
                 _maxBuffer = 25, # fifo max size
                 _firstFrameCallback = None, # callback for first read frame
                 _inputFrameCallback = None, # callback for next frames before changing anything
                 _outputFrameCallback = None, # callback for next frames just before displaying it
                 _infoCallback = None, # callback to get informations to display in legend
                 _detailsCallback = None, # callback to get detailled informations to display when 'i' pressed
                 _popCallback = None, # callback when a frame is popped from history
                 _stepByStep = True, # play/pause mode at startup
                 _showInput = True, # show input in a window
                 _showOutput = True, # show output in a window
                 _skip = 0, # frames to skip at startup
                 _frameRate = None # give framerate
    ):
        self.inputFile = _input
        self.bufferMax = _maxBuffer

        self.inputFrameCallback = _inputFrameCallback
        self.outputFrameCallback= _outputFrameCallback
        self.infoCallback  = _infoCallback
        self.detailsCallback  = _detailsCallback
        self.popCallback = _popCallback

        self.stepByStep = _stepByStep

        self.showInput = _showInput
        if (self.showInput):
            cv2.namedWindow("raw")
        self.showOutput = _showOutput
        if (self.showOutput):
            cv2.namedWindow("output")

        self.cap = cv2.VideoCapture(self.inputFile)

        # skip first frames if asked for
        if _skip != 0 :
            print "Skipping", _skip, "frames ..."
            for i in range( 1 , _skip ) :
                if not self.cap.grab():
                    exit(1)

        # read first frame, deduce size
        ret, frame = self.cap.read()
        if not ret:
            exit(1)

        self.history = History(_next = self.getNextFrame, _first = { "input": frame },
                          _maxSize = _maxBuffer, _initialIndex = _skip,
                          _removeCallback = self.innerPopCallback)

        step = self.history.get()

        self.width = step['input'].shape[1]
        self.height = step['input'].shape[0]
        self.nbPixel = self.width * self.height


        if type(self.inputFile) is int:
            self.frameRate = None
            self.start = time()
            if _frameRate is None:
                raise Exception("capture from cam must specify framerate")

            elif _frameRate == 'auto':
                # read some frame to evaluate framerate
                for f in range(1,50):
                    ret, frame = self.cap.read()
                    if not ret:
                        print "End of capture during framerate calculation ..."
                        return None
                self.frameRate = 50 / (time() - self.start)

            else:
                self.frameRate = _frameRate
                print "force cam to frame rate", _frameRate
                if not self.cap.set(cv2.cv.CV_CAP_PROP_FPS, _frameRate):
                    print "Failed to force framerate"
                    # TODO : look a v4l compilation options to enable fps set ?
#             self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
#             self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 200)

        else:
            self.frameRate = self.cap.get(cv2.cv.CV_CAP_PROP_FPS)
            if self.frameRate <= 0:
                if _frameRate is None:
                    print "Can't get frame rate ({0}), defaults to 25".format(self.frameRate)
                    self.frameRate = 25
                else:
                    self.frameRate = _frameRate

        self.fcc = self.cap.get(cv2.cv.CV_CAP_PROP_FOURCC)
        if self.fcc <= 0:
            print "Can't get fcc, defaults to XVID"
            self.fcc = cv2.cv.CV_FOURCC(*'XVID')

        if self.showInput:
            cv2.imshow("raw", step['input'])

        frame = step['input'].copy()

        if _firstFrameCallback is not None:
            _firstFrameCallback(self, frame)

        step['output'] = frame

        legend = "input '{3}' {0}x{1}@{2}".format(self.width, self.height, self.frameRate, self.inputFile)
        print legend
        cv2.putText(step['output'], legend, (5, self.legendLine*20), self.font, 0.5, (255,255,255))

        if self.showOutput:
            cv2.imshow("output", step['output'])
Exemple #41
0
class VideoReader:
    # max len of buffer # TODO : add argument
    bufferMax = 25

    # step by step mode # TODO : add argument
    stepByStep = True

    # where to insert legend in output image (to be overloaded by sub classes)
    legendLine = 1

    font = cv2.FONT_HERSHEY_SIMPLEX


    def getNextFrame(self):
        ret, frame = self.cap.read()
        if not ret:
            return None
        return { "input": frame }


    def showInfo(self):
        print "Frame {0}".format(self.history.index())


    # return (hours, minutes, seconds, hundredth) from frame number and frame rate 
    def frameToTimestamp(self, frameNum):
        c = frameNum * 100 / self.frameRate
        return self.toTimestamp(c)


     # return (hours, minutes, seconds, hundredth) from start time and "now" time 
    def getTimestamp(self, start, now):
        c = int((now - start)*100)
        return self.toTimestamp(c)

    def toTimestamp(self, c):
        s = int(math.floor(c / 100))
        c = c % 100
        m = int(math.floor(s / 60))
        s = s % 60
        h = int(math.floor(m / 60))
        m = m % 60
        return (h, m, s, c)


    def __init__(self,
                 _input,  # input capture
                 _maxBuffer = 25, # fifo max size
                 _firstFrameCallback = None, # callback for first read frame
                 _inputFrameCallback = None, # callback for next frames before changing anything
                 _outputFrameCallback = None, # callback for next frames just before displaying it
                 _infoCallback = None, # callback to get informations to display in legend
                 _detailsCallback = None, # callback to get detailled informations to display when 'i' pressed
                 _popCallback = None, # callback when a frame is popped from history
                 _stepByStep = True, # play/pause mode at startup
                 _showInput = True, # show input in a window
                 _showOutput = True, # show output in a window
                 _skip = 0, # frames to skip at startup
                 _frameRate = None # give framerate
    ):
        self.inputFile = _input
        self.bufferMax = _maxBuffer

        self.inputFrameCallback = _inputFrameCallback
        self.outputFrameCallback= _outputFrameCallback
        self.infoCallback  = _infoCallback
        self.detailsCallback  = _detailsCallback
        self.popCallback = _popCallback

        self.stepByStep = _stepByStep

        self.showInput = _showInput
        if (self.showInput):
            cv2.namedWindow("raw")
        self.showOutput = _showOutput
        if (self.showOutput):
            cv2.namedWindow("output")

        self.cap = cv2.VideoCapture(self.inputFile)

        # skip first frames if asked for
        if _skip != 0 :
            print "Skipping", _skip, "frames ..."
            for i in range( 1 , _skip ) :
                if not self.cap.grab():
                    exit(1)

        # read first frame, deduce size
        ret, frame = self.cap.read()
        if not ret:
            exit(1)

        self.history = History(_next = self.getNextFrame, _first = { "input": frame },
                          _maxSize = _maxBuffer, _initialIndex = _skip,
                          _removeCallback = self.innerPopCallback)

        step = self.history.get()

        self.width = step['input'].shape[1]
        self.height = step['input'].shape[0]
        self.nbPixel = self.width * self.height


        if type(self.inputFile) is int:
            self.frameRate = None
            self.start = time()
            if _frameRate is None:
                raise Exception("capture from cam must specify framerate")

            elif _frameRate == 'auto':
                # read some frame to evaluate framerate
                for f in range(1,50):
                    ret, frame = self.cap.read()
                    if not ret:
                        print "End of capture during framerate calculation ..."
                        return None
                self.frameRate = 50 / (time() - self.start)

            else:
                self.frameRate = _frameRate
                print "force cam to frame rate", _frameRate
                if not self.cap.set(cv2.cv.CV_CAP_PROP_FPS, _frameRate):
                    print "Failed to force framerate"
                    # TODO : look a v4l compilation options to enable fps set ?
#             self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
#             self.cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 200)

        else:
            self.frameRate = self.cap.get(cv2.cv.CV_CAP_PROP_FPS)
            if self.frameRate <= 0:
                if _frameRate is None:
                    print "Can't get frame rate ({0}), defaults to 25".format(self.frameRate)
                    self.frameRate = 25
                else:
                    self.frameRate = _frameRate

        self.fcc = self.cap.get(cv2.cv.CV_CAP_PROP_FOURCC)
        if self.fcc <= 0:
            print "Can't get fcc, defaults to XVID"
            self.fcc = cv2.cv.CV_FOURCC(*'XVID')

        if self.showInput:
            cv2.imshow("raw", step['input'])

        frame = step['input'].copy()

        if _firstFrameCallback is not None:
            _firstFrameCallback(self, frame)

        step['output'] = frame

        legend = "input '{3}' {0}x{1}@{2}".format(self.width, self.height, self.frameRate, self.inputFile)
        print legend
        cv2.putText(step['output'], legend, (5, self.legendLine*20), self.font, 0.5, (255,255,255))

        if self.showOutput:
            cv2.imshow("output", step['output'])


    def run(self):
        try:
            if not self.showOutput and not self.showInput:
                import keys
                self.keys = keys.single_keypress()
    
            # read frames
            while(True):
                isNew = False
    
                if self.showOutput or self.showInput:
                    # force redraw + handle keys
                    if self.stepByStep:
                        kc = translateKey(cv2.waitKey(0))
                    else:
                        kc = translateKey(cv2.waitKey(1))
                else:
                    # no window -> use stdin
                    if self.stepByStep:
                        kc = self.keys.read(True)
                    else:
                        kc = self.keys.read()
    
                if kc == 'i':
                    print "Frame {0}".format(self.history.index())
                    if self.detailsCallback is not None:
                        print "  ", self.detailsCallback(self, index, step)
                    continue

                elif kc == 'q':
                    break

                elif kc == 's' or kc == 'p':
                    self.stepByStep = not self.stepByStep
                    continue
    
                # left key => go backward
                elif kc == 'LEFT':
                    step = self.history.backward()
                    if step is None:
                        continue
    
                else:
                    step, isNew = self.history.forward()
    
                if step is None:
                    break;

                if self.showInput:
                    cv2.imshow("raw", step['input'])
    
                if isNew:
                    step['output'] = step['input'].copy()
                    self.frameNumber = self.history.index()
    
                    if self.inputFrameCallback is not None:
                        self.inputFrameCallback(self, self.frameNumber, step)
    
                    if type(self.inputFile) is int:
                        legend = "{0:02}:{1:02}:{2:02}.{3:02}".format(*self.getTimestamp(self.start, time()))
                    else:
                        legend = "{0:02}:{1:02}:{2:02}.{3:02}".format(*self.frameToTimestamp(self.frameNumber))
                    if self.infoCallback is not None:
                        legend = legend + ": " + self.infoCallback(self, self.frameNumber, step)
                    cv2.putText(step['output'], legend, (5, self.legendLine*20), self.font, 0.5, (255,255,255))
                    if self.outputFrameCallback is not None:
                        self.outputFrameCallback(self, self.frameNumber, step)
    
                if self.showOutput:
                    cv2.imshow("output", step['output'])

   
            # We're at end of video input => force output of last frames in history
            if len(self.history.buffer) != 0 and self.popCallback is not None:
                print "popping queue {1} frames from {0}".format(self.history.iter0, len(self.history.buffer))
    
                while self.history.pop() is not None:
                    # pop() will call its callback -> nothing to do in loop
                    pass

        finally:
            if not self.showOutput and not self.showInput:
                self.keys.stop()

    def innerPopCallback(self, stepNumber, step):
        if self.popCallback is not None:
            self.popCallback(self, stepNumber, step)
from Menu import Menu
from Integrals import Integrals
from History import History
from InputReader import InputReader
from Help import Help

# creating necessary objects
log = History()
menu = Menu()
reader = InputReader()
sub = Integrals()
myHelp = Help()

while True:

    selection = menu.select()           #

    if selection == 'q':                # Quiting the main menu
        break
    elif selection == '1':              # choosing single integral option
        result = sub.singleIntegral()
        log.add(result)
    elif selection == '2':              # choosing many integrals
        results = sub.manyIntegrals()
        log.add(results)
    elif selection == '3':              # choosing history
        log.show()
        raw_input("Press any key to close...")
    elif selection == '4':              # choosing help
        myHelp.show()
        raw_input("Press any key to close...")
Exemple #43
0
class ScribbleArea(QtGui.QWidget):
	"""
	class adds canvas area to MainWindow, overrides parent's event functions
	"""
	def __init__(self, parent = None):
		QtGui.QWidget.__init__(self, parent)
		
		self.parent = parent
		
		self.usersScribbleHistory = None

		self.setAttribute(QtCore.Qt.WA_StaticContents)
		self.modified = False
		self.scribbling = False
		self.myPenWidth = 2
		self.myPenColor = QtGui.QColor(0, 170, 255)
		imageSize = QtCore.QSize()
		self.image = QtGui.QImage(imageSize, QtGui.QImage.Format_RGB32)
		self.lastPoint = QtCore.QPoint()
		"""
		self.scrollArea = QtGui.QScrollArea()
		self.scrollArea.setBackgroundRole(QtGui.QPalette.Dark)
		self.scrollArea.setWidget(self.scribbleArea)
		"""		
		self.history = History()

	def saveImage(self, fileName, fileFormat):
		visibleImage = self.image
		#self.resizeImage(visibleImage, self.size())

		if visibleImage.save(fileName, fileFormat):
			self.modified = False
			return True
		else:
			return False

	def setPenColor(self, newColor):
		self.myPenColor = newColor

	def setPenWidth(self, newWidth):
		self.myPenWidth = newWidth
		
	def draw(self, d, stroke, stroke_width):
		color = stroke.split(" ")
		points = d.split()
		lastP = None
		painter = QtGui.QPainter()
		painter.begin(self.image)
		painter.setPen(QtGui.QPen(QtGui.QColor(int(color[0]), int(color[1]), int(color[2])), int(stroke_width),
										  QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
										  QtCore.Qt.RoundJoin))	  
		for i in range(0, len(points), 2):
			p = QtCore.QPoint(int(points[i]), int(points[i+1]))
			if lastP is not None:
				
				painter.drawLine(QtCore.QPoint(lastP), QtCore.QPoint(p))
				rad = int(stroke_width) / 2
				self.update(QtCore.QRect(lastP, p).normalized()
										 .adjusted(-rad, -rad, +rad, +rad))
			lastP = p		
		painter.end()

	def mousePressEvent(self, event):
		if event.button() == QtCore.Qt.LeftButton:
			self.lastPoint = event.pos()
			self.scribbling = True
			self.history.newStroke(event.x(), event.y(), self.myPenWidth, \
				self.myPenColor.red(), self.myPenColor.green(), \
				self.myPenColor.blue())
			#self.history.printXEP113()

	def mouseMoveEvent(self, event):
		if (event.buttons() & QtCore.Qt.LeftButton) and self.scribbling:
			self.drawLineTo(event.pos())
			self.history.newPoint(event.x(), event.y(), self.myPenWidth, \
				self.myPenColor.red(), self.myPenColor.green(), \
				self.myPenColor.blue())
				
	def redraw(self):
		# used for undo
		
		# clear canvas
		self.image.fill(QtGui.qRgb(255, 255, 255))
		self.modified = False
		self.update()
		
		# redraw
		painter = QtGui.QPainter()
		
		for stroke in self.history.history:
			startX = -1
			startY = -1
			for point in stroke:
				x = point.getX()
				y = point.getY()
				w = point.getW()
				cR = point.getCR()
				cG = point.getCG()
				cB = point.getCB()
				
				if startX == -1 and startY == -1:
					startX = x
					startY = y
					continue
					
				painter.begin(self.image)		
				painter.setPen(QtGui.QPen(QtGui.QColor(cR, cG, cB), w,
										  QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
										  QtCore.Qt.RoundJoin))
				painter.drawLine(QtCore.QPoint(startX, startY), QtCore.QPoint(x, y))		
				painter.end()
				rad = self.myPenWidth / 2
				self.update(QtCore.QRect(QtCore.QPoint(startX, startY), QtCore.QPoint(x, y))
					.normalized().adjusted(-rad, -rad, +rad, +rad))	
					
				startX = x
				startY = y						 

	def mouseReleaseEvent(self, event):
		if event.button() == QtCore.Qt.LeftButton and self.scribbling:
			self.drawLineTo(event.pos())
			self.scribbling = False
			
			d, stroke, stroke_width = self.history.getLastXEP113()

	def paintEvent(self, event):	 
		painter = QtGui.QPainter()
		painter.begin(self)
		painter.drawImage(QtCore.QPoint(0, 0), self.image)
		painter.end()

	def resizeEvent(self, event):
		if self.width() > self.image.width() or self.height() > self.image.height():
			newWidth = max(self.width() + 128, self.image.width())
			newHeight = max(self.height() + 128, self.image.height())
			self.resizeImage(self.image, QtCore.QSize(newWidth, newHeight))
			self.update()

		QtGui.QWidget.resizeEvent(self, event)

	def drawLineTo(self, endPoint):
		painter = QtGui.QPainter()
		painter.begin(self.image)
		painter.setPen(QtGui.QPen(self.myPenColor, self.myPenWidth,
								  QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
								  QtCore.Qt.RoundJoin))
		
		painter.drawLine(self.lastPoint, endPoint)
		painter.end()
		self.modified = True

		rad = self.myPenWidth / 2
		self.update(QtCore.QRect(self.lastPoint, endPoint).normalized()
										 .adjusted(-rad, -rad, +rad, +rad))
		self.lastPoint = QtCore.QPoint(endPoint)

	def resizeImage(self, image, newSize):
		if image.size() == newSize:
			return

		newImage = QtGui.QImage(newSize, QtGui.QImage.Format_RGB32)
		newImage.fill(QtGui.qRgb(255, 255, 255))
		painter = QtGui.QPainter()
		painter.begin(newImage)
		painter.drawImage(QtCore.QPoint(0, 0), image)
		painter.end()
		self.image = newImage
	
	def isModified(self):
		return self.modified

	def penColor(self):
		return self.myPenColor

	def penWidth(self):
		return self.myPenWidth
	
	def clearImage(self):
		# triggered on pressing 'Clear' (Shift+X)
		if self.modified == True:
			reply = QtGui.QMessageBox.question(self, "Clear", 
				"Are you sure to clear the canvas?", 
				QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, 
				QtGui.QMessageBox.No)
			
			if reply == QtGui.QMessageBox.Yes:
				# clears image
				self.image.fill(QtGui.qRgb(255, 255, 255))
				self.modified = False
				self.update()
				self.history.clear()