Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Did you know that python print command takes sep argument as a separator between string arguments?

print('ali', 'reza', sep=', ') # output: ali, reza


#python #print #sep #separator
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
If you want to implement "Python Capitalize" method in Go:

strings.Title(strings.ToLower("MYNAME")) // output: Myname


There is another method called ToTitle which is used for one character long (but to letters):

str := "dz"
fmt.Println(strings.ToTitle(str)) // output: Dz


#python #golang #go #ToLower #Title #strings #capitalize #ToTitle
How to add nested documents in Marshmallow python?

books = fields.List(fields.Dict(
keys=fields.String(validate=OneOf(('title', 'author', 'publication_date'))),
values=fields.String(required=True)))


#python #data_class #marshmallow #fields #list #OneOf
Run a specific unit test in pytest:

pytest tests/test_user.py -k 'TestClassName and test_method_name'


#python #unitest #pytest
In marshmallow you can have a schema field which can be filled with a method output. Let's see with an example:

class AuthorSchema(Schema):
id = fields.Int(dump_only=True)
first = fields.Str()
last = fields.Str()
formatted_name = fields.Method("format_name", dump_only=True)

def format_name(self, author):
return "{}, {}".format(author.last, author.first)


As you see formatted_name field is filled by a method called format_name. Examples are endless, you can calculate average score based on the given scores for instance.

#python #marshmallow #fields #fields_method
In order to expand a CIDR in python you can use ipaddress module as below:

import ipaddress
available_ips = [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')]


#python #ipaddress #ipv4 #IPv4Network