Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42d640c478 | ||
|
|
9376d05d52 | ||
|
|
8c9fe9c141 | ||
|
|
bcbd78b8be | ||
|
|
711d51d4cf | ||
|
|
b931342984 | ||
|
|
9466035cc7 | ||
|
|
fabbefa7af | ||
|
|
96e32846ec | ||
|
|
0dce7022aa | ||
|
|
9c24838596 | ||
|
|
3451971f8e | ||
|
|
931e7ed937 | ||
|
|
5c70bc5b44 | ||
|
|
bcac8d5cfc | ||
|
|
77f0e00fa1 | ||
|
|
d57304504e | ||
|
|
46edf10e9d | ||
|
|
354ce2e307 | ||
|
|
98ebd5b40e | ||
|
|
c1924f3630 | ||
|
|
b579683287 | ||
|
|
ccdeea9ccf | ||
|
|
027665d9eb |
@@ -274,3 +274,6 @@ docs/_build/
|
||||
# PyBuilder
|
||||
target/
|
||||
*.pypirc
|
||||
MANIFEST
|
||||
|
||||
fmatch_idea.txt
|
||||
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 126 KiB |
|
Before Width: | Height: | Size: 58 KiB |
@@ -1,14 +0,0 @@
|
||||
# HumbleMatch
|
||||
Will **Sanitize your type-checks and duck-checks** for you
|
||||
|
||||
Available at [HumbleMatch@pypi]()
|
||||
|
||||
|
||||
## What the heck is it?
|
||||
|
||||
HumbleMatch is made to *add a zing* to your mundane type-checking and duck-checking code.
|
||||
|
||||
* Check your function arguments reliably, add *more flexible signatures* and most of all, have a lovely API.
|
||||
* Or just *sprinkle some assert* checks to ensure the data is what you expect it to be at any point.
|
||||
* Provide helpful debugging errors, *dont just fail with cryptic errors*
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
Title: HumbleMatch - Go sanitize your type-checking
|
||||
Date: 2015-06-19 16:56
|
||||
Authors: bendtherules
|
||||
Category: Projects
|
||||
Tags: HumbleMatch, Launch stuff, Projects
|
||||
Slug: HumbleMatch_Launch
|
||||
|
||||
## Intro
|
||||
|
||||
Hi, I am [Abhas](blog.codesp.in) and personally, I love Python mostly for its intuitive, flexible and beautiful API. But some things, I really hate doing. HumbleMatch is my attempt at solving that pain.
|
||||
Hi, I am [Abhas](http://blog.codesp.in) and personally, I love Python mostly for its intuitive, flexible and beautiful API. But some things, I really hate doing. HumbleMatch is my attempt at solving that pain.
|
||||
|
||||
|
||||
## The Pain Point #1
|
||||
@@ -241,4 +235,6 @@ Infact now that we know this, in Test 2 we can remove the repetion of `w([float]
|
||||
|
||||
I purposedly missed one method which is already there, called `w(obj).save_as(arg_name)`. How it works is that whatever it matches with is stored as `"arg_name"` and returns a dict filled with all such values, when `==`d with `other`. For eg `w([2, w(int).save_as("a"), OR(str,dict)).save_as("b")]) == [2,5,{"q":1}]` returns `{"a":5,"b":{"q":1}}`. I didnt document it well beacuse I am not mentally ok with a `==` call returning anything other than a `boolean`. Maybe, I will change the API in some way to make it better and document it then. But feel free to also try this, I am proud of this feature :)
|
||||
|
||||
I have not yet talked about how it helps duck-typing code, but I'll add it soon (there is no more API for it, just need to apply these to objects and use FunctionType as type).
|
||||
|
||||
And thats all for now, have fun and write more code.
|
||||
|
Before Width: | Height: | Size: 521 KiB After Width: | Height: | Size: 521 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
@@ -0,0 +1,34 @@
|
||||
# HumbleMatch
|
||||
Will **Sanitize your type-checks and duck-checks** for you
|
||||
|
||||
")
|
||||
|
||||
**Install using**
|
||||
`pip install humblematch`
|
||||
|
||||
|
||||
Github Repo at [github.com/bendtherules/humblematch](http://github.com/bendtherules/humblematch)
|
||||
|
||||
Available at [HumbleMatch@pypi](https://pypi.python.org/pypi/humblematch)
|
||||
|
||||
|
||||
## What the heck is it?
|
||||
|
||||
HumbleMatch is made to *add a zing* to your mundane type-checking and duck-checking code.
|
||||
|
||||
* Check your function arguments reliably, add *more flexible signatures* and most of all, have a lovely API.
|
||||
* Or just *sprinkle some assert* checks to ensure the data is what you expect it to be at any point.
|
||||
* Provide helpful debugging errors, *dont just fail with cryptic errors*
|
||||
|
||||
## Impress me
|
||||
|
||||
Lets say we want a Point class with two __init__ signatures - `Point(x,y)` and `Point([x,y])`.
|
||||
|
||||
Assume its `__init__` signature looks like `def __init__(self, x, y=None)`
|
||||
|
||||
This is the only typechecking code you'll need for valid arguments.
|
||||
|
||||
from humblematch import w
|
||||
assert( w([int, int]) == [x, y] or w([[int, int], None]) == [x, y] )
|
||||
|
||||
If you like it, please move to the [Tutorials](./Tutorial). Its a bit long, but that all the documentation youll need to know.
|
||||
@@ -1,125 +0,0 @@
|
||||
fmatch
|
||||
.matched
|
||||
.wrap
|
||||
.Any
|
||||
.old_matched
|
||||
|
||||
list_ -> fmatch -> [2,+(wrap([Int])*Any), [Any]*4 and fmatch(matched.len < old_matched[-1].len) ] (1)
|
||||
matches [2, 3,5,8,9,1, [1,2,3,4]]
|
||||
|
||||
*(wrap([Int])*Any) => *(wrap([Int]).times(Any))
|
||||
|
||||
list_ -> fmatch -> [2,Any, wrap(int)*4 and (matched[0]*2 > 5 or all([m >6 for m in matched]) ] -> _, a, b
|
||||
|
||||
|
||||
list_ -> fmatch -> [2,Any*Any,5]
|
||||
|
||||
basically need a mock object of sorts which stores everything, tries to stop evaluation, so that invalid (wrt types,values) ones work. invalid syntax still wont work.
|
||||
|
||||
|
||||
macros in python
|
||||
|
||||
pass in a string or function, have a great pattern matching like lisp and then do your own stuff ( like http://stackoverflow.com/questions/267862/what-makes-lisp-macros-so-special#4621882 )
|
||||
|
||||
look for reaasembler in python
|
||||
|
||||
(1) -> [2, multi_0, list(Any,Any,Any,Any)]
|
||||
|
||||
|
||||
wrap([Int]) = wrap_obj(data =[Int])
|
||||
creates wrap_obj with data = param and ops =[]
|
||||
if ops are applied store them
|
||||
apply right then correctly or later (???)
|
||||
|
||||
|
||||
|
||||
(wrap([Int, Float])*Any) = wrap_obj(data =[Int, Float], ops =[__mult__:Any])
|
||||
if ops[__mult__] is Any and data is list:
|
||||
this == wrap_obj(data =[ wrap_multi_obj(data = [Int, Float],multi = Any, ops =None) ], ops =None)
|
||||
if (data not list):
|
||||
# eg 2
|
||||
try normal __mult__ anyway
|
||||
if ops[__mult__] is not Any:
|
||||
# eg [2,3]*5
|
||||
try normal __mult__ anyway
|
||||
|
||||
## Done
|
||||
WrapObj
|
||||
WrapMultiObj
|
||||
|
||||
## Done
|
||||
wrap(Float).times(Any) == wrap_multi_obj(data = [Float],multi = Any, ops =None)
|
||||
wrap([2,3.0]).times(Any) == wrap_multi_obj(data = [[2,3.0]],multi = Any, ops =None)
|
||||
.times means the whole thing will repeat n times
|
||||
first doing all ops and, replace wrap_obj with wrap_multi_obj putting new data=[data]
|
||||
to repeat multiple items sequentially -> (2,3) say n times as in 2,3,2,3,2,3,.. do +(wrap([2,3.0]).times(Any))
|
||||
|
||||
### Not needed ###
|
||||
+(wrap([Int])*Any) == wrap(Int).times(Any)
|
||||
== wrap_multi_obj(data = [Int],multi = Any, ops =None)
|
||||
if prev is wrap_obj and data is list:
|
||||
removes prev node, and replace with wrap_multi_obj with same data
|
||||
|
||||
+ is the unwrap op
|
||||
|
||||
XXXX
|
||||
types of indirect matching
|
||||
|
||||
1. type-based (X This is what exact match does)
|
||||
2. Checking subclass-tree (issubclass)
|
||||
3. explicit conversion-based (converts 1st arg of fmatch to type of 2nd arg) -- usually quite good
|
||||
4. ABC-based somewhat
|
||||
|
||||
try to use abc for multiple types
|
||||
|
||||
functional prog is like passing little chunks of data, however less you can. but you will lose sense of what data it is.
|
||||
imperative prog is like having a structure of data and partially modifying it, yet sending the whole thing over.
|
||||
if you know about every data, fp - like experts.
|
||||
if you want to ask about everything, imp. safer in some sense
|
||||
|
||||
types like putting a label. one label attests to a number of similar properties, so if you got type use all those properties even with the assumptions that how they behave with each other
|
||||
|
||||
duck typing is like letting anyone with a known speciific skill work in your office. They dont need degrees or anything if they know that one thing very well which you'll need. And yes that is very flexible and democratic. But never assume that they will even have some related but different skill. Degrees attest to the fact that they know about a set of skills. All that cool.
|
||||
Now if you have any more requirement, ask if they can do it and if they cant, fire them. Or force them to do it, if they fail fire them. But rest assured, you are more flexible so anyone with both those skills are welcome now, even the one you just fired if they end up learning that skill.
|
||||
|
||||
issue of flexibility vs reliability, you sometimes want both :)
|
||||
|
||||
Ceveats
|
||||
This wont work
|
||||
1. WrapObj({str:5}) == {"w":5} as dicts are not searched for key and yet they are ambiguous if searched. So, keys are taken for what they are, even if it is a class. So, here str is considered as the key
|
||||
|
||||
for mixed type:
|
||||
allow this kind of things -> int or float("inf")
|
||||
|
||||
IDEAS TO DO
|
||||
|
||||
add more debug info
|
||||
debug info or raise custom exception
|
||||
better __repr__ print
|
||||
|
||||
allow matches to store in var (done)
|
||||
|
||||
add __mult__ alternative to .times (done)
|
||||
|
||||
what about dict ? (dicts ok, )
|
||||
|
||||
maybe dict to obj? (done)
|
||||
|
||||
save_as in multimatch with list behaviour(done)
|
||||
|
||||
Any and OR save_as (done)
|
||||
|
||||
test if data == Multi branch, raise error doesnt break anything
|
||||
more docs
|
||||
|
||||
test integration save_as and times
|
||||
|
||||
do smart .times for single value
|
||||
|
||||
allow Any with a module.Class(Any), which allows only object - Any
|
||||
|
||||
more doctest for other methods and helpers
|
||||
|
||||
test helpers
|
||||
|
||||
name change to humblematch
|
||||
@@ -1,4 +1,4 @@
|
||||
from wrap_obj import WrapObj, Any, OR, w
|
||||
from humblematch import WrapObj, Any, OR, w
|
||||
import collections
|
||||
import pytest
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
site_name: HumbleMatch
|
||||
|
||||
theme: cerulean
|
||||
|
||||
docs_dir: humblematch/docs
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
Run doctest -
|
||||
python -m doctest .\wrap_obj.py
|
||||
|
||||
Run py.test-
|
||||
py.test
|
||||
@@ -0,0 +1,56 @@
|
||||
# HumbleMatch
|
||||
--------------
|
||||
Will **Sanitize your type-checks and duck-checks** for you
|
||||
|
||||
")
|
||||
|
||||
**Install using**
|
||||
`pip install humblematch`
|
||||
|
||||
Github Repo at [github.com/bendtherules/humblematch](http://github.com/bendtherules/humblematch)
|
||||
|
||||
Available at [HumbleMatch@pypi](https://pypi.python.org/pypi/humblematch)
|
||||
|
||||
More Docs at [humblematch.readthedocs.org](http://humblematch.readthedocs.org/en/latest/)
|
||||
|
||||
## What the heck is it?
|
||||
|
||||
HumbleMatch is made to *add a zing* to your mundane type-checking and duck-checking code.
|
||||
|
||||
* Check your function arguments reliably, add *more flexible signatures* and most of all, have a lovely API.
|
||||
* Or just *sprinkle some assert* checks to ensure the data is what you expect it to be at any point.
|
||||
* Provide helpful debugging errors, *dont just fail with cryptic errors*
|
||||
|
||||
|
||||
## Get Running
|
||||
--------------
|
||||
|
||||
### Run doctest -
|
||||
|
||||
python -m doctest .\humblematch\wrap_obj.py
|
||||
|
||||
### Run tests-
|
||||
cd humblematch
|
||||
py.test
|
||||
|
||||
### To build code -
|
||||
|
||||
python setup.py sdist bdist_wheel bdist_msi bdist_egg
|
||||
|
||||
python setup.py build --plat-name=win-amd64 bdist_wininst
|
||||
|
||||
python setup.py build --plat-name=win32 bdist_wininst
|
||||
|
||||
### To upload to PYPI-
|
||||
|
||||
twine upload dist/* -r pypi
|
||||
|
||||
### To build or serve docs
|
||||
|
||||
mkdocs build
|
||||
|
||||
or
|
||||
|
||||
mkdocs serve
|
||||
|
||||
-----
|
||||
@@ -1,2 +1,5 @@
|
||||
[metadata]
|
||||
description-file = README
|
||||
description-file = readme.md
|
||||
|
||||
[bdist_wheel]
|
||||
universal=1
|
||||
|
||||
@@ -1,14 +1,43 @@
|
||||
from distutils.core import setup
|
||||
import sys
|
||||
from setuptools import setup
|
||||
from setuptools.command.test import test as TestCommand
|
||||
|
||||
|
||||
class PyTest(TestCommand):
|
||||
user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]
|
||||
|
||||
def initialize_options(self):
|
||||
TestCommand.initialize_options(self)
|
||||
self.pytest_args = []
|
||||
|
||||
def finalize_options(self):
|
||||
TestCommand.finalize_options(self)
|
||||
self.test_args = []
|
||||
self.test_suite = True
|
||||
|
||||
def run_tests(self):
|
||||
# import here, cause outside the eggs aren't loaded
|
||||
import pytest
|
||||
errno = pytest.main(self.pytest_args)
|
||||
sys.exit(errno)
|
||||
|
||||
|
||||
setup(
|
||||
name='humblematch',
|
||||
packages=['humblematch'], # this must be the same as the name above
|
||||
version='0.1.dev1',
|
||||
version='0.1.1',
|
||||
description='Will Sanitize your type-checks and duck-checks for you',
|
||||
author='bendtherules',
|
||||
author_email='bendtherules@codesp.in',
|
||||
url='https://github.com/bendtherules/humblematch', # use the URL to the github repo
|
||||
download_url='https://github.com/bendtherules/humblematch/tarball/0.1.dev1', # I'll explain this in a second
|
||||
download_url='https://github.com/bendtherules/humblematch/tarball/0.1.dev3', # I'll explain this in a second
|
||||
license='WTFPL',
|
||||
keywords=['type-checking', 'testing', 'assert'], # arbitrary keywords
|
||||
classifiers=['Development Status :: 3 - Alpha', ],
|
||||
package_data={
|
||||
# Include all files in all packages
|
||||
# 'humblematch': ['docs/*.md', 'test/*'],
|
||||
},
|
||||
tests_require=['pytest'],
|
||||
cmdclass={'test': PyTest},
|
||||
)
|
||||
|
||||