Skip to content

Instantly share code, notes, and snippets.

View martin-martin's full-sized avatar
⛰️
hill view from the balcony

Martin Breuss martin-martin

⛰️
hill view from the balcony
View GitHub Profile
@martin-martin
martin-martin / index.html
Created March 13, 2016 21:14
visual comment on a graph
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>d3eath sentence</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style type='text/css'>
/*some style for the visualization*/
@martin-martin
martin-martin / week_calc.py
Last active June 10, 2018 10:33
example using variables and integers for a simple calculation
# simple addition using variables and integers
pre_course_weeks = 2
onsite_weeks = 6
online_weeks = 4
course_duration = pre_course_weeks + onsite_weeks + online_weeks
print(course_duration)
@martin-martin
martin-martin / floating.py
Created June 10, 2018 10:56
about python's basic float type
# simple float calculation
print(42.42 / 3)
# mixing integers with floats yields floats
part_of_whole = 0.5
whole = part_of_whole * 2
print(whole)
@martin-martin
martin-martin / string_syntax.py
Created June 10, 2018 11:19
about python strings
# creating a python string with single quotes
job = 'coding'
# creating a python string with double quotes
location = "nomad"
print(job, location) # both of them work fine
# however, a mix-up of the two does not work and produces a SyntaxError
@martin-martin
martin-martin / immutable_string.py
Last active June 10, 2018 12:18
CheerySoreSequel created by martin_martin - https://repl.it/@martin_martin/CheerySoreSequel
name = 'codingnomads'
# trying to change the string directly causes an error
# name[8:] = 'rmals'
# TypeError: 'str' object does not support item assignment
# we need to create a new string
new_name = name[:8] + 'rmals' # using string slicing and concatenation
print(name, new_name)
# after re-assigning the value, the old 'name' is overwritten
name = "Mycroft"
def print_name_box():
print(name)
def smaller_box():
print(name)
def smallest_box():
print(name)
name = "Mycroft"
def print_name_box():
print(name)
def smaller_box():
'''
(re)assigning a variable within the same scope
overwrites the same variable from an outer scope
-> you cannot use it *before* assigning it,
# a function decorator to avoid digital screaming
def lowercase(func):
def wrapper(text):
initial_result = func(text)
new_result = initial_result.lower()
return new_result
return wrapper
@lowercase
def say_something(text):
@martin-martin
martin-martin / friend_count.py
Created June 22, 2018 12:24
get user's friend count from twitter using tweepy
import os
import tweepy
# fetch the secrets from our virtual environment variables
CONSUMER_KEY = os.environ['TWITTER_CONSUMER_KEY']
CONSUMER_SECRET = os.environ['TWITTER_CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['TWITTER_ACCESS_TOKEN']
ACCESS_SECRET = os.environ['TWITTER_ACCESS_SECRET']
@martin-martin
martin-martin / views.py
Created August 7, 2018 09:48
working authentication with Django 2.1 (including some gotchas!)
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login, authenticate, logout
# Create your views here.
def signup_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid(): # first see whether all is good