linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	linux-gpio@vger.kernel.org
Subject: Re: [libgpiod v2][PATCH v3 2/4] bindings: python: add examples
Date: Tue, 18 Oct 2022 00:20:18 +0800	[thread overview]
Message-ID: <Y02Awqv4w3oC1Yib@sol> (raw)
In-Reply-To: <Y01+JhOMc53Dhps1@sol>

On Tue, Oct 18, 2022 at 12:09:10AM +0800, Kent Gibson wrote:
> On Mon, Oct 17, 2022 at 05:53:52PM +0200, Bartosz Golaszewski wrote:
> > On Mon, Oct 17, 2022 at 4:19 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:
> > >
> > > On Mon, Oct 17, 2022 at 10:07:17PM +0800, Kent Gibson wrote:
> > > > On Mon, Oct 17, 2022 at 04:49:55PM +0300, Andy Shevchenko wrote:
> > > > > On Mon, Oct 17, 2022 at 08:11:28PM +0800, Kent Gibson wrote:
> > > > > > On Mon, Oct 17, 2022 at 02:00:15PM +0200, Bartosz Golaszewski wrote:
> > > > > > > On Thu, Oct 13, 2022 at 5:09 AM Kent Gibson <warthog618@gmail.com> wrote:
> > > > > > > > On Fri, Oct 07, 2022 at 04:55:19PM +0200, Bartosz Golaszewski wrote:
> > >
> > > ...
> > >
> > > > > > > >         lvs = [ arg.split('=') for arg in sys.argv[2:] ]
> > > > > >             lvs = [ (x,int(y)) for (x,y) in lvs ]
> > > > > > > >         lines = [ x[0] for x in lvs ]
> > > > > > > >         values = dict(lvs)
> > > > > > >
> > > > > >
> > > > > > An extra pass to fix the int values.
> > > > >
> > > > > In Python we have map(), which I think is the best for that kind of job.
> > > > >
> > > >
> > > > My understanding is map/filter is old school and list comprehensions
> > > > have replaced map, as generators have replaced filter.
> > > >
> > > > i.e.
> > > >     list(map(function, iterable))
> > > > becomes
> > > >     [function(x) for x in iterable]
> > >
> > > Definitely it does not cover all the cases map() is taking care of.
> > > So it can't be old school :-)
> > >
> > > * Yes, in this particular case it may be map() or list comprehension.
> > >   But I think with map() the two lines can become one.
> > >
> > > > Either way, what we are missing here is a parser function that gives us
> > > > exactly the (offset,value) output we want from the command line string.
> > > >
> > > > Oh, and we need both the lines list and the values dict, both of which
> > > > are easily created from the interim lvs.
> > > >
> > > > > > You could do it in one with a more appropriate parser function.
> > > > >
> > > > > It seems we need some Python guru to revisit the code, because to me
> > > > > it looks a bit C:ish :-)
> > > >
> > > > The for loop or the list comprehension?
> > > > Last I checked only one of those is available in C.
> > > > And yeah, the for loop version reads as C, so not at all Pythonic,
> > > > which is why I suggested the list comprehension.
> > >
> > > Yes, but I believe it does not utilize the powerfulness of the current Python.
> > > Anyway, I'm not a Py guru, take my remarks with a grain of salt.
> > >
> > 
> > How about this?
> > 
> >     lvs = list(
> >         map(
> >             lambda val: [val[0], Value(int(val[1]))],
> >             [arg.split("=") for arg in sys.argv[2:]],
> >         )
> >     )
> 
> which is the same as
> 
>     lvs = [ (x,Value(int(y))) for (x,y) in [ arg.split("=") for arg in sys.argv[2:]] ]
> 
> which is the same as my two liner, just nested - though it may only
> iterate through the list once if the inner list comprehension is
> treated as a generator.  Not sure.
> 
> Either way, not too fussed - it is only example code.
> As long as it isn't a for loop ;-).
> 

Oh, btw, the parser fn version would be something like:

def parse_value(arg):
     (x,y) = arg.split("=")
     return (x, Value(int(y)))

lvs = [ parse_value(arg) for arg in sys.argv[2:]
...

Is that clearer?

Cheers,
Kent.


  reply	other threads:[~2022-10-17 16:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-07 14:55 [libgpiod v2][PATCH v3 0/4] bindings: implement python bindings for libgpiod v2 Bartosz Golaszewski
2022-10-07 14:55 ` [libgpiod v2][PATCH v3 1/4] bindings: python: remove old version Bartosz Golaszewski
2022-10-07 14:55 ` [libgpiod v2][PATCH v3 2/4] bindings: python: add examples Bartosz Golaszewski
2022-10-13  3:09   ` Kent Gibson
2022-10-17 12:00     ` Bartosz Golaszewski
2022-10-17 12:11       ` Kent Gibson
2022-10-17 13:49         ` Andy Shevchenko
2022-10-17 14:07           ` Kent Gibson
2022-10-17 14:19             ` Andy Shevchenko
2022-10-17 15:53               ` Bartosz Golaszewski
2022-10-17 16:09                 ` Kent Gibson
2022-10-17 16:20                   ` Kent Gibson [this message]
2022-10-17 16:55                     ` Andy Shevchenko
2022-10-17 16:57                       ` Andy Shevchenko
2022-10-17 17:26                         ` Bartosz Golaszewski
2022-10-17 16:24                 ` Andy Shevchenko
2022-10-17 16:39                   ` Kent Gibson
2022-10-07 14:55 ` [libgpiod v2][PATCH v3 3/4] bindings: python: add tests Bartosz Golaszewski
2022-10-13  3:09   ` Kent Gibson
2022-10-07 14:55 ` [libgpiod v2][PATCH v3 4/4] bindings: python: implement python bindings for libgpiod v2 Bartosz Golaszewski
2022-10-07 15:26   ` Andy Shevchenko
2022-10-07 18:19     ` Bartosz Golaszewski
2022-10-13  3:10   ` Kent Gibson
2022-10-13 11:12     ` Kent Gibson
2022-10-26 12:32     ` Bartosz Golaszewski
2022-10-26 12:56       ` Kent Gibson
2022-10-12 12:34 ` [libgpiod v2][PATCH v3 0/4] bindings: " Bartosz Golaszewski
2022-10-12 12:41   ` Kent Gibson
2022-10-12 12:51     ` Bartosz Golaszewski
2022-10-12 13:03       ` Kent Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y02Awqv4w3oC1Yib@sol \
    --to=warthog618@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).