Saturday, December 21, 2013

Feed the Beast Ubuntu install script

This is a simple install script I made for Feed the Beast. It will set up the launcher in the actual applications folder, which makes it easier to add to steam and your desktop. Also included the icon for it.

#!/bin/bash

USRDIR=/usr/share/feedthebeast

if [ "$(id -u)" != "0" ]; then
   echo "Must be root." 1>&2
   read
   exit 1
fi

echo "Setting up install..."
mkdir "$USRDIR"
cd "$USRDIR"

echo "Downloading files..."
wget -O FTB_Launcher.jar "http://www.creeperrepo.net/direct/FTB2/881b4fb9d6df8ed81144c9e09c867e6b/launcher%5EFTB_Launcher.jar"
wget -O ftbicon.png "https://lh5.googleusercontent.com/VGBHxrBBaQL1YZmxT2bcxOTWaE-HsAPaziXtisc5kFRVsOqKWWwOEc28uTPqM7lRsg=w1278-h518"

echo "Adding application..."
cat <<_END > /usr/games/feedthebeast
#!/bin/bash

java -jar $USRDIR/FTB_Launcher.jar
_END
chmod +x /usr/bin/feedthebeast

cd /usr/share/applications

cat <<_END > feedthebeast.desktop
[Desktop Entry]
Name=Feed the Beast
GenericName=Feed the Beast
Comment=Feed the Beast
Exec=/usr/games/feedthebeast
Terminal=false
Type=Application
Icon=/usr/share/feedthebeast/ftbicon.png
_END

echo "Adding man page..."
cd /tmp
cat <<_END > /tmp/feedthebeast.6
.\" Manpage for Feed The Beast.
.TH man 6 "$(date +'%d %b %Y')" "1.0" "feedthebeast man page"
.SH NAME
feedthebeast \- Launch client
.SH SYNOPSIS
feedthebeast
.SH DESCRIPTION
Launches Feed the Beast client launcher
_END
gzip -c /tmp/feedthebeast.6 > /usr/share/man/man6/feedthebeast.6.gz

echo "Updating database..."
cd /usr/share/man/man6
mandb -q

echo "Installation complete."
echo "May require relog to show up."
read -p "Press enter to end. "

Thursday, December 19, 2013

Purge old man pages for apropos

This is something I wanted to do for a while on Ubuntu, and I'm not sure if it will work on other distributions. The main problem is that I would remove a program and the man page for it would remain. This gets annoying when I'm searching for stuff and end up double checking on what was removed or decide to try a command I removed. The solution is simple, but takes a few minutes to run. As root, run these commands.

cd /usr/share/man
rm -R /var/cache/man
mandb

As simple as that and all the old stuff gets removed. This just simply removed the cached manual pages then reconstructs it.

Monday, September 23, 2013

Verizon Samsung Galaxy SIII Rooting and Unauthorized Software

*** This includes information for getting back the original phone setup without losing data and can be used for rooting and unlocking the bootloader (see near end of post)

I was playing around with my GS3 that I had rooted, and I wanted to try out a custom ROM. I didn't bother reading about it and am still pretty new to smart phones, so I risked screwing up or even bricking my phone. So I got the custom ROM on their and restarted my phone and was met with a yellow triangle and a warning about detecting unauthorized software and to take my phone to the nearest Verizon store to fix it.

After a bit of searching, I found a way to fix it. So the first thing you need is a Windows computer. This won't work on Wine and can be a bit buggy through a virtual machine. Keep in mind, no data will be lost, but this will reset the system back a ways.
  1. Install the Samsung USB driver
  2. Download Odin 3.07
  3. Download Kies Home GS3
  4. Boot the phone into Odin recovery by holding down the volume down, home and power button at the same time (don't let go when it vibrates the first time)
  5. Start up Odin and connect the phone, one of the boxes should turn blue which means it's ready
  6. On the bootloader, browse to Kies, it will take a while for it to check out, once done just run it
Now the next time you restart, all should be back to the way it was, only a bit out of date.

Now onto rooting and unlocking the bootloader. This can be done with Linux or Windows, but for it to work on Linux, you need Java up to the very latest version, probably want to use the official version.
  1. Download Casual
  2. Turn on USB debugging (System settings -> Developer options)
  3. Connect and select to root
  4. To unlock the bootloader, first install the Clockworkmod Recovery
  5. Then select to unlock the bootloader
Like magic, everything works, hopefully. After I got it all working, I used ROM Manager to install the Cyanogen Mod.

Friday, August 16, 2013

Apps for Rooted Android Phones

I just recently rooted my Samsung Galaxy S3, and figured in the spirit of rooting, I'd mention some of the apps I found useful. The two obvious apps were Root Browser and SuperSU, but these are more so background things to have. The main reason for me rooting was to get better handling and removal of bloatware.

Clean Master
This is an app that lets you remove all that useless junk taking up space from your phone. You can clean out caches, remove system applications (be careful with this), bulk uninstall, clear out tasks, and delete private data. Also can create a widget shortcut that lets you selectively remove tasks from memory.

Disk Digger
Okay, so let's say you goofed and deleted something you actually needed. How do you get it back? Well, disk digger is a nice little recovery tool to get that information back. While it is not guaranteed 100%, no recovery process is. Something like this is more so a last ditch effort to possibly save your butt.

Greenify
This nifty little app may earn you some extra battery life. It will list off to you apps that may potentially drain battery life and allows you to hibernate them. I can't be all too sure if it has helped me any, but I am hoping it is and not just a placebo effect.

Those are the only ones I have found that are actually useful so far. Most of the other things I wanted didn't work well and there are a whole lot that I don't have any need for, despite being on many "must have" apps for rooted users. And even further, a lot did the same stuff as others. I also didn't put a custom ROM on, but I may experiment with that later.

Friday, July 19, 2013

Online Privacy Advice

A lot of people browse the web. A good portion of people browsing the web don't realize how often they are being tracked. Even the people that know they are being tracked still do very minimal covering of their privacy. I'm going to explain a bit about how I personally try to secure a little more privacy.

For starters, I use Firefox. The reason for this is there are a lot of good addons and Firefox just has more control, in my opinion. There were times when I was trying to spoof some request to a site using only Javascript in the browser and Chrome shot it down for being "insecure." I feel Firefox recognizes that the user should be in control of their browsing, not the server. Chrome won't even let you force new windows into tabs, which gets annoying having multiple windows because of popups.

Now with a clean install of Firefox, there are some options you may want to set. I personally tell Firefox not to remember history, searches or download history and clear cookies when I close out. I also recommend checking off "Tell websites I do not want to be tracked." These options are all under Edit > Preferences in the Privacy tab. Telling websites you do not want to be tracked sends the server a DNT header, which means Do Not Track. While not all websites support it and it is up to the website to decide whether to support it or not, I think it's best to at the very least ask.

So with that, we now look to addons to help further secure our privacy. So here are the ones I use:

Ad Block Plus

Ads often have stuff for tracking tacked on with them. Not to mention if the same ad appears on multiple sites, they can see all those sites you are visiting with the ads. While a lot of websites are maintained by ad revenue, some are just obnoxious. Luckily ABP lets you filter ads through if you want or just allow all ads for any site you want... like say this site?

NoScript

NoScript has a lot of security, but can be a pain in the butt at times to always keep it active. It lets you selectively allow or disallow Javascript to be executed. You have a whitelist you can add different source domains to, but I've heard people complain it lags if you have too big of a whitelist. I personally keep a button up that allows me to temporarily allow all on a page just to make things easier. On top of this, it adds protection for things like XSS and clickjacking (essentially it relays information about where you clicked or keys you pressed). It also checks the URL for things like SQLi and other types of attacks.

RefControl

Despite a couple bad reviews on the addon page, it works as expected for me. What you want to do after installing this is change the default for all to block, then add an exception for any sites you want, like say this site? This basically will control it so any requests going out to other sources get no referer, so they won't know where the request came from. This also gets around certain leeching prevention measures certain sites use. You could also change it to say it's from the root of the site or use your own custom referer. Could always use a custom one that contains SQLi and XSS, then just wait and see what poorly designed site falls victim, but that would be mean.

BetterPrivacy

This is an obvious addon to use. It does a lot of things and I keep it around because why not. It takes control over certain stuff, prevents various things from happening and in general is to help keep away tracking stuff.

There are lots of other addons out there that aid in privacy, but these are the three I use. While these are great, I highly recommend adding exceptions for trusted sites and sites that you want to support who rely on ad revenue to keep going. I also think it's important to understand that some tracking is necessary to make the Internet and search engines more usable. So be sure to remember to make exceptions when it is actually a good idea, keep the good parts of the web growing.

Wednesday, July 17, 2013

Multiple Operating Systems on One Flashdrive from Linux

Recently I decided to take some old flash drives I have not been using and turn them into tools for things like security and data recovery. The problem became I had so many tools I wanted to cram on them, but only a handful of flashdrives. Furthermore, a good portion of what I wanted was small and putting them on their own dedicated drive was a bad use of space.

So now the hunt was on to turn one of the larger flash drives into a multi-boot. Normally I use UNetBootin to create bootable flash drives. I did this for Kali and PartedMagic because I started with making those tools first. Now after much trying to use UNetBootin to create multiple operating systems on one drive, tried multiple partitions, I went to Google. Google gave some possible ways to do it, but they were inefficient. So finally I found something late at night.

Multisystem is a quick and easy way to set this up. Despite it being in French (according to Google), but I only speak English and can work it with no problem, so it's all good. You can load operating systems onto it by dragging and dropping. You can view more options by clicking the button on the left hand side with the eyeball. To see what any of the buttons do, just hover over them. Everything else should be pretty straight-forward.

Now after doing all of this, I found one slight problem. Installing tinycore is done slightly incorrect and makes it so that you can't setup programs for loading on the boot as a result. So if you want to set up tinycore, here's what you should do after it is all set up. First, you want to browse the files. Change the folder "cde" to "tce". Next, open up boot/grub/grub.cfg. Look for something along the line of this in the file:

#MULTISYSTEM_MENU_DEBUT|12-07-2013-00:13:33-278461639|cde|multisystem-tinycore|12Mio|
menuentry "Tiny Core Linux" {
linux /cde/boot/vmlinuz quiet cde showapps desktop=flwm_topside
initrd /cde/boot/core.gz
}
#MULTISYSTEM_MENU_FIN|12-07-2013-00:13:33-278461639|cde|multisystem-tinycore|12Mio|

Now you want to change it so that all the places it says "cde" say "tce". So the end result of the example posted looks like this.

#MULTISYSTEM_MENU_DEBUT|12-07-2013-00:13:33-278461639|tce|multisystem-tinycore|12Mio|
menuentry "Tiny Core Linux" {
linux /tce/boot/vmlinuz quiet tce showapps desktop=flwm_topside
initrd /tce/boot/core.gz
}
#MULTISYSTEM_MENU_FIN|12-07-2013-00:13:33-278461639|tce|multisystem-tinycore|12Mio|

I have not come across any other problems yet, but I haven't really messed around with everything on it yet.

Wednesday, July 3, 2013

Damn Vulnerable Linux Full Install on Hard Drive

Damn Vulnerable Linux (DVL) is a Linux distribution that was made purposefully insecure and configured poorly for a learning experience to teach things like exploitation. I managed to find an ISO of it off of some torrent a while back and never really did anything with it. Recently, I got an old laptop that was too broken to do what was wanted with it, so I figured I may as well use it for something. So I installed DVL on to it so I have a machine I can experiment with and get back to learning hacking and other such things. Now most of what I found online was installing into a virtual machine, and the directions were also wrong on quite a few steps. So using my limited knowledge, I played around until I got it right. So here's a quick step-by-step to install DVL on a USB flash drive. I'll try to set up a download link at some point as well.

Put the image on a flash drive, I used Unetbootin.

When booting up the computer with the flash drive, remember to check the boot order in the BIOS so that the flash drive is booted first.

When the system loads, log in. Username is root, password is toor.

Now you should be in the prompt. These instructions are for a clean install. First thing we want to do here is identify what device is where, as far as the hard drive and the USB drive. For me, the hard drive was /dev/hda and the USB drive was /dev/sda. This may change if you have multiple hard drives. We need to tweak some stuff on the hard drive, so we want to unmount it first. Check what is mounted in /mnt, then unmount every folder related to the hard drive. For me, it was just this.

umount /mnt/hda

Next we want to repartition  everything because why not. Run this command, change the hda part if you're setting up on a different system.

fdisk /dev/hda

Now in fdisk, you want to delete any partitions. Press p then enter to see all partitions. Delete all of them. The letter d then enter should do the trick, if there's more than one, just repeat starting at 1 and moving up until it says there are no more. Now we want to write a partition. Press n then enter sticking with the default values (just press enter for the prompts) because that makes life easy. Now to save it, press w then enter and it should take a moment to write the partitions. After this, you should be back at the original prompt.

Okay, now we have a partition, but we have not formatted it to a file system type. So if you've been working on hda, just follow along, if not, remember to change hda to your device. Run this command.

mkfs.ext3 /dev/hda1

Let that run and BAM, ext3 partition. The tutorial I found didn't put the number, which lead to everything completely screwing up, so it's important. If not, it gives a warning message I should have listened to.

So now we want to mount back our newly partitioned and formatted space.

mkdir /mnt/dvl
mount /dev/hda1 /mnt/dvl

Okay, now we should be ready and raring to go to the GUI!

startx

Okay, now in the GUI, we do some fun stuff. In the bottom right hand corner is a German flag with a "de" written on it (short for deustch, for those that didn't know and are curious). Right click on that to bring up the language settings and change that to your locale. Trying to use an American keyboard on a German layout does not work at all.

Okay, now that that is out of the way, in the bottom left corner is the thing for the KDE start menu. Click on that, go to the "Be ReSlaxed" > System > BackTrack Installer.

Leave the first box blank, set the second box to /mnt/dvl if it isn't already. Change the radio buttons from Live to Real. Click the install button and let it do its thing. Once it's finished, hold and because we're not done yet. You can click the close button then go to the logout option under the start menu. Now we should be back at the prompt.

At this prompt, you want to put in this command.

lilo -v -b /dev/hda

If you have a different hard drive setup, this command may need to be different. If that is the case, take a moment to look more into the lilo man pages because I don't know much about it.

If all went well, type this command.

poweroff

After it shuts down, remove the USB drive, start it back up and make sure everything works. It took myself 3 times because I kept goofing up, first time I accidentally wiped the USB drive. Second time I forgot to put lilo on. Third time was the charm.

Thursday, June 6, 2013

Functional Programming in Python

A while back, I tried looking into functional programming in Python. Most of what I saw and read made little to no sense. After spending quite some time playing with Haskell, it began to become clearer what functional programming is and how to do it. I think the main problem is that while Python can approach things from a functional standpoint, its very nature makes functional programming hard as it is not directly reflective of it.

So, the first question to be answered is what is functional programming. Functional programming is a style of programming, like object oriented, structured, procedural and the like. While languages can be designed to be more so for a specific style, you can often use other styles of programming. The two key points of functional programming is that every function must return a value and you do not want to track states. Variables have states, and these states can change. State changes are considered "side effects" and are not functional because everything needs to be constant. When things are constant, you can always expect the same output when running the same case. If the same case does not yield the same result, then it has a side effect, which is what you want to avoid in pure functional code.

Other aspects of functional style programming can include higher order functions. A higher-order function is rather useful, and if you have ever use map or filter, you've used a higher-order function. Higher-order functions will take a function as an argument and/or return a function.

Higher-order functions are very useful for making code more versatile and reusable. Notice with map, we can reuse it over and over and to change how it works, we simply change an argument and that can change its entire purpose. Folds also are in the same category, in Python it is the reduce function. The reduce function takes on a left fold.

Other higher-order functions you might have seen also take the form of decorators. While a decorator has a special syntax, it still takes a function as an argument, then returns a function. While not all decorators are functional, they are higher-order functions.

Another piece of functional programming is anonymous functions. In Python, this is using the lambda keyword. The lambda keyword creates a function that is more functional by design in that you don't store variables, there is no step-by-step procedure and the end result it returned. This is not to say you can't branch, that can be accomplished with the ternary operator. However, looping cannot be done as loops rely on states and checking of states.

So using some of the things mentioned, a quick bit of Python code in a functional design may look like this.

reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])

That is in essence, the sum function. So great, we created code that already does what other code, what's the big deal? Let's modify this a bit to get something more complex.

reduce(lambda x, y: x + y, map(lambda x: 0.5 * x, [1, 0, 0])

Like that, I've created a dot operator function (with some inputs from my perceptron post). Okay, so it looks long, so let's clean it up and create it into an actual function.

def dotProduct (weight, inputs):
    return sum(map(lambda x: weight * x, inputs))

Notice we do not store variables (ignore the argument names beings treated as variables) and we return a value. On top of that, we use the higher order function map and an anonymous function. I replace the reduce with sum because it only makes sense.

Now here's the problem, at the heart of it all, Python cannot create true functional programming. Methods like reduce, map, sum, filter and all that cools stuff, use looping. Granted if it didn't, since Python has no pattern matching syntax, if we did not use looping and fully functional recursion, each function would end up with blocks of if/elif/else statements to match the case style pattern matching.

Other "functional" programming techniques used in Python include list comprehension (modeled after Haskell's) and generator expressions (reminiscent of thunks). Python list comprehension in the root of it all is a loop. Now the reason generator expressions aren't more common, despite their ability to represent infinite data, are slow like thunks.

All that aside, functional programming. Functions must return a value, always, in every case. There must be no side-effects (if code gets too long, you can asign pieces to variables, but you must treat them as constants).

This is a broad overview of some of the key components of functional programming style. Stay tuned for more parts where I get into other stuff more specifically.

Saturday, May 18, 2013

Perceptrons in Haskell

I was talking to some friends about artificial intelligence and wanting to learn something a bit more complicated than some rigid pattern matching. So I was linked an article about perceptrons. Now as far as I've learned about them, a perceptron can learn linear patterns. The learning part is what makes it cool, the linear part makes it a bit restricted without some more advanced stuff, but I'm not that far into it yet. So let's just dive into how a perceptron works.

So a perceptron has a "weight" that it uses to get a value from the inputs using a dot product, compares it to a threshold and gives a binary output. The output is then compared to an expected output, if this is incorrect, it will attempt to "learn" by adjusting the weights.

So the first thing we need to know is what a dot product is. A dot product takes two equal length sets of data and returns a single product.

-- weights (dot) inputValues
dotProduct :: Fractional a => [a] -> [a] -> a
dotProduct (w:ws) (i:is) =
    w * i + dotProduct ws is

dotProduct [] [] =
    0.0

Mathematically, it is the summation of weightn times inputn. So far so good, however we want a datatype to represent the perceptron. The way I did it was to have a list of weights and the threshold, I just figured it would be good to keep with it as the threshold is when the perceptron should know when to fire. This one is nice and easy to understand.

data Perceptron a = Perceptron {
    threshold   :: a,   -- When it should fire
    weights     :: [a]  -- The weight each value is worth
} deriving Show

I didn't do any enforcement of type because the functions handle all of that. I actually kept any error handling or pattern matching to a minimum because if you're not going to use it right, well then there's really no recovery from that point.

Now the next thing we want to do is make a function to create a perceptron because writing out a record syntax in Haskell is a bit of a nuisance when we can make it more compact and easy to read kind of sort of.

initPerceptrons :: Fractional a => a -> [a] -> Perceptron a
initPerceptrons t w =
    Perceptron {
        threshold = t,
        weights = w
    }

Okay, so now that we have a function to create perceptrons, we want to make a function to teach the perceptrons. To teach a perceptron, we alter the weights. The process of teaching involves changing every weight in the correct direction by a certain rate, meaning it may take multiple iterations to get the correct weights. The equation is simply this:

weightn = weightn + learning_rate × (expected_output - actual_output) × input

Converting this to code is a bit of a two-step because I'm using the record syntax.

teach :: Fractional a => Perceptron a -> [a] -> a -> a -> Perceptron a
teach (Perceptron { threshold = t, weights = w }) inputs err rate =
    Perceptron {
        threshold = t,
        weights = getWeights w inputs rate err
    }
    where
        getWeights :: Fractional a => [a] -> [a] -> a -> a -> [a]
        getWeights (w:ws) (i:is) rate err =
            w + (rate * err * i) : getWeights ws is rate err

        getWeights [] [] _ _ = []

If you have been following closely, you may notice that err is in place of (expected_output - actual_output). That is because it would never change per calculation on top of the value is used elsewhere outside of this function. As a result, I decided to just reduce it in place of the larger picture. This won't become apparent until the full example I have is shown.

Now there's one more thing I want to cover that I have not seen anyone in anything I've read really get into, however it was mentioned on the wiki page for perceptrons. That is, when working with binary inputs, a zero represents a negative input (-1). With some testing done by a friend and myself, when we change the 0 inputs with a -1 for the teaching process, the amount of iterations needed to learn were greatly reduced. This to myself also makes sense because a zero input should still have signficance as far as weight when working with binary. So that's how I do it and I made a quick little function to do it for me with some list comprehension because why not.

setBinInputs inputs =
    [if i == 0 then (-1) else 1 | i <- inputs]

Now this is all I used to implement a test case. My test case will teach OR logic to starting weights all of zero. Read all the way to see why there are 3 inputs (summary in comments).

testCaseOR = testCaseOR1 $ initPerceptrons 0.5 [0, 0, 0]

testCaseOR1 ps =
    testCaseOR2 0 is os ps
    where
        is =
            [ [1, 0, 0]  -- All the inputs
            , [1, 0, 1]  -- First input remains constant
            , [1, 1, 0]  -- This takes place of a bias
            , [1, 1, 1]] -- It's the zeroth order term

        os = [ 0, 1, 1, 1] -- Boring expected outputs

testCaseOR2 errCount (i:is) (o:os) ps
    -- No error, no need to teach
    -- Can try to if you want, weights wouldn't change
    | err == 0 = testCaseOR2 errCount is os ps
    -- This means there was an error, time to get some learning done
    | otherwise =
        testCaseOR2 (errCount + 1) is os (teach ps (setBinInputs i) err rate)
    where
        rate = 0.1 -- Too large or small can cause a problem

        out =
            if ((weights ps) `dotProduct` i) > (threshold ps)
                then 1
                else 0

        err = o - out

testCaseOR2 errCount [] [] ps
    -- There were no errors, everything works
    | errCount == 0 = ps
    -- There were errors, need to iterate again
    | otherwise =
        testCaseOR1 ps

You'll have to excuse the imperative undertone, however when doing this, it made sense to keep some state information. It's also a bit messy, but that's what happens when you're tired and try to do something rather than opt in for some sleep.

Now as to why there are three inputs is a bit strange and I don't fully understand it myself, but here's my best attempt at explaining what I know. It is a term that cannot change. The bias itself can be shown in one of two ways, adding it to the dot product of the inputs or as a weight itself (the example uses it as a weight). The bias can be any non-zero number. When we set it up like it is in the example, it can be any constant non-zero number but traditionally people use one. Without the bias, the result would always be zero because of some property about planes and lines that I don't quite understand.

Either way, the bottom line is if you remove that, you get an infinite loop and nothing works.

Hope this helps someone. If you see any errors or something I overlooked, let me know so I can fix it.

Thursday, May 2, 2013

The cogs of functools.partial

(SOURCE CODE AT BOTTOM)

I noticed people ending up at my site looking for how functools.partial in Python works. I didn't know the answer, but I figured it would be worth looking into since I think partial function application has some good potential to it. My findings where a bit deeper and more complicated than I anticipated, so feel free to correct me if I get something wrong.

Now the rough pure Python example given on the documents page is actually pretty easy to pick apart. The design is a function that creates a function that calls a function. Might seem hard to follow at first, but it's similar to a decorator.

def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        newkeywords.update(fkeywords)
        return func(*(args + fargs), **newkeywords)
    newfunc.func = func
    newfunc.args = args
    newfunc.keywords = keywords
    return newfunc

First off, partial will return a function. The function returned is responsible for storing arguments and keyword arguments as well as the function itself. When the return function is called, it appends the arguments then merges the keyword arguments (meaning keyword arguments can overwrite one that is already there), calls the argument with all that, then returns the value.

As simple as this is, it is a very rough explanation of how the real partial function actually works. One of the first differences is the guts of partial is written in C. My guess is this is to improve speed. Another difference is that partial returns the type partial and not a function. While this is treated like a function, it's a type.

Now as to how it works, this is a bit confusing. From what I gather, it creates a partial object that stores arguments (args), keyword arguments (keywords), and a function (func), all of which are actually rather easy to access.

>>> part = functools.partial(dict, a=1, b=2)
>>> dir(part)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', 'args', 'func', 'keywords']
>>> part.keywords
{'a': 1, 'b': 2}
>>> type(part)
<type 'functools.partial'>
>>> part.keywords = {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: readonly attribute

The last part is the kicker. Attributes cannot be modified like any other object.

When the partial object is called, it goes through a process of appending on arguments, merging keywords (updating), then calls it with those arguments and keyword arguments. So a similar concept and pattern occurs. Advantages of using C is that it is faster and the readonly is a nice touch, as partial applications should use read only. I'm personally not a fan of being to update keyword arguments, seems inconsistent and like it could lead to misunderstandings and unnecessary side effects (from a functional programming standpoint, where I grew accustomed to partial applications and currying).

Now for the source code:
http://hg.python.org/cpython/file/c6880edaf6f3/Modules/_functoolsmodule.c

Wednesday, April 10, 2013

Resistor Calculator wxPython

So recently I decided to take a serious attempt at programming with using a GUI. While it has not been easy, the results are very satisfying. This program was entirely from scratch to get my feet wet, but I think my next project will be converting an already existing program to a GUI.

I chose wx over Tkinter for a couple reasons. The first was wx looks native while Tkinter looks like a Java application. Also, much like a Java application, however the windows are made with Tkinter, they have a bad habit of glitching out and making pieces illegible. The downside to wx is that it is not included with Python, however the wx packages are easy enough to get a hold of (in many different languages) and easy to install.

I'm happy with the end result, both the GUI and the code. I spent some extra time afterwards cleaning up the code, expanding variable names and commenting. This proved useful since I deleted a bunch of code that didn't do anything. Granted it's not perfect, I'm happy that overall it works.

You can check it out on my site here, or just copy/paste the source code.

#!/usr/bin/env python
# Author: Matthew DeSantis
#
# Resistor Calc

import wx

# Colors aranged by value (index=value)
COLOR = [
    'black',
    'brown',
    'red',
    'orange',
    'yellow',
    'green',
    'blue',
    'violet',
    'grey',
    'white']

# Colors because the default ones suck
COLORCODE = {
    "black":    "#000000",
    "brown":    "#a52a2a",
    "red":      "#ff0000",
    "orange":   "#ffa500",
    "yellow":   "#ffff00",
    "green":    "#008000",
    "blue":     "#0000ff",
    "violet":   "#800080",
    "grey":     "#808080",
    "white":    "#ffffff",
    "silver":   "#c0c0c0",
    "gold":     "#d4a017"}

# Multipliers in a dictionary, organized by value for ease of reading
MULTIPLIER = {
    1e-2:   'silver',
    1e-1:   'gold',
    1e0:    'black',
    1e1:    'brown',
    1e2:    'red',
    1e3:    'orange',
    1e4:    'yellow',
    1e5:    'green',
    1e6:    'blue',
    1e7:    'violet',}

# Converts a number to a list of resistor colors of equivalent value
def getColors(num):
    # To-be return value
    returnVals = []
    failReturnVals = [COLOR[0], COLOR[0], MULTIPLIER[1e0]]
    # Process the number
    indexPlaceholder = 0
    numStr = str(num)
    bandValues = ""

    # For anything smaller than 0
    if num < 1 and num >= 0.01:
        while indexPlaceholder < len(numStr):
            # Discard leading 0's and decimal
            if numStr[indexPlaceholder] == "0" or numStr[indexPlaceholder] == ".":
                indexPlaceholder += 1
            else:
                break

        # It's all zeroes, time to leave
        else:
            return failReturnVals

        bandValues = numStr[indexPlaceholder:]

        # Short a value, needs a black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        # Ready return color values
        before = True

        for value in bandValues:
            if len(returnVals) < 2:
                returnVals.append(COLOR[int(value)])
            elif value != "0":
                ret.append(COLOR[int(value)])
            else:
                break

    # And everything else
    elif num >= 1 and num <= 9990000000:
        indexPlaceholder = 2
        bandValues = numStr[:indexPlaceholder]

        # If there's 4 stripes
        if (num / float(bandValues)) % 10:
            indexPlaceholder = 3
            bandValues = numStr[:indexPlaceholder]

        # If there's a third band to add
        if len(numStr) > indexPlaceholder:
            if numStr[indexPlaceholder] != "0":
                bandValues = numStr[:indexPlaceholder + 1]

        # Needs another black band
        if len(bandValues) < 2:
            bandValues = bandValues + "0"

        for value in bandValues:
            if value == ".":
                continue

            returnVals.append(COLOR[int(value)])

    # Outside the limit
    else:
        return failReturnVals

    # Get the multiplier
    returnVals.append(MULTIPLIER[round(num / float(bandValues.replace(".", "")), 2)])

    return returnVals

#List of colors to Ohms
def getOhms(colors):
    bandValues = ""
    # Key storage
    multiply = 0

    # Get the index numbers of the colors
    while len(colors) > 1:
        bandValues = bandValues + str(COLOR.index(colors.pop(0)))

    # Find the key (multiplier) based on color (no easy way to do this)
    for key, value in MULTIPLIER.items():
        if value == colors[0]:
            multiply = key
            break

    # Color value * multiplier
    return float(bandValues) * multiply

def main():
    # Initialize app
    app = wx.App()
    frame = Window(None, "Resistor Calc")
    frame.display()
    app.MainLoop()

# The GUI class setup
class Window(wx.Frame):
    def __init__(self, parent, title):
        # Initialize the frame
        super(Window, self).__init__(
            parent,
            title=title,
            style=wx.DEFAULT_FRAME_STYLE,
            size=(550, 200))

        # UI setup
        self.uiInit()
        self.evtInit()
        self.colorSet()

        # Control variables
        self.lock = False
        self.prvs = self.ohmstxt.GetValue()

    def uiInit(self):
        # Third band list (needs optional added)
        self.r3colors = COLOR[:]
        self.r3colors.insert(0, "-")

        # Fourth band has some extra as well (organize by values)
        self.r4colors = COLOR[:-2]
        self.r4colors.insert(0, "gold")
        self.r4colors.insert(0, "silver")

        # Create panel and sub panels for colored boxes
        self.panel = wx.Panel(self)

        self.pr1 = wx.Panel(self.panel)
        self.pr2 = wx.Panel(self.panel)
        self.pr3 = wx.Panel(self.panel)
        self.pr4 = wx.Panel(self.panel)

        # Initialize font
        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(12)

        # Boxes for resizing and aligning
        # 3 tall
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        # First row
        lblohms = wx.StaticText(self.panel, label="Ohms:")
        lblohms.SetFont(font)

        self.ohmstxt = wx.TextCtrl(self.panel, value="0.0")

        # Second row
        lblcolor = wx.StaticText(self.panel, label="Colors:")
        lblcolor.SetFont(font)

        self.r1 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r2 = wx.ComboBox(
            self.panel,
            choices=COLOR,
            style=wx.CB_READONLY)

        self.r3 = wx.ComboBox(
            self.panel,
            choices=self.r3colors,
            style=wx.CB_READONLY)

        self.r4 = wx.ComboBox(
            self.panel,
            choices=self.r4colors,
            style=wx.CB_READONLY)

        # Create first row
        hbox1.Add(lblohms, flag=wx.RIGHT, border=8)
        hbox1.Add(self.ohmstxt, proportion=1)

        # Create second row
        hbox2.AddMany([
            (lblcolor, wx.RIGHT, 8),
            (self.r1,),
            (self.r2,),
            (self.r3,),
            (self.r4,)])

        # Create third row
        hbox3.AddMany([
            (self.pr1, 1, wx.EXPAND),
            (self.pr2, 1, wx.EXPAND),
            (self.pr3, 1, wx.EXPAND),
            (self.pr4, 1, wx.EXPAND)])

        # Insert rows with padding
        self.vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(hbox3, 1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
        self.vbox.Add((-1, 10))

        # Sets everything in place on the window
        self.panel.SetSizer(self.vbox)

        # Set combobox selections (not intialized by default
        self.r1.SetSelection(0)
        self.r2.SetSelection(0)
        self.r3.SetSelection(0)
        self.r4.SetSelection(2)

    # Initialize events
    def evtInit(self):
        self.Bind(wx.EVT_COMBOBOX, self.onCBChange)
        self.Bind(wx.EVT_TEXT, self.onTextChange)

    # Combobox was changed
    def onCBChange(self, e):
        self.lock = True

        bandColors = []
        thirdBand = self.r3.GetValue()

        bandColors.append(self.r1.GetValue())
        bandColors.append(self.r2.GetValue())

        # Third band is optional, check to see if it's actually used
        if thirdBand != "-":
            bandColors.append(thirdBand)

        bandColors.append(self.r4.GetValue())

        self.ohmstxt.SetValue(str(getOhms(bandColors)))
        self.colorSet()

    # Text change
    def onTextChange(self, e):
        ohms = self.ohmstxt.GetValue()

        # This is because event fires twice for some unknown reason
        if self.lock or ohms == self.prvs:
            self.prvs = ohms
            self.lock = False
            return None

        # If no value given, default to zero
        if not ohms:
            ohms = "0"

        try:
            bandColors = getColors(float(ohms))
            self.r1.SetSelection(COLOR.index(bandColors[0]))
            self.r2.SetSelection(COLOR.index(bandColors[1]))

            # All 4 bands are present
            if len(bandColors) > 3:
                self.r3.SetSelection(self.r3colors.index(bandColors[2]))
                self.r4.SetSelection(self.r4colors.index(bandColors[3]))
            # Third band is not present
            else:
                self.r3.SetSelection(0)
                self.r4.SetSelection(self.r4colors.index(bandColors[2]))

            self.colorSet()

        # Not a valid number or possible value
        except (ValueError, KeyError):
            pass

    # Display the colors for the bands
    def colorSet(self):
        self.pr1.SetBackgroundColour(COLORCODE[self.r1.GetValue()])
        self.pr2.SetBackgroundColour(COLORCODE[self.r2.GetValue()])

        # If band 3 isn't used, don't show it
        if self.r3.GetValue() == "-":
            self.pr3.SetBackgroundColour(wx.NullColour)
        else:
            self.pr3.SetBackgroundColour(COLORCODE[self.r3.GetValue()])

        self.pr4.SetBackgroundColour(COLORCODE[self.r4.GetValue()])

    def display(self):
        self.Centre
        self.Show()

if __name__ == '__main__':
    main()

Sunday, April 7, 2013

Shared Object with GHC 7

I was hitting my face on my desk for a few hours today trying to work this out. So here's my post to hopefully save anyone else who ended up stuck on this. I made a bash script to do it all, so here's the script. Note that this makes dynamic shared objects (.so files), still trying to figure out how to do it statically.

#!/bin/bash

TMPHELP="module_init.c"
FILENAME=${1%.*}
declare -i EXITCODE

cat <<TMPHELPER > $TMPHELP
#define CAT(a,b) XCAT(a,b)
#define XCAT(a,b) a ## b
#define STR(a) XSTR(a)
#define XSTR(a) #a

#include <HsFFI.h>

extern void CAT(__stginit_, MODULE)(void);

static void library_init(void) __attribute__((constructor));
static void library_init(void) {
    /* This seems to be a no-op, but it makes the GHCRTS envvar work. */
    static char *argv[] = { STR(MODULE) ".so", 0 }, **argv_ = argv;
    static int argc = 1;

    hs_init(&argc, &argv_);
    hs_add_root(CAT(__stginit_, MODULE));
}

static void library_exit(void) __attribute__((destructor));
static void library_exit(void) {
    hs_exit();
}
TMPHELPER

ghc -O2 --make -dynamic -shared -fPIC $2 -o "${FILENAME}.so" -optl-Wl,-rpath,/usr/lib/ghc/ -lHSrts_debug-ghc7.4.1 $1 $TMPHELP  >&1

EXITCODE=$?

set -x

if [[ $EXITCODE -eq 0 ]] ; then
    rm "${FILENAME}.hi"
    rm "${FILENAME}.o"
fi

if [[ -f ${FILENAME}_stub.h ]] ; then
    rm "${FILENAME}_stub.h"
fi

rm $TMPHELP
exit $EXITCODE

I'm honestly not 100% sure how everything works, but my main goal was to create shared objects so I could use them in Python. One thing to note if you are loading a shared object into Python made with Haskell, the IO functions don't seem to display on IDLE (works fine on the terminal).

Tuesday, April 2, 2013

Arduino Wii Nunchuck on Linux

I came across a DIY project over on the website for Maker magazine to connected a Wii Nunchuck to your computer as a mouse using an Arduino board and Python. Problem was, they only made it for Windows (they also suck at Python programming, but whatever). So after quite a bit of work over the past three days, I finally got it working.

The problem is Windows is not very secure and anything running on it can really do whatever it pleases, like say move the mouse around or click on things, without much effort. Linux has a bit more to it.

I went in originally planning on using libX11.so through ctypes.cdll, but setting up an event on that was not only pure hell, I don't even know if it is possible. Plan B presented itself as Xlib. The documents are not that complete, examples seem to be scarce and I don't have much in the way of patience.

You can check everything out here, I'm going to skim over the details and then show the Python code, all commented.

First thing first, get the library for interfacing the Nunchuck here. The readme there should tell you how to use it.

Next you want to jam some wires into your Nunchuck (the jumper wires from the starter kit fit fine without breaking anything) like so and connect it to your board to the corresponding holes.


From there you want to upload the example code for the Wii Nunchuck library so we can just process the data as a string rather than a bytestream.

Now we just need to run some Python code and you get all the mouse control I could jam into the thing (two buttons, joystick and accelerometer gave me just enough room for everything a standard modern day mouse has).

#!/usr/bin/env python
import serial, sys, Xlib, Xlib.display, Xlib.ext.xtest

# This will be later on, setting it now
global d, root

# The port to which your Arduino board is connected
# Change for your system and board
port = '/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_64131383331351306291-if00'

# The baudrate of the Arduino program
baudrate = 19200

# Variables indicating the center position (no movement) of the controller
# Increase the amount of play if to sensitive, decrease if not sensitive enough
# Increase speed to cut it down, decrease to raise it (1 is the highest)
midAccelX   = 515 # Accelerometer X
midAccelY   = 590 # Accelerometer Y
midAccelZ   = 710 # Accelerometer Z (not used)
midAnalogY  = 134 # Analog Y
midAnalogX  = 127 # Analog X
tollerance  = 5   # Amount of play
accelToll   = 80  # Amout of accelerometer play
speed       = 4   # difference / speed = movement

# Generic mouse movement
def move_mouse(x,y):
    x = int((x - midAnalogX) / speed)
    y = int((midAnalogY - y) / speed)

    # Make sure it's not just white noise
    if abs(x) < tollerance:
        x = 0
    # Move in the right direction the right amount
    else:
        if x < 0:
            x += tollerance
        else:
            x -= tollerance

    # Same deal, filtering white noise
    if abs(y) < tollerance:
        y = 0
    # Move in the right direction the right amount
    else:
        if y < 0:
            y += tollerance
        else:
            y -= tollerance

    # Save the Xorg processing if we're not moving
    if x != 0 or y != 0:
        d.warp_pointer(x, y, src_window = root)
        d.flush()

# Simulated button down
def mouse_down(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonPress, button)
    d.sync()

#simulated button up
def mouse_up(button):
    Xlib.ext.xtest.fake_input(d, Xlib.X.ButtonRelease, button)
    d.sync()

def main():
    # Connect to the serial port
    try:
        ser = serial.Serial(port, baudrate)
    except Exception as err:
        sys.stderr.write(str(err) + "\n")
        return 0

    # Mouse buttons
    leftDown = False
    rightDown = False
    middleDown = False

    # While the serial port is open
    while ser.isOpen():
        # Read and process the line
        line = ser.readline()
        line = line.strip('\r\n')
        line = line.split(' ')

        # Not sane yet, nothing to see here
        if len(line) != 7:
            continue

        # Alias the info so we can make sense of the rest
        try:
            analogX = int(line[0])
            analogY = int(line[1])
            accelX  = int(line[2])
            accelY  = int(line[3])
            accelZ  = int(line[4])
            zButton = int(line[5])
            cButton = int(line[6])

        # It hiccuped, ignore and move on
        except ValueError:
            continue

        # Left Mouse Button (Z)
        # Button down, don't need to press again if it's already down
        if zButton and not leftDown:
            leftDown = True
            mouse_down(Xlib.X.Button1)
        # Release the button
        elif leftDown and not zButton:
            leftDown = False
            mouse_up(Xlib.X.Button1)

        # Gestures to map more buttons since we only have two
        # It's the C button
        if cButton:
            # Tilt right, right click
            if (not rightDown) and accelX - midAccelX > accelToll:
                rightDown = True
                mouse_down(Xlib.X.Button3)
            elif rightDown and accelX - midAccelX <= accelToll:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            # Tilt left, middle click
            if (not middleDown) and midAccelX - accelX > accelToll:
                middleDown = True
                mouse_down(Xlib.X.Button2)
            elif rightDown and accelX - midAccelX <= accelToll:
                middleDown = False
                mouse_up(Xlib.X.Button2)

            # Tilt forward, scroll up
            if accelY - midAccelY > accelToll:
                mouse_down(Xlib.X.Button4)
                mouse_up(Xlib.X.Button4)
            # Tilt back, scroll down
            elif midAccelY - accelY > accelToll:
                mouse_down(Xlib.X.Button5)
                mouse_up(Xlib.X.Button5)
        # Cleanup any buttons down when gesture stops
        else:
            if rightDown:
                rightDown = False
                mouse_up(Xlib.X.Button3)

            if middleDown:
                middleDown = False
                mouse_up(Xlib.X.Button2)

        # Move the mouse
        move_mouse(analogX, analogY)

    # After the program is over, close the serial port connection
    ser.close()

if __name__ == '__main__':
    # Create display and get the root
    d = Xlib.display.Display(None)
    root = d.screen().root
    try:
        main()

    except KeyboardInterrupt:
        print ""

    except Exception as err:
        sys.stderr.write(str(err) + "\n")

    # Regardless of what happens, close the display
    finally:
        d.close()

Remember to change your port for your board and system and adjust any values to adjust to your sensitivity and any excess give or lack there of that it may have.

Sunday, March 24, 2013

Anamorphism

Anamorphism is the opposite of catamorphism. Since catamorphism is folding, then anamorphism can be considered unfolding. So let's first look at the mechanics for unfolding.

To unfold, we return a sequence of data. This sequence does not need to be finite. The amount returned can be dynamic or static or infinite. Things like list comprehension and generators are some quick examples of unfolding. In Python, xrange and range are anamorphic in a way.

The usefulness of unfolding is that we can get a series of data about some information without even needing to know anything about it before hand. Some useful math you can do with this would be things like getting all the divisors of a number. I have used anamorphism for generating Fibonacci numbers in sequence as well.

divisors :: Integral a => a -> [a]
divisors n = [i | i <- [1..(n `div` 2)], rem n i == 0]

fibgen :: Num a => a -> a -> [a]
fibgen a b = a : (fibgen b (a + b))

The divisors function returns a finite sequence of data whereas the fibgen returns an infinite amount. Also note that divisors is using list comprehension whereas fibgen is a normal function.

That is about all there is to anamorphism, an unfolding of data. Even replicating data can be an anamorphism. Oh, and an anamorphism can even take lists of data or multiple lists of data and return a list of data, like zipping two lists together.

Friday, March 22, 2013

Catamorphism

Many types of morphism concepts exist in programming, and catamorphism is one of them. Catamorphism is a functional style and is basically a fold. The idea is you take a sequence of data and sequentially go through it, folding data in on itself until you end up with a different value. The folding is usually recursive in nature and one thing to be careful of is not to fold causing exponential growth.

One thing to remember is that when using a lazy fold, it is not immediately calculated, making it a little less catamorphic by nature having an intermediate step with thunks*. Strict folding causes immediate evaluation. The basic theory is that a fold is catamorphic. So how does this work?

Let's say we have a list of data:
[1, 2, 7, 5, 3]

Now let's say we want to multiply all that data together. Using a (strict) fold, this is what will happen.

* [1, 2, 7, 5, 3]
1 * [2, 7, 5, 3]
(1 * 2) => 2 * [7, 5, 3]
(2 * 7) => 14 * [5, 3]
(14 * 5) => 70 * [3]
(70 * 3) => 210

Thus the data folds in on itself around the function, in this case multiplying. In Haskell, this would look like

foldl1' (*) [1, 2, 7, 5, 3]

Looking at other examples, they also seem to include fancy mapping stuff and function composition, but that is overlooking the basic theory. On Wikipedia, the example is this.

data Tree a = Leaf a
            | Branch (Tree a) (Tree a)
 
type TreeAlgebra a r = (a -> r, r -> r -> r)
 
foldTree :: TreeAlgebra a r -> Tree a -> r
foldTree (f, g) (Leaf x)     = f x
foldTree (f, g) (Branch l r) = g (foldTree (f, g) l) (foldTree (f, g) r)
 
treeDepth :: TreeAlgebra a Integer
treeDepth = (const 1, \l r -> 1 + max l r)
 
sumTree :: (Num a) => TreeAlgebra a a
sumTree = (id, (+))

After much examining, the idea behind it is to map some function to all the items in the list, then fold them all on some function. Examples on the Haskell wiki follow suit, only using functors and function composition (fmap and the period). The end result is a fold, the difference being some form of mapping can also occur over the data. On a normal list, this can be achieved and summed up as such.

cata f g xs = foldl1' f $ fmap g xs

With this, we apply function g to all members of list xs, then fold the mapped result on f. This could then be expanded to get the exact same results as we achieved before, or to do other things.

-- The same as we did before with foldl1'
cata (*) id [1, 2, 7, 5, 3]

-- To get the length of a string
cata (+) (const 1) "Hello!"

This can be further expanded by throwing some functors into the mix, but the very end result is data is folded in on itself around a function to get a result. The cata function I created is to make a very general purpose fold, however any function that folds down on itself is catamorphic.

So a catamorphism is folding, no matter how simple or complex the route to get to it is. It can also use a right fold, I just prefer a left fold. When it does something more than folding, it could be hylomorphic or metamorphic.

* Lazy folds

When using a lazy fold, what happens can change. Since lazy is more "on demand" evaluation, you end up with something that looks like this.

* [1, 2, 7, 5, 3]
1 * 2 * [7, 5, 3]
1 * 2 * 7 * [5, 3]
1 * 2 * 7 * 5 * [3]
1 * 2 * 7 * 5 * 3
2 * 7 * 5 * 3
14 * 5 * 3
70 * 3
210

This may not reflect exactly what happens but is the general idea of lazy evaluation as apposed to strict. The catch is that tail recursion optimization can change the nature to prevent this expansion and tracking of extra information, in other words, evaluate as it goes since part can be evaluated and is not needed for the outcome. It's just something to keep conscious of so your fold doesn't swallow your computer's memory whole.

Thursday, March 21, 2013

Polymorphism

Back when I first started programming, I was originally working in Java. One of the concepts we touched on was polymorphism. For a long time after that when I moved on to other languages, I never really understood what it was or how to use it. Then one day it just clicked, it all made sense (the basic idea of it, at least), and I saw how to use it. People I help who are just learning to program get caught on concepts like these, and fall to misconceptions making the idea seem useless and they go about in some strange manor of doing things that could be simplified. So, what is polymorphism?

To understand polymorphism, you must first understand inheritance. For the moment, we will look at object oriented programming. Inheritance is when you create a class that extends another class. The inheritance part is when it inherits the methods of the class it is extending. Let us look at a quick example in Python.

class Parent (object):
    def parentMethod(self):
        pass

class Child (Parent):
    def childMethod(self):
        pass

In this example, the Child class extends the Parent class. The parent extends the object in Python, which is just something you should do when making a base class. The Parent class has the method parentMethod. This method is also in the Child class due to inheritance because Child extends the Parent class. However, Parent does not contain the childMethod, as this is something that was created in the Child class. Some languages also support multiple inheritances,  but don't worry about that until you get down basic inheritance.

If you're wondering how this could be in anyway useful, the best example I can think of is game entities. By creating a base mob class, you could then use inheritance to quickly create different types of mobs, but still keep a basic framework, shortening your code and keeping it clean and easy to maintain.

So, how does this tie into polymorphism? Well, polymorphism involves overriding inherited methods. So after we extend a class, we then override a method that was inherited, thus changing it slightly, yet still remaining the same. It is a bit confusing, so let's see an example. Since polymorphism always reminds me of when I played WoW, let's turn a person into a sheep.

class Person (object):
    def __init__(self):
        self.position = 0

    def walk(self):
        self.position += 1

    def talk(self):
        print "Hello!"

class Sheep (Person):
    def talk(self):
        print "Ba-a-a-a-ah!"

We now turned a person into a sheep. Like a person, a sheep can walk. Unlike a person, sheep don't speak words like we do, they make sheep sounds, so clearly we needed to change what they would say when they talk. Thus, the person and a sheep both walk and talk, the difference here is what they say.

The use in this is to allow extending a base class, but without being restricted to the default behaviour of the base class. So if we go back to the game example, we have a mob base. Now let's say we want to make a boss mob, but to do so, that would require changing part of the base mob just for the boss. Well, rather than say ignoring that one method and copy-pasting it into all the other mobs or creating some intermediate mob class just for one method, we can just polymorph the boss class.

Polymorphism also isn't specific to object oriented programming, although that is where it seems to be the most prevalent. Haskell also support polymorphism, however this is in relation to typeclasses as apposed to objects. This concept is very similar, and looks something like this.

data Tree a = Empty | Node {
    nodeValue :: a,
    nodeLeft :: Tree a,
    nodeRight :: Tree a
} deriving (Show)

instance (Eq a) => Eq (Tree a) where
    Empty                   == Empty                = True
    Node {nodeValue = x}    == Node {nodeValue = y} = x == y
    _                       == _                    = False

The deriving part is where I am just extending a type class, but for where the way it does so is less than ideal, you create and instance. An instance is where you create custom behaviour by overriding the default functions of the type class. This means a function of a type class can be used on custom data type, so you do not need to create all your own functions, this makes code a bit more versatile and friendly.

Wednesday, February 27, 2013

Chrome CORS

This is a bit of a follow-up post to my Refused to set unsafe header post. A question I was asked was about how to do something with CORS in Chrome, and my immediate findings were that it is impossible. As of recently I looked back into it and have come across a couple possible solution.

The first possible solution is to make sure to send the proper headers, as Chrome will take into account what the server says is allowed. For this, you can set the access control headers however you want. For the example, I will show the php version I found.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true ");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");

This will allow you to set the various headers you would need for a CORS request. Another possibility is by using a content type that the control isn't strictly enforced. With that, it seems by setting the Content-Type to plain/text, it should go through without a problem.

I personally haven't tried either of these because I have yet to have a use for it with what I do, however I wanted to share this bit of information should anyone be curious and happen across here.

Wednesday, February 20, 2013

Fibonacci Generator

I was reading up on various types of morphism recently. While I was disappointed in the examples, as they were showing off bad practice, even if they were just an example, I got an idea. While I do have a post showing off a similar bad example with calculating a Fibonacci number, I have since refined it into a faster and more efficient method and an explanation to it you can find here.

My main focus of interest was looking at catamorphism and anamorphism. In the process of reading up, I came across other types of morphisms that while were mathematically interesting, I only saw bad programming being shown emerging from the ideas.

To explain in the simplest way possible, catamorphism is a generator and the opposite of anamorphism, which is a fold. Generators are functions that produce data on-call, using the generated data to provide more data upon the next call. This can create an infinite data type or have an end. Counting can be catamorphic in nature. For the explanation, let's look at a Python generator.

def counting(x):
    while 1:
        yield x
        x += 1

This is the equivalent to this Haskell code.

counting x = [x..]

The Python code has a bit of a more robust description to it, but the idea is the same for both, they will count from x on up until forever. Anamorphism includes things like the sum function, a list of stuff goes in, and it's all folded together with a function (in the case of sum, adding) until one result remains.

Now to the cool part and what this all means. This all means that with the magic of generators, there is an easy way to create a fibonacci generator. This is a simple one-liner in Haskell and as far as I can tell, can outdo the crappy version of the recursive Fibonacci shown everywhere else.

fibgen a b = a : (fibgen b (a + b))

That's all there is to it. The idea is you supply the first two numbers and it does the rest, forever and ever. So if you want the numbers from it, just take them. Now I suppose I can be nice and give something similar in Python, although to be honest it will look pretty much like the counting function I made.

def fibgen(a, b):
    while 1:
        yield a
        a, b = b, a + b

With only some very small tweaking, I turned the counting function into a Fibonacci generator. Now, like magic, we have an easy way to cycle through a sequence of Fibonacci numbers. I want to try to work on various problems more in this manor for when list processing or other related activities that require sequential computations, I feel it simplifies the process and gives a more elegant solution (or if nothing else, it looks cool).

Sunday, February 10, 2013

Haskell: No instance for (Eq a)

I've been taking some time to dig a bit deeper into Haskell and was messing around with custom data types and records when I came across this little pesky error. After much Googleing and answer presenting itself, I decided to put my nose back into the ebook I was reading on Haskell and try to see if I could find what I was missing. Had I just scrolled a bit further down on some of the Haskell doc pages, my answer would have been staring me right in the face, as it did when I finally scrolled to the right part.

So while I thought I needed to qualify in some way that the datatype in my record would work with Eq, it turns out I just needed to qualify it with the instance declaration. So here's the erroring code:

instance Eq (MyType a) where

And now the fix. Ready for this complicated piece of work?

instance (Eq a) => Eq (MyType a) where

Then like magic, it decides all is right with the world and it works. From what I gather, it has to do with the pattern matching. I would show the rest of the code, but it's still a work in progress and my amature attempt at a binary tree datatype complete with functions for working it. If it works well, I'll show that off when I am done.

***Edit:
The code that I'm using it for is in my Code Dump, which is on my PyGopherd server. There's probably a few hundred better ways to do what I made, but when messing around with it in ghci, it did pretty well. Gonna be using my Gopher server as a dumping ground for code that I don't necessarily have a use for, but deem it okay enough to show others.

Wednesday, February 6, 2013

Python Partial Functions

One of the things I found to be rather cool when playing around with Haskell is partial function applications. With this, it was easy to pass a function to something for list processing, like folding, mapping or filtering.

After talking to someone who was making something in Perl, I was inclined to ask if he knew about currying functions or partial application, as he was taking advantage of how Perl handles and makes functions. Not really being a Perl programmer myself, I decided to look up currying in Python.

So after venturing into some of the PEP stuff for Python, I came across functools.partial(). The reason I think this is cool is because I don't like lambda functions in Python. While lambda functions are nice at first and allow you to do partial application in a sense, it's aggravating because any variables you use in it are in a the scope of the parent. This means, to isolate a lambda function, you would need to place it in a named function, thus defeating the purpose.

To give a rather bad example with no real application I can think of, consider this code.

>>> x = [lambda a: a*y for y in xrange(1, 10)]
>>> x[0](10)
90
>>> x[1](10)
90
>>> y
9
>>> y = 1
>>> x[2](10)
10


As you can see, everything results in the same because variables in list comprehension are also exposed to the global scope. So there are a few ways we can do this to get the results we expect. We can have a function that returns a function, either using lambda or nested functions. I prefer the nested function for something complicated, but for a simple thing like this, lambda will work as well.

>>> def a(b):
...  def c(d):
...   return d * b
...  return c
... 
>>> x = [a(y) for y in xrange(1, 10)]
>>> x[0](10)
10
>>> x[1](10)
20
>>> def a(b):
...  return lambda c: c * b
... 
>>> x = [a(y) for y in xrange(1, 10)]
>>> x[0](10)
10
>>> x[1](10)
20

Or we can do this with partial application, which I think would prove more versatile. The reason I think partial application could be more versatile is because there is no need to write a specialized function to return the function desired. So this next bit of code allows a general purpose function and no use of lambda, just a partial application.

>>> import functools
>>> def a(b, c):
...  return c * b
... 
>>> x = [functools.partial(a, y) for y in xrange(1, 10)]
>>> x[0](10)
10
>>> x[1](10)
20

Those with sharp eyes may notice that I do b * c instead of c * b. This is because it does the partial application of variables in order. It does allow keyword arguments, which will allow you to pass arguments to a partial application in any order. Now while a lambda function in a single case would get the same results, remember that they still have an exposed scope.

So in the end, my favorite way to do this is with the partial application offered by functools. This is not to say lambdas and returning nested functions isn't good, as they do have their uses and lambda can make a quick and easy script for something small.

For more info, check these out:

Tag Cloud

.NET (1) A+ (1) addon (6) Android (3) anonymous functions (5) application (9) arduino (1) artificial intelligence (2) bash (3) c (7) camera (1) certifications (1) cobol (1) comptia (2) computing (2) css (2) customize (15) encryption (2) error (15) exploit (13) ftp (2) gadget (2) games (2) Gtk (1) GUI (5) hardware (6) haskell (15) help (5) HTML (4) irc (1) java (5) javascript (20) Linux (18) Mac (4) malware (1) math (8) network (5) objects (2) OCaml (1) perl (4) php (8) plugin (6) programming (42) python (24) radio (1) regex (3) security (21) sound (1) speakers (1) ssh (1) telnet (1) tools (11) troubleshooting (1) Ubuntu (3) Unix (4) virtualization (1) web design (14) Windows (6) wx (2)