PYTHON

Add Padding to a number in python

    version = 1

    version_padded = str(version).zfill(3) #add padding

Convert List to a dictionary simple way


myDic = dict.fromkeys(myList,"defaultValue")

Basic string Placeholder format

#method 1

"%s is so %s" % ("apple","sweet")

# Result: apple is so sweet



#method 2

user = "mohan"

id   = "123"

print(f"hello {user}, your id is {id}")

Iterate over dictionary

dict = {"test2":"test1","test2":"test2"}

for key,value in dict.items():

    print(key,value)

Rename Multiple files with similar names


import os

def moSeqRename(dir,oldStringToReplace,newString):

    ## defining variables

    root    = dir

    oldList = os.listdir(root)

    oldname = ""

    newname = ""

    oldfile = ""

    newfile = ""

    

    # loop through each file in the root

    for oldname in oldList:

        oldfile = root + oldname

        newname = oldname.replace(oldStringToReplace,newString)

        newfile =  root + newname

        os.rename(oldfile,newfile)

    return 

Copy to clipboard without any external modules

import os

os.system("echo -n '%s' | xclip -selection c"  % string)


Remove first and last characters from string 

mystring = mystring[1:-1]

Difference between lists

(list(set(li1) - set(li2))) 

Edit each string in list

aovs = ["diffuse","specular","coat","transmission","sss","emission"]

aovs = ['/{0}/'.format(elem) for elem in aovs]

#this will return ["/diffuse/","/specular/" ...]


STRING MANIPULATION:

split

rsplit

lsplit

#Example:

path  = "/usr/mohan-p/deskop/m.jpg"

split = path.split("/",3)[-1]

print split

##this will print desktop/m.jpg

PySide simple button ui

import sys

from PySide.QtCore import *

import PySide.QtGui as QtGui

####

window = QtGui.QWidget()

lay = QtGui.QVBoxLayout()

####font

font = QtGui.QFont()

font.setPointSize(25)

font.setBold(True)

####Create button

button = QtGui.QPushButton("Create Read")

button.setFont(font)

button.setMinimumSize(200,50)

button.setMaximumSize(400,100)

imagePath = "/usr/people/mohan-p/mohan_pugaz/RND/NUKE_PLUGINS/ICONS/M.png"

button.setStyleSheet("background-color: url("+imagePath+") ; color: black")

lay.addWidget(button)

window.setLayout(lay)

window.show()

SETTING UP PyQt

http://www.paulwinex.ru/installpyqteng/


List to string

myString = '\n'.join(mylist)

SEND GMAIL

import smtplib

fromAdrs = "mohanpugaz@gmail.com"

toAdrs = "mohanpugaz@gmail.com"

username = "mohanpugaz"

password = "*********"

msg = "Render Completed"

subject = "render completed"

server = smtplib.SMTP('smtp.gmail.com:587')

server.starttls()

server.login(username,password)

server.sendmail(fromAdrs,toAdrs,subject,msg)

server.quit

Python Refference:

http://python-reference.readthedocs.io/en/latest/intro.html

SIMPLE UI Creation with PyQt4

https://nikolak.com/pyqt-qt-designer-getting-started/

Great Read about creating a python package

https://iq-inc.com/importerror-attempted-relative-import/