본문 바로가기

Programming/python36

[python] variable 이 존재하는지 check 하는 방법 http://stackoverflow.com/questions/9748678/which-is-the-best-way-to-check-for-the-existence-of-an-attribute Which is a better way to check for the existence of an attribute?Jarret Hardie provided this answer:if hasattr(a, 'property'): a.propertyI see that it can also be done this way:if 'property' in a.__dict__: a.propertyIs one approach typically used more than others? There is no "best" way, b.. 2014. 2. 25.
python thread http://www.tutorialspoint.com/python/python_multithreading.htm Running several threads is similar to running several different programs concurrently, but with the following benefits: Multiple threads within a process share the same data space with the main thread and can therefore share information or communicate with each other more easily than if they were separate processes.Threads sometimes .. 2013. 8. 21.
python 파이썬 파일 이름 변경 filename change 파일 이름을 일괄적으로 변경해야 될 경우가 있다. 한두개라면 손으로 하면 되나 수십개 이상 되면 무조건 코드를 짜야한다. 1.제일 코드가 간단한건 아무래도 MATLAB. 대신 MATLAB인 만큼 속도가 역시 느리다. >> moivefile(name1, name2) 하면 파일 이름이 name1인 파일이 name2로 변경. 2.두번째로는 shell script.. 터미널 윈도우에서 mv a b 하는 것과 같다. 어짜피 쉘스크림트라는게 터미널에서 도는 shell 의 스크립트이니깐.. 3. 세번쨰로는 C(or C++)에서 하는 방법. 언어 자체에서 이런 걸 지원하지 않으니 system api를 써야한다. 윈도에선 구글검색 하면 바로 나오니 패스. 리눅스에선 이 블로그에 예전 포스팅 했었다. LINK 4. PYT.. 2012. 9. 25.
[python] 연속된 8Bit Bytes 를 float 등의 타입으로 바꾸는 방법. Struct module 서로 다른 컴퓨터(언어, 시스템) 끼리 소켓통신을 할때 데이터 타입의 보존및 원활한 통신을 위해 1 byte array를 서로 주고 받으면서 통신한다. matlab 같은 경우는 typecast(data,'uint8') , typecast(data,'single') 이런식으로 ,c같은 경우엔 byte array를 원하는 변수타입에 넣기만 하면 알아서 casting되는 듯 한데Python은 어떻게 할까? 바로 struct 모듈을 이용한다. >>> import struct >>> struct.pack('f', 3.141592654) '\xdb\x0fI@' >>> struct.unpack('f', '\xdb\x0fI@') (3.1415927410125732,) >>> struct.pack('4f', 1.0, 2.. 2012. 6. 23.