Monday, November 14, 2011

Java: Copying and Cloning Lists: public Object clone()

import java.util.ArrayList;
import java.util.List;

public class MainClass {
  public static void main(String[] a) {

    List list = new ArrayList();
    list.add("A");

    List list2 = ((List) ((ArrayList) list).clone());

    System.out.println(list);
    System.out.println(list2);

    list.clear();

    System.out.println(list);
    System.out.println(list2);
  }
}

Source:

Python: Private Variables and Methods


# Demonstrates private variables and methods


class Critter(object):
    """A virtual pet"""
    def __init__(self, name, mood):
        print "A new critter has been born!"
        self.name = name            # public attribute
        self.__mood = mood          # private attribute


    def talk(self):
        print "\nI'm", self.name
        print "Right now I feel", self.__mood, "\n"


    def __private_method(self):
        print "This is a private method."


    def public_method(self):
        print "This is a public method."
        self.__private_method()


# main
crit = Critter(name = "Poochie", mood = "happy")
crit.talk()
crit.public_method()


Source:
http://www.java2s.com/Code/Python/Class/Demonstratesprivatevariablesandmethods.htm

Sunday, November 6, 2011

Python: Great Python Manual

Javascript: get and show attributes from object


var myObject = { name: "Cody", status: "Surprised" };
for (var propertyName in myObject) {
    document.writeln(propertyName+" : "+myObject[propertyName]);
}


Source:
http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object


Python: get length of a list


a = ['spam', 'eggs', 100, 1234]
print len(a) // 4


Source:
http://www.java2s.com/Code/Python/List/Builtinfunctionlengetthelengthofalist.htm

Python: formatting Datetime


import datetime

datenow = datetime.datetime.now()
formatedDateNow = datenow.strftime("%Y-%m-%d")


References:
http://docs.python.org/library/datetime.html
http://effbot.org/librarybook/datetime.htm

Python: Get variable type

Use type() function:

>>> d=['one','two']
>>> print d
['one', 'two']
>>> type(d)
<type 'list'>

Source:
http://linux.byexamples.com/archives/342/python-how-to-identify-the-type-of-your-variable/

LibreOffice: Round number up


CEILING

Rounds a number up to the nearest multiple of Significance.

Syntax

CEILING(Number; Significance; Mode) 

Example

=CEILING(56100;1000) 
//returns 57000


Source:
http://help.libreoffice.org/Calc/Mathematical_Functions#CEILING

Wednesday, November 2, 2011

HTML: Great page to generate loading gifs

HTML: Generate Custom Hex Code Colors

ExtJS: doing a preloader screen

CSS: Writting comments

/* Comment here */

{
margin: 1em; /* Comment here */
padding: 2em; 
/* color: white; */
background-color: blue;
}
/*
multi-line
comment here
*/
Source:
http://css.maxdesign.com.au/selectutorial/rules_comments.htm

Python: string replace


str = "this is string example....wow!!! this is really string";


print str.replace("is", "was");    
# thwas was string example....wow!!! thwas was really string


print str.replace("is", "was", 3); 
# thwas was string example....wow!!! thwas is really string


Source:
http://www.tutorialspoint.com/python/string_replace.htm

Python: "Extend" like method for a Dict


a = { "a" : 1, "b" : 2 }
b = { "c" : 3, "d" : 4 }
a.update(b)
print a # prints { "a" : 1, "b" : 2, "c" : 3, "d" : 4 }


Source:
http://stackoverflow.com/questions/577234/python-extend-for-a-dictionary
Reference:
http://docs.python.org/library/stdtypes.html#mapping-types-dict