1

What is the equivalent in python to this code line in perl

$conf{svnlook} log --revision $rev $repo

i'm looking in python for something as neat as that...

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Jas
  • 14,493
  • 27
  • 97
  • 148
  • its a line i used in a perl script... – Jas Apr 14 '11 at 09:53
  • 2
    Was it in some backticks or `system()` call? It looks like you are calling an external program, whose name is stored in the `$conf{svnlook}` hash. – eumiro Apr 14 '11 at 09:55

3 Answers3

4

That's not really valid Perl. Are you sure it wasn't something like this?

my $log = `$conf{svnlook} log --revision $rev $repo`;

That's calling the svnlook program, an external program not part of Perl. You can do the same thing in Python.

Community
  • 1
  • 1
Schwern
  • 153,029
  • 25
  • 195
  • 336
3

To be exact, what you have there is not a perl command, that is a formatted string... I am assuming you are sending a string command to the shell... to do so in python you can do this...

# assign conf as a dict()
# assign rev and repo as variables
import os
os.system('%s log --revision %s %s' % (conf['svnlook'], rev, repo))

EDIT To answer your question about string formatting, there are python template strings and python format strings... I will demonstrate format strings in the python shell...

>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>>

However, this is still more verbose than perl

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • I must say in perl it looks nicer because they can incorporate $rev $repo inside the string itself I wish i had that in python!!! nothing like that? – Jas Apr 14 '11 at 10:31
  • its still too much heavy i just wanted simple reference to varialbe in strings... :/ with the {} i still need to do later .format etc – Jas Apr 14 '11 at 10:53
  • @Jason, I understand... I was addicted to `perl` for 10 years before switching to `python`... although I must confess that overall, I am glad I switched... – Mike Pennington Apr 14 '11 at 10:55
3
from subprocess import call
call([conf['svnlook'], 'log', '--revision', rev, repo])
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378