You can extend Python relatively easily with the development libraries. Once installed, you can write a module in C, build it and start using it in your Python code.
In today’s post, I’ll create a Hello world module and use it from python.
Environment
In order to get started, you’ll need to prepare your environment with the right tools. It’s also and idea to create a bit of a project structure.
Create a directory that your code will go into. My source structure looks like this:
Note the Python.h header as well as the PyObject types being used. These are a part of the python-dev library that we installed before. This header file then gets implemented pretty simply. Here I’ve cheated using printf to do the printing for us:
#include"hello.h"staticcharmodule_doc[]="This is a simple, useless, hello module";staticcharsay_hello_doc[]="This function will say hello";staticPyMethodDefmodule_methods[]={{"say_hello",hello_say_hello,METH_VARARGS,say_hello_doc},{NULL,NULL,0,NULL}};PyMODINIT_FUNCinit_hello(void){PyObject*m=Py_InitModule3("_hello",module_methods,module_doc);if(m==NULL)return;}PyObject*hello_say_hello(PyObject*self,PyObject*args){printf("I'm here");returnPy_None;}
A brief analysis of this code sees us building a PyMethodDef array. We expose it out using Py_InitModule3' from within the initialization function (typed with PyMODINIT_FUNC`).
To out actual function itself, we’re printing “I’m here” to the console and then bailing out with a return value of Py_None, which is equivalent to None.
Building
To build our module, we’ll use setup.py. It’ll read as follows:
CQRS stands for Command Query Responsibility Segregation and is a software pattern focused on separating code that reads a data model’s state from code that updates a data model’s state. Ultimately, the implementation of this pattern leads to performance gains, scalability and headroom to support changes to the system down the line.
Separating out your reads and your writes can also give you an increased level of security.
The query model is in charge of all of your retrieves. The whole premise of having a query model is that a query will only read information and not change anything on the way through. Making this part of the process pure in the interest of the model.
The command model are all of the items of work that we’re going to perform against our model (our update model) that changes state.
Tie it all together
This is an event sourcing system, so update messages will be routed through a command layer. Where the join back to the data store that retrieves come out of is an implementation detail.
Having this separation directly at the data layer may incur eventual consistency scenarios; desirable in some settings, unacceptable in others. An event bus manages the marshaling of commands from the user interface through to the data layer. This is also an opportunity to put these commands into an event stream.
Final notes
This pattern isn’t for every situation. It should be used/applied the same way as you’d apply any other pattern; with a great measure of study and common sense. Scenarios where you have a very high contention rate for data writers would be a very good fit.
Adding logging to your python applications is simple with the standard logging module. The best part about having a standard logging module, is that every application has the ability to send log information in the same way.
In today’s post, I’ll go through a small howto on setting up logging.
Logging levels
Much like any other logging framework, python’s logging framework expects that you’ll send messages out to the logger that belong to a particular class (or level). The levels are as follows:
Level
Description
DEBUG
Debug output, trace
INFO
Informational output on successful events
WARNING
Pre-emptive notification of failures or unexpected events
ERROR
Processing failed
CRITICAL
Processing failed and the application can not recover
You notice a couple of things here; first we asked for a debug and info message here but we never saw one. Secondly, we see the format of the messages being written:
LEVEL:NAME:MESSAGE
We’re using the root logger.
The default logging level is WARNING. Anything requested below this (like INFO and DEBUG) are not emitted by the logger. We can change this with basicConfig.
logging.basicConfig(level=logging.DEBUG)
Being that DEBUG is the lowest level logger that you can ask for, we should see all of the messages.
Further configuration
To give your logger a little more context for your application, you can control the formatting parameters. The information that you can specify into your log lines is specified here.
For this example, we’ll just have the time and the log line.
Many books have been written in the past on the topic of software design patterns. The Design Patterns being one of the best. In today’s post, I’m going to skim through these patterns really quickly; just offering hint code (in java) for them.
Structural patterns are used to demonstrate the relationship between entities clearly.
Adapter
Adapter is a structural pattern and its purpose is to provide a bridge between two different interfaces to support a common contract. To illustrate, we have our way of adding two numbers and how we describe the inputs to a function.
To make an Adapter, we need to create an object that responds to TheirAdder but adapts to usage of MyAdder. To achieve this, we’ll use an adapter which just maps one interface to another. Here’s an adapter going from MyAdder over to TheirAdder.
The usage of these classes and interfaces now looks like this:
publicstaticintexecuteTheirAdder(TheirAddertheirAdder){returntheirAdder.getLeftHandSide()+theirAdder.getRightHandSide();}publicstaticvoidmain(String[]args){MyAdderImpladder=newMyAdderImpl(7,2);MyAdderToTheirAdderAdapteradapter=newMyAdderToTheirAdderAdapter(adder);intanswer=executeTheirAdder(adapter);System.out.println("The answer was "+answer);}
We’re using out adder implementation, but integrating with a function that expects their adder. So we adapt the values internally using our adapter, MyAdderToTheirAdderAdapter.
Facade
Facade is a structural pattern that allows a developer to produce a much simpler (or different) interface on top of a much more complex class. The whole idea is about making a cleaner interface against a complex object. The following class details a few of the things that you’d do when starting and stopping a car. Note how verbose these operations are:
I have already grouped these methods into two. The top set of methods apply to starting the car, the second are about stopping the car. We can create a much simpler interface across this object with a facade class:
From here, we no longer need to call purClutchIn then turnKeyOn then pumpFuel, etc. to start the car. We create a CarOperationFacade instance (passing in our Car instance) and the we can use start and stop.
Composite
The Composite pattern is about being able to treat different objects in the same manner. This is useful for when groups of object types need to be treated the same at some level. For this example, we’re going to create an expression tree for mathematical expressions. Everything in our expression language will be based off a root concept called an Atom. All an Atom does is emit its value:
publicabstractclassAtom{publicdoublegetValue();}
We then create other sub classifications from this class like LiteralValue which will just represent a number and Operation which will allow us to define two Atoms concatenated by an operator:
Operation holding two further Atom references where it’s an Atom reference itself.
Bridge
The Bridge pattern is simply dividing out implementation details from the abstraction. If your class tree (hierarchy) has the potential to be too complex, the bridge pattern can assist in keeping your abstraction as implementation-dependency-free as possible.
publicabstractclassDrawing{publicvoidputPixel(intx,inty,intcolour);}publicclassGLDrawingextendsDrawing{@OverridepublicvoidputPixel(intx,inty,intcolour){/* OpenGL code here */}}publicclassDirectXDrawingextendsDrawing{@OverridepublicvoidputPixel(intx,inty,intcolour){/* DirectX code here */}}
As more drawing libraries came along, you’d be creating more Drawing derivatives directly tying the implementation to your abstraction. If we separate this out though, we can free the Drawing class of any derivatives.
publicabstractclassDrawingImpl{publicvoidputPixel(intx,inty,intcolour);}publicclassDrawing{privatefinalDrawingImplimpl;publicDrawing(DrawingImplimpl){this.impl=impl;}publicvoidputPixel(intx,inty,intcolour){this.impl.putPixel(x,y,colour);}}publicclassGLDrawingextendsDrawingImpl{@OverridepublicvoidputPixel(intx,inty,intcolour){/* OpenGL code here */}}publicclassDirectXDrawingextendsDrawingImpl{@OverridepublicvoidputPixel(intx,inty,intcolour){/* DirectX code here */}}
Proxy
The proxy pattern is a structural pattern that provides a wrapper to access another object. It comes in a few different flavors:
Remote
Virtual
Protection
Remote proxies are used when the object is not in the local system-space; like accessible over the network or as a target of inter-process-communication (i.e. in a different JVM). Virtual proxies are used to defer work and make initialization more-lazy and finally the Protection proxy is about forming a layer that determines the safety of interacting with an object.
Creational patterns
Creational patterns aim to control the construction phase of an object. Constraining this process allows a developer to control the lifetime of their object system effectively.
Singleton
Singleton controls the instance count of a class in your application. These classes come in two different flavors: eager and lazy. An eagersingleton will instance itself as soon as the class’s static members come into scope:
One of the important pieces of this pattern is that the class’s constructor is marked private. This prevents integrating code from creating extra instances. This is exactly what this pattern aims to prevent.
The singleton reference is acquired through the use of getInstance.
A lazy variant on the class takes the initialization (construction) and places it into the singleton acquisition method getInstance.
This is handy when your constructor is performing work that you’d prefer to defer until you actually need the class. As a final note, the aim of this pattern is to control the developer’s ability to instantiate this class as such you’ll need to control any cloning/rehydration/deserialization techniques that developers may be able to use to inadvertently create another instance of your singleton.
Builder
The builder pattern gives the developer some syntactic sugar over the construction process, cleaning up massive constructor signatures.
We can then start creating Vehicle objects and these will depend on the factory:
// create some carsAssemblyLineassemblyLine=newAssemblyLine(newCarFactory());Vehiclecar=assemblyLine.getVehicle();
Prototype
The prototype pattern is a creational pattern that focuses on lowering the construction overhead. In order to achieve this in Java, the pattern will lean on java’s Cloneable interface.
publicinterfaceMembershipPrototypeextendsCloneable{publicMembershipPrototypeclone()throwsCloneNotSupportedException;}publicclassMembershipimplementsMembershipPrototype{@OverridepublicMembershipclone(){return(Membership)super.clone();}}publicclassMembershipProvider{privatestaticMap<String,Membership>map=newHashMap<String,Membership>();static{/* TODO: build membership map in this block */}publicstaticMembershipgetMembership(Stringkey){Membershipm=map.get(key);if(m!=null){returnm.clone();}returnnull;}}
The expensive load part is put in the static block above, only executed once. From there, objects are cloned into the sytstem.
Behavioral patterns
Behavioral patterns focus on the relationships between objects and how they communicate with each other.
Observer
The observer pattern is a behavioral pattern that provides a class the ability to publish and subscribe (pub/sub) state changes and messages.
Java already has the observer pattern baked in with Observer and Observable members of the java.util namespace. In this example, a cricket game is being played. Two batsmen are being observed by the scoreboard. So we define our batsmen with names and run totals; they also have the ability to score runs:
There would be heaps of observers, but in this case it’s going to be the scoreboard that observes the batsmen. Here’s our scoreboard:
publicstaticclassScoreboardimplementsObserver{@Overridepublicvoidupdate(Observableobs,Objectx){Batsmanb=(Batsman)obs;System.out.println(String.format("%s just scored %d runs and is currently on %d",b.getName(),x,b.getRuns()));}}
Each time that a batsmen scores runs with scoreRuns, the scoreboard gets notified; we at least it does if we use addObserver:
You can use the mediator pattern to loosen the coupling of your objects. It’s the job of the mediator pattern to define how a set of objects interact.
When you have many objects performing operations on each other, the direct dependency is removed by abstracting your operations out into an interface. The interface is your mediator that satisfies all of the now disparate parts of your object system.
In this example, the program is managing a baseball game. The following interface denotes all of the actions that can go on in our game.
All of our system objects now become BaseballParcipant derivatives. They, as usual, hold their actions that they manage (batter hits a ball, fielder catches a ball, etc.) but the implementation of these actions enforces its side-effects through the usage of the BaseballMediator instance that gets set.
The chain of responsibility pattern serves as a request forwarder. A request that you supply to the chain moves through the chain itself until the contents of the request match the implementation. If you needed to perform some processing on a Executive, Manager, Supervisor or Employee you would start by creating your processing contract:
The important part of this contract really is the setHandler. It’s going to to simulate our chain for us:
publicclassExecutiveimplementsStaffHandler{privateStaffHandlerhandler;@OverridepublicvoidsetHandler(StaffHandlerhandler){this.handler=handler;}@Overridepublicvoidprocess(Jobjob){if(job.getLevel()>1000){/* perform "Executive implementation" here */}elseif(this.handler!=null){/* move down the chain */handler.process(job);}else{System.out.println("Job type not supported");}}}
Manager, Supervisor and Employee implementations would look very similar to Executive.
The flyweight pattern takes the construction of objects and caches instances to save on the construction process. Really useful when you have a large volume of objects that are quite similar.
publicfinalclassStaffFactory{privatestaticMap<String,Staff>map=newHashMap<String,Staff>();publicstaticsynchronizedStaffgetEmployee(StringemployeeType){Staffstaff=map.get(employeeType);if(staff==null){/* construct the staff member */map.put(employeeType,staff);}returnstaff;}}
Momento
The momento pattern is all about taking snapshots of an object so that you can provide undo functionality to your class.
In this example, toString traverses a tree but offers the developer getLeafText to control the leaf’s representation in the string.
State
The state pattern will allow you to explicitly manage state changes in your objects by defining the equivalent of a cartesian map of functionality. The CarState interface in the following example lays out all of the actions that our objects will perform.
For each state that we define, we now need a concrete implementation of the CarState interface:
publicclassCarStateTurnOnimplementsCarState{privatefinalCarcar;publicCarStateTurnOn(Carcar){this.car=car;}@OverridepublicvoidturnOn(){System.out.println("Car is already turned on");}@Overridepublicvoiddrive(){System.out.println("Driving . . .");this.car.changeState(this.car.getDriveCarState());}@Overridepublicvoidstop(){System.out.println("Stopping . . .");this.car.changeState(this.car.getStopCarState());}@OverridepublicvoidturnOff(){System.out.println("Turn off . . .");this.car.changeState(this.car.getTurnOffCarState());}}
Our Car implementation itself will use this state interface to define its structure, but we’ll also define a variable (of type CarState) for every state that the car can be in.
publicclassCarimplementsCarState{privateCarStatecarTurnOn;privateCarStatecarDrive;privateCarStatecarStop;privateCarStatecarTurnOff;privateCarStatecurrent;publicCar(){this.carTurnOn=newCarStateTurnOn(this);this.carDrive=newCarStateDrive(this);this.carStop=newCarStateStop(this);this.carTurnOff=newCarStateTurnOff(this);this.current=this.carTurnOff;}publicCarStategetTurnOnCarState(){returnthis.carTurnOn;}publicCarStategetDriveCarState(){returnthis.carDrive;}publicCarStategetStopCarState(){returnthis.carStop;}publicCarStategetTurnOffCarState(){returnthis.carTurnOff;}privatevoidchangeState(CarStatenewState){this.current=newState;}@OverridepublicvoidturnOn(){// from state x to state ythis.current.turnOn();// reflect state change internallychangeState(this.carTurnOn);}@Overridepublicvoiddrive(){// from state x to state ythis.current.drive();// reflect state change internallychangeState(this.carDrive);}@Overridepublicvoidstop(){// from state x to state ythis.current.stop();// reflect state change internallychangeState(this.carStop);}@OverridepublicvoidturnOff(){// from state x to state ythis.current.turnOff();// reflect state change internallychangeState(this.carTurnOff);}}
Strategy
The strategy design pattern is more in the name than anything else. From a common base, you’ll create derivatives that a executioner will use to perform an action.
Being able to interpolate values from 0 up to 1 by means of linear or trigonometric methods is an implementation detail for the strategy pattern.
Command
The command design pattern is all about dispatching messages (or instructions) from an invoker to a receiver. In sorts, you can think of it a message pump:
publicinterfaceDataJob{publicvoidexecute();}publicclassCreatePersonimplementsDataJob{@Overridepublicvoidexecute(){// TODO: database code here to create a person}}publicclassDeletePersonimplementsDataJob{@Overridepublicvoidexecute(){// TODO: database code here to delete a person}}publicclassActionManager{publicList<DataJob>jobs=newArrayList<DataJob>();publicvoidrun(){// TODO: process the job queue, calling "execute"// on items fetched}}
Interpreter
The interpreter pattern is about making an interpreted languages (and resulting execution environments). There are a lot of different tools already available to take language agnostic gammar definitions and output source code that will interpret this.
Decorator
The decorator patterns allows a developer to separate commonly described attributes into smaller class implementations. These smaller pieces are the composed together (or the original instance is decorated) so that we end up with an object that has more extras.
The Car interface defines what’s important to us about the car, where as CarDecorator is how our system explains the pieces that we’re going to decorate our Car with.
Paint and Wheel are both concrete decorations and can be applied like so:
The iterator pattern allows us to treat a sequence of objects in a uniform way. The action of traversal is supplied in a common way so that sets can be enumerated in similar ways.
In java, this is achieved using the Iterator interface.
Pretty Good Privacy is a cryptographic computer program used to encrypt and authenticate messages. GnuPG is the free replacement to this program. It is an implementation of the OpenPGP Message Format.
Today’s post will be about the creation of GPG keys, using them to encrypt and authenticate a message as well as the verification process.
Creating your keys
First job is to create your credentials. These keys are what people are going to use that certify that something was from you.
To generate your keys:
gpg --gen-key
You’re then asked what kind of key that you want:
gpg (GnuPG) 1.4.18; Copyright (C) 2014 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
The default of RSA and RSA is fine. This says that we’ll use RSA keys to sign and encrypt our messages.
Size and expiration are specified next.
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
In my example here, I’ve chosen a key length of 2048 and a key won’t expire. Now you’re asked for your Real Name, E-mail address and Comment. It’s a good idea to use real or correct values for Real Name and E-mail address as people will see these and use them as a visual indicator to authenticate you. In the Comment field, I normally put the key’s purpose (email/personal/office/etc).
After some generation, the key generation process will present you with your fingerprint details. The 8 HEX chars that identify your public key are significant. This is the ID that you’ll use to reference this key.
At any point you can take a look at this fingerprint information with the following:
gpg --fingerprint user@email.com
Backup your keys
You always need a way that you can get hold of your keys. In the next steps we’re going to export your private and public keys to file.
It’s important to backup your private key in case you lose it. Your hard drive crashes, or house gets flooded. You need another copy of your secret put somewhere else. So, you can get a text copy of the key with the following.
You need to be able to freely distribute your public key to people as this certificate is what people will use to verify messages sent by you are actually send by you! It’s also what’s used in the encryption process for messages sent to you.
So that other people will have the opportunity to look up your public key for authentication or decryption purposes, you’ll need to send your public key to a key server.
gpg --send-key ABCD1234
By default, this sends the key out to keys.gnupg.net.
gpg: sending key ABCD1234 to hkp server keys.gnupg.net
You can change this behavior and send your key to another server by specifying the keyserver using the --keyserver switch.
Certificate revocation
It’s also a good idea to generate and store away a revocation certificate so that at any time, you can revoke your generated certificate.
If you do need to execute on this, it’s just about importing this revocation certificate into the store.
gpg --import user-email-revoke.asc
To let everyone know about it, you also need to push the certificate out to the key server.
gpg --keyserver subkeys.pgp.net --send ABCD1234
Testing it out
There are heaps of different programs that integrate with GnuPG, but for today’s purposes we’re just going to create a simple text message; sign and verify it.
So, let’s create a message:
cat> message.txt
Hello, world!
^C
We can use a binary format to sign and verify:
# sign the message
gpg --output message.sig --sign message.txt
# verify and extract the original message
gpg --output message.verf.txt --decrypt message.sig
This should be pretty straight forward. The more familiar text armored messages are done using the --clearsign switch:
# sign the message
gpg --clearsign message.txt
# verify the message
gpg --verify message.txt.asc