EggLib

Previous topic

External application tools

Next topic

C++ Library

This Page

Miscellaneous utilities

These are tools that, in principle, do not deserve distribution but have been implemented for use in other parts of EggLib.

class egglib.misc.TempFile

Each instance of this class creates a temporary file that is guaranteed to be deleted at destruction time or when clean() is called. The file is closed immediately after creation. The is no error if the file is deleted before destruction of the corresponding instance.

clean()

Remove the underlying temporary file if it exists.

fname

Name of the temporary file.

class egglib.misc.Pool(generator, arguments, recycle, recycle_args=None)

Minimal object pool implementation. This class is aimed to manage object recycling. Objects are yielded by get() and returned after use by put(). len(pool) returns the number of objects currently store in the pool (although you normally don’t need to worry about it). Similarly, one can clear the stored objects with clear().

Parameters:
  • generator – method allowing to create new objects.
  • arguments – arguments to be passed to the generator method. Use None or an empty sequence to pass no arguments.
  • recycle – method to recycle an object returning to the pool. This method should be take this type of objects as unique argument. If None, no recycling method is applied.
  • recycle_args – arguments to be passed to the recycling method. Use None or an empty sequence to pass no arguments (which is the default).

Here is a demonstration with Align objects:

import egglib

P = egglib.misc.Pool(egglib.Align, (), egglib.Align.reset)
print 'pool size:', len(P)
aln1 = P.get()
aln2 = P.get()
print 'pool size:', len(P)
print 'fresh objects:', aln1.ns, aln1.ls, aln2.ns, aln2.ls
aln1.add_sample('', 'AAAAAAAAAA')
aln1.add_sample('', 'AAAAAAAAAA')
aln1.add_sample('', 'AAAAAAAAAA')
aln2.add_sample('', 'AAAAAAAAAAAAAAAAAAAA')
aln2.add_sample('', 'AAAAAAAAAAAAAAAAAAAA')
print 'modified objects:', aln1.ns, aln1.ls, aln2.ns, aln2.ls
P.put(aln1)
P.put(aln2)
print 'objects back to pool:', aln1.ns, aln1.ls, aln2.ns, aln2.ls
print 'one is not supposed to keep a reference of recycled objects!'
print 'pool size:', len(P)
aln3 = P.get()
print 'recycled object:', aln3.ns, aln3.ls

This produces the following output:

pool size: 0
pool size: 0
fresh objects: 0 0 0 0
modified objects: 3 10 2 20
objects back to pool: 0 0 0 0
one is not supposed to keep a reference of recycled objects!
pool size: 2
recycled object: 0 0
get()

Get an object from the pool.

put(obj)

Return an object to the pool.