Sunday, October 8, 2017

Logging in Python using dynamic log file

Sometimes we need to send logs to a log file supplied dynamically. The standard mechanism of the python is to set the file as part of configuration. I will put down a way to do this.

Let's write a python file which captures the initialization of logger.

Let the file name be init-log.py

Friday, October 6, 2017

Virtualenv in python

A decent python code needs multiple libraries to run. The usual approach is to install those libs and dependencies using pip install.  However when you ship the code to someone else, the environment details need to be provided including the dependencies with the version numbers. It becomes very cumbersome in that way to manage it.

A better way is to create a virtual environment. The virtual environment contains all the required dependencies in a local folder and the whole environment can be shipped. It can now run in any foreign environment without worrying about what is available or not. However this does makes the size of the artifacts quite large.

To create a virtual environment, follow the following command. (You can wrap it in a script)

Tuesday, March 28, 2017

Dictionary in Python

A dictionary is a dictionary. It's a lookup and contains key value pair. Dictionaries are mutable.

#create an empty dictionary
>>> capital = {}

@asssign elements to the dictionary
>>> capital['India'] = 'Delhi'
>>> capital['Germany'] = 'Berlin'

#print the dictionary
>>> capital
{'Germany': 'Berlin', 'India': 'Delhi'}

Monday, March 27, 2017

Tuple in Python

Tuple is like a list. Only difference is it's immutable whereas list is mutable. What it means is that one can change the elements of a list after initial assignment, however the elements of the tuple can't be changed. Tuple in that sense provides a write protection guarantee.

#Create a tuple of Country and Capital and we don't want to change it
#Note that tuple use parenthesis for definition
>>>tuple = (['India','Delhi'],['USA','Washington'],['Germany','Berlin'])

#Trying to change the tuple element fail
>>> tuple[0] = ['India','Pune']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

#accessing element of tuple
>>> tuple[0]
['India', 'Delhi']

#accessing sub-element of tuple
>>> tuple[0][1]
'Delhi'

#be careful you can change sub-element
>>> tuple[0][1] = 'Pune'
>>> tuple
(['India', 'Pune'], ['USA', 'Washington'], ['Germany', 'Berlin'])



Is your Big Data an Asset or Liability?

Balance sheets are funny things. In one stroke, they tell your positives and negatives. And it takes no time for assets to convert into liabilities also. The best examples of assets and liabilities can be taken from our daily life. If the kid turn out to be good (whatever it means to one), the kids are assets otherwise they become liability. A marriage or a partnership can become both asset or liability. 

Companies should also look into their data from a similar perspective. Data in itself has no meaning if you can't mint it. You can boast of having Terabytes and Petabytes of data, having clusters and farms and ranches  and villas and islands of servers to store them.  However if you are not able to make any money about of it, is it even worth the salt?

Counter in Python

Counter is a simple way to find the text occurrences in a given text. You can use it to create tag cloud also. Let's look into an example to see how this works

Let's see the word occurrences from a given url in the web. The code can be used to process any palin text also. Just pass it to the Counter method as a list of words.

Listifying String and Strigifying List in Python

At times we want to convert a text into individual elements and to convert a list of words into texts

Let's say we want to convert the following text into a list of words. Let's have the famous Lorem ipsum text. This text is a scrambled version of De finibus bonorum et malorum, a 1st century BC Latin text by Cicero.