<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sriram sridhar</title>
    <description>The latest articles on DEV Community by sriram sridhar (@sriramsridhar).</description>
    <link>https://dev.to/sriramsridhar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F98%2F8370902e-e17a-4588-9e69-ae8ec9e90230.jpg</url>
      <title>DEV Community: sriram sridhar</title>
      <link>https://dev.to/sriramsridhar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sriramsridhar"/>
    <language>en</language>
    <item>
      <title>HOCON - secret behind .conf files</title>
      <dc:creator>sriram sridhar</dc:creator>
      <pubDate>Thu, 19 Dec 2024 08:43:48 +0000</pubDate>
      <link>https://dev.to/sriramsridhar/hocon-secret-behind-conf-files-3051</link>
      <guid>https://dev.to/sriramsridhar/hocon-secret-behind-conf-files-3051</guid>
      <description>&lt;p&gt;HOCON (Human-Optimized Config Object Notation) is a flexible and user-friendly configuration format often used in .conf files. It builds on JSON but introduces several enhancements that make it more human-readable and easier to work with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Syntax Simplicity&lt;/em&gt;: HOCON allows for relaxed syntax:
Commas between fields are optional.
Quotes are not always required around keys or strings.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Inheritance&lt;/em&gt;: It supports configuration merging and inheritance, enabling overrides or extensions of base configurations.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Comments&lt;/em&gt;: Unlike JSON, HOCON supports comments (# or //), making files easier to document.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Substitutions&lt;/em&gt;: It allows variable substitution using placeholders (${}) for dynamic configurations.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Conciseness&lt;/em&gt;: Supports multi-line strings, unquoted keys, and compact object definitions.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Extensibility&lt;/em&gt;: Easily integrates with tools like the Typesafe Config library, often used in Scala and Java applications, especially with frameworks like Akka and Play.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To Interact with .conf files in python - you need pyhocon library&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install pyhocon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below is the sample code to create config files on runtime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pyhocon import ConfigFactory, HOCONConverter
import json

# Create a configuration object
config = ConfigFactory.parse_string("""
app {
    name = "MyApp"
    version = "1.0.0"
    features = {
        enable_feature_x = true
        enable_feature_y = false
    }
    database {
        url = "jdbc:postgresql://localhost:5432/mydb"
        user = "db_user"
        password = "db_password"
    }
}
""")

# Save the configuration to a file
with open('config.conf', 'w') as file:
    file.write(HOCONConverter.convert(config, 'hocon'))
print("HOCON file created: config.conf")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can read the created files as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pyhocon import ConfigFactory

# Load the configuration file
config = ConfigFactory.parse_file('config.conf')

# Access configuration values
app_name = config.get('app.name')
db_url = config.get('app.database.url')
enable_feature_x = config.get('app.features.enable_feature_x')

# Print configuration values
print(f"App Name: {app_name}")
print(f"Database URL: {db_url}")
print(f"Is Feature X Enabled? {enable_feature_x}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the output will be as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HOCON file created: config.conf
App Name: MyApp
Database URL: jdbc:postgresql://localhost:5432/mydb
Is Feature X Enabled? True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can flag features with the help of configuration management. &lt;/li&gt;
&lt;li&gt;You can refresh the configuration over periodic intervals or expose an API endpoint to refresh the configuration of the running application. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Library link : &lt;a href="https://github.com/chimpler/pyhocon" rel="noopener noreferrer"&gt;https://github.com/chimpler/pyhocon&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you found this helpful, let me know by leaving a 👍 or a comment!, or if you think this post could help someone, feel free to share it! Thank you very much! 😃&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>pyhocon</category>
      <category>tooling</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
