Friday, May 21, 2010

Recipe 577237: Prevent star imports (Python)

Recipe 577237
Use this code in your module to prevent people using the "from foo import *" syntax with your module.

@apply
class __all__(object):
    def __getitem__(self, _):
        raise ImportError("Star imports not supported")

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()
Path('/path/to/some')
>>> p.parent().parent()
Path('/path/to')
>>> p.parent().parent().parent()
Path('/path')
>>> p.parent().parent().parent().parent()
Path('/')
>>> p.parent().parent().parent().parent().parent()
Path('/')
>>> p.parent().child('other').child('text.txt')
Path('/path/to/some/other/text.txt')
>>> p[:-1]
Path('/path/to/some')
>>> p[1:-1]
Path('path/to/some')
>>> p[2:-1]
Path('to/some')

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)
        else:
            return Path(self.path + rhs)

    def __eq__(self, rhs):
        if isinstance(rhs, Path):
            rpath = rhs.path
        elif isinstance(rhs, unicode):
            rpath = rhs
        else:
            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

    @staticmethod
    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.

Saturday, March 27, 2010

google translate

Japanese:*****
google translate
-> English:I do not have to surgery. No way, thank you.
-> Japanese:*****
-> English:I do not need the surgery. So no thank you.
-> Japanese:*****
-> English:I do not need the surgery. Well, thank you.
-> Japanese:*****
-> English:I do not need the surgery. Well, thank you.

It's interesting the final translation of the second sentence is closer to the right meaning, though the first translation is completely opposite. Google translate needs convergence ;)
The meaning of the first sentence remained opposite. It means "You need to have an operation".

Sunday, March 21, 2010

So what am I doing now?

I haven't updated "on topic" issues for quite a long time. Now I'm working as a freelancer at a company which is making a GI renderer and mainly working with 3ds Max (especially with material part of its SDK, that explains why I am NOT a big fan of Max). At home I'm reading siggraph papers, papers, papers. Siggraph papers are like treasure boxes but they are often hard to open, the key of a box is often inside another, and it needs yet another to open ...

Saturday, March 20, 2010

particle breakout

Particle breakout
Nice idea and beautiful.