The Most Useful Python Code Snippets I Use

Separating Large Lists into Chunks for Data Processing

import random
res = [random.randrange(1, 50, 1) for i in range(100)]

chunk_size = 25
    for i in range(0, len(res), chunk_size):
        chunk = res[i:i + chunk_size]

Reading Multiple Lines of Data When Reading from a File

with open(file_name) as f:
    for line1,line2 in itertools.zip_longest(*[f]*2):
        text1 = list(filter(None, line1.split(',')))
        text2 = list(filter(None, line2.split(',')))

Writing Lists as Columns to a CSV

with open(os.path.join(f'{s}', f'{ticker_symbol}.csv'), 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(zip(dates, datapoints)) 

Finding the drive letter of a known file or directory path

I’m a avid user of Google Drive Stream at work and sometimes we share code amongst different users but each user has a different drive letter depending on how many drives we each have on our own systems. The below code is helpful in determining the drive letters automatically. (Reference)

The snippet uses os.path.exists and os.path.join which you can reference the python docs if you do not know how to use the commands.

from string import ascii_uppercase

for drive in ascii_uppercase:
    if os.path.exists(os.path.join(drive + ':\\', 'Path', 'To', 'Directory')):
        file_path = os.path.join(drive + ':\\', 'Path', 'To', 'Directory')

Transfer files using SCP to a remote server programmatically

This example uses a pem file instead of a password login as this is how my server is configured but you can use either method. (SCP Documentation).

from scp import SCPClient
from paramiko import SSHClient

def createSSHClient(self, server, user, pem_file):
    client = SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(server, username=user, key_filename=pem_file)
    return client

def progress4(self, filename, size, sent, peername):
    sys.stdout.write("(%s:%s) %s's progress: %.2f%%   \r" % (peername[0], peername[1], filename, float(sent)/float(size)*100))

ssh = self.createSSHClient(server, user, pem_file)
scp = SCPClient(ssh.get_transport(), progress4=self.progress4)

''' 
Different options to download, upload or delete files can be done here. There are many options that you can find in the documentation including transferring entire directories and their contents 
'''
scp.put("filename.doc") #Upload
scp.remove("/path/to/file/filename.doc") #Delete
scp.get("/path/to/file/filename.doc") #Download

Writing a list to a file without the trailing newline

# list of names
names = ['Jessa', 'Eric', 'Bob']

with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp:
    fp.write('\n'.join(names))

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.