我正在使用DataContractSerializer序列化包含Dictionary<string,object>成员的对象,该成员标有[DataMember()].我们的想法是拥有一个灵活的对象属性包,我不知道这些属性是什么.
当我把这个伟大的工程int,double和string对象到字典中,但是当我把List<string>它未能反序列化对象有:
System.InvalidOperationException: Node type Element is not supported in this operation.
Run Code Online (Sandbox Code Playgroud)
整个字典被序列化为XML,看起来很合理:
<Attributes xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>name</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Test object</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>x</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:double">0.5</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>y</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:double">1.25</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>age</d2p1:Key>
<d2p1:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:int">4</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
<d2p1:KeyValueOfstringanyType>
<d2p1:Key>list-of-strings</d2p1:Key>
<d2p1:Value>
<d2p1:string>one string</d2p1:string>
<d2p1:string>two string</d2p1:string>
<d2p1:string>last string</d2p1:string>
</d2p1:Value>
</d2p1:KeyValueOfstringanyType>
</Attributes>
Run Code Online (Sandbox Code Playgroud)
请注意list-of-strings那里的结尾.它有所有的价值,但没有任何迹象表明它是一个List<string>或任何东西.
处理这种情况的正确方法是什么?
我正在开发一个系统的一部分,其中进程限制在大约350MB的RAM; 我们使用cx_Oracle从外部系统下载文件进行处理.
外部系统将文件存储为BLOB,我们可以抓住它们执行以下操作:
# ... set up Oracle connection, then
cursor.execute(u"""SELECT filename, data, filesize
FROM FILEDATA
WHERE ID = :id""", id=the_one_you_wanted)
filename, lob, filesize = cursor.fetchone()
with open(filename, "w") as the_file:
the_file.write(lob.read())
Run Code Online (Sandbox Code Playgroud)
lob.read()MemoryError当我们点击一个大于300-350MB的文件时,显然会失败,所以我们尝试过这样的东西,而不是一次性读取所有内容:
read_size = 0
chunk_size = lob.getchunksize() * 100
while read_size < filesize:
data = lob.read(chunk_size, read_size + 1)
read_size += len(data)
the_file.write(data)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我们仍然MemoryError经历了几次迭代.从时间开始lob.read(),以及我们最终获得的内存不足情况,看起来好像每次都lob.read()从数据库中提取(chunk_size + read_size)字节.也就是说,即使缓冲区相当小,读取也需要O(n)时间和O(n)存储器.
为了解决这个问题,我们尝试过类似的方法:
read_size = 0
while read_size < filesize:
q = u'''SELECT dbms_lob.substr(data, 2000, …Run Code Online (Sandbox Code Playgroud) 我在Unity3D项目中使用了一些旧的C#代码(特别是这个Fortune的Voronoi图算法),我想更新它以使用适当的泛型,重构和一般清理.
理想情况下,我这样做不会破坏任何东西; 代码工作,它的算法实现是合理的.单元测试显然可以帮助我重构它而不会搞砸它.
不幸的是,我真的不理解数学或算法,而且代码密集且无评论.
如何为这样的代码编写单元测试?