Tagged: python Toggle Comment Threads | Keyboard Shortcuts

  • penguin 11:06 on 2018-10-17 Permalink | Reply
    Tags: , , python,   

    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(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)

     
  • penguin 18:27 on 2018-04-12 Permalink | Reply
    Tags: python, vscode   

    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.

     
  • penguin 18:13 on 2017-10-17 Permalink | Reply
    Tags: jira, python   

    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.

     
    • Mika 14:40 on 2017-12-20 Permalink | Reply

      Hi Axel, hast Du auch einen Twitter Account, in dem Du über neue Updates aus Deinem Blog informierst, oder ist der RSS Feed meine (einzige?) Anlaufstelle?

      • penguin 18:29 on 2017-12-27 Permalink | Reply

        Hey Mika, gute Idee eigentlich 🙂 . Aktuell ist es nur RSS. Ich könnte allerdings einen Twitter-Feed dafür einrichten … .
        Freut mich allerdings dass du auf dem Laufenden bleiben möchtest 😀 . Und sorry wegen der langen Antwortzeit – ich war 4 Wochen im Urlaub. Dringend notwendig.

  • penguin 14:45 on 2017-01-17 Permalink | Reply
    Tags: , , pycharm, python   

    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:
     
  • penguin 11:27 on 2016-01-29 Permalink | Reply
    Tags: , python   

    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.

    [code]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()
    [/code]

    Result is:

    [code]I’m (<MyThread(Thread-1, started 140195858716416)>) in directory /
    I’m (<MyThread(Thread-2, started 140195850323712)>) in directory /[/code]

     
  • penguin 10:35 on 2015-06-22 Permalink | Reply
    Tags: convenience, , python   

    OpenStack floating IP convenience 

    Problem: I am working in a tenant which has a couple of hosts with floating IPs assigned. I always have to look them up either manually using the command line clients (and dealing with all those UUIDs), or manually in the web GUI. Didn’t like.

    Solution: Python script, which outputs FLOATING_IP -> HOST_NAME.

    Here it is.

    #!/usr/bin/env python
    
    from novaclient import client
    import novaclient.v2.floating_ips as os_fips
    import novaclient.v2.servers as os_servers
    import novaclient.v2.networks as os_networks
    
    #from pprint import pprint as pp
    import os
    from sys import exit
    
    
    def error(printme):
        print("ERROR: {}".format(printme))
        exit(-1)
    
    def check_env():
        for a in ("OS_TENANT_ID", "OS_TENANT_NAME", "OS_AUTH_URL", "OS_USERNAME", "OS_PASSWORD"):
            if not os.environ.get(a): error("Please set ${}".format(a))
    
    def get_client():
        check_env()
        return client.Client(2,
                             os.environ["OS_USERNAME"],
                             os.environ["OS_PASSWORD"],
                             os.environ["OS_TENANT_NAME"],
                             os.environ["OS_AUTH_URL"])
    
    
    if __name__=="__main__":
        nova = get_client()
        fipman = os_fips.FloatingIPManager(nova)
        servman = os_servers.ServerManager(nova)
        netman = os_networks.NetworkManager(nova)
    
        ips = fipman.list()
        srs = servman.list()
    
        id2server = {}
        for a in srs:
            id2server[a.id] = a
    
        ips = [(ip.ip, ip.instance_id) for ip in ips]
        # filter out unused floating ips (which have  as instance id)
        ips = filter(lambda x: x[1], ips)
        # create (IP, SERVER_NAME pairs)
        ips = map(lambda x: (x[0], id2server[x[1]].name), ips)
        # sort for convenience by host instance name
        ips = sorted(ips, key=lambda x: x[1].lower())
    
        for a in ips:
            print("{:18s} {}".format(a[0], a[1]))
    

    Sample output:

    $ tools/list_ips
    10.64.108.163      ab_1695_1_dml
    10.64.108.199      ab_1695_1_jump
    10.64.108.160      media0
    10.64.109.97       RJD1_CouchbaseA1
    10.64.109.99       RJD1_JumpServer
    10.64.109.98       RJD1_OpenshiftMaster
    10.64.109.148      tla-centos66

    Groovy.


    Main sources:

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel