captainandcoke
captainandcoke

Reputation: 1115

python replace print with function

I'm using python 2.6 and have a bunch of print statments in my long program. How can I replace them all with my custom print function, lets call it scribble(). Because if I just search and replace print with scribble( there is no closing parentesis. I think regular expressions is how, but I have experimented with them for a day or so and I can't seem to get it to work.

Upvotes: 6

Views: 7258

Answers (6)

Lorem Ipsum
Lorem Ipsum

Reputation: 4534

Here's an Emacs implementation that, by default, converts a print statement to a print function, with the option to change any statement to any function. I've tried to document it clearly, but please let me know if anything is unclear.

OP could use it interactively with M-x my-statement-to-function RET scribble RET or programmatically with (my-statement-to-function "print" "scribble").

(defun my-statement-to-function (&optional statement func)
  "Convert STATEMENT to FUNC.

For use with statements in Python such as 'print'.  Converts
statements like,

  print \"Hello, world!\"

to a function like,

  print(\"Hello, world\")

Also works generally so that the STATEMENT can be changed to any
FUNC.  For instance, a 'print' statement,

  print \"Hello, world!\"

could be changed to a function,

  banana(\"Hello, world!\")

Default STATEMENT is 'print'.  Default FUNC is
STATEMENT (e.g. 'print').  Prompt for STATEMENT and FUNC when
called with universal prefix, `C-u'."
  (interactive "p")
  (let* ((arg statement)  ; statement argument overwritten, so preserve value
     ;; only prompt on universal prefix; 1 means no universal, 4 means universal
     (statement (cond ((eql arg 1) "print")  ; no prefix
                      ((eql arg 4) (read-string "Statement (print): " "" nil "print")))) ; C-u
     (func (cond ((eql arg 1) statement)  ; no prefix
                 ((eql arg 4) (read-string (concat "Function " "(" statement "): ") "" nil statement)))) ; C-u
     ;; [[:space:]]*  -- allow 0 or more spaces
     ;; \\(\"\\|\'\\) -- match single or double quotes
     ;; \\(\\)        -- define capture group for statement expression; recalled with \\2
     (regexp (concat statement "[[:space:]]*\\(\"\\|\'\\)\\(.*?\\)\\(\"\\|'\\)"))
     ;; replace statement with function and place statement expression within parentheses \(\)
     (replace (concat func "\(\"\\2\"\)")))
    (goto-char (point-min))
  (while (re-search-forward regexp nil t)
    (replace-match replace))))

Upvotes: 0

Alvin
Alvin

Reputation: 259

Using Pycharm

If you are using pycharm, you can use Regex function by this pattern:

Replace: print\s(.*)
With: print($1)

enter image description here

Upvotes: 11

John Y
John Y

Reputation: 14529

Actually, you can convert all your print statements to print() functions using the included 2to3 tool. While this tool is normally used to convert a Python 2 program to a Python 3 program as completely as possible, it is actually a collection of small fixes, and you can choose which fixes to run. In your case, you can run only the print fixer by giving the argument -f print when you invoke 2to3.

Upvotes: 3

Overv
Overv

Reputation: 8529

Using editor

I don't know which editor you're using, but if it supports RegEx search and replace, you can try something like this:

Replace: print "(.*?)"
With: scribble( "\1" )

I tested this in Notepad++.

Using Python

Alternatively, you can do it with Python itself:

import re

f = open( "code.py", "r" )
newsrc = re.sub( "print \"(.*?)\"", "scribble( \"\\1\" )", f.read() )
f.close()

f = open( "newcode.py", "w" )
f.write( newsrc )
f.close()

Upvotes: 8

Jay M
Jay M

Reputation: 4297

Rather than replacing it, you could overload the print function!

In python 2.x this is not directly possible. But there are tools that convert python 2.x into python 3 code.

Run your code through the converter, then overload the print function.

Versions of python below 2.6 still support print functions (and hence overloading) by using from future. So once coverted you code should still work on older versions. Though it seems most if not using 3.x are using 2.7 so you might not need from future

Upvotes: 1

Endophage
Endophage

Reputation: 21483

The is one place where a real IDE would help you out if you're not using one already. Using an IDE like PyCharm or Eclipse you could use refactoring to replace all calls to a particular function with a different call.

Upvotes: 0

Related Questions