How to emulate text justification in monospace font? You will be given a single-lined text and the expected justification width. The longest word will never be greater than this width.
Here are the rules:
Use spaces to fill in the gaps between words.
Each line should contain as many words as possible.
Use '\n' to separate lines.
Gap between words can't differ by more than one space.
Lines should end with a word not a space.
'\n' is not included in the length of a line.
Large gaps go first, then smaller ones ('Lorem--ipsum--dolor--sit-amet,' (2, 2, 2, 1 spaces)).
Last line should not be justified, use only one space between words.
Last line should not contain '\n'
Strings with one word do not need gaps ('somelongword\n').
Example with width=30:
Also you can always take a look at how justification works in your text editor or directly in HTML (css: text-align: justify).
#python #codewars
Here are the rules:
Use spaces to fill in the gaps between words.
Each line should contain as many words as possible.
Use '\n' to separate lines.
Gap between words can't differ by more than one space.
Lines should end with a word not a space.
'\n' is not included in the length of a line.
Large gaps go first, then smaller ones ('Lorem--ipsum--dolor--sit-amet,' (2, 2, 2, 1 spaces)).
Last line should not be justified, use only one space between words.
Last line should not contain '\n'
Strings with one word do not need gaps ('somelongword\n').
Example with width=30:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Vestibulum sagittis dolor
mauris, at elementum ligula
tempor eget. In quis rhoncus
nunc, at aliquet orci. Fusce
at dolor sit amet felis
suscipit tristique. Nam a
imperdiet tellus. Nulla eu
vestibulum urna. Vivamus
tincidunt suscipit enim, nec
ultrices nisi volutpat ac.
Maecenas sit amet lacinia
arcu, non dictum justo. Donec
sed quam vel risus faucibus
euismod. Suspendisse rhoncus
rhoncus felis at fermentum.
Donec lorem magna, ultricies a
nunc sit amet, blandit
fringilla nunc. In vestibulum
velit ac felis rhoncus
pellentesque. Mauris at tellus
enim. Aliquam eleifend tempus
dapibus. Pellentesque commodo,
nisi sit amet hendrerit
fringilla, ante odio porta
lacus, ut elementum justo
nulla et dolor.
Also you can always take a look at how justification works in your text editor or directly in HTML (css: text-align: justify).
#python #codewars
Tech C**P
marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes. - https://pypi.org/project/marshmallow/ #python #marshmallow #ORM #ODM
Object serialization deserialization ORM/ODM documentation
https://marshmallow.readthedocs.io/en/2.x-line/quickstart.html#serializing-objects-dumping
#python #marshmallow #ORM #ODM #readthedocs
https://marshmallow.readthedocs.io/en/2.x-line/quickstart.html#serializing-objects-dumping
#python #marshmallow #ORM #ODM #readthedocs
https://www.youtube.com/watch?v=XMu0T6L2KRQ&list=PLEsfXFp6DpzTOcOVdZF-th7BS_GYGguAS
#python #django #rest
#python #django #rest
YouTube
Blog API with Django Rest Framework 1 of 33 - Welcome
Blog API with Django Rest Framework 1 of 33 - Welcome
** Blog API with Django Rest Framework ** is a project to build a RESTful API service for the blog we created in Advancing the Blog and Try Django 1.9.
RESTful API services allow our applications to…
** Blog API with Django Rest Framework ** is a project to build a RESTful API service for the blog we created in Advancing the Blog and Try Django 1.9.
RESTful API services allow our applications to…
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
Introduction to Python Microservices With Nameko
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