Re: Best way to check if there is internet?

2022-02-22 Thread Abdur-Rahmaan Janhangeer
A front end eng sent me this for how to check for the internet in JS

https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-online

But it also says:

"This attribute is inherently unreliable. A computer can be connected to a
network without having Internet access."

As discussed here but, it would have been nevertheless great to have this
tiny function instead of
nothing

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius


On Mon, Feb 7, 2022 at 1:17 PM Abdur-Rahmaan Janhangeer <
arj.pyt...@gmail.com> wrote:

> Greetings,
>
> Using the standard library or 3rd party libraries, what's the
> best way to check if there is internet? Checking if google.com
> is reachable is good but I wonder if there is a more native,
> protocol-oriented
> way of knowing?
>
> Even this URL recommends checking if a domain is up as a way to check for
> internet connectivity:
>
> https://www.ibm.com/support/pages/checking-network-connectivity-when-using-python-and-ibm-resilient-circuits
>
> Kind Regards,
>
> Abdur-Rahmaan Janhangeer
> about  | blog
> 
> github 
> Mauritius
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Angelico
On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
 wrote:
>
> As discussed here but, it would have been nevertheless great to have this
> tiny function instead of
> nothing
>

Here's a function that determines whether or not you have an internet
connection. It's almost as reliable as some of the other examples
given - I know this, because I tried it ten times, and it gave the
correct result every time!

def has_internet():
return True

Tell me, is it useful to have something that doesn't always give the
right answer, even if it usually does? Is there any value whatsoever
in a lie?

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


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Angelico
On Tue, 22 Feb 2022 at 19:40, Chris Angelico  wrote:
>
> On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > As discussed here but, it would have been nevertheless great to have this
> > tiny function instead of
> > nothing
> >
>
> Here's a function that determines whether or not you have an internet
> connection. It's almost as reliable as some of the other examples
> given - I know this, because I tried it ten times, and it gave the
> correct result every time!
>
> def has_internet():
> return True
>
> Tell me, is it useful to have something that doesn't always give the
> right answer, even if it usually does? Is there any value whatsoever
> in a lie?

Obligatory XKCD:

https://xkcd.com/937/

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


Re: Best way to check if there is internet?

2022-02-22 Thread Abdur-Rahmaan Janhangeer
Thanks for the function but i think having the function
as described won't return True all time

Me: dipping my foot in hot water, yes we can go ...

In the sprit of "practicality beats purity", devs needs a function
XD

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

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


Aw: Re: Best way to check if there is internet?

2022-02-22 Thread Karsten Hilbert
> Is there any value whatsoever in a lie?

Do we _know_ it's a lie ?

Does a lie become a Falsed Truth once it becomes known ?

Karsten

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


One-liner to merge lists?

2022-02-22 Thread Frank Millman

Hi all

I think this should be a simple one-liner, but I cannot figure it out.

I have a dictionary with a number of keys, where each value is a single 
list -


>>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}

I want to combine all values into a single list -

>>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']

I can do this -

>>> a = []
>>> for v in d.values():
...   a.extend(v)
...
>>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']

I can also do this -

>>> from itertools import chain
>>> a = list(chain(*d.values()))
>>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>>>

Is there a simpler way?

Thanks

Frank Millman


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


Re: One-liner to merge lists?

2022-02-22 Thread Chris Angelico
On Tue, 22 Feb 2022 at 20:24, Frank Millman  wrote:
>
> Hi all
>
> I think this should be a simple one-liner, but I cannot figure it out.
>
> I have a dictionary with a number of keys, where each value is a single
> list -
>
>  >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
>
> I want to combine all values into a single list -
>
>  >>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>
> I can do this -
>
>  >>> a = []
>  >>> for v in d.values():
> ...   a.extend(v)
> ...
>  >>> a
> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>
> I can also do this -
>
>  >>> from itertools import chain
>  >>> a = list(chain(*d.values()))
>  >>> a
> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>  >>>
>
> Is there a simpler way?
>

itertools.chain is a good option, as it scales well to arbitrary
numbers of lists (and you're guaranteed to iterate over them all just
once as you construct the list). But if you know that the lists aren't
too large or too numerous, here's another method that works:

>>> sum(d.values(), [])
['aaa', 'bbb', 'ccc', 'fff', 'ggg']

It's simply adding all the lists together, though you have to tell it
that you don't want a numeric summation.

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


Re: One-liner to merge lists?

2022-02-22 Thread Frank Millman

On 2022-02-22 11:30 AM, Chris Angelico wrote:

On Tue, 22 Feb 2022 at 20:24, Frank Millman  wrote:


Hi all

I think this should be a simple one-liner, but I cannot figure it out.

I have a dictionary with a number of keys, where each value is a single
list -

  >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}

I want to combine all values into a single list -

  >>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']

I can do this -

  >>> a = []
  >>> for v in d.values():
...   a.extend(v)
...
  >>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']

I can also do this -

  >>> from itertools import chain
  >>> a = list(chain(*d.values()))
  >>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
  >>>

Is there a simpler way?



itertools.chain is a good option, as it scales well to arbitrary
numbers of lists (and you're guaranteed to iterate over them all just
once as you construct the list). But if you know that the lists aren't
too large or too numerous, here's another method that works:


sum(d.values(), [])

['aaa', 'bbb', 'ccc', 'fff', 'ggg']

It's simply adding all the lists together, though you have to tell it
that you don't want a numeric summation.



Thanks, that is neat.

However, I did see this -

>>> help(sum)
Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of 
numbers


When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values
and may reject non-numeric types.
>>>

So it seems that it is not recommended.

I think I will stick with itertools.chain.

Frank

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


Re: One-liner to merge lists?

2022-02-22 Thread Chris Angelico
On Tue, 22 Feb 2022 at 20:46, Frank Millman  wrote:
>
> On 2022-02-22 11:30 AM, Chris Angelico wrote:
> > On Tue, 22 Feb 2022 at 20:24, Frank Millman  wrote:
> >>
> >> Hi all
> >>
> >> I think this should be a simple one-liner, but I cannot figure it out.
> >>
> >> I have a dictionary with a number of keys, where each value is a single
> >> list -
> >>
> >>   >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
> >>
> >> I want to combine all values into a single list -
> >>
> >>   >>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >>
> >> I can do this -
> >>
> >>   >>> a = []
> >>   >>> for v in d.values():
> >> ...   a.extend(v)
> >> ...
> >>   >>> a
> >> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >>
> >> I can also do this -
> >>
> >>   >>> from itertools import chain
> >>   >>> a = list(chain(*d.values()))
> >>   >>> a
> >> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >>   >>>
> >>
> >> Is there a simpler way?
> >>
> >
> > itertools.chain is a good option, as it scales well to arbitrary
> > numbers of lists (and you're guaranteed to iterate over them all just
> > once as you construct the list). But if you know that the lists aren't
> > too large or too numerous, here's another method that works:
> >
>  sum(d.values(), [])
> > ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >
> > It's simply adding all the lists together, though you have to tell it
> > that you don't want a numeric summation.
> >
>
> Thanks, that is neat.
>
> However, I did see this -
>
>  >>> help(sum)
> Help on built-in function sum in module builtins:
>
> sum(iterable, /, start=0)
>  Return the sum of a 'start' value (default: 0) plus an iterable of
> numbers
>
>  When the iterable is empty, return the start value.
>  This function is intended specifically for use with numeric values
> and may reject non-numeric types.
>  >>>
>
> So it seems that it is not recommended.
>
> I think I will stick with itertools.chain.
>

Yup, itertools.chain is definitely the recommended way to do things.
It's short, efficient, and only slightly unclear. If the clarity is a
problem, you can always wrap it into a function:

def sum_lists(iterable):
return list(chain.from_iterable(iterable))


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


Re: Best way to check if there is internet?

2022-02-22 Thread Antoon Pardon



Op 22/02/2022 om 09:40 schreef Chris Angelico:

On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
  wrote:

As discussed here but, it would have been nevertheless great to have this
tiny function instead of
nothing


Here's a function that determines whether or not you have an internet
connection. It's almost as reliable as some of the other examples
given - I know this, because I tried it ten times, and it gave the
correct result every time!


So, you discovered a way of testing that is not very thorough.


def has_internet():
 return True

Tell me, is it useful to have something that doesn't always give the
right answer, even if it usually does? Is there any value whatsoever
in a lie?


Yes that is useful. Live is full of that kind of situations. We in computerland
are spoiled with the accuracy we can enjoy. It seems even spoiled to the extend
that when offered a solution that is not 100% accurated we consider it a lie.

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


Re: Python

2022-02-22 Thread Mats Wichmann
On 2/21/22 23:17, SASI KANTH REDDY GUJJULA wrote:
> Pip files are not installing after the python 3.10.2 version installing in my 
> devise. Please solve this for me.

Please ask a clearer question.

Can you tell us what "are not installing" means? Are you getting
permission errors?  Are you installing and then unable to import what
you have installed?  Something else?

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


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Angelico
On Wed, 23 Feb 2022 at 01:06, Antoon Pardon  wrote:
>
>
> Op 22/02/2022 om 09:40 schreef Chris Angelico:
> > On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
> >   wrote:
> >> As discussed here but, it would have been nevertheless great to have this
> >> tiny function instead of
> >> nothing
> >>
> > Here's a function that determines whether or not you have an internet
> > connection. It's almost as reliable as some of the other examples
> > given - I know this, because I tried it ten times, and it gave the
> > correct result every time!
>
> So, you discovered a way of testing that is not very thorough.
>
> > def has_internet():
> >  return True
> >
> > Tell me, is it useful to have something that doesn't always give the
> > right answer, even if it usually does? Is there any value whatsoever
> > in a lie?
>
> Yes that is useful. Live is full of that kind of situations. We in 
> computerland
> are spoiled with the accuracy we can enjoy. It seems even spoiled to the 
> extend
> that when offered a solution that is not 100% accurated we consider it a lie.
>

Cool! Then you have a very useful internet-availability testing
function. Have fun!

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


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Green
Chris Angelico  wrote:
> On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > As discussed here but, it would have been nevertheless great to have this
> > tiny function instead of
> > nothing
> >
> 
> Here's a function that determines whether or not you have an internet
> connection. It's almost as reliable as some of the other examples
> given - I know this, because I tried it ten times, and it gave the
> correct result every time!
> 
> def has_internet():
> return True
> 
> Tell me, is it useful to have something that doesn't always give the
> right answer, even if it usually does? Is there any value whatsoever
> in a lie?
> 
That's sort of in the same area as a stopped clock being right more
often than one that runs just a bit slow (or fast).  :-)

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


RE: One-liner to merge lists?

2022-02-22 Thread David Raymond
> Is there a simpler way?

>>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
>>> [a for b in d.values() for a in b]
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>>>

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


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Angelico
On Wed, 23 Feb 2022 at 02:33, Chris Green  wrote:
>
> Chris Angelico  wrote:
> > On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
> >  wrote:
> > >
> > > As discussed here but, it would have been nevertheless great to have this
> > > tiny function instead of
> > > nothing
> > >
> >
> > Here's a function that determines whether or not you have an internet
> > connection. It's almost as reliable as some of the other examples
> > given - I know this, because I tried it ten times, and it gave the
> > correct result every time!
> >
> > def has_internet():
> > return True
> >
> > Tell me, is it useful to have something that doesn't always give the
> > right answer, even if it usually does? Is there any value whatsoever
> > in a lie?
> >
> That's sort of in the same area as a stopped clock being right more
> often than one that runs just a bit slow (or fast).  :-)
>

Oddly enough, if your options for clocks are "right twice a day",
"right once a week", "right once a month", and "never right", the most
useful is probably actually the one that's never right, because if it
ticks at the right rate but is set slightly wrong, it'll always be
exactly that same offset away from correct (which you can adjust for).

But a function to tell you whether you have the internet or not is
like a clock that tells you whether you have time or not. In a
technical (and vacuous) sense, yes, you do... in a practical and
useful sense, who knows?

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


Re: Best way to check if there is internet?

2022-02-22 Thread 2QdxY4RzWzUUiLuE
On 2022-02-09 at 11:15:34 +0400,
Abdur-Rahmaan Janhangeer  wrote:

> I think for me having the internet means ability to request urls

You can always ask.

The real question is what will the response be?  ;-)

This entire exercise is a race condition, just like checking for that a
file exists before deleting it, or that it doesn't exist before creating
it.

If you "have internet" when you check, what assurance do you have that
you will still "have internet" when you actually want to use it?

And if you don't "have internet" when you check, when do you check
again?

I think someone said it way upthread:  don't check, just do whatever you
came to do, and it will work or it will fail (presumably, your program
can tell the difference, regardless of a past snapshot of being able to
retrieve data from an arbitrary URL).

EAFP, anyone?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: One-liner to merge lists?

2022-02-22 Thread Avi Gross via Python-list
Frank,

Given this kind of data: d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}

You seem focused on a one-liner, however ridiculous or inefficient. Does this 
count as a somewhat weird one liner?

comb = [];   _ = [comb.append(v) for v in d.values()]

If you run the code and ignore the returned result, the value in comb is:

print(comb)

[['aaa', 'bbb', 'ccc'], ['fff', 'ggg']]

As discussed the other day here, this is an example of a variant on the 
anonymous function discussion as what is wanted is a side effect. I could omit 
assigning the result to anything and run this code after the previous like this:

[comb.append(v) for v in d.values()]
[None, None]
comb
[['aaa', 'bbb', 'ccc'], ['fff', 'ggg'], ['aaa', 'bbb', 'ccc'], ['fff', 'ggg']]

As shown the list comprehension itself is not returning anything of value and 
need not be assigned to anything. But it then generally is set to autoprint and 
that can be supressed by assigning the value to _ or anything you can ignore.

I am NOT saying this is a good way but that it is sort of a one-liner. There 
are good reasons I do not see people doing things this way!

-Original Message-
From: Frank Millman 
To: python-list@python.org
Sent: Tue, Feb 22, 2022 4:19 am
Subject: One-liner to merge lists?


Hi all



I think this should be a simple one-liner, but I cannot figure it out.



I have a dictionary with a number of keys, where each value is a single 

list -



 >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}



I want to combine all values into a single list -



 >>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']



I can do this -



 >>> a = []

 >>> for v in d.values():

...   a.extend(v)

...

 >>> a

['aaa', 'bbb', 'ccc', 'fff', 'ggg']



I can also do this -



 >>> from itertools import chain

 >>> a = list(chain(*d.values()))

 >>> a

['aaa', 'bbb', 'ccc', 'fff', 'ggg']

 >>>



Is there a simpler way?



Thanks



Frank Millman





-- 

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

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


Re: Best way to check if there is internet?

2022-02-22 Thread Abdur-Rahmaan Janhangeer
Well, nice perspective.

It's a valid consideration, sound theory
but poor practicality according to me.

It you view it like this then between the moment
we press run or enter key on the terminal,
our Python interpreter might get deleted.

We never know, it can happen.

Though it's nice to go in and catch exceptions,
if you have a long operation upcoming it might be nice
to see if your key element is present.

Much like checking if goal posts are present before
starting a football game. You can of course start
the match, pass the ball around and when shooting
you stop the match as you realise that the goal posts
are not around.

Checking by exceptions is what the snippet
i shared does. But we check for the ability to do it
before we start.

Of course, it can change in the seconds that follow. But it's too much pure
logic at work.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-22 Thread Avi Gross via Python-list
As usual, his discussion is wandering a bit.

Yes, some things are not reliable but sometimes a heuristic answer is better 
than none.

Internet connections are beyond flaky and worse the connection you want may be 
blocked for many reasons even if other connections you try are working. Any 
number of routers can lose power or you personally may be blocked by something 
if they deem the place you want unsafe and much more.

In Statistics, you often work not with TRUE and FALSE but with ranges of 
probability and you may deem something as tentatively true if it is 95% or 99% 
likely it did not happen by chance given your sample.

But for a long-running program, checking if the internet is up ONCE is clearly 
not good enough if it runs for months at a time. Checking right before making a 
connection may be a bit much too.

But computers often spend a majority of their time doing things like checking. 
If I am using a GUI and the process is waiting to see if I type some key (or 
even am depressing the Shift key which normally does not generate any symbols 
by itself) and also testing if my mouse is moving or has moved over an area or 
one of several buttons has been clicked, then what may be happening is 
something in a loop that keeps checking and when an event has happened, 
notifies something to continue and perhaps provides the results. Something 
along those lines can happen when say a region of memory/data is being shared 
and one part wants to know if the memory is still locked or has finally been 
written to by another part and can be read and so on.

For something important, it is not enough to check if a file system is mounted 
and available and then write the file and move on. You may want to not only 
test the exit status of the write, or arrange to catch exceptions, but to then 
re-open the file and read in what you wrote and verify that it seems correct. 
In some cases, you get even more paranoid and save some kind of log entry that 
can be used to recreate the scenario if something bad happens and store that in 
another location in case there is a fire or something. 

So the answer is that it is up to you to decide how much effort is warranted. 
Can you just try a connection and react if it fails or must you at least get a 
decent guess that there is an internet connection of some kind up and running 
or must it be ever more bulletproof?

A scenario might be one where you are providing info and can adjust depending 
on various settings. If the user has an internet connection on, you can use 
video and graphics and so on that result in something current. But if they have 
settings suggesting they want to minimize the data they send and receive, the 
same request may result in the browser you invoked showing them just some 
alternative text. You have no way of knowing. Again, if the internet is not up, 
you may just present your own canned text, perhaps dated. Or, you can suggest 
to the user they turn on the internet and try again, or ...

It sounds to me like your main issue is not whether the internet is up right 
now but whether the user has an internet connection. If they run the program on 
a laptop at home or work then they may well be connected, but sitting in their 
car in some parking lot, may well not be.




-Original Message-
From: Abdur-Rahmaan Janhangeer 
To: Python 
Sent: Tue, Feb 22, 2022 12:32 pm
Subject: Re: Best way to check if there is internet?


Well, nice perspective.

It's a valid consideration, sound theory
but poor practicality according to me.

It you view it like this then between the moment
we press run or enter key on the terminal,
our Python interpreter might get deleted.

We never know, it can happen.

Though it's nice to go in and catch exceptions,
if you have a long operation upcoming it might be nice
to see if your key element is present.

Much like checking if goal posts are present before
starting a football game. You can of course start
the match, pass the ball around and when shooting
you stop the match as you realise that the goal posts
are not around.

Checking by exceptions is what the snippet
i shared does. But we check for the ability to do it
before we start.

Of course, it can change in the seconds that follow. But it's too much pure
logic at work.

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

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


Re: Best way to check if there is internet?

2022-02-22 Thread Chris Angelico
On Wed, 23 Feb 2022 at 05:38, Avi Gross via Python-list
 wrote:
> It sounds to me like your main issue is not whether the internet is up right 
> now but whether the user has an internet connection. If they run the program 
> on a laptop at home or work then they may well be connected, but sitting in 
> their car in some parking lot, may well not be.
>

Very seldom, a computer actually has *no* network connection. Much
more often, you have a network connection, but it's not very useful.
For example, you might have full network connectivity... but only on
localhost. Or you might be on a LAN, and fully able to access its
resources, but your uplink to the rest of the internet is down. Or
maybe you're a server inside Facebook, and the rest of the internet is
slowly getting disconnected from you. Do you have an internet
connection? Do you have what you need?

*There is no way* to answer the question of whether you have internet
access, other than to try the exact thing you want to do, and see if
it fails. People who think that there's a binary state of "have
internet" // "do not have internet" have clearly never worked in
networking

(No, I've never accidentally misconfigured a network so that we had
full access to everything *except* the local network. Why do you ask?
Anyway, I fixed it within a few minutes.)

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


Re: Best way to check if there is internet?

2022-02-22 Thread Abdur-Rahmaan Janhangeer
I've got my answers but the 'wandering' a bit
on this thread is at least connected to the topic ^^.

Last question: If we check for wifi or ethernet cable connection?

Wifi sometimes tell of connected but no internet connection.
So it detected that there is or there is no internet ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-22 Thread Barry


> On 22 Feb 2022, at 17:44, Abdur-Rahmaan Janhangeer  
> wrote:
> 
> Well, nice perspective.
> 
> It's a valid consideration, sound theory
> but poor practicality according to me.
> 
> It you view it like this then between the moment
> we press run or enter key on the terminal,
> our Python interpreter might get deleted.

This is under your control usually so you do not need to check for python to be 
installed.
> 
> We never know, it can happen.
> 
> Though it's nice to go in and catch exceptions,
> if you have a long operation upcoming it might be nice
> to see if your key element is present.

You might check for enough disk space before starting to ensure that you can 
write the results, again this is usually under your control.

But you do not control the internet as an end user.
As it is not under your control you have to code to survive its failure modes.
For example email delivery is retried until the MTA gives up or succeeds.

> 
> Much like checking if goal posts are present before
> starting a football game. You can of course start
> the match, pass the ball around and when shooting
> you stop the match as you realise that the goal posts
> are not around.

Check before you start. But you are going to notice people attempting to take 
goal posts away while you play.

> Checking by exceptions is what the snippet
> i shared does. But we check for the ability to do it
> before we start.
> 
> Of course, it can change in the seconds that follow. But it's too much pure
> logic at work.

Since you have to deal with things that you do not control changing after you 
check what is the point in checking? You have to write the code to recover 
anyway.

Barry

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

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


Re: One-liner to merge lists?

2022-02-22 Thread Dan Stromberg
On Tue, Feb 22, 2022 at 7:46 AM David Raymond 
wrote:

> > Is there a simpler way?
>
> >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
> >>> [a for b in d.values() for a in b]
> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >>>
>

I like that way best.

But I'd still:
1) use a little more descriptive identifiers
2) put it in a function with a descriptive name
3) give the function a decent docstring
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to check if there is internet?

2022-02-22 Thread Abdur-Rahmaan Janhangeer
>
> > Since you have to deal with things that you do not control changing
> after you check what is the point in checking? You have to write the code
> to recover anyway.
>


Well foe my usecase, the operation running is not
in months or a really long time. As Avi mentionned above,
that would be pointless.

The upcoming operation is short enough and
though we cannot assume that it will be the case
when we are running the operation, we sensibly
assume it will still be the case.

This is more an extra check to not start an operation than
either relying to check at the start or when query.
It's both.

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


Re: One-liner to merge lists?

2022-02-22 Thread Chris Angelico
On Wed, 23 Feb 2022 at 15:04, Dan Stromberg  wrote:
>
> On Tue, Feb 22, 2022 at 7:46 AM David Raymond 
> wrote:
>
> > > Is there a simpler way?
> >
> > >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
> > >>> [a for b in d.values() for a in b]
> > ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> > >>>
> >
>
> I like that way best.
>
> But I'd still:
> 1) use a little more descriptive identifiers
> 2) put it in a function with a descriptive name
> 3) give the function a decent docstring

If you're going to do that, then you may as well use list(chain()) and
let the work be done in C.

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


Re: One-liner to merge lists?

2022-02-22 Thread Avi Gross via Python-list
I had to think about it too, David.

Renaming it lets it make a bit more sense:

[item for sublist in d.values() for item in sublist]

This spells it out for me that you are taking one list within the main list at 
a time and then taking one item at a time from the list. Since the nesting is 
consistent in this case it makes sense.

Sadly, I could break it easily by just adding a non-list item like this:

d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg'], 3: 666}

And, of course, if you nest it any deeper, it is not completely flattened.

Chris makes the point that using the chain version may be better as it is 
compiled in C. Really? I would prefer a solution that even if slower, is more 
robust. Is there some function already written that will flatten what is given 
to it by checking the type of what is supplied, and based on the type, 
returning any simple objects like numbers or a character string, and converting 
many other composite things to something like a list and after popping off an 
entry, calling itself recursively on the rest until the list is empty? 

This would be a good start:

def flatten2list(object):
gather = []
for item in object:
if isinstance(item, (list, tuple, set)):
gather.extend(flatten2list(item))
else:
gather.append(item)
return gather

Of course I had to call the above as:

flatten2list(d.values())
['aaa', 'bbb', 'ccc', 'fff', 'ggg']

Here is a more complex version of the dictionary:

d = {1: ['aaa', 'bbb', 'ccc'], 2: [['fff', ['ggg']]], 3: 666, 4: set(["a", "e", 
"i", "o", "u"])}
d.values()
dict_values([['aaa', 'bbb', 'ccc'], [['fff', ['ggg']]], 666, {'e', 'u', 'a', 
'o', 'i'}])

flatten2list(d.values())

['aaa', 'bbb', 'ccc', 'fff', 'ggg', 666, 'e', 'u', 'a', 'o', 'i']


The quest for a one-liner sometimes forgets that a typical function call is 
also a one-liner, with the work hidden elsewhere, often in a module written in 
python or mainly in C and so on. I suspect much more detailed functions like 
the above are available with a little search that handle more including just 
about any iterable that terminates.


-Original Message-
From: Dan Stromberg 
To: David Raymond 
Cc: python-list@python.org 
Sent: Tue, Feb 22, 2022 11:02 pm
Subject: Re: One-liner to merge lists?


On Tue, Feb 22, 2022 at 7:46 AM David Raymond 
wrote:

> > Is there a simpler way?
>
> >>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
> >>> [a for b in d.values() for a in b]
> ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
> >>>
>

I like that way best.

But I'd still:
1) use a little more descriptive identifiers
2) put it in a function with a descriptive name
3) give the function a decent docstring

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

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


Re: One-liner to merge lists?

2022-02-22 Thread Frank Millman

On 2022-02-22 5:45 PM, David Raymond wrote:

Is there a simpler way?



d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
[a for b in d.values() for a in b]

['aaa', 'bbb', 'ccc', 'fff', 'ggg']






Now that's what I was looking for.

I am not saying that I will use it, but as an academic exercise I felt 
sure that there had to be a one-liner in pure python.


I had forgotten about nested comprehensions. Thanks for the reminder.

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


Re: Best way to check if there is internet?

2022-02-22 Thread Dennis Lee Bieber
On Wed, 23 Feb 2022 07:08:27 +0300, Abdur-Rahmaan Janhangeer
 declaimed the following:


>The upcoming operation is short enough and
>though we cannot assume that it will be the case
>when we are running the operation, we sensibly
>assume it will still be the case.
>
>This is more an extra check to not start an operation than
>either relying to check at the start or when query.
>It's both.

So you have a success/fail tree of...

1   attempt a connection to some IP/port
1a  success -- attempt connection for desired operation
1a1 success -- operation completes
1a2 failure -- report that the operation can not be 
completed
1b  failure -- report that you will not attempt the operation
because your test connection to some site failed
EVEN IF THE OPERATION COULD SUCCEED
(if the problem is with "some IP/port", not
the rest of the network)

vs

1   attempt connection for desired operation
1a  success -- operation completes
1b  failure -- report that the operation cannot be completed

Personally the second tree is simpler -- AND NEEDS TO BE PART OF THE
OTHER TREE ANYWAY! [short tree 1/1a/1b are long tree 1a/1a1/1a2] The only
way the longer tree makes sense is by making step 1 a connection to the
IP/port requested in the desired operation -- and if that succeeds you've
basically performed the shorter tree.

>
>>


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list