ste*_*tej 5 .net ironpython db4o object-oriented-database
我想使用对象数据库来保存在IronPython中创建的一些类.该数据库是db4o for .NET 2.0(今天下载).代码如下所示:
import clr
clr.AddReferenceToFileAndPath(r"C:\dev\Db4objects\db4o-7.12-net20\bin\net-2.0\Db4objects.Db4o.dll")
from Db4objects.Db4o import *
db = Db4oFactory.OpenFile(r'G:\IronPython\test\db4o\database.db')
class Person(object):
def __init__(self, name, age):
self.Name = name
self.Age = age
def __str__(self):
return 'Person: ' + self.Name + ' ' + str(self.Age)
p1 = Person('testp', 34)
db.Store(p1)
Run Code Online (Sandbox Code Playgroud)
我得到例外 db.Store(p1)
Unexpected char '$'
ThrowUncheckedException at offset 4 in file:line:column <filename unknown>:0:0
FatalShutdown at offset 136 in file:line:column <filename unknown>:0:0
AsTopLevelCall at offset 112 in file:line:column <filename unknown>:0:0
AsTopLevelStore at offset 34 in file:line:column <filename unknown>:0:0
StoreInternal at offset 69 in file:line:column <filename unknown>:0:0
Store at offset 66 in file:line:column <filename unknown>:0:0
Store at offset 12 in file:line:column <filename unknown>:0:0
Store at offset 15 in file:line:column <filename unknown>:0:0
v Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.CallWithInstance(Object[] args, Boolean& shouldOptimize)
v IronPython.Runtime.Types.BuiltinFunction.BuiltinMethodCaller`2.Call1(CallSite site, CodeContext context, TFuncType
func, T0 arg0)
v System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
v Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
v Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
v Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
v IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
v IronPython.Compiler.PythonScriptCode.Run(Scope scope)
v IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction>b__0()
Exception: Unexpected char '$'
CLR Exception:
Exception
:
Unexpected char '$'
Run Code Online (Sandbox Code Playgroud)
我怀疑问题出在IronPython及其类型系统上,因为该类Person不是标准的.NET类.我试着坚持下去System.IO.FileInfo,效果很好.如何在IronPython中存储作为类实例的对象?
Db4o 使用 CLR 反射机制来检索对象的元信息,以便存储它。这对于 C#、VB.NET 等来说效果很好。IronPython 和 IronRuby 等语言在 CLR 之上使用了一个附加层来允许所有精彩的动态功能。
现在,当 db4o 使用 CLR 反射时,会添加用于实现动态功能的附加内容。尝试存储此信息失败。目前还没有一个简单优雅的解决方案 =(
另一种可能性是使用专门为 Python 创建的 ODBMS,例如ZODB而不是 db4o。但我不确定 ZOBR 是否在 IronPython 上运行。
| 归档时间: |
|
| 查看次数: |
621 次 |
| 最近记录: |