Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33233

Re: Understanding Code

From Peter Otten <__peter__@web.de>
Subject Re: Understanding Code
Date 2012-11-13 11:41 +0100
Organization None
References <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3620.1352803323.27098.python-list@python.org> (permalink)

Show all headers | View raw


subhabangalore@gmail.com wrote:

> Dear Group,
> To improve my code writing I am trying to read good codes. Now, I have
> received a code,as given below,(apology for slight indentation errors) the
> code is running well. Now to comprehend the code, I am looking to
> understand it completely.
> 
> class Calculate:
>       def __init__(self):
>         self.prior = {}
>         self.total = {}
>         self.count = 0
>       def add(self, cls, obs):
>         self.prior[cls] = self.prior.get(cls, 0) + 1
>         for idx, val in enumerate(obs):
>             key = cls, idx, val
>             self.total[key] = self.total.get(key, 0) + 1
>         self.count += 1
>       def discr(self, cls, obs):
>         result = self.prior[cls]/self.count
>         for idx, val in enumerate(obs):
>             freq = self.total.get((cls, idx, val), 0)
>             result *= freq/self.prior[cls]
>         return result
>       def classify(self, obs):
>         candidates = [(self.discr(c, obs), c) for c in self.prior]
>         return max(candidates)[1]
> 
> I am not understanding many parts of it, I am understanding many parts of
> it also.
> 
> So I am looking for an exercise what are the things I should know to
> understand it, (please do not give answers I would get back with the
> answers in a week and would discuss even how to write better than this).

Start with running the code for the simplest piece of the class:
>>> c = Calculate()
>>> c.add("x", [1,2,3])

Then inspect the attributes:

>>> c.prior
{'x': 1}
>>> c.total
{('x', 2, 3): 1, ('x', 1, 2): 1, ('x', 0, 1): 1}
>>> c.count

Now read the code for Calculate.add(). Do you understand what

>         self.prior[cls] = self.prior.get(cls, 0) + 1

does? Experiment with a dict and its get() method in the interactive 
interpreter. Next to the loop.

>         for idx, val in enumerate(obs):
>             key = cls, idx, val
>             self.total[key] = self.total.get(key, 0) + 1
>         self.count += 1

Do you understand what enumerate() does? If not read its documentation with

>>> help(enumerate)

Do you understand what key looks like? If you don't add a print statement

>         for idx, val in enumerate(obs):
>             key = cls, idx, val
              print key
>             self.total[key] = self.total.get(key, 0) + 1
>         self.count += 1

What does

>             self.total[key] = self.total.get(key, 0) + 1

do? Note that this line is very similar to

>         self.prior[cls] = self.prior.get(cls, 0) + 1

which you have studied before.

>         self.count += 1

This like the rest of your class is left as an exercise. The routine is 
always the same: 

- break parts that you don't understand into smaller parts
- consult the documentation on unknown classes, functions, methods, 
preferrably with help(some_obj) or dir(some_obj)
- run portions of the code or similar code in the interactive interpreter or 
with a little throw-away script.
- add print statements to inspect variables at interesting points in your 
script.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Understanding Code subhabangalore@gmail.com - 2012-11-13 01:05 -0800
  Re: Understanding Code Peter Otten <__peter__@web.de> - 2012-11-13 11:41 +0100
    Re: Understanding Code subhabangalore@gmail.com - 2012-11-16 12:39 -0800
    Re: Understanding Code subhabangalore@gmail.com - 2012-11-16 12:39 -0800

csiph-web