Factory pattern again
Hi
I was wondering whether anyone could help me, I'm pretty new to python
coming from a PHP background and I'm having a few products in getting my
head round how to write the factory pattern within python.
I'm currently looking to try to return values from a db and load up the
relevant objects, values returned are product type (I,S) and product code
(123).
At the moment I've adapted some code I've found illustrating the factory
method but ideally I would like to use the type to load up the relevant
object.
Another issue I've found is that I don't seem to be able to access to the
price attribute of each of the object. I'm sure these are very
straightforward issues however I seem to have tied myself in knots over this
and could do with a fresh set of 'pythonic' eyes to help me out.
registry = {}
class MetaBase(type):
def __init__(cls, name, bases, dict):
registry[name] = cls
class Product(object):
__metaclass__ = MetaBase
class Item(Product):
def __init__(self, *args, **kw):
self.price = 1
class Set(Product):
def __init__(self, *args, **kw):
self.price = 2
def factory(kind, *args, **kw):
return registry[kind](*args, **kw)
item = registry['Item']
print item.price
--
View this message in context:
http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11825158
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Factory pattern again
Thanks that makes absolute sense.
I was sure it was something simple, thanks for your time.
Bruno Desthuilliers-5 wrote:
>
> Mike Howarth a écrit :
>> Hi
>>
>> I was wondering whether anyone could help me, I'm pretty new to python
>> coming from a PHP background and I'm having a few products in getting my
>> head round how to write the factory pattern within python.
>>
>> I'm currently looking to try to return values from a db and load up the
>> relevant objects, values returned are product type (I,S) and product code
>> (123).
>>
>> At the moment I've adapted some code I've found illustrating the factory
>> method but ideally I would like to use the type to load up the relevant
>> object.
>>
>> Another issue I've found is that I don't seem to be able to access to the
>> price attribute of each of the object. I'm sure these are very
>> straightforward issues however I seem to have tied myself in knots over
>> this
>> and could do with a fresh set of 'pythonic' eyes to help me out.
>>
>> registry = {}
>>
>> class MetaBase(type):
>> def __init__(cls, name, bases, dict):
>> registry[name] = cls
>>
>> class Product(object):
>> __metaclass__ = MetaBase
>>
>> class Item(Product):
>> def __init__(self, *args, **kw):
>> self.price = 1
>>
>> class Set(Product):
>> def __init__(self, *args, **kw):
>> self.price = 2
>>
>> def factory(kind, *args, **kw):
>> return registry[kind](*args, **kw)
>>
>>
>> item = registry['Item']
>
> This returns the Item *class*, not an instance of... So the following:
>
>> print item.price
>
> cannot work, since price is an instance attribute, not a class attribute.
>
> What you want is:
>
> item = factory('Item')
> print item.price
>
> HTH
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
--
View this message in context:
http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11828597
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Factory pattern again
Having overcome my first hurdle with the factory pattern, I've now hit
another stumbling block
At the moment I'm trying to return around 2000 records from a db and load up
the relevant product object, what I've found is this is running extremely
slowly (> 20mins), the db is normalized and indexes exist.
Can anyone advise on how best I could speed this up?
def getWebRangeProducts(self):
if self.c.connected:
cu = self.c.cursor #create the cursor
sql = "SELECT type, code FROM products"
cu.execute(sql)
if cu.rowcount > 0:
rows = cu.fetchall()
for row in rows:
self.loadProduct(row[0].strip(),row[1].strip())
return self.products
def loadProduct(self,type,product_code):
print type + ":" + product_code
if type == 'I':
try:
product = factory('Item', product_code)
self.products.append(product)
except:
print 'problem occured:' + type + ':' + product_code
elif type == 'S':
try:
item_set = factory('Set', product_code)
item_set.getItems()
self.products.extend(item_set.getItems())
except:
print 'problem occured:' + type + ':' + product_code
else:
pass
Bruno Desthuilliers-5 wrote:
>
> Mike Howarth a écrit :
>> Hi
>>
>> I was wondering whether anyone could help me, I'm pretty new to python
>> coming from a PHP background and I'm having a few products in getting my
>> head round how to write the factory pattern within python.
>>
>> I'm currently looking to try to return values from a db and load up the
>> relevant objects, values returned are product type (I,S) and product code
>> (123).
>>
>> At the moment I've adapted some code I've found illustrating the factory
>> method but ideally I would like to use the type to load up the relevant
>> object.
>>
>> Another issue I've found is that I don't seem to be able to access to the
>> price attribute of each of the object. I'm sure these are very
>> straightforward issues however I seem to have tied myself in knots over
>> this
>> and could do with a fresh set of 'pythonic' eyes to help me out.
>>
>> registry = {}
>>
>> class MetaBase(type):
>> def __init__(cls, name, bases, dict):
>> registry[name] = cls
>>
>> class Product(object):
>> __metaclass__ = MetaBase
>>
>> class Item(Product):
>> def __init__(self, *args, **kw):
>> self.price = 1
>>
>> class Set(Product):
>> def __init__(self, *args, **kw):
>> self.price = 2
>>
>> def factory(kind, *args, **kw):
>> return registry[kind](*args, **kw)
>>
>>
>> item = registry['Item']
>
> This returns the Item *class*, not an instance of... So the following:
>
>> print item.price
>
> cannot work, since price is an instance attribute, not a class attribute.
>
> What you want is:
>
> item = factory('Item')
> print item.price
>
> HTH
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
--
View this message in context:
http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11831500
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.org/mailman/listinfo/python-list
Database objects? Persistence? Sql Server woes
I've been having a few problems with connecting to SQL Server, initially I was using dblib however found some problems with returning text fields whereby all text fields were ignored and it bawked at multiline sql statements. Having found these major stumbling blocks I've started using pymssql which seems less flaky. One problem I've stumbled across is that I seem to reach my max connections on the database fairly easily (given how my script was written) and therefore have used the singleton pattern on my database object. Great problem solved, or so I thought. Using the following code I'm finding that the Set is now also being enforced as a singleton as well. class Set(Database,Product): def __init__(self, *args, **kw): Database.__init__(self) Product.__init__(self) def dosomething(self): cu = self.c.cursor() Having sat back and thought about it, its easy to understand why this is occurring given I'm indicating that Set belongs to Database and therefore this is a singleton as well. Being relatively new to Python I'm unsure on how to then go about using a database object between each class. Can anyone advise me on how they approach this, and whether there is a common approach to this? -- View this message in context: http://www.nabble.com/Database-objects--Persistence--Sql-Server-woes-tf4171199.html#a11866779 Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
String/Decimal issues
Hi Seem to be having a bit of brainfreeze this evening. Basically I'm reducing an array of prices like so: >> subtotal = reduce(operator.add, itemprices) This gives me a string of '86.00.00' which I am trying to use with decimal objects. Python 2.4 is not particularly happy with this. Additionally I can't simply convert the string to a decimal as it would be invalid given it has multiple decimal points. Being relatively new to python, I'm not sure how I could round the string or similar. Anyone got any ideas? -- View this message in context: http://www.nabble.com/String-Decimal-issues-tf4783150.html#a13683811 Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
