2

I want to create wrapper over print function or any logging function so that if internally I need to change logging/print strategy it should work without changing in all module.For ex:

print "Hello this is test A:%d B:%d C:%d",a,b,c

instead of this it should look like:

MyPrint "Hello this is test A:%d B:%d C:%d",a,b,c 

which will give me same output.

This gives me flexiblity of changing definition of MyPrint without affecting print in whole code.

In C++ this can be acheived through macro but not sure how we can achieve in python.

3
  • Isn't this more or less what logging is for? Minus the syntax of the print statement. Commented Feb 10, 2018 at 11:02
  • You could also later replace sys.stdout (where print writes to) with a different object. Commented Feb 10, 2018 at 11:04
  • refer to @Omar's answer Commented Feb 10, 2018 at 11:52

2 Answers 2

3

Doing something like this should do:

def my_print(message, *args):
    print(message % args)

Then you could call it like this:

my_print("Hello this is test A:%d B:%d C:%d", 1, 2, 3)

# Output: Hello this is test A:1 B:2 C:3
Sign up to request clarification or add additional context in comments.

5 Comments

Why not just forward the parameters as they are and call print(*args, **kwargs) from a my_print(*args, **kwargs)?
It would leave the string formatting to the called print function (not the print statement, though).
Not sure if I tested the code as you suggested, but the test outputs Hello this is test A:%d B:%d C:%d 1 2 3
@OmarEinea yes this is output that I was getting.
@OmarEinea with ndim suggestion
2

Looks like you should be using logging with a custom format

eg. as this answer shows

# create formatter
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s",
                              "%Y-%m-%d %H:%M:%S")

4 Comments

no this is not what I was looking for, lets say I am using this logger, with some syntax say logging.error("Content") but in future if I need to change logger then I have to change all the logging.error() lines , so to overcome this if I have my logger("Content ") than I wont need to change code I need to change logger() handling logic.
@AnonymousDev I don't get how this doesn't solve your problem? Your example was a bit unclear to me
lets say I am using logger of x package but in future I need to change to logger of some y package , so I need to create a wrapper that will handle logging and since in code will be using my wrapper apis so there wont be change if I need to change logger of x to y package. Refer to ans that might clear your doubt.
@AnonymousDev Everyone just uses the python logging module. If they were to use another logging module, theyd just use that from the start. Nobody really switches between them

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.