Python ascii to binary

Aug 11
2010
import string
 
asci_lower = dict(map(lambda x,y: [x,y], string.ascii_lowercase,
                                      map(bin, map(ord, string.ascii_lowercase))))
 
asci_upper = dict(map(lambda x,y: [x,y], string.ascii_uppercase, 
                                       map(bin, map(ord, string.ascii_uppercase))))
 
print asci_lower['a']
0b1100001
 
print asci_upper['A']
0b1000001

Christmas names

Dec 15
2009
>>>from nltk.corpus import wordnet as wn
 
>>>wn.synsets('Christmas')[0].lemma_names
 
['Christmas', 'Christmastide', 'Christmastime', 'Yule', 'Yuletide', 'Noel']

the actual meaning of “Merry Christmas”

Dec 14
2009
import re
 
greeting = "Merry Christmas"
 
greeting_list = map(chr, [ord(i) - len(greeting) - 1 for i in greeting])
greeting_list = filter(lambda x: re.match('[a-z]|[A-Z]', x), greeting_list)
 
# greeting_list is set(['c', 'b', 'd', 'i', 'Q', 'U', 'Y', 'X'])
# the 1st 3 elements are not clear to me: c,b,d
# 'i' means 'I', 'Q' means 'question', 'U' means 'you', 'Y' means 'why', 'X' means 'Christmas'
# which makes the compound: 'I question you - why Christmas?'

Right?

Upgrade Trac seamlessly

Nov 28
2009

Find your Trac path:

python -c’import trac;print trac’

Edit env.py on line 598:

1
2
3
4
5
6
if needs_upgrade:
    try:
        os.system("/usr/bin/trac-admin %s upgrade" % env_path)
    except TracError:
        raise TracError(_('The Trac Environment needs to be upgraded.\n\n'
                              'Run "trac-admin %(path)s upgrade"', path=env_path))

Restart Trac daemon ot whatever http server is serving it.

The Alphabet

Oct 20
2009

Python

import re
 
_char = re.compile(r'\w')
 
''.join([chr(i) for i in xrange(65,122) if _char.match(chr(i))])
 
# or just:
import string
 
string.ascii_letters
string.ascii_lowercase
string.ascii_uppercase

Ruby

(65..122).to_a.map{ |e| e.chr =~ /\w/ && e.chr }.compact.to_s

Perl

$chars .= chr() =~ /\w/ ? chr() : q{} for 65..122;

I don’t like the mess of Ruby, and Perl.

Filtering Lists

Dec 01
2008

Ruby

arr = (1..10).to_a
arr.select{ |e| e % 2 != 0 }

Python

arr = range(1,11)
[e for e in arr if e % 2]

Perl

@arr = (1..10);
grep($_ % 2, @arr);

Simple Email Hex Encoding

Nov 18
2008
#!/usr/bin/python
 
import re
 
hex_encode = lambda x: ("&#%i;" * len(x)) % (tuple(map(ord, re.findall('.', x))))
 
# hex_encode("root@example.com") =>;
# root@vidul.com

Sequence Types and Slice

Oct 21
2008

Ruby slice:

a = [1, 2, 3, 4, 5, 6]
a[0..-1] # [1, 2, 3, 4, 5, 6]
a[(0..-1)] # tha same

Python slice:

a = [1, 2, 3, 4, 5, 6]
a[:-1] # [1, 2, 3, 4, 5]
a[:] # [1, 2, 3, 4, 5, 6]
a[0:len(a):2] # [2, 4, 6]  Nice, isn't it?

three ways to print the alphabet

Jul 27
2008

String methods:

"a".upto("z"){ |i| puts i }
a = String.new "a"
0.upto(0){ p a }
1.upto(25){ p a.next! }

Range method:

("a".."z").each{ |e| puts e }

Lists and Values

Nov 30
2007

Perl

($a, $b) = (1, 2);
@arr    = (1, 2);

Python

 a, b = 1, 2
arr = 1, 
arr = 1,2

Ruby

 a, b = 1, 2
arr  = 1, 2
arr  = 1,2, *[11,22] # the same as
arr  = [1,2, [11,22]].flatten

Calendar

February 2012
M T W T F S S
« Sep    
 12345
6789101112
13141516171819
20212223242526
272829  

Tags