Saturday, April 24, 2010

Path class

I hope one of these stuffs is in the standard library.

>>> p = Path('/path/to/some/text.txt')
>>> p.parent()
>>> p.parent().parent()
>>> p.parent().parent().parent()
>>> p.parent().parent().parent().parent()
>>> p.parent().parent().parent().parent().parent()
>>> p.parent().child('other').child('text.txt')
>>> p[:-1]
>>> p[1:-1]
>>> p[2:-1]

You cannot write e.g. p[2:3] = p('aaa') since conceptually the Path class is immutable.

import os

class Path(object):
    def __init__(self, path):
        if isinstance(path, Path):
            path = path.path
        self.path = path

    def parent(self):
        return Path(os.path.dirname(self.path))

    def child(self, name):
        if isinstance(name, Path):
            name = name.path
        childPath = os.path.join(self.path, name)
        return Path(childPath)

    def base(self):
        return Path(os.path.basename(self.path))

    def abspath(self):
        return Path(os.path.abspath(self.path))

    def __str__(self):
        return self.path

    def __repr__(self):
        return self.__class__.__name__ + '(' + repr(self.path) + ')'

    def __add__(self, rhs):
        if isinstance(rhs, Path):
            return self.child(rhs.path)
            return Path(self.path + rhs)

    def __eq__(self, rhs):
        if isinstance(rhs, Path):
            rpath = rhs.path
        elif isinstance(rhs, unicode):
            rpath = rhs
            return False
        return os.path.abspath(self.path) == os.path.abspath(rpath)

    def __ne__(self, rhs):
        return not self.__eq__(rhs)

    def __getitem__(self, key):
        this, parent = self, self.parent()
        bases = [this.base()]
        while this != parent:
            bases[:0] = [parent.base()]
            this, parent = parent, parent.parent()
        bases[0] = this
        bases = bases[key]
        retPath = bases[0]
        for base in bases[1:]:
            retPath = retPath.child(base)
        return retPath

    def getCurrent():
        return Path(os.getcwd())

To extract the Python string a Path object has, you can pass the object to str() or unicode(). A friend of mine gave me an idea to make the class to be a subclass of str/unicode so that a Path object can be used as a Python string. But the meaning of __eq__, __ne__, and __getitem__ is quite different between str/unicode and my class.

Wednesday, April 14, 2010

MRV multi-platform python development environment

MRV v1.0.0-preview documentation

Looks like a high abstract level Maya API Python wrapper (and more?). Anybody tested it?

Sunday, April 4, 2010

Detexify2 - LaTeX symbol classifier

Detexify2 - LaTeX symbol classifier

What is this?

Anyone who works with LaTeX knows how time-consuming it can be to find a symbol in symbols-a4.pdf that you just can't memorize. Detexify is an attempt to simplify this search.
How does it work?

Just draw the symbol you are looking for into the square area above and look what happens!

Wrong usage.