Python 子对象 inplace 升级成父对象,有啥高性能但更好懂的方法吗? - V2EX
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 2163 次点击
    这是一个创建于 3327 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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     Solana     983 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 20:02 PVG 04:02 LAX 12:02 JFK 15:02
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86