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 / insert_element_list.go
Last active August 29, 2015 14:05
How to insert an element at position i in an arrray of strings
package main
import (
"fmt"
)
func main() {
list_a := []string{"apple", "orange", "grapes"}
fmt.Println(list_a)
i := 1
@andreagrandi
andreagrandi / read_config.go
Created August 21, 2014 16:26
Read values from a JSON configuration file
// Parse a conf.json file with this content:
//
// {
// "username": "YourUser",
// "password": "YourPassword"
// }
package main
import (
@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")
@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 / 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 / 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 / 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 / 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 / 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 / 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)