Skip to content

Instantly share code, notes, and snippets.

View RenSys's full-sized avatar

kbeigan RenSys

  • Renegade Systems PTY LTD
  • Western Australia
View GitHub Profile
@RenSys
RenSys / Standard - Merge even & uneven lists.py
Last active April 22, 2017 01:07
Standard - Merge even & uneven lists
from itertools import izip_longest
names = ['Karl','Savannah', 'Harvey', 'Scarlett']
letters = [len(n) for n in names]
print " --- merge even lists ---"
for name, count in izip_longest(names, letters):
print "Name '{0}' has {1} letters".format(name, count)
print " --- merge uneven lists ---"
from functools import partial
def raise_to_power(value, power):
return value ** power
square = partial(raise_to_power, power=2)
@RenSys
RenSys / Standard - File Exists check.py
Last active April 22, 2017 01:05
Standard - isfile() Check if File Exist
from os.path import isfile
if isfile('filename.xxx'):
print 'file exists'
else:
print 'no file exists'
@RenSys
RenSys / Pendulum Package - number days month.py
Last active February 17, 2017 08:33
Simple Date Calc using Pendulum
import pendulum
# pip install pendulum
if __name__ == '__main__':
_1st_jan_2011 = pendulum.create(day=1, month=1, year=2011)
print _1st_jan_2011.days_in_month
@RenSys
RenSys / Pandas - TimeZone conversions.py
Last active February 17, 2017 08:31
Pandas - Convert Timezones
import pandas as pd
df = pd.DataFrame(data = [1,2,3,4,5], index = pd.date_range("7:00", "9:00", freq="30min", tz = 'UTC'))
# convert UTC Timezone to Australia Perth
df.index = df.index.tz_convert('Australia/Perth')
# Remove TZ information from the datetime stamp, this is useful when exporting to Excel
df.index = df.index.tz_localize(None)
@RenSys
RenSys / Standard - List all timezones.py
Last active February 17, 2017 08:31
List all Timezones
from pytz import all_timezones
print all_timezones
@RenSys
RenSys / Pandas - Create custom datetime range.py
Last active April 22, 2017 01:08
Pandas - Create custom datetime range
import pandas as pd
from datetime import datetime
dd = 01
mm = 01
yyyy = 2011
pd.set_option('display.expand_frame_repr', False)
print '------ Day Shift ---------'
@RenSys
RenSys / Package Pendulum - First & Last Day datetime calcs.py
Last active February 17, 2017 08:34
Calc first & last day of previous month
import pendulum
# pip install pendulum
if __name__ == '__main__':
_1st_day = pendulum.now().subtract(months=1).start_of('month')
last_day = pendulum.now().subtract(months=1).end_of('month')
print 'First day of previous month: {}'.format(_1st_day.to_date_string())
print 'Last day of previous month: {}'.format(last_day.to_date_string())
@RenSys
RenSys / Standard - parallel multiprocessing tasks.py
Last active February 17, 2017 08:21
spawn multi processors were total is CPU cores - 1 (ensure it doesn't lock the computer)
import multiprocessing
def increment_value(num):
num = num + 1
print 'this is number ->{}'.format(num)
return num
if __name__ == '__main__':
'''
@RenSys
RenSys / Standard - counting list items.py
Last active February 17, 2017 08:20
Count the number of unique items in a list
from collections import Counter
word_count = Counter('my my name is is is Hal hal'.split())
for word, count in word_count.items():
print 'Word Count: {} -> {}'.format(count, word)