我正在尝试提高SQLAlchemy数据库查询的性能.我们正在使用psycopg2.在我们的生产系统中,我们选择使用Java,因为它速度提高了至少50%,即使不是接近100%.所以我希望Stack Overflow社区中的某个人能够提高我的表现.
我认为我的下一步将是最终修补psycopg2库,使其行为类似于JDBC驱动程序.如果是这种情况并且有人已经这样做了,那就没问题,但我希望我仍然可以通过Python进行设置或重构调整.
我有一个简单的"SELECT*FROM someLargeDataSetTable"查询运行.数据集的大小为GB.快速表现图如下:
Records | JDBC | SQLAlchemy[1] | SQLAlchemy[2] | Psql
--------------------------------------------------------------------
1 (4kB) | 200ms | 300ms | 250ms | 10ms
10 (8kB) | 200ms | 300ms | 250ms | 10ms
100 (88kB) | 200ms | 300ms | 250ms | 10ms
1,000 (600kB) | 300ms | 300ms | 370ms | 100ms
10,000 (6MB) | 800ms | 830ms | 730ms | 850ms
100,000 (50MB) | 4s | 5s | 4.6s | 8s
1,000,000 (510MB) … 以下将起作用,但我宁愿不需要__hash__在每个子类中重复。有没有办法告诉数据类继承哈希函数(即不将其设置为None)?
from dataclasses import dataclass
@dataclass
class Hashable:
def __hash__(self):
hashed = hash((
getattr(self, key)
for key in self.__annotations__
))
return hashed
@dataclass
class Node(Hashable):
name: str = 'Undefined'
def __hash__(self):
return Hashable.__hash__(self)
Run Code Online (Sandbox Code Playgroud) 我遇到了我认为是一个错误,我正在寻找确认或我不明白这个方法是如何工作的.
这是我的基本输出:
(Pdb) x = 'KEY_K'
(Pdb) x.lstrip('K')
'EY_K'
(Pdb) x.lstrip('KE')
'Y_K'
(Pdb) x.lstrip('KEY')
'_K'
(Pdb) x.lstrip('KEY_')
''
(Pdb) import sys
(Pdb) sys.version
'2.7.11 (default, Dec 5 2015, 14:44:47) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)]'
Run Code Online (Sandbox Code Playgroud)
我的理解是,该示例中的最终'lstrip'应该返回'K',但事实并非如此.有谁知道为什么?
我正在为Luigi Tasks构建一个包装器,我遇到了一个Register实际上是ABC元类的类,而且当我创建一个动态时它是不可挑选的type.
以下代码或多或少是我用来开发动态类的代码.
class TaskWrapper(object):
'''Luigi Spark Factory from the provided JobClass
Args:
JobClass(ScrubbedClass): The job to wrap
options: Options as passed into the JobClass
'''
def __new__(self, JobClass, **options):
# Validate we have a good job
valid_classes = (
ScrubbedClass01,
# ScrubbedClass02,
# ScrubbedClass03,
)
if any(vc == JobClass for vc in valid_classes) or not issubclass(JobClass, valid_classes):
raise TypeError('Job is not the correct class: {}'.format(JobClass))
# Build a luigi task class dynamically
luigi_identifier = 'Task' …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮我解决我的ARM + GCC + UCLIBC链接问题与crossdev?
同时发布到Gentoo论坛:http://forums.gentoo.org/viewtopic-t-925012.html
最近,我被分配到一个项目,该项目包含使用旧的GCC和OABI开发的可执行文件.作为参考,这里是来自可执行文件的readelf的头输出,它在系统上运行得很好:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: ARM
ABI Version: 0
Type: EXEC (Executable file)
Machine: ARM
Version: 0x1
Entry point address: 0x9464
Start of program headers: 52 (bytes into file)
Start of section headers: 540956 (bytes into file)
Flags: 0x202, has entry point, GNU EABI, software …Run Code Online (Sandbox Code Playgroud) 我花了最近几个星期的时间来看看openGL.虽然我对一些较旧的NeHe示例没有问题,但从我读过的所有内容来看,OpenGL4是一个完全不同的过程.我可以访问红皮书和超级圣经,但前者仍然提供传统的opengl调用,后者使用自己的库.在理解如何将代码放在项目中时,两者都没有特别有用.例如,我目前的理解是glu和glut是遗留的,不应该用于opengl 4.
我可以非常容易地为假设的模型空间生成顶点.我很难理解模型最终会如何出现在我的屏幕上.大约95%的尝试最终都是黑屏.
提前致谢.
这是一些代码:
# primatives.py
from collections import Iterable
from functools import reduce
import operator
import numpy as np
from exc import UnimplementedMethod
class Primative(object):
SIZE = 1 # number of pixels on a default grid
def __init__(self, point=None, *args, **kwargs):
self.point = point if isinstance(point, Iterable) else [0, 0, 0]
self.point = np.array(self.point, dtype=np.float32)
scaler = [self.SIZE/2]*len(self.point)
self.point = (self.point * scaler).tolist()
@property
def active(self):
attr = "__active__"
if not hasattr(self, attr):
setattr(self, attr, False)
return getattr(self, …Run Code Online (Sandbox Code Playgroud) 我的期望是pypy可能比python快一个数量级,但结果表明pypy实际上比预期慢.
我有两个问题:
结果时间:
Python 2.7.5
Pypy 2.2.1
算法:
我正在使用一个简单的算法生成一个空间点列表,我正在尝试优化算法.
def generate(size=32, point=(0, 0, 0), width=32):
"""
generate points in space around a center point with a specific width and
number of divisions (size)
"""
X, Y, Z = point
half = width * 0.5
delta = width
scale = width / size
offset = scale * 0.5
X = X …Run Code Online (Sandbox Code Playgroud) 我有以下简化代码:
async def asynchronous_function(*args, **kwds):
statement = await prepare(query)
async with conn.transaction():
async for record in statement.cursor():
??? yield record ???
...
class Foo:
def __iter__(self):
records = ??? asynchronous_function ???
yield from records
...
x = Foo()
for record in x:
...
Run Code Online (Sandbox Code Playgroud)
我不知道如何填写???上面的内容。我想产生记录数据,但是如何包装asyncio代码真的不是很明显。
欲望:
我想要一种方法来合并两个数据帧并保留指定数据帧中的非相交数据。
问题:
我有重复的数据,我希望这一行能够删除重复的数据:
final_df = new_df[~new_df.isin(previous_df)].dropna()
Run Code Online (Sandbox Code Playgroud)
示例数据及数据测试:
record = Record(1000, 9300815, '<redacted type>', '<redacted id>')
test_df = pd.DataFrame([record])
if not final_df.empty:
# this produces an empty data frame
empty_df = test_df[test_df.isin(final_df)].dropna()
# this produces the record
record_df = final_pdf[final_pdf.col01 == record.col01]
Run Code Online (Sandbox Code Playgroud)
背景:
我正在加载 xml 数据并将 xml 文件转换为几种不同的记录类型作为命名元组。我将每个记录类型拆分为自己的数据帧。previous_df然后,我通过如下构造将 xml 文件中的当前数据集与已加载到数据库中的数据进行比较:
previous_df = pd.read_sql_table(table_name, con=conn, schema=schema, columns=columns)
Run Code Online (Sandbox Code Playgroud)
列是根据命名元组中的字段动态创建的。数据库模式是使用 sqlalchemy 生成的,UniqueConstraint当我认为数据库中存在重复项时,我添加了管理。
预先感谢您提供的任何帮助。
我在下面有一些代码试图更新GTK Label元素.我包括两个文件:ui文件和py文件.
UI文件:
<glade-interface>
<widget class="GtkWindow" id="ApplicationFrame">
<property name="width_request">320</property>
<property name="height_request">240</property>
<property name="visible">True</property>
<property name="events">GDK_KEY_PRESS_MASK</property>
<property name="title" translatable="yes">Simple</property>
<property name="resizable">False</property>
<property name="window_position">center-always</property>
<property name="default_width">320</property>
<property name="default_height">240</property>
<property name="decorated">False</property>
<property name="gravity">center</property>
<child>
<widget class="GtkFixed" id="layout">
<property name="width_request">320</property>
<property name="height_request">240</property>
<property name="visible">True</property>
<child>
<widget class="GtkLabel" id="l1">
<property name="width_request">320</property>
<property name="height_request">40</property>
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">l1</property>
</widget>
<packing>
<property name="y">43</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
Run Code Online (Sandbox Code Playgroud)
Python文件
import os
from time import sleep as wait
import gtk as gtk
import gtk.glade …Run Code Online (Sandbox Code Playgroud) 这是一个错误吗?
#!/usr/bin/env python3.6
# filename: tmp.py
import sys
print(sys.argv)
Run Code Online (Sandbox Code Playgroud)
调用:
python tmp.py find . -name '*.py'
Run Code Online (Sandbox Code Playgroud)
实际输出:
['tmp.py', 'find', '.', '-name', '*.py']
Run Code Online (Sandbox Code Playgroud)
预期输出:
['tmp.py', 'find', '.', '-name', "'*.py'"]
Run Code Online (Sandbox Code Playgroud)
请注意实际输出中缺少引用。
有没有办法在 tox.ini 中设置任意变量?
一个例子是可以以多种方式使用的项目名称。使用相当复杂的 tox.ini,我发现自己复制并粘贴到我应该只需要在顶部设置一个变量的地方。
作为参考,示例 tox.ini:
[tox]
envlist = clean, py{27,35,py}, license, style
skipsdist = True
skip_missing_interpreters = True
sitepackages = False
[testenv:clean]
deps = coverage
skip_install = true
commands =
hash -r
find {toxinidir} -name '*.pyc' -delete
find {toxinidir} -name '__pycache__' -delete
coverage erase
rm -Rf {toxinidir}/docs/_build {toxinidir}/docs/coverage {toxinidir}/docs/reports
[testenv]
passenv = *
whitelist_externals = *
install_command = {envpython} -m pip install -q --process-dependency-links {opts} {packages}
envdir = {env:WORKON_HOME}/tox-<project_name>/{envname}
sitepackages = False
recreate = True
commands =
# …Run Code Online (Sandbox Code Playgroud) 我想建立一个基本上在sql中看起来像这样的索引:
CREATE INDEX IF NOT EXISTS new_index ON schema.tablename USING gist (tsrange(start, "end"))
Run Code Online (Sandbox Code Playgroud)
我的声明性ORM模型如下所示:
import sqlalchemy as sa
class Tablename(Mixins):
__table_args__ = (
sa.Index('index_name', postgresql_using="gist"), # ????
{'schema': 'schema'}
)
start = sa.Column(pg.TIMESTAMP, autoincrement=False, primary_key=True)
end = sa.Column(pg.TIMESTAMP, nullable=False)
Run Code Online (Sandbox Code Playgroud)
然后,我想使用alembic其中应包括降级的内容,例如:
op.drop_index('index', 'tablename', schema='schema')
Run Code Online (Sandbox Code Playgroud)
有效地具有以下SQL:
DROP INDEX IF EXISTS schema.index
Run Code Online (Sandbox Code Playgroud) python ×10
postgresql ×3
performance ×2
sqlalchemy ×2
arm ×1
data-class ×1
gcc ×1
gentoo ×1
gtk ×1
hash ×1
iterator ×1
jdbc ×1
luigi ×1
methods ×1
numpy ×1
opengl-4 ×1
optimization ×1
pandas ×1
pickle ×1
pipeline ×1
psycopg2 ×1
pyglet ×1
pygtk ×1
pypy ×1
rendering ×1
shell ×1
string ×1
tox ×1
uclibc ×1