Skip to content

Instantly share code, notes, and snippets.

@bplaxco
bplaxco / naming_guidelines.md
Created March 29, 2023 23:23 — forked from IwoHerka/naming_guidelines.md
Naming Guidelines

Naming guidelines

1. Syntax

1.1 Be consistent

Consistency in naming makes reading and memory retrieval much, much easier. Conversely, changing rules and mixing conventions are very confusing and significantly increase cognitive load. Follow language, company, and project conventions for names, even if you don't like them.

1.2 Follow conventions

@bplaxco
bplaxco / Override.py
Created October 2, 2016 14:20
An implementation of Java's Override annotation as a python decorator. (Only lightly tested)
class Annotation(object):
def __init__(self, method):
self.method = method
# end __init__
def __get__(self, obj, objtype):
return lambda *args, **kwargs: self.__call__(obj, *args, **kwargs)
# end __get__
# end Annotation
@bplaxco
bplaxco / normalize.py
Last active March 4, 2016 17:34
Quickly runs `func` on elements in dict or list/set/tuple climbing down the nested elements.
import copy
def next(a, i):
j = a.index(i) + 1
return a[j] if j < len(a) else None
# end next
def get_keys(x, default=()):
if hasattr(x, 'keys'):
@bplaxco
bplaxco / open_struct.py
Last active October 27, 2015 23:01
An attempt of something like an OpenStruct in ruby
class OpenStruct(dict):
__getattr__= dict.__getitem__
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
@bplaxco
bplaxco / __init__.py
Created September 25, 2015 15:17
An __init__.py to break classes up into single files without changing the import path.
# Import classes from files in the sub directory
# so files in models/ClassName.py with ClassName in it
# can be imported as models.ClassName
import pkgutil
__all__ = []
for _, name, ispkg in pkgutil.walk_packages(__path__):
@bplaxco
bplaxco / session.sh
Last active August 29, 2015 14:27
A way to share your session with a friend
#! /bin/bash
# Create the named pipe that script will go out to
rm -R /tmp/session_fifo
mkfifo /tmp/session_fifo
# Start up the server and get the pid
nc -l 1337 < /tmp/session_fifo &
nc_pid=$!
@bplaxco
bplaxco / logrotate-example
Last active August 29, 2015 14:07
Example log rotation file to put in `/etc/logrotate.d/`
/var/log/<app_name>/*.log {
daily # Rotate Daily.
rotate 30 # Keep Last 30 Logs.
missingok # If file is missing, go on to the next one without issuing an error message.
copytruncate # Truncate the original log file in place after creating a copy.
compress # Old versions of log files are compressed with gzip(1) by default.
delaycompress # Postpone compression of the previous log file to the next rotation cycle.
notifempty # Do not rotate the log if it is empty.
olddir /var/log/<app_name>/history # Logs are moved into directory for rotation.
dateext # Archive old versions of log files adding a daily extension.