반응형

상황

스트링으로 되어있는 비교 연산자를 사용해서 비교를 할 수 있을까?

 

해결

import operator


def check(left, string_operator, right):
    operators = {'>': operator.gt,
                 '<': operator.lt,
                 '>=': operator.ge,
                 '<=': operator.le,
                 '=': operator.eq}
    return operators[string_operator](left, right)


print(check(3, '>', 2))  # True
print(check(3, '<', 2))  # False
print(check(3, '>=', 2))  # True
print(check(3, '<=', 2))  # False
print(check(3, '=', 2))  # False

 

반응형
반응형

서문

파이썬에서 string format을 지정하는 방식에 대해서 정리해보자

 

 

방법

# 1
string = '%s %d' % (STR_VALUE, INT_VALUE)

# 2
VALUE_LIST = [1,2,3,4]
'value {0} {1}'.format(*VALUE_LIST)

# 3
VALUE_LIST = {'name':'abc', 'ok':'ddd'}
'VALUE {name} {ok}'.format(**VALUE_LIST)


# 4
'string : {} int : {}'.format("Something", 100)

# 5
'string : {0} int : {1}'.format("Something", 100)

# 6
'string : {name} int_value : {int_value}'.format(name="abc", int_value=100)

# 7
'string {0} int_value {int_value}'.format("abc", int_value=100)'

 

 

반응형
반응형
brew install python3

# 어쩌고 저쩌고 에러 나면
sudo mkdir /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/Frameworks/
brew link python

# edit bash_profile
vi ~/.bash_profile
alias python='python3'

# reload
. ~/.bash_profile
반응형
반응형

PyJWT 를 사용해서 JWT(json web token) 구현해보자

설치

pip install pyjwt

 

알고리즘 'HS256', 'HS384', 'HS512' 를 쓸 경우엔 위와 같이 설치한다.

 

그냥 해보니 

NotImplementedError: Algorithm 'RS256' could not be found. Do you have cryptography installed?

 

에러가 발생.. 소스 확인해보니,

알고리즘 'RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES521', 'ES512', 'PS256', 'PS384', 'PS512'

를 쓸 경우엔 추가 설치 필요한 것 같다.

 

pip install pyjwt[crypto]

 

예제

RS256 알고리즘 사용할 예정

 

소스 코드

 

import time
import jwt

# Encoding
private_key = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
now = int(time.time())
exp = now + 2000
jwt_payload = {'iss': 'ISS', 'iat': now, 'exp': exp}
encoded = jwt.encode(jwt_payload, key, 'RS256')

# Decoding
public_key = '...'
decoded = jwt.decode(encoded, public_key, algorithms='RS256')

 

 

JWT : ( https://jwt.io/introduction/ )

PyJWT : ( https://pyjwt.readthedocs.io/en/latest/usage.html#encoding-decoding-tokens-with-rs256-rsa )

 

 

 

 

 

반응형
반응형

AttributeError: module 'yaml' has no attribute 'FullLoader'

 

FullLoader 는 PyYAML 5.1 이후부터 사용 가능하니, PyYAML 업데이트 하세요

 

 

pip install -U PyYAML

 

반응형

'python' 카테고리의 다른 글

[python] string format  (0) 2020.03.19
[Python] python3으로 업그레이드 (mac)  (0) 2020.02.28
[python][pyjwt] JWT example  (0) 2020.02.03
[Python] python3으로 업그레이드 (centos7)  (0) 2020.01.14
[Python] pip 설치 centos7  (0) 2020.01.14
반응형
sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
sudo yum update
sudo yum install -y python36u python36u-libs python36u-devel python36u-pip
python3.6 -V

 

반응형
반응형
sudo yum install python-pip -y

 

반응형

+ Recent posts