Re: frozenset can be altered by |=

2021-11-22 Thread Marco Sulla
Yes, and you do this regularly. Indeed integers, for example, are immutables and

a = 0
a += 1

is something you do dozens of times, and you simply don't think that
another object is created and substituted for the variable named `a`.

On Mon, 22 Nov 2021 at 14:59, Chris Angelico  wrote:
>
> On Tue, Nov 23, 2021 at 12:52 AM David Raymond  
> wrote:
> > It is a little confusing since the docs list this in a section that says 
> > they don't apply to frozensets, and lists the two versions next to each 
> > other as the same thing.
> >
> > https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset
> >
> > The following table lists operations available for set that do not apply to 
> > immutable instances of frozenset:
> >
> > update(*others)
> > set |= other | ...
> >
> > Update the set, adding elements from all others.
>
> Yeah, it's a little confusing, but at the language level, something
> that doesn't support |= will implicitly support it using the expanded
> version:
>
> a |= b
> a = a | b
>
> and in the section above, you can see that frozensets DO support the
> Or operator.
>
> By not having specific behaviour on the |= operator, frozensets
> implicitly fall back on this default.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: About get_axes() in Pandas 1.2.3

2021-11-22 Thread Mahmood Naderan via Python-list
>I can help you narrow it down a bit. The problem actually occurs inside
>this function call somehow. You can verify this by doing this:
>
>
>fig,axes = plt.subplots(2,1, figsize=(20, 15))
>
>print ("axes[0].get_figure()=",axes[0].get_figure())
>
>You'll find that get_figure() is returning None, when it should be
>returning Figure(2000x1500). So plt.subplots is not doing something
>properly which was corrected at some point. Oddly enough, with pandas
>1.1.4 and matplotlib 3.2.2 (which is what my system has by default),
>there is no error, although the graph is blank.
>
>In my venv, when I upgrade matplotlib from 3.3.4 to 3.5, the problem
>also goes away.  3.4.0 also works.
>
>Honestly your solution is going to be to provide a virtual environment
>with your script.  That way you can bundle the appropriate dependencies
>without modifying anything on the host system.



Thanks for the feedback. You are right.
I agree that virtualenv is the most safest method at this time.


Regards,
Mahmood


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


Re: About get_axes() in Pandas 1.2.3

2021-11-22 Thread Michael Torrie
On 11/22/21 2:03 AM, Mahmood Naderan via Python-list wrote:
> Hi
> 
> I asked a question some days ago, but due to the lack of minimal
> producing code, the topic got a bit messy. So, I have decided to ask
> it in a new topic with a clear minimum code.
> import pandas as pd
> import csv,sys
> import matplotlib
> import matplotlib.pyplot as plt
> 
> df = pd.read_csv('test.batch.csv')
> print(df)
> 
> print("matplotlib version = ",  matplotlib.__version__)
> print("pandas version = ", pd.__version__)
> print("sys version", sys.version_info)
> 
> fig,axes = plt.subplots(2,1, figsize=(20, 15))
   ^
I can help you narrow it down a bit. The problem actually occurs inside
this function call somehow. You can verify this by doing this:

fig,axes = plt.subplots(2,1, figsize=(20, 15))
print ("axes[0].get_figure()=",axes[0].get_figure())

You'll find that get_figure() is returning None, when it should be
returning Figure(2000x1500). So plt.subplots is not doing something
properly which was corrected at some point. Oddly enough, with pandas
1.1.4 and matplotlib 3.2.2 (which is what my system has by default),
there is no error, although the graph is blank.

In my venv, when I upgrade matplotlib from 3.3.4 to 3.5, the problem
also goes away.  3.4.0 also works.

Honestly your solution is going to be to provide a virtual environment
with your script.  That way you can bundle the appropriate dependencies
without modifying anything on the host system.

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


Re: copy.copy

2021-11-22 Thread ast

Le 22/11/2021 à 16:02, Jon Ribbens a écrit :

On 2021-11-22, ast  wrote:




For immutable types, copy(foo) just returns foo.



ok, thx
--
https://mail.python.org/mailman/listinfo/python-list


Re: copy.copy

2021-11-22 Thread Jon Ribbens via Python-list
On 2021-11-22, ast  wrote:
> Hi,
>
> >>> a = 6
> >>> b = 6
> >>> a is b
> True
>
> ok, we all know that Python creates a sole instance
> with small integers, but:
>
> >>> import copy
> >>> b = copy.copy(a)
> >>> a is b
> True
>
> I was expecting False

Why did you expect False?

For immutable types, copy(foo) just returns foo.
-- 
https://mail.python.org/mailman/listinfo/python-list


copy.copy

2021-11-22 Thread ast

Hi,

>>> a = 6
>>> b = 6
>>> a is b
True

ok, we all know that Python creates a sole instance
with small integers, but:

>>> import copy
>>> b = copy.copy(a)
>>> a is b
True

I was expecting False
--
https://mail.python.org/mailman/listinfo/python-list


Re: frozenset can be altered by |=

2021-11-22 Thread Chris Angelico
On Tue, Nov 23, 2021 at 12:52 AM David Raymond  wrote:
> It is a little confusing since the docs list this in a section that says they 
> don't apply to frozensets, and lists the two versions next to each other as 
> the same thing.
>
> https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset
>
> The following table lists operations available for set that do not apply to 
> immutable instances of frozenset:
>
> update(*others)
> set |= other | ...
>
> Update the set, adding elements from all others.

Yeah, it's a little confusing, but at the language level, something
that doesn't support |= will implicitly support it using the expanded
version:

a |= b
a = a | b

and in the section above, you can see that frozensets DO support the
Or operator.

By not having specific behaviour on the |= operator, frozensets
implicitly fall back on this default.

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


RE: frozenset can be altered by |=

2021-11-22 Thread David Raymond
>> (venv_3_10) marco@buzz:~$ python
>> Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18)
>> [GCC 10.1.1 20200718] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> a = frozenset((3, 4))
>> >>> a
>> frozenset({3, 4})
>> >>> a |= {5,}
>> >>> a
>> frozenset({3, 4, 5})
> 
> That's the same as how "x = 4; x += 1" can "alter" four into five.
> 
> >>> a = frozenset((3, 4))
> >>> id(a), a
> (140545764976096, frozenset({3, 4}))
> >>> a |= {5,}
> >>> id(a), a
> (140545763014944, frozenset({3, 4, 5}))
> 
> It's a different frozenset.
> 
> ChrisA

Another possible option is instead of

a |= {5,}

change it to

a.update({5,})

If a is a regular set it will update the original object, and if a is a 
frozenset it will raise an AttributeError. Which may not be what you want, but 
at least it won't quietly do something you weren't expecting.

It is a little confusing since the docs list this in a section that says they 
don't apply to frozensets, and lists the two versions next to each other as the 
same thing.

https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset

The following table lists operations available for set that do not apply to 
immutable instances of frozenset:

update(*others)
set |= other | ...

Update the set, adding elements from all others.
-- 
https://mail.python.org/mailman/listinfo/python-list


About get_axes() in Pandas 1.2.3

2021-11-22 Thread Mahmood Naderan via Python-list
Hi

I asked a question some days ago, but due to the lack of minimal producing 
code, the topic got a bit messy. So, I have decided to ask it in a new topic 
with a clear minimum code.

With Pandas 1.2.3 and Matplotlib 3.3.4, the following plot() functions returns 
error and I don't know what is wrong with that.



import pandas as pd
import csv,sys
import matplotlib
import matplotlib.pyplot as plt

df = pd.read_csv('test.batch.csv')
print(df)

print("matplotlib version = ",  matplotlib.__version__)
print("pandas version = ", pd.__version__)
print("sys version", sys.version_info)

fig,axes = plt.subplots(2,1, figsize=(20, 15))
df.columns = range(1, len(df.columns)+1)   # Ignore the column header
row = df.iloc[0].astype(int)  # First row in the dataframe
plt.subplot(2, 1, 1)
print("axes=", axes)
print("axes[0]=", axes[0])
print("row=", row)
ax1 = row.plot(ax=axes[0])   # Line chart <-- ERROR
ax1.set_ylabel( 'test' )
plt.subplot(2, 1, 2)
df2 = row.value_counts()
df2.reindex().plot(kind='bar', ax=axes[1])   # Histogram

plt.show()




The output is



$ cat test.batch.csv
Value,Value
10,2
5,2
10,2

$ python3 test.py
   Value  Value.1
0 102
1  52
2 102
matplotlib version =  3.3.4
pandas version =  1.2.3
sys version sys.version_info(major=3, minor=8, micro=10, releaselevel='final', 
serial=0)
axes= [ ]
axes[0]= AxesSubplot(0.125,0.53;0.775x0.35)
row= 110
2 2
Name: 0, dtype: int64
Traceback (most recent call last):
  File "test.py", line 20, in 
ax1 = row.plot(ax=axes[0])   # Line chart
  File 
"/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_core.py", 
line 955, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
  File 
"/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py",
 line 61, in plot
plot_obj.generate()
  File 
"/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py",
 line 283, in generate
self._adorn_subplots()
  File 
"/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py",
 line 483, in _adorn_subplots
all_axes = self._get_subplots()
  File 
"/home/mahmood/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py",
 line 903, in _get_subplots
ax for ax in self.axes[0].get_figure().get_axes() if isinstance(ax, Subplot)
AttributeError: 'NoneType' object has no attribute 'get_axes'



Although the plot() crashes, I see that row and axes variables are valid. So, I 
wonder what is the workaround for this code without upgrading  Pandas or 
Matplotlib. Any idea?



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