What is __init__.py used for?

Explanation 1

The primary use of __init__.py is to initialize Python packages. The easiest way to demonstrate this is to take a look at the structure of a standard Python module.

package/
    __init__.py
    file.py
    file2.py
    file3.py
    subpackage/
        __init__.py
        submodule1.py
        submodule2.py

As you can see in the structure above the inclusion of the __init__.py file in a directory indicates to the Python interpreter that the directory should be treated like a Python package

What goes in __init__.py?

__init__.py can be an empty file but it is often used to perform setup needed for the package(import things, load things into path, etc).

One common thing to do in your __init__.py is to import selected Classes, functions, etc into the package level so they can be convieniently imported from the package.

In our example above we can say that file.py has the Class File. So without anything in our __init__.py you would import with this syntax:

from package.file import File

However you can import File into your __init__.py to make it available at the package level:

# in your __init__.py
from file import File

# now import File from package
from package import File

Another thing to do is at the package level make subpackages/modules available with the __all__ variable. When the interpeter sees an __all__ variable defined in an __init__.pyit imports the modules listed in the __all__ variable when you do:

from package import *

__all__ is a list containing the names of modules that you want to be imported with import * so looking at our above example again if we wanted to import the submodules in subpackage the __all__ variable in subpackage/__init__.py would be:

__all__ = ['submodule1', 'submodule2']

With the __all__ variable populated like that, when you perform

from subpackage import *

it would import submodule1 and submodule2.

Explanation 2

What is a Python package?

A Python package is simply an organized collection of python modules. A python module is simply a single python file.

Creating a package with __init__.py is all about making it easier to develop l

Why would I want to create a package using __init__.py?

arge Python projects. It provides an easy way for you to group large folders of many seperate python scripts into a single importable module.

Let’s run through some examples

The best way to understand why you would use __init__.pyand to learn how to use it to create a package is to run through some quick examples!

The code in this tutorial should work for Python 2 or 3. Just remember, if you are using 2 then you will need to use the from __future__ import print_function functionality.

Say we have three modules we have created:

someFolder
|-- stringLength.py
|-- stringToUpper.py
`-- stringToLower.py

(Remember a module is considered to be any single python file)

Let’s say the content of these files is the following:

# stringLength.py

def stringLength(inStr):
    return len(inStr)
# stringToUpper.py

def stringToUpper(inStr):
    return inStr.upper()
# stringToLower.py

def stringToLower(inStr):
    return inStr.lower()

Obviously, these functions are useless, but it helps to serve as a model for the basic concept that we have some python modules that we have already written that are somehow related.

So, without creating a package and using __init__.py, how do we use the functions in these files?

Well, we can only import these files if they are in the current directory that whatever script we are running is running from.

Well, we can use these files in a new Python script but with one key caveat:

  • The files must be in the same directory as the script we are trying to use them in.

To illustrate that, let’s create a file called example1.py that leverages our modules:

# example1.py

import stringLength
import stringToLower
import stringToUpper

some_string = "Hello, Universe!"

print(stringLength.stringLength(some_string))
print(stringToLower.stringToLower(some_string))
print(stringToUpper.stringToUpper(some_string))

Adding a blank __init__.py

What if we wanted to seperate these scripts into a folder in order to keep them more organized?

Well, that is where the __init__.py file comes into play.

First, lets move our scripts into a new subfolder and call it: string_func. Then create an empty file in that folder called __init__.py

Here is our new file/folder structure:

someFolder
|-- string_func
|   |-- __init__.py
|   |-- stringToUpper.py
|   |-- stringToLower.py
|   `-- strengthLength.py
`-- example1.py

So, now let’s test out exactly what __init__.py allows us to do:

Let’s make a new example2.py file.

# example2.py

import string_func.stringLength
import string_func.stringToLower
import string_func.stringToUpper

some_string = "Hello, Universe!"

print(string_func.stringLength.stringLength(some_string))
print(string_func.stringToLower.stringToLower(some_string))
print(string_func.stringToUpper.stringToUpper(some_string))

So, now we can access our string functions in this manner. This is great, because they are all in a seperate folder, but the syntax is definitely not very succinct. Let’s see if we can clean things up a bit by editing our __init__.py file.

Adding imports to init.py

Open your __init__.py file and make the following changes:

# __init__.py
from .stringLength import stringLength
from .stringToLower import stringToLower
from .stringToUpper import stringToUpper

# Explanation about the init import below
# from .stringLength(stringLength.py file) import stringLength function

Note that the . before the module name is neccessary as of Python 3 since it is more strict regarding relative imports: https://stackoverflow.com/questions/12172791/changes-in-import-statement-python3?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

And so with that in our __init__.py we can now shorten our code to:

# example3.py

import string_func

some_string = "Hello, Universe!"

print(string_func.stringLength(some_string))
print(string_func.stringToLower(some_string))
print(string_func.stringToUpper(some_string))

Now the syntax is a lot shorter and you can see that string_func is behaving like its own module.

So, that is basically what __init__.py does! It allows you to treat a directory as if it was a python module. Then you can further define imports inside your __init__.py file to make imports more succinct, or you can just leave the file blank.

For more information about python modules and packages you can see check the python documentation on it.

As you can see __init__.py can be very useful besides its primary function of indicating that a directory is a module. If you have any comments or questions, hit up the comments

 

 

Advertisements

Python cyclic error solution- Explained with example

What is a Circular Dependency?

A circular dependency occurs when two or more modules depend on each other. This is due to the fact that each module is defined in terms of the other (See Figure 1).

For example:

functionA():  
    functionB()

And

functionB():  
    functionA()

The code above depicts a fairly obvious circular dependency. functionA() calls functionB(), thus depending on it, and functionB() calls functionA(). This type of circular dependency has some obvious problems, which we’ll describe a bit further in the next section.

Figure 1

Problems with Circular Dependencies

Circular dependencies can cause quite a few problems in your code. For example, it may generate tight coupling between modules, and as a consequence, reduced code reusability. This fact also makes the code more difficult to maintain in the long run.

In addition, circular dependencies can be the source of potential failures, such as infinite recursions, memory leaks, and cascade effects. If you’re not careful and you have a circular dependency in your code, it can be very difficult to debug the many potential problems it causes.

What is a Circular Import?

Circular importing is a form of circular dependency that is created with the import statement in Python.

For example, let’s analyze the following code:

# module1
import module2

def function1():  
    module2.function2()

def function3():  
    print('Goodbye, World!')
# module2
import module1

def function2():  
    print('Hello, World!')
    module1.function3()
# __init__.py

import module1

module1.function1()  

When Python imports a module, it checks the module registry to see if the module was already imported. If the module was already registered, Python uses that existing object from cache. The module registry is a table of modules that have been initialized and indexed by module name. This table can be accessed through sys.modules.

If it was not registered, Python finds the module, initializes it if necessary, and executes it in the new module’s namespace.

In our example, when Python reaches import module2, it loads and executes it. However, module2 also calls for module1, which in turn defines function1().

The problem occurs when function2() tries to call module1’s function3(). Since module1 was loaded first, and in turn loaded module2 before it could reach function3(), that function isn’t yet defined and throws an error when called:

$ python __init__.py
Hello, World!  
Traceback (most recent call last):  
  File "__init__.py", line 3, in <module>
    module1.function1()
  File "/Users/scott/projects/sandbox/python/circular-dep-test/module1/__init__.py", line 5, in function1
    module2.function2()
  File "/Users/scott/projects/sandbox/python/circular-dep-test/module2/__init__.py", line 6, in function2
    module1.function3()
AttributeError: 'module' object has no attribute 'function3'  

How to Fix Circular Dependencies

In general, circular imports are the result of bad designs. A deeper analysis of the program could have concluded that the dependency isn’t actually required, or that the depended functionality can be moved to different modules that wouldn’t contain the circular reference.

A simple solution is that sometimes both modules can just be merged into a single, larger module. The resulting code from our example above would look something like this:

# module 1 & 2

def function1():  
    function2()

def function2():  
    print('Hello, World!')
    function3()

def function3():  
    print('Goodbye, World!')

function1()  

However, the merged module may have some unrelated functions (tight coupling) and could become very large if the two modules already have a lot code in them.

So if that doesn’t work, another solution could have been to defer the import of module2 to import it only when it is needed. This can be done by placing the import of module2 within the definition of function1():

# module 1

def function1():  
    import module2
    module2.function2()

def function3():  
    print('Goodbye, World!')

In this case, Python will be able to load all functions in module1 and then load module2 only when needed.

This approach doesn’t contradict Python syntax, as the Python documentation says: “It is customary but not required to place all import statements at the beginning of a module (or script, for that matter)”.

The Python documentation also says that it is advisable to use import X, instead of other statements, such as from module import *, or from module import a,b,c.

You may also see many code-bases using deferred importing even if there isn’t a circular dependency, which speeds up the startup time, so this is not considered bad practice at all (although it may be bad design, depending on your project).

Wrapping up

Circular imports are a specific case of circular references. Generally, they can be resolved with better code design. However, sometimes, the resulting design can contain a large amount of code, or mix unrelated functionalities (tight coupling).

Have you run in to circular imports in your own code? If so, how did you fix it? Let us know in the comments!

pubnub IoT Cloud – What is a channel and how to create?

Channels are how messages are sent and received. Clients that subscribe to a channel will receive messages that are published on that channel. Channels are very lightweight and flexible. Channels exist merely by using them. Just publish or subscribe to a channel name and nothing more. And, use as many channels as your app requires. There is no additional charge for the number of channels you use. But, you should use them in a way that makes the most sense for your use case.  And a channel is just a channel. What you use it for is completely up to your application’s requirements. For example, a channel might be used for 1 to 1 private chat, many to many group chat, 1 to many broadcasting, or many to 1 fan-in. And a channel that is being used for private chat can suddenly be used for group chat. It is just up to you to allow more users to start subscribing and publishing to that channel. So channels are just blank communication devices for which you determine the actual usage

There is no UI to "create" a channel. Channels are just strings.

# Code to create a new channel

// Subscribe to a channel

 pubnub.subscribe({
    channel: 'my_channel', message: function(m){console.log(m)} });

 

How do I access environment variables from Python?

Environment variables are accessed through os.environ

import os
print(os.environ['HOME'])

Or you can see a list of all the environment variables using:

os.environ

As sometimes you might need to see a complete list!

# using get will return `None` if a key is not present rather than raise a `KeyError`
print(os.environ.get('KEY_THAT_MIGHT_EXIST'))

# os.getenv is equivalent, and can also give a default value instead of `None`
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))

Python default installation on Windows is C:\Python. If you want to find out the running python on your system, try the below:

import sys
print(sys.prefix)

What is python-dev package used for?

python-dev contains the header files you need to build Python extensions. lxml is a Python C-API extension that is compiled when you do pip install lxml. The lxml sources have at least something like #include  in the code.  The compiler looks for the Python.h file during compilation, hence those files need to be on your system such that they can be found. On Linux typically, the binary libraries and header files of packages like python are separate. Hence you can have Python installed and everything works fine but when you want to build extensions you need to install the corresponding dev package.

python-dev is the package that contains the header files for the Python C API, which is used by lxml because it includes Python C extensions for high performance.

Github package link with installation steps:  https://gist.github.com/dwayne/e77f267d871244471e3b#file-install-md

Python callback function – quick understanding with simple example

A classic use of callbacks in Python (and other languages) is to assign events to UI elements.

Here is a very trivial example of the use of a callback in Python. First define two functions, the callback and the calling code, then pass the callback function into the calling code.

>>> def get_square(val):
...     """ the callback """
...     return val ** 2
...
>>> def caller(func, val):
...     return func(val)
...
>>> caller(get_square, 5)
25

What is an abscissa and ordinate?

An ordered pair consists of two terms—the abscissa (horizontal, usually x) and the ordinate (vertical, usually y)—which define the location of a point in two-dimensional rectangular space.

Is abscissa X or Y?

The distance of a point from the y-axis on a graph in the Cartesian coordinate system. It is measured parallel to the x-axis. For example, a point having coordinates (2,3) has 2 as its abscissa

Is the ordinate the x or y axis?

The distance of a point from the xaxis on a graph in the Cartesian coordinate system. It is measured parallel to the yaxis. For example, a point having coordinates (2,3) has 3 as its ordinate

How to lock rows and columns in excel using Freeze panes

To keep an area of a worksheet visible while you scroll to another area of the worksheet, go to the View tab, where you can Freeze Panes to lock specific rows and columns in place, or you can Split panes to create separate windows of the same worksheet.

Go to the below URL, watch the video (1:27 minutes length) for quick understanding

https://support.office.com/en-us/article/Freeze-panes-to-lock-rows-and-columns-dab2ffc9-020d-4026-8121-67dd25f2508f

Python Regular expression – r’^$’  explanation and it’s use

In Python, r’^$’  is a regular expression that matches an empty line

This looks like a regular expression (regex) commonly used in Django URL configurations.

The ‘r’ in front tells Python the expression is a raw string. In a raw string, escape sequences are not parsed. For example, ‘\n’ is a single newline character. But, r’\n’ would be two characters: a backslash and an ‘n’.

Raw strings are handy in regex, in which the backslash is used often for its own purposes. Using ‘[\\w]’ versus r'[\w]’ results in easier to read expressions. This is more salient when you’re escaping backslashes within your regular expression: ‘[\\\\]?’ (shudders).

The ‘^$’ is the actual regular expression, and represents an empty line. The ‘^’ indicates the start of a line, and the ‘$’ indicated the end of a line. Having nothing between the two therefore matches an empty line.

In Django this expression will usually appear at the bottom of your urlpatterns list. It will catch all URLs that weren’t matched by the preceding patterns.

For this particular case, the raw string was unnecessary. Having it here will make it more consistent with urlpatterns that do benefit from being a raw string.

Example: The example code below attempts to explain this, the two entries in the list that match are the completely empty string and the string with just a newline character:

 import re
 regexp = r'^$'
 example_strings = [
   'this regular expression should just match empty lines',
   'the r prefix just denotes a raw string (to ignore special charcters)',
   'the ^ looks for the start of a line',
   'the $ looks for the end of a line',
   'lets try an empty line to test that theory',
   '',
   'and a line with just a newline character',
   '\n']
 for eg in example_strings:
    print('checking: ' + eg)
    if re.match(regexp, eg):
       print(' it matches')
    else:
      print(' no match')

Python & pip Windows installation

Python install

At the time of this writing is Python 2.* compatible. This could be/probably will be changing in the future (spring 2015?).

Installation of Python itself should be fairly straight-forward.

  • Download and execute the latest Python 2.* installation package from here.
    At the time of this writing, Python 2.7.8 is the latest.
    While either 32-bit (x86) or 64-bit (x86-64) versions should work just fine, I tend to gravitate to 32-bit installs as I have encountered other libraries/modules in the past that only offered 32-bit versions. I have no idea if those modules that pushed me to 32-bit in the past still do not support 64-bit, but I’m a creature of habit.
  • Verify a successful installation by opening a command prompt window and navigating to your Python installation directory (default is C:\Python27). Type python from this location to launch the Python interpreter.
    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.
    
    C:\Users\Username>cd C:\Python27
    
    C:\Python27>python
    Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  • It would be nice to be able to run Python from any location without having to constantly reference the full installation path name. This can by done by adding the Python installation path to Windows’ PATH ENVIRONMENT VARIABLE
    *In Windows 7 and Windows 8, simply searching for “environment variables” will present the option to Edit the system environment variables. This will open the System Properties / Advanced tab
    *In Windows XP, right click on My Computer->Properties to open System Properties and click on the Advanced tab.
  1. On the System Properties / Advanced tab, click Environment Variables to open User Variables and System Variables
  2. Create a new System Variable named Variable name: PYTHON_HOME and Variable value: c:\Python27 (or whatever your installation path was)
  3. Find the system variable called Path and click Edit
  4. Add the following text to the end of the Variable value:;%PYTHON_HOME%\;%PYTHON_HOME%\Scripts\ 
  5. Verify a successful environment variable update by opening a new command prompt window (important!) and typing python from any location
    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.
    
    C:\Users\Username>python
    Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

Pip install

The easiest way to install the python modules and keep them up-to-date is with a Python-based package manager called Pip

There are many methods for getting Pip installed, but my preferred method is the following:

  • Download get-pip.py to a folder on your computer. Open a command prompt window and navigate to the folder containing get-pip.py. Then run python get-pip.py. This will install pip.
  • Verify a successful installation by opening a command prompt window and navigating to your Python installation’s script directory (default is C:\Python27\Scripts). Type pip freeze from this location to launch the Python interpreter.
    pip freeze displays the version number of all modules installed in your Python non-standard library; On a fresh install, pip freeze probably won’t have much info to show but we’re more interested in any errors that might pop up here than the actual content

    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.
    
    C:\Users\Username>cd c:\Python27\Scripts
    
    c:\Python27\Scripts>pip freeze
    antiorm==1.1.1
    enum34==1.0
    requests==2.3.0
    virtualenv==1.11.6
    
  • It would be nice to be able to run Pip from any location without having to constantly reference the full installation path name. If you followed the Python installation instructions above, then you’ve already got the pip install location (default = C:\Python27\Scripts) in your Windows’ PATH ENVIRONMENT VARIABLE. If you did not follow those steps, refer to them above now.
  • Verify a successful environment variable update by opening a new command prompt window (important!) and typing pip freeze from any location
    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.
    
    C:\Users\Username>pip freeze
    antiorm==1.1.1
    enum34==1.0
    requests==2.3.0
    virtualenv==1.11.6