Friday, March 26, 2010

comp.lang.python - 25 new messages in 8 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* python logging writes an empty file - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/d85aae7f2485a150?hl=en
* Revisiting Generators and Subgenerators - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/8c38b742691fcbc8?hl=en
* Have you embraced Python 3.x yet? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/aa1a1b6626b5b203?hl=en
* sum for sequences? - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/139c0887a359405b?hl=en
* Classes as namespaces? - 7 messages, 6 authors
http://groups.google.com/group/comp.lang.python/t/ddaed9721e7bbf45?hl=en
* OT: Meaning of "monkey" - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.python/t/9bb59d02c28e6ffb?hl=en
* Python database of plain text editable by notepad or vi - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/f8888a80f2f98438?hl=en
* Create a class at run-time - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/c6539cb7e78776cc?hl=en

==============================================================================
TOPIC: python logging writes an empty file
http://groups.google.com/group/comp.lang.python/t/d85aae7f2485a150?hl=en
==============================================================================

== 1 of 3 ==
Date: Fri, Mar 26 2010 7:29 am
From: Ovidiu Deac


> You set le level of your handler, but did not set the level of the logger
> itself.
> Replace file.setLevel(logging.INFO) by
> logging.getLogger().setLevel(logging.INFO)
>
> Log events are matched versus the logger level 1st, then the handler level
> (if applicable). Most of the time you don't need to tune your handler
> levels.

Right. Thanks a lot!!!

Now for the second part, any idea why logging.config.fileConfig doesn't work?

Now the logging.conf looks like this:

[formatters]
keys: detailed

[handlers]
keys: console,file

[loggers]
keys: root

[formatter_detailed]
format: %(name)s:%(levelname)s %(module)s:%(lineno)d: %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: detailed

[handler_file]
class=FileHandler
formatter=detailed
args=('output.log', 'w')
filename=output.log
mode=w

[logger_root]
level: INFO
handlers: file


== 2 of 3 ==
Date: Fri, Mar 26 2010 8:44 am
From: Jean-Michel Pichavant


Ovidiu Deac wrote:
>> You set le level of your handler, but did not set the level of the logger
>> itself.
>> Replace file.setLevel(logging.INFO) by
>> logging.getLogger().setLevel(logging.INFO)
>>
>> Log events are matched versus the logger level 1st, then the handler level
>> (if applicable). Most of the time you don't need to tune your handler
>> levels.
>>
>
> Right. Thanks a lot!!!
>
> Now for the second part, any idea why logging.config.fileConfig doesn't work?
>
> Now the logging.conf looks like this:
>
> [formatters]
> keys: detailed
>
> [handlers]
> keys: console,file
>
> [loggers]
> keys: root
>
> [formatter_detailed]
> format: %(name)s:%(levelname)s %(module)s:%(lineno)d: %(message)s
>
> [handler_console]
> class: StreamHandler
> args: []
> formatter: detailed
>
> [handler_file]
> class=FileHandler
> formatter=detailed
> args=('output.log', 'w')
> filename=output.log
> mode=w
>
> [logger_root]
> level: INFO
> handlers: file
>
I'm not sure, I don't use configuration file my loggers.

I don't think the handler_file should have a filename or mode field
though. Try removing them.

JM


== 3 of 3 ==
Date: Fri, Mar 26 2010 9:26 am
From: Ovidiu Deac


Anyway, thanks for the first part.

Anybody else has any idea why using the same configuration file works
when running the tests with nosetests and doesn't work with
logging.config.fileConfig() ?

==============================================================================
TOPIC: Revisiting Generators and Subgenerators
http://groups.google.com/group/comp.lang.python/t/8c38b742691fcbc8?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Mar 26 2010 7:29 am
From: Sebastien Binet


On Mar 25, 10:39 pm, Winston <winst...@stratolab.com> wrote:
> Here's my proposal again, but hopefully with better formatting so you
> can read it easier.
>
> -Winston
> -----------------
>
> Proposal for a new Generator Syntax in Python 3K--
> A Baton object for generators to allow subfunction to yield, and to
> make
> them symetric.

isn't a Baton what CSP calls a channel ?

there is this interesting PyCSP library (which implements channels
over greenlets, os-processes (via multiprocessing) or python threads)
http://code.google.com/p/pycsp

cheers,
sebastien.

==============================================================================
TOPIC: Have you embraced Python 3.x yet?
http://groups.google.com/group/comp.lang.python/t/aa1a1b6626b5b203?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Mar 26 2010 7:30 am
From: Gnarlodious


I have been using Py3 since October, I switched over for the
OrderedDict feature. Some parts are a little hard to get used to, but
others are easier. I am doing web programming, so the UTF 8 default is
a big improvement.

-- Gnarlie


== 2 of 2 ==
Date: Fri, Mar 26 2010 10:38 am
From: Nobody


On Fri, 26 Mar 2010 13:23:25 +0000, Harishankar wrote:

> Have you people embraced Python 3.x or still with 2.5 or 2.6?

Still with 2.6, and probably will be indefinitely.

I use Python mostly for Unix scripting: the kind of task which would
traditionally have used Bourne shell. For that purpose, life is much
simpler when everything is byte strings rather than character strings.

If I was doing the same thing on Windows, Python 3.x would probably make
more sense, as all of the OS APIs use Unicode (although there's still a
hell of a lot of software written using the "ANSI" interfaces; my
AppLocale folder has over a hundred entries).


==============================================================================
TOPIC: sum for sequences?
http://groups.google.com/group/comp.lang.python/t/139c0887a359405b?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Mar 26 2010 7:31 am
From: Steve Howell


On Mar 24, 4:19 pm, Paul Rubin <no.em...@nospam.invalid> wrote:
> kj <no.em...@please.post> writes:
> > Is there a sequence-oriented equivalent to the sum built-in?  E.g.:
> >   seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> use itertools.chain for this.  A few people have mentioned that sum will
> also work, but I think for that purpose it could have O(n**2)
> complexity.

I agree on the practical matter that itertools.chain and other
solutions are usually the way to go for most tasks that involve
iterating through several lists.

From a purely academic standpoint, I'm not convinced that sum() is
inefficient in terms of big-O complexity, though.

showell@showell-laptop:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
>>> class StupidList:
... def __init__(self, lst):
... print 'creating', lst
... self.lst = lst
... def __add__(self, other):
... self.lst += '|'
... self.lst.extend(other.lst)
... return self
...
>>> result = sum([StupidList([1, 2]), StupidList([3,4]),
StupidList([5,6])], StupidList([0]))
creating [1, 2]
creating [3, 4]
creating [5, 6]
creating [0]
>>> result.lst
[0, '|', 1, 2, '|', 3, 4, '|', 5, 6]

If I'm interpreting the above program correctly, then sum() is doing
the most efficient thing under the hood--it appears to do the
equivalent of += without creating unnecessary objects for intermediate
sums.

I think the special-case error message might be a case where
practicality simply beats out purity. It would be nice if sum() were
completely duck-typed-let-you-shoot-yourself-in-foot-if-you-know-what-
you-are-doing, but maybe this was such a pitfall at one time, that
extra safeguards were put into sum(). I wonder how severely sum(),
without the restriction, would underperform join() on modern versions
of Python, though.

>>> sum('1', '2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]

Note that you can easily fake out sum() to get duck typing.

>>> class EmptyStringStarter:
... def __add__(self, other): return other
...
>>> empty = EmptyStringStarter()
>>> sum(['hello ', 'world'], empty)
'hello world'

== 2 of 2 ==
Date: Fri, Mar 26 2010 7:56 am
From: Steve Howell


On Mar 26, 7:31 am, Steve Howell <showel...@yahoo.com> wrote:
> On Mar 24, 4:19 pm, Paul Rubin <no.em...@nospam.invalid> wrote:
>
> > kj <no.em...@please.post> writes:
> > > Is there a sequence-oriented equivalent to the sum built-in?  E.g.:
> > >   seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> > use itertools.chain for this.  A few people have mentioned that sum will
> > also work, but I think for that purpose it could have O(n**2)
> > complexity.
>
> I agree on the practical matter that itertools.chain and other
> solutions are usually the way to go for most tasks that involve
> iterating through several lists.
>
> From a purely academic standpoint, I'm not convinced that sum() is
> inefficient in terms of big-O complexity, though.
>
>  showell@showell-laptop:~$ python
>  Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
>  [GCC 4.3.3] on linux2
>  >>> class StupidList:
>  ...   def __init__(self, lst):
>  ...     print 'creating', lst
>  ...     self.lst = lst
>  ...   def __add__(self, other):
>  ...     self.lst += '|'
>  ...     self.lst.extend(other.lst)
>  ...     return self
>  ...
>  >>> result = sum([StupidList([1, 2]), StupidList([3,4]),
> StupidList([5,6])], StupidList([0]))
>  creating [1, 2]
>  creating [3, 4]
>  creating [5, 6]
>  creating [0]
>  >>> result.lst
>  [0, '|', 1, 2, '|', 3, 4, '|', 5, 6]
>
> If I'm interpreting the above program correctly, then sum() is doing
> the most efficient thing under the hood--it appears to do the
> equivalent of += without creating unnecessary objects for intermediate
> sums.
>
> I think the special-case error message might be a case where
> practicality simply beats out purity.  It would be nice if sum() were
> completely duck-typed-let-you-shoot-yourself-in-foot-if-you-know-what-
> you-are-doing, but maybe this was such a pitfall at one time, that
> extra safeguards were put into sum().  I wonder how severely sum(),
> without the restriction, would underperform join() on modern versions
> of Python, though.
>
>  >>> sum('1', '2')
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>  TypeError: sum() can't sum strings [use ''.join(seq) instead]
>
> Note that you can easily fake out sum() to get duck typing.
>
>  >>> class EmptyStringStarter:
>  ...   def __add__(self, other): return other
>  ...
>  >>> empty = EmptyStringStarter()
>  >>> sum(['hello ', 'world'], empty)
>  'hello world'

Looking at the code answers my own questions:

http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=markup

Look for builtin_sum().

First you see the guard against strings:

/* reject string values for 'start' parameter */
if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
PyErr_SetString(PyExc_TypeError,
"sum() can't sum strings [use ''.join(seq) instead]");
Py_DECREF(iter);
return NULL;
}
Py_INCREF(result);


Also, Paul's suspicion that sum() works in O(N squared) for lists is
confirmed by this comment:

/* It's tempting to use PyNumber_InPlaceAdd instead of
PyNumber_Add here, to avoid quadratic running time
when doing 'sum(list_of_lists, [])'. However, this
would produce a change in behaviour: a snippet like
empty = []
sum([[x] for x in range(10)], empty)
would change the value of empty. */

It's interesting, though, that you can construct classes pretty
easily, as I did above, that avoid the quadratic behavior, as long as
you do not mind mutating the start value, which I think is usually
fine, since the original start value usually is not useful afterward
anyway.

==============================================================================
TOPIC: Classes as namespaces?
http://groups.google.com/group/comp.lang.python/t/ddaed9721e7bbf45?hl=en
==============================================================================

== 1 of 7 ==
Date: Fri, Mar 26 2010 7:49 am
From: kj


What's the word on using "classes as namespaces"? E.g.

class _cfg(object):
spam = 1
jambon = 3
huevos = 2

breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)


Granted, this is not the "intended use" for classes, and therefore
could be viewed as a misuse ("that's what dictionaries are for",
etc.). But other than this somewhat academic objection[*], I really
can see no problem with using classes in this way.

And yet, I've come across online murky warnings against using
classes as "pseudo-namespaces". Is there some problem that I'm
not seeing with this technique?

~K

[*] My own subjective dislike for the widespread practice of using
triple quotes to comment out code is formally similar to this one
("the 'intended use' for triple-quoting is not to comment out code",
etc.). Here I find myself on the opposite side of the purist/pragmatic
divide. Hmmm.


== 2 of 7 ==
Date: Fri, Mar 26 2010 8:04 am
From: Harishankar


On Fri, 26 Mar 2010 14:49:02 +0000, kj wrote:

> What's the word on using "classes as namespaces"? E.g.
>
> class _cfg(object):
> spam = 1
> jambon = 3
> huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
>
> Granted, this is not the "intended use" for classes, and therefore could
> be viewed as a misuse ("that's what dictionaries are for", etc.). But
> other than this somewhat academic objection[*], I really can see no
> problem with using classes in this way.
>
> And yet, I've come across online murky warnings against using classes as
> "pseudo-namespaces". Is there some problem that I'm not seeing with
> this technique?
>
> ~K
>
> [*] My own subjective dislike for the widespread practice of using
> triple quotes to comment out code is formally similar to this one ("the
> 'intended use' for triple-quoting is not to comment out code", etc.).
> Here I find myself on the opposite side of the purist/pragmatic divide.
> Hmmm.

I myself am a humble beginner in many ways, but generally isn't that
(namespacing) achieved by using modules?

I don't find the need generally to assign namespace to local variables
and when there is a need for it, module level objects do the job.


== 3 of 7 ==
Date: Fri, Mar 26 2010 8:08 am
From: Philip Semanchuk

On Mar 26, 2010, at 10:49 AM, kj wrote:

>
>
> What's the word on using "classes as namespaces"? E.g.
>
> class _cfg(object):
> spam = 1
> jambon = 3
> huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
>
> Granted, this is not the "intended use" for classes, and therefore
> could be viewed as a misuse ("that's what dictionaries are for",
> etc.). But other than this somewhat academic objection[*], I really
> can see no problem with using classes in this way.
>
> And yet, I've come across online murky warnings against using
> classes as "pseudo-namespaces". Is there some problem that I'm
> not seeing with this technique?

I hope it's not problematic; I use it all the time.

A few differences about the way I do it:
- I respect PEP 8 for the class name (CamelCaps)
- If the attributes are supposed to be constants, I capitalize the
attributes
- I often add NONE with a value of zero so that bool(MyClass.NONE)
will evaluate to False and everything else will be True

Here's an example from my code:

class Apodization(object):
""" Apodization constants """
# These constants are arbitrary and may change.
# However bool(NONE) is guaranteed to be False
NONE = 0
GAUSSIAN = 1
LORENTZIAN = 2

Cheers
Philip

== 4 of 7 ==
Date: Fri, Mar 26 2010 8:47 am
From: Jon Clements


On 26 Mar, 14:49, kj <no.em...@please.post> wrote:
> What's the word on using "classes as namespaces"?  E.g.
>
> class _cfg(object):
>     spam = 1
>     jambon = 3
>     huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
> Granted, this is not the "intended use" for classes, and therefore
> could be viewed as a misuse ("that's what dictionaries are for",
> etc.).  But other than this somewhat academic objection[*], I really
> can see no problem with using classes in this way.
>
> And yet, I've come across online murky warnings against using
> classes as "pseudo-namespaces".  Is there some problem that I'm
> not seeing with this technique?
>
> ~K
>
> [*] My own subjective dislike for the widespread practice of using
> triple quotes to comment out code is formally similar to this one
> ("the 'intended use' for triple-quoting is not to comment out code",
> etc.).  Here I find myself on the opposite side of the purist/pragmatic
> divide.  Hmmm.

Given this example, I would go for the module and CONSTANT_NAMING
approach.

But yes, even in the docs. you can use a class as a C type-of struct.

I stick to the convention of a class knows what it's doing,
what it's doing it on, and a module just happens to contain those
classes.

C++ std::algorithm for instance,
makes sense it's called std, ditto algorithm and has shed loads in it,
but would I create a class called algorithm (unlikely).

I would tend to view modules as "namespace". Rightly or wrongly, just
lets you make the right design choice.

Jon.

== 5 of 7 ==
Date: Fri, Mar 26 2010 8:49 am
From: Jack Diederich


On Fri, Mar 26, 2010 at 10:49 AM, kj <no.email@please.post> wrote:
>
>
> What's the word on using "classes as namespaces"?  E.g.
>
> class _cfg(object):
>    spam = 1
>    jambon = 3
>    huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)

Classes as namespaces are a valid use case (I do it all the time).
Python 3 has a small cleanup that makes classes even closer to module
namespaces; namely the concept of "unbound methods" goes away. In
3.x when you get a function from a class you get the function itself
and not an unbound function.

-Jack


== 6 of 7 ==
Date: Fri, Mar 26 2010 8:50 am
From: Jean-Michel Pichavant


kj wrote:
> What's the word on using "classes as namespaces"? E.g.
>
> class _cfg(object):
> spam = 1
> jambon = 3
> huevos = 2
>
> breakfast = (_cfg.spam, _cfg.jambon, _cfg.huevos)
>
>
> Granted, this is not the "intended use" for classes, and therefore
> could be viewed as a misuse ("that's what dictionaries are for",
> etc.). But other than this somewhat academic objection[*], I really
> can see no problem with using classes in this way.
>
You cannot see the problem because there's no problem using classes as
namespaces.
> And yet, I've come across online murky warnings against using
> classes as "pseudo-namespaces". Is there some problem that I'm
> not seeing with this technique?
>
> ~K
>
import this
[snip]
Namespaces are one honking great idea -- let's do more of those!

Modules and dictionaries are no more namespaces than classes. So any
container is potentially a namespace.

JM


== 7 of 7 ==
Date: Fri, Mar 26 2010 10:51 am
From: kj

Thanks for all your comments.

I see that modules are arguably Python's standard way for implementing
namespaces. I guess I tend to avoid modules primarily because of
lingering mental trauma over incidents of insane/bizarro import
bugs in the past. (It's not rational, I know; it's like when one
develops an aversion for some previously liked food after a bout
of food poisoning with it.) Now I postpone creating a new Python
module until the pain of not doing so forces me beyond my phobia.
(Yes, you got that right, I'm a basket case.)

==============================================================================
TOPIC: OT: Meaning of "monkey"
http://groups.google.com/group/comp.lang.python/t/9bb59d02c28e6ffb?hl=en
==============================================================================

== 1 of 6 ==
Date: Fri, Mar 26 2010 7:56 am
From: Robert Kern


On 2010-03-26 08:14 AM, Luis M. González wrote:
> Webmonkey, Greasemonkey, monkey-patching, Tracemonkey, Jägermonkey,
> Spidermonkey, Mono (monkey in spanish), codemonkey, etc, etc, etc...
>
> Monkeys everywhere.
> Sorry for the off topic question, but what does "monkey" mean in a
> nerdy-geek context??

Partly because "monkey" is just a funny word.

As for monkey-patching, it came from the Zope community:

http://en.wikipedia.org/wiki/Monkey_patching

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

== 2 of 6 ==
Date: Fri, Mar 26 2010 8:45 am
From: Grant Edwards


On 2010-03-26, Luis M Gonz?lez <luismgz@gmail.com> wrote:
> Webmonkey, Greasemonkey, monkey-patching, Tracemonkey, J?germonkey,
> Spidermonkey, Mono (monkey in spanish), codemonkey, etc, etc, etc...
>
> Monkeys everywhere.
> Sorry for the off topic question, but what does "monkey" mean in a
> nerdy-geek context??

In colloquial English, "<something>-monkey" is a slang term for a
person who does a particular job for a living. For example "grease
monkey" is a slang term for an auto mechanic. A "code monkey" is
somebody who writes code for a living.

It can be slightly derogitory in some situations since it implies that
the task is mechanical and repetitive and doesn't require a lot of
creative thinking.

However, it can be used among peers in an affectionate way. One may
refer to one's peer as "code monkey" without offense, but a manager
could not refer to one of his employees as a "code monkey" without
risking it being seen as an insult.

Many people are accustomed to speaking anthopomorphically about
computers and programs, so when somebody writes a program that does
"foo", the name "foo monkey" seems natural for that program.

--
Grant Edwards grant.b.edwards Yow! I don't know WHY I
at said that ... I think it
gmail.com came from the FILLINGS in
my rear molars ...


== 3 of 6 ==
Date: Fri, Mar 26 2010 9:06 am
From: Robert Kern


On 2010-03-26 10:45 AM, Grant Edwards wrote:
> On 2010-03-26, Luis M Gonz?lez<luismgz@gmail.com> wrote:
>> Webmonkey, Greasemonkey, monkey-patching, Tracemonkey, J?germonkey,
>> Spidermonkey, Mono (monkey in spanish), codemonkey, etc, etc, etc...
>>
>> Monkeys everywhere.

> Many people are accustomed to speaking anthopomorphically about

"simiomorphically"?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

== 4 of 6 ==
Date: Fri, Mar 26 2010 9:08 am
From: Mel


Grant Edwards wrote:
> However, it can be used among peers in an affectionate way. One may
> refer to one's peer as "code monkey" without offense, but a manager
> could not refer to one of his employees as a "code monkey" without
> risking it being seen as an insult.

Somewhere on the Internet there's a particularly brilliant pop song called
"Code Monkey".

Mel.


== 5 of 6 ==
Date: Fri, Mar 26 2010 9:14 am
From: Jon Clements


On 26 Mar, 15:45, Grant Edwards <inva...@invalid.invalid> wrote:
> On 2010-03-26, Luis M  Gonz?lez <luis...@gmail.com> wrote:
>
> > Webmonkey, Greasemonkey, monkey-patching, Tracemonkey, J?germonkey,
> > Spidermonkey, Mono (monkey in spanish), codemonkey, etc, etc, etc...
>
> > Monkeys everywhere.
> > Sorry for the off topic question, but what does "monkey" mean in a
> > nerdy-geek context??
>
> In colloquial English, "<something>-monkey" is a slang term for a
> person who does a particular job for a living.  For example "grease
> monkey" is a slang term for an auto mechanic.  A "code monkey" is
> somebody who writes code for a living.
>
> It can be slightly derogitory in some situations since it implies that
> the task is mechanical and repetitive and doesn't require a lot of
> creative thinking.
>
> However, it can be used among peers in an affectionate way.  One may
> refer to one's peer as "code monkey" without offense, but a manager
> could not refer to one of his employees as a "code monkey" without
> risking it being seen as an insult.
>
> Many people are accustomed to speaking anthopomorphically about
> computers and programs, so when somebody writes a program that does
> "foo", the name "foo monkey" seems natural for that program.
>
> --
> Grant Edwards               grant.b.edwards        Yow! I don't know WHY I
>                                   at               said that ... I think it
>                               gmail.com            came from the FILLINGS in
>                                                    my rear molars ...

Can I take the slight risk that actually it can also be (as you said
'affectionately') in a very positive sense. The same way "geek" or
"nerd" can be applied. I used to be called "Big Geek" from the last
company I worked for on PAYE, but that was a compliment. But, I've
heard my step-dad call someone a "Geek" which is derogatory.

No winning when you have language that can mean "bad" (in meaning
"wicked/very good/awesome" (and even 'wicked' means good sometimes -
as in enthusiasm for an idea)) or actually "bad/not good [add your own
synonyms]". All valid, but which is good/bad :)

Anyway, this' a group for Python, not English :)

Feel better for my rant :)


Jon.

== 6 of 6 ==
Date: Fri, Mar 26 2010 9:54 am
From: Tim Wintle


On Fri, 2010-03-26 at 12:08 -0400, Mel wrote:
> Somewhere on the Internet there's a particularly brilliant pop song
> called "Code Monkey".

http://www.jonathancoulton.com/2006/04/14/thing-a-week-29-code-monkey/

(he's linked to the mp3 from there)


==============================================================================
TOPIC: Python database of plain text editable by notepad or vi
http://groups.google.com/group/comp.lang.python/t/f8888a80f2f98438?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Mar 26 2010 7:58 am
From: Jon Clements


On 26 Mar, 09:49, James Harris <james.harri...@googlemail.com> wrote:
> On 25 Mar, 22:56, Jon Clements <jon...@googlemail.com> wrote:
>
>
>
> > On 25 Mar, 22:40, James Harris <james.harri...@googlemail.com> wrote:
>
> > > I am looking to store named pieces of text in a form that can be
> > > edited by a standard editor such as notepad (under Windows) or vi
> > > (under Unix) and then pulled into Python as needed. The usual record
> > > locking and transactions of databases are not required.
>
> > > Another way to look at it is to treat the separate files as entries in
> > > a dictionary. The file name would be the key and the lines of the file
> > > the value.
>
> > > Anyone know of a database (with a Python interface) which will allow
> > > text files to be treated as database fields? If not I can just write
> > > it but I thought it best to ask if there was an existing solution
> > > first.
> ...
> > I could be missing something here, but aren't you basically just
> > talking about an OS's filesystem?
>
> For storage, yes. The files would be marked-up text stored in the
> filesystem. The "dbms" (I use the term loosely!) would provide access
> to them by some full or partial key mechanism yet to be determined.
> Read-only access would do - at least for now.
>
> > glob or listdir somewhere, then create a dict using the file contents
> > would meet your criteria, with very little lines of code -- but I
> > would be interested to know what the use-case was for this... Is it
> > read completely at start up time, or if each file contains a large
> > amount of lines and aren't fixed width (or has no other indexing
> > support without maintenance), then is a complete sequential-scan
> > required each time, or do you just tell the user to not update when
> > running (unless I s'pose something along the lines of a SIGHUP for
> > config files is applicable).
>
> All good questions. For now, at least, the files can be read-only and
> I'd want those on disk to be the master copies at all times. If I was
> writing it myself I'd probably 'cache' some files in memory and stat
> them before use. If newer I would reread the file.
>

It's hard to bore this group :)

>
>
> > Sorry, just don't understand why you'd want this.
>
> I tried to avoid boring folks with the details. I'm toying with some
> ideas for a way to help generate source code (in various languages,
> not just Python). If it goes ahead the text files would be mainly
> marked-up code snippets - with or without symbols that need to be
> replaced.
>
> Rather than write one single monolithic app I thought to split it into
> reusable components. One part being data access could perhaps be an
> existing database (and I'll take a look at jkn's suggestion).
>
> Think of the database as similar to an associative array stored on
> disk. The only difference is I may want to play fast and loose with
> the keys in some ways - e.g. check for partial key matches or return a
> list of part-matched keys. The language name could be part of the key
> but I'd also need to store variants for specific language versions.
> I'm not sure yet how it will all pan out. As I say, just throwing
> around some ideas.
>
> James

Thanks for the explanation.

Who admins and, who's editing this data?

I couldn't 100% guarantee that I could modify a text file and always
put the right
delimiter in the right place and remember to escape the relevant chars
(and I'm
probably not the 'average' user).

Any opposition to just putting it in a 'proper' DB, then 'blobbing'
the values?
(or just integrate a procedure/script/function whatever your chosen
RDBMS calls to choose it).
Or in some systems, 'externally referencing'... loads of DB's have
free front-ends,
and there are lots of Python libraries.

I think perhaps, all I'm saying is, I'd choose a different approach.
I'd provide a front-end, rather than choose to re-write the wheel over
DB's.

Be nice to know how you get on, if you'd be so kind?

Cheers,

Jon.

==============================================================================
TOPIC: Create a class at run-time
http://groups.google.com/group/comp.lang.python/t/c6539cb7e78776cc?hl=en
==============================================================================

== 1 of 3 ==
Date: Fri, Mar 26 2010 8:00 am
From: Michel


Well, I don't have the reference to the instance. The class is
actually instantiated later by a the unittest library.

On Mar 25, 6:18 pm, Michiel Overtoom <mot...@xs4all.nl> wrote:
> On 2010-03-25 23:00, Michel wrote:
>
> > I'm trying to dynamically create a class. What I need is to define a
> > class, add methods to it and later instantiate this class. Methods
> > need to be bound to the instance though, and that's my problem.
>
> Maybe this snippet is of any help?
>
> import functools
>
> class Template(object):
>      pass
>
> def printmyname(self):
>      print self.name
>
> t=Template()
> t.name="Pete"
> t.printmyname=functools.partial(printmyname,t)
>
> u=Template()
> u.name="Mary"
> u.printmyname=functools.partial(printmyname,u)
>
> t.printmyname()
> u.printmyname()
>
> Greetings,
>
> --
> "The ability of the OSS process to collect and harness
> the collective IQ of thousands of individuals across
> the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html

== 2 of 3 ==
Date: Fri, Mar 26 2010 8:54 am
From: Michel


I want to add a method to a class such that it can be invoked on
specifics instances.
You solution works (as well as Patrick's one), thanks !
I still have a question though. If I print the type of the self object
I get when my method is
called, I get "<class 'test.TestClass'>". I guess this is because the
method is defined as a class method.
This is ok in my case, but just out of curiosity, what should I do to
change this method to an instance method?

Thanks for your help,

Michel.

On Mar 25, 9:53 pm, I V <ivle...@gmail.com> wrote:
> On Thu, 25 Mar 2010 15:00:35 -0700, Michel wrote:
> > I'm trying to dynamically create a class. What I need is to define a
> > class, add methods to it and later instantiate this class. Methods need
> > to be bound to the instance though, and that's my problem. Here is what
> > I have so far:
>
> I'm not entirely sure what you mean by binding methods to the instance.
> Do you mean you need to dynamically add methods to a specific instance?
> Or that you need to add methods to a class, such that they can be invoked
> on specific instances? For the latter, just do:
>
> TestClass.test_foo = test_foo
>
> For the former, try:
>
> tc = TestClass()
> tc.test_foo = types.MethodType(test_foo, tc)

== 3 of 3 ==
Date: Fri, Mar 26 2010 10:29 am
From: Peter Otten <__peter__@web.de>


Michel wrote:

> Hi everyone,
>
> I'm trying to dynamically create a class. What I need is to define a
> class, add methods to it and later instantiate this class. Methods
> need to be bound to the instance though, and that's my problem. Here
> is what I have so far:
>
> method_template = "def test_foo(self):\
> #actual test_foo\
> pass"
> exec method_template
>
> TestClass = types.ClassType("MyTestClass", (unittest.TestCase, ), {})
> TestClass.__module__ = "test"
>
> now what to do next?

Just assign it:

>>> import unittest
>>> class MyTestClass(unittest.TestCase): pass
...
>>> def test_foo(self):
... self.assertEquals(1, 2)
...
>>> MyTestClass.test_foo = test_foo # <----
>>> unittest.main()
F
======================================================================
FAIL: test_foo (__main__.MyTestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 2, in test_foo
AssertionError: 1 != 2

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

If you don't know the method name beforehand use

setattr(MyTestClass, method_name, method), e. g:

>>> import unittest
>>> class MyTestClass(unittest.TestCase): pass
...
>>> def make_method(n):
... def test(self): self.assertEquals(2, n)
... return test
...
>>> for i in range(3):
... setattr(MyTestClass, "test_%d" % i, make_method(i))
...
>>> unittest.main()
FF.
======================================================================
FAIL: test_0 (__main__.MyTestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 2, in test
AssertionError: 2 != 0

======================================================================
FAIL: test_1 (__main__.MyTestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 2, in test
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (failures=2)

Peter

==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate