Git Product home page Git Product logo

nspace's Introduction

nspace

Smart namespacing for python

Note that this is currently vaporware. It's just some ideas, and more to follow. In other words, "nothing to see here, folks"... yet.

A few features we want to have:

Method hiding

Initially only module scope, but eventually in classes too. We want to choose exactly which methods we will export, and hide the rest (we can also have the opposite default, and use a @hidden decorator for functions we want to hide). For example:

foo.py:

from nspace import *

def bar(x):
    return x + 1

@export
def baz(y):
    return bar(y) * 3

In the interpreter:

>>> from nspace import *
>>> include("foo")
>>> foo.baz(3)
12
>>> foo.bar(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'bar'

We can expand this to allow method hiding for classes.

from nspace import *

@export
class Foo(object):
    @export
    def __init__(self, x):
        self.x = x

    def bar(self, y):
        return self.x + y

    @export
    def baz(self, z):
        return self.bar(z) * self.x

Test:

>>> from nspace import *
>>> include("foo")
>>> my_foo = foo.Foo(10)
>>> my_foo.baz(11)
210
>>> my_foo.bar(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'

More powerful imports

We introduce some alternatives to traditional python import: include, include_from and include_all.

This is similar to import foo:

foo = include('foo')

The following two are both similar to 'from foo import bar'

bar = include('foo.bar')
include_from('foo', 'bar')

We can give aliases like with python as. The following are equivalent:

cool = include('foo.bar')
include_from('foo', ('bar', 'cool'))

We can include multiple things

foo, bar = include('foo', 'bar')
include_from('foo', 'bar', 'baz', ('qux', 'super_cool'))

Similar to from foo import *

include_all('foo')

Sometimes we only need to use something once in a module. We don't need to pollute the namespace just for that. Why not a with statement?

# `join` and `up` are only defined inside the with statement
with include_from('os.path', 'join', ('dirname', 'up')):
    filename = join(up(__file__), 'my_file.txt')

def random_name():
    names = ['Tom', 'Dick', 'Harry']
    with include('random.choice') as choice:
         return choice(names)

We will also throw an error if attempting to include a hidden method.

Package system

nspace expands on the basic python module system, giving it the ability to combine modules into other modules and form Java-style package hierarchies. More on this to come later :-)

nspace's People

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.