Skip to content

MattyO/typify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic Usage

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 library's objective is to aid the creation of objects from common text based representation formats (JSON, YAML and Python Dictionaries). 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_dict

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()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages