Python unable to execute script "invalid syntax"

Status
Not open for further replies.

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Okay, so basically we're using PHP to setup a dynamic Python script where the variables will be changing from request to request depending on the equation the user want's solved.
Currently when producing the script, I'm using \r\n and \n to define indentations.
Unfortunately this doesn't work because of Pythons weird indentation syntax.
How do I make it possible for this script to be run in the SageMath CLI?


Example on how some of the script is being produced:
PHP:
public function addTimeout($callback) {
  $this->cmd .= "\ndef solveEquation(): \n";
  $callback->bindTo($this)->__invoke();

  $this->cmd .= "\np = multiprocessing.Process(target=solveEquation, name='Solve Equation'); \n";
  $this->cmd .= "p.start(); \n";
  $this->cmd .= "p.join(".$this->timeout.");\n";

  $this->cmd .= "if p.is_alive(): \n";
  $this->cmd .= " print 'terminated' \n";
  $this->cmd .= " p.terminate() \n";
  $this->cmd .= " p.join();\n";
}
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
Why isn't PHP just accepting the dynamic variables and passing it to the Python script with exec? They both use the same implementation of argv

PHP:
<?php

$foo = 'var1';
$bar = 'var2';
$run = exec("python script.py .$foo .$bar");
echo $run;

I haven't used Sage but I guess if it's a binary on the system (and in $PATH) can use exec("sage script.py .$foo .$bar");[/php]
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Why isn't PHP just accepting the dynamic variables and passing it to the Python script with exec? They both use the same implementation of argv

PHP:
<?php

$foo = 'var1';
$bar = 'var2';
$run = exec("python script.py .$foo .$bar");
echo $run;

I haven't used Sage but I guess if it's a binary on the system (and in $PATH) can use exec("sage script.py .$foo .$bar");[/php]
What?
The CLI usage is exactly the same as Python:
./path/to/sage -c <script goes here>
PHP:
$this->cmd = self::$sagePath . ' -c "' . str_replace('"', '\\"', $this->cmd) . '" 2>&1';
exec($this->cmd, $out, $return_value);

Only thing different is that Sage binds the library to the script so that it can be imported.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
I guess I got stuck on the dynamic variable bit. Still not sure what you are trying to do and why everything from $this->cmd isn't in it's own python script. then exec("/path/to/sage -c thiscmd.py .$arg1 .$arg2");

just seems weird that all the python code is being passed through php in the first. clarify why the python code can't be in it's own script?
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
I guess I got stuck on the dynamic variable bit. Still not sure what you are trying to do and why everything from $this->cmd isn't in it's own python script. then exec("/path/to/sage -c thiscmd.py .$arg1 .$arg2");

just seems weird that all the python code is being passed through php in the first. clarify why the python code can't be in it's own script?
Because it's not only variables that'll be passed, but also Python functions that'll be generated in PHP and having to be executed, so it's nowhere close to being a static script.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Not sure about your Python code, but if it's not static in full but it can be split up in multiple "static files", you can fetch the content's of the file that contains the pieces you need, and construct something that way. Some kind of building block idea.
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Not sure about your Python code, but if it's not static in full but it can be split up in multiple "static files", you can fetch the content's of the file that contains the pieces you need, and construct something that way. Some kind of building block idea.
I don't know if this will work, but it'll definitely be too slow.
tbh just tired of the fucking shitty code they've written here

sadly I can't use sage as a standalone python library, otherwise I could just use
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Hm, that might be a solution actually.
tbh just tired of the fucking shitty code they've written here
It will at least take away the syntax problems, probably. But it really depends on your code and what's needed.

Also, if I were you, I'd dial down on shit talking your employer's code on a public forum. Company's do not like that, and it's also really unprofessional.
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
It will at least take away the syntax problems, probably. But it really depends on your code and what's needed.

Also, if I were you, I'd dial down on shit talking your employer's code on a public forum. Company's do not like that, and it's also really unprofessional.
They've said it themselves, otherwise I wouldn't be mentioning it, but yeah you're right.
 
Status
Not open for further replies.

Users who are viewing this thread

Top