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

Advertisements

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

Deploying WordPress in Heroku

Download latest version of wordpress.

$ wget http://wordpress.org/latest.zip
$ unzip latest.zip
$ mv wordpress mywebsite # optional if you want to rename the folder.

Create “Procfile” and put this following line.

web: vendor/bin/heroku-php-apache2

Create “composer.json and put this following line.

{"require": {"php": "5.6.27"}}

Configure the wordpress by creating “wp-config.php”. WordPress already had the template, so you just need to rename it.

mv wp-config-sample.php wp-config.php

Edit “wp-config.php” and replace following line.

// ** MySQL settings - You can get this info from your web host ** //
$url = parse_url(getenv('DATABASE_URL') ? getenv('DATABASE_URL') : getenv('CLEARDB_DATABASE_URL'));

/** The name of the database for WordPress */
define('DB_NAME', trim($url['path'], '/'));

/** MySQL database username */
define('DB_USER', $url['user']);

/** MySQL database password */
define('DB_PASSWORD', $url['pass']);

/** MySQL hostname */
define('DB_HOST', $url['host']);

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

define('AUTH_KEY',         getenv('AUTH_KEY'));
define('SECURE_AUTH_KEY',  getenv('SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY',    getenv('LOGGED_IN_KEY'));
define('NONCE_KEY',        getenv('NONCE_KEY'));
define('AUTH_SALT',        getenv('AUTH_SALT'));
define('SECURE_AUTH_SALT', getenv('SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT',   getenv('LOGGED_IN_SALT'));
define('NONCE_SALT',       getenv('NONCE_SALT'));

Now is the deployment process, ensure you have installed heroku toolbelt and already sign in on it.

$ git init
$ git add -A
$ git commit -m "basic setup wordpress heroku"
$ heroku create
$ heroku addons:add cleardb

After that, put following environment variables with new values. You can generate those in here.

heroku config:set AUTH_KEY=''
heroku config:set SECURE_AUTH_KEY=''
heroku config:set LOGGED_IN_KEY=''
heroku config:set NONCE_KEY=''
heroku config:set AUTH_SALT=''
heroku config:set SECURE_AUTH_SALT=''
heroku config:set LOGGED_IN_SALT=''
heroku config:set NONCE_SALT=''

Now we need download all the dependencies, ensure you have composer installed. If you are not, you can install following this.

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

Last step is deployment.

$ git add composer.lock
$ git commit -m "final step"
$ git push heroku master

Reference: https://ksylvest.com/posts/2014-05-02/deploying-wordpress-to-heroku