Thursday, September 2, 2010

So what am I doing now (again)

Now I work for Polygon Pictures as a freelancer and working for some big project. It is O.K, I wish if my work could be a little bit more challenging since it's one of the easiest works I've ever done, but working with artists is always fun.

Apart from my work, I'm trying to make something interesting (interesting for CG guys), but I'm not going to tell you what it is since nothing is worth mentioning until you show an image is an unwritten rule all over the CG industry.

Wednesday, August 11, 2010

Any good module for saving settings file?

I'm thinking about the file format for various settings.
Using XML is like going to a super market by jet plane. YAML is good but not in the standard module.
So far the best choice is using JSON but json module in the standard module is not suitable for my needs since it has no __getstate__/__setstate__ equivalent.

I wonder if there's a nice module that has both pickle and json features. It would

- be able to serialize arbitrary object
- have __getstate__, __setstate__ equivalent
- be able to convert to/from standard Python objects (NOT Tag/Node/Element object ! )
- create json formatted file
- preferably customize the behavior on error
- be a light weight module (not dependent on thousands of other modules)

There's no need to maintain object references, dump() can raise an exception just like json.
I can make one by myself but I don't want to reinvent the wheel. Anybody knows if it exists already?

Saturday, July 17, 2010

Mighty Optical Illusions

If you haven't seen it, take a look at this site! http://www.moillusions.com/.
Some of them I'm fascinated.

Stereo Dino Optical Illusion (must visit)

This Isn't a Painting (must visit)

Shadow and Reflection Sculptures

Illusion Billboards Again

Sunday, July 4, 2010

Makin your Python code fast

Check out a presentatiton by Andrew Bennetts at Pycon Australia 2010.
http://pyconau.blip.tv/
He introduced various ways of Python code profiling.














Material from PyCon AU are licensed under the Creative Commons CC-BY-NC-SA license.

And you may also be interested in "Python Goes to the Movies" by Mark J Streatfield at Dr. D Studios, which is kind of introductory stuffs though because the conference is for Python and not for vfx.

Friday, May 28, 2010

Ohhh iPad !

I didn't know we can do such thinkgs with iPad!

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.