Skip to content

Instantly share code, notes, and snippets.

@pydemo
pydemo / recover_source_code.md
Created April 9, 2018 21:00 — forked from simonw/recover_source_code.md
How to recover lost Python source code if it's still resident in-memory

How to recover lost Python source code if it's still resident in-memory

I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

Attach a shell to the docker container

Install GDB (needed by pyrasite)

apt-get update && apt-get install gdb
@pydemo
pydemo / recover_source_code.md
Created April 9, 2018 21:00 — forked from simonw/recover_source_code.md
How to recover lost Python source code if it's still resident in-memory

How to recover lost Python source code if it's still resident in-memory

I screwed up using git ("git checkout --" on the wrong file) and managed to delete the code I had just written... but it was still running in a process in a docker container. Here's how I got it back, using https://pypi.python.org/pypi/pyrasite/ and https://pypi.python.org/pypi/uncompyle6

Attach a shell to the docker container

Install GDB (needed by pyrasite)

apt-get update && apt-get install gdb
import scala.language.postfixOps
scala> object ? {def z(s: Any) {println(s)}}
defined object $qmark
scala> ?z "Hello world"
Hello world
@pydemo
pydemo / build.sbt
Created May 17, 2018 19:28 — forked from bartschuller/build.sbt
Using Scala's Dynamic trait and reflection to implement delegation. If we swap reflection for macros it will be fast and typesafe again. I guess this could then replace the AutoProxy compiler plugin.
name := "delegation"
scalaVersion := "2.10.0"
libraryDependencies <+= (scalaVersion)("org.scala-lang" % "scala-reflect" % _)
scala> :paste -raw
// Entering paste mode (ctrl-D to finish)
package Foo
class Bar
// Exiting paste mode, now interpreting.
@pydemo
pydemo / dir.scala
Created May 22, 2018 14:33 — forked from dajobe/dir.scala
Get the methods on a class / object - aka python dir()
// Inspired by http://stackoverflow.com/questions/2886446/how-to-get-methods-list-in-scala
def dir(x: AnyRef): Unit = println(x.getClass.getMethods.map(_.getName).distinct.sorted.mkString(" "))
scala> dir("")
charAt codePointAt codePointBefore codePointCount compareTo compareToIgnoreCase concat contains contentEquals copyValueOf endsWith equals equalsIgnoreCase format getBytes getChars getClass hashCode indexOf intern isEmpty lastIndexOf length matches notify notifyAll offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring toCharArray toLowerCase toString toUpperCase trim valueOf wait
@pydemo
pydemo / ArithmeticParser.scala
Created July 13, 2018 14:44 — forked from daixque/ArithmeticParser.scala
Arithmetic parser/evaluator written in scala using parser combinator
import scala.util.parsing.combinator._
abstract class Value {
def eval():Double
}
case class Number(n:Double) extends Value {
def eval():Double = n
}
case class Binomial(op: String, v1:Value, v2:Value) extends Value {
def eval():Double = op match {
@pydemo
pydemo / genshim.cpp
Created August 29, 2018 15:21 — forked from nohbdy/genshim.cpp
Shim DLL Generation
// Code originally found on http://www.hulver.com/scoop/story/2006/2/18/125521/185
// Created by Rogerborg
#include <stdio.h>
#include <windows.h>
#include <list>
using namespace std;
// Change this to the name of the DLL that you want to clone
#define CLONE_DLL "WS2_32"
@pydemo
pydemo / gist:9ac8ea101e3f4fd7d628d66046b28c79
Last active September 7, 2018 16:50
Iterator support in Cython.

To make extension iterable you define iter on it.

cdef class I:

   cdef:

   list data
@pydemo
pydemo / gist.md
Last active September 7, 2018 16:53
Comparison support in Cython

Cython extension types do not support individual comparison methods like

_eq_

_lt_

_le_

Instead Cython provides single method richcmp(x,y,op)