Giter Club home page Giter Club logo

dotty's Introduction

EFILTER Query Language

EFILTER is a general-purpose destructuring and search language implemented in Python, and suitable for integration with any Python project that requires a search function for some of its data.

Quick Example

query = Query("name == 'Bob' and age > (15 + 1)")
solve(query, dict(name="Alice", age=20)) # => False
solve(query, dict(name="Bob", age=20)) # => True
infer_type(query) # => bool
infer_type(Query("15 + 1")) # => int

Integrating EFILTER with your project

Filtering custom classes

Let's have a class in our custom project:

class Customer(object):
	"""My awesome Customer business logic class."""
	
	@property
	def name(self):
		#...
	
	@property
	def age(self):
		#...
	
	@property
	def admin(self):
		#...

We'd like to filter this class's instances using EFILTER, but we need a way to 'tell' EFILTER about it.

EFILTER uses the IAssociative protocol to access members of the objects its asked to filter. Implementing a protocol lets EFILTER know how each type should be accessed:

from efilter.protocols import associative
associative.IAssociative.implement(
	for_type=Customer,  # This is how you access Customer's data.
	implementations={
		# Select is similar to dict().get()
		associative.select: lambda c, key: getattr(c, key, None),
		
		# Resolve is similar, but allowed to use magic to look
		# up more data. For example, selecting 'admin' will
		# return the ID of the admin user, but resolving
		# 'admin' might return the user object representing
		# the admin.
		associative.resolve: lambda c, key: getattr(c, key, None),
		
		# Getkeys is kind of obvious: the keys that can be
		# accessed.
		associative.getkeys: lambda _: ("name", "age", "admin")
	})

Now we can filter the Customer class:

query = Query("name == 'Bob'")
solve(query, Customer(name="Bob")) # => True

License and Copyright

Copyright 2015 Google Inc. All Rights Reserved

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributors

Adam Sindelar

dotty's People

Contributors

the80srobot avatar

Watchers

James Cloos avatar 刘畅 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.