CNTLM in Office

If you are facing a proxy problem , CNTLM is a general option everybody considers. But the working of cntlm is not always guaranteed. I myself get confused every time I need to use it.


Why do I need to use it :


1) Python package installations using PIP
2) Git operations : Cloning and pushing to server.

So here is a set of steps that I came up with which mostly works . You must follow them exactly in the same sequence mentioned here:



  • Launch Internet Explorer . 
  • Go to Tools > Internet Options > Connections > LAN Settings
  • Under Proxy Server, make sure 'Address' should be 'localhost' and proxy should be the one defined in cntlm config file (cntlm.ini)
         Check for the line in config.in , "Listen 3128" to cross check .  (I'm using 3128). 
  • Save and close.
  • Launch a command prompt. 
  • Execute these 2 commands one by one 

git config --global http.proxy http://localhost:3128
git config --global https.proxy https://localhost:3128

  •  Type "net start cntlm". Wait for it to start.
  • If you want to use the PIP command, type
          pip install --proxy=localhost:3128 package-name

          Note: This is important>  --proxy=localhost:3128  
  

  • For git operations , you don't need above step. 




Quick guide to Logging in different files using Python

This is for quick setup of a logging system . 


Step 1: import logging 


Logging is the build-in module for implementing logging in Python.

Step 2: Create a logger for yourself. 


logger = logging.getLogger(__name__)

Step 3: Set Logger level


logger.setLevel(logging.DEBUG)

Step 4: Create a handler. 


hdlr = logging.FileHandler(settings.BASE_LOG_FILE, mode='w')

Step 5: Create a formatter


formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

Step 6: Set formatter and handler


hdlr.setFormatter(formatter)

logger.addHandler(hdlr)



When and why to use Classes in Python

Note: All programming done in Python 2.7

Why to make a class?

When we need to use some common variables across other functions quite often . If we only one or 2 functions to write , we should not need to write a class. But if we are going to have 4-5 functions that need to use common objects between themselves, then its better to use classes.

What are class methods?

Class methods are methods that you can call on the class itself

A good example of classes . Lets have a BankAccount class for managing Bank Accounts and its related operations.


class BankAccount(object):
    #Class methods are methods that you can call on the class itself
    def __init__(self, balance=0):
        self.balance = balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount

The __init__ function is automatically called when a new instance is created. We have two functions that add or subtract  money from account.

Ok....So lets create 2 accounts


a_account = BankAccount()
b_account = BankAccount()

Now lets deposit something in these accounts


>>> a_account.deposit(100)
>>> b_account.deposit(500)


Let's try to transfer some fund from B to A account. We need to add a "transfer" function to our class.


def transfer(self, other_account, amount):
    self.withdraw(amount)
    other_account.deposit(amount)


>>> b_account.transfer(a_account, 100)
>>> a_account.balance
200
>>> b_account.balance
400

As you can see, how we used classes to perform simple operations between two different accounts.

Customize looks of your object

We can have more control over our objects . Like if we print our object , what will it look.
Normally it looks like this.


>>> a_account
<__main__.BankAccount object at 0x0000000001FB3B38>

We want to show some other info when someone calls up the object itself at the time it is created. We can use the __repr__ function for this.

Lets add this function to our class .


def __repr__(self):
    return "Bank_Account Balance={}".format(self.balance)

One thing to note:

 if __repr__ is defined, and __str__ is not, the object will behave as though __str__=__repr__.

Find more info here 


>>> a_account = BankAccount(1000)
>>> a_account
Bank_Account Balance=1000

If we want to control how it will look when its printed , we should use the __str__ method.


def __str__(self):  # When the object is printed or str(object) is used
    return "Bank_Account is ${}".format(self.balance)


What is Super Class

You might have seen before the keyword being used inside classes. Super here helps us call the original base class __init__function . Whenever I have a method to call in the original baseclass.


class NamedBankAccount(BankAccount):
    def __init__(self, name, *args, **kwargs):
        self.name = name
        super(NamedBankAccount, self).__init__(*args, **kwargs)

Now we create an object of above class.


>>> a = NamedBankAccount('Arc', 1000)
>>> a.name
'Arc'
>>> a.balance
1000
>>> print a
Bank_Account is $1000



As you can see, the 'balance' was auto created by the __init__ of BankAccount class.

Lets add a hello function in our base class and lets add that in our Child class.


class BankAccount(object):
    #Class methods are methods that you can call on the class itself
    def __init__(self, balance=0):
        self.balance = balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
    def transfer(self, other_account, amount):
        self.withdraw(amount)
        other_account.deposit(amount)
    def __repr__(self):
        return "Bank_Account Balance={}".format(self.balance)
    def __str__(self):  # When the object is printed or str(object) is used
        return "Bank_Account is ${}".format(self.balance)
    def hello(self):
        print 'Hi'



class NamedBankAccount(BankAccount):
    def __init__(self, name, *args, **kwargs):
        self.name = name
        super(NamedBankAccount, self).__init__(*args, **kwargs)
        super(NamedBankAccount, self).hello()


>>> a = NamedBankAccount(name='Arc')
Hi

I think this is sufficient proof enough that we can call ANY method using super. It just a convenience provided by python.

Note: Just remember that the same can be achieved simple like this, 


BankAccount.__init__(self, 234)

>>> a = NamedBankAccount(name='Arc')
>>> a
Bank_Account Balance=234


Total Pageviews