<commit_before># -*- coding: utf-8 -*-

import random, json, time
from datetime import datetime, timedelta

import gevent
from pymongo import MongoClient


execfile("../conf/config.txt")
client = MongoClient(MONGO_SRV, 27017)
db = client.act
db.authenticate(MONGO_USR, MONGO_PSW)


def T():
    return time.strftime("%Y/%m/%d %H:%M:%S")

g_uid2name = {}
def cacheName():
    global g_uid2name
    ret = db.uname.find()
    for q in ret:
        g_uid2name[q["uid"]] = q["name"]
cacheName()

### 

def setName(**kwargs):
    global g_uid2name, g_updateName
    uid = kwargs["uid"]
    name = kwargs["name"]
    g_uid2name[uid] = name
    g_updateName[uid] = name
    #db.uname.update({"uid": uid}, {"uid": uid, "name": name, "time": datetime.now()}, upsert=True)
    return {}, None


g_updateName = {}
def updateSetName():
    global g_updateName
    while True:
        gevent.sleep(3)
        if len(g_uid2name) == 0:
            continue
        uids = []
        new_items = []
        for u, n in g_updateName.iteritems():
            uids.append(u)
            new_items.append({"uid": u, "name": n, "time": datetime.now()})
        db.uname.remove({"uid": {"$in" :uids}})
        db.uname.insert(new_items)
        g_updateName = {}


def randomMotion(**kwargs):
    count = kwargs["count"]
    pos = random.sample(range(db.motion.count()), count)
    ret = []
    for p in pos:
        ret.append(db.motion.find({}, {"_id": 0}).skip(p).limit(1).next())
    return ret, None


def randomPunish(**kwargs):
    count = kwargs["count"]
    pos = random.sample(range(db.punish.count()), count)<commit_msg>check db empty bulk insert<commit_after># -*- coding: utf-8 -*-

import random, json, time
from datetime import datetime, timedelta

import gevent
from pymongo import MongoClient


execfile("../conf/config.txt")
client = MongoClient(MONGO_SRV, 27017)
db = client.act
db.authenticate(MONGO_USR, MONGO_PSW)


def T():
    return time.strftime("%Y/%m/%d %H:%M:%S")

g_uid2name = {}
def cacheName():
    global g_uid2name
    ret = db.uname.find()
    for q in ret:
        g_uid2name[q["uid"]] = q["name"]
cacheName()

### 

def setName(**kwargs):
    global g_uid2name, g_updateName
    uid = kwargs["uid"]
    name = kwargs["name"]
    g_uid2name[uid] = name
    g_updateName[uid] = name
    return {}, None


g_updateName = {}
def updateSetName():
    global g_updateName
    while True:
        gevent.sleep(3)
        if len(g_uid2name) == 0:
            continue
        uids = []
        new_items = []
        for u, n in g_updateName.iteritems():
            uids.append(u)
            new_items.append({"uid": u, "name": n, "time": datetime.now()})
        if len(uids) > 0:
            db.uname.remove({"uid": {"$in" :uids}})
            db.uname.insert(new_items)
            g_updateName = {}


def randomMotion(**kwargs):
    count = kwargs["count"]
    pos = random.sample(range(db.motion.count()), count)
    ret = []
    for p in pos:
        ret.append(db.motion.find({}, {"_id": 0}).skip(p).limit(1).next())
    return ret, None


def randomPunish(**kwargs):
    count = kwargs["count"]
    pos = random.sample(range(db.punish.count()), count)
{'fraction_non_alphanumeric': 0.16577202399747396, 'fraction_numerical': 0.010419955794126934, 'mean_word_length': 2.84, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6088657', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1110, 'n_words': 247}