Python: Absolute Imports

In Python, Absolute imports can be done in 3 ways. Let's say we have a project structure as below























Let's say you have so many classes and functions inside the products module then we can go with the below syntax.

        
            
            from ecommerce import products
            product = products.Product()
        
    


If we have only one or two classes then we can use the below syntax.

        
  			from ecommerce.products import Product
			product = Product()
		

The last method is very rarely used and I don't see anywhere it's being used, but it's good to know for a specific use case. Let's say you have two but different modules named products and if you want to import both of them, if you follow any of the above methods, for sure you will get name conflict issues. To avoid that you can follow the below syntax

        
  	import ecommerce.products
	product = ecommenrce.products.Product()
    
    

Hope that helps.
Raja


How to create containerized Python development environment using Dockers ?

Hello Everyone, In this article, I will demonstrate how to create your Docker based Python runtime environment. Assume you’re in the folder where you have your python code in a file named main.py and requirements.txt Lets first build our Python container with this setup. With your favorite editor, open a file named pyDockerfile, lets go with custom names instead of default Dockerfile, It would be easy to manage. But its your choice, I am going with pyDockerfile And add below code

FROM python:3.6
RUN mkdir /app
WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY main.py .

ENTRYPOINT ["python"]
CMD ["main.py"]
We are downloading/pulling Python3.6 image creating /app directory inside the container switching context or as current working directory to /app copy the requirements.txt file install the requirements with pip copy main source code, which is in main.py in my case Adding entrypoint as python so strictly my container will accept python based arguments. and executing my code in main.py In your host machine lets create a sample requirements.txt file as

requests
And main.py

import requests
url = {
"google": "https://google.com",
"azure" : "https://portal.azure.com"
}
for item in url :
req = requests.get(url[item])
print(f"{item}", req.status_code)
Now lets build our image

$ docker build -t pyrunner -f pyDockerfile .
adding tag to our image as pyrunner as I have used custom name for dockerfile pyDockerfile once image created lats verify

docker image list
# or
docker images
Verify output of our images by running

docker container run --rm -it pyrunner
You should see output as

google 200
azure 200
Lets modify our code in main.py as

import requests

url = {
    "facebook": "https://facebook.com",
    "google": "https://google.com",
    "azure" : "https://portal.azure.com"
}

for item in url :
    req = requests.get(url[item])
    print(f"{item}", req.status_code)

We have added a new URL for facebook. Lets start a container with command as below

 docker container run --rm -it -v $(pwd):/app pyrunner
 

--rm : this is delete the container immediately after execution completed. -v we are using volume concept and mounting the current directory where we havemain.py and requirements.txt file and mounting at /app mount point in the remote directory. and you should seeing the output as below

facebook 200
google 200
azure 200
so as you have seen we have done changes to our code and our container adopted that automatically in its next execution.

How to get your current version of Python

Find Python Version!!

If you want to findout your current python version from terminal, then you can use below commands.

Python2

      
      · ~ $ python --version
      Python 2.7.18 :: Anaconda, Inc.
       
    
If you want to find out using code, you can use below snippet.
    
    import sys
    print sys.version_info
    print sys.version
    

Python3

    
    · ~ $ python --version
	Python 3.7.4
    
    
Similarly for Python3 you can use below snippet,
    
    import sys
    print(sys.version_info)
    print(sys.version)
    
    

MacOS: Could not read files copied from external drive to your Mac

While you have successfully copied the data from your external devices particularly from NTFS file system to your MacOS's file system (APFS), you still would need to struggle to access those files later for some reason. Or in certain cases being a super user you may be able to access those files, but not your applications.

Here the three simple steps to fix the issue.

Prerequisite:

Step 1: Install the XCode command line tools.

$ xcode-select --install

Actual commands that fixes the issue:

Step 2: Get the file information like file type, creator, and other file attributes etc. and verify.

GetFileInfo <path to file>
Example: GetFileInfo /Users/venkat/backups/VMDiskFile.vmdk


Step:3 Change it's file permissions by executing

SetFile -c "" -t "" <path to file>
Example: SetFile -c "" -t "" /Users/venkat/backups/VMDiskFile.vmdk

Your file is now accessible by all. Of course you may choose set of permissions as per the requirement. The above command just lifts all the restrictions on the file. 

MySQL Table Synchronization

MySQL Table Synchronization
=======================

Step: 1 Download the below package and sample script for table sysnchronization.

# cd /opt
# wget  https://static.spiceworks.com/images/how_to_steps/0000/3025/mysql-table-sync-0.9.3.tar.gz
# wget https://static.spiceworks.com/images/how_to_steps/0000/3026/syncTables.sh

Step: 2 Extract above package

# tar xzvf mysql-table-sync-0.9.3.tar.gz
# cd mysql-table-sync-0.9.3

Step: 3 Install perl-mysql modules

# yum install perl-ExtUtils-MakeMaker
# yum install "perl(DBD::mysql)"

Step: 4 Compile and install mysql-table-sync

# perl Makefile.PL
# make install



Step: 4 Then open syncTables.sh read configuration on top of the script. Change the configuration

Bash: Advanced command line arguments

Bash: Advanced command line arguments

Slicing down bash arguments as you want

echo "arg length:" $#

for i in "${@:1:1}"
do
        echo "$i"
done

for i in "${@:1}"
do
        echo "$i"
done

echo " This is the last arg of cmd-args: "
echo "${@:$#:1}"

echo " args but not last one"
#shift
for i in  "${@:1:$(expr $# - 1 )}"
do
        echo "$i"
done

# Suggestion by Kevin: https://plus.google.com/106297454560345286689
# ${@:1:$# - 1} instead of ${@:1:$(expr $# - 1 )}
for i in  "${@:1:$# - 1}"
do
        echo "$i"
done


How to: Oracle Sqlplus important flags for spool a table into CSV

How to: Oracle Sqlplus important flags for spool a table into CSV

Hello,

If you are trying to export a large table from a database via SQL Developer, it will take a lot of time. So to reduce the time and effort and you can log into or connect to remote Oracle DB via SQLPlus.

And while importing the data, to get proper structuring to your data below flags will help you.

set colsep ,  
set headsep off
set pagesize 0
set trimout on
set feedback off
set trimspool on
set linesize 32767
set echo off
SET TERMOUT OFF

For explanation of those flags, please read : https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm

So if you are planning to export a table then whole .sql file should look like below

set colsep ,
set headsep off
set pagesize 0
set trimout on
set feedback off
set trimspool on
set linesize 32767
set echo off
SET TERMOUT OFF
spool 4802.csv
SELECT player||','||name||','||value FROM player WHERE value IN ('Industry', 'Title');
spool off

Hope it will help you.