ValueError: 在重构 PySpark 以与 Snowpark 兼容时未找到子字符串

huangapple go评论51阅读模式
英文:

ValueError: substring not found when refactoring PySpark to work with snowpark

问题

我正在尝试在重构 PySpark 代码以适配 Snowflake 时,建立在我从 Stack Overflow 社区成员那里收到的帮助的基础上。

有人可以看一下以下的 snowpark Python 代码并告诉我为什么我会遇到无效的语法错误吗?

import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col

def registerDelta(session: snowpark.Session, regName, stage, filePath):

  pos = regName.index(".")
  if pos > 0:
    dbName = stage + regName[0:pos]
  else:
    dbName = stage

  try:
    crtdb = f"CREATE DATABASE IF NOT EXISTS {dbName}"
    session.sql(crtdb)
    print(f"{stage} - Database created")
  except:
    print("Create Database Failed")

  try:
    SQL = f"CREATE TABLE IF NOT EXISTS {stage}{regName} USING DELTA LOCATION '{filePath}'"
    session.sql(SQL)
    print(f"{stage}{regName} - Table created")
  except:
    print("Create Table Failed")

def main(session: snowpark.Session):
    return registerDelta(session, 'test1', 'test2', 'test3')

我有一种感觉这将凸显出我在Python编程方面的不足。不管怎样,有什么想法吗?

英文:

I am trying to build on the help I've received from SO members when refactoring PySpark code to snowflake.

Can someone take a look at the following snowpark python code and let me know why I'm getting invalid syntax error:

ValueError: 在重构 PySpark 以与 Snowpark 兼容时未找到子字符串

import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col

def registerDelta(session: snowpark.Session,regName, stage, filePath):
      
  pos = regName.index(".")
  if pos > 0:
    dbName = stage + regName[0:pos]
  else:
    dbName = stage
  
  try:
    crtdb = f"CREATE DATABASE IF NOT EXISTS {dbName}"
    session.sql(crtdb)
    print(f"{stage} - Database created")
  except:
    print("Create Database Failed")
  
  try:
    SQL = f"CREATE TABLE IF NOT EXISTS {stage}{regName} USING DELTA LOCATION '{filePath}'"
    session.sql(SQL)
    print(f"{stage}{regName} - Table created")
  except:
    print("Create Table Failed")

def main(session: snowpark.Session):
    return registerDelta(session, 'test1', 'test2', 'test3')

I've got feeling this will highlight my lack of Python coding knowledge.

Anyway, any thoughts?

答案1

得分: 1

This issue is purely based on code and it is not specific to Snowpark.

pos = regName.index(".")  # 当字符串未找到时,会引发错误
if pos > 0:
  dbName = stage + regName[0:pos]
else:
  dbName = stage

> 我得到了无效的语法错误:
>
> ValueError: 未找到子字符串

这个错误不是语法错误,而是由于提供的输入引起的运行时错误,这是正确的行为。


Python str.index()
> str.index(sub[, start[, end]])
>
> 类似于find(),但当未找到子字符串时引发ValueError错误

Python str.find()
>
> str.find(sub[, start[, end]])
>
> 返回字符串中子字符串sub第一次出现的最低索引,该子字符串位于s[start:end]的切片内。可选参数start和end的解释与切片表示法中的相同。如果未找到子字符串sub,则返回-1

英文:

This issue is purely based on code and it is not specific to Snowpark.

pos = regName.index(".")  # when string is not found it raises an error
if pos > 0:
  dbName = stage + regName[0:pos]
else:
  dbName = stage

> I'm getting invalid syntax error:
>
> ValueError: substring not found

This error is not a syntax error but a runtime error due to provided input and it is correct behavior.


Python str.index()
> str.index(sub[, start[, end]])
>
> Like find(), but raise ValueError when the substring is not found.

Python str.find()
>
> str.find(sub[, start[, end]])
>
> Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

huangapple
  • 本文由 发表于 2023年5月14日 05:37:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定