5 Replies - 1337 Views - Last Post: 20 July 2014 - 04:47 PM Rate Topic: -----

#1 PyRon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 09:15 AM

Hi all,

I have 0 background in Computer Science. I am a regular coder in Python but when it comes to Python's basic classes such as formatter, StringIO, Abstractwriter and htmllib.HTMLParser, etc. I am unable to understand the code and therefore can not make much use of it.


So I had been understanding this genius code written by someone whom I can not contact because there are no contact details mentioned there. Here is the link:
http://ai-depot.com/...arbitrary-html/


I am unable to see where it is calling methods of TrackingParser. Yet the code works fine. This has taken my entire one day and now it almost seems mind boggling to understand it. Can you help me decoding what this code is doing. I can surely use it as is but understanding it will help me in future.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding Code written with Python's Basic Classes

#2 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,398
  • Joined: 03-December 12

Re: Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 10:57 AM

Looks pretty common to me.

Technically, you don't need to understand something to use it, just how to call it and what it should return.

As for how it calls the methods,

parser = TrackingParser(writer, formatter)
    # Give the parser the raw HTML data.
    parser.feed(html)
    parser.close()


What are you having trouble with? The parser is using the alias of parser.

parser.feed(html)
calls the feed method of the TrackingParser class.

This post has been edited by astonecipher: 20 July 2014 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 12:39 PM

Moved this out of the Advanced Discussion area, as it isn't.
Was This Post Helpful? 0
  • +
  • -

#4 PyRon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Re: Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 01:40 PM

But how is it calculating bytes parsed and length of text parsed. Where is it calling parse_starttag and parse_endtag?

Where is the feed method in TrackingParser?

This post has been edited by andrewsw: 20 July 2014 - 04:24 PM
Reason for edit:: Removed self quote

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,903
  • Joined: 12-December 12

Re: Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 04:24 PM

Although that page is categorized as "tutorial" it is more accurately described as an article. It discusses extracting "useful text" from html.

In particular, if you copy just the code that appears on that page can you run it successfully?
Was This Post Helpful? 0
  • +
  • -

#6 PyRon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Re: Understanding Code written with Python's Basic Classes

Posted 20 July 2014 - 04:47 PM

Yes I can. I just want to understand what is going on in the code. Though the last part which is related to Neural Networks is incomplete without training.txt and in order to generate data myself I need to use LineWriter() which I dont know how to use?

## To get the neural network to learn, we need to gather some data. This is where the earlier LineWriter.output() function comes in handy; it gives us a central point to process all the lines at once, and make a global decision which lines to output.##
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1