Factory Pattern in Python

I recently used the factory Pattern in Python.

It was a little surprising to me comparing typical usage of this pattern in C# v/s Python.

In particular, in C# you’s typically have an Interface defining the methods and then provide an implementation of these methods in the concrete classes.

In Python, there is no interface – In the example above I embed a static  method in the base  to select the appropriate derived class.

Advertisements

Handling Exceptions.

Some simple examples for exception handling:

C#

Python:

x = 10
y = 20
try:
    if x > y:
        print "abcd"
    else:
#        dividing by 0
        print 2 / 0

except Exception as e:
    print "hit an exception : {0} : {1}".format(e, e.message)

#Output:
#hit an exception : integer division or modulo by zero : integer division or modulo by zero
###

In Python there are two keywords related to exceptions:

  • pass.
    • Think of this as same as continue.
  • raise
    • If we want to bubble up the exception, then ‘raise’ it . Else the exception is suppressed.

Shuffling and Splitting Operations

Shuffling is a pretty interesting operation in several scenarios. And different languages / platforms have  interesting features using the shuffling  operation.

Examples:

  • Shuffle the contents of a C# List
  • Select random lines from file  (using ‘shuf’ command in Linux)

References:

Code:

DecisionController

JSON DateTime

“DateTimes in JSON are hard.

The problem comes from the JSON spec itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers, and floats, but it defines no standard for what a date looks like.”

http://www.newtonsoft.com/json/help/html/datesinjson.htm

 

References:

 

Code:

DecisionController

 

Multi-dimensional arrays in Python and C#

While solving a problem which requires representation in matrix form, one requires close understanding of how to represent multi-dimensional arrays.

I was trying to explore how to go about having a matrix representation in C# and Python.

Tips:

  • In Python, use the numpy library.

 

 

References:

 

Code:

 

Understanding the SynchronizationContext and Best Practices in Asynchronous Programming.

For doing asynchronous programming in C#, one needs to understand the concept of SynchronizationContext.

I came across this nice set of posts by Stephen Cleary, where he explains some of the fundamental design choices and best practices.

The primary reason for this line of exploration for me was because of a deadlock I hit when working on a web app using the async pattern. This problem is described here.

References:

 

Redis : Sorted Sets

I recently used Redis SortedSets quite heavily. I had a few pre-requisites:

  1. Should be extremely fast.
    • Think usage in the context of  a near real-time web API
  2. Should scale to billions of entities
  3. Should be able to give me the “rank” of an item.
    • Assume each item has a numeric value that is used to determine rank
  4. Should be able to give me the count of the total number of items in the set
  5. Should be able to give me the sum of the values in the set**
  6. Should be able to give me the cumulative sum of the values from the 0th item to kth item based on their ranks**

**I am yet to figure out how to do 5-6. Perhaps there is a way to do this in Redis as well.  Else, I am planning to do some form of Reservoir Sampling to get an approximation of the sums.

Observations:

  • Redis SortedSets support 1-4 out of the box!
  • In general I am amazed at the advanced internal data structures that are used in Redis.
  • Sorted Sets for example use SkipLists internally.
    • This serves as a great motivation to do a blog post on SkipLists actually.
    • Also, RangeQueries maybe. Sorted Sets support range operations which are quite handly.  Need to understand how those are supported internally.

Code:

 

References: