Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, 4 October 2014

Experiments with Pexpect

The Pexpect python module is used to start up and control new processes from python code.  For the Manchester CoderDojo I needed to incrementally get output from a spawned process over the course of a long run.  To test this I created a simple module that counted to 20 slowly:


"""
Count to 20 in one second steps
"""
from time import sleep

for i in range(1,21):
    print(str(i))
    sleep(1)

Next I created a python module to spawn off a process to call the counter and print the output.

"""
Call the slow-running counter and print the results.
"""

import pexpect

output = pexpect.run('python count-slow.py')
print(output)

The problem of course is that the run command blocks until all the output is produced.  The solution allowing you to  see the output as it appears is to use the pexpect.spawn() function.  This function to creates a sub-process object from which you can read one line at a time.  Pyexpect.readline() will block until it receives a line.  An empty string indicates the process has completed.

"""
Call the slow-running counter and print the results.
"""

import pexpect

child = pexpect.spawn('python count-slow.py')
next_line = child.readline()
while next_line != '':
    print(next_line.strip())
    next_line = child.readline()

Formatting Python Code for the Web with Pygments

I've been unsatisfied with Blogger's lack of easy handling for code formatting.  To fix this I installed Pygments.  I can now type,

pygmentize -f html -O noclasses count-slow.py

...and end up with output that I can paste into the Blogger HTML editor window.

The result is something like this:

"""
Count to 20 in one second steps
"""
from time import sleep

for i in range(1,20):
    print(str(i))
    sleep(1)




Friday, 3 October 2014

Vagrant Working for the Manchester CoderDojo Minecraft Server Build

I've got a basic vagrant setup working for the Manchester CoderDojo Minecraft server build.  Instructions on the GitHub page.

Next task is to get the chunked responses working so that you can see the python output appear in the browser as the code is running.

Monday, 29 September 2014

Vagrant and Docker Workflow

I've recently been looking into using both Vagrant and Docker to develop my projects.  This is the work flow that I've settled on...

  • I'll use Vagrant to support my development work
  • I'll use Docker as the mechanism for getting my projects hosted
  • Start a project with a Vagrant set up file that will launch a Debian vm and apt-get Docker installed on it
  • The Vagrant VM is used to build the code
  • The result of of the development code is to build a Docker image of the project
  • The Docker image is what is then used to deliver the project's services

As an example I created vagrant-docker-flask on GitHub to show how this works.

  1. Clone the git repo: git clone https://github.com/davegoopot/vagrant-docker-flask.git
  2. In the cloned directory run:  vagrant up
  3. You now have the dev machine image installed that you can connect to using: vagrant ssh
  4. Inside vagrant run: cd /vagrant/flaskserver/
  5. Then build the flask server image with: sudo docker build -t="local:flask" .
  6. Start up a container from the image with:  sudo docker run -d -p 5000:5000 local:flask python demo.py
  7. The flask server will run and be accessible from the original host machine at http://localhost:5000/
  8. To work on the code, you can now edit flask/demo.py, run the docker build and the docker run command again and see your changes

Monday, 20 May 2013

Setting Up ScriptCraft

I've been looking at ScriptCraft as something for us to play with at the next MadLab CoderDojo.  Here are my notes on getting the server running...

(1) First I created a new "scriptcraft" user on the server
(2) I then installed Bukkit.  I'm running the "02110_1.5.2-R0.1" beta version of Bukkit so that the 1.5.2 Minecraft client works
(3) I set up a startserver.sh batch file to hold the commands to start things up
(4) I edited the server.properties file to bind Bukkit to port 25566.  There is the vanilla Minecraft server already running on the standard port 25565
(5) I changed to the plugins directory and pulled down the latest ScriptCraft.jar file


wget http://scriptcraftjs.org/download/2013-03-31/ScriptCraft.jar

(6) I then restarted the server and logged in with the Minecraft client, added my user to ops.txt,  typed

/js up().box('35:15', 4, 9, 1)

...and made myself a nice 2001: A Space Odyssey style monolith.


Sunday, 23 September 2012

File Editor And SFTP Combinations

Just a quick note to list my current combinations for editing remote files over SFTP is:

Windows PC Based

Notepad++ and the NppFTP plugin that comes in the default installer.

Mac Based


Android Based

Saturday, 30 April 2011

Send and Receive SMS with Python

I was thinking about writing a text-based game similar to the classic Fighting Fantasy books, but using SMS as the delivery mechanism. To do that I would need a way to send and receive SMS text messages to and from a server. As a challenge to see how that would work I wanted to write a prototype that would look like this:

CPU>>User: "Hello! What is your name?"
User>>CPU: "Fred"
CPU>>User: "Nice to meet you Fred"

The program itself is obviously easy. The problem is how to do the input and output over SMS. On to Google to see if I could find anything. Second link down on my first search I found the Active State code recipe 'Send and receive SMS messages using TextMagic'. It had a nice looking python package available. Reading the TextMagic website it seems that the only way to get started is to buy 200 SMS credits for £20. Hmm a little pricey as a barrier to entry.
I remember using Clickatell for this sort of thing ages ago. I took a look back at their site. The reality-check came in pretty quickly as Clickatell was going to cost minimum of €100 to set up and then €25 per month to run. So, back to TextMagic. And then I found out how to sign up for a free trial account!
With the free trial set up the next job was to set up the python environment. I use virtualenv to keep my python installation clean. To get started therefore created a new virtualenv.

$ virtualenv sms
$ source sms/bin/activate

I then installed the PyTextMagicSMS package.
$ sms/bin/easy_install PyTextMagicSMS
The first job was to try the sample sending program:

import textmagic.client
client = textmagic.client.TextMagicClient('your_username', 'your_api_password')
result = client.send("Hello, World!", "1234567890")

...which worked a treat. Obviously change the method parameters to match your settings.
So onwards to reading the Text Magic documentation to find out how to receive a reply. There are two ways of receiving a reply. Either you can poll your in-box to see what messages you have received, or you can set a callback URL to be notified of the response. The callback URL is clearly the way to go for anything of any complexity, but just to get started we will poll the in-box.

import textmagic.client
client = textmagic.client.TextMagicClient('name', 'pw')

received_messages = client.receive(0)
messages_info = received_messages['messages']
print('%d messages in in-box' % len(messages_info))
if len(messages_info) > 0:
first_message = messages_info[0]
print("Message from: %s" % first_message['from'])
print(first_message['text'])
client.delete_reply(first_message['message_id'])


Okay, so we're getting close to a solution. I'll have one program that sends the hello part of the conversation, a second program that checks the in-box for replies and then sends the final message. Here's the sending one...

import sys
import textmagic.client


client = textmagic.client.TextMagicClient('username', 'password')
no = sys.argv[1]

result = client.send("Hello! What is your name?", no)


...and here's the receiving one...

import textmagic.client
client = textmagic.client.TextMagicClient('username', 'password')

received_messages = client.receive(0)
messages_info = received_messages['messages']
print('%d messages in in-box' % len(messages_info))
if len(messages_info) > 0:
first_message = messages_info[0]
from_no = first_message['from']
name = first_message['text']
print("Message from: %s" % from_no)
print(name)

client.send("Nice to meet you %s!" % name, from_no)
print("I have sent a reply")
client.delete_reply(first_message['message_id'])


All this needs is a main loop round it to keep polling for input and we're done.

Monday, 4 October 2010

Running Python 2.7 on Debian

Here are some quick notes about how I got Python 2.7 running on Debian.  The pointers come from the Python NW Google Group and the Debian documentation.

(1) Set up the apt/sources.list file as follows:

deb http://ftp.uk.debian.org/debian/ unstable main contrib non-free
deb http://ftp.uk.debian.org/debian/ experimental main contrib non-free
deb http://security.debian.org/ testing/updates main contrib


(2) Run aptitude and do an upgrade to refresh the package lists
(3)  I had 45 packages to update so I installed them all
(4) That left me with python 2.6 installed
(5) Searched in aptitude for python 2.7.  Marked it for installation
(6) Aptitude seemed to nicely manage all the dependencies.


With the above complete 'python --version' reports 2.6 and 'python2.7 --version' reports 2.7.

I'll keep the virtual box snapshot of the previous version about for a while just in case.

Monday, 9 August 2010

Iphone and HTML5 Canvas

My father-in-law wants a program to mess about with the Mandelbrot set. Ideally this should work in a browser from the iphone. I'm thinking this would be a good little project to try out using the HTML5 canvas element. Any one know how well HTML5 canvas is supported on the iphone Safari browser?

Saturday, 30 January 2010

Animation Competition

Manchester University CS department is running a schools animation competition. Despite the wording in the rules, it is open to home educated children in the UK. I've been taking a look at the acceptable animation tools before trying them out on DS #1.
Allowed tools
ToolWebsiteNotes
AliceAlice is available as a free download from www.alice.orgDesigned to be a tool for students to be introduced to Object Oriented computer programming.
ScratchScratch is available as a free download from scratch.mit.eduIntended to teach basic computer programming concepts.
Adobe FlashFlash is available for download as a free 1 month trial from AdobeEveryone knows what flash is.
Serifhttp://www.serif.comSerif DrawPlus is a drawing and animation program from Serif Ltd.
GreenfootGreenfoot is available as a free download from www.greenfoot.orgGreenfoot is Java-based programming environment for novice programmers
Scratch
I looked at Scratch on the grounds that nobody got dumber by picking MIT. Before I could get any examples working I had to get the Java plugin installed for Chrome. That was a big download, so I moved on to Alice.
Alice
Alice looks really promising. In particular it has a version just for younger children called Story Telling Alice. I downloaded, unzipped and ran the tutorials. In a few minutes I was merrily scaring the pants off a small boy with a field full of spiders. What looks really interesting about Alice is that while the interface uses a lot of text, you can click and drag the words, hence no typos.
I'm going to stick with Alice for a while and see how far I can get.