Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ docs/_build/

# PyBuilder
target/

# Mac File
.DS_Store
5 changes: 5 additions & 0 deletions ddkangfu/0000/0000.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

from PIL import Image, ImageDraw, ImageFont

"""
0000:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。
类似于图中效果 http://i.imgur.com/sg2dkuY.png?1
"""


def draw_number(file_name, number):
image = Image.open(file_name)
Expand Down
50 changes: 50 additions & 0 deletions ddkangfu/0005/0005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding=utf-8

import os

from PIL import Image

"""
0005:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
"""

# iphone 5 1136×640
IPHONE5_X = 1136
IPHONE5_Y = 640


def get_resized_file_name(file_name):
return "%s_thumb.jpg" % file_name[:-4]


def resize_image(file_name):
img = Image.open(file_name)
x, y = img.size
img_resize = None

if x > IPHONE5_X:
new_y = int(max(y * IPHONE5_X / x, 1))
print (IPHONE5_X, new_y)
img_resize = img.resize((IPHONE5_X, new_y))
elif y > IPHONE5_Y:
new_x = int(max(x * IPHONE5_Y / y, 1))
print (new_x, IPHONE5_Y)
img_resize = img.resize((new_x, IPHONE5_Y))

if img_resize:
file_resize = get_resized_file_name(file_name)
print 'Resize file: %s ' % file_resize
img_resize.save(file_resize, "JPEG")


def walk_dir(image_path):
for root, dirs, files in os.walk(image_path):
for f in files:
if f.lower().endswith('jpg') and not f.lower().endswith('_thumb.jpg'):
full_path = os.path.join(root, f)
resize_image(full_path)


if __name__ == '__main__':
walk_dir("images")
#get_resized_file_name("001.jpg")
Binary file added ddkangfu/0005/images/001.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ddkangfu/0005/images/002.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ddkangfu/0005/images/003.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ddkangfu/0005/images/004.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions ddkangfu/0007/0007.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# coding=utf-8

import os
import re

"""
0007:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
"""

def source_statistic(file_name):
total = 0
blank = 0
comment = 0

with open(file_name) as f:
lines = f.readlines()
total += len(lines)

pattern1 = re.compile(r'^\s*$')
pattern2 = re.compile(r'^\s*\#+')

for line in lines:
if pattern1.match(line):
blank += 1
elif pattern2.match(line):
comment += 1

return total, blank, comment


def walk_dir(image_path):
total = 0
blank = 0
comment = 0

for root, dirs, files in os.walk(image_path):
for f in files:
if f.lower().endswith('.py'):
full_path = os.path.join(root, f)
#resize_image(full_path)
print full_path
result = source_statistic(full_path)
total += result[0]
blank += result[1]
comment += result[2]

return total, blank, comment


if __name__ == '__main__':
result = walk_dir("source")
print 'Total Lines: %d, Blank Lines: %d, Comment Lines: %d' % result
22 changes: 22 additions & 0 deletions ddkangfu/0007/source/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.conf import settings
from django.conf.urls import patterns, url
from django.core.urlresolvers import LocaleRegexURLResolver


def i18n_patterns(prefix, *args):
"""
Adds the language code prefix to every URL pattern within this
function. This may only be used in the root URLconf, not in an included
URLconf.

"""
pattern_list = patterns(prefix, *args)
if not settings.USE_I18N:
return pattern_list
return [LocaleRegexURLResolver(pattern_list)]


# url
urlpatterns = patterns('',
url(r'^setlang/$', 'django.views.i18n.set_language', name='set_language'),
)
27 changes: 27 additions & 0 deletions ddkangfu/0007/source/static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re

from django.conf import settings
from django.conf.urls import patterns, url
from django.core.exceptions import ImproperlyConfigured


def static(prefix, view='django.views.static.serve', **kwargs):
"""
Helper function to return a URL pattern for serving files in debug mode.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

"""
# No-op if not in debug mode or an non-local prefix
if not settings.DEBUG or (prefix and '://' in prefix):
return []
elif not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
return patterns('',
url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
)
21 changes: 21 additions & 0 deletions ddkangfu/0008/0008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding=utf-8

import urllib2

from bs4 import BeautifulSoup

"""
0008:一个HTML文件,找出里面的正文。
"""


def show_content(url, content_tag, content_tag_class):
content = urllib2.urlopen(url).read()

soup = BeautifulSoup(content)
for tag in soup.find_all(content_tag, content_tag_class):
print tag.text


if __name__ == '__main__':
show_content('http://v2ex.com/t/157721#reply10', 'div', 'topic_content')
21 changes: 21 additions & 0 deletions ddkangfu/0009/0009.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding=utf-8

import urllib2

from bs4 import BeautifulSoup

"""
0009:一个HTML文件,找出里面的链接。
"""


def show_href(url):
content = urllib2.urlopen(url).read()

soup = BeautifulSoup(content)
for tag in soup.find_all('a'):
print tag['href']


if __name__ == '__main__':
show_href('http://v2ex.com/t/157721')
56 changes: 56 additions & 0 deletions ddkangfu/0013/0013.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# coding=utf-8

import urllib2
import urllib
import os

from bs4 import BeautifulSoup

"""
0013:用 Python 写一个爬图片的程序,爬 [这个链接里的日本妹子图片 :-)](http://tieba.baidu.com/p/2166231880)
"""


def get_img_src_list(url, img_class):
content = urllib2.urlopen(url).read()

soup = BeautifulSoup(content)
#print soup
src_list = []

for img in soup.find_all('img', img_class):
src_list.append(img['src'])

return src_list


def download_img(src, download_path):
print 'Begin download image : %s ......' % src

file_name = src.split("/")[-1]
dist = os.path.join(download_path, file_name)

urllib.urlretrieve(src, dist, None)

print 'Download image %s Done.' % src



if __name__ == '__main__':
src_list = get_img_src_list('http://tieba.baidu.com/p/2166231880', 'BDE_Image')

if src_list:
save_path = os.path.abspath("./downlaod")
if not os.path.exists(save_path):
os.mkdir(save_path)

print 'Start download %d images.....' % len(src_list)

for src in src_list:
download_img(src, save_path)

print 'Download %d images done.' % len(src_list)
else:
print 'No Imges found !!!'


49 changes: 49 additions & 0 deletions ddkangfu/0014/0014.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# coding=utf-8

import json

import xlwt

"""
0014: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中,如下图所示:

![student.xls](http://i.imgur.com/nPDlpme.jpg)
"""


def load_json_file(file_name):
f = file(file_name)
json_data = json.load(f)
return json_data


def save_as_xls(file_name, data):
excel = xlwt.Workbook()

table = excel.add_sheet('sheet0')

row = 0

for k, v in data.items():
table.write(row, 0, k)
table.write(row, 1, v[0])
table.write(row, 2, v[1])
table.write(row, 3, v[2])
table.write(row, 4, v[3])

row += 1

excel.save(file_name)


if __name__ == '__main__':
data = load_json_file("student.txt")

if data:
save_as_xls('student.xls', data)
5 changes: 5 additions & 0 deletions ddkangfu/0014/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}