本文整理汇总了Python中psycopg2.extensions.parse_dsn方法的典型用法代码示例。如果您正苦于以下问题:Python extensions.parse_dsn方法的具体用法?Python extensions.parse_dsn怎么用?Python extensions.parse_dsn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.parse_dsn方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_conn_id
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import parse_dsn [as 别名]
def get_conn_id(conn):
"""
Extract application_name from dsn
"""
parsed = parse_dsn(conn.dsn)
return parsed['application_name']
示例2: collect_psycopg2_metrics
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import parse_dsn [as 别名]
def collect_psycopg2_metrics(context, trace, instance, args):
try:
from psycopg2.extensions import parse_dsn
except ImportError: # pragma: no cover
from .dbapi import parse_dsn
connection = instance.connection_proxy
dsn = parse_dsn(connection.dsn)
db = dsn.get("dbname")
hostname = dsn.get("host", "localhost")
port = dsn.get("port", 5432)
command, table = None, None
query = sqlparse.parse(instance.query)
if not query and args:
query = sqlparse.parse(args[0])
if query:
query = query[0]
command = query.get_type()
table = query.get_name()
request = Request(
command=ensure_utf8(command),
key=None,
hostname=ensure_utf8(hostname),
port=ensure_utf8(port),
connectionName=None,
db=ensure_utf8(db),
table=ensure_utf8(table),
)
request = request._asdict()
context.iopipe.mark.db_trace(trace, "postgresql", request)
示例3: _set_attributes_from_cursor
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import parse_dsn [as 别名]
def _set_attributes_from_cursor(span: trace.Span, vendor, cursor):
"""Attempt to set db connection attributes by introspecting the cursor."""
if vendor == "postgres":
# pylint: disable=import-outside-toplevel
from psycopg2.extensions import parse_dsn
if hasattr(cursor, "connection") and hasattr(cursor.connection, "dsn"):
dsn = getattr(cursor.connection, "dsn", None)
if dsn:
data = parse_dsn(dsn)
span.set_attribute(_DB, data.get("dbname"))
span.set_attribute(_HOST, data.get("host"))
span.set_attribute(_PORT, int(data.get("port")))
示例4: test_dsn
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import parse_dsn [as 别名]
def test_dsn(engine, pg_params):
params = pg_params.copy()
params['password'] = 'xxx'
params['dbname'] = params.pop('database')
params['port'] = str(params['port'])
assert parse_dsn(engine.dsn) == params
示例5: load
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import parse_dsn [as 别名]
def load(self, features=None, nrows=None):
self.categoricals = {}
self.transforms_log = [[],[],[],[]]
import csv
from io import StringIO
path = self.options['data_path']
if isinstance(path, StringIO):
path.seek(0)
self.df = pd.read_csv(path, encoding='utf-8', escapechar="\\", usecols=features, na_values=['?'], nrows=nrows)
if self.options.get("targetFeature") in self.df.columns:
self.dropna([self.options["targetFeature"]])
else:
if path.startswith("jdbc:"):
import psycopg2
from psycopg2.extensions import parse_dsn
path = path.replace('sslfactory=org.postgresql.ssl.NonValidatingFactory&', '')
ary = path.split('tablename')
path = ary[0]
tablename = ary[1]
dataset_name = tablename
self.dbconn_args = parse_dsn(path[5:])
conn = psycopg2.connect(**self.dbconn_args)
self.df = pd.read_sql("select * from %s"%tablename, con=conn)
else:
path, remote_path = self._check_remote_path()
try:
self.df = self.load_from_file(path, features=features, nrows=nrows)
except:
if remote_path:
logging.exception("Loading local file failed. Download it again...")
self.options['data_path'] = remote_path
path, remote_path = self._check_remote_path(force_download=True)
self.df = self.load_from_file(path, features=features, nrows=nrows)
else:
raise
self.dataset_name = os.path.basename(path)
if self.options.get("targetFeature") in self.df.columns:
self.dropna([self.options["targetFeature"]])
return self