Skip to content

Instantly share code, notes, and snippets.

@raphaelsoul
Created September 7, 2016 03:41
Show Gist options
  • Select an option

  • Save raphaelsoul/1a4b8ea6fa58bb58562546b46f8fc48e to your computer and use it in GitHub Desktop.

Select an option

Save raphaelsoul/1a4b8ea6fa58bb58562546b46f8fc48e to your computer and use it in GitHub Desktop.
def load_apps(apps):
import importlib
GlobalUrls = []
for app in apps:
module = importlib.import_module('app.{AppName}.urls'.format(AppName=app))
GlobalUrls += getattr(module,'urls')
return GlobalUrls
@raphaelsoul

Copy link
Copy Markdown
Author

so we can access app.testapp.urls and get the list named urls in this module dynamically.

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
>>> from main import load_apps
>>> load_apps(['testapp'])
[('/foobar', <class 'app.testapp.handlers.RootHandler'>), ('/', <class 'app.testapp.handlers.RootHandler'>), ('/create', <class 'app.testapp.handlers.CreateHandler'>)]

and if two element in a list for load_apps function were give, it will concat the lists and return it.

This implements the django-like app url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment