I wanted to turn a list like
['*.zip', '*.pyc', '*.log'] into
['--exclude', '*.zip', '--exclude', '*.pyc', '--exclude', '*.log'].
A simple list comprehension doesn't work as desired:
In [1]: excludes = ['*.zip', '*.pyc', '*.log']
In [2]: [('--exclude', e) for e in excludes]
Out[2]: [('--exclude', '*.zip'), ('--exclude', '*.pyc'), ('--exclude', '*.log')]
The trick is to use a nested comprehension:
In [5]: [arg for pattern in excludes
for arg in ['--exclude', pattern]]
Out[5]: ['--exclude', '*.zip', '--exclude', '*.pyc', '--exclude', '*.log']
I worked on a Bash script today that sets up various prerequisites for our build.
We need a recent version of Docker
but our Bamboo build agents are running on Ubuntu 14.04,
which has a very old version of Docker.
The script upgrades Docker when it's first run.
The script may be run more than once during the lifetime of the agent,
so the second and subsequent calls should not upgrade Docker.
Basically, I wanted
if $DOCKER_VERSION < 1.9; then upgrade_docker; fi
Unfortunately, it's not that easy in Bash.
Here's what I came up with.
install_latest_docker() {
if docker --version | python -c "min=[1, 9]; import sys; ↩
v=[int(x)
…continue.
[Previously published at the now defunct MetaBrite Dev Blog.]
I spent some time today struggling with setuptools,
trying to make a Python source package
not only include a data file,
but also install that file.
Building the installer
Consider the following source tree layout:
├── MANIFEST.in
├── README.md
├── my_stuff/
│ ├── bar.py
│ ├── foo.py
│ ├── __init__.py
│ └── quux.py
├── models/
│ └── long_ugly_name_20151221.json
└── setup.py*
I wanted to create a Python source distribution, some_package-N.N.N.tar.gz,
which contains the code in the my_stuff directory,
as well as models/long_ugly_name_20151221.json,
using python setup.py sdist.
It's not that hard to get models/long_ugly_name_20151221.json
included in the tarball.
Add an entry in MANIFEST.in:
include models/*.json
Then be sure to set include_package_data=True
in the call to setup():
from setuptools
…continue.
At this month's PuPPy (Puget Sound Programming Python) Meetup,
I heard a brief mention of Python f-strings
as a new feature coming in Python 3.6.
In essence, they offer a simpler, more versatile method
of string formatting and interpolation
over existing methods.
F-strings can include not only symbol names
but Python expressions within strings.
With str.format, you can write
'Hello, {name}'.format(name=some_name).
You can control various aspects of how name is formatted,
such as being centered within a field—see PyFormat and Python String Format Cookbook
for examples—but no more complex expression is allowed between the braces.
Herewith some examples of f-string expressions
drawn from PEP 0498:
>>> date = datetime.date(1991, 10, 12)
>>> f'{date} was on a
…continue.
On StackOverflow, someone wanted to
print triangles in Python
in an M-shape.
Various clumsy solutions were offered.
Here's mine which uses the left- and right-justification features of
str.format.
- "{0}+++{0}".format('Hello!') produces two copies of the zeroth argument to format
(here 'Hello!'), separated by three plusses: Hello!+++Hello!.
- "{:<4}".format('x') left-justifies 'x' in a 4-character field; i.e., 'x '.
- "{:>4}".format('x') right-justifies 'x' in a 4-character field; i.e., ' x'.
- "{:>{}}".format('x', width) right-justifies 'x' in a width-character field.
- 'ab' * 4 yields 4 copies of 'ab'; i.e., 'abababab'.
Putting them together:
.. code:: pycon
>>> WIDTH = 4
>>>
>>> for a in range(1, WIDTH+1):
... print("{0:<{1}}{0:>{1}}".format('*' * a, WIDTH))
...
* *
**
…continue.
I was debugging a filtering directory walker
(on which, more to follow)
and I was trying to figure out
the mysterious suffix that
fnmatch.translate
appends to its result,
\Z(?ms).
fnmatch.translate takes a Unix-style glob,
like *.py or test_*.py[cod],
and translates it character-by-character into a regular expression.
It then appends \Z(?ms).
Hence the latter glob becomes r'test\_.*\.py[cod]\Z(?ms)',
using Python's raw string notation to avoid the
backslash plague.
Also, the ? wildcard character becomes the . regex special character,
while the * wildcard becomes the .* greedy regex.
A StackOverflow answer partially explains,
which set me on the right track.
(?ms) is equivalent to compiling the regex with re.MULTILINE | re.DOTALL.
The re.DOTALL modifier makes the . special character
match any character,
including …continue.
I'm doing some Python profiling
and I wanted to use the RunSnakeRun utility to view the profile data.
Unfortunately, that's not straightforward on Mac OS X if you use a virtualenv,
and it's even less easy if you're using the Python
installed by the Homebrew (brew) package manager.
There are several problems:
- Installing wxPython on OS X 10.10.
This is the cross-platform GUI API toolkit used by RunSnakeRun.
- Getting wxPython installed in a virtualenv
- Running wxPython apps in a virtualenv on the Mac.
Installing wxPython
I downloaded wxPython3.0-osx-3.0.2.0-cocoa-py2.7.dmg,
released in November 2014.
If you open the DMG and attempt to run the PKG,
you will likely get a misleading error message from OS X:
“wxPython3.0-osx-cocoa-py2.7.pkg” …continue.
We use Nose to run unit tests.
I noticed that we had some tests that weren't being run,
and it took me some time to work out why.
Eventually, I found this checklist,
which told me to "make sure the files in your tests directory are not executable".
A quick chmod -x *.py later and nosetests project/tests suddenly found all the tests.
Now that I know what to look for, I found this in the Nose docs:
It is important to note that the default behavior of nose
is to not include tests from files which are executable.
--exe: Look for tests in python modules that are executable.
Normal behavior …continue.
Every so often, one of our Bamboo builds would break thus:
pkg_resources.ExtractionError: Can't extract file(s) to egg cache
The following error occurred while trying to extract file(s) to the Python egg
cache:
[Errno 17] File exists: '/home/bamboo/.python-eggs'
The Python egg cache directory is currently set to:
/home/bamboo/.python-eggs
Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.
This occurred while trying to make use of PyCrypto.
After a little research, I decided that instead of installing PyCrypto
as a zipped egg (as it does by default) into the …continue.
I did a clean install of OS X 10.10 on my home laptop a week ago.
I tried to launch PyCharm 4.0.4 on it today.
It immediately failed. Every time.
When I looked in the System Console, I saw:
1/25/15 7:46:00.557 PM pycharm[1160]: No matching VM found.
1/25/15 7:46:00.711 PM com.apple.xpc.launchd[1]: (com.jetbrains.pycharm.58252[1160]) Service exited with abnormal code: 1
The JetBrains website wasn't very helpful when I looked there.
In time, I found a StackOverflow answer that put me on the right track
(and reminded me that I had previously solved this problem about a year ago, at work).
PyCharm and some of the other JetBrains IDEs require JDK 1.6,
as …continue.
Previous »
« Next