自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 代碼實(shí)踐小結(jié)

大數(shù)據(jù)
傳遞的數(shù)據(jù)結(jié)構(gòu)如何考慮(是否對(duì)調(diào)用方有先驗(yàn)知識(shí)的要求,比如返回一個(gè) Tuple,則需要用戶了解 tuple 中元素的順序,這樣情況是否應(yīng)該進(jìn)行封裝;),數(shù)據(jù)結(jié)構(gòu)定義清楚了,很多東西也就清楚了。

最近寫了較多的 Python 腳本,將最近自己寫的腳本進(jìn)行一個(gè)總結(jié),其中有些是 Python 獨(dú)有的,有些是所有程序設(shè)計(jì)中共有的:

考慮使用 Logger(logger 怎么配置,需要輸出哪些信息 — 可以反向考慮,自己看到這個(gè) logger 的時(shí)候想了解什么信息)

傳遞的數(shù)據(jù)結(jié)構(gòu)如何考慮(是否對(duì)調(diào)用方有先驗(yàn)知識(shí)的要求,比如返回一個(gè) Tuple,則需要用戶了解 tuple 中元素的順序,這樣情況是否應(yīng)該進(jìn)行封裝;),數(shù)據(jù)結(jié)構(gòu)定義清楚了,很多東西也就清楚了。

如何操作數(shù)據(jù)庫(kù)(可以學(xué)習(xí) sqlalchemy,包括 core 和 orm 兩種 api)

異常如何處理(異常應(yīng)該分開捕獲 — 可以清楚的知道什么情況下導(dǎo)致的,異常之后應(yīng)該打印日志說(shuō)明出現(xiàn)什么問(wèn)題,如果情況惡劣需要進(jìn)行異常再次拋出或者報(bào)警)

所有獲取資源的地方都應(yīng)該做 check(a. 沒(méi)有獲取到會(huì)怎么辦;b.獲取到異常的怎么辦)

所有操作資源的地方都應(yīng)該檢查是否操作成功

每個(gè)函數(shù)都應(yīng)該簡(jiǎn)短,如果函數(shù)過(guò)長(zhǎng)應(yīng)該進(jìn)行拆分(有個(gè)建議值,函數(shù)包含的行數(shù)應(yīng)該在 20-30 行之間,具體按照這個(gè)規(guī)范做過(guò)一次之后就會(huì)發(fā)現(xiàn)這樣真好)

使用 class 之后,考慮重構(gòu) __str__ 函數(shù),用戶打印輸出(如果不實(shí)現(xiàn) __str__,會(huì)調(diào)用 __repr__ ),如果對(duì)象放到 collection 中之后,需要實(shí)現(xiàn) __repr__ 函數(shù),用于打印整個(gè) collection 的時(shí)候,直觀顯示

如果有些資源會(huì)發(fā)生變化,可以單獨(dú)抽取出來(lái),做成函數(shù),這樣后續(xù)調(diào)用就可以不用改變了

附上一份 Python2.7 代碼(將一些私有的東西進(jìn)行了修改)

  1. # -*- coding:utf-8 -*- 
  2.   
  3. from sqlalchemy import create_engine 
  4. import logging 
  5. from logging.config import fileConfig 
  6. import requests 
  7. import Clinet # 私有的模塊 
  8.   
  9. fileConfig("logging_config.ini"
  10. logger = logging.getLogger("killduplicatedjob"
  11.   
  12. #配置可以單獨(dú)放到一個(gè)模塊中 
  13. DB_USER = "xxxxxxx" 
  14. DB_PASSWORD = "xxxxxxxx" 
  15. DB_PORT = 111111 
  16. DB_HOST_PORT = "xxxxxxxxxx" 
  17. DB_DATA_BASE = "xxxxxxxxxxx" 
  18.   
  19. REST_API_URL = "http://sample.com" 
  20.   
  21. engine = create_engine("mysql://%s:%s@%s:%s/%s" % (DB_USER, DB_PASSWORD, DB_HOST_PORT, DB_PORT, DB_DATA_BASE)) 
  22.   
  23. # 這個(gè) class 是為了在函數(shù)間傳遞時(shí),不需要使用方了解屬性的具體順序而寫的,也可以放到一個(gè)單獨(dú)的模塊中 
  24. class DuplicatedJobs(object): 
  25.  def __init__(self, app_id, app_name, user): 
  26.  self.app_id = app_id 
  27.  self.app_name = app_name 
  28.  self.user = user 
  29.   
  30. def __repr__(self): 
  31.  return '[appid:%s, app_name:%s, user:%s]' % (self.app_id, self.app_name, self.user
  32.   
  33. def find_duplicated_jobs(): 
  34.  logger.info("starting find duplicated jobs"
  35.  (running_apps, app_name_to_user) = get_all_running_jobs() 
  36.  all_apps_on_yarn = get_apps_from_yarn_with_queue(get_resource_queue()) 
  37.   
  38. duplicated_jobs = [] 
  39.  for app in all_apps_on_yarn: 
  40.  (app_id, app_name) = app 
  41.   
  42. if app_id not in running_apps: 
  43.  if not app_name.startswith("test"): 
  44.  logger.info("find a duplicated job, prefixed_name[%s] with appid[%s]" % (app_name, app_id)) 
  45.  user = app_name_to_user[app_name] 
  46.  duplicated_jobs.append(DuplicatedJobs(app_id, app_name, user)) 
  47.  else
  48.  logger.info("Job[%s] is a test job, would not kill it" % app_name) 
  49.   
  50. logger.info("Find duplicated jobs [%s]" % duplicated_jobs) 
  51.   
  52. return duplicated_jobs 
  53.   
  54. def get_apps_from_yarn_with_queue(queue): 
  55.  param = {"queue": queue} 
  56.  r = requests.get(REST_API_URL, params=param) 
  57.  apps_on_yarn = [] 
  58.  try: 
  59.  jobs = r.json().get("apps"
  60.  app_list = jobs.get("app", []) 
  61.  for app in app_list: 
  62.  app_id = app.get("id"
  63.  name = app.get("name"
  64.  apps_on_yarn.append((app_id, name)) 
  65.   
  66. except Exception as e: #Exception ***進(jìn)行單獨(dú)的分開,針對(duì)每一種 Exception 進(jìn)行不同的處理 
  67.  logger.error("Get apps from Yarn Error, message[%s]" % e.message) 
  68.   
  69. logger.info("Fetch all apps from Yarn [%s]" % apps_on_yarn) 
  70.   
  71. return apps_on_yarn 
  72.   
  73. def get_all_running_jobs(): 
  74.  job_infos = get_result_from_mysql("select * from xxxx where xx=yy"
  75.   
  76. app_ids = [] 
  77.  app_name_to_user = {} 
  78.  for (topology_id, topology_name) in job_infos: 
  79.  status_set = get_result_from_mysql("select * from xxxx where xx=yy"
  80.  application_id = status_set[0][0] 
  81.  if "" != application_id: 
  82.  configed_resource_queue = get_result_from_mysql( 
  83.  "select * from xxxx where xx=yy"
  84.  app_ids.append(application_id) 
  85.  app_name_to_user[topology_name] = configed_resource_queue[0][0].split(".")[1] 
  86.   
  87. logger.info("All running jobs appids[%s] topology_name2user[%s]" % (app_ids, app_name_to_user)) 
  88.  return app_ids, app_name_to_user 
  89.   
  90. def kill_duplicated_jobs(duplicated_jobs): 
  91.  for job in duplicated_jobs: 
  92.  app_id = job.app_id 
  93.  app_name = job.app_name 
  94.  user = job.user 
  95.  logger.info("try to kill job[%s] with appid[%s] for user[%s]" % (app_name, app_id, user)) 
  96.  try: 
  97.  Client.kill_job(app_id, user
  98.  logger.info("Job[%s] with appid[%s] for user[%s] has been killed" % (app_name, app_id, user)) 
  99.  except Exception as e: 
  100.  logger.error("Can't kill job[%s] with appid[%s] for user[%s]" % (app_name, app_id, user)) 
  101.   
  102. def get_result_from_mysql(sql): 
  103.  a = engine.execute(sql) 
  104.  return a.fetchall() 
  105.   
  106. # 因?yàn)橄旅娴馁Y源可能發(fā)生變化,而且可能包含一些具體的邏輯,因此單獨(dú)抽取出來(lái),獨(dú)立成一個(gè)函數(shù) 
  107. def get_resource_queue(): 
  108.  return "xxxxxxxxxxxxx" 
  109.   
  110. if __name__ == "__main__"
  111.  kill_duplicated_jobs(find_duplicated_jobs()) 
  112.   

其中 logger 配置文件如下(對(duì)于 Python 的 logger,官方文檔寫的非常好,建議讀一次,并且實(shí)踐一次)

  1. [loggers] 
  2. keys=root, simpleLogger 
  3.   
  4. [handlers] 
  5. keys=consoleHandler, logger_handler 
  6.   
  7. [formatters] 
  8. keys=formatter 
  9.   
  10. [logger_root] 
  11. level=WARN 
  12. handlers=consoleHandler 
  13.   
  14. [logger_simpleLogger] 
  15. level=INFO 
  16. handlers=logger_handler 
  17. propagate=0 
  18. qualname=killduplicatedjob 
  19.   
  20. [handler_consoleHandler] 
  21. class=StreamHandler 
  22. level=WARN 
  23. formatter=formatter 
  24. args=(sys.stdout,) 
  25.   
  26. [handler_logger_handler] 
  27. class=logging.handlers.RotatingFileHandler 
  28. level=INFO 
  29. formatter=formatter 
  30. args=("kill_duplicated_streaming.log""a", 52428800, 3,) 
  31.   
  32. [formatter_formatter] 
  33. format=%(asctime)s %(name)-12s %(levelname)-5s %(message)s 
責(zé)任編輯:武曉燕 來(lái)源: 36大數(shù)據(jù)
相關(guān)推薦

2013-06-05 09:51:04

2022-08-16 09:04:23

代碼圈圈復(fù)雜度節(jié)點(diǎn)

2025-01-26 08:30:00

Python代碼編程

2025-01-06 08:00:00

Python代碼編程

2020-05-25 11:14:59

代碼程序開發(fā)

2015-09-23 10:14:48

iOS 代碼實(shí)踐

2012-08-09 09:10:56

代碼審查代碼

2021-04-22 09:58:48

Python代碼內(nèi)存

2021-05-07 13:40:44

Python代碼內(nèi)存

2024-05-14 15:28:09

Python類型提示開發(fā)

2017-05-24 15:07:19

Python爬蟲爬取

2024-08-26 12:57:15

2015-07-27 09:39:24

后臺(tái)代碼

2020-03-30 11:30:52

微服務(wù)架構(gòu)數(shù)據(jù)

2014-10-29 13:52:38

程序員

2015-07-23 10:09:45

后臺(tái)定位代碼

2020-03-09 14:10:48

代碼開發(fā)工具

2023-04-28 08:06:04

低代碼AI智能

2020-10-23 06:57:56

MySQLMaxwell代碼

2017-06-16 14:35:09

FM 測(cè)試Docker實(shí)踐
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)