Skip to content

a collection of strategies for object persistence - (Fork of Nates query-tools repo which is used by the chef install infrastructure)

License

Notifications You must be signed in to change notification settings

vz-risk/query-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

$ wget https://raw.githubusercontent.com/natb1/query_tools/master/requirements.txt
$ pip install -r requirements.txt

...a collection of strategies for object persistence. For example, these objects:

>>> class Penguin(object):
...     def __init__(self, name, mood, id=None):
...         self.name = name
...         self.mood = mood
...         self.id = id
...     def __repr__(self):
...         return '< %s the %s penguin >' % (self.name, self.mood)
...
>>> class Goose(object):
...     def __init__(self, name, favorite_penguin, id=None):
...         self.name = name
...         self.favorite_penguin = favorite_penguin
...         self.id = id
...     def __repr__(self):
...         template = '< %s, the goose that likes %s >'
...         return template % (self.name, repr(self.favorite_penguin))
...
>>> grace = Goose('grace', Penguin('penny', 'fat'))
>>> gale = Goose('gale', Penguin('prince', 'cool'))
>>> ginger = Goose('ginger', Penguin('puck', 'boring'))

The JSON session manager

query_tools.JSONEncoder(ModelType)

... constructs a json session manager where ModelType is the type to be encoded. JSON sessions are adaptors to the python json libs.

>>> import query_tools
>>> goose_json_encoder = query_tools.JSONEncoder(Goose)
>>> with goose_json_encoder.make_session() as session:
...     json_data = session.add_all((grace, gale, ginger))
...     print(json_data) # doctest: +NORMALIZE_WHITESPACE
[ 
  {
    "favorite_penguin": {
      "id": null,
      "mood": "fat",
      "name": "penny"
    },
    "id": null,
    "name": "grace"
  },
  {
    "favorite_penguin": {
      "id": null,
      "mood": "cool",
      "name": "prince"
    },
    "id": null,
    "name": "gale"
  },
  {
    "favorite_penguin": {
      "id": null,
      "mood": "boring",
      "name": "puck"
    },
    "id": null,
    "name": "ginger"
  }
]

The ElasticSearch session manager

query_tools.ElasticSearch(index, ModelType_to_type_name)

... constructs an elasticsearch session manager where index is the name of the index to use for this session manager, and ModelType_to_type_name is a map from ModelType to elasticsearch _type.

>>> goose_elasticsearch = query_tools.ElasticSearch('zoo', {Goose:'goose'})
>>> with goose_elasticsearch.make_session() as session:
...     session.add_all((grace, gale, ginger))

The SQLAlchemy session manager

query_tools.SQLAlchemy(sqla_metadata, materialized_mappers=None)

The CSV session manager

query_tools.CSVEncoder(model_aggregate_map)

... constructs a csv session manager where model_aggregate_map is a mapping_tools.Mapper instance with a map method that returns tabular instances of the model objects.

>>> class GooseAggregate(object):
...     def __init__(self, name, favorite_penguin_name, favorite_penguin_mood,
...                  favorite_penguin_id=None, id=None):
...         self.name = name
...         self.favorite_penguin_name = favorite_penguin_name
...         self.favorite_penguin_mood = favorite_penguin_mood
...         self.favorite_penguin_id = favorite_penguin_id
...         self.id = id
...     def __repr__(self):
...         template = '< %s the goose has a %s penguin mood >' 
...         return template % (self.name, self.favorite_penguin_mood)
...
>>> import mapping_tools
>>> penguin_projection = mapping_tools.make_projection(Penguin)
>>> goose_aggregate_map = mapping_tools.Mapper(GooseAggregate, {
...     ('name', 'id'):mapping_tools.identity,
...     'favorite_penguin':penguin_projection})
>>> goose_csv_encoder = query_tools.CSVEncoder(goose_aggregate_map)
>>> with goose_csv_encoder.make_session() as session:
...     session.add_all((grace, gale, ginger)) # doctest: +NORMALIZE_WHITESPACE
favorite_penguin_id,favorite_penguin_name,favorite_penguin_mood,name,id
,penny,fat,grace,
,prince,cool,gale,
,puck,boring,ginger,

Repository and Encoder Sessions

Repository sessions implement strategies for querying:

class MyRepositorySession:
    def query(ModelType, criteria):
        ...
        return model_type_objects

... where ModelType is the type of object to query, criteria is a query_tools.Criteria object or a query_tools.Conjuction object, and model_type_objects is an iterator of ModelType objects.

Encoder sessions handle objects:

class MyEncoderSession:
    def add_all(self, objects):
        ...

Encoder and repository sessions are constructed by session managers:

class MySessionManager:
    def make_session(self):
        ...
        return context_manager

... where context_manager is a python context manager that returns a repository or encoder session on entry.

Query criteria objects

Query objects to be passed to the query method of repositories:

query_tools.Criteria(path, value, operator)

... where path is a iterator that interprets the path to the criterion, value is the value to be compared at that path, and operator interprets the comparison operator.

query_tools.Conjuction(conjuction, criteria)

... where conjuction interprets the conjuction to be used and criteria is an iterator of criteria.

conjuction.__iter__(self)

... returns iter(self.criteria)

About

a collection of strategies for object persistence - (Fork of Nates query-tools repo which is used by the chef install infrastructure)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages