Wednesday, April 15, 2015

Deep male mysteries

Listen to Into the Deep Male Mysteries - Robert Bly Interview by bladesofgrass #np on #SoundCloud https://soundcloud.com/bladesofgrass/into-the-deep-male-mysteries-robert-bly-interview

The white cow by Rumi

There is a small green island where one white cow lives alone, a meadow of an island. The cow grazes till nightfull, full and fat, but during the night she panics and grows thin as a single hair. What shall I eat tomorrow? There is nothing left. By dawn the grass has grown up again, waist-high. The cow starts eating and by dark the meadow is clipped short. She is full of strength and energy, but she panics in the dark as before and grows abnormally thin overnight. The cow does this over and over, and this is all she does. She never thinks, This meadow has never failed to grow back. Why should I be afraid every night that it won't. The cow is the bodily soul. The island field is this world where that grows lean with fear and fat with blessing, lean and fat. White cow, don't make yourself miserable with what's to come, or not to come.

Wednesday, April 8, 2015

Android Meets Python And they have a picture together!

I recently wrote a simple script to take a picture from an android phone through Python in SL4A and send it over the network through socket programming to a client which saves it to file. Below is the code for the client on android! Please be sure to change your IPs.  A couple of other things, SL4A Python is not working in Android 5.0+. I tested this code successfully on Android 4.4 camera phone as the client and my PC running Windows 7 as the server. If you need more information you can check this out: http://faculty.pieas.edu.pk/fayyaz/ippy/html_demos/android.html

Android Client
Exported from Notepad++
# Client code (for android!) import sys import os import android import socket droid = android.Android() fpath = '/sdcard/foo.jpg' droid.cameraInteractiveCapturePicture(fpath) s = socket.socket() s.connect(('192.168.233.1',9999)) droid.makeToast('Connected!') if os.path.isfile(fpath): str = "file exists...!" else: str = "nope...!" droid.makeToast(str) with open (fpath, "rb") as f: l = f.read(1024) while (l): s.send(l) l = f.read(1024) s.close() droid.makeToast('Success!')







Exported from Notepad++
#Server code import socket import sys s = socket.socket() s.bind(('192.168.233.1',9999)) s.listen(10) sc, address = s.accept() print address l = sc.recv(1024) with open ("pic2.jpg",'wb') as f: while (l): f.write(l) l = sc.recv(1024) sc.close() s.close()

Python Multiprocessing

I created a buffered sender and receiver application to demonstrate multiprocessing in Python. The sender computes some fictitious data and sends it to the receiver which writes it to a file. The data is sent when the buffer is filled up. The receiver is slow (due to a delay) and it demonstrates how multiprocessing can allow you to acquire data from a fast source without missing any data and write it to file! You can use similar ideas to write data acquired from a hardware device to the hard-disk without keeping the device tied-up when the data is being written to the file.


Exported from Notepad++
from multiprocessing import Process, Queue import time def receiver(queue): ## Read from the queue index = 0 with open("test.txt",'w') as file: while True: msg = queue.get() # Read from the queue and do nothing if (msg == 'DONE'): break elif not len(msg): continue for m in msg: file.write(str(m)+"\n") time.sleep(0.1) # Some expensive operation (plotting or other processing) print index, len(msg) index+=1 def sender(count, queue): ## Write to the queue. Sending does not stop if receiver is busy. bsize = 1000 #buffer size buff = [] for ii in xrange(0, count): # d = usbdata buff.append(d) if len(buff)==bsize: #Buffer is filled up! #time.sleep(0.1) queue.put(buff) # Write 'count' numbers into the queue buff = [] queue.put(buff) queue.put('DONE') print "Sender is done" if __name__=='__main__': count = 10**4 queue = Queue() # receiver() reads from queue # sender() writes to queue receiver_p = Process(target=receiver, args=((queue),)) receiver_p.daemon = True receiver_p.start() # Launch receiver() as a separate python process _start = time.time() sender(count, queue) # Send a lot of stuff to receiver() receiver_p.join() # Wait for the receiver to finish print " %s numbers took %s seconds" % (count, (time.time() - _start))

Scrambled Eggs

Often we need to make a list of multiple choice questions and generate a copy of the test with the options scrambled or shuffled. For this purpose I have developed a VBA Macro that can do this for you automatically! You can see the code at https://github.com/foxtrotmike/scrambled-eggs. It also contains comments on how to use it.