Skip to content

Instantly share code, notes, and snippets.

View andreagrandi's full-sized avatar
🏠
Working from home... permanently!

Andrea Grandi andreagrandi

🏠
Working from home... permanently!
View GitHub Profile
@andreagrandi
andreagrandi / fbx_test.py
Created September 18, 2013 11:57
FBX test
import requests
import json
api_base_url = 'http://localhost:8000/api/1/'
glow_api_token = '2d2e1f3e3d40bca24477929c1e77f8f2419492c4'
account_id = '520367d182711919fe02dd52'
auth_header = {'Authorization': 'Token ' + glow_api_token}
pixel_data = {'pixel_data': {'short_name': 'Segment Pixel Test 001 - Andrea', 'account_id': account_id}}
@andreagrandi
andreagrandi / appnexus_client.py
Last active December 25, 2015 02:39
In this example I define a very simple client that authenticate with AppNexus API and return the authentication token. In the test file i mock the post() method of the requests library and I make it return a JSON response that I want. The final result is that when I call the authenticate() method, it doesn't really call the remote API, and the r…
import requests
import json
class AppNexusClient(object):
@staticmethod
def authenticate(username, password):
auth_data = {'auth': {'username': username, 'password': password}}
r = requests.post('https://api.appnexus.com/auth', data = json.dumps(auth_data))
return r.json()['response']['token']
@andreagrandi
andreagrandi / disable_csrf.py
Created October 17, 2013 13:08
Class used to disable CSRF check. Include this class in MIDDLEWARE_CLASSES in settings.py
class DisableCSRF(object):
def process_request(self, request):
setattr(request, '_dont_enforce_csrf_checks', True)
@andreagrandi
andreagrandi / settings.py
Last active November 25, 2023 14:36
Sending emails from Django during development without using a real SMTP server. Python comes with a very basic and integrated SMTP server. To start it just open a terminal and type: python -m smtpd -n -c DebuggingServer localhost:1025 Then configure your settings.py using the following parameters. You will see the email directly in the terminal …
if DEBUG:
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'testing@example.com'
@andreagrandi
andreagrandi / pink.py
Created October 23, 2013 14:28
Pink from Aerosmith, rewritten in Python for Breast Cancer Day 2013.
color = 'Pink'
def refrain_one():
print "%s it was love at first sight " % (color)
print "%s when I turn out the light, and " % (color)
print "%s gets me high as a kite " % (color)
print "And I think everything is going to be all right "
print "No matter what we do tonight "
print ""
@andreagrandi
andreagrandi / unmarshall_example.go
Created August 18, 2014 16:18
Example of Unmarshal (from JSON to struct) in Go
package main
import (
"fmt"
"encoding/json"
)
type Message struct {
Name string
Body string
@andreagrandi
andreagrandi / parse_json_post.go
Created August 19, 2014 13:28
Parse a JSON http POST in GoLang
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type test_struct struct {
Test string
@andreagrandi
andreagrandi / cmd_flag_parser.go
Created August 21, 2014 10:27
Parse command line flags in Go
package main
import (
"flag"
"fmt"
)
var hostName = flag.String("host", "localhost", "Hostname or IP you want to run this service on")
var portNumber = flag.Int("port", 8080, "Port you want this service to listen on (default 8080)")
@andreagrandi
andreagrandi / text_file_reader.go
Created August 21, 2014 13:43
Read a text file and print its lines in Go
package main
import (
"flag"
"fmt"
"io/ioutil"
"strings"
)
var fileName = flag.String("f", "", "Filename to be read")
@andreagrandi
andreagrandi / get_client.go
Created August 21, 2014 15:17
http.get() example to get a remote url and display its content
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
)
var url = flag.String("u", "", "URL to read")