Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 30)

A simple memcache memoizer for python>=2.2

Just a snip­pet of code be­cause ev­ery once in a while I need some­thing like the clas­sic mem­o­ize dec­o­ra­tor but am work­ing on a Cen­tOS 4 bix (with python 2.3!)

I am still test­ing it, and am not even sure it re­al­ly work­s, but it should be close.

cache=memcache.Client(['127.0.0.1:11211'], debug=0)
cachetimeout=30

def memoize(fun):
    def inner(*args, **kwargs):
        key=repr(fun)+repr(args)+repr(kwargs)
        cached = cache.get(key)
        if cached is None:
            val = fun(*args, **kwargs)
            print "Setting: ",key, "to: ",val
            cache.set(key,val,cachetimeout)
            return val
        return cached
    return inner

And lat­er in­side a class:

def myfun(self,arg):
   :
   :
myfun=memoize(myfun)

And that's it. The ba­sic idea I stole from a blog who was in­spired by a Paul Gra­ham book. It can be triv­ial­ly turned in­to a dec­o­ra­tor, of course (but then on­ly works on 2.4 and lat­er).

Weird Django/PyODB bug

I am fin­ish­ing my first large-ish Djan­go app from scratch [1].

To lo­gin a us­er you can do some­thing like this:

[root@wally app]# python manage.py shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from pyodb import *
>>> import django.contrib.auth
>>> print django.contrib.auth.authenticate(username='user',password='pass')
user

And then you use that us­er ob­ject for djan­go.­con­trib.auth.lo­gin

Some of the au­then­ti­ca­tion da­ta is avail­able on a Mi­cro­soft SQL Server, and I get it via py­o­d­b. And this hap­pens when I try to au­then­ti­cate the us­er (as­sume us­er and pass are valid, this is the small­est snip­pet that trig­gers the bug):

[root@wally app]# python manage.py shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from pyodb import Connect
>>> import django.contrib.auth
>>> c1=Connect("sqlserver",uid="user",pwd="pass")
>>> print django.contrib.auth.authenticate(username='user',password='pass')
None

As you can see, the py­o­db stuff should not in­ter­fere with the djan­go stuff at al­l. I am on­ly im­port­ing one func­tion and con­nect­ing to the DB, I don't even ex­e­cute any SQL or use any da­ta from the SQL con­nec­tion. But the calls to au­then­ti­cate fail.

How are you sup­posed to de­bug this? I worked around it by mov­ing the Py­O­DB stuff to an­oth­er mod­ule, but it's weird.

1

It's a list serv­er. Dis­cus­sion group­s, mod­er­a­tion fea­tures, sub­scrip­tion­s, the work­s. Like google group­s, but yours and not ajaxy. It's not Open Source soft­ware, but if you want some­thing sim­i­lar, I am sure my cus­tomer will li­cense it to you :-D

Text-based presentations

There have been a few posts on plan­et KDE about tex­t-based pre­sen­ta­tion tools (1 , 2 , 3) and while their so­lu­tions are all prob­a­bly bet­ter than mine, I have to say I did some­thing in the area about a year ago.

Since I like re­struc­tured text and PyQt that's what it's based on, but you can just use it with vi and a wyse60, if you wan­t.

My project is called To­bo­gan and it has a work­ing ver­sion.

It has a GUI, but let's ig­nore it for this post about tex­t-based soft­ware ;-) and show the text be­hind it:

===============
Why use Tobogan
===============

-------------------
(If you are a nerd)
-------------------

:transitions: from_left,to_left,from_top,to_top,from_right,to_right,to_bottom,fade_out



It's nerd-oriented
------------------

It's trivial to display source code, with
proper syntax highlighting.

.. code-block:: python

    from base64 import *
    def myFirstFunction():
          print b64decode ('YnllIHdvcmxkIQ==')

Even for things like shell sessions.
Dammit, I am a nerd, I will try to add every nerd
feature I deem cool.



It does things simply
---------------------

This is a subtitle?
===================

Wanna show a flickr photo?

.. code-block:: rst

    .. flickr:: myPhotoTitle

How about using openomy.com to share your files?
And whatever else you can think of, it probably
**can**\  be done.

.. footer:: Smart thing goes here

And here is the re­sult af­ter run­ning rst2s­l.py on it.

Hints:

  • If you move the cur­­sor to the top, you will get (bro­ken?) pre­vi­ous/next ar­rows

  • If you click on the win­­dow, you get to the next slide.

I have not touched this in al­most a year, and it would need lots of love, but it's not a bad con­cep­t, IMVHO.

Open with: Google Docs

The eee is smal­l. It has very lit­tle stor­age. So, why should I use dozens of MB on a word pro­ces­sor? Be­cause I get word doc­u­ments in the mail ev­ery once in a while.

In fac­t, the on­ly word doc­u­ments I get are er­ror mes­sages from win­dows user­s. Here's the pro­ce­dure ev­ery one of them seems to have found to tell me what's in­side ex­plor­er's er­ror pages:

  1. Cap­­ture the screen

  2. Paste it in­­­to Word

  3. Mail it to me

I sup­pose copy­&­paste of the page con­tents was too hard. But any­way, I usu­al­ly man­age by hav­ing kword or abi­word handy, but I was think­ing...

I am reading my email. That means I am on the net. That means google docs is right there. And they have a python API! 15 minutes later... open_with_gdocs.py

#!/usr/bin/env python
import gdata.docs.service,gdata,sys,os

# Create a client class which will make HTTP requests with Google Docs server.
client = gdata.docs.service.DocsService()
# Authenticate using your Google Docs email address and password.
client.ClientLogin('joe@gmail.com', 'whateveritis')

ms = gdata.MediaSource(file_path = sys.argv[1], content_type = "application/msword")
entry = client.UploadDocument(ms,"tmp_open_with_gdocs/%s"%sys.argv[1])
os.system("firefox '%s'"%entry.GetAlternateLink().href)

Try it, as long as you have fire­fox, a de­cent ver­sion of python and gda­ta it should open the doc you pass as first ar­gu­ment on google docs in fire­fox.

Re­mem­ber that you need to delete it lat­er if you don't want it, and re­name it if you want to keep it with a de­cent name.

I am not turn­ing it in­to a re­al ap­p, but it's good enough for me. Put it in your path and as­so­ciate it to your .doc files.

A sim­i­lar thing for .xls is triv­ial. A script that would han­dle both, al­so.

Could please some­one take this and make it a re­al ap­p?

rst2pdf: New and improved

My rst2pdf script has had sev­er­al things hap­pen to it.

  1. It got an­oth­er guy work­ing on it: Christoph Zw­er­schke

  2. It's on google­­code now: http://rst2pdf.­­google­­code.org

  3. Christoph made a num­ber of im­prove­­ments:

    • bul­let­ed and enu­mer­at­ed list sim­­­pli­­­fied, use same font as text for bul­lets and num­bers

    • links in ta­ble of con­­­tents work

    • com­­­press lit­er­al sec­­­tions hor­i­­­zon­­­tal­­­ly so that they al­ways fit on the page

  4. I have in­­te­­grat­ed hy­phen­a­tion us­ing wor­­daxe (works on­­ly with re­­port­lab 2.1)

The out­put us­ing hy­phen­ation is re­al­ly ug­ly right now (for ex­am­ple, I get a black square in­stead of an hy­phen) but it's a small step for­ward.


Contents © 2000-2020 Roberto Alsina