Re: automatically import lots of modules at python manage.py shell time?

2010-03-28 Thread Matt Schinckel
On Mar 28, 3:35 am, Rolando Espinoza La Fuente 
wrote:
> On Fri, Mar 26, 2010 at 7:39 PM, Bjunix  wrote:
> > django-extensions[1] brings the shell_plus command which auto-imports
> > all models in installed apps. You can use this or have a look at it's
> > source.
>
> Yes. shell_plus is quite handy.
>
> $ bin/manage.py shell_plus
> [...]
> From 'api' autoload: RequestLog
> From 'lostiempos' autoload: Section, Subsection, Ad, HtmlArchive,
> SearchHistory, SearchHistoryCount
> From 'south' autoload: MigrationHistory
>
> Imports all your models by default. Also with iPython you can write
> "from" then press [Up]
> and will show you the entries starting with "from" in the history.
>

I wrote a similar app called django-bshell which does the same, but
uses bpython as the shell.

You can find this on pypi, and it adds a 'bshell' manage command.

Matt.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: automatically import lots of modules at python manage.py shell time?

2010-03-27 Thread Rolando Espinoza La Fuente
On Fri, Mar 26, 2010 at 7:39 PM, Bjunix  wrote:
> django-extensions[1] brings the shell_plus command which auto-imports
> all models in installed apps. You can use this or have a look at it's
> source.

Yes. shell_plus is quite handy.

$ bin/manage.py shell_plus
[...]
>From 'api' autoload: RequestLog
>From 'lostiempos' autoload: Section, Subsection, Ad, HtmlArchive,
SearchHistory, SearchHistoryCount
>From 'south' autoload: MigrationHistory

Imports all your models by default. Also with iPython you can write
"from" then press [Up]
and will show you the entries starting with "from" in the history.

Regards,

Rolando

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: automatically import lots of modules at python manage.py shell time?

2010-03-26 Thread Bjunix
django-extensions[1] brings the shell_plus command which auto-imports
all models in installed apps. You can use this or have a look at it's
source.

[1] http://github.com/django-extensions/django-extensions

On Mar 26, 11:52 pm, "Tom X. Tobin"  wrote:
> On Fri, Mar 26, 2010 at 5:45 PM, Phlip  wrote:
> > os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings'  #  TODO  look
> > up which one
>
> My local copy actually has the following code in it:
>
> 
> import os
> import re
> import sys
>
> m = re.search(r'^([a-zA-Z0-9_]+)-shell$', os.path.basename(sys.argv[0]))
> mn = 'avclub'
> if m:
>     g = m.group(1)
>     if g in ['avclub', 'onion']:
>         mn = g
>     del g
> del m
> os.environ['DJANGO_SETTINGS_MODULE'] = 'afns.sites.local_%s.settings'
> % (mn,)  # "afns" is the Onion's Python package
> 
>
> This way I can just make symlinks named "avclub-shell" and
> "onion-shell" to the original executable, and it will use the
> respective settings files automatically without having to maintain
> separate copies.  You could also get fancier with optparse, I suppose.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: automatically import lots of modules at python manage.py shell time?

2010-03-26 Thread Tom X. Tobin
On Fri, Mar 26, 2010 at 5:45 PM, Phlip  wrote:
> os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings'  #  TODO  look
> up which one

My local copy actually has the following code in it:


import os
import re
import sys

m = re.search(r'^([a-zA-Z0-9_]+)-shell$', os.path.basename(sys.argv[0]))
mn = 'avclub'
if m:
g = m.group(1)
if g in ['avclub', 'onion']:
mn = g
del g
del m
os.environ['DJANGO_SETTINGS_MODULE'] = 'afns.sites.local_%s.settings'
% (mn,)  # "afns" is the Onion's Python package


This way I can just make symlinks named "avclub-shell" and
"onion-shell" to the original executable, and it will use the
respective settings files automatically without having to maintain
separate copies.  You could also get fancier with optparse, I suppose.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: automatically import lots of modules at python manage.py shell time?

2010-03-26 Thread Phlip
Tom X. Tobin wrote:

> #!/usr/bin/env ipython -i -nobanner
> Save that to a file in your PATH, make it executable, and you should be set.

Awesome--thanks. And, instead of chmod +x-ing it, I added it to our
common fab files, so everyone gets it with 'fab shell'.

And I could not get ip = IPython.ipapi.get() working (not sure why -
it returned a None), so I added it with this contemptible but
bulletproof hack:

def shell():
open('.ipython', 'w').write('''
# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'dev2_settings'  #  TODO  look
up which one

from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache

local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os
''')
os.system('ipython -i -nobanner .ipython')

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: automatically import lots of modules at python manage.py shell time?

2010-03-26 Thread Tom X. Tobin
On Fri, Mar 26, 2010 at 4:47 PM, Phlip  wrote:
> I am in anguish over watching my colleagues fire up python manage.py
> shell...
>
> ...and then typing several incredibly long import lines before
> actually getting anything done.
>
> These same colleagues also seem to have itchy ^C fingers, meaning they
> bail out of the shell too often, too.
>
> How can we fix the first problem? How can this (otherwise useful)
> shell use a minimal or invisible import rubric, to grab, say, a bunch
> of models so they are all ready & available for use?

I use something like this (assuming you have iPython installed):

**
#!/usr/bin/env ipython -i -nobanner

# don't need this part if DJANGO_SETTINGS_MODULE is already set in
your environment
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOUR.SETTINGS.MODULE'

from django.db.models import Count, Max, Min, Q
from django.db.models.loading import cache as appcache

local_dict = locals()
for model_class in appcache.get_models():
local_dict[model_class.__name__] = model_class
del local_dict, appcache, os
**

Save that to a file in your PATH, make it executable, and you should be set.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



automatically import lots of modules at python manage.py shell time?

2010-03-26 Thread Phlip
Djangoists:

I am in anguish over watching my colleagues fire up python manage.py
shell...

...and then typing several incredibly long import lines before
actually getting anything done.

These same colleagues also seem to have itchy ^C fingers, meaning they
bail out of the shell too often, too.

How can we fix the first problem? How can this (otherwise useful)
shell use a minimal or invisible import rubric, to grab, say, a bunch
of models so they are all ready & available for use?

--
  Phlip
  http://penbird.deviantart.com/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.