sandeepk

python

select_for_update is the answer if you want to acquire a lock on the row. The lock is only released after the transaction is completed. This is similar to the Select for update statement in the SQL query.

>>> Dealership.objects.select_for_update().get(pk='iamid')
>>> # Here lock is only required on Dealership object
>>> Dealership.objects.select_related('oem').select_for_update(of=('self',))

select_for_update have these four arguments with these default value – nowait=False – skiplocked=False – of=() – nokey=False

Let's see what these all arguments mean

nowait

Think of the scenario where the lock is already acquired by another query, in this case, you want your query to wait or raise an error, This behavior can be controlled by nowait, If nowait=True we will raise the DatabaseError otherwise it will wait for the lock to be released.

skip_locked

As somewhat name implies, it helps to decide whether to consider a locked row in the evaluated query. If the skip_locked=true locked rows will not be considered.

nowait and skip_locked are mutually exclusive using both together will raise ValueError

of

In select_for_update when the query is evaluated, the lock is also acquired on the select related rows as in the query. If one doesn't wish the same, they can use of where they can specify fields to acquire a lock on

>>> Dealership.objects.select_related('oem').select_for_update(of=('self',))
# Just be sure we don't have any nullable relation with OEM

no_key

This helps you to create a weak lock. This means the other query can create new rows which refer to the locked rows (any reference relationship).

Few more important points to keep in mind select_for_update doesn't allow nullable relations, you have to explicitly exclude these nullable conditions. In auto-commit mode, select_for_update fails with error TransactionManagementError you have to add code in a transaction explicitly. I have struggled around these points :).

Here is all about select_for_update which you require to know to use in your code and to do changes to your database.

Cheers!

#Python #Django #ORM #Database

Today, we are going to see how we can use | operator in our python code to achieve clean code.

Here is the code where we have used map and filter for a specific operation.

In [1]: arr = [11, 12, 14, 15, 18]
In [2]: list(map(lambda x: x * 2, filter(lambda x: x%2 ==1, arr)))
Out[2]: [22, 30]

The same code with Pipes.

In [1]: from pipe import select, where
In [2]: arr = [11, 12, 14, 15, 18]
In [3]: list(arr | where (lambda x: x%2 ==1) | select(lambda x:x *2))
Out[3]: [22, 30]

Pipes passes the result of one function to another function, have inbuilt pipes method like select, where, tee, traverse.

Install Pipe

>> pip install pipe

traverse

Recursively unfold iterable:

In [12]: arr = [[1,2,3], [3,4,[56]]]
In [13]: list(arr | traverse)
Out[13]: [1, 2, 3, 3, 4, 56]

select()

An alias for map().

In [1]: arr = [11, 12, 14, 15, 18]
In [2]: list(filter(lambda x: x%2 ==1, arr))
Out[2]: [11, 15]

where()

Only yields the matching items of the given iterable:

In [1]: arr = [11, 12, 14, 15, 18]
In [2]: list(arr | where(lambda x: x % 2 == 0))
Out[2]: [12, 14, 18]

sort()

Like Python's built-in “sorted” primitive. Allows cmp (Python 2.x only), key, and reverse arguments. By default, sorts using the identity function as the key.

In [1]:  ''.join("python" | sort)
Out[1]:  'hnopty'

reverse

Like Python's built-in “reversed” primitive.

In [1]:  list([1, 2, 3] | reverse)
Out[1]:   [3, 2, 1]

strip

Like Python's strip-method for str.

In [1]:  '  abc   ' | strip
Out[1]:  'abc'

That's all for today, In this blog you have seen how to install the Pipe and use the Pipe to write clean and short code using inbuilt pipes, you can check more over here

Cheers!

#100DaysToOffload #Python #DGPLUG

My work required me to profile one of our Django applications, to help identify the point in the application which we can improve to reach our North Star. So, I thought it will be great to share my learning and tools, that I have used to get the job done.

What is Profiling?

Profiling is a measure of the time or memory consumption of the running program. This data further can be used for program optimization.

They are many tools/libraries out there which can be used to do the job. I have found these helpful.

Apache JMeter

It is open-source software, which is great to load and performance test web applications. It's easy to set up and can be configured for what we want in the result report.

Pyinstrument

Pyinstrument is a Python profiler that offers a Django middleware to record the profiling. The profiler generates profile data for every request. The PYINSTRUMENTPROFILEDIR contains a directory that stores the profile data, which is in HTML format. You can check how it works over here

Django Query Count

Django Query Count is a middleware that prints the number of database queries made during the request processing. There are two possible settings for this, which can be found here

Django Silk

Django Silk is middleware for intercepting Requests/Responses. We can profile a block of code or functions, either manually or dynamically. It also has a user interface for inspection and visualization

So, here are some of the tools which can be of great help in profiling your code and putting your effort in the right direction of optimization applications.

Cheers!

#100DaysToOffload #Python #DGPLUG

Today we are going to talk about the modes of image. The mode of image defines the depth and type of the pixel. These string values help you understand different information about the image. As of this writing, we have 11 standard modes.

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)

  • L (8-bit pixels, black and white)

  • P (8-bit pixels, mapped to any other mode using a color palette)

  • RGB (3x8-bit pixels, true color)

  • RGBA (4x8-bit pixels, true color with transparency mask)

  • CMYK (4x8-bit pixels, color separation)

  • YCbCr (3x8-bit pixels, color video format)

  • LAB (3x8-bit pixels, the Lab color space)

  • HSV (3x8-bit pixels, Hue, Saturation, Value color space)

  • I (32-bit signed integer pixels)

  • F (32-bit floating point pixels)

So, 1-bit pixel range from 0-1 and 8-bit pixel range from 0-255. The common modes are RGB, RGBA, P mode. Image also consist of band, common band like RGB for red, green, blue also have an Alpha(A) transparency, mainly for PNG image.

We can also change these modes with the help of convert or creating new image with the help of pillow library. Let see the code for example

# here we convert RGBA image to RGB image and painting the Alpha 
# trasparency band to white color
image = Image.open("path/to/image")
image.mode # this will tell image mode
new_image = Image.new("RGB", image.size, (255, 255, 255))
new_image.paste(image, mask=image.split()[3])

That's all about the modes. There is also raw modes where you can create even your modes, but will save that for another blog post :)

Cheers!

#100DaysToOffload #Python #Pillow

A data class is a class containing data only, from Python3.7 we can define a data class with the help of decorator @dataclass, which build the class with the basic functionality like __init__ , __repr__, __eq__ and more special methods.

Let see how to define your data class with the decorator @dataclass

from dataclasses import dataclass

@dataclass
class Batch:
    sku: int
    name: str
    qty: int

>>> Batch(1, 'desk', 100)
Batch(sku=1, name='desk', qty=100)

We can also add the default value to the data class, which works exactly as if we do in the __init__ method of regular class. As you have noticed in the above we have defined the fields with type hints, which is kind of mandatory thing in the data class, if you do not do it will not take the field in your data class.

@dataclass
class Batch:
    sku: int = 1
    name: str = 'desk'
    qty: int = 100

# if you don't want to explicity type the fields you can use any
from typing import Any
class AnyBatch:
    sku: Any
    name: Any = 'desk'
    qty: Any = 100

If you want to define mutable default value in data class, it can be done with the help of default_factory and field. Field() is used to customize each field in data class, different parameter that can be passed to field are default_factory, compare, hash, init, you can check about them over here

from dataclasses import dataclass, field
from typing import List

@dataclass()
class Batch:
    sku: int
    name: str
    qty: int = 0
    creator: List[str] = field(default_factory=<function/mutable value>)

Immutable Data Class we can also define our data class as immutable by setting frozen=True, which basically means we cannot assign value to the fields after creation

@dataclass(frozen=True)
class Batch:
    sku: int
    name: str
    qty: int = 0

>>> b = Batch(12, 'desk', 100)
>>> b.qty 
100
>>> b.qty = 90
dataclasses.FrozenInstanceError: cannot assign to field 'qty'

Data class saves us from writing boilerplate code, help us to focus on logic, this new feature of Python3.7 is great, so what waiting for go and right some data classes.

Cheers!

#100DaysToOffload #Python #DataClass #TIL #DGPLUG

This Sunday, I attended the workshop on Creating Python modules using Rust by Kushal Das. The workshop started at 0900 UTC 18th April on Twitch.

The GitHub repo we follow along with the workshop can be found over here. Learned many new things in the workshop where we go through

  • Hello World example
  • User passed arguments
  • Exception Handling
  • File I/O
  • System Interaction (Process, System Information)

Enjoyed my time while going through these examples and like the way Kushal has arranged all the examples branch wise.

Cheers!

Useful Links – https://github.com/kushaldas/workshops/issues/2https://kushaldas.in/

#100DaysToOffload #Python #Rust

Today we are gone to talk about the Foreign Key which is used to establish a many-to-one relationship and different args which can be passed.

Syntax to define a Foreign key relationship

from django.db import models

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

class PurchaseOrder(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE)

Different args for the Foreign Key

  • on_delete
  • limitchoicesto
  • related_name
  • relatedqueryname
  • to_field
  • db_constraints

on_delete This args handle the process, of what to do if the reference keys are deleted, we have three options – CASCADE: It deletes the objects contains the Foreign key – PROTECT: It prevents the deletion of the referenced object by raising ProtectedError – RESTRICT: Introduced in Django 3.1, It only deletes the object, if another references object is being deleted in the same operation, but with CASCADE flow otherwise raise RestrictedError

from django.db import models

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

class PurchaseOrder(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE)

limit_choices_to This is helpful in the form rendering to restrict the option for the reference field. The limited choice can either be a dict, Q object or a callable that return dict or Q object

from django.db import models

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

class PurchaseOrder(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE, limit_choices_to={'tax_type': 'gst'},)
# here we only show the tax of type GST only.

related_name It's the name that can be used for the reverse or backward reference while querying. You can also stop the backward relation by assigning + in related_name.


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

class PurchaseOrder(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE, related_name='+',) # IN this case GSTRate cannot backward realte to the Purchase Order obejct.

class PurchaseOrder(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE, related_name='invoice')

# here you can refer to Invoice inside GSTRate query by the related name.
GSTRate.objects.filter(invoice_id=1232)

related_query_name It has the same value as the related_name, unless not specified explicitly. If it is defined then you have to use that name while querying


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

class OrderConfirmation(models.Model):
    gst = models.ForeignKey(GSTRate, on_delete=models.CASCADE, related_name='order_confirmation', related_query_name='oc')

# here you can refer to Invoice inside GSTRate query by the related name.
GSTRate.objects.filter(oc_id=112)

to_field This is helpful in the case where you want to refer other the primary key of the modal, but the condition whatever key you referred it should be unique=True

db_constraints This is used to create a constraint at the database level, by default value is true and if the user sets the value to False, then accessing a related object that doesn’t exist will raise its DoesNotExist exception.

Conclusion These args are handy which gives us the possibilities of implementing the additional constraint and functionality to query data in the Django ORM query language.

Cheers!

#100DaysToOffload #Python#Django

Template is the one of powerful features of Django Framework, and we will discuss the tag and filter. If you do not have any idea about the template, please go read about that here, I will wait for you.

Template tags which we are going to discuss today are

  • URL
  • Now
  • Spaceless

URL The URL tag is the template equivalent of the reverse function. URL can accept args or kwargs for routes that expect other variables.

<a href="{% url "a_named_view" %}">Go to a named view</a>

Now Now is a convenient method to display information about the current time. It was a problem solver for the copyright year update, which I first have to update every year manually.

&copy; {% now "Y" %} sandeepk.dev.

Spaceless It removes the whitespace, newline, tab from the HTML

{% spaceless %}
    <div>
        <span>I am a good span.</span>
    </div>
{% endspaceless %}

Now let's talk about the filter which we can use in the template.

  • date
  • default
  • pluralize
  • yesno

Date This filter can be used to control the format of the datetime passed in the context of the response.

{{ birth_date|date:"Y-m-d" }}

Default Have you ever been in a condition when you want to show a text for a falsy value, then default filter is the one thing you should use

{{ value_to_check|default:"Nothing to see here." }}

Pluralize When your text considers counts of things. Pluralize can use to save you from if and else check

{ count_items }} item{{ count_items|pluralize }}

0 items
1 item
2 items
3 items
(and so on)

YesNo YesNo is good for converting True|False|None into a meaningful text message

{{ user.name }} has {{ user_accepted|yesno:"accepted,declined,not RSVPed" }} request.

These are the tags and filter which I use very often but there many more which you can check out here

Cheers!

#100DaysToOffload #Python #Django

In Python, we can define private variables and methods. These variables and methods cannot be accessed out of the class or by the inherited class according to the Object-Oriented Paradigm. Things are a bit different in Python.

In python, we can achieve this by using Name Mangling which means adding __ before the variable or method name, let see how

class PPrint:
    def __init__(self):
        self.__secret_msg = 'I am alive' # This is a private variable
        self.check = False

    def __private_method(self): # This is a private method
        print('I am private method !')
                
    def public_method(self): # This public method, which can access the private method
        self.__private_method()
        
    def public_print(self):
        print(self.__secret_msg)
        print(self.check)
 

In the above class, we define the private methods and variables. Let see how to use them


>>> p = PPrint()
>>> p.public_print()
I am alive
False
>>> p.public_method()
I am private method !
# so far so good, let see what happends when we try to access these private methods or variables.
>>> p.__private_msg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PPrint' object has no attribute '__private_msg'
>>> p.__private_method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PPrint' object has no attribute '__private_method'

As you can see python does not allow access to these private variables and methods. But, If I say you can access these private variables and methods.

>>> dir(p)
['_PPrint__private_method', '_PPrint__secret_msg', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'check', 'public_method', 'public_print']

# You can see we have private method and variable '_PPrint__private_method', '_PPrint__secret_msg'

>>> p._PPrint__private_method()
I am private method !

>>> p._PPrint__secret_msg
'I am alive'

So, that's what Name Mangling do it add *<classname>_<method/variablename>.

Conclusion In Python, we can use the name mangling to define the private method and variables, but it not as strict as in other OOP languages. It's almost like a convention which is followed by all python developer. You may also found _ added before the method and variable name, which is just another way to say don't access these methods directly.

Cheers!

#100DaysToOffload #Python

Let get straight to the expression False == False in [True], what you think this expression evaluates to True or False. Fire up your terminal and try, if you get the answer that you have thought of, congratulations!. My guess was wrong :p. Let break down the expression to see how it is evaluated in Python.

# False == False in [True] 
>>> False == False
True
>>> False in [True]
False
>>> (False == False) in [True]
True
>>>False == (False in [True])
True
# but the weird part is when we run
>>> False == False in [True]
False

Isn't this a bit weird? But it is not so, When we look at the expression the way python interprets the expression, things start making sense.

>>> False == False and False in [True]
False

On a bit further looking. I parse the expression to get the Abstract Syntax Tree, which tell a lot about the expression evaluation.

>>> import ast, pprint
>>> pprint.pprint(ast.dump(ast.parse('False == False in [True]')), indent=4)
('Module(body=[Expr(value=Compare(left=Constant(value=False, kind=None), '
 'ops=[Eq(), In()], comparators=[Constant(value=False, kind=None), '
 'List(elts=[Constant(value=True, kind=None)], ctx=Load())]))], '
 'type_ignores=[])')

From the above output of AST you can see that [Eq(), In()] are compound operators and in python, the precedence of these operations is the same.

comparisons, including tests, which all have the same precedence chain from left to right

The below mentioned operators have the same precedence.

in, not in, is, is not, <, <=, >, >=, <>, !=, ==

So, when Python tries to evaluate the expression False == False in [True], it encounters the operators is and == which have the same precedence, so it performs chaining from left to right. – from stackoverflow[0]

# so above expression on evaluating from left to right is done like this.
>>> False == False and False in [True]

Cheers!

References – [0] https://stackoverflow.com/questions/31354429/why-is-true-is-false-false-false-in-python – [1] https://stackoverflow.com/questions/12658197/what-is-the-operator-precedence-when-writing-a-double-inequality-in-python-expl – [2] https://www.youtube.com/watch?v=mRPU3l54Z7I&list=PLWBKAf81pmOamJfoHz4oRdieWQysmUkaW

#100DaysToOffload #Python