바로 실전에 적용할 수 있는 파이썬 웹 개발 레시피. 기초적인 파이썬 지식이 필요합니다.
앞으로 다룰 내용에서 알아둬야 할 파이썬 기능을 다시 한번 짚고 넘어갑니다. 이미 파이썬 고수라면 건너뛰셔도 좋습니다!
- 모듈
import re | |
import urllib | |
from bs4 import BeautifulSoup | |
import datetime | |
def read_percentage(soup): | |
return float(re.search(r'\((\d+\.\d+)\)',repr(soup)).groups()[0]) | |
f = urllib.urlopen('http://info.nec.go.kr/electioninfo/electionInfo_report.xhtml?electionId=0020121219&requestURI=%2Felectioninfo%2F0020121219%2Fvc%2Fvccp09.jsp&topMenuId=VC&secondMenuId=VCCP&menuId=VCCP09&statementId=VCCP09_%231&electionCode=1&cityCode=0&sggCityCode=0&x=26&y=8') | |
html = f.read() |
# encoding = utf-8 | |
require 'nokogiri' | |
require 'iconv' | |
require 'table_parser' | |
module TableParser | |
class Table | |
def each | |
return unless block_given? |
# Python 2.5 & 2.6 | |
1/0 # ZeroDivisionError: integer division or modulo by zero | |
1%0 # ZeroDivisionError: integer division or modulo by zero | |
1.0/0 # ZeroDivisionError: float division | |
1.0%0 # ZeroDivisionError: float modulo | |
# Python 2.7 | |
1/0 # ZeroDivisionError: integer division or modulo by zero | |
1%0 # ZeroDivisionError: integer division or modulo by zero | |
1.0/0 # ZeroDivisionError: float division by zero | |
1.0%0 # ZeroDivisionError: float modulo |
$ python2.7 id_anomaly.py | |
old_func is not new_func | |
('old_func:', 41458576, <bound method classobj.member_func of <class __main__.A at 0x278bf58>>) | |
('new_func:', 41314096, <bound method classobj.member_func of <class __main__.A at 0x278bf58>>) | |
$ python3.2 id_anomaly.py | |
old_func is not new_func | |
old_func: 140100968595896 <bound method type.member_func of <class '__main__.A'>> | |
new_func: 140100968596184 <bound method type.member_func of <class '__main__.A'>> |
export PROJECTS_HOME="$HOME/Projects" | |
function has_virtualenv__() { | |
if [[ ${PWD##$PROJECTS_HOME} != $PWD ]]; then | |
IFS="/" read -ra ADDR <<< "${PWD##$PROJECTS_HOME}" | |
venvname=${ADDR[1]} | |
cur_env=${VIRTUAL_ENV##$WORKON_HOME} | |
if [[ $venvname != "" ]] && [[ -d "$WORKON_HOME/$venvname" ]]; then | |
if [[ ${cur_env:1} != $venvname ]]; then | |
workon "$venvname" |
# coding=utf-8 | |
def score(s, encoding=None): | |
if encoding: s = s.encode(encoding) | |
import hashlib; a = hashlib.md5(s).digest() | |
return (int(round(10+ord(a[0])/255.*90)), # 공격 | |
int(round(10+ord(a[5])/255.*90)), # 민첩 | |
int(round(10+ord(a[1])/255.*90)), # 방어 | |
int(round(10+ord(a[2])/255.*90)), # 명중 | |
int(round(10+ord(a[3])/255.*90)), # 운 | |
int(round(100+ord(a[4])/255.*200))) # 체력 |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Leaflet</title> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.3.1/leaflet.css" /> | |
<script src="http://cdn.leafletjs.com/leaflet-0.3.1/leaflet.js"></script> | |
<script src="http://maps.google.com/maps/api/js?v=3.2&sensor=false"></script> | |
<script src="http://matchingnotes.com/javascripts/leaflet-google.js"></script> | |
</head> | |
<body> |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!