Ph: 20070606

Archive

Posts Tagged ‘python’

Bazaar, GIT, Mercurial, Subversion battle royale!

April 1st, 2009

What is the best version control system?

There’s so many version control projects out there for software development, but which one is the best? Linus Torvalds pushes GIT (see the Google TechTalk). I still use subversion, it gets the job done for me, so why move to something else? I don’t need a distributed version of subverison, so what does GIT buy me besides the ability to tell everyone that I now have a DVCS?

In the last week or so i’ve run into 3 or 4 sites/blogs for software apps that either mentioned that they’ve changed over to a new DVCS or they have published documentation letting you know how to extract the latest or stable versions of their abc software through their xyz distributed version control system.

Today I just read that Python is switching to Mercurial and they included a long winded explanation of why they chose that one. In short, it seems it was deemed a better fit for python development. Meanwhile, ecryptfs is using bazaar (bzr). So I guess it’s just a coke vs. pepsi type of fight for some folks…

Almost random bazaar, dvcs, ecryptfs, git, linus, python, subversion, techtalk, vcs

Python: Get unique elements in a list, sorted or unsorted

March 3rd, 2009

I ran into these two methods for grabbing just the unique elements from a list, one keeps the order, the other relies on sorting the list.

def uniq(list)
    # element order preserved
    set = {}
    return [ set.setdefault(x,x) for x in list if x not in set ]

def uniq(list)
    # element order not preserved
    set = {}
    map( set.__setitem__, list, [] )
    return set.keys()

But then I found some strange code that works really fast. Check out this amazing little one liner!

def getUniqueSynset(hasDupes):
    unique_trick = [ uniq for uniq in hasDupes if uniq not in locals()['_[1]'] ]
    return unique_trick

howto, ooo! Shiny..., python code, elements, list, python, sort, trick, unique, unsorted

TKinter GUI demo for python

January 9th, 2009

A simple Tkinter demo to display a window with some buttons on it for a front end installer app. The buttons just quit in this example, but you can see the event handlers towards the bottom that would make it easy to tweak. I’m still looking for a gui builder for Tkinter, because gui design changes can be a pain and I know the requirements will change while i’m developing code. I like calling python via env, works on multiple systems where python might be installed in local or elsewhere…

#!/usr/bin/env python

from Tkinter import *

class InstallerGUI(Frame):
  def __init__(self, master=None):
    Frame.__init__(self, master)
    self.grid()
    self.createWidgets()

  def createWidgets(self):
    self.installButton = Button(self)
    self.installButton['text'] = 'Install the latest version'
    self.installButton['anchor'] = 'nw'
    self.installButton['width'] = 35
    self.installButton['command'] = self.quit
    self.installButton.pack(side=LEFT)
    self.installButton.bind("<Button-1>", self.installButton_Click)

    self.revertButton = Button(self)
    self.revertButton['text'] = 'Revert to a previous installation'
    self.revertButton['anchor'] = 'nw'
    self.revertButton['width'] = 35
    self.revertButton['command'] = self.quit
    self.revertButton.pack(side=LEFT)
    self.revertButton.bind("<Button-1>", self.revertButton_Click)

    self.helpButton = Button(self)
    self.helpButton['text'] = 'Help'
    self.helpButton['anchor'] = 'nw'
    self.helpButton['width'] = 35
    self.helpButton['command'] = self.quit
    self.helpButton.pack(side=LEFT)
    self.helpButton.bind("<Button-1>", self.helpButton_Click)

    self.quitButton = Button(self)
    self.quitButton['text'] = 'Quit'
    self.quitButton['anchor'] = 'nw'
    self.quitButton['width'] = 35
    self.quitButton['command'] = self.quit
    self.quitButton.pack(side=LEFT)

  def installButton_Click(self, event):
    pass

  def revertButton_Click(self, event):
    pass

  def helpButton_Click(self, event):
    pass

app = InstallerGUI()
app.master.title("MyWickedApp")
app.mainloop()

Update: I ended up going with pyGTK. Although this will work, I think utilizing the GTK+ libraries will be better in the long run with developing other apps…

programming design, frames, gui, python, tkinter, widgets

Eclipse freezes randomly using pydev

January 8th, 2009

I’m doing all my python development in Eclipse using the pydev plugins.  At random and without warning, Eclipse keeps freezing on me!  WTF.  Here’s what I get when I kill it:

JVM terminated. Exit code=1
/usr/bin/gij
-Xms40m
-Xmx256m
-XX:MaxPermSize=128m
-Dosgi.sharedConfiguration.area=/usr/lib64/eclipse/configuration
-jar /usr/share/eclipse/startup.jar
-os linux
-ws gtk
-arch x86_64
-showsplash
-launcher /usr/lib64/eclipse/eclipse
-name Eclipse

-startup /usr/share/eclipse/startup.jar
-exitdata 838010
-vm /usr/bin/gij
-vmargs
-Xms40m
-Xmx256m
-XX:MaxPermSize=128m
-Dosgi.sharedConfiguration.area=/usr/lib64/eclipse/configuration
-jar /usr/share/eclipse/startup.jar –launcher.library /usr/lib64/eclipse/plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.0.0.v20070606/eclipse_1017a.so

This happens on Fedora 8 and Fedora 10 on two entirely different machines AND different workspaces, creating different applications.  I was just creating a tkinter application with some basic buttons in a frame and went to run it after already having run it several times before, and poof, the amazing freezer bug strikes again.  w.t.f.

programming, wtf eclipse, fedora, freezes, kill, pydev, python, tkinter, wtf

Iterators in python are about as easy as “hello world” in most other languages

December 28th, 2008

Iterators in python are so easy. I saw some articles on random sites about building generators to do stuff, but in the end they were only trying to iterate through a file line by line so why make it harder on yourself. Declare an iterator of the type you’re dealing with, like lines in a file, and vrooom, off you go.

f = open(infile,’r')
lines = f.readlines()
i = iter(lines)
while true:
.     try:
.        line = i.next()
.    except StopIteration:
.        pass

programming code, easy button, iterators, python

Simple sockets to talk to a scrolling display using python

November 7th, 2008

I need to talk to a little box that has an ip address and accepts tcp input to send on to a giant LED display.  I’m going to send messages across it as if it were the stock market tickers streaming along, or like the rolling display in Times Square that reports the news.

What do I want to use to do this while keeping it simple and straight forward? Java? C++? hmm… no wait I know, Python!!!  It’s just so freaking easy to do in Python.  Python is quickly becoming my favorite language.  There’s so much support for it, its like you can do just about anything in Python now-a-days!

So here’s a basic little test application to send some messages just so I can make sure the communication works.

from socket import *

host = 10.1.2.3

port = 80

s = socket(AF_INET, SOCK_STREAM)

s.connect((host, port))

s.send(”TEST123\n”)

That’s about as simple as it can get.  This won’t get us very much, but it’s a start and it lets me know I am talking to the right host, and that it’s accepting my connection.  If there was some sort of ACL on the little black box or it was listening for UDP, i’d get an immediate “Connection Refused” message when I executed this code and it got to the s.connect line.

I could start here and later build a nice app that makes letters and words scroll by.  Maybe I’ll even connect multiple displays and locate them physically next to each other.  Then I could have a message scroll through one display and then through the next, and the next.  But i’m getting ahead of myself here.

class mysocket:
        def __init__(self, sock=None):
            if sock is None:
                self.sock = socket.socket(
                    socket.AF_INET, socket.SOCK_STREAM)
            else:
                self.sock = sock
        def connect(host, port):
            self.sock.connect((host, port))
        def mysend(msg):
            totalsent = 0
            while totalsent < MSGLEN:
                sent = self.sock.send(msg[totalsent:])
                if sent == 0:
                    raise RuntimeError, "socket connection broken"
                totalsent = totalsent + sent
        def myreceive():
            msg = ''
            while len(msg) < MSGLEN:
                chunk = self.sock.recv(MSGLEN-len(msg))
                if chunk == '':
                    raise RuntimeError, "socket connection broken"
                msg = msg + chunk
            return msg

Linux, programming python, sockets


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics