Smarter positional argument handling
Project description
Smarter positional argument handling
Installation
Install via pip:
$ pip install typeargs
Done.
If you insist on the (slightly) harder way of installing, from source, you know how to do it already and don’t need my help.
I might later upload the source to: https://github.com/TaylorSMarks/typeargs
Documentation
When you call an ordinary Python function, it blindly passes in your positional arguments, and then it passes in your keyword arguments.
Suppose you had a function with a signature like this, as is the case for many things in Tkinter:
def makeWidget(master = None, contents = '', x = 0, y = 0, xSpan = 1, ySpan = 1):
You want to call this function now, without specifying a master. Here are your options normally:
makeWidget(None, 'This is impossible to read.', 5, 7, 2, 2)
Quick! Without looking at the signature, what the heck are those numbers at the end? Normally you solve it with this:
makeWidget(s = 'Better, but so much typing...', x = 5, y = 7, xSpan = 2, ySpan = 2)
Now that you include all the names, it’s easier to tell what each argument is, but its so verbose… it’s kind of heavy.
This is where typeargs comes in. If you go back to the original function and decorate it with typeargs as such:
@typeargs (Master, str, int, int, int, int)
def makeWidget(master = None, s = '', x = 0, y = 0, xSpan = 1, ySpan = 1):
Now a caller can just type this:
makeWidget('Hello World!', 2, 2, x = 5, y = 7)
Okay now, what’s what? Obviously our string is Hello World!, x is 5, y is 7, and we have two more arguments of ints. Those must be xSpan and ySpan, and no master was included, so it’ll use the default None.
By intelligently handling positional arguments based on their types, you can skip over arguments you want default values for, without using key-word arguments for every name.
Another example - lets say that you want to pass in master at the end:
makeWidget('Hello World!', 2, 2, x = 5, y = 7, master = Master('Window'))
makeWidget('Hello World!', 2, 2, Master('Window'), x = 5, y = 7)
Either one will work - we’re just making argument delivery just a bit smarter than normal.
One more example, new in 1.1.0, you can now match string arguments using regular expressions, if you want. No need to define the re objects in advance - they can be right in the decorator call if you want.
from typeargs import re, typeargs
phoneNumber = re(r'\d{3}-\d{4}')
ssn = re(r'\d{3}-\d{2}-\d{4}')
fullName = re(r'\w+ \w+')
@typeargs(fullName, ssn, phoneNumber)
def infos(fullName, ssn, phoneNumber):
print(fullName, ssn, phoneNumber)
infos('752-2723', 'Taylor Marks', '123-45-6789')
# Prints out ('Taylor Marks', '123-45-6789', '752-2723')
Copyright
This software is Copyright (c) 2016 Taylor Marks <taylor@marksfam.com>.
See the bundled LICENSE file for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file typeargs-1.1.0.tar.gz
.
File metadata
- Download URL: typeargs-1.1.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddeb315d55875f6c8083346c681a3c140f9f2e155e2726add160b76225e43d2c |
|
MD5 | 445fc52f0733b0398952c4ee58999e70 |
|
BLAKE2b-256 | a427cd4f96d1f50f39b91e5ddc638e7b7681c9d3cc443b43c0baab9fa3ce5efe |