当前位置:   article > 正文

【动态更新】弃用deprecated登记_optionengine' object has no attribute 'execute

optionengine' object has no attribute 'execute

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


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)
  • 1

调整方案:

tables_list = engine.table_names()

# 改为
from sqlalchemy import inspect

insp = inspect(engine)
tables_list = insp.get_table_names()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2023-02-10更新

警告内容:

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

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

engine.execute(stmt)

# 改为
with engine.connect() as conn:
    result = conn.execute(stmt)
    conn.commit()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

2023-02-23更新

警告内容:

'OptionEngine' object has no attribute 'execute'
  • 1

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

df = pd.read_sql_query(sql, engine)

# 改为
from sqlalchemy import text

df = pd.DataFrame(engine.connect().execute(text(sql)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

2023-03-28更新

LegacyCursorResult弃用

# LegacyCursorResult弃用
sqlalchemy.engine.cursor.LegacyCursorResult
# 改为
sqlalchemy.engine.cursor.CursorResult
  • 1
  • 2
  • 3
  • 4

2023-06-27更新

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

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

session.execute(text('DELETE FROM table'))
  • 1
  • 2
  • 3
  • 4
  • 5

2023-06-29更新

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

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

报错:

expected schema argument to be a string, got <class 'sqlalchemy.engine.base.Engine'>.
  • 1

改为

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

table = Table('table_name', metadata, autoload=True)
  • 1
  • 2
  • 3
  • 4
  • 5

第二种:

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

table = Table('table_name', metadata, autoload_with=engine)
  • 1
  • 2
  • 3
  • 4

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

2023-07-25更新

/app/module/impalapi.py:38: SAWarning: Dialect impala:impala will not make use of SQL compilation caching as it does not set the 'supports_statement_cache' attribute to ``True``.  This can have significant performance implications including some performance degradations in comparison to prior SQLAlchemy versions.  Dialect maintainers should seek to set this attribute to True after appropriate development and testing for SQLAlchemy 1.4 caching support.   Alternatively, this attribute may be set to False which will disable this warning. (Background on this warning at: https://sqlalche.me/e/20/cprf)
  • 1
from impala.sqlalchemy import ImpalaDialect

ImpalaDialect.import_dbapi = ImpalaDialect.dbapi
ImpalaDialect.supports_statement_cache = False
  • 1
  • 2
  • 3
  • 4

参考:https://stackoverflow.com/questions/72190865/snowflake-sqlalchemy-supports-statement-cache-warning

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.
  • 1

调整方案:

df = df.append(df_table)

# 改为
df = pd.concat([df, df_table])
  • 1
  • 2
  • 3
  • 4

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)`
  • 1

调整方案:

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

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

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)`
  • 1

调整方案:

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('')
  • 1
  • 2
  • 3
  • 4
  • 5

2023-10-18更新

警告内容:

FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.
  • 1

调整方案:

df1 = pd.DataFrame({"A": [.1, .2, .3]})
df2 = pd.DataFrame(columns=["A"], dtype="object")

out = pd.concat([df1, df2]) ; print(out)

# 改为
out = (df1.copy() if df2.empty else df2.copy() if df1.empty
       else pd.concat([df1, df2]) # if both DataFrames non empty
      )
# 或
out = pd.concat([df1.astype(df2.dtypes), df2.astype(df1.dtypes)])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

参考:https://stackoverflow.com/questions/77254777/alternative-to-concat-of-empty-dataframe-now-that-it-is-being-deprecated

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.
  • 1

调整方案:
待补充…

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.
  • 1
  • 2

调整方案:
修改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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/584769
推荐阅读
相关标签
  

闽ICP备14008679号