matplotlib 이용하기

Reference

그래프를 위한 모듈이다. MatLab을 python에 적용한 듯한 느낌이다.
OS X에서 다음과 같이 설치하였다. 설치에 별다른 어려움은 없었다.

pip3 install matploblib

Reference의 코드대로 입력하였더니, 잘 그려졌다.
다음은 line을 긋는 예제이다.

import matplotlib.pyplot as plt 
plt.plot([1, 2, 3, 4])
plt.show()

다음 예제는 좌표에 점을 찍는 예제이다.

import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()

'Python' 카테고리의 다른 글

Python3와 encoding  (0) 2014.08.30
Python Module  (0) 2014.07.31
Python Note  (0) 2014.07.24
Code Academy - Python  (0) 2014.07.11

Python 3와 Encoding

Reference

Python을 공부하면서 Web Page를 Scrap하는 코드를 만들어보았다.
웹에 대한 지식이 없는지라, 여러가지 시행 착오를 겪었는데 특히 encoding에 애를 먹었다.

우리에게 익숙한 진수로 먼저 예를 들어보자.
화면에 보이는 10진수 10은 2진수로는 ‘0b1010’, 8진수로는 ‘0o12’, 16진수는 ‘0xA’로 표현할(encode) 수 있다.
이와 반대로 ‘10’ 이라는 숫자는 2진수로 해석하면 10진수 2, 8진수로 해석하면 10진수 8, 16진수로 해석하면 10진수 16이 될것이다.

이제 우리가 눈에 보이는 문자열(str)을 기준으로 시작해보자.
문자열은 화면에 보이는 문자들의 모임이다. 내부적으로는 어떤 식으로 되어 있던 간에(UTF건, ASCII건, CP949건) 적절하게 처리하여 사용자에게 보여준다. 그런데 이 문자열을 외부로 (web, local file 등) 전송하기 위해서는bytes형태로 전송되고, 그 bytes를 상대방이 이해할 수 있게 알려주어야 한다.
위에서 예를 들었던 ‘10’을 다시 생각해보자. 내가 Web page에서 byte 10을 읽어 왔는데, 이를 2진수, 8진수, 10진수에 따라 의미를 달리 해석하는것처럼 UTF-8, CP949에 따라 다른 결과가 나올것이다.

내 환경은 다음과 같다.

  • Python 3.4
  • Windows 7, OS X
  • Sublime Text 2

실제로 내가 겪었던 문제는 특정 Web page의 parsing 결과를 출력할때 에러가 발생하였다. 그런데 OS X와 Windows 결과가 다르고, 더 나아가 OS별 Sublime Text별로도 다르게 나와서 소위 멘붕에 빠졌다. (어쨌건 처음엔 다 안되었다). IDLE(Python GUI IDE)도 헷갈리고 하였고.

좀 더 확인해보니 문제가 발생하는 최초 문자는 ‘-‘ (EM DASH)인데, 이는 확장 ASCII table의 151에 해당한다 (http://www.ascii-code.com/). 따라서 이 문자를 기준으로 문제를 살펴보았다.

a = '—' # EM DASH     # <class 'str'>
b = a.encode('utf-8') # b'\xe2\x80\x94'  <class 'bytes'>
c = b.decode('utf-8') # '—' <class 'str'>
print(b)              # b'\xe2\x80\x94'
print(c)              # UnicodeEncodeError: 'cp949' codec can't encode character '\u2014' in position 0: illegal multibyte sequence

python 기준으로 정리해보자.
Python 문자는 내부적으로 어떤 bytes의 모임이다. 이를 PC에게 출력해달라고 bytes를 보내면, PC는 이 bytes를 적절하게 해석을 하고 해당 문자를 찾아서 보여주게 된다. 따라서 해당 bytes에 해당하는 문자를 찾지 못하면 에러가 발생하는 것이다.

다음을 이해하는 것이 중요하다.
문자는 숫자(bytes)로 코드화할(encode) 수 있고, 숫자(bytes)는 그것을 문자로 해석할(decode) 수 있다.

str  -- encode( ) --> bytes -- decode( ) --> str

Web page를 열어서 값을 읽어 오는 경우를 정리하면,
해당 값은 bytes 모임이고, 내 PC 내부적으로 처리하는 해석방법과 동일하다면 다행이지만, 그렇지 않다면 적절하게 바꿔줘야 할것이다.

근본 문제는 UTF-8로 encode된 bytes가 들어왔는데, 출력단에서는 이를 해석했고, CP949의 문자들로 출력하려 한것으로 보인다. 그런데 bytes 중에 EM DASH가 있는데, 이는 CP949에는 없는 문자인 듯하다. (일반적인 문자라서 그럴리는 없을텐데…) 따라서 최종단을 UTF-8로 변경하기로 한다.

Mac OS X
  • shell : 참고로 Mac OS X는 UTF-8로 처리되고, Web Page 내용도 UTF-8 형식이라 그냥 잘 되었다. (-_-) Sublime Text 2에서 에러가 발생했고 이리저리 코드를 손 댄 덕분에 처음에 잘 안되었던 것이다.
  • Sublime Text 2 : 실행하면 이상하게도 DOS형식의 ‘CP949’ 관련 에러가 발생한다. 이는 출력단으로 ‘CP949’로 내보내는 것이라고 판단되었고, Sublime Text 2의 Python Build 옵션을 수정했더니 잘 되었다. (Sublime Text 2 encoding error with python3 build )
Windows
  • cmd : Windows에서는 CP949를 사용한다. 위의 Python code를 실행하면 error가 발생하는데, cmd 창을 UTF-8로 바꿔줌으로써 정상적으로 실행되었다. (cmd(도스 명령창/실행창)에서 UTF-8 , 한글적용하기) 그런데 항상 적용되는 게 아니라, 매번 창을 열고 설정을 변경해줘야 한다.
  • Sublime Text 2 : DOS가 기본적으로 CP949라 Python Build 옵션의 encoding 변경으로도 안되었다. cmd 변경하는 방법이 있을것 같은데 아직 찾지 못함.

이 방법 말고도 encode()의 옵션에서 해당 문자를 무시(ignore)하거나 대체(replace)하는 방법도 사용할 수 있다.

'Python' 카테고리의 다른 글

matplotlib 이용하기  (0) 2014.09.15
Python Module  (0) 2014.07.31
Python Note  (0) 2014.07.24
Code Academy - Python  (0) 2014.07.11

Python Module

urllib
python3에서 urllib2는 urllib에 흡수되었다.

urllib는 다음 모듈을 모아 놓은 패키지이다.

  • urllib.request for opening and reading URLs
  • urllib.error containing the exceptions raised by urllib.request
  • urllib.parse for parsing URLs
  • urllib.robotparser for parsing robots.txt files

urllib는 url처리 모듈이다.
다음은 가장 간단한 사용방법이다.

# code from https://docs.python.org/3/howto/urllib2.html
import urllib.request
url = 'http://dna.daum.net'

response = urllib.request.urlopen(url) # file open과 유사
html = response.read()
response.close()

urlopen()은 url 또는 Request객체를 인자로 받는다. Request 객체를 사용하면, POST mehtod를 사용하거나, header를 추가할 수 있다. urlopen()에 data를 넘기면 POST method로 수행된다.

import urllib.request
url = 'http://dna.daum.net'
data = urllib.parse.urlencode({'spam':1, 'eggs':2})
data = data.encode('utf-8')
req1 = urllib.request.Request(url)
response = urllib.request.urlopen(url, data)
urllib.request
출처 : https://docs.python.org/3/library/urllib.request.html#module-urllib.request

urllib.request는 URL관련한 열기, 인증, 쿠키 등에 대한 클래스와 함수를 정의한다.

urllib.parse

# Class
urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

# Function
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False)

기본 사용형태는 다음과 같다

import urllib.request

url = "http://www.naver.com"

req = urllib.request.Request(url)
response = urllib.request.urlopen(req)

result = response.read().decode('utf-8')

BeautifulSoup4

Reference

기본 사용방법은 html문서를 BeautfilSoup에 넣어주면 된다.

import bs4

soup = bs4.BeautifulSoup(html_doc)

print(soup.prettify())  # 보기 좋게 출력

soup.title        # title tag에 접근
soup.body        # body tag에 접근

for talk in soup.find_all('div', {'class':'talk'}):  # div tag의 속성으로 검색
    print(talk.span.text)
# <div class=\'talk\'><span>Mom: How was school today, Sam?</span></div>

find_all()의 결과는 ResultSet인데, 각 결과를 담고 있는 list이다. 따라서 list의 원소에 대해 다시 find_all()을 사용가능하다. 일출 시간을 가져오는 예제이다. (https://github.com/hyde1004/sunrise_info)

url = 'http://astro.kasi.re.kr/Life/Knowledge/sunmoon_map/sunmoon_popup.php?year=2014&month=9&location=%C3%B5%BE%C8'

soup = bs4.BeautifulSoup(html)

day_info = soup.tbody.find_all('tr')
for info in day_info:
    sunrise_info = info.find_all('td')
    print(sunrise_info[2].text)

unittest

Skipping tests

@unittest.skip( )을 사용하면, 해당 test를 skip할 수 있다. 이외에 여러 조건을 지정할 수 있다.

class MyTestCase(unittest.TestCase):
    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

'Python' 카테고리의 다른 글

matplotlib 이용하기  (0) 2014.09.15
Python3와 encoding  (0) 2014.08.30
Python Note  (0) 2014.07.24
Code Academy - Python  (0) 2014.07.11

Python Note

정수 나눗셈
3 / 2 # 1.5
3 // 2 # 1
List Comprehension

아래에서 결국엔 len(n)으로 List가 채워진다.

len_cycles = [len(n) for n in cycles]
Directory operators

https://docs.python.org/3.4/library/pathlib.html#basic-use

zip function

여러 리스트를 김밥말듯 말아서, 자른다.

a = [1, 2, 3, 4, 5]
b = ['a', 'e', 'i', 'o', 'u']
for x, y in zip(a, b):
    print(x, y)

1 a
2 e
3 i
4 o
5 u
import 사용법
# import 파일명 형태
import module1
module1.hello()

# from 파일명 import 함수명 형태 (파일명 생략가능)
from module1 import hello()

if __name__ == '__main__' 구문은 파일 단독으로 실행하는 경우만 동작하고, 다른 파일에서 재사용할때는 동작하지 않는 부분이다.

list slice
출처 : http://codingbat.com/prob/p148661

다음의 결과는 서로 다르다.

def rotate_left3(nums):
    return nums[1:] + nums[0:1] # list + list
    return nums[1:] + nums[0] # 불가 : list + int
진수 관련

10진수를 제외한 진수은 0을 붙이고 시작한다.

a = 10     # 10진수 10
b = 0b10   # 2진수 10, 10진수 2
c = 0o10   # 8진수 10, 10진수 8
d = 0x10   # 16진수 10, 10진수 16

10진수를 다른 진수로 변환하기

bin(10)    # '0b1010'
oct(10)    # '0o12'
hex(10)    # '0xa'

문자를 해당 진수로 해석하기

int("10")     # 10
int("10", 2)  # 2
int("10", 8)  # 8
int("10", 16) # 16

'Python' 카테고리의 다른 글

matplotlib 이용하기  (0) 2014.09.15
Python3와 encoding  (0) 2014.08.30
Python Module  (0) 2014.07.31
Code Academy - Python  (0) 2014.07.11
Reference : http://www.codecademy.com/tracks/python

Python Syntax

Multi-Line Comments
"""Sipping from your cup 'til it runneth over,
Holy Grail.
"""

Strings & Console Output

Escaping characters
'There\'s a snake in my boot!'
String methods
  • len()
  • lower()
  • upper()
  • str()
"Ryan".lower()
"Ryan".upper()
str(3.14)
len("roar")
String Formatting with %
print "The %s who %s %s!" % ("Knights", "say", "Ni")
# This will print "The Knights who say Ni!"

Date and Time

Getting the Current Date and Time
from datetime import datetime

now = datetime.now()
print now
Extracting Information
from datetime import datetime
now = datetime.now()

current_year = now.year
current_month = now.month
current_day = now.day
Pretty Time
from datetime import datetime
now = datetime.now()

print '%s:%s:%s' % (now.hour, now.minute, now.second)

Conditionals & Control Flow

And
  • 1 < 2 and 2 < 3 is True
  • 1 < 2 and 2 > 3 is False
This and That (or This, But Not That!)

Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:

  • not is evaluated first
  • and is evaluated next
  • or is evaluated last
The Big If
if this_might_be_true():
    print "This really is true."
elif that_might_be_true():
    print "That is true."
else:
    print "None of the above."

PygLatin

Input!
name = raw_input("What's your name?")
print name
Check Yourself… Some More
x = "J123"
x.isalpha()  # False

Functions

Generic Imports
# Ask Python to print sqrt(25) on line 3.
import math

print math.sqrt(25)
Function Imports
from module import function

print sqrt(25)
Universal Imports
from module import *
Here Be Dragons
import math            # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything       # Prints 'em all!
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
None
On Beyond Strings
def biggest_number(*args):
    print max(args)
    return max(args)

biggest_number(-10, -5, 5, 10)
Built-in Functions
maximum = max(1, 1.1, -3.2)
print maximum

minimum = min(-100, 0, 44, 100.3)
print minimum

absolute = abs(-42)
print absolute

print type(4)     # <type 'int'>
print type(4.2)   # <type 'float'>
print type('hello') # <type 'str'>

Python Lists and Dictionaries

Introduction to Lists

Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name.

Late Arrivals & List Length
letters = ['a', 'b', 'c']
letters.append('d')
print len(letters)
print letters
List Slicing
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[0:2]  # The first and second items (index zero and one)
middle = suitcase[2:4]  # Third and fourth items (index two and three)
last   = suitcase[4:6]  # The last two items (index four and five)
Slicing Lists and Strings
animals = "catdogfrog"
cat  = animals[:3]   # The first three characters of animals
dog  = animals[3:6]              # The fourth through sixth characters
frog = animals[6:]              # From the seventh character to the end
Maintaining Order
animals = ["ant", "bat", "cat"]
print animals.index("bat")

animals.insert(1, "dog")
print animals
For One and All
my_list = [1,9,3,8,5,7]

for number in my_list:
    # Your code here
    print 2 * number
More with ‘for’
animals = ["cat", "ant", "bat"]
animals.sort()

for animal in animals:
    print animal
This Next Part is Key
# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

print residents['Puffin'] # Prints Puffin's room number

# Your code here!
print residents['Sloth']
print residents['Burmese Python']
New Entries
dict_name[new_key] = new_value
Changing Your Mind
del dict_name[key_name]

dict_name[key] = new_value
Remove a Few Things
beatles = ["john","paul","george","ringo","stuart"]
beatles.remove("stuart")
print beatles
>> ["john","paul","george","ringo"]
It’s Dangerous to Go Alone! Take This
my_dict = {
    "fish": ["c", "a", "r", "p"],
    "cash": -4483,
    "luck": "good"
}
print my_dict["fish"][0]

A Day at the Supermarket

This is KEY!
# A simple dictionary
d = {"foo" : "bar"}

for key in d: 
    print d[key]  # prints "bar"

Student Becomes the Teacher

Lesson Number One
animal_sounds = {
    "cat": ["meow", "purr"],
    "dog": ["woof", "bark"],
    "fox": [],
}
print animal_sounds["cat"]

Lists and Functions

Appending to a list
n = [1, 3, 5]
# Append the number 4 here
n.append(4)
Removing elements from lists

This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called n:

  1. n.pop(index) will remove the item at index from the list and return it to you:
    n = [1, 3, 5]
    n.pop(1)
    # Returns 3 (the item at index 1)
    print n
    # prints [1, 5]
    
  2. n.remove(item) will remove the actual item if it finds it:
    n.remove(1)
    # Removes 1 from the list,
    # NOT the item at index 1
    print n
    # prints [3, 5]
    
  3. del(n[1]) is like .pop in that it will remove the item at the given index, but it won’t return it:
    del(n[1])
    # Doesn't return anything
    print n
    # prints [1, 5]
    
Passing a range into a function
range(6) # => [0,1,2,3,4,5]
range(1,6) # => [1,2,3,4,5]
range(1,6,3) # => [1,4]
  • range(stop)
  • range(start, stop)
  • range(start, stop, step)
Iterating over a list in a function

Now that we’ve learned about range, we have two ways of iterating through a list.

  • Method 1 : for item in list

    for item in list:
     print item
    
  • Method 2 : iterate through indexes

    for i in range(len(list)):
     print list[i]
    

Method 1 is useful to loop through the list, but it’s not possible to modify the list this way. Method 2 uses indexes to loop through the list, making it possible to also modify the list if needed. Since we aren’t modifying the list, feel free to use either one on this lesson!

Using two lists as two arguments in a function
a = [1, 2, 3]
b = [4, 5, 6]
print a + b
# prints [1, 2, 3, 4, 5, 6]

Battleship!

Make a List
print ["O"] * 5
# prints ['O', 'O', 'O', 'O', 'O']
Printing Pretty
letters = ['a', 'b', 'c', 'd']
print " ".join(letters)
print "---".join(letters)
Hide…
from random import randint
coin = randint(0, 1) # 0, 1
dice = randint(1, 6) # 1, 2, 3, 4, 5, 6

Loops

While / else
while condition:
    pass
else:
    pass
For your strings
word = "eggs!"
for c in word:
    print c
Looping over a dictionary
d = {'x': 9, 'y': 10, 'z': 20}
for key in d:
    if d[key] == 10
        print "This dictionary has the value 10!"
Counting as you go
choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'
for index, item in enumerate(choices):
    print index + 1, item
Multiple lists

It’s also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy.

zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.

zip can handle three or more lists as well!

For / else
names = ['Hyde', 'Jack', 'Dorothy', 'Jane']

for name in names:
    print name
else:
    print "No more name"

Advanced Topics in Python

keys() and values()
my_dict = {
    "Name" : "Junggu Lee",
    "Company" : "LGE",
    "Age" : 41
}

print my_dict.keys()
print my_dict.values()
print my_dict.items()
The ‘in’ Operator
for number in range(5):
    print number,

d = { "name": "Eric", "age": 26 }
for key in d:
    print key, d[key],

for letter in "Eric":
    print letter,  # note the comma!
List Comprehension Syntax
new_list = [x for x in range(1,6)]
# => [1, 2, 3, 4, 5]

doubles = [x*2 for x in range(1,6)]
# => [2, 4, 6, 8, 10]

doubles_by_3 = [x*2 for x in range(1,6) if (x*2)%3 == 0]
# => [6]
List Slicing Syntax
[start:end:stride]
Reversing a List
letters = ['A', 'B', 'C', 'D', 'E']
print letters[::-1]
Anonymous Functions

See the lambda bit? Typing

lambda x: x % 3 == 0

Is the same as

def by_three(x):
    return x % 3 == 0
Lambda Syntax
my_list = range(16)
print filter(lambda x: x % 3 == 0, my_list)
Try it!
cubes = [x**3 for x in range(1, 11)]
filter(lambda x: x % 3 == 0, cubes)

Binary Representaion

Lesson 10:The Base 2 Number System
print 0b1,    #1
print 0b10,   #2
The bin() Function
  • bin() function
  • oct() function
  • hex() function
print bin(1)
print bin(2)
int()’s Second Parameter
int("42")
# ==> 42

int("110", 2)
# ==> 6

Introduction to Classes

Class Syntax
class NewClass(object):
    # Class magic here
    pass
Let’s Not Get Too Selfish
class Animal(object):
    def __init__(self, name):
        self.name = name
Inheritance Syntax
class DerivedClass(BaseClass):
    # code goes here
Override!
class Employee(object):
    def __init__(self, name):
        self.name = name
    def greet(self, other):
        print "Hello, %s" % other.name

class CEO(Employee):
    def greet(self, other):
        print "Get back to work, %s!" % other.name

ceo = CEO("Emily")
emp = Employee("Steve")
emp.greet(ceo)
# Hello, Emily
ceo.greet(emp)
# Get back to work, Steve!

``` This Looks Like a Job For …

class Derived(Base):
   def m(self):
       return super(Derived, self).m()

File Input/Output

See It to Believe It
my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10

f = open("output.txt", "w")

for item in my_list:
    f.write(str(item) + "\n")

f.close()
Reading
my_file = open("output.txt", "r")
print my_file.read()
my_file.close()
Reading Between the Lines
my_file = open("text.txt", "r")

print my_file.readline()
print my_file.readline()
print my_file.readline()

my_file.close()
The ‘with’ and ‘as’ Keywords

Programming is all about getting the computer to do the work. Is there a way to get Python to automatically close our files for us?

Of course there is. This is Python.

You may not know this, but file objects contain a special pair of built-in methods: __enter__() and __exit__(). The details aren’t important, but what is important is that when a file object’s __exit__() method is invoked, it automatically closes the file. How do we invoke this method? With with and as.

The syntax looks like this:

with open("file", "mode") as variable:
    # Read or write to the file
with open("text.txt", "w") as textfile:
    textfile.write("Success!")
Case Closed?
f = open("bg.txt")
f.closed
# False
f.close()
f.closed
# True

'Python' 카테고리의 다른 글

matplotlib 이용하기  (0) 2014.09.15
Python3와 encoding  (0) 2014.08.30
Python Module  (0) 2014.07.31
Python Note  (0) 2014.07.24

+ Recent posts