Understanding Code

Discussion in 'Python' started by subhabangalore@gmail.com, Nov 13, 2012.

  1. Guest

    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).

    If any one of the expert members of the room kindly help me to do this.

    Thanking You in Advance,
    Regards,
    Subhabrata.
    , Nov 13, 2012
    #1
    1. Advertising

  2. Peter Otten Guest

    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.
    Peter Otten, Nov 13, 2012
    #2
    1. Advertising

  3. Guest

    On Tuesday, November 13, 2012 4:12:52 PM UTC+5:30, Peter Otten wrote:
    > 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.


    Dear Sir,

    Thank you for your kind guidance.
    I tried to do the following exercises,

    (i) On dict.get():

    >>> tel = {'jack': 4098, 'sape': 4139, 'obama':3059,'blair':3301}
    >>> dict.get('obama')


    >>> tel.get('obama')

    3059

    >>> for i in tel:

    x1=tel.get(i)
    print x1


    4139
    3301
    4098
    3059
    >>>
    >>> tel.get('blair',0)

    3301
    >>>
    >>> tel.get('blair',0)+1

    3302
    >>>



    (ii) On enumerate:
    >>> list1=["Man","Woman","Gentleman","Lady","Sir","Madam"]
    >>> for i,j in enumerate(list1):

    print i,j


    0 Man
    1 Woman
    2 Gentleman
    3 Lady
    4 Sir
    5 Madam
    >>>


    (iii) Trying to check the values individually:
    >>> 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
    print key
    self.total[key] = self.total.get(key, 0) + 1
    self.count += 1


    >>> x1=Calculate()
    >>> x1.add("x", [1,2,3])

    ('x', 0, 1)
    ('x', 1, 2)
    ('x', 2, 3)

    >>> 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)
    print freq
    result *= freq/self.prior[cls]
    print result


    >>> x2=Calculate()
    >>> x2.add("x", [7,8,9])
    >>> x2.discr("x", [7,8,9])

    1
    1.0
    1
    1.0
    1
    1.0
    >>>


    Based on these exercises,I tried to we can say,
    self.total is dictionary format,and key is cls,idx,val, where cls is assigned in the self.prior.

    The next function is more or less same but here cls is called from earlier function.

    Please let me know how I am addressing it and if I have to do any extra bit?

    Regards,
    Subhabrata.
    , Nov 16, 2012
    #3
  4. Guest

    On Tuesday, November 13, 2012 4:12:52 PM UTC+5:30, Peter Otten wrote:
    > 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.


    Dear Sir,

    Thank you for your kind guidance.
    I tried to do the following exercises,

    (i) On dict.get():

    >>> tel = {'jack': 4098, 'sape': 4139, 'obama':3059,'blair':3301}
    >>> dict.get('obama')


    >>> tel.get('obama')

    3059

    >>> for i in tel:

    x1=tel.get(i)
    print x1


    4139
    3301
    4098
    3059
    >>>
    >>> tel.get('blair',0)

    3301
    >>>
    >>> tel.get('blair',0)+1

    3302
    >>>



    (ii) On enumerate:
    >>> list1=["Man","Woman","Gentleman","Lady","Sir","Madam"]
    >>> for i,j in enumerate(list1):

    print i,j


    0 Man
    1 Woman
    2 Gentleman
    3 Lady
    4 Sir
    5 Madam
    >>>


    (iii) Trying to check the values individually:
    >>> 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
    print key
    self.total[key] = self.total.get(key, 0) + 1
    self.count += 1


    >>> x1=Calculate()
    >>> x1.add("x", [1,2,3])

    ('x', 0, 1)
    ('x', 1, 2)
    ('x', 2, 3)

    >>> 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)
    print freq
    result *= freq/self.prior[cls]
    print result


    >>> x2=Calculate()
    >>> x2.add("x", [7,8,9])
    >>> x2.discr("x", [7,8,9])

    1
    1.0
    1
    1.0
    1
    1.0
    >>>


    Based on these exercises,I tried to we can say,
    self.total is dictionary format,and key is cls,idx,val, where cls is assigned in the self.prior.

    The next function is more or less same but here cls is called from earlier function.

    Please let me know how I am addressing it and if I have to do any extra bit?

    Regards,
    Subhabrata.
    , Nov 16, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,152
    Andy Dingley
    Oct 1, 2005
  2. Dhruva Hein

    Help understanding code

    Dhruva Hein, Apr 10, 2005, in forum: Python
    Replies:
    2
    Views:
    320
    Bruno Desthuilliers
    Apr 11, 2005
  3. Fredrik Lundh

    Re: Help understanding code

    Fredrik Lundh, Apr 10, 2005, in forum: Python
    Replies:
    1
    Views:
    328
    Duncan Booth
    Apr 11, 2005
  4. Thomas Ploch
    Replies:
    0
    Views:
    336
    Thomas Ploch
    Nov 17, 2006
  5. yezi

    Code understanding problem

    yezi, Nov 30, 2005, in forum: C Programming
    Replies:
    2
    Views:
    301
    Niklas Norrthon
    Dec 1, 2005
Loading...

Share This Page