Skip to content

Instantly share code, notes, and snippets.

View durgaswaroop's full-sized avatar
💭
Having Fun

Swaroop durgaswaroop

💭
Having Fun
View GitHub Profile
@durgaswaroop
durgaswaroop / image_resizer.py
Created October 28, 2018 21:53
Image Resizer.
#!/usr/bin/env python
# coding: utf-8
# Run this from the directory where you see data/.
# %%sh
# ls -F
# data/
# Also create directory data_resized with test and train subdirectories
# %%sh
@durgaswaroop
durgaswaroop / JavaOptionalsReference.md
Last active October 5, 2023 12:00
Java Optional usage and Best practices
  • Optional.empty() - An empty optional
  • Optional.of(t) - returns a present Optional containing t. (t must be non-null)
  • Optional.ofNullable(t) - returns an Optional with t that can be null

Rules

  1. Never return null from a method that's supposed to return an optional. It defeats the purpose of Optional
  2. Never do opt.get() unless you can prove that the optional is present.
  3. Prefer alternatives to using opt.isPresent() followed by opt.get()
  4. It's generally a bad idea to create an Optional for the sole purpose of chaining methods from it to get a value.
  5. If an Optional chain is nested or has an intermediate result of Optional<Optional>, it is probably too complex.
@durgaswaroop
durgaswaroop / blog.py
Last active March 14, 2023 08:35
Script to create md and html files with the given title
import sys
import subprocess
import os.path
import inflect
# alias blog="python ~\Desktop\30DaysOfBlogging\blog.py"
# Run the script with `blog <title>`
def main():
@durgaswaroop
durgaswaroop / Autoblogging-on-medium.py
Created December 29, 2017 08:51
Write articles on Medium without leaving command line
import json
import requests
access_token = '181d415f34379af07b2c11d144dfbe35d' #Fake token, obviously!
headers = {
'Authorization': "Bearer " + access_token,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84'
}
@durgaswaroop
durgaswaroop / description.md
Created November 8, 2020 15:11
Making a git like command line application with argpase in python

This is just to show how to use argparse to create a git like command line structure. It does not have any actual functionality right now.

Output of --help is shown below in this file. The actual code with argparse will be in the other file of this gist.

 % python git_clone_cmd.py clone --help
usage: git_clone_cmd.py clone [-h] [--template <template_directory>] [--local]
                              <repository> [<directory>]

positional arguments:
@durgaswaroop
durgaswaroop / application1.py
Last active April 12, 2020 17:32
Mocking with Pytest-mock articles
# application1.py
from time import sleep
def is_windows():
# This sleep could be some complex operation instead
sleep(5)
return True
@durgaswaroop
durgaswaroop / tweet-with-tweepy.py
Created December 24, 2017 22:21
Tweet with python using tweepy
import tweepy
import os
# Read all the auth keys from environment variables
consumer_key = os.environ["t_consumer_key"]
consumer_secret = os.environ["t_consumer_secret"]
access_token = os.environ["t_access_token"]
access_token_secret = os.environ["t_access_token_secret"]
# Using the keys, setup the authorization
@durgaswaroop
durgaswaroop / pandas_groupby_recipes.ipynb
Last active May 19, 2019 12:37
Pandas Groupby Recipes
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@durgaswaroop
durgaswaroop / image_resizer.py
Last active October 28, 2018 22:03
Resizes images in one directory and saves them in another directory.
#!/usr/bin/env python
# coding: utf-8
from PIL import Image
from resizeimage import resizeimage
import os
def resize_image(directory, image_name, new_directory, new_dims=[256, 256]):
@durgaswaroop
durgaswaroop / image_resizer.py
Created October 28, 2018 21:53
Image Resizer.
#!/usr/bin/env python
# coding: utf-8
# Run this from the directory where you see data/.
# %%sh
# ls -F
# data/
# Also create directory data_resized with test and train subdirectories
# %%sh