2014-01-12 52 views
0

有不希望在project_item和分支組合已經運行運行ssh命令SSH命令我基礎上,project_item和branch..i運行,所以我想添加一個條件來檢查project_item和分支組合是否已被解析,有關如何添加它的任何想法?添加一個條件,以檢查是否project_item和分支已經被解析

for project_item in projects: 
     branch = manifest_data(project_item) 
     print "PROJECT_ITEM: " + project_item 
     print "BRANCH: " + branch 
     /*********condition to check if the project_item/branch combo is already parsed/*********** 
     mft = manifest_table(project_item,branch) 
     if 'win32' in OS_Type and branch != 'PROJECT NOT ENBALED': 
     #print OS_Type 
     cmd = "\"\"%s\Git\\bin\sh.exe\" --login -c \"ssh -l user -p 29418 review-android.company.com gerrit query project:%s branch:%s --format=JSON status:open --commit-message --current-patch-set\"\" >> gerrit_output.txt" %(os.environ['ProgramFiles(x86)'], project_item,branch) 
     elif 'linux' in OS_Type: 
     #print OS_Type 
     cmd = "ssh -l user -p 29418 review-android.company.com gerrit query project:%s branch:%s --format=JSON status:open --commit-message --current-patch-set\"\" >> gerrit_output.txt" %(project_item,branch) 
+0

你是什麼意思的「解析」?從你的編輯看起來你的意思是「叫」。 PS:似乎有一個錯字:「ENBALED」。 –

回答

0

我不完全理解你的問題,但似乎你問如何檢查函數是否已被調用一次只計算一次?

的 「memoize的」 模式很好地實現了這一點。一個版本can be found here

完整的代碼看起來是這樣的:

import collections 
import functools 

class memoized(object): 
    '''Decorator. Caches a function's return value each time it is called. 
    If called later with the same arguments, the cached value is returned 
    (not reevaluated). 
    ''' 
    def __init__(self, func): 
     self.func = func 
     self.cache = {} 
    def __call__(self, *args): 
     if not isinstance(args, collections.Hashable): 
     # uncacheable. a list, for instance. 
     # better to not cache than blow up. 
     return self.func(*args) 
     if args in self.cache: 
     return self.cache[args] 
     else: 
     value = self.func(*args) 
     self.cache[args] = value 
     return value 
    def __repr__(self): 
     '''Return the function's docstring.''' 
     return self.func.__doc__ 
    def __get__(self, obj, objtype): 
     '''Support instance methods.''' 
     return functools.partial(self.__call__, obj) 

@memoized 
def fibonacci(n): 
    "Return the nth fibonacci number." 
    if n in (0, 1): 
     return n 
    return fibonacci(n-1) + fibonacci(n-2) 

print fibonacci(12) 

它只是緩存函數的返回值。您可能需要一些更智能的緩存,當然,您應該查看真正的緩存,但看起來您只是在優化,所以也許這是一個開始。

緩存在Python已經在這裏討論:Is there a Python caching library?

+0

- 如果你看我的代碼..有基於project_item和分支運行的ssh命令..我不想在project_item上運行ssh命令,分支組合已經運行,明確嗎? – user3131197

+0

可能我們可以把它的哈希值,然後檢查哈希表,如果project_item和組合已經被解析與否,而不是檢查我們檢查project_item和分支組合已經被解析的command..can .. – user3131197

1

如果你只是想避免運行相同的命令兩次,你只需要記住的命令,然後檢查是否已經運行它:

# Create a set to remember your executed commands. Sets are better than 
# lists here, since you will repeatedly check if an item exists. 
# See: https://wiki.python.org/moin/TimeComplexity 
executed_commands = set() 

for project_item in projects: 
    # Your posted code... 

    identifier = hash((project_item, branch)) 
    # check if command has already been executed 
    if identifier not in executed_commands: 
     executed_commands.add(identifier) 

     # Actually run cmd... 
+0

? – user3131197

+0

「解析」是什麼意思?這是'manifest_data'函數的作用嗎? –

+0

-i意思是,我不想的條件 – user3131197

0

爲什麼不簡單地將解析對象的散列存儲在列表中?

parsed_list=[] 
for project_item in projects: 
    branch = manifest_data(project_item) 
    item_hash = hash(branch+project_item) 
    print "PROJECT_ITEM: " + project_item 
    print "BRANCH: " + branch 
    if item_hash not in parsed_list: 
     parsed_list.append(item_hash) 
     # more code 
+0

它不僅分支.. project_item和分支的組合應該在parset_list ..我們應該檢查是否已經解析了project_item和分支.. – user3131197

相關問題