2

I am using tornado web server for my application.

following is one of the url mapped to a handler.

from publish.handler import PublishHandler, PublishedHandler

URLS = [(r'/public/project/(?P<project>.*?)/?$', PublishHandler),
        (r'/stitchemapp-public/project/(?P<project>.*?)/version/(?P<version>v\d{1,}.*)/image/(?P<image>.*?)/$',
                                                    PublishedHandler),
    ]

All import and call to the handler is happening fine.

But some problem with kwargs being generated from the second url map tuple in the URLS list. When I do

print kwargs

it prints :

{'project': u'clearsoup', 'version': u'v2/image/project_home_page_v1.jpg'}

But I am expecting:

{'project': u'clearsoup', 'version': u'v2', 'image': 'project_home_page_v1.jpg'}

Where I am doing wrong. I can always write hacks to get the exact info from the kwargs which handler is getting, but that's not the right way.

Kindly suggest me where I am doing wrong.

Thanks in advance for the help.

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
Somesh
  • 1,235
  • 2
  • 13
  • 29

1 Answers1

0

I know this is very old, but it is as simple as the version regex is using a greedy quantifier. According to Python's re documentation:

"The '', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.> is matched against b , it will match the entire string, and not just . Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only ."

So, to fix, replace the .* with .*?.

You used the non-greedy quantifier in the other 2 regexes, so I imagine this was a mistake.

Geoff Lentsch
  • 1,054
  • 11
  • 17