mikegrouchy.com

Code WTF: Check Ethernet Address

So I guess I am daring, but not many software developers/programmers would publicly disparage their code like I am about to do. I was looking at some code I wrote about 3.5 years ago when I was first learning python during a summer job in the computer science department at my University and this is what I found.

Basically the intention of the code shutdown computers remotely(x number of hosts in a computer lab) in a wakeable state and had a function to do a wake on lan(for x number of wakeable hosts). This is used in a web based lab administration system and it works pretty well.

Here is the code in question(just the Wake on Lan part):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 import socket
 import struct
 import string
 
 def Wol(eth_addr, ip):
     """wake on lan given an ethernet/mac address and IP"""
     byte_addr = []
 
     # check that our eth address is the correct format
     addr = CheckEthFormat(eth_addr)
     if addr == False:
         print "Ethernet/MAC Address in wrong format"
         return False
 
     #take our given ethernet address and put it into 6 sub strings
     for x in range(len(addr)):
          if ((x*2) + 1) > len(addr): break
          byte_addr.append(addr[x*2] + addr[(x*2)+1])
 
     #construct a string with a 6 byte hardware address
     eth_addrB = struct.pack('BBBBBB',int(byte_addr[0]),
             int(byte_addr[1]),
             int(byte_addr[2]),
             int(byte_addr[3]),
             int(byte_addr[4]),
             int(byte_addr[5]))
 
     #construct our message for the Wake on Lan packet
     # 12 leading F's + target machines mac address 16 times
     packet = "F" * 12 + eth_addrB * 16
     #create a datagram socket to send our packet
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
     # send our message
     sock.sendto(packet, ip)
     #close the socket
     sock.close()

So whats the wtf here?

Read the rest of this entry »

Related Posts Plugin

Kind of trying to crowd source this one here. Does anyone know a great related posts plugin(allows you to create a list of related posts at the bottom of every post) for WordPress?

If you have wrote a plugin or know of a good one, let me know in the comments!

Blog Migration

When I started the latest iteration of this blog, I forgot I had backups for my blog so I couldn’t take the SQL export and import it directly to this blog or I would break everything.

Today however, I decided I would put in the bit of effort to get all the old blog posts and comments imported to this blog. To get it to work, I actually had to start a new WordPress blog on this domain in test sandbox I have and then I did the full sql import. Following the import I upgraded the WordPress database using the WordPress database update tool to get it back up to date(the last version of wordpress I used before this was 2.6). After that was done I used the WordPress Export tool to export all the posts/tags/categories and imported them all to this blog.

So now there is an extra ~150 or so posts and ~300 comments, so take a look around and check them out, there might be something you are interested in. Also if you notice anything particularly messed up please leave a comment to let me know.

Linux Command Line Basics

This is not meant to be a complete guide to the Linux command line but it is meant to be a reasonable place to start. I don’t claim to know everything about Linux but I have worked on Linux/Unix machines quite a bit so I am just going to provide the important bits that the absolute beginner will find useful. Hopefully If I get some more time I will post some more advanced tutorials.

1. What is a terminal?

A terminal is a window that lets you interact with the shell. This raises another question, what is the shell? The shell is a program that takes commands inputted by you with the keyboard and gives them to the operating system to perform. In Linux/Unix this used to be the only way to interact with the computer, now there are graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.

Some terminal emulators you may encounter while using a computer running Linux are :

Konsole, xterm, gnome-terminal, rxvt, eterm and many others.

Read the rest of this entry »

Why is the Jython Website so ugly?

I know this might be a shocker, but I love python! Don’t get me wrong, I don’t discriminate against languages. I will use whatever language I feel is best for the job at hand, I have been known to use Java, C, C++, C#, Bash scripting, python and even VB.NET(..shhh don’t tell anyone). Programming languages are tools though so I always choose whatever tools is best for the job. That being said, if the tool doesn’t really matter, I am likely to choose Python.

I feel like Python is a mature language, its very productive, the syntax is very clear(and I think in a lot of ways elegant) and you can do fun things like rapidly test ideas in the interactive console. Python has been helping me solve my problems for years so the other day I was looking around to see if there were any problems I could solve for Python(we are pals like that), so in that vein I decided to check out Jython and see if there was anything I could contribute there.

Read the rest of this entry »

Things I Like: Readability (by Arc90)

I thought I would take a little time out from time to time to highlight software and other things hanging around the internet I like. Today I would like to mention a wonderful bookmarklet by Arc90 called Readability.

From the site:

Reading anything on the Internet has become a full-on nightmare. As media outlets attempt to eke out as much advertising revenue as possible, we’re left trying to put blinders on to mask away all the insanity that surrounds the content we’re trying to read.

It’s almost like listening to talk radio, except the commercials play during the program in the background. It’s a pretty awful experience. Our friend to date has been the trusty “Print View” button. Click it and all the junk goes away. I click it all the time and rarely print. It’s really become the “Peace & Quiet” button for many.

Readability basically address’s this issue of clutter on many web pages and with one click of the bookmarklet hides all the unnecessary cruft on a webpage and leaves you with what you want, the content!

So you don’t need to take my word on it, you can visit the website and see the webcast about Readability or get started and set it up with your preferences from the setup page.

Python and MySQL(db) – Part 1 – the absolute basics

Recently, I have been working on some of the code for MUN’s LabNET, which has a pretty large database component. I figured that along with this work, I would whip up a friendly little tutorial which allows developers to get started using MySQL in conjunction with Python.

First off, I will make the assumption that you at least have some familiarity with Python and MySQL (or any other relational database). Even if you have never used them together, this tutorial will not be very simple to follow. The library for MySQL connectivity that we will be using for this tutorial is the MySQLdb module, which basically handles everything you need to make MySQL work in Python.

Read the rest of this entry »

Scheduling and Interruptions

On hacker news for the last couple of days an essay by Paul Graham has been on the front page, its called Makers Schedule, Managers Schedule.

Paul put into words something I have known for a long time, generally software developers(I use myself and people I know as examples) when interrupted tend to take longer to get back on track to where they once were. I never quite thought about it into how we allocate the day though, unless I am specifically working on a predefined schedule but looking at my workflow I tend to feel more confident about starting bigger things If I am sure I can see it through to the end in one sitting.

When describing to my girlfriend what its like when I am interrupted I always say “If you interrupt me for 15 seconds, I lose 15 minutes of productivity”. This is something I feel that is completely true and falls in line with the above article, if I start writing some code, generally I think about it for a few minutes even though I have the structure mapped out normally(kind of like the whole “measure twice, cut once” idea) and then I start coding and I like to code until I am completed what I am working on. If someone interrupts me for a phone call or to ask me a question, I need to kind of work myself back into what I was thinking about when writing the code I was writing. Generally this involves rereading the code I just wrote and trying to follow exactly what I was doing then continuing.

Sometimes I think Michael from Microserfs had the right Idea, let me lock myself in a room, you can slip kraft-singles and pop tarts under my door until I am done.

Does anyone else feel like this sometimes?

On Lexers, Syntax Highlighting and a good suggestion.

In my last post I mentioned I am working on a code snippet repository for storing and sharing code snippets. In the comments Jason Gedge(a very smart man I might add) mentioned that I should probably store code snippets as XML and then use XSL/XSLT to do the markup for the syntax highlighting. This is one of the many ideas I have thrown around and after some more thought I think it is the one I am going to go with.

That being said, to make things more difficult on myself I think I am going to write a lexer.
From the Wikipedia Article:

In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens. Programs performing lexical analysis are called lexical analyzers or lexers. A lexer is often organized as separate scanner and tokenizer functions, though the boundaries may not be clearly defined.

So the intent is for my lexer to take grammars I write for individual programming languages(it would be nicer if I could actually find a set grammars for common languages and then just use them) and then generate the xml based on the particular language and its parse before I store it in the database.

I then I will just use XSL/XSLT(I think this is what I will do) and use it to transform my XML to XHTML . It won’t accomplish what Jason suggested in allowing for intake of generic code and outputting it into multiple languages, but I think it should solve this sane code storage/syntax highlighting issue.

Needless to say, this should be interesting, I have never written a lexer before so I can only imagine how it will turn out! Ideally, as long as it gets the job done and its relatively fast(light) I will consider it a success.

Code Snippets? I’ll give you code snippets!

Well I actually won’t give you code snippets, but I will give you the ability to store and organize them.

Storing code snippets is not the worlds greatest unconquered problem or anything like that, however that being said I have tried much of the software out there for storing code snippets and I still feel like I want to create my own. I have a few interesting ideas that I think may be really neat for storing and retrieving code snippets(at least I think they are neat), so I have decided to code them.

I cannot imagine I will do anything new with this project that isn’t readily available amongst the dozens of code snippet sites out there but at the very least I will try collate the things I really like into one site and maybe accomplish something different along the way.

Read the rest of this entry »