This tells Python that the string is a “raw string” – its contents should not interpret backslashes.


In normal Python strings, backslashes are used for escaping special characters – such as in the string “\n”, which is a one-character string containing a newline. When you add the r to make it a raw string, Python does not apply its backslash escaping – so, “r'\n'” is a two-character string containing a literal backslash and a lowercase “n”.


Django는 URL 패턴을 검사하기 전에 들어오는 모든 URL 앞에 슬래시를 제거합니다.


즉 /hello/ 라고 치면 제일 처음 / 는 의미가 없어져버리는 것임.


그리고 제일 뒤에 $ 달러사인이 없어버리면 모든 


^hello/ 라고 쳤을 경우 뒤에 모든 문자열이 매칭이 되어버림.


꼭 뒤에 $ 를 붙이도록 하자.


후행슬래쉬는 무조건 필요하다. APPEND_SLASH 설정이 되어있을경우는

없어도 된다.


또한 중요하게 생각해야 한다는 것은. hello라는 Function을 전달했다는 것이 포인트이다.


이것은 Python의 핵심기능중 하나임. (함수포인터라고 생각하면 편할듯...)


[장고에서 자주사용하는 정규표현식]


SymbolMatches
. (dot)Any single character
\dAny single digit
[A-Z]Any character between A and Z (uppercase)
[a-z]Any character between a and z (lowercase)
[A-Za-z]Any character between a and z (case-insensitive)
+One or more of the previous expression (e.g., \d+ matches one or more digits)
[^/]+One or more characters until (and not including) a forward slash
?Zero or one of the previous expression (e.g., \d? matches zero or one digits)
*Zero or more of the previous expression (e.g., \d* matches zero, one or more than one digit)
{1,3}Between one and three (inclusive) of the previous expression (e.g., \d{1,3} matches one, two or three digits)


'Python > Django' 카테고리의 다른 글

[Django] RestFrameWork 튜토리얼 - 1. 직렬화  (0) 2017.03.10
[Django] 템플릿 태그와 필터  (0) 2017.03.08
[Django] 템플릿  (0) 2017.03.08
[Django] Dynamic URL  (0) 2017.03.08
[Django] VIew와 URL conf  (0) 2017.03.07
Posted by C마노
,
class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        print "getter of x called"
        return self._x

    @x.setter
    def x(self, value):
        print "setter of x called"
        self._x = value

    @x.deleter
    def x(self):
        print "deleter of x called"
        del self._x


'Python' 카테고리의 다른 글

[Flask] Flask - wsgi - nginx 연동.  (0) 2017.05.15
[Python] YAPF  (0) 2017.03.11
[Python] 공부해야 할 것.  (0) 2017.02.15
[Python]pywin32 설치기.  (0) 2016.12.15
python vim설정  (0) 2016.12.05
Posted by C마노
,

for문을 돌거나 다른 반복 처리중 보이는 마지막 몇가지 항목을 유지하고 싶다면 어떻게 할까?


그렇다면 collections 모듈에 deque를 import해서 사용하면 된다.


아래의 코드는 특정 텍스트를 인자로 던져주면 해당 특정 텍스트를 파일에서 찾게되면,


해당라인의 전 history 라인만큼 리턴해주는 Python 코드이다


from collections import deque

def search(lines, pattern, history=5):
    previous_lines = deque(maxlen=history)
    for line in lines:
        if pattern in line:
            yield line, previous_lines
        previous_lines.append(line)

# Example use on a file
if __name__ == '__main__':
    with open('somefile.txt') as f:
        for line, prevlines in search(f, 'python', 5):
            for pline in prevlines:
                print(pline, end='')
            print(line, end='')
            print('-'*20)

어떠한 item을 검색하기 위한 코드를 작성할때는 yield를 쓰는것이 일반적이다.


Posted by C마노
,