it's a common practice to store configuration variables in a configuration file and retrieve the properties while they are needed.
Python provides a module called ConfigParser to help managed application configurations. The configParser module can be used to create a configuration file as well as to retrieve property values from a configuration file.
A configuration file in Python is similar to a Windows version of init (.ini) file. The file is divided by “Sections” and “options”. Following is a sample configuration file:
[DEFAULT]
work_directory =/tmp
Log_directory = /var/logs
app_user = app
[DEVELOPMENT]
hostname = mydevhost.com
database = devdb
user_id =devuser
Password =
[STAGE]
hostname = mystagehost.com
database = stagedb
user_id =stageuser
Password =
In the above example, [DEFAULT], [DEVELOPMENT], and [STAGE] are called section. Work_directory, log_directory, hostname,etc are called option.
Here is an example of how to retrieve the properties from the configuration file assuming the above configuration filename is called config.cfg:
import configparser
class GetConfigProperty():
def get_property(self,config_file: str, section: str, property: str) -> str:
property_value: str
config = configparser.ConfigParser()
config.read_file(open('./config/' + config_file))
property_value = config.get( section, property,fallback=None)
return property_value
Now, we can call the above function to get the configuration properties in the application like the following:
getConfigProperty = GetConfigProperty()
working_dir : str = getConfigProperty.get_property("config.cfg", "DEFAULT", "working_dir");
dev_hostname : str = getConfigProperty.get_property("file_config.cfg", "DEVELOPMENT", "hostname");
Related Post: Setup Logging In Python
Friday, March 20, 2020
Saturday, May 11, 2019
Docker Command Cheat Sheet
List docker images on the host
$ docker images
List docker containers that are currently running:
$ docker ps
List Containers
$ docker container ls
Delete an image
$ docker rmi <image id>
$ docker rmi --force <image id> #force delete
Remove all images
$ docker rmi $( docker images -q)
Stop All the containers
$ docker stop $( docker ps -aq)
Remove all containers
$ docker rm $( docker ps -aq)
Log Into an Container
$ docker exec -i -t <container id> sh
Run an Docker Image
$ docker run -i -t <image id>
Docker Documents
Sunday, November 11, 2018
SQL Query To Select Row Number, Rank, And Dese Rank
Use Analytic Functions
SELECT e.name as emp_name, d.name dept_name, e.salary, ROW_NUMBER() OVER(Partition by d.name ORDER BY e.salary DESC) AS row_num FROM employee e join department d on e.dept_id = d.dept_id SELECT e.name as emp_name, d.name dept_name, e.salary, RANK() OVER(Partition by d.name ORDER BY e.salary DESC) AS rank FROM employee e join department d on e.dept_id = d.dept_id SELECT e.name as emp_name, d.name dept_name, e.salary, DENSE_RANK() OVER(Partition by d.name ORDER BY e.salary DESC) AS dense_rank FROM employee e join department d on e.dept_id = d.dept_id
Without Using Analytic Function
select emp.*, (select count(*) from employee emp2 where emp2.dept_id = emp.dept_id
and(emp2.salary > emp.salary or emp2.salary = emp.salary
and emp2.emp_id <= emp.emp_id ) ) as "row_number", (select 1 + count(*) from employee emp2 where emp2.dept_id = emp.dept_id and emp2.salary > emp.salary ) as "rank", (select count(distinct salary) from employee emp2 where emp2.dept_id = emp.dept_id and emp2.salary >= emp.salary ) as "dense_rank" from employee emp;
Subscribe to:
Comments (Atom)