Skip to content

Instantly share code, notes, and snippets.

View kennethlove's full-sized avatar
🦀
Python

Kenneth Love kennethlove

🦀
Python
View GitHub Profile
from __future__ import print_function
import logging
from sys import exit
from random import randint
logging.basicConfig(filename='game.log', level=logging.DEBUG)
try:
# raw_input became input in Python 3
input = raw_input
import unittest
def is_prime(n):
if n in (2, 3):
return True
if n < 2 or n % 2 == 0:
return False
if n < 9:
return True
class Car(Model):
make = CharField()
model = CharField()
year = IntegerField()
@classmethod
def add_car(cls, make, model, year):
try:
cls.create(make=make, model=model, year=year)
except IntegrityError:
;;; packages.el --- writing Layer packages File for Spacemacs
;;
;; Copyright (c) 2012-2014 Sylvain Benner
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
import xml.etree.ElementTree as ET
def get_hosts(tree_root):
'''Takes an ETree root
For every sin in a given xml_file, return a list containing the FQDN.
'''
return [server.get('host') for server in tree_root.iter('sin')]
@kennethlove
kennethlove / books.py
Last active December 5, 2018 00:26
Smarter book object.
class Book:
title = ''
pages = 0
def __init__(self, title='', pages=0):
self.title = title
self.pages = pages
def __add__(self, other):
"""Control adding two Books together or a Book and a number"""
from random import choice
from moves import Rock, Paper, Scissors
class Game:
def __init__(self, rounds=3):
self.rounds = rounds
self.player = input("What's your name? ")
self.score = [0, 0]

Keybase proof

I hereby claim:

  • I am kennethlove on github.
  • I am kennethlove (https://keybase.io/kennethlove) on keybase.
  • I have a public key whose fingerprint is 7ADE 253B 0A52 B295 F5FD FB52 4254 D672 C5D3 B12C

To claim this, I am signing this object:

Traceback (most recent call last):
File "/Users/martin/Projects/md5checker/playground/md5checker/tests.py", line 15, in test_new_md5hash_derived_field
self.assertEqual(m.md5, md5)
AssertionError: '' != '9a618248b64db62d15b300a07b00580b'
+ 9a618248b64db62d15b300a07b00580b

Defining Functions in Python

(this is all assumed to be done in the shell)

Before we start, we need to get into our Python shell. Once you open up your terminal program of choice, you'll want to type python. You should now see some information about the version of Python you're running and three greater-than symbols. If you have IPython installed, feel free to use that instead.

In programming, a "function" or "method" is often the name we give to blocks of code that the computer can repeat for us. In our human world, this would be like us going through the same sets of folds for all of the napkins at a wedding reception.

In Python, these stand-alone, repeatable code blocks are called "functions" and we create new ones with the def keyword. Function names have to start with an underscore or a letter and can't contain spaces or hyphens. We'll call this function "hello". Function definitions always end with an open parenthesis. We'll talk about passing arguments to functions