Show OSX Battery Cycle Count in Terminal

In case you are too lazy to find info about your battery cycle count in your OSX. Here is how to check battery cycle count through terminal.

$ system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}'

Here is table information for maximum cycle count for your laptop.
mac battery count

Reference: http://apple.stackexchange.com/a/116435/199418

Create Python Class On The Fly

I just found a cool trick in python to create a class dynamically on the fly without creating the class definition.

The story behind this, I’m using Django in my project and I need to create serializer class dynamically based on user’s input. Here is how I solving my problem by creating serializer class on the fly.

def serializer_factory(list_context):
    fields = {}
    for i in list_context:
        fields[i] = serializers.CharField(required=True)
    GeneratedClass = type("SerializerFactory", (serializers.Serializer,), fields)
    return GeneratedClass

And here is how I used it.

context = ['name', 'address', 'current_location']
PeopleClass = serializer_factory(context)
serializer = PeopleClass(data=request.data)
serializer.is_valid()
"""
code above equivalent with.

class SerializerFactory(serializers.Serializer):
    name = serializers.CharField(required=True)
    address = serializers.CharField(required=True)
    current_location = serializers.CharField(required=True)
"""

“type” in python commonly uses in meta programming, and here is the detail for creating class using this keyword.

type("", (), {})

1. The first String argument is for defining class name.
2. Second argument is tuple to define your base class.
3. Third argument is dictionary for defining class property.

Create Empty Object in Python

I need to create dummy object in python for testing purpose. Here is how to create dummy object in python on the fly using “type”.

request = type('request', (), {})()  # Create request object on the fly
print request is None                # False
print request                        # <__main__.request at 0x1073f8590>

Reference: http://stackoverflow.com/a/19476841/1936697