Entries with tag "code"

Configure ssh on Netgear WGR614 v7 router

Need to access Windowz XP computer via ssh through a Netgear WGR614 v7 wireless router...

  1. Install ssh (openSSH) on server computer.
  2. Configure the ssh daemon so that it's running properly.
  3. Add ssh port 22 as an exception to the Windowz Firewall.
  4. Go to 192.168.1.1 (or www.routerlogin.net) with browser to configure router.
  5. In 'Port Forwarding' under 'Advanced', hit the 'Add Custom Service'.
  6. Add ssh, port 22, and set the 'Server IP Address' to the (internal) IP of the Windowz machine assigned by the router. To check what this is, run ipconfig from the command line (should look something like 192.168.1.xxx).
  7. Check that the port is indeed accessible from the outside with http://canyouseeme.org. If that works, you are set.
  8. ssh using the public IP of the Windowz machine (given by http://canyouseeme.org or http://whatismyip.com) from a remote client.

Posted on:
2010.06.10 -0500

Tags:
code

Python __slots__

From the Python documentation:

By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances. The default can be overridden by defining __slots__ in a new-style class definition. The __slots__ declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__ is not created for each instance.
To test this I created two dummy classes, one with the __slots__ tuple attribute and one without it. Then I compared the instantiation time for the two classes with timeit and did some memory profiling with heapy.
### Code Start ###
import timeit
from guppy import hpy

class Foo(object):
   def __init__(self):
      self.x = 1
      self.y = 2

class FooSlots(object):
   __slots__ = ('x', 'y')
   def __init__(self):
      self.x = 1
      self.y = 2

# test speed...
passes = 1000
print 'Number of instantiations:', passes

t = timeit.Timer('Foo( )', 'from __main__ import Foo')
print 'Foo class instantiation times:', t.timeit(passes)

t = timeit.Timer('FooSlots( )', 'from __main__ import FooSlots')
print 'FooSlots class instantiation times:', t.timeit(passes)

## test memory...
hp = hpy( )
hp.setrelheap( )

class_count = 1000

l = [FooSlots( ) for i in range(class_count)]
h = hp.heap( )
print h

hp.setrelheap( )
l = [Foo( ) for i in range(class_count)]
h = hp.heap( )
print h
### Code End ###
The result of running the tests show that there is not a big difference in instantiation times, but there is a substantial memory saving:
Number of instantiations: 1000
Foo class instantiation times: 0.00138711929321
FooSlots class instantiation times: 0.00114798545837
Partition of a set of 1003 objects. Total size = 32588 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0   1000 100    28000  86     28000  86 __main__.FooSlots
     1      1   0     4128  13     32128  99 list
     2      1   0      448   1     32576 100 types.FrameType
     3      1   0       12   0     32588 100 int
Partition of a set of 2003 objects. Total size = 168540 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0   1000  50   136000  81    136000  81 dict of __main__.Foo
     1   1000  50    28000  17    164000  97 __main__.Foo
     2      1   0     4128   2    168128 100 list
     3      1   0      400   0    168528 100 types.FrameType
     4      1   0       12   0    168540 100 int

Posted on:
2010.01.14 -0600

Tags:
code

Real-time Linux for time critical tasks

My Tepozchiquilichtli pieces so far have required a time resolution of 1 millisecond (1 millisecond is the tatum of the pieces). Controlling the timing of the pieces using the Python time.sleep( ) function from within my general purpose computer (Intel Core Duo CPU T2250 @ 1.73GHz, cpu MHz: 800.000, cache: 2048 KB) running plain vanilla Linux proved useless. The function is unable to sleep for less than 4 milliseconds and the inaccuracy is too high.

One way around this problem is to have a dedicated micro-controller do the timing and have it send all the data to the printers. This works great and has the advantages of compactness (there are other issues with this solution, but accurate timing is not one of them). I still wanted to find a way to get the accuracy I need from a general purpose computer because this is more convenient for testing and prototyping.

Enter Real-time Linux and powernap. powernap is a Python module that uses the real-time clock (dev/rtc) to do the timing. With powernap and RT Linux I can get the accuracy of 1 millisecond I need with variances of about 0.002 milliseconds.

The powernap website point to this real-time module, but since there's no documentation for it, I decided to go with the Linux kernel and the CONFIG_PREEMPT_RT Patch instead. Here are instructions to compile the Linux kernel in Debian, and here are general instructions for patching the source for real-time capabilities.

Posted on:
2009.09.04 -0500

Tags:
code , gnu/linux , music , Tepozchiquilichtli

LilyPond in Leopard

First time I try to install LilyPond in Leopard. Not as smooth as it is in Linux, Windows, or older versions of OS X, it appears. Just putting the LilyPond.app file in the /Applications folder does nothing to allow us to run LilyPond from the command line. Putting a simple simlink /usr/bin/lilypond pointing to /Applications/LilyPond.app/Contents/Resources/bin/lilypond does not help either because the lilypond executable can not find other files it needs (located in /Applications/LilyPond.app) in order to run. So, looking around I found Hans Fugal's post about how to fix this. I modified his script just a bit, called it lilypond and put it in /usr/bin/. Now it works!

#! /bin/sh
APP=/Applications/LilyPond.app
PATH=$APP/Contents/Resources/bin:$PATH
exec lilypond "$@"

Posted on:
2009.08.05 -0500

Tags:
code

SVN branching overview

In Subversion v.1.5.
Create branch.

$ svn copy https://server.com/abjad/trunk \
   https://server.com/abjad/branches/mybranch \
   -m "Creating mybranch."
Check out the branch.
$ svn co https://server.com/abjad/branches/mybranch
Keep in sync with trunk and make sure branch is clean.
$ pwd
/path/to/mybranch
$ svn status
(nothing to commit)
$ svn merge http://server.com/abjad/trunk
[in Subversion 1.6 you can also do: $ svn merge ^/trunk]
$ svn status
(modifications shown due to merging)
$ svn commit -m "Merged latest trunk changes to mybranch."
Work on branch: make changes and commits. When branch is done, do a final commit...
   
(more modifications to branch...)
$ svn commit -m "Final merge of trunk changes to mybranch."
...and merge branch to trunk. Go to trunk.
$ pwd
/path/to/trunk
Make sure we are clean.
$ svn update
(At revision XXX)
Merge.
$ svn merge --reintegrate http://server.com/abjad/branches/mybranch
[In Subversion 1.6: $ svn merge --reintegrate ^/branches/mybranch]
Test and verify that the merge works (run tests). Do final commit.
$ svn commit -m "Merged mybranch into the trunk"
Remove branch.
$ svn delete http://server.com/abjad/branches/mybranch -m "Removed mybranch."

Posted on:
2009.08.03 -0500

Tags:
code

PCB design workflow

Printed Circuit Board design workflow. Here working with KiCAD.

  1. Create schematic.
    1. Add components.
    2. Add connections.
    3. Label connections.
  2. Annotate all component automatically with "Schematic Annotation".
  3. Check that all connections are correct and vaild with "Test ERC".
    1. Fix errors, if any.
    2. Repeate ERC until no errors are found.
  4. Generate net list with "Netlist generation".
  5. Link footprints to components with "Cvpcb".
  6. Save Netlist again.
  7. Save project with "files" -> "Save Project".
  8. Create "Bill of materials".
  9. Start PCB design by running "Pcbnew".
  10. Set "Dimensions" -> "Tracks and Vias" to match capabilities of PCB board-making machine.
  11. "Read Netlist" in.
  12. Distribute components and show ratsnest.
  13. Add tracks and vias (manually or automatically).
  14. Add zones.
  15. Check design by running the "Pcb Design Rules Check".
    1. "Test DRC".
    2. "List Unconn".
  16. Save.
  17. Export GERBER file!

Posted on:
2009.07.28 -0500

Tags:
code

Multiple svn repositories

You have multiple projects and you want each to be managed in its own svn repository. At the same time, you want them all served by the same server machine myserver.org: In the server myserver.org that will house the repositories, create the new repositories under the same directory:

$ pwd
/home/user
$ mkdir svn
$ svnadmin create svn/repo1
$ svnadmin create svn/repo2
Assuming we have two already existing projects project1 and project2 in our projects directory, we now import each of the projects into each of the repositories:
$ svn import /home/user/projects/project1 file:///home/user/svn/repo1/project1/trunk -m "import project1"
$ svn import /home/user/projects/project2 file:///home/user/svn/repo2/project2/trunk -m "import project2"
Now that our two projects are under subversion control, we can delete the original project files:
$ rm -rf projects/project1
$ rm -rf projects/project2
Finally, in order to be able to access these repositories remotely, all we have to do is run the svn server svnserve on the directory housing our new repositories:
$ svnserve -d -r /home/user/svn/
This will run svnserve in deamon mode while the machine is on. Don't forget setting permissions for each of the repositories. The two files to modify are svnserve.conf and passwd under the conf directory or each repository:
~/svn/project1/conf/svnserve.conf
~/svn/project1/conf/passwd
in the first file you set the permissions anon-access = none, auth-access = write, and point to the password file: password-db = passwd. In the second you set the allowed users and their corresponding passwords. If everything is set properly you should be able to checkout our projects from any other machine:
$ svn checkout svn://myserver.org/project1

Posted on:
2009.05.10 -0500

Tags:
code