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.
@echo off
if NOT EXIST MP3 md MP3
if NOT EXIST WAV md WAV

for %%f in (*.wav) do (
	cls
	lame -b 96 -V 5 -B 128 -m j --vbr-new -q 5 "%%f" "MP3\%%f.mp3"
	move "%%f" WAV
)

Update DynDNS hostname

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

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.

def prettySize(size):
	suffixes = [("B",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
	for suf, lim in suffixes:
		if size > lim:
			continue
		else:
			return round(size/float(lim/2**10),2).__str__()+suf

print prettySize(213458923)
# Output: 203.57M

print prettySize(1234)
# 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.

import random, math

class calcPi:
    def __init__(self):
        self.times = pow(10,6)
        self.i = 0
        self.isnot = 0

    def IsOnCircle(self,x,y):
        if math.sqrt(x**2+y**2) < 1:
            return True
        else:
            return False
    
    def run(self):
        for x in range(self.times):
            x,y = random.random(),random.random()
            if self.IsOnCircle(x,y):
                self.i+=1
            else:
                self.isnot+=1
    
    def getResults(self):
        return (float(self.i), float(self.isnot))

    def getPi(self):
        self.run()
        r = self.getResults()
        return r[0]/(r[0]+r[1])*4

if __name__ == '__main__':
    print calcPi().getPi()

Progress bar

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

Example:
[++++++      ]


class Bar:
	def __init__(self):
		self.len = 80
		self.chars = (' ', '+')
		self.wrap = ('[', ']')
		self.filledc = 0

	def fill(self, i):
		assert not (i > 100) or (i < 0)
		self._setP(i)

	def _setP(self, p):
		self.filledc = int(round(float(self.len*p)/100))

	def show(self):
		out = []
		out.append(self.wrap[0])
		out.append(self.filledc*self.chars[1])
		out.append((self.len-self.filledc)*self.chars[0])
		out.append(self.wrap[1])
		return "".join(out)

b = Bar()
b.len = 20
b.fill(20)
print b.show()
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS