Just A Small Guideline

Feel free to browse the site. Don't be afraid to leave comments, they're supportive.
Please follow me on Twitter @ehldmnt and retweet my posts.

Wednesday, June 12, 2013

Python 3: Classes; a quick examination.

Object Oriented Programming [OOP] is not really a new concept. It's something that Python does, and does well [even if there is room for improvement]. This guide goes as far to explain pretty much how classes work in Python. Comment below with any questions or comments.

I feel that there really aren't many tutorials out there to explain this a bit more in depth. Here is a much more well rounded explanation [compared to the sparse material I've found]:

Classes are basically what they're called, "classes" or "classifications" for certain "kinds" of things. We can take for instance a chair, and a dog. They both have four legs, but which one is made of wood? And what exactly is a leg? Something that can move or something that holds the chair up? We could create a class called "animate".

class Animate:
pass

Well, isn't that nice? We have a class that does absolutely nothing. But that's okay, it'll all fall into place. The pass statement allows us to create something that syntactically requires some code or data, but doesn't actually do anything at all. Let's create a second class called "Inanimate".

class Inanimate:
pass

Once again, we have a class that does nothing, but the diference between the two of them is their namespace. We now have an "Animate" and an "Inanimate" class. Now for objects.

chair = Inanimate()

dog = Animate()

This seems like old news, "We're just renaming the classes to chair and dog!". Not quite. In OOP [Object Oriented Programming] we can have as many inanimate or animate objects as we'd like.

pencil = Inanimate()

cat = Animate()

 Are two other seperate "instances" of the class. In fact, this is called "instantiating." In this case, for those of us who don't quite understand, a dog is an "instance" or a particular "existence" while a "cat" is a separate instance. To learn more about what can be done, let's edit our classes directly.

class Inanimate:
def __init__(self, name, type):
self.name = name
self.type = type

We've done a lot here, so let's go over it piece by piece. Each class has a special "function" called a constructor. The __init__ function. It allows us to specify attributes of each particular class that we have. It makes us creating different classes a lot more structured. Inside of the __init__ constructor, we see one element called "self" and we see two elements called "name" and "type". If they were specified like this:

def __init__(self, name, type = "Furniture"):

Then it would make all inanimate objects "Furniture" by default, but it would also make the particular attribute "type" optional, like optional arguments on a command line, which is both a good and a bad thing. If you made it "item" or even just an empty string, that would be far more practical. Because in that case, that would mean that "Pencil" would be furniture, too. The reason why you set self.name to the "name" parameter, is because you're basically setting the dot operator, as well as making whatever you put in the class at instantiation permanent to that specific instance of the class.

Pro Tip:

You can make each of the variables private by adding one or two underscores (_) to the beginning of each attribute like this:
def __init__(self, name, type:)
self._name = name
self._type = type

This actually much more use, like for example, if you didn't want people to alter your class, or when they didn't need to alter the class at all, using the API to work with it.

"But what is self?!" Self is referring to the "variable" of the object itself. When the python interpretor runs, it goes like this:

- It sees the class you made.
- It sees that you instantiated [Remember, that means 'create'!] an object.
- It takes the name of that object and goes back to read the class. It replaces self with the title of the object you had given it.
- It continues running the script.
 
Now we must edit our "chair" object.

chair = Inanimate("Chair", "Furniture")

dog = Animate()

Hm... That's nice, but let's say you wanted to return self.name? How would you go about doing that? Why, with a method of course! Methods are just functions that exist inside of a class that can be used along with flow controls [if, for, while] in order to increase the functionality. Let's create a method that returns the name of the item!

class Inanimate:
def __init__(self, name, type):
self.name = name
self.type = type

def getName(self):
return self.name

chair = Inanimate("Chair", "Furniture")

print(chair.getName())

Using this, we can actually reduce the amount of code we write later. Because we're using classes, we can do this for EVERY class. And that is some of the power of Object Oriented Programming.

Monday, June 3, 2013

How to screen capture in linux, audio, video, or both.

I initially under estimated the ffmpeg command and have been abusing it for its abilities to convert my audio files after downloading them from youtube!

The all-mighty screen capture command:

ffmpeg -f x11grab -s wxga [or instead of 'wxga' another screen size like 1300, 800] -r 25 -i :0.0 -qscale 0 -f alsa -i hw:0 -strict -2
The all-powerful audio capture command:

ffmpeg -f alsa -i hw:0 [or another alsa device] -strict -2

[Basic] How To Create Classes In Python [ Object Oriented Programming ]



Let's start by explaining one thing at a time...


Classes
in python, are a way to bundle data [information that is fed to the code, or that you 'hard-code' yourself], with associated code, making it easier to manage the code itself and fix bugs if they ever arise. They can help by allowing you to model the problem you’re trying to solve and manage a program’s complexity better.


Let's say you're creating a fighting game where there are two types of NPC's. Bystanders, and fighters. You'd create the "bystander" class as easily as this:

class Bystander:
    pass


You've created the Bystander class. Why is this so great? Because this means that you can set this class to as many variables as you want, and you'll be able to have numerous different "versions" or "instances" of each:

bobby = Bystander()

sarah = Bystander()

"bobby" and "sarah" Are two different "instances" of the same class called "Bystander".

They are what are known as objects. Individual "occurances" or "copies" of the same class. You'll understand why this is so cool in a minute.

Let's modify our class a bit to get a better grasp of what we're dealing with here.

# We created the class called "bystander".
class Bystander:
# Here, we'll have a docstring. They're good for telling what
# our classes do.
"""A Simple Bystander class. These Individuals all have their own greeting."""
    # Now we create what's known as a "constructor" [__init__]
    # Which allows us to take the variables that are passed to the
    # class when the object is "instantiated" or "created".
    # It uses self because it is actually referring to the variable
    # that you use to create the objects.
    def __init__(self, name, greeting = ""):
        self.name = name
        self.greeting = greeting

 #Then we instantiate our objects.
bobby = Bystander("Bobby", "Hiya!")

sarah = Bystander("Sarah", "Konnichi wa!")
 # Notice that Alex doesn't have a greeting, but there will not be an error.
 # That's because the greeting is a "kwarg" or a "keyword argument". It
 # Is an optional argument or "parameter" that can be passed.
alex = Bystander("Alex")
# Please note that no matter how many objects we have, we'll always
# know what objects of this specific class are capable of in the case of 'Methods'.

A Method is a function of a class that is made specifically to be associated with the class itself. Methods must always take the "self" parameter because when it uses "self" it is actually referring to the object itself.


class Bystander:
""" A Simple Bystander class. These individuals have their own greeting."""

def __init__(self, name, greeting = ""):
self.name = name
self.greeting = greeting 

def say_Name(self):
print(self.greeting + " My name is", self.name + ".") 

bobby = Bystander("Bobby", "Hiya!")

bobby.say_Name()

Kira = Bystander("Kira", "Oh, yeah, baby!")

Kira.say_name()
Running this through the interpretor, we should get:

Hiya! My name is Bobby.
Oh yeah, baby! My name is Kira.

I hope this should provide a basic enough insight on how classes and objects are made. I'll leave the rest up to your imagination.

Sunday, June 2, 2013

How I Stop My Blogging Procrastination [and Other Procrastination in General] -- It works every time [Almost ;)]

Sometimes, when I'm sitting around thinking about things, or considering what to write, I start off by googling for some bit of information that I'm looking for, and I usually attempt to explain it in depth... Unless I get lazy.

It happens to the best of us, even those with obsessive hobbies like myself. But my solution that's been working the best for me is to start an article and then go back to it later, but I limit myself to three articles and do not allow myself to go on to other subjects!

Another trick is to limit distractions, like my iphone, I have this habit of texting other people when I've got nothing to do instead of staying focused. I'm a person who likes quiet and focuses better without distractions, even if I might want the occasional distraction to keep my wandering brain from becoming bored.

Or maybe I'll go on Facebook, or Twitter. I like Twitter a lot by the way. For some reason, in my opinion, it tends to lack in all of the drama that comes with interacting with your family and friends. I'm a believe a little in the Zodiac [horoscopes], and being a Gemini, it's hard for me to stay focused, which is my reason for writing this post in the first place.

But all of that is besides the point. Que; bullets:

  • Minimize distractions: If you're not the type of person who can remain focused on one topic for two long, or who has a tendency to stray from things to do others of greater personal interest, then it's best to minimize those other things around you that have a chance of catching your attention.
  • Take breaks: There's nothing wrong with this. You're only human, don't beat yourself up over the fact that you weren't able to write a 5000 words tl;dr post for the third time this week.
  • Limit the number of things on your plate: Like I said earlier in this post, it's better to limit yourself to maybe two or three posts to write on and stick to them.
  • Brainstorm: Writer's block is a bitch, 'nuff said. Sometimes there's just not anything interesting to talk about, going back to my 'googling talk'. There's no shame in knowing nothing, there is in knowing everything. The best thing to do in this case, is to relax and not stress so much about the content but the quality of your writing. Go back, fix grammatical mistakes, spelling errors, and polish up your post. You could have the best content in the world and if it looks like garbage with "u shud" and "dis is y u mus", a lot of the time, people are going to keep on scrolling on google. It isn't you, it's their standards being such that if the person doesn't have time to write properly, who's to say their guide isn't rushed?
  • Have fun: When you stop having fun, you stop wanting to work, and when you stop working nothing gets done. Keep this in mind.
  • Use the three S's and work from there: Short, Simple, Straight to the point. Once you bang out the bare bones of the knowledge that you need, go around and add your fluff. No need to stress and force yourself to work harder than you need to unless you really feel like it. Besides, if you keep things on point, you can better keep your own attention. You can always add on pieces, here or there to make it easier on yourself, and maybe re-advertise your content in a non-illegal and malicious fashion, of course.
Follow me on Twitter: @ehldmnt

Returning to my roots: Tidbits on Bash Shell Scripting

Ah, while creating a small shell script to perform three tasks:

  • Upload to the Testing-PyPi Server (http://testinpypy.python.org/pypi))
  • Reduce the amount of typing on the command line.
  • Possibly provide a way that I could use it in another or different python script to automate this process.
  • (BONUS) Take down the task that I'd like to perform in natively python once I become much more proficient.
I quickly googled the means by which to pass user input to STD-IN to set it to a variable in bash and here is how it's done:



To learn more, pay a visit to the all mighty google. ;D

Follow me on Twitter! @ehldmnt

A random coding tidbit... How to pause your Python code.

Ever needed a pause for dramatic effect. Why not use the native "time" module to get the job done?