Skip to content

Instantly share code, notes, and snippets.

# Sort a list of dictionary objects by a key - case sensitive
from operator import itemgetter
mylist = sorted(mylist, key=itemgetter('name'))
# Sort a list of dictionary objects by a key - case insensitive
mylist = sorted(mylist, key=lambda k: k['name'].lower())
@voidnologo
voidnologo / dict.py
Created November 16, 2017 15:31
Command line access to Dictionary in Mac OS X. Need to have pyobjc installed in environment.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
from DictionaryServices import DCSCopyTextDefinition
PURPLE = '\033[95m'
BLUE = '\033[94m'
@voidnologo
voidnologo / trees.py
Created October 29, 2017 03:02
Playing with trees in Python
#!/usr/bin/env python
from queue import Queue
# https://medium.com/the-renaissance-developer/learning-tree-data-structure-27c6bb363051
'''
Root - the topmost node of the tree
Edge - the link between two nodes
Child - A node that has a parent node
@voidnologo
voidnologo / auto_now_test_ex.md
Last active November 3, 2015 19:44
Testing Django model fields with 'auto_now'
class MyModel(models.Model):
  time_field = models.DateTimeField(auto_now=True)

This makes testing difficult, because the created field will always be the current time. If you try changeing it with

myobject.time_field = _sometime
myobject.save()

it will get reset to the current time when saving.

@voidnologo
voidnologo / nested_dicts.md
Last active October 3, 2021 17:54
Displaying nested dictionaries in a Django template

Recently came across a situtuation where I was trying to display a dictionary of dictionaries of lists in a django template. In the python code, the data structure was built like:

      @property
      def nested(self):
          from collections import defaultdict
          data = defaultdict(lambda: defaultdict(list))
          for x in self.a_list_of_objects:
               data['key'][x.an_attribute].append(x)
@voidnologo
voidnologo / nav_example.md
Last active August 29, 2015 14:27
Bootstrap 3 navbar covers text when using internal anchors

A Bootstrap3 navbar using class="navbar-fixed-top will overlay the first bit of other elements on your page. The recommendation is to add body {padding-top = 60px;} to your css after bootstrap is loaded. (http://getbootstrap.com/components/#navbar-fixed-top)

This works great, until you try to go to an internal anchor, which will then get buried under the navbar. There seem to be a couple of fixes for this. (Issue reported: twbs/bootstrap#1768)

Example of problem: see z_example.html

###Solution 1: CSS

 
@voidnologo
voidnologo / qq2.py
Created May 19, 2015 02:22
Quiz Question 2
Def a_function(a, b, c):
print(a)
print(b)
print(c)
def another_function(robot, cat)
awesome = robot + cat
return awesome
variable1 = 5
@voidnologo
voidnologo / qq1.py
Created May 19, 2015 02:12
Quiz Question 1
def double_it(x):
x = x * 2
return x
def get_input():
return int(input("Enter a number: "))
# main
x = get_input()
y = double_it(x)

Situation: Have some code that repeats over and over with only minor variation.

        self.likes_comedy = BooleanVar()
        Checkbutton(
            self,
            text='Comedy',
            variable=self.likes_comedy,
            command=self.update_text
 ).grid(row=2, column=0, sticky=W)