From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 41EC644178; Thu, 6 Jun 2024 23:15:00 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EB7B740EDF; Thu, 6 Jun 2024 23:14:59 +0200 (CEST) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mails.dpdk.org (Postfix) with ESMTP id 4EFCE40EAB for ; Thu, 6 Jun 2024 23:14:58 +0200 (CEST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C72602F4; Thu, 6 Jun 2024 14:15:21 -0700 (PDT) Received: from [192.168.50.86] (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A00933F792; Thu, 6 Jun 2024 14:14:56 -0700 (PDT) Message-ID: <1233a90e-1219-420c-b55e-0cb525908d7d@arm.com> Date: Thu, 6 Jun 2024 22:14:55 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 3/5] dts: add parsing utility module Content-Language: en-GB To: Jeremy Spewock Cc: dev@dpdk.org, =?UTF-8?Q?Juraj_Linke=C5=A1?= , Paul Szczepanek References: <20240412111136.3470304-1-luca.vizzarro@arm.com> <20240606091718.151516-1-luca.vizzarro@arm.com> <20240606091718.151516-4-luca.vizzarro@arm.com> From: Luca Vizzarro In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org On 06/06/2024 19:52, Jeremy Spewock wrote: >> + @classmethod >> + def from_str(cls, text: str): >> + match text: >> + case "black": >> + return cls.BLACK >> + case "white": >> + return cls.WHITE >> + case _: >> + return None # unsupported colour >> + >> + @classmethod >> + def make_parser(cls): >> + # make a parser function that finds a match and >> + # then makes it a Colour object through Colour.from_str >> + return TextParser.wrap(cls.from_str, TextParser.find(r"is a (\w+)")) > > I think this example is backwards now that you changed the parameters > to calling order isn't it? We need to call find first and then pass > that into from_str. aargh! yes, you are right. thank you! and well spotted! >> + >> + @dataclass >> + class Animal(TextParser): >> + kind: str = field(metadata=TextParser.find(r"is a \w+ (\w+)")) >> + name: str = field(metadata=TextParser.find(r"^(\w+)")) >> + colour: Colour = field(metadata=Colour.make_parser()) >> + age: int = field(metadata=TextParser.find_int(r"aged (\d+)")) >> + >> + steph = Animal.parse("Stephanie is a white cat aged 10") >> + print(steph) # Animal(kind='cat', name='Stephanie', colour=, age=10) >> + """ >> + > >> + @staticmethod >> + def find( >> + pattern: str | re.Pattern[str], >> + flags: re.RegexFlag = re.RegexFlag(0), >> + named: bool = False, >> + ) -> ParserFn: >> + """Makes a parser function that finds a regular expression match in the text. >> + >> + If the pattern has any capturing groups, it returns None if no match was found, otherwise a >> + tuple containing the values per each group is returned.. If the pattern has only one > > It looks like there are two periods here by mistake. well spotted again!