Question

Good morning everybody! (Yeah I know, that not necessary the morning on your side ;-) )

I come to you because I'm starting my day with a little bug on a piece of code that I can't explain.

I'm currently building a class which aim to do two simple things:

Opening a YAML formatted configuration file and then loading this one.

Unfortunately for me, my brand new revolutionar program (That's a joke obviously) do not want to be a good soldier today, and refuse to do what it is supposed to do.

Here is my code:

The main.py part:

#!/usr/bin/env python
import socket
from models import setting

run = setting.app_config()
run.get_config()

and here is the setting module:

import os
import yaml

class app_config:

def __init__(self, custom_cfg=None):

    self.workdir = os.getcwd()
    self.default_config_name = 'app_config.yaml'
    self.default_config_path = self.workdir+"/assets/"

    if custom_cfg is None:
        self.config_file_path = self.default_config_path+self.default_config_name

    else:
        self.config_file_path = custom_cfg

def get_config(self):

    app_config_file = open(self.config_file_path, 'r')
    parsed_config_file = yaml.load(app_config_file, 'r')
    app_config_file.close()
    network_settings = parsed_config_file['network']
    hostname = network_settings['host']
    socket = network_settings['port']
    buffer_size = network_settings['buffer_size']

    print network_settings, hostname, socket, buffer_size

So, now, the problem with those two pieces of code seems coming from this declaration:

parsed_config_file = yaml.load(app_config_file, 'r') <-- Line 22

which lead to a pretty beautiful error message:

Traceback (most recent call last):
File "main.py", line 6, in <module>
  run.get_config()
File "/home/dri/devil_project/models/setting.py", line 22, in get_config
  parsed_config_file = yaml.load(app_config_file, 'r')
File "/usr/local/lib/python2.6/dist-packages/yaml/__init__.py", line 69, in load
  loader = Loader(stream)
TypeError: 'str' object is not callable

Now, if I try to print my app_config_file variable or trying to read this file using a for loop, everything is perfect, print is working fine, my file open and I can read it.

Well, if someone already faced this situation using YAML module (PyYAML one), I'll be interested by his solution ;-)

Many thanks!

Was it helpful?

Solution

Your telling the load function you want to open something for reading, so its assuming youre give it a file. Get rid of the 'r' argument.

Unless you want to specify a file path, in which case I think what you meant to load was app_config_file_path

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top