From 09ac2aecbdfdfe27fc20108cc0d844f13ae1479d Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Tue, 4 Nov 2014 13:06:32 +0100 Subject: [PATCH] Even more bullsh*t --- benchmark.py | 25 +++++++++++++++++ bin2hex.py | 4 +++ chunksizer.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ ds.py | 20 ++++++++++++++ fib.py | 21 +++++++++++++++ gcd.py | 16 +++++++++++ test.py | 5 ++++ 7 files changed, 166 insertions(+) create mode 100644 benchmark.py create mode 100644 bin2hex.py create mode 100644 chunksizer.py create mode 100644 ds.py create mode 100644 fib.py create mode 100644 gcd.py create mode 100644 test.py diff --git a/benchmark.py b/benchmark.py new file mode 100644 index 0000000..91ebd22 --- /dev/null +++ b/benchmark.py @@ -0,0 +1,25 @@ +import gc +import timeit +import time + +class Timer: + def __init__(self, timer=None, disable_gc=False, verbose=True): + if timer is None: + timer = timeit.default_timer + self.timer = timer + self.disable_gc = disable_gc + self.verbose = verbose + self.start = self.end = self.interval = None + def __enter__(self): + if self.disable_gc: + self.gc_state = gc.isenabled() + gc.disable() + self.start = self.timer() + return self + def __exit__(self, *args): + self.end = self.timer() + if self.disable_gc and self.gc_state: + gc.enable() + self.interval = self.end - self.start + if self.verbose: + print('time taken: %f seconds' % self.interval) diff --git a/bin2hex.py b/bin2hex.py new file mode 100644 index 0000000..61fb71f --- /dev/null +++ b/bin2hex.py @@ -0,0 +1,4 @@ +while True: + r = int(input(">"),2) + print("%X" % r) + diff --git a/chunksizer.py b/chunksizer.py new file mode 100644 index 0000000..986771c --- /dev/null +++ b/chunksizer.py @@ -0,0 +1,75 @@ +# -*- coding: cp1252 -*- +import Tkinter, tkFileDialog, tkMessageBox, os + +def getsize(directory): + total_size = 0 + for dirpath, dirnames, filenames in os.walk(directory): + for f in filenames: + fp = os.path.join(dirpath, f) + total_size += os.path.getsize(fp) + return total_size + +def calcChunkCount(chunksizeKB, total_size): + chunksizeB = chunksizeKB * 1024 + chunkcount = total_size / chunksizeB + return chunkcount + +def chunklabel(size, dirsize, parent): + chunklabel = Tkinter.Label(parent, text=str(size)+"KB: " + str(calcChunkCount(size, dirsize))) + return chunklabel + +def choosedir(): + path = tkFileDialog.askdirectory() + dirsize = getsize(path) + mainFrame = Tkinter.LabelFrame(window, text=path) + mainFrame.pack() + + resultsFrame = Tkinter.Frame(mainFrame) + resultsFrame.pack() + + infoFrame = Tkinter.LabelFrame(resultsFrame, text="Infos") + labPath = Tkinter.Label(infoFrame, text="Verzeichnis: " + path ) + labSize = Tkinter.Label(infoFrame, text="Groesse: " + str(dirsize/1024) + "KB" ) + labPath.pack() + labSize.pack() + infoFrame.pack(side="left") + + chunksizeFrame = Tkinter.LabelFrame(resultsFrame, text="Chunk-Counts") + chunk64K = chunklabel(64, dirsize, chunksizeFrame) + chunk64K.pack() + chunk128K = chunklabel(128, dirsize, chunksizeFrame) + chunk128K.pack() + chunk256K = chunklabel(256, dirsize, chunksizeFrame) + chunk256K.pack() + chunk512K = chunklabel(512, dirsize, chunksizeFrame) + chunk512K.pack() + chunk1024K = chunklabel(1024, dirsize, chunksizeFrame) + chunk1024K.pack() + chunk2048K = chunklabel(2048, dirsize, chunksizeFrame) + chunk2048K.pack() + chunk4096K = chunklabel(4096, dirsize, chunksizeFrame) + chunk4096K.pack() + chunk8192K = chunklabel(8192, dirsize, chunksizeFrame) + chunk8192K.pack() + chunksizeFrame.pack(side="right") + + buttonFrame2 = Tkinter.Frame(mainFrame) + closeBtn = Tkinter.Button(buttonFrame2, text="schliessen", command=mainFrame.destroy) + closeBtn.pack() + buttonFrame2.pack() + + +window = Tkinter.Tk() +window.title("Torrent Chunksize Calculator by BSOD") +window.minsize(width=400, height=200) +l = Tkinter.Label(window, text="dieses Tool soll euch beim Torrents erstellen helfen :)") +l.pack() + +buttonFrame = Tkinter.Frame() +choosedirBtn = Tkinter.Button(buttonFrame, text="neues Verzeichnis für Berechnung wählen", command=choosedir) +choosedirBtn.pack(side="left") +quitBtn = Tkinter.Button(buttonFrame, text="Beenden", command=window.destroy) +quitBtn.pack(side="right") +buttonFrame.pack() + +window.mainloop() diff --git a/ds.py b/ds.py new file mode 100644 index 0000000..b54c734 --- /dev/null +++ b/ds.py @@ -0,0 +1,20 @@ +class Time: + def __init__(self, hour, minute): + self.hour = hour + self.minute = minute + + def addminutes(self,dminutes): + self.minute += dminutes + self.hour += int(self.minute / 60) + self.minute = self.minute % 60 + + def __str__(self): + return "%02d:%02d" % (self.hour,self.minute) + + +t = Time(7,30) + +for i in range(8): + print("%d.DS: %s" % (i+1,str(t))) + t.addminutes(90+20) + diff --git a/fib.py b/fib.py new file mode 100644 index 0000000..9ac1ddf --- /dev/null +++ b/fib.py @@ -0,0 +1,21 @@ +from functools import lru_cache + +@lru_cache(maxsize=1000) +def fibc(n): + if n < 2: + return 1 + else: + return fibc(n-1)+fibc(n-2) + + +def fibs(n): + i = 1 + a = 1 + b = 1 + while i < n: + (a,b) = (b,a+b) + i+=1 + return b + +print(fibc(1000)) +print(fibs(1000)) \ No newline at end of file diff --git a/gcd.py b/gcd.py new file mode 100644 index 0000000..c8f8ce0 --- /dev/null +++ b/gcd.py @@ -0,0 +1,16 @@ +import benchmark +import random + +def gcd(a,b): + while a != b: + if a > b: + a = a - b + else: + b = b - a + + return a + +with benchmark.Timer(): + for i in range(10000): + gcd(random.randint(2,10000), random.randint(2,10000)) + diff --git a/test.py b/test.py new file mode 100644 index 0000000..e0d85a9 --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ + +for x in [1]: + print(x) +else: + print("List empty")