Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python compile()

#1
Python compile()

<div><p>If you’re like me, you love those <strong>TLDR;</strong> overviews to grasp the big picture quickly. Here is mine about Python’s <code>compile()</code> function:</p>
<p class="has-pale-cyan-blue-background-color has-background">Python’s built-in <code>compile()</code> method returns an executable code object as an <strong><em>“Abstract Syntax Tree”</em></strong> represented as an <code>ast</code> object. By passing this code object into the <code>exec()</code> or <code>eval()</code> functions, you can run it dynamically in your Python code. This way, you can programmatically create source code and execute it at runtime. To use the function, pass the string code to be executed, the filename, and the execution mode. For example <code>compile('print("hi")', '&lt;string&gt;', 'exec')</code> creates a code object consisting of one line <code>print("hi")</code>. </p>
<p>This article shows you how to use <a href="https://blog.finxter.com/python-built-in-functions/" target="_blank" rel="noreferrer noopener" title="Python Built-In Functions">Python’s built-in</a> <code>compile()</code> method.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2020/12/compile-1-1024x576.jpg" alt="Python compile() built-in function -- Illustrated Explanation" class="wp-image-19392" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/compile-1-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<h2>Usage compile()</h2>
<p>Learn by example! Let’s say, you have a code file with the following content:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># some_code.py file
customers = {"Alice": 40000, "Bob": 33000}
print(f"Bob: {customers['Bob']}")</pre>
<p>Here’s an example on how to use the <code>compile()</code> <a href="https://blog.finxter.com/python-built-in-functions/">built-in function</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Read the code from the file or define it explicitly:
code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")' # Create the ast code object
code_obj = compile(code, '&lt;string>', 'exec') # Execute the ast code object
exec(code_obj) # Result:
# Bob: 33000</pre>
<p>First, you create the code as a string. Second, you pass the string into the <code>compile()</code> function to create an <code>ast</code> object. You can then pass this object into the <code><a href="https://blog.finxter.com/python-exec/" target="_blank" rel="noreferrer noopener" title="Python exec() — A Hacker’s Guide to A Dangerous Function">exec()</a></code> function and run it dynamically. </p>
<h2>Video compile()</h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="python built in compile" width="1400" height="788" src="https://www.youtube.com/embed/mkT7YB4oOkk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<h2>Why Using compile() Instead of exec() With a String?</h2>
<p>Before you dive into the syntax, you may not be motivated to use the <code>compile()</code> function in the first place. Why? Because you can also use the <code><a href="https://blog.finxter.com/python-exec/" target="_blank" rel="noreferrer noopener" title="Python exec() — A Hacker’s Guide to A Dangerous Function">exec()</a></code> function on a string instead of a code object. </p>
<p><a href="https://blog.finxter.com/python-exec/" target="_blank" rel="noreferrer noopener" title="Python exec() — A Hacker’s Guide to A Dangerous Function">Python’s <code>exec()</code> function</a> executes the Python code you pass as a string or executable object argument. This is called <em><strong>dynamic execution</strong></em> because, in contrast to normal <em>static </em>Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code.</p>
<p>Here’s the same code without <code>compile()</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")'
exec(code)
# Bob: 33000</pre>
<p>The output is the same. However, there are <strong>two advantages</strong> of using a code object:</p>
<ul>
<li><strong>You make it explicit. </strong>Without compiling the string explicitly, the <code>exec()</code> method would do the same work implicitly. Thus, if you need to run the same code multiple times, you can save significant compilation overhead by just compiling it once in advance.</li>
<li><strong>You can use the powerful <code>ast</code> object otherwise.</strong> This gives you access to some helper methods and additional information about the code such as the names defined in the code:</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> code_obj.co_names
('customers', 'print')</pre>
<p>You obtain this information from a compiled string which is very convenient!</p>
<h2>Syntax compile()</h2>
<p>You can use the <code>compile()</code> method with a number of different arguments. </p>
<pre class="wp-block-preformatted"><strong>Syntax: </strong>
<code><strong>compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)</strong></code><code> </code></pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Arguments</strong></td>
<td><code>source</code></td>
<td>A string to be compiled into a code object.</td>
</tr>
<tr>
<td></td>
<td><code>filename</code></td>
<td>The file from which the code given in source was read. If this was a string, use <code>'&lt;string&gt;'</code>. </td>
</tr>
<tr>
<td></td>
<td><code>mode</code></td>
<td>The execution mode—must be one of the following:<br /><code>'exec'</code> — If <code>source</code> is a sequence of statements<br /><code>'eval'</code> — If <code>source</code> is a single expression<br /><code>'single'</code> —If <code>source</code> is a single interactive statement</td>
</tr>
<tr>
<td></td>
<td>Optional: <code>flags=0</code></td>
<td>Controls which <a href="https://docs.python.org/3/library/ast.html#ast-compiler-flags" target="_blank" rel="noreferrer noopener" title="https://docs.python.org/3/library/ast.html#ast-compiler-flags">compiler options</a> should be activated and which future features should be allowed.</td>
</tr>
<tr>
<td></td>
<td>Optional: <code>dont_inherit=False</code></td>
<td>Do you want to avoid inheritance of the compiler options and future features?</td>
</tr>
<tr>
<td></td>
<td>Optional: <code>optimize=-1</code></td>
<td>Optimization level of the compiler:<br /><code>-1</code> selects the optimization level of the interpreter as given by <a href="https://docs.python.org/3/using/cmdline.html#cmdoption-o" target="_blank" rel="noreferrer noopener"><code>-O</code></a> options. <br /><code>0</code> selects no optimization and <code>__debug__</code> to <code>True</code>.<br /><code>1</code> specifies that asserts are removed and sets <code>__debug__</code> to <code>False</code>. <br /><code>2</code> additionally removes docstrings.</td>
</tr>
<tr>
<td><strong>Return Value</strong></td>
<td><code><a href="https://docs.python.org/3/library/ast.html#module-ast" target="_blank" rel="noreferrer noopener" title="https://docs.python.org/3/library/ast.html#module-ast">ast</a></code></td>
<td>Returns an AST object that represents the code as an abstract syntax tree.</td>
</tr>
</tbody>
</table>
</figure>
<h2>Interactive Shell Exercise: Understanding compile()</h2>
<p>Consider the following interactive code:</p>
<figure class="wp-block-image size-large"><a href="https://colab.research.google.com/drive/1f1EuRqeeVTq2yKrzeGvxwdNMzfuAZyim?usp=sharing" target="_blank" rel="noopener noreferrer"><img loading="lazy" width="650" height="275" src="https://blog.finxter.com/wp-content/uploads/2020/12/image-83.png" alt="" class="wp-image-19352" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/image-83.png 650w, https://blog.finxter.com/wp-content/uplo...00x127.png 300w, https://blog.finxter.com/wp-content/uplo...150x63.png 150w" sizes="(max-width: 650px) 100vw, 650px" /></a></figure>
<p><em><strong>Exercise</strong>: </em>Print the number associated to Alice in the dictionary!</p>
<hr class="wp-block-separator"/>
<p><strong>But before we move on, I’m excited to present you my brand-new Python book <a rel="noreferrer noopener" href="https://amzn.to/2WAYeJE" target="_blank" title="https://amzn.to/2WAYeJE">Python One-Liners</a></strong> (Amazon Link).</p>
<p>If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it’s also an <strong>introduction to computer science</strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!</em></strong></p>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img loading="lazy" width="215" height="283" src="https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png" alt="" class="wp-image-5969"/></a></figure>
</div>
<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>
<p>Link: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>
<hr class="wp-block-separator"/>
<h2>How to Read and Compile Code from a File</h2>
<p>Say, you have the following code in a code file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># filename.py file
customers = {"Alice": 40000, "Bob": 33000}
print("Bob: " + str(customers['Bob']))</pre>
<p><strong><em>How to read the code from a file, compile it, and execute it at runtime?</em></strong></p>
<p>Here’s the code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># 1. Read code from file
f = open('filename.py', 'r')
code = f.read()
f.close() # 2. Compile code string
code_obj = compile(code, 'filename.py', 'exec') # 3. Run the code object (ast)
exec(code_obj)</pre>
<p>Alternatively, you can also use the following <a href="https://blog.finxter.com/how-to-read-all-lines-of-a-file-in-a-python-one-liner/" title="How to Read All Lines of a File in a Python One-Liner?">one-liner to read the code from a file</a>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">code = open('filename.py').read()</pre>
<p>Frankly, I don’t see a huge problem with not closing the file if you have a small script and you don’t access the file anywhere else. Python will close it automatically after the code terminates.</p>
<h2>Summary</h2>
<p>The Python <code>compile()</code> method returns an executable code object as an “Abstract Syntax Tree” that is represented as an <code>ast</code> object. </p>
<p>There are many applications of an <code>ast</code> such as the following: You can pass this code object into the <code>exec()</code> or <code>eval()</code> functions and run it dynamically in your Python code. </p>
<p>This way, you can programmatically create source code and execute it at runtime. </p>
<p>To use the function, you must pass the string code to be executed, the filename, and the execution mode. </p>
<p>For example <code>compile('print("hi")', '&lt;string&gt;', 'exec')</code> would be a valid call that creates a code object consisting of one line <code>print("hi")</code>.</p>
<hr class="wp-block-separator"/>
<p>I hope you enjoyed the article! To improve your Python education, you may want to join the popular free <a href="https://blog.finxter.com/email-academy/" target="_blank" rel="noreferrer noopener" title="Email Academy">Finxter Email Academy</a>:</p>
<p>Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</p>
</p>
<p>The post <a href="https://blog.finxter.com/python-compile/" target="_blank" rel="noopener noreferrer">Python compile()</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/12/...n-compile/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016