Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Heading to Zoodroom, wish me luck 😍
#job #python
Is there a way to create ObjectID from an INT in MongoDB?

import bson

def object_id_from_int(n):
s = str(n)
s = '0' * (24 - len(s)) + s
return bson.ObjectId(s)

def int_from_object_id(obj):
return int(str(obj))

n = 12345
obj = object_id_from_int(n)
n = int_from_object_id(obj)
print(repr(obj)) # ObjectId('000000000000000000012345')
print(n) # 12345


#mongodb #objectid #pymongo #python #bson #int
What does select_related do in Django?

select_related does a join in case needed on the DB side and reduce query counts. Let's look at an example:

# Hits the database.
e = Entry.objects.get(id=5)

# Hits the database again to get the related Blog object.
b = e.blog


In the above code 2 queries are issued in DB side. First it gets Entry record and then blog is fetched from DB when e.blog is called. And here’s select_related lookup:

# Hits the database.
e = Entry.objects.select_related('blog').get(id=5)

# Doesn't hit the database, because e.blog has been prepopulated
# in the previous query.
b = e.blog


You can follow foreign keys in a similar way to querying them. If you have the following models:

from django.db import models

class City(models.Model):
# ...
pass

class Person(models.Model):
# ...
hometown = models.ForeignKey(
City,
on_delete=models.SET_NULL,
blank=True,
null=True,
)

class Book(models.Model):
# ...
author = models.ForeignKey(Person, on_delete=models.CASCADE)


Then a call to Book.objects.select_related('author__hometown').get(id=4) will cache the related Person and the related City:

# Hits the database with joins to the author and hometown tables.
b = Book.objects.select_related('author__hometown').get(id=4)
p = b.author # Doesn't hit the database.
c = p.hometown # Doesn't hit the database.

# Without select_related()...
b = Book.objects.get(id=4) # Hits the database.
p = b.author # Hits the database.
c = p.hometown # Hits the database.


#python #django #select_related #join #database #models
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