Giter VIP home page Giter VIP logo

Comments (7)

osantana avatar osantana commented on August 14, 2024 3

I'd like to make a design suggestion: avoid configuration handling in libraries. The "right place" to handle configuration is your project (that requires/uses the library).

It's better make your library configuration-agnostic, so, the users of your library can use any configuration system. And you can also reduce the number of depedencies.

"Wrong" way

# mylib.py

class Client:
    def __init__(self, url=None):
        self.url = config("URL", default="http://localhost/")
# myproject.py

from mylib import Client

client = Client()

"Right" way

# mylib.py

class Client:
    def __init__(self, url):
        self.url = url
# myproject.py

from mylib import Client

url = config("URL", "http://localhost")
client = Client(url)

from python-decouple.

rmax avatar rmax commented on August 14, 2024 2

I faced this issue when trying to use decouple.config in a jupyter notebook.

This PR #28 allows the users to instantiate AutoConfig with a custom initial search path. For example, in a jupyter notebook:

config = AutoConfig(os.getcwd())

from python-decouple.

henriquebastos avatar henriquebastos commented on August 14, 2024 1

When you from decouple import config you're using the default AutoConfig instance.

It has a magic that searches for the settings.ini file from the caller's module directory. If it can't find there, it keeps searching up the tree.

If you want to have full controle and point directly where your settings.ini is:

from decouple import RepositoryIni, Config
config = Config(RepositoryIni('path/to/settings.ini'))

DEBUG = config('DEBUG')

This API has room for improvement. You're the 2nd to request something like this.

from python-decouple.

nielsonsantana avatar nielsonsantana commented on August 14, 2024

I have interest in this feature too. I solve it by override _caller_path

from decouple import AutoConfig

def _caller_path(self):
    return os.getcwd()

AutoConfig._caller_path = _caller_path
config = AutoConfig()

from python-decouple.

henriquebastos avatar henriquebastos commented on August 14, 2024

@nielsonsantana can you provide a patch so we can invert the dependency on the path?

from python-decouple.

nielsonsantana avatar nielsonsantana commented on August 14, 2024

I trying figure out how change your code. Are you suggesting to override "_caller_path"?
Replace:

def _caller_path(self):
    # MAGIC! Get the caller's module path.
    frame = sys._getframe()
    path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
    return path 

for

def _caller_path(self):
    return os.getcwd()

In my tests, it's work based on where it is called from. Per example, if it is called from a terminal, it will use the local .env based on the path of the terminal.

from python-decouple.

henriquebastos avatar henriquebastos commented on August 14, 2024

Fixed tks to @rmax

from python-decouple.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.