Exemplo n.º 1
0
def upload(data):
    """Upload a new Pcap File"""
    tmpPath = app.config["UPLOAD_FOLDER"] + "tmp.cap"
    with open(tmpPath, "w") as f:
        f.write(data["fileContent"])
    parse(tmpPath)
    os.remove(tmpPath)
    reloadData()
    socketio.emit("successfullUpload", {"success": "Got it !"})
Exemplo n.º 2
0
def upload(data):
    """Upload a new Pcap File"""
    tmpPath = app.config['UPLOAD_FOLDER'] + "tmp.cap"
    with open(tmpPath, 'w') as f:
        f.write(data['fileContent'])
    parse(tmpPath)
    os.remove(tmpPath)
    reloadData()
    socketio.emit('successfullUpload', {'success': 'Got it !'})
Exemplo n.º 3
0
def stream_logs(app_name, source, dyno):
    while True:
        # Main Thread Loop
        try:
            for line in HerokuInterface().stream_log(app_name=app_name,
                                                     source=source,
                                                     dyno=dyno,
                                                     timeout=100):
                # Search for the required keywords in this line
                logging.debug(line.decode('utf-8'))
                exit = parse(line.decode('utf-8'), app_name, source, dyno)

                if exit:
                    break

            root = app_name + settings.SEPERATOR + source + settings.SEPERATOR + dyno
            exit = True if root not in settings.RULES else False

            if exit:
                break

        except Exception:
            logging.error(traceback.format_exc())
            # TODO: Handle specific exceptions here
            # Cooling period in case of errors
            time.sleep(1)

    logging.info("Stopping log thread: {}:{}:{}".format(
        app_name, source, dyno))
Exemplo n.º 4
0
def main(argc, argv):
    visual = False
    native = False
    if "-v" in argv:
        visual = True
    if "-n" in argv:
        native = True
    try:
        f_content = read_all(argv[argv.index("-f") + 1])
    except:
        print("Usage: python main.py [-v] -f file_name")
        exit()
    tokens = lex(f_content)
    parsed = parse(tokens)
    # print(json.dumps(parsed, indent=4))

    question_facts = parsed["question_facts"]
    Rules = parsed["rules"]
    check_rules(Rules)
    Facts = parsed["facts"]
    Graph = nx.Graph(parsed["graph_body"])

    config.init(Rules, Facts, Graph, visual, native)

    init_pos()
    if config.visual:
        draw_graph()
    solve()
    if config.visual:
        draw_graph()

    # Result
    for q in question_facts:
        print(q, Facts[q])
Exemplo n.º 5
0
def match_wind_fname(wind_fname_list, query_body, max_sec_bound=21960):
    # If there is wind file, then return the closest one within the max_sec_bound (in seconds)
    time_objs = []
    trash_holder = []
    wind_fname_list.sort()
    for item in wind_fname_list:
        if item.endswith('.npz'):
            time_string = re.findall('\d\d\d\d\d\d\d\d_\d\d\d\d_\d\d\d',
                                     item)[0]
            time_obj = parser.parse('%s %s'%(time_string[:8], time_string[9:13])) + \
                        datetime.timedelta(hours = int(time_string[-3:]))
            time_diff = (time_obj - baseline_time).total_seconds()
            if time_diff not in trash_holder:
                trash_holder.append(time_diff)
                time_objs.append([item, time_diff])
            else:
                time_objs.pop(-1)
                time_objs.append([item, time_diff])
        else:
            pass

    time_objs = np.array(time_objs, dtype=np.object)
    wind_ftime_tree = cKDTree(time_objs[:, -1].reshape(-1, 1))
    query_dist, query_index = wind_ftime_tree.query(
        query_body.reshape(-1, 1), p=1, distance_upper_bound=max_sec_bound)
    valid_query = query_index < time_objs.shape[0]  # binary array
    return query_index, valid_query, time_objs, wind_ftime_tree
Exemplo n.º 6
0
 def test_parse2(self):
     sentence = "return x*y"
     gram, tokenList, exeToken, param_list = parser.parse(sentence)
     print(gram)
     print(tokenList)
     print(exeToken)
     print(param_list)
Exemplo n.º 7
0
 def test_parse3(self):
     sentence = "nodes = graphFromParam.V #set"
     gram, tokenList, exeToken, param_list = parser.parse(sentence)
     print(gram)
     print(tokenList)
     print(exeToken)
     print(param_list)
Exemplo n.º 8
0
 def test_parse4(self):
     sentence = "for i = 0 to 9 step 2"
     gram, tokenList, exeToken, param_list = parser.parse(sentence)
     print(gram)
     print(tokenList)
     print(exeToken)
     print(param_list)
Exemplo n.º 9
0
def random_solver_solution(filename, trials_max, error_max):
    """
     Génère la solution de l'algorithme aléatoire

     :param: filename : Nom du fichier (sans extension)
     :param: trials_max : Nombre d'essai de l'application de l'algorithme aléatoire
     :param: error_max : Nombre d'erreur maximal pour le placement des bâtiments
    """
    max_score = 0
    trials_count = 0
    path_out = os.path.join(os.path.curdir, 'data', 'output')

    while trials_count < trials_max:
        if not os.path.exists(path_out):
            os.makedirs(path_out)
        cityplan, project_list = parser.parse(
            filename)  # Génère un CityPlan vide et une liste de Project
        cityplan, replica_list = _random_solver(
            cityplan, project_list,
            error_max)  # Rempli le CityPlan et renvoi une liste de Replica
        score = scoring.scoring_from_replica_list(replica_list, cityplan,
                                                  project_list)
        trials_count += 1

        if score >= max_score:
            max_score = score
            parser.imgify(filename, cityplan, project_list, replica_list)
            parser.textify(replica_list, filename)

    _print_solution(filename, trials_max, max_score)
Exemplo n.º 10
0
 def test_parse5(self):
     sentence = "else if y < 0"
     gram, tokenList, exeToken, param_list = parser.parse(sentence)
     print(gram)
     print(tokenList)
     print(exeToken)
     print(param_list)
Exemplo n.º 11
0
def upload():
    if request.method == "POST":
        tmpPath = app.config["UPLOAD_FOLDER"] + "tmp.cap"
        request.files["files"].save(tmpPath)
        pcap = parse(tmpPath)
        os.remove(tmpPath)

        #         except:
        #             flash(u"Impossible to download ", "error")
        #             return jsonify(error='An error occured')

        return jsonify(success="Pcap uploaded successfully !")
Exemplo n.º 12
0
def upload():
    if request.method == "POST":
        tmpPath = app.config['UPLOAD_FOLDER'] + "tmp.cap"
        request.files['files'].save(tmpPath)
        pcap = parse(tmpPath)
        os.remove(tmpPath)

        #         except:
        #             flash(u"Impossible to download ", "error")
        #             return jsonify(error='An error occured')

        return jsonify(success='Pcap uploaded successfully !')
Exemplo n.º 13
0
def match_ncwf_fname(start_time, query_body, max_sec_bound=7200):
    # If there is any weather instance within the max_sec_bound, then return only the closest one (w.r.t. time)
    time_obj_wx = []
    time_diff_wx = []
    ncwf_fname_hourly = []
    for obj in start_time:
        tmp_fname = '%d_%s_%s_%s00Z.npz' % (obj[0], str(
            obj[1]).zfill(2), str(obj[2]).zfill(2), str(obj[3]).zfill(2))
        tmp_time = parser.parse('%d/%d/%d %d:00:00' %
                                (obj[1], obj[2], obj[0], obj[3]))
        ncwf_fname_hourly.append(tmp_fname)
        time_obj_wx.append(tmp_time)
        time_diff_wx.append((tmp_time - baseline_time).total_seconds())

    ncwf_fname_hourly = np.array(ncwf_fname_hourly)
    time_diff_wx = np.array(time_diff_wx)
    time_obj_wx = np.array(time_obj_wx)
    wx_ftime_tree = cKDTree(time_diff_wx.reshape(-1, 1))

    query_dist, query_index = wx_ftime_tree.query(
        query_body.reshape(-1, 1), p=1, distance_upper_bound=max_sec_bound)
    valid_query = query_index < time_diff_wx.shape[0]
    return query_index, valid_query, time_obj_wx, ncwf_fname_hourly, wx_ftime_tree


# test_wind_npz = np.load('../../DATA/filtered_weather_data/namanl_small_npz/namanl_218_20130101_0000_000.npz')
# grbs_common_info = np.load('/media/storage/DATA/filtered_weather_data/grbs_common_info.npz')
# basegrid_lat = grbs_common_info['basegrid_lat']
# basegrid_lon = grbs_common_info['basegrid_lon']
# basegrid = grbs_common_info['basegrid']
# smallgrid = grbs_common_info['smallgrid']
# small_idx = grbs_common_info['small_idx']
# lvls = grbs_common_info['levels']
# lvls_dict = {}
# i = 0
# for lvl in lvls:
#     lvls_dict[lvl] = i
#     i += 1

# with open('/media/storage/DATA/filtered_weather_data/grbs_level_common_info.pkl', 'rb') as pfile:
#     lvls_dict = pickle.load(pfile)
# with open('/media/storage/DATA/filtered_weather_data/grbs_smallgrid_kdtree.pkl', 'rb') as pfile:
#     smallgrid_tree = pickle.load(pfile)

# wx_pointer = np.load('../../DATA/NCWF/gridded_storm.npz')
# ncwf_arr = wx_pointer['ncwf_arr']
# start_time = wx_pointer['start_time']
# unique_alt = wx_pointer['unique_alt']
# with open('../../DATA/NCWF/alt_dict.pkl', 'rb') as pfile:
#     alt_dict = pickle.load(pfile)
Exemplo n.º 14
0
    def clean(self, *args, **kwargs):
        sys.path.append(os.path.realpath('.'))
        from utils import parser

        cleaned_data = super().clean()
        url = cleaned_data.get('url')
        title = cleaned_data.get('title')
        if url!='':
            if parser.parse(url):
                return super(TaskCreateForm, self).clean(*args, **kwargs)
            else:
                print("Check url")
                raise forms.ValidationError("Проверьте ссылку") # TODO: добавить некорректность ссылки как ошибку
        elif title=='':
            print("No title")
            raise forms.ValidationError("Название или ссылка на ресурс должны быть обязательно")
        return super(TaskCreateForm, self).clean(*args, **kwargs)
Exemplo n.º 15
0
def match_ncwf_fname(start_time, query_body, max_sec_bound=7200):
    # If there is any weather instance within the max_sec_bound, then return only the closest one (w.r.t. time)
    time_obj_wx = []
    time_diff_wx = []
    ncwf_fname_hourly = []
    for obj in start_time:
        tmp_fname = '%d_%s_%s_%s00Z.npz' % (obj[0], str(
            obj[1]).zfill(2), str(obj[2]).zfill(2), str(obj[3]).zfill(2))
        tmp_time = parser.parse('%d/%d/%d %d:00:00' %
                                (obj[1], obj[2], obj[0], obj[3]))
        ncwf_fname_hourly.append(tmp_fname)
        time_obj_wx.append(tmp_time)
        time_diff_wx.append((tmp_time - baseline_time).total_seconds())

    ncwf_fname_hourly = np.array(ncwf_fname_hourly)
    time_diff_wx = np.array(time_diff_wx)
    time_obj_wx = np.array(time_obj_wx)
    wx_ftime_tree = cKDTree(time_diff_wx.reshape(-1, 1))

    query_dist, query_index = wx_ftime_tree.query(
        query_body.reshape(-1, 1), p=1, distance_upper_bound=max_sec_bound)
    valid_query = query_index < time_diff_wx.shape[0]
    return query_index, valid_query, time_obj_wx, ncwf_fname_hourly, wx_ftime_tree
Exemplo n.º 16
0
from gen_utils import *
from nin_model import NiNModel
from utils.parser import parse

FLAGS = parse()

model = NiNModel(FLAGS)

with tf.Session() as sess:
    model.train(sess)
Exemplo n.º 17
0
def elitist_solver_solution(filename, error_max, generation_max):
    """
     Algorithme élitiste de notre projet

     :param: filename : Nom du fichier (sans extension)
     :param: trials_max : Nombre d'essai de l'application de l'algorithme aléatoire
     :param: error_max : Nombre d'erreur maximal pour le placement des bâtiments
     :return: Affichage d'infomrations et génération de l'output
     :rtype: String

    """
    cityplan, project_list = parser.parse(
        filename)  # Génère un CityPlan vide et une liste de Project
    best_building_with_points = []
    taille_matrix = cityplan.matrix.shape

    for generation_number in range(1, generation_max + 1):
        buildings_with_points = []
        configuration_list = []

        # on récupère un cityplan et un replica_list avec les anciens meilleurs batiments
        cityplan, replica_list = _copy_best_buildings_in_matrix(
            cityplan, project_list, best_building_with_points, taille_matrix)
        # réinitialisation de la variable
        best_building_with_points = []

        print("Elitist solver for :", cityplan.name_project)
        print("...")

        # Génération d'une population aléatoire
        cityplan, replica_list = _advanced_random_solver(
            cityplan, project_list, error_max,
            replica_list)  # Rempli le CityPlan et renvoi une liste de Replica

        parser.imgify(filename + str(generation_number), cityplan,
                      project_list, replica_list)
        parser.textify(replica_list, filename + str(generation_number))

        begin_time = time.time()
        tested_replica = 0
        len_replica_list = len(replica_list)
        # score_total correspond au score total d'une génération
        score_total = 0

        # Lecture des repliques placées dans la map
        for building in replica_list:
            project_number = int(building[0])
            if type(project_list[project_number]) is Residential:
                if (tested_replica % 500) == 0 and tested_replica != 0:
                    scoring._affichage_score(tested_replica, len_replica_list,
                                             score_total, begin_time, False)

                row = building[1][0]
                col = building[1][1]
                building_score = scoring.building_score(
                    cityplan, row, col, project_list, project_number,
                    replica_list)  # Calcul des scores de chaque batiments
                score_total += building_score

                plan = project_list[project_number].matrix
                resid_adapted_coordinates = scoring._coordinates_adaptation(
                    plan, row, col)

                used_surface = project_list[
                    project_number].get_manhattan_surface(
                        int(cityplan.dist_manhattan_max), cityplan.matrix,
                        resid_adapted_coordinates)

                list_used_surface = list(used_surface)

                # Récupération de toutes les cases remplies pour notre configuration
                id_utility_list = []
                cases_configuration = []
                for cases in list_used_surface:
                    if str(cityplan.matrix[cases]) != ".":
                        number_replica = int(cityplan.matrix[cases])
                        build = project_list[int(replica_list[int(
                            cityplan.matrix[cases])][0])]

                        if type(build) is Utility:
                            if not (number_replica in id_utility_list):
                                id_utility_list.append(number_replica)
                                building_coordinates = scoring._coordinates_adaptation(
                                    build.matrix,
                                    replica_list[number_replica][1][0],
                                    replica_list[number_replica][1][1])

                                for coor in building_coordinates:
                                    cases_configuration.append(coor)

                # Calcul de l'espace utilisée par la configuration
                if cases_configuration:
                    cases_configuration.sort(key=lambda x: (x[0], x[1]))
                    row_top = int(cases_configuration[0][0])
                    row_bottom = int(
                        cases_configuration[len(cases_configuration) - 1][0])

                    cases_configuration.sort(key=lambda x: (x[1], x[0]))
                    col_top = int(cases_configuration[0][1])
                    col_bottom = int(
                        cases_configuration[len(cases_configuration) - 1][1])

                    taille = [
                        row_bottom - row_top + 1, col_bottom - col_top + 1
                    ]
                    taille_reelle = taille[0] * taille[1]
                    densite = building_score / taille_reelle

                    configuration_list.append([
                        densite, (row, col), project_number, generation_number,
                        taille_reelle
                    ])  # Ajout des scores de chaque batiments
            tested_replica += 1

        print(" =-=-=-=-=-= =-=-=-=-=-=")
        print("Total score Generation ", generation_number,
              "------------------------> ", score_total)
        print("--- %s seconds ---" % (time.time() - begin_time))
        print(" =-=-=-=-=-= =-=-=-=-=-=")

        # Triage des scores par ordre décroissant de la densitées
        configuration_list.sort(reverse=True)

        # Suppression des résidences qui rapportent zéro points
        for idx in range(0, len(configuration_list)):
            if configuration_list[idx][0] > 0:
                buildings_with_points.append(configuration_list[idx])

        # Garde les meilleurs generation_number/generation_max
        for idx in range(
                0,
                int((generation_number / generation_max) *
                    len(buildings_with_points))):
            best_building_with_points.append(buildings_with_points[idx])
Exemplo n.º 18
0
    def test_parse(self):
        #Test1: function
        sentence = "function sort(array)"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        self.assertEquals(gram, 'function_def')
        self.assertEquals(tokenList, ((12, 'function'), (1, 'sort'), (54, '('), (0, 'array'), (55, ')')))
        self.assertEquals(param_list, ['sort', ('array',)])

        sentence = "function sort()"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        print(len(param_list[1]))

        #Test2: variable definition in TYPE VAR = XXX format
        sentence = "Stack x = [1,2]"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        self.assertEquals(gram, 'variable_def')
        self.assertEquals(exeToken, 'x = Stack([ 1 , 2 ] )')
        self.assertEquals(tokenList, ((1, 'Stack'), (0, 'x'), (51, '='), (66, '['), (2, '1'), (64, ','), (2, '2'), (67, ']')))
        self.assertEquals(param_list, ['x'])

        #Test3: variable definition expression
        sentence = "x = 'fang'"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        self.assertEquals(gram, 'expression')
        self.assertEquals(exeToken, "x = 'fang' ")
        self.assertEquals(tokenList, ((0, 'x'), (51, '='), (2, "'fang'")))
        self.assertEquals(param_list, [])

        #Test4: statements:for loop
        sentence = "for x in range(4)"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        self.assertEquals(gram, 'statement')
        self.assertEquals(exeToken, "")
        self.assertEquals(tokenList, ((5, 'for'), (0, 'x'), (14, 'in'), (1, 'range'), (54, '('), (2, '4'), (55, ')')))
        # execute function not finished yet
        # self.assertEquals(param_list, ['x', 0])

        sentence = "for x = 0 to 9"
        # gram, tokenList, exeToken, param_list = parser.parse(sentence)
        # self.assertEquals(gram, 'statement')
        # self.assertEquals(exeToken, "")
        # self.assertEquals(tokenList, ((5, 'for'), (0, 'x'), (51, '='), (2, '0'), (18, 'to'), (2, '9')))
        # #execute function not finished yet
        # self.assertEquals(param_list, ['x', range(0, 0)])
        #
        sentence = "for x = 0 to 9 step 2"
        print(parser.lexical_analyze(sentence))
        # # gram, tokenList, exeToken, param_list = parser.parse(sentence)
        # # self.assertEquals(gram, 'statement')
        # # self.assertEquals(exeToken, "")
        # # self.assertEquals(tokenList, ((5, 'for'), (0, 'x'), (51, '='), (2, '0'), (18, 'to'), (2, '9'), (19, 'step'), (2, '2')))
        # #execute function not finished yet
        # # self.assertEquals(param_list, ['x', range(0, 9, 2)])
        #
        #
        sentence = "while x < 9"
        gram, tokenList, exeToken, param_list = parser.parse(sentence)
        self.assertEquals(gram, 'statement')
        self.assertEquals(exeToken, "x < 9 ")
        self.assertEquals(tokenList, ((6, 'while'), (0, 'x'), (45, '<'), (2, '9')))
        #execute function not finished yet
        self.assertEquals(param_list, ["x < 9 "])
Exemplo n.º 19
0
            self.lon) + "," + str(self.alt) + '\n'

    def parse(self, string):
        m = self.regex.match(string)
        if not m:
            return

        try:
            self.time = utils.parser.parseDate(int(m.group(2)),
                                               int(m.group(3)))
            self.startTime = self.time - datetime.timedelta(
                seconds=int(m.group(1)) / 1000.0)

            self.lat = utils.parser.parseFloat(m.group(5))
            self.lon = utils.parser.parseFloat(m.group(4))
            self.alt = utils.parser.parseFloat(m.group(6))
            self.numSats = int(m.group(7))
            self.hdop = int(m.group(8))

            self.millis = int(m.group(1))
            return (self.millis, self.time, (self.lat, self.lon, self.alt),
                    self.numSats)
        except Exception as e:
            print e


if __name__ == "__main__":
    parser = Parser()
    print parser.parse("hi")
    print parser.parse(
        "#GPS:1329604,80415,18163100,f:E8D91542,f:4B8CF4C2,f:9A196A43,6,132")
Exemplo n.º 20
0
import re, datetime, utils.parser



class Parser:
	def __init__(self):
		self.type = "PRS"
		self.millis = 0
		self.temperature = 0
		self.pressure = 0

	def __str__(self):
		return str(self.millis) + "," + str(self.pressure) + "," + str(self.temperature) + '\n'

	regex = re.compile(r'#PRS:(\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+)')
	def parse(self, string):
		m = self.regex.match(string)
		if not m:
			return
		
		millis = int(m.group(1))
		self.temperature = float(m.group(2))
		self.pressure = float(m.group(3))
		return (self.millis, self.pressure, self.temperature)
if __name__ == "__main__":
	parser = Parser()
	print parser.parse("hi")
	print parser.parse("#PRS:182794,23.10,992.5100")
Exemplo n.º 21
0
import time, datetime
import argparse
import random
import os, sys
import subprocess
import cv2
import matplotlib.pyplot as plt

import matplotlib
#matplotlib.use('Agg') # to generate plot without X server

from utils import data_tool, model_tool, general_tool
from utils.parser import parse
from builders import model_builder

args = parse()

#######################################################################################
# prepare model and dataset
#######################################################################################

dataset_dir = (args.dataset_path if args.dataset_path != None else "dataset/" +
               args.dataset)
class_names_list, label_values_list, class_names_str = data_tool.get_label_info(
    dataset_dir)
nb_class = len(class_names_list)
dataset_file_name = data_tool.get_dataset_file_name(dataset_dir=dataset_dir)

input_size = (data_tool.get_minimal_size(dataset_dir=dataset_dir) if
              (not args.crop_height and not args.crop_width) else {
                  'height': args.crop_height,
Exemplo n.º 22
0
from time import ctime


class Parser:
    regex = re.compile(r'#HTU:(\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+)')

    def __init__(self):
        self.type = "HTU"
        self.millis = -1
        self.temperature = -1
        self.humidity = -1

    def __str__(self):
        return str(self.millis) + "," + str(self.temperature) + "," + str(
            self.humidity) + '\n'

    def parse(self, string):
        m = self.regex.match(string)
        if not m:
            return

        self.millis = int(m.group(1))
        self.temperature = float(m.group(2))
        self.humidity = float(m.group(3))
        return (self.millis, self.temperature, self.humidity)


if __name__ == "__main__":
    parser = Parser()
    print parser.parse("hi")
    print parser.parse("#HTU:3412302,15.40,59.60")
Exemplo n.º 23
0
 def stop(self):
     case_recorder = caseRecorder(self._ansible_ctl.get_data_path_per_case())
     self._ansible_ctl.collect_data()
     case_recorder.write_end()
     parser.parse(self._ansible_ctl.get_data_path_per_case())
Exemplo n.º 24
0
import re, datetime, utils.parser



class Parser:
	regex = re.compile(r'#DHT:(\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+)')
	def __init__(self):
		self.type = "DHT"
		self.millis = -1
		self.temperature = -1
		self.humidity = -1

	def __str__(self):
		return str(self.millis) + "," + str(self.temperature) + "," + str(self.humidity) + '\n'
	
	def parse(self, string):
		m = self.regex.match(string)
		if not m:
			return
		
		self.millis = int(m.group(1))
		self.temperature = float(m.group(2))
		self.humidity = float(m.group(3))
		return (self.millis, self.temperature, self.humidity)
if __name__ == "__main__":
	parser = Parser()
	print parser.parse("hi")
	print parser.parse("#DHT:3412302,15.40,59.60")
Exemplo n.º 25
0
def recursive(content, index, module):
    """
    recursively execute sentences

    Grammar structure:
    definition: Define variables or functions. Assignment statements are also considered as definition

    Expression: Operations

    Statement: Including if, while, for and repeat/until, break, continue, return.
    """
    if module.end_recursive:
        return
    else:
        # code compilation in progress
        if index < len(content):
            glb.current_line = module.line + index

            # empty line or comment line
            if not content[index].split("#")[0] or content[index].split("#")[0].isspace():
                index += 1
            else:
                # console output on server side
                # print("compile content: {}".format(content[index]))

                # lexicial analyze the statement
                grammar_type, tokenList, exeToken, param_list = parser.parse(content[index])

                # Case1: function definition
                if grammar_type == "function_def":
                    lineCount = get_block_count(content, index)
                    module_content = content[index + 1 : index + lineCount + 1]
                    func_name = param_list[0]
                    func_param_list = param_list[1]
                    glb.current_line += 1  # navigate to the next line
                    funcModule = functionmodule(func_name, func_param_list, module_content, glb.current_line)
                    module._func_inc(func_name, funcModule)
                    index += lineCount + 1
                # Case2: expression
                elif grammar_type == "expression":
                    from utils.consoleManager import stdoutIO

                    with stdoutIO() as s:
                        execute(exeToken)
                    consoleOutput = s.getvalue()
                    if consoleOutput:
                        glb.console_output.append(consoleOutput)

                    plot.plot()

                    index += 1
                # Case3: statement
                elif grammar_type == "statement":
                    # continue, break, return

                    # 3.1 return statement
                    if tokenList[0][1] == "return":
                        try:
                            for item in reversed(glb.variable_stack):
                                if isinstance(item, functionmodule):
                                    item.return_list = evaluate(exeToken)
                                    break
                        except AttributeError:
                            raise Exception("SyntaxError: return statement must be used inside function.")

                        # set end_recursive to true
                        for item in reversed(glb.variable_stack):
                            if isinstance(item, functionmodule):
                                break
                            elif isinstance(item, basemodule):
                                item.setEnd()

                        # TODO plot.printVar() #print variable status before return

                        return  # terminate the function

                    # 3.2 break/continue statement
                    elif tokenList[0][1] == "break" or tokenList[0][1] == "continue":
                        for item in reversed(glb.variable_stack):
                            if isinstance(item, functionmodule):
                                raise Exception("Break/continue can only be used in while and for loops")

                            # terminate the modules inside loops
                            if isinstance(item, basemodule):
                                if not isinstance(item, loopType):
                                    item.setEnd()
                                else:
                                    item.setEnd()
                                    if tokenList[0][1] == "continue":
                                        item.setContinue()
                                    break
                        # TODO plot.printVar() #print variable status
                        return

                    # 3.3 if, while, for statement
                    else:
                        lineCount = get_block_count(content, index)
                        module_content = content[index + 1 : index + lineCount + 1]

                        if tokenList[0][1] == "if":
                            conditionList = [param_list[0]]
                            contentList = [module_content]

                            index += lineCount + 1

                            if index < len(content):
                                grammar_type, tokenList, exeToken, param_list = parser.parse(content[index])

                                while len(tokenList) > 0 and tokenList[0][1] == "else":
                                    lineCount = get_block_count(content, index)
                                    contentList.append(content[index + 1 : index + lineCount + 1])
                                    index += lineCount + 1
                                    conditionList.append(param_list[0])

                                    # continue looping
                                    if index >= len(content):
                                        break
                                    grammar_type, tokenList, exeToken, param_list = parser.parse(content[index])

                            ifModule = ifelsemodule(conditionList, contentList, glb.current_line)

                            ifModule.run()

                        elif tokenList[0][1] == "for":
                            forModule = formodule(param_list, module_content, glb.current_line)
                            forModule.run()
                            index += lineCount + 1

                        elif tokenList[0][1] == "while":
                            whileModule = whilemodule(param_list[0], module_content, glb.current_line)
                            whileModule.run()
                            index += lineCount + 1

                        else:
                            raise Exception(
                                'Unsupported keyword: {} in statement "{}"'.format(tokenList[0][1], content[index])
                            )

            # recursively compile the content
            recursive(content, index, module)
Exemplo n.º 26
0
def recursive(content, index, module):
    """
    recursively execute sentences

    Grammar structure:
    definition: Define variables or functions. Assignment statements are also considered as definition

    Expression: Operations

    Statement: Including if, while, for and repeat/until, break, continue, return.
    """
    if module.end_recursive:
        return
    else:
        #code compilation in progress
        if index < len(content):
            glb.current_line = module.line + index

            #empty line or comment line
            if not content[index].split("#")[0] or content[index].split(
                    "#")[0].isspace():
                index += 1
            else:
                #console output on server side
                # print("compile content: {}".format(content[index]))

                #lexicial analyze the statement
                grammar_type, tokenList, exeToken, param_list = parser.parse(
                    content[index])

                #Case1: function definition
                if grammar_type == 'function_def':
                    lineCount = get_block_count(content, index)
                    module_content = content[index + 1:index + lineCount + 1]
                    func_name = param_list[0]
                    func_param_list = param_list[1]
                    glb.current_line += 1  #navigate to the next line
                    funcModule = functionmodule(func_name, func_param_list,
                                                module_content,
                                                glb.current_line)
                    module._func_inc(func_name, funcModule)
                    index += (lineCount + 1)
                #Case2: expression
                elif grammar_type == 'expression':
                    from utils.consoleManager import stdoutIO

                    with stdoutIO() as s:
                        execute(exeToken)
                    consoleOutput = s.getvalue()
                    if consoleOutput:
                        glb.console_output.append(consoleOutput)

                    plot.plot()

                    index += 1
                #Case3: statement
                elif grammar_type == 'statement':
                    #continue, break, return

                    #3.1 return statement
                    if tokenList[0][1] == 'return':
                        try:
                            for item in reversed(glb.variable_stack):
                                if isinstance(item, functionmodule):
                                    item.return_list = evaluate(exeToken)
                                    break
                        except AttributeError:
                            raise Exception(
                                "SyntaxError: return statement must be used inside function."
                            )

                        #set end_recursive to true
                        for item in reversed(glb.variable_stack):
                            if isinstance(item, functionmodule):
                                break
                            elif isinstance(item, basemodule):
                                item.setEnd()

                        # TODO plot.printVar() #print variable status before return

                        return  #terminate the function

                    #3.2 break/continue statement
                    elif tokenList[0][1] == 'break' or tokenList[0][
                            1] == 'continue':
                        for item in reversed(glb.variable_stack):
                            if isinstance(item, functionmodule):
                                raise Exception(
                                    "Break/continue can only be used in while and for loops"
                                )

                            #terminate the modules inside loops
                            if isinstance(item, basemodule):
                                if not isinstance(item, loopType):
                                    item.setEnd()
                                else:
                                    item.setEnd()
                                    if tokenList[0][1] == 'continue':
                                        item.setContinue()
                                    break
                        #TODO plot.printVar() #print variable status
                        return

                    #3.3 if, while, for statement
                    else:
                        lineCount = get_block_count(content, index)
                        module_content = content[index + 1:index + lineCount +
                                                 1]

                        if tokenList[0][1] == 'if':
                            conditionList = [param_list[0]]
                            contentList = [module_content]

                            index += (lineCount + 1)

                            if index < len(content):
                                grammar_type, tokenList, exeToken, param_list = parser.parse(
                                    content[index])

                                while len(tokenList
                                          ) > 0 and tokenList[0][1] == 'else':
                                    lineCount = get_block_count(content, index)
                                    contentList.append(
                                        content[index + 1:index + lineCount +
                                                1])
                                    index += (lineCount + 1)
                                    conditionList.append(param_list[0])

                                    #continue looping
                                    if index >= len(content):
                                        break
                                    grammar_type, tokenList, exeToken, param_list = parser.parse(
                                        content[index])

                            ifModule = ifelsemodule(conditionList, contentList,
                                                    glb.current_line)

                            ifModule.run()

                        elif tokenList[0][1] == 'for':
                            forModule = formodule(param_list, module_content,
                                                  glb.current_line)
                            forModule.run()
                            index += (lineCount + 1)

                        elif tokenList[0][1] == 'while':
                            whileModule = whilemodule(param_list[0],
                                                      module_content,
                                                      glb.current_line)
                            whileModule.run()
                            index += (lineCount + 1)

                        else:
                            raise Exception(
                                "Unsupported keyword: {} in statement \"{}\"".
                                format(tokenList[0][1], content[index]))

            #recursively compile the content
            recursive(content, index, module)
Exemplo n.º 27
0
def recursive(content, index, module):
    """
    recursively execute sentences

    Grammer structure:
    Defination: Define variables or functions, because the character of psudocode,
                assignment statement are also considered as defination

    Expression: Operations

    Statement: Including if, while, for and repeat/until, break, continue, return.
    """
    if module.end_recursive:
        return
    else:
        if (index < len(content)):
            if not content[index]:
                index += 1
            else:  # and (content[index]):
                glb.current_content = content[index]
                glb.current_line = module.line + index
                print('compile content : {}'.format(content[index]))
                # preprocess should remove all annotations and useless whitespaces as well as blank lines
                gramm_type, tokens, extoken, param_list = parser.parse(
                    content[index], module)
                #----------DEFINATION---------
                if gramm_type == 'defination':
                    if tokens[0][1] == 'function':
                        count = get_module_index(content, index)
                        module_content = [
                            content[index + i] for i in range(1, count)
                        ]
                        func_name = param_list[0]
                        param_list = param_list[1]
                        glb.current_line += 1
                        funcModule = funcmodule(func_name, param_list,
                                                module_content,
                                                glb.current_line)
                        module._func_inc(func_name, funcModule)
                        index += count
                    else:
                        var_name = param_list[0]
                        module._var_inc(var_name, 0)
                        execute(extoken, module)
                        index += 1
                #---------EXPRESSION----------
                elif gramm_type == 'exp':
                    execute(extoken, module)
                    index += 1
                    #---------STATEMENT----------
                elif gramm_type == 'statement':

                    # continue, break, and return
                    # haven't tested yet, waiting for debug

                    # deal with "return" statement:
                    #    reverse stop modules in module_stack until meet last function module
                    if tokens[0][1] == 'return':
                        try:
                            # test return multiple values
                            module.return_list = eval(extoken,
                                                      glb.global_var_list,
                                                      module.var_list)
                        except AttributeError:
                            print(
                                'SyntaxError: return statement should be in a function'
                            )
                            sys.exit(1)
                        for module in reversed(glb.module_stack):
                            if not isinstance(module, function_module):
                                module.setEnd()
                            else:
                                break
                        return

                    # deal with "break" and "continue "statement:
                    #   similiar with "return" statement
                    if (tokens[0][1] == 'break') or (tokens[0][1]
                                                     == 'continue'):
                        for module in reversed(glb.module_stack):
                            if not isinstance(module, loop_module):
                                module.setEnd()
                            else:
                                # first "not loop" module
                                module.setEnd()
                                if tokens[0][1] == 'continue':
                                    module.setContinue()
                                break
                        return

                    # if, while, and for
                    count = get_module_index(content, index)
                    module_content = [
                        content[index + i] for i in range(1, count)
                    ]
                    exp = param_list[0]

                    if tokens[0][1] == 'if':
                        exps = [exp]
                        contents = [module_content]
                        index += count

                        # find better way to write this part, a little bit ugly
                        if index < len(content):
                            gramm_type, tokens, extoken, param_list = parser.parse(
                                content[index], module)
                            while tokens[0][1] == 'else':
                                count = get_module_index(content, index)
                                contents.append([
                                    content[index + i]
                                    for i in range(1, count)
                                ])
                                index += count
                                exp = param_list[0]
                                exps.append(exp)
                                if index >= len(content):
                                    break
                                gramm_type, tokens, extoken, param_list = parser.parse(
                                    content[index], module)

                        glb.current_line += 1
                        ifModule = ifelsemodule(module.var_list,
                                                module.func_list, exps,
                                                contents, glb.current_line)
                        ifModule.run()
                        index -= count

                    elif tokens[0][1] == 'for':
                        glb.current_line += 1
                        forModule = formodule(module.var_list,
                                              module.func_list, exp,
                                              module_content, glb.current_line)
                        forModule.run()

                    elif tokens[0][1] == 'while':
                        # glb.current_line+=1
                        whileModule = whilemodule(module.var_list,
                                                  module.func_list, exp,
                                                  module_content,
                                                  glb.current_line)
                        whileModule.run()
                    # elif tokens[0][1] == 'repeat':
                    #     pass

                    index += count

            recursive(content, index, module)
Exemplo n.º 28
0
async def inline_kb_answer_callback_handler(query: types.CallbackQuery):
    await bot.send_message(query.from_user.id, parse())
Exemplo n.º 29
0
        self.numSats = 0

    def __str__(self):
        return str(self.time) + "," + str(self.lat) + "," + str(self.lon) + "," + str(self.alt) + "\n"

    def parse(self, string):
        m = self.regex.match(string)
        if not m:
            return

        try:
            self.time = utils.parser.parseDate(int(m.group(2)), int(m.group(3)))
            self.startTime = self.time - datetime.timedelta(seconds=int(m.group(1)) / 1000.0)

            self.lat = utils.parser.parseFloat(m.group(5))
            self.lon = utils.parser.parseFloat(m.group(4))
            self.alt = utils.parser.parseFloat(m.group(6))
            self.numSats = int(m.group(7))
            self.hdop = int(m.group(8))

            self.millis = int(m.group(1))
            return (self.millis, self.time, (self.lat, self.lon, self.alt), self.numSats)
        except Exception as e:
            print e


if __name__ == "__main__":
    parser = Parser()
    print parser.parse("hi")
    print parser.parse("#GPS:1329604,80415,18163100,f:E8D91542,f:4B8CF4C2,f:9A196A43,6,132")
Exemplo n.º 30
0
import numpy as np
import matplotlib.pyplot as plt

import utils.parser as parser
# parse args
args = parser.parse()

import keras
from keras.models import Model
from keras.layers import Input, Dense, Dropout, Lambda, Activation, Reshape
from keras import backend as K

import utils.data_loader as data_loader
from autoencoders.models import *
import utils.viz as viz

# load data
data1, data2 = data_loader.load(args)
x_train_orig, y_train_orig, x_test_orig, y_test_orig = data1
x_train, y_train, xy_train, x_xy_y_train, x_test, y_test_gen = data2

IMG_SIZE = x_train_orig.shape[1]
INPUT_SIZE = x_train.shape[1]
LATENT_SIZE = 32
EPOCHS = 50
EPOCHS_COMPLETE = 5
BATCH_SIZE = 64

##############
# Autoencoder
if args['model'] == 'flatten':
Exemplo n.º 31
0
 def stop(self):
     case_recorder = caseRecorder(
         self._ansible_ctl.get_data_path_per_case())
     self._ansible_ctl.collect_data()
     case_recorder.write_end()
     parser.parse(self._ansible_ctl.get_data_path_per_case())
Exemplo n.º 32
0
def recursive(content, index, module):
    """
    recursively execute sentences

    Grammer structure:
    Defination: Define variables or functions, because the character of psudocode,
                assignment statement are also considered as defination

    Expression: Operations

    Statement: Including if, while, for and repeat/until, break, continue, return.
    """
    if module.end_recursive:
        return
    else:
        if (index < len(content)):
            if not content[index]:
                index += 1
            else:# and (content[index]):
                glb.current_content = content[index]
                glb.current_line = module.line + index
                print('compile content : {}'.format(content[index]))
                # preprocess should remove all annotations and useless whitespaces as well as blank lines
                gramm_type, tokens, extoken, param_list = parser.parse(content[index], module)
                #----------DEFINATION---------
                if gramm_type == 'defination':
                    if tokens[0][1] == 'function':
                        count = get_module_index(content, index)
                        module_content = [content[index+i] for i in range(1, count)]
                        func_name = param_list[0]
                        param_list = param_list[1]
                        glb.current_line+=1
                        funcModule = funcmodule(func_name, param_list, module_content,glb.current_line)
                        module._func_inc(func_name, funcModule)
                        index += count
                    else:
                        var_name = param_list[0]
                        module._var_inc(var_name, 0)
                        execute(extoken, module)
                        index += 1
                #---------EXPRESSION----------
                elif gramm_type == 'exp':
                    execute(extoken, module)
                    index += 1
                    #---------STATEMENT----------
                elif gramm_type == 'statement':

                    # continue, break, and return
                    # haven't tested yet, waiting for debug

                    # deal with "return" statement:
                    #    reverse stop modules in module_stack until meet last function module
                    if tokens[0][1] == 'return':
                        try:
                            # test return multiple values
                            module.return_list = eval(extoken, glb.global_var_list, module.var_list)
                        except AttributeError:
                            print('SyntaxError: return statement should be in a function')
                            sys.exit(1)
                        for module in reversed(glb.module_stack):
                            if not isinstance(module, function_module):
                                module.setEnd()
                            else:
                                break
                        return

                    # deal with "break" and "continue "statement:
                    #   similiar with "return" statement
                    if (tokens[0][1] == 'break') or (tokens[0][1] == 'continue'):
                        for module in reversed(glb.module_stack):
                            if not isinstance(module, loop_module):
                                module.setEnd()
                            else:
                                # first "not loop" module
                                module.setEnd()
                                if tokens[0][1] == 'continue':
                                    module.setContinue()
                                break
                        return

                    # if, while, and for
                    count = get_module_index(content, index)
                    module_content = [content[index+i] for i in range(1, count)]
                    exp = param_list[0]

                    if tokens[0][1] == 'if':
                        exps = [exp]
                        contents = [module_content]
                        index += count

                        # find better way to write this part, a little bit ugly
                        if index < len(content):
                            gramm_type, tokens, extoken, param_list = parser.parse(content[index],
                                    module)
                            while tokens[0][1] == 'else':
                                count = get_module_index(content, index)
                                contents.append([content[index+i] for i in range(1, count)])
                                index += count
                                exp = param_list[0]
                                exps.append(exp)
                                if index >= len(content):
                                    break
                                gramm_type, tokens, extoken, param_list = parser.parse(content[index],
                                        module)

                        glb.current_line+=1
                        ifModule = ifelsemodule(module.var_list,
                                                module.func_list,
                                                exps,
                                                contents,
                                                glb.current_line)
                        ifModule.run()
                        index -= count

                    elif tokens[0][1] == 'for':
                        glb.current_line+=1
                        forModule = formodule(module.var_list,
                                              module.func_list,
                                              exp,
                                              module_content,
                                              glb.current_line)
                        forModule.run()

                    elif tokens[0][1] == 'while':
                        # glb.current_line+=1
                        whileModule = whilemodule(module.var_list,
                                                  module.func_list,
                                                  exp,
                                                  module_content,
                                                  glb.current_line)
                        whileModule.run()
                    # elif tokens[0][1] == 'repeat':
                    #     pass

                    index += count

            recursive(content, index, module)
Exemplo n.º 33
0
import re, datetime, utils.parser



class Parser:
	regex = re.compile(r'#GPS:(\d+),(\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+),(\d+),(\d+)')
	def parse(self, string):
		m = self.regex.match(string)
		if not m:
			return

		millis = int(m.group(1))
		return (millis)
if __name__ == "__main__":
	parser = Parser()
	print parser.parse("hi")
	print parser.parse("#GPS:2136,12,37.462688,-122.274070,229.20,9,114")
Exemplo n.º 34
0
import re, datetime, utils.parser


class Parser:
    def __init__(self):
        self.type = "PRS"
        self.millis = 0
        self.temperature = 0
        self.pressure = 0

    def __str__(self):
        return str(self.millis) + "," + str(self.pressure) + "," + str(
            self.temperature) + '\n'

    regex = re.compile(r'#PRS:(\d+),([-+]?\d+\.\d+),([-+]?\d+\.\d+)')

    def parse(self, string):
        m = self.regex.match(string)
        if not m:
            return

        millis = int(m.group(1))
        self.temperature = float(m.group(2))
        self.pressure = float(m.group(3))
        return (self.millis, self.pressure, self.temperature)


if __name__ == "__main__":
    parser = Parser()
    print parser.parse("hi")
    print parser.parse("#PRS:182794,23.10,992.5100")
Exemplo n.º 35
0
 def setUp(self):
     self.test_list = parse("D:/Progs/IB/utils/9.2.3.Camellia.vectors1.txt")