C酸洗装载时包装

2024-10-03 09:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我很蠢

对不起大家。我的循环不起作用的原因是一个极其愚蠢的原因;我的论证顺序不对泡菜。倾倒(). 在

行动

第一次酸洗对象,抱歉,如果这是一个关键的问题;已经看过其他各种eoferor帖子,但没有一个工作或似乎适用。在

另外,虽然我关闭了.pkl文件,但当脚本运行时,它的文件大小始终为零字节。我不明白为什么。在

我在OSX(Mavericks)上;下面是我的代码(为了可读性的目的稍微减少了一些;to_file_inspections()def是一个类方法,一些var名称对于普通读者来说更加有用,否则就是逐字逐句的。而且FileInspection是逐字的!在

def to_file_inspections(data_store)
    jar = open("jar.pkl","wb")
    for f in data_store:
        f_ob = FileInspection(f) #custom class, not very elaborate
        cPickle.dump(jar,f_ob, -1)
    jar.close()
    #now just to test it...
    data = cPickle.load(open("jar.pkl", "rb"))
    print repr(data)

结果:

^{pr2}$

如果有关系,这是我的FileInspection类:

class FileInspection:
    def __init__(self, path):
        self._path = path.rstrip('\n')
        self._hash = self.hash_file()
        self._inspectable = True
        stats = os.stat(self._path)
        self._size = stats.st_size
        self._last_mod = stats.st_mtime

    def hash_file(self):
        read_size = 1024 # You can make this bigger
        checksum = hashlib.md5()
        with open(self._path) as f:
            data = f.read(read_size)
            while data:
                checksum.update(data)
                data = f.read(read_size)
        checksum = checksum.hexdigest()

        return checksum

    def toString(self):
        return '{0} = {1}"checked":false, "path":"{2}", "hash":{3} "inspectable":{4}, "filesize":{5}, "lastmod":"{6}"{7}'.format(
            self._hash, "{", self._path, self._inspectable, self._hash, self._last_mod, "}\n")

    def toJSON(self):
        return '\t"{0}":{1}\n\t\t"checked":false,\n\t\t"path":"{2}",\n\t\t"hash":"{3}",\n\t\t"inspectable":"{4}",\n\t\t"filesize":"{5}",\n\t\t"last_mod":"{6}"\n\t{7}'.format(
            self._hash, "{", self._path, self._hash, self._inspectable, self._size, self._last_mod, "}")

    @property
    def path(self):
        return self._path

    @property
    def hash(self):
        return self._hash

    @property
    def inspectable(self):
        return self._inspectable

    @property
    def size(self):
        return self._size

    @property
    def last_mod(self):
        return self._last_mod

Tags: pathselfmodreaddatasizereturndef