Recent Updates Toggle Comment Threads | Keyboard Shortcuts

  • domenicosolazzo 8:25 pm on October 6, 2011 Permalink | Reply  

    Thank you, Steve! 

    Thank you, Steve.

    You have been an inspiration for me.
    I will always remember your vision, your passion and your achievements in life. They will help me every day in my career, life and fighting for my dream.

    Thank you Steve, for being who you were. Rest in Peace, Steve.

     
  • domenicosolazzo 8:35 am on September 5, 2011 Permalink | Reply  

    Django, PIL and Virtualenv 

    I had some trouble using the ImageField in Django with virtualenv because I was getting the following error:
    "Upload a valid image. The file you uploaded was either not an image or a corrupted image.” 

    PIL was installed in  my virtualenv but I did not pay any attention to the PIL Setup Summary, which was showing that there was not JPEG support.

    I tried reinstalling PIL but I had the same final result. I solved my problem in two simple steps.

    1)I’ve removed PIL from my virtualenv:
    pip uninstall virtualenv

    2)I’ve installed pillow, which is a saner package of PIL.
    pip install pillow

    It solved my problem and now the error in Django is now successfully eliminated.

    You can find another useful post about the same issue here.

     
  • domenicosolazzo 11:18 pm on March 12, 2011 Permalink | Reply
    Tags:   

    How to: Install Python 2.7 with Pythonbrew 

    This is a simple tutorial for installing Python 2.7 with Pythonbrew using Ubuntu 10.10.

    1. Check the version of python installed on your machine: python – V
      You should get something like this : ‘Python x.y.z’ where x.y.z is the version of Python (e.g. 2.6.6)
    2. Get pythonbrew using curl.
      curl -kL http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install | bash
    3. . $HOME/.pythonbrew/etc/bashrc
    4. Install Python 2.7.1 : pythonbrew install 2.7.1
    5. Switch to Python 2.7.1: pythonbrew switch 2.7.1

    It can also be used to install python 3.2. In the step 4, you have to substitute 2.7.1 with 3.2.

    Hope it will be useful to anyone.

    Cheers,

    Domenico

     
    • Soren 1:42 pm on March 16, 2011 Permalink | Reply

      Great, I didn’t know about pythonbrew – and I am also annoyed with old Python 2.6.x in Ubuntu 10.10 🙂

  • domenicosolazzo 9:22 pm on February 2, 2011 Permalink | Reply  

    Lean startup junkies… 

    This is hilarious!

     
  • domenicosolazzo 12:42 pm on January 23, 2011 Permalink | Reply
    Tags: Cyclomatic Complexity, LOC,   

    Analysis of code complexity 

    Complexity is everywhere. This is also true in software development. Complex code has often more bugs the simple code and it is more difficult to maintain.
    Hence, it is very important to understand and measure the code complexity.

    Even if personal measures like gut feeling, subjective measurement based on code review and pair-programming or code smells can be effective, objective measures can help to find where the bugs are hidding.

    Lines of code (LOC) could be used but it is often discarded as accurated complexity measure. The reason is that it depends from whom is reading the code. There are many factors, such as code format, to take into consideration when using LOC as complexity measure.

    The next code will show that the same code could be evaluated like 2, 4 or 8 lines of codes.

    • 2 LOC:
    if (code == true) dothis;
    else dothat;

    – 4 LOC:

    if (code == true)
        dothis;
    else
        dothat;

    – 8 LOC:

    if (code == true)
    {
       dothis;
    }
    else
    {
      dothat;
    }

    Another measurement is called ciclomatic complexity developed by Thomas McCabe, which identifies the number of linearly independent paths in a function.
    It is means that when a particular method does not contain any conditional operators, it will have only one independent path through the program. Any conditional statements will increase the number of paths through the method.

    The first step is to create control flow graph based on a source code and then calculating the complexity based on the graph.

    The formula for calculating the complexity is equals to:

    edges – nodes + 2,

    where each statement (conditional or not) is a node and an edge is the line connecting two nodes.
    Next image will show an example:

    Cyclomatic Complexity - Software testing

    Example of cyclomatic complexity measurement

    Many tools are available for large methods with a lots of decision points, such as CCCC and Code Analyzer. A detailed list of tools for calculating the complexity of large source code can be found in the end of this post.

    The main use of this measure is to check the testability of a particul function.
    McCabe gives guidelines about cyclomatic complexity. A cyclomatic complexity between 1 to 10 can be considered a simple function with little risk. Between 11 to 20 the complexity starts to be moderate as well as the risk.
    When the cyclomatic complexity of a function is up 21 (or more), the risk of that function is high and that can be considered untestable is some cases (cyclomatic complexity up to 50).

    It is important to measure code complexity in a software project but high complexity does not mean that a software is buggy, but that there is an higher risk to find them. Correlation between complexity metrics and bugs has been shown in numerous studies. Moreover, it is common that maintenance of complex code is extremely difficult because it is needed more time to get familiar with it, bug fixes and it is often challenging to implement new features without increase the complexity of the code.

    Tools:

     
  • domenicosolazzo 12:13 pm on December 25, 2010 Permalink | Reply  

    Merry Christmas! 

    Merry Christmas to everyone!

     
  • domenicosolazzo 3:18 pm on November 15, 2010 Permalink | Reply
    Tags: Programming,   

    Python: List of weekdays for an arbitrary period of time. 

    In this code example, it is shown how to construct a list of all weekdays for an arbitrary period of time. Hence, it will not include any Saturday/Sunday’s.
    The code is written using Python 2.7.

    
    import datetime
    
    class MyCalendar():
        def __init__(self, startDate, endDate):
            """
            The input parameters should be dates. Moreover, the start date should be lower or
            equal to the end date.
            """
            if type(startDate) != datetime.date or type(endDate) != datetime.date:
                raise StandardError('The parameters in input should be dates!')
            if startDate > endDate:
                raise StandardError('The start date should be lower or equal than the end date!')
            self.__startDate = startDate
            self.__endDate = endDate
    
        def getWeekDays(self):
            """
            Return a list of week days in a date range.
            """
            dateRange = (self.__endDate +datetime.timedelta(days=1)-self.__startDate).days
            # According to http://docs.python.org/library/datetime.html, the weekday function will return a value from
            # 0(Monday) to 6(Sunday).
            FRIDAY = 4
            days =  [(self.__startDate + datetime.timedelta(days=i)) for i in range(dateRange) if (self.__startDate+datetime.timedelta(days=i)).weekday() <= FRIDAY]
            return days
    
    if __name__ == "__main__":
        fromDate = datetime.date(2010,1,3)
        toDate = datetime.date(2010,1,16)
        cal = MyCalendar(fromDate, toDate)
    
        print cal.getWeekDays()
    
     
  • domenicosolazzo 6:21 pm on October 24, 2010 Permalink | Reply
    Tags: Personal   

    Stay Hungry, Stay Foolish! 

    “Stay hungry, Stay foolish!”.
    I think this is one of the most inspirational video I have never seen.
    I am sure that I will connect the dots in my life and step by step, it is clearer what I love to do, where I will be and when I will realize my dream.
    I am hungry. I am hungry of my dream!

    Watch and enjoy it.
    Stay hungry, stay foolish!

     
  • domenicosolazzo 10:31 am on October 3, 2010 Permalink | Reply  

    Test Patterns: Equivalence Class Partitioning – Part II 

    In the previous article, I described a test pattern called Equivalence Class Partitioning.

    Now, it is time to show some examples to understand it better.

    Example 1.
    Let’s a store contains a quantity of some particular item between 0 and 2000. The valid equivalence class would be:
    Valid class: 0 <= Quantity 2000
    Invalid class: Quantity < 0

    n a computer store, the computer item can have a quantity between -500 to +500. What are the equivalence classes?
    Answer: Valid class: -500 <= QTY +500
    Invalid class: QTY < -500

    Example 2.
    Let's an object X be of a certain type, such as Person, Animal, House.
    The valid equivalence classes for the particular object would be:
    Valid class: [ Type of X == Person ]
    Valid class: [ Type of X == Animal ]
    Valid class: [ Type of X == House ]

    The invalid class for the X are:
    Invalid class: [ Type of X != ( Person OR Animal OR House) ]

    These are two little examples that show how simple this technique is in practice.

     
  • domenicosolazzo 7:24 pm on September 26, 2010 Permalink | Reply
    Tags: Algorithm, Data Structures, HeapSort,   

    HeapSort: A Python example 

    In this post, I will give an introduction to a famous sorting algorithm as well as it implementation in Python.

    Heapsort is a sorting algorithm, which sorts in place like the insertion sort but it has a running time of O(n logn) as the merge-sort.  This algorithm use a data structure called “heap”, that can be compared nearly to a binary tree. Furthermore, this tree can be represented as an array and each element of this array corresponds to a node of the tree.

    The array that represents the heap has two properties, which are:

    • length[Array]: number of elements in the array
    • heap-size[Array]: number of elements in the heap stored within the array.

    The array will contain valid numbers between 1 and lenght[A]. The root of the heap can be found in the first element of the array and given an index j, it is possibile to find the indices of the parent element in the tree ( Parent( j ) ), the left element ( Left( j ) ) as well as the right element ( Right( j ) ).

    They can be calculated as follows:

    def Parent( j ): 
        return j/2
    def Left( j ): 
        return 2*j
    def Right( j ): 
        return 2*j+1
    

    There are two kind of binary heaps, called max-heap and min-heap and each of them will satisfy an different heap-property. In the case of the max-heap, the property states that, given the index j, for each node of the tree other than the root, the value contained in the array to the index Parent( j ) will be higher or equal to the value stored to the index j.

    Array[ Parent( j ) ] >= Array[ j ]

    The min-heap will satisfy the opposite property described as follows:

    Array[ Parent( j ) ] <= Array[ j ]

    Given this property, it is possible to know if the element in the root is the biggest or the smallest one.
    The algorithm is implemented using three procedures called Heapify, BuildHeapSort and HeapSort.

    The first procedure that is described is Heapify. It runs in O( logn ) and it allows to maintain the heap property in the binary tree. Its implementation in Python is shown below.

    def Heapify( A, i, n ): 
        l = Left( i )
        r = Right( i )
        if l  A[ i ]: largest = l
        else: largest = i
        if r  A[ largest ]:
            largest = r
        if largest != i:
            A[ i ], A[ largest ] = A[ largest ], A[ i ]
            Heapify( A, largest, n )
    

    BuildHeapSort procedure, which runs in O( n ), produces a heap from an unordered input array.It uses the Heapify procedure in a bottom-up manner from the element to the index n/2 to 1(the root), where n is the length of the array.

    def HeapLength( A ): return len( A ) - 1
    def BuildHeap( A ): 
        n = HeapLength( A )
        for i in range( n/2 ,0 ,-1 ):
            Heapify( A, i, n )
    

    The last procedure is called HeapSort and it runs in O( n logn ). The first step in this procedure use the BuildHeapSort subroutine to build a heap given an input array and then use the Heapify procedure to mantain the heap property.

    def HeapSort( A ): 
        BuildHeap( A )
        HeapSize = HeapLength( A )
        for i in range( HeapSize, 1 , -1 ):
            A[ 1 ],A[ i ] = A[ i ],A[ 1 ]
            HeapSize = HeapSize-1 
            Heapify( A,1,HeapSize )
    

    The original code can be found here.
    An implementation in Java as well as a nice animation of how this algorithm works can be found at http://www.cs.auckland.ac.nz/software/AlgAnim/heapsort.html.

    The algorithm is described in detail in Introduction to Algorithms,
    Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
    MIT PRESS

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel