懒癌晚期的import方法——pyforest

前言

  日常 Python 开发过程中,必然会存在 import 库的操作,import 语句一般都卸载 py 文件的最前面。每次重新开启一个建模流程或者分析过程时,会重新敲一遍 import 或者将之前的 import 代码 copy 进去。虽然已经用的滚花烂熟了,但是确耗费不必要的时间。

import requests
import path
import os
import numpy as np

......

  这时,一个适合懒癌晚期的库就出现了——pyforest

pip install pyforest

使用方式

  使用方式非常简单,只需要在文件最前面写上一句就可以实现对标准库的 import

from pyforest import *

  如果使用 Jupyter 或 IPython,甚至可以跳过此行,因为 pyforest 会将其自身添加到自动启动中。而且,完成脚本后,可以通过以下方式导出所有导入语句:

active_imports()  # 通过这个指令可以看到脚本所涉及到的全部已经被省略import的库。

库不在 pyforest 中怎么办

  pyforest 支持大部分流行的数据科学库,比如 pandas,numpy,matplotlib,seaborn,sklearn,tensorflow 等等,以及常用的辅助库如 os,sys,re,pickle 等。

### Data Wrangling
pd = LazyImport("import pandas as pd")

np = LazyImport("import numpy as np")

dd = LazyImport("from dask import dataframe as dd")
SparkContext = LazyImport("from pyspark import SparkContext")

load_workbook = LazyImport("from openpyxl import load_workbook")

### Data Visualization and Plotting
mpl = LazyImport("import matplotlib as mpl")
plt = LazyImport("import matplotlib.pyplot as plt")

sns = LazyImport("import seaborn as sns")

py = LazyImport("import plotly as py")
go = LazyImport("import plotly.graph_objs as go")
px = LazyImport("import plotly.express as px")

dash = LazyImport("import dash")

bokeh = LazyImport("import bokeh")

alt = LazyImport("import altair as alt")

pydot = LazyImport("import pydot")

# statistics
statistics = LazyImport("import statistics")

### Machine Learning
sklearn = LazyImport("import sklearn")
OneHotEncoder = LazyImport("from sklearn.preprocessing import OneHotEncoder")
TSNE = LazyImport("from sklearn.manifold import TSNE")
train_test_split = LazyImport("from sklearn.model_selection import train_test_split")
svm = LazyImport("from sklearn import svm")
GradientBoostingClassifier = LazyImport(
    "from sklearn.ensemble import GradientBoostingClassifier"
)
GradientBoostingRegressor = LazyImport(
    "from sklearn.ensemble import GradientBoostingRegressor"
)
RandomForestClassifier = LazyImport(
    "from sklearn.ensemble import RandomForestClassifier"
)
RandomForestRegressor = LazyImport("from sklearn.ensemble import RandomForestRegressor")

TfidfVectorizer = LazyImport(
    "from sklearn.feature_extraction.text import TfidfVectorizer"
)

# TODO: add all the other most important sklearn objects
# TODO: add separate sections within machine learning viz. Classification, Regression, Error Functions, Clustering

# Deep Learning
tf = LazyImport("import tensorflow as tf")
keras = LazyImport("import keras")

# NLP
nltk = LazyImport("import nltk")
gensim = LazyImport("import gensim")
spacy = LazyImport("import spacy")
re = LazyImport("import re")

### Helper
sys = LazyImport("import sys")
os = LazyImport("import os")
re = LazyImport("import re")
glob = LazyImport("import glob")
Path = LazyImport("from pathlib import Path")

pickle = LazyImport("import pickle")

dt = LazyImport("import datetime as dt")

tqdm = LazyImport("import tqdm")
View Code

  并且,pyforest 支持向其中添加库。操作方法也很简单,找到 pyforest 库的 user_imports.py 文件,添加相应语句

#############################
### User-specific imports ###
#############################
# You can save your own imports in ~/.pyforest/user_imports.py
# Please note: imports in ~/.pyforest/user_imports.py take precedence over the
# imports above.

速度问题

  也许会有疑问,这种导入方式会不会很慢?答案是不会,因为只有真正使用了到了 pyforest 里的包含的库程序才会真正 import,否则不会。

结语

  本人身为一个有精神洁癖,略带极客精神的码农,内心上是极度排斥这种懒人库的。感觉可能会造成库的污染或者导入库不清晰,所以在大项目中不推荐使用。自己做小脚本开发或者其他不重要的时候倒是可以适度使用。

  pyforest 虽好,可不要贪杯哦

                

原文地址:https://www.cnblogs.com/zhuminghui/p/15070562.html