Apologies if the question title is too vague - would welcome edits.
I'm trying to parse XML using BeautifulSoup, but because each function call could return None, I have to parse a single element over a number of lines. This gets unwieldy very quickly:
books_count = result.books_count
if not books_count:
return None
books_count = books_count.string
if not books_count:
return None
books_count = int(books_count)
if not books_count:
return None
Doing the same thing in Swift, a language I'm far more familiar with, it's a lot cleaner:
guard let booksCountString = result.booksCount?.string,
let booksCountInt = Int(booksCountString) else {
return nil
}
Is there a similar way I could do this in Python? I want to avoid using try/catch as causing it to potentially have a runtime error constantly in my code doesn't feel like good practice.
Use str. Call str. split(sep) to parse the string str by the delimeter sep into a list of strings. Call str. split(sep, maxsplit) and state the maxsplit parameter to specify the maximum number of splits to perform.
To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .
If you represent your sequence of None
checks using lambdas, like so:
check_1 = lambda r: r.books_count
check_2 = lambda r: r.string
check_3 = lambda r: int(r)
Then we can generalize a solution for any number of those checks. This function is similar (in a way) to how functools.reduce works:
def my_reduce(fs, arg, stop_value=None):
res = arg
for f in fs:
res = f(res)
if res == stop_value:
return stop_value
return res
Then use it like this:
my_reduce([check_1, check_2, check_3], good_value)
Out[1]: 42
my_reduce([check_1, check_2, check_3], bad_value) is None
Out[2]: True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With