20160331_automate the boring stuff with python

29
Copyright@ 2016 All reserved by KrDAG AUTOMATE THE BORING STUFF WITH PYTHON 삶을 윤택하게 해주는 코딩 KRDAG 장성만 ([email protected])

Upload: sungman-jang

Post on 13-Feb-2017

171 views

Category:

Engineering


7 download

TRANSCRIPT

Page 1: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

AUTOMATE THE BORING STUFF WITH PYTHON 삶을 윤택하게 해주는 코딩

KRDAG

장성만 ([email protected])

Page 2: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Part 1 - The Basics of Python Programming (~143p) Introduction / Python Basics / Flow Control / Functions Lists / Dictionaries and Structuring Data / Manipulating Strings

Part 2 - Automating Tasks (~439p) Pattern Matching with Regular Expressions Reading and Writing Files / Organizing Files Debugging / Web Scraping Working with Excel Spreadsheets Working with PDF and Word Documents Working with CSV Files and JSON Data Time, Scheduling Tasks, and Launching Programs Sending Email and Text Messages Manipulating Images Controlling the Keyboard and Mouse with GUI Automation

책 소개 Automate the Boring Stuff with Python

https://automatetheboringstuff.com/ https://github.com/iliyahoo/Automate-The-Boring-Stuff-With-Python

Page 3: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Comparison to Java / C++ Python run slower; but take much less time to develop. 3-5 times shorter than Java, 5-10 times shorter than C++ no type declaring, powerful polymorphic list and dictionary types Python as a "glue" language, while others as a low-level implementation language. Everything in python is an object, and it sits on the heap

Python is similar to Java Python is interpreted language while C++ is a compiled language Python class members are determined at run-time C++ has no has automatic memory management (garbage collection) Python have a full set of reflection capabilities; Serialization by pickle.saves()

WHY PYTHON? Trade-off between Productivity and Performance?

https://www.python.org/doc/essays/comparisons/ https://www.quora.com/What-is-the-difference-between-Python-and-C++#!n=12

Page 4: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Python 기초배우기

Page 5: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 1. Python Basics

https://en.wikipedia.org/wiki/High_availability

#include <stdio.h> int main() { float percent; scanf("%f", &percent); printf("%f",(100-percent)*0.01*365*24*60); return 0; } gcc –o main main.c ./main

Presenter
Presentation Notes
(100-float(input()))*0.01*365*24*60
Page 6: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 2. Flow Control

@cafepear

Presenter
Presentation Notes
while input('최근 살이 쪘나요?')!='Yes': print('다이어트하세요ㅠ') print('1577-1991')
Page 7: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 2. Flow Control

http://www.soccerline.co.kr/slboard/view.php? uid=1988123089&page=41&code=totalboard

Presenter
Presentation Notes
print('저런! 커트 앵글의 머리밀기 저주에 걸리셨군요!') i=0 while i < 3: comment=input('댓글을 입력하세요:') if comment=='빅쇼 모발이여 자라나라 머리머리!': i=i+1 print('저주가 풀렸습니다')
Page 8: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 3. Functions

Presenter
Presentation Notes
def printNines(percent): print ((100-percent)*0.01*365*24*60) userInput=float(input('가용성 수치를 입력하세요:')) printNines(userInput) def printNines(percent): result=(100-percent)*0.01*365*24*60 if result > 1: print (str(result)+'분') else: print (str(result*60)+'초') userInput=float(input('가용성 수치를 입력하세요:')) printNines(userInput)
Page 9: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 4. Lists

Presenter
Presentation Notes
import random foods=['한식','중식','양식','일식','치킨','기타'] food=random.choice(foods) print(food) foods=['한식','중식','양식','일식','치킨','기타'] food=foods[4] print(food)
Page 10: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 5. Dictionaries and Structuring Data

http://kin.naver.com/qna/detail.nhn?d1id=3&dirId=30104&docId=201995942&qb=7Jew7JiI7J24IDEwMOuqhQ==&enc=utf8&section=kin&rank=1&search_sort=0&spq=0

Presenter
Presentation Notes
import operator message = "김태희 송혜교 전지현 한가인 성유리 하지원 유인나 손예진 하연수 김소은 구혜선 고아라 박보영 문근영 서우 이민정 임수정 윤승아 김사랑 한효주 이연희 신민아 홍수현 홍수아 이나영 윤은혜 박한별 문채원 진세연 한그루 고준희 박민영 송지효 박신혜 한예슬 한지민 김하늘 김희선 남규리 박하선 수애 민효린 강소라 김지원 장나라 백진희 한혜진 신세경 이열음 클라라 이유비 심은경 오연서 윤소희 수지 윤아 손나은 설리 크리스탈 박초롱 태연 아이유 지연 나나 혜리 유라 강민경 소희 손담비 산다라박 전효성 지민 권리세 티파니 구하라 유리 유이 현아 에일리 민아 초아 가인 한선화 주니엘 리지 세라 우희 김재경 혜이니 앤씨아 주연 보아 김지숙 한승연 서현 도희 설현 지나 선미 빅토리아 박봄" count = {} for character in message: count.setdefault(character, 0) count[character] = count[character] + 1 sortedCount = sorted(count.items(), key=operator.itemgetter(1)) print(sortedCount)
Page 11: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 1 - THE BASICS OF PYTHON PROGRAMMING Chapter 6. Manipulating Strings

Presenter
Presentation Notes
import pyperclip clip=pyperclip.paste() Pyperclip.copy(clip.upper()) print(clip.upper())
Page 12: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Python 활용하기

Page 13: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Chapter 7. Pattern Matching with Regular Expressions

Presenter
Presentation Notes
import re searchRegex=re.compile(r'(SKT|SK텔레콤).+(네트워크|SDN)') myfile=open('c:\\sdn.txt') contents=myfile.readlines() for line in contents: result = searchRegex.search(line) if result: print(result.string)
Page 14: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Chapter 8/9. Reading and Writing Files / Organizing Files

Presenter
Presentation Notes
import os,re searchRegex=re.compile(r'\d{4}-\d{2}-\d{2}') targetDir=“C:\\2016-02-15 Cisco Live 2016 Berlin\\" for filename in os.listdir(targetDir): if not searchRegex.search(filename): os.rename(targetDir+filename, targetDir+"2016-02-15 "+filename)
Page 15: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Chapter 11. Web Scraping

http://xkcd.com/927/

Presenter
Presentation Notes
import requests, os, bs4 url = 'http://xkcd.com' os.makedirs('xkcd', exist_ok=True) while not url.endswith('#'): print('Downloading page %s...' % url) res = requests.get(url) soup = bs4.BeautifulSoup(res.text) comicElem = soup.select('#comic img') comicUrl = comicElem[0].get('src') print('Downloading image %s...' % (comicUrl)) res = requests.get('http:'+comicUrl) imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb') for chunk in res.iter_content(100000): imageFile.write(chunk) imageFile.close() prevLink = soup.select('a[rel="prev"]')[0] url = 'http://xkcd.com' + prevLink.get('href')
Page 16: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Chapter 13. Working with PDF and Word Documents

https://github.com/rk700/PyMuPDF

Presenter
Presentation Notes
import fitz,os for filename in os.listdir('.'): if filename.endswith('pdf'): print(filename) doc = fitz.Document(filename) if doc.needsPass and doc.authenticate('PW'): doc.save('decrypted_'+filename)
Page 17: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

PART 2 - AUTOMATING TASKS Chapter 18. Controlling the Keyboard and Mouse with GUI Automation

Presenter
Presentation Notes
import pythoncom, pyHook def OnKeyboardEvent(event): print('Key:'+event.Key) return True hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent hm.HookKeyboard() pythoncom.PumpMessages()
Page 18: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

APPENDIX Goorm위에서 개발하기 http://www.goorm.io/

Page 19: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 20: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 21: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 22: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 23: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 24: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 25: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 26: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 27: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 28: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG

Page 29: 20160331_Automate the boring stuff with python

Copyright@ 2016 All reserved by KrDAG