Giter Club home page Giter Club logo

typify's Introduction

Basic Ussage

class Record(typify.Model):                                                                                                                                                                    
    id = typify.IntegerMatcher()                                                                                                                                                               
    name = typify.StringMatcher())  

record_json = "{\"id\":1,\"name\":\"testname\"}

a_record = Record.from_json(record_json)
print record.id
print record.name
print a_record.to_json()

This is library is to help facilitate creating objects from common text based representation formates(json, yaml, and python dics). The following response will be used for a common example.

{
   "id":1,
   "name":"Gold Start Ablum",
   "postal_code":44024,
   "created_at":"2015-04-24T21:51:50.691473",
   "songs":[
      {
         "id":1,
         "name":"songname"
      }
   ],
   "author":{
      "id":1,
      "name":"test author",
      "info":{
         "id":1,
         "email":"[email protected]"
      }
   },
   "_links":{
      "self":"testselflink",
      "next":"testnextlink",
      "embedded":{
         "embedded_link":"testembeddedlink"
      }
   },
   "list_thing":[
      1,
      2,
      3,
      4
  ]
}

Supported Formats

  • from_json

  • from_yaml

  • from_dict

  • to_json

  • to_yaml

  • to_dicttw

Renaming Attributes

class Record(typify.Model):
    zip = typify.IntegerMatcher(key='postal_code')

Parsing Attributes into complex type

class Record(typify.Model):
    created_at = typify.StringMatcher(parse=lambda x: datetime.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.%f'))

Merging parts of the document

class Record(typify.Model):
    merged_stuff = typify.StringMatcher(merge=lambda doc: str(doc['id']) + ':' + doc['name'] )

Converting complex type when writing to json

class Record(typify.Model):
    created_at = typify.StringMatcher(convert=lambda x: x.isoformat(), default=lambda: datetime.datetime.now())

Wrapped attributes

class Record(typify.Model):
    self = typify.StringMatcher(wrap='_links')
    embedded_link = typify.StringMatcher(wrap=['_links', 'embedded'])

Nested Objects

class Author(typify.Model):                                                                                                                                                                    
    id = typify.IntegerMatcher()                                                                                                                                                               
    name = typify.StringMatcher()
    
class Record(typify.Model): 
    author = typify.ObjectMatcher(Author)

Objects can be nested forever and ever

 class AuthorInfo(typify.Model):                                                                                                                                                                
    id = typify.IntegerMatcher()                                                                                                                                                               
    email = typify.StringMatcher()  

class Author(typify.Model):                                                                                                                                                                    
    id = typify.IntegerMatcher()                                                                                                                                                               
    name = typify.StringMatcher()
    info  = typify.ObjectMatcher(AuthorInfo)
    
class Record(typify.Model): 
    author = typify.ObjectMatcher(Author)

Collections of objects

class Song(typify.Model):                                                                                                                                                                      
    id = typify.IntegerMatcher()                                                                                                                                                               
    name = typify.StringMatcher() 

class Record(typify.Model): 
    songs = typify.CollectionMatcher(Song)

List of basic types

class Record(typify.Model): 
    list_thing = typify.ListMatcher()

typify's People

Contributors

mattyo avatar

Watchers

Ezekiel N Bukari 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.