Re: All permutations from 2 lists

2022-03-02 Thread Peter Otten

On 02/03/2022 01:32, Rob Cliffe via Python-list wrote:


itertools.product returns an iterator (or iterable, I'm not sure of the
correct technical term).


There's a simple test:

iter(x) is x --> True  # iterator
iter(x) is x --> False  # iterable


So:

>>> from itertools import product
>>> p = product("ab", [1, 2])
>>> iter(p) is p  # iterator
True
>>> items = [1, 2]  # iterable
>>> iter(items) is items
False

Another interesting property of (finite) iterators/iterables

list(iterable) == list(iterable) --> Generally True, but not guaranteed.

a = list(iterator)  # whatever
b = list(iterator)  # [] (*)

(*) Kill the coder if that doesn't hold ;)
--
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Chris Angelico
On Wed, 2 Mar 2022 at 19:34, Peter Otten <__pete...@web.de> wrote:
>
> On 02/03/2022 01:32, Rob Cliffe via Python-list wrote:
>
> > itertools.product returns an iterator (or iterable, I'm not sure of the
> > correct technical term).
>
> There's a simple test:
>
> iter(x) is x --> True  # iterator
> iter(x) is x --> False  # iterable

iter(x) --> TypeError # not iterable
iter(x) is x --> True # iterator
iter(x) is x --> False # iterable but not iterator

All (non-broken) iterators are themselves iterable. By and large,
itertools is full of classes which are their own iterators, so for
instance itertools.product(...) is an iterator, not just an iterable.

> So:
>
>  >>> from itertools import product
>  >>> p = product("ab", [1, 2])
>  >>> iter(p) is p  # iterator
> True
>  >>> items = [1, 2]  # iterable
>  >>> iter(items) is items
> False

This is important, because:

>>> items = [1,2,3,4,5,6]
>>> i1 = iter(items); print(next(i1), next(i1))
1 2
>>> i2 = iter(items); print(next(i2), next(i2), next(i2))
1 2 3
>>> print(next(i2), next(i1))
4 3

Every time you iterate over a list, you get the same items, but if you
partially pump one of those iterators, it needs to remember where it
was up to. In contrast, itertools.product is its own iterator, so you
can't restart it.

> Another interesting property of (finite) iterators/iterables
>
> list(iterable) == list(iterable) --> Generally True, but not guaranteed.

There's been discussion now and then about giving a name to that
property. I'm personally in favour of "reiterable", as in, "you can
iterate over this more than once and get the same content". (Of
course, mutating the list in between would mean you get different
content, but it doesn't remember the position of the iterator.)

> a = list(iterator)  # whatever
> b = list(iterator)  # [] (*)
>
> (*) Kill the coder if that doesn't hold ;)

I would call that a broken iterator. It used to be possible to have a
generator function that could be broken in weird ways like that, but
the worst of them now raise RuntimeError. Similarly, Python won't stop
you from doing this, but it is absolutely horrid code, and something
you really don't want to have to debug:

>>> class BrokenIter:
... def __next__(self):
... return "spam"
...
>>> class Broken:
... def __iter__(self):
... return BrokenIter()
...

All it takes is forgetting the "def __iter__(self): return self" in
the iterator, and you create something that LOOKS usable, but
occasionally breaks.

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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Tue, Mar 1, 2022 at 7:21 PM <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2022-03-01 at 19:12:10 -0500,
> Larry Martell  wrote:
>
> > If I have 2 lists, e.g.:
> >
> > os = ["Linux","Windows"]
> > region = ["us-east-1", "us-east-2"]
> >
> > How can I get a list of tuples with all possible permutations?
> >
> > So for this example I'd want:
> >
> > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > "us-east-1"), "Windows", "us-east-2')]
> >
> > The lists can be different lengths or can be 0 length. Tried a few
> > different things with itertools but have not got just what I need.
>
> [(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe  wrote:
>
> I would not use `os` as an identifier, as it is the name of an important
> built-in module.

This is part of a much larger data structure, I created a simplified
example. It is not actually called os.

> I think itertools.product is what you need.
> Example program:
>
> import itertools
> opsys = ["Linux","Windows"]
> region = ["us-east-1", "us-east-2"]
> print(list(itertools.product(opsys, region)))

This does not work if region = []. I wrote in question that either
list could be empty.

> Output:
>
> [('Linux', 'us-east-1'), ('Linux', 'us-east-2'), ('Windows',
> 'us-east-1'), ('Windows', 'us-east-2')]
>
> itertools.product returns an iterator (or iterable, I'm not sure of the
> correct technical term).
> If you only want to use the result once you can write e.g.
>
>  for ops, reg in itertools.product(opsys, region):
>  etc.
>
> If you need it more than once, you can convert it to a list (or tuple),
> as above.
> Best wishes
> Rob Cliffe
>
> On 02/03/2022 00:12, Larry Martell wrote:
> > If I have 2 lists, e.g.:
> >
> > os = ["Linux","Windows"]
> > region = ["us-east-1", "us-east-2"]
> >
> > How can I get a list of tuples with all possible permutations?
> >
> > So for this example I'd want:
> >
> > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > "us-east-1"), "Windows", "us-east-2')]
> >
> > The lists can be different lengths or can be 0 length. Tried a few
> > different things with itertools but have not got just what I need.
> >
> > TIA!
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon



Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martell  wrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.


What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

--
Antoon Pardon.

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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:
>
>
> Op 2/03/2022 om 14:27 schreef Larry Martell:
> > On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  wrote:
> >> On 2022-03-01 at 19:12:10 -0500,
> >> Larry Martell  wrote:
> >>
> >>> If I have 2 lists, e.g.:
> >>>
> >>> os = ["Linux","Windows"]
> >>> region = ["us-east-1", "us-east-2"]
> >>>
> >>> How can I get a list of tuples with all possible permutations?
> >>>
> >>> So for this example I'd want:
> >>>
> >>> [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> >>> "us-east-1"), "Windows", "us-east-2')]
> >>>
> >>> The lists can be different lengths or can be 0 length. Tried a few
> >>> different things with itertools but have not got just what I need.
> >> [(o, r) for o in os for r in region]
> > This does not work if region = []. I wrote in my question that either
> > list could be empty.
>
> What do you mean it doesn't work? The result seems to be an empty list,
> which IMO is a perfectly valid result.
>
> All possible permutations over two collections where one collection is
> empty, should IMO give you an empty collection.

If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Joel Goldstick
On Wed, Mar 2, 2022 at 8:46 AM Larry Martell  wrote:
>
> On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:
> >
> >
> > Op 2/03/2022 om 14:27 schreef Larry Martell:
> > > On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  wrote:
> > >> On 2022-03-01 at 19:12:10 -0500,
> > >> Larry Martell  wrote:
> > >>
> > >>> If I have 2 lists, e.g.:
> > >>>
> > >>> os = ["Linux","Windows"]
> > >>> region = ["us-east-1", "us-east-2"]
> > >>>
> > >>> How can I get a list of tuples with all possible permutations?
> > >>>
> > >>> So for this example I'd want:
> > >>>
> > >>> [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > >>> "us-east-1"), "Windows", "us-east-2')]
> > >>>
> > >>> The lists can be different lengths or can be 0 length. Tried a few
> > >>> different things with itertools but have not got just what I need.
> > >> [(o, r) for o in os for r in region]
> > > This does not work if region = []. I wrote in my question that either
> > > list could be empty.
> >
> > What do you mean it doesn't work? The result seems to be an empty list,
> > which IMO is a perfectly valid result.
> >
> > All possible permutations over two collections where one collection is
> > empty, should IMO give you an empty collection.
>
> If one list is empty I want just the other list. What I am doing is
> building a list to pass to a mongodb query. If region is empty then I
> want to query for just the items in the os list. I guess I can test
> for the lists being empty, but I'd like a solution that handles that
> as down the road there could be more than just 2 lists.
> --
> https://mail.python.org/mailman/listinfo/python-list

Does this help you out:

>>> [(o,r) for o in opsys for r in region or "x"]
[('Linux', 'x'), ('Window', 'x')]


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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 8:54 AM Joel Goldstick  wrote:
>
> On Wed, Mar 2, 2022 at 8:46 AM Larry Martell  wrote:
> >
> > On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:
> > >
> > >
> > > Op 2/03/2022 om 14:27 schreef Larry Martell:
> > > > On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  
> > > > wrote:
> > > >> On 2022-03-01 at 19:12:10 -0500,
> > > >> Larry Martell  wrote:
> > > >>
> > > >>> If I have 2 lists, e.g.:
> > > >>>
> > > >>> os = ["Linux","Windows"]
> > > >>> region = ["us-east-1", "us-east-2"]
> > > >>>
> > > >>> How can I get a list of tuples with all possible permutations?
> > > >>>
> > > >>> So for this example I'd want:
> > > >>>
> > > >>> [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > > >>> "us-east-1"), "Windows", "us-east-2')]
> > > >>>
> > > >>> The lists can be different lengths or can be 0 length. Tried a few
> > > >>> different things with itertools but have not got just what I need.
> > > >> [(o, r) for o in os for r in region]
> > > > This does not work if region = []. I wrote in my question that either
> > > > list could be empty.
> > >
> > > What do you mean it doesn't work? The result seems to be an empty list,
> > > which IMO is a perfectly valid result.
> > >
> > > All possible permutations over two collections where one collection is
> > > empty, should IMO give you an empty collection.
> >
> > If one list is empty I want just the other list. What I am doing is
> > building a list to pass to a mongodb query. If region is empty then I
> > want to query for just the items in the os list. I guess I can test
> > for the lists being empty, but I'd like a solution that handles that
> > as down the road there could be more than just 2 lists.
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> Does this help you out:
>
> >>> [(o,r) for o in opsys for r in region or "x"]
> [('Linux', 'x'), ('Window', 'x')]

That doesn't work if opsys = [] - either list could be empty.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Joel Goldstick
On Wed, Mar 2, 2022 at 9:01 AM Larry Martell  wrote:
>
> On Wed, Mar 2, 2022 at 8:54 AM Joel Goldstick  
> wrote:
> >
> > On Wed, Mar 2, 2022 at 8:46 AM Larry Martell  
> > wrote:
> > >
> > > On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:
> > > >
> > > >
> > > > Op 2/03/2022 om 14:27 schreef Larry Martell:
> > > > > On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  
> > > > > wrote:
> > > > >> On 2022-03-01 at 19:12:10 -0500,
> > > > >> Larry Martell  wrote:
> > > > >>
> > > > >>> If I have 2 lists, e.g.:
> > > > >>>
> > > > >>> os = ["Linux","Windows"]
> > > > >>> region = ["us-east-1", "us-east-2"]
> > > > >>>
> > > > >>> How can I get a list of tuples with all possible permutations?
> > > > >>>
> > > > >>> So for this example I'd want:
> > > > >>>
> > > > >>> [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > > > >>> "us-east-1"), "Windows", "us-east-2')]
> > > > >>>
> > > > >>> The lists can be different lengths or can be 0 length. Tried a few
> > > > >>> different things with itertools but have not got just what I need.
> > > > >> [(o, r) for o in os for r in region]
> > > > > This does not work if region = []. I wrote in my question that either
> > > > > list could be empty.
> > > >
> > > > What do you mean it doesn't work? The result seems to be an empty list,
> > > > which IMO is a perfectly valid result.
> > > >
> > > > All possible permutations over two collections where one collection is
> > > > empty, should IMO give you an empty collection.
> > >
> > > If one list is empty I want just the other list. What I am doing is
> > > building a list to pass to a mongodb query. If region is empty then I
> > > want to query for just the items in the os list. I guess I can test
> > > for the lists being empty, but I'd like a solution that handles that
> > > as down the road there could be more than just 2 lists.
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> >
> > Does this help you out:
> >
> > >>> [(o,r) for o in opsys for r in region or "x"]
> > [('Linux', 'x'), ('Window', 'x')]
>
> That doesn't work if opsys = [] - either list could be empty.

So, maybe what you need to do is determine the length of the longest
list, and pad the other lists as required before producing the tuples?

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


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon

Op 2/03/2022 om 14:44 schreef Larry Martell:

On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:


Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>   wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martell   wrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.

What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.


How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

Antoon Pardon.

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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 9:10 AM Antoon Pardon  wrote:
>
> Op 2/03/2022 om 14:44 schreef Larry Martell:
> > On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:
> >>
> >> Op 2/03/2022 om 14:27 schreef Larry Martell:
> >>> On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>   
> >>> wrote:
>  On 2022-03-01 at 19:12:10 -0500,
>  Larry Martell   wrote:
> 
> > If I have 2 lists, e.g.:
> >
> > os = ["Linux","Windows"]
> > region = ["us-east-1", "us-east-2"]
> >
> > How can I get a list of tuples with all possible permutations?
> >
> > So for this example I'd want:
> >
> > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> > "us-east-1"), "Windows", "us-east-2')]
> >
> > The lists can be different lengths or can be 0 length. Tried a few
> > different things with itertools but have not got just what I need.
>  [(o, r) for o in os for r in region]
> >>> This does not work if region = []. I wrote in my question that either
> >>> list could be empty.
> >> What do you mean it doesn't work? The result seems to be an empty list,
> >> which IMO is a perfectly valid result.
> >>
> >> All possible permutations over two collections where one collection is
> >> empty, should IMO give you an empty collection.
> > If one list is empty I want just the other list. What I am doing is
> > building a list to pass to a mongodb query. If region is empty then I
> > want to query for just the items in the os list. I guess I can test
> > for the lists being empty, but I'd like a solution that handles that
> > as down the road there could be more than just 2 lists.
>
> How about the following: Keep a list of your lists you want to permute over.
> Like the following:
>
> permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]
>
> permutation = itertools.product(*permutation_elements)
>
> If you don't include the empty list, you will get more or less what you
> seem to want.

But I need to deal with that case.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon




Op 2/03/2022 om 15:29 schreef Larry Martell:

On Wed, Mar 2, 2022 at 9:10 AM Antoon Pardon  wrote:

Op 2/03/2022 om 14:44 schreef Larry Martell:

On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon   wrote:

Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martellwrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.

What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.

How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

But I need to deal with that case.


What does that mean? How does using the above method to produce the permutations
you want, prevent you from dealing with an empty list however you want when you
encounter them? Just don't add them to the permutation_elements.

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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  wrote:
>
>
>
> Op 2/03/2022 om 15:29 schreef Larry Martell:
> > On Wed, Mar 2, 2022 at 9:10 AM Antoon Pardon  wrote:
> >> Op 2/03/2022 om 14:44 schreef Larry Martell:
> >>> On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon   
> >>> wrote:
>  Op 2/03/2022 om 14:27 schreef Larry Martell:
> > On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>
> > wrote:
> >> On 2022-03-01 at 19:12:10 -0500,
> >> Larry Martellwrote:
> >>
> >>> If I have 2 lists, e.g.:
> >>>
> >>> os = ["Linux","Windows"]
> >>> region = ["us-east-1", "us-east-2"]
> >>>
> >>> How can I get a list of tuples with all possible permutations?
> >>>
> >>> So for this example I'd want:
> >>>
> >>> [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
> >>> "us-east-1"), "Windows", "us-east-2')]
> >>>
> >>> The lists can be different lengths or can be 0 length. Tried a few
> >>> different things with itertools but have not got just what I need.
> >> [(o, r) for o in os for r in region]
> > This does not work if region = []. I wrote in my question that either
> > list could be empty.
>  What do you mean it doesn't work? The result seems to be an empty list,
>  which IMO is a perfectly valid result.
> 
>  All possible permutations over two collections where one collection is
>  empty, should IMO give you an empty collection.
> >>> If one list is empty I want just the other list. What I am doing is
> >>> building a list to pass to a mongodb query. If region is empty then I
> >>> want to query for just the items in the os list. I guess I can test
> >>> for the lists being empty, but I'd like a solution that handles that
> >>> as down the road there could be more than just 2 lists.
> >> How about the following: Keep a list of your lists you want to permute 
> >> over.
> >> Like the following:
> >>
> >> permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]
> >>
> >> permutation = itertools.product(*permutation_elements)
> >>
> >> If you don't include the empty list, you will get more or less what you
> >> seem to want.
> > But I need to deal with that case.
>
> What does that mean? How does using the above method to produce the 
> permutations
> you want, prevent you from dealing with an empty list however you want when 
> you
> encounter them? Just don't add them to the permutation_elements.

I need to know what items are in which position. If sometimes the
regions are in one index and sometimes in another will not work for
me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon




Op 2/03/2022 om 15:58 schreef Larry Martell:

On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  wrote:



If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.

How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

But I need to deal with that case.

What does that mean? How does using the above method to produce the permutations
you want, prevent you from dealing with an empty list however you want when you
encounter them? Just don't add them to the permutation_elements.

I need to know what items are in which position. If sometimes the
regions are in one index and sometimes in another will not work for
me.


I am starting to suspect you didn't think this through. What you are telling 
here
contradicts what you told earlier that if either list was empty, you just wanted
the other list. Because then you wouldn't know what items were in that list.

The only solution I can see now is that if a list is empty, you either add 
[None] or
[""] to the permutation_elements (whatever suits you better) and then use
itertools.product
--
https://mail.python.org/mailman/listinfo/python-list


lxml empty versus self closed tag

2022-03-02 Thread Robin Becker

I'm using lxml.etree.XMLParser and would like to distinguish



from



I seem to have e.getchildren()==[] and e.text==None for both cases. Is there a 
way to get the first to have e.text==''
--
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 10:26 AM Antoon Pardon  wrote:
>
>
>
> Op 2/03/2022 om 15:58 schreef Larry Martell:
> > On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  wrote:
> >>
> > If one list is empty I want just the other list. What I am doing is
> > building a list to pass to a mongodb query. If region is empty then I
> > want to query for just the items in the os list. I guess I can test
> > for the lists being empty, but I'd like a solution that handles that
> > as down the road there could be more than just 2 lists.
>  How about the following: Keep a list of your lists you want to permute 
>  over.
>  Like the following:
> 
>  permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]
> 
>  permutation = itertools.product(*permutation_elements)
> 
>  If you don't include the empty list, you will get more or less what you
>  seem to want.
> >>> But I need to deal with that case.
> >> What does that mean? How does using the above method to produce the 
> >> permutations
> >> you want, prevent you from dealing with an empty list however you want 
> >> when you
> >> encounter them? Just don't add them to the permutation_elements.
> > I need to know what items are in which position. If sometimes the
> > regions are in one index and sometimes in another will not work for
> > me.
>
> I am starting to suspect you didn't think this through. What you are telling 
> here
> contradicts what you told earlier that if either list was empty, you just 
> wanted
> the other list. Because then you wouldn't know what items were in that list.
>
> The only solution I can see now is that if a list is empty, you either add 
> [None] or
> [""] to the permutation_elements (whatever suits you better) and then use
> itertools.product

I found a way to pass this directly into the query:

def query_lfixer(query):
for k, v in query.items():
if type(v)==list:
query[k] = {"$in": v}
return query

self._db_conn[collection_name].find(query_lfixer(query))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Timezone for datetime.date objects

2022-03-02 Thread Peter J. Holzer
On 2022-02-28 23:28:23 +0100, Morten W. Petersen wrote:
> Well, let's say I specify the datetime 2022-02-22 02:02 (AM). I think
> everyone could agree that it also means 2022-02-22 02:02:00:00, to
> 2022-02-22 02:02:59:59.

I disagree. The datetime 2022-02-22 02:02 specifies a point in time, not
a time period. It means 2022-02-22 02:02:00.0.

In reality, a point in time may be fuzzy. "The train departs at 02:02"
definitely doesn't mean that the train will depart exactly at 02:02:00,
but it also doesn't mean that it will depart between 02:02 and 02:03.
Rather it's a smooth probability distribution starting a bit before
02:02:00 (a train should never leave early, but sometimes clocks are
wrong or somebody doesn't pay attention) a peak shortly after 02:02:00
and a very long tail.

> And I think the same applies for a date.

Depends on the context, but without s specific context (like business
days) I would agree. A day *is* a time period with a beginning and an
end. 

hp

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


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


Re: All permutations from 2 lists

2022-03-02 Thread Om Joshi
For completeness, the itertools solution (which returns an iterator) is

>>> os = ["Linux","Windows"]
>>> region = ["us-east-1", "us-east-2"]
>>> import itertools
>>> itertools.product(os,region)

>>> list(itertools.product(os,region))
[('Linux', 'us-east-1'), ('Linux', 'us-east-2'), ('Windows', 'us-east-1'), 
('Windows', 'us-east-2')]

There are probably use cases where you want the iterator and others where you 
want the list comprehension. I think the itertools looks nice and it's easier 
to generalize it to N=3,4,5,... lists than writing out `for a in list1 for b in 
list2 for c in list3` etc -- also, you can do things like 
itertools.product(*list_of_lists) to get the product if you have a variable 
number of lists. Not exactly sure about speed comparison but my instinct is 
that a double/triple list comprehension is going to be slower than letting 
itertools do its magic.


  On Tue, 01 Mar 2022 18:20:16 -0600  <2qdxy4rzwzuui...@potatochowder.com> 
wrote 
 > On 2022-03-01 at 19:12:10 -0500,
 > Larry Martell  wrote:
 > 
 > > If I have 2 lists, e.g.:
 > > 
 > > os = ["Linux","Windows"]
 > > region = ["us-east-1", "us-east-2"]
 > > 
 > > How can I get a list of tuples with all possible permutations?
 > > 
 > > So for this example I'd want:
 > > 
 > > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
 > > "us-east-1"), "Windows", "us-east-2')]
 > > 
 > > The lists can be different lengths or can be 0 length. Tried a few
 > > different things with itertools but have not got just what I need.
 > 
 > [(o, r) for o in os for r in region]
 > 
 > Feel free to import itertools, but it's not really necessary.  ;-)
 > -- 
 > https://mail.python.org/mailman/listinfo/python-list
 > 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: SQLAlchemy: JSON vs. PickleType vs. raw string for serialised data

2022-03-02 Thread Loris Bennett
Dennis Lee Bieber  writes:

> On Tue, 01 Mar 2022 08:35:05 +0100, Loris Bennett
>  declaimed the following:
>
>>Thanks for the various suggestions.  The data I need to store is just a
>>dict with maybe 3 or 4 keys and short string values probably of less
>>than 32 characters each per event.  The traffic on the DB is going to be
>>very low, creating maybe a dozen events a day, mainly triggered via a
>>command-line interface, although I will probably set up one or two cron
>>jobs, each of which might generate another 0 to maybe 5 records a day.
>>
>>I could go for JSON (or rather LONGSTRING, as JSON is just an alias for
>>LONGSTRING, but JSON is not available on the version of MariaDB I am
>>using).  However, that seems like overkill, since I am never going to
>>have to store anything near 4 GB in the field.  So I should probably in
>>fact just use say VARCHAR(255).
>>
>>WDYT?
>>
>
>   Having taken a few on-line short courses on database normalization and
> SQL during my first lay-off, my view would be to normalize everything
> first... Which, in your description, means putting that dictionary into a
> separate table of the form (I also tend to define an autoincrement primary
> key for all tables):
>
> DICTDATA(*ID*, _eventID_, dictKey, dictValue)
>
> where * delimits primary key, _ delimits foreign key to parent (event?)
> record.

Ah, yes, you are right. That would indeed be the correct way to do it.
I'll look into that.  Up to now I was thinking I would only ever want to
read out the dict in its entirety, but that's probably not correct.

> Caveat: While I have a book on SQLAlchemy, I confess it makes no sense to
> me -- I can code SQL joins faster than figuring out how to represent the
> same join in SQLAlchemy.

I currently can't code SQL joins fast anyway, so although doing it in
SQLAlchemy is might be relatively slower, absolutely there's maybe
not going to be much difference :-)

Cheers,

Loris

-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Om Joshi
I sent this 17hrs ago but I guess it just went through. Apologies for the 
redundant comments...  On Tue, 01 Mar 2022 18:57:02 -0600  
om+pyt...@omajoshi.com  wrote For completeness, the itertools solution 
(which returns an iterator) is

>>> os = ["Linux","Windows"]
>>> region = ["us-east-1", "us-east-2"]
>>> import itertools
>>> itertools.product(os,region)

>>> list(itertools.product(os,region))
[('Linux', 'us-east-1'), ('Linux', 'us-east-2'), ('Windows', 'us-east-1'), 
('Windows', 'us-east-2')]

There are probably use cases where you want the iterator and others where you 
want the list comprehension. I think the itertools looks nice and it's easier 
to generalize it to N=3,4,5,... lists than writing out `for a in list1 for b in 
list2 for c in list3` etc -- also, you can do things like 
itertools.product(*list_of_lists) to get the product if you have a variable 
number of lists. Not exactly sure about speed comparison but my instinct is 
that a double/triple list comprehension is going to be slower than letting 
itertools do its magic.


  On Tue, 01 Mar 2022 18:20:16 -0600  <2qdxy4rzwzuui...@potatochowder.com> 
wrote 
 > On 2022-03-01 at 19:12:10 -0500,
 > Larry Martell  wrote:
 > 
 > > If I have 2 lists, e.g.:
 > > 
 > > os = ["Linux","Windows"]
 > > region = ["us-east-1", "us-east-2"]
 > > 
 > > How can I get a list of tuples with all possible permutations?
 > > 
 > > So for this example I'd want:
 > > 
 > > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
 > > "us-east-1"), "Windows", "us-east-2')]
 > > 
 > > The lists can be different lengths or can be 0 length. Tried a few
 > > different things with itertools but have not got just what I need.
 > 
 > [(o, r) for o in os for r in region]
 > 
 > Feel free to import itertools, but it's not really necessary.  ;-)
 > -- 
 > https://mail.python.org/mailman/listinfo/python-list
 > 
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Om Joshi
Something like this?itertools.product(x or ("",) for x in perm_elems)Out of 
curiousity, how might one adapt this if x is not a list but an iterator, 
without doing `itertools.product(list(x) or ("",) for x in perm_elems)`?   
On Wed, 02 Mar 2022 09:25:42 -0600  antoon.par...@vub.be  wrote 

Op 2/03/2022 om 15:58 schreef Larry Martell:
> On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  wrote:
>>
> If one list is empty I want just the other list. What I am doing is
> building a list to pass to a mongodb query. If region is empty then I
> want to query for just the items in the os list. I guess I can test
> for the lists being empty, but I'd like a solution that handles that
> as down the road there could be more than just 2 lists.
 How about the following: Keep a list of your lists you want to permute 
 over.
 Like the following:

 permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

 permutation = itertools.product(*permutation_elements)

 If you don't include the empty list, you will get more or less what you
 seem to want.
>>> But I need to deal with that case.
>> What does that mean? How does using the above method to produce the 
>> permutations
>> you want, prevent you from dealing with an empty list however you want when 
>> you
>> encounter them? Just don't add them to the permutation_elements.
> I need to know what items are in which position. If sometimes the
> regions are in one index and sometimes in another will not work for
> me.

I am starting to suspect you didn't think this through. What you are telling 
here
contradicts what you told earlier that if either list was empty, you just wanted
the other list. Because then you wouldn't know what items were in that list.

The only solution I can see now is that if a list is empty, you either add 
[None] or
[""] to the permutation_elements (whatever suits you better) and then use
itertools.product
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: lxml empty versus self closed tag

2022-03-02 Thread Dieter Maurer
Robin Becker wrote at 2022-3-2 15:32 +:
>I'm using lxml.etree.XMLParser and would like to distinguish
>
>
>
>from
>
>
>
>I seem to have e.getchildren()==[] and e.text==None for both cases. Is there a 
>way to get the first to have e.text==''

I do not think so (at least not without a DTD):
`' is just a shorthand notation for '' and
the difference has no influence on the DOM.

Note that `lxml` is just a Python binding for `libxml2`.
All the parsing is done by this library.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Cameron Simpson
On 02Mar2022 08:29, Larry Martell  wrote:
>On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe  wrote:
>> I think itertools.product is what you need.
>> Example program:
>>
>> import itertools
>> opsys = ["Linux","Windows"]
>> region = ["us-east-1", "us-east-2"]
>> print(list(itertools.product(opsys, region)))
>
>This does not work if region = []. I wrote in question that either
>list could be empty.

What do you want to get if a list is empty? You haven't said. My 
personal expectation would be an empty result.

Alternatively, if you expect an empty list to imply some single default 
the the experession:

the_list or (the_default,)

might be of use.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 5:00 PM Cameron Simpson  wrote:
>
> On 02Mar2022 08:29, Larry Martell  wrote:
> >On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe  wrote:
> >> I think itertools.product is what you need.
> >> Example program:
> >>
> >> import itertools
> >> opsys = ["Linux","Windows"]
> >> region = ["us-east-1", "us-east-2"]
> >> print(list(itertools.product(opsys, region)))
> >
> >This does not work if region = []. I wrote in question that either
> >list could be empty.
>
> What do you want to get if a list is empty? You haven't said. My
> personal expectation would be an empty result.
>
> Alternatively, if you expect an empty list to imply some single default
> the the experession:
>
> the_list or (the_default,)
>
> might be of use.

I've solved the issue.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Joel Goldstick
On Wed, Mar 2, 2022 at 5:07 PM Larry Martell  wrote:
>
> On Wed, Mar 2, 2022 at 5:00 PM Cameron Simpson  wrote:
> >
> > On 02Mar2022 08:29, Larry Martell  wrote:
> > >On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe  
> > >wrote:
> > >> I think itertools.product is what you need.
> > >> Example program:
> > >>
> > >> import itertools
> > >> opsys = ["Linux","Windows"]
> > >> region = ["us-east-1", "us-east-2"]
> > >> print(list(itertools.product(opsys, region)))
> > >
> > >This does not work if region = []. I wrote in question that either
> > >list could be empty.
> >
> > What do you want to get if a list is empty? You haven't said. My
> > personal expectation would be an empty result.
> >
> > Alternatively, if you expect an empty list to imply some single default
> > the the experession:
> >
> > the_list or (the_default,)
> >
> > might be of use.
>
> I've solved the issue.
> --
> https://mail.python.org/mailman/listinfo/python-list

Would you be so kind as to show the results of your solution?

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


Re: All permutations from 2 lists

2022-03-02 Thread Larry Martell
On Wed, Mar 2, 2022 at 5:31 PM Joel Goldstick  wrote:
>
> On Wed, Mar 2, 2022 at 5:07 PM Larry Martell  wrote:
> >
> > On Wed, Mar 2, 2022 at 5:00 PM Cameron Simpson  wrote:
> > >
> > > On 02Mar2022 08:29, Larry Martell  wrote:
> > > >On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe  
> > > >wrote:
> > > >> I think itertools.product is what you need.
> > > >> Example program:
> > > >>
> > > >> import itertools
> > > >> opsys = ["Linux","Windows"]
> > > >> region = ["us-east-1", "us-east-2"]
> > > >> print(list(itertools.product(opsys, region)))
> > > >
> > > >This does not work if region = []. I wrote in question that either
> > > >list could be empty.
> > >
> > > What do you want to get if a list is empty? You haven't said. My
> > > personal expectation would be an empty result.
> > >
> > > Alternatively, if you expect an empty list to imply some single default
> > > the the experession:
> > >
> > > the_list or (the_default,)
> > >
> > > might be of use.
> >
> > I've solved the issue.
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> Would you be so kind as to show the results of your solution?

I posted it at 10:49am Eastern time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread gene heskett
On Wednesday, 2 March 2022 17:46:49 EST Larry Martell wrote:
> On Wed, Mar 2, 2022 at 5:31 PM Joel Goldstick 
 wrote:
> > On Wed, Mar 2, 2022 at 5:07 PM Larry Martell 
 wrote:
> > > On Wed, Mar 2, 2022 at 5:00 PM Cameron Simpson  
wrote:
> > > > On 02Mar2022 08:29, Larry Martell  
wrote:
> > > > >On Tue, Mar 1, 2022 at 7:32 PM Rob Cliffe 
 wrote:
> > > > >> I think itertools.product is what you need.
> > > > >> Example program:
> > > > >> 
> > > > >> import itertools
> > > > >> opsys = ["Linux","Windows"]
> > > > >> region = ["us-east-1", "us-east-2"]
> > > > >> print(list(itertools.product(opsys, region)))
> > > > >
> > > > >This does not work if region = []. I wrote in question that
> > > > >either
> > > > >list could be empty.
> > > > 
> > > > What do you want to get if a list is empty? You haven't said. My
> > > > personal expectation would be an empty result.
> > > > 
> > > > Alternatively, if you expect an empty list to imply some single
> > > > default> > > 
> > > > the the experession:
> > > > the_list or (the_default,)
> > > > 
> > > > might be of use.
> > > 
> > > I've solved the issue.
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > 
> > Would you be so kind as to show the results of your solution?
> 
> I posted it at 10:49am Eastern time.

You may want to repost it Larry, at 9pm eastern it has not arrived here.

Cheers, Gene Heskett.
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
 - Louis D. Brandeis
Genes Web page 



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


Re: All permutations from 2 lists

2022-03-02 Thread gene heskett
On Wednesday, 2 March 2022 10:49:11 EST Larry Martell wrote:
> On Wed, Mar 2, 2022 at 10:26 AM Antoon Pardon  
wrote:
> > Op 2/03/2022 om 15:58 schreef Larry Martell:
> > > On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  
wrote:
> > > If one list is empty I want just the other list. What I am
> > > doing is
> > > building a list to pass to a mongodb query. If region is empty
> > > then I
> > > want to query for just the items in the os list. I guess I can
> > > test
> > > for the lists being empty, but I'd like a solution that handles
> > > that
> > > as down the road there could be more than just 2 lists.
> >  
> >  How about the following: Keep a list of your lists you want to
> >  permute over. Like the following:
> >  
> >  permutation_elements = [["Linux","Windows"],["us-east-1",
> >  "us-east-2"]]
> >  
> >  permutation = itertools.product(*permutation_elements)
> >  
> >  If you don't include the empty list, you will get more or less
> >  what you seem to want.
> > >>> 
> > >>> But I need to deal with that case.
> > >> 
> > >> What does that mean? How does using the above method to produce
> > >> the permutations you want, prevent you from dealing with an empty
> > >> list however you want when you encounter them? Just don't add
> > >> them to the permutation_elements.> > 
> > > I need to know what items are in which position. If sometimes the
> > > regions are in one index and sometimes in another will not work for
> > > me.
> > 
> > I am starting to suspect you didn't think this through. What you are
> > telling here contradicts what you told earlier that if either list
> > was empty, you just wanted the other list. Because then you wouldn't
> > know what items were in that list.
> > 
> > The only solution I can see now is that if a list is empty, you
> > either add [None] or [""] to the permutation_elements (whatever
> > suits you better) and then use itertools.product
> 
> I found a way to pass this directly into the query:
> 
> def query_lfixer(query):
> for k, v in query.items():
> if type(v)==list:
> query[k] = {"$in": v}
> return query
> 
> self._db_conn[collection_name].find(query_lfixer(query))

I take it back, kmail5 had decided it was a different thread. My bad, no 
biscuit.

Cheers, Gene Heskett.
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
 - Louis D. Brandeis
Genes Web page 



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


Re: All permutations from 2 lists

2022-03-02 Thread Chris Angelico
On Thu, 3 Mar 2022 at 13:05, gene heskett  wrote:
> I take it back, kmail5 had decided it was a different thread. My bad, no
> biscuit.
>

Awww, I was going to make a really bad joke about timezones :)

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


Re: All permutations from 2 lists

2022-03-02 Thread Avi Gross via Python-list
Larry,

i waited patiently to see what others will write and perhaps see if you explain 
better what you need. You seem to gleefully swat down anything offered. So I am 
not tempted to engage.

Some later messages suggest you may not be specifying quite what you want. It 
sounds like you are asking for something like a function that combines two 
things a certain way. Initially it sounded like two simple lists of any length 
and something like this was proposed:

[(f, s) for f in os for s in region]

It returns a list of tuples, but could just as easily return a list of lists or 
dictionary or  other variants.

You pointed out, validly, that if either of the original lists is empty, it 
fails. True. 

But that opens the question of what happens when any random thing is thrown 
there such as "hello" which Python happily treats as ['h', 'e', 'l', 'l', 'o'] 
for this purpose. You now seem to want a bulletproof solution that might start 
with a very basic function like this but add more checks and deal appropriately:

def combine_2(first, second):
l_first = len(first)
l_second =  len(second)

if ( l_first + l_second == 0 ): return([])
if ( l_first == 0 ): return(second)
if ( l_second == 0 ): return(first)
return([(f, s) for f in first for s in second])

The above works fine if I give it [] for one or both but maybe not. It returns 
an empty list if both are empty, which may be what you want or you may want an 
error or an empty tuple, ...

You wanted tuples in return. But if either one is empty, the above returns the 
original, a list without tuples. To return the valid list with each element as 
a singleton tuple could easily be done but you have not initially specified the 
expected behavior.

And it is hard to guess as it is not clear what you will do with this. Do you 
want to be able to do this for more than 2 at a time? At some point, the above 
approach is not right and an approach that works for an arbitrary number of 
lists to combine may make sense. Some python modules do something similar to 
the R function expand.grid() that makes all combinations into rows of a 
data.frame such as this expand_grid in the pandas module: 

https://pandas.pydata.org/pandas-docs/version/0.17.1/cookbook.html#creating-example-data

I assume that could be used as-is in some applications but could also be easily 
converted back into a list of tuples or have a generator deliver one result 
each time ...

And, yes, the above approach needs to convert your arguments into a single 
dictionary but that is easy enough. Getting back a list of lists horizontally 
may be something like this:

 df.values.tolist()

As others have pointed out, you are actually not stating what you want and are 
getting the right answer if you ALLOW any of your items to be empty. It is like 
any operation using an NaN or an Inf do not work well as the result is 
generally nonsense. If you do not want any empty sets, take care to exclude 
them. In my dictionary example above, you might add only non-empty lists to the 
dictionary, for example, and if the dictionary remains empty, then there is 
nothing to combine.

I also see a subtle requirement that is annoying. You want the NAME of each 
entry preserved in order even as some may be EMPTY. So i ask where these names 
are stored so you can communicate them. This is trivial in languages like R but 
perhaps not in Python without some help. My suggestion is that numpy/pandas and 
their data types can help. Once you make a structure like I describe above, the 
columns have headers you can ask about and then you can pass the headers of 
surviving entities that perfectly match the columns, even if you take the 
columns out into a list of tuples.

It looks to some like you want lots of things without being willing to earn 
them. Any number of methods will easily remove or filter what you keep. Once 
you have clean data, the remainder can be trivial.

You then supplied a solution you said worked fine that looked pretty much 
NOTHING like what we have been discussing:

def query_lfixer(query):
for k, v in query.items():
if type(v)==list:
query[k] = {"$in": v}
return query

self._db_conn[collection_name].find(query_lfixer(query))


The above code seems be taking a dictionary and modifying it? And what you 
needed in the end was a dictionary? So why did so many of us bother? I will 
keep that in mind for future questions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting Syslog working on OSX Monterey

2022-03-02 Thread Philip Bloom via Python-list
I'm probably asking on the wrong list, and probably should bother wherever
apple's ASL experts live for changes in monterey.   Guess nobody else is
seeing this?

The same exact code is working just fine on OSX Big Sur, but on OSX
Monterey it doesn't work at all.  Users that haven't updated are having the
program produce logs as it has for years through
logging.handlers.SysLogHandler.  Mac OSX definitely has a listening socket
at '/var/run/syslog' which shows up in Console.app.

Apologies, Barry.  I'm not quite understanding your responses.

When we say OSX has no listener for Syslog, what is the Apple System Log
and the general output to Console.app then?  I thought that was the local
syslog on OSX and had, for years, been behaving as such when using
logging.handlers.SysLogHandler with a config in /etc/asl/ to define how it
should be routed and the rollover/cleanup frequency.

Thanks for replying, just having trouble understanding.


On Mon, Feb 28, 2022 at 2:07 PM Barry Scott  wrote:

>
>
> > On 28 Feb 2022, at 21:41, Peter J. Holzer  wrote:
> >
> > On 2022-02-27 22:16:54 +, Barry wrote:
> >> If you look at the code of the logging modules syslog handle you will
> see that
> >> it does not use syslog. It’s assuming that it can network to a syslog
> listener.
> >> Such a listener is not running on my systems as far as I know.
> >>
> >> I have always assumed that if I want a logger syslog handler that I
> would have
> >> to implement it myself. So far I have code that uses syslog directly
> and have
> >> not written that code yet.
> >
> > What do you mean by using syslog directly? The syslog(3) library
> > function also just sends messages to a "syslog listener" (more commonly
> > called a syslog daemon) - at least on any unix-like system I'm familiar
> > with (which doesn't include MacOS). It will, however, always use the
> > *local* syslog daemon - AFAIK there is no standard way to open a remote
> > connection (many syslog daemons can be configured to forward messages to
> > a remote server, however).
>
> I'm re-reading the code to check on what I'm seeing. (Its been a long
> time since I last look deeply at this code).
>
> You can write to /dev/log if you pass that to
> SysLogHandler(address='/dev/log'), but the default is to use a socket
> to talk to a network listener on localhost:514. There are no deamons
> listening on port 514 on my Fedora systems or mac OS.
>
> That is not what you would expect as the default if you are using the C
> API.
>
> What you do not see used in the SyslogHandler() is the import syslog
> and hence its nor using openlog() etc from syslog API.
>
> Barry
>
>
>
> >hp
> >
> > --
> >   _  | Peter J. Holzer| Story must make more sense than reality.
> > |_|_) ||
> > | |   | h...@hjp.at |-- Charles Stross, "Creative writing
> > __/   | http://www.hjp.at/ |   challenge!"
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Philip Bloom
Director, Services Engineering
*AppLovin Corporation*
M: (786)-338-1439 <786-338-1439>
[image: LinkedIn]  [image:
Twitter]  [image: Facebook]
 [image: Instagram]

[image: AppLovin] 
-- 
https://mail.python.org/mailman/listinfo/python-list