本文整理汇总了Python中db.Db.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Db.connect方法的具体用法?Python Db.connect怎么用?Python Db.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类db.Db
的用法示例。
在下文中一共展示了Db.connect方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _record_whorls
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def _record_whorls(whorls):
db = Db()
db.connect()
# actually update the fingerprint table...
db.record_fingerprint(whorls)
md5_whorls = FingerprintHelper.value_or_md5(whorls)
db.update_totals(md5_whorls)
示例2: epoch_calculate_totals
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def epoch_calculate_totals(epoch_beginning):
db = Db()
db.connect()
old_epoch_beginning = db.get_epoch_beginning()
columns_to_update = FingerprintHelper.whorl_names.keys()
columns_to_update.append('signature')
db.epoch_calculate_totals(
old_epoch_beginning, epoch_beginning, columns_to_update, FingerprintHelper.md5_keys)
示例3: single_whorl_uniqueness
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def single_whorl_uniqueness(whorl_name, whorl_value):
print "here"
db = Db()
db.connect()
md5_whorl_value = FingerprintHelper.value_or_md5({whorl_name: whorl_value})[whorl_name]
try:
count = db.get_whorl_value_count(whorl_name, md5_whorl_value, config.epoched)
except TypeError:
return {"status": "Error: that value has not yet been recorded for '" + whorl_name + "'"}
total = db.get_total_count(config.epoched)
uniqueness = {"bits": round(-log(count / float(total), 2), 2), "one_in_x": round(float(total) / count, 2)}
return uniqueness
示例4: _fetch_counts
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def _fetch_counts(whorls):
db = Db()
db.connect()
# result dict. 'total' is the total number of entries, 'sig_matches' is
# the number matching signature in whorls, other keys are variables in
# signature.
counts = {}
# query an incrementally updated totals table which has an index on
# unique (variable, value)
md5_whorls = FingerprintHelper.value_or_md5(whorls)
for i in FingerprintHelper.whorl_names:
counts[i] = db.get_whorl_value_count(i, md5_whorls[i], config.epoched)
total = db.get_total_count(config.epoched)
matching = db.get_signature_matches_count(whorls["signature"], config.epoched)
return counts, total, matching
示例5: record_tracking_results
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def record_tracking_results(cookie, results, ip_addr, key):
db = Db()
db.connect()
valid_print = {}
for i in results:
if i in TrackingHelper.valid_fields.keys():
valid_print[i] = results[i]
ip, google_style_ip = get_ip_hmacs(ip_addr, key)
db.record_tracking_results(
cookie,
ip,
google_style_ip,
results['ad'],
results['tracker'],
results['dnt'],
results['known_blockers'])
return True
示例6: _need_to_record
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
def _need_to_record(cookie, signature, ip_addr, key):
db = Db()
db.connect()
if cookie:
# okay, we have a cookie; check whether we've seen it with this
# fingerprint before
seen = db.count_sightings(cookie, signature)
write_cookie = cookie
else:
seen = 0 # always count cookieless browsers
# we need to log the HMAC'd IP even if there's no cookie so use a
# dummy
write_cookie = 'no cookie'
# now log the cookie, along with encrypted versions of the IP address
# and a quarter-erased IP address, like the one Google keeps
ip, google_style_ip = get_ip_hmacs(ip_addr, key)
db.record_sighting(
write_cookie, signature, ip, google_style_ip)
return seen == 0
示例7: Pyrefly
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
class Pyrefly(Handler):
def __init__(self, config):
self.config = config
self.client = Client(self.config.get('id'))
self.client.addHandler(self)
self.db = Db('pyrefly', self.config.get('account', 'db'), self.config.get('password', 'db'), self.config.get('spreadsheet', 'db'))
self.plugins = {}
self.pluginModules = {}
self.handlers = [Core(self)]
# Set up the import path for plugins
myPath = os.path.abspath(__file__)
pluginPath = os.path.join(myPath.rsplit(os.sep, 1)[0], "plugins")
print "Path for plugins is: %s" % pluginPath
sys.path.append(pluginPath)
def connect(self):
self.db.connect()
result, err = self.client.connect(self.config.get('password'), 'bot' + self.config.hash[:6])
if not result:
print "Error connecting: %s" % err
exit(1)
def initialize(self):
toJoin = []
for mucId in self.config.getRoomList():
toJoin.append({'muc': mucId, 'nick': self.config.get('nick', mucId), 'password': ''})
for mucToJoin in toJoin:
muc = self.join(mucToJoin['muc'], mucToJoin['nick'], password=mucToJoin['password'])
if muc is not None:
muc.data = mucToJoin
def join(self, muc, nick, password=''):
return self.client.join(muc, nick, password=password)
def process(self, timeout=0.1):
self.client.process(timeout=timeout)
return True
def onMucMessage(self, *args, **kwargs):
for handler in self.handlers:
handler.onMucMessage(*args, **kwargs)
def registerHandler(self, handler):
self.handlers.append(handler)
def unregisterHandler(self, handler):
self.handlers.remove(handler)
def loadPlugin(self, name):
if name in self.plugins:
return (False, "Plugin %s is already loaded" % name)
source = None
if name in self.pluginModules:
source = "reloaded"
reload(self.pluginModules[name])
else:
importName = name.lower()
source = "imported"
try:
self.pluginModules[name] = __import__(importName, globals(), locals(), [], 0)
except ImportError:
return (False, "No such module: %s" % importName)
if not self.pluginModules[name]:
print "import failed!"
del self.pluginModules[name]
return (False, "Module not defined after import")
print "Imported: %s" % self.pluginModules[name]
try:
clazz = getattr(self.pluginModules[name], name)
except AttributeError:
return (False, "Module has no class defined")
print "Class: %s" % clazz
if not clazz:
return (False, "Class not defined after import")
self.plugins[name] = clazz()
self.plugins[name].onLoad(self)
return (True, source)
def unloadPlugin(self, name):
if name not in self.plugins:
return (False, None, "not loaded")
toUnload = []
unloaded = []
for pluginName, plugin in self.plugins.items():
if name in plugin.getDependencies():
toUnload.append(pluginName)
for pluginName in toUnload:
result, extraUnloaded, err = self.unloadPlugin(pluginName)
for unloadedName in extraUnloaded:
unloaded.append(unloadedName)
if not result:
#.........这里部分代码省略.........
示例8: Pyrefly
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
class Pyrefly(Handler):
def __init__(self, config):
self.config = config
self.client = Client(self.config.get("id"))
self.client.addHandler(self)
self.db = Db(
"pyrefly",
self.config.get("account", "db"),
self.config.get("password", "db"),
self.config.get("spreadsheet", "db"),
)
self.plugins = {}
self.pluginModules = {}
self.dispatcher = Dispatcher()
self.handlers = [Core(self), self.dispatcher]
# Set up the import path for plugins
myPath = os.path.abspath(__file__)
pluginPath = os.path.join(myPath.rsplit(os.sep, 1)[0], "plugins")
print "Path for plugins is: %s" % pluginPath
sys.path.append(pluginPath)
def connect(self):
self.db.connect()
result, err = self.client.connect(self.config.get("password"), "bot" + self.config.hash[:6])
if not result:
print "Error connecting: %s" % err
exit(1)
def initialize(self):
joinMap = {}
table = self.db.table("rooms")
if table is not None:
rows = table.get({"autojoin": "y"})
for row in rows:
joinMap[row["muc"]] = row
for mucId in self.config.getRoomList():
if mucId not in joinMap:
group = self.config.get("group", mucId)
if not group:
group = "global"
nick = self.config.get("nick", mucId)
name = mucId.split("@")[0]
data = {
"name": name,
"muc": mucId,
"nick": nick,
"password": "",
"group": group,
"autojoin": "y",
"control": "n",
}
joinMap[mucId] = data
if table is not None:
table.put(data)
for mucToJoin in joinMap.values():
muc = self.join(mucToJoin["muc"], mucToJoin["nick"], password=mucToJoin["password"])
if muc is not None:
muc.data = mucToJoin
def join(self, muc, nick, password=""):
return self.client.join(muc, nick, password=password)
def process(self, timeout=0.1):
self.client.process(timeout=timeout)
return True
def onMucMessage(self, *args, **kwargs):
for handler in self.handlers:
handler.onMucMessage(*args, **kwargs)
def registerHandler(self, handler):
self.handlers.append(handler)
def unregisterHandler(self, handler):
self.handlers.remove(handler)
def loadPlugin(self, name):
if name in self.plugins:
return (False, "Plugin %s is already loaded" % name)
source = None
if name in self.pluginModules:
source = "reloaded"
reload(self.pluginModules[name])
else:
importName = name.lower()
source = "imported"
try:
self.pluginModules[name] = __import__(importName, globals(), locals(), [], 0)
except ImportError:
return (False, "No such module: %s" % importName)
if not self.pluginModules[name]:
del self.pluginModules[name]
return (False, "Module not defined after import")
try:
clazz = getattr(self.pluginModules[name], name)
except AttributeError:
#.........这里部分代码省略.........
示例9: Pyrefly
# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import connect [as 别名]
class Pyrefly(Handler):
def __init__(self, config):
self._config = config
self._client = XmppClient(self._config.get('id'))
self._db = Db('pyrefly', self._config.get('account', 'db'), self._config.get('password', 'db'), self._config.get('spreadsheet', 'db'))
self._plugins = {}
self._pluginModules = {}
self._dispatcher = Dispatcher()
self._broadcaster = EventBroadcaster()
self._broadcaster.addHandler(Core(self))
self._broadcaster.addHandler(self._dispatcher)
self._client.addHandler(self._broadcaster)
# Set up the import path for plugins
myPath = os.path.abspath(__file__)
self._pluginPath = os.path.join(myPath.rsplit(os.sep, 1)[0], "plugins")
print "Path for plugins is: %s" % self._pluginPath
sys.path.append(self._pluginPath)
def getDispatcher(self):
return self._dispatcher
def getDb(self):
return self._db
def getClient(self):
return self._client
def connect(self):
self._db.connect()
result, err = self._client.connect(self._config.get('password'), 'bot' + self._config.hash[:6])
if not result:
print "Error connecting: %s" % err
exit(1)
print "Connected!"
def initialize(self):
joinMap = {}
table = self._db.table('rooms')
if table is not None:
rows = table.get({'autojoin': 'y'})
for row in rows:
joinMap[row['muc']] = row
for mucId in self._config.getRoomList():
if mucId not in joinMap:
group = self._config.get('group', mucId)
if not group:
group = 'global'
nick = self._config.get('nick', mucId)
name = mucId.split('@')[0]
data = {'name': name, 'muc': mucId, 'nick': nick, 'password': '', 'group': group, 'autojoin': 'y', 'control': 'n'}
joinMap[mucId] = data
if table is not None:
table.put(data)
for mucToJoin in joinMap.values():
muc = self.join(mucToJoin['muc'], mucToJoin['nick'], password=mucToJoin['password'])
if muc is not None:
muc.data = mucToJoin
def join(self, muc, nick, password=''):
return self._client.join(muc, nick, password=password)
def process(self, timeout=0.1):
self._client.process(timeout=timeout)
return True
def registerHandler(self, handler):
self._broadcaster.addHandler(handler)
def unregisterHandler(self, handler):
self._broadcaster.removeHandler(handler)
def _tryCompile(self, pluginName):
pluginPath = "%s/%s.py" % (self._pluginPath, pluginName)
print "Compiling: %s" % pluginPath
try:
res = py_compile.compile(pluginPath, "%sc" % pluginPath, pluginPath, True)
return None
except py_compile.PyCompileError as err:
return str(err.msg).strip()
def loadPlugin(self, name):
if name in self._plugins:
return (False, "Plugin %s is already loaded" % name)
source = None
res = self._tryCompile(name.lower())
if res is not None:
return (False, res)
if name in self._pluginModules:
source = "reloaded"
reload(self._pluginModules[name])
else:
importName = name.lower()
source = "imported"
try:
self._pluginModules[name] = __import__(importName, globals(), locals(), [], 0)
except ImportError:
return (False, "No such module: %s" % importName)
#.........这里部分代码省略.........