Posts about python (old posts, page 14)

2007-10-15 12:37

rst2pdf: now with working tables

Yup. Figured out column/cell spans in reportlab, found old rst2rst code that regularizes the table row/columns to be all the same number of elements, and voilá, tables work, including examples like this:

+---------------------------------+
| header                          |
+==========+======================+
|          |    tr                |
+  tl      +------------+---------+
|          |    c       |  br     |
+----------+------------+         +
|           bl          |         |
+-----------------------+---------+

Which generates nicely on PDF now. See it

The bad side is that it's now not a single file anymore, so I am needing a place to host SVN and whatever, because it's turning into a real project... and googlecode doesn't let me start more stuff.

2007-10-15 09:23

Maybe I should write an office suite.

Really. I already have a functioning slideshow program, a spreadsheet and I could write a word processor in a week (for some values of "word", "processor", "write", "week" and "a".)

Of course it would be a very awful office suite, but at least it would be small. I think I can put all that in 1MB including icons.

I could make stupidsheet pretty good, and tobogan is actually useful (if rough).

2007-10-11 08:30

rst2pdf again

I did a little (very little) more work on rst2pdf [1]

Better:

  • Headers
  • Footers
  • Footnotes (as endnotes, real footnotes are too much work because you have to reflow the text)
  • External links (http, email)
  • Some styling improvements (meaning: the output is not so painful in the eyes)
  • Paper size support
  • Real separators

Then there are some things you just get because it's done using reportlab:

  • TrueType font embedding (check the example below, and look at the monospaced font)
  • No hyphenation (although there is a project on the web that claims to have done it, I should check it out)

Still broken:

  • No way to put things like page numbers or section names in headers/footers
  • The footnotes are not linked to their contents and viceversa
  • In fact, no internal links work, including title references
  • Tables are pretty broken

And here is the usual version of the rst demo showing improvements.

[1] My tool to convert Rstructured Text to PDF

2007-10-06 11:26

Done with rst2pdf for now

It works much better than it did last night.

The main missing/broken things are:

  • Tables
  • Links
  • Footnotes/citations
  • Headers/Footers
  • References
  • Table HEaders/Spanning cells
  • Lists that don't start at 1

The rest seems to be in working order and producing decent output already. It can process the ReST demo and it doesn't look bad: check it out

I say, good for half a day of hacking, and will now stop, because it's a weekend and I have a family :-)

But that doesn't mean you can't try it for yourself. Just run it thus:

python rst2pdf.py myrstfile.txt

And you will get (hopefully) a myrstfile.txt.pdf

Let me know how it works, and if it doesn't and it's not related to one of the things I mentioned above as broken, send me a test file!

2007-10-05 22:26

Generating PDFs from Restructured text

This has always been possible, going via LaTeX.

However, LaTeX being what it is, you either need to learn it, or you end up with rather plain-looking documents.

While that's ok for a manual, I want to use Restructured Text for everything involving documents.

So, I looked for another solution. Sadly, I could not make the existing rlpdf writer work, sooooo I decided to write my own tool.

Since I intensely dislike the Visitor pattern involved in writing a regular docutils writer, I adapted my old rst2rst.py and ended with rst2pdf.py which just traverses the tree recursively and writes the PDF using ReportLab.

And it took me about 3 hours to make it work:

  • For a limited subset of RST (no footnotes, no links, no decoration)
  • For some subset of tables (no col/row spanning)
  • With limited "styling" (it's mostly there, but I need to write a lot of ReportLab styles.

How well does it work... rather well.

Here's a generated PDF of The RestructuredText Primer

Ignore aesthetics, and consider function, it's pretty good.

2007-09-14 18:35

New library: ChipScene

This is the real outcome of my PyWeek failure: a neat library.

Take Chipmunk and Qt mash them up, and what do you get?

A OpenGL-accelerated, multiplatform, easy-to-use playground!

But watch the silly video that shows no interesting features instead:

You can't see it but there are 29 balloons, numbered, that bounce around happily.

Performance in the video sucks because I had to disable OpenGL in order to capture it correctly (and video recording kills my notebook,anyway). That demo normally runs in 3 seconds, not 57.

Here's the non-boilerplate code for that demo:

def fillWorld(scene):
  items=[]
  for x in range(1, 29):
      b=cs.CPBodyItem(bpos=[0+13*random.randint(0,25), -50-30*random.randint(0,10)],m=10)
      s=cs.CPCircleShapeItem(10, b, e=.5, offset=[0, 0])
      t=QtGui.QGraphicsSimpleTextItem(str(x), s)
      t.setPos(-5, -5)
      items.append(b)
  items.append(cs.CPSegmentShapeItem([0, 50], [500, 450], 1, None, e=.7))
  items.append(cs.CPSegmentShapeItem([0, 450], [500, 50], 1, None, e=.7))

  for i in items:
      scene.addItem(i)

Neat, isn't it?

You can get it at the ChipScene google code project including, of course, the source.

2007-09-10 07:54

What I learned at PyWeek

  1. I don't have the time for this kind of deadlines anymore. Not even one all-nighter? I did nothing on Saturday except real work and family reunion?
  2. It's really easy to write games with Python. It's mostly just a matter of having a good game concept. The coding is the easy part.
  3. Chipmunk is cool. Qt's graphic scene stuff is cool. My ChipmunkScene is coolest ;-) I should rethink the API but the concept is killer stuff. With a little work this thing is like LEGO!
  4. I will try again in PyWeek6.

2007-09-06 15:27

A little further on TLB

A bit of progress, although not much time to work on it anymore so I will probably not make it.

Objects now can do things when they are hit by other kinds of things.

Example: If a ball hits the bottom of the catapult cup, the catapult shoots. If something hits the target object, you win the level.

2007-09-06 08:43

First pic of TLB

Not a game yet, but the engine is starting to look good.

tlb1

The ball drops, follows both ramps, bounces down the stairs, hits the dominoes, the last one falls on the pivoting ramp, slides down and to the left, falls standing and leans left, then hits the rest of the dominoes.

2007-09-05 17:01

PyWeek progress: the 4 hour mark

Suddenly I was having a calm day at work, and Rosario is taking care of the baby, so I spent a few hours on the PyWeek project.

I have integrated Chipmunk with QGraphicsScene.

What does it mean? That I can now...

  • Create a scene
  • Create a view onto that scene
  • Create balls, walls and polygons as scene items
  • Watch said balls/walls/polygons bounce around happily under Chipmunk direction.

For example, here's enough code to create a few balls a box and a staircase:

self.scene=ChipScene()
self.ui.view.setScene(self.scene)

for x in range (0, 10):
    self.scene.addBall(x*50.0+10, 50.0, 10.)
    self.scene.addBall(x*50.0+20, 20.0, 10.)
self.scene.addWall(0., 0., 0., 500.)
self.scene.addWall(0., 500., 500., 500.)
self.scene.addWall(500., 500., 500., 0.)
self.scene.addWall(500., 0., 0., 0.)
for i in range (0, 20):
    self.scene.addWall(i*20, 200+i*20, i*20+20, 200+i*20)
    self.scene.addWall(i*20+20, 200+i*20, i*20+20, 200+i*20+20)

self.scene.addPoly([[0, 50], [0, 100], [100, 100], [100, 50], [0, 50]])

I declare that nifty.

Contents © 2000-2018 Roberto Alsina