Factory pattern again

M

Mike Howarth

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
 
B

Bruno Desthuilliers

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
 
M

Mike Howarth

Thanks that makes absolute sense.

I was sure it was something simple, thanks for your time.

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
 
M

Mike Howarth

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






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
 
S

Steve Holden

Mike said:
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
There's definitely *something* wrong - this shouldn't take 20 seconds,
let alone 20 minutes.

How frequently do you see the

print type + ":" + product_code

statement producing output? In other words, is the slowness distributed
across the task, or is there a delay at the beginning or end? Are the
Set and Item classes exactly as shown in your last post?

regards
Steve

PS: Please put your answer at the bottom, not the top!
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
C

cunheise

first you need find the bottleneck of your script db or function
if the bottleneck is db
1. which db do you use do you optimize the db from read
2. the sql you write do not use any index "maybe select code, type
from products where type = 'I' or type = 'S' will help" and you need
create index on type field maybe you need set limit 2000 to sql depend
on your requirement

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


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:
cannot work, since price is an instance attribute, not a class attribute.
What you want is:
item = factory('Item')
print item.price
 
S

Steve Holden

first you need find the bottleneck of your script db or function
if the bottleneck is db
1. which db do you use do you optimize the db from read
2. the sql you write do not use any index "maybe select code, type
from products where type = 'I' or type = 'S' will help" and you need
create index on type field maybe you need set limit 2000 to sql depend
on your requirement
Whether a relational database is indexed or not the SQL to retrieve
information from it will be exactly the same - the whole point if
indexing is that it represents a change to the physical schema that
improves retrieval efficiency without altering the logical schema.

Mike stated quite clearly "the db is normalized and indexes exist".

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top