Author: adiyatmubarak

Handle Base64 Encoded Binary File in DRF

I’m writing a simple class for DRF field for handling base64 string for attachment both image and file. I’m using DRF 3 at this case.

And here is how to use it following below.

class MyModelSerializer(serializers.ModelSerializer):
    company_logo = Base64CharField(required=False)

    class Meta:
        model = MyModel
        fields = '__all__'

Feel free to use and improve my code. 😀

Fix Java Won’t Update in OSX Sierra

I have an issue using Oracle JDK in my laptop that the auto updater successfully update the JDK but the update is not applied on my system. I check my java version using.

$ java -version

But still showing the old version rather than the latest one.
I fix my issue by putting this following line on my bash configuration.

export PATH="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:$PATH"

Python: Fix getting “about:blank” in Selenium with PhantomJS

Having an issue with selenium when loading some URL with selenium using PhantomJS.
When I get the current_url some URL returned “about:blank” instead of the valid URL.

After several workarounds, the issue because PhantomJS by default using “SSLv3” and many websites had a bug in SSL migrate to TLS and caused got a blank page.

Here is how I solve my issue.

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])

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

Python: Fix “OSError: [Errno 9] Bad file descriptor” in Selenium using PhantomJS

I’m having an issue when closing selenium driver and sometimes the selenium driver failed to quit. Here is how to I solve this issue.

import signal
from selenium import webdriver

driver = webdriver.PhantomJS()
# Some code goes here
driver.service.process.send_signal(signal.SIGTERM)
driver.quit()

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

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.