A listing of random software, tips, tweaks, hacks, and tutorials I made for Ubuntu
Misc python functions
September 30, 2012Posted by on
As relinux 0.4 is my first real project in python, I would like to share some functions I made along the way that I found useful (in hopes that it might be useful for others).
You can do whatever you want to these functions, all I ask is to give credit. If you really can’t, that’s fine, but if you can, that would be greatly appreciated :)
Generic UTF-8 function (works on python 2 and 3, still a WIP):
# Check if a string is ASCII or not def is_ascii(s): for c in s: if ord(c) >= 128: return False return True # Convert a string to UTF-8 def utf8(string): if not sys.version_info >= (3, 0): if isinstance(string, unicode): return string.encode("utf-8") if not isinstance(string, str): if sys.version_info >= (3, 0) and isinstance(string, bytes): string_ = string.decode("utf-8") string = string_ else: string_ = str(string) string = string_ if not is_ascii(string): if sys.version_info >= (3, 0): return string return string.decode("utf-8").encode("utf-8") return string
I have no idea why python doesn’t have this built-in, but anyways, here is a list flattener (which works both on python 2 and 3). Most of this code was based on http://stackoverflow.com/a/4676482/999400, but I made some minor style changes (like as if anyone cares :P)
def flatten(list_): nested = True while nested: iter_ = False temp =  for element in list_: if isinstance(element, list): temp.extend(element) iter_ = True else: temp.append(element) nested = iter_ list_ = temp[:] return list_ # Example: # flatten(["test", ["test1", "test2", ["test", "test1"], "test3"], "test4"] -> ["test", "test1", "test2", "test", "test1", "test3", "test4"]
I feel that this function is too simple to even post, but for some reason (maybe lack of caffeine?) I couldn’t figure this out when I needed it. As you can see from the code, this function simply removes duplicate values from an array.
def remDuplicates(arr): returnme =  for i in arr: if not i in returnme: returnme.append(i) return returnme # Example: # remDuplicates(["test", "test1", "test2", "test", "test2"]) -> ["test", "test1", "test2"]
While we are on the subject of nearly useless convenience functions, here is a function that generates an MD5 checksum from a filename (it technically just opens it and reads it through md5’s update function)
import md5 ... def genMD5(file_, blocksize = 65536): if not os.path.isfile(file_): return files = open(file_, "r") buffers = files.read(blocksize) m = hashlib.md5() while len(buffers) > 0: m.update(buffers) if sys.version_info >= (3, 0): buffers = bytes(files.read(blocksize), "utf-8") else: buffers = bytes(files.read(blocksize)) return m.hexdigest() # Example: # genMD5("/path/to/file.txt")
I find that using sys.hexversion is the easiest way to compare python versions, but it’s a bit harder when it comes to displaying it to the user. Sorry for the bad formatting, by the way
def parsePyHex(string1): string = "%x" % string1 count = 0 result = "" for char in string: if count == 0 or count == 2 or count == 4: result += char if count != 4: result += "." elif count == 5: if char.lower() == "f": break else: result += char.lower() elif count == 6: result += char count += 1 return result # Examples: # parsePyHex(sys.hexversion) -> "2.7.3" (under python 2.7.3 final) # parsePyHex(sys.hexversion) -> "3.0.0a1" (under python 3 alpha 1)
After moving relinux to Qt, I tried to remove all references of Tkinter, but I then realized that some of my functions used Tkinter’s StringVar and IntVar. Since they are quite useful, I decided to make my own version of them that supports any python object.
class EventVar(): def __init__(self, **kw): self.__value__ = None self.writenotify =  self.readnotify =  if "value" in kw: self.set(kw["value"]) def set(self, newvalue): self.__value__ = newvalue for i in self.writenotify: i(newvalue) def get(self): for i in self.readnotify: i() return self.__value__ def trace(self, rw, func): if rw.lower() == "r": self.readnotify.append(func) elif rw.lower() == "w": self.writenotify.append(func) # Example: # def myFunc(var): # print("From myFunc: " + str(var)) # v = EventVar(value=None) # v.trace("w", myFunc) # v.set("test")
Hope someone can find use of these!