Git Product home page Git Product logo

ordered-keyword-args's Introduction

Ordered Keyword Args for Python

Normally, Python doesn't preserve the order of keyword arguments passed to a function. That’s because it uses a dictionary to pass the arguments, and Python’s dictionary doesn’t remember the order in which elements are added.

That’s a problem in certain situations. For instance, if you want to define data models for serialization:

def define_record(record_name, **fields) :
    print "record: %s" % (record_name,)
    print "fields: %s" % (fields.items(),)

define_record("Point", x="Int32", y="Int32")

This is the output you get:

record: Point
fields: [('y', 'Int32'), ('x', 'Int32')]

As you can see, the order of the fields is wrong.

The orderedkwargs module solves this problem. Add the @orderedkwargs decorator, and you'll receive the keyword arguments as a sequence of (name, value) tuples. Note that keyword arguments are received as var-args, so a single asterisk (*) should be used instead of two.

from orderedkwargs import orderedkwargs

@orderedkwargs
def define_record(record_name, *fields) :
    print "record: %s" % (record_name,)
    print "fields: %s" % (fields,)

define_record("Point", x="Int32", y="Int32")

Now the output is:

record: Point
fields: (('x', 'Int32'), ('y', 'Int32'))

How it works

The @orderedkwargs decorator wraps the function in an outer function that inspects the stack, parses the caller's byte code, and finds the order in which the keyword arguments are passed.

Caveats

Because the orderedkwargs module depends on the internals of the Python VM, it is implementation specific. This particular code has been tested with CPython 2.7 and works only with that version. Also, don't use this in performance sensitive code. It is suitable for one-time tasks such as initialization at startup.

ordered-keyword-args's People

Contributors

kssreeram avatar

Watchers

James Cloos avatar Bertalan Pecsi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.