/
githubmirror
/
cmssw
Обзор
Документация
Войти
/
githubmirror
/
cmssw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
FWCore/Utilities/python/FileUtils.py
76 строк
3 KB
Shahzad Malik Muzaffar
[CORE] py2/3 compatibility:drop use of __future__
22 ноя 2024, 20:16
22 ноя 2024, 20:16
4759e53
Код
Авторство
О чём код?
import os import re import pprint as pprint # for testing def loadListFromFile (filename): """Loads a list of strings from file. Will append to given list if asked.""" retval = [] filename = os.path.expanduser (filename) if not os.path.exists (filename): print("Error: file '%s' does not exist."%(filename)) raise RuntimeError("Bad filename") source = open (filename, 'r') for line in source.readlines(): line = re.sub (r'#.+$', '', line) # remove comment characters line = line.strip() if len (line): retval.append (line) source.close() return retval def sectionNofTotal (inputList, currentSection, numSections): """Returns the appropriate sublist given the current section (1..numSections)""" currentSection -= 1 # internally, we want 0..N-1, not 1..N size = len (inputList) perSection = size // numSections extra = size % numSections start = perSection * currentSection num = perSection if currentSection < extra: # the early sections get an extra item start += currentSection num += 1 else: start += extra stop = start + num return inputList[ start:stop ] ############################################################################## ## ######################################################################## ## ## ## ## ## ## ######################################################################## ## ############################################################################## if __name__ == "__main__": ############################################# ## Load and save command line history when ## ## running interactively. ## ############################################# import os, readline import atexit historyPath = os.path.expanduser("~/.pyhistory") def save_history(historyPath=historyPath): import readline readline.write_history_file(historyPath) if os.path.exists(historyPath): readline.read_history_file(historyPath) atexit.register(save_history) readline.parse_and_bind("set show-all-if-ambiguous on") readline.parse_and_bind("tab: complete") if os.path.exists (historyPath) : readline.read_history_file(historyPath) readline.set_history_length(-1) ############################ # Example code starts here # ############################