0

I'm having a bit of an issue. For some reason, this class I'm trying to put together is throwing a NoneType error when, for all intents and purposes it should be responding properly as far as I can tell.

import re
import subprocess

class progEnv( object ) :

    hostName_regex = re.compile( '[a-z][a-z]prog[a-z][a-z][a-z]\d\d' )
    hostDomain_regex = re.compile( '(prod|dev|qa1|stag)\.company\.net' )
    hostNum_regex = re.compile( '[a-z][a-z]prog[a-z][a-z][a-z](\d\d)' )
    hostPrefix_regex = re.compile( '\w\wprog' )
    hostTier_regex = re.compile( '(web|app)' )
    hostId_regex = re.compile( '[a-z][a-z]prog[a-z][a-z][a-z]\d\d([a-z])' )
    hostEnv_regex = re.compile( '(prod|dev|qa1|stag)' )

    def __init__( self ) :
        self.hostnameProc = subprocess.Popen( 'hostname', stdout=subprocess.PIPE )
        self.fqdn = self.hostnameProc.stdout.read()
        self.hostName = self.hostName_regex.search( self.fqdn )
        self.hostDomain = self.hostDomain_regex.search( self.fqdn )
        self.hostNum = self.hostNum_regex.search( self.hostName.group() )
        self.hostPrefix = self.hostPrefix_regex.search( self.hostName.group() )
        self.hostTier = self.hostTier_regex.search( self.hostName.group() )
        self.hostId = self.hostId_regex.search( self.hostName.group() )
        self.hostEnv = self.hostEnv_regex.search( self.hostName.group() )

When I instantiate the progEnv class the program fails on the call to self.hostName.group() with an error of:

Traceback (most recent call last):
  File "./test.py", line 5, in <module>
    env = prog_env.progEnv()
  File "/prog/eclipse/workspace/PROG Management Command/prog_env.py", line 28, in __init__
    self.hostNum = self.hostNum_regex.search( self.hostName.group() )
AttributeError: 'NoneType' object has no attribute 'group'

Any thoughts on what is happening?

3 Answers 3

3

This means that the regex didn't match, so self.hostName_regex.search(self.fqdn) returned None. And of course you can't use the .group() method on None.

Sign up to request clarification or add additional context in comments.

1 Comment

You were the first to respond and I just realized exactly what was happening. Time to take a moment and facepalm. I just realized the reason it is throwing the error is because I'm running it in a server that doesn't match the regex scheme. Ugh. Thanks for the help!
0

The error message says that you are trying to access the attribute group of None. From the code line that is displayed that means self.hostName must be None. The regex search you used to initialise self.hostName must have failed to match anything.

BTW, you seem to not fully understand the error message. It isn't "throwing a NoneType error", it is throwing an AttributeError: the error message for an AttributeError shows you the type of the object and the type of None is NoneType.

Comments

0
self.hostName = self.hostName_regex.search( self.fqdn )

This line sets the value of self.hostName to None, when self.fqdn is not matched by regex. Which is causing the error. Change the dependent lines to something like,

self.hostNum = self.hostName and self.hostNum_regex.search( self.hostName.group() )

just to check self.hostName before using its group method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.