Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Very clear article about oracle dataguard Oracle
  • Is It Recommended To Apply Patch Bundles When PSU Is Available? -ID 743554.1 Oracle
  • Temporary tablespace explaination Oracle
  • Mutating Table Error while using database trigger Oracle
  • Remove DOS CR/LFs (^M) Linux/Unix
  • DBMS_Shared_pool pinning triggers Oracle
  • Read CSV file using PySpark Python/PySpark
  • Find total file sizes Linux/Unix
  • PLSQL Table Syntax 1 Oracle
  • changing kernel parameter in Oracle Enterpise Linux Linux/Unix
  • default permission on ~/.ssh/authorized_keys2 or authorized_keys Linux/Unix
  • shutdown linux Linux/Unix
  • For Perl DBI installation and testing program PHP/MYSQL/Wordpress
  • How To Resolve Stranded DBA_2PC_PENDING Entries ID 401302.1 (Very Good prooven) Oracle
  • scripts to take listener.log backup Linux/Unix

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

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

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
  • sql_doing_fts.sql Oracle
  • v$backup.status information Oracle
  • Oracle Internal Good Websites 1 Oracle
  • Useful Solaris Commands on 28-SEP-2005 Linux/Unix
  • Jai Shree Ram PHP/MYSQL/Wordpress
  • Find execution plan from dba_hist_sql_plan for a specific SQL_ID and PLAN_HASH_VALUE fplan.sql Oracle
  • Pending Transaction Neighbors Script Oracle
  • Find Time Consuming SQL Statements in Oracle 10g Oracle

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme