2017-03-22 3 views
0

Der Versuch, python3.4 Klassenmethoden in einer Schleife aufzurufen. Aber bekommen diese ""python3.4 Einrückungsfehler in "for-Schleife" (pyvmomi)

Ich sah den Code in beiden "vim" und "gedit/sublime" und sie zeigen keine offensichtlichen Fehler.

Ist es nicht unbedingt eine Einrückung, sondern ein anderer Fehler?

Danke.

$ cat pyvmomi-loop.py 
from __future__ import (absolute_import, division, 
         print_function, unicode_literals) 
from builtins import * 
import atexit 
import sys 
sys.path.insert(0, '/usr/lib/python2.7/site-packages') 
import pyVmomi 
import argparse 
import atexit 
import itertools 
from pyVim.connect import SmartConnect, Disconnect 
import humanize 
from pyVim import connect 
from pyVmomi import vmodl 
from pyVmomi import vim 

## cred 
host="1.2.3.4" 
user="aa\bb" 
password="[email protected]" 
port=443 

## Ignore certificate error 
import ssl 
try: 
    _create_unverified_https_context = ssl._create_unverified_context 
except AttributeError: 
    pass 
else: 
    print ("Ignoring SSL Error") 
    ssl._create_default_https_context = _create_unverified_https_context 

## Fetch id an dpw 

def GetArgs(): 

    parser = argparse.ArgumentParser(
     description='Process args for retrieving all the Virtual Machines') 
    parser.add_argument('-s', '--host', required=True, action='store', 
         help='Remote host to connect to') 
    parser.add_argument('-o', '--port', type=int, default=443, action='store', 
         help='Port to connect on') 
    parser.add_argument('-u', '--user', required=True, action='store', 
         help='User name to use when connecting to host') 
    parser.add_argument('-p', '--password', required=False, action='store', 
         help='Password to use when connecting to host') 
    args = parser.parse_args() 
    return args 


## Method to fetch VM info 

def printHost(host): 

    try: 
     summary = host.summary 
     stats = summary.quickStats 
     hardware = host.hardware 
     cpuUsage = stats.overallCpuUsage 
     memoryCapacity = hardware.memorySize 
     memoryCapacityInMB = hardware.memorySize/MBFACTOR 
     memoryUsage = stats.overallMemoryUsage 
     freeMemoryPercentage = 100 - (
      (float(memoryUsage)/memoryCapacityInMB) * 100 
     ) 
     print ("--------------------------------------------------") 
     print ("Host name: ", host.name) 
     print ("Host CPU usage: ", cpuUsage) 
     print ("Host memory capacity: ", humanize.naturalsize(memoryCapacity, 
                  binary=True)) 
     print ("Host memory usage: ", memoryUsage/1024, "GiB") 
     print ("Free memory percentage: " + str(freeMemoryPercentage) + "%") 
     print ("--------------------------------------------------") 
    except Exception as error: 
     print ("Unable to access information for host: ", host.name) 
     print (error) 
     pass 



## Main method 

def main(): 

    # argsCred = GetArgs() 

    try: 
     service_instance = connect.SmartConnect(host=host, 
               user=user, 
               pwd=password, 
               port=port, 
               ) 

## What to do when exiting 
     atexit.register(connect.Disconnect, service_instance) 

##Content object 
     content = service_instance.RetrieveContent() 

     container = content.rootFolder # starting point to look into 
     viewType = [vim.VirtualMachine] # object types to look for 
     recursive = True # whether we should look into it recursively 

## Create a view 
     containerView = content.viewManager.CreateContainerView(
      container, viewType, recursive) 

## Loop through all obhects to return name and VMware tools version 
     children = containerView.view 
     for child in children: 
      if child.summary.guest is not None: 
       try: 
        tools_version = child.summary.guest.toolsStatus 
          osFam = child.summary.guest.guestId 
        print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) 
         print("VM: {}, OS Fam: {}".format(child.name, osFam)) 
       except: 
        print("Vmware-tools: None") 




    except vmodl.MethodFault as error: 
     print("Caught vmodl fault : " + error.msg) 
     return -1 

    return 0 

## Start program 
if __name__ == "__main__": 
    main() 

Antwort

0

Dieser Code hat Einrückungsprobleme.

ändern

for child in children: if child.summary.guest is not None: try: tools_version = child.summary.guest.toolsStatus osFam = child.summary.guest.guestId print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) print("VM: {}, OS Fam: {}".format(child.name, osFam)) except: print("Vmware-tools: None")

Um for child in children: if child.summary.guest is not None: try: tools_version = child.summary.guest.toolsStatus osFam = child.summary.guest.guestId print("VM: {}, VMware-tools: {}".format(child.name, tools_version)) print("VM: {}, OS Fam: {}".format(child.name, osFam)) except: print("Vmware-tools: None")

Wenn das Ihre ganze Py-Datei ist, Fehler in Zeile 114 und 116 Kopieren Sie nicht diesen Code einfügen, weil es sein könnte zu viele Leerzeichen. Einfach den Code aus den Zeilen einrücken, die ich erwähnt habe.

Edit: Es ist keine gute Praxis, in vim oder gedit oder anderen Texteditoren zu programmieren. Werfen Sie einen Blick auf Pycharm :)

+0

das war die ganze Py-Datei. aber sehen Sie andere Einrückungen, die den Einzug fälschlicherweise an dieser Stelle kennzeichnen könnten? Vielen Dank! – SndLt

+0

Ihre Meinung zu vim ist nichts anderes als eine schlechte Meinung. Sie sollten es entfernen, weil es nicht relevant ist. –

+0

habe gerade pycharm. Ich bin mir sicher, dass dies sehr hilfreich sein wird. Danke vielmals! – SndLt

Verwandte Themen