├── 2부
├── 19장
│ ├── package
│ │ ├── animal
│ │ │ ├── etc.py
│ │ │ ├── bird
│ │ │ │ ├── penguin.py
│ │ │ │ ├── sparrow.py
│ │ │ │ └── __init__.py
│ │ │ ├── mammal
│ │ │ │ ├── human.py
│ │ │ │ ├── monkey.py
│ │ │ │ └── __init__.py
│ │ │ └── __init__.py
│ │ └── setup.py
│ ├── book
│ │ ├── book
│ │ │ ├── book_xml.py
│ │ │ ├── bookDataBase
│ │ │ │ ├── booksql.py
│ │ │ │ └── __init__.py
│ │ │ ├── book_internet.py
│ │ │ └── __init__.py
│ │ └── setup.py
│ ├── simple Example
│ │ ├── MANIFEST
│ │ ├── xml
│ │ │ ├── etree
│ │ │ │ ├── cElementTree.py
│ │ │ │ ├── __init__.pyc
│ │ │ │ ├── ElementPath.pyc
│ │ │ │ ├── ElementTree.pyc
│ │ │ │ ├── __init__.py
│ │ │ │ ├── ElementInclude.py
│ │ │ │ └── ElementPath.py
│ │ │ ├── __init__.pyc
│ │ │ ├── dom
│ │ │ │ ├── domreg.pyc
│ │ │ │ ├── __init__.pyc
│ │ │ │ ├── minidom.pyc
│ │ │ │ ├── NodeFilter.pyc
│ │ │ │ ├── minicompat.pyc
│ │ │ │ ├── xmlbuilder.pyc
│ │ │ │ ├── expatbuilder.pyc
│ │ │ │ ├── NodeFilter.py
│ │ │ │ ├── minicompat.py
│ │ │ │ ├── domreg.py
│ │ │ │ └── __init__.py
│ │ │ ├── sax
│ │ │ │ ├── __init__.pyc
│ │ │ │ ├── handler.pyc
│ │ │ │ ├── _exceptions.pyc
│ │ │ │ ├── xmlreader.pyc
│ │ │ │ ├── __init__.py
│ │ │ │ ├── _exceptions.py
│ │ │ │ └── saxutils.py
│ │ │ ├── parsers
│ │ │ │ ├── expat.pyc
│ │ │ │ ├── __init__.pyc
│ │ │ │ ├── expat.py
│ │ │ │ └── __init__.py
│ │ │ └── __init__.py
│ │ ├── setup.py
│ │ └── book.py
│ ├── ext_book
│ │ ├── MANIFEST
│ │ ├── book.c
│ │ ├── page.c
│ │ ├── setup.cfg
│ │ └── setup.py
│ ├── py_book
│ │ ├── MANIFEST
│ │ ├── book.py
│ │ └── setup.py
│ └── setup configurate
│ │ ├── MANIFEST
│ │ ├── setup.cfg
│ │ ├── setup.py
│ │ └── book.py
├── 22장
│ ├── standard
│ │ ├── requirements.txt
│ │ ├── app.yaml
│ │ ├── index.yaml
│ │ ├── appengine_config.py
│ │ ├── bookmark.py
│ │ ├── templates
│ │ │ └── home.html
│ │ └── main.py
│ └── flexible
│ │ ├── requirements.txt
│ │ ├── app.yaml
│ │ ├── templates
│ │ └── home.html
│ │ ├── bookmark.py
│ │ └── main.py
├── 16장
│ ├── book.py
│ └── book.xml
├── 17장
│ ├── gmail.py
│ ├── logo.gif
│ ├── xmlbook.py
│ ├── MixedType.py
│ ├── internetbook.py
│ ├── simpleEmail.py
│ ├── socket_client.py
│ ├── socket_server.py
│ ├── logo.html
│ ├── book.xml
│ ├── openapi_http.py
│ ├── openapi_urllib.py
│ ├── launcher.py
│ └── mysmtplib.py
├── 14장
│ ├── 14-2-2.py
│ ├── 14-2-4.py
│ ├── 14-1-1.py
│ ├── 14-2-3.py
│ ├── 14-2-1.py
│ ├── 14-6-1.py
│ ├── 14-4-1.py
│ ├── 14-3-1.py
│ ├── 14-5-1.py
│ └── pysqlite_command.py
├── 11장
│ └── sleep.py
├── 21장
│ └── prime.py
└── 13장
│ └── tree.py
├── 1부
├── 4장
│ ├── 4-1-1.py
│ ├── 4-2-1.py
│ ├── 4-2-2.py
│ ├── 4-3-1.py
│ ├── 4-4-2.py
│ ├── 4-4-1.py
│ └── 4-5-1.py
├── 9장
│ ├── python36_d.dll
│ ├── python36_d.lib
│ ├── spam2
│ │ ├── setup.py
│ │ └── spammodule.c
│ ├── circle
│ │ ├── circle.c
│ │ └── setup.py
│ ├── spam
│ │ ├── spammodule.c
│ │ └── spam.vcxproj
│ └── circle_prototype
│ │ ├── setup.py
│ │ └── circle_prototype.c
├── 7장
│ ├── 7-2-1.py
│ ├── 7-4-1.py
│ ├── 7-1-4.py
│ ├── 7-2-2.py
│ ├── 7-1-6.py
│ ├── 7-1-7.py
│ ├── 7-1-1.py
│ ├── 7-1-2.py
│ ├── 7-1-5.py
│ ├── 7-1-3.py
│ └── 7-3-1.py
└── 5장
│ ├── 5-1-1.py
│ ├── 5-7-1.py
│ ├── 5-2-1.py
│ ├── 5-5-2.py
│ ├── 5-4-1.py
│ ├── 5-3-1.py
│ ├── 5-7-2.py
│ ├── 5-3-3.py
│ ├── 5-5-3.py
│ ├── 5-7-4.py
│ ├── 5-5-1.py
│ ├── 5-7-3.py
│ ├── 5-3-2.py
│ ├── 5-6-2.py
│ ├── 5-6-1.py
│ ├── 5-6-3.py
│ ├── 5-6-5.py
│ └── 5-6-4.py
└── 3부
├── 24장
├── hello_alexa
│ ├── utteranrce.txt
│ ├── templates.yaml
│ ├── interaction_model.json
│ ├── hello_alexa.py
│ └── hello_alexa2.py
└── baseball
│ ├── templates.yaml
│ ├── utterances.txt
│ ├── interaction_model.json
│ └── baseball.py
├── 26장
├── datasheet
│ └── Laser Dust Sensor Control Protocol_V1.3.docx
├── WebApp
│ ├── app.yaml
│ ├── appengine_config.py
│ ├── air.py
│ ├── main.py
│ └── templates
│ │ └── home.html
└── Pi
│ ├── 2n222.py
│ ├── led.py
│ ├── btn.py
│ ├── oled.py
│ ├── sds011.py
│ ├── air.py
│ ├── bme280.py
│ └── myAir.py
├── 23장
├── nba_2016_06.csv
├── nba_2015_10.csv
├── nba_2016_05.csv
├── nba_2015_2016_standing.csv
├── nba_2014_2015_standing.csv
├── nba_decision_tree.py
└── nba_2016_04.csv
└── 25장
├── teller.py
└── noti.py
/2부/19장/package/animal/etc.py:
--------------------------------------------------------------------------------
1 | print ("etc...")
--------------------------------------------------------------------------------
/2부/19장/book/book/book_xml.py:
--------------------------------------------------------------------------------
1 | print("book xml")
2 |
--------------------------------------------------------------------------------
/2부/22장/standard/requirements.txt:
--------------------------------------------------------------------------------
1 | Flask==0.12.1
2 |
--------------------------------------------------------------------------------
/2부/19장/book/book/bookDataBase/booksql.py:
--------------------------------------------------------------------------------
1 | print ("booksql")
--------------------------------------------------------------------------------
/2부/19장/package/animal/bird/penguin.py:
--------------------------------------------------------------------------------
1 | print ("penguin...")
--------------------------------------------------------------------------------
/2부/19장/package/animal/bird/sparrow.py:
--------------------------------------------------------------------------------
1 | print ("sparrow...")
--------------------------------------------------------------------------------
/2부/19장/package/animal/mammal/human.py:
--------------------------------------------------------------------------------
1 | print('human...')
--------------------------------------------------------------------------------
/2부/19장/package/animal/mammal/monkey.py:
--------------------------------------------------------------------------------
1 | print('monkey...')
--------------------------------------------------------------------------------
/2부/19장/simple Example/MANIFEST:
--------------------------------------------------------------------------------
1 | book.py
2 | setup.py
3 |
--------------------------------------------------------------------------------
/2부/19장/book/book/book_internet.py:
--------------------------------------------------------------------------------
1 | print("book internet")
2 |
--------------------------------------------------------------------------------
/2부/19장/ext_book/MANIFEST:
--------------------------------------------------------------------------------
1 | book.c
2 | page.c
3 | setup.py
4 |
--------------------------------------------------------------------------------
/2부/19장/py_book/MANIFEST:
--------------------------------------------------------------------------------
1 | setup.py
2 | c:\python30\test.py
3 |
--------------------------------------------------------------------------------
/2부/19장/package/animal/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ["bird", "mammal","etc"]
2 |
--------------------------------------------------------------------------------
/2부/19장/setup configurate/MANIFEST:
--------------------------------------------------------------------------------
1 | book.py
2 | setup.cfg
3 | setup.py
4 |
--------------------------------------------------------------------------------
/2부/19장/package/animal/bird/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ["penguin", "sparrow"]
2 |
--------------------------------------------------------------------------------
/2부/19장/package/animal/mammal/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ["human", "monkey"]
2 |
--------------------------------------------------------------------------------
/1부/4장/4-1-1.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/4장/4-1-1.py
--------------------------------------------------------------------------------
/2부/16장/book.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/16장/book.py
--------------------------------------------------------------------------------
/2부/17장/gmail.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/gmail.py
--------------------------------------------------------------------------------
/2부/17장/logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/logo.gif
--------------------------------------------------------------------------------
/2부/19장/book/book/bookDataBase/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = [
2 | 'booksql',
3 | ]
--------------------------------------------------------------------------------
/2부/17장/xmlbook.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/xmlbook.py
--------------------------------------------------------------------------------
/2부/19장/book/book/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ["book_xml", "book_internet","bookDataBase"]
2 |
--------------------------------------------------------------------------------
/1부/9장/python36_d.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/python36_d.dll
--------------------------------------------------------------------------------
/1부/9장/python36_d.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/python36_d.lib
--------------------------------------------------------------------------------
/1부/9장/spam2/setup.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/spam2/setup.py
--------------------------------------------------------------------------------
/2부/17장/MixedType.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/MixedType.py
--------------------------------------------------------------------------------
/1부/9장/circle/circle.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/circle/circle.c
--------------------------------------------------------------------------------
/2부/17장/internetbook.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/internetbook.py
--------------------------------------------------------------------------------
/2부/17장/simpleEmail.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/simpleEmail.py
--------------------------------------------------------------------------------
/2부/19장/ext_book/book.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/ext_book/book.c
--------------------------------------------------------------------------------
/2부/19장/ext_book/page.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/ext_book/page.c
--------------------------------------------------------------------------------
/2부/19장/py_book/book.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/py_book/book.py
--------------------------------------------------------------------------------
/2부/22장/flexible/requirements.txt:
--------------------------------------------------------------------------------
1 | Flask==0.12.1
2 | gunicorn==19.7.1
3 | google-cloud==0.24.0
4 |
--------------------------------------------------------------------------------
/3부/24장/hello_alexa/utteranrce.txt:
--------------------------------------------------------------------------------
1 | NameIntent My name is {name}
2 | NameIntent I am {name}
--------------------------------------------------------------------------------
/1부/4장/4-2-1.py:
--------------------------------------------------------------------------------
1 | a = 0
2 | if a & 10 / a:
3 | print("a가 0입니다.")
4 | else:
5 | print("에러 없이 통과!")
--------------------------------------------------------------------------------
/1부/4장/4-2-2.py:
--------------------------------------------------------------------------------
1 | a = 0
2 | if a and 10 / a:
3 | print("a가 0입니다.")
4 | else:
5 | print("에러 없이 통과!")
--------------------------------------------------------------------------------
/1부/9장/spam/spammodule.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/spam/spammodule.c
--------------------------------------------------------------------------------
/1부/9장/spam2/spammodule.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/1부/9장/spam2/spammodule.c
--------------------------------------------------------------------------------
/2부/17장/socket_client.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/socket_client.py
--------------------------------------------------------------------------------
/2부/17장/socket_server.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/17장/socket_server.py
--------------------------------------------------------------------------------
/2부/19장/ext_book/setup.cfg:
--------------------------------------------------------------------------------
1 | [sdist]
2 | dist_dir=sdist_pkg
3 |
4 | [bdist]
5 | dist_dir=bdist_pkg
6 |
--------------------------------------------------------------------------------
/2부/19장/setup configurate/setup.cfg:
--------------------------------------------------------------------------------
1 | [sdist]
2 | home=sdist_pkg
3 |
4 | [bdist]
5 | dist_dir=bdist_pkg
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/cElementTree.py:
--------------------------------------------------------------------------------
1 | # Wrapper module for _elementtree
2 |
3 | from _elementtree import *
4 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/__init__.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/domreg.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/domreg.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/__init__.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/minidom.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/minidom.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/sax/__init__.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/handler.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/sax/handler.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/NodeFilter.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/NodeFilter.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/minicompat.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/minicompat.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/xmlbuilder.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/xmlbuilder.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/etree/__init__.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/parsers/expat.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/parsers/expat.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/_exceptions.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/sax/_exceptions.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/xmlreader.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/sax/xmlreader.pyc
--------------------------------------------------------------------------------
/1부/7장/7-2-1.py:
--------------------------------------------------------------------------------
1 | def RaiseErrorFunc():
2 | raise NameError
3 |
4 | try:
5 | RaiseErrorFunc()
6 | except:
7 | print("NameError is Catched")
--------------------------------------------------------------------------------
/1부/7장/7-4-1.py:
--------------------------------------------------------------------------------
1 | def foo(x):
2 | assert type(x) == int, "Input value must be integer"
3 | return x * 10
4 |
5 | ret = foo("a")
6 | print(ret)
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/expatbuilder.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/dom/expatbuilder.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/ElementPath.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/etree/ElementPath.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/ElementTree.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/etree/ElementTree.pyc
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/parsers/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/2부/19장/simple Example/xml/parsers/__init__.pyc
--------------------------------------------------------------------------------
/2부/14장/14-2-2.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect("./test.db")
3 | cur = con.cursor()
4 | cur.execute("SELECT * FROM PhoneBook;")
5 | print(cur.fetchall())
--------------------------------------------------------------------------------
/2부/19장/simple Example/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup
2 |
3 | setup(name='book',
4 | version='1.0',
5 | py_modules=['book']
6 | )
7 |
--------------------------------------------------------------------------------
/2부/14장/14-2-4.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect("./commit.db")
3 | cur = con.cursor()
4 | cur.execute("SELECT * FROM PhoneBook;")
5 | print(cur.fetchall())
--------------------------------------------------------------------------------
/2부/19장/py_book/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup
2 |
3 | setup(name='book',
4 | version='1.0',
5 | py_modules=['c:\\python30\\test'],
6 | )
7 |
--------------------------------------------------------------------------------
/2부/19장/setup configurate/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup
2 |
3 | setup(name='book',
4 | version='1.0',
5 | py_modules=['book']
6 | )
7 |
--------------------------------------------------------------------------------
/3부/24장/hello_alexa/templates.yaml:
--------------------------------------------------------------------------------
1 | start: Hi, My name is Alexa. What is your name?
2 | repeat: Could you tell me your name? please
3 | answer: Nice to see you. {{ name }}
--------------------------------------------------------------------------------
/1부/4장/4-3-1.py:
--------------------------------------------------------------------------------
1 | for n in [1, 2]:
2 | print("-- {0} 단 --".format(n))
3 | for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
4 | print("{0} * {1} = {2}".format(n, i, n * i))
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/parsers/expat.py:
--------------------------------------------------------------------------------
1 | """Interface to the Expat non-validating XML parser."""
2 | __version__ = '$Revision: 17640 $'
3 |
4 | from pyexpat import *
5 |
--------------------------------------------------------------------------------
/3부/24장/baseball/templates.yaml:
--------------------------------------------------------------------------------
1 | start: Hi, Let's start the baseball game. Are you ready?
2 | rule: Tell me three non-overlapping numbers from 0 to 9.
3 | end: OK. Good bye.
--------------------------------------------------------------------------------
/1부/9장/circle/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup, Extension
2 | setup(name="circle",
3 | version="1.0",
4 | ext_modules=[Extension("circle", ["circle.c"])])
--------------------------------------------------------------------------------
/3부/26장/datasheet/Laser Dust Sensor Control Protocol_V1.3.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wikibook/python36/HEAD/3부/26장/datasheet/Laser Dust Sensor Control Protocol_V1.3.docx
--------------------------------------------------------------------------------
/1부/9장/circle_prototype/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup, Extension
2 | setup(name="circle",
3 | version="1.0",
4 | ext_modules=[Extension("circle", ["circle_prototype.c"])])
5 |
--------------------------------------------------------------------------------
/2부/14장/14-1-1.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 |
3 | con = sqlite3.connect(":memory:")
4 |
5 | with open('script.txt') as f:
6 | SQLScript = f.read()
7 |
8 | cur = con.cursor()
9 | cur.executescript(SQLScript)
--------------------------------------------------------------------------------
/1부/7장/7-1-4.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 | try:
4 | c = divide(5, "af")
5 | except TypeError as e:
6 | print('에러: ', e.args[0])
7 | except Exception:
8 | print('음~ 무슨 에러인지 모르겠어요!!')
--------------------------------------------------------------------------------
/2부/17장/logo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | welcome to the python world
5 |
6 | test mail
7 |
8 |
9 |
--------------------------------------------------------------------------------
/1부/4장/4-4-2.py:
--------------------------------------------------------------------------------
1 | L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2 | for i in L:
3 | if i > 5:
4 | break
5 | print("Item: {0}".format(i))
6 | else:
7 | print("Exit without break.")
8 | print("Always this is printed")
--------------------------------------------------------------------------------
/1부/4장/4-4-1.py:
--------------------------------------------------------------------------------
1 | L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2 | for i in L:
3 | if i % 2 == 0:
4 | continue
5 | print("Item: {0}".format(i))
6 | else:
7 | print("Exit without break.")
8 | print("Always this is printed")
--------------------------------------------------------------------------------
/2부/11장/sleep.py:
--------------------------------------------------------------------------------
1 | import time
2 | t = time.time()
3 | time.sleep(10)
4 | t2 = time.time()
5 |
6 | spendtime = t2 - t
7 | print("Before timestemp: ", t)
8 | print("After timestemp: ", t2)
9 | print("Wait {0} seconds".format(spendtime))
--------------------------------------------------------------------------------
/2부/19장/ext_book/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup, Extension
2 | setup(name='book',
3 | version='1.0',
4 | ext_package='pkg',
5 | ext_modules=[ Extension('book', ['book.c']),
6 | Extension('book.page', ['page.c'])],
7 | )
8 |
--------------------------------------------------------------------------------
/3부/24장/hello_alexa/interaction_model.json:
--------------------------------------------------------------------------------
1 | {
2 | "intents": [{
3 | "intent": "NameIntent",
4 | "slots": [{
5 | "name": "name",
6 | "type": "AMAZON.US_FIRST_NAME"
7 | }]
8 | }]
9 | }
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/parsers/__init__.py:
--------------------------------------------------------------------------------
1 | """Python interfaces to XML parsers.
2 |
3 | This package contains one module:
4 |
5 | expat -- Python wrapper for James Clark's Expat parser, with namespace
6 | support.
7 |
8 | """
9 |
--------------------------------------------------------------------------------
/1부/5장/5-1-1.py:
--------------------------------------------------------------------------------
1 | str = "NOT Class Member"
2 | class GString:
3 | str = ""
4 | def Set(self, msg):
5 | self.str = msg
6 | def Print(self):
7 | print(str)
8 |
9 | g = GString()
10 | g.Set("First Message")
11 | g.Print()
--------------------------------------------------------------------------------
/2부/14장/14-2-3.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect("./commit.db")
3 | cur = con.cursor()
4 | cur.execute("CREATE TABLE PhoneBook(Name text, PhoneNum text);")
5 | cur.execute("INSERT INTO PhoneBook VALUES('Derick', '010-1234-5678');")
6 | con.commit()
--------------------------------------------------------------------------------
/1부/7장/7-2-2.py:
--------------------------------------------------------------------------------
1 | def RaiseErrorFunc():
2 | raise NameError("NameError의 인자")
3 |
4 | def PropagateError():
5 | try:
6 | RaiseErrorFunc()
7 | except:
8 | print("에러 전달 이전에 먼저 이 메시지가 출력됩니다.")
9 | raise
10 |
11 | PropagateError()
--------------------------------------------------------------------------------
/2부/19장/book/setup.py:
--------------------------------------------------------------------------------
1 |
2 | from distutils.core import setup, Extension
3 | setup(name='book',
4 | version='1.0',
5 | classifiers = [ 'book','book.bookDataBase'],
6 | packages=['book','book.bookDataBase'],
7 | package_dir={'bookDataBase': 'book'},
8 | )
9 |
10 |
--------------------------------------------------------------------------------
/1부/5장/5-7-1.py:
--------------------------------------------------------------------------------
1 | class Tiger:
2 | def Jump(self):
3 | print("호랑이처럼 멀리 점프하기")
4 |
5 | class Lion:
6 | def Bite(self):
7 | print("사자처럼 한입에 꿀꺽하기")
8 |
9 | class Liger(Tiger, Lion):
10 | def Play(self):
11 | print("라이거만의 사육사와 재미있게 놀기")
--------------------------------------------------------------------------------
/1부/7장/7-1-6.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 |
4 | try:
5 | c = divide(5, 0)
6 | except ArithmeticError:
7 | print('수치 연산 관련 에러입니다.')
8 | except TypeError:
9 | print('모든 인자는 숫자여야 합니다.')
10 | except Exception:
11 | print('음~ 무슨 에러인지 모르겠어요!!')
--------------------------------------------------------------------------------
/1부/7장/7-1-7.py:
--------------------------------------------------------------------------------
1 | FilePath = './test.txt'
2 |
3 | try:
4 | f = open(FilePath, 'r')
5 | try:
6 | data = f.read(128)
7 | print(data)
8 | finally:
9 | f.close()
10 | except IOError:
11 | print("Fail to open {0} file".format(FilePath))
--------------------------------------------------------------------------------
/1부/7장/7-1-1.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 |
4 | try:
5 | c = divide(5, 'string')
6 | except ZeroDivisionError:
7 | print('두 번째 인자는 0이어서는 안 됩니다.')
8 | except TypeError:
9 | print('모든 인자는 숫자여야 합니다.')
10 | except:
11 | print('음~ 무슨 에러인지 모르겠어요!!')
--------------------------------------------------------------------------------
/2부/22장/standard/app.yaml:
--------------------------------------------------------------------------------
1 | # [START runtime]
2 | runtime: python27
3 | api_version: 1
4 | threadsafe: true
5 | # [END runtime]
6 |
7 | # [START handlers]
8 | handlers:
9 | - url: /static
10 | static_dir: static
11 | - url: /.*
12 | script: main.app
13 | # [END handlers]
--------------------------------------------------------------------------------
/3부/26장/WebApp/app.yaml:
--------------------------------------------------------------------------------
1 | # [START runtime]
2 | runtime: python27
3 | api_version: 1
4 | threadsafe: true
5 | # [END runtime]
6 |
7 | # [START handlers]
8 | handlers:
9 | - url: /static
10 | static_dir: static
11 | - url: /.*
12 | script: main.app
13 | # [END handlers]
--------------------------------------------------------------------------------
/1부/5장/5-2-1.py:
--------------------------------------------------------------------------------
1 | class MyClass:
2 | def __init__(self, value):
3 | self.Value = value
4 | print("Class is created! Value = ", value)
5 |
6 | def __del__(self):
7 | print("Class is deleted!")
8 |
9 | def foo():
10 | d = MyClass(10)
11 |
12 | foo()
--------------------------------------------------------------------------------
/1부/7장/7-1-2.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 |
4 | try:
5 | c = divide(5, 'apple')
6 | except Exception:
7 | print('음~ 무슨 에러인지 모르겠어요!!')
8 | except ZeroDivisionError:
9 | print('두 번째 인자는 0이어서는 안 됩니다.')
10 | except TypeError:
11 | print('모든 인자는 숫자여야 합니다.')
--------------------------------------------------------------------------------
/2부/14장/14-2-1.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect("./test.db")
3 | cur = con.cursor()
4 | cur.execute("CREATE TABLE PhoneBook(Name text, PhoneNum text);")
5 | cur.execute("INSERT INTO PhoneBook VALUES('Derick', '010-1234-5678');")
6 | cur.execute("SELECT * FROM PhoneBook;")
7 | print(cur.fetchall())
--------------------------------------------------------------------------------
/1부/7장/7-1-5.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 |
4 | try:
5 | c = divide(5, 0)
6 | except (ZeroDivisionError, OverflowError, FloatingPointError):
7 | print('수치 연산 관련 에러입니다.')
8 | except TypeError:
9 | print('모든 인자는 숫자여야 합니다.')
10 | except Exception:
11 | print('음~ 무슨 에러인지 모르겠어요!!')
--------------------------------------------------------------------------------
/1부/5장/5-5-2.py:
--------------------------------------------------------------------------------
1 | class GString:
2 | def __init__(self, init=None):
3 | self.content = init
4 |
5 | def __sub__(self, str):
6 | print("- opreator is called!")
7 |
8 | def __isub__(self, str):
9 | print("-= opreator is called!")
10 |
11 | g = GString("aBcdef")
12 | g - "a"
13 | g -= "a"
14 |
--------------------------------------------------------------------------------
/1부/5장/5-4-1.py:
--------------------------------------------------------------------------------
1 | class GString:
2 | def __init__(self, init=None):
3 | self.content = init
4 |
5 | def __sub__(self, str):
6 | for i in str:
7 | self.content = self.content.replace(i, '')
8 | return GString(self.content)
9 |
10 | def Remove(self, str):
11 | return self.__sub__(str)
--------------------------------------------------------------------------------
/1부/5장/5-3-1.py:
--------------------------------------------------------------------------------
1 | class CounterManager:
2 | insCount = 0
3 | def __init__(self):
4 | CounterManager.insCount += 1
5 | def printInstaceCount():
6 | print("Instance Count: ", CounterManager.insCount)
7 |
8 | a, b, c = CounterManager(), CounterManager(), CounterManager()
9 | CounterManager.printInstaceCount()
10 | b.printInstaceCount()
--------------------------------------------------------------------------------
/3부/24장/baseball/utterances.txt:
--------------------------------------------------------------------------------
1 | YesIntent yes
2 | YesIntent sure
3 |
4 | NoIntent no
5 | NoIntent no. thanks
6 | NoIntent not yet
7 |
8 | AnswerIntent {first} {second} {third}
9 | AnswerIntent {first} {second} and {third}
10 | AnswerIntent My answer is {first} {second} {third}
11 | AnswerIntent My answer is {first} {second} and {third}
12 |
--------------------------------------------------------------------------------
/2부/19장/package/setup.py:
--------------------------------------------------------------------------------
1 | from distutils.core import setup, Extension
2 |
3 | classifierList = [ 'animal', 'animal::etc', 'animal::bird::penguin','animal::bird::sparrow',
4 | 'animal::mammal::human','animal::mammal::monkey']
5 |
6 | setup(name='animal',
7 | version='1.0',
8 | classifiers = classifierList,
9 | packages=['animal', 'animal.bird', 'animal.mammal'],
10 |
11 | )
--------------------------------------------------------------------------------
/1부/5장/5-7-2.py:
--------------------------------------------------------------------------------
1 | class Tiger:
2 | def Jump(self):
3 | print("호랑이처럼 멀리 점프하기")
4 |
5 | def Cry(self):
6 | print("호랑이: 어흥~")
7 |
8 | class Lion:
9 | def Bite(self):
10 | print("사자처럼 한입에 꿀꺽하기")
11 |
12 | def Cry(self):
13 | print("사자: 으르렁~")
14 |
15 | class Liger(Tiger, Lion):
16 | def Play(self):
17 | print("라이거만의 사육사와 재미있게 놀기")
--------------------------------------------------------------------------------
/1부/5장/5-3-3.py:
--------------------------------------------------------------------------------
1 | class CounterManager:
2 | __insCount = 0
3 | def __init__(self):
4 | CounterManager.__insCount += 1
5 |
6 | def staticPrintCount():
7 | print("Instance Count: %d" % CounterManager.__insCount)
8 | SPrintCount = staticmethod(staticPrintCount)
9 |
10 | a, b, c = CounterManager(), CounterManager(), CounterManager()
11 | CounterManager.SPrintCount()
--------------------------------------------------------------------------------
/1부/7장/7-1-3.py:
--------------------------------------------------------------------------------
1 | def divide(a, b):
2 | return a / b
3 |
4 | try:
5 | c = divide(5, 2)
6 | except ZeroDivisionError:
7 | print('두 번째 인자는 0이어서는 안 됩니다.')
8 | except TypeError:
9 | print('모든 인자는 숫자여야 합니다.')
10 | except:
11 | print('ZeroDivisionError, TypeError를 제외한 다른 에러')
12 | else:
13 | print('Result: {0}'.format(c))
14 | finally:
15 | print('항상 finally 블록은 수행됩니다.')
--------------------------------------------------------------------------------
/3부/26장/Pi/2n222.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 | import RPi.GPIO as GPIO # GPIO 임포트
4 | import time
5 |
6 | pinNum = 12
7 | GPIO.setmode(GPIO.BCM) #GPIO num으로 사용
8 | GPIO.setup(pinNum, GPIO.OUT) #GPIO 12번 핀을 출력용으로 선언
9 |
10 | try:
11 | while True:
12 | val = input("switch [on:1, off:0] :")
13 | GPIO.output(pinNum, val)
14 | finally:
15 | GPIO.cleanup() #GPIO를 초기화
--------------------------------------------------------------------------------
/2부/14장/14-6-1.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect(":memory:")
3 | cur = con.cursor()
4 |
5 | cur.execute("CREATE TABLE PhoneBook(Name text, PhoneNum text);")
6 | cur.execute("INSERT INTO PhoneBook VALUES('Derick', '010-1234-5678');")
7 | list = (('Tom', '010-543-5432'), ('DSP', '010-123-1234'))
8 | cur.executemany("INSERT INTO PhoneBook VALUES(?, ?);", list)
9 |
10 | for l in con.iterdump():
11 | print(l)
--------------------------------------------------------------------------------
/1부/4장/4-5-1.py:
--------------------------------------------------------------------------------
1 | import time
2 | l = range(1000)
3 |
4 | t = time.mktime(time.localtime())
5 | for i in l:
6 | print(i,)
7 | t1 = time.mktime(time.localtime()) - t
8 |
9 | t = time.mktime(time.localtime())
10 | print(", ".join(str(i) for i in l))
11 | t2 = time.mktime(time.localtime()) - t
12 |
13 | print("for 문으로 각 인자를 출력")
14 | print("Take {0} seconds".format(t1))
15 | print("join() 메서드로 출력")
16 | print("Take {0} seconds".format(t2))
--------------------------------------------------------------------------------
/1부/5장/5-5-3.py:
--------------------------------------------------------------------------------
1 | class Sequencer:
2 | def __init__(self, maxValue):
3 | self.maxValue = maxValue
4 |
5 | def __len__(self):
6 | return self.maxValue
7 |
8 | def __getitem__(self, index):
9 | if 0 < index <= self.maxValue:
10 | return index * 10
11 | else:
12 | raise IndexError("Index out of range")
13 |
14 | def __contains__(self, item):
15 | return 0 < item <= self.maxValue
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/1부/5장/5-7-4.py:
--------------------------------------------------------------------------------
1 | class Animal:
2 | def __init__(self):
3 | print("Animal __init__()")
4 |
5 | class Tiger(Animal):
6 | def __init__(self):
7 | super().__init__()
8 | print("Tiger __init__()")
9 |
10 | class Lion(Animal):
11 | def __init__(self):
12 | super().__init__()
13 | print("Lion __init__()")
14 |
15 | class Liger(Tiger, Lion):
16 | def __init__(self):
17 | super().__init__()
18 | print("Liger __init__()")
--------------------------------------------------------------------------------
/3부/24장/hello_alexa/hello_alexa.py:
--------------------------------------------------------------------------------
1 | import flask
2 | import flask_ask
3 |
4 | app = flask.Flask(__name__)
5 | ask = flask_ask.Ask(app, "/")
6 |
7 | @ask.launch
8 | def launch_hello_alexa():
9 | msg = "Hi, My name is Alexa. What is your name?"
10 | return flask_ask.question(msg)
11 |
12 | @ask.intent('NameIntent')
13 | def hello(name):
14 | msg = "Nice to see you. {0}".format(name)
15 | return flask_ask.statement(msg)
16 |
17 | if __name__ == '__main__':
18 | app.run(debug=True)
--------------------------------------------------------------------------------
/1부/5장/5-5-1.py:
--------------------------------------------------------------------------------
1 | class GString:
2 | def __init__(self, init=None):
3 | self.content = init
4 |
5 | def __sub__(self, str):
6 | for i in str:
7 | self.content = self.content.replace(i, '')
8 | return GString(self.content)
9 |
10 | def __abs__(self):
11 | return GString(self.content.upper())
12 |
13 | def Print(self):
14 | print(self.content)
15 |
16 | g = GString("aBcdef")
17 | g -= "df" '-'
18 | g.Print()
19 | g = abs(g)
20 | g.Print()
--------------------------------------------------------------------------------
/3부/24장/baseball/interaction_model.json:
--------------------------------------------------------------------------------
1 | {
2 | "intents": [{
3 | "intent": "YesIntent"
4 | }, {
5 | "intent": "NoIntent"
6 | }, {
7 | "intent": "AnswerIntent",
8 | "slots": [{
9 | "name": "first",
10 | "type": "AMAZON.NUMBER"
11 | }, {
12 | "name": "second",
13 | "type": "AMAZON.NUMBER"
14 | }, {
15 | "name": "third",
16 | "type": "AMAZON.NUMBER"
17 | }]
18 | }]
19 | }
--------------------------------------------------------------------------------
/1부/5장/5-7-3.py:
--------------------------------------------------------------------------------
1 | class Animal:
2 | def __init__(self):
3 | print("Animal __init__()")
4 |
5 | class Tiger(Animal):
6 | def __init__(self):
7 | Animal.__init__(self)
8 | print("Tiger __init__()")
9 |
10 | class Lion(Animal):
11 | def __init__(self):
12 | Animal.__init__(self)
13 | print("Lion __init__()")
14 |
15 | class Liger(Tiger, Lion):
16 | def __init__(self):
17 | Tiger.__init__(self)
18 | Lion.__init__(self)
19 | print("Liger __init__()")
--------------------------------------------------------------------------------
/1부/7장/7-3-1.py:
--------------------------------------------------------------------------------
1 | class NegativeDivisionError(Exception):
2 | def __init__(self, value):
3 | self.value = value
4 |
5 | def PositiveDivide(a, b):
6 | if(b < 0):
7 | raise NegativeDivisionError(b)
8 | return a / b
9 |
10 | try:
11 | ret = PositiveDivide(10, -3)
12 | print('10 / 3 = {0}'.format(ret))
13 | except NegativeDivisionError as e:
14 | print('Error - Second argument of PositiveDivide is ', e.value)
15 | except ZeroDivisionError as e:
16 | print('Error - ', e.args[0])
17 | except :
18 | print("Unexpected exception!")
--------------------------------------------------------------------------------
/3부/26장/Pi/led.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 | import RPi.GPIO as GPIO # GPIO 임포트
4 | import time
5 |
6 | pinNum = 5
7 | GPIO.setmode(GPIO.BCM) #GPIO num으로 사용
8 | GPIO.setup(pinNum, GPIO.OUT) #GPIO 5번 핀을 출력용으로 선언
9 |
10 | cnt = 10
11 | flag = True
12 | while cnt > 0 :
13 | if flag :
14 | print ("LED is ON")
15 | else :
16 | print ("LED is OFF")
17 | GPIO.output(pinNum, flag) # flag가 True이면 LED가 켜지고 False이면 LED가 꺼진다
18 | cnt-=1
19 | flag = not flag
20 | time.sleep(0.5) # 0.5초 동안 sleep
21 | GPIO.cleanup() #GPIO를 초기화
22 |
--------------------------------------------------------------------------------
/3부/23장/nba_2016_06.csv:
--------------------------------------------------------------------------------
1 | Date,Start (ET),Visitor/Neutral,PTS,Home/Neutral,PTS,,,Notes
2 | Thu Jun 4 2015,9:00 pm,Cleveland Cavaliers,100,Golden State Warriors,108,Box Score,OT,
3 | Sun Jun 7 2015,8:00 pm,Cleveland Cavaliers,95,Golden State Warriors,93,Box Score,OT,
4 | Tue Jun 9 2015,9:00 pm,Golden State Warriors,91,Cleveland Cavaliers,96,Box Score,,
5 | Thu Jun 11 2015,9:00 pm,Golden State Warriors,103,Cleveland Cavaliers,82,Box Score,,
6 | Sun Jun 14 2015,8:00 pm,Cleveland Cavaliers,91,Golden State Warriors,104,Box Score,,
7 | Tue Jun 16 2015,9:00 pm,Golden State Warriors,105,Cleveland Cavaliers,97,Box Score,,
--------------------------------------------------------------------------------
/1부/5장/5-3-2.py:
--------------------------------------------------------------------------------
1 | class CounterManager:
2 | insCount = 0
3 | def __init__(self):
4 | CounterManager.insCount += 1
5 |
6 | def staticPrintCount():
7 | print("Instance Count: ", CounterManager.insCount)
8 | SPrintCount = staticmethod(staticPrintCount)
9 |
10 | def classPrintCount(cls):
11 | print("Instance Count: ", cls.insCount)
12 | CPrintCount = classmethod(classPrintCount)
13 |
14 | a, b, c = CounterManager(), CounterManager(), CounterManager()
15 |
16 | CounterManager.SPrintCount()
17 | b.SPrintCount()
18 |
19 | CounterManager.CPrintCount()
20 | b.CPrintCount()
--------------------------------------------------------------------------------
/3부/26장/Pi/btn.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 | import RPi.GPIO as GPIO
4 | import time
5 |
6 | LedPinNum = 5 # LED 제어 핀번호
7 | BtnPinNum = 6 # 버튼 입력 핀번호
8 | GPIO.setmode(GPIO.BCM)
9 | GPIO.setup(LedPinNum, GPIO.OUT)
10 | # 내부 풀업 저항을 이용
11 | GPIO.setup(BtnPinNum, GPIO.IN, pull_up_down=GPIO.PUD_UP)
12 |
13 | try:
14 | while True:
15 | #플업 회로에서는 버튼을 누르면 LOW 상태가 됨.
16 | if GPIO.input(BtnPinNum)==0:
17 | GPIO.output(LedPinNum, True)
18 | else:
19 | GPIO.output(LedPinNum, False)
20 | time.sleep(0.25)
21 |
22 | finally:
23 | GPIO.cleanup()
--------------------------------------------------------------------------------
/2부/22장/standard/index.yaml:
--------------------------------------------------------------------------------
1 | indexes:
2 |
3 | # AUTOGENERATED
4 |
5 | # This index.yaml is automatically updated whenever the dev_appserver
6 | # detects that a new type of query is run. If you want to manage the
7 | # index.yaml file manually, remove the above marker line (the line
8 | # saying "# AUTOGENERATED"). If you want to manage some indexes
9 | # manually, move them above the marker line. The index.yaml file is
10 | # automatically uploaded to the admin console when you next deploy
11 | # your application using appcfg.py.
12 |
13 | - kind: bookmark
14 | ancestor: yes
15 | properties:
16 | - name: date
17 | direction: desc
18 |
--------------------------------------------------------------------------------
/3부/24장/hello_alexa/hello_alexa2.py:
--------------------------------------------------------------------------------
1 | import flask
2 | import flask_ask
3 |
4 | app = flask.Flask(__name__)
5 | ask = flask_ask.Ask(app, "/")
6 |
7 | import logging
8 | logging.getLogger("flask_ask").setLevel(logging.DEBUG)
9 |
10 | @ask.launch
11 | def launch_hello_alexa():
12 | msg = flask.render_template('start')
13 | repeat_msg = flask.render_template('repeat')
14 | return flask_ask.question(msg).reprompt(repeat_msg)
15 |
16 | @ask.intent('NameIntent')
17 | def hello(name):
18 | msg = flask.render_template('answer', name = name)
19 | return flask_ask.statement(msg)
20 |
21 | if __name__ == '__main__':
22 | app.run(debug=True, port=5001)
--------------------------------------------------------------------------------
/1부/5장/5-6-2.py:
--------------------------------------------------------------------------------
1 | class Person:
2 | " 부모 클래스 "
3 | def __init__(self, name, phoneNumber):
4 | self.Name = name
5 | self.PhoneNumber = phoneNumber
6 |
7 | def PrintInfo(self):
8 | print("Info(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
9 |
10 | def PrintPersonData(self):
11 | print("Person(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
12 |
13 | class Student(Person):
14 | " 자식 클래스 "
15 | def __init__(self, name, phoneNumber, subject, studentID):
16 | Person.__init__(self, name, phoneNumber)
17 | self.Subject = subject
18 | self.StudentID = studentID
--------------------------------------------------------------------------------
/2부/21장/prime.py:
--------------------------------------------------------------------------------
1 | import sys,time
2 |
3 | if __name__=="__main__":
4 | print sys.version
5 | t1 = time.time() # start time
6 | N = 5000000
7 | # init
8 | sieve = {}
9 | for i in range(2, N+1):
10 | sieve[i] = 0
11 | # Sieve of Eratosthenes
12 | for i in range(2, N+1):
13 | if sieve[i]==0:
14 | n=2
15 | while i*n <= N:
16 | sieve[i*n]=1
17 | n+=1
18 | # print results count
19 | cnt = 0
20 | for i in range(2, N+1):
21 | if sieve[i]==0:
22 | cnt += 1
23 | t2 = time.time() # end time
24 | print cnt
25 | print "elapsed time=", t2-t1, " sec"
--------------------------------------------------------------------------------
/1부/5장/5-6-1.py:
--------------------------------------------------------------------------------
1 | class Person:
2 | " 부모 클래스 "
3 | def __init__(self, name, phoneNumber):
4 | self.Name = name
5 | self.PhoneNumber = phoneNumber
6 |
7 | def PrintInfo(self):
8 | print("Info(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
9 |
10 | def PrintPersonData(self):
11 | print("Person(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
12 |
13 | class Student(Person):
14 | " 자식 클래스 "
15 | def __init__(self, name, phoneNumber, subject, studentID):
16 | self.Name = name
17 | self.PhoneNumber = phoneNumber
18 | self.Subject = subject
19 | self.StudentID = studentID
--------------------------------------------------------------------------------
/2부/14장/14-4-1.py:
--------------------------------------------------------------------------------
1 | class Average:
2 | def __init__(self):
3 | self.sum = 0
4 | self.cnt = 0
5 |
6 | def step(self, value):
7 | self.sum += value
8 | self.cnt += 1
9 |
10 | def finalize(self):
11 | return self.sum / self.cnt
12 |
13 | import sqlite3
14 | con = sqlite3.connect(":memory:")
15 | cur = con.cursor()
16 |
17 | cur.execute("CREATE TABLE User(Name text, Age int);")
18 | list = (('Tom', '16'),
19 | ('DSP', '33'),
20 | ('Derick', '25'))
21 | cur.executemany("INSERT INTO User VALUES(?, ?);", list)
22 |
23 | con.create_aggregate("avg", 1, Average)
24 |
25 | cur.execute("SELECT avg(Age) FROM User")
26 | print(cur.fetchone()[0])
--------------------------------------------------------------------------------
/2부/16장/book.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The Very Hungry Caterpillar Pop-Up Book
5 |
6 |
7 | Philomel Books
8 | Celebrating the 40th anniverary of one of the most popular children's books ever created
9 |
10 |
11 | The Shack
12 |
13 |
14 | You Can Negotiate Anything
15 |
16 | Negotiate and narrative skill
17 |
18 |
--------------------------------------------------------------------------------
/2부/17장/book.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The Very Hungry Caterpillar Pop-Up Book
5 |
6 |
7 | Philomel Books
8 | Celebrating the 40th anniverary of one of the most popular children's books ever created
9 |
10 |
11 | The Shack
12 |
13 |
14 | You Can Negotiate Anything
15 |
16 | Negotiate and narrative skill
17 |
18 |
--------------------------------------------------------------------------------
/2부/14장/14-3-1.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | con = sqlite3.connect(":memory:")
3 | cur = con.cursor()
4 |
5 | cur.execute("CREATE TABLE PhoneBook(Name text, Age integer);")
6 | list = (('Tom', 24),('Derick',30), ('Peter',53), ('Jane',29))
7 | cur.executemany("INSERT INTO PhoneBook VALUES(?, ?);", list)
8 |
9 | cur.execute("SELECT length(Name), upper(Name), lower(Name) FROM PhoneBook")
10 | print("== length(), upper(), lower() ==")
11 | print([r for r in cur])
12 |
13 | cur.execute("SELECT max(Age), min(Age), sum(Age) FROM PhoneBook")
14 | print("== max(), min(), sum() ==")
15 | print([r for r in cur])
16 |
17 | cur.execute("SELECT count(*), random(*) FROM PhoneBook")
18 | print("== count(*), random(*) ==")
19 | print([r for r in cur])
--------------------------------------------------------------------------------
/2부/13장/tree.py:
--------------------------------------------------------------------------------
1 | import glob, os.path
2 | ndir = nfile = 0
3 |
4 | def traverse(dir, depth):
5 | global ndir, nfile
6 | for obj in glob.glob(dir + '/*'):
7 | if depth == 0:
8 | prefix = '|--'
9 | else:
10 | prefix = '|' + ' ' * depth + '|--'
11 | if os.path.isdir(obj):
12 | ndir += 1
13 | print(prefix + os.path.basename(obj))
14 | traverse(obj, depth + 1)
15 | elif os.path.isfile(obj):
16 | nfile += 1
17 | print(prefix + os.path.basename(obj))
18 | else:
19 | print(prefix + 'unknown object :', obj)
20 |
21 | if __name__ == '__main__':
22 | traverse('.', 0)
23 | print('\n', ndir, 'directories,', nfile, 'files')
--------------------------------------------------------------------------------
/2부/22장/standard/appengine_config.py:
--------------------------------------------------------------------------------
1 | # Copyright 2016 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from google.appengine.ext import vendor
16 |
17 | # Add any libraries installed in the "lib" folder.
18 | vendor.add('lib')
19 |
--------------------------------------------------------------------------------
/3부/26장/WebApp/appengine_config.py:
--------------------------------------------------------------------------------
1 | # Copyright 2016 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from google.appengine.ext import vendor
16 |
17 | # Add any libraries installed in the "lib" folder.
18 | vendor.add('lib')
19 |
--------------------------------------------------------------------------------
/1부/5장/5-6-3.py:
--------------------------------------------------------------------------------
1 | class Person:
2 | " 부모 클래스 "
3 | def __init__(self, name, phoneNumber):
4 | self.Name = name
5 | self.PhoneNumber = phoneNumber
6 |
7 | def PrintInfo(self):
8 | print("Info(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
9 |
10 | def PrintPersonData(self):
11 | print("Person(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
12 |
13 | class Student(Person):
14 | " 자식 클래스 "
15 | def __init__(self, name, phoneNumber, subject, studentID):
16 | Person.__init__(self, name, phoneNumber)
17 | self.Subject = subject
18 | self.StudentID = studentID
19 |
20 | def PrintStudentData(self):
21 | print("Student(Subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID))
--------------------------------------------------------------------------------
/2부/17장/openapi_http.py:
--------------------------------------------------------------------------------
1 | # -*- coding:cp949 -*-
2 | import os
3 | import sys
4 | import http.client
5 | from xml.dom.minidom import parseString
6 |
7 | client_id = "CLIENT ID"
8 | client_secret = "CLIENT SECRET"
9 |
10 | #openAPI가 https 프로토콜을 사용하기 때문에 HTTPSConnection 을 사용함.
11 | conn = http.client.HTTPSConnection("openapi.naver.com")
12 | #conn.set_debuglevel(1) #debug mode 설정
13 | headers = {"X-Naver-Client-Id": client_id, "X-Naver-Client-Secret": client_secret}
14 | encText = "love"
15 | params = "?query=" + encText + "&display=10&start=1"
16 |
17 | conn.request("GET", "/v1/search/book.xml" + params, None, headers)
18 | res = conn.getresponse()
19 |
20 | if int(res.status) == 200 :
21 | print(parseString(res.read().decode('utf-8')).toprettyxml())
22 | else:
23 | print ("HTTP Request is failed :" + res.reason)
24 | print (res.read().decode('utf-8'))
25 |
26 | conn.close()
--------------------------------------------------------------------------------
/2부/14장/14-5-1.py:
--------------------------------------------------------------------------------
1 | class Point(object):
2 | def __init__(self, x, y):
3 | self.x, self.y = x, y
4 |
5 | def __repr__(self):
6 | return "Point(%f, %f)" % (self.x, self.y)
7 |
8 | import sqlite3
9 | def PointAdapter(point):
10 | return "%f:%f" % (point.x, point.y)
11 |
12 | def PointConverter(s):
13 | x, y = list(map(float, s.decode().split(":")))
14 | return Point(x, y)
15 |
16 | sqlite3.register_adapter(Point, PointAdapter)
17 | sqlite3.register_converter("point", PointConverter)
18 |
19 | p = Point(4, -3.2)
20 | p2 = Point(-1.4, 6.2)
21 |
22 | con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
23 | cur = con.cursor()
24 | cur.execute("create table test(p point)")
25 | cur.execute("insert into test values (?)", (p, ))
26 | cur.execute("insert into test(p) values (?)", (p2,))
27 |
28 | cur.execute("select p from test")
29 | print([r[0] for r in cur])
30 | cur.close()
31 | con.close()
32 |
--------------------------------------------------------------------------------
/2부/17장/openapi_urllib.py:
--------------------------------------------------------------------------------
1 | # -*- coding:cp949 -*-
2 | import os
3 | import sys
4 | import urllib.request
5 | from xml.dom.minidom import parseString
6 |
7 | client_id = "CLIENT ID"
8 | client_secret = "CLIENT SECRET"
9 | encText = urllib.parse.quote("love")
10 | url = "https://openapi.naver.com/v1/search/book.xml?query=" + encText
11 | url += "&display=10&start=1"
12 | resp = None
13 |
14 | req = urllib.request.Request(url)
15 | req.add_header("X-Naver-Client-Id",client_id)
16 | req.add_header("X-Naver-Client-Secret",client_secret)
17 | try:
18 | resp = urllib.request.urlopen(req)
19 | except urllib.error.URLError as e:
20 | print(e.reason)
21 | print(parseString(e.read().decode('utf-8')).toprettyxml())
22 | except urllib.error.HTTPError as e:
23 | print("error code=" + e.code)
24 | print(parseString(e.read().decode('utf-8')).toprettyxml())
25 | else:
26 | response_body = resp.read()
27 | print(parseString(response_body.decode('utf-8')).toprettyxml())
--------------------------------------------------------------------------------
/1부/5장/5-6-5.py:
--------------------------------------------------------------------------------
1 | class Person:
2 | " 부모 클래스 "
3 | def __init__(self, name, phoneNumber):
4 | self.Name = name
5 | self.PhoneNumber = phoneNumber
6 |
7 | def PrintInfo(self):
8 | print("Info(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
9 |
10 | def PrintPersonData(self):
11 | print("Person(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
12 |
13 | class Student(Person):
14 | " 자식 클래스 "
15 | def __init__(self, name, phoneNumber, subject, studentID):
16 | Person.__init__(self, name, phoneNumber)
17 | self.Subject = subject
18 | self.StudentID = studentID
19 |
20 | def PrintStudentData(self):
21 | print("Student(Subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID))
22 |
23 | def PrintInfo(self):
24 | Person.PrintPersonData(self)
25 | print("Info(Subject:{0}, Student ID:{1})".format(self.Subject, self.StudentID))
26 |
--------------------------------------------------------------------------------
/2부/22장/flexible/app.yaml:
--------------------------------------------------------------------------------
1 | # Copyright 2015 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # This file specifies your Python application's runtime configuration.
16 | # See https://cloud.google.com/appengine/docs/managed-vms/python/runtime
17 | # for details.
18 |
19 | # [START runtime]
20 | runtime: python
21 | env: flex
22 | entrypoint: gunicorn -b :$PORT main:app
23 |
24 | runtime_config:
25 | python_version: 3
26 | # [END runtime]
27 |
--------------------------------------------------------------------------------
/1부/5장/5-6-4.py:
--------------------------------------------------------------------------------
1 | class Person:
2 | " 부모 클래스 "
3 | def __init__(self, name, phoneNumber):
4 | self.Name = name
5 | self.PhoneNumber = phoneNumber
6 |
7 | def PrintInfo(self):
8 | print("Info(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
9 |
10 | def PrintPersonData(self):
11 | print("Person(Name:{0}, Phone Number: {1})".format(self.Name, self.PhoneNumber))
12 |
13 | class Student(Person):
14 | " 자식 클래스 "
15 | def __init__(self, name, phoneNumber, subject, studentID):
16 | Person.__init__(self, name, phoneNumber)
17 | self.Subject = subject
18 | self.StudentID = studentID
19 |
20 | def PrintStudentData(self):
21 | print("Student(Subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID))
22 |
23 | def PrintInfo(self):
24 | print("Info(Name:{0}, Phone Number:{1})".format(self.Name, self.PhoneNumber))
25 | print("Info(Subject:{0}, Student ID:{1})".format(self.Subject, self.StudentID))
26 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/NodeFilter.py:
--------------------------------------------------------------------------------
1 | # This is the Python mapping for interface NodeFilter from
2 | # DOM2-Traversal-Range. It contains only constants.
3 |
4 | class NodeFilter:
5 | """
6 | This is the DOM2 NodeFilter interface. It contains only constants.
7 | """
8 | FILTER_ACCEPT = 1
9 | FILTER_REJECT = 2
10 | FILTER_SKIP = 3
11 |
12 | SHOW_ALL = 0xFFFFFFFF
13 | SHOW_ELEMENT = 0x00000001
14 | SHOW_ATTRIBUTE = 0x00000002
15 | SHOW_TEXT = 0x00000004
16 | SHOW_CDATA_SECTION = 0x00000008
17 | SHOW_ENTITY_REFERENCE = 0x00000010
18 | SHOW_ENTITY = 0x00000020
19 | SHOW_PROCESSING_INSTRUCTION = 0x00000040
20 | SHOW_COMMENT = 0x00000080
21 | SHOW_DOCUMENT = 0x00000100
22 | SHOW_DOCUMENT_TYPE = 0x00000200
23 | SHOW_DOCUMENT_FRAGMENT = 0x00000400
24 | SHOW_NOTATION = 0x00000800
25 |
26 | def acceptNode(self, node):
27 | raise NotImplementedError
28 |
--------------------------------------------------------------------------------
/2부/22장/standard/bookmark.py:
--------------------------------------------------------------------------------
1 | from google.appengine.ext import ndb
2 | import time
3 |
4 | bookmark_list = []
5 |
6 | #create bookmark model.
7 | class bookmark(ndb.Model):
8 | date = ndb.DateTimeProperty(auto_now_add=True)
9 | name = ndb.StringProperty()
10 | url = ndb.StringProperty()
11 |
12 | @classmethod
13 | def query_bookmark(cls):
14 | return cls.query().order(-cls.date) #order by date, descending
15 |
16 | def get_list():
17 | del bookmark_list[:]
18 | bms = bookmark.query_bookmark().fetch() #query bookmark
19 |
20 | for bm in bms:
21 | entity = {}
22 | entity['name'] = bm.name
23 | entity['url'] = bm.url
24 | entity['id'] = bm.key.id()
25 | #print (entity)
26 | bookmark_list.append(entity)
27 |
28 | return bookmark_list
29 |
30 | def update(data, id=None):
31 |
32 | #bm bookmark()
33 | if id:
34 | bm = ndb.Key('bookmark', id)
35 | else:
36 | bm = bookmark()
37 | bm.id = int(time.time())
38 |
39 | bm.name = data['name']
40 | bm.url = data['url']
41 | bm_key = bm.put()
42 |
43 | return bm_key.id()
44 |
45 | add = update
46 |
47 | def delete(id):
48 | del_key = ndb.Key('bookmark', id)
49 | del_key.delete()
50 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/__init__.py:
--------------------------------------------------------------------------------
1 | """Core XML support for Python.
2 |
3 | This package contains four sub-packages:
4 |
5 | dom -- The W3C Document Object Model. This supports DOM Level 1 +
6 | Namespaces.
7 |
8 | parsers -- Python wrappers for XML parsers (currently only supports Expat).
9 |
10 | sax -- The Simple API for XML, developed by XML-Dev, led by David
11 | Megginson and ported to Python by Lars Marius Garshol. This
12 | supports the SAX 2 API.
13 |
14 | etree -- The ElementTree XML library. This is a subset of the full
15 | ElementTree XML release.
16 |
17 | """
18 |
19 |
20 | __all__ = ["dom", "parsers", "sax", "etree"]
21 |
22 | # When being checked-out without options, this has the form
23 | # "Revision: x.y "
24 | # When exported using -kv, it is "x.y".
25 | __version__ = "$Revision: 41660 $".split()[-2:][0]
26 |
27 |
28 | _MINIMUM_XMLPLUS_VERSION = (0, 8, 4)
29 |
30 |
31 | try:
32 | import _xmlplus
33 | except ImportError:
34 | pass
35 | else:
36 | try:
37 | v = _xmlplus.version_info
38 | except AttributeError:
39 | # _xmlplus is too old; ignore it
40 | pass
41 | else:
42 | if v >= _MINIMUM_XMLPLUS_VERSION:
43 | import sys
44 | _xmlplus.__path__.extend(__path__)
45 | sys.modules[__name__] = _xmlplus
46 | else:
47 | del v
48 |
--------------------------------------------------------------------------------
/3부/26장/WebApp/air.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | from google.appengine.ext import ndb
3 | from datetime import datetime, timedelta
4 | from decimal import Decimal
5 | import time
6 | import json
7 |
8 | class airInfo(ndb.Model):
9 | date = ndb.DateTimeProperty(auto_now_add=True)
10 | temp = ndb.FloatProperty()
11 | press = ndb.FloatProperty()
12 | hum = ndb.FloatProperty()
13 | pm25 = ndb.FloatProperty()
14 | pm10 = ndb.FloatProperty()
15 |
16 | def toDictFromNDB(p) :
17 | return { 'temp': str(p['temp']), 'pm10' : str(p['pm10']),\
18 | 'pm25': str(p['pm25']), 'hum':str(p['hum']), 'press':str(p['press']),\
19 | 'date':(p['date']+timedelta(hours=9)).strftime("%Y-%m-%d %H:%M %Z") }
20 |
21 | def getAirInfoList(period):
22 | dt = datetime.now()
23 | td = timedelta(days=period)
24 |
25 | airInfos = airInfo.query().order(airInfo.date).filter(airInfo.date > (dt-td)).fetch()
26 | return json.JSONEncoder().encode([toDictFromNDB(p.to_dict()) for p in airInfos])
27 |
28 | def saveAirInfoList(data):
29 | airinfo = airInfo()
30 | airinfo.pm10 = float(data['pm10'])
31 | airinfo.pm25 = float(data['pm25'])
32 | airinfo.temp = float(data['temp'])
33 | airinfo.hum = float(data['hum'])
34 | airinfo.press = float(data['press'])
35 |
36 | airinfo_key = airinfo.put()
37 | return airinfo_key.id()
38 |
39 | def clearAirInfoList():
40 | ndb.delete_multi(airInfo.query().fetch(keys_only=True))
41 |
--------------------------------------------------------------------------------
/2부/22장/flexible/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | bookmark sample
5 |
6 |
7 |
8 |
9 |
10 |
11 | {# [START form] #}
12 |
20 | {# [END form] #}
21 |
22 |
25 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/2부/22장/standard/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | bookmark sample
5 |
6 |
7 |
8 |
9 |
10 |
11 | {# [START form] #}
12 |
20 | {# [END form] #}
21 |
22 |
25 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/2부/22장/flexible/bookmark.py:
--------------------------------------------------------------------------------
1 | from datetime import datetime
2 | from google.cloud import datastore
3 |
4 | YOUR_PROJECT_ID = "fastpy001"
5 | DS_KIND = "bookmark"
6 | bookmark_list = []
7 |
8 | def get_client():
9 | return datastore.Client(YOUR_PROJECT_ID)
10 |
11 | def get_list():
12 | ds = get_client()
13 | query = ds.query(kind=DS_KIND, order=['name'])
14 | query_iter = query.fetch()
15 | bookmark_list.clear()
16 | for entity in query_iter:
17 | #print ("id:" + str(entity.key.id) + " name:" + entity['name'])
18 | entity['id'] = entity.key.id
19 | bookmark_list.append(entity)
20 |
21 | return bookmark_list
22 |
23 | def update(data, id=None):
24 | ds = get_client()
25 |
26 | if id:
27 | key = ds.key(DS_KIND, int(id))
28 | else:
29 | key = ds.key(DS_KIND)
30 |
31 | entity = datastore.Entity(key=key,
32 | exclude_from_indexes=['date','url'])
33 | entity.update(data)
34 | # entity['name'] = 'daum'
35 | ds.put(entity)
36 | return entity.key.id
37 |
38 | add = update
39 |
40 | def delete(id):
41 | ds = get_client()
42 | key = ds.key(DS_KIND, int(id))
43 | ds.delete(key)
44 |
45 | if __name__ == '__main__':
46 | print ("it is bookmark app")
47 | newbm = {"name":"google", "url":"http://www.google.com", "date": datetime.now() }
48 | print("insert google url to bookmark")
49 | id = add(newbm)
50 | print ("added key's id:" + str(id))
51 | get_list()
52 | for en in bookmark_list:
53 | print ("id:" + str(en['id']) + " name:" + en['name'])
54 | print("delete google url")
55 | delete(id)
56 | get_list()
57 | for en in bookmark_list:
58 | print ("id:" + str(en['id']) + " name:" + en['name'])
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/__init__.py:
--------------------------------------------------------------------------------
1 | # $Id: __init__.py 1821 2004-06-03 16:57:49Z fredrik $
2 | # elementtree package
3 |
4 | # --------------------------------------------------------------------
5 | # The ElementTree toolkit is
6 | #
7 | # Copyright (c) 1999-2004 by Fredrik Lundh
8 | #
9 | # By obtaining, using, and/or copying this software and/or its
10 | # associated documentation, you agree that you have read, understood,
11 | # and will comply with the following terms and conditions:
12 | #
13 | # Permission to use, copy, modify, and distribute this software and
14 | # its associated documentation for any purpose and without fee is
15 | # hereby granted, provided that the above copyright notice appears in
16 | # all copies, and that both that copyright notice and this permission
17 | # notice appear in supporting documentation, and that the name of
18 | # Secret Labs AB or the author not be used in advertising or publicity
19 | # pertaining to distribution of the software without specific, written
20 | # prior permission.
21 | #
22 | # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
23 | # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
24 | # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
25 | # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
26 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
28 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 | # OF THIS SOFTWARE.
30 | # --------------------------------------------------------------------
31 |
32 | # Licensed to PSF under a Contributor Agreement.
33 | # See http://www.python.org/2.4/license for licensing details.
34 |
--------------------------------------------------------------------------------
/2부/22장/flexible/main.py:
--------------------------------------------------------------------------------
1 | # Copyright 2015 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # [START app]
16 | from flask import redirect, render_template, request, url_for, Flask, current_app
17 | from bookmark import get_list, add, delete
18 |
19 | app = Flask(__name__)
20 | app.debug = True #debug mode
21 |
22 | @app.route('/')
23 | def bookmarklist():
24 | """Return a friendly HTTP greeting."""
25 | bookmarks = get_list()
26 | return render_template(
27 | "home.html",
28 | bookmarks=bookmarks)
29 |
30 | @app.route('/addbookmark', methods=['POST'])
31 | def addbookmark():
32 | if request.method == 'POST':
33 | data = request.form.to_dict(flat=True)
34 |
35 | id = add(data)
36 | print ("added id is " + str(id))
37 |
38 | return redirect("/")
39 |
40 | return "error"
41 |
42 | @app.route('/delbookmark', methods=['POST'])
43 | def delbookmark():
44 | if request.method == 'POST':
45 | data = request.form.getlist('check')
46 | for id in data:
47 | delete(int(id))
48 | print ("deleted id is " + str(id))
49 |
50 | return redirect("/")
51 |
52 | return "error"
53 | if __name__ == '__main__':
54 | app.run(host='127.0.0.1', port=8080)
55 | # [END app]
56 |
--------------------------------------------------------------------------------
/2부/22장/standard/main.py:
--------------------------------------------------------------------------------
1 | # Copyright 2015 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # [START app]
16 | from flask import redirect, render_template, request, url_for, Flask, current_app
17 | from bookmark import get_list, add, delete
18 |
19 | app = Flask(__name__)
20 | app.debug = True #debug mode
21 |
22 | @app.route('/')
23 | def bookmarklist():
24 | """Return a friendly HTTP greeting."""
25 | bookmarks = get_list()
26 | #bookmarks = []
27 | return render_template(
28 | "home.html",
29 | bookmarks=bookmarks)
30 |
31 | @app.route('/addbookmark', methods=['POST'])
32 | def addbookmark():
33 | if request.method == 'POST':
34 | data = request.form.to_dict(flat=True)
35 |
36 | id = add(data)
37 | print ("added id is " + str(id))
38 |
39 | return redirect("/")
40 |
41 | return "error"
42 |
43 | @app.route('/delbookmark', methods=['POST'])
44 | def delbookmark():
45 | if request.method == 'POST':
46 | data = request.form.getlist('check')
47 | for id in data:
48 | delete(int(id))
49 | print ("deleted id is " + str(id))
50 |
51 | return redirect("/")
52 |
53 | return "error"
54 | if __name__ == '__main__':
55 | app.run(host='127.0.0.1', port=8080)
56 | # [END app]
57 |
--------------------------------------------------------------------------------
/1부/9장/circle_prototype/circle_prototype.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | typedef struct {
4 | PyObject_HEAD
5 | } circle_CircleObject;
6 |
7 | static PyTypeObject circle_CircleType = {
8 | PyObject_HEAD_INIT(NULL)
9 | "circle.Circle", /* tp_name */
10 | sizeof(circle_CircleObject), /* tp_basicsize */
11 | 0, /* tp_itemsize */
12 | 0, /* tp_dealloc */
13 | 0, /* tp_print */
14 | 0, /* tp_getattr */
15 | 0, /* tp_setattr */
16 | 0, /* tp_reserved */
17 | 0, /* tp_repr */
18 | 0, /* tp_as_number */
19 | 0, /* tp_as_sequence */
20 | 0, /* tp_as_mapping */
21 | 0, /* tp_hash */
22 | 0, /* tp_call */
23 | 0, /* tp_str */
24 | 0, /* tp_getattro */
25 | 0, /* tp_setattro */
26 | 0, /* tp_as_buffer */
27 | Py_TPFLAGS_DEFAULT, /* tp_flags */
28 | "Circle objects", /* tp_doc */
29 | };
30 |
31 | static PyModuleDef circlemodule = {
32 | PyModuleDef_HEAD_INIT,
33 | "circle",
34 | "Example module that creates an extension type.",
35 | -1,
36 | NULL, NULL, NULL, NULL, NULL
37 | };
38 |
39 | PyMODINIT_FUNC
40 | PyInit_circle(void)
41 | {
42 | PyObject* m;
43 | circle_CircleType.tp_new = PyType_GenericNew;
44 | if (PyType_Ready(&circle_CircleType) < 0)
45 | return NULL;
46 |
47 | m = PyModule_Create(&circlemodule);
48 | if (m == NULL)
49 | return NULL;
50 |
51 | Py_INCREF(&circle_CircleType);
52 | PyModule_AddObject(m, "Circle", (PyObject *)&circle_CircleType);
53 | return m;
54 | }
55 |
--------------------------------------------------------------------------------
/2부/14장/pysqlite_command.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 | import sys
3 | import re
4 |
5 | # 데이터베이스 경로 설정
6 | if len(sys.argv) == 2:
7 | path = sys.argv[1]
8 | else:
9 | path = ":memory:"
10 |
11 | con = sqlite3.connect(path)
12 | con.isolation_level = None
13 | cur = con.cursor()
14 |
15 | buffer = ""
16 |
17 | def PrintIntro():
18 | "프로그램 소개 메시지"
19 | print("pysqlite의 command 프로그램입니다.")
20 | print("특수 명령어를 알고 싶으시면 '.help;'를 입력하세요.")
21 | print("SQL 구문은 ';'으로 끝나야 합니다.")
22 |
23 | def PrintHelp():
24 | "도움말"
25 | print(".dump\t\t데이터베이스의 내용을 덤프합니다.")
26 |
27 | def SQLDump(con, file=None):
28 | "데이터베이스 내용 덤프"
29 | if file != None:
30 | f = open(file, "w")
31 | else:
32 | f = sys.stdout
33 |
34 | for l in con.iterdump():
35 | f.write("{0}\n".format(l))
36 |
37 | if f != sys.stdout:
38 | f.close()
39 |
40 | PrintIntro()
41 |
42 | while True:
43 | line = input("pysqlite>> ")
44 | if buffer == "" and line == "":
45 | break
46 | buffer += line
47 |
48 | if sqlite3.complete_statement(buffer):
49 | buffer = buffer.strip()
50 |
51 | if buffer[0]==".":
52 | cmd = re.sub('[ ;]', ' ', buffer).split()
53 | if cmd[0] == '.help':
54 | PrintHelp()
55 | elif cmd[0] == '.dump':
56 | if len(cmd) == 2:
57 | SQLDump(con, cmd[1])
58 | else:
59 | SQLDump(con)
60 | else:
61 | try:
62 | buffer = buffer.strip()
63 | cur.execute(buffer)
64 |
65 | if buffer.lstrip().upper().startswith("SELECT"):
66 | print(cur.fetchall())
67 | except sqlite3.Error as e:
68 | print("Error: ", e.args[0])
69 | else:
70 | print("구문이 성공적으로 수행되었습니다.")
71 | buffer=""
72 | con.close()
73 | print("프로그램을 종료합니다. 야옹~")
--------------------------------------------------------------------------------
/3부/23장/nba_2015_10.csv:
--------------------------------------------------------------------------------
1 | Date,Start (ET),Visitor/Neutral,PTS,Home/Neutral,PTS,,,Notes
2 | Tue Oct 28 2014,10:30 pm,Houston Rockets,108,Los Angeles Lakers,90,Box Score,,
3 | Tue Oct 28 2014,8:00 pm,Orlando Magic,84,New Orleans Pelicans,101,Box Score,,
4 | Tue Oct 28 2014,8:00 pm,Dallas Mavericks,100,San Antonio Spurs,101,Box Score,,
5 | Wed Oct 29 2014,7:30 pm,Brooklyn Nets,105,Boston Celtics,121,Box Score,,
6 | Wed Oct 29 2014,7:00 pm,Milwaukee Bucks,106,Charlotte Hornets,108,Box Score,OT,
7 | Wed Oct 29 2014,9:00 pm,Detroit Pistons,79,Denver Nuggets,89,Box Score,,
8 | Wed Oct 29 2014,7:00 pm,Philadelphia 76ers,91,Indiana Pacers,103,Box Score,,
9 | Wed Oct 29 2014,8:00 pm,Minnesota Timberwolves,101,Memphis Grizzlies,105,Box Score,,
10 | Wed Oct 29 2014,7:30 pm,Washington Wizards,95,Miami Heat,107,Box Score,,
11 | Wed Oct 29 2014,8:00 pm,Chicago Bulls,104,New York Knicks,80,Box Score,,
12 | Wed Oct 29 2014,10:00 pm,Los Angeles Lakers,99,Phoenix Suns,119,Box Score,,
13 | Wed Oct 29 2014,10:30 pm,Oklahoma City Thunder,89,Portland Trail Blazers,106,Box Score,,
14 | Wed Oct 29 2014,10:00 pm,Golden State Warriors,95,Sacramento Kings,77,Box Score,,
15 | Wed Oct 29 2014,7:30 pm,Atlanta Hawks,102,Toronto Raptors,109,Box Score,,
16 | Wed Oct 29 2014,9:00 pm,Houston Rockets,104,Utah Jazz,93,Box Score,,
17 | Thu Oct 30 2014,8:00 pm,New York Knicks,95,Cleveland Cavaliers,90,Box Score,,
18 | Thu Oct 30 2014,8:30 pm,Utah Jazz,102,Dallas Mavericks,120,Box Score,,
19 | Thu Oct 30 2014,10:30 pm,Oklahoma City Thunder,90,Los Angeles Clippers,93,Box Score,,
20 | Thu Oct 30 2014,8:00 pm,Detroit Pistons,91,Minnesota Timberwolves,97,Box Score,,
21 | Thu Oct 30 2014,7:00 pm,Washington Wizards,105,Orlando Magic,98,Box Score,,
22 | Fri Oct 31 2014,8:00 pm,Cleveland Cavaliers,114,Chicago Bulls,108,Box Score,OT,
23 | Fri Oct 31 2014,7:00 pm,Memphis Grizzlies,97,Indiana Pacers,89,Box Score,,
24 | Fri Oct 31 2014,10:30 pm,Los Angeles Clippers,118,Los Angeles Lakers,111,Box Score,,
25 | Fri Oct 31 2014,8:30 pm,Philadelphia 76ers,81,Milwaukee Bucks,93,Box Score,,
26 | Fri Oct 31 2014,10:00 pm,San Antonio Spurs,89,Phoenix Suns,94,Box Score,,
27 | Fri Oct 31 2014,10:00 pm,Portland Trail Blazers,94,Sacramento Kings,103,Box Score,,
--------------------------------------------------------------------------------
/2부/17장/launcher.py:
--------------------------------------------------------------------------------
1 | # -*- coding: cp949 -*-
2 | loopFlag = 1
3 | from internetbook import *
4 |
5 | #### Menu implementation
6 | def printMenu():
7 | print("\nWelcome! Book Manager Program (xml version)")
8 | print("========Menu==========")
9 | print("Load xml: l")
10 | print("Print dom to xml: p")
11 | print("Quit program: q")
12 | print("print Book list: b")
13 | print("Add new book: a")
14 | print("sEarch Book Title: e")
15 | print("Make html: m")
16 | print("----------------------------------------")
17 | print("Get book data from isbn: g")
18 | print("send maIl : i")
19 | print("sTart Web Service: t")
20 | print("========Menu==========")
21 |
22 | def launcherFunction(menu):
23 | if menu == 'l':
24 | LoadXMLFromFile()
25 | elif menu == 'q':
26 | QuitBookMgr()
27 | elif menu == 'p':
28 | PrintDOMtoXML()
29 | elif menu == 'b':
30 | PrintBookList(["title",])
31 | elif menu == 'a':
32 | ISBN = str(input ('insert ISBN :'))
33 | title = str(input ('insert Title :'))
34 | AddBook({'ISBN':ISBN, 'title':title})
35 | elif menu == 'e':
36 | keyword = str(input ('input keyword to search :'))
37 | printBookList(SearchBookTitle(keyword))
38 | elif menu == 'g':
39 | isbn = str(input ('input isbn to get :'))
40 | #isbn = '0596513984'
41 | ret = getBookDataFromISBN(isbn)
42 | AddBook(ret)
43 | elif menu == 'm':
44 | keyword = str(input ('input keyword code to the html :'))
45 | html = MakeHtmlDoc(SearchBookTitle(keyword))
46 | print("-----------------------")
47 | print(html)
48 | print("-----------------------")
49 | elif menu == 'i':
50 | sendMain()
51 | elif menu == "t":
52 | startWebService()
53 | else:
54 | print ("error : unknow menu key")
55 |
56 | def QuitBookMgr():
57 | global loopFlag
58 | loopFlag = 0
59 | BooksFree()
60 |
61 | ##### run #####
62 | while(loopFlag > 0):
63 | printMenu()
64 | menuKey = str(input ('select menu :'))
65 | launcherFunction(menuKey)
66 | else:
67 | print ("Thank you! Good Bye")
68 |
--------------------------------------------------------------------------------
/3부/26장/Pi/oled.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | import time
5 | import bme280
6 |
7 | import Adafruit_GPIO.SPI as SPI
8 | import Adafruit_SSD1306
9 |
10 | # Python Image Library를 임포트 합니다.
11 | from PIL import Image
12 | from PIL import ImageDraw
13 | from PIL import ImageFont
14 |
15 | # 라즈베리 파이와 연결된 BCM 핀번호를 선언합니다.
16 | RST = 24
17 | DC = 23
18 | SPI_PORT = 0
19 | SPI_DEVICE = 0
20 |
21 | # SPI를 사용하는 128x64 해상도 디스플레이를 선언합니다.
22 | disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
23 |
24 | # 라이브러리를 초기화
25 | disp.begin()
26 |
27 | # 디스플레이를 클리어 해줍니다.
28 | disp.clear()
29 | disp.display()
30 |
31 | width = disp.width
32 | height = disp.height
33 | # 디스플레이와 같은 사이즈(128x64)의 흑백 (1bit) PIL Image 객체를 생성합니다.
34 | image = Image.new('1', (width, height))
35 |
36 | # 이미지를 그리기위한 draw 객체를 가져옵니다. 일종의 캔버스라고 생각하면 됩니다.
37 | draw = ImageDraw.Draw(image)
38 |
39 | try:
40 | while True:
41 | # 캔버스에 1픽셀 두께의 사각형을 그리고 내부는 0 (검은색)으로 칠해줍니다.
42 | draw.rectangle((0,0,width-1,height-1), outline=1, fill=0)
43 |
44 | # 트루 타입 폰트를 로딩합니다. 폰트 사이즈는 15입니다.
45 | font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 15)
46 | # 트루 타입 한글 폰트
47 | hanFont = ImageFont.truetype('/usr/share/fonts/truetype/unfonts-core/UnDinaru.ttf', 12)
48 |
49 | # 온도, 대기압, 습도를 BME280부터 읽어 옵니다.
50 | temperature,pressure,humidity = bme280.readBME280All()
51 |
52 | top = 3
53 | labelX = 5
54 | dataX = 40
55 |
56 | # (labelX, top) 좌표를 기준으로 텍스트를 출력합니다.
57 | draw.text((labelX, top), u"온도", font=hanFont, fill=255)
58 | draw.text((labelX, top+20), u"습도", font=hanFont, fill=255)
59 | draw.text((labelX, top+40), u"대기압", font=hanFont, fill=255)
60 |
61 | draw.text((dataX, top), ":" + str(temperature) + "C", font=font, fill=255)
62 | draw.text((dataX, top+20), ":" + str(round(humidity,1)) + "%", font=font, fill=255)
63 | draw.text((dataX, top+40), ":" + str(round(pressure,2)), font=font, fill=255)
64 |
65 | # PIL Image 객체를 Bitmap으로 변환합니다.
66 | disp.image(image)
67 | # Bitmap 버퍼를 디스플레이 장치로 전송합니다.
68 | disp.display()
69 | time.sleep(3)
70 | finally:
71 | disp.clear()
72 | disp.display()
73 |
--------------------------------------------------------------------------------
/3부/24장/baseball/baseball.py:
--------------------------------------------------------------------------------
1 | import random
2 | import logging
3 | import flask
4 | import flask_ask
5 |
6 | app = flask.Flask(__name__)
7 | ask = flask_ask.Ask(app, "/")
8 | logging.getLogger("flask_ask").setLevel(logging.DEBUG)
9 |
10 | def get_baseball_num():
11 | num = []
12 | num.append(random.randrange(0, 10, 1))
13 | num.append(num[0])
14 | num.append(num[0])
15 |
16 | while (num[0] == num[1]):
17 | num[1] = random.randrange(0, 10, 1)
18 |
19 | while (num[0] == num[2] or num[1] == num[2]):
20 | num[2] = random.randrange(0, 10, 1)
21 |
22 | return num
23 |
24 | def check_baseball_num(input_num):
25 | strike_cnt = 0
26 | ball_cnt = 0
27 | answer_num = flask_ask.session.attributes['answer']
28 |
29 | for i in range(0, 3):
30 | for j in range(0, 3):
31 | if input_num[i] == answer_num[j] and i == j:
32 | strike_cnt += 1
33 | elif input_num[i] == answer_num[j]:
34 | ball_cnt += 1
35 | if strike_cnt == 3:
36 | msg = "3 strikes! Good Job"
37 | return (msg, True)
38 | elif ball_cnt == 3:
39 | msg = "3 balls"
40 | return (msg, False)
41 | else:
42 | msg = "{0} balls and {1} strikes".format(ball_cnt, strike_cnt)
43 | return (msg, False)
44 |
45 | @ask.launch
46 | def launch_game():
47 | msg = flask.render_template('start')
48 | return flask_ask.question(msg)
49 |
50 | @ask.intent("YesIntent")
51 | def start_game():
52 | msg = flask.render_template('rule')
53 | answer = get_baseball_num()
54 | flask_ask.session.attributes['answer'] = answer
55 | logging.info("Answer is {0}".format(answer))
56 |
57 | return flask_ask.question(msg)
58 |
59 | @ask.intent("NoIntent")
60 | def end_game():
61 | msg = flask.render_template('end')
62 | return flask_ask.statement(msg)
63 |
64 | @ask.intent("AnswerIntent", convert={'first': int, 'second': int, 'third': int})
65 | def answer(first, second, third):
66 | input_num = [first, second, third]
67 | logging.info("input number is {0}".format(input_num))
68 | msg, ret = check_baseball_num(input_num)
69 |
70 | if ret:
71 | return flask_ask.statement(msg)
72 | else:
73 | msg += ". Try again!"
74 | return flask_ask.question(msg)
75 |
76 | if __name__ == '__main__':
77 | app.run(debug=True, port=7000)
--------------------------------------------------------------------------------
/2부/17장/mysmtplib.py:
--------------------------------------------------------------------------------
1 | # -*- coding: cp949 -*-
2 | import smtplib
3 | from email.base64mime import body_encode as encode_base64
4 |
5 | class MySMTP(smtplib.SMTP):
6 | def login(self, user, password):
7 | def encode_cram_md5(challenge, user, password):
8 | challenge = base64.decodestring(challenge)
9 | response = user + " " + hmac.HMAC(password, challenge).hexdigest()
10 | return encode_base64(response)
11 |
12 | def encode_plain(user, password):
13 | s = "\0%s\0%s" % (user, password)
14 | return encode_base64(s.encode('ascii'), eol='')
15 |
16 | AUTH_PLAIN = "PLAIN"
17 | AUTH_CRAM_MD5 = "CRAM-MD5"
18 | AUTH_LOGIN = "LOGIN"
19 |
20 | self.ehlo_or_helo_if_needed()
21 |
22 | if not self.has_extn("auth"):
23 | raise SMTPException("SMTP AUTH extension not supported by server.")
24 |
25 | authlist = self.esmtp_features["auth"].split()
26 | preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
27 |
28 | authmethod = None
29 | for method in preferred_auths:
30 | if method in authlist:
31 | authmethod = method
32 | break
33 |
34 | if authmethod == AUTH_LOGIN:
35 | (code, resp) = self.docmd("AUTH",
36 | "%s %s" % (AUTH_LOGIN, encode_base64(user)))
37 | if code != 334:
38 | raise SMTPAuthenticationError(code, resp)
39 | (code, resp) = self.docmd(encode_base64(password))
40 | elif authmethod == AUTH_PLAIN:
41 | temp_encode_plain = str(encode_plain(user, password))
42 | temp_encode_plain = temp_encode_plain.replace("\n","")
43 | (code, resp) = self.docmd("AUTH",
44 | AUTH_PLAIN + " " + temp_encode_plain)
45 | elif authmethod == AUTH_CRAM_MD5:
46 | (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
47 | if code == 503:
48 | return (code, resp)
49 | (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
50 | elif authmethod is None:
51 | raise SMTPException("No suitable authentication method found.")
52 | if code not in (235, 503):
53 | raise SMTPAuthenticationError(code, resp)
54 | return (code, resp)
--------------------------------------------------------------------------------
/3부/26장/Pi/sds011.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 | import serial
4 | import time
5 | import RPi.GPIO as GPIO # GPIO 임포트
6 |
7 | se = serial.Serial()
8 | se.port = "/dev/serial0" # 포트
9 | se.baudrate = 9600 # 통신속도
10 | bjtNum = 12 #트랜지스터를 제어할 때 사용할 핀번호
11 | GPIO.setmode(GPIO.BCM) #GPIO num으로 사용
12 | GPIO.setup(bjtNum, GPIO.OUT) #GPIO 12번 핀을 출력용으로 선언
13 | isWork = False
14 |
15 | def isWorkingMode():
16 | global isWork
17 | return isWork
18 |
19 | def setSleep():
20 | global isWork
21 | isWork = False
22 | GPIO.output(bjtNum, GPIO.LOW) # 트랜지스터 Base의 전압을 0V으로 설정
23 |
24 | def setWork():
25 | # 220옴 저항 때문에 트랜지스터 Base핀의 전압이 1V가 된다.
26 | global isWork
27 | isWork = True
28 | GPIO.output(bjtNum, GPIO.HIGH)
29 |
30 | def start():
31 | se.open()
32 | se.flushInput()
33 |
34 | def end():
35 | se.close()
36 | GPIO.cleanup() #GPIO를 초기화
37 |
38 | def getParticle():
39 | byte = 0
40 | # Message Header (0xAA)를 만날 때 까지 시리얼 버퍼에서 1바이트씩 읽어 옵니다.
41 | while byte != '\xaa':
42 | byte = se.read(size=1)
43 |
44 | data = se.read(size=9) # 패킷 사이즈 (9바이트)만큼 데이터를 가져옵니다.
45 | print(' '.join(x.encode('hex') for x in data)) # 디버깅을 위해 data를 HEX 형식으로 프린트 합니다.
46 |
47 | # Command ID가 0xC0, Message Tail이 0xAB인지 확인 합니다.
48 | if data[0] == '\xc0' and data[8] == '\xab' :
49 | # ord 함수를 이용해 data를 ascii (int)로 변환합니다.
50 | # DATA 1 부터 DATA 6을 더한 값의 low byte이 check-sum data입니다.
51 | # DATA 1부터 DATA 6 더한 값의 low byte만 사용합니다.
52 | checkSum = (ord(data[1]) + ord(data[2]) + ord(data[3]) + ord(data[4]) + ord(data[5]) +ord(data[6]))%256
53 |
54 | if checkSum == ord(data[7]) :
55 | pm25 = float(ord(data[2])*256 + ord(data[1]))/10
56 | pm10 = float(ord(data[4])*256 + ord(data[3]))/10
57 | return (pm25, pm10)
58 | else :
59 | return None
60 | else :
61 | return None
62 |
63 | if __name__=="__main__":
64 | start()
65 | setWork() # 전원이 SDS011에 공급됩니다.
66 | time.sleep(10)
67 | cnt = 10
68 | while cnt:
69 | particle = getParticle()
70 | if particle :
71 | pm25, pm10 = particle
72 | print ("PM2.5 :" + str(pm25))
73 | print ("PM10 :" + str(pm10))
74 | print ("")
75 | time.sleep(5)
76 | cnt -= 1
77 | setSleep() # SDS011에 전원이 끊어 집니다.
78 | time.sleep(1)
79 | end()
--------------------------------------------------------------------------------
/3부/26장/WebApp/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 | # Copyright 2015 Google Inc.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 |
17 | # [START app]
18 | from flask import redirect, render_template, request, url_for, Flask, current_app
19 | from air import getAirInfoList, saveAirInfoList, clearAirInfoList
20 | import json
21 |
22 | #테스트
23 | app = Flask(__name__)
24 | app.debug = True #debug mode
25 |
26 | latestAirInfo = {'pm25': 0.0, 'pm10': 0.0, 'temp': 0.0, 'hum':0, 'press':0}
27 |
28 | @app.route('/')
29 | def home():
30 | # 최근 7일간의 데이터를 가져와 템플릿 파일이 렌더링 될 때 JS Object 형태로 삽입함.
31 | return render_template("home.html",
32 | jsonAirInfoList = getAirInfoList(7))
33 |
34 | @app.route('/getLatestAirInfo', methods=['GET'])
35 | def getLatestAirInfo():
36 | #현재 서버에 저장되어 있는 미세먼지, 초미세 먼지, 온도, 습도, 기압 정보를 JSON형태로 출력함.
37 | if request.method == 'GET':
38 | return json.dumps(latestAirInfo)
39 | return "error"
40 |
41 | @app.route('/setAirInfo', methods=['GET'])
42 | def setAirInfo():
43 | # 공기 측정 결과를 서버에 저장 - 라즈베리 파이에서 요청
44 | if request.method == 'GET':
45 | data = request.args.to_dict(flat=True)
46 | latestAirInfo['pm25'] = data['pm25']
47 | latestAirInfo['pm10'] = data['pm10']
48 | latestAirInfo['temp'] = data['temp']
49 | latestAirInfo['hum'] = data['hum']
50 | latestAirInfo['press'] = data['press']
51 | return "success"
52 |
53 | @app.route('/saveAirInfo', methods=['GET'])
54 | def saveAirInfo():
55 | # 공기 측정 결과를 Google Cloud Datasotre에 저장
56 | if request.method == 'GET':
57 | data = request.args.to_dict(flat=True)
58 | return str(saveAirInfoList(data))
59 |
60 |
61 | @app.route('/clearAirInfo', methods=['GET'])
62 | def clearAirInfo():
63 | # 모든 데이터를 삭제.
64 | if request.method == 'GET':
65 | clearAirInfoList()
66 |
67 | if __name__ == '__main__':
68 | app.run(host='127.0.0.1', port=8080)
69 | # [END app]
70 |
--------------------------------------------------------------------------------
/3부/25장/teller.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # coding=utf-8
3 |
4 | import sys
5 | import time
6 | import sqlite3
7 | import telepot
8 | from pprint import pprint
9 | from urllib.request import urlopen
10 | from bs4 import BeautifulSoup
11 | import re
12 | from datetime import date, datetime, timedelta
13 | import traceback
14 |
15 | import noti
16 |
17 |
18 | def replyAptData(date_param, user, loc_param='11710'):
19 | print(user, date_param, loc_param)
20 | res_list = noti.getData( loc_param, date_param )
21 | msg = ''
22 | for r in res_list:
23 | print( str(datetime.now()).split('.')[0], r )
24 | if len(r+msg)+1>noti.MAX_MSG_LENGTH:
25 | noti.sendMessage( user, msg )
26 | msg = r+'\n'
27 | else:
28 | msg += r+'\n'
29 | if msg:
30 | noti.sendMessage( user, msg )
31 | else:
32 | noti.sendMessage( user, '%s 기간에 해당하는 데이터가 없습니다.'%date_param )
33 |
34 | def save( user, loc_param ):
35 | conn = sqlite3.connect('users.db')
36 | cursor = conn.cursor()
37 | cursor.execute('CREATE TABLE IF NOT EXISTS users( user TEXT, location TEXT, PRIMARY KEY(user, location) )')
38 | try:
39 | cursor.execute('INSERT INTO users(user, location) VALUES ("%s", "%s")' % (user, loc_param))
40 | except sqlite3.IntegrityError:
41 | noti.sendMessage( user, '이미 해당 정보가 저장되어 있습니다.' )
42 | return
43 | else:
44 | noti.sendMessage( user, '저장되었습니다.' )
45 | conn.commit()
46 |
47 | def check( user ):
48 | conn = sqlite3.connect('users.db')
49 | cursor = conn.cursor()
50 | cursor.execute('CREATE TABLE IF NOT EXISTS users( user TEXT, location TEXT, PRIMARY KEY(user, location) )')
51 | cursor.execute('SELECT * from users WHERE user="%s"' % user)
52 | for data in cursor.fetchall():
53 | row = 'id:' + str(data[0]) + ', location:' + data[1]
54 | noti.sendMessage( user, row )
55 |
56 |
57 | def handle(msg):
58 | content_type, chat_type, chat_id = telepot.glance(msg)
59 | if content_type != 'text':
60 | noti.sendMessage(chat_id, '난 텍스트 이외의 메시지는 처리하지 못해요.')
61 | return
62 |
63 | text = msg['text']
64 | args = text.split(' ')
65 |
66 | if text.startswith('지역') and len(args)>1:
67 | print('try to 지역', args[1])
68 | replyAptData( '201705', chat_id, args[1] )
69 | elif text.startswith('저장') and len(args)>1:
70 | print('try to 저장', args[1])
71 | save( chat_id, args[1] )
72 | elif text.startswith('확인'):
73 | print('try to 확인')
74 | check( chat_id )
75 | else:
76 | noti.sendMessage(chat_id, '모르는 명령어입니다.\n지역 [지역번호], 저장 [지역번호], 확인 중 하나의 명령을 입력하세요.')
77 |
78 |
79 | today = date.today()
80 | current_month = today.strftime('%Y%m')
81 |
82 | print( '[',today,']received token :', noti.TOKEN )
83 |
84 | bot = telepot.Bot(noti.TOKEN)
85 | pprint( bot.getMe() )
86 |
87 | bot.message_loop(handle)
88 |
89 | print('Listening...')
90 |
91 | while 1:
92 | time.sleep(10)
--------------------------------------------------------------------------------
/3부/23장/nba_2016_05.csv:
--------------------------------------------------------------------------------
1 | Date,Start (ET),Visitor/Neutral,PTS,Home/Neutral,PTS,,,Notes
2 | Fri May 1 2015,8:00 pm,Atlanta Hawks,111,Brooklyn Nets,87,Box Score,,
3 | Sat May 2 2015,8:00 pm,San Antonio Spurs,109,Los Angeles Clippers,111,Box Score,,
4 | Sun May 3 2015,1:00 pm,Washington Wizards,104,Atlanta Hawks,98,Box Score,,
5 | Sun May 3 2015,3:30 pm,Memphis Grizzlies,86,Golden State Warriors,101,Box Score,,
6 | Mon May 4 2015,7:00 pm,Chicago Bulls,99,Cleveland Cavaliers,92,Box Score,,
7 | Mon May 4 2015,9:30 pm,Los Angeles Clippers,117,Houston Rockets,101,Box Score,,
8 | Tue May 5 2015,8:00 pm,Washington Wizards,90,Atlanta Hawks,106,Box Score,,
9 | Tue May 5 2015,10:30 pm,Memphis Grizzlies,97,Golden State Warriors,90,Box Score,,
10 | Wed May 6 2015,7:00 pm,Chicago Bulls,91,Cleveland Cavaliers,106,Box Score,,
11 | Wed May 6 2015,9:30 pm,Los Angeles Clippers,109,Houston Rockets,115,Box Score,,
12 | Fri May 8 2015,8:00 pm,Cleveland Cavaliers,96,Chicago Bulls,99,Box Score,,
13 | Fri May 8 2015,10:30 pm,Houston Rockets,99,Los Angeles Clippers,124,Box Score,,
14 | Sat May 9 2015,8:00 pm,Golden State Warriors,89,Memphis Grizzlies,99,Box Score,,
15 | Sat May 9 2015,5:00 pm,Atlanta Hawks,101,Washington Wizards,103,Box Score,,
16 | Sun May 10 2015,3:30 pm,Cleveland Cavaliers,86,Chicago Bulls,84,Box Score,,
17 | Sun May 10 2015,8:30 pm,Houston Rockets,95,Los Angeles Clippers,128,Box Score,,
18 | Mon May 11 2015,9:30 pm,Golden State Warriors,101,Memphis Grizzlies,84,Box Score,,
19 | Mon May 11 2015,7:00 pm,Atlanta Hawks,106,Washington Wizards,101,Box Score,,
20 | Tue May 12 2015,7:00 pm,Chicago Bulls,101,Cleveland Cavaliers,106,Box Score,,
21 | Tue May 12 2015,9:30 pm,Los Angeles Clippers,103,Houston Rockets,124,Box Score,,
22 | Wed May 13 2015,8:00 pm,Washington Wizards,81,Atlanta Hawks,82,Box Score,,
23 | Wed May 13 2015,10:30 pm,Memphis Grizzlies,78,Golden State Warriors,98,Box Score,,
24 | Thu May 14 2015,8:00 pm,Cleveland Cavaliers,94,Chicago Bulls,73,Box Score,,
25 | Thu May 14 2015,10:30 pm,Houston Rockets,119,Los Angeles Clippers,107,Box Score,,
26 | Fri May 15 2015,9:30 pm,Golden State Warriors,108,Memphis Grizzlies,95,Box Score,,
27 | Fri May 15 2015,7:00 pm,Atlanta Hawks,94,Washington Wizards,91,Box Score,,
28 | Sun May 17 2015,3:30 pm,Los Angeles Clippers,100,Houston Rockets,113,Box Score,,
29 | Tue May 19 2015,9:00 pm,Houston Rockets,106,Golden State Warriors,110,Box Score,,
30 | Wed May 20 2015,8:30 pm,Cleveland Cavaliers,97,Atlanta Hawks,89,Box Score,,
31 | Thu May 21 2015,9:00 pm,Houston Rockets,98,Golden State Warriors,99,Box Score,,
32 | Fri May 22 2015,8:30 pm,Cleveland Cavaliers,94,Atlanta Hawks,82,Box Score,,
33 | Sat May 23 2015,9:00 pm,Golden State Warriors,115,Houston Rockets,80,Box Score,,
34 | Sun May 24 2015,8:30 pm,Atlanta Hawks,111,Cleveland Cavaliers,114,Box Score,OT,
35 | Mon May 25 2015,9:00 pm,Golden State Warriors,115,Houston Rockets,128,Box Score,,
36 | Tue May 26 2015,8:30 pm,Atlanta Hawks,88,Cleveland Cavaliers,118,Box Score,,
37 | Wed May 27 2015,9:00 pm,Houston Rockets,90,Golden State Warriors,104,Box Score,,
--------------------------------------------------------------------------------
/3부/25장/noti.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # coding=utf-8
3 |
4 | import sys
5 | import time
6 | import sqlite3
7 | import telepot
8 | from pprint import pprint
9 | from urllib.request import urlopen
10 | from bs4 import BeautifulSoup
11 | import re
12 | from datetime import date, datetime, timedelta
13 | import traceback
14 |
15 | key = '여기에 API KEY를 입력하세요'
16 | TOKEN = '여기에 텔레그램 토큰을 입력하세요'
17 | MAX_MSG_LENGTH = 300
18 | baseurl = 'http://openapi.molit.go.kr:8081/OpenAPI_ToolInstallPackage/service/rest/RTMSOBJSvc/getRTMSDataSvcAptTrade?ServiceKey='+key
19 | bot = telepot.Bot(TOKEN)
20 |
21 | def getData(loc_param, date_param):
22 | res_list = []
23 | url = baseurl+'&LAWD_CD='+loc_param+'&DEAL_YMD='+date_param
24 | #print(url)
25 | res_body = urlopen(url).read()
26 | #print(res_body)
27 | soup = BeautifulSoup(res_body, 'html.parser')
28 | items = soup.findAll('item')
29 | for item in items:
30 | item = re.sub('<.*?>', '|', item.text)
31 | parsed = item.split('|')
32 | try:
33 | row = parsed[3]+'/'+parsed[6]+'/'+parsed[7]+', '+parsed[4]+' '+parsed[5]+', '+parsed[8]+'m², '+parsed[11]+'F, '+parsed[1].strip()+'만원\n'
34 | except IndexError:
35 | row = item.replace('|', ',')
36 |
37 | if row:
38 | res_list.append(row.strip())
39 | return res_list
40 |
41 | def sendMessage(user, msg):
42 | try:
43 | bot.sendMessage(user, msg)
44 | except:
45 | traceback.print_exc(file=sys.stdout)
46 |
47 | def run(date_param, param='11710'):
48 | conn = sqlite3.connect('logs.db')
49 | cursor = conn.cursor()
50 | cursor.execute('CREATE TABLE IF NOT EXISTS logs( user TEXT, log TEXT, PRIMARY KEY(user, log) )')
51 | conn.commit()
52 |
53 | user_cursor = sqlite3.connect('users.db').cursor()
54 | user_cursor.execute('CREATE TABLE IF NOT EXISTS users( user TEXT, location TEXT, PRIMARY KEY(user, location) )')
55 | user_cursor.execute('SELECT * from users')
56 |
57 | for data in user_cursor.fetchall():
58 | user, param = data[0], data[1]
59 | print(user, date_param, param)
60 | res_list = getData( param, date_param )
61 | msg = ''
62 | for r in res_list:
63 | try:
64 | cursor.execute('INSERT INTO logs (user,log) VALUES ("%s", "%s")'%(user,r))
65 | except sqlite3.IntegrityError:
66 | # 이미 해당 데이터가 있다는 것을 의미합니다.
67 | pass
68 | else:
69 | print( str(datetime.now()).split('.')[0], r )
70 | if len(r+msg)+1>MAX_MSG_LENGTH:
71 | sendMessage( user, msg )
72 | msg = r+'\n'
73 | else:
74 | msg += r+'\n'
75 | if msg:
76 | sendMessage( user, msg )
77 | conn.commit()
78 |
79 | if __name__=='__main__':
80 | today = date.today()
81 | current_month = today.strftime('%Y%m')
82 |
83 | print( '[',today,']received token :', TOKEN )
84 |
85 | pprint( bot.getMe() )
86 |
87 | run(current_month)
88 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/minicompat.py:
--------------------------------------------------------------------------------
1 | """Python version compatibility support for minidom."""
2 |
3 | # This module should only be imported using "import *".
4 | #
5 | # The following names are defined:
6 | #
7 | # NodeList -- lightest possible NodeList implementation
8 | #
9 | # EmptyNodeList -- lightest possible NodeList that is guarateed to
10 | # remain empty (immutable)
11 | #
12 | # StringTypes -- tuple of defined string types
13 | #
14 | # defproperty -- function used in conjunction with GetattrMagic;
15 | # using these together is needed to make them work
16 | # as efficiently as possible in both Python 2.2+
17 | # and older versions. For example:
18 | #
19 | # class MyClass(GetattrMagic):
20 | # def _get_myattr(self):
21 | # return something
22 | #
23 | # defproperty(MyClass, "myattr",
24 | # "return some value")
25 | #
26 | # For Python 2.2 and newer, this will construct a
27 | # property object on the class, which avoids
28 | # needing to override __getattr__(). It will only
29 | # work for read-only attributes.
30 | #
31 | # For older versions of Python, inheriting from
32 | # GetattrMagic will use the traditional
33 | # __getattr__() hackery to achieve the same effect,
34 | # but less efficiently.
35 | #
36 | # defproperty() should be used for each version of
37 | # the relevant _get_() function.
38 |
39 | __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
40 |
41 | import xml.dom
42 |
43 | StringTypes = (str,)
44 |
45 |
46 | class NodeList(list):
47 | __slots__ = ()
48 |
49 | def item(self, index):
50 | if 0 <= index < len(self):
51 | return self[index]
52 |
53 | def _get_length(self):
54 | return len(self)
55 |
56 | def _set_length(self, value):
57 | raise xml.dom.NoModificationAllowedErr(
58 | "attempt to modify read-only attribute 'length'")
59 |
60 | length = property(_get_length, _set_length,
61 | doc="The number of nodes in the NodeList.")
62 |
63 | def __getstate__(self):
64 | return list(self)
65 |
66 | def __setstate__(self, state):
67 | self[:] = state
68 |
69 |
70 | class EmptyNodeList(tuple):
71 | __slots__ = ()
72 |
73 | def __add__(self, other):
74 | NL = NodeList()
75 | NL.extend(other)
76 | return NL
77 |
78 | def __radd__(self, other):
79 | NL = NodeList()
80 | NL.extend(other)
81 | return NL
82 |
83 | def item(self, index):
84 | return None
85 |
86 | def _get_length(self):
87 | return 0
88 |
89 | def _set_length(self, value):
90 | raise xml.dom.NoModificationAllowedErr(
91 | "attempt to modify read-only attribute 'length'")
92 |
93 | length = property(_get_length, _set_length,
94 | doc="The number of nodes in the NodeList.")
95 |
96 |
97 | def defproperty(klass, name, doc):
98 | get = getattr(klass, ("_get_" + name))
99 | def set(self, value, name=name):
100 | raise xml.dom.NoModificationAllowedErr(
101 | "attempt to modify read-only attribute " + repr(name))
102 | assert not hasattr(klass, "_set_" + name), \
103 | "expected not to find _set_" + name
104 | prop = property(get, set, doc=doc)
105 | setattr(klass, name, prop)
106 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/domreg.py:
--------------------------------------------------------------------------------
1 | """Registration facilities for DOM. This module should not be used
2 | directly. Instead, the functions getDOMImplementation and
3 | registerDOMImplementation should be imported from xml.dom."""
4 |
5 | from xml.dom.minicompat import * # isinstance, StringTypes
6 |
7 | # This is a list of well-known implementations. Well-known names
8 | # should be published by posting to xml-sig@python.org, and are
9 | # subsequently recorded in this file.
10 |
11 | well_known_implementations = {
12 | 'minidom':'xml.dom.minidom',
13 | '4DOM': 'xml.dom.DOMImplementation',
14 | }
15 |
16 | # DOM implementations not officially registered should register
17 | # themselves with their
18 |
19 | registered = {}
20 |
21 | def registerDOMImplementation(name, factory):
22 | """registerDOMImplementation(name, factory)
23 |
24 | Register the factory function with the name. The factory function
25 | should return an object which implements the DOMImplementation
26 | interface. The factory function can either return the same object,
27 | or a new one (e.g. if that implementation supports some
28 | customization)."""
29 |
30 | registered[name] = factory
31 |
32 | def _good_enough(dom, features):
33 | "_good_enough(dom, features) -> Return 1 if the dom offers the features"
34 | for f,v in features:
35 | if not dom.hasFeature(f,v):
36 | return 0
37 | return 1
38 |
39 | def getDOMImplementation(name = None, features = ()):
40 | """getDOMImplementation(name = None, features = ()) -> DOM implementation.
41 |
42 | Return a suitable DOM implementation. The name is either
43 | well-known, the module name of a DOM implementation, or None. If
44 | it is not None, imports the corresponding module and returns
45 | DOMImplementation object if the import succeeds.
46 |
47 | If name is not given, consider the available implementations to
48 | find one with the required feature set. If no implementation can
49 | be found, raise an ImportError. The features list must be a sequence
50 | of (feature, version) pairs which are passed to hasFeature."""
51 |
52 | import os
53 | creator = None
54 | mod = well_known_implementations.get(name)
55 | if mod:
56 | mod = __import__(mod, {}, {}, ['getDOMImplementation'])
57 | return mod.getDOMImplementation()
58 | elif name:
59 | return registered[name]()
60 | elif "PYTHON_DOM" in os.environ:
61 | return getDOMImplementation(name = os.environ["PYTHON_DOM"])
62 |
63 | # User did not specify a name, try implementations in arbitrary
64 | # order, returning the one that has the required features
65 | if isinstance(features, str):
66 | features = _parse_feature_string(features)
67 | for creator in registered.values():
68 | dom = creator()
69 | if _good_enough(dom, features):
70 | return dom
71 |
72 | for creator in well_known_implementations.keys():
73 | try:
74 | dom = getDOMImplementation(name = creator)
75 | except Exception: # typically ImportError, or AttributeError
76 | continue
77 | if _good_enough(dom, features):
78 | return dom
79 |
80 | raise ImportError("no suitable DOM implementation found")
81 |
82 | def _parse_feature_string(s):
83 | features = []
84 | parts = s.split()
85 | i = 0
86 | length = len(parts)
87 | while i < length:
88 | feature = parts[i]
89 | if feature[0] in "0123456789":
90 | raise ValueError("bad feature name: %r" % (feature,))
91 | i = i + 1
92 | version = None
93 | if i < length:
94 | v = parts[i]
95 | if v[0] in "0123456789":
96 | i = i + 1
97 | version = v
98 | features.append((feature, version))
99 | return tuple(features)
100 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/__init__.py:
--------------------------------------------------------------------------------
1 | """Simple API for XML (SAX) implementation for Python.
2 |
3 | This module provides an implementation of the SAX 2 interface;
4 | information about the Java version of the interface can be found at
5 | http://www.megginson.com/SAX/. The Python version of the interface is
6 | documented at <...>.
7 |
8 | This package contains the following modules:
9 |
10 | handler -- Base classes and constants which define the SAX 2 API for
11 | the 'client-side' of SAX for Python.
12 |
13 | saxutils -- Implementation of the convenience classes commonly used to
14 | work with SAX.
15 |
16 | xmlreader -- Base classes and constants which define the SAX 2 API for
17 | the parsers used with SAX for Python.
18 |
19 | expatreader -- Driver that allows use of the Expat parser with SAX.
20 | """
21 |
22 | from .xmlreader import InputSource
23 | from .handler import ContentHandler, ErrorHandler
24 | from ._exceptions import SAXException, SAXNotRecognizedException, \
25 | SAXParseException, SAXNotSupportedException, \
26 | SAXReaderNotAvailable
27 |
28 |
29 | def parse(source, handler, errorHandler=ErrorHandler()):
30 | parser = make_parser()
31 | parser.setContentHandler(handler)
32 | parser.setErrorHandler(errorHandler)
33 | parser.parse(source)
34 |
35 | def parseString(string, handler, errorHandler=ErrorHandler()):
36 | from io import BytesIO
37 |
38 | if errorHandler is None:
39 | errorHandler = ErrorHandler()
40 | parser = make_parser()
41 | parser.setContentHandler(handler)
42 | parser.setErrorHandler(errorHandler)
43 |
44 | inpsrc = InputSource()
45 | inpsrc.setByteStream(BytesIO(string))
46 | parser.parse(inpsrc)
47 |
48 | # this is the parser list used by the make_parser function if no
49 | # alternatives are given as parameters to the function
50 |
51 | default_parser_list = ["xml.sax.expatreader"]
52 |
53 | # tell modulefinder that importing sax potentially imports expatreader
54 | _false = 0
55 | if _false:
56 | import xml.sax.expatreader
57 |
58 | import os, sys
59 | if "PY_SAX_PARSER" in os.environ:
60 | default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
61 | del os
62 |
63 | _key = "python.xml.sax.parser"
64 | if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
65 | default_parser_list = sys.registry.getProperty(_key).split(",")
66 |
67 |
68 | def make_parser(parser_list = []):
69 | """Creates and returns a SAX parser.
70 |
71 | Creates the first parser it is able to instantiate of the ones
72 | given in the list created by doing parser_list +
73 | default_parser_list. The lists must contain the names of Python
74 | modules containing both a SAX parser and a create_parser function."""
75 |
76 | for parser_name in parser_list + default_parser_list:
77 | try:
78 | return _create_parser(parser_name)
79 | except ImportError as e:
80 | import sys
81 | if parser_name in sys.modules:
82 | # The parser module was found, but importing it
83 | # failed unexpectedly, pass this exception through
84 | raise
85 | except SAXReaderNotAvailable:
86 | # The parser module detected that it won't work properly,
87 | # so try the next one
88 | pass
89 |
90 | raise SAXReaderNotAvailable("No parsers found", None)
91 |
92 | # --- Internal utility methods used by make_parser
93 |
94 | if sys.platform[ : 4] == "java":
95 | def _create_parser(parser_name):
96 | from org.python.core import imp
97 | drv_module = imp.importName(parser_name, 0, globals())
98 | return drv_module.create_parser()
99 |
100 | else:
101 | def _create_parser(parser_name):
102 | drv_module = __import__(parser_name,{},{},['create_parser'])
103 | return drv_module.create_parser()
104 |
105 | del sys
106 |
--------------------------------------------------------------------------------
/3부/23장/nba_2015_2016_standing.csv:
--------------------------------------------------------------------------------
1 | ,,,Place,Place,Conference,Conference,Division,Division,Division,Division,Division,Division,All-Star,All-Star,Margin,Margin,Month,Month,Month,Month,Month,Month,Month
2 | Rk,Team,Overall,Home,Road,E,W,A,C,SE,NW,P,SW,Pre,Post,≤3,≥10,Oct,Nov,Dec,Jan,Feb,Mar,Apr
3 | 1,Golden State Warriors,73-9,39-2,34-7,27-3,46-6,9-1,8-2,10-0,15-3,15-1,16-2,48-4,25-5,7-2,44-5,3-0,16-0,11-2,14-2,9-1,15-2,5-2
4 | 2,San Antonio Spurs,67-15,40-1,27-14,24-6,43-9,9-1,7-3,8-2,15-3,14-4,14-2,45-8,22-7,4-4,44-6,1-1,13-3,14-2,11-2,11-1,13-3,4-3
5 | 3,Cleveland Cavaliers,57-25,33-8,24-17,35-17,22-8,14-4,8-8,13-5,8-2,8-2,6-4,38-14,19-11,4-7,32-8,2-1,11-3,8-5,13-3,8-5,11-5,4-3
6 | 4,Toronto Raptors,56-26,32-9,24-17,39-13,17-13,14-2,11-7,14-4,5-5,5-5,7-3,35-17,21-9,6-6,28-10,2-0,9-7,9-6,12-2,7-4,11-5,6-2
7 | 5,Oklahoma City Thunder,55-27,32-9,23-18,18-12,37-15,6-4,4-6,8-2,13-3,12-6,12-6,40-14,15-13,8-6,33-5,2-0,9-7,12-3,13-3,6-5,11-5,2-4
8 | 6,Los Angeles Clippers,53-29,29-12,24-17,22-8,31-21,7-3,6-4,9-1,11-7,9-7,11-7,35-18,18-11,8-4,28-15,3-0,7-8,11-5,11-3,7-4,8-8,6-1
9 | 7,Atlanta Hawks,48-34,27-14,21-20,29-23,19-11,11-7,10-8,8-8,6-4,5-5,8-2,31-24,17-10,5-7,29-17,2-1,10-7,9-5,6-9,6-5,12-4,3-3
10 | 8,Boston Celtics,48-34,28-13,20-21,31-21,17-13,10-6,9-9,12-6,6-4,7-3,4-6,32-23,16-11,5-7,26-13,1-1,9-7,8-6,9-8,9-3,7-7,5-2
11 | 9,Charlotte Hornets,48-34,30-11,18-23,33-19,15-15,13-5,12-6,8-8,4-6,5-5,6-4,27-26,21-8,6-5,25-17,0-2,10-5,7-7,6-11,7-3,13-3,5-3
12 | 10,Miami Heat,48-34,28-13,20-21,31-21,17-13,10-8,11-7,10-6,6-4,5-5,6-4,29-24,19-10,7-5,24-20,1-1,9-5,8-7,9-8,6-5,10-5,5-3
13 | 11,Indiana Pacers,45-37,26-15,19-22,30-22,15-15,13-5,8-8,9-9,4-6,5-5,6-4,28-25,17-12,9-9,21-13,0-3,11-2,7-9,7-8,6-7,8-7,6-1
14 | 12,Detroit Pistons,44-38,26-15,18-23,29-23,15-15,11-7,10-6,8-10,7-3,5-5,3-7,27-27,17-11,6-5,21-20,3-0,6-9,9-6,7-8,6-6,9-6,4-3
15 | 13,Portland Trail Blazers,44-38,28-13,16-25,15-15,29-23,5-5,5-5,5-5,11-5,10-8,8-10,27-27,17-11,2-7,23-21,1-2,6-9,7-10,9-5,9-2,8-8,4-2
16 | 14,Dallas Mavericks,42-40,23-18,19-22,15-15,27-25,8-2,4-6,3-7,11-7,9-9,7-9,29-26,13-14,6-11,19-18,1-1,9-7,9-5,9-9,4-6,5-10,5-2
17 | 15,Memphis Grizzlies,42-40,26-15,16-25,17-13,25-27,7-3,7-3,3-7,9-9,9-9,7-9,31-22,11-18,8-3,15-23,2-1,8-7,8-8,10-4,7-4,6-10,1-6
18 | 16,Chicago Bulls,42-40,26-15,16-25,25-27,17-13,12-6,10-6,3-15,5-5,6-4,6-4,27-25,15-15,10-4,14-19,2-1,8-4,8-7,8-8,4-8,8-9,4-3
19 | 17,Houston Rockets,41-41,23-18,18-23,13-17,28-24,6-4,3-7,4-6,9-9,11-7,8-8,27-28,14-13,5-8,14-18,0-2,7-9,9-7,9-7,4-6,8-8,4-2
20 | 18,Washington Wizards,41-41,22-19,19-22,30-22,11-19,9-9,11-7,10-6,2-8,4-6,5-5,23-28,18-13,7-6,22-22,2-1,4-7,8-8,7-8,8-6,7-9,5-2
21 | 19,Utah Jazz,40-42,24-17,16-25,16-14,24-28,6-4,6-4,4-6,8-8,9-9,7-11,26-26,14-16,6-8,23-13,2-1,6-7,6-9,7-8,7-6,9-7,3-4
22 | 20,Orlando Magic,35-47,23-18,12-29,21-31,14-16,12-6,5-13,4-12,7-3,2-8,5-5,23-29,12-18,6-10,12-20,0-2,9-6,10-5,2-12,5-7,6-11,3-4
23 | 21,Denver Nuggets,33-49,18-23,15-26,15-15,18-34,6-4,5-5,4-6,4-12,7-11,7-11,22-32,11-17,10-5,11-21,1-1,5-11,6-9,6-9,5-7,9-8,1-4
24 | 22,Milwaukee Bucks,33-49,23-18,10-31,21-31,12-18,9-9,4-12,8-10,4-6,4-6,4-6,22-32,11-17,6-4,12-27,0-2,7-9,6-10,7-8,5-6,6-9,2-5
25 | 23,Sacramento Kings,33-49,18-23,15-26,14-16,19-33,6-4,5-5,3-7,7-11,8-8,4-14,22-31,11-18,8-4,11-19,1-2,6-10,5-8,8-7,4-7,6-11,3-4
26 | 24,New York Knicks,32-50,18-23,14-27,21-31,11-19,8-8,7-11,6-12,5-5,4-6,2-8,23-32,9-18,4-7,14-23,2-1,6-9,7-8,8-9,2-9,5-10,2-4
27 | 25,New Orleans Pelicans,30-52,21-20,9-32,10-20,20-32,4-6,4-6,2-8,8-10,8-10,4-12,20-33,10-19,5-5,11-23,0-3,4-10,6-9,8-6,5-7,5-12,2-5
28 | 26,Minnesota Timberwolves,29-53,14-27,15-26,11-19,18-34,5-5,2-8,4-6,4-12,10-8,4-14,17-37,12-16,7-7,9-23,2-0,6-9,4-12,2-14,5-6,6-9,4-3
29 | 27,Phoenix Suns,23-59,14-27,9-32,6-24,17-35,1-9,1-9,4-6,6-12,6-10,5-13,14-40,9-19,4-5,10-32,2-1,6-8,4-14,2-12,1-9,5-11,3-4
30 | 28,Brooklyn Nets,21-61,14-27,7-34,12-40,9-21,6-10,4-14,2-16,4-6,3-7,2-8,14-40,7-21,7-4,6-34,0-3,4-10,5-10,3-13,5-7,4-11,0-7
31 | 29,Los Angeles Lakers,17-65,12-29,5-36,9-21,8-44,4-6,2-8,3-7,3-15,2-14,3-15,11-44,6-21,2-7,4-44,0-2,2-12,4-13,3-14,2-8,5-10,1-6
32 | 30,Philadelphia 76ers,10-72,7-34,3-38,3-49,7-23,2-14,0-18,1-17,2-8,4-6,1-9,8-45,2-27,0-6,5-41,0-2,0-16,3-13,4-10,1-11,1-14,1-6
--------------------------------------------------------------------------------
/3부/23장/nba_2014_2015_standing.csv:
--------------------------------------------------------------------------------
1 | ,,,Place,Place,Conference,Conference,Division,Division,Division,Division,Division,Division,All-Star,All-Star,Margin,Margin,Month,Month,Month,Month,Month,Month,Month
2 | Rk,Team,Overall,Home,Road,E,W,A,C,SE,NW,P,SW,Pre,Post,≤3,≥10,Oct,Nov,Dec,Jan,Feb,Mar,Apr
3 | 1,Golden State Warriors,67-15,39-2,28-13,25-5,42-10,9-1,7-3,9-1,15-3,13-3,14-4,42-9,25-6,5-3,45-9,1-0,13-2,11-3,12-3,8-3,16-2,6-2
4 | 2,Atlanta Hawks,60-22,35-6,25-16,38-14,22-8,12-6,14-4,12-4,8-2,8-2,6-4,43-11,17-11,6-4,30-10,0-1,9-5,14-2,17-0,7-4,9-7,4-3
5 | 3,Houston Rockets,56-26,30-11,26-15,23-7,33-19,9-1,8-2,6-4,15-3,10-8,8-8,36-17,20-9,8-4,31-14,2-0,11-4,9-5,11-6,7-3,10-6,6-2
6 | 4,Los Angeles Clippers,56-26,30-11,26-15,19-11,37-15,7-3,6-4,6-4,15-3,12-4,10-8,35-19,21-7,3-5,33-9,2-0,9-5,11-6,11-4,5-6,11-5,7-0
7 | 5,Memphis Grizzlies,55-27,31-10,24-17,20-10,35-17,8-2,5-5,7-3,13-5,13-5,9-7,39-14,16-13,9-3,26-13,2-0,13-2,8-6,12-4,7-4,9-8,4-3
8 | 6,San Antonio Spurs,55-27,33-8,22-19,23-7,32-20,7-3,7-3,9-1,12-6,12-6,8-8,34-19,21-8,6-7,34-7,1-1,11-3,8-10,10-4,6-5,12-3,7-1
9 | 7,Cleveland Cavaliers,53-29,31-10,22-19,35-17,18-12,12-6,11-5,12-6,6-4,7-3,5-5,33-22,20-7,6-4,30-14,1-1,7-6,10-7,11-6,8-3,11-4,5-2
10 | 8,Portland Trail Blazers,51-31,32-9,19-22,20-10,31-21,8-2,7-3,5-5,11-5,10-8,10-8,36-17,15-14,5-1,30-11,1-1,12-3,13-3,6-9,6-3,10-6,3-6
11 | 9,Chicago Bulls,50-32,27-14,23-18,33-19,17-13,16-2,8-8,9-9,6-4,5-5,6-4,34-20,16-12,7-2,20-16,1-1,10-5,11-4,8-9,7-3,8-7,5-3
12 | 10,Dallas Mavericks,50-32,27-14,23-18,21-9,29-23,9-1,5-5,7-3,13-5,9-9,7-9,36-19,14-13,5-4,22-18,1-1,12-4,10-5,9-7,7-5,6-7,5-3
13 | 11,Toronto Raptors,49-33,27-14,22-19,33-19,16-14,11-5,8-10,14-4,7-3,6-4,3-7,36-17,13-16,6-5,29-11,1-0,12-4,11-4,9-7,4-7,7-8,5-3
14 | 12,Washington Wizards,46-36,29-12,17-24,30-22,16-14,11-7,9-9,10-6,6-4,4-6,6-4,33-21,13-15,9-4,21-14,1-1,9-4,12-4,9-8,3-9,7-7,5-3
15 | 13,New Orleans Pelicans,45-37,28-13,17-24,16-14,29-23,6-4,5-5,5-5,10-8,11-7,8-8,27-26,18-11,10-4,19-14,1-0,6-8,9-8,9-6,6-5,8-7,6-3
16 | 14,Oklahoma City Thunder,45-37,29-12,16-25,20-10,25-27,6-4,5-5,9-1,10-6,11-7,4-14,28-25,17-12,4-8,26-14,0-2,5-10,11-5,7-7,9-3,10-5,3-5
17 | 15,Milwaukee Bucks,41-41,23-18,18-23,30-22,11-19,14-4,7-9,9-9,5-5,5-5,1-9,30-23,11-18,5-9,17-16,1-1,9-7,7-8,8-6,7-5,4-11,5-3
18 | 16,Boston Celtics,40-42,21-20,19-22,28-24,12-18,12-4,9-9,7-11,6-4,3-7,3-7,20-31,20-11,8-6,17-14,1-0,3-10,7-8,5-11,7-4,10-8,7-1
19 | 17,Phoenix Suns,39-43,22-19,17-24,18-12,21-31,8-2,6-4,4-6,9-9,6-10,6-12,29-25,10-18,4-12,14-19,2-0,8-8,8-8,10-5,3-8,7-8,1-6
20 | 18,Brooklyn Nets,38-44,19-22,19-22,24-28,14-16,10-6,7-11,7-11,6-4,6-4,2-8,21-31,17-13,10-2,15-23,0-1,6-8,9-7,3-12,6-5,9-7,5-4
21 | 19,Indiana Pacers,38-44,23-18,15-26,28-24,10-20,9-9,8-8,11-7,4-6,2-8,4-6,21-33,17-11,4-12,19-20,1-1,6-9,5-11,5-11,7-2,8-8,6-2
22 | 20,Utah Jazz,38-44,21-20,17-24,15-15,23-29,6-4,6-4,3-7,9-7,7-11,7-11,19-34,19-10,5-10,18-19,0-2,5-10,6-9,6-9,6-5,10-6,5-3
23 | 21,Miami Heat,37-45,20-21,17-24,25-27,12-18,14-4,5-13,6-10,4-6,7-3,1-9,22-30,15-15,4-10,15-28,1-0,8-7,5-12,6-7,5-7,9-7,3-5
24 | 22,Charlotte Hornets,33-49,19-22,14-27,25-27,8-22,10-8,7-11,8-8,5-5,2-8,1-9,22-30,11-19,6-8,13-22,1-0,3-14,6-9,10-4,3-6,8-9,2-7
25 | 23,Detroit Pistons,32-50,18-23,14-27,23-29,9-21,8-10,6-10,9-9,2-8,3-7,4-6,21-33,11-17,7-6,19-25,0-2,3-12,5-9,10-7,5-6,6-9,3-5
26 | 24,Denver Nuggets,30-52,19-22,11-30,11-19,19-33,1-9,6-4,4-6,6-10,8-10,5-13,20-33,10-19,4-4,17-28,1-0,7-8,5-11,6-10,1-9,8-8,2-6
27 | 25,Sacramento Kings,29-53,18-23,11-30,11-19,18-34,4-6,4-6,3-7,8-10,7-9,3-15,18-34,11-19,6-4,12-28,1-1,8-7,4-11,4-10,3-7,6-11,3-6
28 | 26,Orlando Magic,25-57,13-28,12-29,15-37,10-20,6-12,5-13,4-12,4-6,4-6,2-8,17-39,8-18,6-8,4-27,0-2,7-10,6-10,2-13,4-6,3-11,3-5
29 | 27,Los Angeles Lakers,21-61,12-29,9-32,12-18,9-43,4-6,5-5,3-7,5-13,2-14,2-16,13-40,8-21,6-6,5-30,0-3,4-10,6-9,3-12,3-7,4-12,1-8
30 | 28,Philadelphia 76ers,18-64,12-29,6-35,12-40,6-24,2-14,5-13,5-13,4-6,1-9,1-9,12-41,6-23,3-7,5-41,0-2,0-14,4-10,6-12,3-7,5-12,0-7
31 | 29,New York Knicks,17-65,10-31,7-34,11-41,6-24,5-11,2-16,4-14,2-8,2-8,2-8,10-43,7-22,2-7,3-39,1-1,3-13,1-15,4-9,3-8,2-14,3-5
32 | 30,Minnesota Timberwolves,16-66,9-32,7-34,9-21,7-45,4-6,3-7,2-8,4-12,2-16,1-17,11-42,5-24,6-6,4-40,1-1,3-10,1-14,3-14,5-6,3-13,0-8
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/dom/__init__.py:
--------------------------------------------------------------------------------
1 | """W3C Document Object Model implementation for Python.
2 |
3 | The Python mapping of the Document Object Model is documented in the
4 | Python Library Reference in the section on the xml.dom package.
5 |
6 | This package contains the following modules:
7 |
8 | minidom -- A simple implementation of the Level 1 DOM with namespace
9 | support added (based on the Level 2 specification) and other
10 | minor Level 2 functionality.
11 |
12 | pulldom -- DOM builder supporting on-demand tree-building for selected
13 | subtrees of the document.
14 |
15 | """
16 |
17 |
18 | class Node:
19 | """Class giving the NodeType constants."""
20 |
21 | # DOM implementations may use this as a base class for their own
22 | # Node implementations. If they don't, the constants defined here
23 | # should still be used as the canonical definitions as they match
24 | # the values given in the W3C recommendation. Client code can
25 | # safely refer to these values in all tests of Node.nodeType
26 | # values.
27 |
28 | ELEMENT_NODE = 1
29 | ATTRIBUTE_NODE = 2
30 | TEXT_NODE = 3
31 | CDATA_SECTION_NODE = 4
32 | ENTITY_REFERENCE_NODE = 5
33 | ENTITY_NODE = 6
34 | PROCESSING_INSTRUCTION_NODE = 7
35 | COMMENT_NODE = 8
36 | DOCUMENT_NODE = 9
37 | DOCUMENT_TYPE_NODE = 10
38 | DOCUMENT_FRAGMENT_NODE = 11
39 | NOTATION_NODE = 12
40 |
41 |
42 | #ExceptionCode
43 | INDEX_SIZE_ERR = 1
44 | DOMSTRING_SIZE_ERR = 2
45 | HIERARCHY_REQUEST_ERR = 3
46 | WRONG_DOCUMENT_ERR = 4
47 | INVALID_CHARACTER_ERR = 5
48 | NO_DATA_ALLOWED_ERR = 6
49 | NO_MODIFICATION_ALLOWED_ERR = 7
50 | NOT_FOUND_ERR = 8
51 | NOT_SUPPORTED_ERR = 9
52 | INUSE_ATTRIBUTE_ERR = 10
53 | INVALID_STATE_ERR = 11
54 | SYNTAX_ERR = 12
55 | INVALID_MODIFICATION_ERR = 13
56 | NAMESPACE_ERR = 14
57 | INVALID_ACCESS_ERR = 15
58 | VALIDATION_ERR = 16
59 |
60 |
61 | class DOMException(Exception):
62 | """Abstract base class for DOM exceptions.
63 | Exceptions with specific codes are specializations of this class."""
64 |
65 | def __init__(self, *args, **kw):
66 | if self.__class__ is DOMException:
67 | raise RuntimeError(
68 | "DOMException should not be instantiated directly")
69 | Exception.__init__(self, *args, **kw)
70 |
71 | def _get_code(self):
72 | return self.code
73 |
74 |
75 | class IndexSizeErr(DOMException):
76 | code = INDEX_SIZE_ERR
77 |
78 | class DomstringSizeErr(DOMException):
79 | code = DOMSTRING_SIZE_ERR
80 |
81 | class HierarchyRequestErr(DOMException):
82 | code = HIERARCHY_REQUEST_ERR
83 |
84 | class WrongDocumentErr(DOMException):
85 | code = WRONG_DOCUMENT_ERR
86 |
87 | class InvalidCharacterErr(DOMException):
88 | code = INVALID_CHARACTER_ERR
89 |
90 | class NoDataAllowedErr(DOMException):
91 | code = NO_DATA_ALLOWED_ERR
92 |
93 | class NoModificationAllowedErr(DOMException):
94 | code = NO_MODIFICATION_ALLOWED_ERR
95 |
96 | class NotFoundErr(DOMException):
97 | code = NOT_FOUND_ERR
98 |
99 | class NotSupportedErr(DOMException):
100 | code = NOT_SUPPORTED_ERR
101 |
102 | class InuseAttributeErr(DOMException):
103 | code = INUSE_ATTRIBUTE_ERR
104 |
105 | class InvalidStateErr(DOMException):
106 | code = INVALID_STATE_ERR
107 |
108 | class SyntaxErr(DOMException):
109 | code = SYNTAX_ERR
110 |
111 | class InvalidModificationErr(DOMException):
112 | code = INVALID_MODIFICATION_ERR
113 |
114 | class NamespaceErr(DOMException):
115 | code = NAMESPACE_ERR
116 |
117 | class InvalidAccessErr(DOMException):
118 | code = INVALID_ACCESS_ERR
119 |
120 | class ValidationErr(DOMException):
121 | code = VALIDATION_ERR
122 |
123 | class UserDataHandler:
124 | """Class giving the operation constants for UserDataHandler.handle()."""
125 |
126 | # Based on DOM Level 3 (WD 9 April 2002)
127 |
128 | NODE_CLONED = 1
129 | NODE_IMPORTED = 2
130 | NODE_DELETED = 3
131 | NODE_RENAMED = 4
132 |
133 | XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
134 | XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/"
135 | XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
136 | EMPTY_NAMESPACE = None
137 | EMPTY_PREFIX = None
138 |
139 | from .domreg import getDOMImplementation, registerDOMImplementation
140 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/book.py:
--------------------------------------------------------------------------------
1 | from xml.dom.minidom import parse, parseString
2 | from xml.etree import ElementTree
3 |
4 | ##### global
5 | loopFlag = 1
6 | xmlFD = -1
7 | BooksDoc = None
8 |
9 | #### Menu implementation
10 | def printMenu():
11 | print("\nWelcome! Book Manager Program (xml version)")
12 | print("========Menu==========")
13 | print("Load xml: l")
14 | print("Print dom to xml: p")
15 | print("Quit program: q")
16 | print("print Book list: b")
17 | print("Add new book: a")
18 | print("sEarch Book Title: e")
19 | print("==================")
20 |
21 | def launcherFunction(menu):
22 | global BooksDoc
23 | if menu == 'l':
24 | BooksDoc = LoadXMLFromFile()
25 | elif menu == 'q':
26 | QuitBookMgr()
27 | elif menu == 'p':
28 | PrintDOMtoXML()
29 | elif menu == 'b':
30 | PrintBookList(["title",])
31 | elif menu == 'a':
32 | AddBook()
33 | elif menu == 'e':
34 | printBookList(SearchBookTitle())
35 | else:
36 | print ("error : unknow menu key")
37 |
38 | #### xml function implementation
39 | def LoadXMLFromFile():
40 | #fileName = str(input ("please input file name to load :"))
41 | fileName = "C:\\xml\\book.xml"
42 | global xmlFD
43 | try:
44 | xmlFD = open(fileName)
45 | except IOError:
46 | print ("invalid file name or path")
47 | else:
48 | try:
49 | dom = parse(xmlFD)
50 | except Exception:
51 | print ("loading fail!!!")
52 | else:
53 | print ("XML Document loading complete")
54 | return dom
55 | return None
56 |
57 | def BooksFree():
58 | if checkDocument():
59 | BooksDoc.unlink()
60 |
61 | def QuitBookMgr():
62 | global loopFlag
63 | loopFlag = 0
64 | BooksFree()
65 |
66 | def PrintDOMtoXML():
67 | if checkDocument():
68 | print(BooksDoc.toxml())
69 |
70 | def PrintBookList(tags):
71 | global BooksDoc
72 | if not checkDocument():
73 | return None
74 |
75 | booklist = BooksDoc.childNodes
76 | book = booklist[0].childNodes
77 | for item in book:
78 | if item.nodeName == "book":
79 | subitems = item.childNodes
80 | for atom in subitems:
81 | if atom.nodeName in tags:
82 | print("title=",atom.firstChild.nodeValue)
83 |
84 | def AddBook():
85 | global BooksDoc
86 | if not checkDocument() :
87 | return None
88 |
89 | ISBN = str(input ('insert ISBN :'))
90 | title = str(input ('insert Title :'))
91 | #create Book element
92 | newBook = BooksDoc.createElement('book')
93 | newBook.setAttribute('ISBN',ISBN)
94 | #create Title Element
95 | titleEle = BooksDoc.createElement('title')
96 | #create title text element
97 | titleNode = BooksDoc.createTextNode(title)
98 | print(type(titleNode))
99 | # connect to title node
100 | try:
101 | titleEle.appendChild(titleNode)
102 | except Exception:
103 | print ("append child fail- please,check the parent element & node!!!")
104 | return None
105 | else:
106 | titleEle.appendChild(titleNode)
107 |
108 | # connect to book node
109 | try:
110 | newBook.appendChild(titleEle)
111 | booklist = BooksDoc.firstChild
112 | except Exception:
113 | print ("append child fail- please,check the parent element & node!!!")
114 | return None
115 | else:
116 | if booklist != None:
117 | booklist.appendChild(newBook)
118 |
119 | def SearchBookTitle():
120 | global BooksDoc
121 | retlist = []
122 | if not checkDocument():
123 | return None
124 |
125 | keyword = str(input ('input keyword to search :'))
126 |
127 | try:
128 | tree = ElementTree.fromstring(str(BooksDoc.toxml()))
129 | except Exception:
130 | print ("Element Tree parsing Error : maybe the xml document is not corrected.")
131 | return None
132 |
133 | #get Book Element
134 | bookElements = tree.getiterator("book") # return list type
135 | for item in bookElements:
136 | strTitle = item.find("title")
137 | if (strTitle.text.find(keyword) >=0 ):
138 | retlist.append((item.attrib["ISBN"], strTitle.text))
139 |
140 | return retlist
141 |
142 | def printBookList(blist):
143 | for res in blist:
144 | print (res)
145 |
146 | def checkDocument():
147 | global BooksDoc
148 | if BooksDoc == None:
149 | print("Error : Document is empty")
150 | return False
151 | return True
152 |
153 |
154 | ##### run #####
155 | while(loopFlag > 0):
156 | printMenu()
157 | menuKey = str(input ('select menu :'))
158 | launcherFunction(menuKey)
159 | else:
160 | print ("Thank you! Good Bye")
161 |
--------------------------------------------------------------------------------
/2부/19장/setup configurate/book.py:
--------------------------------------------------------------------------------
1 | from xml.dom.minidom import parse, parseString
2 | from xml.etree import ElementTree
3 |
4 | ##### global
5 | loopFlag = 1
6 | xmlFD = -1
7 | BooksDoc = None
8 |
9 | #### Menu implementation
10 | def printMenu():
11 | print("\nWelcome! Book Manager Program (xml version)")
12 | print("========Menu==========")
13 | print("Load xml: l")
14 | print("Print dom to xml: p")
15 | print("Quit program: q")
16 | print("print Book list: b")
17 | print("Add new book: a")
18 | print("sEarch Book Title: e")
19 | print("==================")
20 |
21 | def launcherFunction(menu):
22 | global BooksDoc
23 | if menu == 'l':
24 | BooksDoc = LoadXMLFromFile()
25 | elif menu == 'q':
26 | QuitBookMgr()
27 | elif menu == 'p':
28 | PrintDOMtoXML()
29 | elif menu == 'b':
30 | PrintBookList(["title",])
31 | elif menu == 'a':
32 | AddBook()
33 | elif menu == 'e':
34 | printBookList(SearchBookTitle())
35 | else:
36 | print ("error : unknow menu key")
37 |
38 | #### xml function implementation
39 | def LoadXMLFromFile():
40 | #fileName = str(input ("please input file name to load :"))
41 | fileName = "C:\\xml\\book.xml"
42 | global xmlFD
43 | try:
44 | xmlFD = open(fileName)
45 | except IOError:
46 | print ("invalid file name or path")
47 | else:
48 | try:
49 | dom = parse(xmlFD)
50 | except Exception:
51 | print ("loading fail!!!")
52 | else:
53 | print ("XML Document loading complete")
54 | return dom
55 | return None
56 |
57 | def BooksFree():
58 | if checkDocument():
59 | BooksDoc.unlink()
60 |
61 | def QuitBookMgr():
62 | global loopFlag
63 | loopFlag = 0
64 | BooksFree()
65 |
66 | def PrintDOMtoXML():
67 | if checkDocument():
68 | print(BooksDoc.toxml())
69 |
70 | def PrintBookList(tags):
71 | global BooksDoc
72 | if not checkDocument():
73 | return None
74 |
75 | booklist = BooksDoc.childNodes
76 | book = booklist[0].childNodes
77 | for item in book:
78 | if item.nodeName == "book":
79 | subitems = item.childNodes
80 | for atom in subitems:
81 | if atom.nodeName in tags:
82 | print("title=",atom.firstChild.nodeValue)
83 |
84 | def AddBook():
85 | global BooksDoc
86 | if not checkDocument() :
87 | return None
88 |
89 | ISBN = str(input ('insert ISBN :'))
90 | title = str(input ('insert Title :'))
91 | #create Book element
92 | newBook = BooksDoc.createElement('book')
93 | newBook.setAttribute('ISBN',ISBN)
94 | #create Title Element
95 | titleEle = BooksDoc.createElement('title')
96 | #create title text element
97 | titleNode = BooksDoc.createTextNode(title)
98 | print(type(titleNode))
99 | # connect to title node
100 | try:
101 | titleEle.appendChild(titleNode)
102 | except Exception:
103 | print ("append child fail- please,check the parent element & node!!!")
104 | return None
105 | else:
106 | titleEle.appendChild(titleNode)
107 |
108 | # connect to book node
109 | try:
110 | newBook.appendChild(titleEle)
111 | booklist = BooksDoc.firstChild
112 | except Exception:
113 | print ("append child fail- please,check the parent element & node!!!")
114 | return None
115 | else:
116 | if booklist != None:
117 | booklist.appendChild(newBook)
118 |
119 | def SearchBookTitle():
120 | global BooksDoc
121 | retlist = []
122 | if not checkDocument():
123 | return None
124 |
125 | keyword = str(input ('input keyword to search :'))
126 |
127 | try:
128 | tree = ElementTree.fromstring(str(BooksDoc.toxml()))
129 | except Exception:
130 | print ("Element Tree parsing Error : maybe the xml document is not corrected.")
131 | return None
132 |
133 | #get Book Element
134 | bookElements = tree.getiterator("book") # return list type
135 | for item in bookElements:
136 | strTitle = item.find("title")
137 | if (strTitle.text.find(keyword) >=0 ):
138 | retlist.append((item.attrib["ISBN"], strTitle.text))
139 |
140 | return retlist
141 |
142 | def printBookList(blist):
143 | for res in blist:
144 | print (res)
145 |
146 | def checkDocument():
147 | global BooksDoc
148 | if BooksDoc == None:
149 | print("Error : Document is empty")
150 | return False
151 | return True
152 |
153 |
154 | ##### run #####
155 | while(loopFlag > 0):
156 | printMenu()
157 | menuKey = str(input ('select menu :'))
158 | launcherFunction(menuKey)
159 | else:
160 | print ("Thank you! Good Bye")
161 |
--------------------------------------------------------------------------------
/3부/26장/Pi/air.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | import time
5 | import bme280
6 | import sds011 # 미세먼지 측정 모듈 임포트
7 |
8 | import Adafruit_GPIO.SPI as SPI
9 | import Adafruit_SSD1306
10 |
11 | # Python Image Library를 임포트 합니다.
12 | from PIL import Image
13 | from PIL import ImageDraw
14 | from PIL import ImageFont
15 |
16 | import urllib
17 |
18 | # 라즈베리 파이와 연결된 BCM 핀번호를 선언합니다.
19 | RST = 24
20 | DC = 23
21 | SPI_PORT = 0
22 | SPI_DEVICE = 0
23 | BtnPinNum = 6 # 버튼 입력 핀번호
24 |
25 | # SPI를 사용하는 128x64 해상도 디스플레이를 선언합니다.
26 | disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
27 |
28 | # 라이브러리를 초기화
29 | disp.begin()
30 |
31 | # 디스플레이를 클리어 해줍니다.
32 | disp.clear()
33 | disp.display()
34 |
35 | sds011.start()
36 |
37 | width = disp.width
38 | height = disp.height
39 | image = Image.new('1', (width, height))
40 |
41 | draw = ImageDraw.Draw(image)
42 |
43 | mode = 0 # 0: auto, 1: 온도,습도,대기압, 2: 온도, 3: 습도, 4 대기압, 5:미세먼지, 6: 초미세먼지, 7: 화면 꺼짐
44 | displayMode = 0 # 1: 온도,습도,대기압, 2: 온도, 3: 습도, 4 대기압, 5:미세먼지, 6: 초미세먼지
45 | firstFlag = True
46 |
47 | meanInterval = 60 # 60초 마다 온도, 습도, 대기압 측정
48 | particleMeanInterval = 9 # 온,습도 대기압 측정 9번 하면 1번 미세 먼지 측정을 한다.
49 | displayInterval = 5 # 5초 마다 화면 갱신을 한다.
50 | saveAirInfoInterval = 10 # 180분 마다 한번 측정정보를 Google Cloud Datastore에 저장한다.
51 | sendAirInfoInterval = 3 # 3분 마다 측정정보를 서버에 보낸다.
52 |
53 | airInfo = {'temp': 0.0, 'hum': 0.0, 'press':0.0, 'pm25':0.0, 'pm10': 0.0} # 측정 데이터 저장소
54 |
55 | try:
56 |
57 | font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 15)
58 | hanFont = ImageFont.truetype('/usr/share/fonts/truetype/unfonts-core/UnDinaru.ttf', 12)
59 | prevTimeStamp = time.time() # 이전 측정 timestamp 값
60 | particleCnt = 0
61 | sendCountdown = 0
62 | saveCountdown = 0
63 | while True:
64 | if ((time.time() - prevTimeStamp > meanInterval) or firstFlag) :
65 | prevTimeStamp = time.time()
66 | # 온도 습도 대기압 정보를 읽어온다.
67 | airInfo['temp'], airInfo['press'], airInfo['hum'] = bme280.readBME280All()
68 | airInfo['temp'] = round(airInfo['temp'],1)
69 | airInfo['press'] = round(airInfo['press'],1)
70 | airInfo['hum'] = round(airInfo['hum'],1)
71 |
72 | if (particleCnt > particleMeanInterval) and sds011.isWorkingMode() :
73 | particle = sds011.getParticle()
74 | if particle :
75 | airInfo['pm25'], airInfo['pm10'] = particle
76 | airInfo['pm25'] = round(airInfo['pm25'],1)
77 | airInfo['pm10'] = round(airInfo['pm10'],1)
78 |
79 | particleCnt = 0
80 | sds011.setSleep()
81 | time.sleep(2)
82 | elif particleCnt == particleMeanInterval :
83 | sds011.setWork() # sds011을 동작시킨다. 다음 인터벌(60초) 때 측정을 한다.
84 |
85 | if firstFlag :
86 | sds011.setWork()
87 | particleCnt = 9
88 | firstFlag = False
89 |
90 | #측정을 3번 할 때 (3 * 60 = 180초) 1번 서버에 데이터를 전달한다.
91 | if sendCountdown == sendAirInfoInterval :
92 | print ("send data..")
93 | params = urllib.urlencode(airInfo) #URL에 데이터를 실어서 서버로 데이터를 전달.
94 | conn = urllib.urlopen("http://fastpy003.appspot.com/setAirInfo?%s" % params)
95 | print (conn.read().decode("utf-8") )
96 | sendCountdown = 0
97 |
98 | if saveCountdown == saveAirInfoInterval:
99 | print ("save data..")
100 | params = urllib.urlencode(airInfo) #URL에 데이터를 실어서 서버로 데이터를 전달.
101 | conn = urllib.urlopen("http://fastpy003.appspot.com/saveAirInfo?%s" % params)
102 | print (conn.read().decode("utf-8") )
103 | saveCountdown = 0
104 |
105 | particleCnt += 1
106 | sendCountdown += 1
107 | saveCountdown += 1
108 |
109 | draw.rectangle((0,0,width-1,height-1), outline=1, fill=0) # 테두리 그리기
110 |
111 | top = 3
112 | labelX = 5
113 | dataX = 40
114 |
115 | # (labelX, top) 좌표를 기준으로 텍스트를 출력합니다.
116 | draw.text((labelX, top), u"온도", font=hanFont, fill=255)
117 | draw.text((labelX, top+20), u"습도", font=hanFont, fill=255)
118 | draw.text((labelX, top+40), u"대기압", font=hanFont, fill=255)
119 |
120 | draw.text((dataX, top), ":" + str(airInfo['temp']) + "C", font=font, fill=255)
121 | draw.text((dataX, top+20), ":" + str(round(airInfo['hum'],1)) + "%", font=font, fill=255)
122 | draw.text((dataX, top+40), ":" + str(round(airInfo['press'],2)), font=font, fill=255)
123 |
124 | # PIL Image 객체를 Bitmap으로 변환합니다.
125 | disp.image(image)
126 | # Bitmap 버퍼를 디스플레이 장치로 전송합니다.
127 | disp.display()
128 |
129 | time.sleep(0.5)
130 |
131 | finally:
132 | disp.clear()
133 | disp.display()
134 | sds011.setSleep() # SDS011에 전원이 끊어 집니다.
135 | time.sleep(1)
136 | sds011.end()
137 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/_exceptions.py:
--------------------------------------------------------------------------------
1 | """Different kinds of SAX Exceptions"""
2 | import sys
3 | if sys.platform[:4] == "java":
4 | from java.lang import Exception
5 | del sys
6 |
7 | # ===== SAXEXCEPTION =====
8 |
9 | class SAXException(Exception):
10 | """Encapsulate an XML error or warning. This class can contain
11 | basic error or warning information from either the XML parser or
12 | the application: you can subclass it to provide additional
13 | functionality, or to add localization. Note that although you will
14 | receive a SAXException as the argument to the handlers in the
15 | ErrorHandler interface, you are not actually required to throw
16 | the exception; instead, you can simply read the information in
17 | it."""
18 |
19 | def __init__(self, msg, exception=None):
20 | """Creates an exception. The message is required, but the exception
21 | is optional."""
22 | self._msg = msg
23 | self._exception = exception
24 | Exception.__init__(self, msg)
25 |
26 | def getMessage(self):
27 | "Return a message for this exception."
28 | return self._msg
29 |
30 | def getException(self):
31 | "Return the embedded exception, or None if there was none."
32 | return self._exception
33 |
34 | def __str__(self):
35 | "Create a string representation of the exception."
36 | return self._msg
37 |
38 | def __getitem__(self, ix):
39 | """Avoids weird error messages if someone does exception[ix] by
40 | mistake, since Exception has __getitem__ defined."""
41 | raise AttributeError("__getitem__")
42 |
43 |
44 | # ===== SAXPARSEEXCEPTION =====
45 |
46 | class SAXParseException(SAXException):
47 | """Encapsulate an XML parse error or warning.
48 |
49 | This exception will include information for locating the error in
50 | the original XML document. Note that although the application will
51 | receive a SAXParseException as the argument to the handlers in the
52 | ErrorHandler interface, the application is not actually required
53 | to throw the exception; instead, it can simply read the
54 | information in it and take a different action.
55 |
56 | Since this exception is a subclass of SAXException, it inherits
57 | the ability to wrap another exception."""
58 |
59 | def __init__(self, msg, exception, locator):
60 | "Creates the exception. The exception parameter is allowed to be None."
61 | SAXException.__init__(self, msg, exception)
62 | self._locator = locator
63 |
64 | # We need to cache this stuff at construction time.
65 | # If this exception is thrown, the objects through which we must
66 | # traverse to get this information may be deleted by the time
67 | # it gets caught.
68 | self._systemId = self._locator.getSystemId()
69 | self._colnum = self._locator.getColumnNumber()
70 | self._linenum = self._locator.getLineNumber()
71 |
72 | def getColumnNumber(self):
73 | """The column number of the end of the text where the exception
74 | occurred."""
75 | return self._colnum
76 |
77 | def getLineNumber(self):
78 | "The line number of the end of the text where the exception occurred."
79 | return self._linenum
80 |
81 | def getPublicId(self):
82 | "Get the public identifier of the entity where the exception occurred."
83 | return self._locator.getPublicId()
84 |
85 | def getSystemId(self):
86 | "Get the system identifier of the entity where the exception occurred."
87 | return self._systemId
88 |
89 | def __str__(self):
90 | "Create a string representation of the exception."
91 | sysid = self.getSystemId()
92 | if sysid is None:
93 | sysid = ""
94 | linenum = self.getLineNumber()
95 | if linenum is None:
96 | linenum = "?"
97 | colnum = self.getColumnNumber()
98 | if colnum is None:
99 | colnum = "?"
100 | return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
101 |
102 |
103 | # ===== SAXNOTRECOGNIZEDEXCEPTION =====
104 |
105 | class SAXNotRecognizedException(SAXException):
106 | """Exception class for an unrecognized identifier.
107 |
108 | An XMLReader will raise this exception when it is confronted with an
109 | unrecognized feature or property. SAX applications and extensions may
110 | use this class for similar purposes."""
111 |
112 |
113 | # ===== SAXNOTSUPPORTEDEXCEPTION =====
114 |
115 | class SAXNotSupportedException(SAXException):
116 | """Exception class for an unsupported operation.
117 |
118 | An XMLReader will raise this exception when a service it cannot
119 | perform is requested (specifically setting a state or value). SAX
120 | applications and extensions may use this class for similar
121 | purposes."""
122 |
123 | # ===== SAXNOTSUPPORTEDEXCEPTION =====
124 |
125 | class SAXReaderNotAvailable(SAXNotSupportedException):
126 | """Exception class for a missing driver.
127 |
128 | An XMLReader module (driver) should raise this exception when it
129 | is first imported, e.g. when a support module cannot be imported.
130 | It also may be raised during parsing, e.g. if executing an external
131 | program is not permitted."""
132 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/ElementInclude.py:
--------------------------------------------------------------------------------
1 | #
2 | # ElementTree
3 | # $Id: ElementInclude.py 1862 2004-06-18 07:31:02Z Fredrik $
4 | #
5 | # limited xinclude support for element trees
6 | #
7 | # history:
8 | # 2003-08-15 fl created
9 | # 2003-11-14 fl fixed default loader
10 | #
11 | # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved.
12 | #
13 | # fredrik@pythonware.com
14 | # http://www.pythonware.com
15 | #
16 | # --------------------------------------------------------------------
17 | # The ElementTree toolkit is
18 | #
19 | # Copyright (c) 1999-2004 by Fredrik Lundh
20 | #
21 | # By obtaining, using, and/or copying this software and/or its
22 | # associated documentation, you agree that you have read, understood,
23 | # and will comply with the following terms and conditions:
24 | #
25 | # Permission to use, copy, modify, and distribute this software and
26 | # its associated documentation for any purpose and without fee is
27 | # hereby granted, provided that the above copyright notice appears in
28 | # all copies, and that both that copyright notice and this permission
29 | # notice appear in supporting documentation, and that the name of
30 | # Secret Labs AB or the author not be used in advertising or publicity
31 | # pertaining to distribution of the software without specific, written
32 | # prior permission.
33 | #
34 | # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
35 | # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
36 | # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
37 | # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
38 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
40 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
41 | # OF THIS SOFTWARE.
42 | # --------------------------------------------------------------------
43 |
44 | # Licensed to PSF under a Contributor Agreement.
45 | # See http://www.python.org/2.4/license for licensing details.
46 |
47 | ##
48 | # Limited XInclude support for the ElementTree package.
49 | ##
50 |
51 | import copy
52 | from . import ElementTree
53 |
54 | XINCLUDE = "{http://www.w3.org/2001/XInclude}"
55 |
56 | XINCLUDE_INCLUDE = XINCLUDE + "include"
57 | XINCLUDE_FALLBACK = XINCLUDE + "fallback"
58 |
59 | ##
60 | # Fatal include error.
61 |
62 | class FatalIncludeError(SyntaxError):
63 | pass
64 |
65 | ##
66 | # Default loader. This loader reads an included resource from disk.
67 | #
68 | # @param href Resource reference.
69 | # @param parse Parse mode. Either "xml" or "text".
70 | # @param encoding Optional text encoding.
71 | # @return The expanded resource. If the parse mode is "xml", this
72 | # is an ElementTree instance. If the parse mode is "text", this
73 | # is a Unicode string. If the loader fails, it can return None
74 | # or raise an IOError exception.
75 | # @throws IOError If the loader fails to load the resource.
76 |
77 | def default_loader(href, parse, encoding=None):
78 | file = open(href)
79 | if parse == "xml":
80 | data = ElementTree.parse(file).getroot()
81 | else:
82 | data = file.read()
83 | if encoding:
84 | data = data.decode(encoding)
85 | file.close()
86 | return data
87 |
88 | ##
89 | # Expand XInclude directives.
90 | #
91 | # @param elem Root element.
92 | # @param loader Optional resource loader. If omitted, it defaults
93 | # to {@link default_loader}. If given, it should be a callable
94 | # that implements the same interface as default_loader.
95 | # @throws FatalIncludeError If the function fails to include a given
96 | # resource, or if the tree contains malformed XInclude elements.
97 | # @throws IOError If the function fails to load a given resource.
98 |
99 | def include(elem, loader=None):
100 | if loader is None:
101 | loader = default_loader
102 | # look for xinclude elements
103 | i = 0
104 | while i < len(elem):
105 | e = elem[i]
106 | if e.tag == XINCLUDE_INCLUDE:
107 | # process xinclude directive
108 | href = e.get("href")
109 | parse = e.get("parse", "xml")
110 | if parse == "xml":
111 | node = loader(href, parse)
112 | if node is None:
113 | raise FatalIncludeError(
114 | "cannot load %r as %r" % (href, parse)
115 | )
116 | node = copy.copy(node)
117 | if e.tail:
118 | node.tail = (node.tail or "") + e.tail
119 | elem[i] = node
120 | elif parse == "text":
121 | text = loader(href, parse, e.get("encoding"))
122 | if text is None:
123 | raise FatalIncludeError(
124 | "cannot load %r as %r" % (href, parse)
125 | )
126 | if i:
127 | node = elem[i-1]
128 | node.tail = (node.tail or "") + text
129 | else:
130 | elem.text = (elem.text or "") + text + (e.tail or "")
131 | del elem[i]
132 | continue
133 | else:
134 | raise FatalIncludeError(
135 | "unknown parse type in xi:include tag (%r)" % parse
136 | )
137 | elif e.tag == XINCLUDE_FALLBACK:
138 | raise FatalIncludeError(
139 | "xi:fallback tag must be child of xi:include (%r)" % e.tag
140 | )
141 | else:
142 | include(e, loader)
143 | i = i + 1
144 |
--------------------------------------------------------------------------------
/3부/26장/Pi/bme280.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #--------------------------------------
3 | # ___ ___ _ ____
4 | # / _ \/ _ \(_) __/__ __ __
5 | # / , _/ ___/ /\ \/ _ \/ // /
6 | # /_/|_/_/ /_/___/ .__/\_, /
7 | # /_/ /___/
8 | #
9 | # bme280.py
10 | # Read data from a digital pressure sensor.
11 | #
12 | # Official datasheet available from :
13 | # https://www.bosch-sensortec.com/bst/products/all_products/bme280
14 | #
15 | # Author : Matt Hawkins
16 | # Date : 25/07/2016
17 | #
18 | # http://www.raspberrypi-spy.co.uk/
19 | #
20 | #--------------------------------------
21 | import smbus
22 | import time
23 | from ctypes import c_short
24 | from ctypes import c_byte
25 | from ctypes import c_ubyte
26 |
27 | DEVICE = 0x76 # Default device I2C address
28 |
29 |
30 | bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1
31 | # Rev 1 Pi uses bus 0
32 |
33 | def getShort(data, index):
34 | # return two bytes from data as a signed 16-bit value
35 | return c_short((data[index+1] << 8) + data[index]).value
36 |
37 | def getUShort(data, index):
38 | # return two bytes from data as an unsigned 16-bit value
39 | return (data[index+1] << 8) + data[index]
40 |
41 | def getChar(data,index):
42 | # return one byte from data as a signed char
43 | result = data[index]
44 | if result > 127:
45 | result -= 256
46 | return result
47 |
48 | def getUChar(data,index):
49 | # return one byte from data as an unsigned char
50 | result = data[index] & 0xFF
51 | return result
52 |
53 | def readBME280ID(addr=DEVICE):
54 | # Chip ID Register Address
55 | REG_ID = 0xD0
56 | (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
57 | return (chip_id, chip_version)
58 |
59 | def readBME280All(addr=DEVICE):
60 | # Register Addresses
61 | REG_DATA = 0xF7
62 | REG_CONTROL = 0xF4
63 | REG_CONFIG = 0xF5
64 |
65 | REG_CONTROL_HUM = 0xF2
66 | REG_HUM_MSB = 0xFD
67 | REG_HUM_LSB = 0xFE
68 |
69 | # Oversample setting - page 27
70 | OVERSAMPLE_TEMP = 2
71 | OVERSAMPLE_PRES = 2
72 | MODE = 1
73 |
74 | # Oversample setting for humidity register - page 26
75 | OVERSAMPLE_HUM = 2
76 | bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM)
77 |
78 | control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE
79 | bus.write_byte_data(addr, REG_CONTROL, control)
80 |
81 | # Read blocks of calibration data from EEPROM
82 | # See Page 22 data sheet
83 | cal1 = bus.read_i2c_block_data(addr, 0x88, 24)
84 | cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)
85 | cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)
86 |
87 | # Convert byte data to word values
88 | dig_T1 = getUShort(cal1, 0)
89 | dig_T2 = getShort(cal1, 2)
90 | dig_T3 = getShort(cal1, 4)
91 |
92 | dig_P1 = getUShort(cal1, 6)
93 | dig_P2 = getShort(cal1, 8)
94 | dig_P3 = getShort(cal1, 10)
95 | dig_P4 = getShort(cal1, 12)
96 | dig_P5 = getShort(cal1, 14)
97 | dig_P6 = getShort(cal1, 16)
98 | dig_P7 = getShort(cal1, 18)
99 | dig_P8 = getShort(cal1, 20)
100 | dig_P9 = getShort(cal1, 22)
101 |
102 | dig_H1 = getUChar(cal2, 0)
103 | dig_H2 = getShort(cal3, 0)
104 | dig_H3 = getUChar(cal3, 2)
105 |
106 | dig_H4 = getChar(cal3, 3)
107 | dig_H4 = (dig_H4 << 24) >> 20
108 | dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)
109 |
110 | dig_H5 = getChar(cal3, 5)
111 | dig_H5 = (dig_H5 << 24) >> 20
112 | dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)
113 |
114 | dig_H6 = getChar(cal3, 6)
115 |
116 | # Wait in ms (Datasheet Appendix B: Measurement time and current calculation)
117 | wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575)
118 | time.sleep(wait_time/1000) # Wait the required time
119 |
120 | # Read temperature/pressure/humidity
121 | data = bus.read_i2c_block_data(addr, REG_DATA, 8)
122 | pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
123 | temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
124 | hum_raw = (data[6] << 8) | data[7]
125 |
126 | #Refine temperature
127 | var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11
128 | var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14
129 | t_fine = var1+var2
130 | temperature = float(((t_fine * 5) + 128) >> 8);
131 |
132 | # Refine pressure and adjust for temperature
133 | var1 = t_fine / 2.0 - 64000.0
134 | var2 = var1 * var1 * dig_P6 / 32768.0
135 | var2 = var2 + var1 * dig_P5 * 2.0
136 | var2 = var2 / 4.0 + dig_P4 * 65536.0
137 | var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0
138 | var1 = (1.0 + var1 / 32768.0) * dig_P1
139 | if var1 == 0:
140 | pressure=0
141 | else:
142 | pressure = 1048576.0 - pres_raw
143 | pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1
144 | var1 = dig_P9 * pressure * pressure / 2147483648.0
145 | var2 = pressure * dig_P8 / 32768.0
146 | pressure = pressure + (var1 + var2 + dig_P7) / 16.0
147 |
148 | # Refine humidity
149 | humidity = t_fine - 76800.0
150 | humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity)))
151 | humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0)
152 | if humidity > 100:
153 | humidity = 100
154 | elif humidity < 0:
155 | humidity = 0
156 |
157 | return temperature/100.0,pressure/100.0,humidity
158 |
159 | def main():
160 |
161 | (chip_id, chip_version) = readBME280ID()
162 | print "Chip ID :", chip_id
163 | print "Version :", chip_version
164 |
165 | temperature,pressure,humidity = readBME280All()
166 |
167 | print "Temperature : ", temperature, "C"
168 | print "Pressure : ", pressure, "hPa"
169 | print "Humidity : ", humidity, "%"
170 |
171 | if __name__=="__main__":
172 | main()
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/etree/ElementPath.py:
--------------------------------------------------------------------------------
1 | #
2 | # ElementTree
3 | # $Id: ElementPath.py 1858 2004-06-17 21:31:41Z Fredrik $
4 | #
5 | # limited xpath support for element trees
6 | #
7 | # history:
8 | # 2003-05-23 fl created
9 | # 2003-05-28 fl added support for // etc
10 | # 2003-08-27 fl fixed parsing of periods in element names
11 | #
12 | # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved.
13 | #
14 | # fredrik@pythonware.com
15 | # http://www.pythonware.com
16 | #
17 | # --------------------------------------------------------------------
18 | # The ElementTree toolkit is
19 | #
20 | # Copyright (c) 1999-2004 by Fredrik Lundh
21 | #
22 | # By obtaining, using, and/or copying this software and/or its
23 | # associated documentation, you agree that you have read, understood,
24 | # and will comply with the following terms and conditions:
25 | #
26 | # Permission to use, copy, modify, and distribute this software and
27 | # its associated documentation for any purpose and without fee is
28 | # hereby granted, provided that the above copyright notice appears in
29 | # all copies, and that both that copyright notice and this permission
30 | # notice appear in supporting documentation, and that the name of
31 | # Secret Labs AB or the author not be used in advertising or publicity
32 | # pertaining to distribution of the software without specific, written
33 | # prior permission.
34 | #
35 | # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
36 | # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
37 | # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
38 | # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
39 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
40 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
41 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
42 | # OF THIS SOFTWARE.
43 | # --------------------------------------------------------------------
44 |
45 | # Licensed to PSF under a Contributor Agreement.
46 | # See http://www.python.org/2.4/license for licensing details.
47 |
48 | ##
49 | # Implementation module for XPath support. There's usually no reason
50 | # to import this module directly; the ElementTree does this for
51 | # you, if needed.
52 | ##
53 |
54 | import re
55 |
56 | xpath_tokenizer = re.compile(
57 | "(::|\.\.|\(\)|[/.*:\[\]\(\)@=])|((?:\{[^}]+\})?[^/:\[\]\(\)@=\s]+)|\s+"
58 | ).findall
59 |
60 | class xpath_descendant_or_self:
61 | pass
62 |
63 | ##
64 | # Wrapper for a compiled XPath.
65 |
66 | class Path:
67 |
68 | ##
69 | # Create an Path instance from an XPath expression.
70 |
71 | def __init__(self, path):
72 | tokens = xpath_tokenizer(path)
73 | # the current version supports 'path/path'-style expressions only
74 | self.path = []
75 | self.tag = None
76 | if tokens and tokens[0][0] == "/":
77 | raise SyntaxError("cannot use absolute path on element")
78 | while tokens:
79 | op, tag = tokens.pop(0)
80 | if tag or op == "*":
81 | self.path.append(tag or op)
82 | elif op == ".":
83 | pass
84 | elif op == "/":
85 | self.path.append(xpath_descendant_or_self())
86 | continue
87 | else:
88 | raise SyntaxError("unsupported path syntax (%s)" % op)
89 | if tokens:
90 | op, tag = tokens.pop(0)
91 | if op != "/":
92 | raise SyntaxError(
93 | "expected path separator (%s)" % (op or tag)
94 | )
95 | if self.path and isinstance(self.path[-1], xpath_descendant_or_self):
96 | raise SyntaxError("path cannot end with //")
97 | if len(self.path) == 1 and isinstance(self.path[0], type("")):
98 | self.tag = self.path[0]
99 |
100 | ##
101 | # Find first matching object.
102 |
103 | def find(self, element):
104 | tag = self.tag
105 | if tag is None:
106 | nodeset = self.findall(element)
107 | if not nodeset:
108 | return None
109 | return nodeset[0]
110 | for elem in element:
111 | if elem.tag == tag:
112 | return elem
113 | return None
114 |
115 | ##
116 | # Find text for first matching object.
117 |
118 | def findtext(self, element, default=None):
119 | tag = self.tag
120 | if tag is None:
121 | nodeset = self.findall(element)
122 | if not nodeset:
123 | return default
124 | return nodeset[0].text or ""
125 | for elem in element:
126 | if elem.tag == tag:
127 | return elem.text or ""
128 | return default
129 |
130 | ##
131 | # Find all matching objects.
132 |
133 | def findall(self, element):
134 | nodeset = [element]
135 | index = 0
136 | while 1:
137 | try:
138 | path = self.path[index]
139 | index = index + 1
140 | except IndexError:
141 | return nodeset
142 | set = []
143 | if isinstance(path, xpath_descendant_or_self):
144 | try:
145 | tag = self.path[index]
146 | if not isinstance(tag, type("")):
147 | tag = None
148 | else:
149 | index = index + 1
150 | except IndexError:
151 | tag = None # invalid path
152 | for node in nodeset:
153 | new = list(node.getiterator(tag))
154 | if new and new[0] is node:
155 | set.extend(new[1:])
156 | else:
157 | set.extend(new)
158 | else:
159 | for node in nodeset:
160 | for node in node:
161 | if path == "*" or node.tag == path:
162 | set.append(node)
163 | if not set:
164 | return []
165 | nodeset = set
166 |
167 | _cache = {}
168 |
169 | ##
170 | # (Internal) Compile path.
171 |
172 | def _compile(path):
173 | p = _cache.get(path)
174 | if p is not None:
175 | return p
176 | p = Path(path)
177 | if len(_cache) >= 100:
178 | _cache.clear()
179 | _cache[path] = p
180 | return p
181 |
182 | ##
183 | # Find first matching object.
184 |
185 | def find(element, path):
186 | return _compile(path).find(element)
187 |
188 | ##
189 | # Find text for first matching object.
190 |
191 | def findtext(element, path, default=None):
192 | return _compile(path).findtext(element, default)
193 |
194 | ##
195 | # Find all matching objects.
196 |
197 | def findall(element, path):
198 | return _compile(path).findall(element)
199 |
--------------------------------------------------------------------------------
/3부/26장/Pi/myAir.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | import time
5 | from datetime import datetime, timedelta
6 | import bme280
7 | import sds011 # 미세먼지 측정 모듈 임포트
8 |
9 | import Adafruit_GPIO.SPI as SPI
10 | import Adafruit_SSD1306
11 | import RPi.GPIO as GPIO # GPIO 임포트
12 |
13 | # Python Image Library를 임포트 합니다.
14 | from PIL import Image
15 | from PIL import ImageDraw
16 | from PIL import ImageFont
17 |
18 | import urllib
19 |
20 | # 라즈베리 파이와 연결된 BCM 핀번호를 선언합니다.
21 | RST = 24
22 | DC = 23
23 | SPI_PORT = 0
24 | SPI_DEVICE = 0
25 | btnPinNum = 6 # 버튼 입력 핀번호
26 | ledPinNum = 5 #LED 버튼 입력 번호
27 |
28 | # SPI를 사용하는 128x64 해상도 디스플레이를 선언합니다.
29 | disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))
30 |
31 | # 라이브러리를 초기화
32 | disp.begin()
33 |
34 | # 디스플레이를 클리어 해줍니다.
35 | disp.clear()
36 | disp.display()
37 |
38 | # sds011을 초기화 해줍니다.
39 | sds011.start()
40 |
41 | width = disp.width
42 | height = disp.height
43 | image = Image.new('1', (width, height))
44 |
45 | #LED 핀 초기화
46 | GPIO.setmode(GPIO.BCM) #GPIO num으로 사용
47 | GPIO.setup(ledPinNum, GPIO.OUT) #GPIO 5번 핀을 출력용으로 선언
48 |
49 | draw = ImageDraw.Draw(image)
50 | server_address = "http://fastpy003.appspot.com" # 측정 데이터를 저장할 곳의 웹서비스 주소
51 | airInfo = {'temp': 0.0, 'hum': 0.0, 'press':0.0, 'pm25':0.0, 'pm10': 0.0} # 측정 데이터 저장소
52 |
53 | pm10WarnVal = 81 #81 # 미세 먼지 경고 기준값
54 | cancelHumVal = 80
55 |
56 | # 화면에 어떤 데이터를 그릴지 정합니다.
57 | displayMode = 1 # 1: 온도, 2: 습도, 3 대기압, 4:미세먼지, 5: 초미세먼지,
58 |
59 | # timestamp를 이용해 일정 시간이 마다 측정을 하고 데이터를 처리 할 수 있도록 구현됨.
60 | displayInterval = timedelta(seconds=5) # 5초 마다 1번씩 화면을 업데이트 한다.
61 | checkBME280Interval = timedelta(seconds=60) # 60초 마다 온도, 습도, 대기압 측정
62 | checkSDS011Interval = timedelta(minutes=9) # 9분동안 sleep상태 1분 동안 미세먼지 측정
63 | sendDataInterval = timedelta(minutes=3) # 3분 한번 데이터를 서버로 전송한다.
64 | saveDataInterval = timedelta(minutes=12) # 12분 한번 데이터를 서버에 저장한다. 1시간에 5번
65 |
66 | # 처음 시작 할 때 바로 데이터를 읽어 올 수 있도록 기준값을 현재 시간보다 하루 빠르게 한다.
67 | prevDisplayTValue = datetime.now() - timedelta(days=1)
68 | prevBME280TValue = datetime.now() - timedelta(days=1)
69 | prevSDS011TValue = datetime.now() - timedelta(days=1)
70 | SDS011WorkingTValue = datetime.now() # 미세먼지 센서는 1분간 동작 시킨다.
71 | prevSendDataTValue = datetime.now()
72 | prevSaveDataTValue = datetime.now()
73 |
74 | try:
75 | drawTop = 3
76 | drawSubLabelX = 5
77 | font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 27)
78 | hanFont = ImageFont.truetype('/usr/share/fonts/truetype/unfonts-core/UnDinaru.ttf', 15)
79 | midHanFont = ImageFont.truetype('/usr/share/fonts/truetype/unfonts-core/UnDinaru.ttf', 12)
80 |
81 | while True:
82 | currentDateTime = datetime.now()
83 |
84 | if currentDateTime - prevBME280TValue > checkBME280Interval :
85 | # 온도, 습도, 기압 정보를 가져온다.
86 | prevBME280TValue = datetime.now() # 현재시간 저장
87 | print("read BME280")
88 | airInfo['temp'], airInfo['press'], airInfo['hum'] = bme280.readBME280All()
89 | airInfo['temp'] = round(airInfo['temp'],1)
90 | airInfo['press'] = round(airInfo['press'],1)
91 | airInfo['hum'] = round(airInfo['hum'],1)
92 |
93 | if currentDateTime - prevSDS011TValue > checkSDS011Interval :
94 | #미세먼지 측정을 위해 sds011을 켠다.
95 | prevSDS011TValue = datetime.now() # 현재시간 저장
96 | print("turn on SDS011")
97 | if not sds011.isWorkingMode() :
98 | SDS011WorkingTValue = currentDateTime + timedelta(minutes=1)
99 | sds011.setWork()
100 |
101 | if currentDateTime > SDS011WorkingTValue :
102 | #미세먼지 측정 후 60초가 지나면 데이터를 읽고 센서 전원을 끊는다.
103 | if sds011.isWorkingMode() : # 센서가 동작하고 있으면 미세먼지 정보를 읽어온다.
104 | print("get on particle info")
105 | particle = sds011.getParticle()
106 | if particle :
107 | # 습도가 cancelHumVal 이상 넘어가면 정확도가 떨어지기 때문에 측정값을 무시한다.
108 | if airInfo['hum'] < cancelHumVal :
109 | airInfo['pm25'], airInfo['pm10'] = particle
110 | airInfo['pm25'] = round(airInfo['pm25'],1)
111 | airInfo['pm10'] = round(airInfo['pm10'],1)
112 |
113 | if airInfo['pm10'] > pm10WarnVal:
114 | GPIO.output(ledPinNum, True)
115 | else:
116 | GPIO.output(ledPinNum, False)
117 | else :
118 | airInfo['pm25'] = 0.0
119 | airInfo['pm10'] = 0.0
120 | GPIO.output(ledPinNum, False)
121 |
122 | sds011.setSleep() # SDS011을 sleep 상태로 바꿔준다.
123 | print("turn off SDS011")
124 |
125 | if currentDateTime - prevDisplayTValue > displayInterval :
126 | prevDisplayTValue = datetime.now()
127 |
128 | print ("draw display :" + str(displayMode))
129 | draw.rectangle((0,0,width-1,height-1), outline=1, fill=0) # 테두리 그리기
130 |
131 | if displayMode == 1:
132 | draw.text((drawSubLabelX, drawTop), u"온도 ( ℃ )", font=hanFont, fill=255)
133 | draw.text((drawSubLabelX + 25, drawTop+25), str(airInfo['temp']) , font=font, fill=255)
134 | elif displayMode == 2:
135 | draw.text((drawSubLabelX, drawTop), u"습도 ( % )", font=hanFont, fill=255)
136 | draw.text((drawSubLabelX + 25, drawTop+25), str(airInfo['hum']) , font=font, fill=255)
137 | elif displayMode == 3:
138 | draw.text((drawSubLabelX, drawTop), u"대기압 ( hPa )", font=hanFont, fill=255)
139 | draw.text((drawSubLabelX + 12, drawTop+25), str(airInfo['press']) , font=font, fill=255)
140 | elif displayMode == 4:
141 | draw.text((drawSubLabelX, drawTop), u"미세먼지 (ug/m^3)", font=midHanFont, fill=255)
142 | draw.text((drawSubLabelX + 25, drawTop+25), str(airInfo['pm10']) , font=font, fill=255)
143 | elif displayMode == 5:
144 | draw.text((drawSubLabelX, drawTop), u"초미세먼지 (ug/m^3)", font=midHanFont, fill=255)
145 | draw.text((drawSubLabelX + 25, drawTop+25), str(airInfo['pm25']) , font=font, fill=255)
146 | displayMode = 0 # 처음으로 돌아 간다.
147 |
148 | displayMode += 1
149 |
150 | #draw display
151 | disp.image(image)
152 | disp.display()
153 |
154 | # Send Data to Server
155 | if currentDateTime - prevSendDataTValue > sendDataInterval :
156 | prevSendDataTValue = datetime.now()
157 | print ("send data")
158 | params = urllib.urlencode(airInfo) #URL에 데이터를 실어서 서버로 데이터를 전달.
159 | conn = urllib.urlopen( server_address + "/setAirInfo?%s" % params)
160 | print (conn.read().decode("utf-8") )
161 |
162 | if currentDateTime - prevSaveDataTValue > saveDataInterval :
163 | prevSaveDataTValue = datetime.now()
164 | print ("save data")
165 | params = urllib.urlencode(airInfo) #URL에 데이터를 실어서 서버로 데이터를 전달.
166 | conn = urllib.urlopen( server_address + "/saveAirInfo?%s" % params)
167 | print (conn.read().decode("utf-8") )
168 |
169 | time.sleep(0.5)
170 |
171 | finally:
172 | disp.clear()
173 | disp.display()
174 | sds011.setSleep() # SDS011에 전원이 끊어 집니다.
175 | time.sleep(1)
176 | sds011.end()
--------------------------------------------------------------------------------
/3부/23장/nba_decision_tree.py:
--------------------------------------------------------------------------------
1 | import os
2 | import pandas
3 |
4 | home_folder = os.path.expanduser("~")
5 | data_folder = os.path.join(home_folder, "Documents", "python", "nba_winner")
6 |
7 | data_files=["nba_2015_10.csv", "nba_2015_11.csv", "nba_2015_12.csv",
8 | "nba_2016_01.csv", "nba_2016_02.csv", "nba_2016_03.csv",
9 | "nba_2016_04.csv", "nba_2016_05.csv", "nba_2016_06.csv"]
10 |
11 | cvs_objs = []
12 | for f in data_files:
13 | month_data = os.path.join(data_folder, f)
14 | cvs_objs.append(pandas.read_csv(month_data))
15 |
16 | season_result = pandas.concat(cvs_objs, ignore_index=True)
17 | season_result.columns = ["Date", "StartTime", "VisitorTeam", "VisitorPts",
18 | "HomeTeam", "HomePts", "ScoreType", "Overtime", "Notes"]
19 |
20 | print(season_result[-5:])
21 |
22 | # HomeWin: 홈팀이 승리한 경우에는 True, 아닌 경우에는 False으로 입력
23 | season_result["HomeWin"] = season_result["HomePts"] > season_result["VisitorPts"]
24 |
25 | # 홈팀으로 적극적인 응원을 받는 경우, 팀의 승리에 유리한지 확인
26 | score = 100 * season_result["HomeWin"].sum() / season_result["HomeWin"].count()
27 | print("Home Win percentage: {0:.1f}%".format(score))
28 |
29 | # 작년 시즌의 순위를 고려
30 | standing_file = os.path.join(data_folder, "nba_2014_2015_standing.csv")
31 | standing_result = pandas.read_csv(standing_file, skiprows=[0])
32 |
33 | # 선수 개개인의 능력치를 고려
34 | player_file = os.path.join(data_folder, "nba_2016_player_stat.csv")
35 | player_result = pandas.read_csv(player_file)
36 | print(player_result["PLAYER"][:3])
37 |
38 | # 팀 약자와 팀명을 맵핑
39 | team_name = {"GS" : "Golden State Warriors",
40 | "SA" : "San Antonio Spurs",
41 | "CLE" : "Cleveland Cavaliers",
42 | "TOR" : "Toronto Raptors",
43 | "OKC" : "Oklahoma City Thunder",
44 | "LAC" : "Los Angeles Clippers",
45 | "ATL" : "Atlanta Hawks",
46 | "BOS" : "Boston Celtics",
47 | "CHA" : "Charlotte Hornets",
48 | "MIA" : "Miami Heat",
49 | "IND" : "Indiana Pacers",
50 | "DET" : "Detroit Pistons",
51 | "POR" : "Portland Trail Blazers",
52 | "DAL" : "Dallas Mavericks",
53 | "MEM" : "Memphis Grizzlies",
54 | "CHI" : "Chicago Bulls",
55 | "HOU" : "Houston Rockets",
56 | "WSH" : "Washington Wizards",
57 | "UTAH" : "Utah Jazz",
58 | "ORL" : "Orlando Magic",
59 | "DEN" : "Denver Nuggets",
60 | "MIL" : "Milwaukee Bucks",
61 | "SAC" : "Sacramento Kings",
62 | "NY" : "New York Knicks",
63 | "NO" : "New Orleans Pelicans",
64 | "MIN" : "Minnesota Timberwolves",
65 | "PHX" : "Phoenix Suns",
66 | "BKN" : "Brooklyn Nets",
67 | "LAL" : "Los Angeles Lakers",
68 | "PHI" : "Philadelphia 76ers",
69 | }
70 |
71 | # NBA 모든 선수를 정보를 순회하면서 각 선수의 팀과 PER 값을 추출
72 | team_per = {}
73 | for key, value in team_name.items():
74 | team_per[value] = []
75 |
76 | for idx, row in player_result.iterrows():
77 | player = row["PLAYER"]
78 | per = row["PER"]
79 | team_list = player.split(',')[1].strip(' ').split("/")
80 | for team in team_list:
81 | team_per[team_name[team]].append(per)
82 |
83 | # PER 지수 비교
84 | import numpy
85 | print("Golden State Warriors: Sum of PER: {0:.2f} / Mean of PER: {1:.2f}"
86 | .format(numpy.sum(team_per["Golden State Warriors"]), numpy.mean(team_per["Golden State Warriors"])))
87 | print("Philadelphia 76ers: Sum of PER: {0:.2f} / Mean of PER: {1:.2f}"
88 | .format(numpy.sum(team_per["Philadelphia 76ers"]), numpy.mean(team_per["Philadelphia 76ers"])))
89 |
90 |
91 | # 홈팀, 원정팀의 연승 횟수를 세기 위한 추가 컬럼
92 | season_result["HomeWinStreak"] = 0
93 | season_result["VisitorWinStreak"] = 0
94 |
95 | from collections import defaultdict
96 | winning_streak = defaultdict(int)
97 |
98 | for index, row in season_result.iterrows():
99 | home = row["HomeTeam"]
100 | visitor = row["VisitorTeam"]
101 | row["HomeWinStreak"] = winning_streak[home]
102 | row["VisitorWinStreak"] = winning_streak[visitor]
103 | season_result.ix[index] = row
104 |
105 | if row["HomeWin"]:
106 | winning_streak[home] += 1
107 | winning_streak[visitor] = 0
108 | else:
109 | winning_streak[home] = 0
110 | winning_streak[visitor] += 1
111 |
112 | # 홈팀의 승리 결과는 y_test 변수에 저장
113 | y_test = season_result["HomeWin"].values
114 |
115 |
116 | from sklearn.tree import DecisionTreeClassifier
117 | from sklearn.model_selection import cross_val_score
118 |
119 | clf = DecisionTreeClassifier(random_state=7)
120 | x_test = season_result[["HomeWinStreak", "VisitorWinStreak"]].values
121 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
122 |
123 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
124 |
125 | # 각 팀별 상위 10명의 선수의 PER 합계를 저장
126 | standing_result["PER_Sum"] = 0
127 | for idx, row in standing_result.iterrows():
128 | team = row["Team"]
129 | row["PER_Sum"] = numpy.sum(team_per[team][:10])
130 | standing_result.ix[idx] = row
131 |
132 | # 매 경기마다 Home team의 PER이 높은 경우를 1, 아닌 경우를 0으로 설정
133 | season_result["HomePERHigh"] = 0
134 | for idx, row in season_result.iterrows():
135 | home = row["HomeTeam"]
136 | visitor = row["VisitorTeam"]
137 |
138 | home_per = standing_result[standing_result["Team"] == home]["PER_Sum"].values[0]
139 | visitor_per = standing_result[standing_result["Team"] == visitor]["PER_Sum"].values[0]
140 | row["HomePERHigh"] = int(home_per > visitor_per)
141 | season_result.ix[idx] = row
142 |
143 | x_test = season_result[["HomePERHigh"]].values
144 | clf = DecisionTreeClassifier(random_state=7)
145 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
146 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
147 |
148 | x_test = season_result[["HomeWinStreak", "VisitorWinStreak", "HomePERHigh"]].values
149 | clf = DecisionTreeClassifier(random_state=7)
150 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
151 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
152 |
153 |
154 | # 각 팀의 이름을 숫자로 변환: Label encoding
155 | from sklearn.preprocessing import LabelEncoder
156 | name_encoding = LabelEncoder()
157 | name_encoding.fit(season_result["HomeTeam"].values)
158 |
159 | print(name_encoding.transform(["Golden State Warriors", "Cleveland Cavaliers"]))
160 | print(name_encoding.inverse_transform([9, 5]))
161 |
162 | # 숫자로 변환된 팀 경기 정보를 team_match에 저장
163 | home_teams = name_encoding.transform(season_result["HomeTeam"].values)
164 | visitor_teams = name_encoding.transform(season_result["VisitorTeam"].values)
165 | team_match = numpy.vstack([home_teams, visitor_teams]).T
166 |
167 | from sklearn.preprocessing import OneHotEncoder
168 | onehot = OneHotEncoder()
169 | x_test = onehot.fit_transform(team_match).todense()
170 |
171 | clf = DecisionTreeClassifier(random_state=7)
172 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
173 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
174 |
175 | # 작년 시즌의 팀의 순위를 고려하는 경우
176 | season_result["HomeRankHigh"] = 0
177 | for idx, row in season_result.iterrows():
178 | home = row["HomeTeam"]
179 | visitor = row["VisitorTeam"]
180 |
181 | home_rank = standing_result[standing_result["Team"] == home]["Rk"].values[0]
182 | visitor_rank = standing_result[standing_result["Team"] == visitor]["Rk"].values[0]
183 | row["HomeRankHigh"] = int(home_rank > visitor_rank)
184 | season_result.ix[idx] = row
185 |
186 | x_test = season_result[["HomeRankHigh"]].values
187 | clf = DecisionTreeClassifier(random_state=7)
188 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
189 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
190 |
191 | # 작년 시즌의 팀의 순위와 per, 연승 정보를 같이 고려하는 경우
192 | x_test = season_result[["HomeRankHigh", "HomePERHigh", "HomeWinStreak", "VisitorWinStreak"]].values
193 | clf = DecisionTreeClassifier(random_state=7)
194 | scores = cross_val_score(clf, x_test, y_test, scoring='accuracy')
195 | print("Accuracy: {0:.1f}% (+/- {1:.2f}%)".format(numpy.mean(scores) * 100, numpy.std(scores)))
196 |
197 |
198 |
--------------------------------------------------------------------------------
/1부/9장/spam/spam.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 |
23 |
24 |
25 | {585E3499-4B4E-42CE-9FBD-91E0B067C211}
26 | Win32Proj
27 | spam
28 | 8.1
29 |
30 |
31 |
32 | DynamicLibrary
33 | true
34 | v140
35 | Unicode
36 |
37 |
38 | DynamicLibrary
39 | false
40 | v140
41 | true
42 | Unicode
43 |
44 |
45 | DynamicLibrary
46 | true
47 | v140
48 | Unicode
49 |
50 |
51 | DynamicLibrary
52 | false
53 | v140
54 | true
55 | Unicode
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | true
77 | $(VC_IncludePath);$(WindowsSDK_IncludePath);c:\Python36\include
78 |
79 |
80 | true
81 |
82 |
83 | false
84 |
85 |
86 | false
87 |
88 |
89 |
90 |
91 |
92 | Level3
93 | Disabled
94 | WIN32;_DEBUG;_WINDOWS;_USRDLL;SPAM_EXPORTS;%(PreprocessorDefinitions)
95 | true
96 | c:\python36\include
97 |
98 |
99 | Windows
100 | true
101 | c:\python36\libs
102 | kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies);python36_d.lib
103 | /export:PyInit_spam %(AdditionalOptions)
104 | $(OutDir)\spam.pyd
105 |
106 |
107 |
108 |
109 |
110 |
111 | Level3
112 | Disabled
113 | _DEBUG;_WINDOWS;_USRDLL;SPAM_EXPORTS;%(PreprocessorDefinitions)
114 | true
115 |
116 |
117 | Windows
118 | true
119 |
120 |
121 |
122 |
123 | Level3
124 |
125 |
126 | MaxSpeed
127 | true
128 | true
129 | WIN32;NDEBUG;_WINDOWS;_USRDLL;SPAM_EXPORTS;%(PreprocessorDefinitions)
130 | true
131 | c:\Python36\include
132 |
133 |
134 | Windows
135 | true
136 | true
137 | true
138 | c:\Python36\libs
139 |
140 |
141 |
142 |
143 | Level3
144 |
145 |
146 | MaxSpeed
147 | true
148 | true
149 | NDEBUG;_WINDOWS;_USRDLL;SPAM_EXPORTS;%(PreprocessorDefinitions)
150 | true
151 |
152 |
153 | Windows
154 | true
155 | true
156 | true
157 |
158 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/3부/26장/WebApp/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | AIR
5 |
6 |
7 |
8 |
9 |
198 |
199 |
200 |
201 |
202 | 현재 공기 정보
203 |
208 | 공기 정보 차트
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
--------------------------------------------------------------------------------
/2부/19장/simple Example/xml/sax/saxutils.py:
--------------------------------------------------------------------------------
1 | """\
2 | A library of useful helper classes to the SAX classes, for the
3 | convenience of application and driver writers.
4 | """
5 |
6 | import os, urllib.parse, urllib.request
7 | from . import handler
8 | from . import xmlreader
9 |
10 | # See whether the xmlcharrefreplace error handler is
11 | # supported
12 | try:
13 | from codecs import xmlcharrefreplace_errors
14 | _error_handling = "xmlcharrefreplace"
15 | del xmlcharrefreplace_errors
16 | except ImportError:
17 | _error_handling = "strict"
18 |
19 | def __dict_replace(s, d):
20 | """Replace substrings of a string using a dictionary."""
21 | for key, value in d.items():
22 | s = s.replace(key, value)
23 | return s
24 |
25 | def escape(data, entities={}):
26 | """Escape &, <, and > in a string of data.
27 |
28 | You can escape other strings of data by passing a dictionary as
29 | the optional entities parameter. The keys and values must all be
30 | strings; each key will be replaced with its corresponding value.
31 | """
32 |
33 | # must do ampersand first
34 | data = data.replace("&", "&")
35 | data = data.replace(">", ">")
36 | data = data.replace("<", "<")
37 | if entities:
38 | data = __dict_replace(data, entities)
39 | return data
40 |
41 | def unescape(data, entities={}):
42 | """Unescape &, <, and > in a string of data.
43 |
44 | You can unescape other strings of data by passing a dictionary as
45 | the optional entities parameter. The keys and values must all be
46 | strings; each key will be replaced with its corresponding value.
47 | """
48 | data = data.replace("<", "<")
49 | data = data.replace(">", ">")
50 | if entities:
51 | data = __dict_replace(data, entities)
52 | # must do ampersand last
53 | return data.replace("&", "&")
54 |
55 | def quoteattr(data, entities={}):
56 | """Escape and quote an attribute value.
57 |
58 | Escape &, <, and > in a string of data, then quote it for use as
59 | an attribute value. The \" character will be escaped as well, if
60 | necessary.
61 |
62 | You can escape other strings of data by passing a dictionary as
63 | the optional entities parameter. The keys and values must all be
64 | strings; each key will be replaced with its corresponding value.
65 | """
66 | entities = entities.copy()
67 | entities.update({'\n': '
', '\r': '
', '\t':' '})
68 | data = escape(data, entities)
69 | if '"' in data:
70 | if "'" in data:
71 | data = '"%s"' % data.replace('"', """)
72 | else:
73 | data = "'%s'" % data
74 | else:
75 | data = '"%s"' % data
76 | return data
77 |
78 |
79 | class XMLGenerator(handler.ContentHandler):
80 |
81 | def __init__(self, out=None, encoding="iso-8859-1"):
82 | if out is None:
83 | import sys
84 | out = sys.stdout
85 | handler.ContentHandler.__init__(self)
86 | self._out = out
87 | self._ns_contexts = [{}] # contains uri -> prefix dicts
88 | self._current_context = self._ns_contexts[-1]
89 | self._undeclared_ns_maps = []
90 | self._encoding = encoding
91 |
92 | def _write(self, text):
93 | if isinstance(text, str):
94 | self._out.write(text)
95 | else:
96 | self._out.write(text.encode(self._encoding, _error_handling))
97 |
98 | def _qname(self, name):
99 | """Builds a qualified name from a (ns_url, localname) pair"""
100 | if name[0]:
101 | # The name is in a non-empty namespace
102 | prefix = self._current_context[name[0]]
103 | if prefix:
104 | # If it is not the default namespace, prepend the prefix
105 | return prefix + ":" + name[1]
106 | # Return the unqualified name
107 | return name[1]
108 |
109 | # ContentHandler methods
110 |
111 | def startDocument(self):
112 | self._write('\n' %
113 | self._encoding)
114 |
115 | def startPrefixMapping(self, prefix, uri):
116 | self._ns_contexts.append(self._current_context.copy())
117 | self._current_context[uri] = prefix
118 | self._undeclared_ns_maps.append((prefix, uri))
119 |
120 | def endPrefixMapping(self, prefix):
121 | self._current_context = self._ns_contexts[-1]
122 | del self._ns_contexts[-1]
123 |
124 | def startElement(self, name, attrs):
125 | self._write('<' + name)
126 | for (name, value) in attrs.items():
127 | self._write(' %s=%s' % (name, quoteattr(value)))
128 | self._write('>')
129 |
130 | def endElement(self, name):
131 | self._write('%s>' % name)
132 |
133 | def startElementNS(self, name, qname, attrs):
134 | self._write('<' + self._qname(name))
135 |
136 | for prefix, uri in self._undeclared_ns_maps:
137 | if prefix:
138 | self._out.write(' xmlns:%s="%s"' % (prefix, uri))
139 | else:
140 | self._out.write(' xmlns="%s"' % uri)
141 | self._undeclared_ns_maps = []
142 |
143 | for (name, value) in attrs.items():
144 | self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
145 | self._write('>')
146 |
147 | def endElementNS(self, name, qname):
148 | self._write('%s>' % self._qname(name))
149 |
150 | def characters(self, content):
151 | self._write(escape(content))
152 |
153 | def ignorableWhitespace(self, content):
154 | self._write(content)
155 |
156 | def processingInstruction(self, target, data):
157 | self._write('%s %s?>' % (target, data))
158 |
159 |
160 | class XMLFilterBase(xmlreader.XMLReader):
161 | """This class is designed to sit between an XMLReader and the
162 | client application's event handlers. By default, it does nothing
163 | but pass requests up to the reader and events on to the handlers
164 | unmodified, but subclasses can override specific methods to modify
165 | the event stream or the configuration requests as they pass
166 | through."""
167 |
168 | def __init__(self, parent = None):
169 | xmlreader.XMLReader.__init__(self)
170 | self._parent = parent
171 |
172 | # ErrorHandler methods
173 |
174 | def error(self, exception):
175 | self._err_handler.error(exception)
176 |
177 | def fatalError(self, exception):
178 | self._err_handler.fatalError(exception)
179 |
180 | def warning(self, exception):
181 | self._err_handler.warning(exception)
182 |
183 | # ContentHandler methods
184 |
185 | def setDocumentLocator(self, locator):
186 | self._cont_handler.setDocumentLocator(locator)
187 |
188 | def startDocument(self):
189 | self._cont_handler.startDocument()
190 |
191 | def endDocument(self):
192 | self._cont_handler.endDocument()
193 |
194 | def startPrefixMapping(self, prefix, uri):
195 | self._cont_handler.startPrefixMapping(prefix, uri)
196 |
197 | def endPrefixMapping(self, prefix):
198 | self._cont_handler.endPrefixMapping(prefix)
199 |
200 | def startElement(self, name, attrs):
201 | self._cont_handler.startElement(name, attrs)
202 |
203 | def endElement(self, name):
204 | self._cont_handler.endElement(name)
205 |
206 | def startElementNS(self, name, qname, attrs):
207 | self._cont_handler.startElementNS(name, qname, attrs)
208 |
209 | def endElementNS(self, name, qname):
210 | self._cont_handler.endElementNS(name, qname)
211 |
212 | def characters(self, content):
213 | self._cont_handler.characters(content)
214 |
215 | def ignorableWhitespace(self, chars):
216 | self._cont_handler.ignorableWhitespace(chars)
217 |
218 | def processingInstruction(self, target, data):
219 | self._cont_handler.processingInstruction(target, data)
220 |
221 | def skippedEntity(self, name):
222 | self._cont_handler.skippedEntity(name)
223 |
224 | # DTDHandler methods
225 |
226 | def notationDecl(self, name, publicId, systemId):
227 | self._dtd_handler.notationDecl(name, publicId, systemId)
228 |
229 | def unparsedEntityDecl(self, name, publicId, systemId, ndata):
230 | self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata)
231 |
232 | # EntityResolver methods
233 |
234 | def resolveEntity(self, publicId, systemId):
235 | return self._ent_handler.resolveEntity(publicId, systemId)
236 |
237 | # XMLReader methods
238 |
239 | def parse(self, source):
240 | self._parent.setContentHandler(self)
241 | self._parent.setErrorHandler(self)
242 | self._parent.setEntityResolver(self)
243 | self._parent.setDTDHandler(self)
244 | self._parent.parse(source)
245 |
246 | def setLocale(self, locale):
247 | self._parent.setLocale(locale)
248 |
249 | def getFeature(self, name):
250 | return self._parent.getFeature(name)
251 |
252 | def setFeature(self, name, state):
253 | self._parent.setFeature(name, state)
254 |
255 | def getProperty(self, name):
256 | return self._parent.getProperty(name)
257 |
258 | def setProperty(self, name, value):
259 | self._parent.setProperty(name, value)
260 |
261 | # XMLFilter methods
262 |
263 | def getParent(self):
264 | return self._parent
265 |
266 | def setParent(self, parent):
267 | self._parent = parent
268 |
269 | # --- Utility functions
270 |
271 | def prepare_input_source(source, base = ""):
272 | """This function takes an InputSource and an optional base URL and
273 | returns a fully resolved InputSource object ready for reading."""
274 |
275 | if isinstance(source, str):
276 | source = xmlreader.InputSource(source)
277 | elif hasattr(source, "read"):
278 | f = source
279 | source = xmlreader.InputSource()
280 | source.setByteStream(f)
281 | if hasattr(f, "name"):
282 | source.setSystemId(f.name)
283 |
284 | if source.getByteStream() is None:
285 | sysid = source.getSystemId()
286 | basehead = os.path.dirname(os.path.normpath(base))
287 | sysidfilename = os.path.join(basehead, sysid)
288 | if os.path.isfile(sysidfilename):
289 | source.setSystemId(sysidfilename)
290 | f = open(sysidfilename, "rb")
291 | else:
292 | source.setSystemId(urllib.parse.urljoin(base, sysid))
293 | f = urllib.request.urlopen(source.getSystemId())
294 |
295 | source.setByteStream(f)
296 |
297 | return source
298 |
--------------------------------------------------------------------------------
/3부/23장/nba_2016_04.csv:
--------------------------------------------------------------------------------
1 | Date,Start (ET),Visitor/Neutral,PTS,Home/Neutral,PTS,,,Notes
2 | Wed Apr 1 2015,7:30 pm,Indiana Pacers,87,Boston Celtics,100,Box Score,,
3 | Wed Apr 1 2015,7:00 pm,Detroit Pistons,78,Charlotte Hornets,102,Box Score,,
4 | Wed Apr 1 2015,8:00 pm,Sacramento Kings,111,Houston Rockets,115,Box Score,,
5 | Wed Apr 1 2015,10:30 pm,New Orleans Pelicans,113,Los Angeles Lakers,92,Box Score,,
6 | Wed Apr 1 2015,8:00 pm,Chicago Bulls,91,Milwaukee Bucks,95,Box Score,,
7 | Wed Apr 1 2015,8:00 pm,Toronto Raptors,113,Minnesota Timberwolves,99,Box Score,,
8 | Wed Apr 1 2015,7:30 pm,Brooklyn Nets,100,New York Knicks,98,Box Score,,
9 | Wed Apr 1 2015,8:00 pm,Dallas Mavericks,135,Oklahoma City Thunder,131,Box Score,,
10 | Wed Apr 1 2015,7:00 pm,San Antonio Spurs,103,Orlando Magic,91,Box Score,,
11 | Wed Apr 1 2015,10:00 pm,Los Angeles Clippers,126,Portland Trail Blazers,122,Box Score,,
12 | Wed Apr 1 2015,9:00 pm,Denver Nuggets,84,Utah Jazz,98,Box Score,,
13 | Wed Apr 1 2015,7:00 pm,Philadelphia 76ers,93,Washington Wizards,106,Box Score,,
14 | Thu Apr 2 2015,8:00 pm,Miami Heat,88,Cleveland Cavaliers,114,Box Score,,
15 | Thu Apr 2 2015,8:30 pm,Houston Rockets,108,Dallas Mavericks,101,Box Score,,
16 | Thu Apr 2 2015,10:30 pm,Phoenix Suns,106,Golden State Warriors,107,Box Score,,
17 | Fri Apr 3 2015,7:30 pm,Milwaukee Bucks,110,Boston Celtics,101,Box Score,,
18 | Fri Apr 3 2015,7:30 pm,Toronto Raptors,109,Brooklyn Nets,114,Box Score,,
19 | Fri Apr 3 2015,8:00 pm,Detroit Pistons,82,Chicago Bulls,88,Box Score,,
20 | Fri Apr 3 2015,7:00 pm,Charlotte Hornets,74,Indiana Pacers,93,Box Score,,
21 | Fri Apr 3 2015,10:30 pm,Portland Trail Blazers,107,Los Angeles Lakers,77,Box Score,,
22 | Fri Apr 3 2015,8:00 pm,Oklahoma City Thunder,92,Memphis Grizzlies,100,Box Score,,
23 | Fri Apr 3 2015,8:00 pm,Orlando Magic,97,Minnesota Timberwolves,84,Box Score,,
24 | Fri Apr 3 2015,10:00 pm,New Orleans Pelicans,101,Sacramento Kings,95,Box Score,,
25 | Fri Apr 3 2015,8:30 pm,Denver Nuggets,93,San Antonio Spurs,123,Box Score,,
26 | Fri Apr 3 2015,7:00 pm,New York Knicks,87,Washington Wizards,101,Box Score,,
27 | Sat Apr 4 2015,7:30 pm,Brooklyn Nets,99,Atlanta Hawks,131,Box Score,,
28 | Sat Apr 4 2015,7:00 pm,Philadelphia 76ers,91,Charlotte Hornets,92,Box Score,,
29 | Sat Apr 4 2015,8:30 pm,Golden State Warriors,123,Dallas Mavericks,110,Box Score,,
30 | Sat Apr 4 2015,9:00 pm,Los Angeles Clippers,107,Denver Nuggets,92,Box Score,,
31 | Sat Apr 4 2015,7:30 pm,Miami Heat,98,Detroit Pistons,99,Box Score,,
32 | Sat Apr 4 2015,8:00 pm,Washington Wizards,92,Memphis Grizzlies,83,Box Score,,
33 | Sat Apr 4 2015,8:30 pm,Orlando Magic,97,Milwaukee Bucks,90,Box Score,,
34 | Sat Apr 4 2015,10:00 pm,Utah Jazz,85,Phoenix Suns,87,Box Score,,
35 | Sat Apr 4 2015,10:00 pm,New Orleans Pelicans,90,Portland Trail Blazers,99,Box Score,,
36 | Sat Apr 4 2015,7:30 pm,Boston Celtics,117,Toronto Raptors,116,Box Score,OT,
37 | Sun Apr 5 2015,3:30 pm,Chicago Bulls,94,Cleveland Cavaliers,99,Box Score,,
38 | Sun Apr 5 2015,6:00 pm,Miami Heat,89,Indiana Pacers,112,Box Score,,
39 | Sun Apr 5 2015,9:30 pm,Los Angeles Clippers,106,Los Angeles Lakers,78,Box Score,,
40 | Sun Apr 5 2015,7:30 pm,Philadelphia 76ers,91,New York Knicks,101,Box Score,,
41 | Sun Apr 5 2015,1:00 pm,Houston Rockets,115,Oklahoma City Thunder,112,Box Score,,
42 | Sun Apr 5 2015,9:00 pm,Utah Jazz,101,Sacramento Kings,95,Box Score,,
43 | Sun Apr 5 2015,7:00 pm,Golden State Warriors,92,San Antonio Spurs,107,Box Score,,
44 | Mon Apr 6 2015,7:00 pm,Portland Trail Blazers,96,Brooklyn Nets,106,Box Score,,
45 | Tue Apr 7 2015,7:30 pm,Phoenix Suns,69,Atlanta Hawks,96,Box Score,,
46 | Tue Apr 7 2015,10:30 pm,Los Angeles Lakers,100,Los Angeles Clippers,105,Box Score,,
47 | Tue Apr 7 2015,7:30 pm,Charlotte Hornets,100,Miami Heat,105,Box Score,,
48 | Tue Apr 7 2015,8:00 pm,Golden State Warriors,100,New Orleans Pelicans,103,Box Score,,
49 | Tue Apr 7 2015,8:00 pm,San Antonio Spurs,113,Oklahoma City Thunder,88,Box Score,,
50 | Tue Apr 7 2015,10:00 pm,Minnesota Timberwolves,111,Sacramento Kings,116,Box Score,,
51 | Wed Apr 8 2015,7:30 pm,Atlanta Hawks,114,Brooklyn Nets,111,Box Score,,
52 | Wed Apr 8 2015,7:00 pm,Toronto Raptors,92,Charlotte Hornets,74,Box Score,,
53 | Wed Apr 8 2015,9:30 pm,Phoenix Suns,104,Dallas Mavericks,107,Box Score,,
54 | Wed Apr 8 2015,9:00 pm,Los Angeles Lakers,101,Denver Nuggets,119,Box Score,,
55 | Wed Apr 8 2015,7:00 pm,Boston Celtics,113,Detroit Pistons,103,Box Score,,
56 | Wed Apr 8 2015,8:00 pm,New Orleans Pelicans,74,Memphis Grizzlies,110,Box Score,,
57 | Wed Apr 8 2015,8:00 pm,Cleveland Cavaliers,104,Milwaukee Bucks,99,Box Score,,
58 | Wed Apr 8 2015,7:30 pm,Indiana Pacers,102,New York Knicks,86,Box Score,,
59 | Wed Apr 8 2015,7:00 pm,Chicago Bulls,103,Orlando Magic,105,Box Score,,
60 | Wed Apr 8 2015,7:00 pm,Washington Wizards,119,Philadelphia 76ers,90,Box Score,,
61 | Wed Apr 8 2015,10:00 pm,Minnesota Timberwolves,91,Portland Trail Blazers,116,Box Score,,
62 | Wed Apr 8 2015,8:30 pm,Houston Rockets,98,San Antonio Spurs,110,Box Score,,
63 | Wed Apr 8 2015,9:00 pm,Sacramento Kings,91,Utah Jazz,103,Box Score,,
64 | Thu Apr 9 2015,10:30 pm,Portland Trail Blazers,105,Golden State Warriors,116,Box Score,,
65 | Thu Apr 9 2015,8:00 pm,Chicago Bulls,89,Miami Heat,78,Box Score,,
66 | Fri Apr 10 2015,7:30 pm,Charlotte Hornets,80,Atlanta Hawks,104,Box Score,,
67 | Fri Apr 10 2015,7:30 pm,Washington Wizards,80,Brooklyn Nets,117,Box Score,,
68 | Fri Apr 10 2015,7:30 pm,Boston Celtics,99,Cleveland Cavaliers,90,Box Score,,
69 | Fri Apr 10 2015,9:00 pm,Dallas Mavericks,144,Denver Nuggets,143,Box Score,2OT,
70 | Fri Apr 10 2015,7:30 pm,Indiana Pacers,107,Detroit Pistons,103,Box Score,,
71 | Fri Apr 10 2015,8:00 pm,San Antonio Spurs,104,Houston Rockets,103,Box Score,,
72 | Fri Apr 10 2015,10:30 pm,Minnesota Timberwolves,98,Los Angeles Lakers,106,Box Score,,
73 | Fri Apr 10 2015,8:00 pm,Phoenix Suns,75,New Orleans Pelicans,90,Box Score,,
74 | Fri Apr 10 2015,7:30 pm,Milwaukee Bucks,99,New York Knicks,91,Box Score,,
75 | Fri Apr 10 2015,8:00 pm,Sacramento Kings,103,Oklahoma City Thunder,116,Box Score,,
76 | Fri Apr 10 2015,7:00 pm,Toronto Raptors,101,Orlando Magic,99,Box Score,,
77 | Fri Apr 10 2015,9:00 pm,Memphis Grizzlies,89,Utah Jazz,88,Box Score,,
78 | Sat Apr 11 2015,8:00 pm,Philadelphia 76ers,107,Chicago Bulls,114,Box Score,,
79 | Sat Apr 11 2015,10:30 pm,Minnesota Timberwolves,101,Golden State Warriors,110,Box Score,,
80 | Sat Apr 11 2015,10:00 pm,Memphis Grizzlies,86,Los Angeles Clippers,94,Box Score,,
81 | Sat Apr 11 2015,7:30 pm,Toronto Raptors,107,Miami Heat,104,Box Score,,
82 | Sat Apr 11 2015,7:00 pm,New York Knicks,80,Orlando Magic,79,Box Score,,
83 | Sat Apr 11 2015,10:00 pm,Utah Jazz,111,Portland Trail Blazers,105,Box Score,,
84 | Sun Apr 12 2015,3:00 pm,Cleveland Cavaliers,78,Boston Celtics,117,Box Score,,
85 | Sun Apr 12 2015,5:00 pm,Sacramento Kings,111,Denver Nuggets,122,Box Score,,
86 | Sun Apr 12 2015,3:30 pm,Charlotte Hornets,77,Detroit Pistons,116,Box Score,,
87 | Sun Apr 12 2015,7:00 pm,New Orleans Pelicans,114,Houston Rockets,121,Box Score,,
88 | Sun Apr 12 2015,6:00 pm,Oklahoma City Thunder,104,Indiana Pacers,116,Box Score,,
89 | Sun Apr 12 2015,9:30 pm,Dallas Mavericks,120,Los Angeles Lakers,106,Box Score,,
90 | Sun Apr 12 2015,3:00 pm,Brooklyn Nets,73,Milwaukee Bucks,96,Box Score,,
91 | Sun Apr 12 2015,7:00 pm,Phoenix Suns,91,San Antonio Spurs,107,Box Score,,
92 | Sun Apr 12 2015,6:00 pm,Atlanta Hawks,99,Washington Wizards,108,Box Score,,
93 | Mon Apr 13 2015,7:30 pm,New York Knicks,112,Atlanta Hawks,108,Box Score,,
94 | Mon Apr 13 2015,7:30 pm,Chicago Bulls,113,Brooklyn Nets,86,Box Score,,
95 | Mon Apr 13 2015,7:00 pm,Houston Rockets,100,Charlotte Hornets,90,Box Score,,
96 | Mon Apr 13 2015,7:00 pm,Detroit Pistons,97,Cleveland Cavaliers,109,Box Score,,
97 | Mon Apr 13 2015,10:30 pm,Memphis Grizzlies,107,Golden State Warriors,111,Box Score,,
98 | Mon Apr 13 2015,10:30 pm,Denver Nuggets,103,Los Angeles Clippers,110,Box Score,,
99 | Mon Apr 13 2015,7:30 pm,Orlando Magic,93,Miami Heat,100,Box Score,,
100 | Mon Apr 13 2015,8:00 pm,New Orleans Pelicans,100,Minnesota Timberwolves,88,Box Score,,
101 | Mon Apr 13 2015,8:00 pm,Portland Trail Blazers,90,Oklahoma City Thunder,101,Box Score,,
102 | Mon Apr 13 2015,7:00 pm,Milwaukee Bucks,107,Philadelphia 76ers,97,Box Score,,
103 | Mon Apr 13 2015,10:00 pm,Los Angeles Lakers,92,Sacramento Kings,102,Box Score,,
104 | Mon Apr 13 2015,9:00 pm,Dallas Mavericks,92,Utah Jazz,109,Box Score,,
105 | Tue Apr 14 2015,7:30 pm,Toronto Raptors,93,Boston Celtics,95,Box Score,,
106 | Tue Apr 14 2015,8:00 pm,Washington Wizards,95,Indiana Pacers,99,Box Score,2OT,
107 | Tue Apr 14 2015,10:30 pm,Los Angeles Clippers,112,Phoenix Suns,101,Box Score,,
108 | Wed Apr 15 2015,8:00 pm,Orlando Magic,88,Brooklyn Nets,101,Box Score,,
109 | Wed Apr 15 2015,8:00 pm,Atlanta Hawks,85,Chicago Bulls,91,Box Score,,
110 | Wed Apr 15 2015,8:00 pm,Washington Wizards,108,Cleveland Cavaliers,113,Box Score,OT,
111 | Wed Apr 15 2015,8:00 pm,Portland Trail Blazers,98,Dallas Mavericks,114,Box Score,,
112 | Wed Apr 15 2015,10:30 pm,Denver Nuggets,126,Golden State Warriors,133,Box Score,,
113 | Wed Apr 15 2015,8:00 pm,Utah Jazz,91,Houston Rockets,117,Box Score,,
114 | Wed Apr 15 2015,10:30 pm,Sacramento Kings,122,Los Angeles Lakers,99,Box Score,,
115 | Wed Apr 15 2015,9:30 pm,Indiana Pacers,83,Memphis Grizzlies,95,Box Score,,
116 | Wed Apr 15 2015,8:00 pm,Boston Celtics,105,Milwaukee Bucks,100,Box Score,,
117 | Wed Apr 15 2015,8:00 pm,Oklahoma City Thunder,138,Minnesota Timberwolves,113,Box Score,,
118 | Wed Apr 15 2015,8:00 pm,San Antonio Spurs,103,New Orleans Pelicans,108,Box Score,,
119 | Wed Apr 15 2015,8:00 pm,Detroit Pistons,112,New York Knicks,90,Box Score,,
120 | Wed Apr 15 2015,8:00 pm,Miami Heat,105,Philadelphia 76ers,101,Box Score,,
121 | Wed Apr 15 2015,7:00 pm,Charlotte Hornets,87,Toronto Raptors,92,Box Score,,
122 | Sat Apr 18 2015,7:00 pm,Milwaukee Bucks,91,Chicago Bulls,103,Box Score,,
123 | Sat Apr 18 2015,3:30 pm,New Orleans Pelicans,99,Golden State Warriors,106,Box Score,,
124 | Sat Apr 18 2015,9:30 pm,Dallas Mavericks,108,Houston Rockets,118,Box Score,,
125 | Sat Apr 18 2015,12:30 pm,Washington Wizards,93,Toronto Raptors,86,Box Score,OT,
126 | Sun Apr 19 2015,5:30 pm,Brooklyn Nets,92,Atlanta Hawks,99,Box Score,,
127 | Sun Apr 19 2015,3:00 pm,Boston Celtics,100,Cleveland Cavaliers,113,Box Score,,
128 | Sun Apr 19 2015,10:30 pm,San Antonio Spurs,92,Los Angeles Clippers,107,Box Score,,
129 | Sun Apr 19 2015,8:00 pm,Portland Trail Blazers,86,Memphis Grizzlies,100,Box Score,,
130 | Mon Apr 20 2015,8:00 pm,Milwaukee Bucks,82,Chicago Bulls,91,Box Score,,
131 | Mon Apr 20 2015,10:30 pm,New Orleans Pelicans,87,Golden State Warriors,97,Box Score,,
132 | Tue Apr 21 2015,7:00 pm,Boston Celtics,91,Cleveland Cavaliers,99,Box Score,,
133 | Tue Apr 21 2015,9:30 pm,Dallas Mavericks,99,Houston Rockets,111,Box Score,,
134 | Tue Apr 21 2015,8:00 pm,Washington Wizards,117,Toronto Raptors,106,Box Score,,
135 | Wed Apr 22 2015,7:00 pm,Brooklyn Nets,91,Atlanta Hawks,96,Box Score,,
136 | Wed Apr 22 2015,10:30 pm,San Antonio Spurs,111,Los Angeles Clippers,107,Box Score,OT,
137 | Wed Apr 22 2015,8:00 pm,Portland Trail Blazers,82,Memphis Grizzlies,97,Box Score,,
138 | Thu Apr 23 2015,7:00 pm,Cleveland Cavaliers,103,Boston Celtics,95,Box Score,,
139 | Thu Apr 23 2015,8:00 pm,Chicago Bulls,113,Milwaukee Bucks,106,Box Score,2OT,
140 | Thu Apr 23 2015,9:30 pm,Golden State Warriors,123,New Orleans Pelicans,119,Box Score,OT,
141 | Fri Apr 24 2015,7:00 pm,Houston Rockets,130,Dallas Mavericks,128,Box Score,,
142 | Fri Apr 24 2015,9:30 pm,Los Angeles Clippers,73,San Antonio Spurs,100,Box Score,,
143 | Fri Apr 24 2015,8:00 pm,Toronto Raptors,99,Washington Wizards,106,Box Score,,
144 | Sat Apr 25 2015,3:00 pm,Atlanta Hawks,83,Brooklyn Nets,91,Box Score,,
145 | Sat Apr 25 2015,5:30 pm,Chicago Bulls,90,Milwaukee Bucks,92,Box Score,,
146 | Sat Apr 25 2015,8:00 pm,Golden State Warriors,109,New Orleans Pelicans,98,Box Score,,
147 | Sat Apr 25 2015,10:30 pm,Memphis Grizzlies,115,Portland Trail Blazers,109,Box Score,,
148 | Sun Apr 26 2015,1:00 pm,Cleveland Cavaliers,101,Boston Celtics,93,Box Score,,
149 | Sun Apr 26 2015,9:00 pm,Houston Rockets,109,Dallas Mavericks,121,Box Score,,
150 | Sun Apr 26 2015,3:30 pm,Los Angeles Clippers,114,San Antonio Spurs,105,Box Score,,
151 | Sun Apr 26 2015,6:30 pm,Toronto Raptors,94,Washington Wizards,125,Box Score,,
152 | Mon Apr 27 2015,7:00 pm,Atlanta Hawks,115,Brooklyn Nets,120,Box Score,OT,
153 | Mon Apr 27 2015,8:00 pm,Milwaukee Bucks,94,Chicago Bulls,88,Box Score,,
154 | Mon Apr 27 2015,10:30 pm,Memphis Grizzlies,92,Portland Trail Blazers,99,Box Score,,
155 | Tue Apr 28 2015,8:00 pm,Dallas Mavericks,94,Houston Rockets,103,Box Score,,
156 | Tue Apr 28 2015,10:30 pm,San Antonio Spurs,111,Los Angeles Clippers,107,Box Score,,
157 | Wed Apr 29 2015,7:00 pm,Brooklyn Nets,97,Atlanta Hawks,107,Box Score,,
158 | Wed Apr 29 2015,9:30 pm,Portland Trail Blazers,93,Memphis Grizzlies,99,Box Score,,
159 | Thu Apr 30 2015,7:00 pm,Chicago Bulls,120,Milwaukee Bucks,66,Box Score,,
160 | Thu Apr 30 2015,9:30 pm,Los Angeles Clippers,102,San Antonio Spurs,96,Box Score,,
--------------------------------------------------------------------------------