http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
Recent Updates Page 2 Toggle Comment Threads | Keyboard Shortcuts
-
Mads Madsen
-
Mads Madsen
One-liner search and replace
1katy:~ mm$ perl -pi.bak -e "s/search/replace/g" file.sql -
Mads Madsen
Install old subversion on Mac OS X with homebrew (E155036)
Today I got a new macbook and I ran into a problem because the subversion installed on the laptop was too new for our subversion system.
Error message:
1svn: E155036: Working copy '/Users/mm/some/repository' is too old (format 10, created by Subversion 1.6)So I had install an older version of subversion, here is how I did it.
Tap homebrew versions.
12345678910katy:~ mm$ brew tap homebrew/versionsCloning into '/usr/local/Library/Taps/homebrew-versions'...remote: Reusing existing pack: 1654, done.remote: Counting objects: 7, done.remote: Compressing objects: 100% (7/7), done.remote: Total 1661 (delta 4), reused 0 (delta 0)Receiving objects: 100% (1661/1661), 544.38 KiB | 457.00 KiB/s, done.Resolving deltas: 100% (864/864), done.Checking connectivity... doneTapped 130 formulaInstall subversion16
1katy:~ mm$ brew install subversion16Enjoy!
-
Mads Madsen
Change a user’s UID
First login as root – Clean login – sudo will not work if you’re changing a UID on an already logged in user.
Find the user’s old UID:
1id user1uid=1000(user1) ….
Change the UID:
1usermod -u 1001 user1Update files and directories owned by the user to the new UID:
1find / -uid 1000 -exec chown -h 1001 '{}' \+Reboot and the UID has been changed.
-
Mads Madsen
Reverse scroll direction on Windows 8
Powershell commands:
1234567891011# View registry settingsGet-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Enum\HID\*\*\Device` Parameters FlipFlopWheel -EA 0# Change registry settings# Reverse mouse wheel scroll FlipFlopWheel = 1# Normal mouse wheel scroll FlipFlopWheel = 0Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Enum\HID\*\*\Device` Parameters FlipFlopWheel -EA 0 | ForEach-Object { Set-ItemProperty $_.PSPath FlipFlopWheel 1 }# The command for normal (non-inverted) scrolling has the `0` and `1` swapped:# Restore default scroll directionGet-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Enum\HID\*\*\Device` Parameters FlipFlopWheel -EA 1 | ForEach-Object { Set-ItemProperty $_.PSPath FlipFlopWheel 0 }Switching around from Windows and Mac OS x can be a bit confusing, so I decided to reverse the scroll direction on my Windows machines, so they would feel the same as under Mac OS x.
Here is how I did it:- Find the Hardware ID for your mouse
- Go to the mouse control panel
- Select “Hardware” tab
- Click “Properties” button
- Select “Details” tab
- From the drop-down list choose “Hardware IDs”
- Save the VID*** entry ( e.g. VID_045E&PID_0039 )
- Find and change the corresponding configuration settings in the registry
- Run regedit.exe (With administrator rights)
- Navigate to the Key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\HID
- Find the Hardware ID entry of your mouse
- Under all subkeys of the Hardware ID key look for the “DeviceParameters” key and change the “FlipFlopWheel” value from 0 to 1
- Making it work:
- Unplug your mouse
- Wait a few moments
- Plug in your mouse
Source: http://www.vvse.com/blog/blog/2012/02/27/reverse-scrolling-on-windows-7/
- Find the Hardware ID for your mouse
-
Mads Madsen
TYPO3 Flow nginx rewrite rules.
Tested on nginx 1.4 and nginx 1.2.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849server {listen 80;server_name hostname;error_log /var/www/flow/logs/error.log debug;access_log /var/www/flow/logs/access.log combined;root /var/www/flow/Web;index index.html index.php;location ~ ^/_Resources/Persistent/ {rewrite "^/(_Resources/Persistent/.{40})/.+(\..+)" /$1$2 break;}location ~ ^/_Resources/ {access_log off;log_not_found off;expires max;break;}location ~* /favicon.ico {access_log off;log_not_found off;break;}location ~* /\.ht {deny all;access_log off;log_not_found off;break;}location / {try_files $uri $uri/ /index.php?$args;}location ~ \.php {fastcgi_split_path_info ^(.+\.php)(/.+)$;fastcgi_pass 127.0.0.1:9000;include /etc/nginx/fastcgi_params;fastcgi_index index.php;fastcgi_param FLOW_CONTEXT Development;fastcgi_param FLOW_REWRITEURLS 1;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_read_timeout 30m;}} -
Mads Madsen
Port forwarding in Windows 8
One of the drawbacks of Hyper-V on Windows 8 Pro, is that lack of NAT. Luckily Windows 8’s networking supports port proxies in netsh.
1234Host ip = 192.168.1.25Guest ip = 192.168.137.147netsh interface portproxy add v4tov4 listenport=22 listenaddress=192.168.1.25 connectport=22 connectaddress=192.168.137.147Source: http://technet.microsoft.com/en-us/library/cc776297(v=ws.10).aspx
-
Mads Madsen
Fixing eth numbering
While moving virtual machines around, it will usually mean a change on MAC addresses on the networking interfaces, which will create new eth nodes on the machine.
Reasons for the renaming of eth nodes on the machine, is due to the persistent mapping on Linux systems using udev.
Fixing this is done by removing the entities in the /etc/udev/rules.d/70-persistent-net.rules and reboot the machine. -
Mads Madsen
Simple WoL script
Simple wake-on-lan script, to wake up a WoL enabled computer when it receives a connection. This script is currently running on an old router.
It requires the wol command.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596#!/usr/bin/php -q<?phpif (!function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');declare(ticks=1);//Configuration!$addressBind = '192.168.1.5';$portBind = 10066;$wolEnabledIP = '192.168.1.3';$wolEnabledNetwork = '192.168.1.255';$wolEnabledMAC = '00:12:34:56:78:90';//End-Configuration!$pid = pcntl_fork();if ($pid == -1) {die('could not fork');} else if ($pid) {exit;} else {//we are the child}if (posix_setsid() == -1) {die("could not detach from terminal");}//Write pid$posid=posix_getpid();$fp = fopen("/var/run/wol-daemon.pid", "w");fwrite($fp, $posid);fclose($fp);//Signal handlerspcntl_signal(SIGTERM, "sig_handler");pcntl_signal(SIGHUP, "sig_handler");function sig_handler($signo) {switch ($signo) {case SIGTERM:exit;break;}}/* Allow the script to hang around waiting for connections. */set_time_limit(0);//Turn on implicit output flushing so we see what we're getting as it comes in.ob_implicit_flush();if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";}if (socket_bind($sock, $addressBind, $portBind) === false) {echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";}if (socket_listen($sock, 5) === false) {echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";}do {if (($msgsock = socket_accept($sock)) === false) {echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";break;}$fp = @fsockopen($wolEnabledIP, 139, $errno, $errstr, 1); //The WoL enabled computer is running Windows, that's why we are testing on port 139.if (!$fp) {$msg = "\nSince Celina is not up, I will try to wake her up for you\nPlease wait...\n";socket_write($msgsock, $msg, strlen($msg));pclose(popen("wol -i '.$wolEnabledNetwork.' -p 7 ".$wolEnabledMAC, "r"));while(true) {sleep(1);$fpC = @fsockopen($wolEnabledIP, 139, $errno, $errstr, 1);if (!$fpC) {continue;} else {$msg = "Celina is up and spinning, feel free get the hell out! ;-)\n";socket_write($msgsock, $msg, strlen($msg));break;}}} else {$msg = "\nCelina is up, so what is your point?\n";socket_write($msgsock, $msg, strlen($msg));fclose($fp);}socket_write($msgsock, $msg, strlen($msg));socket_close($msgsock);} while (true);socket_close($sock); -
Mads Madsen
WIP: “Simulated” auto scaling with PHP
Source: AutoScale on BitBucket
General idea:
Use a locale memcached database to keep an eye on load for the current server, using data from processes using resources on the node. (Eg. not connection time to database server, only pure processing power used on current node).Usage:
Edit config.php.
execute agent.phpTodo:
- Allow possibility to change testing to “loadavg”, as a source for determine load.
Reply