我有一个用Python编写的应用程序,并使用PyInstaller进行"编译".它还使用PyQt作为GUI框架.
在主窗口加载并显示之前,运行此应用程序会有大约10秒的延迟.据我所知,这不是因为我的代码缓慢.相反,我怀疑这是由于Python运行时初始化.
问题是该应用程序是使用自定义的laucncher /任务栏应用程序启动的.用户将单击按钮以启动应用程序,看不到任何内容,并单击其他应用程序上的其他位置.当我的应用程序显示它的窗口时,由于SetForegroundWindow的规则,它无法到达前台.
我可以访问PyInstaller win32加载器,Python代码甚至启动器代码的源代码.
我的问题是:
如何让这个应用程序更快启动?
如何衡量流程生命周期的前几秒花费的时间?
在第一个窗口显示之前减少时间的普遍接受的技术是什么?
我想避免添加启动画面有两个原因 - 一个,我希望它不会有帮助(开销是在Python代码运行之前)和两个,我只是不喜欢启动画面:)
如果我需要,我可能编辑PyInstaller加载器存根来创建一个窗口,但这是另一条我不想采取的路径.
我想使用orm在SQLAlchemy中复制模型实例(行).我的第一个想法是这样做:
i = session.query(Model)
session.expunge(i)
old_id = i.id
i.id = None
session.add(i)
session.flush()
print i.id #New ID
Run Code Online (Sandbox Code Playgroud)
然而,显然分离的对象仍然"记住"它具有的id,即使我在分离时将id设置为None.因此,session.flush()尝试执行UPDATE,将主键更改为null.
这是预期的行为吗?如何删除此属性的"内存",并在将重新添加到会话时将其作为新对象处理?一般来说,如何克隆SQLAlchemy模型实例?
我有两个模型,一个MainModel和一个相关的InlineModel,我想在管理员中显示为内联.此InlineModel可用于,例如,记录模型,并应跟踪登录的管理员用户进行更改.虽然这看起来很简单(事实上,当用户字段是MainModel的一部分时,文档显示了这个例子),当字段在Inline上时,我似乎无法掌握它.
具体来说,我的目标是:
我的问题是:
以下是我目前的想法:
#models.py
class MainModel(models.Model):
some_info = models.IntegerField()
class InlineModel(models.Model):
main = models.ForeignKey(MainModel)
data = models.CharField(max_length=255)
user = models.ForeignKey('auth.User')
#admin.py
class InlineModelInline(admin.TabularInline):
model = InlineModel
fields = ('data', 'user')
#readonly_fields = ('data', 'user') #Bonus question later
class MainModelAdmin(admin.ModelAdmin):
list_display = ('id', 'some_info')
inlines = [InlineModelInline]
#def save_model(self, request, obj, form, change):
#http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
#Only called for MainModel, not for any of the inlines
#Otherwise, would be ideal
def save_formset(self, request, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用三段代码:
comobj.dll托管一个COM对象(比方说'MainInteract'),我想从Python中使用它.我已经可以在IronPython中完美地使用这个对象,但由于其他要求,我需要从常规Python中使用它.我相信这里最好的方法是使用win32com,但我根本无法取得任何进展.
首先,一些有效的IronPython代码:
import clr
import os
import sys
__dir__ = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, __dir__)
sys.path.append(r"C:\Path\To\comobj.dll") #This is where the com object dll actually is
clr.AddReferenceToFileAndPath(os.path.join(__dir__, r'comobj_1_1.dll')) #This is the .NET interop assembly that was created automatically via SharpDevelop's COM Inspector
from comobj_1_1 import clsMainInteract
o = clsMainInteract()
o.DoStuff(True)
Run Code Online (Sandbox Code Playgroud)
现在,我在常规Python中尝试的代码:
>>> import win32com.client
>>> win32com.client.Dispatch("{11111111-comobj_guid_i_got_from_com_inspector}")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File …Run Code Online (Sandbox Code Playgroud) 我有一个名为的父表pbx_point有一个point_type列.我还有一个名为的子表pbx_route,其中一个列称为point_id指向pbx_point.
我想使用sqlalchemy的连接表继承来通过声明性基础关联这两个表,并使用多态继承
这样可以正常工作 - 或者更确切地说,如果不是以下附加约束,它也会工作:pbx_point还有一个名为initial_route_id指向的外键pbx_route.
我也在下面使用反射,但db就像我上面所描述的那样.我得到的错误是sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'pbx_point' and 'pbx_route'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly..
这是有道理的,因为"幕后"decarative base正在两个映射类上创建一个relationship()属性.我希望它选择pbx_route.point_id作为parent_id链接,但它也会看到pbx_point.initial_route_id列.如果我正在创建这种关系(),这很容易修复,但我不是 - 声明性继承系统.
我可以传递一个额外的参数__mapper_args__,比如polymorphic_parent_col哪个让我指定我想要的外键?如果没有,我该如何解决这个问题?
谢谢.
class MyBase(DeferredReflection):
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
Base = declarative_base(cls=MyBase)
class pbx_point(Base):
__mapper_args__ = dict(
polymorphic_on='point_type', …Run Code Online (Sandbox Code Playgroud) 我以前有这样的模型:
class AssemblyAnnotation(models.Model):
assembly = models.ForeignKey(Assembly)
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if not self.type.can_annotate_aliases and self.assembly.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
Run Code Online (Sandbox Code Playgroud)
结果是,新的AssemblyAnnotation(通过内联附加)只能为其type属性提供值的子集,具体取决于父程序集.
这很有效.
现在,是时候将这些注释应用于略有不同的其他对象:
class ObjectAnnotation(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if self.content_type == ContentType.objects.get_for_model(Assembly):
if not self.type.can_annotate_aliases and self.content_object.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to …Run Code Online (Sandbox Code Playgroud) 所以我有一个班轮:
import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))
Run Code Online (Sandbox Code Playgroud)
它所做的就是使一个Decimal对象保持100.0,并以各种方式将它与.01(浮点数)进行比较.
我的结果是:
>>> import decimal; h = decimal.Decimal('100.0'); (h > .01, h < .01, h.__gt__(.01), h.__lt__(.01))
(False, True, NotImplemented, NotImplemented)
Run Code Online (Sandbox Code Playgroud)
从文档:"丰富的比较方法可能会返回单例NotImplemented,如果它没有实现给定的参数对的操作."
所以这里真的有三个问题.
当富比较方法返回NotImplemented时会发生什么?为什么不引发异常?
当它得到NotImplemented时,为什么它在第一种情况下返回False,在第二种情况下返回True?bool(NotImplemented)应该是一个常量.
它是否简单地回到id()检查?似乎没有(或是,但是倒退):
(忽略这一行,格式化搞砸了,这就搞定了)
from decimal import Decimal
h = Decimal('100.0')
f = .01
print h < f, id(h) < id(f)
print h > f, id(h) > id(f)
Run Code Online (Sandbox Code Playgroud)
我的结果测试了:
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Python …Run Code Online (Sandbox Code Playgroud) 这是一个复杂的问题,涉及与 COM、CLR 和无 reg-free COM 相关的一些神秘领域。
首先,我的主要应用程序是用 python 编写的。因此,它的代码库(开发中)位于 C:\mainapp\main.py。
当然,在windows上,执行程序的是C:\Python27\python.exe。
我现在想使用 python 中的 reg-free COM(使用 win32com)与我控制的用 C# 编写的 COM 对象交谈(使用 IDispatch),该对象位于 C:\mainapp\ManagedCOMObject.dll
注意:如果您不会说 python / pythoncom,请注意对 Dispatch() 的调用最终归结为 CoCreateInstance()。
#main.py:
import win32com.client
CLSID_ManagedComObject_MyClass = "{zzzzz....}" #This is correct
myclass = win32com.client.Dispatch(CLSID_ManagedComObject_MyClass)
Run Code Online (Sandbox Code Playgroud)
失败,因为对象不在注册表中(正如预期的那样),而且我没有提到任何关于清单文件的内容,而且 python.exe 的清单显然不知道我的对象。
#main.py:
ac = ActivationContext("C:\mainapp\myapp.manifest", asm_dir="C:\mainapp")
with ac.activate():
#The above two lines fill in a ACTCTX structure, call CreateActCtx, and call ActivateActCtx
import win32com.client
CLSID_ManagedComObject_MyClass = "{zzzzz....}" #This is correct
myclass …Run Code Online (Sandbox Code Playgroud) python ×6
com ×2
django ×2
django-admin ×2
sqlalchemy ×2
assemblies ×1
clone ×1
clr ×1
compare ×1
decimal ×1
duplicates ×1
inheritance ×1
inline ×1
instance ×1
interop ×1
models ×1
orm ×1
performance ×1
polymorphism ×1
pyinstaller ×1
win32com ×1
windows ×1