Categories
Uncategorized

Buy freedom with cash

Should you quit your job to do what you want? It’s a bad idea. But is it really what you need?

A break from corporate life. Peace. Freedom. Being your own boss. I hate being micromanaged at work so I feel like quitting. But I also feel I deserve a holiday in the prime of my life. I am almost 31 years old, I am tired of alarm clocks. I am tired of nit picky code reviews.

I have enough cash to ride it out for a couple of years then I will have to work again.

Categories
Uncategorized

A blog in so many lines of code

The following:

class BlogTitle extends React.Component {
	constructor(props) {
		super(props);
	}
	render() {
		return <h2>{this.props.title}</h2>;
	}
	
}

class Title extends React.Component {
	constructor(props) {
		super(props);
	}
	render() {
		return <h1>Sam's Blog</h1>;
	}
	
}
 
// Sam Squire
var template = {
	"data": {
		"posts": [
			{
				"title": "Additive GUI blog",
				"body": "It's great to use additive GUIs to blog."
			},
				{
				"title": "Additive GUI blog",
				"body": "It's great to use additive GUIs to blog."
			}
		]
	},
	"predicates": [
		"header above content",
		"header hasClass header"
	
	],
	"widgets": {
		"header": {
			"predicates": [
				"logo leftOf search"
			]
		},
		"content": {
			"predicates": [
				"title",
				"blogs",
				"title above blogs",
				"blogs backedBy .posts",
				"blogs mappedTo blogItem",
				"blogs key .title",
				"blogs hasClass offset-md-2"
			]
		},
		"blogTitle": {"react": BlogTitle},
		"title": {"react": Title},
		"blogItem": {
			"predicates": [
				"blogTitle above blogBody",
				"blogBody hasContent .body",
			
			],
		}
	}
};

Renders to this.

See additive-guis for more.

Categories
Uncategorized

The world is awful

Suicide rates are going up for men. People are losing their livelihood. Houses are unaffordable for normal people. China is sending Muslims to concentration camps. There is a war on cash.

Everything is set to get that much worse, there’s a lack of compassion from the world’s elites. Poverty hasn’t changed.

Elites are quite simply not doing anything that advances the interests of the poor, sick and disabled. They only work toward things that advance the interests of the wealthy and powerful. So this group of people is shafted by laws and regulations.

Regulation is suffocating entire industries from competing. So most industries have sizable moats around their businesses that keeps competition out. Anti-trust law seems to sleep for years at a time while industries do what they want.

Software is a mess. Software suffers from unnecessary complexity and slowness. Latency is awful.

Labour has lost the capital versus labour war.

I don’t really know what to make of all this. But it’s pretty dire on the front lines.

Categories
Uncategorized

Creating a database

You can create a fast key-value store database with a trie and a hashmap.

class Tree():
    def __init__(self, value, partition_key, lookup_key):
        self.value = value
        self.partition_key = partition_key
        self.lookup_key = lookup_key
        self.left = None
        self.right = None

    def insert(self, value, partition_key, lookup_key):
        if self.left == None and value <= self.value:
            self.left = Tree(value, partition_key, lookup_key)
        elif self.right == None and value > self.value:
            self.right = Tree(value, partition_key, lookup_key)
        elif value > self.value:
            self.right.insert(value, partition_key, lookup_key)
        elif value < self.value:
            self.left.insert(value, partition_key, lookup_key)
        elif self.value == "":
            self.value = value
            self.partition_key = partition_key
            self.lookup_key = lookup_key
        return self

    def walk(self, less_than, stop):
        if self.left:
            yield from self.left.walk(less_than, stop)
        if less_than <= self.value and self.value <= stop:
            yield self.partition_key, self.value, self.lookup_key
        if self.right:
            yield from self.right.walk(less_than, stop)

class Db():
    def __init__(self):
        self.db = {}
        self.sort_index = pygtrie.CharTrie()
        self.between_index = {}

    def store(self, partition_key, sort_key, item):
        lookup_key = partition_key + ":" + sort_key
        self.db[lookup_key] = item
        self.sort_index[lookup_key] = sort_key
        if sort_key not in self.sort_index:
            self.sort_index[sort_key] = pygtrie.CharTrie()
        self.sort_index[sort_key][partition_key] = lookup_key
        if partition_key not in self.between_index:
            self.between_index[partition_key] = Tree("", None, None)
        self.between_index[partition_key].insert(sort_key, partition_key, lookup_key)

    def query_begins(self, partition_key, query, sortmode):
        def items():
            for sort_key, lookup_key in self.sort_index.iteritems(prefix=partition_key + ":" + query):
                yield (sort_key, lookup_key, self.db[sort_key])
        return sorted(items(), key=lambda x: x[0], reverse=sortmode == "desc")


    def query_pk_begins(self, partition_key, query, sortmode):
        def items(partition_key, query):
            for sort_key, value in self.sort_index.iteritems(prefix=partition_key):
                for partition_key, value in value.iteritems(prefix=query):
                    yield (sort_key, partition_key, self.db[value])
        return sorted(items(partition_key, query), key=lambda x: x[0], reverse=sortmode == "desc")

    def query_between(self, partition_key, from_query, to_query, sortmode):
        def items(partition_key, from_query, to_query):
            for partition_key, sort_key, lookup_key in self.between_index[partition_key].walk(from_query, to_query):
                yield (sort_key, partition_key, self.db[lookup_key])
        return sorted(items(partition_key, from_query, to_query), key=lambda x: x[0], reverse=sortmode == "desc")

    def query_before_than(self, partition_key, prefix, target_sort_key, sortmode):
        def less_than():
            for lookup_key, sort_key in self.sort_index.items(prefix=partition_key + ":" + prefix):
                if sort_key < target_sort_key:
                    yield (sort_key, lookup_key, self.db[lookup_key])
        return sorted(less_than(), key=lambda x: x[0], reverse=sortmode == "desc")

    def query_greater_than(self, partition_key, prefix, target_sort_key, sortmode):
        def less_than():
            for lookup_key, sort_key in self.sort_index.items(prefix=partition_key + ":" + prefix):
                if sort_key > target_sort_key:
                    yield (sort_key, lookup_key, self.db[lookup_key])
        return sorted(less_than(), key=lambda x: x[0], reverse=sortmode == "desc")

This sourcecode allows range queries too!


db = Db()
db.store("user#samsquire", "following#dinar", ["Messages 1"])
db.store("user#samsquire", "following#someonelse", ["Messages 2"])
db.store("user#samsquire", "message#2020-05-01T14:39", ["Messages 1"])
db.store("user#samsquire", "profile", ["profile"])
db.store("user#samsquire", "message#2020-06-01T14:39", ["Messages 2"])
db.store("user#samsquire", "message#2020-07-01T09:30", ["Messages 3"])
db.store("user#samsquire", "message#2020-08-01T14:39", ["Messages 4"])
db.store("user#dinar", "message#2020-09-01T14:39", ["Messages 5"])

print("find followers of user user#samsquire")
print(list(db.query_begins("user#samsquire", "following", sortmode="asc")))

print("find messages by samsquire between dates")
print(list(db.query_between("user#samsquire", "message#2020-06-01", "message#2020-07-01~~~~~~~~~", sortmode="asc")))

print("find all users who sent messages")
print(list(db.query_pk_begins("message", "user", sortmode="desc")))

print("before than")
print(list(db.query_before_than("user#samsquire", "message", "message#2020-07", "asc")))

print("greater than")
print(list(db.query_greater_than("user#samsquire", "message", "message#2020-07", "asc")))