This is an interesting figure that shows the entire process of bash initialization.
Autocomplete with Trie Tree -- code in python
11 years ago
python showargs.py a b c d e
mkdir -p ${HOME}/opt/lib/python2.4/site-packages/ echo "PYTHONPATH=\$PYTHONPATH:\${HOME}/opt/lib/python2.4/site-packages/" >> ~/.bashrc echo "export PYTHONPATH" >> ~/.bashrc echo "export PATH=\$PATH:\${HOME}/opt/bin" >> ~/.bashrc source ~/.bashrc easy_install --prefix=${HOME}/opt MySQL-python
path
attribute of the sys
module. Since path
is a list, you can use the append
method to add new directories to the path./home/me/mypy
to the path, just do:
import sys
sys.path.append("/home/me/mypy")
sys.path.insert(0 , "path") #such that python will search it first. export PYTHONPATH=$PYTHONPATH:$HOME/lib/python:$HOME/lib/misc
mydir/spam/__init__.py mydir/spam/module.py
import spam.module
from spam import module
%s Represents a value as a string
%i
Integer %d
Decimal integer %u
Unsigned integer%o
Octal integer%x/%X Hexadecimal integer
%e/%E
Float exponent%f/%F Floa
%C
ASCII character Monica = { "Occupation": "Chef", "Name" : "Monica", "Dating" : "Chandler", "Income" : 40000 }
"%(Name)s %(Income)d" % Monica '40000'
More: http://www.informit.com/articles/article.aspx?p=28790&seqNum=2
Python Expression | Comment |
---|---|
for item in s | iterate over the items of s |
for item in sorted(s) | iterate over the items of s in order |
for item in set(s) | iterate over unique elements of s |
for item in reversed(s) | iterate over elements of s in reverse |
for item in set(s).difference(t) | iterate over elements of s not in t |
for item in random.shuffle(s) | iterate over elements of s in random order |
import httplibimport xml.dom.minidom
data = """
0 " ignoredups="0" ignoredigits="1" ignoreallcaps="1">
%s
- import timeit
- import random
- def generate(num):
- while num:
- yield random.randrange(10)
- num -= 1
- def create_list(num):
- numbers = []
- while num:
- numbers.append(random.randrange(10))
- num -= 1
- return numbers
- print(timeit.timeit("sum(generate(999))", setup="from __main__ import generate", number=1000))
- >>> 0.88098192215 #Python 2.7
- >>> 1.416813850402832 #Python 3.2
- print(timeit.timeit("sum(create_list(999))", setup="from __main__ import create_list", number=1000))
- >>> 0.924163103104 #Python 2.7
- >>> 1.5026731491088867 #Python 3.2
- import timeit
- from ctypes import cdll
- def generate_c(num):
- #Load standard C library
- libc = cdll.LoadLibrary("libc.so.6") #Linux
- #libc = cdll.msvcrt #Windows
- while num:
- yield libc.rand() % 10
- num -= 1
- print(timeit.timeit("sum(generate_c(999))", setup="from __main__ import generate_c", number=1000))
- >>> 0.434374809265 #Python 2.7
- >>> 0.7084300518035889 #Python 3.2
sudo pip install cython
- #cython_generator.pyx
- import random
- def generate(num):
- while num:
- yield random.randrange(10)
- num -= 1
- from distutils.core import setup
- from distutils.extension import Extension
- from Cython.Distutils import build_ext
- setup(
- cmdclass = {'build_ext': build_ext},
- ext_modules = [Extension("generator", ["cython_generator.pyx"])]
- )
python setup.py build_ext --inplace
- import timeit
- print(timeit.timeit("sum(generator.generate(999))", setup="import generator", number=1000))
- >>> 0.835658073425
- #cython_generator.pyx
- cdef extern from "stdlib.h":
- int c_libc_rand "rand"()
- def generate(int num):
- while num:
- yield c_libc_rand() % 10
- num -= 1
>>> 0.033586025238
- import timeit
- import random
- def generate(num):
- while num:
- yield random.randrange(10)
- num -= 1
- def create_list(num):
- numbers = []
- while num:
- numbers.append(random.randrange(10))
- num -= 1
- return numbers
- print(timeit.timeit("sum(generate(999))", setup="from __main__ import generate", number=1000))
- >>> 0.115154981613 #PyPy 1.9
- >>> 0.118431091309 #PyPy 2.0b1
- print(timeit.timeit("sum(create_list(999))", setup="from __main__ import create_list", number=1000))
- >>> 0.140175104141 #PyPy 1.9
- >>> 0.140514850616 #PyPy 2.0b1
- /* functions.c */
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- /* http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#C */
- inline
- void merge(int *left, int l_len, int *right, int r_len, int *out)
- {
- int i, j, k;
- for (i = j = k = 0; i < l_len && j < r_len; )
- out[k++] = left[i] < right[j] ? left[i++] : right[j++];
- while (i < l_len) out[k++] = left[i++];
- while (j < r_len) out[k++] = right[j++];
- }
- /* inner recursion of merge sort */
- void recur(int *buf, int *tmp, int len)
- {
- int l = len / 2;
- if (len <= 1) return;
- /* note that buf and tmp are swapped */
- recur(tmp, buf, l);
- recur(tmp + l, buf + l, len - l);
- merge(tmp, l, tmp + l, len - l, buf);
- }
- /* preparation work before recursion */
- void merge_sort(int *buf, int len)
- {
- /* call alloc, copy and free only once */
- int *tmp = malloc(sizeof(int) * len);
- memcpy(tmp, buf, sizeof(int) * len);
- recur(buf, tmp, len);
- free(tmp);
- }
- int fibRec(int n){
- if(n < 2)
- return n;
- else
- return fibRec(n-1) + fibRec(n-2);
- }
gcc -Wall -fPIC -c functions.c gcc -shared -o libfunctions.so functions.o
- #functions.py
- from ctypes import *
- import time
- libfunctions = cdll.LoadLibrary("./libfunctions.so")
- def fibRec(n):
- if n < 2:
- return n
- else:
- return fibRec(n-1) + fibRec(n-2)
- start = time.time()
- fibRec(32)
- finish = time.time()
- print("Python: " + str(finish - start))
- #C Fibonacci
- start = time.time()
- x = libfunctions.fibRec(32)
- finish = time.time()
- print("C: " + str(finish - start))
Python: 1.18783187866 #Python 2.7 Python: 1.272292137145996 #Python 3.2 Python: 0.563600063324 #PyPy 1.9 Python: 0.567229032516 #PyPy 2.0b1 C: 0.043830871582 #Python 2.7 + ctypes C: 0.04574108123779297 #Python 3.2 + ctypes C: 0.0481240749359 #PyPy 1.9 + ctypes C: 0.046403169632 #PyPy 2.0b1 + ctypes
- #Python Merge Sort
- from random import shuffle, sample
- #Generate 9999 random numbers between 0 and 100000
- numbers = sample(range(100000), 9999)
- shuffle(numbers)
- c_numbers = (c_int * len(numbers))(*numbers)
- from heapq import merge
- def merge_sort(m):
- if len(m) <= 1:
- return m
- middle = len(m) // 2
- left = m[:middle]
- right = m[middle:]
- left = merge_sort(left)
- right = merge_sort(right)
- return list(merge(left, right))
- start = time.time()
- numbers = merge_sort(numbers)
- finish = time.time()
- print("Python: " + str(finish - start))
- #C Merge Sort
- start = time.time()
- libfunctions.merge_sort(byref(c_numbers), len(numbers))
- finish = time.time()
- print("C: " + str(finish - start))
Python: 0.190635919571 #Python 2.7 Python: 0.11785483360290527 #Python 3.2 Python: 0.266992092133 #PyPy 1.9 Python: 0.265724897385 #PyPy 2.0b1 C: 0.00201296806335 #Python 2.7 + ctypes C: 0.0019741058349609375 #Python 3.2 + ctypes C: 0.0029308795929 #PyPy 1.9 + ctypes C: 0.00287103652954 #PyPy 2.0b1 + ctypes
Merge Sort | Fibonacci | |
---|---|---|
Python 2.7 | 0.191 | 1.187 |
Python 2.7 + ctypes | 0.002 | 0.044 |
Python 3.2 | 0.118 | 1.272 |
Python 3.2 + ctypes | 0.002 | 0.046 |
PyPy 1.9 | 0.267 | 0.564 |
PyPy 1.9 + ctypes | 0.003 | 0.048 |
PyPy 2.0b1 | 0.266 | 0.567 |
PyPy 2.0b1 + ctypes | 0.003 | 0.046 |