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


Groups > comp.lang.python > #33233

Re: Understanding Code

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'classes,': 0.05; 'interpreter.': 0.07; 'val': 0.07; 'before.': 0.09; 'dict': 0.09; 'loop.': 0.09; 'methods,': 0.09; 'portions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'exercise': 0.13; '1):': 0.16; '2):': 0.16; '3):': 0.16; 'add(self,': 0.16; 'attributes:': 0.16; 'class:': 0.16; 'errors)': 0.16; 'idx,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'simplest': 0.16; 'this).': 0.16; 'candidates': 0.17; 'wrote:': 0.17; 'script.': 0.17; 'variables': 0.17; '>>>': 0.18; 'code,': 0.18; 'trying': 0.21; 'subject:Code': 0.22; 'statement': 0.23; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; 'header:X-Complaints- To:1': 0.28; 'rest': 0.28; 'run': 0.28; 'consult': 0.29; 'indentation': 0.29; 'inspect': 0.29; 'piece': 0.29; 'routine': 0.29; 'statements': 0.29; 'points': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'code': 0.31; 'running': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'similar': 0.35; 'next': 0.35; 'add': 0.36; 'received:org': 0.36; 'smaller': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'group,': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'email addr:gmail.com': 0.63; 'dear': 0.66; 'exercise.': 0.84; 'experiment': 0.84; 'does?': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Understanding Code
Date Tue, 13 Nov 2012 11:41:55 +0100
Organization None
References <45923b5c-f0bd-4367-84f9-ba08a260da69@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b7d9.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3620.1352803323.27098.python-list@python.org> (permalink)
Lines 95
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352803323 news.xs4all.nl 6985 [2001:888:2000:d::a6]:45651
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33233

Show key headers only | 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