/py-practice

Primary LanguageJupyter Notebook

Compare Python with Node.js

all Node.js knowledge is in ()

basic

  • list (array)

    • method:insert() /pop() / 直接赋值update

    namelist = ['Deng', 'Wei'] namelist[0] 'Deng' namelist[1] 'Wei' namelist[2] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range namelist[-1] 'Wei'

  • tuple (more than const array --- Object.freeze)

    nametuple=('Deng','Wei') nametuple[0] 'Deng' nametuple[2] Traceback (most recent call last): File "", line 1, in IndexError: tuple index out of range nametuple[-1] 'Wei' nametuple[-1]='Wei2' Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment

  • dict (json key-value)

    • method:pop() /直接赋值update/赋值新的key value

    IDNamedict={'001':'dengwei','002':'david','003':'wade'} IDNamedict['001'] 'dengwei' IDNamedict['004'] Traceback (most recent call last): File "", line 1, in KeyError: '004' IDNamedict['004']='xxx'

  • set(Set)

    • method:add() /del()
  • generator(generator)

  • isinstance/type(type of)

    isinstance(namelist,list) True isinstance(namelist,tuble) Traceback (most recent call last): File "", line 1, in NameError: name 'tuble' is not defined isinstance(namelist,tuple) False type(namelist) <class 'list'> type(namelist) is list True

class

  • inheritance(father) (vs extends)

    class Bird(Animal): pass

  • multiple inheritance(mixin: Object.assign(obj.prototype,mixin mixwith.js))

    class MyUDPServer(UDPServer, ThreadingMixIn): pass

package install

  • https://pypi.python.org (npmjs.org)
  • install command: easy_install/pip (npm )
  • pip3 install -r requirements.txt (npm i)
    • pip3 install gunicorn (npm install lodash)

unittest

  • package
    • unitest(mocha) -- internal package

      • run command
        • $python3 test_xx.py
          • 留意
            • 名字命名要 以test_ 开头,class 内的函数 以 test_xx 开头
            • class TestXXX(unittest.TestCase)
        • $python3 -m unittest discover test # test 目录自发现
        • mock(nock)
      	from mock import MagicMock
      	thing = ProductionClass()
      	thing.method = MagicMock(return_value=3)
      	self.assertEqual(thing.method(), 3)
      
    • py.test(mocha) -- 3rd testing framework

decorator

  • some only in ts

important internal package

import 3rd package

data processing

  • package
    • pydash(lodash)
    • panda(more than mathjs)

async

  • async/await(async/await)
    • package
      • asyncio
      • aiohttp
      • threading

db

  • package
    • mysql-connector-python/mysql-connector(sequelize)
    • pymongo(mongoose)

web framework:

  • flask(expressjs)
    • demo
    • FLASK_DEBUG=1 FLASK_APP=hello-world.py flask run (node-dev/nodemon)# auto reload

from python2.7 to python3 causion: may need to handle somelib version change

`
	2to3 2.py -w 
`

property annotation

```
    class Human():
        def __init__(self):
            pass 

        @property 
        def name(self):
            return "Wade"

```

相当于为了实现作为属性的访问:

```
    h =  Human()
    #print(h.name()) #instead of this
	print(h.name)
```