user1471980
user1471980

Reputation: 10626

unable to assign values to variables in python

I am trying to pull datastore values from vcenter and perform usage calculations as below:

s = VIServer()
s.connect(HOST, USER, PASSWORD)


properties = [  "name",
                "summary.capacity",
                "summary.freeSpace",
             ]

results = s._retrieve_properties_traversal(property_names=properties,
                                           obj_type=MORTypes.Datastore)
d = time.strftime('%Y-%m-%d %H:%M:%S')

for item in results:
    for r in item.PropSet:
                if r.Name == "name" :
                        name = r.Val

    for p in item.PropSet:
        global Total_Space,Free_Space
        if p.Name=="summary.capacity":
                Total_Space=p.Val
                Metric="datastore.space_total"
                print Metric,d,p.Val,"datastore="+name,"source="+"datastore"

        if p.Name=="summary.freeSpace":
                Free_Space=p.Val
                Metric="datastore.space_free"
                print Metric,d,p.Val,"datastore="+name,"source="+"datastore"
        if Total_Space>0 &  Free_Space>0:
                Used_Space=Total_Space-Free_Space
                Used_Percent=(Used_Space/Total_Space)*100

                Metric="datastore.space_used"
                print Metric,d,Used_Space,"datastore="+name,"source="+"datastore"
                Metric="datastore.diskPctUsed"
                print Metric,d,Used_Percent,"datastore="+name,"source="+"datastore"

When I run this I get this error:

Traceback (most recent call last):
  File "datastore.py", line 41, in <module>
    if Total_Space>0 &  Free_Space>0:
NameError: global name 'Total_Space' is not defined

Any ideas how I could fix this?

Upvotes: 0

Views: 93

Answers (3)

pmod
pmod

Reputation: 10997

From code logic it makes sense to check these values after for loop. Thus, you will make sure that they are initialized and checked once:

 ...
 Total_Space = 0
 Free_Space = 0
 for p in item.PropSet:
     ...

 if Total_Space and Free_Space:
     ...

However, if you'd initialize them to None, you can have additional check for presence them in PropSet :

 ...
 Total_Space = None
 Free_Space = None
 for p in item.PropSet:
     ...

 if Total_Space == None:
     print "error: no Total_Space in PropSet"
 elif Free_Space == None:
     print "error: no Free_Space in PropSet"
 elif Total_Space and Free_Space:
     ...

Some notes:

  • You should use and boolean operator instead of & for bitwise operator

  • Moreover, & has higher priority than > and expression will be evaluated as

    if Total_Space > (0 & Free_Space) > 0:

  • In conditionals, you can use just if value instead of if value>0

Upvotes: 1

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

initialize Total_Space and Free_Space with a value in first of your program.

Total_Space = 0
Free_Space = 0

Upvotes: 1

ChesterL
ChesterL

Reputation: 347

Because you did not declare 'Total_Space' globally before say global Total_Space, your should add Total_Space = None # or whatever value you like globally, i.e. outside of for loop or any function

Upvotes: 1

Related Questions