Thursday, September 26, 2013

new instructions for installing Java JDK 8 on Ubuntu


http://www.java.com/en/download/faq/java_win64bit.xml


Which Java download should I choose for my 64-bit  operating system?


Verify that you are using a 64-bit OS

In Firefox type

about:support


Oracle Java download site


From Oracle Java installation instructions:


# Download from this page
# http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
# Linux x64 180.95 MB jdk-8u151-linux-x64.tar.gz

sudo mkdir -p /usr/local/java
cd /usr/local/java
sudo cp ~/Downloads/jjdk-8u151-linux-x64.tar.gz .
sudo tar xvf jdk-8u151-linux-x64.tar.gz
ls -a
sudo emacs /etc/profile

# Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file:
# Type/Copy/Paste:
#
export JAVA_VERSION=jdk1.8.0_151
export JAVA_HOME=/usr/local/java/$JAVA_VERSION

export PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

# In the shell

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/$JAVA_VERSION/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/$JAVA_VERSION/bin/javac" 1

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/$JAVA_VERSION/bin/javaws" 1

sudo update-alternatives --set java /usr/local/java/$JAVA_VERSION/bin/java

sudo update-alternatives --set javac /usr/local/java/$JAVA_VERSION/bin/javac

sudo update-alternatives --set javaws /usr/local/java/$JAVA_VERSION/bin/javaws

. /etc/profile

cd /opt/google/chrome/plugins
sudo rm -f libnpjp2.so
sudo ln -s /usr/local/java/$JAVA_VERSION/jre/lib/amd64/libnpjp2.so .

sudo mkdir -p /usr/lib/mozilla/plugins
cd /usr/lib/mozilla/plugins
sudo rm -f libnpjp2.so
sudo ln -s /usr/local/java/$JAVA_VERSION/jre/lib/amd64/libnpjp2.so

Tuesday, September 17, 2013

Python pages

Short cuts to useful Python tutorials from other web pages.
Snippets of code from the other pages is included in this blog.
Attribution is given int he form of a link to the original reference.

Lamda and multiple args

Python lists explained
The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.

Python List len() Method

list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
print "First list length : ", len(list1);
print "Second list length : ", len(list2);


The Python yield keyword explained
3,400+ votes on Stack Overflow

calling the function str.lower and assigning the return value which is definitely not iterable
Change the line
content = inputFile.read().lower
to
content = inputFile.read().lower()
Your original line assigns the built-in function lower to your variable content instead of calling the function str.lower and assigning the return value which is definitely not iterable.

Python sets
> > > y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
> > >; sorted(set(y))
[1, 6, 8]

> > > s = set([1,6,8])
> > > print(s)
{8, 1, 6}
> > > s.update(range(10,100000))
> > > for v in range(10, 100000):
    s.remove(v> > >> > > print(s){1, 6, 8}

Python regexs
Python regexs 2

NameError: name 're' is not defined
-import the re module

Python - Using regex to find multiple matches and print them out

line = 'bla bla bla
Form 1
some text...
Form 2
more text?'
matches = re.findall('
(.*?)
'
, line, re.S) print matches


Python Dictionary (hashes)

Python : List of dict, if exists increment a dict value, if not append a new dict

urls = {'http://www.google.fr/' : 1 }
for url in list_of_urls:
    if not url in urls:
        urls[url] = 1
    else:
        urls[url] += 1
http://stackoverflow.com/questions/13757835/make-python-list-unique-in-functional-way-map-reduce-filter?rq=1

Is there a way in Python of making a List unique through functional paradigm ?
Input : [1,2,2,3,3,3,4]
Output: [1,2,3,4] (In order preserving manner)




In [29]: a = [1,2,2,3,3,3,4]
In [30]: reduce(lambda ac, v: ac + [v] if v not in ac else ac, a, [])
Out[30]: [1, 2, 3, 4]


Pretty printing a dictionary
Use the module name to reference the pprint function
import pprint
pprint.pprint(...)

Python sets

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
Here is a brief demonstration:
>>>
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket)               # create a set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit                 # fast membership testing
True
>>> 'crabgrass' in fruit
False

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b                              # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b                              # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b                              # letters in both a and b
set(['a', 'c'])
>>> a ^ b                              # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
Similarly to list comprehensions, set comprehensions are also supported:
>>>
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
set(['r', 'd'])

 python remove whitespaces in string

Looping through python regex matches

import re

s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')

for (letters, numbers) in re.findall(pattern, s):
    print numbers, '*', letters

Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this:

>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False

How to test if a dictionary contains a specific key? [duplicate]

x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
    ....
How to check a string for specific characters?

 How to return more than one value from a function in Python? [duplicate]