From patchwork Sat Feb 19 20:19:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygmunt Krynicki X-Patchwork-Id: 3849 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5013CC433F5 for ; Sat, 19 Feb 2022 20:20:24 +0000 (UTC) Received: from new2-smtp.messagingengine.com (new2-smtp.messagingengine.com [66.111.4.224]) by mx.groups.io with SMTP id smtpd.web10.14306.1645302022282141067 for ; Sat, 19 Feb 2022 12:20:23 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=BuwhIU83; spf=fail (domain: huawei.com, ip: 66.111.4.224, mailfrom: zygmunt.krynicki@huawei.com) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 494AB58055F; Sat, 19 Feb 2022 15:20:21 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sat, 19 Feb 2022 15:20:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:message-id:mime-version:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=P8GzF0w6o87FX4YM08H6N4bwpBjtg fT1TmsQ/1Xt5oM=; b=BuwhIU838dCs+AADoCobkzGW3m33xAxSRD9fbAN86fJ3X 3xw7cZmfpznWlPbZncz47/Yht9IhCt7LHwyeUKf0K9nfnR9AxDGuJGs6Isr+HQFj /aPpZ1yz2FY0I9IDEn0sevpn3rGiVRobFl6iP2jK5wx6ZvIFHzpdhQ7yeQ+YWmZd +o+A5uRH2JK60k1Xl+FrfmseNZZHa0WZkEvQtHGlKzGW0yV3GydjEY6QklGB1j9a 43QCNG5A7myrALnLTMfxhtzpECt6HhRN3e6ag46kvJgvhdDD8+7gV5FZWnxXOAQ7 rBQ08cbXZidGRfPOkyX5rPcfGvW/UmB9ONtXAdetA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrkedvgddufeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgggfestdekredtre dttdenucfhrhhomhepkgihghhmuhhnthcumfhrhihnihgtkhhiuceoiiihghhmuhhnthdr khhrhihnihgtkhhisehhuhgrfigvihdrtghomheqnecuggftrfgrthhtvghrnhepiedvhf eltdeileeiffeugfehkedvfeffieehvdejtefhlefhffdtheevkeehveehnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepiiihghhmuhhnthdrkh hrhihnihgtkhhisehhuhgrfigvihdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 19 Feb 2022 15:20:19 -0500 (EST) From: Zygmunt Krynicki To: bitbake-devel@lists.openembedded.org Cc: Zygmunt Krynicki Subject: [PATCH 1/3] simplediff: Remove redundant parentheses Date: Sat, 19 Feb 2022 21:19:55 +0100 Message-Id: <20220219201957.8329-1-zygmunt.krynicki@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Sat, 19 Feb 2022 20:20:24 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/bitbake-devel/message/13389 Signed-off-by: Zygmunt Krynicki --- lib/simplediff/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py index 57ee3c5c..11eb7e70 100644 --- a/lib/simplediff/__init__.py +++ b/lib/simplediff/__init__.py @@ -90,7 +90,7 @@ def diff(old, new): # now we are considering all values of iold such that # `old[iold] == new[inew]`. _overlap[iold] = (iold and overlap.get(iold - 1, 0)) + 1 - if(_overlap[iold] > sub_length): + if _overlap[iold] > sub_length: # this is the largest substring seen so far, so store its # indices sub_length = _overlap[iold] From patchwork Sat Feb 19 20:19:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygmunt Krynicki X-Patchwork-Id: 3851 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33F43C433F5 for ; Sat, 19 Feb 2022 20:20:33 +0000 (UTC) Received: from new2-smtp.messagingengine.com (new2-smtp.messagingengine.com [66.111.4.224]) by mx.groups.io with SMTP id smtpd.web09.14199.1645302029333276833 for ; Sat, 19 Feb 2022 12:20:29 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=YpngSSqZ; spf=fail (domain: huawei.com, ip: 66.111.4.224, mailfrom: zygmunt.krynicki@huawei.com) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id AFC2B580133; Sat, 19 Feb 2022 15:20:28 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 19 Feb 2022 15:20:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=FiAoik hUTDL6O7M7m8IIfDkgcKOgRC3GTUca2xPOuZw=; b=YpngSSqZzS3jgAoX/cRHu4 fHPx/0E5KrSI8kB1fx57xLfYdffV9rVgZ4gMFEIkqj9HfoziPIXNhyuXzf1JO9ki qx2RPMZfFAJF4AYlJZDFyc+DWH5K2DKP9HUex4LLDt4DEax+wImIbJUjQtX9UDMT JZ7owndhNvlN6LX5+En8LhUP80w6Bxl9rgIW6kNzxI7FJIlkp4pjA9QtFjBrVj+S NAoZfQijMirZyzac6ORMjnVzfq7BEjELV5uCEcwtV2tUCy1ogNDspofBtPr+r1Mx z3GDo620enzcxBIXrph2fRtQDsac0jniUV4zwizDxS8bLVj7/zIsT63/+liGEcng == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrkedvgddufeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepkgihghhmuhhnthcumfhrhihnihgtkhhiuceoiiihghhmuhhn thdrkhhrhihnihgtkhhisehhuhgrfigvihdrtghomheqnecuggftrfgrthhtvghrnhepgf ffgfejffefieeffeehtedvudfhkeeuteegieejvdehlefhhffhgfelheefueehnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepiiihghhmuhhnth drkhhrhihnihgtkhhisehhuhgrfigvihdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 19 Feb 2022 15:20:27 -0500 (EST) From: Zygmunt Krynicki To: bitbake-devel@lists.openembedded.org Cc: Zygmunt Krynicki Subject: [PATCH 2/3] simplediff: Use "a if b else c" expression Date: Sat, 19 Feb 2022 21:19:56 +0100 Message-Id: <20220219201957.8329-2-zygmunt.krynicki@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220219201957.8329-1-zygmunt.krynicki@huawei.com> References: <20220219201957.8329-1-zygmunt.krynicki@huawei.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Sat, 19 Feb 2022 20:20:33 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/bitbake-devel/message/13390 The diff code used "a and b or c" as an old-style Python implementation of the ternary operator. The more correct way to do this is to use "b if a else c", as it has cleaner type signature. Signed-off-by: Zygmunt Krynicki --- lib/simplediff/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py index 11eb7e70..64ec64b4 100644 --- a/lib/simplediff/__init__.py +++ b/lib/simplediff/__init__.py @@ -100,7 +100,7 @@ def diff(old, new): if sub_length == 0: # If no common substring is found, we return an insert and delete... - return (old and [('-', old)] or []) + (new and [('+', new)] or []) + return ([('-', old)] if old else []) + ([('+', new)] if new else []) else: # ...otherwise, the common substring is unchanged and we recursively # diff the text before and after that substring From patchwork Sat Feb 19 20:19:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygmunt Krynicki X-Patchwork-Id: 3850 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A4B3C433EF for ; Sat, 19 Feb 2022 20:20:32 +0000 (UTC) Received: from new2-smtp.messagingengine.com (new2-smtp.messagingengine.com [66.111.4.224]) by mx.groups.io with SMTP id smtpd.web12.14214.1645302031091928703 for ; Sat, 19 Feb 2022 12:20:31 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=n096PBrb; spf=fail (domain: huawei.com, ip: 66.111.4.224, mailfrom: zygmunt.krynicki@huawei.com) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id 6E1EC580560; Sat, 19 Feb 2022 15:20:30 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 19 Feb 2022 15:20:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=56ZDIo RqZwNHsE23bKkq2A4EISjLxBhItCWsJCkWuwI=; b=n096PBrbsxqtGOLTEMCuvt dAMkx4lKmY+aRZJKS1+72/44qR+QX+4aEIarYxctGJ+stbQ3BgfZrmRQq9tWMXOt YzMzPTNdvwxeNGeW/xRz7CbSDZdh308iavEXebswcmTDJuVk73AzexPm/DXVOSum ZJ70DTUCaImKRN4bBzZFwN/p9o9A7Z54Kt8YyI5ncvk12a3G2NA/lbxEVFBRbn6X szyq9g1NMCN8D8bZ+XNMkp7F1yBCNUa+djDxumTf/tHDUKEF6ajvlqyTtU3AUqyP bneoU9GL0fCgvEDjxwjLEI4LrX2QLGaWe6TYCcTC8u/arjeqGMrPVBMyuKtgXKCw == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrkedvgddufeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepkgihghhmuhhnthcumfhrhihnihgtkhhiuceoiiihghhmuhhn thdrkhhrhihnihgtkhhisehhuhgrfigvihdrtghomheqnecuggftrfgrthhtvghrnhepgf ffgfejffefieeffeehtedvudfhkeeuteegieejvdehlefhhffhgfelheefueehnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepiiihghhmuhhnth drkhhrhihnihgtkhhisehhuhgrfigvihdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 19 Feb 2022 15:20:29 -0500 (EST) From: Zygmunt Krynicki To: bitbake-devel@lists.openembedded.org Cc: Zygmunt Krynicki Subject: [PATCH 3/3] simplediff: Add type annotations Date: Sat, 19 Feb 2022 21:19:57 +0100 Message-Id: <20220219201957.8329-3-zygmunt.krynicki@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220219201957.8329-1-zygmunt.krynicki@huawei.com> References: <20220219201957.8329-1-zygmunt.krynicki@huawei.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Sat, 19 Feb 2022 20:20:32 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/bitbake-devel/message/13391 I was trying to grok how various parts of bitbake work. As a habit I make type annotations, as those help both me and, perhaps, others. Introducing typing to an existing project is somewhat challenging, but by now, well established. It's good to start from any leaf module and work up from there. I've picked, somewhat at random, the simplediff module, since it's small, self-contained and might be used as a starting point for the discussion around type hints. With the patch applied, one can run mypy as follows: mypy --strict ./lib/simplediff The module now has strict typing compatibility. Allowing it to be used in a typed context. Signed-off-by: Zygmunt Krynicki --- lib/simplediff/__init__.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py index 64ec64b4..a1866ba1 100644 --- a/lib/simplediff/__init__.py +++ b/lib/simplediff/__init__.py @@ -13,8 +13,18 @@ May be used and distributed under the zlib/libpng license __all__ = ['diff', 'string_diff', 'html_diff'] __version__ = '1.0' - -def diff(old, new): +from typing import ( + Callable, + Dict, + List, + Sequence, + Tuple, + TypeVar, +) + +T = TypeVar('T', str, int) + +def diff(old: List[T], new: List[T]) -> List[Tuple[str, List[T]]]: ''' Find the differences between two lists. Returns a list of pairs, where the first value is in ['+','-','='] and represents an insertion, deletion, or @@ -53,7 +63,7 @@ def diff(old, new): ''' # Create a map from old values to their indices - old_index_map = dict() + old_index_map: Dict[T, List[int]] = dict() for i, val in enumerate(old): old_index_map.setdefault(val,list()).append(i) @@ -73,7 +83,7 @@ def diff(old, new): # seen so far (`sub_length`), we update the largest substring # to the overlapping strings. - overlap = dict() + overlap: Dict[int, int] = dict() # `sub_start_old` is the index of the beginning of the largest overlapping # substring in the old list. `sub_start_new` is the index of the beginning # of the same substring in the new list. `sub_length` is the length that @@ -85,7 +95,7 @@ def diff(old, new): sub_length = 0 for inew, val in enumerate(new): - _overlap = dict() + _overlap: Dict[int, int] = dict() for iold in old_index_map.get(val,list()): # now we are considering all values of iold such that # `old[iold] == new[inew]`. @@ -110,7 +120,7 @@ def diff(old, new): new[sub_start_new + sub_length : ]) -def string_diff(old, new): +def string_diff(old: str, new: str) -> List[Tuple[str, List[str]]]: ''' Returns the difference between the old and new strings when split on whitespace. Considers punctuation a part of the word @@ -139,7 +149,7 @@ def string_diff(old, new): return diff(old.split(), new.split()) -def html_diff(old, new): +def html_diff(old: str, new: str) -> str: ''' Returns the difference between two strings (as in stringDiff) in HTML format. HTML code in the strings is NOT escaped, so you @@ -160,13 +170,14 @@ def html_diff(old, new): >>> html_diff('The quick brown fox', 'The fast blue fox') 'The quick brown fast blue fox' ''' - con = {'=': (lambda x: x), + con: Dict[str, Callable[[str], str]] = { + '=': (lambda x: x), '+': (lambda x: "" + x + ""), '-': (lambda x: "" + x + "")} return " ".join([(con[a])(" ".join(b)) for a, b in string_diff(old, new)]) -def check_diff(old, new): +def check_diff(old: Sequence[str], new: Sequence[str]) -> None: ''' This tests that diffs returned by `diff` are valid. You probably won't want to use this function, but it's provided for documentation and