- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在查看维护大量字符串的 python 脚本。这些字符串实际上是来自回归测试运行的日志文件名的完整路径。文件数量太大,脚本开始达到虚拟地址空间限制。
像这样的字符串几乎没有熵,所以我认为压缩它们会很好。问题是我考虑的压缩库(如在 Python - Compress Ascii String 中讨论的那些)似乎单独压缩每个字符串(存储解压缩所需的所有信息)。常识表明,将整个字符串集压缩到单个全局 blob/字典中并使用某种短引用来引用单个字符串在空间方面效率更高。
这里有一些(伪)代码来阐明这个想法:
class TestResult:
def __init__(self):
self._Id = None
....
self.__appLogList = []
return
....
def setAppLogList(self, currentAppLogList):
self.__appLogList = currentAppLogList
# what I want to do here instead is
# for each s in currentAppLogList
# add s to global compressed blob and
# store reference in __appLogList
def getAppLogList(self):
return self.__appLogList
self.__appLogList = currentAppLogList
# what I want to do here instead is
# currentAppLogList = []
# for each ref in __appLogList
# decompress ref into string s and add s to currentAppLogList
# return currentAppLogList
# for each test
# add new TestResult to result map
# setAppLogList
# for selected tests
# getAppLogList and access logs
这是否可以使用现有的公开可用的 Python 库来实现?
最佳答案
这是我之前回答的变体或扩展。主要区别在于它能够通过利用字符串实际上是可能包含一个或多个公共(public)子组件的文件路径这一事实来“压缩”字符串。通过从该信息构建树数据结构来消除这种冗余,这意味着对于任何给定级别,每个唯一的组件值仅存储一次。树数据结构本身是使用本质上是字典的字典来实现的。由于使用了内置的 reduce()
函数,它的创建应该相对较快。
下面的更新版本现在有一个属性——与以前的版本不同——创建的内容可以被腌制,这意味着它可以保存并在以后从文件中原封不动地读回。
和我一样answer ,这不是你所谓的“真正的”压缩,但是我认为做类似的事情对于你想要完成的事情来说可能有点矫枉过正,因为这会解决你的问题,相对较快,而且维护起来会更简单,增强,并在以后扩展。
import collections
import cPickle as pickle # use C version for performance
import operator
import os
class DefaultDict(collections.defaultdict):
""" pickle-able defaultdict """
def __reduce__(self):
args = (self.default_factory,) if self.default_factory else tuple()
return type(self), args, None, None, self.iteritems()
def Tree(): return DefaultDict(Tree)
class Leaf(object): pass
def print_tree(d, level=0, indent=' '*4):
""" Tree structure pretty printer """
if not d:
print indent * level + '<empty>'
else:
for key, value in sorted(d.iteritems()):
print indent * level + str(key)
if isinstance(value, dict):
print_tree(value, level+1, indent)
elif not isinstance(value, Leaf):
print indent * (level+1) + str(value)
# create Tree structure from paths
tree = Tree()
LEAF = Leaf()
with open('log_file_paths.txt') as inf:
for line in inf:
path = line.strip().split(os.sep)
reduce(operator.getitem, path[:-1], tree)[path[-1]] = LEAF
print_tree(tree)
# save tree to a file
with open('file_tree.pk', 'wb') as outf:
pickle.dump(tree, outf, pickle.HIGHEST_PROTOCOL)
# try reading it back in
del tree
with open('file_tree.pk', 'rb') as inf:
tree = pickle.load(inf)
print_tree(tree) # display reconstituted tree
所以如果输入文件由这些文件路径组成:
tests/first/set1.log
tests/first/set2.log
tests/first/subfirst/set3.log
tests/first/subfirst/set4.log
tests/second/set5.log
tests/second/subsecond/set6.log
tests/second/subsecond/subsubsecond/set7.log
tests/third/set8.log
以下树结构是在内存中创建并显示(然后写入、回读和重新显示)以保存它们的内容:
tests
first
set1.log
set2.log
subfirst
set3.log
set4.log
second
set5.log
subsecond
set6.log
subsubsecond
set7.log
third
set8.log
关于python - 压缩大量相似的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437009/
我遇到了一个让我非常难过的问题。但现在看来这一定是一个普遍的问题,我很惊讶我找不到关于它的另一个问题。 基本上,我的网站上会出现一些图标。我希望它们在每台设备上都显示出大致相同的大小——大致是可触摸的
我是一名 Java 专业人士。现在我喜欢转向其他编程语言。谁能建议我哪种编程语言更容易消化,并且该语言具有 Web 开发功能。我对 Web 开发感兴趣。 最佳答案 C# - 网络开发非常好(只需看看这
我需要为不同的 url 发送不同的响应。但是我正在使用的正则表达式不起作用。 有问题的两个正则表达式是 "/v1/users/[^/]+/permissions/domain/HTTP/" (例
让我描述一下我的问题。有一个输入字符串和一个包含数千个字符串的表。我正在寻找搜索与输入字符串最相似* 字符串的最佳方法。搜索应该返回一个包含 ~10 个建议字符串的列表,按相似度排序。字符串在数据库中
我在使用 crypt() 时遇到问题,如果用户有密码(本例中为 password1),并且他们将其更改为 password2,则散列会返回相同的结果。您可以在此处进行测试:旧链接输入 password
我问这个是因为我发现 XML 在视觉上没有吸引力。我知道 XML 非常有名,很有用,而且它被大多数(如果不是全部)程序员接受为一种非常有用的定义和结构化数据的语言;然而,我真的不觉得盯着它看、看着它或
我有工作需要在 14 天内完成。我有5名 worker 。一天正好需要3个 worker 。每个 worker 最多只能工作 9 天。每个 worker 都有自己的日期偏好,每个 worker 每天都
FileStream 或 DB 句柄类型的对象必须正确关闭,因为它在内部使用 Windows 句柄。同样,Threads 也使用 Windows 句柄吧?线程是否有一些内部机制来释放那些 Window
如果这是非常基本的,请原谅。我一直在尝试遍历行数组中的每个单元格包括标题单元格。我可以在 Nokogiri CSS 选择器中使用 OR 运算符吗? thang= Nokogiri::HTML(IO.r
默认情况下,RichTextFx(顶部控件)看起来不像 TextArea(底部控件): 我找到了一种添加圆角的方法(只需从 modena.css 复制并配置 some styles): .virtua
在 64 位 Java 中,每个对象实例往往包含一个 192-bit header , 包含 类指针, 标志和 锁(每个 64 位)。 这可能会导致小对象的大量内存开销。 尼姆的情况是否类似?用两种语
我正在开发一个具有多个功能(通过 tableview 选择)的应用程序,这些功能几乎都做同样的事情,相似的布局、相似的按钮、相似的输入字段等等。但在我相信它们自己的地方有足够的不同类(class)。每
题目地址:https://leetcode-cn.com/problems/similar-rgb-color/ 题目描述 Inthe following, every capital lette
我正在编写 R 代码,我希望它在“非调试”或“调试”模式下运行。在 Debug模式下,我希望代码打印出运行时信息。 在其他语言中,我通常会有某种打印功能,除非打开标志(用于编译或运行时),否则什么都不
我不明白这个OP接受的答案的一部分: Javascript function scoping and hoisting 作者说: " 此外,在这种情况下, function a() {} 行为与相同
我正在做一个bcmp(&v6_addr1,&v6_addr2,sizeof(v6_addr1)) (gdb) p v6_addr2 $15 = {u = {b = "0\001", '\000' ,
我有一个充满按钮的 Canvas,我将其 ID 存储在字典中。 Canvas 很长,带有垂直滚动条。有一种方法可以自动将 View 定位在给定的按钮上吗? 当使用 Text 时,txt.see(pos
我正在尝试使用 Python 2.6 和 PIL 计算两个图像的相似度(读作:Levenshtein 距离)。 我打算给我们e python-levenshtein用于快速比较的库。 主要问题: 什么
换句话说,php $object->method(); 和 $object->property = 'someValue'; 相当于,js: $object.method(); 和 $object.p
我写了一个简单的函数,它接收实现 .index() 的东西和要检查的字符列表。 我的假设是,由于字符串和元组都是不可变的,因此它们具有相似的性能(或者至少,元组的性能优于列表)。相反,元组似乎等同于列
我是一名优秀的程序员,十分优秀!