Blog do Fernando Meyer

Só mais um blog do WordPress

Writing a domain specific language DSL with python

View Comments

A domain-specific language is a piece of software designed to be useful for a specific task in a fixed problem domain, they’re gaining popularity because they enhance productivity and reusability of artifacts. DSLs also enable expression and validation of concepts at the level of abstraction of the problem domain, this approach is very useful when you need to describe a user interface, a business process, a database, or the flow of information.

The DSL concept isn’t new after all, special-purpose programming languages and all kinds of modeling, specification languages have always existed, but this term rise due the popularity of domain-specific model.

You can easily implement dsls using the ruby language, Java or even C# if you prefer, but this isn’t the main propose of this article. The sine qua non become visible when I was implementing a simple test case with python. Indeed, there are a lot of python BDD-like frameworks, mostly who are self claimed the silver bullet, that are mismatching a lot of basic principles, but like I said, we are talking about dsls =)

With python we can easily create a piece of software that expresses some basic desired behavior, like rspec does, but much more pythonic.

spec.py

# coding: pyspec
    class Bow:
        def shot(self):
            print "got shot"
 
        def score(self):
            return 5
 
    describe Bowling:
        it "should score 0 for gutter game":
            bowling = Bow()
            bowling.shot()
            assert that bowling.score.should_be(5)

we can easily make this test dsl a runnable piece of python code, whiteout writing incompressible regular expressions, just using the python codecs and tokenizer.

Fist of all we need to define a new encoding for pyspec – our pre defined spec file syntax – this neat hacking enables a new path to tokenize this file

tokenizer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
import tokenize
    import codecs, cStringIO, encodings
    from encodings import utf_8
 
    class StreamReader(utf_8.StreamReader):
        def __init__(self, *args, **kwargs):
            codecs.StreamReader.__init__(self, *args, **kwargs)
            data = tokenize.untokenize(translate(self.stream.readline))
            self.stream = cStringIO.StringIO(data)
 
    def search_function(s):
        if s!='pyspec': return None
        utf8=encodings.search_function('utf8') # Assume utf8 encoding
        return codecs.CodecInfo(
            name='pyspec',
            encode = utf8.encode,
            decode = utf8.decode,
            incrementalencoder=utf8.incrementalencoder,
            incrementaldecoder=utf8.incrementaldecoder,
            streamreader=StreamReader,
            streamwriter=utf8.streamwriter)
 
    codecs.register(search_function)

Our tiny translate function defines a easy way to translate both describe and it into a traditional python class and method definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
def method_for_it(token):
        return token.strip().replace(" ", "_").replace("\"","" ) + "(self)"
 
    def translate(readline):
        previous_name = ""
        for type, name,_,_,_ in tokenize.generate_tokens(readline):
            if type ==tokenize.NAME and name =='describe':
                yield tokenize.NAME, 'class'
            elif type ==tokenize.NAME and name =='it':
                yield tokenize.NAME, 'def'
            elif type == 3 and previous_name == 'it':
                yield 3, method_for_it(name)
            else:
                yield type,name
            previous_name = name

Clever isn’t ? Now, fell yourself free to fork this project on GitHub and finish the job =) maybe someday we can have a real BDD python framework. http://github.com/fmeyer/pydsl/tree/master

References:

  1. codecs — Codec registry and base classes
  2. tokenize — Tokenizer for Python source
  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

September 5th, 2009 at 6:51 pm

Posted in python

Venha trabalhar na globo.com

View Comments

A vaga é para uma das equipes responsáveis pelo desenvolvimento de novos produtos globo.com. Somos uma equipe multidisciplinar totalmente responsável pelos projetos desde sua concepção, criação, desenvolvimento, deployment até o suporte.

Não estamos procurando especificamente desenvolvedores Java, PHP, Ruby, Python, C ou C++, queremos uma pessoa totalmente agnóstica que não tenha medo de experimentar e aprender. É óbvio que cada um tenha suas especialidades e/ou preferências, mas estamos procurando programadores multidisciplinares e que além disso sejam capazes não só de programar mas de arquitetar, analisar, testar e trabalhar com novas tecnologias o tempo todo.

Mesmo assim, fortes conhecimentos em Ruby, Javascript, CSS, HTML, Banco de dados schema free, Infra e Amazon EC2 são um grande diferencial.

Por último mas não menos importante, todos nós somos geeks, apaixonados por tecnologia e super atualizados com as últimas novidades da Internet e do mercado. Nossa equipe é jovem, irreverente, descontraída e em constante evolução. É uma pessoa assim que estamos procurando.

A empresa oferece contratação apenas por CLT, com salário de mercado e plano de benefícios. Estamos localizados na Barra da Tijuca (Rio de Janeiro).

Se você acha que se enquadra, mande um email para nós beta@corp.globo.com com uma breve introdução, com todos os seus contatos github, twitter, LinkedIn e blog, mande também a lista dos 3 últimos livros que leu.

@fmeyer, @gcirne, @nevesbruno, @peleteiro

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

September 2nd, 2009 at 6:36 pm

Posted in Sem categoria

Pagerank isn’t for humans

View Comments

Transitioning from a web of links and a sequence of words to a web of content, meaning and knowledge is probably the next great moving that we are going to see on the next two or maybe three years.

I was chatting with a friend, how can we bring knowledge to the web, how can we use the web to make a really efficient human driven search engine, last week a few former Googlers made some noise about a totally new SE named cuil, I tried some queries on that page, but it does work almost like google, you must be pretty much binary to retrieve some interesting information, in another words, nothing has changed since 90’s on this area. This problem is around researcher’s minds for a long time, trust, influence, authority when applied to the web are essentially people based issues.

The propose is the content being an asset with information about what it really means running against the link/words algorithms with no explicit meaning and a simple assumption “yes … we know you’re a good reference because you have a lot of links”.

Make yourself a question, how to ask something? How do I ask for information?

You ask your close friend: “Sunday night guitar red cap TV?” when you really want to know about the Sunday night TV show where a girl with a funny red cap playing a guitar. Things does change when you bring meaning to it, thats what a human being does.

Indeed, Google is still leading this running, with several fields under extreme research, articles about Data Mining, Collective Intelligence and AI being published denotes the new approach.

Yes … things are about to change

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 30th, 2009 at 2:13 am

Posted in Sem categoria

Configuring Joseki + Pellet + TDB

View Comments

I was looking for some documentation about using TDB and Pellet
reasoser on Jena without pet peeves, didn’t found anything usable at all, so after a few
attempts I was still fighting against an ambiguity error,
whenever Joseki tries to build a dataset, the following exception pops up

18:18:49 WARN  Configuration        :: Failed to build dataset from
description (service name: reason): cannot find a most specific type
for file:///joseki-config.ttl#dataset_reason, which has as
possibilities: ja:RDFDataset ja:InfModel.
com.hp.hpl.jena.assembler.exceptions.AmbiguousSpecificTypeException:
cannot find a most specific type for file:///Users/fmeyer/projects/semantic/
servicos/joseki/joseki-config.ttl#dataset_reason, which has as possibilities: 
ja:RDFDataset ja:InfModel.
 at com.hp.hpl.jena.assembler.assemblers.AssemblerGroup$PlainAssemblerGroup.
 open(AssemblerGroup.java:104)
 at com.hp.hpl.jena.assembler.assemblers.AssemblerGroup$ExpandingAssemblerGroup.
 open(AssemblerGroup.java:70)
 at com.hp.hpl.jena.assembler.assemblers.AssemblerBase.open(AssemblerBase.java:41)
 at com.hp.hpl.jena.assembler.assemblers.AssemblerBase.open(AssemblerBase.java:38)
 at org.joseki.DatasetDesc.newDataset(DatasetDesc.java:65)
 at org.joseki.DatasetDesc.initialize(DatasetDesc.java:60)
 at org.joseki.Configuration.processModel(Configuration.java:112)
 at org.joseki.Configuration.(Configuration.java:83)
 at org.joseki.Dispatcher.setConfiguration(Dispatcher.java:130)
 at org.joseki.Dispatcher.initServiceRegistry(Dispatcher.java:100)
 at org.joseki.Dispatcher.initServiceRegistry(Dispatcher.java:93)
 at org.joseki.RDFServer.init(RDFServer.java:79)
 at org.joseki.RDFServer.(RDFServer.java:64)
 at joseki.rdfserver.main(rdfserver.java:85)

We just need to define a GraphTDB as a subclass of a Model as follow to fix this issue

@prefix ja:     <http ://jena.hpl.hp.com/2005/11/Assembler#> .
@prefix tdb:     </http><http ://jena.hpl.hp.com/2008/tdb#> .
 
## Initialize TDB.
 
[] ja:loadClass "com.hp.hpl.jena.tdb.TDB" .
tdb:DatasetTDB  rdfs:subClassOf  ja:RDFDataset .
tdb:GraphTDB    rdfs:subClassOf  ja:Model .
 
## ---- A whole dadaset managed by TDB
< #dataset_reason> rdf:type      ja:RDFDataset ;
  ja:defaultGraph < #inf_graph> .
 
< #dataset> rdf:type      ja:RDFDataset ;
  ja:defaultGraph < #def_graph> ;
  .
 
< #inf_graph> rdf:type  ja:InfModel ;
    ja:reasoner [
        ja:reasonerClass
            "org.mindswap.pellet.jena.PelletReasonerFactory" ;
       ] ;
    ja:baseModel < #tdbGraph> .
 
< #def_graph> rdf:type  ja:InfModel ;
    ja:baseModel < #tdbGraph> .
 
< #tdbGraph> rdf:type tdb:GraphTDB ;
    tdb:location "DB" ;
    .
  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 27th, 2009 at 12:45 am

Posted in Sem categoria

Debug Backwards in time

View Comments

What if a debugger could allow you to simply step BACKWARDS? Instead of all that hassle with guessing where to put breakpoints and the fear of typing “continue” one too many times… What if you could simply go backwards to see what went wrong? This is the essence of the “Omniscient Debugger” — it remembers everything that happened during the run of a program, and allows the programmer to “step backwards in time” to see what happened at any point of the program. All variable values, all objects, all method calls, all exceptions are recorded and the programmer can now look at anything that happened at any time

HomePage: http://www.lambdacs.com/debugger/debugger.html

Article: http://www.lambdacs.com/debugger/Article.html

Video: http://video.google.com/videoplay?docid=3897010229726822034&q=engedu+debugging

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria

Berkeley e-classes for your IPod

View Comments

Last April, UC Berkeley, one of the premiere schools in the United States, announced its plan to put complete academic courses on iTunes. Fast forward nine months, and you can already find 59 full courses ready for your iPod. Simply click here to access Berkeley’s iTunes site (or here for the Rss feed).

No matter where you live, you can access at no cost the very same courses attended by students paying full tuition. And, given the critical mass of courses being offered across a range of disciplines, you can put together your own personalized curriculum and expand your horizons on the fly.

That’s include interesting computer’s science coursers like, CS 162 – Operating systems and System programming and CS 61C – Machine Structures. You can listen this podcast and forget about these classes in university (but I think that your teacher will not appreciate)

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria

Firefox is crashing on my macbookpro

View Comments

I don’t know why but it’s happening, every time when i open a second gmail tab my Firefox crashes

Exception:  EXC_BAD_INSTRUCTION (0×0002)
Code[0]:    0×0000000d
Code[1]:    0×00006e64

Thread 0 Crashed: 0 libSystem.B.dylib 0×90029ae5 _longjmp + 37 1 00000000 0xc00101dd 0 + -1073675811

I was looking for some similar bug at firefox bugzilla but, I didn’t found anything about it. That isn’t the unique problem, I’m coding a web interface to access and test some Webservices, then I discovered another bug: div element with visibility:hidden still show scrollbars, I couldn’t reproduce the problem on Firefox running over Win32 with the same html file, so I think that’s a bug.

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria

Linux Kernel in a Nutshell is now Creative Commons: free pdf download

View Comments

Written by a leading developer and maintainer of the Linux kernel, Linux Kernel in a Nutshell is a comprehensive overview of kernel configuration and building, a critical task for Linux users and administrators.The book is available for download in either PDF or DocBook format for the entire book, or by the individual chapter.

To quote of the book’s author:

If you want to know how to build, configure, and install a custom Linux kernel on your machine, buy this book. It is written by someone who spends every day building, configuring, and installing custom kernels as part of the development process of this fun, collaborative project called Linux. I’m especially proud of the chapter on how to figure out how to configure a custom kernel based on the hardware running on your machine. This is an essential task for anyone wanting to wring out the best possible speed and control of your hardware.

http://www.kroah.com/lkn/

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria

Switching from bloglines to vienna

View Comments

I’m changing my default RSS reader from the web-based bloglines to Vienna, a rss reader for Mac OS X, Vienna keeps following RSS feeds simple and functional with smart folders, groups, an integrated browser and item flagging. It’s the perfect tool for offline readers like me.

ps: Yeap, I’m having some problems to get an internet connection here in Colombia; (sic)

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria

Learning English Spanish, Italian, Portuguese

View Comments

The English language has been at the forefront of globalization. English is celebrated as the language of global corporate management, the Internet, youth culture and science. At the same time, there appears to be a crisis in foreign language learning amongst native English speakers – it seems that there is no need any more to learn foreign languages if everyone now speaks English. But if you want to learn another language like Spanish, German or even Brazilian Portuguese, you can access these podcasts.
Note: You must have ITunes installed in your computer.
Learn Arabic Learn Chinese Learn Chinese Learn Chinese Learn Chinese English As a Second Language ESL English For Business Beginner French Learn French Learn French Learn French Learn French Verbs Learn German I II III IV Learn German Grammar Learn German Learn Greek Learn Hindi Learn Italian Learn Italian Learn Japanese With Video Japanese for Beginners Learn Japanese Symbols Learn Korean Learn Portuguese Learn Brazillian Portuguese in Spanish Learn Russian Learn Russian For Businesses Russian Literature Learn Spanish Learn Spanish Learn Spanish

  • Facebook
  • Slashdot
  • WordPress
  • LiveJournal
  • Plurk
  • Read It Later
  • Digg
  • Posterous
  • Reddit
  • Tumblr
  • Orkut
  • Yahoo Bookmarks
  • DZone
  • Google Bookmarks
  • Twitter
  • Google Gmail
  • Delicious
  • Google Reader
  • LinkedIn
  • Yahoo Messenger
  • Share/Bookmark

Written by fmeyer

August 26th, 2009 at 8:52 pm

Posted in Sem categoria