V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yupbank
V2EX  ›  Python

Python 子对象 inplace 升级成父对象,有啥高性能但更好懂的方法吗?

  •  
  •   yupbank · 2016-10-25 21:57:14 +08:00 · 1627 次点击
    这是一个创建于 2742 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Hi ,我来请教一个问题呗。

    class Old(object):
        def  __init__(self):
            self.current = {}
            self.new_added = []
    
        def keep_history(self, key):
            if key in self.filed:
                self.current[key] = self.field[key]
            else:
                self.new_added.append(key)
        def born(self):
            self.son = Normal(self._fields)
            self._fields = None
            return self.son
    
    
    class Normal(object):
        def __init__(self, field=None):
            self.filed = filed or {}
    
        def mutate(self, key, value):
            self.aging()
            self.keep_history(key)
            self._filed[key] = value
            
            return self.born()
        
        def aging(self):
             self.__class__ = Old
             self.__init__()
    

    现在是这种设计,想 Normal 对象在 mutate 的时候,保留一份 最老-》比较老-》年轻 的记录

    当前据说考虑 GC 的顺序,是老对象指向新对象,不因为最新对象而阻碍了老对象回收。

    没用 weak ref 是因为 weak ref 开销大。。。

    但这样的代码确实不美观。。 我在想如何写的性能又高,代码又好读。。

    请各位大神指点。。。

    1 条回复    2016-10-25 22:42:28 +08:00
    yupbank
        1
    yupbank  
    OP
       2016-10-25 22:42:28 +08:00
    use case of Normal and Old.
    to retain change history and able to mutate a dictionary base object in rdd

    ```python
    data = [dict(sound=1, counting=2, c=4) for _ in xrange(1000000)]

    data_rdd = sc.parallelize(map(Normal, data))
    #sc is pyspark context

    def function(normal):
    new_normal = normal.mutate('counting', 3).mutate('sound', 2)
    #still do some calculation with normal [ 1 ]
    #even do something with the change history through normal.son.current. normal.son.son.current [ 2 ]
    return new_normal

    data_rdd.map(function).collect()
    ```
    ----
    但有些 use case 不会发生[ 1 ],[ 2 ]两种情况。 那样 gc 直接回收 old 。 因为在 spark 集群跑,所以一点点的性能优势可以放大好多。。

    我想改进代码可读性。。毕竟直接 inplace 的改变对象的__class__ 好粗暴

    (之前的设计不是我搞得。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1094 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:41 · PVG 02:41 · LAX 11:41 · JFK 14:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.