gpt4 book ai didi

python - 使用工作流在 github 中对 databricks python 代码进行 flake8 linting

转载 作者:行者123 更新时间:2023-12-04 13:37:22 25 4
gpt4 key购买 nike

我在 github 中有我的数据块 python 代码.我设置了一个基本的工作流程来使用 flake8 lint python 代码.这失败是因为当我的脚本在 spark 上运行时隐式可用的名称(如 scdbutilsgetArgumentflake8 等)不可用在 databricks 之外(在 github ubuntu vm 中)对其进行 lints。

如何在 github 中整理 databricks 笔记本使用 flake8 ?

例如。 我得到的错误:

test.py:1:1: F821 undefined name 'dbutils'
test.py:3:11: F821 undefined name 'getArgument'
test.py:5:1: F821 undefined name 'dbutils'
test.py:7:11: F821 undefined name 'spark'

我在 github 上的笔记本:
dbutils.widgets.text("my_jdbcurl", "default my_jdbcurl")

jdbcurl = getArgument("my_jdbcurl")

dbutils.fs.ls(".")

df_node = spark.read.format("jdbc")\
.option("driver", "org.mariadb.jdbc.Driver")\
.option("url", jdbcurl)\
.option("dbtable", "my_table")\
.option("user", "my_username")\
.option("password", "my_pswd")\
.load()

我的 .github/workflows/lint.yml
on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: 3.8
- run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

最佳答案

你可以做的一件事是:

from pyspark.sql import Spark Session


spark = SparkSession.builder.getOrCreate()
无论是否使用 Databricks,这都适用于普通 Python 或 pyspark客户。
要检测您是在文件中还是在 Databricks 笔记本中,您可以运行:
try:
__file__
print("We are in a file, like in our IDE or being tested by flake8.")
except NameError:
print("We are in a Databricks notebook. Act accordingly.")
然后,您可以有条件地为 display() 初始化或创建虚拟变量。和其他工具。
这只是部分解决方案。我正在研究一个更好的解决方案,我会更新这个答案。

关于python - 使用工作流在 github 中对 databricks python 代码进行 flake8 linting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61019498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com