Python

@fd-python

Public team

Community (0)
No community contribution yet

Joined on Feb 24, 2021

  • 因不支援副檔名為xmls的檔案,故須透過 offiece 轉成xls,也不能直接從檔名改,否則一樣不支援 Ref. Localizable.strings2Excel
     Like  Bookmark
  • Ref. Python内置库:configparser(INI格式配置文件解析)
     Like  Bookmark
  • Ref. 【python的異常處理】異常的捕捉
     Like  Bookmark
  • 調整了範例,讓 timer t 可在任意位置取消 timer 的運作,指令 t.cancel() import threading # global t t = None def set_interval(func, sec): global t
     Like  Bookmark
  • Ref. Python 呼叫其他程式
     Like  Bookmark
  • sample code import configparser import os conf_directory = "conf" config_file_path = os.path.join('.', conf_directory + '/sysConn.cfg').replace('\\', '/') config = configparser.ConfigParser() config.read(config_file_path)
     Like  Bookmark
  • 建立 ConfigParser 物件時加上參數 allow_no_value=True config = configparser.ConfigParser(comment_prefixes='/', allow_no_value=True) Ref. python修改ini保留注释_更新INI文件而不删除注释
     Like  Bookmark
  • 示範如何寫入 .cfg 檔 import configparser import os import logging class SysConnConfigUtil: _instance = None FILE_NAME = "sysConn.cfg" conf_directory = "conf"
     Like  Bookmark
  • hasattr(類別實例, 類別成員名稱) Ref. How to check if an attribute exists in an object in Python
     Like  Bookmark
  • import socket def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) ip = s.getsockname()[0] s.close() return ip print(get_ip())
     Like  Bookmark
  • import re string = '123456789abcdefg' re.findall(r'.{3}', string) ['123', '456', '789', 'abc', 'def'] Ref. python 按照固定长度分割字符串的方法小结
     Like  Bookmark
  • # create outer class class Doctors: def __init__(self): self.name = 'Doctor' self.den = self.Dentist() self.car = self.Cardiologist() def show(self): print('In outer class') print('Name:', self.name)
     Like  Bookmark
  • 另外實作客製的new() 來建立物件, 切記要 return self class People: self.name = "" self.age = 0 def __init__(self): pass def new(self, name, age):
     Like  Bookmark
  • fruits = {"apple":"red", "banana":"yellow"} for i, key in enumerate(fruits): print (i, key) output 0 apple 1 banana
     Like  Bookmark
  • 實體方法(Instance Method) 定義方法沒有加任何裝飾詞 (Decorator) 一定要包含 self 參數,self 表示類別物件本身 透過self 參數可以自由的存取物件屬性及其他方法,藉此來改變物件的狀態 類別方法(Class Method) 定義方法之前會有 @classmethod 裝飾詞 一定要包含 cls 參數,表示類別自己本身
     Like  Bookmark
  • with 適用於對資源進行存取的時機,使用 with 的話,檔案使用完之後就會自動關閉,也確保存取發生異常時,會執行後續的 error 操作。 EX:文件使用后自动关闭/线程中锁的自动获取和释放等。 雖然也可以透過以下 try-except 來達成一樣的目的,但比較冗長,還需要另外寫 finally: try: f = open("1.txt") except: print('fail to open') exit(-1)
     Like  Bookmark
  • Ref. Creating and Viewing HTML Files with Python
     Like  Bookmark
  • Python 修改全域變數時不像其他程式語言一樣直覺,必須再加上關鍵字,如下: 修改全域變數,必須在全域變數前面加關鍵字 global,不加關鍵字會報錯。 a = 10 def test(): global a # 一定要加 global 關鍵字 a = a + 1 print(a) test()
     Like  Bookmark
  • Ref. Python之Websocket介紹與實作 python 簡易 websocket server
     Like  Bookmark
  • Ref. Python switch/case语句实现方法
     Like  Bookmark