Apr 16, 2015 - Back


I’ve been away from blogging for a while. Business has been prety hectic.
As a CTO of a growing startup, I am facing the inevitable decrease of coding time, and an increase of management time.
I have to proactively find time to code and try new things, otherwise my time gets filled up with non-technical matters more and more.

There are things I want to do - going deeper into Python(Django) and Ruby(Rails), trying out new PHP frameworks, trying Cassandra, reading into widely used open source projects, etc…

As a profession neck-deep into technology, I don’t want to slow down my pace of learning new technologies. A challenge for my coming years…

Meanwhile, one thought I had which is worth noting here:

Feb 25, 2015 - Python class variables and instance variables


I’m still a Python beginner, still getting used to the concepts. It’s a relatively simple and clean language to use. I haven’t gotten into using frameworks yet, but since the fundamentals of the language are very simple, I’m ready to jump into frameworks (like Django) already.

Here’s a little something I want to keep note of - the scope and accessing of class variables and instance variables.

Instance Variables

Instance variables are declared and accessed with {instance}.{name}.
Within a method of the same class the {instance} will translate to self. Below is an example of two ways of accessing the instance variable var.

class MyClass:
	def myMethod (self):
		self.var = 10

obj = MyClass()
assert obj.var == 10

Class Variables

Class variables are declared as such:

class MyClass:
	var = 10

assert MyClass.var == 10

The class variable can be accessed by {ClassName}.{name}.
But actually, you can read the class variable by {instance}.{name}. This takes us to the next question:

Instance Variables with the same name as Class Variables

What if you declare an instance variable, with the same name as a class variable?
Here is the quick answer via code:

class MyClass:
	var = 10

c1 = MyClass()

assert hex(id(c1.var)) == hex(id(MyClass.var))

MyClass.var += 1
assert hex(id(c1.var)) == hex(id(MyClass.var))
assert c1.var == MyClass.var

classVarAddr = hex(id(MyClass.var))
c1.var += 1
assert hex(id(c1.var)) != classVarAddr
assert hex(id(MyClass.var)) == classVarAddr
assert c1.var == MyClass.var+1

Note: hex(id({variable})) gives us the memory address of the variable.

First, the c1.var and MyClass.var have the same memory address, when reading the variable.
Next, MyClass.var is incremented. The value and memory address of c1.var and MyClass.var are still both identical.
The next step is the interesting part - When c1.var is modified, the memory address of c1.var and MyClass.var differ. The address of MyClass.var does not change from previously, but c1.var is allocated a new address - meaning, it becomes a new variable.

In the example we are writing into c1.var with the += operand, which is in fact c1.var = c1.var + 1. So the class variable is read, incremented, then stored into the new memory location.

This was an interesting behavior of the Python language, and wanted to take note of it. Otherwise the Object-Oriented Programming in Python seems fairly intuitive and straight-forward as I know.

Feb 24, 2015 - Updating iOS Certificates, Provisioning Profiles


Managing iOS certificates are always very tiring. Once a year, we have to go through some dulling manual labour of re-generating and applying certificates (which I cannot seem to find a way to automate).
As a memo, I will be leaving some notes on how to manage these stuff.

App Certificates

First is creating certificates. There is the iOS Development and iOS Distribution certificate for managing your application (I will talk about the APNS certificate later). The Development certificate is used for creating provisioning profiles for your development apps, and the Distribution certificate is used for provisioning profiles which you want to submit to the App Store.
To create one, go ahead and create a new certificate in the Apple Developer page.
Select either iOS App Development or App Store and Ad hoc, and continue.

Creating a CSR file

If you do not have a CSR, just follow the instructions and create a new CSR.
[Note] You don’t have to be creating CSRs every time you want to create/renew a certificate. Just save your CSR file once, maybe one for your organization/team - and just use that.
If you want to feel more secure, you may want to have two CSR files - one for Development, one for Distribution.

Provisioning Profiles

After you create your certificate, you can create your provisioning profiles. (I will omit the step of adding devices because it’s pretty easy)

Just to clarify:
A Provisioning Profile is a file that manages the combination between devices and Certificates - in simplified words, it says “A certain app signed with a certain certificate can run on a certain device/environment”.

So when you go and create a new Provisioning Profile, you select the app (bundle ID), the certificate, and for a Developement Provisioning Profile, a list of devices to allow.

[Note] When there are new devices to allow, you must edit that Provisioning Profile to allow the new device, and re-create the Provisioning Profile.

Once you create the Provisioning Profile, you are ready to create your app.
On your Xcode project, select the certificate you’ve created in the Code Signing Identity, and select the created provisioning profile in the Provisioning Profile.
Your app should run on whichever device you’ve allowed on the Profile, or it should be good for submission if you’ve selected the Distribution Profile.

APNS Certificates

Sending APNS from your server also require a PEM file, which is made using certificates.
First, create APNS certificates just as you did the App Certificates. (I assume you can use the same CSR file also).

Next, follow these steps to create a PEM file (On a Mac OS):

  1. Download and double-click the certificate file. This should import the certificate into your keychain.
  2. Open the Keychain Assistant, and look for the certificate you just imported.
  3. Expand the certificate, then select ONLY the certificate. (It won’t work properly if you select both)
  4. Right-click the highlighted files, and “Export 2 elements”. Save them without a passphrase.
  5. On your terminal, run the following command:
    openssl pkcs12 -in {exported_certificate}.p12 -out apns.pem -nodes -clcerts

Then the apns.pem should be created.
Now you can use that PEM file for your APNS.

This should be enough to get you through the certificates of iOS apps.