2

I'm working on my first python script. I want to use a variable within a callback-function:

def run(self, edit):
    gitFolder = os.path.basename(gitRoot)
    #Get branch
    self.run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], self.branchDone)


def branchDone(self, result):
    build =  "./Build-Release.ps1 " + gitFolder + " " + result +" -Deploy;"
    print build

How do I make gitFolder available to the method branchDone?

Himmators
  • 14,278
  • 36
  • 132
  • 223
  • return gitFolder - after `self.run_command(...)` in your `run(...)` function definition. try that. Also, you can add gitFolder as an argument in your `branchDone(self,result,gitFolder)` where you can pass it in. Otherwise, just declare gitFolder globally, and use it in both functions. – ha9u63a7 Nov 12 '14 at 08:31
  • nope, File ".\Panagora.py", line 40, in branchDone NameError: global name 'gitFolder' is not defined – Himmators Nov 12 '14 at 08:33
  • I would guess it's because the return has not executed when branchDone is run. – Himmators Nov 12 '14 at 08:35
  • possible duplicate of [Using global variables in a function other than the one that created them](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – ha9u63a7 Nov 12 '14 at 08:36

4 Answers4

2

simply return gitFolder from run and call run in branchcode Try this:-

def run(self, edit):
    gitFolder = os.path.basename(gitRoot)
    #Get branch
    self.run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], self.branchDone)
    return gitFolder

def branchDone(self, result):
    build =  "./Build-Release.ps1 " + run() + " " + result +" -Deploy;" #run() returns gitfolders
    print build

There is another way

   def run(self, edit):
        self.gitFolder = os.path.basename(gitRoot)
        #Get branch
        self.run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], self.branchDone)


    def branchDone(self, result):
        build =  "./Build-Release.ps1 " + self.gitFolder + " " + result +" -Deploy;"  

But it carries a problem that you need to execute the run function before executing branchcode, else self.gitfolder will be undefined and raise Attribute error.

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
1

As I am observing, these are class methods, you can define a class attribute to share gitFolder across them.

def run(self, edit):
    self.gitFolder = os.path.basename(gitRoot)
    #Get branch
    self.run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], self.branchDone)


def branchDone(self, result):
    build =  "./Build-Release.ps1 " + self.gitFolder + " " + result +" -Deploy;"
    print build
Devesh Saini
  • 557
  • 5
  • 16
1

One way would be using functools.partial:

def run(self, edit):
    gitFolder = os.path.basename(gitRoot)
    #Get branch
    callback = functools.partial(self.branchDone, gitFolder)
    self.run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], callback)

def branchDone(self, gitFolder, result):
    build =  "./Build-Release.ps1 " + gitFolder + " " + result +" -Deploy;"
    print build
bereal
  • 32,519
  • 6
  • 58
  • 104
1

If you don't need branchDone except for as a callback, consider defining it as a closure inside run:

from subprocess import check_output

def run_command(command, callback):
    callback(check_output(command).strip())

class Foo(object):
    def run(self, edit):
        gitFolder = "some_magic"
        def branch_done(result):
            print "./Build-Release.ps1 " + gitFolder + " " + result + " -Deploy;"
        run_command(['git', 'rev-parse','--abbrev-ref','HEAD'], branch_done)

Foo().run("edit")
Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64