Static HTML file browser for Dropbox

December 2, 2014 3 comments

Two of my students worked on a project that creates static HTML files for a public Dropbox folder (find it at github). I use it in production, check it out here.

If you created your Dropbox account before October 4, 2012, then you are lucky and you have a Public folder. Accounts opened after this date have no Public folder :(

So, if you have a Public folder and you want to share the content of a folder recursively, then you can try this script. It produces a file and directory list that is similar to an Apache output.

Screenshot

Authors and Contributors

Categories: python Tags: , , ,

make a script run under Python 2.x and 3.x too

November 3, 2014 Leave a comment

Problem
I installed Manjaro Linux on one of my laptops, just to try something new. I’ve been using it for a week and I like it so far :) On my older laptop it runs smoother than Ubuntu.

Anyway, Manjaro switched to Python 3.x, that’s the default, thus “python” points to Python 3. I use Ubuntu on my other machines where Python 2 is the default. I would like to modify my scripts (at least some of them) to run on both systems.

For instance, in Python 2.x you call “raw_input”, while this function was renamed to “input” in Python 3.x.

Solution
Well, since January 2014 I start all my new scripts with this line:

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

It ensures a nice transition from Python 2 to Python 3.

To solve the “raw_input” problem, you can add these lines:

import sys

if sys.version_info >= (3, 0):
    raw_input = input

You can continue using “raw_input”, but if it’s executed with Python 3.x, “raw_input” will point to the “input” function.

Of course, the ideal solution would be to switch to Python 3, but I’m not ready for that yet :)

Categories: python Tags: , , ,

python setup.py uninstall

November 2, 2014 Leave a comment

Problem
Sometimes you need to install a software with “python setup.py install“. However, if you want to get rid of it, there is no “python setup.py uninstall” or “python setup.py remove”. Stupid, but true.

Solution
I found the solution here. In short:

# install:
$ (sudo) python setup.py install

# uninstall:
$ pip freeze | grep PATTERN_BASED_ON_PACKAGE_NAME
# then remove with pip:
$ (sudo) pip uninstall PACKAGE_NAME
Categories: python Tags: , ,

make bpython use Python 3

October 14, 2014 Leave a comment

Problem
On my Linux box the default Python is version 2.7. I installed bpython system-wide, thus when I launch bpython, it uses Python 2.7.

How could I have another bpython that uses Python 3? I would like to have both though. Sometimes I need Python 2, sometimes Python 3, and I would like to use the bpython shell with them.

Solution
Simply install bpython in a Python 3 virtual environment. That is, create a Python 3 environment, and install bpython in it with pip. This bpython installation will use Python 3. Then use the absolute path of this bpython executable. And no, you don’t have to activate the virt. env. each time you want to use this bpython.

In a previous post I showed how to patch virtualenvwrapper to make it suitable for creating not only Python 2 but Python 3 environments too. Using this patched virtualenvwrapper, the process looked like this on my machine:

$ cd
$ mkdir bpython_python3
$ cd bpython_python3
$ mkvirtualenv bpython_python3  # creates a Python 3 virt. env.
(bpython_python3)$ pip install bpython
(bpython_python3)$ which bpython
/home/jabba/.virtualenvs/bpython_python3/bin/bpython  # sample; will be different in your case
(bpython_python3)$ deactivate
$

I suggest adding the following aliases to your $HOME/.bashrc:

alias bpy='bpython'
alias bpy3='/home/jabba/.virtualenvs/bpython_python3/bin/bpython'

Of course, you need to customize the path for bpy3.

Now “bpy” launches bpython with Python 2, while “bpy3” starts bpython with Python 3.

Categories: ubuntu Tags: ,

[django] virtualenvwrapper with Python 3

September 12, 2014 Leave a comment

Problem
Django 1.7 came out ten days ago. If you look at the updated poll tutorial, you will notice that it’s written for Python 3! So far I’ve used Python 2 only, so I thought I would redo this tutorial for two reasons: (1) learn about migrations, and (2) start using Python 3.

For my Django projects I use virtualenvwrapper to manage my virtual environments, so the first question was: how to create a Python 3 virt. env. with virtualenvwrapper?

Well, maybe my installation got corrupted somehow, but I ran into this problem:

$ mkvirtualenv -p `which python3` tmpenv
Running virtualenv with interpreter /usr/bin/python3
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module>
    import base64
  File "/usr/lib/python3.4/base64.py", line 9, in <module>
    import re
  File "/usr/lib/python3.4/re.py", line 324, in <module>
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module>
    from copy_reg import *
ImportError: No module named 'copy_reg'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
    if not enabled():
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
    import re
  File "/usr/lib/python3.4/re.py", line 324, in <module>
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module>
    from copy_reg import *
ImportError: No module named 'copy_reg'

Original exception was:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module>
    import base64
  File "/usr/lib/python3.4/base64.py", line 9, in <module>
    import re
  File "/usr/lib/python3.4/re.py", line 324, in <module>
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module>
    from copy_reg import *
ImportError: No module named 'copy_reg'

Solution
I posted this question to reddit (see here) and I found the following solution:

First, install pip3:

sudo apt-get install python3-pip
sudo pip3 install virtualenvwrapper

Then, add the following lines to your .bashrc:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace    # customize if needed
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

Try if it works for you. In my case, I had to edit the file “/usr/local/bin/virtualenv“. I changed its first line to

#!/usr/bin/env python3

The consequence of changing the first line was that creating Python 3 environments became the default, but at least it works now.

Create a Python 3 virt. env.:

mktmpenv

Create a Python 2 virt. env.:

mktmpenv -p `which python2`

If you use the command “virtualenv“, it will also create Python 3 environments by default. Thus, “virtualenv myproject3” will be a Python 3 env., while “virtualenv -p python2 myproject2” will be a Python 2 env.

rotate a matrix

August 22, 2014 1 comment

Rotate clockwise:

rotated = zip(*original[::-1])

Rotate counterclockwise:

rotated_ccw = zip(*original)[::-1]

I found this trick in this thread.

[django] awesome Django apps, projects and resources

August 12, 2014 Leave a comment

A curated list of awesome Django apps, projects and resources: https://github.com/rosarior/awesome-django.

Categories: django Tags:
Follow

Get every new post delivered to your Inbox.

Join 78 other followers