lanceadd's recent timeline updates
lanceadd

lanceadd

V2EX member #366907, joined on 2018-11-30 15:45:34 +08:00
python3 多个多层 dict 合并
问与答  •  lanceadd  •  Dec 8, 2020  •  Lastly replied by lanceadd
5
Reportlab 如何向已经存在 pdf 中的指定位置插入一个饼状图
  •  1   
    Python  •  lanceadd  •  Nov 13, 2020  •  Lastly replied by renmu123
    2
    Nest.js 和 Egg.js 选哪个
    JavaScript  •  lanceadd  •  Jan 19, 2021  •  Lastly replied by fengyunselang
    7
    如何解决 Mac 下 vim 中无法使用 commad+c/v 来复制粘贴
    Vim  •  lanceadd  •  Mar 2, 2020  •  Lastly replied by hfpp2012
    17
    如何让 Macvim 和 vim 分别使用不同的配置文件
    Vim  •  lanceadd  •  Mar 2, 2020  •  Lastly replied by lanceadd
    6
    优化家庭 wifi 信号覆盖
    问与答  •  lanceadd  •  Dec 18, 2019  •  Lastly replied by lanceadd
    5
    Mac os 的证书本地劫持如何解决
  •  1   
    问与答  •  lanceadd  •  Aug 10, 2019  •  Lastly replied by wonderingray
    3
    lanceadd's recent replies
    emmm 你们还是手搓的 controller 吗,直接写个代码生成器,设计完接口生成一下,controller ,service ,serviceMockImpl 啥的都生成好,然后同事只需要实现那个 service ,xxxServiceImpl 里他想怎么弄都行,最后加个注解 @Primary 把原来 MockImpl 挤下去完事了,避免没必要的手搓就没这些问题
    Apr 28, 2025
    Replied to a topic by loveyou1 旅行 大家五一都准备去哪里
    @LetsGiao 怀旧服吗
    @highkay 刚好最近想做这个,star+1 参考下老哥的项目
    Dec 8, 2020
    Replied to a topic by lanceadd 问与答 python3 多个多层 dict 合并
    解决了
    ```
    def recursive_update(
    default: dict,
    custom: dict
    ):
    """
    递归更新 dict
    :param default:
    :param custom:
    :return:
    """
    if not isinstance(default, dict) or not isinstance(custom, dict):
    raise TypeError('Params of recursive_update should be dicts')

    for key in custom:
    if isinstance(custom[key], dict) and isinstance(default.get(key), dict):
    default[key] = recursive_update(default[key], custom[key])
    else:
    default[key] = custom[key]

    return default
    ```
    Dec 7, 2020
    Replied to a topic by lanceadd 问与答 python3 多个多层 dict 合并
    多个嵌套字典合并为一个
    Dec 7, 2020
    Replied to a topic by lanceadd 问与答 python3 多个多层 dict 合并
    @renmu123 @westoy 抱歉抱歉我也不知道为啥会缺了一块, 发布前预览是没问题的, 另外一个 dict 也是这个,但是 user_id,title_id 这些 key 可能不相同,然后可能层级也不一样,可能会只有两层那种,然后想把一个 dict,update 到另外一个 dict 上,如果那一层的 key 相同 value 不同就合并上去,举个例子按照我上面那个结构,user_id 、title_id 相同但是 relation_id 不同,怎么把它俩合并起来,我原本以为 dict.update()就好了,但是发现对多层的 dict 直接 update 会直接覆盖,然后发现只能一层一层的 update,就很尴尬,除了递归还有啥办法可以更新不同深度的嵌套字典的值吗
    我会了
    ```
    import io

    from PyPDF2 import PdfFileReader, PdfFileWriter
    from reportlab.graphics import renderPDF
    from reportlab.graphics.charts.piecharts import Pie
    from reportlab.graphics.shapes import Drawing
    from reportlab.lib.pagesizes import A4
    from reportlab.pdfgen import canvas

    packet = io.BytesIO()

    can = canvas.Canvas(packet, pagesize=A4)

    pie = Pie()
    pie.data = [20, 10, 5, 5, 5]
    pie.labels = ['a', 'b', 'c', 'd', 'e']
    pie.sideLabels = True

    d = Drawing(100, 100)
    d.add(pie)
    renderPDF.draw(d, can, 20, 20)
    can.save()
    packet.seek(0)
    new_pdf = PdfFileReader(packet)

    existing_pdf = PdfFileReader(open('example_06.pdf', 'rb'))
    output = PdfFileWriter()
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    outputStream = open('result.pdf', 'wb')
    output.write(outputStream)
    outputStream.close()

    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   914 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 20:24 · PVG 04:24 · LAX 13:24 · JFK 16:24
    ♥ Do have faith in what you're doing.