Browse Source

Introspection, Virtual environments

pull/164/head
Jure Šorn 1 year ago
parent
commit
dbf5fdb905
2 changed files with 22 additions and 23 deletions
  1. 21
      README.md
  2. 24
      index.html

21
README.md

@ -2184,9 +2184,9 @@ first_element = op.methodcaller('pop', 0)(<list>)
Introspection
-------------
```python
<list> = dir() # Names of local variables (incl. functions).
<dict> = vars() # Dict of local variables. Also locals().
<dict> = globals() # Dict of global variables.
<list> = dir() # Names of local variables, functions, classes, etc.
<dict> = vars() # Dict of local variables, etc. Also locals().
<dict> = globals() # Dict of global vars, etc. (incl. '__builtins__').
```
### Attributes
@ -3537,15 +3537,16 @@ cdef class <class_name>:
cdef enum <enum_name>: <member_name>, <member_name>, ...
```
### PyInstaller
### Virtual Environments
**System for installing libraries directly into project's directory.**
```bash
$ pip3 install pyinstaller
$ pyinstaller script.py # Compiles into './dist/script' directory.
$ pyinstaller script.py --onefile # Compiles into './dist/script' console app.
$ pyinstaller script.py --windowed # Compiles into './dist/script' windowed app.
$ pyinstaller script.py --add-data '<path>:.' # Adds file to the root of the executable.
$ python3 -m venv <name> # Creates virtual environment in current directory.
$ source <name>/bin/activate # Activates venv. On Windows run `<name>\Scripts\activate`.
$ pip3 install <library> # Installs the library into active environment.
$ python3 <path> # Runs the script in active environment. Also `./<path>`.
$ deactivate # Deactivates virtual environment.
```
* **File paths need to be updated to `'os.path.join(sys._MEIPASS, <path>)'`.**
### Basic Script Template
```python

24
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>July 31, 2023</aside>
<aside>August 1, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1805,9 +1805,9 @@ first_element = op.methodcaller(<span class="hljs-string">'pop'</span>, <span
<li><strong>Bitwise operators require objects to have and(), or() and xor() special methods, unlike logical operators that work on all types of objects.</strong></li>
<li><strong>Also: <code class="python hljs"><span class="hljs-string">'&lt;bool&gt; = &lt;bool&gt; &amp;|^ &lt;bool&gt;'</span></code> and <code class="python hljs"><span class="hljs-string">'&lt;int&gt; = &lt;bool&gt; &amp;|^ &lt;int&gt;'</span></code>.</strong></li>
</ul>
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of local variables (incl. functions).</span>
&lt;dict&gt; = vars() <span class="hljs-comment"># Dict of local variables. Also locals().</span>
&lt;dict&gt; = globals() <span class="hljs-comment"># Dict of global variables.</span>
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of local variables, functions, classes, etc.</span>
&lt;dict&gt; = vars() <span class="hljs-comment"># Dict of local variables, etc. Also locals().</span>
&lt;dict&gt; = globals() <span class="hljs-comment"># Dict of global vars, etc. (incl. '__builtins__').</span>
</code></pre></div>
<div><h3 id="attributes">Attributes</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir(&lt;object&gt;) <span class="hljs-comment"># Names of object's attributes (incl. methods).</span>
@ -2886,16 +2886,14 @@ cdef &lt;ctype/void&gt; &lt;func_name&gt;(&lt;ctype&gt; &lt;arg_name&gt;): ...
</code></pre>
<pre><code class="python language-python hljs">cdef enum &lt;enum_name&gt;: &lt;member_name&gt;, &lt;member_name&gt;, ...
</code></pre>
<div><h3 id="pyinstaller">PyInstaller</h3><pre><code class="bash language-bash hljs">$ pip3 install pyinstaller
$ pyinstaller script.py <span class="hljs-comment"># Compiles into './dist/script' directory.</span>
$ pyinstaller script.py --onefile <span class="hljs-comment"># Compiles into './dist/script' console app.</span>
$ pyinstaller script.py --windowed <span class="hljs-comment"># Compiles into './dist/script' windowed app.</span>
$ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment"># Adds file to the root of the executable.</span>
<div><h3 id="virtualenvironments">Virtual Environments</h3><p><strong>System for installing libraries directly into project's directory.</strong></p><pre><code class="bash language-bash hljs">$ python3 -m venv &lt;name&gt; <span class="hljs-comment"># Creates virtual environment in current directory.</span>
$ <span class="hljs-built_in">source</span> &lt;name&gt;/bin/activate <span class="hljs-comment"># Activates venv. On Windows run `&lt;name&gt;\Scripts\activate`.</span>
$ pip3 install &lt;library&gt; <span class="hljs-comment"># Installs the library into active environment.</span>
$ python3 &lt;path&gt; <span class="hljs-comment"># Runs the script in active environment. Also `./&lt;path&gt;`.</span>
$ deactivate <span class="hljs-comment"># Deactivates virtual environment.</span>
</code></pre></div>
<ul>
<li><strong>File paths need to be updated to <code class="python hljs"><span class="hljs-string">'os.path.join(sys._MEIPASS, &lt;path&gt;)'</span></code>.</strong></li>
</ul>
<div><h3 id="basicscripttemplate">Basic Script Template</h3><pre><code class="python language-python hljs"><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-comment">#</span>
<span class="hljs-comment"># Usage: .py</span>
@ -2932,7 +2930,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>July 31, 2023</aside>
<aside>August 1, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save