Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
format() is way better than % in many different ways. One of those is:
>>> names = ['ali', 'reza', 'alireza', 'mohsen']
>>> print map('Hello Mr. {}'.format, names)
['Hello Mr. ali', 'Hello Mr. reza', 'Hello Mr. alireza', 'Hello Mr. mohsen']

#python #format #string_formatter #map
How much do you know about python traceback? Here we will dive deep into this concept.

What is a traceback?
This module provides a standard interface to extract, format and print stack traces of Python programs.

It acts a lot like a python interpreter when it print a stack trace. traceback module has many functions, we will review some of them here.

traceback.print_exc():
Print exception information and up to limit stack trace entries from the traceback tb to file. This is a shorthand for print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file).

traceback.format_exc():
This is like print_exc(limit) but returns a string instead of printing to a file.

A sample traceback usage derived from python documentation:
import sys, traceback

def run_user_code(envdir):
source = raw_input(">>> ")
try:
exec source in envdir
except:
print "Exception in user code:"
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60

envdir = {}
while 1:
run_user_code(envdir)

#python #traceback #exception #format_exc #print_exc
Thousand separator using format in python:

your_number = 35200000
print '{:,}'.format(your_number)

#python #format #thousand_separator
The simplest way to generate hour, minute, seconds from seconds in python is divmod:
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "{}:{}:{}".format(h, m, s)

#python #divmod #format
As you may already know in Python you can format your string using format as below:

file_name = "/root/openvpn/{}.ovpn".format(my_file_name)
// Or
file_name = "/root/openvpn/%s.ovpn" % my_file_name



In golang you need to use Sprintf method of fmt package like follow:

var fileName = fmt.Sprintf("/root/openvpn/%s.ovpn", myFileName)


#python #golang #go #fmt #sprintf #format