textfile: copy between 2 keywords

2015-09-10 Thread Gerald
Hey,

is there a easy way to copy the content between 2 unique keywords in a .txt 
file?

example.txt

1, 2, 3, 4
#keyword1
3, 4, 5, 6
2, 3, 4, 5
#keyword2 
4, 5, 6 ,7


Thank you very much


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


Mastering Python

2007-03-16 Thread Gerald
Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my
curriculum did not include Python programming yet I see many vacancies
for Python developers.I studied programming Pascal,C++ and Delphi.So I
need to catch up quickly and master Python programming.How do you
suggest that I achieve this goal?Is python platform independent?What
is the best way?And how long would it take before I can develop
applications using python?Can you recommend websites that feature a
gentle introduction to Python?

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


RE: Please critique my script

2011-07-14 Thread Gerald Britton
For me, there are some things I don't like much.  One-character
variable names stand out (tend to make the code hard to read).
Violation of PEP 8 guidelines, especially wrt spacing.  e.g.

result.append("%s[%s]%s" % (lastprefix, tails, lastsuffix))

not

result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))

Similarly for many of the assignments and expressions.

I see that you initialize count3 at line 39 but don't use it until
line 123.  I'd suggest that you move the initialization closer to its
use.

I think that you should move the call to open closer to where you're
going to write to the file.  In fact, you can do the thing a bit
neater with a context manager like this:

#--Print the dial-peers to file
with open(x, 'w') as o:
for line in catlist2:
figureDpn = count3 + 1000
dpn = str(figureDpn)
label = "dial-peer voice " + dpn
o.write(label)
o.write('\n')

...

Note that if you use this approach you don't need a separate call to close().

Also, you can do all the writing in one call to write(), something like this:

o.write(
label + '\n' +
"description *** local outbound dialpeer ***" + '\n' +
destpatt + '\n' +
"port " + p + '\n'
"forward-digits 7" if line[0:3] == y and q == "y" else ""
'\n'
)

Which keeps the eye focused on the data being written (at least for me).

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


Compile time evaluation of dictionaries

2011-03-10 Thread Gerald Britton
Today I noticed that an expression like this:

"one:%(one)s two:%(two)s" % {"one": "is the loneliest number", "two":
"can be as bad as one"}

could be evaluated at compile time, but is not:

>>> dis(compile(
... '"one:%(one)s two:%(two)s" % {"one": "is the loneliest number",
"two": "can be as bad as one"}',
... '','exec'))
 1           0 LOAD_CONST               0 ('one:%(one)s two:%(two)s')
             3 BUILD_MAP                2
             6 LOAD_CONST               1 ('is the loneliest number')
             9 LOAD_CONST               2 ('one')
            12 STORE_MAP
            13 LOAD_CONST               3 ('can be as bad as one')
            16 LOAD_CONST               4 ('two')
            19 STORE_MAP
            20 BINARY_MODULO
            21 POP_TOP
            22 LOAD_CONST               5 (None)
            25 RETURN_VALUE
>>>

Any idea why Python works this way?  I see that, in 3.2, an
optimization was done for sets (See "Optimizations" at
http://docs.python.org/py3k/whatsnew/3.2.html) though I do not see
anything similar for dictionaries.

--
Gerald Britton



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


Compile time evaluation of dictionaries

2011-03-14 Thread Gerald Britton
Jean-Michel Pichavan wrote:
>> Today I noticed that an expression like this:
>>
>> "one:%(one)s two:%(two)s" % {"one": "is the loneliest number", "two":
>> "can be as bad as one"}
>>
>> could be evaluated at compile time, but is not:
>>
>>
[snip]
>> Any idea why Python works this way?  I see that, in 3.2, an
>> optimization was done for sets (See "Optimizations" at
>> http://docs.python.org/py3k/whatsnew/3.2.html) though I do not see
>> anything similar for dictionaries.

>
>1/ because no one would ever see the difference.

The same thing could be said about sets, yet a similar optimization
was added to 3.2

>2/ immutables can always be evaluated before any high CPU consuming loop

immutables could also be evaluated at compile time, which would
obviate any such concern.

>3/ it would make the implementation more complex (i.e. more work for our
>beloved active community) for no gain

See my reply to 1/ above.

>4/ you can write C code to speed up things:
>http://docs.python.org/extending/extending.html, when really needed.

How do you spell red herring?

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


value of pi and 22/7

2011-03-20 Thread Gerald Britton
Surely on track for the *slowest* way to compute pi in Python (or any
language for that matter):

math.sqrt( sum( pow(k,-2) for k in xrange(sys.maxint,0,-1)  ) * 6.  )

Based on the Riemann zeta function:

   The sum of

1/k^2 for k = 1:infinity

   converges to pi^2 / 6

Depending on your horsepower and the size of sys.maxint on your
machine, this may take a few *days* to run.

Note: The sum in the Python expression above runs in reverse to
minimize rounding errors.

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


Namespaces in functions vs classes

2011-04-17 Thread Gerald Britton
I apologize if this has been answered before or if it is easy to find
in the docs. (I couldn't find it but might have missed it)

I'm trying to understand the differences between namespaces in class
definitions vs. function definitions.  Consider this function:

>>> def a():
... foo = 'foo'
... def g(x):
... return foo
... print g(1)
...
>>> a()
foo
>>>

Now, I replace the first "def" with "class":

>>> class a():
... foo = 'foo'
... def g(x):
... return foo
... print g(1)
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 5, in a
  File "", line 4, in g
NameError: global name 'foo' is not defined

So, the variable "foo" is not available inside the function inside
class as it is inside the (inner) function.  I figured this was just
because the class was still being defined, so I tried this:

>>> class a():
... foo = 'foo'
... def g(x):
... return foo
...
>>> x = a()
>>> x.g()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in g
NameError: global name 'foo' is not defined
>>>

which still fails.  I had expected that "foo" would be available
within the namespace of the object instance.  I was wrong.  For my
final attempt, I add the prefix "a." to my use of "foo"

>>> class a():
... foo = 'foo'
... def g(x):
... return a.foo
...
>>> x = a()
>>> x.g()
'foo'

So, this works and I can use it.  However,  I would like a deeper
understanding of why I cannot use "foo" as an unqualified variable
inside the method in the class.  If Python allowed such a thing, what
problems would that cause?

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


Re: Namespaces in functions vs classes

2011-04-19 Thread Gerald Britton
Ethan -- I'm just getting back to this question.  If you recall, you asked:

[snip]

8<
"script with possible name clashes"

eggs = 'scrambled eggs'
meat = 'steak'

class Breakfast():
 meat = 'spam'
 def serve(self):
 print("Here's your %s and %s!" %
(eggs, meat))

Breakfast().serve()
8<

>What will serve print?

Well, I think it should print the same thing as:

def Breakfast():
meat = 'spam'
def serve(self):
print("Here's your %s and %s!" %
   (eggs, meat))
return serve(None)

Breakfast()

but it doesn't!  The function definition uses the local (to the
function) variable "meat", whereas the class method uses the global
definition of "meat".  The class attribute "meat" is not seen by the
serve method unless it is qualified.  I now understand the Python does
not consider a class definition as a separate namespace as it does for
function definitions.  That is a helpful understanding.

Anyway, thanks for jumping in to the discussion.

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


Re: Namespaces in functions vs classes

2011-04-19 Thread Gerald Britton
>Gerald Britton wrote:
>> I now understand the Python does
>> not consider a class definition as a separate namespace as it does for
>> function definitions.  That is a helpful understanding.

>That is not correct.  Classes are separate namespaces -- they just
>aren't automatically searched.  The only namespaces that are
>automatically searched are local, non-local, global, and built-in.

I see you misunderstood my observation:  Python does not consider a class
definition as a separate namespace *as it does* for function definitions.

Of course classes are separate namespaces, or they would not work at
all.  However,
Python does not automatically search class namespaces -- even within the class
definition -- *as it does* within function definitions.

That is the key insight I was seeking.  To search a class namespace one must
qualify the lookup with the class or instance name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Question about abstract base classes and abstract properties -- Python 2.7

2016-09-04 Thread Gerald Britton
I was rereading the 2.7 docs  about abstract base classes the other day.  I
found this:

"This defines a read-only property; you can also define a read-write
abstract property using the ‘long’ form of property declaration:"

along with an example.  so I copied the example and put in a little
surrounding code:


from abc import ABCMeta, abstractproperty

class C:
__metaclass__ = ABCMeta
def getx(self): pass
def setx(self, value): pass
x = abstractproperty(getx, setx)

class D(C):
@property
def x(self):self._x

d = D()
print(d)

When I ran this, I expected an exception, since I defined a read/write
abstract property but only implemented the read operation.  However, the
example runs fine. That is the class D can be instantiated without error.
Of course I cannot set the property since I didn't implement that part.

Now, If I don't implement the property at all, I can' instantiate the
class.  I get:

"TypeError: Can't instantiate abstract class D with abstract methods x"

which is what I would expect.  What I don't understand is why I don't get a
similar error when I implement the read operation for the property but not
the write operation.

If this actually doesn't work (catching the non-implementation at
instantiation time), then why is it documented this way.  To me at least
the doc implies that it *will* raise on the missing write property
implementation.

Is this a doc bug, an  ABC bug or just me? (I've been known to be buggy
from time to time!)

-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Any ReST aware editors?

2016-09-22 Thread Gerald Britton
>
> I have editors which will use syntax highlighting on .rst files, but I'm
> hoping for something a bit smarter. What I'd like is an editor with a
> split window, one side showing the rst
> that I can edit, the other side showing the formatted text updated as I
> type. (Or at least, updated every thirty seconds or so.) Anybody know
> anything like that?


Visual Studio Code does an OK job with the

reStructuredText Language Support for Visual Studio Code

Extension
-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to reduce the DRY violation in this code

2016-09-27 Thread Gerald Britton
>
> I have a class that takes a bunch of optional arguments. They're all
> optional, with default values of various types. For simplicity, let's say
> some are ints and some are floats: class Spam:
> def __init__(self, bashful=10.0, doc=20.0, dopey=30.0,
> grumpy=40, happy=50, sleepy=60, sneezy=70):
> # the usual assign arguments to attributes dance...
> self.bashful = bashful
> self.doc = doc
> # etc.


This looks like a situation where the GoF Builder pattern might help

-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


Question about abstract base classes and abstract properties -- Python 2.7

2016-10-22 Thread Gerald Britton
I was rereading the 2.7 docs  about abstract base classes the other day.  I
found this line in the usage section of the abc.abstractproperty function:

"This defines a read-only property; you can also define a read-write
abstract property using the ‘long’ form of property declaration:"

along with an example.  so I copied the example and put in a little
surrounding code:


from abc import ABCMeta, abstractproperty

class C(object):
__metaclass__ = ABCMeta
def getx(self): pass
def setx(self, value): pass
x = abstractproperty(getx, setx)

class D(C):
@property
def x(self):self._x

d = D()
print(d)

When I ran this, I expected an exception, since I defined a read/write
abstract property but only implemented the read operation.  However, the
example runs fine. That is the class D can be instantiated without error.
Of course I cannot set the property since I didn't implement that part.

Now, If I don't implement the property at all, I can' instantiate the
class.  I get:

"TypeError: Can't instantiate abstract class D with abstract methods x"

which is what I would expect.  What I don't understand is why I don't get a
similar error when I implement the read operation for the property but not
the write operation.

If this actually doesn't work (catching the non-implementation at
instantiation time), then why is it documented this way.  To me at least
the doc implies that it *will* raise on the missing write property
implementation (otherwise, what's the point of the example?)

Is this a doc bug, an  ABC bug or just me? (I've been known to be buggy
from time to time!)

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


Re: Question about abstract base classes and abstract properties -- Python 2.7

2017-01-10 Thread Gerald Britton
I was rereading the 2.7 docs  about abstract base classes the other day.  I
found this line in the usage section of the abc.abstractproperty function:

"This defines a read-only property; you can also define a read-write
abstract property using the ‘long’ form of property declaration:"

along with an example.  so I copied the example and put in a little
surrounding code:


from abc import ABCMeta, abstractproperty

class C(object):
__metaclass__ = ABCMeta
def getx(self): pass
def setx(self, value): pass
x = abstractproperty(getx, setx)

class D(C):
@property
def x(self):self._x

d = D()
print(d)

When I ran this, I expected an exception, since I defined a read/write
abstract property but only implemented the read operation.  However, the
example runs fine. That is the class D can be instantiated without error.
Of course I cannot set the property since I didn't implement that part.

Now, If I don't implement the property at all, I can't instantiate the
class.  I get:

"TypeError: Can't instantiate abstract class D with abstract methods x"

which is what I would expect.  What I don't understand is why I don't get a
similar error when I implement the read operation for the property but not
the write operation.

If this actually doesn't work (catching the non-implementation at
instantiation time), then would it be appropriate to amend the
documentation?  To me at least the doc implies that it *will* raise on the
missing write property implementation (otherwise, what's the point of the
example?)

Or, am I just not grokking it properly?
-- 
https://mail.python.org/mailman/listinfo/python-list


Fwd: What are your opinions on .NET Core vs Python?

2017-01-29 Thread Gerald Britton
>
> As you guys might know, .NET Core is up and running, promising a
> "cross-platform, unified, fast, lightweight, modern and open source
> experience" (source: .NET Core official site). What do you guys think about
> it? Do you think it will be able to compete with and overcome Python in the
> opensource medium?


It's an apples/oranges comparison.

.NET is a library that can be used from many languages, including Python.
 (Not just IronPython, but also Python for .NET (pythonnet.sourceforge*.*net
*))*

Python is a language that can use many libraries, including .NET

The set of libraries that can be used from all the languages that can also
use .NET (out of the box, that is) is smaller.
-- 
https://mail.python.org/mailman/listinfo/python-list


What are your opinions on .NET Core vs Python?

2017-01-30 Thread Gerald Britton
On Sun, Jan 29, 2017 at 1:06 AM, Juan C. https://mail.python.org/mailman/listinfo/python-list>> wrote:
>
> >
> >* As you guys might know, .NET Core is up and running, promising a 
> >"cross-platform, unified, fast, lightweight, modern and open source 
> >experience" (source: .NET Core official site). What do you guys think about 
> >it? Do you think it will be able to compete with and overcome Python in the 
> >opensource medium?
> *

Oh, I forgot to say, I was indeed talking about .NET Core in general,
> the framework, but I also had C#/ASP .NET Core in mind. The real
> comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I
> personally had to work with C# a few times, it's a great language, but
> my main problem was that it was Windows only and most of my personal
> programs run on Linux. Yes, we have Mono, but it really didn't feel
> "right" to me, it seemed like a workaround, I wanted something really
> "native".

Python still has my heart, but .NET Core tempts me. One great thing of
> coding in C# would be no GIL.

For what it's worth, IronPython, which is implemented in .NET, has no GIL
and doesn't need it since ir runs on the CLR.  That means that, for some
things, IronPython can be more performant.

No word yet if the IronPython project intends to port to .NET core or
enable to run it on OS's other than Windows.

Also, it's worth noting that the PythonNet project has a branch working on
.NET core support


-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


Question about imports and packages

2016-05-24 Thread Gerald Britton
I'm trying to understand packages in Python, especially Intra Package
References.

>From https://docs.python.org/2/tutorial/modules.html#packages i see that:

you can use absolute imports to refer to submodules of siblings packages.



This is what I can't get to work in my case. Here's the setup:

directory testpkg containing three files:
1. an empty __init__.py

2. a testimport.py which has:


from testpkg.testimported import A
a = A()
print type(a)

3. a testimported.py which has:


class A():
pass

When I run

python testimport.py

I get:

Traceback (most recent call last):
File "testimport.py", line 1, in 
from testpkg.testimported import A
ImportError: No module named testpkg.testimported

However, I thought I was doing what the doc describes for intra package
imports. What am I missing?

Or is the problem simply that I do not have subpackages?

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


Question about imports and packages

2016-05-24 Thread Gerald Britton
On Wed, 25 May 2016 10:00 am, Steven D'Aprano wrote:
>On Wed, 25 May 2016 09:35 am, Gerald Britton wrote:
>
>For brevity, here's your package setup:
>
>
>testpkg/
>+-- __init__.py
>+-- testimport.py which runs "from testpkg.testimported import A"
>+-- testimported.py containing class A
>
>Your package layout is correct. But:
>
>> When I run
>>
>> python testimport.py
>>
>> I get:
>>
>> Traceback (most recent call last):
>> File "testimport.py", line 1, in 
>> from testpkg.testimported import A
>> ImportError: No module named testpkg.testimported
>
>The problem is that you are running the Python script from *inside* the
>package. That means, as far as the script can see, there is no longer a
>package visible -- it cannot see its own outside from the inside.
>
>cd to a directory *outside* the package, and run:
>
>python testpkg/testimport.py
>
>and it should work. Better: make sure the testpkg directory is somewhere in
>your PYTHONPATH, and run:
>
>python -m testpkg.testimport
>
>
>which tells Python to search for the package, rather than using a hard-coded
>path.
>
>
>The package directory has to be visible to the import system. That means it
>must be INSIDE one of the locations Python searches for modules. The
>current directory will do, but to be absolutely clear, that means that the
>package directory itself must be IN the current directory:
>
>
>./
>+-- pkgtest/
>+-- __init__.py
>+-- testimport.py
>+-- testimported.py
>
>
>Or you can use any other directory found in sys.path.
>
>
>
>--
>Steven

Thanks for the explanation, Steven.  I'm just having trouble
reconciling this with the docs which seems to imply that an intra
package import should work from inside the package.

This bit:

"When packages are structured into subpackages (as with the sound
package in the example), you can use absolute imports to refer to
submodules of siblings packages. For example, if the module
sound.filters.vocoder needs to use the echo module in the
sound.effects package, it can use from sound.effects import echo."

seems to say that what I was trying to do should work, since I'm
trying to use an absolute import to refer to a sibling module.  From
that I got the idea that the importer will ascend the package tree as
well as follow it down.

Or is the key idea here *submodule*? Since there are no modules in the
"sound" package in the example, only other packages?

FWIW this all came about when I was using pylint on a package where
the imports were relative.  Pylint tells me to add the package name as
the first qualifier (it warns about relative imports), which I tried
to do, only to find that doesn't work.

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


Package setup best practice style question

2016-05-28 Thread Gerald Britton
suppose I have a simple python project setup like this:

Project diectory
 prog.py
 pkg directory
  __init__.py
  mod1.py
   class A:

In order to have class A (unqualified) available from prog.py, there are a
few options that I know about.  I'm currently considering two of them and
would like some feedback on best practices.

1. in pkg.__init__.py add:

   from pkg.mod1 import A

in prog.py add:

   from pkg import A

2. leave __init__.py empty
in prog.py add:

 from pkg.mod1 import A


Is there a preference or best practice that would indicate to prefer method
1 or method 2?  Are there methods 3, 4, 5, ... that I should consider that
are even better?
-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


re: Operator Precedence/Boolean Logic

2016-07-16 Thread Gerald Britton
>Earlier you said:

>* > 1. Graphs -- the kind mathematicians define with "Let G =(V,E) be a
*>* > graph..."
*>  >* I would make the empty graph (zero nodes) falsey, and non-empty
graphs (one
*>* or more nodes) truthy.
*>  >  >* > 2. Automata which in a way are special kinds of graphs
*>  >* As above.
*

>Now you say:

>* > If on the other hand you are giving that “return True”as a serious useful
*>* > definition?
*>  >* Sure. An automata is an object, and by default, all objects are
"something"
*>* and hence truthy. That's the "no-brainer" answer, it requires no
*>* justification at all. If you think the answer should be something else,
*>* *that* is what needs justification. Why shouldn't it be truthy?
*

>How do we put these two together>


easy peasy:  lists, tuples, dicts etc are also objects.  They define
their truthiness by the presence of members.  An empty list has a
boolean value of false.

for a custom object, you can accept the default boolean value of true
or override __bool__ so that it returns what you want.  So for a graph
G, I'd say if |V| = |E| = 0, G has a boolean value of false.

Maybe 
thusly?https://en.wikibooks.org/wiki/Sufism/Nasrudin#Of_any_two_options_choose_the_third

--


   - Previous message (by thread): Operator Precedence/Boolean Logic
   <https://mail.python.org/pipermail/python-list/2016-July/711778.html>
   - Next message (by thread): EuroPython 2016: Recruiting Offers
   <https://mail.python.org/pipermail/python-list/2016-July/711769.html>
   - *Messages sorted by:* [ date ]
   <https://mail.python.org/pipermail/python-list/2016-July/date.html#711777>
[ thread ]
   <https://mail.python.org/pipermail/python-list/2016-July/thread.html#711777>
[ subject ]
   <https://mail.python.org/pipermail/python-list/2016-July/subject.html#711777>
[ author ]
   <https://mail.python.org/pipermail/python-list/2016-July/author.html#711777>

--
More information about the Python-list mailing list
<https://mail.python.org/mailman/listinfo/python-list>

-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


Learning Descriptors

2016-07-31 Thread Gerald Britton
Today, I was reading RH's Descriptor HowTo Guide at

https://docs.python.org/3/howto/descriptor.html?highlight=descriptors

I just really want to fully "get" this.

So I put together a little test from scratch.  Looks like this:

class The:
class Answer:
def __get__(self, obj, type=None):
return 42

>>> The.Answer

>>>

but, I expected to see 42.

So, digging deeper I read:

For classes, the machinery is in type.__getattribute__() which transforms
B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:

def __getattribute__(self, key):
"Emulate type_getattro() in Objects/typeobject.c"
v = object.__getattribute__(self, key)
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v

OK, so I copied this function, then ran it and got:

>>> __getattribute__(The, 'Answer')
42

So, what I don't get is why the "B.x into B.__dict__['x'].__get__(None, B)"
part doesn't work in my case.

I'm sure I'm missing something here (that`s usually the case for me <:‑|) ,
but what?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Learning Descriptors

2016-07-31 Thread Gerald Britton
>On Sun, Jul 31, 2016 at 6:33 AM, Gerald Britton
> wrote:
>> Today, I was reading RH's Descriptor HowTo Guide at
>>
>> https://docs.python.org/3/howto/descriptor.html?highlight=descriptors
>>
>> I just really want to fully "get" this.
>>
>> So I put together a little test from scratch.  Looks like this:
>>
>> class The:
>> class Answer:
>> def __get__(self, obj, type=None):
>> return 42
>>
>>>>> The.Answer
>> 
>>>>>
>>
>> but, I expected to see 42.
>>
>> So, digging deeper I read:
>>
>> For classes, the machinery is in type.__getattribute__() which transforms
>> B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:
>>
>> def __getattribute__(self, key):
>> "Emulate type_getattro() in Objects/typeobject.c"
>> v = object.__getattribute__(self, key)
>> if hasattr(v, '__get__'):
>> return v.__get__(None, self)
>> return v
>>
>> OK, so I copied this function, then ran it and got:
>>
>>>>> __getattribute__(The, 'Answer')
>> 42
>>
>> So, what I don't get is why the "B.x into B.__dict__['x'].__get__(None,
B)"
>> part doesn't work in my case.
>>
>> I'm sure I'm missing something here (that`s usually the case for me
<:‑|) ,
>> but what?
>
>Obviously that __getattribute__ is not exactly like the real one. The
>real one looks up __get__ as a *method* of B.__dict__['x'], which
>requires that B.__dict__['x'] be an instance of some class that
>defines __get__, not the class itself. Try this:

Thanks Ian, that helps, though I suppose I should point out that the
article covers both objects and classes.  It was the section about classes
I was referring to.  Reading it again, it's obvious that 'B' is a class and
'x' is some attribute of B.  What's not clear is that 'x' must be an
*instance* of something for this to work as I expected.

Even so, If I translate my The.Answer to
The.__dict__['Answer'].__get__(None, The), (as per the article) it works as
I expect:

>>> The.__dict__['Answer'].__get__(None, The)
42

Note that 'Answer' is not an instance; it's another class.  So I suppose
the document is a little ambiguous on that point.

That's OK. I just need a mental note to remember what it really means.

>
>py> class Answer:
>... def __get__(self, obj, type):
>... return 42
>...
>py> class The:
>... Answer = Answer()
>...
>py> The.Answer
>42

On Sun, Jul 31, 2016 at 8:33 AM, Gerald Britton 
wrote:

> Today, I was reading RH's Descriptor HowTo Guide at
>
> https://docs.python.org/3/howto/descriptor.html?highlight=descriptors
>
> I just really want to fully "get" this.
>
> So I put together a little test from scratch.  Looks like this:
>
> class The:
> class Answer:
> def __get__(self, obj, type=None):
> return 42
>
> >>> The.Answer
> 
> >>>
>
> but, I expected to see 42.
>
> So, digging deeper I read:
>
> For classes, the machinery is in type.__getattribute__() which transforms
> B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:
>
> def __getattribute__(self, key):
> "Emulate type_getattro() in Objects/typeobject.c"
> v = object.__getattribute__(self, key)
> if hasattr(v, '__get__'):
> return v.__get__(None, self)
> return v
>
> OK, so I copied this function, then ran it and got:
>
> >>> __getattribute__(The, 'Answer')
> 42
>
> So, what I don't get is why the "B.x into B.__dict__['x'].__get__(None,
> B)" part doesn't work in my case.
>
> I'm sure I'm missing something here (that`s usually the case for me <:‑|) ,
> but what?
>
>


-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Abstract Methods & Abstract Class

2005-10-20 Thread Gerald Klix
Isn't 

class AbstractBase:
def method(self):
raise NotImplementedError( "abstract method called" )

the right thing to do?

Gerald

- Original Message - 
From: "Andreas Kostyrka" <[EMAIL PROTECTED]>
To: "Iyer, Prasad C" <[EMAIL PROTECTED]>
Cc: 
Sent: Thursday, October 20, 2005 8:56 AM
Subject: Re: Abstract Methods & Abstract Class


> On Thu, Oct 20, 2005 at 12:05:05PM +0530, Iyer, Prasad C wrote:
> > 
> > Do we have something like abstract methods & Abstract class.
> > 
> > So that my class would just define the method. And the implementation
> > would be defined by somebody else.
> 
> class AbstractBase:
> def method(self):
> raise TypeError("abstract method called")
> 
> But basically, Python does not have abstract methods, and usually doesn't
> need them.
> 
> Andreas
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Listen in promiscuous mode (Sniffer) on UDP port 162 and copy packetsto another port

2005-10-26 Thread Gerald Klix
Hi Henko,
the proper solution to this problem ist to use on libpcap's python bindings,
like for example Billy The Kid.

Here are some pointers:
http://home.student.utwente.nl/g.v.berg/btk/
http://pycap.sourceforge.net/
http://monkey.org/~dugsong/pypcap/
http://www.tcpdump.org/ (libpcap)

HTH Gerald

- Original Message -
From: Henko Gouws (H)
To: [email protected]
Sent: Tuesday, October 25, 2005 10:26 AM
Subject: Listen in promiscuous mode (Sniffer) on UDP port 162 and copy
packetsto another port


Dear reader

An application A opens UDP port 162 and listens for incoming packets.  We
have another application B that wants to receive the same information from
UDP port 162 but this application cannot open the port 162 because it is
already opened by application A.  We want both A and B to receive
information from port 162.

Does anyone know how to implement such a solution using Python or give
relevant advice?  Alternatively does anyone know about any postings that
involved Ethernet sniffers or Python programs listening to network traffic
in promiscuous mode?

Thank you

regards
Henko Gouws (Pr. Eng.)
Senior Engineer: Technical Product Development
Telkom Development Lab
Tel:  +27 12 529 7385
Fax: +27 12 548 0065

~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at
http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~





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

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


Re: Is there no compression support for large sized strings in Python?

2005-12-01 Thread Gerald Klix
Did you consider the mmap library?
Perhaps it is possible to avoid to hold these big stings in memory.
BTW: AFAIK it is not possible in 32bit windows for an ordinary programm 
to allocate more than 2 GB. That restriction comes from the jurrasic 
MIPS-Processors, that reserved the upper 2 GB for the OS.

HTH,
Gerald

Claudio Grondi schrieb:
> "Fredrik Lundh" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
> news:[EMAIL PROTECTED]
> 
>>Claudio Grondi wrote:
>>
>>
>>>What started as a simple test if it is better to load uncompressed data
>>>directly from the harddisk or
>>>load compressed data and uncompress it (Windows XP SP 2, Pentium4  3.0
> 
> GHz
> 
>>>system with 3 GByte RAM)
>>>seems to show that none of the in Python available compression libraries
>>>really works for large sized
>>>(i.e. 500 MByte) strings.
>>>
>>>Test the provided code and see yourself.
>>>
>>>At least on my system:
>>> zlib fails to decompress raising a memory error
>>> pylzma fails to decompress running endlessly consuming 99% of CPU time
>>> bz2 fails to compress running endlessly consuming 99% of CPU time
>>>
>>>The same works with a 10 MByte string without any problem.
>>>
>>>So what? Is there no compression support for large sized strings in
> 
> Python?
> 
>>you're probably measuring windows' memory managment rather than the com-
>>pression libraries themselves (Python delegates all memory allocations
>>256 bytes
>>to the system).
>>
>>I suggest using incremental (streaming) processing instead; from what I
> 
> can tell,
> 
>>all three libraries support that.
>>
>>
> 
> 
> Have solved the problem with bz2 compression the way Frederic suggested:
> 
> fObj = file(r'd:\strSize500MBCompressed.bz2', 'wb')
> import bz2
> objBZ2Compressor = bz2.BZ2Compressor()
> lstCompressBz2 = []
> for indx in range(0, len(strSize500MB), 1048576):
>   lowerIndx = indx
>   upperIndx = indx+1048576
>   if(upperIndx > len(strSize500MB)): upperIndx = len(strSize500MB)
> 
> lstCompressBz2.append(objBZ2Compressor.compress(strSize500MB[lowerIndx:upper
> Indx]))
> #:for
> lstCompressBz2.append(objBZ2Compressor.flush())
> strSize500MBCompressed = ''.join(lstCompressBz2)
> fObj.write(strSize500MBCompressed)
> fObj.close()
> 
> :-)
> 
> so I suppose, that the decompression problems can also be solved that way,
> but  :
> 
> This still doesn't for me answer the question what the core of the problem
> was, how to avoid it and what are the memory request limits which should be
> considered when working with large strings?
> Is it actually so, that on other systems than Windows 2000/XP there is no
> problem with the original code I have provided?
> Maybe a good reason to go for Linux instead of Windows? Does e.g. Suse or
> Mandriva Linux have also a memory limit a single Python process can use?
> Please let me know about your experience.
> 
> Claudio
> 
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: Examples of Quality Technical Writing

2005-12-07 Thread Gerald Klix
That's the most accurate description of Xah's behaviour I've read so far.

Jon Perez schrieb:
> Sherm Pendley wrote:
> 
> 
>>Xah's a pretty well-known troll in these parts. I suppose he thinks someone
>>is going to take the bait and rush to "defend" the other languages or some
>>such nonsense.
> 
> 
> Actually, I think Xah often has a point, except he can't
> seem to express it without resorting to profanity and
> a controlled manner, thus giving the impression he's a
> troll.
> 
> Also, he seems to be blissfully unaware of the concept
> of netiquette. ;-)
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient 'tail' implementation

2005-12-08 Thread Gerald Klix
As long as memory mapped files are available, the fastest
method is to map the whole file into memory and use the
mappings rfind method to search for an end of line.

The following code snippets may be usefull:
 reportFile = open( filename )
 length = os.fstat( reportFile.fileno() ).st_size
 if length == 0:
 # Don't map zero length files, windows will barf
 continue
 try:
 mapping = mmap.mmap( reportFile.fileno(), length,
 mmap.MAP_PRIVATE, mmap.PROT_READ )
 except AttributeError:
 mapping = mmap.mmap(
 reportFile.fileno(),
 0, None,
 mmap.ACCESS_READ )

Then you can use
mapping.rfind( os.linesep )
to find the end of the but last line and so on.

This is very fast, because nearly all work is done by are rfind, which
is implemented in C and the OS' paging logic.

HTH,
Gerald

[EMAIL PROTECTED] schrieb:
> Mike Meyer wrote:
> 
>>It would probably be more efficient to read blocks backwards and paste
>>them together, but I'm not going to get into that.
>>
> 
> That actually is a pretty good idea. just reverse the buffer and do a
> split, the last line becomes the first line and so on. The logic then
> would be no different than reading from beginning of file. Just need to
> keep the last "half line" of the reversed buffer if the wanted one
> happens to be across buffer boundary.
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do a "Python beginners e-mail list" exist?

2005-07-07 Thread Gerald Klix
Perhaps irc://irc.freenode.net##python
Note the double #

This channel is less crowed as the #python channels are.

Alessandro Brollo schrieb:
> Far from a professional programmer, I'm simply a
> newbie Python user. Two basic questions:
> 
> 1. I don't want to post banal questions about Python
> to main Python list. Does a "banal Python questions
> list" or a "Python beginners list" exist?
> 
> 2. There is somewhere a very patient fellow willing to
> be my free "python tutor" by personal e-mailing
> outside the mail list? . The ideal candidate would be
> someone, sharing with me some other fields of interest
> (I'm a middle-aged Italian pathologist, with some
> dBase III and dBase IV past programming experience,
> and I like nature and mainly horses). 
> 
> Thanks
> 
> Alessandro Brollo
> 
> 
> 
>   
> 
>   
>   
> ___ 
> Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
> http://mail.yahoo.it

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


gmail access

2005-09-03 Thread gerald gillespie
how do I access my new Gmail account [EMAIL PROTECTED]

_
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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


Re: Python GUIs

2005-09-21 Thread Gerald Klix
if you write
B = '\x12','\x32'
you get an immutable tuple.

To get a mutable list use:
B = [ '\x12','\x32' ]

HTH,
Gerald

Tuvas schrieb:
> As a bit more of an update, I have decided to create a list of strings,
> but am having a problem. To illistrate this in a simple manner.
> 
> B='\x12','\x32'
> B[0]='\x12'
> 
> I cannot get this to work, and I need to get it to work somehow. How
> can I make it happen? Is there a function that I should use, a special
> trick, etc? Or is there just no way to make it work? Thanks!
> 

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: Making a DLL with python?

2005-03-31 Thread Gerald Klix
I think you can, as long as you have a C-Compiler available.
I used pyrex to embedd python into a Linux PAM-Module and
i used C-Types to embbed Python into a Windows DLL. With hindsight, the 
pyrex solution was much fatser to develop and less complicated.

Pyrex provides an example.
Ctypes: http://starship.python.net/crew/theller/ctypes/
Pyrex: http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/
Feel free to ask, if you need a more complex example,
than the one, that comes with pyrex.
HTH,
Gerald
Larry Bates schrieb:
I don't think you can make a .DLL (but someone else might).
Why can't you use a COM server?  MS seems to have written
some pretty sophisticated software using COM objects.
Almost all languages can dispatch a COM object easily.
-Larry
[EMAIL PROTECTED] wrote:
Can I use python to make a regular Windows DLL that will be called from
other programs?
I know I can use the win32 extensions to make a COM server, but I need
a straight DLL.
Regards,
Phillip

Phillip Piper
A man's life does not consist in the abundance of his possessions
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python backend binding to PAM, NSS or pppd

2005-04-07 Thread Gerald Klix
Hi Heiko, Hi all,
I have a PAM-library available that embedds Python.
Just tell me if you need it and I will publish it.
HTH,
Gerald
Heiko Wundram schrieb:
Hey all!
Before I start hacking away, I'm looking for a Python backend binding for 
libpam or libnss, or a python binding for the pppd plugin mechanism.

I'm trying to set up an SQL authentication scheme for virtual user accounts 
used for mail and PPTP-VPN-access, and I'd love to do the authentication bit 
in Python. And, yes, I know about pam-mysql and nss-mysql, but both projects 
are old and unmaintained, and I use Oracle as backend DB anyway.

If anybody knows of any project which has implemented a part of this, I'd love 
to hear about the effort...


--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python backend binding to PAM, NSS or pppd

2005-04-07 Thread Gerald Klix
Well, I am actually playing, right now. For http://www.carelix.org I 
implemented
a module that
  * adds a user to passwd and
  * authenticates that user given a certificate and some other info on 
removable media
  * it creates an encrypted loopback file, that is mounted as the 
user's home directory utilising Loop-AES and the certficate on the floppy
  * and it opens an openvpn connection using the same certificate
  * and, of course, unmounts the loopback file and terminates the vpn 
on logout.

Perhaps this is somewhat more than playing :)
cya,
Gerald
Diez B. Roggisch schrieb:
I've been using pyton-pam before. Works as expected - but pam frustrated
me a bit, and you gotta run as root for it to work - a thing I didn't want
to do.

Ok, I just found that you wanted to play from the other side of the fence -
never mind my answer. 
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: variables exist

2005-04-11 Thread Gerald Klix
try:
 myVariable
except NameError:
 print "Not bound"
else:
 print "Bound"
If you want to distinguish between the local an the global environment:
if globals().has_key( "myVariable" ):
 ...
versus
if locals().has_key( ".
HTH,
Gerald
fabian schrieb:
how testing if a variable exists in python as isset in php??
thanks
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Problem with import "from omniORB import CORBA, PortableServer"

2005-04-11 Thread gerald . maher
Hi,

I am trying to excuate the follwong code:

[
import sys
from omniORB import CORBA, PortableServer
]

I get the following error:
[
Traceback (most recent call last):
  File
"C:\omniORBpy-2.5-win32-python2.3\omniORBpy-2.5\examples\echoMyTest\example_echo_coloc.py",
line 9, in ?
from omniORB import CORBA, PortableServer
ImportError: No module named omniORB
]

Here is my Lib path:
['C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\examples\\echoMyTest',
'C:\\omniORB\\omniORB-4.0.3\\lib\\python',
'C:\\omniORB\\omniORB-4.0.3\\lib\\x86_win32',
'C:\\WINNT\\system32\\python24.zip', 'C:\\Python24',
'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
'C:\\Python24\\Lib\\site-packages\\pythonwin',
'C:\\Python24\\lib\\site-packages',
'C:\\Python24\\lib\\site-packages\\win32',
'C:\\Python24\\lib\\site-packages\\win32\\lib',
'C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\examples\\echoMyTest',
'C:\\omniORB\\omniORB-4.0.3\\lib\x86_win32',
'C:\\omniORB\\omniORB-4.0.3\\lib\x86_win32']

What am i doing wrong ?

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


Re: Problem with import "from omniORB import CORBA, PortableServer"

2005-04-11 Thread gerald . maher
Thank for the reply, I think your right  my problem is the what version
to us, I want to write some Python scripts calling Corba Objects what
do i need ?

omnipython-1.5.2-1
omniORBpy 2.5
omniORB 4.0.5
Python 2.4.1


Is this correct ? i want to excuat the example
C:\omniORBpy-2.5-win32-python2.3\omniORBpy-2.5\examples\echo\example_echo_coloc.py

Ciao
Gerald

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


Correct Versions to execute Corba calls using omniORBpy and omniORB.

2005-04-11 Thread gerald . maher
Hi, I just want to execute a script that used omniORB, omniORBpy
Python,

Here are the version that I Tried, is this correct ?

omniORBpy 2.5
omniORB 4.0.5
Python 2.4.1

The script is below:

#!/usr/bin/env python

import sys
from omniORB import CORBA, PortableServer

# Import the stubs and skeletons for the Example module
import Example, Example__POA

# Define an implementation of the Echo interface
class Echo_i (Example__POA.Echo):
def echoString(self, mesg):
print "echoString() called with message:", mesg
return mesg

# Initialise the ORB
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)

# Find the root POA
poa = orb.resolve_initial_references("RootPOA")

# Create an instance of Echo_i
ei = Echo_i()

# Create an object reference, and implicitly activate the object
eo = ei._this()

# Activate the POA
poaManager = poa._get_the_POAManager()
poaManager.activate()

# Call the object's echoString() operation

message = "Hello"
result  = eo.echoString(message)

print "I said '%s'. The object said '%s'." % (message,result)


What are the correct version i should use ?

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


Missing Module when calling 'Import' _omnipy

2005-04-11 Thread gerald . maher
I am trying to run an Corba example using Python and i get the follwing
error:


import _omnipy
ImportError: No module named _omnipy

Where can i find this Module ?

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


Re: Programming Language for Systems Administrator

2005-04-12 Thread Gerald Klix

Kanthi Kiran Narisetti schrieb:
Hi All,
Thank You for your suggestionsI request you all to eloborate the
Uses(In Practical) for systems administrator.Some of my questions
regarding the same follows.
1)Can i build web applications in Python ? If so how. I am planning to
build a web application for intranet use which deals with workflow of
Internal office communication.
Yes, you can.
There are far too many options, to be listed here:
Here are just 3 pointers:
http://www.zope.org/
http://www.cherrypy.org/
http://www.webwareforpython.org/
http://skunkweb.sourceforge.net/
2)Which is best opensource database to be used with Python ?
It's hard to say. It depends on our taste and the type of your application.
There is a python standard for a relation DB-API
and there are som object relational adapters, like:
http://python-dbo.sourceforge.net/
http://sqlobject.org/
http://skunkweb.sourceforge.net/PyDO/
http://dustman.net/andy/python/SQLDict
And, of course, there is ZODB the object oriented DB below Zope:
http://www.zope.org/Products/ZODB3.3

3)When i write a remote execution script in python is it required that
python should be installed in remote system.
No.
Using xmlrpc the server and may be written in any programming language.
xmlrpc is include din the standard libray.
Other remote execution systems use CORBA or do things upon their own:
http://pyro.sourceforge.net/
It is also possible to package python applications in an executable using:
http://starship.python.net/crew/theller/py2exe/
http://starship.python.net/crew/atuining/cx_Freeze/
or the Freeze utility delivered with the python standard distribution.
4)I heard about Perl/CGI and that CGI application done by python
too.Is CGI still valid when PHP has taken over the WebApplication
Development, Dominating.
No it's rather slow, but widley used. There is a mod_python for Apache
which is used be some of the web application frameworks mentioned above.
Sorry if these questions are out of this group , but answers to these ?
will help me a lot.
The are perfectly valid.
HTH,
Gerald
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: Missing Module when calling 'Import' _omnipy

2005-04-12 Thread gerald . maher
*_omnipy is Missing*

I have searched my C drive can not find any file called *_omnipy.so or
.dll where do I get it from ?
I found the fike _omnipy.pyd is that what you mean?


Here is my Lib Path:

>>>
['C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\examples\\echoMyTest',
'C:\\WINNT\\system32\\python23.zip', 'C:\\Python23',
'C:\\Python23\\DLLs', 'C:\\Python23\\lib',
'C:\\Python23\\lib\\plat-win', 'C:\\Python23\\lib\\lib-tk',
'C:\\Python23\\Lib\\site-packages\\pythonwin',
'C:\\Python23\\lib\\site-packages',
'C:\\Python23\\lib\\site-packages\\win32',
'C:\\Python23\\lib\\site-packages\\win32\\lib',
'C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\examples\\echoMyTest',
'C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\lib\\python',
'C:\\omniORBpy-2.5-win32-python2.3\\omniORBpy-2.5\\lib\x86_win32',
'C:\\omniORB\\omniORB-4.0.5\\omniORB-4.0.5\\lib\\python\\omniidl',
'C:\\omniORB\\omniORB-4.0.5\\omniORB-4.0.5\\lib\x86_win32']

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


Re: pythonic use of properties?

2005-04-14 Thread Gerald Klix
The python rationale is "We are all consenting adults.".
You shoukd change "tens" to "_tens" and "ones" to "_ones", in order to
syntacticly mark these attributes as internal.
If someone not consenting, wants to mess with your internal
representation, it's his fault.
HTH,
Gerald

Marcus Goldfish schrieb:
I'd like advice/opinions on when it is appropriate to do
attribute/property validation in python.  I'm coming from a C#/Java
background, where of course tons of "wasted" code is devoted to
property validation.  Here is a toy example illustrating my question:
#   Example: mixing instance attributes with properties.  Is it pythonic to
#   validate property data in setters?  Since tens and ones are never
#   validated, the class can be "broken" by setting these directly
class SillyDecimal(object):
   """A silly class to represent an integer from 0 - 99."""
   def __init__(self, arg=17):
   if isinstance(arg, tuple):
   self.tens = arg[0]
   self.ones = arg[1]
   else:
   self.number = arg
   def getNumber(self):
   return self.tens*10 + self.ones
   def setNumber(self, value):
   if value < 0 or value > 99:
   raise ArgumentException("Must in [0, 99]")
   self.tens = value // 10
   self.ones = value % 10
   number = property(getNumber, setNumber, None, "Complete number, [0-99]")
x = SillyDecimal()
x.number, x.tens, x.ones# returns (17, 7, 1)
Even though "tens", "ones" and "number" all appear as attributes, only
"number" has its input validated.  Since the class is designed to only
hold numbers 0 - 99, one can 'break' it by setting self.tens=11, for
example.  Should tens and ones be made into full-fledged properties
and validated?  Should number have no validation?  Is it more pythonic
to encapsulate tightly, or rely on "responsible use."
Marcus
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: eval function not working how i want it dag namn

2005-04-15 Thread Gerald Klix
How about using the vars builtin?
Michael Hoffman schrieb:
robcarlton wrote:
I've written this function to make a list of all of an objects
attributes and methods (not for any reason, I'm just learning)
def list_members(obj)
l = dir(obj)
return map(lambda x : eval('obj.'+x), l)

That works fine for me with Python 2.4.
This is the best way to do it:
def list_members(obj):
return [getattr(obj, name) for name in dir(obj)]
Although personally I would prefer to have this information in dict 
form, so i'd use:

return dict((name, getattr(obj, name)) for name in dir(obj))
For objects defined in CPython, you can use obj.__dict__, but this is 
somewhat hacky, and I'd avoid it.
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex over files

2005-04-25 Thread Gerald Klix
Map the file into RAM by using the mmap module.
The file's contents than is availabel as a seachable string.
HTH,
Gerald
Robin Becker schrieb:
Is there any way to get regexes to work on non-string/unicode objects. I 
would like to split large files by regex and it seems relatively hard to 
do so without having the whole file in memory. Even with buffers it 
seems hard to get regexes to indicate that they failed because of buffer 
termination and getting a partial match to be resumable seems out of the 
question.

What interface does re actually need for its src objects?
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
--
http://mail.python.org/mailman/listinfo/python-list


AW: Calling foreign functions from Python? ctypes?

2006-01-10 Thread Gerald Klix
I read the whol email thread carefully and could not find any sentence by
Guido, which states that he does not accept ctypes for the standard library.
He just declined to rewrite winreg. Did I miss something? 

Cya,
Gerald

-Ursprüngliche Nachricht-
Von: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Im Auftrag von
Thomas Heller
Gesendet: Dienstag, 10. Januar 2006 12:07
An: [email protected]
Betreff: Re: Calling foreign functions from Python? ctypes?

"Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes:

> Thomas Heller wrote:
>
>> As the author, I would be happy to see ctypes included with the 
>> standard Python build.
>
> I'm sure you know the magical incantation to get that to happen ...
>
> 1. Propose it on python-dev.
>
> 2. Commit to maintain it in the python core (or alternatively, propose 
> to use the ElementTree maintenance method).
>
> 3. Get it accepted by Guido.
>
> 4. Do it.
>
> If a library isn't put forward for inclusion by the *author*, it won't 
> be included - even if it's obviously best-of-breed. This was the 
> sticking point for ElementTree (until the author found out what was 
> going on and happily put it forward).

I have done 1.  I commit myself to 2.  I promise to do 3., if someone
convinces Guido to accept it (or whoever has the power to accept it).

This is my response to Martin v. Löwis, who wrote:

> I don't have such a plan. To my knowledge, ctypes has never been 
> contributed to Python, either, so its author apparently has no plan, 
> either.

It has been proposed to include it in core Python, but Guido didn't accept
it.  The thread starts here:

http://mail.python.org/pipermail/python-dev/2004-January/thread.html#41814

And Guido's last word was this (see the end of the message):

http://mail.python.org/pipermail/python-dev/2004-January/041856.html

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

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


AW: Python Embedding Questions

2006-08-01 Thread Gerald Klix



Hi Sean,
perhaps it may help if you try Python 2.4.3 instead of 
2.4.2. The release notes (http://www.python.org/download/releases/2.4.3/NEWS.txt)
mention a lot of fixed bugs, including a segfault, that 
is similar to yours. Also all defects, that the folks at coverty (http://scan.coverity.com/) 
discovered
with their static analysis tools, were 
fixed.
 
Some answers:
ad 1) I don't know, I did it twice and had similar 
problems as you.
ad 2) AFAIK some games embed python, perhaps someone 
with more experience can proviode more explanation.
 
HTH,
Gerald


Von: 
[EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED] Im Auftrag von 
Sean RyanGesendet: Dienstag, 1. August 2006 15:47An: 
[email protected]: Python Embedding 
Questions
Hi Guys,I have a couple of questions (and some concerns) 
about embedded python within my application.The application is a 
multi-threaded relatively complex c++ middleware Solaris 9 based 
application.  A decision was made some time ago to provide some scripting 
capabilities and python was selected as the scripting language.  As 
such,  an interpreter was embedded within the application and threading was 
managed using PyEval_SaveThread, PyEval_RestoreThread and friends.  
The application switches between python and c++ many, many times during 
the execution of one business request - typically the flow will bec++ 
-> python -> c++ -> python -> c++ -> python etc (roughly 60 - 40 
split in terms of coverage)Originally python2.2.1 was embedded within 
the application - we now have a business case (performance being a primary 
driver) to move to python2.4(.2).  We are having some stability 
issues (random core dumps), and in an attempt to track down the root cause I 
executed some stress tests under a purify environment and noticed the following 
:Python2.4.2 trips quite a few ABR (array bounds read) and FMR (free 
memory read) violations - python2.2.1  less so. 
purify notes the following as the innermost frames for a 
FMR. frame_dealloc  
[frameobject.c:76] PyEval_EvalCodeEx 
[ceval.c:2596] fast_function  
[ceval.c:3161]Is this a known issue with embedded python?I can 
trigger a similar crash with python 2.2.1 in a stress test 
environment.At this stage, I am getting very concerned about our 
implementation of embedded python and wondered :1.  Is our use of 
python as an embedded language commonplace?  2.  Does anyone have 
names of commerical applications that use python in this way3.  Are we 
switching between python and c++ code too frequently (as many as 40 times in the 
execution of 1 request)4.  Are there any consultants / contractors that 
we could engage to aid debugging our application / implementation.5.  
Are the warnings that purify is raising related to loss of stability in the 
application 6.  Is python the correct choice for an embedded 
application (I am aware of the GIL issue)We need to make a concise 
decision very quickly regarding our use of python, and as such would appreciate 
the community's honest opinion.Best 
regards,Sean
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: does anybody earn a living programming in python?

2006-09-26 Thread Gerald Klix
AOL^H^H^H, me too.
And it's paid better than C++ programming.

HTH,
Gerald

Gabriel Genellina schrieb:
> At Monday 25/9/2006 20:09, walterbyrd wrote:
> 
> I do.
> 
>> If so, I doubt there are many.
> 
> 
> That's why they get well paid :)
> (uhm, not really... :( )
> 
> 
> 
> Gabriel Genellina
> Softlab SRL
> 
> 
> 
>
> __
> Preguntá. Respondé. Descubrí.
> Todo lo que querías saber, y lo que ni imaginabas,
> está en Yahoo! Respuestas (Beta).
> ¡Probalo ya! http://www.yahoo.com.ar/respuestas
> 
> 

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


Re: What's going on here?

2006-11-22 Thread Gerald Klix
Perhaps this piece of code might explain the behaviour:


 >>> class C( object ):
... __slots__ = ()
...
 >>> o = C()
 >>> o.a = 1
Traceback (most recent call last):
   File "", line 1, in ?
AttributeError: 'C' object has no attribute 'a'


object behaves like having an implict __slots__ attribute.


HTH,
Gerald


Dale Strickland-Clark schrieb:
> Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
> [GCC 4.1.0 (SUSE Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>a = object()
>>>>a
> 
> 
> 
>>>>a.spam = 1
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
> AttributeError: 'object' object has no attribute 'spam'
> 
>>>>class b(object):
> 
> ...pass
> ...
> 
>>>>a = b()
>>>>a
> 
> <__main__.b object at 0xb7b4dcac>
> 
>>>>a.spam = 1
>>>>
> 
> 
> What is subclassing adding to the class here? Why can't I assign to
> attributes of an instance of object?

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


[no subject]

2005-05-15 Thread gerald hereford
 
 

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file in dir independently of operating system

2005-05-25 Thread Gerald Klix
Hi,
it`s

import os
f = open( os.path.join( dir , 'configuration.smo' ), 'r' )

HTH,
Gerald

Joerg Schuster schrieb:
> Hello,
> 
> 
> I want to open the file 'configuration.smo' that is in directory dir.
> Yet, I don't know on which os my program is being run. On Unix I would
> say:
> 
> f = open(dir + '/configuration.smo', 'r')
> 
> What is the os-independent version of this line?
> 
> (I have read the manual of the module os, but I didn't see how to do
> it.)
> 
> 
> Jörg Schuster
> 

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: Knowing the signature of a function

2005-06-08 Thread Gerald Klix
Use the inspect module like:

 >>> def tf( a, b, c, *arguments, **keywordArguments ):
...  print "tf"
...
 >>> import inspect
 >>> inspect.getargspec( tf )
(['a', 'b', 'c'], 'arguments', 'keywordArguments', None)
 >>>

Xavier Décoret schrieb:
> Hello,
> 
> I have the following code:
> 
> def foo(x,y):
>   pass
> 
> How can I query the function object foo to know the number of parameters 
> it expects.  I can find it is a function using callable(f), I can find 
> some information (listed by dir(foo)) such as the name of the 
> function,etc.. but nowhere I can find the number of arguments.
> 
> I would like to know wether the function expects one or zero arguments.

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: [OT ?] (Pythonic) detection word protected files

2005-06-13 Thread Gerald Klix
Perhaps you can use OpenOffice and it's python UNO Bindings?
I only know about their existence, but perhaps this will be a starting 
point: http://udk.openoffice.org/

HTH,
Gerald

Gilles Lenfant schrieb:
> Hi,
> 
> This is certainly off topic, but as my problem must have a pythonic answer.
> 
> I'm building an utility that makes a catalog of M$ word files in a giant 
> directory tree. The password protected files must be marked, and I 
> didn't find how to guess which files are password protected and which 
> ones are not.
> 
> I can't use the COM interface for this because the utility must run on a 
> Linux Samba server.
> 
> I didn't find anything satisfying in M$ related sites (like msdn) or 
> forums or google.
> 
> Any hint ?
> 
> Many thanks by advance.
> 
> --
> Gilles

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: strxfrm works with unicode string ?

2005-06-17 Thread Gerald Klix
How about:

import locale
s=u'\u00e9'
print s


locale.setlocale(locale.LC_ALL, '')


locale.strxfrm( s.encode( "latin-1" ) )

---
HTH,
Gerald

[EMAIL PROTECTED] schrieb:
> I am trying to use strxfm with unicode strings, but it does not work.
> This is what I did:
> 
> 
>>>>import locale
>>>>s=u'\u00e9'
>>>>print s
> 
> é
> 
>>>>locale.setlocale(locale.LC_ALL, '')
> 
> 'French_Switzerland.1252'
> 
>>>>locale.strxfrm(s)
> 
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> locale.strxfrm(s)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
> position 0: ordinal not in range(128)
> 
> 
> Someone sees what I did wrong ?
> 

-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: strxfrm works with unicode string ?

2005-06-17 Thread Gerald Klix
Sali Nicolas :)),
please see below for my answers.

[EMAIL PROTECTED] schrieb:
> Gruëzi, Gerald ;-)
> 
> Well, ok, but I don't understand why I should first convert a pure
> unicode string into a byte string.
> The encoding ( here, latin-1) seems an arbitrary choice.
Well "latin-1" is only encoding, about which I know that it works on
my xterm and which I can type without spelling errors :)
> 
> Your solution works, but is it a workaround or the real way to use
> strxfrm ?
> It seems a little artificial to me, but perhaps I haven't understood
> something ...
In Python 2.3.4 I had some strange encounters with the locale module,
In the end I considered it broken, at least when it came to currency 
formating.
> 
> Does this mean that you cannot pass a unicode string to strxfrm ?
This works here for my home-grown python 2.4 on Jurrasic Debian Woody:

import locale
s=u'\u00e9'
print s

print locale.setlocale(locale.LC_ALL, '')
print repr( locale.strxfrm( s.encode( "latin-1" ) ) )
print repr( locale.strxfrm( s.encode( "utf-8" ) ) )

The output is rather strange:

é
de_DE
"\x10\x01\x05\x01\x02\x01'@/locale"
"\x0c\x01\x0c\x01\x04\x01'@/locale"

Another (not so) weird thing happens when I unset LANG.

[EMAIL PROTECTED]:~ > unset LANG
[EMAIL PROTECTED]:~ > python2.4 ttt.py
Traceback (most recent call last):
   File "ttt.py", line 3, in ?
 print s
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in 
position 0: ordinal not in range(128)

Acually it's more weird, that printing works with LANG=de_DE.

Back to your question. A quick glance at the C-sources of the
_localemodule.c reveals:

 if (!PyArg_ParseTuple(args, "s:strxfrm", &s))

So yes, strxfrm does not accept unicode!

I am inclined to consider this a bug.
A least it is not consistent with strcoll.
Strcoll accepts either 2 strings or 2 unicode strings,
at least when HAVE_WCSCOLL was defined when python
was compiled on your plattform.

BTW: Which platform do you use?

HTH,
Gerald

PS: If you have access to irc, you can also ask at 
irc://irc.freenode.net#python.de.



-- 
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

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


Re: Is there a way to hook into module destruction?

2007-06-16 Thread Gerald Kaszuba
On Jun 17, 6:16 am, Neal Becker <[EMAIL PROTECTED]> wrote:
> Code at global scope in a module is run at module construction (init).  Is
> it possible to hook into module destruction (unloading)?

Try the __del__ method.

See http://docs.python.org/ref/customization.html for the docs.

--
Gerald Kaszuba
http://geraldkaszuba.com

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


Re: Is there a way to hook into module destruction?

2007-06-16 Thread Gerald Kaszuba
> I doubt python calls __del__ when unloading module... and plus, I
> don't really think python does module unloading though. del module

Ah! Apologies... I mis-read the question.

Unloading or at least destroying the reference to a loaded module, you
could go for "del module_name"?

--
Gerald Kaszuba
http://geraldkaszuba.com

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


Re: Setting thread priorities

2007-05-13 Thread Gerald Kaszuba
Hi John

On May 13, 4:46 pm, John Nagle <[EMAIL PROTECTED]> wrote:
>There's no way to set thread priorities within Python, is there?

Not exactly. You can however use the ctypes module to access the o/s
methods of pthread_setschedparam() for UNIX and SetThreadPriority()
for Windows.

I'm not sure why this hasn't been implemented in Python.

Gerald
http://geraldkaszuba.com/

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


Re: Wanted: a python24 package for Python 2.3

2007-03-20 Thread Gerald Klix
Hi,
You can't import subproces from future, only syntactic and semantic 
changes that will become standard feature in future python version can 
be activated that way.

You can copy the subprocess module from python 2.4 somewhere where it 
will be found from python 2.3. At least subporcess is importable after that:

--- snip ---
[EMAIL PROTECTED]:~/ttt> cp -av /usr/local/lib/python2.4/subprocess.py .
»/usr/local/lib/python2.4/subprocess.py« -> »./subprocess.py«
[EMAIL PROTECTED]:~/ttt> python2.3
Python 2.3.3 (#1, Jun 29 2004, 14:43:40)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import subprocess
 >>>
--- snip ---

HTH,
Gerald

[EMAIL PROTECTED] schrieb:
> On Mar 20, 10:33 am, Jonathan Fine <[EMAIL PROTECTED]> wrote:
> 
>>Hello
>>
>>My problem is that I want a Python 2.4 module on
>>a server that is running Python 2.3.  I definitely
>>want to use the 2.4 module, and I don't want to
>>require the server to move to Python 2.4.
>>
>>More exactly, I am using subprocess, which is
>>new in Python 2.4.  What I am writing is something
>>like
>>===
>>from subprocess import Popen
>>===
>>
>>This will fail in Python 2.3, in which case I
>>would like to write something like
>>===
>>try:
>> from subprocess import Popen
>>else ImportError:
>> from somewhere_else import Popen
>>===
>>
>>Put this way, it is clear (to me) that somewhere_else
>>should be python24.
>>
>>In other words, I'm asking for a python24 package that
>>contains all (or most) of the modules that are new to
>>Python 2.4.
>>
>>I've looked around a bit, and it seems that this
>>formulation of the solution is new.  I wonder if
>>anyone else has encountered this problem, or has
>>comments on my solution.
>>
>>--
>>Jonathan
> 
> 
> You might be able to use the "from future import SomeModule" syntax to
> accomplish this, but I am not sure. Other than that, I would just
> recommend using the os.popen calls that are native to 2.3
> 
> Mike
> 

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


pycallgraph 0.1.0

2007-02-09 Thread Gerald Kaszuba
Hi

I've just released the first version of Python Call Graph. I haven't
tested it too much so don't expect it not to crash :)

An example of its output is:
http://pycallgraph.slowchop.com/files/examples/mongballs-client.png

It's easy to use... you just need Graphviz installed and in your path
and a few lines of code changes.

More details at http://pycallgraph.slowchop.com/

If you have any problems, don't hesitate to ask here or email me directly.

Enjoy!

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


Re: pycallgraph 0.1.0

2007-02-09 Thread Gerald Kaszuba
On 2/10/07, Stef Mientki <[EMAIL PROTECTED]> wrote:
> ... but isn't "__main__." non-information ?

Good point -- I'll consider removing it in the next version.

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


pycallgraph 0.2.0

2007-02-09 Thread Gerald Kaszuba
Hi

I just released a new version of pycallgraph. It has many improvements
over 0.1.0. Here is an example of a call graph made in pycallgraph
0.2.0:
http://pycallgraph.slowchop.com/pycallgraph/wiki/RegExpExample

There are more examples on the web site:
http://pycallgraph.slowchop.com/

The changes are:
* Windows access denied bug fixed
* graph uses different colours depending on the number of calls made
to the function
* graph settings and look are very customisable
* exclude and include filters
* stop_trace() is not required anymore, it is called automatically by
make_graph()
* will throw an exception if there is an error from dot/neato
* removed obvious use of __main__ as the module name
* added some examples

There is no documentation yet but if you browse the source you'll be
able to figure out a lot.

Enjoy!

Gerald

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


Re: pycallgraph 0.1.0

2007-02-10 Thread Gerald Kaszuba
>http://pycallgraph.slowchop.com/files/examples/mongballs-client.png
>
> indicate you are subject to the slashdot effect?

Steve,

No /. effect :)

I rearranged some files around because of the new version of
pycallgraph. The new preview image is:
http://pycallgraph.slowchop.com/pycallgraph/wiki/RegExpExample

Gerald

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


Re: pycallgraph 0.2.0

2007-02-10 Thread Gerald Kaszuba
On Feb 10, 11:14 pm, Stef Mientki <[EMAIL PROTECTED]>
wrote:
> My first test was terrible: a file of 800kB was created,
> Trying to view it, resulted in the following:
> - Paint Shop Pro told me it was not a valid PNG file,
> - Mozilla crashed after 5 minutes,
> - GIMP gave me a preview after half an hour ;-)

Impressive! :)

> So a few suggestions:
> - is there a way to limit the number of nodes ?

Not yet, I will have a maximum stack-depth option in the next version.

> - "pycallgraph." nodes are "non-information"
> (maybe you can find another way to place your "signature" ;-)

This should be fixed in the next version.

> - how do I exclude modules/files/objects (sorry I'm still a newbie in Python)

Say you want to only display your own modules called 'foo' and 'bar':
import pycallgraph
pycallgraph.settings['include_module'] = ['foo', bar]
pycallgraph.start_trace()
pycallgraph.make_graph('blah.png')

>From the source there are these options for inclusion and exclusion:

'exclude_module': [],
'exclude_class': [],
'exclude_func': [],
'exclude_specific': ['stop_trace', 'make_graph'],
'include_module': [],
'include_class': [],
'include_func': [],
'include_specific': [],

"specific" means the whole name of a node, e.g.
"foo.MyBarClass.__init__"

Gerald

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


Re: pycallgraph 0.2.0

2007-02-10 Thread Gerald Kaszuba
On Feb 11, 12:28 am, [EMAIL PROTECTED] wrote:
> Looks nice.  Before you get too far... Is there any chance that you can
> support ASCII output mode (obviously not laid out in two dimensions, just
> using indentation) so the GraphViz requirement can become optional?
> GraphViz has so many dependencies of its own (and many of them are
> GTK-related) that the chance of me satisfying them is very small.  (I'm on
> Solaris and Mac, not Linux, so I don't have the benefit of a Linux distro to
> solve those particular headaches for me.)

It's easy enough. I'll put it on my todo list.

Gerald

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


ANN: Python Call Graph 0.3.0

2007-02-14 Thread Gerald Kaszuba
Hi,

I just released pycallgraph 0.3.0. There are many examples on the web
site and linked from the web site including a 16188 x 4187 sized call
graph!
http://pycallgraph.slowchop.com/

The changes are:
* Renamed make_graph to make_dot_graph to allow different output types
in the future
* Callback filter patch by Alec Thomas -- allows more flexibility when
filtering calls, including wildcards
* Added filter example
* Added docstrings

The documentation is slowly coming along with epydoc generated
documentation from the docstrings. More will come soon.

Also on the horizon are different types of formats like ASCII and HTML
output.

Have fun.

Gerald

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


ANN: pyraknet 0.1.4

2007-02-20 Thread Gerald Kaszuba
I just released a new version of pyraknet - a UDP game network
library.

http://pyraknet.slowchop.com/

The changes are:
* Changed license to LGPL
* Mac OS X binaries for Python 2.4 (thanks to Simon Howe for testing)
* Added Peer.is_active(...)
* Added Peer.get_max_connections(...)
* setup.py will tell you what's wrong when you don't have Pyrex
installed

Gerald

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


ANN: Python Google Chart (pygooglechart) 0.1.2

2007-12-15 Thread Gerald Kaszuba
Python Google Chart is a complete wrapper to the Google Chart API.

http://pygooglechart.slowchop.com/

 * Added more examples
 * Fixed pie labels encoding
 * Fixed MANIFEST.in to allow examples and COPYING in the source
distribution
 * Added more metadata in setup.py

--
Gerald Kaszuba
http://geraldkaszuba.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Function Overloading and Python

2008-02-25 Thread Gerald Klix
Stefan Behnel schrieb:
> Allen Peloquin wrote:
>> On Feb 24, 11:44 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote:
>>> Allen Peloquin wrote:
>>>> class B
>>>> {
>>>> fun(A x, A y, A z)...
>>>> fun(A1 x, A y, A z)...
>>>> }
>>>> class B1
>>>> {
>>>> fun(A1 x, A y, A z)...
>>>> }
>>>> Such that any previous behavior is inherited, but behaves
>>>> polymorphically because of the single function name.
>>> Try something like this:
>>>
>>> class B(object):
>>> def fun(x,y,z):
>>> if isinstance(x, A1):
>>> return self._fun(x,y,z)
>>> # ...
>>>
>>> def _fun(x,y,z):
>>> # ...
>>>
>>> class B1(B):
>>> def _fun(x,y,z):
>>> # ...
>>>
>>> Stefan
>> The problem is that I want to reuse the code of the parent classes
>> through inheritance, otherwise this would work fine.
> 
> Ok, I didn't see you were going to add new subtypes, that makes it more tricky
> to dispatch in the superclass.
> 
> An alternative would be a more generic dispatcher pattern then:
> 
>class B(object):
>_func_implementations = {}
>_dispatch = _func_implementations.get
> 
>def func(self, x,y,z):
>self._dispatch(type(x), self._func)(self,x,y,z)
> 
>def _func(self, x,y,z):
># ...
> 
> class B1(B):
>def _func(self, x,y,z):
># ...
> 
> B._func_implementations[B1] = B1._func
> 
> Or, you could dispatch based on type /names/:
> 
>class B(object):
>def func(self, x,y,z):
>func = getattr(self, "_%s_func" % type(x).__name__, self._func)
>    func(x,y,z)
> 
>def _A_func(self, x,y,z):
># ...
> 
> class B1(B):
>def _A1_func(self, x,y,z):
># ...
> 
> Stefan

The BDFL came across that problem, too. You will find his thoughts here:
<http://www.artima.com/weblogs/viewpost.jsp?thread=101605>
The reference implementation for his solution is here:
<http://svn.python.org/view/sandbox/trunk/overload/>

HTH,
Gerald
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: detect current timezone set by kde

2008-03-18 Thread Gerald Klix
I suggest to change /etc/timezone by invoking sudo tzselect.

HTH,
Gerald

Pradnyesh Sawant schrieb:
> Hello,
> can someone please tell me how can I programatically detect the timezone
> information that has been set through kde?
> 
> basically, I have a small pyqt4 app which shows the current time. however
> it shows me my system time (dunno where that is stored; basically it shows
> me time in IST). however, I'm right now in hk and would like the app to
> show me time in my current timezone (hong kong). Any guidelines how may I
> go about doing this?
> 
> thanks a lot in advance :-)
> 

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


Re: Lazy List Generator Problem

2009-01-16 Thread Gerald Britton
For those interested in the Sieve of Eratosthenes, have a look at:

http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf

The examples in the paper are in Haskell, but I have been
corresponding with the author who provided this Python version:

def sieve():
innersieve = sieve()
prevsquare = 1
table  = {}
i = 2
while True:
if (table.has_key(i)):
prime = table[i]
del(table[i])
next = i+prime
while next in table:
 next = next + prime
table[next] = prime
else:
yield i
if i > prevsquare:
j = innersieve.next()
prevsquare = j * j
table[prevsquare] = j
i = i + 1

Only of 65056 bytes (less than 1/16 MB) of heap is used when
calculating the millionth prime.  It is also very fast but can be even
further optimized using a wheel as described in the paper.  FWIW I was
so intrigued I went off to learn Haskell just so I could follow the
paper properly.
--
http://mail.python.org/mailman/listinfo/python-list


is None vs. == None

2009-01-23 Thread Gerald Britton
Hi -- Some time ago I ran across a comment recommending using  is
None instead of  == None (also  is not None, etc.)  My own
testing indicates that the former beats the latter by about 30% on
average.  Not a log for a single instruction but it can add up in
large projects.

I'm looking for a (semi)-official statement on this, but couldn't find
one with normal googling.  Can someone please send a link?
--
http://mail.python.org/mailman/listinfo/python-list


Python Google Chart 0.2.1 released

2008-08-25 Thread Gerald Kaszuba
pygooglechart 0.2.1 has been released.

http://pygooglechart.slowchop.com/

Here are the changes:

 * Added support for QR Code chart (#8)
 * Added legend positioning (chdlp) (Steve Brandt)
 * Added line styles (chm=D) (Steve Brandt)
 * Added "colours within series" option to chart (chco=xxx|xxx) (Steve
Brandt)
 * Added QR codes and more line examples
 * Axis labels are now casted to strings automatically
 * Bug fixed where pie charts stopped working due to automatic scaling
 * Bug fixed where the module would download twice (#7) (Evan Lezar)
 * Bug fixed when automatic scaling is on and None values are in a
data set (#5) (Alec Thomas)
 * Bug fixed with auto-scaling, where the minimum y range was always
0. (#6) (Rohit Jenveja)
 * Bug fixed, replaced "1" with "0" in add_horizontal_range and
add_vertical_range (incorrect syntax for Google) (Steve Brandt)
 * Better clipping checks

I've also updated the home page with more examples.

Gerald

--
Gerald Kaszuba
http://geraldkaszuba.com/
--
http://mail.python.org/mailman/listinfo/python-list


Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Yesterday I stumbled across some old code in a project I was working
on.  It does something like this:

mystring = '\n'.join( [ line for line in lines if  ] )

where "lines" is a simple list of strings.  I realized that the code
had been written before you could put a list comprehension as an
argument to join().  I figured that I could drop the '[' and ']' and
leave the rest as a list comprehension since that works with current
Python (and the project's base is 2.5).  So I rewrote the original
statement like this:

mystring = '\n'.join( line for line in lines if  )

It works as expected.  Then I got curious as to how it performs.  I
was surprised to learn that the rewritten expression runs more than
twice as _slow_.  e.g.:

>>> l
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

>>> Timer("' '.join([x for x in l])", 'l = map(str,range(10))').timeit()
2.9967339038848877

>>> Timer("' '.join(x for x in l)", 'l = map(str,range(10))').timeit()
7.2045478820800781

Notice that I dropped the condition testing that was in my original
code.  I just wanted to see the effect of two different expressions.

I thought that maybe there was some lower bound on the number of the
items in the list or list comprehension beyond which the comprehension
would prove more efficient.  There doesn't appear to be one.  I scaled
the length of the input list up to 1 million items and got more or
less the same relative performance.

Now I'm really curious and I'd like to know:

1. Can anyone else confirm this observation?

2. Why should the "pure" list comprehension be slower than the same
comprehension enclosed in '[...]' ?

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


Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Thanks!  Good explanation.

On Tue, Jan 19, 2010 at 10:57 AM, Alf P. Steinbach  wrote:
> * Gerald Britton:
>>
>> Yesterday I stumbled across some old code in a project I was working
>> on.  It does something like this:
>>
>> mystring = '\n'.join( [ line for line in lines if > depending on line> ] )
>>
>> where "lines" is a simple list of strings.  I realized that the code
>> had been written before you could put a list comprehension as an
>> argument to join().  I figured that I could drop the '[' and ']' and
>> leave the rest as a list comprehension since that works with current
>> Python (and the project's base is 2.5).  So I rewrote the original
>> statement like this:
>>
>> mystring = '\n'.join( line for line in lines if > depending on line> )
>>
>> It works as expected.  Then I got curious as to how it performs.  I
>> was surprised to learn that the rewritten expression runs more than
>> twice as _slow_.  e.g.:
>>
>>>>> l
>>
>> ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>
>>>>> Timer("' '.join([x for x in l])", 'l = map(str,range(10))').timeit()
>>
>> 2.9967339038848877
>>
>>>>> Timer("' '.join(x for x in l)", 'l = map(str,range(10))').timeit()
>>
>> 7.2045478820800781
>>
>> Notice that I dropped the condition testing that was in my original
>> code.  I just wanted to see the effect of two different expressions.
>>
>> I thought that maybe there was some lower bound on the number of the
>> items in the list or list comprehension beyond which the comprehension
>> would prove more efficient.  There doesn't appear to be one.  I scaled
>> the length of the input list up to 1 million items and got more or
>> less the same relative performance.
>>
>> Now I'm really curious and I'd like to know:
>>
>> 1. Can anyone else confirm this observation?
>
>>>> Timer("' '.join([x for x in l])", 'l = map(str,range(10))').timeit()
> 5.8625191190500345
>>>> Timer("' '.join(x for x in l)", 'l = map(str,range(10))').timeit()
> 12.093135300715574
>>>> _
>
>
>> 2. Why should the "pure" list comprehension be slower than the same
>> comprehension enclosed in '[...]' ?
>
> Regarding (2) the unparenthesized expression in join is *not* a list
> comprehension but a generator expression.
>
> And as such it involves join calling next() on the generator object
> repeatedly, with each next() call involving a light-weight context shift.
>
> In addition the docs mumble something about "lazy" evaluation, and that may
> also contribute to the overhead.
>
> I think that in contrast, the interpreter can evaluate a list comprehension,
> [x for x in blah], directly without any context shifting, just by
> transforming it to equivalent code and putting the target expressions
> innermost there.
>
> And so the main factor causing a slowdown for a list comprehension would, I
> think, be paging and such if the list it produced was Really Huge, while for
> the generator there's no memory issue but rather much calling & context
> shifting.
>
>
> Cheers & hth.,
>
> - Alf
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Interestingly, I scaled it up to a million list items with more or
less the same results.  It's helpful to see that your results are
different.  That leads me to suspect that mine are somehow related to
my own environment.

Still I think the key is the overhead in calling next() for each item
in the generator expression.  That in itself probably accounts for the
differences since function calls are somewhat expensive IIRC.

On Tue, Jan 19, 2010 at 11:18 AM, Stephen Hansen  wrote:
> On Tue, Jan 19, 2010 at 7:30 AM, Gerald Britton 
> wrote:
> [snip]
>>
>> mystring = '\n'.join( line for line in lines if > depending on line> )
>
> Note, this is not a list comprehension, but a generator comprehension.
> A list comprehension is used to build, in one sweep, a list and return it.
> A generator comprehension is used to build an generator that can be iterated
> over to produce a sequence of items.
> I think you're seeing not performance of the expression, but the function
> call overhead which generators include. A generator requires one to call its
> next method to get each item: a list comprehension is just syntactical sugar
> for a for loop.
> As to which is faster, I think it depends. Your test-case is using *really*
> small ranges-- just ten items. In this case, just doing a simple loop to
> build a list and then pass it through join is probably faster. If you're
> using a much larger list though, the characteristics of the problem may
> change, where the lazy evaluation of a generator expression may be more
> desirable.
> A list comprehension includes a waste of memory, too: you have to build up a
> complete list before you return it, and if you have a lot of lines? That can
> be a problem.
> As you can see, the performance characteristics between the two narrow
> considerably if you compare a larger sample:
>  >>> Timer("' '.join([str(x) for x in l])", 'l = xrange(100)').timeit()
> 50.092024087905884
>>>> Timer("' '.join(str(x) for x in l)", 'l = xrange(100)').timeit()
> 54.591049909591675
> --S



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


Re: Create list/dict from string

2010-01-19 Thread Gerald Britton
Can't you just use the dict() built-in function like this:

dict({"action_name": "action_1", "val": "asdf"})

Of course if the list is not properly formed, this will fail.  But I
guess you have thought of that already.

On Tue, Jan 19, 2010 at 11:33 AM, SoxFan44  wrote:
> I was wondering if there was a way to create a list (which in this
> case would contain several dicts) based on a string passed in by the
> user.  Security is not an issue.  Basically I want to be able to have
> the user pass in using optparse:
> --actions=[{"action_name": "action_1", "val": "asdf", "val2":
> "asdf"},
>               {"action_name": "action_2", "val": "asdf", "val2":
> "asdf"},
>               {"action_name": "action_1", "val": "asdf", "val2":
> "asdf"}]
>
> And have this create a list/dict.  I'm aware of pickle, but it won't
> work as far as I can tell.
>
> Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
[snip]

>
> Yes, list building from a generator expression *is* expensive. And
> join has to do it, because it has to iterate twice over the iterable
> passed in: once for calculating the memory needed for the joined
> string, and once more to actually do the join (this is implementation
> dependent, of course). If the iterable is a list already, the list
> building is not needed.

if join has to iterate twice over the iterable, how does this work?

$ python3.1
Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l = map(str, (x for x in range(10) if int(x)%2))
>>> '.'.join(l)
'1.3.5.7.9'
>>>

If join had to iterate twice over l, it would be consumed on the first
pass.  If it works as you say then join would have to copy the
iterable on the first pass, effectively turning it into a list.
Though I didn't read through it, I would suppose that join could use a
dynamic-table approach to hold the result, starting with some
guesstimate then expanding the result buffer if and when needed.

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



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


Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
That's surprising. I wouldn't implement it that way at all.  I'd use a
dynamically-expanding buffer as I suggested.  That way you get a
single pass and don't have to calculate anything before you begin.  In
the best case, you'd use half the memory (the result just fits in the
buffer after its last expansion and no saved tuple).  In the worst
case, the memory use is about the same (you just expanded the buffer
using a 2x expansion rule then you hit the last item).

Still I suppose the author thought of that approach and rejected it
for reasons I can't yet see.

On Tue, Jan 19, 2010 at 4:01 PM, Arnaud Delobelle
 wrote:
> Gerald Britton  writes:
>
>> [snip]
>>
>>>
>>> Yes, list building from a generator expression *is* expensive. And
>>> join has to do it, because it has to iterate twice over the iterable
>>> passed in: once for calculating the memory needed for the joined
>>> string, and once more to actually do the join (this is implementation
>>> dependent, of course). If the iterable is a list already, the list
>>> building is not needed.
>>
>> if join has to iterate twice over the iterable, how does this work?
>>
>> $ python3.1
>> Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22)
>> [GCC 4.4.1] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> l = map(str, (x for x in range(10) if int(x)%2))
>>>>> '.'.join(l)
>> '1.3.5.7.9'
>>>>>
>>
>> If join had to iterate twice over l, it would be consumed on the first
>> pass.  If it works as you say then join would have to copy the
>> iterable on the first pass, effectively turning it into a list.
>> Though I didn't read through it, I would suppose that join could use a
>> dynamic-table approach to hold the result, starting with some
>> guesstimate then expanding the result buffer if and when needed.
>
> Looking at the source (py3k):
>
> PyObject *
> PyUnicode_Join(PyObject *separator, PyObject *seq)
> {
>    [skip declarations]
>
>    fseq = PySequence_Fast(seq, "");
>    if (fseq == NULL) {
>        return NULL;
>    }
>
>    [code that works out the length of the joined string then allocates
>     memory, then fills it]
> }
>
> Where PySequence_Fast(seq, "") returns seq if seq is already a tuple or
> a list and otherwise returns a new tuple built from the elements of seq.
>
> So no, it doesn't guess the size of the joined string and yes, it
> iterates twice over the "sequence" (I would have thought it should be
> called an iterable) by copying it into a tuple.
>
> --
> Arnaud
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: MySQLdb

2009-11-22 Thread Gerald Walker
Kill Joy wrote:
> Hi all.
> 
> I have a mod_python script with two query:
> 
>   cursor = db.cursor()
> 
>   sql = 'SELECT * FROM users where username=\'' + username +'\''
>   cursor.execute(sql)
>   result = cursor.fetchall()
>   num =  int(cursor.rowcount)
> 
>   if num == 0 :
>   sql2 = 'insert into users values (null, \'' + username + '\', 
> \'' +
> password +'\', \'no\',\'fdfdf\')'
>   cursor.execute(sql2)
db.commit()
>   warning = "Registration ok"
>
>   else :
>   warning = "EXIST!"
> 
> The first query is executed... but not the second. It doesn't insert.
> Why?
> I'm a newbie... sorry.
> 
> Many thanks.
> 

I added db.commit() after cursor.execute(sql2).

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


Re: Waiting for receiving data

2009-11-23 Thread Gerald Walker
Anjanesh Lekshminarayanan wrote:
> fp = urllib.urlopen(url)
> data = fp.read()
> 
> Retrieving XML data via an XML service API.
> Very often network gets stuck in between. No errors / exceptions.
> 
> CTRL+C
> 
>   File "get-xml.py", line 32, in 
> fp = urllib.urlopen(url)
>   File "/usr/lib/python2.6/urllib.py", line 87, in urlopen
> return opener.open(url)
>   File "/usr/lib/python2.6/urllib.py", line 206, in open
> return getattr(self, name)(url)
>   File "/usr/lib/python2.6/urllib.py", line 348, in open_http
> errcode, errmsg, headers = h.getreply()
>   File "/usr/lib/python2.6/httplib.py", line 1048, in getreply
> response = self._conn.getresponse()
>   File "/usr/lib/python2.6/httplib.py", line 974, in getresponse
> response.begin()
>   File "/usr/lib/python2.6/httplib.py", line 391, in begin
> version, status, reason = self._read_status()
>   File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
> line = self.fp.readline()
>   File "/usr/lib/python2.6/socket.py", line 397, in readline
> data = recv(1)
> KeyboardInterrupt
> 
> Is there I can do to try something else if its taking too long to
> retrieve from the network ? Like kill previous attempt and retry ?
> 
> Thanks
> Anjanesh Lekshmnarayanan



import socket
from urllib2 import urlopen

# A one-hundredths of a second (0.01) timeout before socket throws
# an exception to demonstrate catching the timeout.
# Obviously, this you will set this greater than 0.01 in real life.
socket.setdefaulttimeout(0.01)

# example xml feed
xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml";

try:
  data = urlopen(xml_source)
except urllib2.URLError, e:
  print 'URLError = ' + str(e.reason)



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


Re: Waiting for receiving data

2009-11-23 Thread Gerald Walker

> 
> import socket
> from urllib2 import urlopen
> 
> # A one-hundredths of a second (0.01) timeout before socket throws
> # an exception to demonstrate catching the timeout.
> # Obviously, this you will set this greater than 0.01 in real life.
> socket.setdefaulttimeout(0.01)
> 
> # example xml feed
> xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml";
> 
> try:
>   data = urlopen(xml_source)
> except urllib2.URLError, e:
>   print 'URLError = ' + str(e.reason)

Also, if you are using multiple threads to retrieve the xml source(s)
and any thread blocks due to network problems, the thread can go way by
itself after the default timeout expires.


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


Re: Waiting for receiving data

2009-11-23 Thread Gerald Walker

> 
> Also, if you are using multiple threads to retrieve the xml source(s)
> and any thread blocks due to network problems, the thread can go way by
> itself after the default timeout expires.
> 
> 

Typo, edited for clarity:
That is: "..the thread can go *away* by itself after the default timeout
expires." You don't need to explicitly kill it.


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


Re: Waiting for receiving data

2009-11-23 Thread Gerald Walker

> 
> import socket
> from urllib2 import urlopen
> 
> # A one-hundredths of a second (0.01) timeout before socket throws
> # an exception to demonstrate catching the timeout.
> # Obviously, this you will set this greater than 0.01 in real life.
> socket.setdefaulttimeout(0.01)
> 
> # example xml feed
> xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml";
> 
> try:
>   data = urlopen(xml_source)
> except urllib2.URLError, e:
>   print 'URLError = ' + str(e.reason)

Oops, the above doesn't run. This version works:

import socket
import urllib2

# A one-hundredths of a second (0.01) timeout before socket throws
# an exception to demonstrate catching the timeout.
# Obviously, this you will set this greater than 0.01 in real life.
socket.setdefaulttimeout(0.01)

# example xml feed
xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml";

try:
  data = urllib2.urlopen(xml_source)
except urllib2.URLError, e:
  print 'URLError = ' + str(e.reason)

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


Iterating over a function call

2010-02-01 Thread Gerald Britton
Hi -- I have many sections of code like this:

for value in value_iterator:
 value_function(value)

I noticed that this does two things I don't like:

1. looks up "value_function" and "value" for each iteration, but
"value_function" doesn't change.
2. side effect of (maybe) leaking the iterator variable "value" into
the code following the loop (if the iterator is not empty).

I can take care of 2 by explicitly deleting the variable at the end:

   del value

but I'd probably forget to do that sometimes.  I then realized that,
in the 2.x series, I can accomplish the same thing with:

map(value_function, value_iterator)

and avoid both problems BUT map() returns a list which is never used.
Not a big deal for small iterables, I guess, but it seems messy.  Upon
conversion to 3.x I have to explicitly list-ify it:

list(map(value_function, value_iterator))

which works but again the list returned is never used (extra work) and
has to be gc'd I suppose (extra memory).

It's easy to make a little function to take care of this (2.x):

from itertools import imap
def apply(function, iterable):
for item in imap(function, iterable):
pass

then later:

   apply(value_function, value_iterator)

or something similar thing in 3.x, but that just adds an additional
function def that I have to include whenever I want to do something
like this.

So.I'm wondering if there is any interest in an apply() built-in
function that would work like map() does in 2.x (calls the function
with each value returned by the iterator) but return nothing.  Maybe
"apply" isn't the best name; it's just the first one that occurred to
me.

Or is this just silly and should I forget about it?

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


Re: Iterating over a function call

2010-02-01 Thread Gerald Britton
[snip[

> You have itertools.consume which is close to what you want:
>
>    consume(imap(func, iterable)) # 2.x
>
>    consume(map(func, iterable)) # 3.x
>
> HTH

It does! Though in my case this is simpler:

deque(imap(func, iterable), 0)

since the recipe for consume just calls deque anyway when you want to
eat up the rest of the iterable.  It also solves the iterator-variable
leakage problem and is only a wee bit slower than a conventional
for-loop.

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



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


Re: Adding methods from one class to another, dynamically

2010-02-01 Thread Gerald Britton
Or you could just do a mixin:

tee.__class__.__bases__ = (test,) +  tee.__class__.__bases__


On Mon, Feb 1, 2010 at 3:25 PM, Chris Rebert  wrote:
> On Mon, Feb 1, 2010 at 12:06 PM, Oltmans  wrote:
>> Hello Python gurus,
>>
>> I'm quite new when it comes to Python so I will appreciate any help.
>> Here is what I'm trying to do. I've two classes like below
>>
>> import new
>> import unittest
>>
>> class test(unittest.TestCase):
>>    def test_first(self):
>>        print 'first test'
>>    def test_second(self):
>>        print 'second test'
>>    def test_third(self):
>>        print 'third test'
>>
>> class tee(unittest.TestCase):
>>    pass
>>
>> and I want to attach all test methods of 'test'(i.e. test_first(),
>> test_second() and test_third()) class to 'tee' class. So I'm trying to
>> do something like
>>
>> if __name__=="__main__":
>>    for name,func in inspect.getmembers(test,inspect.ismethod):
>>        if name.find('test_')!= -1:
>>            tee.name = new.instancemethod(func,None,tee)
>
> This ends up repeatedly assigning to the attribute "name" of tee; if
> you check dir(tee), you'll see the string "name" as an entry. It does
> *not* assign to the attribute named by the string in the variable
> `name`.
> You want setattr(): http://docs.python.org/library/functions.html#setattr
> Assuming the rest of your code chunk is correct:
>
> setattr(tee, name, new.instancemethod(func,None,tee))
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Iterating over a function call

2010-02-02 Thread Gerald Britton
[snip]
>> 2. side effect of (maybe) leaking the iterator variable "value" into
>> the code following the loop (if the iterator is not empty).
>
> So? it is sometime useful.

Except that you can't guarantee that it will be set since the for loop
will not execute if the iterable is empty.

>>
>> I can take care of 2 by explicitly deleting the variable at the end:
>>
>>    del value
>>
>> but I'd probably forget to do that sometimes.
>
> So? If having 'value' bound breaks your subsequent code, I consider it
> buggy.

Quite so.  I just like to eliminate the possibility up front.  If
'value' is never bound, the the bug will show up sooner.


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


Re: lists as an efficient implementation of large two-dimensional arrays(!)

2010-02-02 Thread Gerald Britton
Did you try it with an array object using the array module?

On Tue, Feb 2, 2010 at 3:14 PM, Mitchell L Model  wrote:
> An instructive lesson in YAGNI ("you aren't going to need it"), premature
> optimization, and not making assumptions about Python data structure
> implementations.
>
> I need a 1000 x 1000 two-dimensional array of objects. (Since they are
> instances of application classes it appears that the array module is
> useless; likewise, since I am using Python 3.1, so among other things, I
> can't use numpy or its relatives.) The usage pattern is that the array is
> first completely filled with objects. Later, objects are sometimes accessed
> individually by row and column and often the entire array is iterated over.
>
> Worried (unnecessarily, as it turns out) by the prospect of 1,000,000
> element list I started by constructing a dictionary with the keys 1 through
> 1000, each of which had as its value another dictionary with the keys 1
> through 1000. Actual values were the values of the second level dictionary.
>
> Using numbers to fill the array to minimize the effect of creating my more
> complex objects, and running Python 3.1.1 on an 8-core Mac Pro with 8Gb
> memory, I tried the following
>
> #create and fill the array:
> t1 = time.time()
> d2 = {}
> for j in range(1000):
>    d2[j] = dict()
>    for k in range(1000):
>        d2[j][k] = k
> print( round(time.time() - t1, 2))
>
> 0.41
>
> # access each element of the array:
> t1 = time.time()
> for j in range(1000):
>    for k in range(1000):
>        elt = d2[j][k]
> print( round(time.time() - t1, 2))
>
> 0.55
>
>  My program was too slow, so I started investigating whether I could improve
> on the two-level dictionary, which got used a lot. To get another baseline I
> tried a pure 1,000,000-element list, expecting the times to be be
> horrendous, but look!
>
> # fill a list using append
> t1 = time.time()
> lst = []
> for n in range(100):
>    lst.append(n)
> print( round(time.time() - t1, 2))
>
> 0.26
>
> # access every element of a list
> t1 = time.time()
> for n in range(100):
>    elt = lst[n]
> print( round(time.time() - t1, 2))
>
> 0.25
>
> What a shock! I could save half the execution time and all my clever work
> and awkward double-layer dictionary expressions by just using a list!
>
> Even better, look what happens using a comprehension to create the list
> instead of a loop with list.append:
>
> t1 = time.time()
> lst = [n for n in range(100)]
> print( round(time.time() - t1, 2))
>
> 0.11
>
> Half again to create the list.
>
> Iterating over the whole list is easier and faster than iterating over the
> double-level dictionary, in particular because it doesn't involve a
> two-level loop. But what about individual access given a row and a column?
>
> t1 = time.time()
> for j in range(1000):
>    for k in range(1000):
>        elt = lst[j * 1000 + k]
> print( round(time.time() - t1, 2))
>
> 0.45
>
> This is the same as for the dictionary.
>
> I tried a two-level list and a few other things but still haven't found
> anything that works better than a single long list -- just like 2-D arrays
> are coded in old-style languages, with indices computed as offsets from the
> beginning of the linear sequence of all the values. What's amazing is that
> creating and accessing 1,000,000-element list in Python is so efficient. The
> usual moral obtains: start simple, analyze problems (functional or
> performance) as they arise, decide whether they are worth the cost of
> change, then change in very limited ways. And of course abstract and
> modularize so that, in my case, for example, none of the program's code
> would be affected by the change from a two-level dictionary representation
> to using a single long list.
>
> I realize there are many directions an analysis like this can follow, and
> many factors affecting it, including patterns of use. I just wanted to
> demonstrate the basics for a situation that I just encountered. In
> particular, if the array was sparse, rather than completely full, the
> two-level dictionary implementation would be the natural representation.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Trouble with os.system

2010-02-03 Thread Gerald Britton
Can you post your code?

On Wed, Feb 3, 2010 at 12:47 PM, Cpa  wrote:
> Hi there,
>
> I'm having some trouble with os.system on Fedora 12.
> I have a bunch of .tex files in tmp/ and I want to compile them.
> In my shell, the following commands work perfectly : 'for file in tmp/
> *.tex; do pdflatex "$file"; done'.
>
> But if I use the same command using os.system(), it will compile
> correctly every file except the last one, for which it raises an error
> (I get a prompt, as if I did a syntax error in tex document).
>
> I suspected some kind of escaping issue, but it won't even work with
> files such as : foo.txt, bar.txt.
>
> Any idea ?
> Thanks,
> Cpa
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Trouble with os.system

2010-02-03 Thread Gerald Britton
It kinda worked for me but I had to change it a little:

os.system('for file in /tmp/*.tex; do pdflatex "$file"; done')

Maybe you're picking up other files in /tmp that  are not .tex files?

On Wed, Feb 3, 2010 at 12:58 PM, Cpa  wrote:
> Sure.
>
> import sys,re,os
> files2create = sys.argv[1:]
> os.system('mkdir tmp')
>
> # Some code to create the .tex
>
> # Compile tex files
> os.system('for file in tmp/*; do pdflatex "$file"; done')
>
> Pretty simple, alas.
>
> --
> Cpa
>
>
> On 3 fév, 18:54, Gerald Britton  wrote:
>> Can you post your code?
>>
>>
>>
>> On Wed, Feb 3, 2010 at 12:47 PM, Cpa  wrote:
>> > Hi there,
>>
>> > I'm having some trouble with os.system on Fedora 12.
>> > I have a bunch of .tex files in tmp/ and I want to compile them.
>> > In my shell, the following commands work perfectly : 'for file in tmp/
>> > *.tex; do pdflatex "$file"; done'.
>>
>> > But if I use the same command using os.system(), it will compile
>> > correctly every file except the last one, for which it raises an error
>> > (I get a prompt, as if I did a syntax error in tex document).
>>
>> > I suspected some kind of escaping issue, but it won't even work with
>> > files such as : foo.txt, bar.txt.
>>
>> > Any idea ?
>> > Thanks,
>> > Cpa
>> > --
>> >http://mail.python.org/mailman/listinfo/python-list
>>
>> --
>> Gerald Britton
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: exec within function

2010-02-03 Thread Gerald Britton
I get no error:

>>> def a():
...  exec('b=1')
...  print(b)
...
>>> a()
1
>>>


On Wed, Feb 3, 2010 at 2:59 PM, Terry Reedy  wrote:
> On 2/3/2010 3:30 AM, Simon zack wrote:
>>
>> hi,
>> I'm not sure how I can use exec within a function correctly
>> here is the code i'm using:
>>
>> def a():
>>     exec('b=1')
>>     print(b)
>>
>> a()
>>
>> this will raise an error, but I would like to see it outputting 1
>
> Always **copy and paste** **complete error tracebacks** when asking a
> question like this. (The only exception would be if it is v e r y long, as
> with hitting the recursion depth limit of 1000.)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: which

2010-02-05 Thread Gerald Britton
[snip]

> Last August [1], I offered this alternative:
>
>  self.cmd = (cmd.replace(r'${ADDR}',ip)
>              if isinstance(cmd, str) else
>              cmd)
>
> But it didn't get much love in this forum!

I'd probably go for that one as well though I might consider removing
the outer parentheses.

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


Re: list.extend([]) Question

2010-02-05 Thread Gerald Britton
I think it's because when you do ['a'].extend([]) or whatever, the
result is whatever the method "extend" returns.  But "extend" has no
return value, hence you will see None if you do this interactively.

On Fri, Feb 5, 2010 at 10:55 AM, Aahz  wrote:
> In article <[email protected]>,
> Dan Brown   wrote:
>>
>>Why does extending a list with the empty list result in None?  It
>>seems very counterintuitive to me, at least --- I expected ['a'].extend
>>([]) to result in ['a'], not None.
>
> http://www.python.org/doc/faq/general/#why-doesn-t-list-sort-return-the-sorted-list
> --
> Aahz ([email protected])           <*>         http://www.pythoncraft.com/
>
> import antigravity
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: which

2010-02-05 Thread Gerald Britton
>
> Did you mean to take this off-list?

Nope -- just hit the wrong key

 Also, I'm contractually obligated to
> admonish you not to "top post".

Contract?

>
> At any rate, I proposed the 3-line format specifically because it separates
> the data values from the if-then-else machinery, making it easier (for me)
> to read. But there was considerable resistance to spending so much vertical
> space in the source code.

Weird!  It's three lines and the original was four lines was it not>?

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


Re: Editor for Python

2010-02-05 Thread Gerald Britton
2010/2/5 Laszlo Nagy :
>
>   Hi All,
>
> I know that this question was put up on this list a thousand times. I know
> that most of the editors are listed here:
> http://wiki.python.org/moin/PythonEditors
>
> I already tried most of them. But still, I need something that is not listed
> there. Requirements:
>
> starts and works fast
> has code folding, displays identation guides
> auto completion
> class browser
> source code browser (e.g. a list of definitions, when you click it jumps to
> the definition
> integration with pychecker or pylint
> UTF-8 files
> free, or not-so-expensive
> works on linux and windows
>
> The one I'm using now is Geany. It does everything, except class browser and
> pychecker/pylint. Now maybe, I can install (or write??) a geany extension
> that would allow me to use pychecker. But I could not find information on
> that. There where others I tried (PyPE, DrPython, KomodoEdit, Editra etc.)
> but all of them failed for some reason. Can you please suggest another
> editor that I could try? Or send me a link that tells how to write or
> install pychecked plugin for Geany.
>
> Thanks,
>
>    Laszlo
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

I use Geany too.  It lets me see the classes in my module, though not
within a package (Is that what you mean)?  You'd probably have to
write a plugin to support pychecker or pylint though I believe that
its not too difficult.  Who knows?  Perhaps someone already has such a
plugin that you can use.

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


Re: Exception class documentation

2010-02-05 Thread Gerald Britton
On Fri, Feb 5, 2010 at 12:55 PM, Charles Yeomans  wrote:
> I am so far unable to find the information I want about the Exception class.
>  Information like the signature of __init__ seems to be unavailable.  Any
> suggestions where I might find such information?
>
>
> Charles Yeomans
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Though not documented, some silly tests indicate that it will accept
pretty much anything:

>>> Exception(1,2,4,54)
Exception(1, 2, 4, 54)
>>> Exception(*range(10))
Exception(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> Exception(*range(50))
Exception(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49)
>>> Exception('a','b','c','d','e')
Exception('a', 'b', 'c', 'd', 'e')
>>> Exception(Exception(1))
Exception(Exception(1,),)

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


Re: method names nounVerb or verbNoun

2010-02-05 Thread Gerald Britton
On Fri, Feb 5, 2010 at 2:53 PM, Wanderer  wrote:
> Which is the more accepted way to compose method names nounVerb or
> verbNoun?
>
> For example voltageGet or getVoltage? getVoltage sounds more normal,
> but voltageGet is more like voltage.Get. I seem to mix them and I
> should probably pick one way and stick with it.
>
> Thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I'd say noun_verb (note the underscore in accordance with the style
guide in PEP 8):

http://www.python.org/dev/peps/pep-0008/

Function Names

  Function names should be lowercase, with words separated by underscores
  as necessary to improve readability.

  mixedCase is allowed only in contexts where that's already the
  prevailing style (e.g. threading.py), to retain backwards compatibility.

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


Re: Last M digits of expression A^N

2010-02-05 Thread Gerald Britton
On Fri, Feb 5, 2010 at 3:14 PM, mukesh tiwari
 wrote:
> Hello everyone. I am kind of new to python so pardon me if i sound
> stupid.
> I have to find out the last M digits of expression.One thing i can do
> is (A**N)%M but my  A and N are too large (10^100) and M is less than
> 10^5. The other approach   was  repeated squaring and taking mod of
> expression. Is there any other way to do this in python more faster
> than log N.
>
> def power(A,N,M):
>    ret=1
>    while(N):
>        if(N%2!=0):ret=(ret*A)%M
>        A=(A*A)%M
>        N=N//2
>    return ret
> --
> http://mail.python.org/mailman/listinfo/python-list
>

http://docs.python.org/3.1/library/decimal.html#decimal.Context.power

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


Re: Exception class documentation

2010-02-06 Thread Gerald Britton
If you browse the Python source tree, you should be able to find it.

http://svn.python.org/view/python/trunk/Objects/exceptions.c?revision=77045&view=markup

On Fri, Feb 5, 2010 at 7:27 PM, Charles Yeomans  wrote:
>
> On Feb 5, 2010, at 2:13 PM, Gerald Britton wrote:
>
>> On Fri, Feb 5, 2010 at 12:55 PM, Charles Yeomans 
>> wrote:
>>>
>>> I am so far unable to find the information I want about the Exception
>>> class.
>>>  Information like the signature of __init__ seems to be unavailable.  Any
>>> suggestions where I might find such information?
>>>
>>
>> Though not documented, some silly tests indicate that it will accept
>> pretty much anything:
>>
>>>>> Exception(1,2,4,54)
>>
>> Exception(1, 2, 4, 54)
>>>>>
>>>>> Exception(*range(10))
>>
>> Exception(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>>>>
>>>>> Exception(*range(50))
>>
>> Exception(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
>> 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
>> 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49)
>>>>>
>>>>> Exception('a','b','c','d','e')
>>
>> Exception('a', 'b', 'c', 'd', 'e')
>>>>>
>>>>> Exception(Exception(1))
>>
>> Exception(Exception(1,),)
>
> I had also tried such tests.  If you pass a single argument msg, it is
> assigned to the message property, and the args property is set to (msg,). If
> you pass more than one argument, the tuple of arguments is assigned to the
> args property, and nothing is assigned to the message property.  I was
> hoping to at least find source code that provides a definitive answer.
>
>
> Charles Yeomans
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Checking the coding style

2010-02-07 Thread Gerald Britton
pylint and pychecker are good options

On Sun, Feb 7, 2010 at 1:36 PM, Pablo Recio Quijano
 wrote:
> Hi!
> I'm finishing a project writen in Python, and I realize about the document
> PEP8 - Style Guide for Python Code [1].
> Is there any app or script that checks if my project pass that style guide?
> I also worked with Drupal, and I know there is some modules and scripts that
> checks its coding standars [2] and it's very usefull to clean the code.
> Thanks in advance!
> [1] http://www.python.org/dev/peps/pep-0008/
> [2] http://drupal.org/coding-standards
>
> --
> Pablo Recio Quijano
>
> Estudiante de Ingeniería Informática (UCA)
> Alumno colaborador del Departamento de Lenguajes y Sistemas Informáticos
> Participante del IV Concurso Universitario de Software Libre
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



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


Re: "if {negative}" vs. "if {positive}" style

2010-02-10 Thread Gerald Britton
On Wed, Feb 10, 2010 at 3:56 PM, Ethan Furman  wrote:
> Tim Chase wrote:
>>
>> Any thoughts on how others make the choice?
>>
>> -tkc
>
> If one branch is only a few lines, it comes first.  As often as not, that
> tiny branch is checking for errors, and the "else" branch doesn't need to be
> indented.
>
> def func(arg1):
>    if arg1 is 'stupid':
>        raise ValueError("that's a stupid argument!")
>    do_something
>    do_something_else
>    etc, etc
>
>
> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Something similar in a for-loop:

for x in y:
   if not should_process(x): continue

  # process x

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


  1   2   >