如何使Python类可序列化?
一个简单的课程:
class FileItem:
def __init__(self, fname):
self.fname = fname
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能得到输出:
>>> import json
>>> my_file = FileItem('/foo/bar')
>>> json.dumps(my_file)
TypeError: Object of type 'FileItem' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
没有错误(__CODE__)
输入: "tableapplechairtablecupboard..."很多单词
将这样的文本拆分为单词列表并得到的有效算法是什么?
输出: ["table", "apple", "chair", "table", ["cupboard", ["cup", "board"]], ...]
想到的第一件事就是要经历所有可能的单词(从第一个字母开始)并找到最长的单词,继续 position=word_position+len(word)
PS
我们列出了所有可能的单词.
单词"橱柜"可以是"杯子"和"板子",选择最长.
语言:python,但主要的是算法本身.
我在Django中使用抽象模型,如:
class Tree(models.Model):
parent = models.ForeignKey('self', default=None, null=True, blank=True,
related_name="%(app_label)s_%(class)s_parent")
class Meta:
abstract = True
class Genre(Tree):
title = models.CharField(max_length=150)
Run Code Online (Sandbox Code Playgroud)
抽象模型中的所有字段都首先出现在Django的管理面板中:
parent:
abstract_field2:
title:
model_field2:
...
Run Code Online (Sandbox Code Playgroud)
有没有办法将它们(抽象类中的字段)放在列表的末尾?
或者更一般的方式来定义字段的顺序?
我有两个不同数据库的2个模型:
数据库是手动创建的,但它不会改变任何东西.
class LinkModel(models.Model): # in 'urls' database
id = models.AutoField(primary_key=True)
host_id = models.IntegerField()
path = models.CharField(max_length=255)
class Meta:
db_table = 'links'
app_label = 'testapp'
def __unicode__(self):
return self.path
class NewsModel(models.Model): # in default database
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
link = models.ForeignKey(LinkModel)
class Meta:
db_table = 'news'
app_label = 'test'
def __unicode__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
在以下代码之后出现错误
newsItem, created = NewsModel.objects.get_or_create( title="test" )
link = LinkModel.objects.using('urls').get( id=1 )
newsItem.link = link # error!
Cannot assign "<LinkModel: />": instance is on …Run Code Online (Sandbox Code Playgroud) 有:
f = open(...)
r = re.compile(...)
Run Code Online (Sandbox Code Playgroud)
需要:
找一个大文件中第一个匹配正则表达式的位置(开始和结束)?
(从current_pos=...) 开始
我怎样才能做到这一点?
我想要这个功能:
def find_first_regex_in_file(f, regexp, start_pos=0):
f.seek(start_pos)
.... (searching f for regexp starting from start_pos) HOW?
return [match_start, match_end]
Run Code Online (Sandbox Code Playgroud)
文件'f'预计会很大.
class A():
def __init__(self, data=''):
self.data = data
def __str__(self):
return str(self.data)
d = {}
elem = A()
d[elem] = 'abc'
elem2 = A()
print d[elem2] # KeyError
# actually elem2! was used not elem
Run Code Online (Sandbox Code Playgroud)
如何实现这一点而不会出错?
编辑:
FFFUUU,错误是:
我试图得到d[elem2](而不是elem)另一个A()BUT实例与相同的内容.(对我感到羞耻)
还是......我怎么能这样做?重新定义__hash__?
组织模式.
我有一个很大的任务树,我想在缓冲区中只选择1个(只为它编写注释)并在编辑时隐藏其他任务.
我怎样才能做到这一点?
task-1
subtask-1.1
subtask-1.2
task-2
subtask2.1
...
...
Run Code Online (Sandbox Code Playgroud)
例如,我想只为我显示:
subtask-1.2
(notes for this subtask)
Run Code Online (Sandbox Code Playgroud) 当我gdiplus.h在编译好的程序中包含第一个(有很多)错误时,我会得到:
c:\program files (x86)\microsoft sdks\windows\v7.0a\include\GdiplusImaging.h(77): error C2504: 'IUnknown' : base class undefined
Run Code Online (Sandbox Code Playgroud)
GdiplusImaging.h的一部分:
IImageBytes : public IUnknown <<< error!
{
public:
...
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这个IUnknown课程在哪里?为什么它不在GdiplusImaging.h中?
我的系统是Windows7 x64.VisualStudio 2010.
包括部分:
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")
Run Code Online (Sandbox Code Playgroud) 所以我希望像MathJax一样使用KaTeX内联公式.
但到目前为止,我发现只有render()一个"绘制"字符串到元素的函数.
我需要修改DOM中文本节点的一部分.
我真的找不到如何用KaTeX做到这一点.它有这样的功能吗?
MathJax可以做到这一点.
我什么时候宣布
boost::thread t1, t2;
Run Code Online (Sandbox Code Playgroud)
在我的程序中然后使用UPX压缩.exe文件,压缩成功.但是当我尝试启动压缩的exe时,Windows告诉我它是"无效的win32应用程序".
UPX有一个错误报告(类似的错误),但它有不同的错误消息("应用程序无法正确初始化(0xc0000005)").
在我的情况下,操作系统认为文件已损坏或其他东西,所以它甚至无法启动显示错误!为什么??
Win7x64,C++,VisualStudio,boost 1.47,UPX3.07
奇怪的消息:
解包exe会使损坏的exe抛出与此处完全相同的错误.("应用程序无法正确初始化(0xc0000005)")这是针对解压缩的exe,而不是在bug报告中打包.
extern "C" void tss_cleanup_implemented(void) {}
在包含boost的线程头之前并不重要.结果是一样的.
main.cpp中:
#include <boost/thread.hpp>
int main(int argc, char** argv)
{
boost::thread t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
可能有人会尝试编译和压缩?
python ×4
c++ ×2
django ×2
algorithm ×1
boost ×1
boost-thread ×1
dictionary ×1
emacs ×1
foreign-keys ×1
javascript ×1
json ×1
katex ×1
key ×1
mysql ×1
org-mode ×1
regex ×1
split ×1
text ×1
upx ×1
visual-c++ ×1