-
 KDE-Apps.org Applications for the KDE-Desktop 
 GTK-Apps.org Applications using the GTK Toolkit 
 GnomeFiles.org Applications for GNOME 
 MeeGo-Central.org Applications for MeeGo 
 CLI-Apps.org Command Line Applications 
 Qt-Apps.org Free Qt Applications 
 Qt-Prop.org Proprietary Qt Applications 
 Maemo-Apps.org Applications for the Maemo Plattform 
 Java-Apps.org Free Java Applications 
 eyeOS-Apps.org Free eyeOS Applications 
 Wine-Apps.org Wine Applications 
 Server-Apps.org Server Applications 
 apps.ownCloud.com ownCloud Applications 
--
-
 KDE-Look.org Artwork for the KDE-Desktop 
 GNOME-Look.org Artwork for the GNOME-Desktop 
 Xfce-Look.org Artwork for the Xfce-Desktop 
 Box-Look.org Artwork for your Windowmanager 
 E17-Stuff.org Artwork for Enlightenment 
 Beryl-Themes.org Artwork for the Beryl Windowmanager 
 Compiz-Themes.org Artwork for the Compiz Windowmanager 
 EDE-Look.org Themes for your EDE Desktop 
--
-
 Debian-Art.org Stuff for Debian 
 Gentoo-Art.org Artwork for Gentoo Linux 
 SUSE-Art.org Artwork for openSUSE 
 Ubuntu-Art.org Artwork for Ubuntu 
 Kubuntu-Art.org Artwork for Kubuntu 
 LinuxMint-Art.org Artwork for Linux Mint 
 Arch-Stuff.org Art And Stuff for Arch Linux 
 Frugalware-Art.org Themes for Frugalware 
 Fedora-Art.org Artwork for Fedora Linux 
 Mandriva-Art.org Artwork for Mandriva Linux 
--
-
 KDE-Files.org Files for KDE Applications 
 OpenTemplate.org Documents for OpenOffice.org
 GIMPStuff.org Files for GIMP
 InkscapeStuff.org Files for Inkscape
 ScribusStuff.org Files for Scribus
 BlenderStuff.org Textures and Objects for Blender
 VLC-Addons.org Themes and Extensions for VLC
--
-
 KDE-Help.org Support for your KDE Desktop 
 GNOME-Help.org Support for your GNOME Desktop 
 Xfce-Help.org Support for your Xfce Desktop 
--
openDesktop.orgopenDesktop.org:   Applications   Artwork   Linux Distributions   Documents    Linux42.org    OpenSkillz.com   
 
Home
Apps
Artwork
News
Groups
Knowledge
Events
Forum
People
Jobs
Register
Login

-
- News . 
0
votes
click to vote up

Joel Leclerc: Misc python functions


Published Sep 30 2012 via RSS

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). All of these functions are located at https://github.com/MiJyn/relinux/tree/master/src/relinux (most of them are located in utilities.py).

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 config.python3:
        if isinstance(string, unicode):
            return string.encode("utf-8")
    if not isinstance(string, str):
        if config.python3 and isinstance(string, bytes):
            string_ = string.decode("utf-8")
            string = string_
        else:
            string_ = str(string)
            string = string_
    if not is_ascii(string):
        if config.python3:
            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 config.python3:
            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!




BackRead original postSend to a friend

Add comment Add comment
Show all posts




-



 
 
 Who we are
Contact
More about us
Frequently Asked Questions
Register
Twitter
Blog
Explore
Apps
Artwork
Jobs
Knowledge
Events
People
Updates on identi.ca
Updates on Twitter
Content RSS   
Events RSS   

Participate
Groups
Forum
Add Content
Public API
About openDesktop.org
Legal Notice
Spreadshirt Shop
CafePress Shop
Advertising
Sponsor us
Report Abuse
 

Copyright 2007-2016 openDesktop.org Team  
All rights reserved. openDesktop.org is not liable for any content or goods on this site.
All contributors are responsible for the lawfulness of their uploads.
openDesktop is a trademark of the openDesktop.org Team