Re: [libgpiod v2][PATCH v3 2/4] bindings: python: add examples

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx> wrote:
> > > > On Fri, Oct 07, 2022 at 04:55:19PM +0200, Bartosz Golaszewski wrote:
> > > > > This adds the regular set of example programs implemented using libgpiod
> > > > > python bindings.
> > > > >
> > > > > Signed-off-by: Bartosz Golaszewski <brgl@xxxxxxxx>
> > > > > +    path = sys.argv[1]
> > > > > +    values = dict()
> > > > > +    lines = []
> > > > > +    for arg in sys.argv[2:]:
> > > > > +        arg = arg.split("=")
> > > > > +        key = int(arg[0]) if arg[0].isdigit() else arg[0]
> > > > > +        val = int(arg[1])
> > > > > +
> > > > > +        lines.append(key)
> > > > > +        values[key] = Value(val)
> > > > > +
> > > >
> > > >         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]

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.

> Maybe I can ask colleague of mine, if he has time for a such...
> No guarantees, though.
> 

Maybe I'm wrong and they've flipped back to map/filter.
Stanger things have happened.

Cheers,
Kent.



[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux