Search

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

20 April, 2021

Jupyter notebook and IPython first steps.

In this post, I am going to go through some initial steps for people who are looking to try out Jupyter Notebooks (JN). We will see how to:

    • Use a venv in our Jupyter notebook to work on projects.
    • Shortcuts for working with JN.
    • Basic ways of using new python shortcuts provided in IPython.

Que.  How to install IPython inside a virtualenv and use it?

Ans: These are a few ways to launch the python installed in your venv

- venv/bin/ipython

- python -m IPython

12 June, 2019

Python unittest and exceptions: Writing custom exceptions, and catching them.


Generally in unit test, we match the responses with an expected value . But there are cases , where calling a method or passing an incorrect value (or type of value) may raise an exception . This exception might me a custom exception . Or the message or error code might me specific to the situation. So how to write test for the same.

1. Calling a method with incorrect type , raises an exception.


class MyTest(unittest.TestCase):
    def test_exception(self):
         with self.assertRaises(xmlrpclib.Fault) as cm:
            self.Proxy.setParameter('name', 1234)
            self.fail('Expected exception of type xmlrpc.Fault')


31 May, 2019

Send email with excel file as attachment

We want to send a mail which will have some content in it's body and an attachment of excel file. The excel file is not physically saved on anywhere. We have just created it, filled it and and now , we want to send it in an email.

So what we really have as an attachment is a workbook object.

Let's get started.


    from io import BytesIO
    output = BytesIO()
    attachment.save(output)
    msg = EmailMultiAlternatives(subject, "", from_email, to_email, cc=cc, bcc=bcc)
    msg.attach('my_file.xlsx', output.getvalue(), 'application/vnd.ms-excel')
    msg.attach_alternative(content, "text/html")
    try:
        msg.send()
        success = True
    except socket.gaierror:
        success = False
    return success



10 May, 2019

Django: Creating PDF , writing content and returning as a downloaded file

Hi guys,

It took me hours to make it finally work but working with PDFs is a pain. I want to share what worked for me.

The canvas should be thought of as a sheet of white paper with points on the sheet identified using Cartesian (X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page.


from reportlab.pdfgen import canvas
import io

pdf_buffer = io.BytesIO()
p = canvas.Canvas(pdf_buffer)
p.drawString(0, 100, "Hello People")
p.showPage()
p.save()

response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="some_file.pdf"'
pdf_value = pdf_buffer.getvalue()
response.write(pdf_value)
return response

Above code , generates a pdf with the words "Hello People" written in it. 

15 April, 2019

PyCharms Handy short cuts

Hi friends. Today we are going to see some extremely useful (and must know) shortcuts for PyCharms .

Moving to last working point:


Ctrl+Shift+Backspace

04 April, 2019

How and when to use *args and **kwargs in Python

args and kwargs are special arguments (the words itself are just convention) which can be passed to a function.

13 March, 2019

Python Special Methods : __call__

Python has some methods that are special in a way . If  these methods are created inside a class, they can bring in some unique behaviors . We will talk about the method '__call__' today.

04 March, 2019

21 December, 2018

Python Global Variables

The keyword "global" has a special purpose . If you look into the help docstring of this keyword, you must notice this line :

 It would be impossible to assign to a global variable without "global " 

The word to note is "Assign"

Now . In python , if you declare a variable outside the scope of all functions or classes, it is AVAILABLE GLOBALLY. Meaning I can use it anywhere anytime.

But what is NOT clear , is , you CANNOT change the value\contents of this variable.

Bringing statefulness into your program (e.x an integrating counter ), might need you to keep updating some variable's value . That means, functions must be able to change that value.

Hence, the keyword "global" .

31 October, 2018

Migrating your Django Project database from SQlite3 to MySQL

Django comes with an in-build default setup for SQLLite3 which is good for learning and minor static page based websites. Like blogs which is a standard example . But as soon you decide to create projects that might entail multiple users performing different operations at the same time, SQLLite is not recommended.

Hence the decision to migrate to a better DB system must be as early as possible. I had to under go a similar experience recently . It look a lot of time, trials to finally get it working . I am going to share you the complete steps assuming you don't even have a MySQL setup .

22 October, 2018

Quick guide to Logging to a file using Python

This is for setting up of a file logging system using Logging module


Step 1: Import the logging module.



import logging


Step 2: Create a logger for yourself. 


logger = logging.getLogger(__name__)


19 September, 2018

Working with context processors and WITH statement

We use. We forget. This is for myself so I remember the magic called Context Manager. You already must have used the "with statement" for file handling, threads, socket and other operations. I am going to talk about creating you OWN context managers.

The magic operator to be used is : @contextmanager. It can be imported from the module "contextlib"

To create a context manager, use the decorator contextlib.contextmanager


@contextlib.contextmanager
def getListCounts(x):
    try:
        from collections import Counter
        yield Counter(x)
    finally:
        print 'No of elements in the list : %d' % len(x)

11 September, 2018

Python: Running executables using subprocess module

There are many ways to run a bat/exe file but the most widely used is the subprocess module.
And I would like to clearly show, how exactly to do that. When should you use what arguments.
I am going to specifically talk about the check_output module .

A better example of Class Inheritance in Python

Lots of people ask me, why should they use classes at all ? Well. I think its about control, and ease . It solves the headache of data sharing and accessibility. I am going to show you an example , which hopefully proves that classes and fundamentals like inheritance  are brilliant.

29 August, 2018

Python: Saving regular expression results in variables directly

Python Regular expressions has a powerful tools under it's regular expressions module which lets you create variables and save your search results directly.

It is called named groups . When you search for something and you want to save it in a variable when found, this is what you should use.

24 August, 2018

Regular Expressions: Look ahead and Look behind

Regular expressions are fun. There are two important concepts that are quite often sought for. Find something based on content that follows our search pattern . OR. Find something based on content that lies ahead. Let's see some examples to see what I mean.

LOOK AHEAD

LOOK AHEAD means , I want to see what lies ahead first, and then decide, to do something now.


Syntax of regex pattern:  pattern1(?=pattern2)


14 August, 2018

Calling Flask URL from a script tag

The goal is , to call a FLASK route without reloading the page, when a button is pressed. The content of a div must change with the response text received from the flask route call.

13 August, 2018

How to use Python unit test module and it's methods?

Unit test module is an in-build library in python, used to write test scripts . It gives some helpful methods while helps us designing out test in a way , that emulates a real production environment.

While writing test, we need to keep these things in mind

1. Test should start with a clean slate. Meaning the subject under test must not hold any info or be in a state, that might affect the test results.

2. Every tests should test one and only one feature . Clubbing multiple tests in one test,  is a bad idea.

Unit test provides some special methods. These can be used to handle the flow of tests as per scenario. Below is a generic template of a unit test showing all methods and their tasks.

16 July, 2018

Generator functions. Why and when should we use them.

You probably have heard of them. But as a beginner python programmer, we tend to ignore it's values because we fail to find uses of it. When we have loops and itertools and other fancy toys offered, why should we bother to understand them. If you want to take your programming  or understanding of Python to the next level, you must understand and use generators more and more.

25 June, 2018

How to test POST call using Python?


I got into a situation recently where I needed to test if my POST function was indeed working. 
    Its easy to test GET method . You just visit the URL and that's it . But for POST, you an use Plugins. But unfortunately I can't do that , because my browser (admin restrictions ) doesn't allow me to install plugins . Fortunately if you have Python , you can easily do that .