def test_classify_success(self):
        tree = Node("Gender",
                ("Female", Node("Bush Approval",
                        ("Approve", Label("McCain")),
                        ("Disapprove", Label("Obama")))),
                ("Male", Node("Ideology",
                        ("Liberal", Label("Obama")),
                        ("Moderate", Label("Obama")),
                        ("Conservative", Label("McCain")))))

        c = tree.classify(['Female', 'Approve', None],
                          ['Gender', 'Bush Approval', 'Ideology'])
        self.assertEqual(c, 'McCain')
        # same but dif order of features
        c = tree.classify(['Approve', 'Female', None],
                          ['Bush Approval', 'Gender', 'Ideology'])
        self.assertEqual(c, 'McCain')
        # dif last node
        c = tree.classify(['Disapprove', 'Female', None],
                          ['Bush Approval', 'Gender', 'Ideology'])
        self.assertEqual(c, 'Obama')
        # dif first node
        c = tree.classify([None, 'Male', 'Liberal'],
                          ['Bush Approval', 'Gender', 'Ideology'])
        self.assertEqual(c, 'Obama')
Example #2
0
    def _parse(self, entries, skip_self):
        nodes = entries['Node']
        if not skip_self:
            nodes += entries['Self']

        for node in nodes:
            n = Node(node.hostname, node.latlon)
            n.add_ip(node.ip)
            self._nodes[node.hostname] = n
            self._ips[node.ip] = n

        mids_unknown = []
        for mid in entries['Mid']:
            try:
                node = self._ips[mid.ip]
                node.add_ip(mid.alias)
                self._ips[mid.alias] = node
            except KeyError:
                mids_unknown.append(mid)

        links = entries['Link'] + entries['PLink']
        links_unknown = []
        for link in links:
            try:
                if skip_self:
                    link_ips = [link.src_ip, link.dst_ip]
                    if any(s.ip in link_ips  for s in entries['Self']):
                        continue

                node = self._ips[link.src_ip]
                l = Link(self._ips[link.dst_ip], link.lq)
                node.add_link(l)
            except KeyError:
                links_unknown.append(link)
def partition_swap_node(start_pre_node: Node, start_node: Node,
                        end_node: Node) -> Node:
    """
    将这段链表进行分区(交换节点的方式)

    说明:
        范围是左闭右开[start_node, end_node)

    :param start_pre_node: 开始节点的父节点
    :param start_node: 开始节点
    :param end_node: 结束节点
    """
    little_head_node, big_head_node = Node(0), Node(0)
    little_node, big_node = little_head_node, big_head_node
    current_node = start_node.next_node
    while current_node and current_node != end_node:
        if current_node < start_node:
            little_node.next_node = current_node
            little_node = current_node
        else:
            big_node.next_node = current_node
            big_node = current_node

        current_node = current_node.next_node

    little_node.next_node = start_node
    big_node.next_node = end_node
    start_node.next_node = big_head_node.next_node
    start_pre_node.next_node = little_head_node.next_node

    return start_node
Example #4
0
 def get(self, name):
     name = name.lower()
     m_obj = Member.get_by_key_name(name)
     if m_obj:
         self.echo(
             'member.html',
             {
                 'title':
                 m_obj.name,
                 'm_obj':
                 m_obj,
                 'mi_obj':
                 MemberInfo.get_by_key_name(name),
                 'newest_node':
                 Node.get_newest(),
                 #'recent_node': Node.get_recent_node(),
                 'hot_node':
                 Node.get_hot_node(),
                 'recent_topic_objs':
                 KeyStrValue.get_topic_key_title('recent-topic-home'),
                 'reply_topic_objs':
                 KeyStrValue.get_topic_key_title('recent-reply-topic'),
             },
             layout='_layout.html')
     else:
         self.error(404)
         self.echo(
             'error.html', {
                 'page': '404',
                 'title': "Can't find out this URL",
                 'h2': 'Oh, my god!',
                 'msg': 'Something seems to be lost...'
             })
         return
Example #5
0
def match_entity(sentence):
    result = []
    for wh_phrase in wh_phrases:
        if sentence.startswith(wh_phrase):
            result.append(Node(wh_phrase, 'wh', []))
            sentence = sentence[len(wh_phrase) + 1:]
    keywords = ''
    is_start = False
    start_index = -1
    for end_index, w in se_keywords.iter(sentence):
        keywords = keywords + ' ' + w
        if not is_start:
            start_index = end_index - len(w) + 1
            is_start = True
    if start_index != -1:
        word = sentence[start_index:len(sentence)]
        rule_candidates = sparql.full_text_search(word)
        if rule_candidates:
            result.append(Node(word, 'Rule', rule_candidates))
    for i, w in entity_dic.iter(sentence):
        if not is_repeat(result, w.word):
            result.append(Node(w.word, 'Name', w.entity))
    for i, w in class_dic.iter(sentence):
        if not is_repeat(result, w.word):
            result.append(Node(w.word, 'OntologyType', [w.entity]))
    return result
Example #6
0
 def get(self, nodeid):
     n_obj = Node.get_by_id(int(nodeid))
     if not n_obj:
         self.error(404)
         self.echo('error.html', {
             'page': '404',
             'title': "Can't find out this URL",
             'h2': 'Oh, my god!',
             'msg': 'Something seems to be lost...'
         })
         return
     
     from_id = int(self.request.get('id', '0'))
     if from_id<=0 and n_obj.count:
         from_id = n_obj.count
     
     to_id = from_id - EACH_PAGE_POST_NUM
     if to_id<0:
         to_id = 0
     
     newest_node = Node.get_newest()
     self.echo('nodedetail.html', {
         'title': n_obj.name,
         'n_obj': n_obj,
         'from_id': from_id,
         'to_id': to_id,
         'topic_objs': Node.get_page_topic(nodeid, from_id, to_id),
         'newest_node': newest_node,
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         'recent_topic_objs': KeyStrValue.get_topic_key_title('recent-topic-home'),
         'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'),            
     }, layout='_layout.html')
     if len(newest_node)==10:
         KeyStrValue.add_node_key(nodeid)
Example #7
0
def getNumLeafs(n):
    numLeafs = 0
    if isinstance(n.children, list):
        newNode = Node()
        template = '\n'.join(
            [' '.join(child.logTemplate) for child in n.children])
        template = "\n".join(
            textwrap.wrap(template,
                          width=15,
                          expand_tabs=True,
                          replace_whitespace=False,
                          break_long_words=True))
        # for node in n.children[1:]:
        #     template = getTemplate(template,node.logTemplate)
        # token = ' '.join(template)
        newNode.token = template
        n.children = {template: newNode}
    for c in n.children:
        if isinstance(c, list):
            numLeafs += 1
            continue
        current = n.children.get(c)
        if current.children != {}:
            numLeafs += getNumLeafs(current)
        else:
            numLeafs += 1
    return numLeafs
Example #8
0
class Cruncher(object):
    """XML Cruncher!!!"""
    
    current_node = None
    nodes = []
    
    def start(self, tag, attrib):
        """parser enters tag"""
        
        if tag == "node":
            self.current_node = Node(attrib["id"], attrib["lat"], attrib["lon"])
        elif tag == "tag" and self.current_node:
            self.current_node.add_tag(attrib["k"], attrib["v"])
    
    def end(self, tag):
        """parses end tags"""
        if tag == "node" and self.current_node:
            if self.current_node.tags.has_key("name"):
                self.nodes.append(self.current_node)
            self.current_node = None
    
    def close(self):
        """parser closes"""
        nodes, self.nodes = self.nodes, []
        return nodes
Example #9
0
def best_first_graph_search(problem, f=lambda node: node.depth):
    frontier = utils.PriorityQueue(min, f)

    explored = utils.PriorityQueue(min, f)
    frontier.append(Node(problem.initial))

    while frontier:
        children = utils.PriorityQueue(min, f)
        node = frontier.pop()
        if problem.goal_test(node[0].state):
            return node[0]
        explored.append(node[0])
        children.extend(node[0].expand(problem))

        for x in children.A:
            existing = 0
            for y in explored.A:
                if x[1] == y[1]:
                    existing = 1
                    break

            if existing == 0:
                for z in frontier.A:
                    if x[1] == z[1]:
                        existing = 1
                        if x[0] < z[0]:
                            frontier.A.__delitem__(frontier.A.index(z))
                            frontier.append(x[1])
                        break

                if existing == 0:
                    frontier.append(x[1])

    return Node('fail')
Example #10
0
 def get(self):
     n_id = str(self.get_argument('id',''))
     self.echo('addnode.html', {
         'title': "添加分类",
         'n_obj':Node.get_by_id_for_edit(n_id),
         'newest_node': Node.get_newest(),
     }, layout='_layout.html')
Example #11
0
 def get(self):
     n_id = str(self.get_argument('id', ''))
     self.echo('addnode.html', {
         'title': "添加分类",
         'n_obj': Node.get_by_id_for_edit(n_id),
         'newest_node': Node.get_newest(),
     },
               layout='_layout.html')
 def combineLeaves(self, nodeList):
     res = []
     ans = Node()
     template = nodeList[0].logTemplate
     for node in nodeList[1:]:
         template = getTemplate(template, node.logTemplate)
     ans.token = template
     res.append([ans])
     return res
Example #13
0
 def get(self):
     self.echo('home.html', {
         'title': "首页",
         'topic_objs': KeyStrValue.get_topic_objs('recent-topic-home'),
         'site_counts': Counter.get_site_counts(),
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'),
     }, layout='_layout.html')
Example #14
0
    def get(self, nodeid, topicid):
        t_key = 't-%s-%s' % (str(nodeid), str(topicid))
        t_obj = Topic.get_by_key(t_key)
        if not t_obj or t_obj.has_key('hide'):
            self.set_status(404)
            self.echo(
                'error.html', {
                    'page': '404',
                    'title': "Can't find out this URL",
                    'h2': 'Oh, my god!',
                    'msg': 'Something seems to be lost...'
                })

            return
        if t_obj['cnum']:
            cnum = int(t_obj['cnum'])
            from_id = int(self.get_argument('id', '1'))
            if from_id > 1 and from_id % EACH_PAGE_COMMENT_NUM != 1:
                self.redirect('/' + t_key)
                return
            to_id = from_id + EACH_PAGE_COMMENT_NUM - 1
            if to_id > cnum:
                to_id = cnum
            c_objs = Comment.get_comments(t_key, from_id, to_id)
        else:
            c_objs = None
            from_id = to_id = cnum = 0

        self.echo('topicdetail.html', {
            'title':
            t_obj['title'] + ' - ' + t_obj['nodename'],
            'description':
            'description',
            't_obj':
            t_obj,
            'c_objs':
            c_objs,
            'from_id':
            from_id,
            'to_id':
            to_id,
            'cnum':
            cnum,
            'newest_node':
            Node.get_newest(),
            'recent_node':
            Node.get_recent_node(),
            'hot_node':
            Node.get_hot_node(),
            'recent_topic_objs':
            Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
            'comment_topic_objs':
            Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),
        },
                  layout='_layout.html')
Example #15
0
def initialize_matrix(x, y):
    mat = []
    for i in range(len(y) + 1):
        mat.append([Node(i, False)])

    val = 0
    for i in range(len(x)):
        val += (1 if get_type(x[i]) == 'tag' else 0)
        mat[0].append(Node(val, True))

    return mat
Example #16
0
class MongoLoader(object):
    """Parses xml nodes and inserts appropriate ones to db"""

    def __init__(self, collection_name, named_only, tag_names):
        """C'tor sets tag wich we'll count"""
        self._named_only = named_only
        
        if isinstance(tag_names, list):
            self._tag_names = set(tag_names)
        else:
            self._tag_names = set([tag_names])
        
        self._current_node = None
        self._count = 0
        
        conn = Connection('localhost', 27017)
        db = conn['openstreet']
        self._ca = db[collection_name]
    
    def start(self, tag, attrib):
        """parser enters tag"""
        
        if tag == "node":
            # Start node writing
            self._current_node = Node(  float(attrib["lat"]),
                                        float(attrib["lon"]),
                                        id = int(attrib["id"])  )
        elif tag == "tag" and self._current_node:
            # Add tag to node
            self._current_node.add_tag(attrib["k"], attrib["v"])
    
    def end(self, tag):
        """parser reached end of tag"""
        if tag == "node" and self._current_node:
            
            # tags_set = set(self._current_node.tags)
            # if not self._named_only or "name" in tags_set:
            #     if len(tags_set & self._tag_names):
            #         self._save_node()
            
            if "name" in self._current_node.tags:
                self._save_node()
                
            self._current_node = None
    
    def close(self):
        """parser closes"""
        count, self._count = self._count, 0
        return count
    
    def _save_node(self):
        """Save current_node to list"""
        self._ca.insert(self._current_node)
        self._count += 1
Example #17
0
 def get(self):
     cur_user = self.cur_user()
     
     self.echo('notifications.html', {
         'title': "站内提醒",
         'topic_objs': Commomkvdb.get_notifications(cur_user['name'], cur_user.get('notic', '')),
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'recent_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
         'comment_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),                
     }, layout='_layout.html')        
Example #18
0
 def get(self):
     self.echo('home.html', {
         'title': "首页",
         'topic_objs': Commomkvdb.get_topic_by_keys('recent-topic-home'),
         'site_counts': Count.get_site_counts(),
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         #'recent_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
         'comment_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),
     }, layout='_layout.html')
    def test_classify_invalid_value_for_column(self):
        tree = Node("Gender",
                ("Female", Node("Bush Approval",
                        ("Approve", Label("McCain")),
                        ("Disapprove", Label("Obama")))),
                ("Male", Node("Ideology",
                        ("Liberal", Label("Obama")),
                        ("Moderate", Label("Obama")),
                        ("Conservative", Label("McCain")))))

        result = tree.classify(['Male', -1, -1], ['Gender', 'Ideology', 'c'])
        self.assertIsNone(result)
Example #20
0
    def get(self, nodeid, topicid):
        topic_key = '%s-%s' % (nodeid, topicid)
        t_obj = Topic.get_by_key_name(topic_key)
        if not t_obj:
            self.error(404)
            self.echo(
                'error.html', {
                    'page': '404',
                    'title': "Can't find out this URL",
                    'h2': 'Oh, my god!',
                    'msg': 'Something seems to be lost...'
                })
            return

        if t_obj.cnum:
            from_id = int(self.request.get('id', '1'))
            if from_id > 1 and from_id % EACH_PAGE_COMMENT_NUM != 1:
                self.redirect('/t-' + topic_key)
                return
            to_id = from_id + EACH_PAGE_COMMENT_NUM - 1
            if to_id > t_obj.cnum:
                to_id = t_obj.cnum
            c_objs = Comment.get_comments(topic_key, from_id, to_id)
        else:
            c_objs = None
            from_id = to_id = cnum = 0

        self.echo(
            'topicdetail.html',
            {
                'title':
                t_obj.title + ' - ' + t_obj.node.name,
                'description':
                'description',
                't_obj':
                t_obj,
                'c_objs':
                c_objs,
                'from_id':
                from_id,
                'to_id':
                to_id,
                'newest_node':
                Node.get_newest(),
                #'recent_node': Node.get_recent_node(),
                'hot_node':
                Node.get_hot_node(),
                'recent_topic_objs':
                KeyStrValue.get_topic_key_title('recent-topic-home'),
                'reply_topic_objs':
                KeyStrValue.get_topic_key_title('recent-reply-topic'),
            },
            layout='_layout.html')
Example #21
0
    def start(self, tag, attrib):
        """parser enters tag"""

        from model import Node

        if tag == "node":
            # Start node writing
            self._current_node = Node(float(attrib["lat"]),
                                      float(attrib["lon"]),
                                      id=int(attrib["id"]))
        elif tag == "tag" and self._current_node:
            # Add tag to node
            self._current_node.add_tag(attrib["k"], attrib["v"])
Example #22
0
 def get(self):
     if self.cur_user:
         self.echo('notifications.html', {
             'title': "站内提醒",
             'newest_node': Node.get_newest(),
             'recent_node': Node.get_recent_node(),
             'hot_node': Node.get_hot_node(),
             'recent_topic_objs': KeyStrValue.get_topic_key_title('recent-topic-home'),
             'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'),            
         }, layout='_layout.html')
         
     else:
         self.redirect('/')
Example #23
0
 def get(self, nodeid='1'):
     n_obj = Node.get_by_key('n-'+str(nodeid))
     if not n_obj:
         self.set_status(404)
         self.write('404')
         return
     self.echo('newpost.html', {
         'title': "发新帖子",
         'errors':[],
         'n_obj': n_obj,
         't_obj': TOPIC_DICT.copy(),
         'newest_node': Node.get_newest(),
     }, layout='_layout.html')
Example #24
0
 def get(self, nodeid='1'):
     n_obj = Node.get_by_key('n-' + str(nodeid))
     if not n_obj:
         self.set_status(404)
         self.write('404')
         return
     self.echo('newpost.html', {
         'title': "发新帖子",
         'errors': [],
         'n_obj': n_obj,
         't_obj': TOPIC_DICT.copy(),
         'newest_node': Node.get_newest(),
     },
               layout='_layout.html')
def build_query_graph():
    global dependency_tree
    dependency_tree = nlp.dependency_parse(question)
    convert_tree()
    edges = []
    # 2-2 find path
    if len(nodes) == 1:
        n = nodes[0]
        pre_attribute = ' '.join(tokens[:n.index - 1])
        edges.append(Edge(pre_attribute, Node('', 'wh', []), n))
        return edges
    for n in nodes:
        node_indexes.append(n.index)
    for i in range(len(node_indexes) - 1):
        n1 = node_indexes[i]
        for j in range(i + 1, len(node_indexes)):
            n2 = node_indexes[j]
            path = find_path(tree_node_dic[n1], tree_node_dic[n2])
            if path:
                word = ""
                for k in range(1, len(path)):
                    for value in tree_node_dic[path[k]].values:
                        word = word + tokens[value - 1] + ' '
                edges.append(Edge(word.strip(), index_node_dic[n1], index_node_dic[n2]))
    return edges
Example #26
0
    def get(self):
        if USER_MODEL == 2:
            self.redirect('/sigin')
            return

        open_id = str(self.get_cookie('open_id', ''))
        access_token = str(self.get_cookie('access_token', ''))

        if open_id and access_token:
            access_token_in_kvdb = kv.get('qq_' + str(open_id))
            if access_token_in_kvdb == access_token:
                pass
            else:
                self.redirect('/sigin')
                return
        else:
            self.redirect('/sigin')
            return

        self.echo('setname.html', {
            'title': "设置名字",
            'errors': [],
            'name': '',
            'newest_node': Node.get_newest(),
        },
                  layout='_layout.html')
Example #27
0
 def start(self, tag, attrib):
     """parser enters tag"""
     
     if tag == "node":
         self.current_node = Node(attrib["id"], attrib["lat"], attrib["lon"])
     elif tag == "tag" and self.current_node:
         self.current_node.add_tag(attrib["k"], attrib["v"])
Example #28
0
 def get(self): 
     if USER_MODEL==2:
         self.redirect('/sigin')
         return
     
     open_id = str(self.get_cookie('open_id',''))
     access_token = str(self.get_cookie('access_token',''))
     
     if open_id and access_token:
         access_token_in_kvdb = kv.get('qq_' +str(open_id))
         if access_token_in_kvdb == access_token:
             pass
         else:
             self.redirect('/sigin')
             return
     else:
         self.redirect('/sigin')
         return
     
     self.echo('setname.html', {
         'title': "设置名字",
         'errors':[],
         'name':'',
         'newest_node': Node.get_newest(),
     }, layout='_layout.html')
def selection_sort_swap_value(link: Link) -> Link:
    """
    选择排序

    不交换节点,只交换值的方式
    """
    head_node = Node(0, link.head_node)
    start_node = head_node.next_node
    while start_node:
        swap_node = None
        current_node = start_node.next_node
        while current_node:
            if current_node < start_node:
                if not swap_node or swap_node > current_node:
                    swap_node = current_node

            current_node = current_node.next_node

        if swap_node:
            swap_node.element, start_node.element = start_node.element, swap_node.element

        start_node = start_node.next_node

    link.head_node = head_node.next_node
    return link
Example #30
0
    def get(self, node_id):

        urlstr = """<url><loc>%s</loc><changefreq>%s</changefreq><priority>%s</priority></url>\n """
        urllist = []
        urllist.append('<?xml version="1.0" encoding="UTF-8"?>\n')
        urllist.append(
            '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n')

        n_obj = Node.get_by_key('n-' + str(node_id))
        if n_obj:
            max_id = int(n_obj['count'])
            to_id = max_id - 40000
            if to_id < 0:
                to_id = 0
            for i in xrange(max_id, to_id, -1):
                urllist.append(urlstr %
                               ("%s/t-%s-%d" %
                                (BASE_URL, str(node_id), i), 'weekly', '0.5'))

            urllist.append('</urlset>')

            self.set_header('Content-Type', 'text/xml')
            self.write(''.join(urllist))
        else:
            self.write('')
def optimize_insertion_sort(link: Link) -> Link:
    """
    优化插入排序

    测试:
        链表的长度为 1000 --- Total execution time: 235 ms
        链表的长度为 10000 --- Total execution time: 23785 ms

    leetcode:
        link: https://leetcode-cn.com/problems/insertion-sort-list/
        执行用时: 220 ms, 在所有 Python3 提交中击败了 74.71% 的用户
        内存消耗: 15.3 MB, 在所有 Python3 提交中击败了 12.50% 的用户
    """
    head_node = Node(0, link.head_node)

    current_node = link.head_node
    while current_node and current_node.next_node:
        if current_node <= current_node.next_node:
            current_node = current_node.next_node
            continue

        start_node = head_node
        while start_node.next_node < current_node.next_node:
            start_node = start_node.next_node

        temp_node = current_node.next_node
        current_node.next_node = temp_node.next_node
        temp_node.next_node = start_node.next_node
        start_node.next_node = temp_node

    link.head_node = head_node.next_node
    return link
Example #32
0
    def post(self):
        name = str(self.get_argument('name', '').lower().encode('utf-8'))
        pw = self.get_argument('pw', '')

        errors = []
        if name and pw:
            if len(name) < 20 or len(pw) < 20:
                if re.search('^[a-zA-Z0-9]+$', name):
                    pwmd5 = md5(pw.encode('utf-8')).hexdigest()
                    u_obj = kv.get('m-' + name)
                    if u_obj:
                        member_dict = decode_dict(u_obj)
                        if pwmd5 == member_dict['code'] and int(
                                member_dict['flag']) >= 1:
                            #set sr_code
                            code_list = [member_dict['code']]
                            u_topic_time = kv.get('u_topic_time:' + name)
                            if u_topic_time:
                                code_list.append(u_topic_time)
                            u_comment_time = kv.get('u_comment_time:' + name)
                            if u_comment_time:
                                code_list.append(u_comment_time)
                            #login
                            self.set_cookie('username',
                                            name,
                                            path="/",
                                            expires_days=365)
                            self.set_cookie(
                                'usercode',
                                md5(''.join(code_list)).hexdigest(),
                                path="/",
                                expires_days=365)
                            self.set_cookie('userflag',
                                            member_dict['flag'],
                                            path="/",
                                            expires_days=365)
                            if member_dict['flag'] == '1':
                                self.redirect('/setavatar')
                            else:
                                self.redirect('/')
                            return

                        else:
                            errors.append('用户名或密码不对或已被禁用')
                    else:
                        errors.append('用户名或密码不对')
                else:
                    errors.append('用户名只能包含字母和数字')
            else:
                errors.append('用户名或密码太长了')
        else:
            errors.append('用户名和密码必填')

        self.echo('login.html', {
            'title': "登录",
            'errors': errors,
            'name': name,
            'newest_node': Node.get_newest(),
        },
                  layout='_layout.html')
Example #33
0
 def get(self, nodeid):
     n_obj = Node.get_by_key('n-'+str(nodeid))
     if not n_obj:
         self.set_status(404)
         self.write('404')
         return
     
     from_id = int(self.get_argument('id', '0'))
     if from_id<=0 and n_obj['count']:
         from_id = int(n_obj['count'])
     
     to_id = from_id - EACH_PAGE_POST_NUM
     if to_id<0:
         to_id = 0
     
     self.echo('nodedetail.html', {
         'title': n_obj['name'],
         'n_obj': n_obj,
         'from_id': from_id,
         'to_id': to_id,
         'topic_objs': Node.get_page_topic(str(nodeid), from_id, to_id),
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         'recent_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
         'comment_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),            
     }, layout='_layout.html')
     Node.add_node_key('n-'+str(nodeid))
 def add_node(self,
              text: str,
              pos: tp.Tuple[int, int],
              colour: str = "green",
              multibox: bool = False) -> int:
     new_node = Node(text, pos, colour, multibox)
     new_id = id(new_node)
     self._nodes[new_id] = new_node
     return new_id
Example #35
0
 def get(self):
     if self.cur_user:
         self.echo('setavatar.html', {
             'title': "设置头像",
             'errors':[],
             'newest_node': Node.get_newest(),
         }, layout='_layout.html')
     else:
         self.redirect('/')
Example #36
0
 def create_node(self, drip_campaign_id, title, start_time, template_id, subject, from_email, from_name, initial,
                 description=None):
     """
     create a single drip campaign node, save to mongo
     return object id
     """
     new_content = Content(template_id=template_id, subject=subject, from_email=from_email, from_name=from_name)
     new_node = Node(
         drip_campaign_id=drip_campaign_id,
         title=title,
         start_time=start_time,
         description=description,
         content=new_content,
         initial=initial,
         done=False,
     )
     new_node.save()
     return new_node.id
Example #37
0
class MongoLoader(object):
    """Parses xml nodes and inserts appropriate ones to db"""
    
    def __init__(self, collection_name):
        """C'tor sets tag wich we'll count"""
        
        from pymongo import Connection
        
        conn = Connection('localhost', 27017)
        db = conn['openstreetmaps']
        self._ca = db[collection_name]
        
        self._current_node = None
        self._count = 0
    
    def start(self, tag, attrib):
        """parser enters tag"""
        
        from model import Node
        
        if tag == "node":
            # Start node writing
            self._current_node = Node(  float(attrib["lat"]),
                                        float(attrib["lon"]),
                                        id = int(attrib["id"])  )
        elif tag == "tag" and self._current_node:
            # Add tag to node
            self._current_node.add_tag(attrib["k"], attrib["v"])
    
    def end(self, tag):
        """parser reached end of tag"""
        if tag == "node" and self._current_node:
            self.__save_node()
            self._current_node = None
    
    def close(self):
        """parser closes"""
        count, self._count = self._count, 0
        return count
    
    def __save_node(self):
        """Save current_node to list"""
        self._ca.insert(self._current_node)
        self._count += 1
Example #38
0
class MongoLoader(object):
    """Parses xml nodes and inserts appropriate ones to db"""
    def __init__(self, collection_name):
        """C'tor sets tag wich we'll count"""

        from pymongo import Connection

        conn = Connection('localhost', 27017)
        db = conn['openstreetmaps']
        self._ca = db[collection_name]

        self._current_node = None
        self._count = 0

    def start(self, tag, attrib):
        """parser enters tag"""

        from model import Node

        if tag == "node":
            # Start node writing
            self._current_node = Node(float(attrib["lat"]),
                                      float(attrib["lon"]),
                                      id=int(attrib["id"]))
        elif tag == "tag" and self._current_node:
            # Add tag to node
            self._current_node.add_tag(attrib["k"], attrib["v"])

    def end(self, tag):
        """parser reached end of tag"""
        if tag == "node" and self._current_node:
            self.__save_node()
            self._current_node = None

    def close(self):
        """parser closes"""
        count, self._count = self._count, 0
        return count

    def __save_node(self):
        """Save current_node to list"""
        self._ca.insert(self._current_node)
        self._count += 1
    def get_available_nodes(self):
        listNodes = ast.literal_eval(config.node_list)
        nodes = []
        for node in listNodes:
            nodeModel = Node(node['id'], node['name'])
            nodes.append(nodeModel)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("get_available_nodes(): nodes - " + str(nodes))
        return NodeCollection(nodes)
Example #40
0
    def get(self):
        cur_user = self.cur_user()

        self.echo('notifications.html', {
            'title':
            "站内提醒",
            'topic_objs':
            Commomkvdb.get_notifications(cur_user['name'],
                                         cur_user.get('notic', '')),
            'newest_node':
            Node.get_newest(),
            'recent_node':
            Node.get_recent_node(),
            'recent_topic_objs':
            Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
            'comment_topic_objs':
            Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),
        },
                  layout='_layout.html')
Example #41
0
def edit_workflow(workflow_id):
    wf = Workflow.query.filter_by(id=workflow_id).first()
    if wf is None:
        abort(404)
    if wf.user_id != current_user.id:
        abort(403)

    if request.method == "GET":
        tag_str = " ".join([t.name for t in wf.tags])
        return render_template("edit.html", workflow=wf, tag_str=tag_str)
    elif request.method == "POST":
        form = NewWorkflow()
        tags = []
        for tag in form.tag.data.split(" "):
            tag_name = tag.strip()
            t = Tag.query.filter_by(name=tag_name).first()
            if t is None:
                t = Tag(tag_name)
                db.session.add(t)
                db.session.commit()
            tags.append(t)
        subdir = os.path.join("static", "workflows", str(wf.id))
        if form.knwf.data.filename:
            filename = secure_filename(form.knwf.data.filename)
            form.knwf.data.save(os.path.join(subdir, filename))
            zfile = ZipFile(form.knwf.data)
            node_list = []
            for name in zfile.namelist():
                if name.endswith("workflow.svg"):
                    png_file = os.path.join(subdir, "workflow.png")
                    s = zfile.read(name)
                    svg2png(bytestring=s, write_to=png_file)
                tname = name.split("/")[1]
                node_name = tname.split("(")[0]
                if not node_name.startswith(
                        "workflow") and not node_name.startswith("."):
                    node_name = node_name.rstrip()
                    node_list.append(node_name)
            nl = list(set(node_list))
            nodes = []
            for node_name in nl:
                n = Node.query.filter_by(name=node_name).first()
                if n is None:
                    n = Node(node_name)
                    db.session.add(n)
                    db.session.commit()
                nodes.append(n)
            wf.workflow = filename
            wf.nodes = nodes
        wf.tags = tags
        wf.name = form.name.data
        wf.content = form.content.data
        wf.rendered_content = md.convert(wf.content)
        db.session.commit()
        return redirect(url_for("display_workflow", workflow_id=workflow_id))
def getNumLeafs(n):
    numLeafs = 0
    if isinstance(n.children, list):
        newNode = Node()
        template = n.children[0].logTemplate
        for node in n.children[1:]:
            template = getTemplate(template,node.logTemplate)
        token = ' '.join(template)
        newNode.token = token
        n.children = {token:newNode}
    for c in n.children:
        if isinstance(c,list):
            numLeafs += 1
            continue
        current = n.children.get(c)
        if current.children != {}:
            numLeafs += getNumLeafs(current)
        else:
            numLeafs += 1
    return numLeafs
 def addSeq(self, wordList):
     # get root of trie
     current = self.root
     subWordList = wordList[:self.maxDepth]
     for w in subWordList:
         w = "\n".join(textwrap.wrap(w, width=10, expand_tabs=True,
                       replace_whitespace=False,
                       break_long_words=True))
         # create a child, count + 1
         tokenNode = current.children.get(w)
         if tokenNode == None:
             current.children[w] = Node()
         current = current.children[w]
         current.token = w
     template = " ".join(wordList)
     template = "\n".join(textwrap.wrap(template, width=25, expand_tabs=True,
                                 replace_whitespace=False,
                                 break_long_words=True))
     current.children[template] = Node()
     current.children[template].token = template
Example #44
0
 def start(self, tag, attrib):
     """parser enters tag"""
     
     if tag == "node":
         # Start node writing
         self._current_node = Node(  float(attrib["lat"]),
                                     float(attrib["lon"]),
                                     id = int(attrib["id"])  )
     elif tag == "tag" and self._current_node:
         # Add tag to node
         self._current_node.add_tag(attrib["k"], attrib["v"])
Example #45
0
 def get(self): 
     if IS_SAE:
         if USER_MODEL==1:
             self.redirect('/qqlogin')
             return
     self.echo('sigin.html', {
         'title': "注册",
         'errors':[],
         'name':'',
         'newest_node': Node.get_newest(),
     }, layout='_layout.html')
def ping():
    if request.method == 'POST':
        node_id = request.headers.get('node-id')
        node_floor = request.headers.get('node-floor')
        print 'Incoming ping from node ' + node_id
        incoming_node = Node(node_id, node_floor)
        success = mongodb.add_or_update(incoming_node)

        if success:
            return '200'
        return '500'
Example #47
0
class JSONLoader(object):
    """Parses xml nodes and inserts appropriate ones to db"""
    
    def __init__(self):
        """C'tor sets tag wich we'll count"""
        
        from json import JSONEncoder
        self._encoder = JSONEncoder()
        
        self._current_node = None
        self._count = 0
    
    def start(self, tag, attrib):
        """parser enters tag"""
        
        from model import Node
        
        if tag == "node":
            # Start node writing
            self._current_node = Node(  float(attrib["lat"]),
                                        float(attrib["lon"]),
                                        id = int(attrib["id"])  )
        elif tag == "tag" and self._current_node:
            # Add tag to node
            self._current_node.add_tag(attrib["k"], attrib["v"])
    
    def end(self, tag):
        """parser reached end of tag"""
        if tag == "node" and self._current_node:
            self.__save_node()
            self._current_node = None
    
    def close(self):
        """parser closes"""
        count, self._count = self._count, 0
        return count
    
    def __save_node(self):
        """Save current_node to list"""
        print(self._encoder.encode(self._current_node))
        self._count += 1
Example #48
0
 def get(self, nodeid, topicid):
     t_key = 't-%s-%s' % (str(nodeid), str(topicid))
     t_obj = Topic.get_by_key(t_key)
     if not t_obj or t_obj.has_key('hide'):
         self.set_status(404)
         self.echo('error.html', {
             'page': '404',
             'title': "Can't find out this URL",
             'h2': 'Oh, my god!',
             'msg': 'Something seems to be lost...'
         })
         
         return
     if t_obj['cnum']:
         cnum = int(t_obj['cnum'])
         from_id = int(self.get_argument('id', '1'))
         if from_id>1 and from_id%EACH_PAGE_COMMENT_NUM!=1:
             self.redirect('/'+t_key)
             return
         to_id = from_id + EACH_PAGE_COMMENT_NUM - 1
         if to_id > cnum:
             to_id = cnum
         c_objs = Comment.get_comments(t_key, from_id, to_id)
     else:
         c_objs = None
         from_id = to_id = cnum = 0
         
     self.echo('topicdetail.html', {
         'title': t_obj['title'] + ' - ' + t_obj['nodename'],
         'description':'description',
         't_obj': t_obj,
         'c_objs': c_objs,
         'from_id': from_id,
         'to_id': to_id,
         'cnum': cnum,
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         'recent_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
         'comment_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),            
     }, layout='_layout.html')
Example #49
0
 def get(self, nodeid, topicid):
     topic_key = '%s-%s' % (nodeid, topicid)
     t_obj = Topic.get_by_key_name(topic_key)
     if not t_obj:
         self.error(404)
         self.echo('error.html', {
             'page': '404',
             'title': "Can't find out this URL",
             'h2': 'Oh, my god!',
             'msg': 'Something seems to be lost...'
         })
         return
     
     if t_obj.cnum:
         from_id = int(self.request.get('id', '1'))
         if from_id>1 and from_id%EACH_PAGE_COMMENT_NUM!=1:
             self.redirect('/t-'+topic_key)
             return
         to_id = from_id + EACH_PAGE_COMMENT_NUM - 1
         if to_id > t_obj.cnum:
             to_id = t_obj.cnum
         c_objs = Comment.get_comments(topic_key, from_id, to_id)
     else:
         c_objs = None
         from_id = to_id = cnum = 0
     
     self.echo('topicdetail.html', {
         'title': t_obj.title + ' - ' + t_obj.node.name,
         'description':'description',
         't_obj': t_obj,
         'c_objs': c_objs,
         'from_id': from_id,
         'to_id': to_id,
         'newest_node': Node.get_newest(),
         'recent_node': Node.get_recent_node(),
         'hot_node': Node.get_hot_node(),
         'recent_topic_objs': KeyStrValue.get_topic_key_title('recent-topic-home'),
         'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'),
     }, layout='_layout.html')
Example #50
0
 def get(self):
     req_user = self.request.user
     gu_obj = GoogleUser.get_or_insert(req_user.user_id())
     if gu_obj.name:
         self.redirect('/')
         return
     else:
         self.echo('setname.html', {
             'title': "设置名字",
             'errors':[],
             'name':'',
             'newest_node': Node.get_newest(),
         }, layout='_layout.html')
Example #51
0
 def get(self, nodeid='1'):
     if self.cur_user and self.cur_user.flag>1:
         n_obj = Node.get_by_id(int(nodeid))
         if not n_obj:
             self.error(404)
             self.echo('error.html', {
                 'page': '404',
                 'title': "Can't find out this URL",
                 'h2': 'Oh, my god!',
                 'msg': 'Something seems to be lost...'
             })
             return
         self.echo('newpost.html', {
             'title': "发新帖子",
             'errors':[],
             'n_obj': n_obj,
             't_obj': Topic(),
             'newest_node': Node.get_newest(),
         }, layout='_layout.html')
     else:
         self.error(403)
         self.write('403:forbidden')
Example #52
0
 def get(self):
     if self.cur_user and self.cur_user.flag==99:
         n_id = self.request.get('id')
         if n_id:
             n_obj = Node.get_by_id(int(n_id))
         else:
             n_obj = Node()
             
         if n_obj:
             title = "修改分类"
         else:
             n_obj = Node()
             title = "添加分类"
             
         self.echo('addnode.html', {
             'title': title,
             'n_obj': n_obj,
             'newest_node': Node.get_newest(),
         }, layout='_layout.html')
     else:
         self.error(403)
         self.write('403:forbidden')
Example #53
0
 def post(self):
     n_id = str(self.get_argument('id',''))
     name = self.get_argument('name','')
     imgurl = self.get_argument('imgurl','')
     about = self.get_argument('about','')
     
     if name:
         n_n_id = Node.set_node(n_id, name, imgurl, about)
         if n_n_id:
             self.redirect('/add-node?id=%s'%n_n_id)
             return
     
     self.redirect('/add-node?id=%s'%n_id)
Example #54
0
def addNodes(nodes):
    added = []
    for id in nodes:
        node,revision,tags = getNode(id)
        
        node['version'] = node['vid']
        node['user'] = node['uid']
        node['id'] = node['nid']
        revision['user'] = revision['uid']
        revision['id'] = revision['vid']
        revision['node'] = revision['nid']
        del(node['status'])
        del(node['uid'])
        del(node['nid'])
        del(node['vid'])
        del(revision['uid'])
        del(revision['nid'])
        del(revision['vid'])
        # add the nodes
        try:
            dbnode = Node.get(node['id']) 
            del(node['id'])
            dbnode.set(**node)
        except SQLObjectNotFound:
            dbnode = Node(**node)

        try:
            dbnoder = NodeRevision.get(revision['id'])
            del(revision['id'])
            dbnoder.set(**revision)
        except SQLObjectNotFound:
            dbnoder = NodeRevision(**revision)
        # add the termnodes
        for tag in tags:
            tag['node'] = tag['nid']
            tag['term'] = tag['tid']
            del(tag['nid'])
            del(tag['tid'])
            try:
                dbtag = Term.get(tag['term'])
            except SQLObjectNotFound:
                dbtag = addTags([tag['term']])[0]
            terms = list(TermNode.selectBy(node=dbnode,term=dbtag))
            if not terms:
                term = TermNode(**tag)
            else:
                term = terms[0]

        print "Node %d made!" % id
        added.append(id)
    return added
Example #55
0
 def get(self, name):
     name = name.lower()
     m_obj = Member.get_by_key_name(name)
     if m_obj:
         self.echo('member.html', {
             'title': m_obj.name,
             'm_obj': m_obj,
             'mi_obj': MemberInfo.get_by_key_name(name),
             'newest_node': Node.get_newest(),
             'recent_node': Node.get_recent_node(),
             'hot_node': Node.get_hot_node(),
             'recent_topic_objs': KeyStrValue.get_topic_key_title('recent-topic-home'),
             'reply_topic_objs': KeyStrValue.get_topic_key_title('recent-reply-topic'),            
         }, layout='_layout.html')
     else:
         self.error(404)
         self.echo('error.html', {
             'page': '404',
             'title': "Can't find out this URL",
             'h2': 'Oh, my god!',
             'msg': 'Something seems to be lost...'
         })
         return
Example #56
0
 def get(self, name): 
     name = name.lower()
     m_obj = Member.get_by_name(str(name))
     if m_obj:
         self.echo('member.html', {
             'title': m_obj['name'],
             'm_obj': m_obj,
             'topic_objs': Commomkvdb.get_topic_by_keys('topic-'+str(m_obj['name'])),
             'member_comment_topic_objs': Commomkvdb.get_topic_by_keys('comment-topic-'+str(m_obj['name'])),
             'newest_node': Node.get_newest(),
             'recent_node': Node.get_recent_node(),
             'hot_node': Node.get_hot_node(),
             'recent_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-topic-home'),
             'comment_topic_objs': Commomkvdb.get_comment_topic_by_keys('recent-comment-topic-home'),                
         }, layout='_layout.html')
     else:
         self.set_status(404)
         self.echo('error.html', {
             'page': '404',
             'title': "Can't find out this URL",
             'h2': 'Oh, my god!',
             'msg': 'Something seems to be lost...'
         })