Re: Making command-line args available to deeply-nested functions

2021-08-21 Thread George Fischhof
Loris Bennett  ezt írta (időpont: 2021. aug.
20., P 17:54):

> Julio Di Egidio  writes:
>
> > On Friday, 20 August 2021 at 11:54:00 UTC+2, Loris Bennett wrote:
> >> Hi,
> >>
> >> TL;DR:
> >>
> >> If I have a command-line argument for a program, what is the best way
> >> of making this available to a deeply-nested[1] function call without
> >> passing the parameter through every intermediate function?
> >
> > To not pass arguments you need shared state ("global variables"): and
> > options in shard state, unless you have very good reasons to do
> > otherwise, is simply a no no.
>
> Doesn't that slightly depend on the size of your "globe"?  If a program
> does a few, in some sense unrelated things, and, say, only runs for a
> few minutes, could you not decide to make a particular parameter global,
> even though only one function needs it?  In general, however, I would
> also avoid this.
>
> > 
> >> I can see that the top-level could just create an object from a class
> >> which encapsulates everything, but what if I want to keep the salutation
> >> generation separate, so that I can have a separate program which just
> >> generates the salutation and print it to the terminal?
> >
> > Yes, that's basically the way to go: parse arguments into a structure (an
> > object) that contains all options/parameters then pass that down.  Next
> level:
> > some sections of your code may require a certain subset of those
> options, some
> > may require some other, so you would structure your options object in
> > sub-objects for the various sets of correlated options, then rather pass
> just
> > the sub-object(s) that are relevant to the section of code you are
> calling.
> > Variations are of course possible, anyway that's the basic idea.
> >
> > Also have a look at the "argparse" library, it does all the heavy
> lifting for
> > the parsing and creation of those objects, definitely advised for in non
> trivial
> > cases: .
>
> I am already using 'argparse' ('configargparse' actually).  What aspect
> should I be looking at in order to produce "sub-objects"?
>
> >> I guess I am really asking how to avoid "passing through" arguments to
> >> functions which only need them to call other functions, so maybe the
> >> answer is just to avoid nesting.
> >
> > No, you don't get rid of code structure just not to pass arguments to
> > a function...  Code may be poorly structured, but that's another
> > story.
>
> As I am writing new code it is more a question of imposing structure,
> rather than getting rid of structure.  Unwritten code for a given
> purpose obviously has some sort of structure with regards to, say, loops
> and conditions, but I am less sure about the implications for how the
> code should be nested.  Another argument against deeply-nested functions
> is the increased complexity of testing.
>
> Cheers,
>
> Loris
>
> --
> This signature is currently under construction.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

>
>
>


Hi,

Also you can give a try to click and / or  typer packages.
Putting args into environment variables can be a solution too
All of these depends on several things: personal preferences, colleagues /
firm standards, the program, readability, variable accessibility (IDE
support, auto completition) (env vars not supported by IDEs as they are not
part of code)

BR,
George
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-21 Thread Hope Rouselle
Greg Ewing  writes:

> On 21/08/21 1:36 pm, Hope Rouselle wrote:
>> I wish I could restrict their syntax too, though, but I fear that's
>> not possible.  For instance, it would be very useful if I could
>> remove loops.
>
> Actually you could, using ast.parse to get an AST and then walk
> it looking for things you don't want to allow.

Very interesting!  Thanks very much.  That would let me block them,
though the ideal would be a certain python.exe binary that simply blows
a helpful syntax error when they use something the course doesn't allow.
But surely the course could provide students with a certain module or
procedure which would validate their work.  (Don't turn in unless you
pass these verifications.)

> You could also play around with giving them a custom set of
> builtins and restricting what they can import. Just be aware
> that such things are not foolproof, and a sufficiently smart
> student could find ways around them. (Although if they know
> that much they probably deserve to pass the course anyway!)

So true!  If they can get around such verifications, they should present
their work at an extra-curricular sessions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-21 Thread Greg Ewing

On 21/08/21 1:36 pm, Hope Rouselle wrote:

I wish I could restrict their syntax too, though,
but I fear that's not possible.  For instance, it would be very useful
if I could remove loops.


Actually you could, using ast.parse to get an AST and then walk
it looking for things you don't want to allow.

You could also play around with giving them a custom set of
builtins and restricting what they can import. Just be aware
that such things are not foolproof, and a sufficiently smart
student could find ways around them. (Although if they know
that much they probably deserve to pass the course anyway!)

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


sting to list of enums

2021-08-21 Thread Josef Kleber
Hi,

I need to configure a script with a command line switch and/or
environment variable (-> string) to a list of enums. I found the
following to work:

from enum import Enum

class Provider(Enum):
NONE = 0, ''
Amazon = 40, 'Amazon'
Netflix = 42, 'Netflix'
SkyTicket = 104, 'Sky Ticket'
AmazonChannels = 140, 'Amazon Channels'
Disney = 176, 'Disney+'

def __new__(cls, value, name):
member = object.__new__(cls)
member._value_ = value
member.fullname = name
return member

def __int__(self):
return self.value

def __str__(self):
return self.fullname

providers = []
test = "Amazon, AmazonChannels, Netflix, Disney, SkyTicket"

for t in test.split(','):
providers.append(Provider[t.strip()])

print(providers)

I would prefer a more direct input string like "Provider.Amazon,
Provider.Netfilx" Any idea?

Or even a better way from cli option to enum list?

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


tkinter

2021-08-21 Thread Tony Genter
   Tkinter stopped working overnight from 8/20/2021 to 8/21/2021.  Last night
   I was working on tutorials to work on a GUI and this morning every file
   that uses tkinter is broken stating that no module `tkinter' exists.



   Please let me know if there is some sort of problem.  I am removing visual
   studios, all versions of python, sublime text, atom, etc and reinstalling
   all of it to try and resolve the issue.





   Thanks,



   Talat



   Sent from [1]Mail for Windows





References

   Visible links
   1. https://go.microsoft.com/fwlink/?LinkId=550986
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-21 Thread Chris Angelico
On Sun, Aug 22, 2021 at 4:37 AM Hope Rouselle  wrote:
>
> Greg Ewing  writes:
>
> > On 21/08/21 1:36 pm, Hope Rouselle wrote:
> >> I wish I could restrict their syntax too, though, but I fear that's
> >> not possible.  For instance, it would be very useful if I could
> >> remove loops.
> >
> > Actually you could, using ast.parse to get an AST and then walk
> > it looking for things you don't want to allow.
>
> Very interesting!  Thanks very much.  That would let me block them,
> though the ideal would be a certain python.exe binary that simply blows
> a helpful syntax error when they use something the course doesn't allow.
> But surely the course could provide students with a certain module or
> procedure which would validate their work.  (Don't turn in unless you
> pass these verifications.)
>
> > You could also play around with giving them a custom set of
> > builtins and restricting what they can import. Just be aware
> > that such things are not foolproof, and a sufficiently smart
> > student could find ways around them. (Although if they know
> > that much they probably deserve to pass the course anyway!)
>
> So true!  If they can get around such verifications, they should present
> their work at an extra-curricular sessions.

Agreed... if they do it knowingly. On the other hand, if they just
turn in code copied blindly from Stack Overflow...

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


Re: basic auth request

2021-08-21 Thread Martin Di Paola
While it is correct to say that Basic Auth without HTTPS is absolutely 
insecure, using Basic Auth *and* HTTPS is not secure either.


Well, the definition of "secure" depends of your threat model.

HTTPS ensures encryption so the content, including the Basic Auth 
username and password, is secret for any external observer.


But it is *not* secret for the receiver (the server): if it was 
compromised an adversary will have access to your password. It is much 
easier to print a captured password than cracking the hashes.


Other authentication mechanisms exist, like OAuth, which are more 
"secure".


Thanks,
Martin


On Wed, Aug 18, 2021 at 11:05:46PM -, Jon Ribbens via Python-list wrote:

On 2021-08-18, Robin Becker  wrote:

On 17/08/2021 22:47, Jon Ribbens via Python-list wrote:
...

That's only true if you're not using HTTPS - and you should *never*
not be using HTTPS, and that goes double if forms are being filled
in and double again if passwords are being supplied.


I think I agree with most of the replies; I understood from reading
the rfc that the charset is utf8 (presumably without ':')


The username can't contain a ':'. It shouldn't matter in the password.


and that basic auth is considered insecure. It is being used over
https so should avoid the simplest net scanning.


It's not insecure over HTTPS. Bear in mind the Basic Auth RFC was
written when HTTP was the standard and HTTPS was unusual. The positions
are now effectively reversed.


I googled a bunch of ways to do this, but many come down to 1) using
the requests package or 2) setting up an opener. Both of these seem to
be much more complex than is required to add the header.

I thought there might be a shortcut or more elegant way to replace the
old code, but it seems not


It's only a trivial str/bytes difference, it shouldn't be any big deal.
But using 'requests' instead is likely to simplify things and doesn't
tend to be an onerous dependency.
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: basic auth request

2021-08-21 Thread Chris Angelico
On Sun, Aug 22, 2021 at 4:55 AM Martin Di Paola
 wrote:
>
> While it is correct to say that Basic Auth without HTTPS is absolutely
> insecure, using Basic Auth *and* HTTPS is not secure either.
>
> Well, the definition of "secure" depends of your threat model.

Yes. Which makes statements like "not secure" rather suspect :)

> HTTPS ensures encryption so the content, including the Basic Auth
> username and password, is secret for any external observer.
>
> But it is *not* secret for the receiver (the server): if it was
> compromised an adversary will have access to your password. It is much
> easier to print a captured password than cracking the hashes.
>
> Other authentication mechanisms exist, like OAuth, which are more
> "secure".

If your server is compromised in that way, *all is lost*. If an
attacker is actually running code on your server, listening to your
sockets, after everything's decrypted, then *shut that server down*. I
don't think there is ANY security model that can handle this - if
you're using OAuth, and the server is compromised, then your client ID
and client secret are just as visible to the attacker as passwords
would be.

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


Re: on perhaps unloading modules?

2021-08-21 Thread Hope Rouselle
Chris Angelico  writes:

> On Sun, Aug 22, 2021 at 4:37 AM Hope Rouselle  wrote:
>>
>> Greg Ewing  writes:
>>
>> > On 21/08/21 1:36 pm, Hope Rouselle wrote:
>> >> I wish I could restrict their syntax too, though, but I fear that's
>> >> not possible.  For instance, it would be very useful if I could
>> >> remove loops.
>> >
>> > Actually you could, using ast.parse to get an AST and then walk
>> > it looking for things you don't want to allow.
>>
>> Very interesting!  Thanks very much.  That would let me block them,
>> though the ideal would be a certain python.exe binary that simply blows
>> a helpful syntax error when they use something the course doesn't allow.
>> But surely the course could provide students with a certain module or
>> procedure which would validate their work.  (Don't turn in unless you
>> pass these verifications.)
>>
>> > You could also play around with giving them a custom set of
>> > builtins and restricting what they can import. Just be aware
>> > that such things are not foolproof, and a sufficiently smart
>> > student could find ways around them. (Although if they know
>> > that much they probably deserve to pass the course anyway!)
>>
>> So true!  If they can get around such verifications, they should present
>> their work at an extra-curricular sessions.
>
> Agreed... if they do it knowingly. On the other hand, if they just
> turn in code copied blindly from Stack Overflow...

When a student does something impressive, we should recognize it.
(Don't we do this with ourselves?  That's why we should.  Not because
it's a strategy to sort of encourage the behavior, but because it is our
culture.)  If if turns out they're clueless about their own feat, we
probably would notice by recognizing it in the first time.  Being so
cool, they could present it to everyone.  (This is not a strategy to
sort of catch plagiarism, but rather a sober modus operandi that seems
to work well.)  Copying blindly from somewhere could be the event that
triggers a deep interest.

One of the things that attracted me to computers was the keyboard.  Not
sure if it was the first thing.  But it's pretty.  One time I saw
Microsoft Windows and someone let me play on Paint.  I loved the Bézier
curves and some of the colors.  Then came the Internet.  Wow!  The
Internet brought me UNIX and I began to see computer code.  I was
impressed at how someone could just understand such things.  I wanted to
write some PHP precisely because it looked so much more cryptic than
Allaire ColdFusion.  Then C looked even more cryptic, so I fell in love
with C.

Maybe a little copy from wherever could be the spark for bigger things.
Education is a fantastically difficult thing and making mistakes seems
so involved with it that it's perhaps undetachable from it.  Of course,
we should stop ourselves from making irreversible mistakes such as
falling from a precipice.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter

2021-08-21 Thread Alan Gauld via Python-list
On 21/08/2021 19:37, Tony Genter wrote:
>Tkinter stopped working overnight from 8/20/2021 to 8/21/2021.  Last night
>I was working on tutorials to work on a GUI and this morning every file
>that uses tkinter is broken stating that no module `tkinter' exists.

Are you sure you were running Python v3 and not python v2?
In v2 tkinter is spelled Tkinter.

If that is the issue then reinstalling python v3.x and ensuring
that it is the executable used should resolve the issue.

Another option might be that you are using virtual environments
which could result in all manner of chaos and confusion if you
inadvertently start the wrong one...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: Python and Ubuntu versions

2021-08-21 Thread Peter J. Holzer
On 2021-07-23 15:17:59 +1000, Cameron Simpson wrote:
> On 23Jul2021 07:54, אורי  wrote:
> >I have a production server with Ubuntu 18.04 LTS (currently upgraded to
> >Ubuntu 18.04.5 LTS) and I use Python in virtualenv - currently Python
> >3.6.9. I'm using Django and I read that from Django 4.0, a minimal version
> >of Python 3.8 will be required. I would like to know how I use the latest
> >version of Python (3.10 or 3.9) with my production server
[...]
> 
> I would install a totally separate Python from the vendor (ubunut) 
> provided one. This prevents subtle changes to what the OS has been 
> tested against.
> 
> You can built Python from source and install it, for example with a 
> prefix like /usr/local/ptyhon-3.10. That gets you a different version; 
> nothing in the OS stuff will try to use it, but you use it to invoke 
> your Django app.

If you are using mod_wsgi to run your Python apps you will probably have
to rebuild that as well. The mod_wsgi supplied with Ubuntu uses the
system Python and at least a cursory glance through the docs doesn't
reveal a way to change that.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cyclic imports

2021-08-21 Thread Dan Stromberg
On Tue, Aug 17, 2021 at 11:20 AM Chris Angelico  wrote:

> The main advantage of ast.parse() is that it no longer cares about
> code layout, and it won't be fooled by an import statement inside a
> docstring, or anything like that. It's also pretty easy to handle
> multiple variants (note how "import bar, baz" just has two things in
> the names list).
>

I put together something to detect importcycles using the ast module.  It's
at https://stromberg.dnsalias.org/svn/cycles/trunk for now - no pypi, no
homepage.  I've only run  it on some test inputs, nothing substantial.
Usage example is in the Makefile.

Imports are kind of complicated. Am I missing any important cases?

Sample output looks like:
Detected 2 cycles:
('mutual_1', 'mutual_2')
('directory.mutual_a', 'directory.mutual_b', 'directory.mutual_c')

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


Re: Cyclic imports

2021-08-21 Thread Dan Stromberg
On Sat, Aug 21, 2021 at 3:35 PM Dan Stromberg  wrote:

> On Tue, Aug 17, 2021 at 11:20 AM Chris Angelico  wrote:
>
>> The main advantage of ast.parse() is that it no longer cares about
>> code layout, and it won't be fooled by an import statement inside a
>> docstring, or anything like that. It's also pretty easy to handle
>> multiple variants (note how "import bar, baz" just has two things in
>> the names list).
>>
>
> I put together something to detect importcycles using the ast module.
> It's at https://stromberg.dnsalias.org/svn/cycles/trunk for now - no
> pypi, no homepage.  I've only run  it on some test inputs, nothing
> substantial. Usage example is in the Makefile.
>
> Imports are kind of complicated. Am I missing any important cases?
>
> Sample output looks like:
> Detected 2 cycles:
> ('mutual_1', 'mutual_2')
> ('directory.mutual_a', 'directory.mutual_b', 'directory.mutual_c')
>
> Thanks!
>

BTW, "readline0.py" is in the project as a Subversion external reference,
so just going to the URL above in a browser won't show it.

But you can install an svn binary and then:
svn checkout https://stromberg.dnsalias.org/svn/cycles/trunk
That should get all of it, including readline0.py
-- 
https://mail.python.org/mailman/listinfo/python-list