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: Compare Visual Studio Product Offerings | Visual Studio

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:

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.