Skip to content

Instantly share code, notes, and snippets.

View jfriedly's full-sized avatar

Joel Friedly jfriedly

View GitHub Profile
@jfriedly
jfriedly / thread-example.py
Last active November 16, 2015 04:12
Example of how to code a GUI with a socket listener using Python threads
import socket
import threading
""" Create a thread listening for client connections and enter the GUI mainloop
"""
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((socket.gethostname(), 80))
server_socket.listen(5)
client_sockets = []
@jfriedly
jfriedly / ps-man-page.txt
Created February 23, 2015 22:01
A snippet from the ps manpage...
ENVIRONMENT VARIABLES
The following environment variables could affect ps:
COLUMNS
Override default display width.
LINES
Override default display height.
PS_PERSONALITY
@jfriedly
jfriedly / freq-ex.rst
Created August 28, 2014 02:10
Use this script to demonstrate the effect of setting all CPUs on a machine to their maximum frequency
@jfriedly
jfriedly / c-putc-vs-fputc.rst
Last active March 2, 2023 07:21
The difference between ``putc`` and ``fputc`` in C: notes from a conversation with Shevek

C: putc vs fputc

From the putc man page, "putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once." I wasn't sure what this meant so I asked Shevek. He launched into a lambda calculus explanation that eventually boiled down to: putc may evaluate it's arguments more than once, but fputc evaluates them exactly once. This is because putc is implemented as a macro and the C preprocessor just does string substitution, leading to examples like the ones below.

Ignoring what putc and fputc actually do, let's look at a simplified version where they both simply do one operation twice and another operation once:

$ ./test-driver.py
[******************************] 1000/1000 100% [00:10, 00:00, 98.88 #/s]
Traceback (most recent call last):
File "./test-driver.py", line 29, in <module>
for _ in tqdm(range(1000), leave=True, format_dict=format_dict1):
TypeError: tqdm() got an unexpected keyword argument 'format_dict'
import itertools
class Player()
_count = itertools.count()
def __init__(self):
self.id = _count.next()
# ...
@jfriedly
jfriedly / gist:6723725
Created September 27, 2013 03:20
first-program
# Comments!
PROGRAM joel IS
VARIABLE movecounter = 4
INSTRUCTION default IS
IF next-is-not-friend THEN
infect
END IF
WHILE next-is-empty DO
17:04:49 D, [2013-08-08T17:04:48.397029 #22915] DEBUG -- : error in setup command: Error parsing /tmp/buildd/python-cinderclient-1.0.5/setup.cfg: Exception: Versioning for this project requires either an sdist tarball, or access to an upstream git repository.
17:04:49 D, [2013-08-08T17:04:48.418053 #22915] DEBUG -- : dh_auto_clean: python setup.py clean -a returned exit code 1
17:04:49 D, [2013-08-08T17:04:48.419824 #22915] DEBUG -- : make: *** [clean] Error 1
@jfriedly
jfriedly / enumerate-example.rst
Created July 11, 2013 22:23
How to iterate over something with an index using enumerate. Keywords: counter, i, iterable, list, dict, dictionary, Python