Plone: assign permission to role

I always forget, and a quick search through the eggs folder didn’t yield anything easy to find: how to I assign a permission to a role, in a context? This is a bit of code: from Products.DCWorkflow.utils import modifyRolesForPermission from AccessControl.PermissionMapping import getPermissionMapping perm = 'Delete objects' pm = set(getPermissionMapping(perm, context, st=tuple)) pm.add('Contributor') pm.add('Owner') modifyRolesForPermission(wc, perm, tuple(pm)) This is based on code found in DCWorkflow. I know, the proper code would be: [...]

Python packaging vs npm

I admit, Python packages are a bit more difficult to understand, for a newbie. I have witnessed this problem a lot, lately, when dealing with new people trying to learn the Python development process. There is a mountain of information that needs to be climbed, to understand Python packages: the namespace concept the matter of OS security the full cycle of an application, from development to deployment and maintainance And this are just basics, in addition to the distutils/setuptools/virtualenv/pip or zc. [...]

Generate the route url for a Cornice service or resource

As far as I can tell, there’s no documentation on how to generate the reverse url for a Cornice resource or service. Suppose I want to publish a list of children resources and i want to make them behave as linked data. For that, I want to be able to generate proper URLs, based on the request URL. This is some sample code to show how to achieve that, based on a side project I’m working on: [...]

Another way to index category labels in categorization tasks

One common task in machine-based categorization tasks is to relabel data with a numeric value, an index, where before that data was labeled with a string. The basic Python code would be something like this: label_index = {} labels = [] for l in string_labels: if l not in label_index: label_index[l] = len(label_index) labels.append(label_index[l])> While writing that bit of code from above, I realized that a word tokenizer can do the same thing. [...]

How to shuffle two arrays to the same order

This is a small recipe on how to get two arrays with the same shape (same length) shuffled with the same “random seed”. This is useful when the two arrays hold related data (for example, one holds values and the other one holds labels for those values). It takes advantage of the fact that numpy arrays can be indexed with other arrays, something that seems really magical when compared to regular python arrays. [...]

Editing Short python scripts with vim

Being that I usually find interesting to know about other people’s workflow, here’s a short description of my working environment that I typically use when developing in Python: tmux In the beginning I’ve used Yakuake tabs to split servers and files in separate tabs. As the number grew, I’ve started naming the tabs (and even had a short stint extending Yakuake to fit this use case), but as the number of projects and environments that I have to juggle kept growing, I’ve resorted to splitting each separate tab into “subtabs”, using tmux. [...]

Do you really need that metadata column?

It is one of the tenets of Plone optimization that brain.getObject() should be avoided and instead new metadata columns should be defined, to pass have that information in the brain. In the interest of keeping the ZODB free of junk and avoid duplication of information, I argue that it is possible sometimes to avoid polluting the catalog and instead use the information stored in the index itself.  As an example: when exploring collective. [...]

Auto-bootstrap (n)vim configuration

Ever since I've moved to Neovim and redid my whole setup, I've had this piece of code at the top of my init.vim: if empty(glob('~/.config/nvim/autoload/plug.vim')) silent !curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim autocmd VimEnter * PlugInstall | source $MYVIMRC endif Together with the rest of that dotfile, it automatically bootstraps the whole plugins install process, which makes moving to a new machine a simple operation of just cloning the repo in the proper location, start once Neovim to let it bootstrap, then boom, everything fits in place. [...]

Django templates make me go mad...

Why is this a positive thing? Excerpt from Django Oscar, an eComerce framework. <li class="step2 {% if step == 2 %}active{% else %}{% if step == 3 %}previous{% else %}{% if step > 2 %}visited{% else %}disabled{% endif %}{% endif %}{% endif %} "> Gah... [...]

Always fun, deciphering Ruby DSLs (part 1)

I never back down from a new system or programming language and thankfully my daily work has exposed me to some Ruby and Rails code. So I've been learning some, just enough to be able to tweak code, fix bugs and make minor additions. I'm not a fan of the Ruby on Rails design, but I enjoy it as a challenge. One thing that I found difficult was writing code that uses some of the many DSLs created by the community. [...]