Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
One of the methods to document your API is APIDOC. It uses annotation in variety of languages like Python, PHP, JS, etc. At the below code block you can see an example in Python:

"""
@api {get} /user/:id Request User information
@apiName GetUser
@apiGroup User

@apiParam {Number} id Users unique ID.

@apiSuccess {String} firstname Firstname of the User.
@apiSuccess {String} lastname Lastname of the User.
"""


To read more about the APIDOC itself and the installation process head over to link below:

- http://apidocjs.com/

#javascript #js #python #apidoc #apidocjs #api #documentation #rest #annotation
DO NOT USE UWSGI multi-threaded mode with alpine image!

I've been stuck on this issue for a couple of days as our service returned 503 Gateway timeout while our server load was totally ok around 1.0 (1m load average). So our load test got failed at the be beginning of the test! We found out it is related to docker base image of python alpine. Use python slim image instead. Or in case you have many changes you can stick with alpine and change thread to 1 in uswgi configuration file.


#docker #alpine #uwsgi #python #slim #respawn
Do you log a lot like me in your Python modules? If so, you had the same problem to always find the first occurence of a log after time, filename, etc. Let's clarify this with a sample log:

[2012-10-02 application.py:1 _get()] DEBUG: this is a log content
[2012-10-02 db.py:1005 _fetch_all_info()] INFO: this is a log content


You can see that both have the same log content but it's hard to follow cause of length of file name, line number and function name. To format this better we can have space padding in formatters. spaces are identified by `s. Now lets see the same log, but this time with space padding.

The formatter is as below:

[%(asctime)s %(filename)15s:%(lineno)4s %(funcName)20s()] %(levelname)s %(message)s


NOTE: this is not the exact formatter for the above log, it is for demonstration!


Now the output will something like below:

[2012-10-02   application.py:    1               _get()] DEBUG: this is a log content
[2012-10-02 db.py: 1005 _fetch_all_info()] DEBUG: this is a log content


You can see that log content is so much easier to follow by using space padding. It may not be obvious on telegram with small devices. So try it your self :)))

#python #logging #log #logger #formatter #log_formatter #space_padding #padding
tuples vs list from a different point of view. Tuples of constants can be precomputed by Python's peephole optimizer or AST-optimizer. Lists, on the other hand, get built-up from scratch:

>>> from dis import dis 

>>> dis(compile("(10, 'abc')", '', 'eval'))
1 0 LOAD_CONST 2 ((10, 'abc'))
3 RETURN_VALUE

>>> dis(compile("[10, 'abc']", '', 'eval'))
1 0 LOAD_CONST 0 (10)
3 LOAD_CONST 1 ('abc')
6 BUILD_LIST 2
9 RETURN_VALUE


#python #list #tuple #performance #dis #compile #ast_optimizer
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