If you see MemoryError in your application running within Apache (in my case with wsgi [mod_wsgi]) it is probably because of SELinux.
You have to allow httpd to execute code from the /tmp directory
root # setsebool httpd_tmp_exec on
If you see MemoryError in your application running within Apache (in my case with wsgi [mod_wsgi]) it is probably because of SELinux.
You have to allow httpd to execute code from the /tmp directory
root # setsebool httpd_tmp_exec on
Simply “print” show objects in ugly format. If you want to see object nice formatted use instruction below:
from pprint import pprint
data = data = [(1, {'a':'A', 'b':'B', 'c':'C', 'd':'D'}), (2, {'e':'E', 'f':'F', 'g':'G', 'h':'H', 'i':'I', 'j':'J', 'k':'K', 'l':'L'})]
pprint(data, indent=4, depth=3)
[ (1, { 'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
( 2,
{ 'e': 'E',
'f': 'F',
'g': 'G',
'h': 'H',
'i': 'I',
'j': 'J',
'k': 'K',
'l': 'L'})]
The amount of indentation added for each recursive level is specified by indent; the default is one.
The number of levels which may be printed is controlled by depth. By default, there is no constraint on the depth of the objects being formatted.
1. Create .pythonrc in your HOME directory
2. Add these lines to the previously created file:
import rlcompleter, readline
readline.parse_and_bind("tab: complete")
3. Export PYTHONSTARTUP variable in .bashrc file:
export PYTHONSTARTUP="$HOME/.pythonrc"
Simple:
import random, string ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
where N is a number of characters used in a string.
Just to remember ;)
1. 0: n = 0 2. 1: n = 1 3. F(n-1) + F(n-2): n > 1
def fib(n):
if n Just to remember ;)
Step 1: Get first element from a list
Step 2: Split the list into two parts
Step 3: Return result recursively
Step 4: Add conditions: List is not sorted if count of items in args list is greater than 1
def quick(args):
if len(args) > 1:
first = args[0]
del args[0]
left = [x for x in args if x = first]
return quick(left) + [first] + quick(right)
else:
return args
$ python -m timeit -r 1000 -s "bla=[1,2,3]" "for item in bla:" " item" 1000000 loops, best of 1000: 0.306 usec per loop
$ python -m timeit -r 1000 -s "bla=[1,2,3]" "lista=bla.__iter__()" "while True:" " try:" " lista.next()" " except:" " break" 100000 loops, best of 1000: 3.13 usec per loop
__iter__ + try..except + while is round 11x slower than “for in” loop.
Little experiment:
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "d.has_key('a')"
1000000 loops, best of 100: 0.361 usec per loop
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "'a' in d"
10000000 loops, best of 100: 0.159 usec per loop
Checking if dictionary has key using “in” is faster than using has_key() method.
Tested in Python 2.6