15th April, 2014

Generating Reasonable Passwords with Python

Filed under: python — andy47 @ 4:33 pm

Thanks to a certain recent Open SSL bug there’s been a lot of attention paid to passwords in the media. I’ve been using KeePassX to manage my passwords for the last few years so it’s easy for me to find accounts that I should update. It’s also a good opportunity to use stronger passwords than ‘banana’.

My problem is that I have always resisted the generation function in KeePassX because the resulting strings are very hard to remember and transcribe. This isn’t an issue if you always use one machine but I tend to chop and change and don’t always have my password database on the machine I’m using. I usually have a copy on my phone but successfully typing ‘Gh46^f27EEGR1p{‘ is a hit and miss affair for me. So I prefer passwords that are long but easy to remember, not unlike the advice from XKCD.

Which leaves a problem. Given that I now have to change quite a lot of passwords how can I create suitably random passwords that aren’t too difficult to remember or transcribe? Quite coincidentally I read an article titled “Using Vim as a password manager”. The advice within it is quite sound and at the bottom there is a Python function to generate a password from word lists (in this case the system dictionary). This does a nice job with the caveat that it I understand from a cryptographic standpoint the passwords it creates are not that strong. But useful enough for sites which aren’t my bank or primary email. For those I’m using stupidly long values generated from KeePassX. When I tried the Python function on my machine there was one drawback, it doesn’t work in Python 3. This is because the use of ‘map’ is discouraged in Python 3. But that’s alright because I can replace it with one of my favourite Python constructs – the list comprehension. Here is an updated version of invert’s function that works in Python 3. Use at your own risk.


def get_password():
    import random
    # Make a list of all of the words in our system dictionary
    f = open('/usr/share/dict/words')
    words = [x.strip() for x in f.readlines()]
    # Pick 2 random words from the list
    password = '-'.join(random.choice(words) for i in range(2)).capitalize()
    # Remove any apostrophes
    password = password.replace("'", "")
    # Add a random number to the end of our password
    password += str(random.randint(1, 9999))
    return password

28th March, 2014

My Career

Filed under: General — andy47 @ 11:14 am

The next time anyone asks me what I do for a living I’m just going to point them to this YouTube video.

Found on the twitters thanks to Nick Hodge and Jess Dodson.

10th January, 2014

It’ll Come Out in the Wash

Filed under: python — andy47 @ 3:36 pm

After reading this fine summary of the history of Python 3 by Nick Coghlan I was inspired to update as many of my half finished projects and miscellaneous scripts as possible. Then I looked up and I had lost several hours of my life. To save random internet strangers from the same pain as I experienced here is a catalogue of problems and how I solved them.

tl;dr – Python 3.3 on Ubuntu 12.04 LTS is possible but quite tricky. Unless you like system administration you are probably better off waiting until 14.04 LTS comes out in a few months.

I’ve settled on a default ‘stack’ of Python and related technologies that I use for most of the code that I write these days;

The good news is that all of the Python related components of these technologies have been ported to Python 3. The bad news is that Flask only runs on Python 3.3. Elementary OS is based on Ubuntu 12.04 LTS and that *only* has Python 3.2 in it’s repositories.

So, first things first, how do we get Python 3.3 on Elementary OS? Or Ubuntu 12.04 which are interchangeable for the purposes of the rest of this post. Quite easily thanks to the ‘deadsnakes PPA’ as explained in this askubuntu post.

I also don’t like to pollute my system Python or operating system packages with project specific Python modules so I always use virtualenv. Unfortunately this doesn’t work with the Python 3.3 acquired from the deadsnakes PPA as *that* doesn’t ship with easy_install.

So by this stage I have Python 3.3 running but no way to create or manage a virtual environment. I then remembered Nick’s talk at PyCon AU where he mentioned that a new lightweight virtual environment would be included in the standard library. Sure enough the venv module (and pyvenv script) were present on my system so I decided it was a good idea to use them.

The bad part for me is that (and it says this in the documentation) venv does not include any means of installing other Python packages. How then do you get a package installer/manager when you can’t install packages? The solution was in a c.l.p. post.

That link describes a process of creating a new virtual environment and then bootstrapping distribute. In case my link-fu fails me the simple shell based approach is;

$ pyvenv py3k ~/envs/py3k
$ cd py3k
$ source bin/activate
$ wget http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
$ ./local/bin/easy_install pip

One final little wrinkle though. The c.l.p. post and other references I found indicate that your package scripts (such as ‘pip’ and ‘python’) will be installed in $ENV_HOME/bin. In my environment they are installed in $ENV_HOME/local/bin. It’s probably something I have inadvertently done. But if you follow the instructions and can’t find something in bin have a look in local/bin and you should find it.

12th December, 2013

Playing with Software

Filed under: General — andy47 @ 9:58 pm

I’ve been trying to install open source software. In this case an application called Moodle. I followed the instructions to install it on an Ubuntu Server vm but they were missing a couple of key points.

After installing the Moodle package (under “Moodle Installation” in the instructions) you need to copy the generated Apache configuration file into your Apache conf.d directory. To do this try the following command;

$ sudo ln -s /etc/moodle/apache.conf /etc/apache2/conf.d/moodle.conf

As you can see from this handy guide to configuring Apache on Debian application specific configuration files should go in the conf.d directory rather than hacking the httpd.conf file.

The second problem is that the default http://locahost/moodle URL won’t work on a server because access is restricted to the local machine only by default. To allow access from other machines (in my case the host computer) you need to edit the generated apache.conf file and uncomment the line which says allow from all. This will enable remote access to the instance from your host machine.

7th July, 2013

How To Start Unit Testing

Filed under: apple — andy47 @ 10:57 am

I had the great privilege to present this weekend at PyCon Australia 2013. My talk was originally titled "Why I Use py.test and Maybe You Should Too" but as I wrote the paper and accompanying slides I realised that it should really have been called "How to Start Unit Testing" as that was really the key message from my talk.

I’ve uploaded the slides and the paper I wrote to accompany them to the presentations section of this site. Feedback and corrections are alway welcome. Thanks to the PyCon Australia committee, volunteers and delegates for another great conference.

22nd November, 2012

Living in the Future

Filed under: python,ubuntu — andy47 @ 11:24 am

On my morning commute today I realised that I am actually living in the future. I remember when I got involved in the PythonCard project 10 years ago one of the major questions on the mailing list was why we were building a GUI toolkit when the future was the web. It wasn’t true then but I think that it is now.

Why do I think we have moved now? It is in large part thanks to a book I have started reading called Python for Data Analysis. I have a copy of the book in ePub format and wanted to read it on my laptop. After some research instead of an e-reader I actually installed a web browser plugin called Readium to view the book.

I then wanted to set up an environment for working through the examples from the book. I created a virtualenv on my Ubuntu server based VM and installed the required modules. After a couple of pages I realised that I needed some sort of graphical environment for rendering graphs. Rather than move to a desktop virtual machine I decided to go for another option. I read the documents and fired up an IPython notebook with remote access. The only thing missing from my useful toolset is a VIM instance. I’m sure that can’t be far away.

Or you can try this android smart tv @qway

All of which means that within a single browser (on separate tabs) I am both reading a book and interactively working through the Python code examples from it. I appreciate that there are back end processes involved especially with the IPython notebook. But here in 2012 it is possible to do some amazing things in the browser that I wouldn’t have imagined even a couple of years ago. Did I mention that I really like living in the future?

23rd August, 2012

Interrupted Service

Filed under: General — admin @ 1:52 pm

This site may not be available for various periods over the next couple of days as I move it lock, stock and barrel to a new web host. It has been a good few years at Cornerhost but it’s time to move. After an exhaustive search I’ve signed up at WebFaction and will be up and running on their servers in no time at all.

This does mean that email reception may be spotty as I migrate. In the rare event that you send me an email and I don’t reply please accept my apologies. Then resend your email. See you on the other side.

Update (2012.08.24): the blog and web site migration seems to have worked. Hopefully email will as well.

21st July, 2012

Quotable Quotes

Filed under: General — admin @ 10:01 pm

It started with a tweet from Tim O’Reilly. He mentioned a quote that I’m very familiar with – “Data matures like wine, applications like fish”. When I read it I wondered if it was anything to do with. His tweet linked to a blog post called the 11 best data quotes from the DataMarket blog. On that list (which I highly recommend reading) the quote was tentatively attributed to me based on a write up of my 2009 OSDC presentation entitled “Change Bad!”.

I’d like to take the credit for this, I really would. But I can’t. I did feature it on the 9th slide of my presentation but I didn’t write it. The problem is that I can’t remember where I first read it. Normally I put a reference in at least my notes so that I know which giant’s shoulders I am standing on but I’ve rummaged around in various hard drives and back ups and can’t find any reference. Which is a shame because it is a great quote – not least because it is very true. If anyone does know the real origin of the phrase please do leave a comment and I’ll put proper attribution in my slides.

You can check here http://www.annjewelry.com

The good news is that it has prompted me to clean up and re-publish the papers and slide from various presentations that I have given. I’ve put up a new index page for my presentations and checked and uploaded the correct versions of all of the files.

UPDATE: thanks to A C Censi in the comments the original quote was from James Governor’s Monkchips –
Why Applications Are Like Fish and Data is Like Wine

1st June, 2012

Python path relative to application root

Filed under: python — admin @ 3:53 pm

I’ve recently written some code to wrangle XML files. Part of the code validates a provided file against an XML Schema stored in a file. When I wrote this code I got tangled up in absolute and relative path manipulations trying to load the XML Schema file. Most of the Python file operations work relative to the current working directory and I needed to be able to load my XML Schema from a file relative to the application root directory. Regardless of where the code was executed from the schema file would always be up and across from the directory containing the Python module being executed. A picture will probably help.

Within load_source_file.py I need to load and parse the XML Schema contained in Source_File.xsd. Here’s how I did it. First, we need to work out the root directory of the application relative to load_source_file.py. After a few false starts this tip from StackOverflow was the key – http://stackoverflow.com/a/1271580/2661. The full path to our etl directory is;

root_dir = os.path.abspath(os.path.dirname(__file__))

But we need to go up a directory so we use os.path.split to remove the last component of the path.

root_dir = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]

The final part is simply joining this with the name of the directory and schema file that we wish to load. Then we have a directory that is the same irrespective of where we run code from. To make reading the code easier I split this across a few lines and ended up with.

>>> import os
>>> from lxml import etree
>>> root_dir = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]
>>> schema_file = os.path.join(root_dir, ‘schemas’, ‘Source_File.xsd’)
>>> xmlschema.doc = etree.parse(schema_file)
>>> xmlschema = etree.XMLSchema(xmlschema_doc)

10th November, 2011

Extracting a discrete set of values

Filed under: python — admin @ 3:42 pm

Today’s I love Python moment is bought to you by set types.

I have a file, XML naturally, the contains a series of transactions. Each transaction has a reference number, but the reference number may be repeated. I want to pull the distinct set of reference numbers from this file. The way I learnt to build up a discrete set of items (many years ago) was to use a dict and set default.

>>> ref_nos = {}
>>> for record in records:
>>> ref_nos.setdefault(record.key, 1)
>>> ref_nos.keys()

But Python has had a sets module since 2.3 and the set standard data type since 2.6 so my knowledge is woefully out of date. The latest way to get the unique values from a sequence looks something like this;

>>> ref_nos = set([record.key for record in records])

I think I should get bonus points for using a list comprehension as well.

Next Page »

Powered by WordPress