【动态更新】弃用deprecated登记

由于各项目代码更新频率不一致,导致项目中存在已弃用或即将弃用的方法,故在此记录一下,方便查找,
以下的时间并非弃用时间,仅代表我发现的时间


SQLAlchemy:

2022-04-29更新

警告内容:

SADeprecationWarning: The Engine.table_names() method is deprecated and will be removed in a future release.  Please refer to Inspector.get_table_names(). (deprecated since: 1.4)

调整方案:

tables_list = engine.table_names()

# 改为
from sqlalchemy import inspect

insp = inspect(engine)
tables_list = insp.get_table_names()

2023-02-10更新

警告内容:

AttributeError: 'Engine' object has no attribute 'execute'

自SQLAlchemy 2.0 开始变化
调整方案:

engine.execute(stmt)

# 改为
with engine.connect() as conn:
    result = conn.execute(stmt)
    conn.commit()

参考:https://stackoverflow.com/a/75316945

2023-02-23更新

警告内容:

'OptionEngine' object has no attribute 'execute'

自SQLAlchemy 2.0 开始变化
调整方案:

df = pd.read_sql_query(sql, engine)

# 改为
from sqlalchemy import text

df = pd.DataFrame(engine.connect().execute(text(sql)))

参考:https://stackoverflow.com/a/75309321

2023-03-28更新

LegacyCursorResult弃用

# LegacyCursorResult弃用
sqlalchemy.engine.cursor.LegacyCursorResult
# 改为
sqlalchemy.engine.cursor.CursorResult

2023-06-27更新

2.0开始,需要在SQL语句外用text()方法包裹才能执行

AttributeError: 'str' object has no attribute '_execute_on_connection'
session.execute('DELETE FROM table')
# 改为
from sqlalchemy import text

session.execute(text('DELETE FROM table'))

2023-06-29更新

2.0开始,表的元数据映射有一些变化

metadata = MetaData(engine)
table = Table('table_name', metadata, autoload=True)

报错:

expected schema argument to be a string, got .

改为

# 多线程下可复用metadata对象,而不需要重复创建
metadata = MetaData()
metadata.reflect(bind=engine)

table = Table('table_name', metadata, autoload=True)

第二种:

# 多线程下可复用metadata对象,而不需要重复创建
metadata = MetaData()

table = Table('table_name', metadata, autoload_with=engine)

其中第一种方式会映射所有表的元数据,耗时会比较长,而且数据库中表变化后,需要重新映射;
第二种方法是只映射某个表,所以效率更高一些。
两种方式各有优劣,比如如果数据库中表相对固定,更推荐第一种方式,否则推荐第二种

Pandas

2022-04-29更新

警告内容:

FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

调整方案:

df = df.append(df_table)

# 改为
df = pd.concat([df, df_table])

2022-10-24更新

警告内容:

FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`

调整方案:

df.log[:, ['a', 'b']] = [1, 2]

# 改为
df[['a', 'b']] = [1, 2]

2023-05-30更新

警告内容:

FutureWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`

调整方案:

df.loc[:, df.columns != 'B'] = df.loc[:, df.columns != 'B'].fillna('')

# 改为
cols_to_fill = df.select_dtypes(include=[np.number]).columns.difference(['B'])
df[cols_to_fill] = df[cols_to_fill].fillna('')

Flask

2022-08-18更新

警告内容:

/app/swagger_server/app.py:37: DeprecationWarning: 'app.json_encoder' is deprecated and will be removed in Flask 2.3. Customize 'app.json_provider_class' or 'app.json' instead.

调整方案:
待补充…

Jupyter

2023-03-28更新

之前jupyerhub集成jupyterlab需要安装插件jupyterlab/hub-extension,但jupyter labextension install安装插件的方式已经弃用且该插件也已经弃用

>>> jupyter labextension install @jupyterlab/hub-extension
(Deprecated) Installing extensions with the jupyter labextension install command is now deprecated and will be removed in a future major version of JupyterLab.

调整方案:
修改jupterhub配置文件即可换成jupyterlab界面

>>> vim /etc/jupyterhub/jupyterhub_config.py

#  - You can set `notebook_dir` to `/` and `default_url` to `/tree/home/{username}` to allow people to
#    navigate the whole filesystem from their notebook server, but still start in their home directory.
#  - Start with `/notebooks` instead of `/tree` if `default_url` points to a notebook instead of a directory.
#  - You can set this to `/lab` to have JupyterLab start by default, rather than Jupyter Notebook.
#  Default: ''
c.Spawner.default_url = '/lab'

你可能感兴趣的:(不定时更新,Python,python,deprecated,1024程序员节)