The thread formerly known as "Weekend Coding"

This sort of automagic refactoring/editing wizardry is also easier to pull off in a statically typed language like C++.

2 Likes

The Community Edition has 99% of what you probably would want to do anyway, looking at this comparison: https://visualstudio.microsoft.com/vs/compare/

A post was merged into an existing topic: Advent of Code

Python’s flickrapi is pretty good. I trimmed my flickr photos down to 1000 using my own criteria rather than SmugMug’s most-recent hammer. It wasn’t so much a script as an interactive python session.

The engine used to make Wargroove is now open source.

1 Like

So I actually ran into a “real” coding problem at work that activated the Zachtronics area of my brain. It seems so simple on the surface that it should be trivial. There should just be some easy trick to make it happen, especially in Python. Yet, it actually took me like 20 lines and two recursions, and that feels wrong. There should definitely be some easier way to do this. Anyone want to try?

Ok, so there is a tree, but you don’t have to worry about the whole tree. We only have to look at one branch of the tree. We start with a single node and just follow its parents to the root of the tree and we get a single branch, which is now represented as a dictionary like this:

{'a': 1, 'parent': {'a': 2, 'parent': {'a': 3: 'parent': None}}}

For any such given tree branch, remove all the ‘parent’ keys and instead replace them with ‘child’ keys, basically reversing the dictionary so that the root of the tree is the top level dictionary. Preserve all other keys and values on all the dictionaries. In this case, the only other key is ‘a’. This must work for any branch that is within the recursion limit. The correct output for our test case would be:

{'a': 3, 'child': {'a': 2, 'child': {'a': 1, 'child': None}}}

Here is the code I have written to do this that I feel is far from perfect:

    def parent_to_child(tree):

        def recursive_dict_to_stack(rdict):
            stack = []
            parent = rdict.pop('parent', None)
            if parent is not None:
                stack += recursive_dict_to_stack(parent)
            stack.append(rdict)
            return stack

        stack = recursive_dict_to_stack(tree)
        stack.reverse()

        def stack_to_newtree(stack):
            top = stack.pop()
            if stack:
                top['child'] = stack_to_newtree(stack)
            return top

        return stack_to_newtree(stack)

As you can see I’m basically recursively traversing the branch to push every node onto a stack. Then I peel them off and recursively build a new branch in reverse.

Can you do better?

Don’t bother with recursion.

def invert(tree: dict):
    curr = tree
    stack = []
    while curr is not None and 'parent' in curr:
        stack.append(curr)
        curr = curr.pop('parent')
    
    root = stack.pop()
    curr = root
    while len(stack) > 0:
        curr.pop('parent', None)
        curr['child'] = stack.pop()
        curr = curr['child']

    curr['child'] = None
    return root

Copyright me, all rights reserved

I mean, yeah, that’s good, but it’s basically the same solution, only iterative instead of recursive. You got one loop to fill the stack and one loop to peel it off. I’m looking for a solution that is structurally different.

So not recursive, and not iterative? Some itertools or zip type fancy two liner or something?

Yeah, that’s what I was hoping for. Or at least one loop instead of two.

How about a single recurse?

def invert2(tree, child=None):
    if tree is not None and 'parent' in tree:
        parent = invert2(tree['parent'], tree)
        tree['child'] = child
        tree.pop('parent', None)
        return parent or tree

    return tree
3 Likes

2 Likes

After so much searching I finally found a 2D game engine that is exactly what I want. It’s free. It is primarily for 2D games. It’s easy. It’s not super crazy fancy with all this stuff I will never use. It’s not super limited to just small games. It can make games for all three major desktop OSes, both major mobile OSes, and also just HTML 5. It’s made by the people who make Candy Crush. It is called Defold

I am going through all the tutorials, which is going very quickly since I know how to code already. I got plans to make some tiny games. Anything bigger would be shit-talk.

2 Likes

What language do you write in? Cursory look doesn’t say.

It’s lua, but I think there are ways to use something else if you really want. I don’t see the point of that, though.

1 Like

My webhost updated their server and the old version of Python was no longer there and so I had to update everything including Django and whatnot. 4 hours later, it’s all good.

But now, back on my laptop, the database population stuff isn’t working and it’s all come down to a fucking circular dependency between a Django app models page importing the main app’s admin page that also imports classes from the app’s model page.

I’m sure there’s some really obvious fix for this, but after 2.5 hours it’s not cracking through my brain. I understand the problem, but I can’t retrace the steps to how this became a problem now and was never a problem before. I hope it’ll come back to me in the morning, as for now I’m missing a step.

That sounds like a strange situation, but Django has some magic where you can often use a string of the thing’s name instead of the thing itself.

class MyModel(models.Model):
    foo = models.ForeignKey('OtherModel')  # If this was OtherModel the class, not 'OtherModel' the string, this would be an error


class OtherModel(models.Model):
    bar = models.IntegerField()
    etc...
1 Like

This is awesome:
https://hacks.mozilla.org/2019/04/pyodide-bringing-the-scientific-python-stack-to-the-browser/

Use numpy et al. in the browser!

Just learned about whitenoise.

Apparently it’s ok to serve static files directly out of Django now? At least Mr. Django says so, and I take his word for it.

I guess if your app is really small and insignificant, then this is fine. But seriously, the bare minimum of having nginx serve the static files is super easy and also a perfectly good architecture for even quite large sites.