Example #1
0
 def _run(self, base_path):
     m = Miner([
         detection.WordpressDetector(),
         detection.Drupal7Detector(),
         detection.Drupal8Detector(),
     ])
     print(JSONEncoder().encode(m.mine(base_path)))
Example #2
0
    def __init__(self):
        """
        Initialize the servers and miner required for a peer to peer node to operate.
        """
        self.node_id = randbits(32)  # Create a unique ID for this node
        self.node_pool = NodePool(self.node_id, 30, 105)

        self.miner = Miner()
        self.miner.mine_event.append(self.block_mined)
        self.heartbeat = p2p.Heartbeat(Node.REQUEST_PORT, 30, self.node_id)

        router = RequestRouter(self)
        router.handlers[request_pb2.BLOB] = self.handle_blob
        router.handlers[request_pb2.DISOVERY] = self.handle_discovery
        router.handlers[request_pb2.MINED_BLOCK] = self.handle_mined_block
        router.handlers[request_pb2.RESOLUTION] = self.handle_resolution
        router.handlers[
            request_pb2.BLOCK_RESOLUTION] = self.handle_block_resolution

        self.tcp_router = server.TCPServer(Node.REQUEST_PORT, TCPRouter)
        self.tcp_router.router = router

        self.udp_router = server.UDPServer(Node.REQUEST_PORT, UDPRouter)
        self.udp_router.router = router

        self.input_server = server.TCPServer(9999, DataServer)
        self.input_server.node = self

        self.output_server = server.TCPServer(9998, OutputServer)
        self.output_server.node = self
Example #3
0
def main():
    global root
    parser = argparse.ArgumentParser()
    parser.add_argument('--epsilon', type=float, default=0.9, help='the probability to choose from memories')
    parser.add_argument('--memory_capacity', type=int, default=50000, help='the capacity of memories')
    parser.add_argument('--target_replace_iter', type=int, default=100, help='the iter to update the target net')
    parser.add_argument('--batch_size', type=int, default=16, help='sample amount')
    parser.add_argument('--lr', type=float, default=0.001, help='learning rate')
    parser.add_argument('--n_epochs', type=int, default=20000, help='training epoch number')
    parser.add_argument('--n_critic', type=int, default=100, help='evaluation point')
    parser.add_argument('--test', type=int, default=0, help='whether execute test')
    parser.add_argument('--conv', type=int, default=0, help = 'choose between linear and convolution')
    opt = parser.parse_args()
    print(opt)

    miner = Miner(opt.epsilon, opt.memory_capacity, opt.target_replace_iter, opt.batch_size, opt.lr, opt.conv)
    miner.load_params('eval.pth')
    global root
    # create Tk widget
    root = Tk()
    # set program title
    root.title("Minesweeper")
    # create game instance
    game = GUI(root)
    def sub_func():
        print('pray tell')
        s = game.get_state()
        a = miner.choose_action(s)
        game.lclicked(a)
        print(a)
        root.after(1000, sub_func)
    # run event loop
    root.after(1000, sub_func)
    root.mainloop()
    def _correct_known_words(self, trainer):
        """
        correcting known named entities (named entities in train dataset)
        :param trainer: Neural Network Model
        """

        train_iterator = trainer.trainset.return_batch(trainer.batch_size)
        vecs = trainer.trainset
        known_answer = []
        known_sentence = []
        for i, data in enumerate(train_iterator):
            with torch.no_grad():
                word = vecs.WORD.vocab.vectors[data.word].to(trainer.device)
                char = vecs.CHAR.vocab.vectors[data.char].to(trainer.device)
                mask = data.word != 1
                mask = mask.float().to(trainer.device)
                x = {'word': word, 'char': char}
                decode = trainer.model.decode(x, mask)

                for pred, ans, c in zip(decode, data.label, data.char):
                    known_answer.append([trainer.trainset.LABEL.vocab.itos[i]
                                         for i in ans[:len(pred)]])
                    known_sentence.append([trainer.trainset.CHAR.vocab.itos[i]
                                           for i in c[:len(pred)]])

        miner = Miner(known_answer, [['']],
                      known_sentence, {'PRO': [], 'SHO': []})
        self._known_words = miner.return_answer_named_entities()['unknown']
Example #5
0
    def __init__(self, public_key, private_key):
        # save inputs
        self.private_key = private_key
        self.public_key = public_key
        self.public_key_string = public_key.exportKey().decode("utf-8")

        # build wallet
        self.wallet = Wallet(self.public_key_string)

        # register miner
        pseudonym = resolve_pseudonym(self.public_key_string)
        if pseudonym == "":
            pseudonym = input("\nChoose a pseudonym: ")
        else:
            print(
                "\nThis key has already been registered.\nRegistered pseudonym: "
                + pseudonym)
        self.miner = Miner(pseudonym, self.public_key_string)
        self.s = Serializer()

        # build menu
        self.menu = "\nchoose an number:"
        self.menu += "\n1.) mine"
        self.menu += "\n2.) check balance"
        self.menu += "\n3.) make transaction"
        self.menu += "\nchoice: "
Example #6
0
def test1():
    """
    tests wallets without p2p network,
    the test is using common database
    for all wallets
    """
    logger = Logger('test1')
    logger.info('Test 1 starts')
    block_chain_db = BlockChainDB(logger)

    # creates two wallets in the system
    wallet1 = Wallet.new_wallet(block_chain_db, logger)
    wallet2 = Wallet.new_wallet(block_chain_db, logger)

    # check that their initial value is 0
    assert wallet1.balance == 0
    assert wallet2.balance == 0

    # creates the miner in the system
    wallet3 = Wallet.new_wallet(block_chain_db, logger)
    miner = Miner(wallet3)

    # mine the genesis block and check the miner balance
    miner.mine()
    first_balance = miner.wallet.get_balance()
    assert first_balance > 0

    # transfer from the miner to wallet1
    # and mine the new block
    assert miner.wallet.create_transaction(miner.wallet.balance,
                                           wallet1.address)
    miner.mine()

    # check the new balances
    assert wallet1.get_balance() == first_balance
    second_balance = miner.wallet.get_balance()
    assert second_balance > 0

    # creates new transactions from the miner
    # and wallet1 to wallet2 and mine the new block
    wallet1.create_transaction(wallet1.get_balance(), wallet2.address)
    miner.wallet.create_transaction(second_balance, wallet2.address)
    miner.mine()

    # check the new balances
    assert wallet2.get_balance() == (first_balance + second_balance)
    assert miner.wallet.get_balance() > 0

    # create new transaction that demands change
    # and mine the new block
    wallet2.create_transaction(first_balance + 1, wallet1.address)
    miner.mine()

    # check the balances
    assert wallet2.get_balance() == second_balance - 1
    assert not wallet2.create_transaction(second_balance, wallet1.address)

    logger.info('Finish successfully test 1')
    block_chain_db.close_connection()
def main():
    i = 5
    peers = [Miner.Peer("1270.0.0.1", 8880 + x) for x in range(0, i)]
    miners = [
        Miner("127.0.0.1", 8880 + x, str(x), peers, pow(10, 6))
        for x in range(0, i)
    ]
    bootserver = BootstrapServer.boot(peers)
Example #8
0
    def main(self):
        # initiate block chain with num of zero that we want for proof of work
        num_of_zero = 5
        block_chain = BlockChain(num_of_zero)
        miner = Miner(block_chain.hash_processor, block_chain.num_of_zero)

        block_count = 0
        while block_count < 5:
            transaction_list = []

            # generate random transaction
            num_of_transaction = randint(1, 4)
            for i in range(num_of_transaction):
                from_id = randint(1, 100)
                to_id = randint(1, 100)
                amount = randint(200, 1000)
                transaction = Transaction(from_id, to_id, amount)
                transaction_list.append(transaction)

            # ask miner to get new block to wrap transactions
            # miner will do brute force in order to get the appropriate hash
            block = miner.get_new_block(transaction_list,
                                        block_chain.get_prev_hash())

            # add to block_chain
            # block chain will verify the block
            block_chain.add_block(block)

            # print calculated hash value to user
            # to make sure right number of zero in the hash
            hash_val = block_chain.hash_processor.calculate_hash(
                block.get_hash_num())
            print("block number %d" % (block_count + 1, ))
            print("hash value : " + str(hash_val))
            print("generated block : " + str(block))
            print()

            block_count += 1

        # check mutation validation

        # first should be valid
        print(block_chain.check_is_valid())

        # save current value and change the amount to new one
        prev_amount = block_chain.blocks[1].transaction_list[0].amount
        block_chain.blocks[1].transaction_list[0].amount = 100800

        # check validity again, should be false
        print(block_chain.check_is_valid())

        # change it to original value
        # should be valid again
        block_chain.blocks[1].transaction_list[0].amount = prev_amount

        print(block_chain.check_is_valid())
Example #9
0
class Evaluator:
    def __init__(self,
                 model: BiLSTMCRF,
                 dataset: NestedNERDataset,
                 model_path: str = None,
                 use_gpu: bool = True):
        self.device_str = 'cuda' if torch.cuda.is_available(
        ) and use_gpu else 'cpu'
        self.device = torch.device(self.device_str)
        model.load_state_dict(torch.load(model_path))
        self.model = model.to(self.device)
        self.dataset = dataset

    def evaluate(self, batch_size: int = 64):

        sentences = []
        predicted_labels = []
        answer_labels = []
        test_iterator = self.dataset.get_batch(batch_size, 'test')
        self.model.eval()
        self.dataset.char_encoder.eval()
        with torch.no_grad():
            for data in tqdm(test_iterator):
                mask = data.label0 != 0
                mask = mask.float().to(self.device)
                vecs = self.dataset.to_vectors(data.word,
                                               data.char,
                                               data.pos,
                                               data.subpos,
                                               device=self.device_str)
                sentences.extend([
                    self.dataset.wordid_to_sentence(sentence)
                    for sentence in data.word
                ])
                input_embed = self.model.concat_embedding(list(vecs.values()))
                predicted_labels.extend(self.model.predict(input_embed, mask))
                answer_label = self.dataset.get_batch_true_label(
                    data, 0, self.device_str)
                answer_label = answer_label.numpy()
                mask = mask.cpu().numpy() > 0
                answer_label = [
                    answer_label[i, mask[i]].tolist()
                    for i in range(len(answer_label))
                ]
                answer_labels.extend(answer_label)
        answer_labels = [
            self.dataset.labelid_to_labels(answer) for answer in answer_labels
        ]
        predicted_labels = [
            self.dataset.labelid_to_labels(pred) for pred in predicted_labels
        ]
        self.miner = Miner(answer_labels, predicted_labels, sentences)
        self.miner.default_report(True)
        self.model.train()
        self.dataset.char_encoder.train()
Example #10
0
def create_users_and_ledger():
    """ Create the users and ledger """
    monies = Ledger('monies')
    alice = User('Alice', monies)
    bob = Miner('Bob', monies)
    jon = User('Jon', monies)
    howard = User('Howard', monies)
    rocky = Miner('Rocky', monies)
    # using an anon user for creating an initial balance
    anon = User('Anon', monies)
    users = [alice, jon, howard]
    miners = [rocky, bob]
    return (monies, anon, users, miners)
def test_last_block_hash():
    """
    Test function for last block hash
    """
    # Setup
    mock_ledger = Ledger('monies')
    mock_miner = Miner('Mock', mock_ledger)
    mock_header = {'hdr': 'foo'}
    mock_ledger.block_count = 1
    mock_ledger.all_transactions.append(mock_header)
    mock_message = mock_ledger.all_transactions[0]['hdr']
    mock_nonce_attempts = 1000000
    mock_difficulty = 4
    mock_hash = cryptohash(mock_message, mock_nonce_attempts, mock_difficulty)
    # Assert
    assert mock_miner.last_block_hash() == mock_hash
Example #12
0
def init():
    global tc
    global miner

    pvt_key = app.config.get('PVT_KEY')
    tc = toychain.ToyChain(app.config['PORT'], pvt_key=pvt_key)

    if not pvt_key:
        filename = app.config['CONFIG_FILENAME']
        with open(f'{filename}.py', 'a') as f:
            f.write(f'PVT_KEY = \'\'\'{tc.get_pvt_key()}\'\'\'')

    print(tc.get_address())
    if app.config['MINING']:
        miner = Miner(tc)
        miner.start()
 def newPopulation(self, miners_pop):
     new_pop = []
     soft_max = self.softmax()
     for i in range(self.maxpop):
         father = self.pickParent(miners_pop, soft_max)
         mother = self.pickParent(miners_pop, soft_max)
         if father.fitness() >= 1:
             child = Miner(father.id_number, father.size, father.maze_grid,
                           father.path)
         elif mother.fitness() >= 1:
             child = Miner(mother.id_number, mother.size, mother.maze_grid,
                           mother.path)
         else:
             child = Miner.crossover(father, mother)
         new_pop.append(child)
     return new_pop
    def predict_to_testset(self, trainer):
        """
        predicting labels of test dataset
        :param trainer: Neural Network Model
        """

        iterator = trainer.testset.return_batch(trainer.batch_size)
        vecs = trainer.testset
        for i, data in enumerate(iterator):
            with torch.no_grad():
                word = vecs.WORD.vocab.vectors[data.word].to(trainer.device)
                char = vecs.CHAR.vocab.vectors[data.char].to(trainer.device)
                mask = data.word != 1
                mask = mask.float().to(trainer.device)
                x = {'word': word, 'char': char}
                decode = trainer.model.decode(x, mask)

                for pred, ans, c in zip(decode, data.label, data.char):
                    self._answers.append([trainer.testset.LABEL.vocab.itos[i]
                                          for i in ans[:len(pred)]])
                    self._predicts.append([trainer.testset.LABEL.vocab.itos[i]
                                           for i in pred])
                    self._sentences.append([trainer.testset.CHAR.vocab.itos[i]
                                            for i in c[:len(pred)]])
        self.miner = Miner(self._answers, self._predicts,
                           self._sentences, self._known_words)
Example #15
0
    def __init__(self):
        self.peers = Peers()
        self.blacklist = BlackList()
        self.blockchain = BlockChain()
        self.storage = BlockchainStorage()
        self.mempool = Mempool()
        self.miner = Miner()

        self.ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
        self.ctx.load_cert_chain("ca_certs/server.crt", "ca_certs/server.key")
        self.ctx.load_verify_locations("ca_certs/client.crt")
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((HOST, PORT))
        self.sock.listen(1)
        logger.info(" Listening on {}:{}".format(HOST, PORT))
Example #16
0
    def report(self, show_flag: bool = True)\
            -> Dict[str, Dict[str, Dict[str, float]]]:
        """

        :param show_flag: if True, output reports to a console
        :return: reports for all, unknown only and known only NEs
                 {'all': {'NELABEL1': {'precision': 0.000,
                                       'recall': 0.000,
                                       'f1_score': 0.00},
                          'NELABEL2': {'precision': ...},
                          ...
                  'unknown': ..., 'known': ..., 'misses': ...
                          }}
        """

        sentences = self._dataset.get_sentences('test')
        known_words = self._dataset.known_NEs()
        predicts = self._model.predict_all(self.test_data[0])
        miner = Miner(self.test_data[1], predicts, sentences, known_words)
        return {
            'all': miner.default_report(show_flag),
            'unknown': miner.unknown_only_report(show_flag),
            'known': miner.known_only_report(show_flag),
            'misses': miner.return_miss_labelings(),
            'seg': {
                type_: miner.segmentation_score(type_, show_flag)
                for type_ in ['all', 'unknown', 'known']
            }
        }
Example #17
0
 def new_miners_join(self):
     """
     Arrival process is modeled as a poisson process, with mean arrival set to 3 miners per block/round. 
     The long term growth of the network is steady under this model.
     """
     num_new_miners = poisson(self.new_miner_rate)
     len_miner_set = len(self.M)
     for i in range(len_miner_set, len_miner_set + num_new_miners):
         self.M.add(Miner(ID=i))
Example #18
0
def main():
    chain_mgr.chain.build_chain(dao.get_chain())
    if sys.argv[1] == 'miner':
        miner = Miner(chain_mgr, peer_mgr)
        app.add_task(miner.start)
    app.add_task(sync_chain)
    app.add_task(advertise_self)
    app.add_task(chain_mgr.start)
    app.add_task(peer_mgr.start)
    app.run(host='0.0.0.0', port=5001)
Example #19
0
    def new_miner(self):
        "new miner is initialized if HEAD is updated"
        # prepare uncles
        uncles = set(self.get_uncles(self.head))
        ineligible = set()  # hashes
        blk = self.head
        for i in range(8):
            for u in blk.uncles:  # assuming uncle headers
                u = utils.sha3(rlp.encode(u))
                if u in self:
                    uncles.discard(self.get(u))
            if blk.has_parent():
                blk = blk.get_parent()

        miner = Miner(self.head, uncles, self.config.get('wallet', 'coinbase'))
        if self.miner:
            for tx in self.miner.get_transactions():
                miner.add_transaction(tx)
        self.miner = miner
Example #20
0
    def new_miner(self):
        "new miner is initialized if HEAD is updated"
        # prepare uncles
        uncles = set(self.get_uncles(self.head))
        ineligible = set()  # hashes
        blk = self.head
        for i in range(8):
            for u in blk.uncles:  # assuming uncle headers
                u = utils.sha3(rlp.encode(u))
                if u in self:
                    uncles.discard(self.get(u))
            if blk.has_parent():
                blk = blk.get_parent()

        miner = Miner(self.head, uncles, self.config.get('wallet', 'coinbase'))
        if self.miner:
            for tx in self.miner.get_transactions():
                miner.add_transaction(tx)
        self.miner = miner
    def add_miner(self):
        miner_id = str(uuid.uuid4())

        # make sure miner_id is unique, keep generating new uuid until so
        while miner_id in self.miners:
            miner_id = str(uuid.uuid4())

        new_miner = Miner(id=miner_id)
        self.miners[miner_id] = new_miner
        return new_miner
Example #22
0
    def evaluate(self, batch_size: int = 64):

        sentences = []
        predicted_labels = []
        answer_labels = []
        test_iterator = self.dataset.get_batch(batch_size, 'test')
        self.model.eval()
        self.dataset.char_encoder.eval()
        with torch.no_grad():
            for data in tqdm(test_iterator):
                mask = data.label0 != 0
                mask = mask.float().to(self.device)
                vecs = self.dataset.to_vectors(data.word,
                                               data.char,
                                               data.pos,
                                               data.subpos,
                                               device=self.device_str)
                sentences.extend([
                    self.dataset.wordid_to_sentence(sentence)
                    for sentence in data.word
                ])
                input_embed = self.model.concat_embedding(list(vecs.values()))
                predicted_labels.extend(self.model.predict(input_embed, mask))
                answer_label = self.dataset.get_batch_true_label(
                    data, 0, self.device_str)
                answer_label = answer_label.numpy()
                mask = mask.cpu().numpy() > 0
                answer_label = [
                    answer_label[i, mask[i]].tolist()
                    for i in range(len(answer_label))
                ]
                answer_labels.extend(answer_label)
        answer_labels = [
            self.dataset.labelid_to_labels(answer) for answer in answer_labels
        ]
        predicted_labels = [
            self.dataset.labelid_to_labels(pred) for pred in predicted_labels
        ]
        self.miner = Miner(answer_labels, predicted_labels, sentences)
        self.miner.default_report(True)
        self.model.train()
        self.dataset.char_encoder.train()
Example #23
0
    def new_miner(self):
        "new miner is initialized if HEAD is updated"
        # prepare uncles
        uncles = set(self.get_uncles(self.head))
#        logger.debug('%d uncles for next block %r', len(uncles), uncles)
        ineligible = set() # hashes
        blk = self.head
        for i in range(8):
            for u in blk.uncles: # assuming uncle headres
                u = utils.sha3(rlp.encode(u))
                if u in self:
#                    logger.debug('ineligible uncle %r', u.encode('hex'))
                    uncles.discard(self.get(u))
            if blk.has_parent():
                blk = blk.get_parent()
#        logger.debug('%d uncles after filtering %r', len(uncles), uncles)

        miner = Miner(self.head, uncles, self.config.get('wallet', 'coinbase'))
        if self.miner:
            for tx in self.miner.get_transactions():
                miner.add_transaction(tx)
        self.miner = miner
Example #24
0
    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()

        self.sounds=Sounds()


        self.input_handler = InputHandler()
        self.game_screen = GameScreen(self.TITLE,self.WIDTH,self.HEIGHT)
        self.miner=Miner(8,0)
        self.explosion=Explosion(-1,-1)
        self.game_screen.add_sprite(self.miner)
        self.game_screen.add_sprite(self.explosion)
        self.health = 100
         
        self.clock=pygame.time.Clock()
        self.state=self.WAITING
        self.game_screen.display_opening()
Example #25
0
class Game(object):
    TITLE = "Gold Rush!"
    BOARD_LEFT = 20
    BOARD_TOP = 130
    SQUARE_SIZE = 32
    BLACK = (0,0,0)
    GREEN=(128,255,128)
    YELLOW=(255,255,128)
    RED=(255,128,128)
    FRAMES_PER_SECOND = 30

    ASSAY_X = 540
    ASSAY_Y = 84
    CHARGES_X = 180 
    CASH_X = 20
    CASH_OFFSET = 30 
    GOLD_X = 16
    CHARGES_OFFSET = 32
    HEALTH_X =CHARGES_X + 40
    TITLE_X = 340

    def display_gold(self):
        scoretext='%03d' % self.gold
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),20),(num,0,24,35))

    def display_charges(self):
        scoretext='%02d' % self.charges
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CHARGES_X+self.CHARGES_OFFSET+(i*24),20),(num,0,24,35))

    def display_cash(self):
        scoretext='%05d' % self.cash
        for i in range(len(scoretext)):
            num=int(scoretext[i])*24
            pos=i*24
            self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),66),(num,0,24,35))

    def display_health(self):
         h=int(84*(self.health/100.0))
         b=84-h
         c=self.GREEN
         if self.health<20:
             c=self.RED
         elif self.health<40:
             c=self.YELLOW
         self.screen.fill(c,(self.HEALTH_X,70,h,32))
         self.screen.fill(self.BLACK,(self.HEALTH_X+h,70,b,32))
#        num=int(scoretext[i])*24
#        pos=i*24
#        self.screen.blit(self.digits,(self.CASH_X+self.CASH_OFFSET+(i*24),66),(num,0,24,35))


    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()
        self.screen=pygame.display.set_mode((680,600))
        pygame.display.set_caption(self.TITLE)
        self.pressedkey=None
        self.bellsound=pygame.mixer.Sound('assets/sounds/bell.ogg')
        self.chargesound=pygame.mixer.Sound('assets/sounds/bomb.ogg')
        self.yeehawsound=pygame.mixer.Sound('assets/sounds/yeehaw.ogg')
        self.kachingsound=pygame.mixer.Sound('assets/sounds/kaching.ogg')
        self.board=[]
        self.bgbase=pygame.image.load('assets/images/background.png')
        self.bg=pygame.image.load('assets/images/background.png') 
        self.digits=pygame.image.load('assets/images/digits.png') 
        self.gamearea=pygame.Surface(self.bg.get_size())
        self.is_playing=False
 
# currently 2 nugget images
        self.nuggets=[]
        self.nuggets.append(pygame.image.load('assets/images/gold01-%dpx.png' % self.SQUARE_SIZE))
        self.nuggets.append(pygame.image.load('assets/images/gold02-%dpx.png' % self.SQUARE_SIZE))

        self.explosion=Explosion(0,0,self.SQUARE_SIZE)
        self.explosion_group=pygame.sprite.RenderPlain(self.explosion)

        self.miner=Miner(0,0)

        self.clock=pygame.time.Clock()

# add title
        text=pygame.image.load('assets/images/text_title.png')
        self.screen.blit(text,(self.TITLE_X,self.BOARD_LEFT))

# add assay office
        self.office=pygame.image.load('assets/images/assayoffice.png')
        self.screen.blit(self.office,(self.ASSAY_X+self.BOARD_LEFT,self.ASSAY_Y))

        self.cash=0
        self.gold=0
        self.charges=10
        self.health=100

# add "Gold"
        text=pygame.image.load('assets/images/nugget.png')
        self.screen.blit(text,(self.GOLD_X,self.BOARD_LEFT))
        self.display_gold()

# add "Cash"
        text=pygame.image.load('assets/images/text_cash.png')
        self.screen.blit(text,(self.CASH_X,66))
        self.display_cash()

# add "Charges"
        text=pygame.image.load('assets/images/dynamite.png')
        self.screen.blit(text,(self.CHARGES_X,16))
        self.display_charges()

# add "Miner head"
        text=pygame.image.load('assets/images/miner_head.png')
        self.screen.blit(text,(self.CHARGES_X,66))
        self.display_health()


        self.setup()



    def setup(self):

# initialize score items
        self.cash=0
        self.gold=0
        self.charges=10

# load background image every time
        self.bg=pygame.image.load('assets/images/background.png')

#redraw assay office        
        self.bg.blit(self.office,(self.ASSAY_X,self.ASSAY_Y-self.BOARD_TOP))

        self.board=[]
# top row of empty spaces
        pathsup=2
        self.board.append([' ']*20)
        self.board.append(['*']*20)
        for y in range(2,14):
            row=[]
            for x in range(20):
                c='*'
                if random()<0.4:
# make a hole
                    self.bg.fill(self.BLACK,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    c=' '
                    if y>1:
                        c='G'
                        nugg=self.nuggets[0 if random()<0.5 else 1] 
                        self.bg.blit(nugg,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE))
                row.append(c)
            self.board.append(row)



# add soil
        self.gamearea.blit(self.bg,(0,0))
        pygame.display.flip()
      

 
    def print_board(self):
        for row in self.board:
            print ' '.join(row)




    def mainloop(self):
        deltat=self.clock.tick(self.FRAMES_PER_SECOND)
        tx=self.miner.x
        ty=self.miner.y
        self.miner_group.clear(self.gamearea,self.bg)
        self.explosion_group.clear(self.gamearea,self.bg)
 
        pressedspace=False
        for event in pygame.event.get():
            #print event
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit(0) 
                elif event.key in (K_RIGHT,K_LEFT,K_UP,K_DOWN):
                    self.pressedkey= event.key
                elif event.key == K_SPACE:
                    pressedspace = True
            elif event.type == KEYUP:
                if event.key in (K_RIGHT,K_LEFT,K_UP,K_DOWN):
                    if self.pressedkey == event.key:
                        self.pressedkey = None
                #elif event.key == K_SPACE:
                    #pressedspace = False

# only draw explosion if necessary
        if self.explosion.update(deltat):
            self.explosion_group.update(deltat)
            self.explosion_group.draw(self.gamearea)

        else:
            if pressedspace and self.pressedkey:
# Do explosion
                pressedspace=False
                bx=self.miner.x
                by=self.miner.y
                if self.pressedkey == K_LEFT:
                    bx-=1
                if self.pressedkey == K_RIGHT:
                    bx+=1
                if self.pressedkey == K_UP:
                    by-=1
                if self.pressedkey == K_DOWN:
                    by+=1

                if bx>=0 and bx<20 and (by>0 or (by==0 and self.pressedkey == K_DOWN)) and by<20 and self.charges>0:
                    self.explosion.explode(bx,by)
                    self.charges-=1
#                    print "(%d,%d)->(%d,%d) Boom! %d charges left." % (self.miner.x,self.miner.y,bx,by,self.charges)
                    self.board[by][bx]=' '
                    self.bg.fill(self.BLACK,(bx*self.SQUARE_SIZE,by*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    self.gamearea.blit(self.bg,(0,0))
                    self.display_charges()
                    #self.screen.blit(self.digits,(460+(i*24),20),(num,0,24,35))
                    self.chargesound.play()
                    for j in range(20):
                        x=randint(0,19)
                        y=randint(2,11)
                        o=self.board[y][x]
                        a=self.board[y-1][x]
                        if o==' ' and a=='*':
                            self.board[y][x]='*'
                            xpos=x*self.SQUARE_SIZE
                            ypos=y*self.SQUARE_SIZE
                            self.bg.blit(self.bgbase,(x*self.SQUARE_SIZE,y*self.SQUARE_SIZE),(xpos,ypos,self.SQUARE_SIZE,self.SQUARE_SIZE))
 
            if self.pressedkey == K_RIGHT and self.miner.can_move():
                if tx<19:
                    tx += 1
 
            if self.pressedkey == K_LEFT and self.miner.can_move(): 
                if tx>0:
                    tx -= 1
            if self.pressedkey == K_UP and self.miner.can_move():
                if ty>0:
                    ty -= 1
                else:
                    if tx==17:
                        if self.gold!=0:
                            self.cash+=self.gold*self.charges
                            self.gold=0
                            self.kachingsound.play()
                            self.display_gold()
                            self.display_cash()
                            self.yeehawsound.play()
                            
            if self.pressedkey == K_DOWN and self.miner.can_move():
                if ty<13:
                    ty += 1

            
            o=self.board[ty][tx]

            if (tx!=self.miner.x or ty!=self.miner.y) and o in ' G':
                self.miner.set_location(tx,ty)
                if o=='G':
                    self.board[ty][tx]=' '
                    self.gold += 1
                    self.bellsound.play()
                    self.bg.fill(self.BLACK,(self.miner.x*self.SQUARE_SIZE,self.miner.y*self.SQUARE_SIZE,self.SQUARE_SIZE,self.SQUARE_SIZE))
                    self.gamearea.blit(self.bg,(0,0))
                self.display_gold()

            self.miner.update_move()
            self.miner_group.update(deltat)
            self.miner_group.draw(self.gamearea)
        if self.miner.y>0:
            self.health-=0.25
            if self.health<0:
                self.health=0
                pass
            self.display_health()
        else:
            self.health+=1
            if self.health>100:
                self.health=100
            self.display_health()
  
        self.screen.blit(self.gamearea,(self.BOARD_LEFT,self.BOARD_TOP))

        pygame.display.flip()
Example #26
0
#!/usr/bin/env python
# encoding: utf-8

from miner import Miner, Wife
from entity import EntityManager

em = EntityManager()

m = Miner('Bob')
w = Wife('Elsa')

em.register_entity(m)
em.register_entity(w)

for i in xrange(200):
    m.update()
    w.update()

Example #27
0
## - Comp 4710 - Data Mining
## - Prof: Dr. Carson K. Leung
## - Authors: Trevor Blanchard, Stafan Harris, Brett Small, Sam Peers
## - Sentiment Miner
## - December 10, 2015

from miner import Miner
import utils
from timer import Timer

myTimer = Timer()
myMiner = Miner()

myTimer.start_timer()

print "\nMining for negative patterns\n"

data = utils.load_json("Train/neg_pos.txt")

for index, full_text  in data.iteritems():
	myMiner.mine_text(full_text)

utils.dump_json(myMiner.found,"Train/negative_POS_dict.json")

print "\nDone mining for negative patterns\n"

myMiner.reset()

print "\nMining for positive patterns\n"

data = utils.load_json("Train/pos_pos.txt")
Example #28
0
class Game(object):
    TITLE = "Gold Rush!"
    WIDTH=680
    HEIGHT=600
    WAITING = 1
    PLAYING = 2
    FRAMES_PER_SECOND = 30

    board=[]

    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()

        self.sounds=Sounds()


        self.input_handler = InputHandler()
        self.game_screen = GameScreen(self.TITLE,self.WIDTH,self.HEIGHT)
        self.miner=Miner(8,0)
        self.explosion=Explosion(-1,-1)
        self.game_screen.add_sprite(self.miner)
        self.game_screen.add_sprite(self.explosion)
        self.health = 100
         
        self.clock=pygame.time.Clock()
        self.state=self.WAITING
        self.game_screen.display_opening()

 
    def setup(self):
        self.gold=0
        self.charges=10
        self.cash=0
        self.health=100 
        self.board=[]
       
# top row of empty spaces
        self.board.append([' ']*20)
        self.board.append(['*']*20)
        for y in range(2,14):
            row=[]
            for x in range(20):
                c='*'
                if y>1 and random()<0.4:
                    c=' '
                    if random()<0.5:
                        c='0'
                    else:
                        c='1'
                row.append(c)
            self.board.append(row)
        self.miner.set_location(8,0)
        self.game_screen.setup()
        self.game_screen.set_board(self.board)
        self.game_screen.draw_board()



    def mainloop(self):
        deltat=self.clock.tick(self.FRAMES_PER_SECOND)
        running=True
        while running:
            self.input_handler.check()
            if self.input_handler.exit_action:
                running=False
            elif self.state == self.WAITING:
                if self.input_handler.key_press:
                    self.setup()
                    self.state=self.PLAYING
            else:
                self.game_screen.clear_sprites()
                if self.miner.can_move():
                    kpress=self.input_handler.arrow_press

                    if kpress:
                        dx=0
                        dy=0

                        if kpress == K_RIGHT:
                            dx=1
                        elif kpress == K_LEFT:
                            dx=-1
                        elif kpress == K_UP:
                            dy=-1
                        elif kpress == K_DOWN:
                            dy=1

                        if self.input_handler.space_press and (dx!=0 or dy!=0):
                            self.do_explosion(dx,dy)

                        tx=self.miner.x + dx
                        ty=self.miner.y + dy

                        if (dx!=0 or dy!=0) and (tx>=0 and tx<=19 and ty>=0 and ty<=13):
                            o=self.board[ty][tx]
                            if o in ' 01':
                                self.miner.set_location(tx,ty)
                                if o in '01':
                                    self.take_nugget(tx,ty)

                        elif (dy==-1 and tx==17 and ty==-1 and self.gold!=0):
                            self.cash_out()
                            if self.charges==0:
                                self.state=self.WAITING
                                self.input_handler.reset()
                                self.game_screen.display_gameover()



                    if self.miner.y>0:
                        self.health-=1
                        if self.health<0:
                            self.health=0
                        self.game_screen.display_health(self.health)
                    else:
                        self.health+=1
                        if self.health>100:
                            self.health=100
                        self.game_screen.display_health(self.health)
                

            self.game_screen.draw(deltat)
 
        pygame.quit()


    def do_explosion(self,dx,dy):
        bx=self.miner.x + dx
        by=self.miner.y + dy
        if bx>=0 and bx<20 and (by>0 or (by==0 and dy==1)) and by<14 and self.charges>0:

            self.explosion.explode(bx,by)

            self.charges-=1
            self.board[by][bx]=' '
            self.miner.add_delay(20)

            self.game_screen.clear_square(bx,by)
            self.game_screen.display_charges()
            self.sounds.play_boom()
            self.game_screen.display_charges(self.charges)

            for j in range(20):
                x=randint(0,19)
                y=randint(2,11)
                o=self.board[y][x]
                a=self.board[y-1][x]
                if o==' ' and a=='*':
                    self.board[y][x]='*'
                    self.game_screen.reset_square(x,y)

    def cash_out(self):
        self.cash+=self.gold*self.charges
        self.gold=0
        self.sounds.play_kaching()
        self.game_screen.display_gold(self.gold)
        self.game_screen.display_cash(self.cash)
        self.sounds.play_yeehaw()


    def take_nugget(self,tx,ty):
        self.board[ty][tx]=' '
        self.gold += 1
        self.sounds.play_bell()
        self.game_screen.clear_square(tx,ty)
        self.game_screen.display_gold(self.gold)
Example #29
0
    def __init__(self):
        pygame.mixer.pre_init(44100,-16,2,2048)
        pygame.init()
        self.screen=pygame.display.set_mode((680,600))
        pygame.display.set_caption(self.TITLE)
        self.pressedkey=None
        self.bellsound=pygame.mixer.Sound('assets/sounds/bell.ogg')
        self.chargesound=pygame.mixer.Sound('assets/sounds/bomb.ogg')
        self.yeehawsound=pygame.mixer.Sound('assets/sounds/yeehaw.ogg')
        self.kachingsound=pygame.mixer.Sound('assets/sounds/kaching.ogg')
        self.board=[]
        self.bgbase=pygame.image.load('assets/images/background.png')
        self.bg=pygame.image.load('assets/images/background.png') 
        self.digits=pygame.image.load('assets/images/digits.png') 
        self.gamearea=pygame.Surface(self.bg.get_size())
        self.is_playing=False
 
# currently 2 nugget images
        self.nuggets=[]
        self.nuggets.append(pygame.image.load('assets/images/gold01-%dpx.png' % self.SQUARE_SIZE))
        self.nuggets.append(pygame.image.load('assets/images/gold02-%dpx.png' % self.SQUARE_SIZE))

        self.explosion=Explosion(0,0,self.SQUARE_SIZE)
        self.explosion_group=pygame.sprite.RenderPlain(self.explosion)

        self.miner=Miner(0,0)

        self.clock=pygame.time.Clock()

# add title
        text=pygame.image.load('assets/images/text_title.png')
        self.screen.blit(text,(self.TITLE_X,self.BOARD_LEFT))

# add assay office
        self.office=pygame.image.load('assets/images/assayoffice.png')
        self.screen.blit(self.office,(self.ASSAY_X+self.BOARD_LEFT,self.ASSAY_Y))

        self.cash=0
        self.gold=0
        self.charges=10
        self.health=100

# add "Gold"
        text=pygame.image.load('assets/images/nugget.png')
        self.screen.blit(text,(self.GOLD_X,self.BOARD_LEFT))
        self.display_gold()

# add "Cash"
        text=pygame.image.load('assets/images/text_cash.png')
        self.screen.blit(text,(self.CASH_X,66))
        self.display_cash()

# add "Charges"
        text=pygame.image.load('assets/images/dynamite.png')
        self.screen.blit(text,(self.CHARGES_X,16))
        self.display_charges()

# add "Miner head"
        text=pygame.image.load('assets/images/miner_head.png')
        self.screen.blit(text,(self.CHARGES_X,66))
        self.display_health()


        self.setup()
Example #30
0
 def minePdf(self, decode=True):
     self.miner = Miner(self.content)
     text = self.miner.extract_text()
     self.text = ' '.join(text.split())
     if decode is True:
         self.text = self.text.decode('utf8').encode('ascii', 'ignore')
Example #31
0
class TextExtractor(object):

    def __init__(self, url, fileFormat=None):
        self.url = url
        self.fileFormat = fileFormat

    def getResponse(self):
        self.response = urllib2.urlopen(self.url)
        self.content = self.response.read()
        return self

    def openDocxResponse(self):
        fp = StringIO(self.content)
        zfp = zipfile.ZipFile(fp, 'r')
        xmlContent = zfp.read('word/document.xml')
        self.document = etree.fromstring(xmlContent)
        return self

    def openPdfResponse(self):
        self.fp = StringIO(self.content)
        self.document = PdfFileReader(self.fp)
        return self

    def minePdf(self, decode=True):
        self.miner = Miner(self.content)
        text = self.miner.extract_text()
        self.text = ' '.join(text.split())
        if decode is True:
            self.text = self.text.decode('utf8').encode('ascii', 'ignore')

    def getDocxText(self):
        '''Return the raw text of a document as a list of paragraphs.'''
        paratextlist = []

        # Compile a list of all paragraph (p) elements
        paralist = [e for e in self.document.iter()
                    if e.tag == '{' + nsprefixes['w'] + '}p']

        # Since a single sentence might be spread over multiple text elements,
        # iterate through each paragraph, appending all text (t) children to that
        # paragraphs text.
        for para in paralist:
            paratext = u''
            # Loop through each paragraph
            for element in para.iter():
                # Find t (text) elements
                if element.tag == '{' + nsprefixes['w'] + '}t' and element.text:
                    paratext = paratext + element.text
                elif element.tag == '{' + nsprefixes['w'] + '}tab':
                    paratext = paratext + '\t'
            # Add our completed paragraph text to the list of paragraph text
            if not len(paratext) == 0:
                paratextlist.append(paratext)

        self.text = ' '.join(paratextlist)
        return self

    def getPdfText(self):
        '''Return the raw text of a pdf file'''
        pdf = self.document.pages
        self.text = ' '.join([p.extractText() for p in pdf])
        return self

    def getText(self):
        '''Conditional wrapper for getDocxText and getPdfText'''
        if self.fileFormat == 'pdf':
            self.getPdfText()
        elif self.fileFormat == 'docx':
            self.getDocxText()
        else:
            self.text = ''