Skip to content

Instantly share code, notes, and snippets.

View gideondsouza's full-sized avatar

Gideon Israel Dsouza gideondsouza

  • Dransfeld, Germany
View GitHub Profile
package MyWebService;
use Dancer;
use Dancer::Plugin::REST;
#this line kicks in the plugin. So now whatever you return will be formatted and send back
prepare_serializer_for_format;
#this sets up a route that takes an id a dot and an extension
get '/user/:id.:format' => sub {
User->find(params->{id});#return some reference to a hash or array or what have you
};
@gideondsouza
gideondsouza / lwp_psgi_dance2.pl
Created September 9, 2013 14:57
using LWP::Protocol::PSGI with dancer2
use LWP::UserAgent;
use LWP::Protocol::PSGI;
# can be Mojolicious, Catalyst ... any PSGI application
my $psgi_app = do {
use Dancer;#ATTENTION: changing this to Dancer2 makes this sample NOT WORK.
setting apphandler => 'PSGI';
get '/search' => sub {
return 'googling ' . params->{q};
};
@gideondsouza
gideondsouza / intro_tuple.py
Last active December 22, 2015 10:39
Introducing a tuple
>>> #The tuple is a common CS structure.
>>> T = (1,2,3) #think of it as a record
>>> type(T)
<class 'tuple'>
#you're not limited to numbers, you can store anything in a tuple, list, set etc.
>>> Friends = ("Bill", "Bob", "Will")
>>> Friends[0] #access it's first element.
'Bill'
>>> T[2]
3
@gideondsouza
gideondsouza / intro_set.py
Created September 6, 2013 07:16
Introducing a set
>>>
>>> S = {1,3} #a new set with two elements
>>> type(S) #what is the type of S?
<class 'set'>
>>> S1 = {3,4,5} #initialize two sets S1 and S2
>>> S2 = {3,6,7}
>>> S1 | S2 #this will give you a union of the two.
{3, 4, 5, 6, 7}
>>> #you can also use & for intersection, and ^ for xor
@gideondsouza
gideondsouza / python_comprehensions1.py
Last active December 22, 2015 10:39
Python Comprehensions 1
C:\Gideon\Coding>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> L1 = [1,2,3] #this is how you define a plain list
>>> L1 #type the variable and...
[1, 2, 3] #....this is what it's content looks like
>>> L2 = [x for x in range(20)] #now shove a for into a list and you have a dynamic list
>>> L2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
@gideondsouza
gideondsouza / python_comprehensions3.py
Created September 6, 2013 07:35
Python Comprehensions 3
>>>
>>> L = [1,2,3,4,5] #define a list 1 to 5
>>> [(x,y) for x in L for y in L] # we have two fors running over the list
#this gives all the 25 non-unique combinations
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5),
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
#need unque combinations. No problem! Add an if in the end.
>>> [(x,y) for x in L for y in L if x!=y]
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2),
@gideondsouza
gideondsouza / python_comprehensions2.py
Created September 6, 2013 06:59
Python Comprehensions 2
>>> L4 = [x*x for x in range(10)] #this will evaluate to a list of squares from 1 to 10
>>> L4
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> #the next comprehension will evaluate to a list of even numbers from 1 to 20
>>> L3 = [x for x in range(20) if x % 2 == 0]
>>> L3
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>
@gideondsouza
gideondsouza / login_test.pl
Last active December 22, 2015 07:48
login snippet from cookbook.pod
use Dancer2;
use strict;
use warnings;
set session => "Simple";
hook before => sub {
if (!session('user') && request->dispatch_path !~ m{^/login}) {
forward '/login', { requested_path => request->dispatch_path };
}
@gideondsouza
gideondsouza / login.tt
Created September 4, 2013 17:35
login.tt from the cookbook.pod example
<html>
<head>
<title>Session and log</title>
</head>
<body>
<form action='/login' method='POST'>
User Name : <input type='text' name='user'/>
Password: <input type='password' name='pass' />
<input type='hidden' name='path' value='[% path %]'/>
<input type='submit' value='Login' />
@gideondsouza
gideondsouza / session_test.pl
Last active December 22, 2015 07:38
session test from Dancer2 cookbook.pod
1 use Dancer2;
2 use strict;
3 use warnings;
4
5 set session => 'Simple';
6
7 get '/store' => sub {
8 session foo => 'Something';
9 return "Stored";
10 };