0

I've spent a few days looking at this on and off but I can't work out what I'm doing wrong. I've tried to follow this:

Passing Variables between functions

I'm still not clear on what I need to do though.

This is what I have presently but I've tried various permutations:

def root():
        hivelogin()
        occupancy()
        print (hiveSessionId)
        return ("Done")

I need this to work in lots of places but I'll use hivelogin() as the example:

def hivelogin():
        import requests
        url = "https://api.prod.bgchprod.info:443/omnia/auth/sessions"
        payload = "{\n    \"sessions\": [{\n        \"username\": \"xxxxxxxxxxx\",\n        \"password\": \"xxxxxxxxxxx\",\n        \"caller\": \"WEB\"\n    }]\n}"
        headers = {
            'Content-Type': "application/vnd.alertme.zoo-6.1+json",
            'Accept': "application/vnd.alertme.zoo-6.1+json",
            'X-Omnia-Client': "Hive Web Dashboard",
            'Cache-Control': "no-cache",
            }

        response = requests.request("POST", url, data=payload, headers=headers)
        data=response.json()
        hiveSessionId = (data['sessions'][0]['sessionId'])
        session['hiveSessionId'] = (data['sessions'][0]['sessionId'])
        return hiveSessionId

I've got round it by using a session variable in this case but I don't want to have to do this when I've probably got loads of variables I need to pass around.

So, what I need is for hiveSessionId to my root (or any other) function.

If I do:

def root(hiveSessionId=None)

I get nothing - as I'd expect.

If I do:

def root(hiveSessionId=hiveSessionId)

I get an error - as I'd expect.

So, the simple question is how I pass a variable from the function that creates it to any other function in my app - probably between routes as I'm using Flask but I'm using MySQL to give some persistence.

I've done a tiny bit of coding in the past with PHP but please bear in mind that I'm old, been learning Python for about 3 weeks, and this is my first app. I did the "Hello world" thing, watched a video about Python on Youtube and then did this so it's probably going to make you want to cry.

EDIT: Simplified pseudo version of what I want:

def a():
    b()
    return x

def b():
    x=10
AndyJ
  • 133
  • 1
  • 3
  • 13

2 Answers2

1

Here is the updated version of your pseudo code. Which is getting generated value in function a from another function b.

def a():
    x = b()
    return x

def b():
    x=10
    return x
Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
feroz
  • 54
  • 1
  • 7
  • Boom! Apart from my missing : which I've now fixed, that works exactly as I hoped. Now to take that and apply it to my real life script. Thank you very much for that. I have no idea why I couldn't get to that on my own but it's early days for me. – AndyJ Aug 17 '18 at 10:17
  • Sorry, only just realised how to do that. – AndyJ Aug 19 '18 at 17:16
0

Apparently you are a little unaware of the fundamental order of things. I did not understand a bit where here is the Flask, but if you want to use a variable X in function A, which you calculate in function B, then just return a variable from function like:

def A():
    x = 'x value'
    return x

def B(x_as_argument):
    print(x_as_argument)

X = A()
B(X)

Or you can use a global variable, but it's not a very good practise. In either case, both options will work with Flask.

bigEvilBanana
  • 388
  • 2
  • 8
  • No, I am a LOT unaware of the order of things :) The above is a snippet from my code. At the moment, my Flask app is one route to display a dashboard. That relies on being logged into the Hive API and having a token. This might be used in lots of functions and routes. Thank you for your answer. I'll give that a try tomorrow. – AndyJ Aug 16 '18 at 21:05
  • Ran your script exactly as you have it here and just got: NameError: name 'x' is not defined on line 2. Tried changing the x to X and vice versa but didn't get anywhere so either your code is wrong or I'm missing some other point. – AndyJ Aug 16 '18 at 23:48
  • Sorry, I'm obviously missing something here. That is just going to output x value. If I change that to x = 'Hello Andy' then it'll just output Hello Andy. What I'm looking for is x=10 in function B. Then, function A calls function B first and returns x. I can't do it with a comment so I'll edit my code. – AndyJ Aug 17 '18 at 09:28