Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Reclaim temp tablespace for oracle 8, 8i Oracle
  • create PLAN_TABLE command. Oracle
  • How to analyze statspack or AWR report. Oracle
  • JSON/XML Types in Oracle Oracle
  • Temporary Tablespace Information and restriction. Oracle
  • DBMS_PROFILER for tuning PLSQL programs. Oracle
  • tblwopk.sql /* Find Tables Without PK */ Oracle
  • Transfer SQL Profiles from One database to other database. Oracle
  • Remove DOS CR/LFs (^M) Linux/Unix
  • Oracle Identifiers Oracle
  • ORA-3136 Oracle
  • Some useful Unix Commands Linux/Unix
  • Database logon trigger issue Oracle
  • Insert cause enqueue locks Oracle
  • Consolidated Reference List Of Notes For Migration / Upgrade Service Requests -ID 762540.1 Oracle

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

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

Leave a Reply Cancel reply

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

Categories

  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • Oracle (392)
  • PHP/MYSQL/Wordpress (10)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (341)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • load SPM baseline from cursor cache05-Jun-2025
  • Drop all SPM baselines for SQL handle05-Jun-2025
  • Load SPM baseline from AWR05-Jun-2025
  • Drop specific SQL plan baseline – spm05-Jun-2025
  • findinfo.sql (SQL for getting CPU and Active session info)27-May-2025
  • SQL Tracker by SID sqltrackerbysid.sql22-Apr-2025
  • How to connect to Oracle Database with Wallet with Python.21-Mar-2025
  • JSON/XML Types in Oracle18-Mar-2025
  • CPU Core related projections12-Mar-2025
  • Exadata Basics10-Dec-2024

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • rm_backup_arch_file.ksh Linux/Unix
  • Goldengate Tutorial Oracle
  • Locktree.sql Oracle
  • get_vmstat_solaris Oracle
  • Giving Grant on v$DATABASE Oracle
  • move_arch_files.ksh /* Good One */ Linux/Unix
  • Find Plan Hash value fphv.sql Oracle
  • How to Decide upto what level you can decrement your datafile size. ( Shrink Datafile) Oracle

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme