no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
fuss:python [2023/11/26 15:11] (current) office
Line 1: Line 1:
 +====== Serve Current Directory over HTTP ======
  
 +The following command:
 +<code python>
 +python -m SimpleHTTPServer 8181
 +</code>
 +will serve the current directory where the command is executed by starting a HTTP server on port ''8181''.
 +
 +====== Reverse List ======
 +
 +The ''wasListReverse'' will reverse a list containing elements of any type.
 +
 +<code python>
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      ##
 +###########################################################################
 +def wasListReverse (l):
 +    if len(l) <= 1:
 +        return l
 +    return listReverse(l[1:]) + [ l[0] ]
 +</code>
 +
 +Example call:
 +<code python>
 +i = ["sdf", 11, 22, 39]
 +print listReverse(i)
 +</code>
 +
 +Output:
 +<code>
 +[39, 22, 11, 'sdf']
 +</code>
 +
 +===== Derivations =====
 +
 +Using the programming language's features, we can derive different forms of ''wasListReverse''.
 +
 +==== Using the Ternary Operator ====
 +
 +The ''listReverseTern'' function makes use of the ternary operator (introduced in Python 2.5) in order to get rid of the conditional in the ''wasListReverse'' function:
 +
 +<code python>
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      ##
 +###########################################################################
 +def listReverseTern (l):
 +    return l if len(l) <= 1 else listReverseTern(l[1:]) + [ l[0] ]
 +</code>
 +
 +
 +==== Using Lambda Expressions ====
 +
 +The ''listReverse'' lambda expression will leverage the ternary operator from the ''listReverseTern'' function and trim down the function to a single line:
 +<code python>
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      ##
 +###########################################################################
 +
 +wasListReverseLambda = lambda x : x if len(x) <= 1 else wasListReverseLambda(x[1:]) + [ x[0] ]
 +</code>
 +
 +Example call:
 +<code python>
 +wasListReverseLambda = lambda x : x if len(x) <= 1 else wasListReverseLambda(x[1:]) + [ x[0] ]
 +
 +print wasListReverseLambda(["sdf", 11, 22, 39])
 +</code>
 +
 +Output:
 +<code>
 +[39, 22, 11, 'sdf']
 +</code>
 +
 +====== Y-Combinator ======
 +
 +By expressing the $Y$ combinator using the $\omega$ combinator [[fuss/lambda_calculus#Implementing the Y-Combinator|as explained]], the $Y$ combinator can be implemented in Python.
 +
 +<code python>
 +#!/usr/bin/python
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      ##
 +###########################################################################
 +
 +w = lambda x : x( x );
 +Y = lambda y : w( lambda f : y( lambda x : w(f)(x) ) );
 +</code>
 +
 +An example call, to calculate the factorial of $10$, would be:
 +
 +<code python>
 +Factorial = Y( lambda f : lambda n : 1 if n <= 1 else n * f(n - 1) );
 +print Factorial(10);
 +</code>
 +
 +====== Only in (Monty) Python ======
 +
 +<code>
 +TypeError: dump() takes at least 2 arguments (2 given)
 +</code>
 +====== Memory Error When Installing Packages with Pip ======
 +
 +In case packages cannot be installed and a ''MemoryError'' exception is thrown, then most likely it is due to pip attempting to load packages in memory. To solve the issue, run pip with the additional ''--no-cache-dir'' parameter.
 +
 +====== Installing Python Applications without Breaking the Environment ======
 +
 +Linux distributions that contain python packages as part of the distribution packaging systems cannot really use ''pip'' to install packages due to the files installed by ''pip'' conflicting with the files from the distribution package. Typically this results in an error along the lines of "error: externally-managed-environment" or "This environment is externally managed" that suggests the user to add the ''--break-system-packages'' flag if the user wants to proceed anyway.
 +
 +An alternative is to use ''pipx'':
 +<code bash>
 +aptitude install pipx
 +</code>
 +
 +and then to install the package using ''pipx'':
 +<code bash>
 +pipx install unmanic
 +</code>
 +where:
 +  * ''unmanic'' is the package to be installed
 +
 +''pipx'' creates a separate virtual environment containing python along with all the requirements for the package to be installed thereby preventing conflicts with distribution files.
 +
 +One trick is to specify ''PIPX_HOME'' and ''PIPX_BIN_DIR'' environment variables in order to make ''pipx'' install the application to a specific directory that will become the application directory instead of the application being installed in the same virtual environment as all other applications installed with ''pipx''
 +
 +For example, the commands:
 +<code bash>
 +export PIPX_HOME=/opt/unmanic
 +export PIPX_BIN_DIR=/opt/unmanic/bin
 +pipx install unmanic
 +</code>
 +
 +will install the ''unmanic'' application by creating a virtual environment starting from the filesystem path ''/opt/unmanic'' and by placing the ''unmanic'' binary in ''/opt/unmanic/bin''.
 +
 +Now, in order to install a different application, simply change the ''PIPX_HOME'' and ''PIPX_BIN_DIR'' environment variables.

fuss/python.txt · Last modified: 2023/11/26 15:11 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.