zc.sourcefactory is very handy to easily create a source (zope.schema.interfaces.IIterableSource to be precise) with corresponding titles and tokens for its contents. Every now and then a source requires an explicit interface. For zc.sourcefactory the following code snippet helps:
"""The source factory."""
"""This class is being instanciated by the factory.
It *must* be called source_class.
def getValues(self, context):
Of course it is also possible to declare the source_class separately from the source factory and reference it. But since its sole purpose is to hold an implements declaration, I’m fine with defining it inline.
The Pyramid documentation recommends using WebTest for functional tests, but coming from the Zope world, zope.testbrowser is the more familiar tool for this job (and it seems a little more high-level than WebTest at first glance).
With the 3.11 release, zope.testbrowser gained out-of-the-box support for talking to WSGI applications (courtesy of wsgi_intercept), so herewith some corresponding example code to show the basic usage and setup. (This assumes you are using zope.testrunner or something else that supports its layers).
from myapp import main
MY_APP_LAYER = MyAppLayer()
layer = MY_APP_LAYER
browser = zope.testbrowser.wsgi.Browser()
self.failUnless('Pyramid' in browser.contents)
Then, add the following lines to the box that should get round corners. Here you can also customize the radius:
-moz-border-radius: 3px;-webkit-border-radius: 3px;
That’s all, the result looks like this:
A box with curvy corners.
Imagine, you want to display a date in a nicer way than just 2009-11-17. It’s not that difficult to localize it into e.g. German and output 17.11.2009. But what, if you need to display Mittwoch, 11.November 2009 instead?
Calling strftime(‘%A, %d.%B %Y’) on your date object would format the date with month- and week names, but in most cases display the names in English and not in German, as we @gocept need it.
Locales provide you with the nessecary functionality to format your datetime object in the users local format and language. You can choose from different templates (parameter length) or even create your own templates.
The example defines a method formatDate which takes a datetime object. It retrieves the users locale settings from the request, chooses a format template and then “mixes” everything together :-).
So, to get something like “Wednesday, 17.November 2009”, you will need to submit the length parameter full as shown in the above example:
def formatDate(self, datetime_date):
if not self.request._locale:
# zope2 doesn't initialize the locale itself
formatter = self.request._locale.dates.getFormatter(
I fiddled a bit for making my VIM tell me when obvious (statically detectable) mistakes sit in my code when saving. This speeds up the testing process a bit when e.g. syntax errors exist and helps me adhere to PEP 8.
Continue reading “Automatic Python syntax and PEP 8 checking in VIM”
We were embedding a spinner to give user feedback while loading data from a server which might take a little longer (but can also be pretty quick in most cases).
Implementing the spinner itself isn’t that hard, but we found that quick responses from the server caused visual artifacts flickering up because the spinner was only visible for a few milliseconds (probably roughly 30ms).
Continue reading “AJAX loading spinner without flickering artifacts”