Skip to content

Commit 35a7bc0

Browse files
authored
Merge pull request #13 from devinaconley/0.1.1
v0.1.1
2 parents 6c97821 + 6667d69 commit 35a7bc0

24 files changed

+547
-498
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018-2021 Devin A. Conley
3+
Copyright (c) 2018-2023 Devin A. Conley
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ Simple **shapes** example:
1919
import objectfactory
2020

2121
@objectfactory.register
22-
class Square( objectfactory.Serializable ):
22+
class Square(objectfactory.Serializable):
2323
side = objectfactory.Field()
2424

25-
def get_area( self ):
25+
def get_area(self):
2626
return self.side * self.side
2727

2828
@objectfactory.register
29-
class Triangle( objectfactory.Serializable ):
29+
class Triangle(objectfactory.Serializable):
3030
base = objectfactory.Field()
3131
height = objectfactory.Field()
3232

33-
def get_area( self ):
33+
def get_area(self):
3434
return 0.5 * self.base * self.height
3535

3636
serialized_data = [
@@ -40,8 +40,8 @@ serialized_data = [
4040
]
4141

4242
for data in serialized_data:
43-
shape = objectfactory.create( data )
44-
print( 'class type: {}, shape area: {}'.format( type( shape ), shape.get_area() ) )
43+
shape = objectfactory.create(data)
44+
print('class type: {}, shape area: {}'.format(type(shape), shape.get_area()))
4545
```
4646

4747
Output:
@@ -52,7 +52,7 @@ class type: <class '__main__.Square'>, shape area: 2.25
5252
```
5353

5454
### More examples
55-
See more advanced examples [here](examples)
55+
See more advanced examples [here](https://github.com/devinaconley/py-object-factory/tree/develop/examples)
5656

5757
## Install
5858
Use [pip](https://pip.pypa.io/en/stable/installing/) for installation

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
# project info
99
project = 'objectfactory'
10-
copyright = '2018-2021, Devin A. Conley'
10+
copyright = '2018-2023, Devin A. Conley'
1111
author = 'Devin A. Conley'
12-
release = '0.1.0'
12+
release = '0.1.1'
1313

1414
# config
1515
extensions = [

docs/source/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ objectfactory
77

88
.. toctree::
99
:maxdepth: 2
10-
:caption: Contents:
1110

1211
quickstart
1312
objectfactory

examples/custom_schema.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def main():
3636
# load and validate each contact
3737
for c in contacts:
3838
try:
39-
contact = objectfactory.create( c, object_type=Contact )
39+
contact = objectfactory.create(c, object_type=Contact)
4040
print(
4141
'Loaded contact for: {} {}, number: {}, email: {}'.format(
4242
contact.first_name,
@@ -46,29 +46,29 @@ def main():
4646
)
4747
)
4848
except marshmallow.ValidationError as e:
49-
print( 'Validation error: {}'.format( e ) )
49+
print('Validation error: {}'.format(e))
5050

5151

52-
class PhoneNumber( marshmallow.fields.Field ):
52+
class PhoneNumber(marshmallow.fields.Field):
5353
"""Custom marshmallow field to validate phone number"""
5454

55-
def _deserialize( self, value, *args, **kwargs ):
55+
def _deserialize(self, value, *args, **kwargs):
5656
try:
57-
x = value.split( '-' )
58-
assert len( x ) == 3
59-
assert len( x[0] ) == 3
60-
assert len( x[1] ) == 3
61-
assert len( x[2] ) == 4
62-
return str( value )
57+
x = value.split('-')
58+
assert len(x) == 3
59+
assert len(x[0]) == 3
60+
assert len(x[1]) == 3
61+
assert len(x[2]) == 4
62+
return str(value)
6363

6464
except AssertionError as e:
65-
raise marshmallow.ValidationError( 'Invalid phone number' )
65+
raise marshmallow.ValidationError('Invalid phone number')
6666

67-
def _serialize( self, value, *args, **kwargs ):
68-
return str( value )
67+
def _serialize(self, value, *args, **kwargs):
68+
return str(value)
6969

7070

71-
class ContactSchema( marshmallow.Schema ):
71+
class ContactSchema(marshmallow.Schema):
7272
"""Custom marshmallow schema for contact info"""
7373

7474
first_name = marshmallow.fields.Str()
@@ -78,7 +78,7 @@ class ContactSchema( marshmallow.Schema ):
7878

7979

8080
@objectfactory.register
81-
class Contact( objectfactory.Serializable, schema=ContactSchema ):
81+
class Contact(objectfactory.Serializable, schema=ContactSchema):
8282
"""
8383
product order from a dollar store vendor
8484
"""

examples/product_orders.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,76 +26,76 @@ def main():
2626
]
2727

2828
# deserialize raw product order
29-
products = [objectfactory.create( order, object_type=Product ) for order in raw_orders]
29+
products = [objectfactory.create(order, object_type=Product) for order in raw_orders]
3030

3131
# calculate overall price
32-
price = sum( [prod.get_price() * prod.quantity for prod in products] )
33-
print( 'Overall order price: ${}'.format( price ) )
32+
price = sum([prod.get_price() * prod.quantity for prod in products])
33+
print('Overall order price: ${}'.format(price))
3434

3535
# estimate delivery
36-
days = max( [prod.get_delivery_time() for prod in products] )
37-
print( 'Estimated delivery time is: {} days'.format( days ) )
36+
days = max([prod.get_delivery_time() for prod in products])
37+
print('Estimated delivery time is: {} days'.format(days))
3838

3939
# validate stocking
40-
in_stock = all( [prod.quantity < prod.get_quantity_in_stock() for prod in products] )
41-
print( 'Products are {}stocked'.format( '' if in_stock else 'not ' ) )
40+
in_stock = all([prod.quantity < prod.get_quantity_in_stock() for prod in products])
41+
print('Products are {}stocked'.format('' if in_stock else 'not '))
4242

4343

44-
class Product( objectfactory.Serializable ):
44+
class Product(objectfactory.Serializable):
4545
"""
4646
base abstract class for our products
4747
"""
4848
product_id = objectfactory.String() # all products will have an id
49-
quantity = objectfactory.Integer( default=1 ) # all products will have a quantity
49+
quantity = objectfactory.Integer(default=1) # all products will have a quantity
5050

51-
def get_price( self ) -> float:
51+
def get_price(self) -> float:
5252
"""
5353
abstract method to calculate price and return
5454
5555
:return: float
5656
"""
57-
raise NotImplementedError( 'get_price method is required' )
57+
raise NotImplementedError('get_price method is required')
5858

59-
def get_delivery_time( self ) -> int:
59+
def get_delivery_time(self) -> int:
6060
"""
6161
abstract method to get required delivery time
6262
6363
:return:
6464
"""
65-
raise NotImplementedError( 'get_delivery_time method is required' )
65+
raise NotImplementedError('get_delivery_time method is required')
6666

67-
def get_quantity_in_stock( self ) -> int:
67+
def get_quantity_in_stock(self) -> int:
6868
"""
6969
abstract method to get quantity in stock
7070
7171
:return:
7272
"""
73-
raise NotImplementedError( 'get_quantity_in_stock method is required' )
73+
raise NotImplementedError('get_quantity_in_stock method is required')
7474

7575

7676
@objectfactory.register
77-
class DollarStoreProduct( Product ):
77+
class DollarStoreProduct(Product):
7878
"""
7979
product order from a dollar store vendor
8080
"""
8181

82-
def get_price( self ) -> float:
82+
def get_price(self) -> float:
8383
"""
8484
everything is a dollar!!!!
8585
8686
:return:
8787
"""
8888
return 1.00
8989

90-
def get_delivery_time( self ) -> int:
90+
def get_delivery_time(self) -> int:
9191
"""
9292
everything takes about a week to ship
9393
9494
:return:
9595
"""
9696
return 7
9797

98-
def get_quantity_in_stock( self ) -> int:
98+
def get_quantity_in_stock(self) -> int:
9999
"""
100100
mock connection to this vendor's supply data
101101
@@ -105,16 +105,16 @@ def get_quantity_in_stock( self ) -> int:
105105
'greeting_card': 300,
106106
'candle': 15,
107107
'glass_vase': 10
108-
}.get( self.product_id, 0 )
108+
}.get(self.product_id, 0)
109109

110110

111111
@objectfactory.register
112-
class EcommerceGiantProduct( Product ):
112+
class EcommerceGiantProduct(Product):
113113
"""
114114
product order from an e-commerce giant
115115
"""
116116

117-
def get_price( self ) -> float:
117+
def get_price(self) -> float:
118118
"""
119119
mock connection to this vendor's pricing data
120120
@@ -125,17 +125,17 @@ def get_price( self ) -> float:
125125
'tv': 450,
126126
'digital_clock': 10,
127127
'virtual_assistant': 50
128-
}.get( self.product_id, None )
128+
}.get(self.product_id, None)
129129

130-
def get_delivery_time( self ) -> int:
130+
def get_delivery_time(self) -> int:
131131
"""
132132
guaranteed 2-day delivery
133133
134134
:return:
135135
"""
136136
return 2
137137

138-
def get_quantity_in_stock( self ) -> int:
138+
def get_quantity_in_stock(self) -> int:
139139
"""
140140
infinite supplies
141141

examples/shapes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ def main():
1616

1717
# load each shape, printing object type and area
1818
for data in serialized_data:
19-
shape = objectfactory.create( data )
20-
print( 'class type: {}, shape area: {}'.format( type( shape ), shape.get_area() ) )
19+
shape = objectfactory.create(data)
20+
print('class type: {}, shape area: {}'.format(type(shape), shape.get_area()))
2121

2222

2323
@objectfactory.register
24-
class Square( objectfactory.Serializable ):
24+
class Square(objectfactory.Serializable):
2525
"""
2626
serializable square class
2727
"""
2828
side = objectfactory.Float()
2929

30-
def get_area( self ) -> float:
30+
def get_area(self) -> float:
3131
"""
3232
calculate area of square
3333
@@ -37,14 +37,14 @@ def get_area( self ) -> float:
3737

3838

3939
@objectfactory.register
40-
class Triangle( objectfactory.Serializable ):
40+
class Triangle(objectfactory.Serializable):
4141
"""
4242
serializable triangle class
4343
"""
4444
base = objectfactory.Float()
4545
height = objectfactory.Float()
4646

47-
def get_area( self ) -> float:
47+
def get_area(self) -> float:
4848
"""
4949
calculate area of triangle
5050

objectfactory/base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from abc import ABC, abstractmethod
88

99

10-
class FieldABC( ABC ):
10+
class FieldABC(ABC):
1111
"""
1212
abstract base class for serializable field
1313
"""
1414

15-
def __init__( self, default=None, key=None, required=False, allow_none=True ):
15+
def __init__(self, default=None, key=None, required=False, allow_none=True):
1616
"""
1717
:param default: default value for field if unset
1818
:param key: dictionary key to use for field serialization
@@ -26,15 +26,15 @@ def __init__( self, default=None, key=None, required=False, allow_none=True ):
2626
self._allow_none = allow_none
2727

2828
@abstractmethod
29-
def __get__( self, instance, owner ):
29+
def __get__(self, instance, owner):
3030
pass
3131

3232
@abstractmethod
33-
def __set__( self, instance, value ):
33+
def __set__(self, instance, value):
3434
pass
3535

3636
@abstractmethod
37-
def marshmallow( self ):
37+
def marshmallow(self):
3838
"""
3939
create generic marshmallow field to do actual serialization
4040
@@ -43,13 +43,13 @@ def marshmallow( self ):
4343
pass
4444

4545

46-
class SerializableABC( ABC ):
46+
class SerializableABC(ABC):
4747
"""
4848
abstract base class for serializable object
4949
"""
5050

5151
@abstractmethod
52-
def serialize( self, include_type: bool = True, use_full_type: bool = True ) -> dict:
52+
def serialize(self, include_type: bool = True, use_full_type: bool = True) -> dict:
5353
"""
5454
serialize model to dictionary
5555
@@ -60,7 +60,7 @@ def serialize( self, include_type: bool = True, use_full_type: bool = True ) ->
6060
pass
6161

6262
@abstractmethod
63-
def deserialize( self, body: dict ):
63+
def deserialize(self, body: dict):
6464
"""
6565
deserialize model from dictionary
6666

0 commit comments

Comments
 (0)