Skip to content

Instantly share code, notes, and snippets.

View andrewsolomon's full-sized avatar

Andrew Solomon andrewsolomon

View GitHub Profile
@andrewsolomon
andrewsolomon / remove_somefile.py
Last active March 1, 2021 14:14
Optimised Python
# Standard Python
try:
os.remove('somefile.tmp')
except FileNotFoundError:
pass
# Improved Python
import contextlib
with contextlib.suppress(FileNotFoundError):
os.remove('somefile.tmp')
@andrewsolomon
andrewsolomon / sum_algorithms.pl
Created January 5, 2020 17:38
ways of calculating the sum of 1 to n
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
# To extend this to infinity, read:
# https://medium.com/cantors-paradise/the-ramanujan-summation-1-2-3-1-12-a8cc23dea793
# For loop
@andrewsolomon
andrewsolomon / catch.pl
Created August 8, 2019 05:57
use Perl's eval
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
sub oops {
die "It happened";
}
@andrewsolomon
andrewsolomon / gitlog2graph.pl
Created April 15, 2019 06:01
Transforming a git repository into a co-modification graph
#!/usr/bin/perl
# Converts a gitlog file to a list of edges in an undirected weighted graph
# Author: Aron Lurie
# Date: 2018-08-15
# Usage: git log --pretty=oneline --name-only | perl git.pl > mylogfile.txt
my @files = ();
my %adjacent = ();
while (<STDIN>) {
> ./car-02.pl
This is a nice Peugeot
Missing required arguments: name at (eval 12) line 49.
{
package Brand;
use Moo::Role;
has name => ( is => 'rw', required => 1 ); # made rw
sub description { "This is a nice ". shift->name }
}
{
{
package Brand;
use Moo::Role;
has name => (
is => 'rw',
lazy => 1,
default => sub { die "name is required" }
);
has name => ( is => 'ro', required => 1 );
# This will die: my $car = Car->new;
./car-03.pl
Use of uninitialized value in concatenation (.) or string at ./car-03.pl line 15.
This is a nice
@andrewsolomon
andrewsolomon / car-01.out
Created December 17, 2017 14:52
Output with an undefined attribute
> ./car-01.pl
This is a nice Peugeot
Use of uninitialized value in concatenation (.) or string at ./car-01.pl line 15.
This is a nice