--Awesome-Python--

파이썬에 대한 간단한 튜토리얼 + 파이썬에 관한 예제 모아두기

GPL-3.0 License

Stars
194

Awesome-Python

Interpreter, Object-Oriented, Dynamically Typed, Iteractive language

https://www.python.org/

Python

  • ,
  • : ,
  • : , (type decoration)
  • Scope : ,
  • :
    •  lambda  
      
  • :
  • : class
  • : Reference counting
  • : PEP8 , Python
    • for statement foreach
    • chaining (a < b < c)
    • switch
    • ,  (and, or, not)
      
    • syntactic sugar (unpack, enumeration, comprehension )
    •    JavaScript 
      
    • Python Python , Python
    • , 'Pythonic'
    • ' ' , 'Best Practice'
    • (.NET IronPython, call stack Stackless Python, JVM Jython, PyPy )

https://github.com/vinta/awesome-python

       .      ,      .   ''       .     ,         ' '         .  C++      , Python      .        ,         .   '   ' .
  • (Effective Python)
  • ([The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/))
    

' ' ''

,     .         ,       .   '   Python '    ,    code snippet     , '       '          .        ,    list comprehension  '   '   .    ,          .

, . Python , ' ' .

, , , , , ' ' , . 'Python ' 'Python ' . + + virtualenv + (setup.py, requirements.txt) + :)

JavaScript npm , Python pip .

$ pip --version

pip install pip uninstall , . pip freeze pip . , .

$ pip uninstall -y $(pip freeze)

Linux Linux (MacOS ), python2 python3 pip pip2(pip) pip3 .

$ pip -V
pip 10.0.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
$ pip3 -V
pip 9.0.3 from /Users/planb/Library/Python/3.6/lib/python/site-packages (python 3.6)

virtual enviroment

virtual environment ** Python ** . ' ?' , 3 ( A Flask 0.12, B Flask 1.0.1 ), numpy, pytz, six ' ' . virtualenv , pip .

$ pip install virtualenv

virtualenv .

$ virtualenv .venv

virtualenv [venv_name] . .venv , virtualenv Python . Python , --python -p Python .

$ virtualenv -p python3 .venv
$ virtualenv --python python2.7 .venv
 .
// MacOS, Linux
$ source .venv/bin/activate

// Windows
$ .venv\Scripts\activate.bat

, .

(.venv) jomingyuui-MacBook-Pro:temp planb$

pip install ... , . .

$ deactivate

Python setup.py requirements.txt . ** ** ** ** .

setup.py

setup.py ** ** .

from setuptools import setup

setup(
    name="MyLibrary",
    version="1.0",
    install_requires=[
        "requests",
        "bcrypt",
    ],
    # ...
)

URL . (abstract dependencies) .

requirements.txt

requirements.txt ** **( ) . . pip requirements , . requirements.txt .

requests==1.2.0
bcrypt==1.0.2

setup.py , . ** ** . pip freeze requirements.txt , pigar .

$ pigar
$ pip3 install -r requirements.txt
 . pigar `-P`  .
$ pigar -P [PATH]

pigar **pigar site-packages ** , virtual environment environment pigar . , Flask 0.12 , virtual environment Flask 1.0.2 pigar requirements.txt Flask==0.12 .

Python , . unittest, pytest, doctest, tox, nose .

  • unittest : Python , . unittest.TestCase , / setUp() tearDown() . Assertion .
import unittest

def fun(x):
    return x + 1

class SomeTest(unittest.TestCase):
    def test(self):
        self.assertEqual(fun(3), 4)
  • doctest : docstring Python , .
def square(x):
    """
    >>> square(2)
    4
    >>> square(-2)
    4
    """

    return x * x

if __name__ == '__main__':
    import doctest
    doctest.testmod()
  • pytest : . , pip . unittest , . , , .
def fun(x):
    return x + 1

def test_fun_answer():
    assert func(3) == 5

py.test .

> py.test
=========================== test session starts ============================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
collecting ... collected 1 items

test_sample.py F

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_fun_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
========================= 1 failed in 0.02 seconds =========================

  • nose : unittest . , test suite . xUnit , .
$ pip3 install nose
$ python3 -m "nose" tests/

Python3 python3 -m "nose" nosetests , Python2 nosetests . , --with-coverage .

$ python3 -m "nose" --with-coverage tests/
`.coverage`  .         `--verbose`   .
> python3 -m "nose" --verbose tests/
testCreationFailure_alreadyExists (Server.tests.transplant_class.<locals>.C) ... ok
testCreationSuccess (Server.tests.transplant_class.<locals>.C) ... ok
testForbidden (Server.tests.transplant_class.<locals>.C) ... ok
...
  • tox : . tox tox.ini .
[tox]
envlist = py27,py35,py36,pypy

[testenv]
commands =
    pip3 install -r requirements.txt
    pytest          # or any other test runner that you might use

tox , tox.ini .

$ pip3 install tox
$ tox

              ,   . (readability)   ,                   .

      (PEP8 PEP20  ) Pythonic  .      ''  ,        ,      .     (a foolish consistency is the hobgoblin of little minds) .

PEP8( )

PEP8 , , . , PEP8 . PEP8 . . PEP8 , pep8 .

$ pip install pep8
$ pep8 sample.py
sample.py:69:11: E401 multiple imports on one line
sample.py:77:1: E302 expected 2 blank lines, found 1
sample.py:88:5: E301 expected 1 blank line, found 0
sample.py:222:32: W602 deprecated form of raising exception
sample.py:544:21: W601 .has_key() is deprecated, use 'in'

PEP20( )

PEP20 . import this . 20 19 , .

>>> import this

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
...
     ['import this and Zen of Python'](https://www.wefearchange.org/2010/06/import-this-and-zen-of-python.html)   .