473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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-patter...html#a11825158
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 27 '07 #1
3 2016
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
Jul 27 '07 #2

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-patter...html#a11828597
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 27 '07 #3
cu******@yahoo.com.cn wrote:
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 -------------

Jul 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: javac | last post by:
for better or worse, my free time is consumed by two Java series books from Sun: Java Platform Performance, 2000, by Wilson and Kesselman Effective Java, 2001, by Bloch 1.) opinions on these...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
2
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
7
by: Steven T. Hatton | last post by:
I have a couple questions about the design pattern presented in the example quoted below. I can appreciate why the destructor is protected, but why is it not virtual? I am forced to assume that I...
16
by: RSH | last post by:
Hi, I am a fairly seasoned developer and have recently started looking at software design patterns. I am starting with the Abstract Factory Pattern and was wondering if someone could help me...
1
by: DAXU | last post by:
Hi, I am bit confused about the differences between plugin pattern and factory pattern. Can someone explain the differences? Many Thanks Jerry
5
by: CSharper | last post by:
I have created a Factory pattern code. One thing I noticed after coding, each factory has some methods they are exactly same and doing the same code using the values specific to each factory, if I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.