Class hierarchy problem

2013-08-06 Thread BrJohan

I'm in need of help to solve this Python (ver. 3.3) problem:

I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, 
SubC,...), each of which is inheriting from a chain of superclasses with 
a common baseclass(Sup) on top. (So far, no problem)


Now, I want to create instances of the correct subclasstype as decided 
by the common baseclass, like this:


i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)

where i can be of any class except Sup itself (as decided by Sup)

Now, the problem:

How to design the __new__() and __init__() methods for the various 
classes in order to achieve what I want?


(Some ten years I had the same problem (in a different context) and was 
helped by asking in this group. However, the solution has disappeared. 
Maybe the 2.x solution is not the same as in 3.x?)

--
http://mail.python.org/mailman/listinfo/python-list


Re: Class hierarchy problem

2013-08-06 Thread BrJohan

On 06/08/2013 16:02, Chris Angelico wrote:


My classhierarchy is like a multilevel tree where each non-leaf node (class)
is given knowledge about its nearest subclasses and their 'capacities'.

So, my idea is to let the 'upper' class recursively choose which of its
nearest subclasses is the 'correct' one, until approaching a 'leaf' class
from which the instance should be created. And, given my knowledge that a
solution along the lines of this idea has been designed and was working, I'm
still hopeful ... (or I'll have to investigate all those old backup-DVDs)


[ responding on-list - I hope it was mere oversight that had this come
privately to me alone ]

This is code smell; this recursive search for the "right" class seems
likely to be wrong. Can you have the classes perhaps register
themselves in some way? On what basis is a superclass to determine
that one of its subclasses should handle this request?

ChrisA




Consider a botanical classification system (somewhat analogous to my 
'problem' as it effectively is related to classification of entities):


A Domain should know about its Kingdoms,
a Kingdom should know about its Phylums,
...
a Genus should know about its Species.

Of course it is possible to implement such a decision tree as a 
'factory'. However, I would rather prefer to encapsulate those decisions 
at the class level where they 'belong'.


BrJohan
--
http://mail.python.org/mailman/listinfo/python-list


Python's re module and genealogy problem

2014-06-11 Thread BrJohan

For some genealogical purposes I consider using Python's re module.

Rather many names can be spelled in a number of similar ways, and in 
order to match names even if they are spelled differently, I will build 
regular expressions, each of which is supposed to match  a number of 
similar names.


I guess that there will be a few hundred such regular expressions 
covering most popular names.


Now, my problem: Is there a way to decide whether any two - or more - of 
those regular expressions will match the same string?


Or, stated a little differently:

Can it, for a pair of regular expressions be decided whether at least 
one string matching both of those regular expressions, can be constructed?


If it is possible to make such a decision, then how? Anyone aware of an 
algorithm for this?

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python's re module and genealogy problem

2014-06-13 Thread BrJohan

On 11/06/2014 14:23, BrJohan wrote:

For some genealogical purposes I consider using Python's re module.

Rather many names can be spelled in a number of similar ways, and in
order to match names even if they are spelled differently, I will build
regular expressions, each of which is supposed to match  a number of
similar names.

I guess that there will be a few hundred such regular expressions
covering most popular names.

Now, my problem: Is there a way to decide whether any two - or more - of
those regular expressions will match the same string?

Or, stated a little differently:

Can it, for a pair of regular expressions be decided whether at least
one string matching both of those regular expressions, can be constructed?

If it is possible to make such a decision, then how? Anyone aware of an
algorithm for this?


Thank you all for valuable input and interesting thoughts.

After having reconsidered my problem, it might be better to approach it 
a little differently.


Either to state the regexps simply like:
"(Kristina)|(Christina)|(Cristine)|(Kristine)"
instead of "((K|(Ch))ristina)|([CK]ristine)"

Or to put the namevariants in some sequence of sets having elements like:
("Kristina", "Christina", "Cristine", "Kristine")
Matching is then just applying the 'in' operator.

I see two distinct advantages.
1. Readability and maintainability
2. Any namevariant occurring in just one regexp or set means no risk of 
erroneous matching.


Comments?


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python for EXIF-info-additions ?

2006-08-21 Thread BrJohan

Bruno Dilly wrote:
> I think you can find what do you need into this repository, it's a
> creative commons tool:
> https://svn.berlios.de/svnroot/repos/cctools/publisher/branches/flickr-storage-branch/
>
> take a look in the follow directory:
> ccpublisher/jpeg/
>
> I'm not sure if it's what you want, let me know.
>
> See you,
>
> Dilly
>

After glancing through the code I think it offers all - or most of -
what I need.
Thank you!

/Bror J.

-- 
http://mail.python.org/mailman/listinfo/python-list


Unpythonic? Impossible??

2006-03-19 Thread BrJohan
Assume having this class hierarchy: (in principle and without details)
class A(object):
class B1(A):
class B2(A):
class C1(A1):
class C2(A1):
class C3(B1):
class C4(B2):

each of those classes have an initializer __init__(self, data):  and an 
overloaded __new__(cls, data):

is it then possible to have this call:
obj = A(data)
return an instance of that particular class (e.g. class C3)  in the 
hierarchy that - as decided by the __new__ functions - is the 'correct' one?

A.__new__ could select between A, B1 and B2, while B1.__new__ could choose 
from B1, C3 and C4.

I know how to use a class factory - and could work around using such a 
mechanism. However I am interested to know if I could let the classes do the 
work by themselves.

/BJ 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unpythonic? Impossible??

2006-03-19 Thread BrJohan

"Scott David Daniels" <[EMAIL PROTECTED]> skrev i meddelandet 
news:[EMAIL PROTECTED]
> BrJohan wrote:
...
>> is it then possible to have this call:
>> obj = A(data)
>> return an instance of that particular class (e.g. class C3)  in the 
>> hierarchy that - as decided by the __new__ functions - is the 'correct' 
>> one?
>>
>> A.__new__ could select between A, B1 and B2, while B1.__new__ could 
>> choose from B1, C3 and C4.
>>
>> I know how to use a class factory - and could work around using such a 
>> mechanism. However I am interested to know if I could let the classes do 
>> the work by themselves.
>
> Yes, it can be done.  Yes, it is unclear (and hence UnPythonic).
> The class factory _is_ the straightforward way to do this.  The
> following is the workaround (if you have to maintain A(...)):
>
>
> class A(object):
> def __new__(class_, *args, **kwargs):
> if class_ is A:
> if want_a_B1(*args, **kwargs):
> return B1(*args, **kwargs)
> elif want_a_B2(*args, **kwargs):
> return B2(*args, **kwargs)
> return object.__new__(class_) # Use *a,... except for object
>
> class B1(A):
> def __new__(class_, *args, **kwargs):
> if class_ is B1:
> if want_a_B1(*args, **kwargs):
> return B1(*args, **kwargs)
> elif want_a_B2(*args, **kwargs):
> return B2(*args, **kwargs)
> return super(B1, class_).__new__(class_, *args, **kwargs)
>
>
> --Scott David Daniels
> [EMAIL PROTECTED]

Agreed that the class factory method most often (maybe always) is the best 
one. For certain reasons, and in this particular case, I prefer the 
UnPythonic way. Sometimes it's good to have "more than one way to do it".

It was the "return object.__new__(class_) " that I did not came to think of 
myself, that did it. Thank you for yor helpfulness.

BrJohan 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python and databases

2017-03-14 Thread BrJohan

On 2017-03-14 20:59, Xristos Xristoou wrote:

I have a database in  microsoft ACCESS with about 150 records.. if I want to 
get some data from this database using  a query in python and i want to store 
in some variables in python that will do this ? to avoid the 150 if ...: Using 
the standard library the python? know easily put it an SQL query but i canot 
use additional library python.

Some years ago I did some experiments using pyodbc and MS Access. Some 
success.

--
https://mail.python.org/mailman/listinfo/python-list