bendudson / py4cl

Call python from Common Lisp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to handle with the TRY and EXCEPT statement use PY4CL?

cl-03 opened this issue · comments

commented

the PYTHON codes bellow:

try:
    c = driver.find_element_by_xpath("//*[@class='el-dialog captchaDilaog']")
    print("exist")
except:
    print("no")

using py4cl with errors:

(defparameter flag (let ((str1 "//*[@class='el-dialog captchaDilaog']")
			 (str2 "exist")
			 (str3 "no"))
		     (py4cl:python-call
 "try:
    c = driver.find_element_by_xpath("(py4cl::pythonize str1) ")
    flag = " (py4cl::pythonize str2)
"except:
    flag = "(py4cl::pythonize str3 ))))

or the other form also error:

(defparameter flag (py4cl:python-eval
 "try:
    c = driver.find_element_by_xpath(\"//*[@class='el-dialog captchaDilaog']\")    flag = \"exist\"
except:
    flag = \"no\""))
commented

Can py4cl deal with this situation?

Apologies, been busy with other tasks. You might also find the documentation of py4cl2 useful, particularly this part:

Unlike lisp, python (and most other languages) make a distinction between statements and expressions: see Quora or stackoverflow.

A general rule of thumb from there is: if you can print it, or assign it to a variable, then it's an expression, otherwise it's a statement.

Both pyeval and pyexec take any type of arguments. The arg is pythonized if the arg is not a string, or it is a string that can be read into a real.

As far as I understand python, since try...except is a statement(s) rather than expression, that part cannot be eval-ed; so, you'd probably want to deal directly with this:

(defparameter flag 
           (handler-case 
               (progn
                 ;; depending on your performance requirements, 
                 ;; you could also store the driver on lisp side (slowest) and call the method using py4cl:python-method
                 (py4cl:python-eval "driver.find_element_by_xpath(\"//*[@class='el-dialog captchaDilaog']\")")
                 "exist")
             (py4cl:python-error (condition)
               (declare (ignore condition))
               "no")))

Either that way, or by converting the block of statements into a function (fastest) and then calling that function using python-eval or python-call.

commented

It doesn't matter and give a thanks to you,Let me have a try later,thanks.

commented

It has been handled At last like bellow:

 (py4cl:python-evec
"try:
    c = driver.find_element_by_xpath(\"//*[@class='el-dialog captchaDilaog']\")
    print(\"exist\")
except:
    print(\"no\")"

Thank you for your kindness。