移植 Python 项目到 typescript,如何处理 Python attrs 库呢 - 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
AkideLiu

移植 Python 项目到 typescript,如何处理 Python attrs 库呢

  •  
  •   AkideLiu 2021 年 12 月 5 日 2773 次点击
    这是一个创建于 1602 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位彦祖好,最近在把一个 python 写的项目改写成 typescript

    发现原始 python 代码里面使用了大量的 attr.ib(),在 attr.ib()里面有很多的参数比如 cmp ,factory ,converter ,validator 等等。

    初步想法是用 ts class 的 constructor 来实现类似的功能,但是这样的话上面提到的细节参数都需要有单独的验证和处理,请问有什么适用于 typescript 类似于 attr 的库方便实现这样的功能吗?

    attrs 的官方文档 : https://www.attrs.org/en/stable/

    看完官方文档和例子感觉有点像 java 的 lombok

    一段例子如下:

    https://github.com/cs50/compare50/blob/main/compare5/_data.py#L114-L149

    @attr.s(slots=True, frozen=True) class Submission: """ :ivar path: the file path of the submission :ivar files: list of :class:`compare50.File` objects contained in the submission :ivar preprocessor: A function from tokens to tokens that will be run on \ each file in the submission :ivar id: integer that uniquely identifies this submission \ (submissions with the same path will always have the same id). Represents a single submission. Submissions may either be single files or directories containing many files. """ _store = IdStore(key=lambda sub: (sub.path, sub.files, sub.large_files, sub.undecodable_files)) path = attr.ib(cOnverter=pathlib.Path, cmp=False) files = attr.ib(cmp=False) large_files = attr.ib(factory=tuple, cOnverter=_to_path_tuple, cmp=False, repr=False) undecodable_files = attr.ib(factory=tuple, cOnverter=_to_path_tuple, cmp=False, repr=False) preprocessor = attr.ib(default=lambda tokens: tokens, cmp=False, repr=False) is_archive = attr.ib(default=False, cmp=False) id = attr.ib(init=False) def __attrs_post_init__(self): object.__setattr__(self, "files", tuple( [File(pathlib.Path(path), self) for path in self.files])) object.__setattr__(self, "id", Submission._store[self]) def __iter__(self): return iter(self.files) @classmethod def get(cls, id): """Retrieve submission corresponding to specified id""" return cls._store.objects[id] 
    3 条回复    2021-12-05 17:09:07 +08:00
    Opportunity
        1
    Opportunity  
       2021 年 12 月 5 日
    gouflv
        2
    gouflv  
       2021 年 12 月 5 日 via iPhone
    class-transformer + class-validator
    pengtdyd
        3
    pengtdyd  
       2021 年 12 月 5 日
    改啥改,又不是不能用
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2720 人线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 41ms UTC 15:24 PVG 23:24 LAX 08:24 JFK 11:24
    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