maj 2012
2 posty
Colors in Git
$ git config --global color.ui true
maj 8
1 tag
How to send POST with curl?
$ curl -d "param1=value1&param2=value2" http://example.com/resource.cgi $ curl -F "fileupload=@filename.txt" http://example.com/resource.cgi For a RESTful HTTP POST containing XML or JSON, use this: $ curl -X POST -d @filename http://example.com/path/to/resource
maj 6
kwiecień 2012
4 posty
2 tagi
First things you need to do after Ubuntu...
First things you need to do after Ubuntu installation: - sudo apt-get install compizconfig-settings-manager (turn on panel autohide and change icons size) - sudo apt-get install vim - sudo apt-get install pysdm (partitions automount; Storage Device Manager) Write your own and extend this list ;)
Kwiec. 28
7 tagów
MemoryError and numpy / matplotlib
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 More info: http://docs.fedoraproject.org/en-US/Fedora/13/html/Managing_Confined_Services/sect-Managing_Confined_Services-The_Apache_HTTP_Server-Booleans.html
Kwiec. 26
2 tagi
Problem with eAccelerator and Doctrine 2
When you see message “Class X is not a valid entity or mapped super class” you can solve this problem with adding two lines to the .htaccess file: php_flag eaccelerator.enable 0 php_flag eaccelerator.optimizer 0 There is also article which describes problem mentioned above.
Kwiec. 25
3 tagi
How to set/change host in Symfony 2 command?
$host = $this->getContainer()->getParameter('host'); $this->getContainer()->get('router')->getContext()->setHost($host); $this->getContainer()->get('router')->getContext()->setScheme('https'); $this->getContainer()->get('router')->getContext()->setBaseUrl('/web');
Kwiec. 5
luty 2012
2 posty
1 tag
scp dir / file with spaces
scp user@host:'"/home/user/file with spaces"' .
Luty 21
1 tag
Python SMTP Debugging Server
python -m smtpd -n -c DebuggingServer localhost:1025
Luty 7
styczeń 2012
35 postów
Google chart API (images) doesn’t allow negative values for line chart. Is it a joke?
Stycz. 30
2 tagi
Adding a startup script in Ubuntu / Debian
Create script in /etc/init.d: case "$1" in start) #something here ;; stop) killproc -TERM /usr/local/bin/noip2 ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 Add script to rc.d: update-rc.d script defaults
Stycz. 30
4 notki
2 tagi
Using INSERT IGNORE with MySQL to prevent...
INSERT IGNORE INTO mytable (primaryKey, field1, field2) VALUES ('abc', 1, 2), ('def', 3, 4), ('ghi', 5, 6);
Stycz. 22
34 notki
2 tagi
Colorized tail
tailcolor() { tail -f -n5000 $1 | perl -pe "s/$2/\e[1;31;43m$&\e[0m/g" }
Stycz. 22
5 notek
2 tagi
Display available memory on Linux / Ubuntu
cat /proc/meminfo
Stycz. 22
1 notka
2 tagi
Compare Directories using Diff in Linux
Use this command: diff -qr --exclude=.svn dir1 dir2|sort -q = print result only where files are different -r = recursive —exclude = use it if something has to be excluded
Stycz. 22
1 tag
How to trim non-breaking space in PHP
Use this snippet: $trim = trim($text, " \xC2\xA0\n\t\r\0\x0B"); \xC2\xA0 is non-breaking space (alt+space in mac os x)
Stycz. 22
3 notki
2 tagi
How to create *.tar.gz and *.tar.bz2 files
tar.gz: $ tar -zcvf archive_name.tar.gz directory_to_compress tar.bz2: $ tar -jcvf archive_name.tar.bz2 directory_to_compress
Stycz. 22
2 tagi
How to exclude dir in grep
Add parameter —exclude-dir to the grep command: grep -R --exclude-dir=example_dir "something" *
Stycz. 22
2 tagi
How To Clone or Copy a VirtualBox Virtual Disk
VBoxManage clonehd "DiskToClone.vdi" "ClonedDisk.vdi" or VBoxManage internalcommands setvdiuuid "CopiedDisk.vdi"
Stycz. 22
6 notek
Terminal colors in Python
It depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here is some python code: class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' def disable(self): self.HEADER = '' self.OKBLUE = '' ...
Stycz. 22
1 tag
Python pretty print
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, { ...
Stycz. 22
4 notki
2 tagi
Tab completion in Python shell
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"
Stycz. 22
1 notka
1 tag
How to generate random string in Python
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.
Stycz. 22
1 tag
Fibonacci in Python
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
Stycz. 22
2 tagi
MySQL Error number 150
I had a little problem with SqlAlchemy and foreign keys. I found in logs information about MySQL errno 150. Below are some steps to rapair it ;) mysql> SHOW ENGINE INNODB STATUS ; ------------------------ LATEST FOREIGN KEY ERROR ------------------------ 090506 11:57:34 Error in foreign key constraint of table some_table/t23_aluno: there is no index in the table which would contain the...
Stycz. 22
6 notek
3 tagi
How to remove all .pyc files from a project
Command: find . -name '*.pyc' -delete
Stycz. 22
3 tagi
How to exlude .svn folders within git
Simple solution: touch .gitignore add to .gitignore these lines: - /.svn/ - */.svn/* git add .gitignore git commit That is all :)
Stycz. 22
4 notki
1 tag
Quicksort in Python
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) +...
Stycz. 22
3 tagi
SSH Tunneling
Just use this command: ssh -f -L local_port:remote_host:remote_port user@remote_host -N
Stycz. 22
1 notka
2 tagi
MySQL time date difference in Minutes
SELECT TIMESTAMPDIFF(MINUTE, pubdate, now()), id, message from message where (TIMESTAMPDIFF(MINUTE, pubdate, now())
Stycz. 22
3 notki
1 tag
Turn off deprecated warnings
Deprecated: Function split() is deprecated in deprecated.php on line 9 Simply turn off these warnings in your php.ini 1.error_reporting = E_ALL & ~E_DEPRECATED Or during runtime 1.error_reporting(E_ALL & ~E_DEPRECATED); Following function will throw this warning: - call_user_method_array() instead use call_user_func_array() - define_syslog_variables() - dl() - ereg_replace() instead...
Stycz. 22
1 notka
3 tagi
Dynamically parameters in Ajax.Autocompleter
Two days ago I had a problem with Ajax.Autocompleter (from script.aculo.us library). I needed to use dynamically parameters sending in request to the server. Basically they are created only statically when class is set up. To change this behaviour use this solution: new Ajax.Autocompleter( 'search', 'autocomplete_choices', 'index.php?controller=search&action=autocomplete, { ...
Stycz. 22
10 notek
1 tag
How to capitalize UTF-8 string in PHP?
Sometimes I have to capitalize words in my pages. The problem is these words are encoded in UTF-8. Simple solution to resolve it: $word = 'kędzierzyn-koźle'; $word = mb_convert_case($word, MB_CASE_TITLE, 'UTF-8');
Stycz. 22
1 notka
2 tagi
How to run screen as daemon?
Use this command: $ screen -dmS screen_name command
Stycz. 22
2 tagi
Python __iter__ performance
$ 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.
Stycz. 22
2 tagi
How to replace text in many files?
perl -pi -w -e 's/source_text/destination_text/g;' *.php
Stycz. 22
2 tagi
Tip: Listing only directories using ls and grep
Just use small snippet: $ ls -l | grep "^d"
Stycz. 22
4 tagi
Solution to enable apache mod_rewrite under Ubuntu
If mod rewrite is not working in your apache server under Ubuntu: Invalid command ‘RewriteEngine’, perhaps mis-spelled or defined by a module not included in the server configuration You have to run the following command in a terminal: sudo a2enmod rewrite
Stycz. 22
6 notek
2 tagi
Checking key in dictionary - performance...
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
Stycz. 22
4 notki
2 tagi
How to write and read array from cookie in Pylons
How to write and read array from cookies ? The easiest way to write an array in cookie is serialize it using pickle module and quote returned string with urllib module. For example: import pickle, urllib cookie = [] response.set_cookie('some_cookie', urllib.quote(pickle.dumps(cookie))) And getting an array from cookie: cookie =...
Stycz. 22
1 notka
2 tagi
The easiest way to modify response headers in...
Remember the trick! The easiest way to modify response headers in Pylons is add this code just before render() function.: response.headerlist = [('Content-type', 'image/png')]
Stycz. 22
1 notka
2 tagi
Simple solution - how to use own decorators in...
This is a simple solution to create own decorator function for any action in Pylons. First, you have to import some functions: from decorator import decorator from pylons.decorators.util import get_pylons # to get request environment Now create decorator function, for example myowndecorator: def myowndecorator(): def wrapper(func, *args, **kwargs): request =...
Stycz. 22
2 tagi
Basic Authentication in Pylons
I had to create basic authentication in my project (based on Pylons), and this is simple solution to do it. First, you have to open your config/middleware.py. It’s necessary to import AuthBasicHandler: from paste.auth.basic import AuthBasicHandler Next step is find: app = PylonsApp() Change to: app = PylonsApp() app = AuthBasicHandler(app, "My realm", authfunc). Of course you...
Stycz. 22
Hello world!
From today any article with code will be published here. I will also move all articles from snipeworld.com to here. I hope it should help me with writing more code, code snippets, etc ;) This place will be an area for my stupid (or not) ideas, opinions about web technologies, IT use cases, videos - everything what is linked with IT. You can not agree with my opinions but I will write here...
Stycz. 22