Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

bindec.scm

// Convert list of 1s and 0s back to base ten number.

   1  
   2  ; Andrew Pennebaker
   3  ; 5 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3479
   6  
   7  (define bin->dec
   8  	(lambda (b)
   9  		(cond
  10  			((integer? b) b)
  11  			((= (length b) 0) 0)
  12  			(else
  13  				(+
  14  					(* (expt 2 (- (length b) 1)) (car b))
  15  					(bin->dec (cdr b)))))))

decbin.scm

// Converts a base ten integer to a list of 1s and 0s

   1  
   2  ; Andrew Pennebaker
   3  ; 3 Feb 2007
   4  ; License: GPL
   5  ; URL: http://snippets.dzone.com/posts/show/3478
   6  
   7  (define dec->bin
   8  	(lambda (d)
   9  		(cond
  10  			((< d 1) (list 0))
  11  			((= d 1) (list 1))
  12  			((> d 1) (append
  13  				(dec->bin (floor (/ d 2)))
  14  				(list (if (= (modulo d 2) 0) 0 1)))))))

html2txt.py

   1  
   2  #!/usr/bin/env python
   3  
   4  __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
   5  __date__="10 Dec 2006"
   6  __copyright__="Copyright 2006 Andrew Pennebaker"
   7  __license__="GPL"
   8  __version__="0.0.1"
   9  __credits__="Based on http://mail.python.org/pipermail/python-list/2004-November/291562.html"
  10  __URL__="http://snippets.dzone.com/posts/show/3127"
  11  
  12  import htmllib
  13  from sgmllib import SGMLParser
  14  
  15  import sys
  16  
  17  class html2txt(SGMLParser):
  18  	"""html2txt()"""
  19  
  20  	def reset(self):
  21  		SGMLParser.reset(self)
  22  		self.pieces=[]
  23  
  24  	def handle_data(self, text):
  25  		self.pieces.append(text)
  26  
  27  	def unknown_starttag(self, tag, attributes):
  28  		pass
  29  
  30  	def unknown_endtag(self, tag):
  31  		pass
  32  
  33  	def handle_entityref(self, ref):
  34  		try:
  35  			self.pieces.append(htmllib.HTMLParser.entitydefs[ref])
  36  		except KeyError, e:
  37  			self.pieces.append("&"+ref)
  38  
  39  	def output(self):
  40  		return "".join(self.pieces)
  41  
  42  if __name__=="__main__":
  43  	html="".join(sys.stdin.readlines())
  44  
  45  	parser = html2txt()
  46  	parser.feed(html)
  47  	parser.close()
  48  
  49  	print parser.output()
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS