Code Diary 03 – AlphaVEN highlights and lowlights

The code this time is too long to reproduce here in its entirety, so check it out here if you’re interested. I’ll be going over some of the more interesting and ugly parts here.

Keyword Arguments Everywhere

Example:

    def __init__(self, **kwargs):
        """Create a new Maker."""
        
        self.title = kwargs["title"] if "title" in kwargs else DEFAULTOUTPUT
        self.fadetime = kwargs["fadetime"] if "fadetime" in kwargs else DEFAULTFADETIME
        self.format = kwargs["format"] if "format" in kwargs else DEFAULTFORMAT      
        #Not yet implemented functionally speaking:
        self.resolution = kwargs["res"] if "res" in kwargs else DEFAULTRES

kwargs are great! Important usage note:

    def createMakers(self):
        """Create a Maker for each of the outputs."""
        self.makers = []
        for para in self.paralist:
            #Extract the inputs and transitions
            paratitle = para.split('\n')[0]
            lines = para.split('\n')[1:]
            
        paramaker = maker.Maker(title=paratitle, **self.gensetdict)

If gensetdict is empty, this calls maker.Maker() without any keyword arguments. This lets me avoid having separate code for calling functions when I don’t have any arguments for them. Very convenient!

The Worst Function I’ve Written (so far)

Speaking of createMakers, it might be the worst function I’ve ever written. I won’t reproduce it here, because it’s huge, and also because it should probably be quarantined. It went through about 5 versions and used to be much worse, but is still 81 lines long and definitely not performing just a single action. It should be set on fire restructured into several functions. However, those would probably have to have a whole bundle of inputs and outputs.

Object Orientation
        if "fadein" in kws:
            if kws["fadein"]:
                self.fadein = kws["fadein"]
            elif not (kws["fadein"] == None or kws["fadein"] == 0):
                #If the fade time isn't explicitly 0
                self.fadein = self.videosegment.parentvideo.maker.fadetime
            else:
                self.fadein = None
        else:
            self.fadein = False

Man, look at that chain of objects! I have no idea if this is a good or bad idea, but it certainly looks cool. And hey, imagine if it were Java:
this.getVideoSegment().getParentVideo().getParentMaker().getFadeTime()
Ewwww. And that’s assuming you don’t need to cast the fadetime to the right type of numeric, heh.

Abusing Truthiness

Notice how in the above code, I check if kws["fadein"] is exactly equal to “None” or “0” rather than just checking if it counts as True (both None and 0 count as False in Python). This is to distinguish three cases:

  • There is a fade-in, and it has a time.
  • There is a fade-in, and it should use the default time.
  • There is a fade-in, and it has 0 duration.

(ok, maybe this is actually the worst code I’ve ever written)

Why Doesn’t This Come With The Package?
def timediff(t1, t2):
    """Find the absolute difference between two datetime Times.
    
    Returns a timedelta object.
    """
    
    delta1 = dt.timedelta(hours=t1.hour, \
                          minutes=t1.minute, \
                          seconds=t1.second, \
                          microseconds=t1.microsecond)
    delta2 = dt.timedelta(hours=t2.hour, \
                          minutes=t2.minute, \
                          seconds=t2.second, \
                          microseconds=t2.microsecond)
    return abs(delta2 - delta1)

Seriously, why did I have to write this? Is there some reason to not define a difference between two times?

Overall Thoughts

On the one hand, AlphaVEN was hideous before this. It didn’t even assemble the videos itself, which meant rendering every video twice – a massive timesink. On the other hand, this was a pain to write, isn’t particularly impressively done, and not necessarily fully functional. There are some definite improvements to be made.

Python is 100% the right language to do this. A powerful, flexible language that still functions like a shell script when needed. (I do use other languages. Sometimes. But usually, I find myself wondering why I would want to make life hard for myself).

This ought to make assembling streams into videos really easy, if I get round to actually doing so.

Advertisements
Code Diary 03 – AlphaVEN highlights and lowlights

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s