From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id QPhUJwbQS192PwAA0tVLHw (envelope-from ) for ; Sun, 30 Aug 2020 16:12:54 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id CD9AIwbQS1+xGQAAB5/wlQ (envelope-from ) for ; Sun, 30 Aug 2020 16:12:54 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9EFFD94021E for ; Sun, 30 Aug 2020 16:12:52 +0000 (UTC) Received: from localhost ([::1]:55262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kCPwn-0002Om-Um for larch@yhetil.org; Sun, 30 Aug 2020 12:12:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kCPwJ-0002OQ-30 for emacs-orgmode@gnu.org; Sun, 30 Aug 2020 12:12:19 -0400 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]:39025) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kCPwH-0002Yf-9B for emacs-orgmode@gnu.org; Sun, 30 Aug 2020 12:12:18 -0400 Received: by mail-pf1-x444.google.com with SMTP id u128so3208256pfb.6 for ; Sun, 30 Aug 2020 09:12:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=LPXuqrQyXUuFfl7aFHxWsbPB25qkodD8Zg9N4Jlcylc=; b=b0eRcySUvf3mrQHV0FtsciawlmtHNzQD8fCC3aQh+n9h3OfEX1FOwoXaNFNDU1QZOK /m4AS32sEcyinfRiaJeMzj3EWsOosIEh3gAIykUZ++7/kcjEKEu+O4nrDTrJzMpnM1+h ZHr93yj5cgvreIirSLjP+ri1KQOubxT+wmVVSBlc/DWcj1atE5VnkgE2Fkt8njJKIcVh Es6ReJrXANwFydWe+0PvClp57XrE59O05yA7Gud3cUvUZKJDMyYOnwmHl8jfW2BLoaYT Dam1UTuzVb8YfB1xyFWKuoZQML8n3AsBSeT3Ag+JZfXdALoeOc3C5MDYXXyGTB+JUe0S BkjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=LPXuqrQyXUuFfl7aFHxWsbPB25qkodD8Zg9N4Jlcylc=; b=jME3M9w57hpscuVBGnjd4zObvpNL66wScpvH8nMg9SKqiegov4ef1XX2J3PUws2rnd M50fT2PwBZy7LiR1TKhOwvOmRzstU4UKv1nOfAgcxNbTc5cxtrc1cZoE9OrQTnL21i3n ZtwmM8hY2RirrlrKcbyBPuXXMObLb8rgXErXwqKqqNfHOcICLHQB/+ThL1QhTxykQ3SM i9fSBL1DcqntUmpqhZr0ap1Q+r6NkSfEEiOGerW1v8AZHu207E1bflL5eRmc8dxrhw5I GbwUYA36oeUF1y5vQRqM8mCbIZ1iUKd4RJiEcSyWck1wGHF3SWITqkNL0YrKwR5kNrl7 bKWA== X-Gm-Message-State: AOAM5323bBMCDeAEmJAlpxx3fC1Qn3hi07WbT4wKwNooPobtgVFMNjC9 RiZcGetWwioFn3B1YjsEMag= X-Google-Smtp-Source: ABdhPJxg44/aKXEZiRkQ4Pb3P8tzmhW5wOIIMbbztmSfqpjidb27Ek9Bl3VPs1930XLszBC/he6PRQ== X-Received: by 2002:a62:1d44:: with SMTP id d65mr6319008pfd.150.1598803935701; Sun, 30 Aug 2020 09:12:15 -0700 (PDT) Received: from localhost (199-83-220-90.PUBLIC.monkeybrains.net. [199.83.220.90]) by smtp.gmail.com with ESMTPSA id d4sm4541789pju.56.2020.08.30.09.12.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Aug 2020 09:12:14 -0700 (PDT) From: Jack Kamm To: Kyle Meyer Subject: Re: [PATCH] Expanded ob-python results handling and plotting In-Reply-To: <87blisfacz.fsf@gmail.com> References: <87eenpfe77.fsf@gmail.com> <871rjptdje.fsf@kyleam.com> <87blisfacz.fsf@gmail.com> Date: Sun, 30 Aug 2020 09:12:14 -0700 Message-ID: <874kokf6zl.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::444; envelope-from=jackkamm@gmail.com; helo=mail-pf1-x444.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: emacs-orgmode@gnu.org Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=b0eRcySU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -0.21 X-TUID: 7ENozcKBbzo4 --=-=-= Content-Type: text/plain After taking another look at my patch, I realized that I was not quite converting dictionaries to proper alists. Attached is a tweak to do this properly. The printing of dictionaries is not quite as pretty, in particular it's not a table anymore: #+begin_src python return {"a": 1, "b": 2} #+end_src #+RESULTS: : ((a . 1) (b . 2)) But, it feels like the right thing to do, since the result handling code works by converting the result to an elisp value, before passing it to org-mode to decide how to render it. And the proper elisp conversion of a dict should be an alist or a plist. Ideally I wouldn't have to do this from the Python code, and could let org-babel-script-escape convert the dict objects. It would also be useful for other languages with similar dictionaries, like javascript. But it seems fairly complex to implement this from the elisp side, and I'm not sure I'm up for it. I also noticed that I had left a couple docstrings as TODOs -- I'll fix those before finalizing the patch over the next couple weeks. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-Convert-dictionary-output-to-a-proper-alist.patch >From 76a1ad4d50e6638244d9aa17e45895b8b38b3cd0 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sun, 30 Aug 2020 08:51:04 -0700 Subject: [PATCH 2/2] Convert dictionary output to a proper alist Note: to be squashed with the previous patch before merging --- lisp/ob-python.el | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index fb8fe380e..08c1c48e9 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -239,9 +239,14 @@ (defconst org-babel-python--def-format-value "\ else: if not set(result_params).intersection(\ ['scalar', 'verbatim', 'raw']): + class alist(dict): + def __str__(self): + return '({})'.format(' '.join(['({} . {})'.format(repr(k), repr(v)) for k, v in self.items()])) + def __repr__(self): + return self.__str__() def dict2alist(res): if isinstance(res, dict): - return [(k, dict2alist(v)) for k, v in res.items()] + return alist({k: dict2alist(v) for k, v in res.items()}) elif isinstance(res, list) or isinstance(res, tuple): return [dict2alist(x) for x in res] else: -- 2.28.0 --=-=-=--