Created
August 21, 2014 00:10
-
-
Save cheecheeo/d08e413cdb51e0547c41 to your computer and use it in GitHub Desktop.
namedtuple derivatives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namedtuple is implemented an eval'd string in python, this implementation would likely copy that | |
```python | |
import geohash | |
Foo = initializednamedtuple('Foo', [ | |
'lat', | |
'long', | |
('geohash', lambda self, geohash: geohash if geohash else geohash.geohash(self.lat, self.long)) | |
]) | |
``` | |
I'm not even sure if closure conversion would work right | |
List arguments are optionally tuples, if a tuple is present the second value is a function, if present as a pair the first function argument is 'self', otherwise the argument is the argument passed | |
```python | |
Foo = typednametuple('Bar', [ | |
Integer('lat'), | |
Integer('long'), | |
Optional(Float('geohash', lambda self: geohash.geohash(self.lat, self.long))) | |
] | |
``` | |
For Optionals, the second argument is the initialization function if it isn't passed and takes self. | |
Integer -> int | |
Float -> float | |
String -> str | |
pystachio like stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment