2011年5月13日金曜日

NSArrayの使い方

int main (int argc, const char * argv[])
{
    
    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc]init];
    
    //配列の作成
    NSArray *array = [NSArray arrayWithObjects:@"abc",@"def",@"ghi",@"jkl",nil];
    NSLog(@"arra: %@",array);//arrayの要素が順番に出力
    
    //配列の要素数を取得
    int arrCount = [array count];
    NSLog(@"arrCount:%d",arrCount);//4
    
    
    //配列の要素を取り出す
    id obj01 = [array objectAtIndex:2];
    id obj02 = [array lastObject];
    NSArray *array2 =[array subarrayWithRange:NSMakeRange(1, 2)];
    
    NSLog(@"obj01: %@",obj01);//obj01: ghi
    NSLog(@"obj02: %@",obj02);//obj02: jkl
    NSLog(@"array2: %@",array2);//def,ghi
    
    //要素のインデックスを取得
    NSString *val = @"pqr";//array["pqr"]を取り出す
    NSUInteger objectIndex = [array indexOfObject:(id)val];
    if(objectIndex == NSNotFound){//ない場合はNSNotFoundという返り値になる
        NSLog(@"ないよ!");//これが実行される
    }else{
        NSLog(@"あるよ!");
    }
    
    //ある要素が含まれているか確認
    NSString *val2 =@"abc"; 
    BOOL isObjectContain =[array containsObject:val2];
    if (isObjectContain){
        NSLog(@"True");//これが実行される
    }else{
        NSLog(@"false");
    }
        
    
    //配列の比較
    NSArray *array3 = [NSArray arrayWithObjects:@"abc",@"def",@"ghi",@"jkl", nil];
    BOOL isArrayEquals =[array isEqualToArray:array3];
    
    if(isArrayEquals){
        NSLog(@"いっしょだよ!");
    }else{
        NSLog(@"違うよ!");
    }

    
    [pool drain];
    return 0;
}


2011年5月11日水曜日

base64の使い方 --python--

'''base64の使い方'''

part = base64.b64encode('pass')

print(part)#cGFzcw==
print(base64.b64decode(part)) #pass

'''URLのパラメータとして使う場合は下記'''
part = base64.urlsafe_b64encode('pass')

print(part)#cGFzcw==
print(base64.urlsafe_b64decode(part))#cGFzcw==

md5,shaの使い方 --python--

'''md5の使い方'''

m = md5.new('pass-word')

print (m) #
print (m.hexdigest()) #ed0eacb4e34e50c1c53f9698600d4b86

import urllib

'''多分クオートしないとエラーになる...'''
try:
	print m.digest()
except UnicodeDecodeError:
	print u'UnicodeDecodeErrorなのだ'


'''URLのパラメータとして使用する場合はクオートする必要がある'''
quoted =''.join([urllib.quote_plus(x) for x in m.digest()])

print quoted #%ED%0E%AC%B4%E3NP%C1%C5%3F%96%98%60%0DK%86
'''shaの使い方'''

m = sha.new('pass-word')

print (m) #
print (m.hexdigest()) #ed0eacb4e34e50c1c53f9698600d4b86

import urllib

'''多分クオートしないとエラーになる...'''
try:
	print m.digest()
except UnicodeDecodeError:
	print u'UnicodeDecodeErrorなのだ'


'''URLのパラメータとして使用する場合はクオートする必要がある'''
quoted =''.join([urllib.quote_plus(x) for x in m.digest()])

print quoted #%ED%0E%AC%B4%E3NP%C1%C5%3F%96%98%60%0DK%86

pickleの使い方--python--

'''pickleの使い方'''

'適当なオブジェクトを用意する'
o = [1,2,3,(1,2,3),{1,2,3,4,(1,2)}]

print o #[1, 2, 3, (1, 2, 3), set([(1, 2), 1, 2, 3, 4])]


"""書き込みモードで開く"""
opened = open('test.dump','w')

'''pickle化'''
pickle.dump(o,opened)		
'''同ディレクトリにtest.dumpとして保存されている'''

opened.close()

'''dumpファイルを開く'''
opened2 = open('test.dump')

o2 = pickle.load(opened2) 

print o2 #[1, 2, 3, (1, 2, 3), set([(1, 2), 1, 2, 3, 4])]
'''ちゃんとオブジェクトとして再現できる'''
print o2[3][2] #3

o2.append({'hoge':'foo'})

print o2 #[1, 2, 3, (1, 2, 3), set([(1, 2), 1, 2, 3, 4]), {'hoge': 'foo'}]
opened2.close()

opened3 = open('test.dump','w')
	
'再度保存するときはdump'
pickle.dump(o2,opened3)

shelveの使い方--python--

import shelve


def main():
	
	"""shelveの使い方"""
	
	'''shelvetestという名前のファイルを保存'''
	d = shelve.open("shelvetest",writeback=True)
	
	'辞書と同じ使い方でデーターを挿入'
	d.update({"one":"1","tow":"2"})
	
	print(d) #{'tow': '2', 'one': '1'}
	
	'''closeした時点でファイルとして保存される'''
	d.close()
	print "-"*30
	
	'ファイル名を指定して開く'
	d2 = shelve.open("shelvetest")
	
	print(d2) #{'tow': '2', 'one': '1'}

	d2.close()
	
if __name__ == '__main__':
	main()


xcode4でpythonを使う

参考したURLが英語だったので日本語に訳す。

  1. Xcode4を開く
  2. メニューバー→File→New→New Projectと選ぶ
  3. Mac OSXの範囲にあるOtherを選ぶ
  4. External Build Systemを選んでNext
  5. 任意のProduct nameを入力する
  6. Build Toolに/usr/local/bin/python3、/usr/bin/python等とpythonの位置を指定してNext
  7. 任意の保存場所を指定してCreate

 

 

  1. メニューバー→File→New→New Fileと選ぶ
  2. Mac OSXの範囲にあるOtherを選ぶ
  3. Emptyを選んでNext
  4. 任意の保存するディレクトリをえらんで.pyのpython形式でファイル名を指定してSave

 

  1. メニューバー→Product→Edit Schemeと選ぶ
  2. 左側にあるRunという項目をクリックする
  3. infoタブにあるExecutableをOtherに変更
  4. ⇧⌘Gと押すと隠しフォルダを指定して移動できるのでExternal Build Systemとして指定したファイルを指定(この方法は今回初めて知った!!便利>o<)
  5. Debuggerの項目はNoneに変更
  6. Base Expansions Onの項目に作成したproduct nameを指定
  7. Arguments Passed On Launchの項目の下にある+ボタンをクリックして・・・

ここからわからん。。。。w

一度保留して、hello worldを試してみたけど、xcodeがすぐ落ちる。。。

だれか、教えて〜〜〜><

  1. Click the "+" icon under "Arguments Passed On Launch". You may have to expand that section by clicking on the triangle pointing to the right.
  2. Type in "$(SOURCE_ROOT)/" without the quotes and then the name of the Python file you want to test. Remember, the Python program must be in the project folder. Otherwise, you will have to type out the full path here.
  3. Click "OK".
  4. Start coding.

原文:http://stackoverflow.com/questions/5276967/python-in-xcode-4

2011年5月2日月曜日

Python覚え書き

file = open('index.html') html = file.read()

↓短縮

html = open("index.html").read()