简体   繁体   中英

TypeError: object of type 'NoneType' has no len() Python on application deployment

I'm just learning python and I'm trying to run my application but get the following:

Traceback (most recent call last):
  File "/Users/plugins/bluegreen.py", line 433, in <module>
    cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]
  File "/Users/plugins/bluegreen.py", line 66, in get_cname
    if len(data.get("cname")) == 0:
TypeError: object of type 'NoneType' has no len()

Here are the contents of line 66 and 433:

66:

def get_cname(self, app):
    response = self.get("/apps/{}".format(app))
    data = json.loads(response.read())
    if len(data.get("cname")) == 0:
      return None
    return data.get("cname")

433:

cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]

Edit 1:

If I place a print(data), I get the following:

{u'entrypoints': [], u'routeropts': {}, u'description': u'', u'repository': u'git@10.10.1.1.nip.io:hello-green.git', u'tags': [], u'lock': {u'Owner': u'', u'Reason': u'', u'AcquireDate': u'0001-01-01T00:00:00Z', u'Locked': False}, u'routers': [{u'type': u'traefik', u'name': u'traefik', u'opts': {}, u'address': u'hello-green.10.10.1.1.nip.io'}], u'deploys': 0, u'routingsettings': None, u'teams': [u'admin'], u'platform': u'go', u'teamowner': u'admin', u'plan': {u'router': u'traefik', u'swap': 0, u'cpushare': 100, u'name': u'autogenerated', u'memory': 0}, u'ip': u'hello-green.10.10.1.1.nip.io', u'owner': u'admin@shipa.io', u'router': u'traefik', u'units': [], u'pool': u'gce', u'name': u'hello-green'}
Traceback (most recent call last):
  File "/Users/brunoandrade/.shipa/plugins/bluegreen.py", line 434, in <module>
    cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]
  File "/Users/brunoandrade/.shipa/plugins/bluegreen.py", line 67, in get_cname
    if len(data.get("cname")) == 0:
TypeError: object of type 'NoneType' has no len()

Edit 2:

Not sure why I get no cname key, since I'm getting it here:

  config = Config.load('shipa-bluegreen.ini')

  app_name = config['name']
  blue = "%s-blue" % app_name
  green = "%s-green" % app_name

  bluegreen = BlueGreen(token, target, config)

  apps = [blue, green]
  cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]

Which is reading from the shipa-bluegreen.ini, which I have in the file:

[Application]
name: hello

Any help would be really appreciated

Thanks!

You printed out data, and got

{u'entrypoints': [], u'routeropts': {}, u'description': u'', u'repository': u'git@10.10.1.1.nip.io:hello-green.git', u'tags': [], u'lock': {u'Owner': u'', u'Reason': u'', u'AcquireDate': u'0001-01-01T00:00:00Z', u'Locked': False}, u'routers': [{u'type': u'traefik', u'name': u'traefik', u'opts': {}, u'address': u'hello-green.10.10.1.1.nip.io'}], u'deploys': 0, u'routingsettings': None, u'teams': [u'admin'], u'platform': u'go', u'teamowner': u'admin', u'plan': {u'router': u'traefik', u'swap': 0, u'cpushare': 100, u'name': u'autogenerated', u'memory': 0}, u'ip': u'hello-green.10.10.1.1.nip.io', u'owner': u'admin@shipa.io', u'router': u'traefik', u'units': [], u'pool': u'gce', u'name': u'hello-green'}

You can clearly see that there is no key called "cname" in here.

There's no cname key in data . data.get("cname") returns None if the key isn't found but you can specify an explicit default. Use an empty string as the default instead.

def get_cname(self, app):
    response = self.get("/apps/{}".format(app))
    data = json.loads(response.read())
    if len(data.get("cname", "")) == 0:
      return None
    return data.get("cname")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM