2021年2月20日 星期六

Python 的基本資料型態(二)

群集型態:
  • Python 支援多種資料結構,包含清單(list)、集合(set)、字典(dict)、Tuple !
  • 每一種資料結構有其使用方式,請注意其不同的使用條件!
清單(list):
  1. 清單(list):其使用特性為有序、具備索引值、內容長度可變動,使用 [] 符號,內容使用逗號區隔!
    • 範例 1:
      #python3
      Python 3.8.3 (default, Aug 31 2020, 16:03:14) 
      [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> names = ['Peter', 'James', 'Mary']
      >>> names
      ['Peter', 'James', 'Mary']
      >>> print(names)
      ['Peter', 'James', 'Mary']
      >>> print(names[0])
      Peter
      
  2. 可使用 append()、extend()、pop()、remove()、reverse()、sort() 等方法,進行資料結構的調整!
    • 範例 2:
      >>> names = ['Peter', 'James', 'Mary']
      >>> names
      ['Peter', 'James', 'Mary', 'D', 'a', 'v', 'i', 'd']
      >>> names.remove('D')
      >>> names
      ['Peter', 'James', 'Mary', 'a', 'v', 'i', 'd']
      >>> names.remove(names[3])
      >>> names
      ['Peter', 'James', 'Mary', 'v', 'i', 'd']
      >>> names.remove(names[3-4])
      >>> names
      ['Peter', 'James', 'Mary', 'v', 'i']
       (註:因為 3-4 = -1,所以是由後往前數一個欄位)
      >>> names.pop(1)
      'James'
      >>> names
      ['Peter', 'Mary', 'v', 'i']
      (註:pop() 是指要提出哪一個欄位的值!提出後,不寫回原欄位!)
      >>> names.sort()
      >>> names
      ['Mary', 'Peter', 'i', 'v']
      >>> names.append('James')
      >>> names
      ['Mary', 'Peter', 'i', 'v', 'James']
      >>> names.reverse()
      >>> names
      ['James', 'v', 'i', 'Peter', 'Mary']
      >>> names.extend(['David','Poll'])
      >>> names
      ['James', 'v', 'i', 'Peter', 'Mary', 'David', 'Poll']
      
集合(set):
  1. 集合(set):其特色為內容是無序但不重複!使用{}符號包含元素,元素間使用逗號分隔!
    • 範例 1:
      #python3
      Python 3.8.3 (default, Aug 31 2020, 16:03:14) 
      [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>>
      >>> names = set()
      >>> names
      set()
      >>> names.add('Peter')
      >>> names
      {'Peter'}
      >>> names = {'Peter','Mary'}
      >>> names
      {'Mary', 'Peter'}
      (註:集合會自動排序)
      >>> names.add('Peter')
      >>> names
      {'Mary', 'Peter'}
      >>> names.remove('Peter')
      >>> names
      {'Mary'}
      >>> 'Peter' in names
      False
      
字典(dict):
  1. 字典(dict):成雙成對的 key-value 架構!
    • 範例 1:
      #python3
      Python 3.8.3 (default, Aug 31 2020, 16:03:14) 
      [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>>
      >>> userpws = {'Peter':32123 , 'David':'abc123'}
      >>> userpws['David']
      'abc123'
      >>> userpws['Peter']
      32123
      >>> userpws['Mary'] = 987654
      >>> userpws
      {'Peter': 32123, 'David': 'abc123', 'Mary': 987654}
      >>> del userpws['David']
      >>> userpws
      {'Peter': 32123, 'Mary': 987654}
      >>> 'Mary' in userpws
      True
      >>> userpws['Mary'] = 12345
      >>> userpws
      {'Peter': 32123, 'Mary': 12345}
      >>> userpws.get('David')
      >>> userpws.get('David')  == None
      True
      >>> userpws.get('David', 'ABC123')
      'ABC123'
      >>> userpws
      {'Peter': 32123, 'Mary': 12345}
      (註:get 只是暫存資料!)
      >>> list(userpws.items())
      [('Peter', 32123), ('Mary', 12345)]
      >>> list(userpws.keys())
      ['Peter', 'Mary']
      >>> list(userpws.values())
      [32123, 12345]
      
      
Tuple(tuple):
  1. Tuple(tuple):有序的資料結構,使用[]指定索引元素!建立之後,就無法變動!
    • 範例 1:
      #python3
      Python 3.8.3 (default, Aug 31 2020, 16:03:14) 
      [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>>
      >>> 10,
      (10,)
      >>> 10,20,30
      (10, 20, 30)
      >>> acct = 1, 'Peter', True
      >>> acct
      (1, 'Peter', True)
      >>> type(acct)
      <class 'tuple'>
      >>> print(acct[1])
      Peter
      >>> acct[1] = "Mary"
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: 'tuple' object does not support item assignment
      >>> id, names, access = acct
      >>> names
      'Peter'
      >>> access, names, id = id, names, access
      >>> access
      1
      (註:用來置換變數值內容)
      >>> a, *b, c = [1,2,3,4,5]
      >>> b
      [2, 3, 4]
      

Python 學習路徑目錄與資料索引

Python 學習路徑目錄
  1. Python 環境安裝與開發工具設定
  2. Python 資料型態
  3. Python 基本程式寫作
  4. Python 類別與物件的使用
以專案開發為觀點的學習路徑(一)
  1. Python 環境安裝與開發工具設定
    • 工具與基本語法請參考上述項目
    • 以下項目以開發桌遊-簡版大富翁遊戲為開發
  2. 開發之初
  3. 開始進行程式撰寫
以專案開發為觀點的學習路徑(二)
  1. 本次專案項目:Line Bot 聊天機器人的設計
    • 了解 Line Bot 聊天機器人的設計原理
    • 學習 Python 網路程式的設計與運作原理
  2. 事前的原理研究與準備工作
  3. 開始進行專案:
參考文獻

2021年2月17日 星期三

Python3 環境設定(一)

設定目標:
  • 在 CentOS 8 上安裝 Python 3.8
  • 在 CentOS 8 上使用 VSCode 做為 Python IDE 環境
快速操作流程:
  1. 在 CentOS 8 安裝 Python 3.8 版本:
    #dnf install epel-release
    #dnf install python38
    #alternatives --config python3
    (選擇第二個 python3.8 的選項)
    #python3 --version
    
  2. 安裝 VSCode :
    #dnf install code
    
  3. 使用一般使用者來啟動 VSCode:
    $code &
    
  4. 在 VSCode 中,選擇 Python 解譯器:
    a. <Ctrl>+<Shift>+<p>
    b.輸入並執行 Python: Select Interpreter

    c.在選單中選擇 python 3.8.3

    d.視窗左下方的紫色工作列,為 Python 解譯器選擇按鍵,按下即可更換解譯器:
  5. 建立一個 Python 檔案:hello.py
    a.按下工作列上的 File -> NewFile
    b.輸入程式內容:
    msg = "Hello World"
    print(msg)
    

    c.按下工作列上的 File -> Save As ...
    d.選擇儲存的目錄,並且輸入檔案名稱:hello.py
    e.按下右上方綠色箭頭按鍵,即可看到執行結果!
參考文獻:
  • https://djangogirlstaipei.gitbooks.io/django-girls-taipei-tutorial/content/django/admin.html
  • http://dokelung-blog.logdown.com/posts/220832-django-notes-6-manage-your-system-admin

2016年12月12日 星期一

Python 好用的網路資源


  • https://pypi.python.org/pypi/sshtunnel

2016年8月8日 星期一

使用 Django 後台管理程式 Admin

設定目標:
  • 利用 Django 的後台管理程式,進行站台管理
快速操作流程:
  1. 先移植一個管理樣板進到 Admin 工具內:
    #python manage.py migrate
    
  2. 利用 createsuperuser 指令,建立一個 super user 帳號:
    # ./manager.py createsuperuser
    
  3. 啟動 Django Web 站台:
    #./manage.py runserver 192.168.5.104:8081
    
  4. 利用瀏覽器登入後台系統:
    #firefox http://192.168.5.104:8081/admin
    
參考文獻:
  • https://djangogirlstaipei.gitbooks.io/django-girls-taipei-tutorial/content/django/admin.html
  • http://dokelung-blog.logdown.com/posts/220832-django-notes-6-manage-your-system-admin

利用 supervisor 來管理 Django 的程序

設定目標:
  • 安裝 supervisor 來進行 Django 程序管理
  • 為配合 nginx ,請參考這一篇安裝好 Nginx 以及 Django
快速安裝流程:
  1. 利用 pip 工具,安裝 supervisor 套件:
    #pip install supervisor
    
  2. 利用 supervisor 工具,產生 supervisor 設定檔:
    #echo_supervisord_conf > /etc/supervisord.conf
    
  3. 編輯 supervisor 設定檔:
    #vim /etc/supervisord.conf
    (在檔案最尾端,加上下列幾行設定:)
    [program:mysite]
    command=/bin/uwsgi --http :8081 --chdir /usr/share/nginx/html/mysite --module mysite.wsgi
    directory=/usr/share/nginx/html/mysite
    startsecs=0
    stopwaitsecs=0
    autostart=true
    autorestart=true
    
  4. 啟動 supervisor 服務:
    #supervisord -c /etc/supervisord.conf
    
  5. 動新啟動 mysite 站台運作:
    #supervisorctl -c /etc/supervisord.conf restart mysite
    
    ※其他操作 supervisorctl 的指令:supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
  6. 開啟防火牆設定:
    #firewall-cmd --permanent --add-port=8081/tcp
    #firewall-cmd --reload
    
  7. 利用 ini 文件來結合 nginx 運作:
    #vim /usr/share/nginx/html/mysite/mysite.ini
    [uwsgi]
    socket = /tmp/mysite.sock
    chdir=/usr/share/nginx/html/mysite
    wsgi-file = mysite/wsgi.py
    touch-reload=/usr/share/nginx/html/mysite/reload
    
    processes = 1
    threads = 2
    
    chmod-socket = 664
    chown-socket=nginx:nginx
    
  8. 重新編輯 supervisor 設定檔:
    #vim /etc/supervisord.conf
       :
       :(略過...)
    [program:mysite]
    command=/bin/uwsgi --ini /usr/share/nginx/html/mysite/mysite.ini
    directory=/usr/share/nginx/html/mysite
    startsecs=0
    
  9. 動新啟動 mysite 站台運作:
    #supervisorctl -c /etc/supervisord.conf restart mysite
    
  10. 在 Nginx 上,新增 mysite 設定檔:
    #vim /etc/nginx/conf.d/mysite.conf
    server {
        listen      8081;
        server_name 192.168.5.104;
        charset     utf-8;
    
        client_max_body_size 75M;
    
        location /media  {
            alias /usr/share/nginx/html/mysite/media;
        }
    
        location /static {
            alias /usr/share/nginx/html/mysite/static;
        }
        location / {
            uwsgi_pass  unix:///tmp/mysite.sock;
            include     /etc/nginx/uwsgi_params;
        }
    }
    
    
  11. 重新啟動 Nginx :
    #systemctl restart nginx
    

參考文獻:
  • http://www.aahyhaa.com/archives/587
  • http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
  • https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7
  • https://www.restran.net/2015/04/09/centos-uwsgi-nginx-django/

2016年8月4日 星期四

Django 程式開發初體驗

快速開發與測試:
  1. 修改 urls.py 檔案內容:
    #vim urls.py
        :
    from views import here  #加入此行
        :
        :
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^here/$', here),  #加入這一行
    ]
    
    
  2. 新增 views.py 檔案:
    #vim views.py
    # -*- coding: utf-8 -*- 
    from django.http import HttpResponse
    
    def here(request):
        return HttpResponse("Hello World!")
    
    
  3. 啟動 django 站台:
    #../manage.py runserver 192.168.5.104:8080
    
  4. 使用 firefox 開啟網頁:
    #firefox http://192.168.5.104:8080/here/