Skip to main content

Writing context managers using Python

We use. We forget. This is for myself so I remember the magic called Context Manager. You already must have used the "with statement" for file handling, threads, socket and other operations. I am going to talk about creating you OWN context managers.

The magic operator to be used is : @contextmanager. It can be imported from the module "contextlib"

To create a context manager, use the decorator contextlib.contextmanager


@contextlib.contextmanager
def getListCounts(x):
    try:
        from collections import Counter
        yield Counter(x)
    finally:
        print 'No of elements in the list : %d' % len(x)


Let's now use the above context manager 
   
Example 1:
>>> with getListCount([10, 10, 12]) as c:
 print "Element occurring the most is %d" % c.most_common()[0][0]

 
Element occurring the most is 10
No of elements in the list : 3

Example 2:
>>> with getListCount([10, 10, 12]) as c:
 print "Element occurring the least is %d" % c.most_common()[-1][0]

 
Element occurring the least is 12
No of elements in the list : 3

Popular posts from this blog

Why should you visit Kashmir sooner?

"If there is paradise on Earth, this is it"
And paradise it was . Early winter is the perfect time to plan for a visit to Kashmir. We will suggest to keep your itinerary  short . And enjoy the best trip of your life. 

   My Visit Month : Mid November 2015

As we were flying close to Srinagar, about 20 minutes before landing , all of a sudden , every passenger in the aircraft was on his toes craning theirs necks over the tiny window to catch a glimpse of the above. A fleet of Snow Giants.


   Visit Meena Bazaar in Dal Lake
  You will find a floating village with houses , shops, and hotels on all kinds of boats.


    A night on a House Boat




Take a Shikara ride.Watch the super clear reflection of the mountain. Spot fishes and water birds.



Take a 2 day stay in Pahalgam. Visit Mini Switzerland first day on a horse ride.


Visit Sonemarg. Enjoy the snow.



On your way to Pahalgam, visit apple orchids . Taste all kind of apples. The golden ones are supposed to be the best.



The dark ominous mou…

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.

What problem are we actually talking about?

While working in offices and restricted environments where firewalls and other invisible barriers stop your requests from going out, you sometimes need a way to temporarily jump over that barrier to get your job done . Example: You might face them while trying to install using pip in command line, trying to clone a personal git repository. 

What is CNTLM exactly?

I think this is best answered by the creator himself. Refer this. You can download it from here. For configuration, you an refer this. There are many uses of CNTLM but I am trying to solve my own problem here. So I will stick to breaking office barriers for now ;) .

Why do I need to use it :
1) Python package installations using PIP
2) Git operations : Cloning and pushing to server.

Here are a set of steps I …

Rajasthan: The Best backpacking destination in India

Rajasthan : The Abode of Rajas


Why Rajasthan indeed?

Suitable for all size of plans . Week long , month long , or a day long.Numerous popular cities to visit.Shopper's paradise.Best forts in India in one State.Diversity - Greenery, Desert, Lakes, Urbanization or Tranquility. You will get it all.Wildlife SanctuariesHeritage cities - Best for people who love cities with a history.Food - A host of popular dishes to cherish. Vegetarians rejoice.Transport - All options are available, abundant and safe. Trains. buses or airways.Budget friendly - One can choose a budget fitting as he likes. Everyone can enjoy irrespective of his pocket strength.
Itinerary:
1.  Pune to Mumbai (Bandra Terminus) (via Bus)
Distance: 159 km
Duration: 3-4 hrs

2. Mumbai to Chittorgarh (via Train 22901 / UDZ SUPER FAST)
Distance: 831 km
Duration: One night stay in Chittorgarh

3. Chittorgarh to Udaipur (via Passenger Train) ,
Distance: 117 km
Duration:  Same day departure for Jodhpur

4. Udaipur to Jodhpur (via St…