Tuesday, April 6, 2010

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

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

comp.lang.python@googlegroups.com

Today's topics:

* Pickle problem while loading a class instance. - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/76e95abf1feb4de4?hl=en
* imports again - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/54bf96fe87be1cf0?hl=en
* pass object or use self.object? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6f51302327b58aac?hl=en
* Performance of list vs. set equality operations - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/818d143c7e9550bc?hl=en
* Python script error when using print - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/b2b4e343b96c65e2?hl=en
* Recommend Commercial graphing library - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/b8609090d6ba19e9?hl=en
* Translation docstrings with gettext - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/5e5a245d71348c9a?hl=en
* python as pen and paper substitute - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/c880ee16ca2d5e99?hl=en
* Simplify Python - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/e1fcfc6ebcb0506f?hl=en
* Impersonating a Different Logon - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/c07dd48ba4835250?hl=en
* plotting in python 3 - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/99d7eb33b8e4cd9e?hl=en
* per-method jit compiler - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4265701a4b0f611a?hl=en
* passing command line arguments to executable - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4f15edabd97830c8?hl=en

==============================================================================
TOPIC: Pickle problem while loading a class instance.
http://groups.google.com/group/comp.lang.python/t/76e95abf1feb4de4?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Apr 6 2010 10:23 am
From: gerardob

Hello, I am new to python and i have a problem using the pickle load
function.
I have an object m of the class MarkovModel and i want to copy it to a file
and load it onto another class:

l=[1,2,3]
m = markov_model.MarkovModel()
m.load_observations(l)
file = open("prueba.txt", 'w')
pickle.dump(m,file,2)
file.close()

#m2 = markov_model.MarkovModel()

file = open("prueba.txt", 'rb')
m2 = pickle.load(file) (THIS IS LINE 36)

The error below appears. In the case i remove the comment to initialize m2,
the same thing happens. Any ideas on how to fix this?

Thanks.

Traceback (most recent call last):
File "C:\Users\gberbeglia\Documents\python\scripting\mycodes\main.py", line
36, in <module>
m2 = pickle.load(file)
File "C:\Python26\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python26\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python26\lib\pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "C:\Python26\lib\pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named markov_model
--
View this message in context: http://old.nabble.com/Pickle-problem-while-loading-a-class-instance.-tp28154964p28154964.html
Sent from the Python - python-list mailing list archive at Nabble.com.

== 2 of 3 ==
Date: Tues, Apr 6 2010 11:02 am
From: Peter Otten <__peter__@web.de>


gerardob wrote:

> Hello, I am new to python and i have a problem using the pickle load
> function.
> I have an object m of the class MarkovModel and i want to copy it to a
> file and load it onto another class:
>
> l=[1,2,3]
> m = markov_model.MarkovModel()
> m.load_observations(l)
> file = open("prueba.txt", 'w')

Remember to open the file in binary mode.

> pickle.dump(m,file,2)
> file.close()
>
> #m2 = markov_model.MarkovModel()
>
> file = open("prueba.txt", 'rb')
> m2 = pickle.load(file) (THIS IS LINE 36)
>
> The error below appears. In the case i remove the comment to initialize
> m2, the same thing happens. Any ideas on how to fix this?

Add the directory containing the markov_model module to your PYTHONPATH
environment variable or move the module into a directory where Python is
already looking (C:/Python26/lib/site-packages or the per-user equivalent).

See also http://docs.python.org/using/windows.html#finding-modules

> Traceback (most recent call last):
> File "C:\Users\gberbeglia\Documents\python\scripting\mycodes\main.py",
> line 36, in <module>
> m2 = pickle.load(file)
> File "C:\Python26\lib\pickle.py", line 1370, in load
> return Unpickler(file).load()
> File "C:\Python26\lib\pickle.py", line 858, in load
> dispatch[key](self)
> File "C:\Python26\lib\pickle.py", line 1090, in load_global
> klass = self.find_class(module, name)
> File "C:\Python26\lib\pickle.py", line 1124, in find_class
> __import__(module)
> ImportError: No module named markov_model

Peter


== 3 of 3 ==
Date: Tues, Apr 6 2010 12:02 pm
From: Lie Ryan


On 04/07/10 03:23, gerardob wrote:

> The error below appears. In the case i remove the comment to initialize m2,
> the same thing happens. Any ideas on how to fix this?
>

When unpickling a user-defined class, you unpickling module must have
access to the original class definition. This means if you do this:

# model.py
class MyClass(object):
pass

# saver.py

import pickle
import model

m = model.MyClass()
pickle.dump(m, open('...', 'w'))

Then the loader.py must be able to import model. If you do not
explicitly import model, pickle will automatically try to `import model`
from the standard module search path.

# loader.py

# the import must succeed, or pickle cannot find Foo's definition
#
# import model

pickle.load(open('...'))

==============================================================================
TOPIC: imports again
http://groups.google.com/group/comp.lang.python/t/54bf96fe87be1cf0?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Apr 6 2010 10:25 am
From: Alex Hall


Sorry this is a forward (long story involving a braille notetaker's
bad copy/paste and GMail's annoying mobile site). Basically, I am
getting errors when I run the project at
http://www.gateway2somewhere.com/sw.zip
and I do not understand why. The bad line was working until I added
more import statements. I know my setup is not good, but I cannot seem
to get my own packages to work (putting files in a folder along with
__init__.py). If anyone can explain what I did and how I can fix it, I
would very much appreciate it!! Requires Windows, tested on Vista/xp/7
with python2.6.

---------- Forwarded message ----------
From: Alex Hall <mehgcap@gmail.com>
Date: Tue, 6 Apr 2010 13:06:24 -0400
Subject: imports again
To: python-users@python.org

Hello all,
My project is stalled because of an import problem. I know my imports
are a tangled mess (1 imports 2 3 and 4, 2 imports 1 and 3, 3 imports
2 and 4, and so on). I would very much appreciate it if someone could
look at the setup and recommend a fix. Right now, an exception is
being thrown when I import a file, even though this import was working
until I added some different import statements. The entire thing,
intended for Windows xp+ and Python2.6, is at
http://www.gateway2somewhere.com/sw.zip
Thanks in advance for any help or suggestions!

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@gmail.com; http://www.facebook.com/mehgcap

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@gmail.com; http://www.facebook.com/mehgcap

==============================================================================
TOPIC: pass object or use self.object?
http://groups.google.com/group/comp.lang.python/t/6f51302327b58aac?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Apr 6 2010 11:42 am
From: Lie Ryan


On 04/06/10 23:52, Tim Arnold wrote:
> Hi,
> I have a few classes that manipulate documents. One is really a
> process that I use a class for just to bundle a bunch of functions
> together (and to keep my call signatures the same for each of my
> manipulator classes).
>
> So my question is whether it's bad practice to set things up so each
> method operates on self.document or should I pass document around from
> one function to the next?
> pseudo code:
>
> class ManipulatorA(object):
> def process(self, document):
> document = self.do_one_thing(document)
> document = self.do_another_thing(document)
> # bunch of similar lines
> return document
>
> or
>
> class ManipulatorA(object):
> def process(self, document):
> self.document = document
> self.do_one_thing() # operates on self.document
> self.do_another_thing()
> # bunch of similar lines
> return self.document

Since in function in python is a first-class object, you can instead do
something like:

def process(document):
# note: document should encapsulate its own logic
document.do_one_thing()
document.do_another_thing()

And when you need some complex logic, you can easily elevate your
function to a class:

class Appender(object):
def __init__(self, text):
self.text = text
def __call__(self, document):
mtext = self.manipulate(document, text)
document.append(mtext)

and I think for your purpose, the mixin pattern could cleanly separate
manipulation and document while still obeying object-oriented pattern
that document is self-sufficient:

# language with only single-inheritance can only dream to do this
class Appendable(object):
def append(self, text):
self.text += text
class Savable(object):
def save(self, fileobj):
fileobj.write(self.text)
class Openable(object):
def open(self, fileobj):
self.text = fileobj.read()
class Document(Appendable, Savable, Openable):
def __init__(self):
self.text = ''

==============================================================================
TOPIC: Performance of list vs. set equality operations
http://groups.google.com/group/comp.lang.python/t/818d143c7e9550bc?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Apr 6 2010 11:11 am
From: Gustavo Narea


Hello!

Could you please confirm whether my understanding of equality
operations in sets and lists is correct? This is how I think things
work, partially based on experimentation and the online documentation
for Python:

When you compare two lists, *every* element of one of the lists is
compared against the element at the same position in the other list;
that comparison is done by the __eq__() method (or the equivalent for
builtin types). This is interrupted when a result is False.

When you compare two sets, there's a loop over all the elements of the
first set, where the hash for that element is looked up in the second
set:
- If this hash matches the hash for one or more elements in the
second set, the element in the first set is compared (with __eq__ or
equivalent) against the elements in the second set which have the same
hash. When a result is True, nothing else is done on that element and
the loop takes the next element in the first set; when all the results
are False, the loop ends and the two sets are not equivalent.
- If the hash doesn't match that of an element in the second set,
then the loop ends and the two sets are not equivalent.

So this means that:
1.- When you have two collections which have the same elements, the
equality operation will *always* be faster with lists.
2.- When you have two collections with different elements, the
equality operation *may* be faster with sets.

For example, if you have two collections of 1,000 elements each and
998 of them are equivalent, comparing both collections as sets will be
slower than doing it with lists. But if you have two collections of
1,000 elements each and 998 of them are not equivalent, then comparing
both collections as lists will be slower than doing it with sets.

The performance of equality operations on sets is directly
proportional to the amount of different elements in both sets, while
the performance of equality operations on lists is simply proportional
to the cardinality of the collection.

In other words: The more different elements two collections have, the
faster it is to compare them as sets. And as a consequence, the more
equivalent elements two collections have, the faster it is to compare
them as lists.

Is this correct?

This is why so many people advocate the use of sets instead of lists/
tuples in similar situations, right?

Cheers,

- Gustavo.


== 2 of 2 ==
Date: Tues, Apr 6 2010 11:28 am
From: Chris Colbert


the proof is in the pudding:

In [1]: a = range(10000)

In [2]: s = set(a)

In [3]: s2 = set(a)

In [5]: b = range(10000)

In [6]: a == b
Out[6]: True

In [7]: s == s2
Out[7]: True

In [8]: %timeit a == b
1000 loops, best of 3: 204 us per loop

In [9]: %timeit s == s2
10000 loops, best of 3: 124 us per loop


On Tue, Apr 6, 2010 at 2:11 PM, Gustavo Narea <me@gustavonarea.net> wrote:
> Hello!
>
> Could you please confirm whether my understanding of equality
> operations in sets and lists is correct? This is how I think things
> work, partially based on experimentation and the online documentation
> for Python:
>
> When you compare two lists, *every* element of one of the lists is
> compared against the element at the same position in the other list;
> that comparison is done by the __eq__() method (or the equivalent for
> builtin types). This is interrupted when a result is False.
>
> When you compare two sets, there's a loop over all the elements of the
> first set, where the hash for that element is looked up in the second
> set:
>  - If this hash matches the hash for one or more elements in the
> second set, the element in the first set is compared (with __eq__ or
> equivalent) against the elements in the second set which have the same
> hash. When a result is True, nothing else is done on that element and
> the loop takes the next element in the first set; when all the results
> are False, the loop ends and the two sets are not equivalent.
>  - If the hash doesn't match that of an element in the second set,
> then the loop ends and the two sets are not equivalent.
>
> So this means that:
>  1.- When you have two collections which have the same elements, the
> equality operation will *always* be faster with lists.
>  2.- When you have two collections with different elements, the
> equality operation *may* be faster with sets.
>
> For example, if you have two collections of 1,000 elements each and
> 998 of them are equivalent, comparing both collections as sets will be
> slower than doing it with lists. But if you have two collections of
> 1,000 elements each and 998 of them are not equivalent, then comparing
> both collections as lists will be slower than doing it with sets.
>
> The performance of equality operations on sets is directly
> proportional to the amount of different elements in both sets, while
> the performance of equality operations on lists is simply proportional
> to the cardinality of the collection.
>
> In other words: The more different elements two collections have, the
> faster it is to compare them as sets. And as a consequence, the more
> equivalent elements two collections have, the faster it is to compare
> them as lists.
>
> Is this correct?
>
> This is why so many people advocate the use of sets instead of lists/
> tuples in similar situations, right?
>
> Cheers,
>
>  - Gustavo.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

==============================================================================
TOPIC: Python script error when using print
http://groups.google.com/group/comp.lang.python/t/b2b4e343b96c65e2?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Apr 6 2010 11:14 am
From: "Albert W. Hopkins"


On Tue, 2010-04-06 at 08:38 -0700, Robbie wrote:
> Hi all,
>
> So, I'm trying to use Python with an apache2 server to create some web
> pages. The web server is configured and seems to work correctly, but
> only with a certain type of script.
>
> For instance, this script works fine
>
> #!/usr/bin/env python
> def index():
> s = "Hello World"
> return s
>
> But, a script like this, does not.
> #!/usr/bin/env python
> print "hello world"
>
> When I try to use the script with print, the server returns a broken
> link error. There is nothing in the apache error log to help me
> understand why it won't work.

Is this a CGI script? You need to return headers (like Content-type):

e.g. (untested)

print "Content-type: text/plain"
print
print "hello world"

See also
http://docs.python.org/library/cgi.html

-a


== 2 of 2 ==
Date: Tues, Apr 6 2010 1:38 pm
From: Pierre Quentel


On 6 avr, 20:14, "Albert W. Hopkins" <mar...@letterboxes.org> wrote:
> On Tue, 2010-04-06 at 08:38 -0700, Robbie wrote:
> > Hi all,
>
> > So, I'm trying to use Python with an apache2 server to create some web
> > pages.  The web server is configured and seems to work correctly, but
> > only with a certain type of script.
>
> > For instance, this script works fine
>
> > #!/usr/bin/env python
> > def index():
> >     s = "Hello World"
> >     return s
>
> > But, a script like this, does not.
> > #!/usr/bin/env python
> > print "hello world"
>
> > When I try to use the script with print, the server returns a broken
> > link error.  There is nothing in the apache error log to help me
> > understand why it won't work.
>
> Is this a CGI script?  You need to return headers (like Content-type):
>
> e.g. (untested)
>
> print "Content-type: text/plain"
> print
> print "hello world"
>
> See alsohttp://docs.python.org/library/cgi.html
>
> -a

Hi,

Are you trying to use some Python web framework behind Apache ? (as
suggested by the fact that your first script "runs", i.e. probably
prints "Hello World"). In this case the "not found" error in the
second script would mean that the framework requires a function in the
script

- Pierre

==============================================================================
TOPIC: Recommend Commercial graphing library
http://groups.google.com/group/comp.lang.python/t/b8609090d6ba19e9?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Apr 6 2010 11:20 am
From: Robert Kern


On 2010-04-06 11:44 AM, superpollo wrote:
> Grant Edwards ha scritto:
>> On 2010-04-06, Grant Edwards <invalid@invalid.invalid> wrote:
>>> On 2010-04-06, Jean-Michel Pichavant <jeanmichel@sequans.com> wrote:
>>>> Pablo Recio Quijano wrote:
>>>>> Why must be commercial, when there is open and free alternatives?
>>>>> Like GNU Plot.
>>>> Gnuplot is ugly. I'm using it because I don't care if it's ugly but
>>>> it clearly lacks of look & feel for presentations, as requested by
>>>> the OP.
>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> In other words, Gnuplot presents information in a clear, obfuscated
>> That should be: unobsuscated
>>
>> stupid spell-checker...
>> Seriously, most of the graphs I've seen in "presentations" would make
>> Ed Tufte spin in his grave.
>
> didn't know he died.

He hasn't.

--
Robert Kern

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

== 2 of 3 ==
Date: Tues, Apr 6 2010 11:22 am
From: Grant Edwards


On 2010-04-06, superpollo <utente@esempio.net> wrote:
> Grant Edwards ha scritto:
>> On 2010-04-06, Grant Edwards <invalid@invalid.invalid> wrote:
>>> On 2010-04-06, Jean-Michel Pichavant <jeanmichel@sequans.com> wrote:
>>>> Pablo Recio Quijano wrote:
>>>>> Why must be commercial, when there is open and free alternatives? Like
>>>>> GNU Plot.
>>>> Gnuplot is ugly. I'm using it because I don't care if it's ugly but it
>>>> clearly lacks of look & feel for presentations, as requested by the OP.
>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>
>>> In other words, Gnuplot presents information in a clear, obfuscated
>>
>> That should be: unobsuscated
>>
>> stupid spell-checker...
>>
>> Seriously, most of the graphs I've seen in "presentations" would make
>> Ed Tufte spin in his grave.
>
> didn't know he died.

If'd seen some of those graphs he would have.

--
Grant Edwards grant.b.edwards Yow! I'm having an
at emotional outburst!!
gmail.com


== 3 of 3 ==
Date: Tues, Apr 6 2010 11:31 am
From: Grant Edwards


On 2010-04-06, Duncan Booth <duncan.booth@invalid.invalid> wrote:
> Grant Edwards <invalid@invalid.invalid> wrote:
>
>> Seriously, most of the graphs I've seen in "presentations" would make
>> Ed Tufte spin in his grave.
>
> http://flowingdata.com/2010/03/20/powerpoint-and-dying-kittens/

:)

Years ago I was walking past a marketing guy's cube, and on his cube
wall he had Minard's famous "Napolean's March" graph that Tufte so
admired. I asked him if he had read Tufte's book "The Visual Display
of Quantitative Information" (in which the graph appears). He replied
that no he hadn't read Tufte's book -- he'd gotten that graph from a
power-point-driven lecture in some MBA class or other.

--
Grant Edwards grant.b.edwards Yow! I'm rated PG-34!!
at
gmail.com

==============================================================================
TOPIC: Translation docstrings with gettext
http://groups.google.com/group/comp.lang.python/t/5e5a245d71348c9a?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Apr 6 2010 12:32 pm
From: Lie Ryan


On 04/06/10 19:52, sapient wrote:
> Lie Ryan, thank you for your answer!
>> Why would you want to translate docstring? Docstring is meant for
>> developers not users.
> I have mentioned that I want to provide API for existing image-
> processing applicaion in Python.
> In my case "developers" are "users".
> Python provides great possibilities for documenting objects, I want to
> use it for things such context help about API modules, classes,
> methods, and want to do it in several languages.
>
>> Maintaining a translated docstring is going to be
>> a maintenance hell and will either hampers your application's agility or
>> you will be left with obsolete docstrings in various languages you don't
>> know.
> You are right, there are problems here, but there are advantages too
> (we are talking about API): developers can write documentation in
> thier "cracked" English (like my), but professional translator can
> correct it after with Poedit.

Fair enough.

>> Anyway, my job is to answer questions, not question the economic
>> feasibility of your decision, so try this:
>>
>> #!python
>> __doc__ = _("""testmodule docstring""")
>>
>> class TestClass:
>> __doc__ = _("""testmodule.TestClass docstring""")
>>
>> def testClassMethod(self):
>> __doc__ = _("""testmodule.TestClass.testClassMethod docstring""")
>> print _("Call TestClass.testClassMethod()")
> Yes, I tried this method, but it does not work with methods and
> functions, this line
>> __doc__ = _("""testmodule.TestClass.testClassMethod docstring""")
> does nothing (There are not output in help()).

Ah, my bad; I didn't notice.

> Is it any way to assign
> docstring for function explicity?

Yes there is, you will need to reassign the (translated) __doc__ from
*outside* the function definition, a decorator will provide nice
wrapper. This works:

__doc__ = _("""testmodule docstring""")__doc__ = _("""testmodule
docstring""")

def tdoc(obj):
obj.__doc__ = _(obj.__doc__)
return obj

@tdoc
class TestClass:
"""testmodule.TestClass docstring"""
@tdoc
def testClassMethod(self):
"""testmodule.TestClass.testClassMethod docstring"""
print _("Call TestClass.testClassMethod()")

# the decorator is equivalent to:
# def testClassMethod(self):
# print _("Call TestClass.testClassMethod()")
# testClassMethod.__doc__ = _(testClassMethod.__doc__)


as I said, you probably will want to use some metaclass magic to
automatically apply tdoc to all classes and methods. If just mandating
that tdoc must decorate everything works for you, then great.

>> If you want to avoid having the explicit assignment to __doc__, you can
>> also try using some metaclass or decorator magic to automatically wraps
>> docstring in a _() call.
>
> Yes, it will be better to avoid them, is it any existing tool/lib/
> workaround that can do it?
> I am follower of aproved, stable solutions, do not like to reinvent
> the wheel.


==============================================================================
TOPIC: python as pen and paper substitute
http://groups.google.com/group/comp.lang.python/t/c880ee16ca2d5e99?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Apr 6 2010 11:40 am
From: Manuel Graune


Hello everyone,

I am looking for ways to use a python file as a substitute for simple
pen and paper calculations. At the moment I mainly use a combination
of triple-quoted strings, exec and print (Yes, I know it's not exactly
elegant). To clarify, I just start an editor, write a file that
might look something like this:

---------snip-----
code="""
a = 1
b = 2
c = 3
result = a + b
"""
exec(code)
print(code)
print("result =\t", result)
print("result + c =\t", result + c)
---------snip------

and feed this to python.

For what it's worth, this approach achieves what it's supposed to,
which is to get some basic control over the output and
to avoid to much redundant typing.

Now I'm wondering if there is a more elegant method to achieve this which
e. g. does not mess up the syntax-hightlighting, does not use exec()
and avoids the redundant mentioning of the variable that holds the
acutal code. Since I have complete control over the input and the files
are not supposed to be shared, security should not a problem and
simplicity is criterion #1.


So, does anyone have tips?

Regards,

Manuel

P.S.: I know Ipython. In the cases where I use the hack shown above
it just does not fit my workflow


--
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99


== 2 of 2 ==
Date: Tues, Apr 6 2010 1:38 pm
From: Johan Grönqvist


Manuel Graune skrev:
> To clarify, I just start an editor, write a file that
> might look something like this:
>
> ---------snip-----
> code="""
> a = 1
> b = 2
> c = 3
> result = a + b
> """
> exec(code)
> print(code)
> print("result =\t", result)
> print("result + c =\t", result + c)
> ---------snip------
>
> and feed this to python.
>

I do not understand your use-case, but as one way of performing the same
task as the above code, without sacrificing syntax-highlighting, I would
suggest:

-------------------------
from __future__ import with_statement
import sys

def print_source():
print sys.argv
with open(sys.argv[0]) as file:
for line in file:
print line,

a = 1
b = 2
c = 3
result = a + b

print_source()
print("result =\t", result)
print("result + c =\t", result + c)

------------------------


Does that help towards a solution of your problem?

/ johan


==============================================================================
TOPIC: Simplify Python
http://groups.google.com/group/comp.lang.python/t/e1fcfc6ebcb0506f?hl=en
==============================================================================

== 1 of 4 ==
Date: Tues, Apr 6 2010 12:04 pm
From: ja1lbr3ak


I'm trying to teach myself Python, and so have been simplifying a
calculator program that I wrote. The original was 77 lines for the
same functionality. Problem is, I've hit a wall. Can anyone help?

loop = input("Enter 1 for the calculator, 2 for the Fibonacci
sequence, or something else to quit: ")
while loop < 3 and loop > 0:
if loop == 1:
print input("\nPut in an equation: ")
if loop == 2:
a, b, n = 1, 1, (input("\nWhat Fibonacci number do you want to
go to? "))
while n > 0:
print a
a, b, n = b, a+b, n-1
loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
sequence, or something else to quit: ")

== 2 of 4 ==
Date: Tues, Apr 6 2010 1:06 pm
From: Christopher Choi


On Tue, 06 Apr 2010 12:04:20 -0700, ja1lbr3ak wrote:

> I'm trying to teach myself Python, and so have been simplifying a
> calculator program that I wrote. The original was 77 lines for the same
> functionality. Problem is, I've hit a wall. Can anyone help?
>
> loop = input("Enter 1 for the calculator, 2 for the Fibonacci sequence,
> or something else to quit: ") while loop < 3 and loop > 0:
> if loop == 1:
> print input("\nPut in an equation: ")
> if loop == 2:
> a, b, n = 1, 1, (input("\nWhat Fibonacci number do you want to
> go to? "))
> while n > 0:
> print a
> a, b, n = b, a+b, n-1
> loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
> sequence, or something else to quit: ")

I'm fairly confused here.

But I would do this instead:

UserInput1 = input("Enter 1 for the calculator, 2 for the Fibonacci
sequence, or something else to quit: ")

While quit = 0:
if UserInput1 == 1:
print input("\nPut in an equation: ")
else if UserInput1 == 2:
a, b, n = 1, 1, (input("\nWhat Fibonacci number do you
want to go to? "))
while n > 0:
print a
a, b, n = b, a+b, n-1
UserInput2 = input("\nEnter 1 for the calculator,
2 for the Fibonacci sequence, or something else to quit: ")

else
quit = 1


the above is not finished... but without actually knowing what your trying
to do.. the above code I just did would make a lot more sense.. I hope...
You hit a wall cause the first while loop you had before never ends..

== 3 of 4 ==
Date: Tues, Apr 6 2010 1:14 pm
From: ja1lbr3ak


On Apr 6, 4:06 pm, Christopher Choi <chu...@gmail.com> wrote:
> On Tue, 06 Apr 2010 12:04:20 -0700, ja1lbr3ak wrote:
> > I'm trying to teach myself Python, and so have been simplifying a
> > calculator program that I wrote. The original was 77 lines for the same
> > functionality. Problem is, I've hit a wall. Can anyone help?
>
> > loop = input("Enter 1 for the calculator, 2 for the Fibonacci sequence,
> > or something else to quit: ") while loop < 3 and loop > 0:
> >     if loop == 1:
> >         print input("\nPut in an equation: ")
> >     if loop == 2:
> >         a, b, n = 1, 1, (input("\nWhat Fibonacci number do you want to
> > go to? "))
> >         while n > 0:
> >             print a
> >             a, b, n = b, a+b, n-1
> >     loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
> > sequence, or something else to quit: ")
>
> I'm fairly confused here.
>
> But I would do this instead:
>
> UserInput1 = input("Enter 1 for the calculator, 2 for the Fibonacci
> sequence, or something else to quit: ")
>
> While quit = 0:
>         if UserInput1 == 1:
>                 print input("\nPut in an equation: ")
>         else if UserInput1 == 2:
>                 a, b, n = 1, 1, (input("\nWhat Fibonacci number do you
> want to go to? "))
>                 while n > 0:
>                         print a
>                         a, b, n = b, a+b, n-1
>                         UserInput2 = input("\nEnter 1 for the calculator,
> 2 for the Fibonacci sequence, or something else to quit: ")
>
>         else
>                 quit = 1
>
> the above is not finished... but without actually knowing what your trying
> to do.. the above code I just did would make a lot more sense.. I hope...
> You hit a wall cause the first while loop you had before never ends..

The while loop is my main program loop. There at the end is the "loop
= input" part again. If you type in something other than 1 or 2, it
exits the while loop and ends the program. I also merged the "quit"
and "UserInput1" variables in your program into one "loop" variable.


== 4 of 4 ==
Date: Tues, Apr 6 2010 1:17 pm
From: Christopher Choi


On Tue, 06 Apr 2010 13:14:44 -0700, ja1lbr3ak wrote:

> On Apr 6, 4:06 pm, Christopher Choi <chu...@gmail.com> wrote:
>> On Tue, 06 Apr 2010 12:04:20 -0700, ja1lbr3ak wrote:
>> > I'm trying to teach myself Python, and so have been simplifying a
>> > calculator program that I wrote. The original was 77 lines for the
>> > same functionality. Problem is, I've hit a wall. Can anyone help?
>>
>> > loop = input("Enter 1 for the calculator, 2 for the Fibonacci
>> > sequence, or something else to quit: ") while loop < 3 and loop > 0:
>> >     if loop == 1:
>> >         print input("\nPut in an equation: ")
>> >     if loop == 2:
>> >         a, b, n = 1, 1, (input("\nWhat Fibonacci number do you
>> >         want to
>> > go to? "))
>> >         while n > 0:
>> >             print a
>> >             a, b, n = b, a+b, n-1
>> >     loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
>> > sequence, or something else to quit: ")
>>
>> I'm fairly confused here.
>>
>> But I would do this instead:
>>
>> UserInput1 = input("Enter 1 for the calculator, 2 for the Fibonacci
>> sequence, or something else to quit: ")
>>
>> While quit = 0:
>>         if UserInput1 == 1:
>>                 print input("\nPut in an equation: ")
>>         else if UserInput1 == 2:
>>                 a, b, n = 1, 1, (input("\nWhat Fibonacci number
>>                 do you
>> want to go to? "))
>>                 while n > 0:
>>                         print a
>>                         a, b, n = b, a+b, n-1
>>                         UserInput2 = input("\nEnter 1 for
>>                         the calculator,
>> 2 for the Fibonacci sequence, or something else to quit: ")
>>
>>         else
>>                 quit = 1
>>
>> the above is not finished... but without actually knowing what your
>> trying to do.. the above code I just did would make a lot more sense..
>> I hope... You hit a wall cause the first while loop you had before
>> never ends..
>
> The while loop is my main program loop. There at the end is the "loop =
> input" part again. If you type in something other than 1 or 2, it exits
> the while loop and ends the program. I also merged the "quit" and
> "UserInput1" variables in your program into one "loop" variable.


Can you post it please?

==============================================================================
TOPIC: Impersonating a Different Logon
http://groups.google.com/group/comp.lang.python/t/c07dd48ba4835250?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Apr 6 2010 12:26 pm
From: Kevin Holleran


Hello,

I am sweeping some of our networks to find devices. When I find a
device I try to connect to the registry using _winreg and then query a
specific key that I am interested in. This works great for machines
that are on our domain, but there are left over machines that are
stand alone and the credentials fail. I understand you cannot pass in
credentials with _winreg but is there a way to simulate a logon of
another user (the machine's local admin) to query the registry?

Thanks for your help.

Kevin


== 2 of 2 ==
Date: Tues, Apr 6 2010 1:11 pm
From: Tim Golden


On 06/04/2010 20:26, Kevin Holleran wrote:
> Hello,
>
> I am sweeping some of our networks to find devices. When I find a
> device I try to connect to the registry using _winreg and then query a
> specific key that I am interested in. This works great for machines
> that are on our domain, but there are left over machines that are
> stand alone and the credentials fail. I understand you cannot pass in
> credentials with _winreg but is there a way to simulate a logon of
> another user (the machine's local admin) to query the registry?

The simplest may well be to use WMI (example from here):

http://timgolden.me.uk/python/wmi/cookbook.html#list-registry-keys

<code - untested>
import wmi

reg = wmi.WMI (
"machine",
user="machine\admin",
password="Secret",
namespace="DEFAULT"
).StdRegProv

result, names = reg.EnumKey (
hDefKey=_winreg.HKEY_LOCAL_MACHINE,
sSubKeyName="Software"
)
for name in names:
print name

</code>

I can't try it out at the moment but in principle it should work.

TJG

==============================================================================
TOPIC: plotting in python 3
http://groups.google.com/group/comp.lang.python/t/99d7eb33b8e4cd9e?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Apr 6 2010 12:23 pm
From: Rolf Camps


Hi,

What's the easiest way to plot graphs in python 3.x? Ís there a package?
Quality doesn't really matter.

Thanks,

Rolf


== 2 of 2 ==
Date: Tues, Apr 6 2010 12:55 pm
From: Christopher Choi


On Tue, 06 Apr 2010 21:23:34 +0200, Rolf Camps wrote:

> Hi,
>
> What's the easiest way to plot graphs in python 3.x? Ís there a package?
> Quality doesn't really matter.
>
> Thanks,
>
> Rolf
> Hi,
>
> What's the easiest way to plot graphs in python 3.x? Ís there a package?
> Quality doesn't really matter.
>
> Thanks,
>
> Rolf

I would like to think you have done some home work yourself....
but anyways... gnuplot with python seems easy..

http://gnuplot-py.sourceforge.net/doc/

Chris

==============================================================================
TOPIC: per-method jit compiler
http://groups.google.com/group/comp.lang.python/t/4265701a4b0f611a?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Apr 6 2010 12:56 pm
From: Irmen de Jong


On 6-4-2010 8:22, Luis M. González wrote:
> The above post gave me an idea (very naive, of couse).
> What if I write a simple decorator to figure out the types of every
> function, and then we use it as a base for a simple method-jit
> compiler for python?
> example:
> def typer(f):
> def wrap(*args):
> a = f.func_code.co_varnames
> b = [type(i) for i in args]
> return dict(zip(a,b))
> return wrap
> @typer
> def spam(a, b, c=3, d=4):
> pass
>>>> spam(8,'hello',9.9, 10)
>
> {'a':<type 'int'>, 'c':<type 'float'>, 'b':<type 'str'>, 'd':
> <type
> 'int'>}
> So by using this information, we record all the argument types used
> the first time each function/method is executed, and then we generate
> optimized code for them.
> From this point on, a guard should check if all arguments remain the
> same and, if so, the optimized code is run.
> Otherwise, just fall back to the interpreter.
> He! I have no idea how to implement it...
> Any guru out there?
> Luis

Isn't this what Psyco (http://psyco.sourceforge.net/) does?

-irmen

==============================================================================
TOPIC: passing command line arguments to executable
http://groups.google.com/group/comp.lang.python/t/4f15edabd97830c8?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Apr 6 2010 1:15 pm
From: Bror Johansson


On 2010-04-03 18:09, mcanjo wrote:
> I have an executable (I don't have access to the source code) that
> processes some data. I double click on the icon and a Command prompt
> window pops up. The program asks me for the input file, I hit enter,
> and then it asks me for and output filename, I hit enter a second time
> and it goes off and does its thing and when it is finished running the
> Command Prompt goes away and I have my new output file in the same
> directory as my executable and input file. I would like to be able to
> batch process a group of files. I thought about using "os.spawnv()" in
> a loop and at each iteration of the loop passing in the file in and
> out names but that didn't work. Does anyone have any ideas?

It's been quite a while since I had to solve problems like the one you
have. Sometimes I used a Python implementation of 'expect' successfully.


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

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

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

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

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

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

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate