Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • find_cons.sql Oracle
  • pvm_metric.sql for gathering report from vmstat tables Oracle
  • Distributed Transaction Troubleshooting. Oracle
  • on IBM-AIX for display Linux/Unix
  • Order by with ROWNUM Oracle
  • PLSQL Table Syntax 1 Oracle
  • Monitor and Trace Unix processes using truss Linux/Unix
  • cold backup scripts to copy locally Linux/Unix
  • Temporary Tablespsace Temp tablespace behaviour Oracle
  • check_copy_progress.sh Linux/Unix
  • SQL_PROFILE – I explaination Oracle
  • findinfo.sql (SQL for getting CPU and Active session info) Oracle
  • reset Sequence Oracle
  • Drop database in Oracle 10g Oracle
  • V$ROLLSTAT status is Full 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
  • 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
  • Python class import from different folders Python/PySpark
  • Read CSV File using Python 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)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (393)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (342)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025
  • Oracle vs MYSQL Architecture differences (For DBAs)24-Jul-2025
  • V$INSTANCE of Oracle in MYSQL24-Jul-2025
  • Day to day MYSQL DBA operations (Compared with Oracle DBA)24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Test Case for Inserting Multiple (2.3 Million rows in 26 Seconds) Oracle
  • How to Use DBMS_STATS to Move Statistics to a Different Database Oracle
  • sbind.sql Find Bind variable from sql_id sqlid Oracle
  • tblwopk.sql tablewopk.sql Oracle
  • Drop tempfiles from database Oracle
  • How to hide author name in WordPress BLOG PHP/MYSQL/Wordpress
  • Implementing Listener Security Oracle
  • Passing from Unix to PLSQL using bind variables Linux/Unix

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme