<div class="parent">
	First
	<div class="child">Child</div>
	Last
</div>

다음과 같을 때 First 를 Second로 바꾸고 싶을 때 어떻게 하면 될까?!

$(".parent").contents()

를 하면 다음과 같이 나온다.

["First", <div class="child">Child</div>, "Last"]
$(".parent").contents()[0].textContent = "Second"

하면 First 값이 Second로 바뀐다.

빈 객체를 체크 하는 방법은 다음과 같다.

var obj = {};
Object.keys(obj).length === 0 && JSON.stringify(obj) === JSON.stringify({});

$.isEmptyObject(obj); // JQuery
[참고] http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
function foo(a)
{
	a = typeof a !== 'undefined' ? a : 1;

	//code!
}

1. virtualenv 로 개발 프로젝트 폴더 생성

1-1) virtualenv {프로젝트 폴더명}

[그림 1-1] virtualenv 실행

2. pycharm 셋팅

2-1) pycharm 프로젝트 오픈

[그림 2-1] pycharm 프로젝트 오픈

2-2) 방금 생성한 폴더를 프로젝트 폴더로 설정 합니다.

2-3) File - settings... 로 이동

[그림 2-2] settings 로 이동

2-4) project Interpreter 설정

[그림 2-3] Project Interpreter 설정

2-5) Interpreter 추가

[그림 2-4] Interpreter 추가

2-6) 생성 폴더/Scripts/python.exe 추가

[그림 2-5] Interpreter 추가

날짜 별로 행의 갯수를 세야 할 때 다음과 같이 쿼리를 작성하면 된다.

SELECT DATE_FORMAT(날짜 필드, '%Y%m%d') AS date, count(*) AS cnt 
FROM 테이블 이름
GROUP BY DATE_FORMAT(날짜 필드, '%Y%m%d') ORDER BY date DESC;

소수점 중 불필요한 0이 있을 수 있다. 

예를 들면

3.150 , 3.0, 4.000 과 같이 특정 이상 소수점 부터는 의미 없는 0일 수 있다.(엄밀히 말하면 의미 없다고 할 수 없지만!) 

그럴 때 다음과 같이 하면 0을 제거 할 수 있다.

print 10.0/2
print "%g" %(10.0/2)

결과는 다음과 같다.

5.0
5
[Finished in 0.1s]

자바 스크립트로 unicode 인코딩 과 디코딩은 다음과 같이 하면 된다.

function decodeUnicode(unicodeString) {
	var r = /\\u([\d\w]{4})/gi;
	unicodeString = unicodeString.replace(r, function (match, grp) {
	    return String.fromCharCode(parseInt(grp, 16)); } );
	return unescape(unicodeString);
}


function encodeUnicode(convertString) {
	var unicodeString = '';
	for (var i=0; i < convertString.length; i++) {
		var theUnicode = convertString.charCodeAt(i).toString(16).toUpperCase();

		while (theUnicode.length < 4) {
			theUnicode = '0' + theUnicode;
		}

		theUnicode = '\\u' + theUnicode;
		unicodeString += theUnicode;
	}

	return unicodeString;
}

encodeUnicode("김초보");
decodeUnicode("\uAE40\uCD08\uBCF4");

결과는

\uAE40\uCD08\uBCF4
김초보

이다.

특정 필드가 업데이트 될 때 트리거를 작동하게 해야 할 때가 있다.

그럴 때는 다음과 같이 트리거를 생성하면 된다.

CREATE  TRIGGER  tg_my_trigger AFTER UPDATE ON tbl_my_table
 FOR EACH ROW
BEGIN
if NEW.column1 <> OLD.column1
begin 
--작성
end
END

python 에서 텍스트를 처리할 때 특수문자를 제거해야 할 때가 있습니다. 이럴 때 정규표현식 으로 처리할 수도 

있지만 다음과 같이 처리할 수도 있습니다.


sample_string = "1234567890abcdefgABCDEFG!@#$%^&*()_{}[]<>"
result_string = ""
 
for c in sample_string:
    if c.isalnum():
        result_string +=c
 
print result_string
결과는 다음과 같습니다.
1234567890abcdefgABCDEFG 
[Finished in 0.2s]


Python 에서 리스트(List)에 값은 함수를 적용하고 싶을 때 map 함수를 사용하면 됩니다.

예제 소스로 확인해 봅시다.

def plus(x):
    return x+x
 
lst = [1,2,3,4,5,6,7,8,9, "A", "B", "C"]
 
print map(plus, lst)

결과는 다음과 같습니다.

[2, 4, 6, 8, 10, 12, 14, 16, 18, 'AA', 'BB', 'CC']
[Finished in 0.1s]

+ Recent posts