6

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.

0

6 Answers 6

11

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

Sign up to request clarification or add additional context in comments.

Comments

8

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()

3 Comments

Doens't python itself support RegEx?
It does, you could use Python to refactor your script. Let me write you up a solution, gimme a min or two.
Find: print "(.*?)" and Replace: print($1) works on Visual Studio Code (enable regular expression)
3

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.

1 Comment

Works like a charm! Don't forget to add from __future__ import print_function at the top of your program.
1

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

1 Comment

This is a bit complex for this problem but it sounds like a clever idea. +1
0

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.

4 Comments

But he's not replacing a function call with another function call. He's replacing a statement with a function call. Not saying IDEs can't be smart enough to do this, but what you wrote doesn't make it clear to me you understand the question.
@JohnY print is just a function with fancy syntax. In Python 3 you have to actually call it as a function to get rid of that inconsistency.
Although it does appear that pycharm at least doesn't allow you to refactor it as it identifies it as a statement, as you said.
Yes, from a computer science standpoint, it's a function. But from a language-specific implementation standpoint, it's really not. For example, you can end the print statement's argument list with a comma to suppress the newline. That is completely outside the realm of Python's function syntax.
0

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))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.