Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Metalink Note: Note:250655.1 : ADDM Basics USING THE AUTOMATIC DATABASE DIAGNOSTIC MONITOR Oracle
  • How to remove blank lines using vi editor command Linux/Unix
  • create trigger syntax Oracle
  • More info about /proc folder and its relation with processes. Linux/Unix
  • ORA-00064: object is too large to allocate on this O/S during startup Oracle
  • How does one overcome the Unix 2 Gig file limit? Linux/Unix
  • Flowers Resize datafiles Oracle
  • True Session Wait Activity in Oracle 10g Verygood Oracle
  • Oracle Statspack survival Guide Oracle
  • How to analyze statspack or AWR report. Oracle
  • Finding locked objects Oracle
  • How To Limit The Access To The Database So That Only One User Per Schema Are Connected (One Concurrent User Per Schema) Oracle
  • get_aix_vmstat.ksh Oracle
  • Deleting first line and lastline of a file using sed Linux/Unix
  • PHP code to add WordPress posts in bulk programmatically PHP/MYSQL/Wordpress

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
  • Add new columns in dataframe Python/PySpark
  • Read CSV file using PySpark Python/PySpark
  • How to connect to Oracle Database with Wallet with Python. Oracle
  • Getting started with notebook 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

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

Recent Posts

  • 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
  • Reading config file from other folder inside class24-Sep-2024
  • Python class import from different folders22-Sep-2024
  • Transfer SQL Profiles from One database to other database.05-Sep-2024
  • Load testing on Oracle 19C RAC with HammerDB18-Jan-2024

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • find_string_in_database.sql Oracle
  • Read CSV File using Python Python/PySpark
  • tblwopk.sql tablewopk.sql Oracle
  • longtx.sql Oracle
  • create a folder in multiple places Linux/Unix
  • For Perl DBI installation and testing program PHP/MYSQL/Wordpress
  • Oracle Metalink useful notes Oracle
  • Add new columns in dataframe Python/PySpark

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme