Skip navigation

Category Archives: Programming

Feel free to grab and see how it works for you!

https://app.box.com/s/uaqlt1mjq6jbstx19jrv

Programming.

Once you realize the power at your hands, it becomes something you simply must do. You realize just how little you have been aware of because the systems you are used to using have been made easy to use and dumbed down to a simplistic level. That is not to imply anyone is dumb, just that the vast majority prefer to remain blissfully ignorant of how an Operating System works underneath the pretty colours and fancy fonts. That’s all fine and good for the sheeple of society but those of us who realize that the $500+ you spend on a computer can do a WHOLE HELL OF A LOT MORE than you were led to believe….well, we are goddamn well gonna check this shit out!

When you use computers to browse youtube for kitty videos, you don’t need to know much(doesn’t even matter what OS you use either for that matter). When you use computers to build websites, scour databases for information or check your office network for security breaches and then patch them…well, then you need more than just a browser. Sure you could type single commands into a terminal/command prompt but if you want to do anything in the scale of more than 2 checks, multiple times, typing will soon become your worst enemy.

Enter the world of scripting.  This is what you might call a ‘bridging language’ between hardcore programming (i.e. C++,  Machine Code) and regular commands.  Lets say you want to check your websites, that you built for a client, are all up and running.  Well, you aren’t going to sit at a browser all day, refreshing pages or typing in URLs for each website are you?  Not when you can write a script once, run it and let it do its job.

I had an idea to do exactly that.  A script that would check my sites for two things.  First, to make sure they are till accessible(i.e. the webserver is up) as well as if there was any change to the home page(i.e. has anyone hacked the site?).  Not that I’m worried about my own sites but it’s an interesting tool for those who do have paid clients.

Now, if it was just that, checking websites, it would be cool.  What if you added a feature that would push a notification to your Android-powered device?  How awesome would that be?  Well, I did just that!  With the use of Notify My Android, I created a script (which is just a collection of commands you would normally type in a terminal) that checks websites and if it finds something wrong, it will send a notification to your Android device that there is a problem!

It’s still being developed/refined but with the help of DarkTherapy(quite a lot actually, he’s pretty awesome at this stuff) from BSOD, we have developed a nice little script that is nearly tweaked just right.  V1 one works but does not check pages.  v2 works but not quite how I wanted it.  v2.x are the current developments.  I suspect things will be pretty set before the end of the week.  Any version 2 will work just fine for a single website.  We are refining for multiple websites at the moment.

This is the working box.net folder.  Feel free to grab a copy/comment, hell, even rewrite it if you like.  https://app.box.com/s/l2emmq9q9nhayvci25r5

Decided to go with single letters to represent menu choices.

Game download page here.

Now…where to go with it from here…..

So I decided to learn python.  I was happy, if occasionally frustrated, with how things were going.  When I got onto the idea of writing my own code (in the form of a text ‘choose your own adventure’ game) I was really getting into learning.  I got this idea in my head to port this game into Android and eventually develop it into more and more sophisticated levels (i.e. add some images, sound, maybe even a video game down the road….wayyyy down the road) but after much research, and in particular the following video, I found that doing so is not terribly practical at the moment.

I wonder if it would be worthwhile pushing for a mobile OS that does use Python as a main programming language (like Obj C for IOS and Java for Android).  It would be like a 3rd entity coming into the split market (as the current 3rd entity (Blackberry) is on its way out).  Sadly, this type of idea is well above my understanding on how such things would come about, not to mention the enormous amount of effort it would take to develop such an OS.  But hey, the idea is here now.  Feel free to pass it around and who knows…maybe someone would give it a shot.

Well, now that I am able to finally breathe, somewhat, I am getting back to developing my Choose Your Own Adventure text game.  Check out the latest minor tweaks to the game here.  Note: The Sci-Fi and Horror text is not working, merely a placeholder for their eventual development.

If anyone is interested in writing out a simple Sci-Fi or Horror storyline, similar in style to the Fantasy one, let me know.  Nothing complicated, just a bit of crazy fun.

My path is not taking me into the world of API programming.  And Code Academy has a neat little course of “How to use APIs with Python“.  Kinda matching up nicely I think 🙂

What I love about the Internet is the community that happens at times.  I posted my game code up for all to see and review and I received a lot of good feedback.  One guy, Bob, event went out of his way to help me understand classes and wrote up a basic structure for my style of game.

from collections import OrderedDict # for storing menus.

# adjust for Python 2 or 3
import sys
if sys.version[0] >= ‘3’:
getUserInput = input
else:
getUserInput = raw_input

def main():
place = ‘road’ # starting place
while place:
place = places[place].enter()
if not place:
ans = getUserInput(‘enter yes if you want to play another round.’)
if ans == ‘yes’:
place = ‘road’

class Choice:
def __init__(self, prompt, next, description=None):
self.prompt = prompt
self.key = prompt.partition(‘ ‘)[0].lower() # get 1st word of prompt
self.description = description
self.next = next

class Place:
defaultChoice = Choice(”, ‘cycle’, “I don’t understand that!”)
def __init__(self, name, description):
self.name = name
self.description = description
self.choices = OrderedDict()
self.menu = ”

def addChoice(self, choice):
key = choice.key
self.choices[key] = choice
self.menu += choice.prompt + ‘\n’

def enter(self):
print(self.description)
while True:
userChoice = getUserInput(self.menu[:-1]).l

ower()
choice = self.choices.get(userChoice, self.defaultChoice)
if choice.description:
print(choice.description)
if choice.next != ‘cycle’:
return choice.next# create the kingdom
places = {}place = Place(‘road’, ‘You are standing on a road. Nearby is a small house’)
place.addChoice(Choice(‘Enter the house’, ‘house’))
place.addChoice(Choice(‘South’, ‘road2’))
places[place.name] = placeplace = Place(‘road2’, ‘You are standing on a road, surrounded by howling wolves.’)
place.addChoice(Choice(‘Run for your life’, ”, ‘outrun a wolf? ha!’))
places[place.name] = placeplace = Place(‘house’, ‘You are in a small house. There are keys here. A stairway ascends.’)
place.addChoice(Choice(‘Climb the stairs’, ”, ‘your foot breaks a weak riser and you fall to your death’))
place.addChoice(Choice(‘Exit’, ‘road’, ‘you leave the house’))
places[place.name] = place

main() # start the game

I tried running it and get an immediate error.
ImportError: cannot import name OrderedDict

Well, let me do some research into this and see if it’s just a Python version issue because in the code there is something about checking version and using different routines.

Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2

Ah ha, the Docs on OrderedDict say new for version 2.7 whereas I seem to have version 2.6.  How odd though, I thought I was running 2.7 all this time.  Off to Google to find a link/how-to on this.

I found this link, which does include an automated script to do the update for you (this one compiles from source) but experience has shown that such things never turn out exact and I really can’t be bothered to spend hours and hours trying to figure out what went wrong.

Then I found this link, which suggests using a mix of Debian versions, which I know causes bad things to happen.  Besides, Debian Wheezy is now stable and as soon as I get an external HD, I’ll be backing everything up and upgrading my OS, which includes v2.7 of Python.  So, time to wait then I can try this code out.

I will say this though.  From what I read in the above code, I’m not convinced that it will be any easier to program.  It just doesn’t look/feel intuitive enough when trying to debug an error message that might come up.  That and it seems to scream ‘complication’ when the following is to be adhered to.

      The Zen of Python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Side Note: Why the hell is WordPress suggesting “Pink(singer)” for this post?!?! Also, they suggest ‘transportation’ for just about every, single post I have written for any of my blogs here after the publish phase.  What gives WordPress??  You desperate for some Transportation posts??

As I’ve been on this quest to understand the Class function in Python, which is a form of Object Orientated Programming, I am slowly finding some resistance to the ‘must use OOP!  All the time!” type of mentality.  So, being the type willing to consider many ideas before making up my own mind, I’ll be reading more on this stuff.  For now, here is some food for thought.  My quick cursory of the article seems to mean OOP is just another tool that may or may not be the best for a given coding scenario.

By , 20 Apr 2013

Introduction

The last decade has seen object oriented programming (OOP) dominate the programming world. While there is no doubt that there are benefits of OOP, some programmers question whether OOP has been overrated and ponder whether alternate styles of coding are worth pursuing. To even suggest that OOP has in some way failed to produce the quality software we all desire could in some instances cost a programmer his job, so why even ask the question?

This is getting ridiculous.  How can you even complete a course when the course gives you the equivalent of ‘bad filename or input’.  It’s an error that tells you nothing.  They need to seriously review their process for automatic code checking when you submit your code for system review.

I finally realized today that I _can_ just continue with the exercises by merely selecting the next one from the drop-down choice and not just be stuck waiting to find out why my code is supposedly ‘wrong’.  I also found out that there is a bug in that particular exercise.  Two days and suddenly realizing that my code is just fine.  Talk about screwing up new people to learning code.

So, taking this new-found knowledge with me as I go, I find this little gem of a bug.

 

So yup, moving right along.

If you are new to learning programming, you need to understand that you will be using someone else’s programming to learn programming, and it is rarely perfect.  Document the error and move on.