[Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi, after using Perl for some years for simple scripting tasks, one of my programs reached a size where an OO design is appropriate. So I rewrote the program using OO techniques in Perl. Because I am not entirely satisfied with the implementation, I decided port the program to Python. The firs

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi, Jan Eden wrote on 07.08.2005: >So how can I add the values of all the paramaters to my class >instance in one step? > apologies for replying to my own post: I just discovered the **args formal parameter. Thanks, Jan -- I was gratified to be able to answer promptly, and I did.

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 07.08.2005: >>So how can I add the values of all the paramaters to my class >>instance in one step? > >There was recently a long discussion of this on comp.lang.python. >http://groups.google.com/group/comp.lang.python/browse_frm/thread/ >7346ad00a14e821a/9dc993d2954

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Jan Eden wrote on 07.08.2005: >But the assignment in the for loop obviously does not work with >instance attributes. I will have to read up some more about instance >attributes. Ok... so this works: class NewClass: def __init__(self, **parameters): self.data = {}

Re: [Tutor] Class instantiation parameters

2005-08-07 Thread Jan Eden
Hi Kent, hi Alan, Alan G wrote on 07.08.2005: >> page = Show(type=type, id=id) >> >> class Show: >> def __init__(self, type, id): >> self.id = id >> ... >>return self > >First a couple of comments. >You don't need to return self from __init__. >You can only instantiate Show *a

[Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Hi, in Perl's DBI, I used fetchrow_hashref() to receive a database row as a dictionary, with the field names being the dictionary keys. MySQLdb's fetchone() returns a tuple Unfortunately, I have a dictionary of SQL queries which return rows of different lengths (read: with a varying number of

Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Kent Johnson wrote on 08.08.2005: >Jan Eden wrote: >>The documentation for MySQLdb says that fetchoneDict() is >>deprecated and the usage of fetchone() is suggested. > >You could just use fetchoneDict(); deprecated isn't the same as >gone... I tend to avoid dep

Re: [Tutor] Fetching dictionaries using MySQLdb

2005-08-08 Thread Jan Eden
Hi Danny, Danny Yoo wrote on 08.08.2005: >On Mon, 8 Aug 2005, Jan Eden wrote: > >>Is there a recommended way to receive the results of an SQL query >>in the form I need? Or do I have to create a dictionary of >>fieldname tuples which can be zipped with the query r

[Tutor] Default class in module

2005-08-09 Thread Jan Eden
Hi, I'd like to pack some modules in a package. Each module contains a single class, which forces me to from Pythonsite.Show import Page page = Page() ... class Page(Site.DB): #class DB from module Site in package Pythonsite ... Is there a way to define a default class for a module whi

[Tutor] Untainting CGI parameters

2005-08-10 Thread Jan Eden
Hi, I would like to untaint all parameters with which my CGI script is called. Example: if parameters.has_key('type'): match = re.search('\w+', parameters['type'].value) type = match.group() else: type = 'page' In Perl, I used the ternary operator to write it like this: my $type = ($pa

Re: [Tutor] Untainting CGI parameters

2005-08-11 Thread Jan Eden
Hi Kent, hi Alan, Kent Johnson wrote on 10.08.2005: >OK, I don't know much Perl but I don't think these two snippets do >the same thing. For one thing the regexes are different, second in >the Python you need to check if the match succeeds. > >>my $type = ($parameters{type} && ($parameters{type}

Re: [Tutor] Untainting CGI parameters

2005-08-11 Thread Jan Eden
Hi Alan, Alan G wrote on 11.08.2005: >> I will combine Kent's and your suggestion, because he included a >> check for an AttributeError: >> > >OK, as a slightly more perl-ish solution to the no Attribute problem >you could also do this: > >try: >type = re.search('\w+', parameters['type'].val

Re: [Tutor] Default class in module

2005-08-12 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 11.08.2005: >I don't know of any way to do exactly what you ask. However you can >use the __init__.py module of the package to promote classes to >package level visibility. > Nice - that's a good start. Thank you, Jan -- Common sense is what tells you that the w

[Tutor] Instance attribute as a parameter's default value

2005-08-26 Thread Jan Eden
Hi, I need to use an instance attribute as the default value for a parameter to a method. This obviously won't work: page.Children() def Children(self, id=self.id, level=2): How can I get the id parameter to use the attribute page.id? I know I could simply use the method call page.Children(

Re: [Tutor] Instance attribute as a parameter's default value

2005-08-26 Thread Jan Eden
Hi, Jan Eden wrote on 26.08.2005: >Hi, > >I need to use an instance attribute as the default value for a parameter to a >method. > >This obviously won't work: > >page.Children() > >def Children(self, id=self.id, level=2): > >How can I get the id paramet

[Tutor] datetime.date objects from SQL query?

2005-09-01 Thread Jan Eden
Hi, I use the following query (simplified): self.dbh.execute("SELECT anreise, abreise FROM buchungen WHERE ferienhaus = '%(ferienhaus)s'" % { 'ferienhaus': ferienhaus} ) Both anreise and abreise are date fields. I initially wrote a little function to turn ISO formatted date strings into a date

Re: [Tutor] PYTHON????

2005-09-01 Thread Jan Eden
[EMAIL PROTECTED] wrote on 29.08.2005: >Dear Python, > How does a calculator multiply? I want to create a computer software that can >multiply. How do I program the computer to multiply? Could this be a spammer aiming at mailing lists? - Jan -- The day Microsoft makes something that doesn't s

Re: [Tutor] datetime.date objects from SQL query?

2005-09-02 Thread Jan Eden
Hi Liam, Liam Clarke-Hutchinson wrote on 02.09.2005: >Hi Jan, > >Are you using pysqlite? If so, then let me quote from the pysqlite >docs - > >"pysqlite has default adapters for the date and datetime types in >the datetime module. They will be sent as ISO dates/ISO timestamps >to SQLite." > > >Pe

Re: [Tutor] mod_python on OS X

2005-09-05 Thread Jan Eden
Hi Danny, Danny Yoo wrote on 05.09.2005: > >On Mon, 5 Sep 2005, Jan Eden wrote: >>[Sat Sep 03 23:01:52 2005] [notice] child pid 2334 exit signal Bus >>error (10) [Sat Sep 03 23:01:53 2005] [notice] child pid 2333 exit >>signal Bus error (10) > >It looks that

[Tutor] List of class instances

2005-09-20 Thread Jan Eden
Hi, I'd like to form a list of class instances. The following does not work (TextfieldLong, Textarea, TextfieldShort etc being class names): fields = [ TextfieldLong(name='title', label='Seitentitel', value=''), Textarea(name='content', label='Inhalt', value=''), Shor

Re: [Tutor] List of class instances

2005-09-20 Thread Jan Eden
Hi, Jan Eden wrote on 20.09.2005: >Hi, > >I'd like to form a list of class instances. The following does not work >(TextfieldLong, Textarea, TextfieldShort etc being class names): > >fields = [ >TextfieldLong(name='title', label='Seiten

Re: [Tutor] List of class instances

2005-09-20 Thread Jan Eden
Hi, Orri Ganel wrote on 20.09.2005: >As a side-note, unless you're okay with only being able to access >those instance variables through the fields list (ie fields[0], >fields[1], fields[2]), you may want to actually name them first. Yes, I am fine with that - I actually prefer to have a sorted

[Tutor] Using superclass __init__ method

2005-09-22 Thread Jan Eden
Hi, I just noticed that in the following environment: class Base: def __init__(self): ... class Child(Base): pass the following statement: child = Child() would automatically execute the superclass __init__ method. This is exactly what I was looking for - but in the Cookb

Re: [Tutor] Using superclass __init__ method

2005-09-22 Thread Jan Eden
Kent Johnson wrote on 22.09.2005: >This is standard behavior for any class attribute - if it's not >defined in the derived class, the base class is checked. It's not >special for __init__. > >I can't find a comprehensive reference for the way attribute lookup >works - anyone else? Well, I did kno

Re: [Tutor] Using superclass __init__ method

2005-09-22 Thread Jan Eden
Hi, paul brian wrote on 22.09.2005: > >class Base: > def __init__(self): > print "hello" > >class Child(Base): > def __init__(self): > Base.__init__(self) > This is how I did it so far. But in my program, I have 10 subclasses with identical __init__ methods, so I can simplify th

[Tutor] __getattr__ causes TypeError

2005-09-25 Thread Jan Eden
Hi, I experienced a strange side effect using a custom __getattr__ method. For a certain attribute, I'd like to return the value of another attribute if the former is not present. So I wrote: def __getattr__(self, attrname): if attrname == 'own_type': return self.child_type else: Attrib

Re: [Tutor] __getattr__ causes TypeError

2005-09-25 Thread Jan Eden
Sorry! Found the typo. - Jan Jan Eden wrote on 25.09.2005: >Hi, > >I experienced a strange side effect using a custom __getattr__ method. > >For a certain attribute, I'd like to return the value of another attribute if >the former is not present. So I wrote: > >d

[Tutor] Simulating case statement

2005-09-27 Thread Jan Eden
Hi, I used to use an extended if...elif sequence to instantiate an object and call this object's display method afterwards: if safe['type'] == pages: page = Show.Page(id=safe['id'], start=safe['start'] ...), elif safe['type'] == pages: author = Show.Author(id=safe['id']...) ... pag

Re: [Tutor] Simulating case statement

2005-09-27 Thread Jan Eden
Kent Johnson wrote on 27.09.2005: >Jan Eden wrote: >>The problem is that the __init__ methods of the respective classes >>take a different number of parameters - this is why I pass the >>whole safe_parameters dictionary. > >I think if you pass the dictionary as keywor

[Tutor] New-style classes

2005-09-29 Thread Jan Eden
Hi, after updating a program to use new-style classes, I am a bit confused. My setup looks like this (simplified): #Show.py import Data class Base(object): ... class Page(Data.Page, Base): ... class Author(Data.Author, Base): ... #Data.py class Base: ... class Page(Base):

Re: [Tutor] New-style classes

2005-09-29 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 29.09.2005: >>Data.Base has no base classes, so it is not based on a built-in >>type: How can it be a new-style class? > >Data.Base is not a new-style class, but the classes in Show are. And >properties may appear to work in old-style classes. This document >http://

[Tutor] Transforming object into subclass instance

2005-10-30 Thread Jan Eden
Hi, my program uses an initial "switch" statement to create an object: valid_types = dict( pages=Show.Page, syndicated=Show.Syndicated, authors=Show.Author, author_list=Show.AuthorList, galleries=Show.Gallery, pictures=Show.Picture, slideshow=Show.SlidePicture, tex

Re: [Tutor] Newbie Question - Python vs Perl

2005-10-31 Thread Jan Eden
Hi Scott, Scott Clausen wrote on 30.10.2005: >As a newbie to Python I'd like to know if someone can tell me some >strengths and weaknesses of this language. The reason I'm asking is >a friend told me I should learn Perl over Python as it's more >prevalent. I'm going to be using it on a Mac. > >I

[Tutor] Subclassing data attributes

2005-11-03 Thread Jan Eden
Hi, the module Data.py stores a number of data attributes. I'd like to structure the storage of these attributes, either in subclasses or in dictionaries. My first attempt was this: class A: templates['attr1'] = 'string' queries['children'] = ... class B(A): templates['attr2']

Re: [Tutor] Subclassing data attributes

2005-11-04 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 04.11.2005: > >OK I'll try again. I still don't understand why you don't like using >straight class attributes - is it to keep the size of the class >namespace smaller? > Yes, and to distinguish between different kinds of attributes in the main script. In the end, i

[Tutor] Class attributes not overriding parameters

2005-11-05 Thread Jan Eden
Hi, I use the following construction to make sure the data attribute site_id is set only once for each object: def GetSiteID(self): return self._site_id def SetSiteID(self, value): if not (hasattr(self, '_site_id') and self._site_id): self._site_id = value site_id = property(GetSit

Re: [Tutor] Class attributes not overriding parameters

2005-11-06 Thread Jan Eden
Hi Liam, Liam Clarke wrote on 06.11.2005: >Hi Jan, > >Won't this > >site_id = property(GetSiteID, SetSiteID) > >and this > >site_id = 1 > >collide? > Yup. After writing my message, I thought about it again: the property function gets never executed when I use the class attribute. So I changed

[Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Hi, I have a number of classes, each of which should inherit from a related superclass in another module: class A(Super.A): ... class B(Super.B): ... class C(Super.C): ... Is there a way to dynamically determine the value of Super at runtime? Background: Depending on certain obje

Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Hi Kent, Kent Johnson wrote on 19.11.2005: >Jan Eden wrote: >> Is there a way to dynamically determine the value of Super at >> runtime? Background: Depending on certain object attributes which are >> set during the object initialization, I need to use a different set &g

Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Kent Johnson wrote on 19.11.2005: >Danny Yoo wrote: >>Here's a small example that shows how classes can be treated just >>like any other value in Python: >> >> # >> def makeYa(superclass): >> class Ya(superclass): >> def sayHi(self): >>

Re: [Tutor] Dynamic inheritance?

2005-11-20 Thread Jan Eden
Kent Johnson wrote on 20.11.2005: >Use getattr() to access attributes by name. SiteA is an attribute of Templates >and Page is an attribute of SiteA so you can get use getattr() twice to get >what >you want: > >site = getattr(Templates, self.site_name) >self.template = getattr(site, self.templa

Re: [Tutor] Dynamic inheritance?

2005-11-22 Thread Jan Eden
Hi, Kent Johnson wrote on 20.11.2005: > >Use getattr() to access attributes by name. SiteA is an attribute of >Templates and Page is an attribute of SiteA so you can get use >getattr() twice to get what you want: > >site = getattr(Templates, self.site_name) self.template = >getattr(site, self.temp

Re: [Tutor] Dynamic inheritance?

2005-11-22 Thread Jan Eden
Hi, Jan Eden wrote on 22.11.2005: >Hi, > >Kent Johnson wrote on 20.11.2005: >> >>Use getattr() to access attributes by name. SiteA is an attribute >>of Templates and Page is an attribute of SiteA so you can get use >>getattr() twice to get what you wa

[Tutor] Malformed CSV

2005-12-02 Thread Jan Eden
Hi, I need to parse a CSV file using the csv module: "hotel","9,463","95","1.00" "hotels","7,033","73","1.04" "hotels hamburg","2,312","73","3.16" "hotel hamburg","2,708","42","1.55" "Hotels","2,854","41","1.44" "hotel berlin","2,614","31","1.19" The idea is to use each single keyword (field 1)

Re: [Tutor] Malformed CSV

2005-12-02 Thread Jan Eden
Kent Johnson wrote on 02.12.2005: >I'm not entirely sure how you want to interpret the data above. One >possibility is to just change the double "" to single " before >processing with csv. For example: > ># data is the raw data from the whole file >data = '''""hotel,hamburg"","1","0","0" >""hotel,

Re: [Tutor] Malformed CSV

2005-12-02 Thread Jan Eden
Kent Johnson wrote on 02.12.2005: >Jan Eden wrote: >>I guess I need to notify the engineer responsible for the CSV >>output and have the quoting corrected. > >If that is possible it is a much better solution. I hate to hack >around bad data - much better to correct t

[Tutor] Exception repeated in a loop

2005-12-06 Thread Jan Eden
Hi, I use the following loop to parse some HTML code: for record in data: try: parser.feed(record['content']) except HTMLParseError, (msg): print "!!!Parsing error in", record['page_id'], ": ", msg Now after HTMLParser encounters a parse error in one record, it repeats to

Re: [Tutor] Exception repeated in a loop

2005-12-06 Thread Jan Eden
Hi Pawel, Pawel Kraszewski wrote on 06.12.2005: >Dnia wtorek, 6 grudnia 2005 16:29, Jan Eden napisa?: >> Hi, >> >> I use the following loop to parse some HTML code: >> >> for record in data: >> try: >> parser.feed(record[&

Re: [Tutor] Exception repeated in a loop

2005-12-06 Thread Jan Eden
Hi, Nelson, Scott wrote on 06.12.2005: >An unhandled exception immediately stops the execution of your code. > >A handled exception (try/except) does not stop code execution (unless >you explicitly tell it to). > >This shows how a handled exception does not stop code execution: > >try: > raise

Re: [Tutor] Exception repeated in a loop

2005-12-06 Thread Jan Eden
Kent Johnson wrote on 06.12.2005: >The parser processes up to the error. It never recovers from the >error. HTMLParser has an internal buffer and buffer pointer that is >never advanced when an error is detected; each time you call feed() >it tries to parse the remaining data and gets the same erro

[Tutor] XSLT module

2005-12-09 Thread Jan Eden
Hi, I intend to use an XSLT processor, and found various modules (4suite, Sab-Pyth) on the web. Could anyone recommend a specific module for transforming XML files? Thanks, Jan -- It's overkill, of course. But you can never have too much overkill. __

[Tutor] Webprogramming with the MVC pattern

2006-03-23 Thread Jan Eden
Hi, I am about to build my first web application using the MVC pattern. Now I am wondering which might be the best way to initialize both a model and a presenter based on the model and its state. My controller should look like this: # model = Models.ModelFactory(cgi_parameters) view =

[Tutor] Re-instantiate within __init__

2006-04-19 Thread Jan Eden
Hi, is it correct that an object cannot be re-instantiated within it's __init__ method? I tried: class Omega: def Display(self): print self class Alpha(Omega): def __init__(self): self = Beta() class Beta(Omega): def __init__(self): pass

[Tutor] MySQLdb.converters.escape() type error

2006-05-28 Thread Jan Eden
Hi, after upgrading to MySQL 5 and MySQLdb 1.2.1_p2, I have the following problem: self.db=MySQLdb.connect(host=host, user=user, passwd=passwd, db=db, cursorclass=MySQLdb.cursors.DictCursor) stringel = 'Some string' stringel = self.db.escape(stringel) File "./test.py", line 12, in ? strin