Executing python against a script stored in database - linux

db: mysql
lang: python
framework:
django
Operating System: Linux (ubuntu)
Hello,
Is there a way to execute a python against a content of a script that is stored in a database? For example, a content of a file is stored in a db column text. Would the only solution be to create a temporary file, dump the content from the db into the file and then run python os command against it? I'm assuming the content of the executed script will need to be stored such that it escapes quotes etc.
I'm open to suggestions on what database to use to accomplish my goal. MySQL will require additional wrappers before storage of the file content and possibly apply others to reply qoutes/datetime/etc.
Please advise if additional information necessary, but in essence i'm looking to store python script content in a db, retrieve it and run it against the python interpreter.
Thank you in advance for any advise.

You can use the compile built in function.
s = """def f(x):
return x + x
print(f(22))
"""
code = compile(s, "string", "exec")
exec(code)
# OUT: 44
Although I'm wondering if you couldn't just store a data structure and use that with some pre-defined code. Executing arbitrary code in this way could be dangerous, and a security risk.

This seems very similar to SO post here:
Dynamically loading Python application code from database under Google App Engine
Here is information on exec
http://docs.python.org/release/2.5.2/ref/exec.html
Python Wiki page for Python+MySQL
http://wiki.python.org/moin/MySQL

Related

Access database instance from command prompt

I’ve tried looking for a similar question but couldn’t find one. Sorry if this question has already been asked.
To my question, I’ve created a flask application that creates a database instance using SQLalchemy. My question is how do I access this .db file from my command prompt? I’d like for example get a list of all the users. In my app I’d write User.query.all() but it gets messy quickly. I’d rather just do it from the command line on my Mac if possible.
Thanks in advance!!
What I’ve tried from the terminal:
sqlite3 database.db
User.query.all() (nothing happens)
And then:
python3 main.py
User.query.all() (nothing happens)
sqlite3 is correct to open the database file. you're going to need to use SQL statements from terminal:
sqlite3 database.db
show tables;
select * from Users; # depends on your table names, obviously
insert into Users... # and so on, please refer to sqlite3 manual if you need to or ask a direct question about what you're trying to manually do to the database
The python3 command is just going to run the interpreter across main.py.. I think flask is started with flask run but you're probably looking for flask shell .. it's been a minute for me on flask, sorry.
https://www.sqlite.org/cli.html

Embed python into looker

Is there a natural way to wrap Python code to display in Looker?
The ideal dataflow for my problem is SQL DB->python-> looker, or alternatively, looker->python-> looker. 
I am hoping to embed a.py into lookML so that I can automate python analysis, ready to display in looker.
You can't call Python code from Looker. Depending on the database you are using, you may want to look into creating a UDF within that database and then call it using SQL.

Open .exe file using python and trying to pass parameter in the same python script

I'm trying to open a .exe file from Python and give it some instructions. Because I have thousands of models to run I need to automatize the process.
Here on Stackoverflow, I found several options that I tried. I am able to open .exe file but not able to fill the information in that and run the .exe. the place is always empty. I'm writing one of these solutions. [I'm using Python3]:
import os
your_bat_file_address = r'"C:\D_drive\testing\SAR\1100_star3\exmple.bat"' # example
os.startfile(your_bat_file_address)
in the exmple.bat file
"C:/D_drive/tool/EXMPLE.exe" --input1 "C:/D_drive/file/1st_file" --input2 "C:/D_drive/file/2st_file" --input3 "C:/D_drive/file/3st_file"

Sending Information from one Python file to another

I would like to know how to perform the below mentioned task
I want to upload a CSV file to a python script 1, then send file's path to another python script in file same folder which will perform the task and send the results to python script 1.
A working code will be very helpful or any suggestion is also helpful.
You can import the script editing the CSV to the python file and then do some sort of loop that edits the CSV file with your script 1 then does whatever else you want to do with script 2.
This is an advantage of OOP, makes these sorts of tasks very easy as you have functions set in a module python file and can create a main python file and run a bunch of functions editing CSV files this way.

Why pandas profiling isn't showing any output in ipython?

I've a quick question about "pandas_profiling" .So basically i'm trying to use the pandas 'profiling' but instead of showing the output it says something like this:
<pandas_profiling.ProfileReport at 0x23c02ed77b8>
Where i'm making the mistake?? or Does it have anything to do with Ipython?? Because i'm using Ipython in Anaconda.
try this
pfr = pandas_profiling.ProfileReport(df)
pfr.to_notebook_iframe()
pandas_profiling creates an object that then needs to be displayed or output. One standard way of doing so is to save it as an HTML:
profile.to_file(outputfile="sample_file_name.html")
("profile" being the variable you used to save the profile itself)
It doesn't have to do with ipython specifically - the difference is that because you're going line by line (instead of running a full block of code, including the reporting step) it's showing you the object itself. The code above should allow you to see the report once you open it up.

Resources