class Deleted(Filter): def init(self): self.watch = [] # 監視対象のスクリーンネームを入れる. 空なら全部通知 self.exclude = [] addr = "localhost" port = 27017 db_name = "twitter" self.connection = Connection(addr, port) self.col = self.connection[db_name].tweet def filter(self, packet): data = packet["data"] if not isinstance(data, dict): return None if data.get("event") == "delete": try: tweet = self.col.find({u"id": data["id"]})[0] except IndexError: return None detail = {"user": tweet[u"user"][u"screen_name"], "post": tweet[u"text"]} if str(detail["user"]) in self.exclude or (self.watch != [] and str(detail["user"]) not in self.watch): return None self.send(u"%(user)s deleted: %(post)s" % detail, exclude=["favbot"]) def cleanup(self): self.connection.close()
class Simple_pymongo(Extend_Connection): @staticmethod def get_instance(): obj = Simple_pymongo(config.mongo["hosts"]) return obj def __init__(self, *args, **kargs): self.conn_obj = Connection(*args, **kargs) def create_cursor(self, dbname=config.mongo["dbname"]): self.db = self.conn_obj.__getattr__(dbname) return self.db def auto_incr_id(self, col, object_id): if type(object_id) == "str": object_id = ObjectId(objectid) self.db.command( "findandmodify", col, query={"_id": object_id}, update={"$inc": {"id": 1}}, upset=True, new=True ) def _set_db(self, dbname): self.db = self.conn_obj.__getattr__(dbname) def _set_collection(self, collection): self.col = self.db.__getattr__(collection)
class DaoSunPosition(): def __init__(self): self.col = Connection()['rdam']['sunpos'] self.bulk = [] def create_datetime_index(self): self.col.create_index('datetime') def persist(self, sunpos): self.bulk.append(sunpos) def flush(self): self.col.insert(self.bulk) self.bulk = [] def find_within_time(self, start_date, end_date): result = [] for bson in self.col.find({ '$and': [ { 'datetime' : { '$gt': start_date }}, { 'datetime' : { '$lt': end_date }} ]}): sunpos = SunPosition(bson['az'], bson['el'], bson['datetime']) result.append(sunpos) return result
def test_set_crud(self): pp = [{"nam":"India","states":["Maharashtra","Karnatka"],"cities":["Aurangabad","Bangalore"],"zip":"d6","zips":[431005,431001],"iso":"IN","stateiso":["MH","KR"],"phoneregex":"+91","localnam":"Hindustan","altname":["None"]},{"nam":"United States of America","states":["California","Texas"],"cities":["San Francisco","Denver"],"zip":"\d4","zips":["None","Empty"],"iso":"US","stateiso":["CA","CO"],"phoneregex":"+1","localnam":"USA","altname":["America","United States"]}] db = Connection()['rontest'] db.safe = True test = db['test'] test.remove() print test.insert(pp)
def main(): connection = Connection() def compute_signature(index): signature = index["ns"] for key in index["key"]: try: signature += "%s_%s" % (key, int(index["key"][key])) except ValueError: signature += "%s_%s" % (key, index["key"][key]) return signature def report_redundant_indexes(current_db): print "Checking DB: %s" % current_db.name indexes = current_db.system.indexes.find() index_map = {} for index in indexes: signature = compute_signature(index) index_map[signature] = index for signature in index_map.keys(): for other_sig in index_map.keys(): if signature == other_sig: continue if other_sig.startswith(signature): print "Index %s[%s] may be redundant with %s[%s]" % ( index_map[signature]["ns"], index_map[signature]["name"], index_map[other_sig]["ns"], index_map[other_sig]["name"], ) for db in connection.database_names(): report_redundant_indexes(connection[db])
def output_name_matrix_of_two_words(): from helper import get_progressive_bar from pymongo import Connection users=Connection().user_profilling.users lastnames=[name.replace('\n','').decode('utf8') for name in open('./lastname')] bar=get_progressive_bar(users.count()) finish_count=0 tf=pickle.load(open('./tf.data')) x=[] y=[] for user in users.find(): name=user['screen_name'] finish_count+=1 if finish_count>5000: break for n in name: if n[0] not in lastnames or len(n)>3 and len(n)<3: continue try: x0=1.0*tf[n[1]][0]/sum(tf[n[1]]) x1=1.0*tf[n[2]][0]/sum(tf[n[2]]) except: continue if user['information']['gender']=='m': y.append(1) else: y.append(0) x.append([x0,x1]) bar.cursor.restore() bar.draw(value=finish_count) dump_train_valid_test(x,y,'gender_name_simple.data')
def setup_database(): # Disconnect from the default mongo db, and use a test db instead. conn = Connection() checkin_service = conn["checkin_service"] results = conn["results"] for collection in ['splice_server', 'marketing_product_usage']: if drop: print('DROPPING') checkin_service.drop_collection(collection) print 'importing %s collection' % collection call(['mongoimport', '--db', 'checkin_service', '-c', collection, '--file', '%s.json' % os.path.join(DUMP_DIR, collection)] ) """ for collection in ['marketing_report_data']: results.drop_collection(collection) print 'importing %s collection' % collection call(['mongoimport', '--db', 'results', '-c', collection, '--file', '%s.json' % os.path.join(DUMP_DIR, collection)] ) """ conn.disconnect()
def change_collection(db_id, coll_id): new_name = request.data # Connect to the database MongoDB try: connection = Connection('localhost', 12345) except: return json.dumps("Unable to connect to the database!") db_names = connection.database_names() if db_id not in db_names: return json.dumps("Database doesn't exist!") db = connection[db_id] coll_names = db.collection_names() if coll_id not in coll_names: return json.dumps("Experiment doesn't exist!") if new_name in coll_names: return json.dumps("New name already exist!") collection = db[coll_id] try: collection.rename(new_name) except: return json.dumps("Unable to change the name of the experiment!") return json.dumps("Experiment's name changed!")
def output_description_matrix(): from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer(min_df=1) from pymongo import Connection users=Connection().user_profilling.users bar=get_progressive_bar(users.count()) corpus=[] finish_count=0 y=[] for user in users.find(): if 'descriptions' not in user['information']: continue description=user['information']['descriptions'] corpus.append(get_str_description(description)) finish_count+=1 bar.cursor.restore() bar.draw(value=finish_count) if user['information']['gender']=='m': y.append(1) else: y.append(0) x = vectorizer.fit_transform(corpus) all_data_x=x.toarray() all_data_y=numpy.array(y) dump_train_valid_test(all_data_x,all_data_y,'gender_description.data')
def store_message(db_id, coll_id): experiment_collection = experiment_results_pb2.Experiment() try: experiment_collection.ParseFromString(request.data) except: return json.dumps('Experiment is not well defined!') # Connect to the database MongoDB try: connection = Connection('localhost', 12345) except: return json.dumps("Unable to connect to the database!") db_names = connection.database_names() if db_id in db_names: db = connection[db_id] else: return json.dumps("No such database!") coll_names = db.collection_names() if coll_id in coll_names: collection = db[coll_id] else: return json.dumps("No such experiment in the database!") try: collection.insert(protobuf_json.pb2json(experiment_collection)) except: return json.dumps("Unable to store data into the database!") return json.dumps('Data stored!')
def create_collection(db_id): coll_id = request.data # Connect to the database MongoDB try: connection = Connection('localhost', 12345) except: return json.dumps("Unable to connect to the database!") db_names = connection.database_names() if db_id in db_names: db = connection[db_id] else: return json.dumps("No such database!") coll_names = db.collection_names() if coll_id in coll_names: return json.dumps("Collection already exists!") try: db.create_collection(coll_id) except: return json.dumps("Unable to create an experiment") return json.dumps('Experiment successfully created!')
class DB(BaseDB): def __init__(self): self.connection = Connection() self.db = self.connection['oauth'] #make sure the Tranform class which is a SONManipulator is used self.db.add_son_manipulator(Transform()) self.models = self.db.models def get(self, key): document = self.models.find_one({'key':key}) return document['class'] def put(self, key, data): #if we don't do this we end up with mutiple copies of the same data if not self.contains(key): document = {'key':key, 'class': data} self.models.insert(document) else: self.update(key, data) def update(self, key, data): #import pdb; pdb.set_trace() #seems you can't use a SONManipulator on updates, #manipulate=True also adds a new _id which causes an #'OperationFailure: Modifiers and non-modifiers cannot be mixed' #Solution: do the transform directly and use the result to #update the entry with and don't set manipulate=True data = json.loads(jsonpickle.encode(data)) self.models.update({'key':key}, {"$set": {'class':data}}, multi=False, safe=True) def delete(self, key): self.models.remove({'key':key}) def commit(self): #we don't need this for MongoDB pass def abort(self): #we could do this I think, #but am currently debating whether to use it or not pass def contains(self, key): #returns None if no entry with that key filter exists #so wrapping it in bool will return False if None, #True on anything else return bool(self.models.find_one({'key':key})) def close(self): self.connection.close()
def entityProperties(entity): # Conexion a Mongo conn = Connection() db = conn.grupo10_taller4 colTypeQueryCache = db.typeQueryCache colDescribeQueryCache = db.describeQueryCache # Revisar si la consulta existe en el cache describeCache = colDescribeQueryCache.find({"term":entity}) if (describeCache.count() > 0): print "Describe cache hit for %s" % entity for cacheResult in describeCache: return cacheResult["results"] sparql = SPARQLWrapper("http://dbpedia.org/sparql") sparql.setQuery(""" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> DESCRIBE ?object WHERE { ?object rdfs:label '%s'@en . } """ % entity ) sparql.setReturnFormat(JSON) results = sparql.query().convert() queryRes = [{"term":entity,"results":results}] colDescribeQueryCache.insert(queryRes) conn.disconnect() return results
def construct_mallet_data(profile_key): from pymongo import Connection from my_progress_bar import progress_bar from collections import Counter users=Connection().jd.weibo_users bar=progress_bar(users.count()) fout=open(MATRIXES_DIR+'mallet/construced_data.mallet','w') data=[] for index,user in enumerate(users.find()): try: label=user['profile'][profile_key].index(1) except: continue reviews=[] for behavior in user['behaviors']: #reviews.append('Pro'+str(behavior['item'])) reviews+=behavior['parsed_review']['review_general'] reviews=Counter(reviews) reviews=' '.join(map(lambda word:'%s:%d'%(word,reviews[word]),reviews.keys())) line='%s %d %s\n'%(user['_id'],label,reviews) data.append((label,line)) data=balance(data,target_index=0) #balanced_data=data for label,line in balanced_data: fout.write(line.encode('utf8')) bar.draw(index)
def start(): global connection, db, dbname global tagscollection, t, started global mongod_process if started: return args.source_dir = os.path.expanduser(args.source_dir) # Start mongodb dbpath = os.path.join(args.source_dir, "mongo_db") mongod_process = start_mongod(dbpath=dbpath) while 1: try: log("trying to connect to mongod...") connection = Connection() break except: sleep(1) continue dbname = args.source_dir.replace('/','|').replace('.','') db = connection[dbname] t = db[tagscollection] log("Found databases: %s" % (', '.join(connection.database_names()))) if dbname not in connection.database_names(): log("Database for this project does not exist. Creating one...") create_project(args.source_dir) started = True
def save(openvas, **kwargs): logger = save.get_logger() openvasTask = Connection().phoenorama.openvasTask openvasTask.insert(openvas.toJSON()) logger.info("Openvas Task was successfully saved")
def setData(self, serialData): try: print serialData decoded = json.loads(serialData, object_hook=json_util.object_hook) id_no = decoded['id_no'] sensor = decoded['sensor'] porta = decoded['porta'] valor = str(decoded['valor']) if sensor == "2": valor = valor + '.00' datahora = decoded['datahora'] lab = decoded['lab'] #p = 1000 #salva no banco de dados con = Connection('127.0.0.1', 27017, safe=True) db = con['inoshare'] pacote = {'id_no':id_no, 'sensor':sensor, 'porta':porta, 'valor':valor, 'datahora':datahora, 'lab': lab} inoshare = db.inoshare inoshare.insert(pacote) print pacote con.close() except Exception as e: print "Erro no filter data: ", e.message, e.args
def index(): connection = Connection(MONGODB_HOST, MONGODB_PORT) collection = connection[DBS_NAME][COLLECTION_NAME] _id = request.args.get('_id','') selected=None try: selected = collection.find({'_id':ObjectId(_id)}).next() except: pass items = collection.find({'visible':'True'}).sort('name') connection.disconnect() fields=deepcopy(FIELDS) editform=False if selected is not None: editform=True for field in fields: try: fieldname = field['name'] field['default_value']=selected[fieldname] except: field['default_value']='' return render_template("index.html", items=items, selected=selected, fields=fields, editform=editform, )
def construct_all_data(): ''' The format of labeled_feature_file is as the same as mallet ''' all_features=get_features(feature_file_name=feature_file_name) all_features_1=get_features(feature_file_name=base_dir+'/features/mention_1.feature',start_index=max(all_features.values())+1) collection=Connection().jd.train_users bar=progress_bar(collection.count()) fout=open(RAW_DATA_DIR+'iterate_label2trainset/all_train.data','w') uid_output=open(RAW_DATA_DIR+'iterate_label2trainset/all_train_uids.data','w') for index,user in enumerate(collection.find()): label=0 fout.write('%d'%label) uid_output.write('%s\n'%user['_id']) features=combine_features(user['mentions_1'],Counter(user['products'])) sorted_feature=[] for f in features: if f not in all_features: continue sorted_feature.append((all_features[f],features[f])) for f,v in user['mentions_1_1'].items(): f=f+'_1' if f not in all_features_1: continue sorted_feature.append((all_features_1[f],v)) sorted_feature=sorted(sorted_feature,key=lambda d:d[0]) for f in sorted_feature: fout.write(' %s:%d'%f) fout.write('\n') bar.draw(index+1)
class Application(tornado.web.Application): def __init__(self, handlers, **settings): tornado.web.Application.__init__(self, handlers, **settings) self.collection = Connection().vapour.urls self.templates = TemplateLookup(directories=["templates"]) def get_link_by_id(self, id): record = self.collection.find_one({'_id': uuid.UUID(id)}) return fix_id(record) def get_links_by_tag(self, tag): records = self.collection.find({'tags': re.compile(tag, re.I)}) return fix_ids(records) def get_links_by_url(self, url): records = self.collection.find({'url': re.compile(url, re.I)}) return fix_ids(records) def insert_link(self, url, desc, tags): return self.collection.insert({ '_id': uuid.uuid4(), 'url': url, 'desc': desc, 'tags': tags, 'added': datetime.datetime.utcnow() })
def __init__(self, uid, follow=None, user=None): self.uid = uid self.user = user self.follow = follow if mongo_host is not None or mongo_port is not None: self.connection = Connection(mongo_host, mongo_port) else: self.connection = Connection() self.db = self.connection.sina self.info_data = self.db.info self.weibo_data = self.db.weibo self.relation_data = self.db.relation self.wait_crawled = self.db.wait_crawled self.completes = self.db.completes self.errors_data = self.db.errors data = self.weibo_data.find_one({'uid': self.uid}) self.crawled = data is not None if data is None and follow is None: self.weibo_data.insert({'uid': self.uid}) elif follow is not None: self.weibo_data.update({'uid': self.uid}, {'$addToSet': {'follows': follow}}, upsert=True) self.replace_reg = re.compile('(http://t.cn/\\S+)|(@\\S+)')
def testModEvent(self): app = Flask(__name__) conn = Connection() with app.test_client() as c: testRequest = c.get('/event.json?_username=tester&_session=1234&'\ '_method=PUT&activity=testing&variables=score&'\ 'types=int&officials=tester&oldid=testing122&'\ 'descr=testing%20stuff&id=testing123') event = {'id': 'testing122', 'activity': 'testing', 'officials': ['tester'], 'descr': 'testing stuff', 'fields': ['participants', 'score'], 'rends': [], 'types': ['varchar', 'int'], 'rstarts': [], 'checks': []} statserv.server.sessions['1234'] = ('tester', None, True) statserv.server.database = conn.test_db db = conn.test_db db.stattrtbls.insert(event) del event['_id'] event['id'] = 'testing123' statserv.server.mod_event(request) actual = db.stattrtbls.find_one({}) del actual['_id'] self.assertEquals(event, actual, 'The mod_event method did something we didn\'t '\ 'expect. Expected: `%s`, actual: `%s`.'\ % (event, actual)) conn.drop_database('test_db')
def do_host_status(self,mongo): con = Connection(host=mongo['host'], port=mongo['port'], slave_okay=True) db = con['admin'] if mongo['user'] and mongo['password']: db.authenticate(mongo['user'], mongo['password']) server_status = db.command('serverStatus') # operations for k, v in server_status['opcounters'].items(): self.submit('mongo_total_operations', k, v, host=mongo['host'], port=mongo['port']) # memory for t in ['resident', 'virtual', 'mapped']: self.submit('mongo_memory', t, server_status['mem'][t], host=mongo['host'], port=mongo['port']) # connections self.submit('mongo_connections', 'connections', server_status['connections']['current'],host=mongo['host'], port=mongo['port']) # locks if mongo['lockTotalTime'] is not None and mongo['lockTime'] is not None: #collectd.warning( "total %d - %d / %d - %d " % (server_status['globalLock']['totalTime'], self.lockTotalTime, server_status['globalLock']['totalTime'], self.lockTime)) if mongo['lockTime']==server_status['globalLock']['lockTime']: value=0.0 else: value=float (server_status['globalLock']['lockTime'] - mongo['lockTime']) *100.0 / float(server_status['globalLock']['totalTime'] - mongo['lockTotalTime'] ) #collectd.warning( "Submitting value %d " % value) self.submit('mongo_percent', 'lock_ratio', value, host=mongo['host'], port=mongo['port']) mongo['lockTotalTime']=server_status['globalLock']['totalTime'] mongo['lockTime']=server_status['globalLock']['lockTime'] # indexes accesses = server_status['indexCounters']['btree']['accesses'] misses = server_status['indexCounters']['btree']['misses'] if misses: self.submit('mongo_cache_ratio', 'cache_misses', accesses / float(misses),host=mongo['host'], port=mongo['port']) else: self.submit('mongo_cache_ratio', 'cache_misses', 0, host=mongo['host'], port=mongo['port']) for mongo_db in mongo['db']: db = con[mongo_db] if mongo['user'] and mongo['password']: db.authenticate(mongo['user'], mongo['password']) db_stats = db.command('dbstats') # stats counts self.submit('mongo_counter', 'object_count', db_stats['objects'], mongo_db, host=mongo['host'], port=mongo['port']) self.submit('mongo_counter', 'collections', db_stats['collections'], mongo_db, host=mongo['host'], port=mongo['port']) self.submit('mongo_counter', 'num_extents', db_stats['numExtents'], mongo_db, host=mongo['host'], port=mongo['port']) self.submit('mongo_counter', 'indexes', db_stats['indexes'], mongo_db, host=mongo['host'], port=mongo['port']) # stats sizes self.submit('mongo_file_size', 'storage', db_stats['storageSize'], mongo_db, host=mongo['host'], port=mongo['port']) self.submit('mongo_file_size', 'index', db_stats['indexSize'], mongo_db, host=mongo['host'], port=mongo['port']) self.submit('mongo_file_size', 'data', db_stats['dataSize'], mongo_db, host=mongo['host'], port=mongo['port']) con.disconnect()
def __init__(self): self.edit_dist_threshold = 0.2 self.tweets = Connection().tweetsDB.tweetsCollection self.businesses = Connection().yelpDB.yelpBusinesses self.photos = Connection().flickrDB.flickrCollection self.linked = Connection().linkedDB.linkedCollection self.MAX_EDIT_DISTANCE = 100000000
def init_apns(): mongodb = None while not mongodb: try: mongodb = Connection(options.mongohost, options.mongoport) except: pass # wait 5 secs to reconnect time.sleep(5) masterdb = mongodb[options.masterdb] apps = masterdb.applications.find({'enableapns': 1}) apnsconns = {} for app in apps: apnsconns[app['shortname']] = [] conns = int(app['connections']) if conns > 5: conns = 5 if conns < 1: conns = 1 if 'environment' not in app: app['environment'] = 'sandbox' if 'certfile' in app and 'keyfile' in app and 'shortname' in app: for instanceid in range(0, conns): try: apn = APNClient(app['environment'], app['certfile'], app['keyfile'], app['shortname'], instanceid) except Exception as ex: logging.error(ex) continue apnsconns[app['shortname']].append(apn) mongodb.close() return apnsconns
def hbase_enum(port): print colored("\n[!] Enumeration Module For NoSQL Framework H-Base Launched.....",'yellow') print colored("[-] Enumerating Cluster Version and Cluster Status",'blue') try: c = Connection(target,port) print colored("[-] Cluster Version: %s"%(str(c.cluster_version)),'green') v=c.cluster_status print colored("[-] Cluster Status ",'green') for key, value in v.iteritems() : print colored("\t [-] "+str(key)+":"+str(value),'green') print colored("[-] Enumerating JVM and Box Details",'blue') for key,value in c.version.iteritems(): print colored("\t[-] "+str(key)+":"+str(value),'green') print colored("[-] Tables Available",'blue') for i in c.tables(): print colored("\t[-] "+i,'green') print colored("Would you like to enumerate columns",'blue') choice=raw_input() if choice=='y': tab=raw_input(colored("[-] Enter tables name ",'blue')) if tab in c.tables(): print colored("[-] Enumerating Columns",'blue') t=c.table(tab) for i in t.columns(): print colored("\t[-] "+str(i),'green') else: print colored("[-] No such table Exists ",'red') except Exception,e: print colored("[-] Error Occured while connection %s "%(str(e)),'red')
def connection(self, kind=0, q=None): ''' Access MongoDB and load topology or events data. :param kink: specify which request should be running in the DataBase. :return: a cursor of topology or events. :rtype: Cursor of elements dictionnary or NoneType. ''' if self.username is None: connection = Connection() db = connection['canopsis'] else: connection = MongoClient(self.MONGO_URL, self.MONGO_PORT) db = connection["canopsis"] # Do the authentication db.authenticate(self.username, self.password) if kind == 0: query = self.qr # Format string json_acceptable = query.replace("'", "\"") query = json.loads(json_acceptable) cursor = db.objectv1.find(query) else: query = q # Format string json_acceptable = query.replace("'", "\"") query = json.loads(json_acceptable) cursor = db.eventsv1.find(query) connection.close() return cursor
def testGetUsers(self): app = Flask(__name__) conn = Connection() with app.test_client() as c: testRequest = c.get('/users.json?_username=test&_session=1234'\ '&callback=blah') statserv.server.sessions['1234'] = ('test', request.remote_addr, True) statserv.server.config['adminuser'] = '******' db = conn['test_db'] adminuser = {u'username': u'admin', u'password': '******', u'profile': ''} otherusers = [{u'username': u'other', u'password': '******', u'profile': u'The first other user'}, {u'username': u'other_two', u'password': '******', u'profile': u'The other other user'}] db.stattrusers.insert(adminuser) db.stattrusers.insert(otherusers[0]) db.stattrusers.insert(otherusers[1]) statserv.server.database = conn['test_db'] del otherusers[0]['_id'] del otherusers[1]['_id'] del otherusers[0]['password'] del otherusers[1]['password'] expected = statserv.server.make_response('blah', {'users': otherusers}) actual = statserv.server.get_users() conn.drop_database('test_db') self.assertEquals(actual, expected, 'The get_users method returned something other '\ 'than what we expected. Expected: `%s`, Actual: '\ '`%s`' % (expected, actual))
def get_all_uids(): from pymongo import Connection users=Connection().jd.weibo_users uids=[] for user in users.find({},{'_id':1}): uids.append(user['_id'].encode('utf8')) return uids
def insert_age_vector(): from collections import Counter users=Connection().jd.weibo_users all_vec=[] for user in users.find(): profile=user['profile'] if user['birthday'] is None: age_vec=[0,0] profile['age']=age_vec users.update({'_id':user['_id']},{'$set':{'profile':profile}}) continue if u'年' not in user['birthday']: age_vec=[0,0] profile['age']=age_vec users.update({'_id':user['_id']},{'$set':{'profile':profile}}) continue age=user['birthday'] age=age[0:age.find(u'年')] if len(age)<4: age='19'+age age=int(age) if age<1950 or age>2010: age_vec=[0,0] profile['age']=age_vec users.update({'_id':user['_id']},{'$set':{'profile':profile}}) continue if age<1987: age_vec=[1,0] else: age_vec=[0,1] profile['age']=age_vec users.update({'_id':user['_id']},{'$set':{'profile':profile}}) all_vec.append(str(age_vec)) print Counter(all_vec)
def get_pymongo_collection(collection): # TODO: How do I find out which host/port/name the test DB has? connection = Connection(settings.DATABASES['default']['HOST'], int(settings.DATABASES['default']['PORT'])) database = connection['test_test'] return database[collection]
def getprofile(username): conn = Connection() db = conn['jsdt'] res = db.jsdt.find({'name': username}, {"_id": False}) info = [x for x in res] return info
def getblogcontent(title): conn = Connection() db = conn['jsdt_blog'] res = db.jsdt_blog.find({'title': title}, {"_id": False}) return res
import activitystream reload(activitystream) ActivityStream = activitystream.ActivityStream logging.basicConfig() log = logging.getLogger() import stripchart reload(stripchart) CMD = Namespace("http://bigasterisk.com/magma/cmd/") CL = Namespace("http://bigasterisk.com/ns/command/v1#") FOAF = Namespace("http://xmlns.com/foaf/0.1/") mongo = Connection("bang", 27017, tz_aware=True) def foafAgent(ctx): h = inevow.IRequest(ctx).getHeader('x-foaf-agent') assert h is not None, "no foaf agent" return URIRef(h) # if this is from a phone, use the little menu. the big menu # should also be ext, and have links to all the inner services. class HomePage(rend.Page): docFactory = loaders.xmlfile("magma-sample2.html") def __init__(self, cmdlog, identity):
# le(n)x is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with le(n)x. If not, see <http://www.gnu.org/licenses/>. # (C) 2009-2010 by Stefan Marsiske, <*****@*****.**> from pymongo import Connection from bson.objectid import ObjectId from lenx.view.models import Doc, Pippi import pprint conn = Connection() db=conn.pippi Docs=db.docs Pippies=db.pippies MiscDb=db.miscdb def getRelatedDocs(_id,cutoff=7): return [Doc(oid=oid) for oid in set([doc['doc'] for frag in Pippies.find({'docs.doc' : _id, 'len' : { '$gte' : int(cutoff) }}, ['docs.doc']) for doc in frag['docs'] if doc['doc'] != _id])] #for p in Pippi('',oid=pippi['frag']).docs
print "Traversing through response JSON object for events" events = events_obj['resultsPage']['results']['event'] for event in events: latest_date = event['start']['date'] for p in event['performance']: artists.append( dict(artist=p['artist']['displayName'], date=latest_date, venue=event['venue']['displayName']) ) if( latest_date == one_week ): break print "This is the latest date: " + latest_date print "Found " + str(len(artists)) + " events so far" # IMPORTANT! counter += 1 print "The counter is now" + str(counter) sleep(5) connection = Connection() db = connection.ts_db events = db.events for a in artists: pprint.pprint(a) events.insert(a) connection.disconnect()
def Main(): #date = sys.argv[1] #region = sys.argv[1] obj = { 'date': ["date"], "Fremont": ["Fremont"], "IDC": ["IDC"], "vCommander+Fremont": ["vCommander+Fremont"], "vCommander+IDC": ["vCommander+IDC"] } try: connection = Connection() db = connection.calculus #define db here... collection = db.collection #db.collection.find({ "region": "vCommander+Fremont", "date_str": "20160612" } ) #mongo = collection.find({"region": region, "date_str": date } ) #mongo = collection.find({"region": "Fremont" }) #mongo = collection.find({"region": "Fremont" }).sort({"date_str": -1}) mongo = collection.find({ "region": "Fremont" }).sort("date_str", pymongo.ASCENDING) #mongo = collection.find({'date.start': 'Wed 06/01/2016'}) for index in mongo: obj['date'].append(index['date_str']) obj['Fremont'].append(index['durations_and_overhead'] ['windows_build']['overhead_mean']) mongo = collection.find({ "region": "IDC" }).sort("date_str", pymongo.ASCENDING) for index in mongo: obj['IDC'].append(index['durations_and_overhead']['windows_build'] ['overhead_mean']) mongo = collection.find({ "region": "vCommander+Fremont" }).sort("date_str", pymongo.ASCENDING) for index in mongo: obj['vCommander+Fremont'].append( index['durations_and_overhead']['windows_build'] ['overhead_mean']) mongo = collection.find({ "region": "vCommander+IDC" }).sort("date_str", pymongo.ASCENDING) for index in mongo: obj['vCommander+IDC'].append(index['durations_and_overhead'] ['windows_build']['overhead_mean']) myfile = open( '/var/www/html/fierybuild/csv/windows_build_overhead_mean.csv', 'w') wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(obj['date']) wr.writerow(obj['Fremont']) wr.writerow(obj['IDC']) wr.writerow(obj['vCommander+Fremont']) wr.writerow(obj['vCommander+IDC']) except: print "ERROR"
from pymongo import ASCENDING, DESCENDING, Connection import datetime client = Connection('localhost', 27017) db = client.local collection = db.test_1 print "first element..." print collection.find_one() print "finding feliciananasi..." for user in collection.find({u'username': u'feliciananasi'}): print user print "counting all users..." print collection.count() print "counting feliciananasis..." print collection.find({u'username': u'feliciananasi'}).count() #print "find users created..." d = "2005-01-01" #for user in collection.find({"created": {"$gt": d}}).sort("username"): # print user #print collection.find({"created": {"$gt": d}}).sort("username").explain()["cursor"] #print collection.find({"created": {"$gt": d}}).sort("username").explain()["nscanned"] print "find indexed users created..." collection.create_index([("created", DESCENDING), ("username", ASCENDING)]) print collection.find({ "created": {
print("PASSED TEST STRESSED ROLBACK") def abort_test(self): print("TEST FAILED") sys.exit(1) if __name__ == '__main__': os.system('rm config.txt; touch config.txt') parser = OptionParser() #-m is for the main address, which is a host:port pair, ideally of the #mongos. For non sharded clusters, it can be the primary. parser.add_option("-m", "--main", action="store", type="string", dest="main_addr", default="27217") (options, args) = parser.parse_args() PORTS_ONE['MONGOS'] = options.main_addr s = DocManager('localhost:30000') s._remove() start_cluster() conn = Connection('localhost:' + PORTS_ONE['MONGOS'], replicaSet="demo-repl") print("STARTING TESTS") unittest.main(argv=[sys.argv[0]]) print("Done with tests") clean_up()
#!/usr/bin/python import pymongo import random import commands import sys import re from pymongo import Connection connection = Connection('10.42.0.1', 27017) db = connection.trollfi collect = db.users stdin = sys.stdin.readline() jpg=re.compile('.*\.jpg'); png=re.compile('.*\.png'); gif=re.compile('.*\.gif'); svg=re.compile('.*\.svg'); if not jpg.match(stdin) and not png.match(stdin) and not gif.match(stdin) and not svg.match(stdin): out = db.users.find({}, { "last": 1 }); for item in out: if item["last"] != "": print item["last"] else: commands.getoutput('/home/josh/HackNY2012/russ.pl') db.users.remove({})
# Settings for logfile, and yararules file are also located in the midas-settings.cfg file. import ConfigParser, exiftool, os, shutil, sys, hashlib, datetime, time, argparse, logging, pymongo, json, urllib, urllib2 from multiprocessing import Pool # Import DB Config from midas-settings.cfg config = ConfigParser.SafeConfigParser() config.read("midas-settings.cfg") dbserver = config.get('midasdb', 'server') dbport = int(config.get('midasdb', 'port')) dbdb = config.get('midasdb', 'db') dbcoll = config.get('midasdb', 'collection') # Database Connection Information from pymongo import Connection metadatacollection = Connection(dbserver, dbport)[dbdb][dbcoll] # Argument Parser and Usage Help parser = argparse.ArgumentParser( description='Metadata Inspection Database Alerting System') parser.add_argument('Path', help='Path to directory of files to be scanned (Required)') parser.add_argument( '-d', '--delete', action='store_true', help= 'Deletes files after scanning and extracting metadata (Default: False)', required=False) parser.add_argument( '-m',
def setup(self): self.fixtures_folder = join(abspath(dirname(__file__)), 'fixtures') self.connection = Connection('localhost', 7777) self.collection = self.connection['thumbor']['images']
def save_to_mongo(data, collection): return collection.insert(data) # # Program entry point # if __name__ == "__main__": # Parse CLI arguments cli_parser = build_cli_arguments() args = cli_parser.parse_args() filename = args.file # Create DB connection mongo = Connection(args.host, args.port) db = mongo[args.database] coll = db[args.collection] # Use one file loader or another depending of its extension if filename.endswith(".gz"): with gzip.open(filename) as f: process_loop(f, args.batch_size) else: with open(filename) as f: process_loop(f, args.batch_size) mongo.disconnect()
for line in cur.fetchall(): actor, repo_name, event_type = line owner = repo_name.rsplit('/', 1) # if not repo_dict[repo_name].get("yesterday"): # repo_dict[repo_name]['yesterday'] = {"Owner_push": 0, "Owner_issue": 0, "Owner_pullrequest": 0, "Other_star": 0, "Other_pullrequest": 0, "Other_issue": 0} key_prefix = "Owner_" if owner == actor else "Other_" cur_item = repo_dict[repo_name]['yesterday'] if event_type == 'PushEvent': cur_item["Owner_push"] += 1 if event_type in ['IssuesEvent', 'IssueCommentEvent']: cur_item[key_prefix + 'issue'] += 1 if event_type in ['PullRequestEvent', 'PullRequestReviewCommentEvent']: cur_item[key_prefix + 'pullrequest'] += 1 if event_type == 'WatchEvent': cur_item["Other_star"] += 1 # Save to mongodb client = MongoClient('localhost', 27017) curation_collection = Connection()['curation']['curation_profile'] for k, v in repo_dict.iteritems(): v['url'] = k curation_collection.insert(v) del curation_collection
if record.has_key('favitemcount') and record.has_key('favshopcount') \ and record.has_key('indirectpay') and record.has_key('directpay') \ and record.has_key('indirectpaycount') and record.has_key('directpaycount'): summary_rpt_dict[campaign_id] = { 'fav': record['favitemcount'] + record['favshopcount'], 'pay': record['indirectpay'] + record['directpay'], 'paycount': record['indirectpaycount'] + record['directpaycount'] } #for key in summary_rpt_dict.keys(): # summary_rpt_dict[key]['impressions'] /=3 # summary_rpt_dict[key]['click'] /=3 # summary_rpt_dict[key]['cost'] /=3 #print "ssss:", summary_rpt_dict return summary_rpt_dict if __name__ == '__main__': MONGODB_HOST = 'app.maimiaotech.com' MONGODB_PORT = 2006 from pymongo import Connection mongoConn = Connection(host=MONGODB_HOST, port=MONGODB_PORT) rpt_effect_db = RptEffectDB(mongoConn, '57620080') rpt_effect_db.save_records([{'a': 1, 'b': 3}, {'a': 5, 'b': 9}])
def test2(): from pymongo import Connection connection = Connection('localhost') db = connection.sm
def connect(self): self.__con = Connection(host=self.host) self.__db = self.__con[self.db] log.info('[%s] mongodb connected.' % self.host)
# Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html from scrapy.exceptions import DropItem # from .items import JpAvItemId from pymongo import Connection MongoCon = Connection('localhost', 27017) # the default is 27017 MiscDB = MongoCon.misc import urllib SPEC_DB = ['tb_user', 'tb_thread', 'tb_fidfname', 'wb_userstatus', 'jp_av'] class MongoPipeline(object): def __init__(self): self.seenDict = dict() self.seenDict['jp_av'] = [ i['slug'] for i in MongoCon.jp_av.items.find() ] def process_item(self, item, spider): if hasattr(spider, 'alias'): alias = urllib.unquote(spider.alias) if spider.name == 'jp_av': if item['slug'] in self.seenDict['jp_av']: raise DropItem('Duplicate item found: %s' % item) # alias = urllib.unquote(spider.slug) MongoCon.jp_av.items.insert(dict(item))
import sys import optparse from collections import Counter, namedtuple from operator import itemgetter, mul, attrgetter import multiprocessing import colorsys from PIL import Image as Im from PIL import ImageChops, ImageDraw from colormath.color_objects import RGBColor import requests from StringIO import StringIO from pymongo import Connection connection = Connection('localhost', 27017) db = connection.nSquared COLLECTION = 'thumbs' Color = namedtuple('Color', ['value', 'prominence']) Palette = namedtuple('Palette', 'colors bgcolor') WHITE = (255, 255, 255) BLACK = (0, 0, 0) # algorithm tuning N_QUANTIZED = 100 # start with an adaptive palette of this size MIN_DISTANCE = 10.0 # min distance to consider two colors different MIN_PROMINENCE = 0.01 # ignore if less than this proportion of image MIN_SATURATION = 0.05 # ignore if not saturated enough MAX_COLORS = 5 # keep only this many colors
import jieba.posseg as pseg import codecs from gensim import corpora, models, similarities import mysql.connector from pymongo import Connection con = Connection('localhost', 21001) db = con.noonde_api_development col = db.no_sql_airbnb_listings
def save(record): db['global'].save(record) if __name__ == "__main__": if (len(sys.argv) != 5): print "Usage: %s <username> <password> <url> <csv-files>" % sys.argv[0] username = sys.argv[1] password = sys.argv[2] url = sys.argv[3] file_list = sys.argv[4:] print username print password print len(file_list) if (len(url) == 0): connection = Connection() # Connect to localhost else: connection = Connection(url) # Connect to remote db db = Database(connection, 'zip') # Get zip database db.authenticate(username, password) # Authenticate for csv_file in file_list: # Add all the files add(csv_file)
def connect_mongodb(): connection = Connection('localhost', 27017); db = connection.wordnet return db
def __init__(self, directory='./', host='localhost', db='monfs', collection='objects'): self.conn = Connection(host) self.db = db self.collection = collection self.readDir(directory)
def test_rollback(self): """Test rollback in oplog_manager. Assertion failure if it doesn't pass We force a rollback by inserting a doc, killing the primary, inserting another doc, killing the new primary, and then restarting both. """ os.system('rm config.txt; touch config.txt') start_cluster() test_oplog, primary_conn, mongos, oplog_coll = self.get_new_oplog() solr = DocManager() test_oplog.doc_manager = solr solr._delete() # equivalent to solr.delete(q='*: *') obj1 = ObjectId('4ff74db3f646462b38000001') mongos['test']['test'].remove({}) mongos['test']['test'].insert({ '_id': obj1, 'name': 'paulie' }, safe=True) while (mongos['test']['test'].find().count() != 1): time.sleep(1) cutoff_ts = test_oplog.get_last_oplog_timestamp() obj2 = ObjectId('4ff74db3f646462b38000002') first_doc = { 'name': 'paulie', '_ts': bson_ts_to_long(cutoff_ts), 'ns': 'test.test', '_id': obj1 } #try kill one, try restarting killMongoProc(primary_conn.host, PORTS_ONE['PRIMARY']) new_primary_conn = Connection('localhost', int(PORTS_ONE['SECONDARY'])) admin = new_primary_conn['admin'] while admin.command("isMaster")['ismaster'] is False: time.sleep(1) time.sleep(5) count = 0 while True: try: current_conn = mongos['test']['test'] current_conn.insert({'_id': obj2, 'name': 'paul'}, safe=True) break except: count += 1 if count > 60: string = 'Call to insert doc failed too many times' logging.error(string) sys.exit(1) time.sleep(1) continue while (mongos['test']['test'].find().count() != 2): print(mongos['test']['test'].find().count()) time.sleep(1) killMongoProc(primary_conn.host, PORTS_ONE['SECONDARY']) startMongoProc(PORTS_ONE['PRIMARY'], "demo-repl", "/replset1a", "/replset1a.log", None) #wait for master to be established while primary_conn['admin'].command("isMaster")['ismaster'] is False: time.sleep(1) startMongoProc(PORTS_ONE['SECONDARY'], "demo-repl", "/replset1b", "/replset1b.log", None) #wait for secondary to be established admin = new_primary_conn['admin'] while admin.command("replSetGetStatus")['myState'] != 2: time.sleep(1) while retry_until_ok(mongos['test']['test'].find().count) != 1: time.sleep(1) self.assertEqual(str(new_primary_conn.port), PORTS_ONE['SECONDARY']) self.assertEqual(str(primary_conn.port), PORTS_ONE['PRIMARY']) last_ts = test_oplog.get_last_oplog_timestamp() second_doc = { 'name': 'paul', '_ts': bson_ts_to_long(last_ts), 'ns': 'test.test', '_id': obj2 } test_oplog.doc_manager.upsert(first_doc) test_oplog.doc_manager.upsert(second_doc) test_oplog.rollback() test_oplog.doc_manager.commit() results = solr._search() assert (len(results) == 1) results_doc = results[0] self.assertEqual(results_doc['name'], 'paulie') self.assertTrue(results_doc['_ts'] <= bson_ts_to_long(cutoff_ts)) #test_oplog.join() print("PASSED TEST ROLLBACK")
# timeRes = timeReg.search(fLine) # fileTime = 0 # if int(timeRes.group(2)) >= 30: # Only checking every half hour # fileTime = 30 # smallName = setFilename(oitName, timeRes) # smallFile = open(smallName, 'w') # smallFile.write(fLine) ######################################### # Starting MongoDB ######################################### # client = MongoClient('localhost', 27017) client = Connection() odb = client['oit-db'] cl_list = odb.collection_names() if len(cl_list) != 0: # if "oit_trace" in cl_list: # odb.drop_collection('oit_trace') if "oit_transition" in cl_list: odb.drop_collection('oit_transition') cl_general = odb['oit_trace'] cl_transition = odb['oit_transition'] kReg = re.compile(r"KERNEL")
from pymongo import Connection from queue import Empty, Queue from time import time from xmlrpc.server import SimpleXMLRPCServer import os import sys sys.path.append(os.getcwd()) from dmut.common.util import log db = Connection()["mutants"] mutants = db.fs.files q = Queue() log("Populating queue with mutants.") for mutant in mutants.find({"killed": False}): q.put(str(mutant["_id"])) log("Queue populated. Ready to serve.") start = time() def get(): try: return q.get() except Empty:
import os import unittest import datetime import json from models.BaseModel import BaseModel from models.EntityManager import EntityManager from pymongo import Connection import settings from models import Logger conn = Connection(settings.DBHOST, settings.DBPORT) _DBCON = conn.test class ChildModel(BaseModel): def __init__(self, DBCON, _id=None): self.fields = [ ('title', None), ('content', None), ('sort_field', None), ('tags', []), ('added', datetime.datetime.now()), ] self.init(DBCON, _id) class EntityManagerTest(unittest.TestCase): def setUp(self):
from pymongo import Connection import sys connection = Connection("localhost", 27017) class Id: def __init__(self, name): self.name = name def __str__(self): return "<{}>".format(self.name) class Tripple: def __init__(self, in_, relation, out): self.in_ = in_ if not isinstance(relation, Id): relation = Id(relation) self.relation = relation self.out = out def _format(self, node): if isinstance(node, Id): return str(node) x = str(node) x = x.replace("\\", "\\\\") if "\\" in x else x x = x.replace('"', '\\"') if '"' in x else x
def edit_card(request, html_key): template_card_id = request.GET.get("id", 0) mongoconn = Connection('182.92.163.118', 27017) cars_db = mongoconn['yaoqingka']["cards"] # print html_key item = cars_db.find_one({'_id': html_key}) # print item card = InviteCard() card.id = 0 card.recipient = item['recipient'] card.content = item['content'].replace('\n', '</br>') card.content = card.content.replace(' ', ' ') # print card.content card.addressor = item['addressor'] # print item['card_image'] image = item['card_image'].replace('http://qstatic.zuimeia.com/img/', '') image_top = item['image_top'].replace('http://qstatic.zuimeia.com/img/', '') image2 = item['card_image2'].replace('http://qstatic.zuimeia.com/img/', '') share_image = item['share_image'].replace( 'http://qstatic.zuimeia.com/img/', '') # print image card.card_image = image card.image_top = image_top card.card_image2 = image2 card.share_image = share_image card.word_postion = item['word_postion'] card.word_color = item['word_color'] card.bg_color = item['bg_color'] card.title_color = item['title_color'] card.meeting_time = item['meeting_time'] card.meeting_location = item['meeting_location'] card.title = item['title'] card.id = item['template_card_id'] card.content = card.content.replace('<p>', '') card.content = card.content.replace('</p>', '') card.content = card.content.replace('</br>', '\n') card.content = card.content.replace(' ', ' ') if request.POST: print request.POST recipient = request.POST.get('recipient') content = request.POST.get('content') addressor = request.POST.get('addressor') card_image = request.POST.get('card_image') image_top = request.POST.get('image_top') card_image2 = request.POST.get('card_image2') share_image = request.POST.get('share_image') word_postion = request.POST.get('word_postion') meeting_time = request.POST.get('meeting_time') meeting_location = request.POST.get('meeting_location') word_color = request.POST.get('word_color') bg_color = request.POST.get('bg_color') title_color = request.POST.get('title_color') template_card_id = request.POST.get('template_card_id') title = request.POST.get('title') # 保存内容生成唯一页面, 存入mongodb chars = (string.ascii_letters + string.digits).lower() html_key = ''.join(random.sample(chars, 20)) # print recipient, content, addressor, card_image item = {} item['_id'] = html_key item['recipient'] = recipient item['content'] = content item['addressor'] = addressor item['card_image'] = card_image item['image_top'] = image_top item['card_image2'] = card_image2 item['share_image'] = share_image item['word_postion'] = word_postion item['meeting_time'] = meeting_time item['meeting_location'] = meeting_location item['word_color'] = word_color item['bg_color'] = bg_color item['title_color'] = title_color item['template_card_id'] = template_card_id item['title'] = title item['created_at'] = datetime.now() cars_db.insert(item) # html_key = 'xxxx' return HttpResponseRedirect('/yqk/card/show/%s/?id=%d' % (html_key, int(template_card_id))) edit_template = False return render_to_response('yaoqingka/write_card.html', locals(), context_instance=RequestContext(request))
# python 1.py tools/libxl/libxl.c libxl 372 import sys from pymongo import Connection connect = Connection('127.0.0.1', 27017) db = connect.satpg collect = db.satpg argvs = sys.argv argc = len(argvs) f = open(argvs[1]) line = f.readline() print "digraph sample {" print "START[color=red penwidth=5];" print "EMPTY[shape=box color=red penwidth=5];" while line: line2 = line.strip("*").replace("*","") tmp = line2.split(",") try: ret = collect.find({"src" : tmp[2]}) for doc in ret: