Wednesday, October 31, 2012

ExtJS4: date formatting reference

Here is a nice guide to handle date formatting with ExtJS4:

http://senchaexamples.com/2012/05/03/formatting-dates-using-ext-js-4/

HTML5 : input date referred types not supported on almost all browsers

Be careful, most of the date referred types like date,datetime,time,month,week are not currently supported by Chrome or FF or IE,

http://www.quirksmode.org/html5/inputs.html

Jquery: change label text

in html file:

...
<label id="resultadoconsulta">my Old Text</label>
...

in javascript:

$('#resultadoconsulta').text("my New Text");

Resource:
http://stackoverflow.com/questions/3584145/change-text-of-label-in-jquery

JQuery Validation Plugin: simple demo

Javascript: convert from string to integer

Use parseInt:

var myString = "34";
var myIntegerFromString = parseInt(myString); // 34 (integer)

console.log("myString ");
console.log(myString); // 34
console.log(typeof myString); //string

console.log("myIntegerFromString ");
console.log(myIntegerFromString); // 34
console.log(typeof myIntegerFromString); //integer

Resource:
http://www.javascripter.net/faq/convert2.htm
http://www.javascriptkit.com/javatutors/determinevar2.shtml


Python: delete element from dict

Use del:

dict2 = {"key1":"value1","key2":"value2","key3":"value3"}

print "############### print before del#################"
for k in dict2.keys():
    myValue = dict2[k]
    print "key: %s , value: %s" % (k,myValue)
    
del dict2["key2"]

print "############### print after del#################"
for k in dict2.keys():
    myValue = dict2[k]
    print "key: %s , value: %s" % (k,myValue)

Resource:
http://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary

Python: validate if attribute is a instance method


Use type() or isinstance() functions:

# test.py

import types

class Person():
    def __init__(self,fistname,lastname):
        self.fistname = fistname
        self.lastname = lastname
        
    def run(self):
        print "run"
        
    def jump(self):
        print "jump"

myPerson = Person("jack","smith")
for name in dir(myPerson):
    print "name: %s" % (name)
    myAttrib = getattr(myPerson, name)
    print "myAttrib: %s" % (myAttrib)    
    print "(type(myAttrib)) == types.MethodType: %s" % ((type(myAttrib)) == types.MethodType)
    print "isinstance(myAttrib, types.MethodType: %s" % (isinstance(myAttrib, types.MethodType))
   
Result:


C:\Users\John\Desktop>python test.py
name: __doc__
myAttrib: None
(type(myAttrib)) == types.MethodType: False
isinstance(myAttrib, types.MethodType: False
name: __init__
myAttrib: <bound method Person.__init__ of <__main__.Person instance at 0x004F85F8>>
(type(myAttrib)) == types.MethodType: True
isinstance(myAttrib, types.MethodType: True
name: __module__
myAttrib: __main__
(type(myAttrib)) == types.MethodType: False
isinstance(myAttrib, types.MethodType: False
name: fistname
myAttrib: jack
(type(myAttrib)) == types.MethodType: False
isinstance(myAttrib, types.MethodType: False
name: jump
myAttrib: <bound method Person.jump of <__main__.Person instance at 0x004F85F8>>
(type(myAttrib)) == types.MethodType: True
isinstance(myAttrib, types.MethodType: True
name: lastname
myAttrib: smith
(type(myAttrib)) == types.MethodType: False
isinstance(myAttrib, types.MethodType: False
name: run
myAttrib: <bound method Person.run of <__main__.Person instance at 0x004F85F8>>
(type(myAttrib)) == types.MethodType: True
isinstance(myAttrib, types.MethodType: True

Resources:
http://stackoverflow.com/questions/624926/how-to-detect-whether-a-python-variable-is-a-function
http://docs.python.org/2/library/types.html
http://stackoverflow.com/questions/402504/how-to-determine-the-variable-type-in-python