Fastest Factorial Function in Python


from functools import reduce
from operator import mul

def fastest_factorial(n):
     return reduce(mul, range(1, n+1), 1)

Tutorial: virtualenvwrapper

0) Make sure you have pip & virtualenv

sudo easy_install pip
sudo pip install virtualenv

1) Install virtualenvwrapper

sudo pip install virtualenvwrapper

2) Add the following lines in your shell startup file (.bashrc or .profile file).

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
### or ###
source /usr/bin/virtualenvwrapper.sh
### if you installed virtualenvwrapper in that directory
### you can use sudo find / -name virtualenvwrapper.sh
### to locate virtualenvwrapper if you installed it somewhere else

Tutorial: pyenv

How to Install pyenv

Manually:

Replace .bashrc to .bash_profile if that is the configuration file you are using.
cd
git clone git://github.com/yyuu/pyenv.git .pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

Automatic:

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

Walk-through

~$ pyenv global
system
~$ pyenv versions
* system (set by /home/user/.pyenv/version)
'pyenv global' shows you what python version is set as the default python version. 'pyenv versions'
shows you all available python versions.
To install a new version:
~$ pyenv install 3.4.0
Installing readline-6.3...
Installed readline-6.3 to /home/user/.pyenv/versions/3.4.0

Installing Python-3.4.0...
Installed Python-3.4.0 to /home/user/.pyenv/versions/3.4.0
To show all available Python versions you can install:
~$ pyenv install --list
~$ pyenv versions
* system (set by /home/user/.pyenv/version)
  3.4.0
pyenv now lists two python versions. To use python 3.4 as the default version:
pyenv global 3.4.0
You can also use pyenv to define a project-specific, or local, version of Python:
~$ pyenv global system

~$ mkdir cuting_edge
~$ cd cuting_edge/
~/cutting_edge$ pyenv local 3.4.0
~/cutting_edge$ python -V
Python 3.4.0
~/cutting_edge$ cd ..
~$ python -V
Python 2.7.6

Virtual Environments

To other virtualenv users, the idea of a local Python might seem familiar. Indeed, a local Python created from pyenv is almost like a Python virtual environment. The main difference is that pyenv actually copies an entire Python installation every time you create a new pyenv version. In contrast, virtualenv makes use of symbolic links to decrease the size of the virtualenv’s. If you can’t function without your virtual environments anymore then fear not, for there is a plugin for that:pyenv-virtualenv. This plugin adds complete virtualenv functionality to pyenv:
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
source ~/.bashrc

~$ mkdir virtual_env
~$ cd virtual_env/
~/virtual_env$ pyenv virtualenv 3.4.0 venv
Ignoring indexes: https://pypi.python.org/simple/
Requirement already satisfied (use --upgrade to upgrade): setuptools in /home/user/.pyenv/versions/venv/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /home/user/.pyenv/versions/venv/lib/python3.4/site-packages
Cleaning up...

~/virtual_env$ pyenv versions
* system (set by /home/user/.pyenv/version)
  3.4.0
  lab_web
  venv
If you want to create a virtualenv from the system Python, then virtualenv needs to be installed at the system level as well.
~/virtual_env$ pyenv activate venv
(venv) ~/virtual_env$ python -V
Python 3.4.0
(venv) ~/virtual_env$ pip list
pip (1.5.4)
setuptools (2.1)
(venv) ~/virtual_env$ pyenv deactivate
~/virtual_env
This last command is the recommended way to deactivate the virtualenv. This ensures that pyenv remains working as normal after you leave the virtualenv.
pyenv’s magic works because it actually redefines your Python command:
~$ which python
/home/user/.pyenv/shims/python
When you try to run Python, it first looks for a .python-version in the current directory to decide which version of python to run. If it doesn’t find this file, then it looks for the user-level file ~/.pyenv/version.
And this is essentially all there is to it. Have fun developing Python code in a safe, environment-friendly way.

Variables & Functions Accessible inside Tornado Templates

Function Result
_ An alias for the locale.translate() function.
current_user The current_user object as returned by RequestHandler.get_current_user()
datetime The datetime module. Example: {{ datetime.date.today().year }} returns the current year
escape A function that escapes a string so it is valid within XML or XHTML.
handler The request handler that called the self.render function to process the template. Example: {{handler.static_url('foo')}} will run the static_url function in RequestHandler returning a full URL path, pre-pending static_url.
json_encode A function that JSON-encodes the given Python object.
locale The locale value as returned by RequestHandler.get_user_locale().
request The request object that is passed into the Request Handler.
reverse_url Given a full module and class (myapp.Homepage) it will look at the handler to URI mapping and return the URL for the given class.
squeeze A function that replaces all sequences of whitespace chars with a single space.
static_url The value of the static_url property passed in to the application settings.
url_escape A function that returns a valid URL-encoded version of the given value.
xsrf_form_html If using the cross-site forgery protection, this function returns the hidden input field containing the xrsf variable.
 

About

Search

PISIKA Copyright © 2009