Is there a way to create ObjectID from an INT in
#mongodb #objectid #pymongo #python #bson #int
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
In the above code 2 queries are issued in DB side. First it gets Entry record and then blog is fetched from DB when
You can follow foreign keys in a similar way to querying them. If you have the following models:
Then a call to
#python #django #select_related #join #database #models
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
https://www.toptal.com/python/introduction-python-microservices-nameko
#nameko #microservice #rabbitmq #python #docker #greenlet
#nameko #microservice #rabbitmq #python #docker #greenlet
Toptal Engineering Blog
Introduction to Python Microservices with Nameko | Toptal®
We will focus on building a proof of concept microservices application using Python. For that, we will use Nameko, a Python microservices framework. It has RPC over AMQP built in, allowing for you to easily communicate between your services.
One of the methods to document your API is
To read more about the
- http://apidocjs.com/
#javascript #js #python #apidoc #apidocjs #api #documentation #rest #annotation
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
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
You can see that both have the same log content but it's hard to follow
The formatter is as below:
Now the output will something like below:
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
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
What is the difference between following 2
And:
Read about it here:
- https://stackoverflow.com/questions/18982610/difference-between-except-and-except-exception-as-e-in-python
#python #try #except
try excepts
?try:
pass
except:
pass
And:
try:
pass
except Exception:
pass
Read about it here:
- https://stackoverflow.com/questions/18982610/difference-between-except-and-except-exception-as-e-in-python
#python #try #except
Stack Overflow
Difference between except: and except Exception as e: in Python
Both the following snippets of code do the same thing. They catch every exception and execute the code in the except: block
Snippet 1 -
try:
#some code that may throw an exception
except:
#
Snippet 1 -
try:
#some code that may throw an exception
except:
#
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
https://hackernoon.com/python-3-7s-new-builtin-breakpoint-a-quick-tour-4f1aebc444c
#python #python37 #breakpoint #pdb #set_trace
#python #python37 #breakpoint #pdb #set_trace
Hackernoon
Python 3.7’s new builtin breakpoint — a quick tour | HackerNoon
Debugging in Python has always felt a bit “awkward” compared with other languages I’ve worked in.
Did you know that python
#python #print #sep #separator
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
In golang you need to use
#python #golang #go #fmt #sprintf #format
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
There is another method called ToTitle which is used for one character long (but to letters):
#python #golang #go #ToLower #Title #strings #capitalize #ToTitle
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
#python #data_class #marshmallow #fields #list #OneOf
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
In
#python #marshmallow #fields #fields_method
marshmallow
you can have a schema field which can be filled with a method output. Let's see with an example:class AuthorSchema(Schema):As you see
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)
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
#python #ipaddress #ipv4 #IPv4Network
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