Skip to content

Instantly share code, notes, and snippets.

View BrianHicks's full-sized avatar

Brian Hicks BrianHicks

View GitHub Profile

So Python has many "magic" methods, marked by double underscores surrounding the name. (for this reason, they're also sometimes called "dunder" methods.) Rake Ketter's guide on the subject is the best you'll find, it's very clear. We're just going to address __init__ here.

So __init__ is basically a method that gets called when you create a new "instance" of a class. Let's examine this with dogs...

# So first we're going to create a new "Dog" object. A dog knows it's name and how to bark.

class Dog(object):
    def __init__(self, name, woof): # __init__ takes a couple variables, and `self`...
        self.name = name            # and assigns them as properties of "self".
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Think Python

Chapters 4-6

Chapter 4

Case study: Turtle

4.2 (Flowers)

@BrianHicks
BrianHicks / storage-and-syncing.md
Created February 2, 2014 22:01
Data Storage and Syncing

Data Storage and Syncing with LevelDB

So here's a thought for a Go interface that would store data to memory or disk and sync it with other instances in an intermittently connected manner.

Querying

The store looks like this:

type Range struct {
from datetime import datetime
class W(object):
def __getattr__(self, attr):
def meta(*args, **kwargs):
def inner(value):
return getattr(value, attr)(*args, **kwargs)
return inner
return meta
# the following is a basically functional but probably broken for some cases (just tested for basic login ATM) implementation of a flask-security UserDataStore for RethinkDB.
#
# This is released as open source under the MIT license and comes with no warranty, yada yada. TODO: actual license header
from flask_security import UserMixin, RoleMixin
from flask_security.datastore import UserDatastore
import rethinkdb as r
class Bunch(object):
def __init__(self, obj, **kws):
self.__dict__.update(obj)

Think Python

Chapters 9-12

Chapter 9

Word Play

DEMO

"""\
TinyObj does the bare minimum to make a nice object layer on top of datastores
which return dicts. It can set defaults, validate data types, and provides a
dot-access style interface for getting and setting attributes (truthfully,
because it just sprinkles a little magic Python dust over the dicts so they are
full Python objects with all of those qualities.)
"""
import datetime
class Field(object):
# I'd like to be able to define a service something like the following...
name = "Some Python Webapp"
[environment.production]
DJANGO_SETTINGS_MODULE=testapp.settings.production
[environment.staging]
DJANGO_SETTINGS_MODULE=testapp.settings.staging
[[service]]

Keybase proof

I hereby claim:

  • I am brianhicks on github.
  • I am brianhicks (https://keybase.io/brianhicks) on keybase.
  • I have a public key whose fingerprint is 8638 C01E DE05 F97D A7A6 89E2 FF1F 407C 0D3C 2430

To claim this, I am signing this object: