Showing posts with label how-to. Show all posts
Showing posts with label how-to. Show all posts

Tuesday, June 25, 2013

Why use Python Properties?

The reasoning is here, but in short  "...[t]he answer is, that in [a simple] use case we would not. In fact, we would write thus:
>>> class MyClass(object):
...   x = 0
...
>>> my = MyClass()
>>> my.x = 4
>>> my.x
4

'But!', I can hear you scream, 'there's no encapsulation!'. What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?

No - we just switch to the property version, add whatever we want, and have not changed the interface one iota! The great thing about properties is not that they replace getters and setters, its that you don't have to write them to future-proof your code. You can start out by writing the simplest implementation imaginable, and if you later need to change the implementation you can still do so without changing the interface. Neat, huh?"

Tuesday, May 3, 2011

Gettext workflow

1. Define your bindtextdomain and textdomain as
bindtextdomain('hello', '/usr/share/locale')
textdomain('hello')

2. Wrap strings to be translated in gettext('translate me') or, using the gettext alias, _('translate me').
return _('this string is to be translated');
3. Extract the strings using the xgettext utility.
xgettext -d hello -s -o hello.pot hello.c
4. Turn the .pot file into a .po file that can be passed on to the translators
msginit -l es_US -o spanish.po -i hello.pot
5. Translate the strings in the .po file
msgid "Hello\n"
msgstr "Hola\n"
6. Convert the .po file into a compiled .mo using the msgfmt utility, note that the output filename must match the domain given as the first argument to bindtextdomain and textdomain and must be placed in the location specified as the second argument to bindtextdomain
joined with the language set in setlocale or set in the environment
msgfmt -c -v -o hello.mo spanish.po
cp hello.mo /usr/share/locale/es_US/LC_MESSAGES
source

Friday, April 22, 2011

Adding locales for gettext

Go to System -> Administration -> Language Support and add the languages you want to support.

Once those have installed, run

sudo dpkg-reconfigure locales

Done

Source

Wednesday, February 9, 2011

mod_proxy in ubuntu

In order to get mod_proxy running in ubuntu I had to:
  • ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf
  • ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
  • ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
Then in my conf file (sites-enabled/my-site) I added between the VirtualHost tag

<proxy>
Order deny,allow
Allow from all
</proxy>

<location /pickup >
ProxyPass http://qa-ox3-datamart-xv-01.xv.dc.openx.org/pickup/
ProxyPassReverse http://qa-ox3-datamart-xv-01.xv.dc.openx.org/pickup/
</location>

Delete a remote branch in git

git push origin :branchname

Thursday, November 4, 2010

How to change auto-save-list directory in emacs

To change the auto-save-list directory in emacs, add the following to your .emacs:

;; Put auto-save-list somewhere else
(set 'auto-save-list-file-prefix "~/.trash/.saves-")

Monday, November 1, 2010

Make mouse pointer disappear with unclutter

Add the following line to .xsession to make the mouse pointer go away after a set number of seconds:

unclutter -idle 1 -noevents &

Wednesday, October 20, 2010

Whole Trackpad as a scrollwheel

Update: The below is all crap. Turns out the best way to do it is to simply set up in your bashrc a call to synclient setting LeftEdge and RightEdge = 0

synclient LeftEdge=0
synclient RightEdge=0





--------------------------------------------------------------------------------------
Using synclient -l to display all values, find the value of LeftEdge and then set the value of RightEdge to the same value in LeftEdge in your xorg.conf under the 'InputDevice' section:

Section "InputDevice"
Identifier "Synaptics Touchpad"
Driver "synaptics"
Option "SendCoreEvents" "true"
Option "Device" "/dev/psaux"
Option "Protocol" "auto-dev"
Option "SHMConfig" "on"
Option "HorizEdgeScroll" "0"
Option "RightEdge" "1752"
EndSection


Source: http://ubuntuforums.org/showthread.php?t=886449

Tuesday, September 7, 2010

Change Gnome login background/theme

1.When logged out, hold down CTRL+Alt+F1
2.Type export DISPLAY=:0.0 and press ENTER
3.Type Sudo -u gdm gnome-control-center and press ENTER
4.Hold down CTRL+Alt+F7
5.Gnome Control Center will appear. Press ''Apperance'' and choose a picture.
6. Close the window and log in

Friday, July 30, 2010

Gnome + Xmonad

From http://markhansen.co.nz/xmonad-ubuntu-lucid
Here’s how to run XMonad as a drop-in replacement for gnome’s default window manager (Metacity):

Install XMonad. Open a terminal and enter:
sudo apt-get install xmonad
Configure XMonad to interact happily with gnome. Make a file ~/.xmonad/xmonad.hs, and put in it:
import XMonad
import XMonad.Config.Gnome
main = xmonad gnomeConfig
Compile this config file by typing xmonad --recompile at the terminal.

Tell gnome to use xmonad instead of metacity. At the terminal, enter:
gconftool-2 -s /desktop/gnome/session/required_components/windowmanager xmonad --type string
Log out and log back in and you're done.

NOTE: To revert back to Metacity from XMonad, at the terminal, enter:
gconftool-2 -s /desktop/gnome/session/required_components/windowmanager gnome-wm --type string

Tuesday, June 22, 2010

Windows Key remapping

So today I wanted to remap more than just capslock -> lctrl. I searched around a bit to try and find scan codes for registry hacks but didnt find much. I did find, this little tool, which is great but you need to make sure that you're editing boot mappings and not user mappings.

Go to Mappings-> Show and select 'Boot Mappings' to ensure that you're editing the right keys.

Wednesday, February 3, 2010

Disable Ease of Access in windows 7

Finally found a command that will completely disable Ease of Access in windows 7. On the command line, run
cacls %windir%\system32\utilman.exe /C /D Everyone
and hit Y when prompted. This will remove the functionality behind that stupid button on the windows screen and will stop the stupid ease of access dialog from popping up when you're typing in your password to the login page.

Monday, November 30, 2009

github, ssh config file and ssh-agent

Github makes use of ssh-keys for checking out your stuff via git. I also have several different keys and store them with different names such as github_rsa or some such. In order to get this to work, you have to create a config file that stores the location of the private key.

For github, in the .ssh/ directory, create a file `config` and enter in the following:
From github's help page:

Host github.com
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile path-to-private-key


In several situations I've had to set up ssh keys for remote machines that only provide me with terminal access. Thus I keep getting pestered to enter the passphrase for the ssh key. ssh-agent takes care of having to keep entering in the passphrase.

After setting up the ssh-keys do:
eval `ssh-agent`; ssh-add ~/.ssh/github_rsa

It will then ask for the passphrase the one time when it adds the key to the agent, and then wont ask again.

Wednesday, October 28, 2009

Displaying and Configuring Character Sets in MySQL

Display all character sets:

show variables like 'character_set%';

Change the character set for a table:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

BUT with the caveat that
The CONVERT TO operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column:

ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;

The reason this works is that there is no conversion when you convert to or from BLOB columns.

The process to completely change all mysql's character sets from latin-1 and make this garbage:
(using the show variables command from above)
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
look beautiful like this:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
involves simply adding the following to the [mysqld] section of your my.cnf (or /etc/mysql/my.cnf if you like, and are on a debian based machine)

collation_server=utf8_unicode_ci
character_set_server=utf8

and adding the following to BOTH the [client] section AND the [mysqld] section:

default-character-set=utf8


This is simply in case you cannot go back and re-compile mysql using
./configure --with-charset=utf8 --with-collation=utf8_general_ci

Tuesday, October 27, 2009

Completely removing gnome from ubuntu.

I use terminal only versions of ubuntu on several vm's that I have set up. Maybe I should use something else, like just plain debian with no gui, but either way I was looking for a way to completely remove all gnome elements from my virtual machines. In the end I found a LONG command that just lists all the gnome elements to send to the apt-get remove command... seems like there should be something more elegant...

The commands are here, but it needs to be noted to remove the `&& sudo apt-get install kubuntu-desktop` from the end of the commands.

Sunday, October 18, 2009

Learning Jquery and rehashing javascript..

Been learning jquery and messing around with hiding things dynamically in the django-admin...


indexOf doesn't work in IE
so basically the work around is to apply it to ie's version of js..

if (!Array.indexOf) {
Array.prototype.indexOf = function (obj, start) {
for (var i = (start || 0); i < this.length; i++) {
if (this[i] == obj) {
return i;
}
}
}
}


There is no string title case function so again, put one in yourself..

// (c)GPL, apv
String.noLC = new Object
({the:1, a:1, an:1, and:1, or:1, but:1, aboard:1,
about:1, above:1, across:1, after:1, against:1,
along:1, amid:1, among:1, around:1, as:1, at:1,
before:1, behind:1, below:1, beneath:1, beside:1,
besides:1, between:1, beyond:1, but:1, by:1, 'for':1,
from:1, 'in':1, inside:1, into:1, like:1, minus:1,
near:1, of:1, off:1, on:1, onto:1, opposite:1,
outside:1, over:1, past:1, per:1, plus:1,
regarding:1, since:1, than:1, through:1, to:1,
toward:1, towards:1, under:1, underneath:1, unlike:1,
until:1, up:1, upon:1, versus:1, via:1, 'with':1,
within:1, without:1});

String.prototype.titleCase = function () {
var parts = this.split(' ');
if ( parts.length == 0 ) return '';

var fixed = new Array();
for ( var i in parts ) {
var fix = '';
if ( String.noLC[parts[i]] )
{
fix = parts[i].toLowerCase();
}
else if ( parts[i].match(/^([A-Z]\.)+$/i) )
{ // will mess up "i.e." and like
fix = parts[i].toUpperCase();
}
else if ( parts[i].match(/^[^aeiouy]+$/i) )
{ // voweless words are almost always acronyms
fix = parts[i].toUpperCase();
}
else
{
fix = parts[i].substr(0,1).toUpperCase() +
parts[i].substr(1,parts[i].length);
}
fixed.push(fix);
}
fixed[0] = fixed[0].substr(0,1).toUpperCase() +
fixed[0].substr(1,fixed[0].length);
return fixed.join(' ');
}
Javascripts objects work as associative arrays
var theStatus = new Object;
theStatus.Home = 'normal';
theStatus['Home'] //will print 'normal'

Arrays are useful as in all languages, but their function set isnt really all that complete and require some inelegant code to remove elements properly. (Mainly just
var arr = [4,5,6];
arr.splice(arr.indexOf(5), 1);
Also, to copy arrays by value, you have to use the splice() fn...

There's no elegant way to copy objects by value, so again.. add it in yourself.
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
Jquery doesn't have a proper way to filter with regexes... so.... yeah... But even that needs to be modified since it only searches on the text() field of an object.
jQuery.extend(
jQuery.expr[':'], {
regex: function(a, i, m, r) {
var r = new RegExp(m[3], 'i');
//Add modifications here....
return r.test(jQuery(a).text());
}
}
);

But a way to get around the lack of regex in some cases is to remember that you can chain [attributeFilters]..
$("input[id][name$='man']").val("only this one");

And finally(for now), you can add attributes to objects with attr.
$('#target').attr("disabled", true); 




I still don't like javascript, but jquery makes working with it a lot nicer.. sorta.

Friday, August 28, 2009

How to uninstall python packages with easy_install.

`easy_install -m packagename` removes the file from your .pth file (and therefore removes it from your python path), then remove .egg file or directory from your site-packages, or dist-packages directory.


Thx to PJE via PL

Wednesday, August 26, 2009

How to revert a file or directory in svn.

Revert a file to revision 854

svn merge -c -854 l3toks.dtx

Grabbed from stack overflow.


Revert a directory to revision 854

svn merge -rHEAD:854 .

Grabbed from some code monkey.

Wednesday, July 29, 2009

Command to copy a vdi in virtual box on a linux machine.

Make sure the vm using the original vdi is not running and then release and remove (Choose KEEP) the vdi from the pool of drives. Then run

VBoxManage clonevdi /Original.vdi /NewCopy.vdi

Once that is done be sure to re-attach the original vdi.

Monday, June 29, 2009

Adding a field to the django auth user.

I've been trying to do this for a while and I keep finding how-to's such as Extending the Django User model with inheritance, but the problem with these methods is that you still cant sign-in to the django admin with any users you create (such as the admin you set up at the beginning of the project).

I finally got the monkey-patching method to work by doing the following.

You can add fields to the user class with the method add_to_class instead of inheriting the User class by doing the following in your models.py:
User.add_to_class('new_field', models.BooleanField(default=False))

Once that is done, in admin.py, modify the UserAdmin and add your new field
from django.contrib.auth.admin import UserAdmin

UserAdmin.list_display += ('new_field',)
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')
VoilĂ , with that done, you're new fields should show up in the auth user on the django admin page.