python에서 날짜와 같은 숫자를 변환하고 싶을 때, 체크하는 함수를 쓰고 싶을 때 사용할 수 있는 코드이다.

int()는 형변환하다가 에러나면 exception 처리 코드에서 확인할 수 있고, 길이는 len으로 확인할 수 있다. 


#!/bin/python

def isInt(value):

  try:

    int(value)

    return True

  except ValueError:

    return False


def is4Digit(value):

  try:

    if len(value) == 4:

        return True

    return False

  except ValueError:

    return False


print isInt("m")

print isInt("2016")

print isInt("1111111111")

print isInt("123@")

print is4Digit("1234444")

print is4Digit("1234")




결과


False

True

True

False

False

True


실제 코드는 아래처럼 써봤다.


if birth_year is not None and isInt(birth_year) and is4Digit(birth_year) :


Posted by '김용환'
,



1. python 다운로드하기

$ wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz

$ tar zxvf Python-2.7.9.tgz

$ cd Python-2.7.9/

$ ./configure --prefix=/usr/local --enable-shared

$ make 

$ sudo make altinstall



python 2.7 있는지 확인

$ ls /usr/local/bin/python2.7


python 2.7을 실행하면 so(shared object) 파일 없다고 에러가 발생한다.

$ /usr/local/bin/python2.7

/usr/local/bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory


$ ls -al /usr/local/lib/libpython2.7.so

 /usr/local/lib/libpython2.7.so -> libpython2.7.so.1.0





2. python 2.7 실행


shared object관련 설정을 진행한다.



$ sudo ln -s /usr/local/lib/libpython2.7.so /usr/lib/libpython2.7.so.1.0


$ cat >> ~/.bash_profile

export LD_LIBRARY_PATH=/usr/local/lib


$ sudo vi /etc/ld.so.conf 

include /usr/local/lib 추가

 

$ sudo ldconfig

 

$ /usr/local/bin/python2.7

Python 2.7.9 (default, Jan 29 2016, 21:43:51)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>> print 11

11




3. python 2.7.9 용 pip 설치

외부가 막혀있다면, 프록시 셋팅하기. 

export https_proxy=...


pip 설치는 아래 글에 있다.

http://knight76.tistory.com/entry/python-python-279%EC%97%90-%ED%95%B4%EB%8B%B9%EB%90%98%EB%8A%94-pip-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0




$  /usr/local/bin/python2.7 setup.py build

Traceback (most recent call last):

  File "setup.py", line 6, in <module>

    from setuptools import setup, find_packages

ImportError: No module named setuptools


pip 설치 도중에 No module named setuptools이 발생한다. 




4. setupmodule 설치 후,  pip 설치하기



$ wget https://bootstrap.pypa.io/ez_setup.py 

sudo -E /usr/local/bin/python2.7 ez_setup.py


$ /usr/local/bin/python2.7 setup.py build

성공

$ sudo -E /usr/local/bin/python2.7 setup.py install

성공 Finished processing dependencies for pip==6.0.6


테스트로 hadoopfs client인 snakebite를 pip로 설치한다.

$ sudo -E pip install snakebite 

성공



5. python 버전을 2.7.9로 변경


$ sudo mv /usr/bin/python /usr/bin/python2.6.6

$ sudo ln -sf /usr/local/bin/python2.7 /usr/bin/python

$ ls -al /usr/bin/python

/usr/bin/python -> /usr/local/bin/python2.7



$ python 

Python 2.7.9 (default, Jan 29 2016, 11:02:25)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

설치한 pip 모듈로 테스트하고 잘 되는지 확인



* 주의 할 점.

/usr/bin/yum이 실행이 안된다..


$ yum

There was a problem importing one of the Python modules

required to run yum. The error leading to this problem was:


   No module named yum


Please install a package which provides this module, or

verify that the module is installed correctly.


It's possible that the above module doesn't match the

current version of Python, which is:

2.7.9 (default, Jan 29 2016, 21:43:51)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]


If you cannot solve this problem yourself, please go to

the yum faq at:

  http://yum.baseurl.org/wiki/Faq



실행하려면, #!/usr/bin/python 를 #!/usr/bin/python2.6으로 변경해야 한다. 


그러나 yum install python-devel 하는 순간 기존 python 버전(python 2.6)으로 되돌아 간다. python devel관련 lib 설치시 업그레이드 전에 설치해야 한다. 


아니면, 바뀌더라도 다시 ln -sf 를 이용해서 원래대로 바뀐다.


아니면, virtual env를 사용해서 python 2.7.9용으로 따로 만들어서 사용해야 한다.





python 2.7.9로 업그레이드 하는 이유는..

pip 땜시.. https://pip.pypa.io/en/stable/installing/

Do I need to install pip?

pip is already installed if you're using Python 2 >=2.7.9 




Posted by '김용환'
,

python-gssapi 설치

python 2016. 1. 25. 19:48

python-gssapi를 설치 내용을 작성한다. 




$ sudo pip install  python-gssapi

Collecting python-gssapi

  Using cached python-gssapi-0.6.4.tar.gz

    zip_safe flag not set; analyzing archive contents...

    six: module references __path__

    Installed /tmp/pip-build-z6H2Wq/python-gssapi/six-1.10.0-py2.7.egg

    Searching for cffi>=0.8

    Reading http://pypi.python.org/simple/cffi/

    Best match: cffi 1.5.0

    Downloading https://pypi.python.org/packages/source/c/cffi/cffi-1.5.0.tar.gz#md5=dec8441e67880494ee881305059af656

    Processing cffi-1.5.0.tar.gz

    Writing /tmp/easy_install-EHxKhb/cffi-1.5.0/setup.cfg

    Running cffi-1.5.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-EHxKhb/cffi-1.5.0/egg-dist-tmp-DxMePM

    Package libffi was not found in the pkg-config search path.

    Perhaps you should add the directory containing `libffi.pc'

    to the PKG_CONFIG_PATH environment variable

    No package 'libffi' found

    Package libffi was not found in the pkg-config search path.

    Perhaps you should add the directory containing `libffi.pc'

    to the PKG_CONFIG_PATH environment variable

    No package 'libffi' found

    Package libffi was not found in the pkg-config search path.

    Perhaps you should add the directory containing `libffi.pc'

    to the PKG_CONFIG_PATH environment variable

    

    .....

    

    


에러만 난 것을 확인하려면, 아래 pkg-config 커맨드를 사용한다.


$ pkg-config --print-errors libffi

Package libffi was not found in the pkg-config search path.

Perhaps you should add the directory containing `libffi.pc'

to the PKG_CONFIG_PATH environment variable

No package 'libffi' found




libffi-devel을 설치한다. 


$ sudo yum install libffi-devel


$ ls -al /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libffi.so

lrwxrwxrwx 1 root root 15 2016-01-25 18:20 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libffi.so -> libffi.so.5.0.6


$ cd /usr/lib

$ ls -al libffi.so

lrwxrwxrwx 1 root root 15 2016-01-25 18:20 libffi.so -> libffi.so.5.0.6



$ rpm -ql libffi-devel

/usr/lib/libffi-3.0.5

/usr/lib/libffi-3.0.5/include

/usr/lib/libffi-3.0.5/include/ffi.h

/usr/lib/libffi-3.0.5/include/ffitarget.h

/usr/lib/libffi.so

/usr/lib/pkgconfig/libffi.pc

/usr/share/info/libffi.info.gz

/usr/share/man/man3/ffi.3.gz

/usr/share/man/man3/ffi_call.3.gz

/usr/share/man/man3/ffi_prep_cif.3.gz

/usr/lib64/libffi-3.0.5

/usr/lib64/libffi-3.0.5/include

/usr/lib64/libffi-3.0.5/include/ffi.h

/usr/lib64/libffi-3.0.5/include/ffitarget.h

/usr/lib64/libffi.so

/usr/lib64/pkgconfig/libffi.pc

/usr/share/info/libffi.info.gz

/usr/share/man/man3/ffi.3.gz

/usr/share/man/man3/ffi_call.3.gz

/usr/share/man/man3/ffi_prep_cif.3.gz



python-gssapi를 pip로 설치한다. 환경변수 -E 때문에 골치아파질 수 있으니 환경변수를 포함하려면 -E를 고려한다.



$ sudo -E pip install python-gssapi

Successfully installed cffi-1.5.0 python-gssapi-0.6.4 six-1.10.0




* multilib 충돌 해결하기

중간에 libffi를 여러 번 설치하다가 3.0.9와 3.0.5가 꼬이는 이슈가 발견했다. libffi는 dependecy가 굉장히 넓어서 해당 모듈만 삭제하도록 해야 한다.

 

 

$  rpm -qa | grep libffi

libffi-3.0.9-1.el5.rf.x86_64


$ sudo yum remove libffi-3.0.9-1.el5.rf.x86_64

<- dependency 때문에 못 지운다.


Protected multilib versions: libffi-3.0.5-3.2.el6.i686 != libffi-3.0.9-1.el5.rf.x86_64 ++ return 1 ++ return 1 Requirements installation failed with status: 1.



이 때는 --nodeps 옵션으로 삭제하고 libffi를 다시 설치한다.



$ sudo rpm -e --nodeps libffi



Posted by '김용환'
,

pip 간단 설명서

python 2016. 1. 25. 11:22


내가 자주 쓰는 pip 간단설명서


0. pip설치

python 2.7.9에 해당되는 pip 설치하기

http://knight76.tistory.com/entry/python-python-279%EC%97%90-%ED%95%B4%EB%8B%B9%EB%90%98%EB%8A%94-pip-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0



1. pip 설치 목록 확인하기

간단하게 설치여부만 확인하고 싶다면, pip list 커맨드를 사용하고, 더 자세히 보고 싶다면, pip show 커맨드를 사용한다.

pip list 

pip list | grep 바이너리

pip show 바이너리


$ pip list | grep SQLAlchemy

SQLAlchemy (1.0.11)


$ pip show SQLAlchemy

---

Name: SQLAlchemy

Version: 1.0.11

Location: /usr/local/lib/python2.7/site-packages

Requires:


구 버전 체크

$ pip list  --outdated



2. pip 설치

pip install

centos라면, sudo pip install 바이너리

proxy를 사용하면, sudo https_proxy=proxy.google.com:22222  http_proxy=proxy.google.com: 22222 pip install  바이너리



버전 지정을 할 수 있다.


 pip install python-gssapi

 pip install 'python-gssapi==0.6.4'

 pip install 'python-gssapi>=0.6.4'




3. pip 삭제

pip uninstall

centos라면, sudo pip uninstall 바이너리




4. pip 검색




$  pip search "scp"

pyxmlescpos               - Print XML-defined Receipts on ESC/POS Receipt Printers

ssh4py                    - SSH4Py is a Wrapper for LibSSH2 (SSH, SFTP, SCP)

python-rdomanager-oscplugin - RDO Manager OSC plugin lib

PyESCPOS                  - Support for Epson ESC/POS printer command system.

python-escpos             - Python library to manipulate ESC/POS Printers

cmdssh                    - Execute commands on local machine and on remote machine via ssh, and a wrapper for paramikos scp.

yascp                     - Yet Another Simple Configuration Parser for INI-style configuration files.

scp                       - scp module for paramiko

check_nscp                - General purpose client for talking with NSclient++

scpy                      - File/Directory syncing over scp

scpclient                 - scp client, for use with paramiko.

PyUpdater-SCP-Plugin      - SCP Uploader Extension

scpi                      - Implement SCPI in pure Python

gocept.zestreleaser.customupload - Plug-in for zest.releaser to allow uploading the created egg via SCP to configurable destinations.

universal_usbtmc          - United interface to different USBTMC (or "SCPI") intstrument implementations in Python.

robotframework-scplibrary - Robot Framework test library for transferring files via Secure Copy (SCP)

onkyo-eiscp               - Control Onkyo receivers over ethernet.

lesscpy                   - Python LESS compiler

bloscpack                 - Command line interface to and serialization format for Blosc

scpack                    - A random test lib

escposprinter             - Python library to manipulate ESC/POS Printers with support for python >= 3

PyiUpdater-SCP-Plugin     - SCP Uploader Extension

UsCpi                     - Tools for parsing the latest US Consumer Price Index

drop                      - Uploads data to a remote www directory via scp and returns a public url.

chef-ssh                  - Chef SSH is a tool to easily query the Chef server's inventory for running scp/ssh to/from the machines

igloo                     - A command line SCP client

scp-marvin                - SCP Wiki entry retrieval script

iscpy                     - Python library to parse ISC style config files.



그 외..

pip 사용 가이드:  https://pip.pypa.io/en/stable/user_guide/

Posted by '김용환'
,



centos에서 python2.7.9에 맞는 pip 설치시 유의해야 할 것이 있다.


centos에서 sudo yum install python-pip 를 실행하면 7.1.0 이 설치되고 python2.7.9와 연동이 되지 않는다.



따라서, 따로 pip를 설치하는 것이 좋은 것 같다.



wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-6.0.6.tar.gz


tar zxvf pip-6.0.6.tar.gz


cd pip-6.0.6


python setup.py build


sudo python setup.py install



/usr/local/bin/pip으로 설치되었다.  pip를 path에 넣어주면 동작이 잘된다.


$ sudo pip install SQLAlchemy




setupmodule 모듈 없다고 하면 다음을 설치한다.



$ wget https://bootstrap.pypa.io/ez_setup.py 

sudo -E /usr/local/bin/python2.7 ez_setup.py


$ /usr/local/bin/python2.7 setup.py build

성공

$ sudo -E /usr/local/bin/python2.7 setup.py install

성공 Finished processing dependencies for pip==6.0.6


테스트로 hadoopfs client인 snakebite를 pip로 설치한다.

$ sudo -E pip install snakebite 

성공



'python' 카테고리의 다른 글

python-gssapi 설치  (0) 2016.01.25
pip 간단 설명서  (0) 2016.01.25
[python] sys모듈의 sys.path 추가하기  (0) 2016.01.22
[python] zookeeper client - kazoo 예제  (0) 2016.01.05
[python] json 처리  (0) 2016.01.05
Posted by '김용환'
,


sys.path.append()는 파이썬에서 모듈이나 파이썬 파일 읽을 때, 유용한 명령어이다. 자바의 classpath와 비슷하게 python path을 추가할 수 있다.



$ python

>>> import sys

>>> sys.path

['', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

>>> sys.path.append("/mydev/")

>>> sys.path

['', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages', '/mydev/']



'python' 카테고리의 다른 글

pip 간단 설명서  (0) 2016.01.25
[python] python 2.7.9에 해당되는 pip 설치하기  (0) 2016.01.22
[python] zookeeper client - kazoo 예제  (0) 2016.01.05
[python] json 처리  (0) 2016.01.05
pythonic 이란.  (0) 2015.08.25
Posted by '김용환'
,



python kazoo 클라이언트로 zookeeper 값 얻어오기.


http://kazoo.readthedocs.org/en/latest/basic_usage.html



#!/usr/bin/python


from kazoo.client import KazooClient

from kazoo.exceptions import NoNodeError

from kazoo.exceptions import NodeExistsError


import logging

import json

import sys


logging.basicConfig()


zk_key="/com/google/kazoo"


zk = KazooClient(hosts='a.google.com:2181,b.google.com:2181,c.google.com:2181')


zk.start()


try :

print zk.exists(zk_location_redis_key)

data, stat = zk.get(zk_key)

print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))

decoded = json.loads(data)

hostAndport = decoded['slaves'][0]

print hostAndport

except (ValueError, KeyError, TypeError):

print "JSON format error"

except (NoNodeError, NodeExistsError):

print "zk error"


zk.stop()





Posted by '김용환'
,

[python] json 처리

python 2016. 1. 5. 14:14


python에서 json 처리 예제. 




>>> import json


>>> json_string = '{"master":"1.1.1.1:1111", "slave":["2.2.2.2:2222", "3.3.3.3:3333"]}'


>>> decoded = json.loads(json_string)


>>> print json.dumps(decoded, indent=2)

{

  "slave": [

    "2.2.2.2:2222",

    "3.3.3.3:3333"

  ],

  "master": "1.1.1.1:1111"

}


>>> print json.dumps(decoded, indent=2, sort_keys=True)

{

  "master": "1.1.1.1:1111",

  "slave": [

    "2.2.2.2:2222",

    "3.3.3.3:3333"

  ]

}



>>> print decoded['master']

1.1.1.1:1111


>>> print decoded['slave']

[u'2.2.2.2:2222', u'3.3.3.3:3333']


>>> print decoded['slave'][0]

2.2.2.2:2222


>>> print decoded['slave'][1]

3.3.3.3:3333

Posted by '김용환'
,

pythonic 이란.

python 2015. 8. 25. 20:35



아. 처음 들어본 단어 pythonic은 파이썬 개발 방식(또는 파이썬 커뮤니티가 수긍하는 개발 방식)을 따르는 것을 의미하는 것 같다.


http://blog.startifact.com/posts/older/what-is-pythonic.html

http://stackoverflow.com/questions/25011078/what-does-pythonic-mean



Exploting the features of the Python language to produce code that is clear, concise and maintainable.

Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used.

This is maybe easiest to explain by negative example, as in the linked article from the other answers. Examples of unpythonic code often come from users of other languages, who instead of learning a Python programming patterns such as list comprehensions or generator expressions, attempt to crowbar in patterns more commonly used in C or java. Loops are particularly common examples of this.

For example in Java I might use

for i in (i; i < items.length ; i++)
 {
  n = items[i];
 ... now do something
 }

In Python we can try and replicate this using while loops but it would be cleaner to use

enter code here
for i in items:
  i.perform_action()

Or, even a generator expression

(i.some_attribute for i in items)

So essentially when someone says something is unpythonic, they are saying that the code could be re-written in a way that is a better fit for pythons coding style.

Typing import this at the command line gives a summary of Python principles. Less well known is that the source code for "import this" is decidedly, and by design, unpythonic! Take a look at it for an example of what know to do.



Posted by '김용환'
,

Pandas 라이브러를 하루 정도 이용해보고 나서 얻은 좋은 정보들이다.


pandas의 slice, dataframe 공부가 쉽지 않다. pandas.pydata.org 의 데이터만으로 해결할 수 있는 문제는 stack overflow를 참조해야 한다. 


https://wikidocs.net/1910


http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.query.html

http://pandas.pydata.org/pandas-docs/stable/dsintro.html#series

http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.query.html

http://cinema4dr12.tistory.com/entry/Statistics-Descriptive-Statistics-Sample-Statistics

http://stackoverflow.com/questions/26414913/normalize-rows-of-pandas-data-frame

http://stackoverflow.com/questions/18792918/pandas-combining-2-data-frames-join-on-a-common-column







Posted by '김용환'
,