Skip to main content

Ralsina.Me — Roberto Alsina's website

Python Trick: the Fundict (or the Diction)

Sup­pose you have made the choice in the past of ex­pos­ing a dic­tio­nary as part of an ob­jec­t's in­ter­face. So, peo­ple are do­ing things like:[foo]['bar']

And now you want people to not have to specify foo because it can be obtained from somewhere else. In fact, what you want now is to expose something like this:, foo='foo')

Here's an idea on how to do that with­out break­ing the old code:

class fundict(dict):
    def __call__(self, bar, foo='foo'):
        return self[foo][bar]

That's a dic­tio­nary that's al­so a callable, and thus in­dis­tin­guish­able from a func­tion. A func­tion-­dic­tionary. A fun­dic­t. And of couse, you could al­so do it the oth­er way around, and im­ple­ment a func­tion that works as a dic­tio­nary. A dic­tio­nary-­func­tion, a dic­tio­n. But since that's more work, I used this one.

Twitter Card Support in Nikola

This fea­ture for Niko­la was im­ple­ment­ed by @amok­leben, and now, if you ask twit­ter your site can have it too.

What are Twit­ter Card­s? Did you no­tice that for some sites, when you click on tweets that have links, you get a small pre­view of the con­tent? Well, that's a twit­ter card.

This still needs a lit­tle im­prove­ment to sup­port the rich­er "me­di­a" card­s, but it does work:


Yes, that's a pic­ture of the twit­ter card for this post. How meta, right?

You don't have to do any­thing in your site, but set­ting the "de­scrip­tion" meta­da­ta to your posts helps make this more ac­cu­rate (and it al­so works for google+)

Nikola 5.4 is out!

I am thrilled to an­nounce the re­lease of ver­sion 5.4 of Niko­la, a stat­ic site/blog gen­er­a­tor

Here's the (in­com­plete!) changel­og for this re­lease [1]:


  • Twit­ter Card / Open Graph sup­­port.

  • Smart math sup­­port

  • New sound­­could di­rec­­tive

  • Cus­­tom "read more" links

  • Bet­ter time dis­­­play, time­­zone sup­­port

  • Bet­ter doit in­­te­­gra­­tion (Is­­sue #151)

  • Make the whole list­ings fold­er brows­able (Is­­sue #128)

  • New GZIP_­­FILES/GZIP_EX­TEN­­SIONS op­­tions to cre­ate gzipped copies of some files (Is­­sue #348)

  • New op­­tion­al path pa­ram­e­ter to new_­­post com­­mand.

  • Word­­press im­­porter: op­­tion to not down­load files

  • Word­­press im­­porter: op­­tion to squash new­­lines

  • Sep­a­rat­ed BLOG_URL in­­­to SITE_URL and BASE_URL

  • Added DIS­­ABLED_­­PLU­G­INS op­­tion (Is­­sue #354)


  • Added mis­s­ing </­di­v> in de­­fault theme tem­­plates.

  • Word­­press im­­port: De­scrip­­tion is left em­p­­ty if no de­scrip­­tion is found.

  • When run­n­ing the build com­­mand it is now pos­si­ble to get help.

  • Load jQuery be­­fore boot­s­trap in JS bun­­dle (Is­­sue #327)

  • Gen­er­ate valid HT­M­L5 for redi­rects (Is­­sue #276)

  • Fixed "niko­la check"

  • Fixed in­­ter­­na­­tion­al­ized RSS links

  • Make com­­mands that need to be run in a site fail grace­­ful­­ly (Is­­sue #342)

  • Use lo­­cal­ized links on lxml fix­er

  • Redi­rec­­tions cre­at­ed dur­ing the im­­port from word­­press are now writ­ten to con­

  • Stop pars­ing meta­­da­­ta in post file on first blank line

  • Meta­­da­­ta han­dling cleanups by Tordek

  • Fixed block­­quote font size in­­­con­­sis­ten­­cy

  • Word­­press Im­­port: Do not break in­­­den­­ta­­tion (is­­sue #189)

  • Make things work even if SITE_URL has a path (Fix #307)

Contents © 2000-2024 Roberto Alsina