Error installing Ruby gems and Python packages in Mac OS X (clang: error: unknown argument)

When we try to install ruby gems or python packages, it’s possible to encounter errors like this:

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

This is because some changes in the LLVM compiler explained in the Xcode 5.1 Release Notes:

The Apple LLVM compiler in Xcode 5.1 treats unrecognized command-line options as errors. This issue has been seen when building both Python native extensions and Ruby Gems, where some invalid compiler options are currently specified.

Projects using invalid compiler options will need to be changed to remove those options. To help ease that transition, the compiler will temporarily accept an option to downgrade the error to a warning:

-Wno-error=unused-command-line-argument-hard-error-in-future

To workaround this issue, set the ARCHFLAGS environment variable to downgrade the error to a warning. For example, you can install a Python native extension with:

$ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install ExtensionName

Similarly, you can install a Ruby Gem with:

$ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install GemName

In summary, in order to install a ruby gem we need to do it with the following line:

$ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install GemName

Or, in Python’s case:

$ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install ExtensionName

Python list and set comprehensions

When we need to convert certain elements from an iterable object, generator, etc. to a list or set, it is possible to do it in just one line thanks to the list an set comprehensions. Lets consider the following list of dictionaries.

>>> days = [{'day': 'monday', 'index': 1}, {'day': 'tuesday', 'index': 2}]

The way to do it without this tool is to store the key index of the dictionaries would be:

>>> l = list()
>>> for day in days:
...    l.append(day['index'])
>>> l
[1, 2]

If we use list comprehensions we would do it like this:

>>> l = [day['index'] for day in days]
>>> l
[1, 2]

Similarly, we could use set comprehensions when needed:

>>> s = {day['index'] for day in days}
>>> s
set([1, 2])

In this examples it doesn’t look like a big deal, but when we work with complex objects and we want to have a neat and legible code (and with less indentation), it is a great alternative!

You can find more examples in the link below from python.org.

Sources:
docs.python.org | Data Structures (List Comprehensions) | Data Structures (Sets)

How to delete duplicated elements from a list in Python

This is a simple way to delete duplicated elements from a list in Python when the order is not important.

>>> l = [1, 2, 2, 3, 4, 1, 3, 5, 6]
>>> l
[1, 2, 2, 3, 4, 1, 3, 5, 6]
>>> l = list(set(l))
>>> l
[1, 2, 3, 4, 5, 6]

In this case, we’re converting our list to a set data structure and take advantage of its “non duplicate elements” property. Then, we convert that object into a list again in order to have the initial data structure but with unique items in it.

Sources:
docs.python.org | Data Structures (Sets)