Skip to content

Instantly share code, notes, and snippets.

View benjbaron's full-sized avatar

Benjamin Baron benjbaron

View GitHub Profile
@benjbaron
benjbaron / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@benjbaron
benjbaron / QGraphicsSceneTest.cpp
Last active April 22, 2022 03:13
Qt QGraphicsScene click, select, move, resize, delete QGraphicsItems
#include <QtGui>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
class CustomItem : public QGraphicsEllipseItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
@benjbaron
benjbaron / CSVParser.java
Created July 10, 2015 08:33
Read CSV with a header
public static ArrayList<Map<String, String>> readCSV(String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
ArrayList<Map<String, String>> al = new ArrayList<Map<String, String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String[] headerLine = br.readLine().split(","); // reads the header of the file
String line = br.readLine(); // reads the first line of data
while (line != null) {
String[] values = line.split(",");
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < values.length; i++) {
@benjbaron
benjbaron / CSVParser.cpp
Created July 10, 2015 08:35
Parse a CSV file with Qt with regex or by specifying the characters delimiters, escape and quote
class CSVParser
{
public:
CSVParser() {}
static int parseString(QString &buffer, QStringList &fields, QString delimChars, QString quoteChars, QString escapeChars) {
QString field; // String in which to accumulate next field
bool escaped = false; // Next char is escaped
bool quoted = false; // In quotes
QChar quoteChar = 0; // Actual quote character used to open quotes
@benjbaron
benjbaron / ConvertFromGEOStoOGR.cpp
Last active April 26, 2018 13:33
Use OGR to parse a Shapefile (here, we want to parse an OSM Shapefile to get the main roads)
OGRLineString * convertFromGEOStoOGR(LineString * ls) {
OGRLineString * lineString = (OGRLineString*) OGRGeometryFactory::createGeometry(wkbLineString);
for(int i = 0; i < ls->getNumPoints(); ++i) {
Point * pt = ls->getPointN(i);
lineString->addPoint(pt->getX(), pt->getY());
}
return lineString;
}
@benjbaron
benjbaron / checkFile.cpp
Created July 16, 2015 17:06
Qt check whether there is a file with the given extensions
QString checkFile(QString foldername, QStringList exts, QString filename) {
foreach(auto ext, exts) {
QFileInfo check(foldername+"/"+filename+"."+ext);
if(check.exists() && check.isFile())
return check.absoluteFilePath();
return QString();
}
}
void OGRLayer::SetSpatialFilterRect( double dfMinX, double dfMinY,
double dfMaxX, double dfMaxY )
{
OGRLinearRing oRing;
OGRPolygon oPoly;
oRing.addPoint( dfMinX, dfMinY );
oRing.addPoint( dfMinX, dfMaxY );
oRing.addPoint( dfMaxX, dfMaxY );
@benjbaron
benjbaron / The Technical Interview Cheat Sheet.md
Created May 22, 2016 07:58 — forked from tsiege/The Technical Interview Cheat Sheet.md
This is my technical interview cheat sheet. Feel free to fork it or do whatever you want with it. PLEASE let me know if there are any errors or if anything crucial is missing. I will add more links soon.

Studying for a Tech Interview Sucks, so Here's a Cheat Sheet to Help

This list is meant to be a both a quick guide and reference for further research into these topics. It's basically a summary of that comp sci course you never took or forgot about, so there's no way it can cover everything in depth. It also will be available as a gist on Github for everyone to edit and add to.

Data Structure Basics

###Array ####Definition:

  • Stores data elements based on an sequential, most commonly 0 based, index.
  • Based on tuples from set theory.
@benjbaron
benjbaron / multiprocessing_two_processes.py
Created April 26, 2018 13:32
Use the `multiprocessing` lbrary to run two processes `f1` and `f2` in parallel and get their respective outputs.
import time
from multiprocessing import Pool
# can also work with ThreadPool, by importing
# from multiprocessing.dummy import Pool
def f1(a,b):
print("run f1(%s,%s)" % (a,b))
time.sleep(2)
print("end f1")
@benjbaron
benjbaron / .gitconfig
Created March 16, 2019 21:51
.gitconfig
[github]
user = benjbaron
[push]
default = current
[pull]
rebase = true
[alias]
st = status
stp = status --porcelain
ci = commit --amend --no-edit