Python default parameters

Writing functions with default arguments seems like a fairly straightforward task. Default the argument in the function definition and you’re all set. That’s right works like a charm as shown in the following example.

def greetings(name, message = "Good Day"):
print(f'Hello {name}. Have a {message}')
greetings('Jim', 'Good Afternoon')

Returns the following output. Perfect! All good.

Hello John. Have a Good Day
Hello Daniel. Have a Good Day
Hello Jim. Have a Good Afternoon
Hello Michael. Have a Good Day

Now, let’s try to introduce a mutable argument in the function. We’re printing the time of execution for…

I recently got into a situation where I was logging payloads in Anypoint Runtime and realized that the payload contained sensitive information (Email, SSN, etc.). However, this information is highly useful as we are developing applications for debugging purposes but it’s quite a hassle to enable/disable logs and re-deploy applications in lower environment when troubleshooting issues.

Like any other product or language, Mulesoft offers a solution for this problem. When printing the logs, you can define the LEVEL of log, e.g. INFO (default), DEBUG, ERROR, etc. The default option INFO would always print the logs in runtime. However, logs that…

If you ever feel the need to host a database on a server and do not have the resources to do so, you can setup the database locally on a laptop and expose the service to the outside world using ngrok. Please be aware that using ngrok has it’s own advantages and disadvantages and the connectivity isn’t as reliable as compared to AWS RDS. However, it’s faster to implement and cost effective for running POC’s and test apps for demo or test purposes.

Question: What is ngrok?

Answer: Ngrok exposes local servers behind NATs and firewalls to the public internet…

Over time as you create pull requests and move your changes from temporary branches over to the dev or master branch, a lot of branches can pile up on your local system and can be noisy when you are trying to list all the branches.

If you’re using GitHub, it conveniently gives you the option to delete the temporary branch after merging into dev or master. But, the local git repository still shows the remote and local branches even though they have been deleted from GitHub. When branches get deleted on origin, your local repository won’t take notice of that…

There are so many ways of hosting custom JAR files and importing them for your projects. You could use a Nexus server, store them in GitHub or even install them locally on all machines. But in a production environment, you may not have access to the build server to go and install custom JAR files. Adding them to your Nexus server in that case (if available) might seem like a good idea, but it comes with a drawback that every developer will have to setup a local Nexus server to keep the build process in sync with the production setup.

Here are instructions to install and run mcrypt on Debian 10 with PHP 7.3+

Check PHP Version

$ php -version
PHP 7.3.9-1~deb10u1 (cli) (built: Sep 18 2019 10:33:23) ( NTS )

Check if mcrypt is installed

$ php -m | grep mcrypt

Install pre-requisites

sudo apt-get install php-dev libmcrypt-dev php-pear

Install mcrypt PHP module

$ sudo pecl channel-update
Updating channel ""
Update of Channel "" succeeded

$ sudo pecl install channel://
Build process completed successfully
Installing '/usr/lib/php/20180731/'
install ok: channel://
configuration option "php_ini" is not set to php.ini location
You should add "" to php.ini


Ever feel the need to sort a JSON by keys, OR sorting a dictionary by keys? Python doesn’t allow that.

Question: Is there an alternative?

Answer: Yes!

Sorting JSON or dictionary by keys in Python

You cannot sort a dictionary by keys, but if the need is to print it out, you could covert it to a JSON and the resulting JSON output can be sorted by keys. Likewise, if you have JSON that you’d like to sort, you can first convert it to a dictionary and then convert it back to JSON and this time sort it by keys. And this is true for a multi-dimentional dictionary and JSON.

Converting dictionary to a sorted JSON

sorted_json = json.dumps(dict_data, sort_keys=True) # This JSON will be sorted recursively

Converting unsorted JSON to sorted JSON by keys

import jsondict_data = json.loads(unsorted_json)
sorted_json = json.dumps(dict_data, sort_keys=True) # This JSON will be sorted recursively

Using psycopg2 PostgreSQL library with Python

Using python/psycopg2 and wondering how life would be so much easier if you could get the query results to be a dictionary and you could get the value by using a keyword on the dictionary. psycopg2 by default returns results as a tuple so you have to use row[0], etc. to get the values. e.g. the code looks something like this

qSelect = "SELECT first_name, last_name, address FROM employee"cursor.execute(qSelect)results = cursor.fetchall()
for row in results:
print("First Name: {}".format(row[0]))
print("Last Name: {}".format(row[1]))
print("Address: {}".format(row[2]))

With the ease of deployment that SaaS services bring, most organizations are moving their infrastructure in the cloud. The ease of deployment can also lead to increasing costs if you do not plan ahead of time. Spinning up a new database for every new customer can easily be automated today, but down the line that would increase the hosting costs. At the same time, sharing customer data within a common database could result in leaking sensitive information across customers which could harm your reputation as a software provider.

Question: What are we trying to learn from this article?

  1. How to…

Anyone ever told you to have several layers of security to protect your application? Damn right. As an application developer or a DB admin, how can you ensure that you’ve setup the right set of security principles for your database? This article will help you answer some of those questions and hopefully make you feel secure about your database and maybe help you have a sound sleep at night.

Segregate privileges

The general principle of segregating privileges to the database is that “If you shouldn’t be using it, you shouldn’t have access to it”. …

Verma Varun

Mastering automation, improving efficiency and connecting systems.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store