Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:50:57+00:00 2026-06-14T05:50:57+00:00

I need to compile an input string (from a database) as a function and

  • 0

I need to compile an input string (from a database) as a function and run it. This is the method I currently use:

Say I already have this function defined:

def foo():
    print "Yo foo! I am a function!"

And the string I read in from the database says I should call this function:

string_from_db = "foo()"

I then form a function (whose name I can control) which returns the function I read in from the database:

my_func_string = "def doTheFunc():\n\t return " + string_from_db

Now I compile the string, and set it to a function name I use later for processing:

exec my_func_string
processor = doTheFunc

I can call it later by running processor() which exclaims: Yo foo! I am a function!

My Question: Above this piece of code there is a comment (left by a long lost developer):

    ###############################################################
    # The following method for getting a function out of the text #
    # in the database could potentially be replaced by using      #
    # Python's ast module to obtain an abstract syntax tree and   #
    # put it inside of a function node                            #
    ###############################################################

I’d like to know more about this “potential replacement.” I have taken a look at AST, but I am very confused on how it can help me do what I am already doing with the code above.

  1. How can I accomplish the same thing using AST? Or where can AST help me in this process?
  2. Is the implementation using AST better, and why?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:50:58+00:00Added an answer on June 14, 2026 at 5:50 am

    AST is a much safer choice when evaluating untrusted code, that could contain malicious instructions, eval or exec can execute any expression, while AST on the other hand, allows you to parse and validate the expression yourself, or execute a limited expression using ast.literal_eval, sort of like a sandbox, which allows only strings, lists, numbers and a few other things. This is just a simple example:

    import ast
    a = ast.literal_eval("[1,2,3,4]") //evaluate an expression safely.
    

    Another example that parses a string into an AST:

    import ast
    source = '2 + 2'
    node = ast.parse(source, mode='eval')
    ast.dump(node)
    

    This prints:

    Expression(body=BinOp(left=Num(n=2), op=Add(), right=Num(n=2)))
    

    There’s a very good tutorial here and also the documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input string containing multiple lines(demarcated by \n). I need to search
Ok let's say I have an ObservableCollection<string> object. Within this object I have a
How can I compile a string from selected rows of a file, run some
I need to compile the unit with /EHsc option, how could i set this
I need to compile my GWT 1.7 project from my ant build file....anyone know
I have a program that is written in Ada, and I need to compile
Problem that i face: -I have an input string, a SQL statement that i
I need to preface this with I am not allowed to use an IDE
I have a multi-line ASCII string coming from some (Windows/UNIX/...) system. Now, I know
I have a user supplied string, that I then need to turn into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.