def test0_read(self):
        self.write(self.string1, self.tempf)
        _cfg = Configuration()
        self.assertRaises(ValueError, _cfg.read, self.tempf)

        self.write(self.string2, self.tempf)
        _cfg.clear()
        self.assertEqual(None, _cfg.read(self.tempf))
Exemple #2
0
def get_block(block_hash):
    """
    Gets a block
    :param block_hash: the block hash
    :return: block
    """
    if block_hash == '':
        conf = Configuration()
        block_hash = conf.get_conf("last_block")
    return read_block(block_hash)
Exemple #3
0
def get_block(block_hash):
    """
    Gets a block
    :param block_hash: the block hash
    :return: block
    """
    if block_hash == '':
        conf = Configuration()
        block_hash = conf.get_conf("last_block")
    return read_block(block_hash)
Exemple #4
0
 def setUpClass(self):
     print("---------------------------------------Tests---------------------------------------\nProduct Name: Visa Direct\nApi Name: Watch List Screening API")
     globalConfig = GlobalConfig()
     config = Configuration()
     config.username = globalConfig.userName
     config.password = globalConfig.password
     config.cert_file = globalConfig.certificatePath
     config.key_file = globalConfig.privateKeyPath
     config.shared_secret = globalConfig.sharedSecret
     config.api_key['apikey'] = globalConfig.apiKey
     config.ssl_ca_cert = globalConfig.caCertPath
     config.proxy_url = globalConfig.proxyUrl
     self.api = WsApi(None)
    def test2_get_config_simulation(self):
        _cfg = Configuration()
        _cfg.read(self.tempf)
        self.assertRaises(ValueError, _cfg.get_config, "simulation")

        self.string2 = re.sub(r"= mode", r"= simulation", self.string2)
        self.write(self.string2, self.tempf)
        _cfg.clear()
        _cfg.read(self.tempf)
        self.assertTrue("path" in _cfg.get_config("simulation"))
 def setUpClass(self):
     print(
         "---------------------------------------Tests---------------------------------------\nProduct Name: Payment Account Validation\nApi Name: Payment Account Validation API"
     )
     globalConfig = GlobalConfig()
     config = Configuration()
     config.username = globalConfig.userName
     config.password = globalConfig.password
     config.cert_file = globalConfig.certificatePath
     config.key_file = globalConfig.privateKeyPath
     config.shared_secret = globalConfig.sharedSecret
     config.api_key['apikey'] = globalConfig.apiKey
     config.ssl_ca_cert = globalConfig.caCertPath
     config.proxy_url = globalConfig.proxyUrl
     self.api = PavApi(None)
Exemple #7
0
def post(res, body):
    h = urllib.request.Request(
        Configuration()['proxy-cli.base-addr'] + res,
        data = json.dumps(str(body)).encode(),
        headers={'Content-Type': 'application/json'}
    )
    return urllib.request.urlopen(h).read()
 def setUp(self):
     self.stdout = sys.stdout
     self.obj = Configuration()
     self.tempf = "./temp.test_cfg"
     self.string1 = ("[foo]\n"
                     "foo = bar\n")
     self.string2 = self.gen_configs()
     sys.stdout = cStringIO.StringIO()
Exemple #9
0
def create_transaction(recipient, amount):
    """
    creates a new transaction and add it to verified_transactions.json
    :param recipient: public address of the recipient
    :param amount: the amount of abc to be sent
    :return: None
    """
    # TODO: Send a success message to client
    conf = Configuration()
    try:
        tx = Transaction()
        tx.add_output(recipient, amount)
        tx.unlock_inputs(get_private_key(), get_public_key("string"))
        save_verified_transaction(tx.get_transaction_id(), tx.get_data())
        conf.subtract_balance(tx.sum_of_outputs())
    except ValueError as e:
        # Will raise if insufficient utxos are found
        raise ValueError("INSUFFICIENT FUNDS")
Exemple #10
0
def create_transaction(recipient, amount):
    """
    creates a new transaction and add it to verified_transactions.json
    :param recipient: public address of the recipient
    :param amount: the amount of abc to be sent
    :return: None
    """
    # TODO: Send a success message to client
    conf = Configuration()
    try:
        tx = Transaction()
        tx.add_output(recipient, amount)
        tx.unlock_inputs(get_private_key(), get_public_key("string"))
        save_verified_transaction(tx.get_transaction_id(), tx.get_data())
        conf.subtract_balance(tx.sum_of_outputs())
    except ValueError as e:
        # Will raise if insufficient utxos are found
        raise ValueError("INSUFFICIENT FUNDS")
Exemple #11
0
def mine():
    # config object
    conf = Configuration()
    # Mines block
    reward_address = conf.get_conf("wallet").get("address")
    reward_amount = conf.get_conf("reward")
    size = 0  # TODO: fix this
    # 2) bundle transactions
    tnx = bundle_tnx(size, reward_amount)

    b = Block(previous_hash=conf.get_conf("last_block"), transactions=tnx)
    b.set_target(conf.get_conf("difficulty"))
    b.mine()
    save_block(b)
    conf.increment_height()
    conf.update_previous_hash(b.block_hash())

    find_incoming_utxos(b.block_hash(), b.transactions)
Exemple #12
0
    def read_all_genotypes_per_generation(config_dir: str, results_dir: str,
                                          experiment_name: str):
        import os, pickle

        print(f"Loading {experiment_name}")
        config = Configuration.from_json(os.path.join(
            config_dir, f"{experiment_name}.json"),
                                         validation=False)
        experiment_results = os.path.join(results_dir, experiment_name)
        # Read all genotypes first
        population = []
        patterns = []

        files = find_filetype_recursivly(experiment_results, filetype=".obj")
        for file_path in files:
            with open(file_path, "rb") as ptr:
                obj = pickle.load(ptr)
                if not isinstance(obj, Pattern):
                    population += [obj]
                else:
                    patterns += [obj]
                print(
                    f" - Reading files, {(len(population) + len(patterns)) / len(files) * 100:.0f}% complete"
                    + "\t" * 3,
                    end="\r",
                )
        print()
        # Map genotype to generation
        generations = []
        pattern_generations = []
        directory = os.path.join(experiment_results, "generations")
        gens = [
            int(generation) for generation in os.listdir(directory)
            if isint(generation)
        ]
        gens.sort()
        for gen in gens:
            gen_dir = os.path.join(directory, str(gen))
            generation = []
            p_generation = []
            for identifier in os.listdir(gen_dir):
                identifier = identifier.strip("'")
                for ind in population:
                    if ind.ID == identifier:
                        generation += [ind]
                        break
                else:
                    for ind in patterns:
                        if ind.ID == identifier:
                            p_generation += [ind]
                            break
            generations += [generation]
            pattern_generations += [p_generation]
        print(" - population sorted into generations")
        return Simulation(config, population, generations, patterns,
                          pattern_generations)
Exemple #13
0
def find_incoming_utxos(block_hash, transactions, isGenesis=False):
    """
    Iterates through all the outputs and looks for any directed to user's wallet.
    If found, save to the utxo pool
    :return:
    """
    myAddress = SHA256.new(get_public_key("string").encode()).hexdigest()
    conf = Configuration()
    for tnx_id, tnx_info in transactions.items():
        # deserialize transaction
        tnx_payload = tnx_info
        tnx_payload["transaction_id"] = tnx_id
        tnx = Transaction(payload=tnx_payload)

        for index in range(len(tnx.outputs)):
            if tnx.outputs[index]["address"] == myAddress and not isGenesis:
                save_utxo(tnx.get_transaction_id(), index, block_hash, tnx.outputs[index]["amount"])
                conf.add_balance(tnx.outputs[index]["amount"])
            elif tnx.outputs[index]["address"] == myAddress and isGenesis:
                save_utxo(tnx.get_transaction_id(), -1, block_hash, tnx.outputs[index]["amount"])
                conf.add_balance(tnx.outputs[index]["amount"])
Exemple #14
0
def mine():
    # config object
    conf = Configuration()
    # Mines block
    reward_address = conf.get_conf("wallet").get("address")
    reward_amount = conf.get_conf("reward")
    size = 0 # TODO: fix this
    # 2) bundle transactions
    tnx = bundle_tnx(size, reward_amount)

    b = Block(previous_hash=conf.get_conf("last_block"), transactions=tnx)
    b.set_target(conf.get_conf("difficulty"))
    b.mine()
    save_block(b)
    conf.increment_height()
    conf.update_previous_hash(b.block_hash())

    find_incoming_utxos(b.block_hash(), b.transactions)
Exemple #15
0
def try_finish(population: [Module], config: Configuration, moo_ops: callable) -> [Module]:
    print(f"--> Possible final solution discovered. Checking...")

    # Changing settings of training steps:
    original_training_settings = copy.deepcopy(config.training)
    config.training.use_restart = False
    config.training.fixed_epochs = True
    config.training.epochs = 100

    # Finding the best networks:
    best = population[:config.compute_capacity(maximum=False)]

    # Performing training step:
    best = workers.start(best, config)

    # Reset settings and return:
    config.training = original_training_settings

    best.sort(key=weighted_overfit_score(config), reverse=True)
    if any(ind.test_acc() >= config.training.acceptable_scores for ind in best):
        generation_finished(best, config, "--> Found final solution:")
        config.results.store_generation(best, config.generation + 1)
        return best, True
    else:
        # A final solution was not found... Keep the best individs:
        population = best + population
        population = nsga_ii(
            population,
            moo_ops.classification_objectives(config),
            moo_ops.classification_domination_operator(
                moo_ops.classification_objectives(config)
            ),
            config
        )
        keep = len(population) - config.population_size
        population, removed = population[keep:], population[:keep]
        generation_finished(population, config, "--> Leaderboards after final solution try failed:")
        generation_finished(removed, config, "--> Removed after final solution try failed:")
        return population, False
    def test_normal(self, os_mock, date_mock):
        os_mock.side_effect = self.os_environ

        date_mock.utcnow.return_value = datetime(2018, 4, 21)

        conf = Configuration()

        eq_(conf.url, "url")
        eq_(conf.icon, "icon")
        eq_(conf.channel, "channel")
        eq_(conf.bot_name, "name")
        eq_(conf.threshold_good, 1)
        eq_(conf.threshold_warn, 2)
        eq_(conf.called_date, datetime(2018, 4, 21))
    def test_normal_default(self, os_mock, date_mock):
        os_mock.side_effect = self.os_environ_url_only

        date_mock.utcnow.return_value = datetime(2018, 4, 21)

        conf = Configuration()

        eq_(conf.url, "url")
        eq_(conf.icon, ":moneybag:")
        eq_(conf.channel, None)
        eq_(conf.bot_name, "aws cost report")
        eq_(conf.threshold_good, 5.0)
        eq_(conf.threshold_warn, 15.0)
        eq_(conf.called_date, datetime(2018, 4, 21))
Exemple #18
0
    def setUpClass(cls):
        # Create a network:
        cls.patterns = initialize_patterns(count=5)
        nets = recombine.combine(cls.patterns,
                                 num_nets=1,
                                 min_size=3,
                                 max_size=5)

        # Parameters:
        cls.epochs = 1
        server_id = 0
        dev_id = 0
        job_id = 0

        # Setting necessary properties:
        cls.net = nets[0]
        cls.config = Configuration.from_json("./tests/fixtures/config.json")
        cls.config.type = "PatternNets"
        cls.training_args = (pickle.dumps(cls.net), pickle.dumps(cls.config),
                             cls.epochs, server_id, dev_id, job_id)
Exemple #19
0
 def setUpClass(self):
     print(
         "---------------------------------------Tests---------------------------------------\nProduct Name: Merchant Measurement\nApi Name: Merchant Benchmark API"
     )
     globalConfig = GlobalConfig()
     config = Configuration()
     config.username = globalConfig.userName
     config.password = globalConfig.password
     config.cert_file = globalConfig.certificatePath
     config.key_file = globalConfig.privateKeyPath
     # config.shared_secret = globalConfig.sharedSecret
     # config.api_key['apikey'] = globalConfig.apiKey
     # config.ssl_ca_cert = globalConfig.caCertPath
     # config.proxy_url = globalConfig.proxyUrl
     self.api = MerchantBenchmarkApi(None)
Exemple #20
0
def main():

    print("""*********************************************
Starting the Apartment Hunter
*********************************************""")

    config = Configuration()
    craigsLister = Craigslist(config.minPrice, config.maxPrice,
                              config.bedrooms, config.minSqft, config.maxSqft)

    apartments = []

    # For the list of neighbourhoods, append the list of apartments to the list
    for neighbourhood in config.neighbourhoods:
        apartments.extend(
            craigsLister.getApartments(neighbourhood, config.pages))

    # Make the list unique
    temp = set(apartments)

    uniqueApartments = list(temp)

    # Sort the apartments based on ranking
    uniqueApartments.sort(key=lambda x: x.ranking, reverse=True)

    print(len(apartments), " apartments found in total.")
    print(len(uniqueApartments), " unique apartments found in total.")

    messenger = Messenger(config.email, config.password)

    # Send the #1 ranked apartment as a text
    messenger.sendTextMessageTMobile(config.phoneNumber,
                                     str(uniqueApartments[0]))

    # Send the top 10 in email
    messenger.sendEmailMessage(
        ('<br/><br/>'.join(apt.formatForEmail()
                           for apt in uniqueApartments[:10])))
Exemple #21
0
def init_configuration():
    """
    This will instantiate the config class
    and check if genesis block is created
    :return: configuration object
    """
    conf = Configuration()

    block_height = conf.get_conf("height")
    if block_height == 0:
        b = genesis_block()
        cwd = os.getcwd()
        try:
            os.makedirs(os.path.join(cwd, r'data'))
        except OSError as e:
            pass
        save_block(b)
        conf.increment_height()
        conf.update_previous_hash(b.block_hash())
        find_incoming_utxos(b.block_hash(), b.transactions, True)

    return conf
Exemple #22
0
def pooling(t):
    # FIXME: dns is not yet ready.
    global toSubmit
    log(0, "Pooling using: " + PoolingLogs.poolingName(t))
    d = None
    if len(toSubmit) > 0:
        d = toSubmit.pop()
    print("Submitting: " + str(d))
    res = request('/pool/' + Configuration()['node.id'], data=d)
    j = json.loads(res)
    for i in j:
        if 'command' in i:
            try:
                jj = json.loads(i['command'])
                if 'name' in jj and jj['name'] == "screenshot":
                    log(0, "Taking screenshot...")
                    d = {"cmd": i['id'], "data": take_screenshot()}
                    toSubmit.append(d)
                    print("adding to the list...")
            except Exception as e:
                print(str(e))
                pass
        else:
            print(str(i))
Exemple #23
0
def init_configuration():
    """
    This will instantiate the config class
    and check if genesis block is created
    :return: configuration object
    """
    conf = Configuration()

    block_height = conf.get_conf("height")
    if block_height == 0:
        b = genesis_block()
        cwd = os.getcwd()
        try:
            os.makedirs(os.path.join(cwd, r'data'))
        except OSError as e:
            pass
        save_block(b)
        conf.increment_height()
        conf.update_previous_hash(b.block_hash())
        find_incoming_utxos(b.block_hash(), b.transactions, True)

    return conf
Exemple #24
0
def find_incoming_utxos(block_hash, transactions, isGenesis=False):
    """
    Iterates through all the outputs and looks for any directed to user's wallet.
    If found, save to the utxo pool
    :return:
    """
    myAddress = SHA256.new(get_public_key("string").encode()).hexdigest()
    conf = Configuration()
    for tnx_id, tnx_info in transactions.items():
        # deserialize transaction
        tnx_payload = tnx_info
        tnx_payload["transaction_id"] = tnx_id
        tnx = Transaction(payload=tnx_payload)

        for index in range(len(tnx.outputs)):
            if tnx.outputs[index]["address"] == myAddress and not isGenesis:
                save_utxo(tnx.get_transaction_id(), index, block_hash,
                          tnx.outputs[index]["amount"])
                conf.add_balance(tnx.outputs[index]["amount"])
            elif tnx.outputs[index]["address"] == myAddress and isGenesis:
                save_utxo(tnx.get_transaction_id(), -1, block_hash,
                          tnx.outputs[index]["amount"])
                conf.add_balance(tnx.outputs[index]["amount"])
Exemple #25
0
#!/usr/bin/env python

from flask import Flask, redirect, request
app = Flask(__name__)
import sys, getopt, json
from playhouse.shortcuts import model_to_dict, dict_to_model
from datetime import date, datetime

from src.configuration import loadProxyConfig, Configuration
loadProxyConfig()
conf = Configuration()
from src.logger import log
from src.database import *
from src.utils import json_serial


@app.before_request
def before_request():
    Database().connect()


@app.after_request
def after_request(response):
    Database().close()
    return response


@app.route('/pool/<node_id>', methods = ['POST', 'GET'])
def pool(node_id):
    """
    """
Exemple #26
0
def give_configuration_object(layout_number, block_number):
    layout_in_use = 'layout_' + str(layout_number)
    exec('from src.layout import ' + layout_in_use)
    environment_config = eval(layout_in_use + '[' + str(block_number) + ']')
    ed = Configuration(env_config=environment_config)
    return ed
Exemple #27
0
def main(config: Configuration):
    # 0.1 How many nets can be trained for each generation?
    solved = False
    compute_capacity = config.compute_capacity()

    # 0.2 Initializing multi objective optimisation sorting:
    moo_objectives = moo.classification_objectives(config)
    domination_operator = moo.classification_domination_operator(
        moo.classification_objectives(config))

    patterns, nets = initialize_population(config, compute_capacity)

    i = 0
    while not solved:

        # 3. Evolve for <x> generations:
        for generation in range(config.generations * i,
                                config.generations * (i + 1)):
            config.generation = generation

            # 3.1 Select some patterns for mutation. Tournament
            selected = selection.tournament(patterns,
                                            size=int(len(patterns) / 2))

            # 3.2 Perform Mutations + Crossover on selected patterns
            mutations, crossovers = selection.divide(selected)
            patterns = patterns + \
                       mutator.apply(mutations) + \
                       crossover.apply(crossovers)

            # 3.3 Evaluate new patterns. Fitness calculation
            nets = recombine.combine(patterns,
                                     compute_capacity,
                                     config.min_size,
                                     config.max_size,
                                     include_optimal=True)
            nets = workers.start(nets, config)
            patterns = evaluation.inherit_results(patterns, nets)

            # 3.4 Rank all patterns using MOO. Diversity in position, 2D vs 1D, scores ++
            patterns = nsga_ii(patterns, moo_objectives, domination_operator,
                               config)

            # 3.5 Evolution of the fittest. Elitism
            patterns = patterns[-config.population_size:]

            # 3.6 Feedback:
            print(f"--> Generation {generation} Leaderboards")
            generation_finished(patterns, config, "    - Patterns:")
            generation_finished(nets, config, "    - Neural networks:")
            config.results.store_generation(patterns, generation)
            config.results.store_generation(nets, generation)

        # To finish up, the best combination of patterns needs to be returned and trained for
        # much longer than what they are during fitness evaluation. The previous steps should only
        # be used for verifying that the combination of layers is good.
        #
        # This might need to be tried multiple times. When a good result is gotten, the algorithm should
        # stop and return the final structure with trained weights.

        print("Testing best combination of patterns")

        # Changing settings of training steps:
        original_training_settings = copy.deepcopy(config.training)
        config.training.use_restart = False
        config.training.fixed_epochs = True
        config.training.epochs = 300

        # Finding the best combined network:@
        config.type = "ea-nas"
        nets.sort(key=weighted_overfit_score(config), reverse=True)
        config.type = "PatternNets"
        best_net = nets[-1]

        # Performing training step:
        best_net = workers.start([best_net], config)[0]

        # Reset settings and return:
        config.training = original_training_settings

        if best_net.test_acc() >= config.training.acceptable_scores:
            print("Found good network! ")
            solved = True

        config.type = "ea-nas"
        generation_finished([best_net], config, "--> Found final solution:")
        config.type = "PatternNets"
        patterns = evaluation.inherit_results(patterns, nets)
        i += 1
Exemple #28
0
from __future__ import print_function
import time
from src.apis.merchant_benchmark_api import MerchantBenchmarkApi
from src.configuration import Configuration

config = Configuration()
# Uncomment this block to enable proxy
# config.proxy_url = 'PROXY_URL'

# Configure HTTP basic authorization: basicAuth
config.username = '******'
config.password = '******'
config.cert_file = 'ABSOLUTE_PATH_TO_CERT_FILE'
config.key_file = 'ABSOLUTE_PATH_TO_KEY_FILE'
config.ssl_ca_cert = 'ABSOLUTE_PATH_TO_CA_CERT_FILE'

# Unblock this block to configure MLE credentials
# config.api_key['keyId'] = 'YOUR_KEY_ID'
# config.encryption_public_key_path = 'ABSOLUTE_PATH_TO_MLE_CERT_FILE'
# config.decryption_private_key_path = 'ABSOLUTE_PATH_TO_MLE_KEY_FILE'

# create an instance of the API class
api_instance = MerchantBenchmarkApi()

# Set all the required parameters in the postmerchant_benchmark. Look at the documentation for further clarification.
merchant_benchmarkpost_payload = src.MerchantBenchmarkpostPayload(
)  # MerchantBenchmarkpostPayload | Merchant Benchmark request payload

try:
    api_response = api_instance.postmerchant_benchmark(
        merchant_benchmarkpost_payload)
Exemple #29
0
from flask.ext.api import FlaskAPI, status, exceptions
from src.configuration import Configuration
from src.validation.validation_handler import initialize_validators as hook_validators

# Initialize Flask API.
app = FlaskAPI(__name__.split('.')[0], static_url_path='/')

# Get routes.
from src.routes import route

# Acquire controllers.
from src.controllers import (Controllers)

# Get configuration.
config = Configuration()

# Set routes for flask application.
route(app, Controllers, config)

# Set debugging if necessary.
if config.ENVIRONMENT == config.ConfigurationConstants.DEVELOPMENT:
    app.debug = True

# Set JSON schemas
app.config['JSONSCHEMA_DIR'] = os.path.join(app.root_path, 'DTO/schemas')

# Set request level validators.
hook_validators(app)

# Run flask application.
Exemple #30
0
def main(args):
    now = datetime.now()

    for size in args.sizes:
        device = "cpu"
        if torch.cuda.is_available():
            from torch.backends import cudnn

            cudnn.benchmark = True
            device = "cuda"

        assert args.model_class in MODEL_MAP, "Model Class not in {}".format(
            MODEL_MAP.keys())
        model = MODEL_MAP[args.model_class].from_file(
            args.model_path.name,
            device,
            gcn_hidden_dimension=args.gcn_hidden_dimension,
            fc_hidden_dimension=args.fc_hidden_dimension,
        )
        # model = torch.load(args.model_path.name, map_location=device)

        positive_ratio, test_loader = get_graph_test_loader(
            args.dataset_path,
            GraphsDataset,
            args.batch_size,
            args.num_workers,
            dataset_size=size,
            positive_ratio=args.positive_ratio,
            collate_fn=collate,
            pin_memory="cuda" in device if device is not None else False,
        )
        dataset_size = size or len(test_loader.dataset)

        log_dir = os.path.join(
            args.output,
            "testing_{}_{}_{}".format(args.model_name, dataset_size,
                                      now.strftime("%Y%m%d_%H%M")),
        )
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        log_level = logging.INFO
        if args.debug:
            log_level = logging.DEBUG
            print("Activated debug mode")

        logger = logging.getLogger("Spread Classification: Test")
        setup_logger(logger, log_dir, log_level)

        logger.debug("Setup model: {}".format(args.model_name))

        configuration = Configuration.from_dict(
            **{
                **vars(args),
                "optimizer": "N/A",
                "epochs": -1,
                "train_batch_size": -1,
                "val_batch_size": args.batch_size,
                "lr": -1,
                "lr_update_every": -1,
                "weight_decay": -1,
                "gamma": -1,
                "restart_every": -1,
                "restart_factor": -1,
                "init_lr_factor": -1,
                "lr_reduce_patience": -1,
                "lr_reduce_factor": -1,
                "early_stop_patience": -1,
                "positive_ratio": positive_ratio,
                "run_index": -1,
                "log_dir": log_dir,
                "log_level": log_level,
                "log_interval": -1,
                "device": device,
                "dataset_size": dataset_size,
                "data_type": "graph",
                "model_name": args.model_path.name,
            })

        metrics = test_on_loader(model, test_loader, logger, configuration)
        with open(os.path.join(log_dir, "metrics.json"), "w") as file:
            json.dump(metrics, file)
Exemple #31
0
def main(args):
    now = datetime.now()

    for size in args.sizes:
        log_dir = os.path.join(
            args.output,
            "training_{}_{}_{}".format(
                args.model_name, size, now.strftime("%Y%m%d_%H%M")
            ),
        )
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        log_level = logging.INFO
        if args.debug:
            log_level = logging.DEBUG
            print("Activated debug mode")

        logger = logging.getLogger("Spread Classification: Train")
        setup_logger(logger, log_dir, log_level)

        device = "cpu"
        if torch.cuda.is_available():
            logger.debug("CUDA is enabled")
            from torch.backends import cudnn

            cudnn.benchmark = True
            device = "cuda"

        logger.debug("Setup model: {}".format(args.model_name))

        logger.debug("Setup train/val dataloaders")
        positive_ratio, loaders = get_text_loaders(
            args.dataset_path,
            args.train_batch_size,
            args.val_batch_size,
            args.num_workers,
            get_model_cls(args.model_class).MAX_LEN,
            dataset_size=size,
            positive_ratio=args.positive_ratio,
            model_class=args.model_class,
        )

        for run_index, (train_loader, val_loader) in it.islice(
            enumerate(loaders), 0, args.max_runs
        ):
            configuration = Configuration.from_dict(
                **{
                    **vars(args),
                    "positive_ratio": positive_ratio,
                    "run_index": run_index,
                    "log_dir": log_dir,
                    "log_level": log_level,
                    "device": device,
                    "dataset_size": size
                    or (len(train_loader.dataset) + len(val_loader.dataset)),
                    "data_type": "text",
                    "gcn_hidden_dimension": -1,
                    "fc_hidden_dimension": -1,
                }
            )

            train_on_loaders(train_loader, val_loader, logger, configuration)
Exemple #32
0
from src.logging.mixin import LoggingMixin

logger = LoggingMixin().logger


def create_api(configuration: Configuration) -> Flask:
    logger.info('[SETUP] API Application')

    api = Flask(__name__)

    api.url_map.strict_slashes = False
    api.config['SQLALCHEMY_DATABASE_URI'] = configuration.database_uri
    api.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    _register_blueprints(api)

    logger.info('[DONE] API Application')
    return api


def _register_blueprints(api_application: Flask) -> None:
    api_application.register_blueprint(ping_blueprint)
    api_application.register_blueprint(index_blueprint)
    api_application.register_blueprint(bike_blueprint)


configuration = Configuration()
api = create_api(configuration)
db.init_app(api)
migration = Migrate(api, db)
Exemple #33
0
def main(color):
	"""
	 configuration: all the attacks and config data from the user and files
	 attacks: configure attacks from files data
	 hashcat: calls to hashcat individual attacks
	"""

	# print start datetime
	start_date = datetime.now()
	Color.showVerbose("Start date: " + Color.datetime_to_string(start_date))
	Color.showVerbose("Press enter or [s] to see hashcat's status...")
	Color.showVerbose("Press [q]' to skip one hashcat command...")
	Color.showVerbose("Press [Ctrl+c] to skip one attack file...")
	Color.showVerbose("Press [Ctrl+c] x3 times to stop all attacks...")

	# get input arguments
	arguments = getArguments()

	if not arguments.attacks_file:
		Color.showError("Nothing happening here... add [-a attacks_file] to execute attacks", True)

	# ctrl+c signal counter for exiting program instead of bypassing resources
	interruptCounter = 0
	lastInterruptTime = datetime.now()

	try:
		"""
		For every attacks_file in the list (all configs)
		"""
		attacks_file_list = Configuration.getConfigFilesArray(arguments.attacks_file)

		for attacks_file in attacks_file_list:

			"""
			For every hash_file in the list, execute all the defined attacks in the attacks_file
			"""
			hash_files_list = Configuration.getHashFilesArray(arguments.hash_file, arguments.hash_type, arguments.extra_params, arguments.hash_files)

			for hash_file_item in hash_files_list:
				parsing_errors = False

				if hash_file_item:
					if hash_file_item["hash_file"]:
						hash_file = hash_file_item["hash_file"]
					else:
						parsing_errors = True
					if hash_file_item["hash_type"]:
						hash_type = hash_file_item["hash_type"]
					else:
						parsing_errors = True
					if len(hash_file_item) == 3 and hash_file_item["extra_params"]:
						extra_params = hash_file_item["extra_params"]
					else:
						extra_params = ""
				else:
					parsing_errors = True

				if parsing_errors:
					Color.showError("Error in the files/types/extra_param parsing... skipping this file", False)
					break

				# load other scripts
				conf = Configuration(hash_file, hash_type, attacks_file, extra_params, arguments.output_dir, arguments.wordlist_custom_file)
				hashcat = Hashcat(conf.static_values, arguments.verbose, color)
				attacks = Attacks(hashcat)

				
				#set logging file
				log_path = os.path.join(conf.results_dir, "autocrackeo.log")
				color.setFileHandler(log_path) # set log file
				Color.showVerbose("The results (potfile, cracked passwords and logfile) will be written to: " + conf.results_dir + "\n")

				# print important info
				Color.showTitle(Color.datetime_to_string(datetime.now()))
				msg = "Attacks config file:" + attacks_file + ", hash file:" + hash_file + ", hash type:" + hash_type + ", extra params:" + extra_params
				Color.showMessage(msg  + "\n") # show attack file
				color.logThis("[i] " + Color.datetime_to_string(datetime.now()) + ", " + msg) # log attack file

				if attacks_file: # if -c/--config
					"""
					Execute a specific selection of hashcat attacks
					previously defined on the configuration json file
					This will be updated gradually as the efficiency of the attacks are measured
					"""
					try:
						for attack_name in conf.attacks:
							if arguments.verbose: Color.showVerbose("Attack type: " + attack_name.replace("_"," ").title()) # nice print
							if "straight" in attack_name:
								attacks.straight_attacks(attack_name, conf.attacks[attack_name], conf.wordlists, conf.rules)
							elif "combinator" in attack_name:
								attacks.combinator_attacks(attack_name, conf.attacks[attack_name], conf.wordlists)
							elif "brute_force" in attack_name:
								attacks.brute_force_attacks(attack_name, conf.attacks[attack_name], conf.masks)
							elif "hybrid" in attack_name:
								attacks.hybrid_attacks(attack_name, conf.attacks[attack_name], conf.wordlists, conf.masks)
							elif "one_word_per_hash" in attack_name:
								attacks.OneWordPerHashAttacks(attack_name, conf.attacks[attack_name], conf.wordlists)
							else:
								Color.showError("This attack name is not recognized!", False)

							# dump plaintext passwords from potfile to custom wordlist
							if arguments.feedback: hashcat.feedback(arguments.wordlist_custom_file)

					except KeyboardInterrupt as ki:
						"""
						Set a SIGINT signal handler 
						to securely skip all the attacks for this hash_file and attacks_file
						and it continues the loop
						but if it is x3 times clicked with less than 1 second of distance, exit program
						"""
						Color.showError("Ctrl+C {0}: Skipping {1} attacks...".format(interruptCounter+1,attacks_file), False)
						color.logThis("[X] Ctrl+C {0}: Skipping {1} attacks...".format(interruptCounter+1,attacks_file))

						hashcat.save_cracked() # dump results output in cracked file

						interruptTime = datetime.now()
						difference = interruptTime - lastInterruptTime
						if (difference.total_seconds() < 1):
							interruptCounter+=1
							if (interruptCounter > 2):
								ki.message = "ctrl+c x3"
								raise
						else:
							interruptCounter = 0
						
						lastInterruptTime = interruptTime

					except Exception as e:
						Color.showException(e, True)

					hashcat.save_cracked() # ALWAYS DUMP RESULTS: for every config file tried, and every hash file/type

	except KeyboardInterrupt as ki:
		Color.showError("Ctrl+C (x3): Exiting attacks...", False)
		color.logThis("[X] Ctrl+C (x3): Exiting attacks...")
	except Exception as e:
		Color.showError(str(e), False)

	"""
	 Print end of execution
	"""	
	# print end datetime and duration
	Color.showTitle("")
	end_date = datetime.now()
	Color.showVerbose("End date: " + Color.datetime_to_string(end_date))
	duration = end_date - start_date
	Color.showVerbose("Duration: " + Color.timedelta_to_string(duration))
Exemple #34
0
def main():
    log(0, "Creating tables... " + str(Configuration().get()))
    Node.create_table()
    CommandQueue.create_table()
    PoolingLogs.create_table()
    CommandResponse.create_table()
Exemple #35
0
        yn.value = value
        yn.update_property_list()

    print("\nCalculating Target Value...:")
    targetValue = sum([yn.target_function() for yn in y])
    print("Current targeted value:", targetValue)

    return targetValue


if __name__ == "__main__":

    welcome()

    confFile = get_cmd_arg()
    cfg = Configuration()
    cfg.read(confFile)
    #TODO no need a class.

    (optMethod, ) = cfg.get_config("optimization")
    (
        initParaTableFile,
        paraTableFile,
        ffForSimulation,
        ffTemplate,
    ) = cfg.get_config('parameters')
    (
        mode,
        execute,
        path,
        processScript,