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

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

Batch loop for wave to mp3 (using lame)

It keeps the input files and sorts both mp3 and wave files nicely in two separate directories.
   1  
   2  @echo off
   3  if NOT EXIST MP3 md MP3
   4  if NOT EXIST WAV md WAV
   5  
   6  for %%f in (*.wav) do (
   7  	cls
   8  	lame -b 96 -V 5 -B 128 -m j --vbr-new -q 5 "%%f" "MP3\%%f.mp3"
   9  	move "%%f" WAV
  10  )

Update DynDNS hostname

See DynDNS Update Specifications <http://www.dyndns.com/developers/specs/syntax.html>.

   1  
   2  curl -v -k -u jakobm "https://members.dyndns.org/nic/update?hostname=jakobm.dyndns.org&myip=217.80.116.128&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"

Filesize with nice units

Simple function to format filesizes in bytes to human-readable units.

   1  
   2  def prettySize(size):
   3  	suffixes = [("B",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
   4  	for suf, lim in suffixes:
   5  		if size > lim:
   6  			continue
   7  		else:
   8  			return round(size/float(lim/2**10),2).__str__()+suf
   9  
  10  print prettySize(213458923)
  11  # Output: 203.57M
  12  
  13  print prettySize(1234)
  14  # Output: 1.21K

Calculate Pi

This script presents a way to compute Pi using a statistic method.
See http://en.wikipedia.org/wiki/Computing_π for further algorithms.

   1  
   2  import random, math
   3  
   4  class calcPi:
   5      def __init__(self):
   6          self.times = pow(10,6)
   7          self.i = 0
   8          self.isnot = 0
   9  
  10      def IsOnCircle(self,x,y):
  11          if math.sqrt(x**2+y**2) < 1:
  12              return True
  13          else:
  14              return False
  15      
  16      def run(self):
  17          for x in range(self.times):
  18              x,y = random.random(),random.random()
  19              if self.IsOnCircle(x,y):
  20                  self.i+=1
  21              else:
  22                  self.isnot+=1
  23      
  24      def getResults(self):
  25          return (float(self.i), float(self.isnot))
  26  
  27      def getPi(self):
  28          self.run()
  29          r = self.getResults()
  30          return r[0]/(r[0]+r[1])*4
  31  
  32  if __name__ == '__main__':
  33      print calcPi().getPi()

Progress bar

This is a simple class to generate char-based progress bars.

Example:
   1  [++++++      ]


   1  
   2  class Bar:
   3  	def __init__(self):
   4  		self.len = 80
   5  		self.chars = (' ', '+')
   6  		self.wrap = ('[', ']')
   7  		self.filledc = 0
   8  
   9  	def fill(self, i):
  10  		assert not (i > 100) or (i < 0)
  11  		self._setP(i)
  12  
  13  	def _setP(self, p):
  14  		self.filledc = int(round(float(self.len*p)/100))
  15  
  16  	def show(self):
  17  		out = []
  18  		out.append(self.wrap[0])
  19  		out.append(self.filledc*self.chars[1])
  20  		out.append((self.len-self.filledc)*self.chars[0])
  21  		out.append(self.wrap[1])
  22  		return "".join(out)
  23  
  24  b = Bar()
  25  b.len = 20
  26  b.fill(20)
  27  print b.show()
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS