Published on

Python: The F#%K It Approach



Not handling exceptions?

Once in a while as a programmer you meet difficult times, be it time-limits, shitty code and much more. It is not advisable to suppress errors. Every programmer should handle exceptions. But what do you do in those times where it proves difficult to approach things rationally in the now and then?

Handling problematic imports

Import the module with fuckit

import fuckit

#import problematic_module

Class with Errors

As a @decorator

class Person:

    AnotherName = 'Sue Ann'

    def __init__(self):
        self.FirstName = 'Tom'
        self.LastName = 'Sneed'

    def get_name(self):
        return self.FirstName + ' ' + self.LastName

class Employee(Person):

    def __init__(self):
        self.empnum = 'abc123'

    def get_emp(self):
        print self.AnotherName
        return self.FirstName + ' ' + 'abc'

# The lines below work just fine

x = Person()
y = Employee()

print x.get_name()
print y.get_emp()

Function with errors

As a @decorator

def printme( str ):
    something_wrong # No Exception
    printme("My string")

Using it in a ‘with’ situation:

def printme( str ):
    with fuckit:
        printme("It will never be an error")
   #Similar to
        printme("It will never be an error")
    except Exception:

Keep in mind that the decorator form of fuckit can’t stop syntax errors

Go on to Github and view the module or run :

$ pip install fuckit