Re: evaluation question

2023-01-30 Thread Greg Ewing

On 30/01/23 10:41 pm, mutt...@dastardlyhq.com wrote:

What was the point of the upheaval of converting
the print command in python 2 into a function in python 3 if as a function
print() doesn't return anything useful?


It was made a function because there's no good reason for it
to have special syntax in the language.

Functions don't need to return things to justify their existence,
and in fact the usual convention is that functions whose purpose
is to have an effect just return None.

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


Re: Custom help format for a choice argparse argument

2023-01-30 Thread Peter Otten

On 27/01/2023 21:31, Ivan "Rambius" Ivanov wrote:

Hello,

I am developing a script that accepts a time zone as an option. The
time zone can be any from pytz.all_timezones. I have

def main():
 parser = argparse.ArgumentParser()
 parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
 args = parser.parse_args()
 print(args)
 print(f"Specified timezone: {args.zone}")

It works, but when I run it with the -h option it dumps all entries in
pytz.all_timezones. I would like to modify the help format for just
-z|--zone option. I read the docs about HelpFormatter and argparse.py
and I ended up with

class CustomHelpFormatter(argparse.HelpFormatter):
 def _metavar_formatter(self, action, default_metavar):
 if action.dest == 'zone':
 result = 'zone from pytz.all_timezones'
 def format(tuple_size):
 if isinstance(result, tuple):
 return result
 else:
 return (result, ) * tuple_size
 return format
 else:
 return super(CustomHelpFormatter,
self)._metavar_formatter(action, default_metavar)


def main():
 parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
 parser.add_argument("-z", "--zone", choices=pytz.all_timezones)
 args = parser.parse_args()
 print(args)
 print(f"Specified timezone: {args.zone}")

This works, but is there a more elegant way to achieve it?


It may be sufficient to specify a metavar:

>>> import argparse
>>> p = argparse.ArgumentParser()
>>> p.add_argument("--foo", choices="alpha beta gamma".split(),
metavar="")
[...]
>>> p.parse_args(["-h"])
usage: [-h] [--foo ]

optional arguments:
  -h, --helpshow this help message and exit
  --foo 

While that helps with --help it doesn't always prevent the choices list
from being spelt out:

>>> p.parse_args(["--foo", "whatever"])
usage: [-h] [--foo ]
: error: argument --foo: invalid choice: 'whatever' (choose from
'alpha', 'beta', 'gamma')

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


Re: evaluation question

2023-01-30 Thread Rob Cliffe via Python-list



On 30/01/2023 09:41, mutt...@dastardlyhq.com wrote:

On Sun, 29 Jan 2023 23:57:51 -0500
Thomas Passin  wrote:

On 1/29/2023 4:15 PM, elvis-85...@notatla.org.uk wrote:

On 2023-01-28, Louis Krupp  wrote:

On 1/27/2023 9:37 AM, mutt...@dastardlyhq.com wrote:



eval("print(123)")

123


Does OP expect the text to come from the eval or from the print?


x = print( [i for i in range(1, 10)] )

[1, 2, 3, 4, 5, 6, 7, 8, 9]


x

   (nothing printed)

Because print() returns nothing (i.e., the statement x is None is True).

I don't understand this. What was the point of the upheaval of converting
the print command in python 2 into a function in python 3 if as a function
print() doesn't return anything useful? Surely even the length of the
formatted string as per C's sprintf() function would be helpful?


That's a fair question, or rather 2 fair questions.
There is an explanation of why the change was made at
    https://snarky.ca/why-print-became-a-function-in-python-3/
In brief: (a) the print() function is more flexible and can be used in 
expressions
               (b) Python's syntax was simplified by dropping the 
special syntax used by the print statement.
sys.stdout.write() does return the number of characters output (you 
could use this instead of print() if you need this;

remember to add a '\n' character at the end of  a line).  I guess the option
of making print() do the same either was not considered, or was 
rejected, when print was made a function.

Best wishes
Rob Cliffe

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


Re: evaluation question

2023-01-30 Thread Muttley
On Sun, 29 Jan 2023 23:57:51 -0500
Thomas Passin  wrote:
>On 1/29/2023 4:15 PM, elvis-85...@notatla.org.uk wrote:
>> On 2023-01-28, Louis Krupp  wrote:
>>> On 1/27/2023 9:37 AM, mutt...@dastardlyhq.com wrote:
>> 
>> 
>>> eval("print(123)")
 123
>> 
>> 
>> Does OP expect the text to come from the eval or from the print?
>> 
> x = print( [i for i in range(1, 10)] )
>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
>> 
> x
>>   (nothing printed)
>
>Because print() returns nothing (i.e., the statement x is None is True). 

I don't understand this. What was the point of the upheaval of converting
the print command in python 2 into a function in python 3 if as a function
print() doesn't return anything useful? Surely even the length of the 
formatted string as per C's sprintf() function would be helpful?

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


Re: Usenet vs. Mailing-list

2023-01-30 Thread Grant Edwards
On 2023-01-28, Dennis Lee Bieber  wrote:
> On Sat, 28 Jan 2023 20:07:44 +, Chris Green  declaimed the
> following:
>
>
>>As far as I am aware the mirroring of the Python mailing list on
>>comp.lan.python works perfectly.  I love gmane! :-)
>
>   Is gmane's gmane.comp.python.general allowing posts to go through
> again? I had to revert to comp.lang.python some time back when gmane kept
> rejecting outgoing posts.

No. FWIW, it's the mailing list that's blocking them, not Gmane.

That's why I wrote this:

https://github.com/GrantEdwards/hybrid-inews

It's an inews work-alike that submits most posts via gmanes NNTP
server, but will deal with particular groups
(e.g. gmane.comp.python.general) that want posts submitted via email.

It allows me to continue to read (and post to) the Python mailling
list via slrn pointed at gmane.

It is, of course, written in Python.


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


Re: Usenet vs. Mailing-list

2023-01-30 Thread Grant Edwards
On 2023-01-28, Chris Green  wrote:

> As far as I am aware the mirroring of the Python mailing list on
> comp.lan.python works perfectly.  I love gmane! :-)

If gmane stopped working, I'd have to retire and give up on computers.

I supposed I might be able to hammer procmail and mutt into something
tolerable, but slrn pointed at gmane works wonderfully without any
futzing.

NNTP and newsreaders are designed specifically for the task to which
people have coopted e-mail into via mailing lists.

--
Grant

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