Adding Dynamically Named Methods to Python Classes

Photo by Inbetween Architects on Unsplash

I recently wanted to dynamically add methods to a Python class based on a list of relationships. The model class, Animal, was related to a number of description classes like Color and Sound. For a given animal, I wanted to be able to easily get the values for its descriptions, such as:

elephant.color_values()
# => ['gray', 'silver', 'taupe']

Although I could have simply written non-dynamic methods like color_values, noise_values, etc. in the Animal class, I had already defined a list of the relevant class names and I prefer not repeating code. So instead I dove into Python’s dynamic pool.

(more…)

2014 Review: Day 9

software screenshot

Taking an opportunity to look back at some of our most-read posts from this year, in case you missed them the first time, as the last few days of the year slip by us…

In our penultimate look back at 2014’s developer blog, Fran Bull talks about using interactive widgets inside of IPython notebooks:

(more…)

2014 Review: Day 8

Taking an opportunity to look back at some of our most-read posts from this year, in case you missed them the first time, as the last few days of the year slip by us…

William Allen White

William Allen White

 

My turn! Earlier this year I wrote about a little Twitterbot that I wrote to periodically tweet out snippets of lyrics from songs by the band They Might Be Giants…

(more…)

2014 Review: Day 4

As 2014 winds down, we’re taking an opportunity to look back at some of our most-read posts from this year, in case you missed them the first time. 

Django logo

Vlad Orlenko wrote a couple of great posts on integrating social logins and posting with web apps written using the Django web app framework:

(more…)

A Django-Style Admin Interface For Pyramid

Photo of Chichen Itza pyramid by Jimmy Baum on Unsplash

Pyramid Logopyramid_formalchemy provides a CRUD interface for Pyramid based on FormAlchemy. Which is to say it looks at your SQLAlchemy models and automagically generates an HTML interface to create, read, update and delete instances of those models from the database, very much like Django’s admin interface. That sounds pretty cool, doesn’t it? In this post I will demonstrate how to get it up and running and then start customizing the forms.

Software screen capture
(more…)