Ramblings

March 18, 2008

Formencode example

Filed under: dev, orm, python, web2py — michaelangela @ 9:20 am

A full example, albeit with SQLObject, but the concept is pretty clear. With web2py, the form.validate would be followed with an explicit data insert. From the looks of the **form_result insert, form.validate returns a list of fields that can be directly inserted. The same format should be able to be used with Storm.

Handling Form data with Formencode + SQLObject

Two of my favorite and most often-used Python packges are formencode and sqlobject.
Using these packages together is done fairly often, but I’ve rarely
seen any documentation describing a full form display, validation, and
insert into a database of new data from said form.

In this example, I’ve tried to wean down the code as much as possible
to get a very concise snippet (only 12 lines of web page Controller
code) that will:

  1. Display a new form to the user
  2. Handle form submission
  3. Validate the form
  4. Coerce form values into the proper types for the database
  5. Insert the form data to the database, or
  6. Display the form with error messages, and maintain their values

The
nice thing about using formencode, is that 3 of the 6 steps I listed
above are handled by it in a fairly automatic way. Formencode will
check the data given a form schema, coerce the values into the Python
types you ask for, fill in errors should they occur, and maintain the
existing values.

Python Decorators

Filed under: cool, dev, python — michaelangela @ 8:19 am

I think I’ve written about David Mertz before. I have been learning as I read through his writings but it takes a while. There’s quite a bit that’s written for those with a much deeper understanding of python. This article is an example. I may have even commented before on my blog but I think I ‘got’ it a bit better this time. It helps to run some tests in iPython’s debugger and step through things to see how it happens at run time as well.

Charming Python: Decorators make magic easy

Listing 9. A decorator to add and remove methods

        
def flaz(self): return 'flaz' # Silly utility method
def flam(self): return 'flam' # Another silly method

def change_methods(new):
"Warning: Only decorate the __new__() method with this decorator"
if new.__name__ != '__new__':
return new # Return an unchanged method
def __new__(cls, *args, **kws):
cls.flaz = flaz
cls.flam = flam
if hasattr(cls, 'say'): del cls.say
return super(cls.__class__, cls).__new__(cls, *args, **kws)
return __new__

class Foo(object):
@change_methods
def __new__(): pass
def say(self): print "Hi me:", self

foo = Foo()
print foo.flaz() # prints: flaz
foo.say() # AttributeError: 'Foo' object has no attribute 'say'

Actually I think I did write about the decorator module before… but now it makes more sense. 🙂 One good example is the “memoize” pattern. The concept is what I would refer to as “caching”:

This decorator implements the memoize pattern, i.e. it caches
the result of a function in a dictionary, so that the next time
the function is called with the same input parameters the result is retrieved
from the cache and not recomputed.

#<_main.py>

from decorator import *

def getattr_(obj, name, default_thunk):
"Similar to .setdefault in dictionaries."
try:
return getattr(obj, name)
except AttributeError:
default = default_thunk()
setattr(obj, name, default)
return default

@decorator
def memoize(func, *args):
dic = getattr_(func, "memoize_dic", dict)
# memoize_dic is created at the first call
if args in dic:
return dic[args]
else:
result = func(*args)
dic[args] = result
return result

#</_main.py>

Here is a test of usage:

>>> @memoize
... def heavy_computation():
... time.sleep(2)
... return "done"
>>> print heavy_computation() # the first time it will take 2 seconds
done
>>> print heavy_computation() # the second time it will be instantaneous
done

Fast, quick, and free for individual use: bitvise Tunnelier

Filed under: cool, dev, tool — michaelangela @ 1:24 am

I had seen this before but it wasn’t free for individual use. Now it is. I tried it out and was very impressed. The speed is what really impressed me though. I have tried samba over ssh, and also remote desktop control over ssh. Both are very slow. Painfully slow.

Tunnelier includes a one click remote desktop which works as advertised. And it’s fast. Faster than logmein.com’s stuff actually and since it is the Remote Desktop Client from Microsoft, it’s native. You’ll still have to use logmein to get to a Mac box but this is good.

File transfers are remarkably fast as well. Apparently if we got their WinSSHD, it could be even faster. But this is already a huge boost for file transfers. Very, very cool.

A side effect of having fast file transfers is a fast SVN. I only recently figured out port forwarding for SVN over SSH which is really cool. I wasn’t looking forward to doing updates but it’s doable for small files. However larger ones were really not fun. Again that is the whole speed issue. I’ll have to see though with larger files in a bit.

(update)

It’s very fast for uploading lots of little files via SVN. Large files are still slow but not as slow as before. 🙂 I just uploaded my local web2py to 1.26 for testing, did an in-place upgrade, and committing it to my local dev trunk. All things considered, this is a nice tool to use for a single developer working with svn over ssh. See their license for details.

Oh yes, browsing a trac instance is also very snappy. The jumpbox for trac/svn is my perferred server as of now.

Tunnelier (Bitvise)

Tunnelier is our SSH and SFTP client for Windows which incorporates:

Create a free website or blog at WordPress.com.