Re: Finding good documentation for gpiod

2023-09-03 Thread Barry via Python-list
E

> On 3 Sep 2023, at 22:49, Chris Green via Python-list  
> wrote:
> 
> Mostly I am managing to get things to work as I want but better
> documentation of gpiod would be a great help.

Ask the author? https://github.com/aswild/python-gpiod

Maybe read the source code for hints?

Barry
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forward References

2023-09-03 Thread MRAB via Python-list

On 2023-09-03 21:43, Jonathan Gossage via Python-list wrote:

I am attempting to use forward references in my program and I am failing.
This also does not work with the older way of putting the name of a class
as a string. Here is some sample code:

from __future__ import annotations

from dataclasses import dataclass
from typing import TypeAlias


ColorDef: TypeAlias = RGB | int | str



@dataclass(frozen=True, slots=True)
class RGB(object):

Can anyone suggest how I should fix this without reversing the statement
order?

 pass

The usual way to deal with forward type references is to use a string 
literal, i.e. 'RGB', but that doesn't work with '|', so use typing.Union 
instead:


from typing import TypeAlias, Union

ColorDef: TypeAlias = Union['RGB', int, str]

--
https://mail.python.org/mailman/listinfo/python-list


Re: iterations destroy reversed() results

2023-09-03 Thread Chris Angelico via Python-list
On Mon, 4 Sept 2023 at 07:44, Pierre Fortin via Python-list
 wrote:
>
> Hi,
>
> reversed() results are fine until iterated over, after which the
> results are no longer available. This was discovered after using
> something like this:
>
> rev = reversed( sorted( list ) )
> sr = sum( 1 for _ in rev )
> # rev is now destroyed
>
> So reversed() results can only be iterated once unlike sorted(), etc...

reversed() is like iter(), and should be used the same way:

for item in reversed(list):

If you want to eagerly construct a full reversed list, instead slice the list:

list[::-1]

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Passing info to function used in re.sub

2023-09-03 Thread Thomas Passin via Python-list

On 9/3/2023 12:10 PM, Jan Erik Moström via Python-list wrote:

I'm looking for some advice for how to write this in a clean way

I want to replace some text using a regex-pattern, but before creating 
replacement text I need to some file checking/copying etc. My code right now 
look something like this:

def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text

and the call comes here

global_var1 = "bla bla"
global_var2 = "pff"

new_text = re.sub(im_pattern,fix_stuff,md_text)


The "problem" is that I've currently written some code that works but it uses 
global variables ... and I don't like global variables. I assume there is a better way to 
write this, but how?

= jem


There are two things to keep in mind here, I think.  First, in Python a 
"global" variable is really module-level, so variables specific to one 
module seem fine and are common practice.


Second, the way you have written this example, it looks like these 
module-level "variables" are in effect constants.  In other words, they 
are just shorthand for specific declared quantities.  If this is so, 
then it makes even more sense to define them as module-level objects in 
the module that needs to use them.


If you still don't want to use them as "global" in your module, then 
define them in a separate module and import them from that module.


--
https://mail.python.org/mailman/listinfo/python-list


Re: iterations destroy reversed() results

2023-09-03 Thread Thomas Passin via Python-list

On 9/1/2023 12:15 PM, Pierre Fortin via Python-list wrote:

Hi,

reversed() results are fine until iterated over, after which the
results are no longer available. This was discovered after using
something like this:

rev = reversed( sorted( list ) )
sr = sum( 1 for _ in rev )
# rev is now destroyed

So reversed() results can only be iterated once unlike sorted(), etc...


reversed() is an iterator these days:

>>> l1 = [1, 2, 3]
>>> rev = reversed( sorted( l1 ) )
>>> type(rev)

>

--
https://mail.python.org/mailman/listinfo/python-list


Re: iterations destroy reversed() results

2023-09-03 Thread Dom Grigonis via Python-list
It is by design. `sorted` returns a list, while `reversed` returns an iterator. 
Iterators are exhaust-able, and not reusable. So be mindful of this and if you 
are going to "re-use” the sequence returned by iterator, convert it to list 
first.

Have a look at `itertools` library, which contains a lot of such functions and 
many good recipes on achieving various things elegantly using iterators.

> On 1 Sep 2023, at 19:15, Pierre Fortin via Python-list 
>  wrote:
> 
> Hi,
> 
> reversed() results are fine until iterated over, after which the
> results are no longer available. This was discovered after using
> something like this:
> 
> rev = reversed( sorted( list ) ) 
> sr = sum( 1 for _ in rev )
> # rev is now destroyed
> 
> So reversed() results can only be iterated once unlike sorted(), etc...
> 
> Script to illustrate the issue:
> /tmp/rev:
> orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
> co = sum( 1 for _ in orig )
> print( 'orig', orig, co )
> # reversing
> rev = reversed(orig)
> print( 'before iteration:', [ x for x in rev ] )
> # list comprehension was an iteration over 'rev'
> print( 'after iteration:', [ x for x in rev ] )
> # how this was discovered...
> orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
> rev = reversed(orig)
> cr = sum( 1 for _ in rev )
> print( 'after sum():', [ x for x in rev ] )
> 
> which produces:
> 
> $ python /tmp/rev
> orig ['x', 'a', 'y', 'b', 'z', 'c'] 6
> before iteration: ['c', 'z', 'b', 'y', 'a', 'x']
> after iteration: []
> after sum(): []
> 
> Regards,
> Pierre
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Finding good documentation for gpiod

2023-09-03 Thread Chris Green via Python-list
I am using the gpiod package for manipulating GPIO inputs/outputs on a
Beaglebone Black SBC (like a Raspberry Pi but with more flexible I/O).

Mostly I am managing to get things to work as I want but better
documentation of gpiod would be a great help.

For example, when one has found an I/O pin (a 'line' in GPIO parlance)
that one wants to use one has to 'request' it using the Line.request()
method.

The help for this is as follows:-

Help on method_descriptor:

request(...)
request(consumer[, type[, flags[, default_val]]]) -> None

Request this GPIO line.

  consumer
Name of the consumer.
  type
Type of the request.
  flags
Other configuration flags.
  default_val
Default value of this line.
Note: default_vals argument (sequence of default values passed down to
LineBulk.request()) is still supported for backward compatibility but is
now deprecated when requesting single lines.


Which is pretty good **except** that I can't find a proper description
of the parameters anywhere, i.e. there's nowhere that even tells me
what types of values/objects the parameters are.  At the end of the
gpiod.Line section of the help there is this:-

 |  ACTIVE_HIGH = 1
 |  
 |  ACTIVE_LOW = 2
 |  
 |  BIAS_AS_IS = 1
 |  
 |  BIAS_DISABLE = 2
 |  
 |  BIAS_PULL_DOWN = 4
 |  
 |  BIAS_PULL_UP = 3
 |  
 |  DIRECTION_INPUT = 1
 |  
 |  DIRECTION_OUTPUT = 2

Which **might** be appropriate values for 'type' or 'flags' but there
doesn't seem to be any way of knowing.

Am I missing something very obvious somewhere?  Is there a 'standard'
way of finding out parameter information?

It may well be that I'm simply banging up against the limit of what
documentation is available, I have managed to get code working OK.
It's just that I'd be happier if I really know what I was doing! :-)



-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


iterations destroy reversed() results

2023-09-03 Thread Pierre Fortin via Python-list
Hi,

reversed() results are fine until iterated over, after which the
results are no longer available. This was discovered after using
something like this:

rev = reversed( sorted( list ) ) 
sr = sum( 1 for _ in rev )
# rev is now destroyed

So reversed() results can only be iterated once unlike sorted(), etc...

Script to illustrate the issue:
/tmp/rev:
orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
co = sum( 1 for _ in orig )
print( 'orig', orig, co )
# reversing
rev = reversed(orig)
print( 'before iteration:', [ x for x in rev ] )
# list comprehension was an iteration over 'rev'
print( 'after iteration:', [ x for x in rev ] )
# how this was discovered...
orig = [ 'x', 'a', 'y', 'b', 'z', 'c' ]
rev = reversed(orig)
cr = sum( 1 for _ in rev )
print( 'after sum():', [ x for x in rev ] )

which produces:

$ python /tmp/rev
orig ['x', 'a', 'y', 'b', 'z', 'c'] 6
before iteration: ['c', 'z', 'b', 'y', 'a', 'x']
after iteration: []
after sum(): []

Regards,
Pierre
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do I always get an exception raised in this __init__()?

2023-09-03 Thread Chris Green via Python-list
Alan Gauld  wrote:
> On 31/08/2023 22:15, Chris Green via Python-list wrote:
> 
> > class Gpiopin:
> > 
> > def __init__(self, pin):
> > # 
> > #  
> > # scan through the GPIO chips to find the line/pin we want 
> > # 
> > for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
> >  
> > chip = gpiod.Chip(c)
> > for l in range(32):
> > line = chip.get_line(l)
> > if pin in line.name():
> > print("Found: ", line.name())
> > return
> > else:
> > raise ValueError("Can't find pin '" + pin + "'")
> 
> You don't store the line anywhere.
> You need to use self.line
> self.line = chip.get_line(l)
> if pin...
> 
> > def print_name(self): 
> > print (self.line.name()) 
> >  
> > def set(self): 
> > self.line.set_value(1) 
> >
> > def clear(self): 
> > self.line.set_value(0) 
> 
> As you do here.
> 
Yes, OK, absolutely.  However that wasn't my original rather basic
problem which was, as I said, that I wasn't running the code I was
looking at.

The above was just a quick hack from some even cruder code doing the
same job, trying to develop it into something better and more general.

It's all on a headless Beaglebone Black (bit like a Raspberry Pi) so
I'm doing everything via multiple ssh connections and sometimes this
results in "the left hand not knowing what the right hand is doing"!

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Forward References

2023-09-03 Thread Jonathan Gossage via Python-list
I am attempting to use forward references in my program and I am failing.
This also does not work with the older way of putting the name of a class
as a string. Here is some sample code:

from __future__ import annotations

from dataclasses import dataclass
from typing import TypeAlias


ColorDef: TypeAlias = RGB | int | str



@dataclass(frozen=True, slots=True)
class RGB(object):

Can anyone suggest how I should fix this without reversing the statement
order?

pass


-- 
Jonathan Gossage
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Passing info to function used in re.sub

2023-09-03 Thread Jan Erik Moström via Python-list
On 3 Sep 2023, at 19:13, MRAB via Python-list wrote:

> You could use pass an anonymous function (a lambda) to re.sub:

Of course !! Thanks.

= jem
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Passing info to function used in re.sub

2023-09-03 Thread MRAB via Python-list

On 2023-09-03 17:10, Jan Erik Moström via Python-list wrote:

I'm looking for some advice for how to write this in a clean way

I want to replace some text using a regex-pattern, but before creating 
replacement text I need to some file checking/copying etc. My code right now 
look something like this:

def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text

and the call comes here

global_var1 = "bla bla"
global_var2 = "pff"

new_text = re.sub(im_pattern,fix_stuff,md_text)


The "problem" is that I've currently written some code that works but it uses 
global variables ... and I don't like global variables. I assume there is a better way to 
write this, but how?


You could use pass an anonymous function (a lambda) to re.sub:

def fix_stuff(m, var1, var2):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + var1 + var2
return replacement_text

global_var1 = "bla bla"
global_var2 = "pff"

new_text = re.sub(im_pattern, lambda m, var1=global_var1, 
var2=global_var2: fix_stuff(m, var1, var2), md_text)


Or, if you prefer a named function, define one just before the re.sub:

def fix_stuff(m, var1, var2):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + var1 + var2
return replacement_text

global_var1 = "bla bla"
global_var2 = "pff"

def fix_it(m, var1=global_var1, var2=global_var2):
return fix_stuff(m, var1, var2)

new_text = re.sub(im_pattern, fix_it, md_text)

--
https://mail.python.org/mailman/listinfo/python-list


Passing info to function used in re.sub

2023-09-03 Thread Jan Erik Moström via Python-list
I'm looking for some advice for how to write this in a clean way

I want to replace some text using a regex-pattern, but before creating 
replacement text I need to some file checking/copying etc. My code right now 
look something like this:

def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + global_var1 + global_var2
return replacement_text

and the call comes here

global_var1 = "bla bla"
global_var2 = "pff"

new_text = re.sub(im_pattern,fix_stuff,md_text)


The "problem" is that I've currently written some code that works but it uses 
global variables ... and I don't like global variables. I assume there is a 
better way to write this, but how?

= jem
-- 
https://mail.python.org/mailman/listinfo/python-list


[Python-announce] [RELEASE] pyspread 2.2.2

2023-09-03 Thread Martin Manns


pyspread 2.2.2
==

This is a bugfix release.

Bug fixes:
 * pip install fixed by replacing deprecated setup.py option `requires` 
with `install_requires`

 * Shebang removed from desktop file



About pyspread
==

Pyspread is a non-traditional spreadsheet that is based on and written
in the programming language Python.

The goal of pyspread is to be the most pythonic spreadsheet application.

Pyspread is free software. It is released under the GPL v3.

Project website:   https://pyspread.gitlab.io/
Download page: https://pypi.org/project/pyspread/
Signature for tarball: 
https://gitlab.com/pyspread/downloads/-/raw/master/releases/pyspread-2.2.2.tar.gz.asc?inline=false

Source code:   https://gitlab.com/pyspread/pyspread


Dependencies


Mandatory:
 * Python (≥ 3.6)
 * numpy (>=1.1)
 * PyQt5 (≥ 5.10, requires PyQt5.Svg)
 * setuptools (>=40.0)
 * markdown2 (>= 2.3)

Recommended:
 * matplotlib (>=1.1.1)
 * pyenchant (>=1.1)
 * pip (>=18)
 * python-dateutil (>= 2.7.0)
 * py-moneyed (>=2.0)
 * rpy2 (>=3.4)
 * plotnine (>=0.8)
 * libvoikko (>=4.3)
 * nuspell (>=5.1)
 * hspell (>= 1.4)
 * ggplot2 (>=3.4)


For building the apidocs with Sphinx see apidocs/requirements.txt


Enjoy

Martin
___
Python-announce-list mailing list -- python-announce-list@python.org
To unsubscribe send an email to python-announce-list-le...@python.org
https://mail.python.org/mailman3/lists/python-announce-list.python.org/
Member address: arch...@mail-archive.com