Beispiel #1
0
def main():
    parser = settings_parser.SettingsParser(sys.argv, "[-server <server host>] [-timeout <timeout>] <settings profile>")
    server = parser.get("server", "127.0.0.1:8080", string)
    timeout = parser.get("timeout", "5", int)
    db = settings_parser.acquire_model(parser)

    if not server.startswith("http://"):
        server = "http://" + server
    if server.endswith("/"):
        server = server[: len(server) - 1]
    if options.timeout > 0:
        socket.timeout = options.timeout

    successful = 0
    failed = 0
    http = httplib2.Http()

    for node in db.index("type_index", "get", "node"):
        try:
            url = "%s/recommendations/%s" % (server, urllib2.quote(node.id))
            res, content = http.request(url)

            if res.status < 200 or res.status > 299:
                sys.stderr.write("Training node '%s' failed: HTTP error code %s\n" % (node.id, res.status))
                failed += 1
        except Exception, e:
            sys.stderr.write("Training node '%s' failed: %s\n" % (node.id, e.message))
            failed += 1
Beispiel #2
0
def main():
    parser = settings_parser.SettingsParser(sys.argv, '[-s <seed>] <config file> <num tests>')
    
    if len(parser.args) != 2:
        parser.error('Must specify number of tests to run')
    
    try:
        tests = int(parser.args[1])
        assert tests > 0
    except:
        parser.error('Test count must be an int greater than 0')
    
    seed = parser.get('seed', '', int)
    db = settings_parser.acquire_model(parser)
    
    http = httplib2.Http()
    rmse = 0.0
    avg = 0.0
    total = 0
    
    for link in db.index('links_index', 'random', seed, tests):
        from_hash, to_hash = link
        from_node = db[from_hash]
        to_node = db[to_hash]
        
        if not to_node.id in from_node.links:
            continue
        
        link = from_node.links[to_node.id]
        del from_node.links[to_node.id]
        db[from_hash] = from_node
        
        from_uri = urllib.quote(from_node.id, '')
        to_uri = urllib.quote(to_node.id, '')
        url = 'http://127.0.0.1:8080/recommendations/%s/%s' % (from_uri, to_uri)
        res, content = http.request(url)
        
        if res.status != 200:
            sys.stderr.write('%s returned unexpected HTTP code %s\n' % (url, res.status))
        else:
            try:
                print 'predicted: %s\tactual: %s' % (content, link.weight)
                distance = abs(float(content) - link.weight)
                rmse += distance ** 2
                avg += abs(distance)
                total += 1
            except:
                sys.std.err.write('could not parse %s\n' % url)
                
        from_node.links[to_node.id] = link
        db[from_hash] = from_node
        
    rmse = sqrt(rmse / total) if total > 0 else 0.0
    print 'rmse: %s' % rmse
    
    avg = avg / total if total > 0 else 0.0
    print 'average distance: %s' % avg
Beispiel #3
0
def main():
    """Exposes utilities for setting up a Snowball instance"""
    
    parser = settings_parser.SettingsParser(sys.argv, '<settings profile> <command 1> ... <command n>')
    db = settings_parser.acquire_model(parser)
    
    for arg in parser.args[1:]:
        if arg == 'db':
            setup_db(db)
        elif arg == 'testusers':
            add_test_users(db)
        else:
            sys.stderr.write("Unknown task: '%s'" % arg)
Beispiel #4
0
def main():
    parser = settings_parser.SettingsParser(sys.argv, '<settings profile> <mode>')
    db = settings_parser.acquire_model(parser)
    
    supported_modes_str = ', '.join(supported_modes[:-1]) + ' or ' + supported_modes[-1]
    if len(parser.args) < 2:
        parser.error('Mode parameter required (%s)' % supported_modes_str)
    if not parser.args[1] in supported_modes:
        parser.error('Unknown mode: %s. Must be %s' % (parser.args[1], supported_modes_str))
        
    mode = parser.args[1]
    
    #Read all of the input code until the user feeds a blank line
    code = ''
    while True:
        line = sys.stdin.readline()
        
        if line == '\n':
            break
        else:
            code += line
        
    if mode == 'add':
        add(db, code)
    else:
        pp = pprint.PrettyPrinter()
    
        for entity_id in db:
            entity = db[entity_id]
            
            if mode == 'iter':
                #Just execute the input code for each entity
                exec code
            elif mode == 'query':
                #Print the current entity if the input code returns True for
                #this entity
                if eval(code):
                    pp.pprint(entity)
            elif mode == 'delete':
                #Delete the current entity if the input code returns True
                if eval(code):
                    print 'Deleting entity ' + base64.b16encode(entity_id)
                    del db[entity_id]
Beispiel #5
0
def main():
    """Runs the Snowball server"""
    
    #Parse the settings
    parser = settings_parser.SettingsParser(sys.argv)
    type = parser.get('type', 'tornado')
    port = parser.get('port', 8080, int)
    db = settings_parser.acquire_model(parser)
    
    admin_enabled = parser.get('admin_enabled', False)
    admin_pass = parser.get('admin_pass')
    
    #Delete db settings, which contains sensitive data (like the database password)
    for setting in parser.settings.keys():
        if setting.startswith('db'):
            del parser.settings[setting]
            
    #This setting means that clients can set an error_format parameter to control the format of error outputs
    parser.settings['output_type_override'] = 'error_format'
    
    #Store a connection to the database in the settings
    parser.settings['db'] = db
    
    #Tornado setting for the path to static files
    parser.settings['static_path'] = os.path.join(os.path.dirname(__file__), 'web/static')
    
    routes = [
        (r'^/recommendations/(.+)/(.+)$', recommendations.RecommendationHandler),
        (r'^/recommendations/(.+)$', recommendations.RecommendationSetHandler),
        
        (r'^/links/(.+)/(.+)$', links.LinkHandler),
        (r'^/links/(.+)$', links.LinkSetHandler),
        
        (r'^/tags/(.+)/(.+)$', tags.TagHandler),
        (r'^/tags/(.+)$', tags.TagHandler),
        (r'^/tags$', tags.TagHandler),
        
        (r'^/nodes/(.+)$', nodes.NodeHandler),
        (r'^/nodes$', nodes.PostNodeHandler),
    ]
    
    #Conditional endpoints
    if admin_enabled:
        routes.append((r'/admin(.*)$', admin.AdminHandler))
        
    #Error endpoint
    routes.append((r'^(?!\/static\/)(.*)$', error.ErrorHandler))
                
    if type == 'wsgi':
        #Runs a WSGI-compliant server
        import tornado.wsgi
        import wsgiref.handlers
        
        app = tornado.wsgi.WSGIApplication(routes, **parser.settings)
        wsgiref.handlers.CGIHandler().run(app)
    elif type == 'tornado':
        #Runs the tornado server
        import tornado.httpserver
        import tornado.ioloop
        
        app = tornado.web.Application(routes, **parser.settings)
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(port)
        tornado.ioloop.IOLoop.instance().start()
    else:
        parser.error('Unknown server type: %s' % type)