Tag: dev

  • Configure Python on Windows

    All right, I have a Windows machine. It’s a PITA, but it’s here. And for some reason I started doing some Python testing on it. So this is how I managed to do it:

    Preparation:

    • Install python with choco (choco install -y python)
    • Run PowerShell as Administrator
      • Execute Set-ExecutionPolicy -ExecutionPolicy Unrestricted (we’ll see why in a very short time)

    Now to code it’s pretty similar to *NIX:

    • Create your code folder
    • Set up a python venv (python -m venv .env)
    • In VS Code, choose this interpreter

    So why the PowerShell stuff? Cause to activate the environment VS Code needs to execute a .ps1 script. Which it can’t, cause “executing scripts is disabled on this machine”, which seems to be the default setting.

    All in all, surprisingly straightforward. And I just noticed even the *NIX keyboard shortcuts (CTRL-A, CTRL-K, for example) work in the terminal window now. Crazy.

  • The 6 ways of returning data from an Azure Function

    Why have one if you can have many? Well, beats me, but I thought I’d collect them here so I have my personal reference. Disclaimer: This is only for JavaScript based functions (most of the examples on the MS pages are C#).

    So, most of them have to do with the file function.json  file:

    {
      "bindings": [
        {
          "type": "httpTrigger",
          "name": "req",
          "and": "so on ..."
        },
        {
          "type": "http",
          "name": "thisIsAnOutputBinding",
          "direction": "out"
        },
        {
          "type": "someOtherType",
          "name": "anotherBinding",
          "direction": "out"
        }
      ]
    }
    

    More precisely, with the “out” type binding that is defined in the 2nd “bindings” object. The “out” binding has a “name” property, which is basically relevant for all of the methods.

    !!!!! WARNING !!!!! – It seems the name of the binding property must be camelCase**. At least I consistently get “invalid binding” errors when I use snake_case**

    Method 1 – context property

    The context object has one property per defined “out” binding. So if our property is named “thisIsAnOutputBinding”, our result property is context.thisIsAnOutputBinding , or as MicroSoft puts it:

    Outputs (bindings of direction === "out" ) can be written to by a function in a number of ways. In all cases, the name property of the binding as defined in function.json corresponds to the name of the object member written to in your function.

    Simply put, you just assign the value to a context  member and you’re done:

    module.exports = function (context, req) {
      context.thisIsAnOutputBinding = {
        my: "return value",
        "in this case": "an object",
      };
      // contect.done() is only needed for synchronous functions context.done();
    };
    

    Source here.

    Method 2 – return an object (async functions only)

    With the bindings given above, you can assign the values to the context property, but you can also return an object whose keys correspond to the binding names:

    module.exports = async function (context) {
      return { thisIsAnOutputBinding: 42, anotherBinding: 43 };
      // of course, instead of 42/43 you can put any js object here.
    };
    

    Source here.

    Method 3 – using context.bindings

    This is basically a variant of method 1. Why does it exist? No one knows. Apparently the context object has a .bindings  property, which in turn again has properties which name-match the defined out bindings. So this is another possibility, and I think the return  is unnecessary:

    module.exports = async function (context) {
      let retMsg = "Hello, world!";
      context.bindings.thisIsAnOutputBinding = {
        body: retMsg,
      };
      context.bindings.anotherBinding = retMsg;
      return;
    };
    

    Source here.

    Method 4 – using context.done()

    If you’re using a sync method, you can’t return an object, but you can call context.done(err, obj) .

    module.exports = async function (context) {
      context.done(null, {
        thisIsAnOutputBinding: { text: "hello there, world", anotherBinding: 42 },
      });
    };
    

    Source here.

    Method 5 – just return it already (only async functions)

    Probably someone said “Well, if I only have one output binding, why should I explicitly address it?”, which is a valid thought. So another way to return data was invented.

    For this the configuration in function.json looks a bit different:

    {
      "bindings": [
        { "type": "someType", "name": "req", "direction": "in" },
        { "type": "someOutType", "direction": "out", "name": "$return" }
      ]
    }
    

    You have to set exactly one “out” type binding, and the name must be “$return”. Then you can do this:

    module.exports = async function (context, req) {
      return "woohooo!!";
    };
    

    Source here.

    Methods 6 – special for http outputs

    You thought we were done? Noooooo. For http methods, you can diretly return the body object which is then used to create the body

    // actually NO IDEA if this works sync, async or both
    // my guess is: sync, because "context.done() is implicitly called"
    module.exports = async function (context, req) {
      rv = { body: "<html/>", status: 201 };
      context.res.send(rv);
    };
    

    Source here.

    Summary

    Let’s just say it’s a mess, and too many ways ruin the map.

    It works though.

  • Misc Django I – forms

    Custom form errors

    If you want to validate something in the view, and return with a custom error message in the same form, you can use the [Form.add_error][1](fieldname, errorstring) method. And then, of course, return to the previous template.

    class MyView(View):
        def get(self, request):
            data = form.cleaned_data
            if len(res) > 0:
                form.add_error( 'login', "Diese Personalnummer existiert bereits!")
            return render(request, 'my_template.html', {'form': form})
    

    Dynamic choice fields in forms

    You want a form which fills its choice field from the database? And if the database changes, if you reload the page, the form should change as well? Of course! Django got you covered.

    class UserForm(forms.Form):
        def __init__(self, *args, **kwargs):
            super(UserForm, self).__init__(*args, **kwargs)
            self.fields['site'] = forms.ModelChoiceField( label="Site", queryset=Site.objects.all().order_by('name'), )
            for field in ('department', 'office', 'phone'):
                self.fields.move_to_end(field)
    
        login = forms.CharField(label="Login")
        email = forms.EmailField(label="Email")
        site = None # this is set in __init__() :)
        department = forms.CharField(label="Department")
        office = forms.CharField(label="Office")
        phone = forms.CharField(label="Phone")
    

    … now, why the “for field in (‘department’ …)” line you ask?

    Simple. The fields dict is an OrderedDict. If you replace a field it is appended to the end again. So in the form the “Site” input box would be displayed last, although it makes more sense to display it where it is in the original definition.

    Using .move_to_end() you can re-adjust this. If someone knows a better method … feel free to tell me.

    (Sources: here)

  • Python & Visual Studio code

    The official python plugin claims that the interpreters of Pipenv are automatically found.

    They are not.

    At least not on my machine.

    Here’s how you set them.

  • JIRA and Python

    I really came to hating JIRA with a passion. And now I have to create about 350 tickets.

    Naturally I don’t do this by hand. But using the JIRA API is kind of … hard, there is a Python library, but usage is rather sparsely documented, and this whole thing is just annoying as hell.

    Although when you did it, it’s quite simple. Here’s an example of how to create a ticket with due date and estimate set from a CSV file:

    from pprint import pprint, pformat
    from getpass import getpass
    
    import jira
    
    
    def do_shit():
        url = read("JIRA url:")
        username = read("JIRA username:")
        password = getpass("JIRA password:")
        jira_inst = jira.JIRA(url, basic_auth=(username, password))
    
        test = {
            "project":          {"key": "TEST"},
            "issuetype":        {"name": "Task"},
    
            "labels":           ["deleteme", "whatisthis"],
    
            "summary":          "woho",
            "description":      "wohooooo",
            "duedate":          "2017-11-11",
    
            "timetracking":    {"originalEstimate": "4d"},
        }
    
        created = jira_inst.create_issues([test])
        pprint(created)
    
    
    if __name__ == "__main__":
        do_shit()
    

    So simple.

  • jq makes AWS “describe-instances” actually useful

    Just so I don’t forget 🙂

    aws ec2 describe-instances | \
      jq '.Reservations[].Instances[] | {IP: .PrivateIpAddress, ID: .InstanceId, Name: .Tags[] | select(.Key=="Name").Value}'
    
  • Syntax highlighting with wordpress

    This is just a test for syntax highlighting. Which I really really really wanted to have. Even if it’s WordPress and not something cool like hugo.

    So, let’s try:

    import random
    
    class RndTest:
        def __init__(self):
            print("Oy! 'this is python!")
            self.rnd_seed = random.random()
    
        @classmethod
        def do_nothing(cls, nothing):
            print("Doing {} now ;)".format(nothing))
    

    That doesn’t look so bad, right?

    How to do this

    • install the crayon syntax highlight plugin
    • when writing posts, your toolbar will have a new icon looking like this: <>
    • press it, and an “add code” dialog will open
    • do your thing
    • save
    • done

    Like.

    Dislikes

    • I don’t know which JS engine this thing uses (if any public one in particular)
    • I like highlight.js

    (So, none really)

    Alternative plugins (untested)

    To be honest, the only real contender in installation base and features seems to be Syntax Highlighter Evolved. I have not tried it, but if you don’t like crayon, that looks like the one to go.

  • PyCharm, Arch linux & Python 3.6

    Love Python. Love PyCharm. Love Arch Linux.

    Unfortunately Arch sneakily updated Python to 3.6. Cool, new version … but hey, why don’t my debug runs in PyCharm work any more??

    ImportError: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory
    

    Yup, pretty confusing. It seems unable to find shared python 3.5 library. Well. After some cursing, turns out the solution is pretty simple (if you know what to do):

    • get pyenv
    • use pyenv to install Python 3.5.2, but with -enable-shared option set
    • use this python version for PyCharm projects (it does not matter if it’s in a virtualenv or not)

    Like this:

    $ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.2
    [...]
    $ sudo $HOME/.pyenv/versions/3.5.2/bin/python "/opt/pycharm-professional/helpers/pydev/setup_cython.py" build_ext --inplace
    [...]
    $ _
    

    That solved it for me 🙂

    References:

  • Really annoying thread properties

    This sucks monkey ass, mainly because I didn’t think of that before. And that’s just one example why multi-threaded (soon to be -processing, probably) applications are hard.

    import subprocess as sp
    import time
    import os
    from threading import Thread
    
    
    class MyThread(Thread):
    
        def __init__(self, mydir):
            super().__init__()
            self.mydir = mydir
    
        def run(self):
            os.chdir(self.mydir)
            time.sleep(2)
            print("I'm (%s) in directory %s"
                  % (str(self), os.getcwd()))
    
    if __name__ == "__main__":
        MyThread("/tmp").start()
        time.sleep(1)
        MyThread("/").start()
    

    Result is:

    I'm (<MyThread(Thread-1, started 140195858716416)>) in directory /
    I'm (<MyThread(Thread-2, started 140195850323712)>) in directory /
    
  • jq stuff

    Waah. In case I *ever* need this again.

    aws ec2 describe-security-groups | \
        jq '.SecurityGroups | \
            map(select(has("Tags"))) | \
            map(select(.Tags[].Key|contains("Name"))) | \
            .[] | \
            [ .Tags , .GroupId ] | \
            [ (.[0]|map(select(.Key=="Name")))[0].Value, .[1] ] | \
            join("=") '
    

    What does it do? It makes a list

    $NAME_TAG=$GROUP_ID
    

    … for each security group which has a name tag. Yeah, right.