Skip to content

Instantly share code, notes, and snippets.

View sidchilling's full-sized avatar

Siddharth Saha sidchilling

View GitHub Profile
@sidchilling
sidchilling / function_as_argument.py
Created April 29, 2013 06:36
Script to show how to pass a function as a parameter to another function
# If a function can return a function, then a function can also take a function
# as an argument
# Defining a function which we will pass as an argument to another function
def scream(word = 'yes'):
return '%s!' %(word.upper())
# Function taking a function as argument
def do_something_before(func):
@sidchilling
sidchilling / function_returning_function.py
Created April 29, 2013 06:35
Script to show how to return a function from another function
# Script to show how functions can return functions
def get_talk(type = 'shout'):
# We define some functions here on the fly.
def shout(word = 'yes'):
return '%s!' %(word.capitalize())
def whisper(word = 'yes'):
return '%s...' %(word.lower())
@sidchilling
sidchilling / function_inside_function.py
Created April 29, 2013 06:32
Script to show how you can define a function inside another function
# Script to show how to define functions inside functions and its scope
def talk():
# We can define a function inside a function like this. In this case we define a
# function whisper() inside the funtion talk()
def whisper(word = 'yes'):
return '%s...' %(word)
# And we can use the function whisper right away inside the wrapping function
@sidchilling
sidchilling / functions_as_objects.py
Created April 29, 2013 06:29
Script to show how functions in python are objects
# Script to show how functions are just objects
def shout(word = 'yes'):
return '%s!' %(word.capitalize())
print shout() # Outputs: Yes!
scream = shout
# As functions are just objects, we can assign them just like a variable assignment
@sidchilling
sidchilling / gist:4178176
Created November 30, 2012 20:01
HttpRequest for POST with file
<?php
$file_path = '/home/siddharthsaha/Desktop/production.html';
$data = array(
'owner' => 'siddharth saha'
);
$request = new HttpRequest($url, HttpRequest::METH_POST);
$request->addPostFields($data); # Adding the data with the POST method
$request->addPostFile('test_file', $file_path); # The file has to be posted with a separate method.
$request->send();
@sidchilling
sidchilling / gist:4178172
Created November 30, 2012 20:00
CURL method to make POST request with file upload
<?php
$url = 'http://localhost/tests/upload_file'; #URL for the service
$file_path = '/home/siddharthsaha/Desktop/production.html'; # File to be uploaded
$data = array(
'owner' => 'siddharth saha',
'test_file' => '@' . $file_path # For files, you have to append a @ before the filepath.
);
# Make the curl request exactly as in the POST example above.
@sidchilling
sidchilling / gist:4178170
Created November 30, 2012 20:00
CURL method to make POST request with file upload
<?php
$url = 'http://localhost/tests/upload_file'; #URL for the service
$file_path = '/home/siddharthsaha/Desktop/production.html'; # File to be uploaded
$data = array(
'owner' => 'siddharth saha',
'test_file' => '@' . $file_path # For files, you have to append a @ before the filepath.
);
# Make the curl request exactly as in the POST example above.
@sidchilling
sidchilling / gist:4178150
Created November 30, 2012 19:56
POST with file upload using Requests
<?php
# Include Requests.php and autoload the internal classes
$data = array(
'owner' => 'siddharth saha',
'test_file' => '@' . $file_path # File name to be pre-pended with @
);
$response = Requests::request($url, array(), $data, Requests::POST);
if ($response->success) {
$response = json_decode($response->body);
}
@sidchilling
sidchilling / gist:4178129
Created November 30, 2012 19:53
POST request with Requests
<?php
# Include Requests.php and autoloader
$response = Requests::request($url, array(), $data, Requests::POST); # empty array for headers if you want to send any
if ($response->success) {
$response = json_decode($response->body);
}
?>
@sidchilling
sidchilling / gist:4178106
Created November 30, 2012 19:50
GET requests with Requests
<?php
require_once('./requests/library/Requests.php'); # have to require this single file
Requests::register_autoloader(); # needed by Requests class to autoload internal classes
$response = Requests::request($url, array(), $data); # The empty array is for headers if you want to send any
if ($response->success) {
$response = json_decode($response->body);
}
?>