Example #1
0
    def try_route(self, payment_request, route, routes, tried_routes):
        if self.route_is_invalid(route, routes):
            return False

        tried_routes.append(route)
        debug("")
        debug("Trying route #%d" % len(tried_routes))
        debug(Routes.print_route(route))

        response = self.lnd.send_payment(payment_request, route)
        is_successful = response.failure.code == 0
        if is_successful:
            debug("")
            debug("")
            debug("")
            debug("Success! Paid fees: %s sat (%s msat)" %
                  (route.total_fees, route.total_fees_msat))
            debug("Successful route:")
            debug(Routes.print_route(route))
            debug("")
            debug("")
            debug("")
            return True
        else:
            self.handle_error(response, route, routes)
            return False
Example #2
0
 def longest_edge(self):
     ret = Routes("", "", 0)
     for city in self.routes:
         for route in self.routes[city]:
             if route.dist > ret.dist:
                 ret = route
     return ret.info()
Example #3
0
    def rebalance(self):
        if self.last_hop_channel:
            debug(("Sending {:,} satoshis to rebalance to channel with ID %d" %
                   self.last_hop_channel.chan_id).format(self.amount))
        else:
            debug("Sending {:,} satoshis.".format(self.amount))
        if self.channel_ratio != 0.5:
            debug("Channel ratio used is %d%%" % int(self.channel_ratio * 100))
        if self.first_hop_channel:
            debug("Forced first channel has ID %d" %
                  self.first_hop_channel.chan_id)

        payment_request = self.generate_invoice()
        routes = Routes(self.lnd, payment_request, self.first_hop_channel,
                        self.last_hop_channel)

        self.initialize_ignored_channels(routes)

        tried_routes = []
        while routes.has_next():
            route = routes.get_next()

            success = self.try_route(payment_request, route, routes,
                                     tried_routes)
            if success:
                return True
        debug("Could not find any suitable route")
        return False
Example #4
0
def new_login():
    form = BlogForm(request.form)
    if request.method == 'POST' and form.validate():
        blog = Routes.response_sql()
        title = form.title.data
        author = form.author.data
        email = form.email.data
        password = md5(form.password.data).hexdigest()

        blog.title = title
        blog.author = author
        blog.email = email
        blog.password = password
        blog.update_date = datetime.datetime.now()
        blog.genre = 'first blog'

        a = Author(author, email, password)
        db.session.add(a)

        db.session.commit()

    return redirect(url_for('admin.default'))

    response = Routes.session_blog()
    return render_template("admin/login.html", blog=response)
Example #5
0
    def findShortestRoute(self, startNode, destinationName):
        # get routes to process by getting all out going links of the starting node
        candidateRoutes = Routes(
            [Route([link]) for link in startNode.getLinks()])
        minRoute = None

        # candiate routes are the routes to process.
        # they can be empty when no more routes can have distances less than the current minimum distance
        # or cannot find any more routes
        while candidateRoutes.len() > 0:
            # finished routes store the routes connecting start node and end node
            finishedRoutes = candidateRoutes.getRoutesEndedWithNode(
                destinationName)
            _minRoute = finishedRoutes.getMinDistance()
            minRoute = _minRoute if minRoute is None or (
                _minRoute is not None and
                minRoute.getDistance() > _minRoute.getDistance()) else minRoute

            # filter out routes having bigger distance than min route
            candidateRoutes = Routes([
                route for route in candidateRoutes.getRoutes()
                if minRoute is None
                or route.getDistance() < minRoute.getDistance()
            ])
            # all candidate routes go one step further. Store the new generated routes
            candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \
                            for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks() \
                            if not route.hasNode(link.child) or (link.child == destinationName and startNode.name == destinationName)])

        return minRoute
Example #6
0
def login():
    response = Routes.session_blog()

    form = AuthorForm(request.form)
    if request.method == 'POST' and form.validate():
        blog = Routes.response_sql()
        email = form.email.data
        password = md5(form.password.data).hexdigest()

        a = Author.query.filter_by(email=email, password=password)

        if a.count() == 1:
            a = a.first()

            session_response = {
                "id_blog": response['id_blog'],
                "author": a.name,
                "title": response['title'],
                "email": a.email
            }
            session['author'] = session_response
        elif a.count() <= 0:
            flash("User not found")
        else:
            flash("42")
    return redirect(url_for('admin.default'))
    return render_template("admin/login.html", blog=response)
Example #7
0
    def rebalance(self):
        debug("Sending %d satoshis to rebalance, remote pubkey: %s" %
              (self.amount, self.remote_pubkey))

        payment_request = self.generate_invoice()
        routes = Routes(self.lnd, payment_request, self.remote_pubkey)

        if not routes.has_next():
            debug("Could not find any suitable route")
            return None

        counter = 0
        while routes.has_next():
            counter += 1
            debug("Trying route #%d" % counter)

            route = routes.get_next()
            response = self.lnd.send_payment(payment_request, [route])
            is_successful = response.payment_error == ""

            if is_successful:
                fees_msat = response.payment_route.total_fees_msat
                fees_satoshi = round(float(fees_msat) / 1000.0, 3)
                debug("Success! Paid %d Satoshi in fees" % fees_satoshi)
                return response
            elif "TemporaryChannelFailure" in response.payment_error:
                debug("TemporaryChannelFailure (not enough funds along the route?)")
            else:
                debug("Error: %s" % response.payment_error)
        return None
Example #8
0
 def shortest_edge(self):
     ret = Routes("", "", 100000)
     for city in self.routes:
         for route in self.routes[city]:
             if route.dist < ret.dist:
                 ret = route
     return ret.info()
Example #9
0
    def rebalance(self):
        first_hop_alias_formatted = ""
        last_hop_alias_formatted = ""
        first_channel_id = 0
        if self.first_hop_channel:
            first_hop_alias_formatted = format_alias(
                self.lnd.get_node_alias(self.first_hop_channel.remote_pubkey))
            first_channel_id = format_channel_id(
                self.first_hop_channel.chan_id)
        if self.last_hop_channel:
            last_hop_alias_formatted = format_alias(
                self.lnd.get_node_alias(self.last_hop_channel.remote_pubkey))
        amount_formatted = format_amount(self.amount)
        if self.first_hop_channel and self.last_hop_channel:
            self.output.print_line(
                f"Sending {amount_formatted} satoshis from channel {first_channel_id} with {first_hop_alias_formatted} "
                f"back through {last_hop_alias_formatted}.")
        elif self.last_hop_channel:
            self.output.print_line(
                f"Sending {amount_formatted} satoshis back through {last_hop_alias_formatted}."
            )
        else:
            self.output.print_line(
                f"Sending {self.amount:,} satoshis from channel {first_channel_id} with {first_hop_alias_formatted}."
            )

        fee_limit_msat = self.get_fee_limit_msat()
        payment_request = self.generate_invoice()
        min_fee_last_hop = None
        if self.first_hop_channel:
            fee_rate_first_hop = self.lnd.get_ppm_to(
                self.first_hop_channel.chan_id)
            policy_first_hop = self.lnd.get_policy_to(
                self.first_hop_channel.chan_id)
            min_fee_last_hop = self.compute_fee(self.amount,
                                                fee_rate_first_hop,
                                                policy_first_hop)
        routes = Routes(self.lnd, payment_request, self.first_hop_channel,
                        self.last_hop_channel, fee_limit_msat, self.output)

        self.initialize_ignored_channels(routes, fee_limit_msat,
                                         min_fee_last_hop)

        tried_routes = []
        while routes.has_next():
            route = routes.get_next()

            success = self.try_route(payment_request, route, routes,
                                     tried_routes)
            if success:
                return True
        self.output.print_line("Could not find any suitable route")
        self.lnd.cancel_invoice(payment_request.payment_hash)
        return False
Example #10
0
    def rebalance(self):
        if self.last_hop_channel:
            debug("â’¾ Sending " + fmt.col_hi("{:,}".format(self.amount)) +
                  " satoshis to rebalance to channel with ID %s" %
                  fmt.col_lo(fmt.print_chanid(self.last_hop_channel.chan_id)))
        else:
            debug("â’¾ Sending " + fmt.col_hi("{:,}".format(self.amount)) +
                  " satoshis.")
        if self.channel_ratio != 0.5:
            debug("â’¾ Channel ratio used is " +
                  fmt.col_hi("%d%%" % int(self.channel_ratio * 100)))
        if self.first_hop_channel:
            debug("â’¾ Forced first channel has ID %s" %
                  fmt.col_lo(fmt.print_chanid(self.first_hop_channel.chan_id)))

        payment_request = self.generate_invoice()

        if self.path:
            myroute = self.lnd.build_route(self.path, self.amount,
                                           self.first_hop_channel.chan_id)
            if isinstance(myroute, Exception):
                debug("")
                debug(fmt.col_err("✘ " + myroute.details()))
                return False
            try:
                success = self.try_route(payment_request, myroute, [myroute],
                                         [])
                if success:
                    return True
            except:
                # since we use --path, myroute isn't a real Routes object
                # assume fees too high
                debug(fmt.col_err("✘ fees too high"))
                return False

        else:
            routes = Routes(self.lnd, payment_request, self.first_hop_channel,
                            self.last_hop_channel, self.deep)

            self.initialize_ignored_channels(routes)

            tried_routes = []
            while routes.has_next():
                route = routes.get_next()

                success = self.try_route(payment_request, route, routes,
                                         tried_routes)
                if success:
                    return True
        debug("")
        debug(fmt.col_err("✘ Could not find any suitable route"))
        return False
Example #11
0
    def test_get_cheapest_path(self):
        expected = deque(['GRU', 'BRC', 'SCL', 'ORL', 'CDG'])

        csv_content_mock = [['GRU', 'BRC', '10'], ['BRC', 'SCL', '5'],
                            ['GRU', 'CDG', '75'], ['GRU', 'SCL', '20'],
                            ['GRU', 'ORL', '56'], ['ORL', 'CDG', '53'],
                            ['ORL', 'CDG', '8'], ['SCL', 'ORL', '20']]

        routes = Routes(csv_content_mock)

        cheapest_path = routes.get_cheapest_path('GRU', 'CDG')

        assert cheapest_path == expected
Example #12
0
    def test_get_available_routes_graph(self):
        expected = [('GRU', 'BRC', '10'), ('BRC', 'SCL', '5'),
                    ('GRU', 'CDG', '75'), ('GRU', 'SCL', '20'),
                    ('GRU', 'ORL', '56'), ('ORL', 'CDG', '53'),
                    ('ORL', 'CDG', '8'), ('SCL', 'ORL', '20')]

        csv_content_mock = [['GRU', 'BRC', '10'], ['BRC', 'SCL', '5'],
                            ['GRU', 'CDG', '75'], ['GRU', 'SCL', '20'],
                            ['GRU', 'ORL', '56'], ['ORL', 'CDG', '53'],
                            ['ORL', 'CDG', '8'], ['SCL', 'ORL', '20']]

        routes = Routes(csv_content_mock)

        routes_graph = routes.get_available_routes_graph()

        assert routes_graph == expected
Example #13
0
    def start(self):
        csv_content = self.read_csv_file(sys.argv[1])

        routes = Routes(csv_content)

        goal = parse_origin_goal(goal=sys.argv[2])

        try:
            best_path = routes.get_cheapest_path(goal[0], goal[1])
        except csv.Error as e:
            sys.exit('%s' % (e))

        formatted_best_path = format_output(best_path)
        print('best route: ' + formatted_best_path)

        sys.exit(0)
class NearSegmentError:
    '''
    NearSegmentError divide all the points of sector to segments of existing in
    this sector edges and calculate distances to nearest point. if the distance
    is less than a constant, we add this point to segmet's route path.

    Methods:
        get_classes: returns a dictionary: key: class identifier; value: pair of points to be merged togather
        get_sectors: returns a dictionary: key: cell identifier; value: a list of Point instances for this cell
    '''
    
    _sectors = dict()
    _unsorted = list()
    _initial_edges = dict()
    _classes = dict()

    def __init__(self):
        self._routes = Routes()
        self._unsorted, self._initial_edges = self._routes.get_points_edges()
        self._set_classes_for_points()

    @timed
    def _map_to_sectors(self):
        for point in self._unsorted:
            if point.cell in self._sectors:
                self._sectors[point.cell].append(point)
            else:
                self._sectors[point.cell] = [point]
        return self._sectors

    @timed
    def _set_classes_for_points(self):
        sectors = self._map_to_sectors()

        for key,value in sectors.items():
            print('calculating classes for sector: {0}'.format(key))
            points_combinations = [item for item in combinations(value, 2)
                                        if (item[0].edge_id == item[1].edge_id) and
                                        self._routes.check_one_by_one_order_in_path(item[0],item[1])]
            for item in points_combinations:
                for point in value:
                    distance_point_segment = distance_point_line(point.x, point.y, item[0].x, item[0].y, item[1].x, item[1].y)
                    if distance_point_segment and distance_point_segment < MERGE_RADIUS:
                        self._classes[len(self._classes)] = {'segment':item, 'point': point}

    def get_classes(self):
        return self._classes
    def __init__(self):
        #self._routes = Routes('dump_routes_after_near_points_error_fix')
        self._routes = Routes()

        self._unsorted, self._initial_edges = self._routes.get_points_edges()

        #self._set_classes_for_points() #to calculate classes without disjoint set
        self._set_classes_for_points_disjoint_set()
Example #16
0
 def __init__(self, filename):
     self.graph = Graph()
     json_data = open(filename)
     data = json.load(json_data)
     for item in data['metros']:
         # print item['code']
         # parse unicode
         new_city = Metros(item['code'], item['name'], item['country'], item['continent'], item['timezone'],
                           item['coordinates'], item['population'], item['region'])
         self.graph.add_city(new_city)
     for item in data['routes']:
         new_route = Routes(item['ports'][0], item['ports'][1], item['distance'])
         # given file, use bidirectional edge
         if filename != "data.json":
             new_route2 = Routes(item['ports'][1], item['ports'][0], item['distance'])
             self.graph.add_route(new_route2)
         self.graph.add_route(new_route)
Example #17
0
def edit_post(id_post):
    blog = Routes.session_blog()
    post = Post.query.filter_by(id_post=id_post,
                                blog_id=blog['id_blog'],
                                status=1)
    if post.count() == 1:
        post = post.first()
        return render_template("admin/edit_post.html", **locals())
    return redirect(url_for('admin.default'))
Example #18
0
def read_profile(id_post):
    blog = Routes.session_blog()
    post = Post.query.filter_by(id_post=id_post,
                                blog_id=blog['id_blog'],
                                status=1)
    if post.count() == 1:
        post = post.first()
        post.content = Markup(markdown.markdown(post.content))
        return render_template("post.html", **locals())
    return redirect(url_for('default.default'))
Example #19
0
def edit_save_post(id_post):
    form = PostForm(request.form)
    if request.method == 'POST' and form.validate():
        blog = Routes.response_sql()
        title = form.title.data
        content = form.content.data
        p = Post.query.filter_by(id_post=id_post, status=1)
        if p.count() == 1:
            p.update(dict(title=title, content=content))
            db.session.commit()
    return redirect(url_for('admin.default'))
Example #20
0
def default():
    for t in Tag.query.all():
        print t.name

    response = Routes.session_blog()
    post = Post.query.filter_by(blog_id=response['id_blog'],
                                status=1).order_by(Post.id_post.desc()).all()
    if response['genre'] == 'tutorial':
        return render_template("admin/new_login_form.html", blog=response)
    elif 'author' in session:
        return render_template("admin/panel.html", blog=response, posts=post)
    return render_template("admin/login.html", blog=response)
Example #21
0
    def findRoutesWithMaxDistance(self, startNode, destinationName, distance):
        # get routes to process by getting all out going links of the starting node
        candidateRoutes = Routes(
            [Route([link]) for link in startNode.getLinks()])
        finishedRoutes = None
        # define filter function with partial function
        lessThanFilterFunc = partial(Dag.distanceLessThanFilter,
                                     distance=distance)
        while candidateRoutes.len() > 0:
            # finished routes store the routes connecting start node and end node
            finishedRoutes = candidateRoutes.getRoutesEndedWithNode(
                destinationName).filter(lessThanFilterFunc).merge(
                    finishedRoutes)
            # all candidate routes go one step further. Store the new generated routes
            candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \
                                for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks()])
            # filter out routes having more distance than the max distance
            candidateRoutes = Routes([
                route for route in candidateRoutes.getRoutes()
                if route.getDistance() < distance
            ])

        return finishedRoutes
    def rebalance(self):
        debug(("Sending {:,} satoshis to rebalance to channel with ID %d" %
               self.last_hop_channel.chan_id).format(self.amount))
        if self.channel_ratio != 0.5:
            debug("Channel ratio used is %d%%" % int(self.channel_ratio * 100))
        if self.first_hop_channel_id:
            debug("Forced first channel has ID %d" % self.first_hop_channel_id)

        payment_request = self.generate_invoice()
        routes = Routes(self.rpc, payment_request, self.first_hop_channel_id,
                        self.last_hop_channel)

        if not routes.has_next():
            debug("Could not find any suitable route")
            return None

        tried_routes = []
        while routes.has_next():
            route = routes.get_next()
            if self.route_is_invalid(route):
                continue
            tried_routes.append(route)

            debug("Trying route #%d" % len(tried_routes))
            debug(Routes.print_route(route))

            response = self.rpc.send_payment_sync(payment_request, [route])
            is_successful = response.payment_error == ""

            if is_successful:
                fees_msat = response.payment_route.total_fees_msat
                fees_satoshi = round(float(fees_msat) / 1000.0, 3)
                debug("Success! Paid %d Satoshi in fees" % fees_satoshi)
                debug("Tried routes:")
                debug("\n".join(
                    Routes.print_route(route) for route in tried_routes))
                debug("Successful route:")
                debug(Routes.print_route(route))

                return response
            elif "TemporaryChannelFailure" in response.payment_error:
                debug(
                    "TemporaryChannelFailure (not enough funds along the route?)"
                )
            else:
                debug("Error: %s" % response.payment_error)
        return None
Example #23
0
 def __init__(self, filename):
     self.graph = Graph()
     json_data = open(filename)
     data = json.load(json_data)
     for item in data['metros']:
         #print item['code']
         new_city = Metros(item['code'], item['name'], item['country'],
                           item['continent'], item['timezone'],
                           item['coordinates'], item['population'],
                           item['region'])
         self.graph.add_city(new_city)
     for item in data['routes']:
         new_route = Routes(item['ports'][0], item['ports'][1],
                            item['distance'])
         self.graph.add_route(new_route)
Example #24
0
    def try_route(self, payment_request, route, routes, tried_routes):
        tried_routes.append(route)

        if self.route_is_invalid(route):
            return False

        response = self.lnd.send_payment(payment_request, route)
        is_successful = response.failure.code == 0
        if is_successful:
            debug("Success in route #%d! Paid fees: %s sat (%s msat) %s" %
                  (len(tried_routes), route.total_fees,
                   route.total_fees_msat,
                   datetime.datetime.now().strftime("%Y-%m%d %H:%M:%S")))
            debug("Successful route: %s" % (Routes.print_route(route)))
            return True
        else:
            self.handle_error(response, route, routes)
            return False
Example #25
0
    def findRoutesWithMaxRound(self, startNode, destinationName, maxRound):
        # get routes to process by getting all out going links of the starting node
        candidateRoutes = Routes(
            [Route([link]) for link in startNode.getLinks()])
        count = 0
        finishedRoutes = None

        while candidateRoutes.len(
        ) > 0 and maxRound is not None and count < maxRound:
            # finished routes store the routes connecting start node and end node
            finishedRoutes = candidateRoutes.getRoutesEndedWithNode(
                destinationName).merge(finishedRoutes)
            # all candidate routes go one step further. Store the new generated routes
            candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \
                                for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks()])
            count = count + 1

        return finishedRoutes
Example #26
0
def save_post():
    form = PostForm(request.form)
    if request.method == 'POST' and form.validate():
        blog = Routes.response_sql()
        title = form.title.data
        content = form.content.data
        p = Post(
            title=title,
            content=content,
            blog_id=blog.id_blog,
            create_date=datetime.datetime.now(),
            update_date=datetime.datetime.now(),
            status=1,
        )
        if 'tag' in request.form.keys() and request.form['tag'] != '':
            tag_post = request.form['tag'].split()
            tag_list = list()
            for tag in tag_post:
                tag_query = Tag.query.filter_by(name=tag_post)
                if tag_query.count() == 1:
                    id_tag = tag_query.first().id_tag
                else:
                    tag_model = Tag(
                        name=tag,
                        create_date=datetime.datetime.now(),
                        update_date=datetime.datetime.now(),
                        status=1,
                    )
                    tag_list += [tag_model]
            if len(tag_list) > 0:
                db.session.add_all(tag_list)
                db.session.commit()
                id_tag = db.session.query(Tag).order_by(
                    Tag.id_tag.desc()).first().id_tag

            p.tag_id = id_tag

        db.session.add(p)
        db.session.commit()
    return redirect(url_for('admin.default'))
Example #27
0
 def __init__(self, host: str):
     self.routes = Routes(host)
Example #28
0
class Client:
    def __init__(self, host: str):
        self.routes = Routes(host)

    def get_authorized_user(self) -> UserInfo:
        result = self.routes.get_me()
        return get_user_info_from_json(result)

    def get_history(self) -> History:
        result = self.routes.get_me_history()
        return get_history_from_json(result)

    def pin_note(self, note: str, pin: bool):
        result = self.routes.put_me_history_note(
            note,
            HistoryUpdateObject(pin).to_json())

    def remove_note_from_history(self, note: str):
        result = self.routes.delete_me_history_note(note)

    def create_note(self, text: str, note: str = "") -> Note:
        if note == "":
            response = self.routes.post_notes(text)
        else:
            response = self.routes.post_notes_note(note, text)
        print(response)
        return get_note_from_json(response)

    def read_note_content(self, note: str) -> str:
        return self.routes.get_notes_note_content(note)

    def read_note(self, note: str) -> Note:
        note_json = self.routes.get_notes_note(note)
        return get_note_from_json(note_json)

    def read_note_metadata(self, note: str) -> NoteMetadata:
        metadata_json = self.routes.get_notes_note_metadata(note)
        return get_metadata_from_json(metadata_json)

    def delete_note(self, note: str):
        self.routes.delete_notes_note(note)

    def write_note(self, text: str, note: str) -> Note:
        response = self.routes.put_notes_note(note, text)
        return get_note_from_json(response)

    def write_note_metadata(self, note: str, note_metadata: NoteMetadata):
        result = self.routes.put_notes_note_metadata(note_metadata.to_json())

    def get_revisions(self, note: str) -> [NoteRevisionMetadata]:
        response = self.routes.get_notes_note_revisions(note)
        return get_revisions_metadatas_from_json(response)

    def get_revision(self, note: str, revision_id: int) -> NoteRevision:
        response = self.routes.get_notes_note_revisions_revision(
            note, revision_id)
        return get_revision_from_json(response)

    def get_monitoring(self) -> ServerStatus:
        response = self.routes.get_monitoring()
        return get_serverstatus_from_json(response)

    def get_monitoring_prometheus(self) -> PrometheusData:
        response = self.routes.get_monitoring_prometheus()
        return get_prometheus_from_json(response)

    def upload_media(self, file: str):
        self.routes.post_media_upload(file)
class NearPointsError:
    '''
    NearPointsError class splits all the points to sectors and then calculate euclidean
    distance for each pair of points in class. if the distance is less than 1 meter
    new point class is created.

    Methods:
        get_classes: returns a dictionary: key: class identifier; value: pair of points to be merged togather
        get_sectors: returns a dictionary: key: cell identifier; value: a list of Point instances for this cell
    '''
    _sectors = dict()
    _unsorted = list()
    _routes = dict()
    _initial_edges = dict()
    _classes = dict()
    _result_ds = list()

    def __init__(self):
        #self._routes = Routes('dump_routes_after_near_points_error_fix')
        self._routes = Routes()

        self._unsorted, self._initial_edges = self._routes.get_points_edges()

        #self._set_classes_for_points() #to calculate classes without disjoint set
        self._set_classes_for_points_disjoint_set()

    def _find(self, sector, el):
        for point in sector:
            if el in point:
                return sector.index(point)

    def _union(self, sector, el1, el2):
        index_element_1 = self._find(sector, el1)
        index_element_2 = self._find(sector, el2)
        if index_element_1 != index_element_2:
            sector[index_element_2] = sector[index_element_1]+sector[index_element_2]
            del sector[index_element_1]
        return sector

    @timed
    def _map_to_sectors(self):
        for point in self._unsorted:
            if point.cell in self._sectors:
                self._sectors[point.cell].append(point)
            else:
                self._sectors[point.cell] = [point]
        return self._sectors

    @timed
    def _map_to_sectors_disjoint_set(self):
        for point in self._unsorted:
            if point.cell in self._sectors:
                self._sectors[point.cell].append([point])
            else:
                self._sectors[point.cell] = [[point]]
        return self._sectors

    @timed
    def _set_classes_for_points(self):
        sectors = self._map_to_sectors()

        for key,value in sectors.items():
            print('calculating classes for sector: {0}'.format(key))
            for item in combinations(value, 2):
                distance_between_points = euclidean([item[0].x, item[0].y], [item[-1].x, item[-1].y])
                if distance_between_points < MERGE_RADIUS and item[0].hierarchy == item[-1].hierarchy:

                    self._classes[len(self._classes)] = item
                    #item[0].set_crossroad(item[-1])
                    #item[-1].set_crossroad(item[0])

    @timed
    def _set_classes_for_points_disjoint_set(self):
        sectors = self._map_to_sectors_disjoint_set()
        print(len(sectors))
        for key,value in sectors.items():
            sector = value
            print('calculating classes for sector: {0}'.format(key))
            for item in combinations(value, 2):
                distance_between_points = euclidean([item[0][0].x, item[0][0].y], [item[-1][0].x, item[-1][0].y])
                if distance_between_points < MERGE_RADIUS and item[0][0].hierarchy == item[-1][0].hierarchy:
                    sector = self._union(sector, item[0][0], item[-1][0])
            self._result_ds.extend(sector)

    # this func is just for testing reasons (resetting all the classes to check correct error handling)
    @timed
    def reset_classes_for_points_disjoint_set(self):
        self._unsorted, self._initial_edges = self._routes.get_points_edges()
        self._result_ds = []
        self._sectors = {}
        sectors = self._map_to_sectors_disjoint_set()
        print(len(sectors))
        for key,value in sectors.items():
            sector = value
            print('calculating classes for sector: {0}'.format(key))
            for item in combinations(value, 2):
                distance_between_points = euclidean([item[0][0].x, item[0][0].y], [item[-1][0].x, item[-1][0].y])
                if distance_between_points < MERGE_RADIUS and item[0][0].hierarchy == item[-1][0].hierarchy:
                    sector = self._union(sector, item[0][0], item[-1][0])
            self._result_ds.extend(sector)

    def get_classes(self):
        return self._classes

    def save_to_file(self, filename='dump_near_points_error_final_as_dict'):
        sys.setrecursionlimit(1000000)
        result = {'routes':self._routes._routes, 'edges':self._routes._edges, 'points':self._routes._points}
        pickle.dump(result, open(filename, 'wb'))

    def merge_points(self, arr):
        self._routes.split_edges(new_point=arr[0], points_arr=arr)

    def get_sectors(self):
        return self._sectors

    def get_ds_result(self):
        return self._result_ds
 def __init__(self):
     self._routes = Routes()
     self._unsorted, self._initial_edges = self._routes.get_points_edges()
     self._set_classes_for_points()
Example #31
0

def test_hub_cities():
    print CSAirline.graph.find_hub_city(3)
    return


def test_launch_map():
    CSAirline.graph.launch_map()
    return


if __name__ == "__main__":
    SCL = Metros('SCL', 'Santiago', 'CL', 'South America', -4, {"S": 33, "W": 71}, 6000000, 1)
    LIM = Metros('LIM', 'Lima', 'PE', 'South America', -5, {"S": 12, "W": 77}, 9050000, 1)
    route1 = Routes('SCL', 'LIM', 2453)
    graph = Graph()
    graph.add_city(SCL)
    graph.add_city(LIM)
    graph.add_route(route1)
    print SCL.info()
    graph.print_cities()
    graph.remove_city('SCL')
    print "Removing SCL"
    graph.print_cities()

    graph.add_city(SCL)
    CSAirline = Airline('test_data.json')
    test_longest_edge()
    test_shortest_edge()
    test_average_distance()
class IntersectionsError:
    '''
    IntersectionsError class splits all the points to sectors and then check if
    any sector in class intersects with another. if we detect this intersection,
    we create a new point in this intersection

    Methods:
        get_classes: returns a dictionary: key: class identifier; value: pair of points to be merged togather
        get_sectors: returns a dictionary: key: cell identifier; value: a list of Point instances for this cell
    '''
    _sectors = dict()
    _unsorted = list()
    _initial_edges = dict()
    _classes = dict()

    def __init__(self):
        #self._routes = self._get_from_file("dump_routes_after_near_points_error_fix")
        self._routes = Routes("dump_near_points_error_final_as_dict")
        self._unsorted, self._initial_edges = self._routes.get_points_edges()
        self._set_classes_for_points()

    @timed
    def _get_from_file(self, filename):
        return pickle.load(open(filename,'rb'))

    def _find(self, sector, el):
        for point in sector:
            if el in point:
                return sector.index(point)

    def _union(self, sector, el1, el2):
        index_element_1 = self._find(sector, el1)
        index_element_2 = self._find(sector, el2)
        if index_element_1 != index_element_2:
            sector[index_element_2] = sector[index_element_1]+sector[index_element_2]
            del sector[index_element_1]
        return sector

    @timed
    def _map_to_sectors(self):
        for point in self._unsorted:
            if point.cell in self._sectors:
                self._sectors[point.cell].append(point)
            else:
                self._sectors[point.cell] = [point]
        return self._sectors

    @timed
    def _set_classes_for_points(self):
        sectors = self._map_to_sectors()

        for key,value in sectors.items():
            print('calculating classes for sector: {0}'.format(key))
            points_combinations = [item for item in combinations(value, 2)
                                        if (
                                        self._routes.check_one_by_one_order_in_path(item[0],item[1])) ]
            for item in combinations(points_combinations, 2):
                intersection = segment_intersection(item[0], item[-1])
                if intersection and item[0][0].hierarchy == item[-1][0].hierarchy: # and item[0][0].edge_id != item[1][0].edge_id: if only different edge can intersect
                    self._classes[len(self._classes)] = item

    # this func is just for testing reasons (resetting all the classes to check correct error handling)
    @timed
    def reset_classes_for_points(self):
        self._unsorted, self._initial_edges = self._routes.get_points_edges()
        self._sectors = {}
        sectors = self._map_to_sectors()
        for key,value in sectors.items():
            points_combinations = [item for item in combinations(value, 2)
                                        if (item[0].edge_id == item[1].edge_id) and
                                        self._routes.check_one_by_one_order_in_path(item[0],item[1])]
            for item in combinations(points_combinations, 2):
                intersection = segment_intersection(item[0], item[-1])
                if intersection and item[0][0].hierarchy == item[0][1].hierarchy: # and item[0][0].edge_id != item[1][0].edge_id: if only different edge can intersect
                    self._classes[len(self._classes)] = item

    def get_classes(self):
        return self._classes

    def fix_intersection(self, segment1, segment2):
        self._routes.fix_intersection(segment1, segment2)
Example #33
0
# limitations under the License.

from flask import Flask

from count import count
from locations import locations
# from clusters import clusters
from routes import Routes

blueprints = (
    (count.create_blueprint(), '/services/admin/users/count'),
    (locations.create_blueprint(), '/services/admin/users/locations'),
)
# (clusters.create_blueprint(), '/services/admin/users/clusters'),)

base_routes = Routes()


class Eclipse2017AdminApp(Flask):
    """
    Eclipse 2017 application.
    """
    def __init__(self,
                 project_id,
                 session_enc_key,
                 google_oauth2_client_id,
                 google_oauth2_client_secret,
                 debug=False,
                 blueprints=blueprints,
                 routes=base_routes,
                 count=count,
 def __init__(self):
     #self._routes = self._get_from_file("dump_routes_after_near_points_error_fix")
     self._routes = Routes("dump_near_points_error_final_as_dict")
     self._unsorted, self._initial_edges = self._routes.get_points_edges()
     self._set_classes_for_points()
Example #35
0
from routes import Routes

app = Flask(__name__)

bcrypt = Bcrypt(app)
api = Api(app)
config = AppConfig(app)


# migrations
@app.before_first_request
def create_table():
    try:
        DB.create_all()
    except ValueError:
        print(ValueError)


# /auth
jwt = JWT(app=app,
          authentication_handler=authenticate,
          identity_handler=identity)

# routes
routes = Routes(api)

if __name__ == '__main__':
    DB.init_app(app)
    app.run(port=config.PORT, host=config.HOST, debug=True)
Example #36
0
from models import app
from routes import Routes

Routes()

if __name__ == '__main__':
    app.run()
Example #37
0
import sys
import os
import webapp2
from routes import Routes

sys.path.append(os.path.join(os.path.dirname(__file__), 'libs'))

app = webapp2.WSGIApplication(Routes.get_routes(), debug=True)