Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • findobj.sql Oracle
  • This is from Temi Oracle
  • setting prompt display with .profile Linux/Unix
  • To find explain plan for a statement that occurred in past. Oracle
  • Korn Shell Arithmatic Linux/Unix
  • Find execution plan from dba_hist_sql_plan for a specific SQL_ID and PLAN_HASH_VALUE fplan.sql Oracle
  • rm_backup_arch_file.ksh Linux/Unix
  • Rollback force for distributed transactions Oracle
  • USE_NL and INDEX hints example Oracle
  • temp_use.sql diplays usage of temp ts Oracle
  • How to remove blank lines using vi editor command Linux/Unix
  • Running some SQL on multiple databases connecting using monitoring userid and password Linux/Unix
  • Search and Replace vi editor command. Linux/Unix
  • Adding addidional hard drive and attach it to a linux box. Linux/Unix
  • Add new columns in dataframe Python/PySpark

Reading config file from other folder inside class

Posted on 24-Sep-2024 By Admin No Comments on Reading config file from other folder inside class

Directory Structure:

root-folder/
    config-folder/
        json-file.config
    class-folder/
        classfile.py
    tests/
        pytest-file1.py

json-file.config:

{
    "setting1": "value1",
    "setting2": "value2"
}

Now, Here I need to access json-file.config in my class file. I do not want to use dbutil, how can i access it and read it from json-file.config in my class file??

  • For this task, update contents of your class file as mentioned below.

classfile.py

import os
import json

class myclass:
    def __init__(self):
        # Get the path of the json-file.config
        config_file_path = os.path.join(os.path.dirname(__file__), '..', 'config-folder', 'json-file.config')

        # Read the JSON config file
        with open(config_file_path, 'r') as file:
            self.config = json.load(file)
    
    def get_setting(self, key):
        return self.config.get(key, "Setting not found")

Explanation:

os.path.join: Constructs the path to the json-file.config file relative to the location of classfile.py by going up one level (..) and then into the config-folder/.

json.load(file): Reads the content of the json-file.config and loads it into a dictionary.

Usage: You can now access the JSON config in your pytest-file1.py by creating an instance of myclass.

Example usage in pytest-file1.py:

from class_folder.classfile import myclass

def test_read_config():
    # Create an instance of the class
    obj = myclass()

    # Access a specific setting from the config
    setting_value = obj.get_setting('setting1')
    
    # Example assertion (for pytest)
    assert setting_value == "value1"
Python/PySpark

Post navigation

Previous Post: Python class import from different folders
Next Post: Exadata Basics

Related Posts

  • Python class import from different folders Python/PySpark
  • Read CSV File using Python Python/PySpark
  • Read CSV file using PySpark Python/PySpark
  • How to connect to Oracle Database with Wallet with Python. Oracle
  • Add new columns in dataframe Python/PySpark
  • Getting started with notebook Python/PySpark

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (150)
  • MYSQL (5)
  • Oracle (403)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (151)
  • SQL scripts (349)
  • SQL Server (6)
  • Uncategorized (5)
  • Videos (0)

Recent Posts

  • Key Management in Oracle: The Core Issue: Missing Master Key12-May-2026
  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sh23-Apr-2026
  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Find nth max and min. Oracle
  • pvmehta.com SQL scripts
    Find which sessions is accessing object that prevent your session to have exclusive locks in Oracle Oracle
  • AWR license Oracle
  • Passing from Unix to PLSQL using bind variables Linux/Unix
  • Generating XML from SQLPLUS Oracle
  • findobj.sql Oracle
  • Formatter Explain plan Output 1 Oracle
  • Display the top 5 salaries for each department using single SQL Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme