diff --git a/19-configuration-management/.env b/19-configuration-management/.env new file mode 100644 index 0000000..78a4768 --- /dev/null +++ b/19-configuration-management/.env @@ -0,0 +1,2 @@ +DATABASE_HOST=localhost +DATABASE_PORT=5432 diff --git a/19-configuration-management/README.md b/19-configuration-management/README.md new file mode 100644 index 0000000..e69de29 diff --git a/19-configuration-management/config.ini b/19-configuration-management/config.ini new file mode 100644 index 0000000..1b70efb --- /dev/null +++ b/19-configuration-management/config.ini @@ -0,0 +1,3 @@ +[Database] +host = 127.0.0.1 +port = 3306 diff --git a/19-configuration-management/configparser_example.py b/19-configuration-management/configparser_example.py new file mode 100644 index 0000000..38aa507 --- /dev/null +++ b/19-configuration-management/configparser_example.py @@ -0,0 +1,21 @@ +import configparser + +# create a ConfigParser object +config = configparser.ConfigParser() + +# read configuration from a file +config.read('config.ini') + +# access a value +database_host = config.get('Database', 'host') +database_port = config.getint('Database', 'port') + +print(f"Database Host: {database_host}") +print(f"Database Port: {database_port}") + +# write to a configuration file +config['Database']['host'] = 'localhost' +config['Database']['port'] = '5432' + +with open('config.ini', 'w') as configfile: + config.write(configfile) diff --git a/19-configuration-management/dotenv_example.py b/19-configuration-management/dotenv_example.py new file mode 100644 index 0000000..0296a90 --- /dev/null +++ b/19-configuration-management/dotenv_example.py @@ -0,0 +1,12 @@ +from dotenv import load_dotenv +import os + +# load environment variables from .env file +load_dotenv() + +# access environment variables +database_host = os.getenv('DATABASE_HOST') +database_port = os.getenv('DATABASE_PORT') + +print(f"Database Host: {database_host}") +print(f"Database Port: {database_port}") diff --git a/20-memory-performance/README.md b/20-memory-performance/README.md new file mode 100644 index 0000000..cbf5b38 --- /dev/null +++ b/20-memory-performance/README.md @@ -0,0 +1,41 @@ + +## Overview + +This repo includes scripts for profiling memory usage and performance in Python. + +## What is Memory Profiling? + +Memory profiling is the process of analyzing a program to understand its memory consumption patterns. This involves tracking how much memory your application uses and identifying memory-intensive parts of your code. + +### Why Use Memory Profiling? + +1. **Optimize Memory Usage**: Memory profiling helps identify memory leaks and inefficient memory usage, which can lead to improved performance and reduced memory consumption. +2. **Enhance Performance**: By understanding which parts of your code are using the most memory, you can refactor or optimize those areas to make your application more efficient. +3. **Resource Management**: For applications running on systems with limited resources, efficient memory usage is critical to maintaining stability and performance. + + +## What is Performance Profiling? + +Performance profiling is the process of analyzing a program to determine its execution time and identify bottlenecks. This involves measuring how long different parts of your code take to execute. + +### Why Use Performance Profiling? + +1. **Identify Bottlenecks**: Performance profiling helps pinpoint parts of your code that are slow or inefficient, allowing you to focus optimization efforts where they are needed most. +2. **Improve Efficiency**: By understanding execution times, you can make informed decisions about code improvements to enhance overall performance. +3. **Benchmarking**: Profiling provides benchmarks that help you measure the impact of optimizations and compare different approaches. + + +### Files + +- `memory_profiler_example.py`: Demonstrates how to use the `memory_profiler` library to track memory usage of a Python function. +- `performance_profiling.py`: Uses the `cProfile` module to profile the execution time of a Python function. + + +### Setup + +To use these profiling scripts, make sure you have the necessary libraries installed: + +```bash +pip install memory_profiler +``` + diff --git a/20-memory-performance/memory_profiler_example.py b/20-memory-performance/memory_profiler_example.py new file mode 100644 index 0000000..b056f00 --- /dev/null +++ b/20-memory-performance/memory_profiler_example.py @@ -0,0 +1,14 @@ +from memory_profiler import profile +import time + +@profile +def compute_sum(n): + total = 0 + for i in range(n): + total += i + time.sleep(0.01) # some delay + return total + +if __name__ == "__main__": + result = compute_sum(1000) + print(f"Sum is {result}") diff --git a/20-memory-performance/performance_profiling.py b/20-memory-performance/performance_profiling.py new file mode 100644 index 0000000..4a28e6f --- /dev/null +++ b/20-memory-performance/performance_profiling.py @@ -0,0 +1,27 @@ +import cProfile +import pstats +import io + +def compute_sum(n): + total = 0 + for i in range(n): + total += i + return total + +def main(): + result = compute_sum(100000) + print(f"Sum is {result}") + +if __name__ == "__main__": + pr = cProfile.Profile() + pr.enable() + + main() + + pr.disable() + s = io.StringIO() + sortby = 'cumulative' + ps = pstats.Stats(pr, stream=s).sort_stats(sortby) + ps.print_stats() + + print(s.getvalue())