Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 11 September 2013

Disappearing log plots and lines

I was doing some plotting using iPython --pylab which uses the handy matplotlib to draw things. The stuff I was plotting was a bit all over the place so I thought I'd switch to log mode using pyplot.yscale() (though you can also use axes().set_yscale()):
yscale('log')
Suddenly a few of my lines just went missing - What was the problem here ?

Basically it comes down to the fact that some lines/points were at zero which is problem for a logarithmic representation as the log of zero is -infinity (-∞). Of course iPython has a work around which is to use their symlog axes scale, which combines log and linear scales using a threshold linthreshy at which they change:
yscale('symlog',linthreshy=1)
You can also have a problem with negative values and log plots as the log of a negative number cannot be represented in normal (real) numbers (use need complex numbers for that). But you can get around that in iPython using the symlog as well - as explained here.
Note: All these commands work for the x axes as well - just change the 'y' for an 'x'.

Friday, 11 February 2011

Python obfuscation

Well there's plenty of talk out there about Python obfuscation. But basically its frowned upon by most, and not that easy (though that's really the case for most languages). But if you'd like to make it a bit harder for people to rip-off your code, then these seem to be the ways to do it:
  • Compile Python code into .pyc - using Python's in-built compileall module - in your code dir run (then delete all your .py files and you can run the .pyc files):
    python -mcompileall .
  • Use [cx]Freeze (or py2exe) to compile your Python project into an executable
  • Use a Python source code obfuscater like this one or that (both a bit old)
  • Use cython instead of Python
  • There are some commercial products out there too (e.g. this)

Thursday, 21 January 2010

Messing with WxPython

I've been playing with Wx Python on OSX and it's not bad for knocking things up. Trying to find out how to do certain things has taken a while - when you just can't find the right search term! Here's a couple of things that I'd been trying to do:
  • Mini sliders/widgets - On OSX Cocoa/Carbon there's some neat small sliders

    • You can enable them on OSX (only) by using this method (there is also a MINI size):
      SetWindowVariant(wx.WINDOW_VARIANT_SMALL).
  • Multiple display/screen or multi-monitor info - like how many displays and what resolution/size are they?

    • Find out info using the wxDisplay() class - e.g. wx.Display.GetCount() tells you how many displays are connected - as seen here.
  • WxPython on Snow Leopard - you need to enable 32-bit python - do it in either of these two ways:

    • defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
    • export VERSIONER_PYTHON_PREFER_32_BIT=yes

Tuesday, 12 January 2010

Using Python for SD84 Servo/Robot controller

I've been playing with an SD84 board which uses 4 x PIC18F2520 chips to provide control for up to 84 servos (and/or analogue/digital input/output). I wanted to use Python on OSX but there are others out there who have used C (see the sd84lib). I've updated this article enough that I thought I repost it with a newer date.

The SD84 also uses a chip from FTDI which provides for the USB-serial interface. FTDI are a company based in Scotland that make a range of chips  e.g. FT232R used in Robot Electronic's SD84, and the Arduino.

Firstly you need to get the FTDI VCP drivers for OSX 10.5 (there are also drivers for Windows and Linux) - I found that the FTDIUSBSerialDriver_v2_2_9.dmg worked better than the latest 2.2.10 version (though there are now newer ones which may well work ok). Initially I installed the 2.2.10 version but firstly the kernel module didn't load when I plugged in the SD84, nor would it load manually (complaining of "can't add kernel extension /System/Library/Extensions/FTDIUSBSerialDriver.kext (not a bundle)") - I tried some suggestions here to no avail. I fixed it by uninstalling that version and cleaning up according to here - also removing my old pl2303 drivers, as mentioned here:
sudo rm -rf /System/Library/Extensions/FTDIUSBSerialDriver.kext \
/System/Library/Extensions.kextcache \
/System/Library/Caches/com.apple.kernelcaches/* \
/Library/Receipts/FTDIUSBSerialDriver.* /Library/Receipts/osx-pl2303-10.4.* \
/System/Library/Extensions/osx-pl2303.kext
I then did a clean install of the 2.2.9 package and rebooted then the drivers just loaded when I plugged in the SD84 - you should see a message about FTDI drivers loading in dmesg, and you should also see the driver instantiate the appropriate device nodes - e.g. /dev/tty.usbserial-XXXX.

There are also FTDI's D2XX drivers which are apparently supported by the Python pyusb module (not sure diff with this one) but I haven't tried them yet - though they potentially provide for better performance. It seems that these can also be used for JTAG based debugging.

I then used Python pyserial module (installed on OSX from ports using sudo port install py-serial, or you can download pyserial package and then run: sudo python setup.py install in the unpacked directory) to control an SD84 - the basics are (which will work on Linux as well, and windows if you replace'/dev/tty.usbserial-A2001mJE' with 'COM3') :
import serial
sync='\xAA\xA0\x55'
SET_SERVO='\x01'
SET_MODE='\x04'
GET_VERSION='\x0A'
# Open Serial port to FTDI usb serial tty
ser = serial.Serial('/dev/tty.usbserial-A2001mJE', baudrate=115200, byte
size=8, parity='N', stopbits=2,timeout=1)
# Send GET_VERSION command
ser.write(sync+GET_VERSION+'\x02\x00')
# Read the returned version and print
ver=ser.read(2)
print "VERSION:"+ver
# set SERVO mode
ser.write(sync+SET_MODE+'\x11\x01\x19')
#Read return code (should be \x00 if all is well)
ser.read(1)

There's also other places with useful info here on OSX and serial setup.
[written on 15/9/9 updated 23oct09, 12jan09]