Skip to content

Instantly share code, notes, and snippets.

View alexmacniven's full-sized avatar
🍪

Alex Macniven alexmacniven

🍪
View GitHub Profile
string = 'Lists of animals\nLists of aquarium life\nLists of biologists by author abbreviation\nLists of cultivars'
stringlist = string.split('\n')
in >>> len(stringList)
out>>> 4
in >>> stringList
out>>> ['Lists of animals', 'Lists of aquarium life', 'Lists of biologists by author abbreviation', 'Lists of cultivars']
@alexmacniven
alexmacniven / gist:bac36ab59ad70cf3cd2abd54a91ef536
Created September 10, 2016 12:52
Getting values from DataFrames, when the values are tuples
>>> df = pandas.DataFrame([[(1,2)],[(3,4)],[(5,6)]],columns=list('A')) # Makes a new DataFrame, df
>>> df # df looks like this
A
0 (1, 2)
1 (3, 4)
2 (5, 6)
>>> df.at[0,'A'] # DataFrame.at[y,x] gets the value at the location in DataFrame
(1, 2)
>>> r = df.at[0,'A'] # Because theyre tuples, we can do this reference the return value
>>> r[0] # And access each item in the tuple like we would a list
@alexmacniven
alexmacniven / card_shuffle.py
Last active June 22, 2018 16:45
A deck of cards as a list of tuples
>>> new_deck = [('10_0f_clubs.jpg', 10), ('2_of_hearts.jpg', 2), ('5_of_spades.jpg', 5)]
>>> random.shuffle(new_deck)
>>> new_deck
[('5_of_spades.jpg', 5), ('2_of_hearts.jpg', 2), ('10_0f_clubs.jpg', 10)]
@alexmacniven
alexmacniven / version_is_new.py
Last active August 2, 2018 14:13
Compare two version numbers in the format 'major.minor.patch'
def version_is_new(a, b):
"""Returns True if version `a` is newer than `b`
Args:
a: String version number to compare
b: String version number to compare against
Returns:
True if `a` is newer than `b`
"""
@alexmacniven
alexmacniven / guardian_reader.py
Last active February 21, 2019 08:56
A (quickly) mocked up script to parse a Guardian RSS feed
# guardian_reader.py
# Excellent package for doing HTTP Post and Gets etc.
import requests
# Out of the box XML parsing.
from xml.etree import ElementTree
URL = 'https://www.theguardian.com/society/deafness/rss'
@alexmacniven
alexmacniven / iterparse_example.py
Created February 21, 2019 13:29
Use xml.etree.ElementTree.iterparse for *very* large xml data
"""
iterparse(source, events=None, parser=None)
Incrementally parse XML document into ElementTree.
This class also reports what's going on to the user based on the
*events* it is initialized with. The supported events are the strings
"start", "end", "start-ns" and "end-ns" (the "ns" events are used to get
detailed namespace information). If *events* is omitted, only
"end" events are reported.
@alexmacniven
alexmacniven / DateConvert.pas
Created February 26, 2019 11:29
Demonstrates converting non-standard dates in Delphi
var
FormatSettings: TFormatSettings;
DateString: String;
DateTime: TDateTime;
begin
FormatSettings := TFormatSettings.Create;
with FormatSettings do
begin
DateSeparator := '-';
@alexmacniven
alexmacniven / CSharpRounding.cs
Created February 26, 2019 20:50
Examples of how to round in CSharp
class Rounding
{
static void Main(String[] args)
{
// Round up = 1
Math.Ceiling(0.5);
// Round down = 0
Math.Floor(0.5);
@alexmacniven
alexmacniven / remove_files.md
Created March 4, 2019 09:18
Removing files under a certain size with Powershell

We can use a simple command to remove files under x size

> Get-ChildItem . -Filter *.xml -recurse -file | ? {$_.length -lt 105} | % {Remove-Item $_.fullname}

Let's break this down at the pipes and look at each section;

Get-ChildItem . -Filter *.xml -recurse -file
@alexmacniven
alexmacniven / unittest_mock_patch_example.py
Created April 17, 2019 14:43
This gist describes using mock.patch from the python module unittest
import unittest
from datetime import datetime, timedelta
from unittest.mock import patch
# Mocking describes replacing objects or functions
# in a piece of logic with another. A prime example
# is when we want to test a function returning a result
# based on datetime.now(). As it is always changing, it's