[Tutor] getattr()

2010-08-04 Thread Pete
Hi,

I'm trying to understand the syntax for reflection in python. I was wondering 
about this.

From the example on diveintopython:

   http://diveintopython.org/power_of_introspection/index.html

import statsout

def output(data, format="text"):
output_function = getattr(statsout, "output_%s" % format, 
statsout.output_text)
return output_function(data) 

I was wondering about how to make this work if the function that you are trying 
to call is not in a module. Is that possible?

Something like this:

def output_text(data):
return_value = "This is text: " + data
return return_value

def output(data, format="text"):
output_function = getattr(*, "output_%s" % format, statsout.output_text)
return output_function(data)

What I can't figure out is what to put in place of *. I've tried 
globals()['__name__'], in various forms, to no avail.

Any pointers would be appreciated.

thanks,

Pete

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getattr()

2010-08-04 Thread Pete
Hey Huy,

thanks. But what is the first parameter in this case? (Note there is nothing 
here I'm trying to accomplish except understand).

In the original example, 'statsout' is the name of the module. In this case 
there is no module so why can you substitute 'output_text'?

thanks,

Pete

On 2010-08-04, at 3:09 PM, Huy Ton That wrote:

> If it is in the same code, and the namespace isn't a module or another class 
> you could do this:
> 
> def output_text(data):
> return_value = "This is text: " + data
> return return_value
> 
> def output(data, format="text"):
> output_function = getattr(output_text, "output_%s" % format, output_text)
> return output_function(data)
> 
> -HTH
> 
> Huy
> 
> On Wed, Aug 4, 2010 at 1:23 PM, Pete  wrote:
> Hi,
> 
> I'm trying to understand the syntax for reflection in python. I was wondering 
> about this.
> 
> From the example on diveintopython:
> 
>http://diveintopython.org/power_of_introspection/index.html
> 
> 
> import statsout
> 
> def output(data, format="text"):
> output_function = getattr(statsout, "output_%s" % format, 
> statsout.output_text)
> return output_function(data) 
> 
> 
> I was wondering about how to make this work if the function that you are 
> trying to call is not in a module. Is that possible?
> 
> Something like this:
> 
> 
> def output_text(data):
> 
> return_value = "This is text: " + data
> return return_value
> 
> 
> 
> def output(data, format="text"):
> output_function = getattr(*, "output_%s" % format, 
> statsout.output_text)
> return output_function(data)
> 
> What I can't figure out is what to put in place of *. I've tried 
> globals()['__name__'], in various forms, to no avail.
> 
> Any pointers would be appreciated.
> 
> thanks,
> 
> Pete
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] import errors

2010-08-11 Thread Pete
Hi,

A common line I've seen in Python code I come across is:

#!/usr/bin/python
import os
import sys
import errors

I was wondering about the line that says, "import errors". I understand what 
it's for, but what I don't get is how Python knows which 'errors.py' to import.

On my laptop:

macbook:bin han$ locate errors.py | grep -E 'py$'
/Library/Frameworks/Mono.framework/Versions/2.4.2.1/lib/IPCE/Lib/_codecs_errors.py
/Library/Frameworks/Mono.framework/Versions/2.4.2.1/lib/IPCE/Lib/fepy/codecs_errors.py
/Library/Python/2.6/site-packages/Skype4Py/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/distutils/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/plat-mac/macerrors.py
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/email/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/macerrors.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/email/errors.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/macerrors.py

... so there seem to be more than one 'errors.py' - one for the email module, 
one for Skype4Py, etc.

How does the interpreter know which one to import?

Thanks for all your time, btw - I'm learning a lot lurking on this list and 
greatly appreciate all the people offering help and advice.

cheers,

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Callbacks and exception handling

2010-08-12 Thread Pete
Hi,

I've been writing some code which uses callbacks. I have not used callbacks 
much before, but it does not seem too difficult so far.

One thing though - I noticed that when an exception is raised in the callback 
function, that exception doesn't actually "show up" in the calling program.

Two questions:

1) Theoretically - why is this? I guess I've seen callback functions a little 
like subroutines, therefore it seems intuitive that an exception would be 
propagated up to the calling routine.

2) How can you catch an exception in the callback function?

The code is not easily isolated, but can post if necessary.

thanks again,

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Callbacks and exception handling

2010-08-12 Thread Pete
> By the way, don't be tempted to write a bare except clause:
> 
> try:
>  ...
> except:
>  ...
> 
> There are very few reasons for such a thing, as they are too broad and 
> catch too many things, masking errors, preventing user keyboard 
> interrupts, etc. Avoid them.

Doh! That is what I did :)

Thanks Steve!

-Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] List comprehension for dicts?

2010-08-19 Thread Pete
Hi,

I've been reading up on list comprehensions lately, all userful and powerful 
stuff - trying to wrap my brain around it :)

As the examples all seem to relate to lists, I was wondering if there is an 
elegant similar way to apply a function to all keys in a dictionary?

(without looping over it, of course)

I'm trying to convert all keys in a dict to uppercase, as in:

INPUT:
age_dict = { 'pete': 42, 'ann': 25, 'carl': 30, 'amanda': 64 }

OUTPUT:
age_dict = { 'PETE': 42, 'ANN': 25, 'CARL': 30, 'AMANDA': 64 }

I googled 'dictionary comprehension' but couldn't get any code to work with the 
examples given.

http://www.siafoo.net/article/52#dictionary-comprehensions

thanks,

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Pete
On 2010-08-19, at 5:25 PM, Emile van Sebille wrote:

> On 8/19/2010 7:51 AM Pete said...
>> Hi,
>> 
>> I've been reading up on list comprehensions lately, all userful and powerful 
>> stuff - trying to wrap my brain around it :)
>> 
>> As the examples all seem to relate to lists, I was wondering if there is an 
>> elegant similar way to apply a function to all keys in a dictionary?
>> 
>> (without looping over it, of course)
> 
> Just fyi, as it sounds you may not understand.  List Comprehensions loop over 
> the list.  There's no other magic going on that avoids that.
> 
> result = [ ii for ii in lst if cond ]
> 
> is just shorthand for
> 
> result = []
> for ii in lst:
>  if cond:
>result.append(ii)
> 
> Emile

Ah, so list comprehensions are a purely syntactic construct?

I had the feeling there might be a performance benefit of some kind.

thanks for all the contributions on this topic, it's been greatly helpful to my 
understanding.

-Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dict of function calls

2010-09-22 Thread Pete
For a plugin mechanism, I'm populating a dict with calls to the implemented 
plugins. 

Consider this:

>>> class foo:
... def do(self):
... print 'do foo'
... 
>>> class bar:
... def do(self):
... print 'do bar'
... 
>>> list = { 'foo': foo.do(), 'bar': bar.do() }
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unbound method do() must be called with foo instance as first 
argument (got nothing instead)

===

Now I get that I need to instantiate foo and bar first before I can refer to 
them in the dict.

so something like

foo_instance = foo()
bar_instance = bar()

list = { 'foo': foo_instance.do(), 'bar': bar_instance.do() }

would probably work. But is that the best/pythonic way to do it? 
I first implemented "do" as a static method but that seemed... wrong (also 
because it needs to access some variables from the instance).

Any hints would be appreciated,

Pete


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dict of function calls

2010-09-22 Thread Pete

On 2010-09-22, at 5:50 PM, Steven D'Aprano wrote:

> On Thu, 23 Sep 2010 06:07:21 am Pete wrote:
>> For a plugin mechanism, I'm populating a dict with calls to the
>> implemented plugins.
> [...]
>> Now I get that I need to instantiate foo and bar first before I can
>> refer to them in the dict.
>> 
>> so something like
>> 
>> foo_instance = foo()
>> bar_instance = bar()
>> 
>> list = { 'foo': foo_instance.do(), 'bar': bar_instance.do() }
>> 
>> would probably work. But is that the best/pythonic way to do it?
> 
> (1) It's generally a bad idea to "shadow" the name of a built-in 
> function like list. Particularly since your variable list isn't 
> actually a list, but a mapping of name:function. Find a better 
> name :)

That was just for the example, of course, but point taken :)

> (2) You end up calling the do() methods inside the dict. You probably 
> mean to save the method itself as a callback function.

I did.

> (3) There's no need to keep the instances floating around as 
> independent names, unless you need direct access to them.
> 
> Putting these together, we get:
> 
> dispatch_table = { 'foo': foo().do, 'bar': bar().do }
> 
> Which you then later call using:
> 
> dispatch_table[name](args)
> 
> where name is "foo" or "bar", and args is anything you need to pass to 
> the do() method.

I see. And are those methods then unbound? (Because when instantiating the 
object it needs to do some initialization of instance variables,
and I think those were not available when calling an unbound method. (My 
understanding of the bound/unbound method distinction is rather vague at this 
point,
I apologise)

> Oh, and (4)... in Python circles, it's traditional but not compulsory 
> to use metasyntactic variables named after Monty Python sketches 
> rather than foo and bar. So spam, ham, eggs, parrot (dead or 
> otherwise), names of cheeses, aardvark..., although there is no 
> standard order.
> 
>  Indeed, when I design my killer language, the identifiers "foo" and
>  "bar" will be reserved words, never used, and not even mentioned in
>  the reference manual. Any program using one will simply dump core
>  without comment. Multitudes will rejoice. -- Tim Peters, 29 Apr 1998

THANK YOU! 

It's this kind of knowledge that I crave, sir.

:)

- Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] __import__()

2010-09-23 Thread Pete
Hiya,

still working on my plugin architecture. I figured out how to import modules of 
which I don't know the name yet at compile time,
by using __import__() instead of import.

So that works fine when I want to have the equivalent of 

import spam

... by using

__import__('spam')

Question:

what is the equivalent of

from spam import *

?

thanks,

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dict of function calls

2010-09-23 Thread Pete
> Oh, and (4)... in Python circles, it's traditional but not compulsory 
> to use metasyntactic variables named after Monty Python sketches 
> rather than foo and bar. So spam, ham, eggs, parrot (dead or 
> otherwise), names of cheeses, aardvark..., although there is no 
> standard order.

Hm, someone maybe should update http://en.wikipedia.org/wiki/Foobar
... as there is a python example listed there...

- Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] syntax error that i cant spot!

2011-01-01 Thread pete

Hi,
Please help just starting out and have come up with the following code 
to create a simple guessing game.


on line 30 print good job etc i get a syntax error! sure it's simple but 
i've looked for ages and cant spot it!


Regards
Pete
# guess the number game
import random

guessesTaken = 0

print 'Hello what is your name?'
myName = raw_input()

number = random.randint(1, 20)
print 'Well, ' + myName + ', I am thinking of a number between 1 & 20.'

while guessesTaken < 6:
print 'Take a guess.' # 4 spaces infront of print
guess = raw_input()
guess = int(guess)

guessesTaken = guessesTaken +1

if guess < number:
print 'Your guess is too low,' # 8 spaces...

if guess > number:
print 'Your guess is too high.'

if guess == number:
break

if guess ==number:
guessesTaken = str(guessesTaken)
print 'Good job, ' +myName "! You guessed the number in " + guessesTaken + ' guesses!'

if guess !=number:
number = str(number)
print 'Nope. The number I was thinking of was ' + number

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3.5 32-bit installer not creating standard registry key

2015-09-14 Thread Pete


Sorry, I've just realised that my previous message was wrong - please 
delete it. I now see that the key in question is actually a Python for 
Windows Extensions key. The problem appears to lie in the Python for 
Windows Extensions 32-bit installer not recognising any of the new 
Python 3.5 32-bit keys. I'll have to find some way of contacting the 
Python for Windows Extensions developers to ask them which key the 
installer is looking for, and then try to determine why I don't have it.


Pete

Subject: Python 3.5 32-bit installer not creating standard registry key
Date: 2015-09-14 15:46
From: Pete 
To: tutor@python.org

Hi there,

I am looking for the best place to post a problem I'm having with Python 
3.5.


The 64-bit installer works fine, creating among other 64-bit registry 
keys a generic one called 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\pywin32-py3.5. 
Installing Python for Windows Extensions afterwards works fine as its 
installer looks for this particular key on installation.


However, the 32-bit Python 3.5 installer does not create a similar 
generic key in the 32-bit registry, meaning that I can't install Python 
for Windows Extensions 32-bit afterwards.


This problem did not occur with Python 3.4 which correctly installed 
both keys (with 3.4 in the key name instead of 3.5).


Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3.5 32-bit installer not creating standard registry key

2015-09-14 Thread Pete


Thanks Laura.

In the end I posted the bug to Python for Windows Extensions 
(http://sourceforge.net/p/pywin32/bugs/702/) because I suspect that the 
fault may well lie with the PWE installer.


If I don't get any resolution there then I'll post to the Python bugs 
link you kindly list. However, if you recommend that I post in both 
places now then I'll do that.


Pete

On 2015-09-14 18:10, Laura Creighton wrote:

In a message of Mon, 14 Sep 2015 15:53:16 +0100, Pete writes:



Sorry, I've just realised that my previous message was wrong - please
delete it. I now see that the key in question is actually a Python for
Windows Extensions key. The problem appears to lie in the Python for
Windows Extensions 32-bit installer not recognising any of the new
Python 3.5 32-bit keys. I'll have to find some way of contacting the
Python for Windows Extensions developers to ask them which key the
installer is looking for, and then try to determine why I don't have 
it.


Pete


post a bug here:
http://bugs.python.org/

Laura

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] conventions for establishing and saving default values for variables

2010-06-15 Thread Pete O'Connell
Hi I was wondering if anyone could give me some insight as to the best way
to get and save variables from a user the first time a script is opened. For
example if the script prompts something like "What is the path to the
folder?" and the result is held in a variable called thePath, what is the
best way to have that variable saved for all subsequent uses of the script
by the same user.
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to numerically sort strings that start with numbers?

2010-09-13 Thread Pete O'Connell
theList = ["21 trewuuioi","3zxc","134445"]
print sorted(theList)

Hi, the result of the sorted list above doesn't print in the order I
want. Is there a straight forward way of getting python to print
['3zxc','21 trewuuioi','134445']
rather than ['134445', '21 trewuuioi', '3zxc']?

Any help would be greatly appreciated
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] filling in web forms

2009-06-20 Thread Pete Froslie
I would to access web forms and fill them out.

I am feeling that the 'ClientForm' module makes sense as a starting place,
but am concerned that maybe it is dated and that there might be a better
starting option.. can anyone help start me along the correct path as I am
pretty new to python and not the most experienced programmer?

thanks so much!

-- 
P Froslie
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] filling in web forms

2009-06-20 Thread Pete Froslie
Thank you so much.. I will start looking into twill and I just finished
installing Mechanize.

FYI:

I use Netbeans as my IDE and encountered an error that took some time to
resolve, as follows:
A java.lang.NoClassDefFoundError exception has occurred

the resolution can be found here if you run into it:
http://forums.netbeans.org/post-38386.html

it seems to have been an issue with mac OS X : java 10.5 update 6.

with that fixed I'll be able to try these options out..



On Sun, Jun 21, 2009 at 1:03 AM, Serdar Tumgoren wrote:

> You might want to read up on Python Mechanize as well:
>
> http://wwwsearch.sourceforge.net/mechanize/
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] DOMForm

2009-06-26 Thread Pete Froslie
Hi, so I've been using Mechanize as suggested to me through tutor to access
web forms and fill them out.. I've found some success though I wish there to
be more documentation than docstrings.. as I am new to python and not
incredibly experienced with code.

I am able to fill forms and submit them in situations such as a
'search-submit' form on a blog.  *My problem is evident when I approach a
form that appears to be linked to a javascript -- I am unable to
submit*(essentially click on the button).. I assume this is because
python does not
handle javascript.  I am now looking at DOMForm as a solution, but am unsure
as to weather or not this is correct..

An example of the form I would like to access looks like this:
*
*
*








***

#the is more like the above I'm leaving out before the standard form
entry html such as the following:

td align="Left" class="LabelCell">Enter
e-mail address of recipient



**


The python code I've put together looks like this:

*import ClientForm
import urllib2
request = 
urllib2.Request("http://www.qinetiq-nacareers.com/qinetiq/jobboard/SendToFriend.aspx?__JobID=*C2B68228BC44075E";)
response = urllib2.urlopen(request)
forms = ClientForm.ParseResponse(response, backwards_compat=False)
response.close()

form = forms[0]
print form

form["Email_To"] = "pfros...@gmail.com"
form["Email_From"] = "pfros...@gmail.com"
form["SenderName"] = "Patrick"
form["Comments"] = "this job looks great"

request2 = form.click("Send")

print form
control = form.find_control("Email_To", type="text")
print control.name, control.value, control.type
print request
print request2*


i've tried a few other options, but essentially feel my trouble boils
down to thos issue with java..

*Thank you so much!-- I appologoze if this post is formatted a little
too confusing-- kind of getting use to communicating about pythono..
*
*
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] egg

2009-07-01 Thread Pete Froslie
Hi, I have a basic question concerning pythons eggs. I have setup easy
install and used it to install mechanize-- this was easy as I only needed to
type in 'Easy_Install Mechanize' in terminal. Now I need to install
'simplejson 2.0.9' from this here: http://pypi.python.org/pypi/simplejson

Can anyone suggest the correct way to do this as it seems it is not as
direct? I've read the EasyInstall documentation and believe I understand
how, but am a little confused regarding my python paths. Using Mac OSX
10.5.7

Thanks so much,

-- 
Pete Froslie
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] egg

2009-07-01 Thread Pete Froslie
responding to myself:

apparently it seems to be as simple again as: 'easy_install simplejson' from
terminal.

Anyone, have a suggestion for a resource where I can read more about the
paths I have setup? I would like to make sure there are not any
redundancies..

thanks

On Wed, Jul 1, 2009 at 4:47 PM, Pete Froslie  wrote:

> Hi, I have a basic question concerning pythons eggs. I have setup easy
> install and used it to install mechanize-- this was easy as I only needed to
> type in 'Easy_Install Mechanize' in terminal. Now I need to install
> 'simplejson 2.0.9' from this here: http://pypi.python.org/pypi/simplejson
>
> Can anyone suggest the correct way to do this as it seems it is not as
> direct? I've read the EasyInstall documentation and believe I understand
> how, but am a little confused regarding my python paths. Using Mac OSX
> 10.5.7
>
> Thanks so much,
>
> --
> Pete Froslie
> http://www.froslie.net
>
>


-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] mac os x executable

2009-07-07 Thread Pete Froslie
Hi,

I'm having trouble finding good tutorials on creating standalone executable
files for mac os x.. I've been looking at 'py2app', but can't seem get a
solid grasp. Any help would be greatly appreciated!

cheers
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mac os x executable

2009-07-07 Thread Pete Froslie
Thanks Bill,

Terminal tells me, 'chmod: app_test.py: No such file or directory'

I assume that's what you meant to do? Drop this at the start of the script:

#!/usr/bin/env python

Then go into terminal and type:

`chmod +x mypython.py''

--pretty new to some of this stuff.

cheers

On Tue, Jul 7, 2009 at 12:54 PM, Bill Campbell  wrote:

> On Tue, Jul 07, 2009, Pete Froslie wrote:
> >Hi,
> >
> >I'm having trouble finding good tutorials on creating standalone
> executable
> >files for mac os x.. I've been looking at 'py2app', but can't seem get a
> >solid grasp. Any help would be greatly appreciated!
>
> For a python script, say mypython.py, it should start with
> something like this:
>
> #!/usr/bin/env python
> # do stuff here
>
> Then ``chmod +x mypython.py'' and it's ready to go.
>
> Bill
> --
> INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
> URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
> Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
> Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792
>
> There has been no greater threat to life, liberty, and property
> throughout the ages than government. Even the most violent and brutal
> private individuals have been able to inflict only a mere fraction of
> the harm and destruction that have been caused by the use of power by
> political authorities. -- Richard Ebeling
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mac os x executable

2009-07-07 Thread Pete Froslie
Wesley,

What I meant is that I would like a file that can be double clicked in order
to run the python script.. so that I can drop it on another mac where
someone can execute it without worrying about Terminal.

Essentially, I need everything to be clean in the end.. My projects are
artworks and function better without the tech confusion when viewers
interact with them.. I hope this helps clarify my trouble

Thanks for the explanation -- it is helpful..



On Tue, Jul 7, 2009 at 2:35 PM, wesley chun  wrote:

> >> >I'm having trouble finding good tutorials on creating standalone
> >> > executable
> >> >files for mac os x.. I've been looking at 'py2app', [...]
>
>
> pete,
>
> welcome to Python!  in order for all to answer your question more
> appropriately, you'll have to describe the problem in slightly more
> detail.
>
> what do you mean when you say "standalone executable?" the reason why
> i ask is because a tool like py2app -- the equivalent to py2exe on PCs
> -- is meant to be used in situations where you're installing the
> application on a Mac which you're not sure whether Python is installed
> on it and/or to bundle up your code in a way that's harder to reverse
> engineer than a plain text .py file.
>
> bill's suggestion comes from the fact that pretty much all Macs these
> days come with Python installed and all you would need to do is to
> just make your .py file executable, hence the "chmod +x
> YOUR_MODULE.py" so that you can run YOUR_MODULE.py directly from the
> shell instead of having to prepend it, i.e, "python YOUR_MODULE.py".
>
> in your example above, you have both mypython.py and app_test.py --
> you need to make sure that you: 1) use the same name (which is why i
> changed them to YOUR_MODULE.py meaning substitute in the actual
> filename in place of "YOUR_MODULE" and 2) that the directory that your
> script is in is also in your PATH environment variable -- otherwise
> you would need to do ./YOUR_MODULE.py if your shell is in the same
> directory as your file.
>
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>



-- 
Pete Froslie
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mac os x executable

2009-07-07 Thread Pete Froslie
Yes, this makes sense to me.. but when treating a project as an artwork I
can't ask people to go through an effort beyond a simple start of the app.
For instance, I'm in Boston and the work is being show on a computer in
gallery in SF-- if it crashes, curators often will be unenthusiastic when
being told over the phone how to re-launch the artwork-- it would be much
easier to say, 'just double click the icon labeled artwork'. Same goes for
an artwork I would like to distribute online to 'non-tech' viewers...

Does this make sense?

thank,
Pete F

On Tue, Jul 7, 2009 at 5:14 PM, Alan Gauld wrote:

>
> "Pete Froslie"  wrote
>
>  I'm having trouble finding good tutorials on creating standalone
>> executable
>> files for mac os x.. I've been looking at 'py2app', but can't seem get a
>> solid grasp. Any help would be greatly appreciated!
>>
>
> My first question would be do you need to? After all with Python 2.3 pre
> installed on MacOS X can't you just distribute the python files?
>
> Just a thought.,
>
> Alan G
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mac os x executable

2009-07-07 Thread Pete Froslie
That's great Alan.. I see what you mean with the shortcut-- going to try it.
Thanks for the suggestion on the Mac OS x manual.. I will certainly start
looking more into OS fundamentals.

I will say that I've been having a really good time with Python and am
excited to see it start working further with the OS (as well as utilizing it
in artworks)

Cheers,
Pete

On Tue, Jul 7, 2009 at 7:12 PM, ALAN GAULD wrote:

> > Yes, this makes sense to me.. but when treating a project as an artwork >
> I can't ask people to go through an effort beyond a simple start of the
> > app. For instance, I'm in Boston and the work is being show on a
> > computer in gallery in SF-- if it crashes, curators often will be
> > unenthusiastic when being told over the phone how to re-launch the
> > artwork-- it would be much easier to say, 'just double click the icon
> > labeled artwork'.
> Sure, But you can do that without creating an executable.All you need is
> to drop a shortcut to the file on the desktop
> and have the shebang line and chmod settings set up
> correctly as per the previous posting. You don't even
> need to have it end in .py just use the name of your
> applicatrion.
>
> But it would be worth while spending a little time learning
> about how Mac OS X works wioth the underlying Unix
> shell and the interaction between icons on the desktop
> and the underlying files. A book like The Missing Manual
> for Mac OS X or almost any other in depth Moc OS X
> manual should explain what you need to know.
>
> But if you are taking the trouble to learn Python you
> really should learn the OS fundamentals too
>
> Alan Gauld
> Author of the Learn To Program website
> http://www.alan-g.me.uk/
>
>
> --
> *From:* Pete Froslie 
> *To:* Alan Gauld 
> *Cc:* tutor@python.org
> *Sent:* Tuesday, 7 July, 2009 10:33:10 PM
> *Subject:* Re: [Tutor] mac os x executable
>
>
>
> Does this make sense?
>
> thank,
> Pete F
>
> On Tue, Jul 7, 2009 at 5:14 PM, Alan Gauld wrote:
>
>>
>> "Pete Froslie"  wrote
>>
>>  I'm having trouble finding good tutorials on creating standalone
>>> executable
>>> files for mac os x.. I've been looking at 'py2app', but can't seem get a
>>> solid grasp. Any help would be greatly appreciated!
>>>
>>
>> My first question would be do you need to? After all with Python 2.3 pre
>> installed on MacOS X can't you just distribute the python files?
>>
>> Just a thought.,
>>
>> Alan G
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Pete Froslie
> 617.314.0957
> http://www.froslie.net
>
>


-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] thesaurus

2009-07-08 Thread Pete Froslie
okay.. I'm getting the hang of python a little more. I'd like to try
something a bit more complicated (for me). I would like to convert each word
in a series of paragraphs to its first matching synonym in a thesaurus. I'm
a bit stuck as how to start.. I think I understand how to split and parse
the original paragraphs, but the thesaurus is throwing me. I'm thinking
maybe find a CSV thesaurus -or- a thesaurus online with an API.. I'm
wondering if anyone has done something similar that may point me in the
correct direction?


thanks,

-- 
Pete F
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: thesaurus

2009-07-08 Thread Pete Froslie
-- Forwarded message --
From: Pete Froslie 
Date: Wed, Jul 8, 2009 at 8:53 PM
Subject: Re: [Tutor] thesaurus
To: Robert Berman 


Thanks Robert,

I will try this out.. at the moment I'm playing with an API from '
http://words.bighugelabs.com/'. It works and pulls the synonyms into python.
It cost money if you want to process more than 10,000 in a day though.

I do have another pretty noob question that I'm figuring out -- once I have
a list of synonyms returned, is there a simple way to replace the words I
looked up inside of the 'txt' file?

For instance, I open and read the 'txt' file, grab the first word, search it
with the thesaurus, get the result, write the result back to the file, and
then grab the next word to repeat the process. It seems like there is
probably a quick shortcut for this..

thanks so much




On Wed, Jul 8, 2009 at 8:25 PM, Robert Berman  wrote:

> http://pywordnet.sourceforge.net/
>
> This will get you started. This is a tad easier to play with than its
> newer implementation. Read and experiment. it may meet most of your
> needs in this arena.
>
> Good Luck,
>
> Robert
>
>
>
> On Wed, 2009-07-08 at 18:28 -0400, Pete Froslie wrote:
> > okay.. I'm getting the hang of python a little more. I'd like to try
> > something a bit more complicated (for me). I would like to convert
> > each word in a series of paragraphs to its first matching synonym in a
> > thesaurus. I'm a bit stuck as how to start.. I think I understand how
> > to split and parse the original paragraphs, but the thesaurus is
> > throwing me. I'm thinking maybe find a CSV thesaurus -or- a thesaurus
> > online with an API.. I'm wondering if anyone has done something
> > similar that may point me in the correct direction?
> >
> >
> > thanks,
> >
> > --
> > Pete F
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Pete Froslie
617.314.0957
http://www.froslie.net




-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: thesaurus

2009-07-08 Thread Pete Froslie
Great Richard, thanks..

I'm getting an error as follows:

from __future__ import with_statement
SyntaxError: from __future__ imports must occur at the beginning of the file

I don't think this is the issue in need of rework and have tried a few quick
reworks.. I'll read up a bit on 'with'

cheers


On Wed, Jul 8, 2009 at 9:41 PM, Rich Lovely wrote:

> 2009/7/9 Pete Froslie :
> >
> >
> > -- Forwarded message --
> > From: Pete Froslie 
> > Date: Wed, Jul 8, 2009 at 8:53 PM
> > Subject: Re: [Tutor] thesaurus
> > To: Robert Berman 
> >
> >
> > Thanks Robert,
> >
> > I will try this out.. at the moment I'm playing with an API from
> > 'http://words.bighugelabs.com/'. It works and pulls the synonyms into
> > python. It cost money if you want to process more than 10,000 in a day
> > though.
> >
> > I do have another pretty noob question that I'm figuring out -- once I
> have
> > a list of synonyms returned, is there a simple way to replace the words I
> > looked up inside of the 'txt' file?
> >
> > For instance, I open and read the 'txt' file, grab the first word, search
> it
> > with the thesaurus, get the result, write the result back to the file,
> and
> > then grab the next word to repeat the process. It seems like there is
> > probably a quick shortcut for this..
> >
> > thanks so much
> >
> >
> >
>
> Assuming lookup() handles punctuation and capitalisation...
>
> import sys
>
> if sys.version_info < (2,5):
>print "This script needs a more recent version of python"
>sys.exit(1)
> elif sys.version_info < (2,6):
>from __future__ import with_statement
>
> buff = []
> with open("path_to_input_file", "r") as fin:
>for line in fin:
>buff.append(" ".join(lookup(word) for word in line.split()))
>
> with open("path_to_output_file", "w") as fout:
>fout.write("\n".join(buff))
>
> This is also a good intro to the with statement, which cleans
> everything up for you.  Unfortunatly, it was only introduced in 2.5 as
> a __future__ feature, and 2.6 as a final feature.  If you've got a
> version prior to that, you'll need to rework it a little, or upgrade.
>
> But I think this gives the general idea.  I don't think there's any
> more concise way of doing it than that in python.
>
> You also might want to use print instead of writing straight to a
> file, and use the terminal's stream redirection to put the output into
> a file.
>
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: thesaurus

2009-07-08 Thread Pete Froslie
No problem, thanks for taking the time.

I'm actually trying to resolve this error now:

   buff.append(" ".join(lookup(Word) for Word in line.split()))
NameError: global name 'lookup' is not defined

..also assume I need to change 'Word' to something that checks the next word
in the text file and then replaces it with the one that is looked up..
working on that now.

Pete F

On Wed, Jul 8, 2009 at 10:10 PM, Rich Lovely wrote:

> 2009/7/9 Pete Froslie :
> > Great Richard, thanks..
> >
> > I'm getting an error as follows:
> >
> > from __future__ import with_statement
> > SyntaxError: from __future__ imports must occur at the beginning of the
> file
> >
> > I don't think this is the issue in need of rework and have tried a few
> quick
> > reworks.. I'll read up a bit on 'with'
> >
> > cheers
> >
> >
>
> (Appologies for my inital, incomplete email...)
> Oops... forgot it wasn't a standard import...
>
> Just move it to the first line of the program, and delete the relevant
> elif branch.
>
> It won't hurt code on more recent versions.
>
> from __future__ import with_statement
> import sys
>
> buff = []
> with open("path_to_input_file", "r") as fin:
>   for line in fin:
>   buff.append(" ".join(lookup(word) for word in line.split()))
>
> with open("path_to_output_file", "w") as fout:
>   fout.write("\n".join(buff))
>
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: thesaurus

2009-07-08 Thread Pete Froslie
I see.. that makes sense. Kind of new with python -- sorry for that.

after printing using this:

print urllib.urlopen('
http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/').read(
)

I'm getting a format like this returned:

adjective|sim|streaming
adjective|sim|swirling
adjective|sim|tossing
adjective|sim|touching
adjective|sim|touring
adjective|sim|traveling
adjective|sim|tumbling

I assume I need to clean this up by reading past  'Adjective|sim|' to
'streaming' and then returning it from lookup()..
this will be happening in the following:

urllib.urlopen('
http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/').read(SOMETHINGHERE)



On Wed, Jul 8, 2009 at 10:29 PM, Rich Lovely wrote:

> 2009/7/9 Pete Froslie :
> > No problem, thanks for taking the time.
> >
> > I'm actually trying to resolve this error now:
> >
> >buff.append(" ".join(lookup(Word) for Word in line.split()))
> > NameError: global name 'lookup' is not defined
> >
> > ..also assume I need to change 'Word' to something that checks the next
> word
> > in the text file and then replaces it with the one that is looked up..
> > working on that now.
> >
> > Pete F
>
> lookup() is the function that looks up the word in the thesaurus.  I
> left implementing that as an exercise for you, as I don't know the
> format of the reference you're using.
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: thesaurus

2009-07-08 Thread Pete Froslie
oops.. I just realized I attached the wrong example for the API-- it was off
by number, this one works:

print urllib.urlopen('
http://words.bighugelabs.com/api/2/e413f24701aa30b8d441ca43a64317be/moving/').read(
)

The example makes sense to me and I can see how it is difficult to figure
out a natural language parser.. as it turns out, I don't mind it for this
project--gibberish is fine. Though I am now pretty curious about NL
parsers-- thanks for the example..

I will look at using the split method of strings..

On Wed, Jul 8, 2009 at 11:12 PM, Rich Lovely wrote:

> 2009/7/9 Pete Froslie :
> > I see.. that makes sense. Kind of new with python -- sorry for that.
> >
> > after printing using this:
> >
> > print
> > urllib.urlopen('
> http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/').read(<http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/%27%29.read%28>
> )
> >
> > I'm getting a format like this returned:
> >
> > adjective|sim|streaming
> > adjective|sim|swirling
> > adjective|sim|tossing
> > adjective|sim|touching
> > adjective|sim|touring
> > adjective|sim|traveling
> > adjective|sim|tumbling
> >
> > I assume I need to clean this up by reading past  'Adjective|sim|' to
> > 'streaming' and then returning it from lookup()..
> > this will be happening in the following:
> >
> > urllib.urlopen('
> http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/').read(SOMETHING<http://words.bighugelabs.com/api/2/e413f24801aa30b8d441ca43a64317be/moving/%27%29.read%28SOMETHING>
> > HERE)
> >
> >
> >
> I don't think there is any easy way of doing that.
>
> You would be better off using the split method of the strings, and
> ignoring the first parts for now.
>
> Have you considered what happens with the word set in the following
> sentance, about testing TV receivers:
>
> We've set the code number of each set to a reasonable value for this
> set of experiments.
>
> I can't get the api to work for me, but the way you're doing it at the
> moment, you'd end up with something like
>
> We've fixed the code number of each fixed to a reasonable value for
> this fixed of experiments.
>
> A contrived example, I know, but it makes the point.
>
> Unless, of course, this sort of gibberish is what you're after.
>
> Natural language parsers are one of the hardest things to create.
> Just look up the word "set" in a dictionary to see why.  Even if you
> did work out that the second "set" was a noun, is it "a radio or
> television receiver" or "a group or collection of things that belong
> together, resemble one another, or are usually found together"
>
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thesaurus

2009-07-11 Thread Pete Froslie
So, I basically have this working so that it pulls a word from a 'txt' file;
searches for the synonym in the thesaurus; returns the result... but then I
am having trouble with probably the most simple part: I cannot seem to go
back into the 'txt' file and replace the word I just searched with the new
word! I tried a few methods. One with re.sub, which I can't seem to get to
work and one adapting Rich's suggestion in this thread. Rich's suggestion
works to a degree-- it replaces all of the words in the 'txt' with the new
word. I'm probably missing something obvious, so thought I'd ask before I
get too far down the confused road. here is the code I'm tinkering with now
(know its a bit messy and will clean it up for a repost  when I'm done):

from __future__ import with_statement
> import re
> from urllib2 import urlopen
>
> word_number = 2 # when incremented-- steps through each word of the text
> file.
> API_URL = '
> http://words.bighugelabs.com/api/2/e413f24701aa30b8d441ca43a64317be/'
> #thesaurus = API_URL + response[word_number]
>
> fin = open("journey_test.txt", "r")
> #really need to make sure I split at all of the delimiters at this point
> response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]\r]", fin.read())
>
> thesaurus = API_URL + response[word_number] + '/'
> print thesaurus # just to test the thesaurus is working
> response2 = urlopen(thesaurus) #set the API to search for the current word
> response3 = re.split(r"[/|/,\n]", response2.read()) # strip the result
> downd to the needed word
>
> print response3[2] # shows the result of the stripping down to the needed
> word
> fin.close()
>
>
> #*HAVING TROUBLE HERE**
> #
> new_word = response3[2]
> old_word = response[word_number]
>
>
> buff = []
> with open("journey_test.txt", "r") as fin:
>   for line in fin:
>   buff.append(" ".join(new_word for old_word in line.split()))
>
> with open("journey_test.txt", "w") as fout:
>   fout.write("\n".join(buff))
> #
> #*HAVING TROUBLE HERE**
>
> """
> #Different Attempt:  I stopped on***
> combine = response3[2]
> combine2 = response[word_number]
>
> fout = open("journey_test.txt", "r/w")
> #data = open("journey_test.txt").read()
> #exchange = re.compile('MAKES')
> fout.write( re.sub(combine, combine2) )  #trying to replace
> fout.close()
> #THIS IS THE AREA OF DIFFICULTY***
> """
>

On Fri, Jul 10, 2009 at 7:26 PM, ALAN GAULD wrote:

>
>
>
>
> > right now, but Chinese apparently has no distinct verb forms for past,
> present,
> > future.  So they rely on other words to indicate which they might mean.
>
>
> Chinese also has the problem of relying on intonation to distinguish
> between
> identically spelled words. We have the same in English - bow(on stage) v
> bow
> (as in arrows), but there are lots of these things in Chinese making
> translation
> of written language almost wholly context dependant, and therefore very
> difficult for a machine.
>
> Alan G
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thesaurus

2009-07-11 Thread Pete Froslie
thanks for the advice Alan.. I am wondering about the following:

new_word = response3[2]
old_word = response[word_number]

#this works but adds carriage returns*
for line in fileinput.FileInput("journey_test.txt",inplace=1):
   line = line.replace(old_word, new_word)
   print line

It seems that if I put it in at the difficulty location it works, but it
also adds carriage returns to the entire text file. Basically it ends up
double spacing a single-spaced document.  I'm also not sure if this is
causing the trouble you suggested against (writing and reading at the same
time).

cheers

On Sat, Jul 11, 2009 at 3:38 PM, ALAN GAULD wrote:

> > I am having trouble with probably the most simple part:
> > I cannot seem to go back into the 'txt' file and replace the word I just
> > searched with the new word!
>
> Its not a good idea to try to read and write to the same file at the same
> time. The normal approach is to weither ead the file into memory and
> process it then write it back to the file or to open a second file and
> write to that then copy the second file over the original.
>
> > One with re.sub, which I can't seem to get to work
>
> re.sub works on a text string it doesn't affect the file.
>
> read the content into a string, close the input file.
> Use re.sub to make the changes (or even just the replace
> method of strings) then write the changed string back out
> to the file.
>
> HTH,
>
> Alan G
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thesaurus

2009-07-11 Thread Pete Froslie
the trailing comma is getting me much closer right away! -- will read about
the other options you suggest (rstrip(); stdout: write()) I clearly have a
few other issues I need to sort out now. i guess I've been assuming that
print was a good way to trace what is happening rather than thinking about
its results in the external file as well..

As for the inline: simply put, I'm an artist trying to learn how to code for
my work, so my decisions are not altogether as informed as they could be.
Thanks for taking the time to make that suggestion.. I will try to rework it
to make use of functions. Then I will address the next set of issues..

This seems the basic form for a function:

*def** hello*():
> *print* "Hello World!"
> *return*
>
> I assume each part that can be separated will be done so in this format at
the start of the code; consequently, allowing me to call 'hello' later when
I need it. Does this also mean that I will be able to call those functions
separately later when I import 'thesaurus.py' into a new code also?

Pete F

On Sat, Jul 11, 2009 at 5:38 PM, Dave Angel  wrote:

> Pete Froslie wrote:
>
>> thanks for the advice Alan.. I am wondering about the following:
>>
>> new_word = response3[2]
>> old_word = response[word_number]
>>
>> #this works but adds carriage returns*
>> for line in fileinput.FileInput("journey_test.txt",inplace=1):
>>   line = line.replace(old_word, new_word)
>>   print line
>>
>> It seems that if I put it in at the difficulty location it works, but it
>> also adds carriage returns to the entire text file. Basically it ends up
>> double spacing a single-spaced document.  I'm also not sure if this is
>> causing the trouble you suggested against (writing and reading at the same
>> time).
>>
>> cheers
>>
>> On Sat, Jul 11, 2009 at 3:38 PM, ALAN GAULD > >wrote:
>>
>>
>>
>>> I am having trouble with probably the most simple part:
>>>> I cannot seem to go back into the 'txt' file and replace the word I just
>>>> searched with the new word!
>>>>
>>>>
>>> Its not a good idea to try to read and write to the same file at the same
>>> time. The normal approach is to weither ead the file into memory and
>>> process it then write it back to the file or to open a second file and
>>> write to that then copy the second file over the original.
>>>
>>>
>>>
>>>> One with re.sub, which I can't seem to get to work
>>>>
>>>>
>>> re.sub works on a text string it doesn't affect the file.
>>>
>>> read the content into a string, close the input file.
>>> Use re.sub to make the changes (or even just the replace
>>> method of strings) then write the changed string back out
>>> to the file.
>>>
>>> HTH,
>>>
>>> Alan G
>>>
>>>
>>>
>> fileinput.FileInput() finesses the problem of updating "in place" very
> nicely.  But it also replaces STDOUT, which can make it harder to debug your
> code, because if you put other print statements there, they'll go to the
> file, by default.  Anyway, here's your answers to the doublespacing:
>
> If you put a trailing comma on the print statement, it won't append an
> extra newline, so you won't end up doublespacing.
> Or you could rstrip() the 'line' variable, to remove the newline that's
> there before print.
> Or you could write to stdout using write() instead of print.
>
> I'd like to know if there's a reason you're putting this code all inline.
>  By breaking it into functions, you'd have a chance to work on the different
> aspects of it independently.  That also might mean that you could change
> different aspects of it independently, or even parameterize it from a
> command line to do slightly different things.
>
> For example, what if you'd like to substitute more than one word for its
> synomym?  Do you really want to pass through the entire file each time?  Or
> what if you want to get your synomyms from somewhere else, or from multiple
> places?
>
> DaveA
>
>


-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thesaurus

2009-07-12 Thread Pete Froslie
so, I've been playing with the functions a bit-- took a little time to get
the hang of them after I started that other mess. Initially, I've done the
opposite suggested here by creating functions without the input values. I do
see the where it will pay off in re-usability that route.. Alan, I'll read
your tutorial on modules and functions.

Things that are arising for me now:

(1)the use of global variables- seems it would be easier to communicate
between functions with 'I/O' values if I understood where the best location
for declaration is..

(2)not sure why this function doesn't work:

word_count = 0 #set up variable to increment through text

def increment(x):
> return  x+1
>
> increment(word_count)
>


(3) related to strings: Trying a function that will strip unwanted words off
of the url before I bounce it off the website. For instance, I don't need to
search for a, was, is... This is working, but with strange results-- often
it will find an 'a' in the middle of a word and replace it with text and
such.

using rstrip, but with varying results:

#this is the url as a string
> http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/
>
> thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and
> re-adding the '/'
>

The url function btw:

def url():
> fin = open("journey_test.txt", "r")
> response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']",
> fin.read())
> thesaurus = API_URL + response[word_number] + '/'  #API_URL is
> established at the start of the code
> return thesaurus
>

Pete F


On Sun, Jul 12, 2009 at 4:00 AM, Alan Gauld wrote:

>
> "Pete Froslie"  wrote
>
>  This seems the basic form for a function:
>>
>> *def** hello*():
>>
>>>*print* "Hello World!"
>>>*return*
>>>
>>
> That is a very limited form that encourages bad practice. The more
> general form is:
>
> def aFunction(inputValue1, inputValue2,...):
> # do some processing here
> return output_value1, outputValue2
>
> By adding the input and output values we make the function
> independant of its immediate surroundings and therefore
> more easily reusable across different programs.
>
> We can  then call it like:
>
> x,y = aFunction(a,b)
>
>  I assume each part that can be separated will be done so in this format at
>>>
>> the start of the code; consequently, allowing me to call 'hello' later
>> when
>> I need it. Does this also mean that I will be able to call those functions
>> separately later when I import 'thesaurus.py' into a new code also?
>>
>
> Exactly. The first benefit of functions is in encapsulating parts of your
> program and providing structure which makles it easier to read but
> the real power of functions is in making those blocks of code available
> to other programs too.
>
> Read my tutorial topic on Moduules and Functions more information.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thesaurus

2009-07-12 Thread Pete Froslie
>
> You don't seem to understand functions.  Part of the point is to *avoid*
> global variables.  If everything a function uses is passed in as arguments,
> and if everything it produces is returned as return value(s), it's much
> easier to see its effect.  And to make sure it doesn't have bugs.


True enough- that's why I seek advice. My trouble was with the bit of code I
was writing inline; while adapting to the functions I misunderstood their
value regarding global variables.  I'm happy to say the code is using
functions more appropriately now and that, as I learn, it will get closer to
something more respectable. I see the advantages for debugging

word_count = 0 #set up variable to increment through text
>
> def increment(x):
>
>>return  x+1
>> increment(word_count)
>>
>>
>>
> This function doesn't have any effect on global variables.  It returns a
> value that's one more than its argument.  Period.  If you wanted to
> increment word_count using that function, you'd do something like:
>

Thanks-- that makes complete sense; it appears that I just was wrapping my
head around functions right away. Initially, I assumed this function could
be something quick for any increments. Rather than writing out: 'word_count
= word_count + 1' when needed..

I have no clue what this url() function is trying to do.  Nor how you expect
> to use it.  Are you planning to open this file for each word contained in
> it?


yes. Essentially, it grabs each word from a text file and combines it with
the other stuff to create a url string that I send through an API to an
online thesaurus. I was attempting to strip it this way as a weak method for
cleaning out the words I don't want searched from the text file.

Along with the the other functions the code currently scans a text file and
replaces each of its words with one of their synonyms.. slightly legible
gibberish, but that's what I'm interested in for now. It is a project to
help me start learning pyhton as well being intended to take a different
form in an artwork I'm working on.

thanks.
petef



On Sun, Jul 12, 2009 at 7:40 PM, Dave Angel  wrote:

> Pete Froslie wrote:
>
>> so, I've been playing with the functions a bit-- took a little time to get
>> the hang of them after I started that other mess. Initially, I've done the
>> opposite suggested here by creating functions without the input values. I
>> do
>> see the where it will pay off in re-usability that route.. Alan, I'll read
>> your tutorial on modules and functions.
>>
>> Things that are arising for me now:
>>
>> (1)the use of global variables- seems it would be easier to communicate
>> between functions with 'I/O' values if I understood where the best
>> location
>> for declaration is..
>>
>>
>>
> You don't seem to understand functions.  Part of the point is to *avoid*
> global variables.  If everything a function uses is passed in as arguments,
> and if everything it produces is returned as return value(s), it's much
> easier to see its effect.  And to make sure it doesn't have bugs.
>
>> (2)not sure why this function doesn't work:
>>
>> word_count = 0 #set up variable to increment through text
>>
>> def increment(x):
>>
>>
>>>return  x+1
>>>
>>> increment(word_count)
>>>
>>>
>>>
>> This function doesn't have any effect on global variables.  It returns a
> value that's one more than its argument.  Period.  If you wanted to
> increment word_count using that function, you'd do something like:
>
> word_count = increment(word_count)
>
>  (3) related to strings: Trying a function that will strip unwanted words
>> off
>> of the url before I bounce it off the website. For instance, I don't need
>> to
>> search for a, was, is... This is working, but with strange results-- often
>> it will find an 'a' in the middle of a word and replace it with text and
>> such.
>>
>> using rstrip, but with varying results:
>>
>> #this is the url as a string
>>
>>
>>> http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/
>>>
>>> thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and
>>> re-adding the '/'
>>>
>>>
>>>
>>
>> The url function btw:
>>
>> def url():
>>
>>
>>>fin = open("journey_test.txt", "r")
>>>response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']",
>>> fin.read())
>>>thesaurus = API_URL + response[word_number] + '/'  #API_URL is
>>> established at the start of the code
>>>return thesaurus
>>>
>>>
>>>
>>
>> Pete F
>>
>>
>>
>
> I have no clue what this url() function is trying to do.  Nor how you
> expect to use it.  Are you planning to open this file for each word
> contained in it?
>
> DaveA
>



-- 
Pete Froslie
617.314.0957
http://www.froslie.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] find and replace relative to an nearby search string in a file

2009-07-12 Thread Pete O'Connell
Hi, I am trying to do a find and replace in a text file (a nuke script).
Here are a couple excerpts from the file that demonstrate what I want to do.
I am always looking for the line " name Write1" as my starting point. In the
first example below, I want to replace the path, which is 2 lines above it.
I have made a basic script to do that and it works fine. The problem I am
having is when the number of lines between " name Write1" and the path above
it is not 1, my script breaks. I'm not sure how to say in python "when you
find the line " name Write1", go back line by line until you find a line
that begins with  " file /Volumes/" and then assign a new variable to the
path on that line".
At the very bottom of this post I have included what I have so far (The
script which works but breaks). Any help would be greatly appreciated.
Pete



Write {
 file /Volumes/raid0/Z353_002_comp_v27.%04d.cin
 file_type cin
 name Write1
 xpos 13762
 ypos -364
}



Write {
 file /Volumes/raid0/Z353_002_comp_v04.%04d.exr
 colorspace linear
 raw true
 file_type exr
 name Write1
 selected true
 xpos -487
 ypos -155
}

# This is the basic script
import re

theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk',"r")

theNukeScriptText = theFile.read()
theFile.close()



#p = re.compile('.+_comp_v\d\d.%04d.cin\n file_type cin\n name Write1')
p = re.compile(r'.+_comp_v\d\d.%04d.cin\n.+\n name Write1')
m =  p.finditer(theNukeScriptText)
the3Lines = p.findall(theNukeScriptText)

the3LinesString = ''.join(the3Lines)

theOldSeq = the3LinesString.split('\n')[0]
print str(the3LinesString.split('\n')[0])
theNewSeq = 'file /Volumes/raid0/Z353_002_comp_v27.%04d.cin'


theFile = open('/Volumes/raid0/Z353_001_comp_v05.nk', "w")
theFile.write(theNukeScriptText.replace(theOldSeq,theNewSeq))
theFile.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] open linux file browser from nuke python script

2011-01-31 Thread Pete O'Connell
Hi, I am trying to get a python script to open up a file browser window with
the location of a folder. I am using kubuntu 10.04.
The string that gets created works fine if I paste it into a shell eg:
'kde-open path/to/the/sequence',
but it doesn't seem to want to run from within my interpreter using either
subprocess.Popen or os.system
For the first argument I have tried 'kde-open', 'konqueror', and a few
others that all work fine in the shell but not in my interpreter.
Here is what my script looks like:

import nuke
import subprocess
def showFolder():
subprocess.Popen(['kde-open', 'path/to/the/sequence'])
showFolder()
###
I must to run it from a specific interpreter (Nuke, the compositing
software)
Anyone have any idea what I might be doing wrong? Can anyone suggest a
workaround?
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] getting the last file in a folder (reliably)

2011-03-21 Thread Pete O'Connell
Hi I have some code which works nine times out of ten. Maybe some could help
me make this a little more robust. I have a bunch of files in a folder with
a strict versioning based naming convention, where I want to open the
highest version number of a nuke script (not necessarily the one with the
newest modification date). So in the example folder contents listed below I
want to open "233_bb0072_comp_comp2k_v05.nk" (fourth from the bottom). Every
once in a while my python script ends up opening
233_bb0072_comp_comp2k_v04.nk (the next to last correct one). The script I
am using is shown below. Can anyone explain why it doesn't always work?


233_bb0072_comp_comp2k_v01.nk
233_bb0072_comp_comp2k_v01.nk~
233_bb0072_comp_comp2k_v02.nk
233_bb0072_comp_comp2k_v03.nk
233_bb0072_comp_comp2k_v03.nk~
233_bb0072_comp_comp2k_v04.nk
233_bb0072_comp_comp2k_v04.nk~
233_bb0072_comp_comp2k_v05.nk
233_bb0072_comp_comp2k_v05.autosave
233_bb0072_comp_comp2k_v05.nk~
233_bb0072_comp_comp2k_v06.nk

###
import os
def openNewestCompCommandLine():

theDirectory = "/path/to/my/comps/"
theFilesInTheFolder = os.listdir(theDirectory)
for aFile in theFilesInTheFolder:
if "~" not in aFile:
if "autosave" not in aFile:
theNukeFileName = aFile

theFullPath = theDirectory + theNukeFileName
os.system("nuke "  + theFullPath)
if __name__ == '__main__':
openNewestCompCommandLine()

for aFile in theFilesInTheFolder:
print aFile

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to temporarily disable a function

2011-07-27 Thread Pete O'Connell
Hi I was wondering if there is a way to disable a function.
Hi have a GUI grid snapping function that I use in a program called Nuke
(the film compositing software)

Here is the function (which loads when Nuke loads):
###
def theAutoplaceSnap():
try:
nuke.thisNode().autoplace()
n = nuke.allNodes();
for i in n:
  nuke.autoplaceSnap(i)
except:
pass

nuke.addOnUserCreate(theAutoplaceSnap)
###

I have many functions which get loaded, but this particular one needs to be
disabled when I am viewing another compositors script in the gui.

I have a python script editor in Nuke in which I can run code if need be to
run code on the fly.

Help
-- 
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] return, why do I need it?

2011-12-11 Thread Pete O'Connell
Hi I have been writing python code for a while now and I never return
anything within any of my functions, I just (eg.) print stuff or make
directories or update a log or what have you. When I look at other people's
code they are always returning in their functions and I was wondering if
someone could give me an example of when I would absolutely have to return
something. The thing I don't like about returning is that when I unindent a
function and try to run the code to inspect parts of it for debugging I
always have to alter the code so as not to get the "return not inside a
function error",  so I will change the word "return" to "print" and in many
cases that's the way I leave it. Anyone have any thoughts on this?

Thanks
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] commands.getoutput equivalent in subprocess

2012-03-18 Thread Pete O'Connell
Hello I print a lot of values at work using grep and need to be
constantly opening a shell window to do this (rather than staying
within my main program which is Nuke by the Foundry). Is there a
simple equilavent to commands.getoutput that is more up to date (I
would assume within the subprocess module)? It sounds like the
commands module will be phased out soonish. I am using Python 2.6

Here is the kind of thing I would like to do in subprocess

import commands
output = commands.getoutput("echo Hello World!")
print output

Thanks
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] commands.getoutput equivalent in subprocess

2012-03-19 Thread Pete O'Connell
Ok thanks a lot.

Pete

On Mon, Mar 19, 2012 at 10:01 PM, Peter Otten <__pete...@web.de> wrote:
>> Pete O'Connell wrote:
>
>> Hi, I am using Python 2.6 I can't use Python 3 in this particular
> situation.
>
> [Please reply to the list, not in private mail.]
>
> You can continue to use commands.getoutput() in 2.6 and 2.7, and once you
> are ready to make the jump to 3.x replace it with subprocess.getoutput().
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] list comprehension, testing for multiple conditions

2012-08-21 Thread Pete O'Connell
Hi I am trying to parse a text file and create a list of all the lines
that don't include: "vn", "vt" or are empty. I want to make this as
fast as possible because I will be parsing many files each containing
thousands of lines. I though I would give list comprehensions a try.
The last 3 lines of the code below have three list comprehensions that
I would like to combine into 1 but I am not sure how to do that.
Any tips would be greatly appreciated

pete

#start
fileName = '/usr/home/poconnell/Desktop/objCube.obj'
theFileOpened = open(fileName,'r')
theTextAsList = theFileOpened.readlines()

theTextAsListStripped = []
for aLine in theTextAsList:

theTextAsListStripped.append(aLine.strip("\n"))

theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x]
theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x]
theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""]
#end

and here is the simple file I am parsing as a test:

#start##
## OBJ file generated by Nuke ##

# vertex list - offset=0
v 0.00 0.00 1.00
v 1.00 0.00 1.00
v 0.00 1.00 1.00
v 1.00 1.00 1.00
v 1.00 0.00 0.00
v 0.00 0.00 0.00
v 1.00 1.00 0.00
v 0.00 1.00 0.00
v 1.00 0.00 1.00
v 1.00 0.00 0.00
v 1.00 1.00 1.00
v 1.00 1.00 0.00
v 0.00 0.00 0.00
v 0.00 0.00 1.00
v 0.00 1.00 0.00
v 0.00 1.00 1.00
v 0.00 1.00 1.00
v 1.00 1.00 1.00
v 0.00 1.00 0.00
v 1.00 1.00 0.00
v 0.00 0.00 0.00
v 1.00 0.00 0.00
v 0.00 0.00 1.00
v 1.00 0.00 1.00

# point texture coordinates - offset=0
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00
vt 0.00 0.00
vt 1.00 0.00
vt 0.00 1.00
vt 1.00 1.00

# vertex normals - offset=0
vn 0.00 0.00 1.00
vn 0.00 0.00 1.00
vn 0.00 0.00 1.00
vn 0.00 0.00 1.00
vn 0.00 0.00 -1.00
vn 0.00 0.00 -1.00
vn 0.00 0.00 -1.00
vn 0.00 0.00 -1.00
vn 1.00 0.00 0.00
vn 1.00 0.00 0.00
vn 1.00 0.00 0.00
vn 1.00 0.00 0.00
vn -1.00 0.00 0.00
vn -1.00 0.00 0.00
vn -1.00 0.00 0.00
vn -1.00 0.00 0.00
vn 0.00 1.00 0.00
vn 0.00 1.00 0.00
vn 0.00 1.00 0.00
vn 0.00 1.00 0.00
vn 0.00 -1.00 0.00
vn 0.00 -1.00 0.00
vn 0.00 -1.00 0.00
vn 0.00 -1.00 0.00

f 1/1/1 2/2/2 4/4/3 3/3/4
f 5/5/5 6/6/6 8/8/7 7/7/8
f 9/9/9 10/10/10 12/12/11 11/11/12
f 13/13/13 14/14/14 16/16/15 15/15/16
f 17/17/17 18/18/18 20/20/19 19/19/20
f 21/21/21 22/22/22 24/24/23 23/23/24

# end of file
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
What a great mailing list!
Thanks for all the responses.
I have a few questions, though, first in regards to Puneeth's code. He
writes to use:

>theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" 
>not in x  and "vt" not in x and x!= ""]

It works but what I don't understand about this line is why the ands
are nor ors ("or" doesn't work even though I would have expected it
to)
I am sure I will have a few more questions over the next couple days
as I work my way through the responses.

Thanks
Pete



On Wed, Aug 22, 2012 at 6:23 PM, Puneeth Chaganti  wrote:
> On Wed, Aug 22, 2012 at 11:35 AM, Pete O'Connell
>  wrote:
>> Hi I am trying to parse a text file and create a list of all the lines
>> that don't include: "vn", "vt" or are empty. I want to make this as
>> fast as possible because I will be parsing many files each containing
>> thousands of lines. I though I would give list comprehensions a try.
>> The last 3 lines of the code below have three list comprehensions that
>> I would like to combine into 1 but I am not sure how to do that.
>> Any tips would be greatly appreciated
>>
>> pete
>>
>> #start
>> fileName = '/usr/home/poconnell/Desktop/objCube.obj'
>> theFileOpened = open(fileName,'r')
>> theTextAsList = theFileOpened.readlines()
>>
>> theTextAsListStripped = []
>> for aLine in theTextAsList:
>>
>> theTextAsListStripped.append(aLine.strip("\n"))
>>
>> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x]
>> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x]
>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""]
>
> Something like this should work :
>
> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped
> if "vn" not in x  and "vt" not in x and x!= ""]
>
> HTH,
> Puneeth



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Thanks Peter. This looks like what I need:

with open(fileName) as lines:
wanted = [line.strip("\n") for line in lines if "vn" not in line
and "vt" not in line and line != "\n"]

Cheers

And in response to Allan's suggestion. I can see using a generator in
a situation where the if statements were more numerous and complex. I
am sure that will come in handy.

Thanks


On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__pete...@web.de> wrote:
> Pete O'Connell wrote:
>
>> Hi I am trying to parse a text file and create a list of all the lines
>> that don't include: "vn", "vt" or are empty. I want to make this as
>> fast as possible because I will be parsing many files each containing
>> thousands of lines. I though I would give list comprehensions a try.
>> The last 3 lines of the code below have three list comprehensions that
>> I would like to combine into 1 but I am not sure how to do that.
>> Any tips would be greatly appreciated
>>
>> pete
>>
>> #start
>> fileName = '/usr/home/poconnell/Desktop/objCube.obj'
>> theFileOpened = open(fileName,'r')
>> theTextAsList = theFileOpened.readlines()
>
> If you have a file with 1,000,000 lines you have now a list of 1,000,000
> strings of which perhaps 1,000 match your criteria. You are squandering
> memory. Rule of thumb: never use readlines(), iterate over the file
> directly.
>
>> theTextAsListStripped = []
>> for aLine in theTextAsList:
>>
>> theTextAsListStripped.append(aLine.strip("\n"))
>>
>> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x]
>> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x]
>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x !=
>> ""]
>
> I think that should be
>
> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x !=
> ""]
>
> You can combine the three if clauses or add them all to one list-comp:
>
> with open(filename) as lines:
> wanted = [line.strip("\n") for line in lines
>   if "vn" not in line and "vt" not in line and line != "\n"]
>
>
> You can even have multiple if clauses in one list-comp (but that is rarely
> used):
>
> with open(filename) as lines:
> wanted = [line.strip("\n") for line
>   if "vn" not in line
>   if "vt" not in x
>   if line != "\n"]
>
> While your problem is simple enough to combine all filters into one list-
> comp some problems are not. You can then prevent the intermediate lists from
> materializing by using generator expressions. The result minimizes memory
> consumption, too, and should be (almost) as fast. For example:
>
> with open(filename) as lines:
> # use gen-exps to remove empty and whitespace-only lines
> stripped = (line.strip() for line in lines)
> nonempty = (line for line in stripped if line)
>
> wanted = [line for line in nonempty
>   if "vt" not in line and "vn" not in line]
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Hi. The next step for me to parse the file as I want to is to change
lines that look like this:
f 21/21/21 22/22/22 24/24/23 23/23/24
into lines that look like this:
f 21 22 23 24

Below is my terribly slow loop for doing this. Any suggestions about
how to make this code more efficient would be greatly appreciated


fileName = '/usr/home/poconnell/Desktop/objCube.obj'

with open(fileName) as lines:
theGoodLines = [line.strip("\n") for line in lines if "vn" not in
line and "vt" not in line and line != "\n"]

for i in range(len(theGoodLines)):
if theGoodLines[i][0] == "f":
aGoodLineAsList = theGoodLines[i].split(" ")
theGoodLines[i] = aGoodLineAsList[0] + " " +
aGoodLineAsList[1].split("/")[-1] + " " +
aGoodLineAsList[2].split("/")[-1] + " " +
aGoodLineAsList[3].split("/")[-1] + " " +
aGoodLineAsList[4].split("/")[-1]

for anItem in theGoodLines:
print anItem
##

Thanks!
Pete








On Wed, Aug 22, 2012 at 9:59 PM, Pete O'Connell  wrote:
> Thanks Peter. This looks like what I need:
>
> with open(fileName) as lines:
> wanted = [line.strip("\n") for line in lines if "vn" not in line
> and "vt" not in line and line != "\n"]
>
> Cheers
>
> And in response to Allan's suggestion. I can see using a generator in
> a situation where the if statements were more numerous and complex. I
> am sure that will come in handy.
>
> Thanks
>
>
> On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__pete...@web.de> wrote:
>> Pete O'Connell wrote:
>>
>>> Hi I am trying to parse a text file and create a list of all the lines
>>> that don't include: "vn", "vt" or are empty. I want to make this as
>>> fast as possible because I will be parsing many files each containing
>>> thousands of lines. I though I would give list comprehensions a try.
>>> The last 3 lines of the code below have three list comprehensions that
>>> I would like to combine into 1 but I am not sure how to do that.
>>> Any tips would be greatly appreciated
>>>
>>> pete
>>>
>>> #start
>>> fileName = '/usr/home/poconnell/Desktop/objCube.obj'
>>> theFileOpened = open(fileName,'r')
>>> theTextAsList = theFileOpened.readlines()
>>
>> If you have a file with 1,000,000 lines you have now a list of 1,000,000
>> strings of which perhaps 1,000 match your criteria. You are squandering
>> memory. Rule of thumb: never use readlines(), iterate over the file
>> directly.
>>
>>> theTextAsListStripped = []
>>> for aLine in theTextAsList:
>>>
>>> theTextAsListStripped.append(aLine.strip("\n"))
>>>
>>> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x]
>>> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x]
>>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x !=
>>> ""]
>>
>> I think that should be
>>
>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x !=
>> ""]
>>
>> You can combine the three if clauses or add them all to one list-comp:
>>
>> with open(filename) as lines:
>> wanted = [line.strip("\n") for line in lines
>>   if "vn" not in line and "vt" not in line and line != "\n"]
>>
>>
>> You can even have multiple if clauses in one list-comp (but that is rarely
>> used):
>>
>> with open(filename) as lines:
>> wanted = [line.strip("\n") for line
>>   if "vn" not in line
>>   if "vt" not in x
>>   if line != "\n"]
>>
>> While your problem is simple enough to combine all filters into one list-
>> comp some problems are not. You can then prevent the intermediate lists from
>> materializing by using generator expressions. The result minimizes memory
>> consumption, too, and should be (almost) as fast. For example:
>>
>> with open(filename) as lines:
>> # use gen-exps to remove empty and whitespace-only lines
>> stripped = (line.strip() for line in lines)
>> nonempty = (line for line in stripped if line)
>>
>> wanted = [line for line in nonempty
>>   if "vt" not in line and "vn" not in line]
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> -



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Thanks eryksun, that is a very clear example.
Cheers
pete
On Wed, Aug 22, 2012 at 11:16 PM, eryksun  wrote:
> On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__pete...@web.de> wrote:
>>
>> wanted = [line.strip("\n") for line in lines
>>   if "vn" not in line and "vt" not in line and line != "\n"]
>
> Here's an equivalent expression with the negation factored out:
>
> not ("vn" in line or "vt" in line or line == "\n")
>
> http://en.wikipedia.org/wiki/De_Morgan%27s_laws
>
> If you have a lot of tests all using the same operator (e.g. "in"),
> you can use "any" (OR) or "all" (AND) with a generator expression:
>
> vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"]
> wanted = [line for line in lines if not any(v in line for v in vals)]
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano  wrote:
> On 22/08/12 20:28, Pete O'Connell wrote:
>>
>> Hi. The next step for me to parse the file as I want to is to change
>> lines that look like this:
>> f 21/21/21 22/22/22 24/24/23 23/23/24
>> into lines that look like this:
>> f 21 22 23 24
>
>
> In English, what is the rule you are applying here? My guess is:
>
> "Given three numbers separated by slashes, ignore the first two numbers
> and keep the third."
>
> E.g. "17/25/97" => 97.
>
> Am I close?

Hi Steve, yes that is correct

>
>
>
>> Below is my terribly slow loop for doing this. Any suggestions about
>> how to make this code more efficient would be greatly appreciated
>
>
> What makes you say it is "terribly slow"? Perhaps it is as fast as it
> could be under the circumstances. (Maybe it takes a long time because
> you have a lot of data, not because it is slow.)

OK maybe I am wrong about it being slow (I thought for loops were
slower than lis comprehensions). But I do know I need it to be as fast
as possible if I need to run it on a thousand files each with hundreds
of thousands of lines

>
> The first lesson of programming is not to be too concerned about speed
> until your program is correct.
>
> Like most such guidelines, this is not entirely true -- you don't want
> to write code which is unnecessarily slow. But the question you should
> be asking is, "is it fast enough?" rather than "is it fast?".
>
> Also, the sad truth is that Python tends to be slower than some other
> languages. (It's also faster than some other languages too.) But the
> general process is:
>
> 1) write something that works correctly;
>
> 2) if it is too slow, try to speed it up in Python;
>
> 3) if that's still too slow, try using something like cython or PyPy
>
> 4) if all else fails, now that you have a working prototype, re-write
> it again in C, Java, Lisp or Haskell.
>
> Once they see how much more work is involved in writing fast C code,
> most people decide that "fast enough" is fast enough :)

OK I will keep it as is and see if I can live with it.

Thanks
Pete

>
>
>
>> with open(fileName) as lines:
>>  theGoodLines = [line.strip("\n") for line in lines if "vn" not in
>> line and "vt" not in line and line != "\n"]
>
>
> I prefer to write code in chains of filters.
>
> with open(fileName) as lines:
> # get rid of leading and trailing whitespace, including newlines
> lines = (line.strip() for line in lines)
> # ignore blanks
> lines = (line in lines if line)
> # ignore lines containing "vn" or "vt"
> theGoodLines = [line in lines if not ("vn" in line or "vt" in line)]
>
> Note that only the last step is a list comprehension using [ ], the others
> are generator expressions using ( ) instead.
>
> Will the above be faster than your version? I have no idea. But I think it
> is more readable and understandable. Some people might disagree.
>
>
>
>> for i in range(len(theGoodLines)):
>>  if theGoodLines[i][0] == "f":
>>  aGoodLineAsList = theGoodLines[i].split(" ")
>>  theGoodLines[i] = aGoodLineAsList[0] + " " +
>> aGoodLineAsList[1].split("/")[-1] + " " +
>> aGoodLineAsList[2].split("/")[-1] + " " +
>> aGoodLineAsList[3].split("/")[-1] + " " +
>> aGoodLineAsList[4].split("/")[-1]
>
>
>
> Start with a helper function:
>
> def extract_last_item(term):
> """Extract the item from a term like a/b/c"""
> return term.split("/")[-1]
>
>
> for i, line in enumerate(theGoodLines):
> if line[0] == "f":
> terms = line.split()
> theGoodLines[i] = " ".join([extract_last_item(t) for t in terms])
>
>
>
> See how you go with that.
>
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld  wrote:

> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped
> if "vn" not in x
> if "vt" not in x
> if x!= ""]
>
> It's slightly more verbose but it makes the rules more explicit, IMHO.

I agree, it seems easier to read when written on multiple lines. I'll
do it that way,
Thanks
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Ok thanks for the advice everyone.

Cheers
Pete

On Thu, Aug 23, 2012 at 10:58 AM, Jerry Hill  wrote:
> On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell  
> wrote:
>> OK maybe I am wrong about it being slow (I thought for loops were
>> slower than lis comprehensions). But I do know I need it to be as fast
>> as possible if I need to run it on a thousand files each with hundreds
>> of thousands of lines
>
> You're quite likely wrong about that.  The overall runtime of your
> code is very likely to end up dominated by I/O with the hard drive,
> not the microsecond differences in how you process each line.  The way
> to know for sure is to write code that is easy to read and does the
> correct thing.  Then run the code an see if it's too slow for your
> purposes.  If it is, profile the code and see where it's spending all
> of its time.  Only then can you actually optimize the right places.
>
> Once you've identified where your code needs to be faster, you're
> likely to get more mileage out of a clever algorithm change than a
> micro-optimization to your filtering. Or maybe you'll discover that it
> really is worthwhile to optimize your filtering, because it's inside a
> tight loop, and that's where all of your time is spent.  There are a
> lot of tricks you can use to slightly speed up your python code, but
> none of them are worth doing until you know for sure that you're
> optimizing in the right place.  And if it turns out that the code's
> runtime is fine as first written, you don't have to waste your time
> doing lots of work for little gain.
>
> --
> Jerry
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Pete O'Connell
Hi, I have tried to simplify things and am running into a bit of trouble.
What i am really trying to do is: Keep all the lines starting with "v " and
then delete those lines whose modulus 5 don't equal zero

I have written it like this which seems to take a really long time (a
couple of  minutes when iteration over a folder with 200 files to parse)
#
with open(theFilePath) as lines:
#keep only the lines beginning with "v " (this works)
theGoodLines = [line.strip("\n") for line in lines if "v " ==
line[0:2]]
theLinesAsListSubset = []
for i in range(len(theGoodLines)):
nuke.tprint(i)
if i%5 != 0:
continue
elif i%5 == 0:
theLinesAsListSubset.append(theGoodLines[i])


I think it would be better to include the modulud test within the original
list comprehension but I am not sure how to access the index of "line":
#something like this is a sketch of what I mean (I know it's wrong)
theGoodLines = [line.strip("\n") for line in lines if "v " ==
line[0:2] and line.getIndex() % 5 == 0]


Do I need enumerate for this maybe?
Thanks
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] reducing a list evenly when deleting elements by index

2012-09-16 Thread Pete O'Connell
Hi, I have a bezier line with 20 points on it and I am trying to reduce
this to a line with 4 points, keeping the first and last points and 2
evenly spaced points in between like so:
**
becomes:
*. .  . .*

These points are removable only by index, so if I remove point 2,
point 3 then becomes point 2 in the next iteration, so I end up having a
spacing like this which is not what I want:
*.  . . .*


Here is the code that I have so far. I hope it makes sense:
##
for aNode in nuke.allNodes():
if aNode.shown():
theRotoNode = aNode

curveKnob = theRotoNode['curves']
rootLayer = curveKnob.rootLayer
theNumberOfPointToReduceTo = 5
for aShape in rootLayer:
theOriginalShape = aShape
thePointList = []
for aPoint in aShape:

thePointList.append(aPoint)
if len(thePointList) > 5:

theNumberOfPoints = len(thePointList)
keepEvery = float(theNumberOfPoints)/theNumberOfPointToReduceTo
theIndicesToKeep = []
theIndicesToKeep.append(0)
theIndicesToKeep.append(1)
for i in range(theNumberOfPoints)[1:-1]:
if i*keepEvery < theNumberOfPoints:
theIndicesToKeep.append(int(round(i*keepEvery)))
theIndicesToKeep.append(theNumberOfPoints-2)
theIndicesToKeep.append(theNumberOfPoints-1)
thePointsToRemove = tuple(set(range(theNumberOfPoints)) -
set(theIndicesToKeep))

#everything is good up to here, the code below doesn't work
properly and I'm kinda stuck

counter = -1
for aPointIndex in thePointsToRemove:
counter+=1
aPointIndex = aPointIndex-(counter)
print aPointIndex
aShape.remove(aPointIndex)


Any advice would be greatly appreciated.

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reducing a list evenly when deleting elements by index

2012-09-16 Thread Pete O'Connell
Ah of course. Thanks very much Oscar!

Pete

On Mon, Sep 17, 2012 at 1:29 PM, Oscar Benjamin
wrote:

> On 17 September 2012 02:15, Pete O'Connell wrote:
>
>> Hi, I have a bezier line with 20 points on it and I am trying to reduce
>> this to a line with 4 points, keeping the first and last points and 2
>> evenly spaced points in between like so:
>> **
>> becomes:
>> *. .  . .*
>>
>> These points are removable only by index, so if I remove point 2,
>> point 3 then becomes point 2 in the next iteration, so I end up having a
>> spacing like this which is not what I want:
>> *.  . . .*
>
>
> But removing item 19 doesn't affect the position of item 2. The trick is
> to start the loop at the end of the list and work back to the beginning.
>
> Oscar
>



-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reducing a list evenly when deleting elements by index

2012-09-17 Thread Pete O'Connell
> 1) You have a Bezier curver consisting of 20 control points and would like
> to
> approximate it with a simpler cubic Bezier curve consisting of only 4
> points.
> I would call this "approximating a Bezier with a lower order Bezier".
>
> Hi Oscar. Thanks for your reply. This one above is the one that I am
trying to do. It is true that choosing arbitrary points evenly selected
from the original points is not ideal, it is good enough for my purposes in
most cases. When I have a bit more time I am going to try to implement the
Ramer–Douglas–Peucker algorithm to improve the accuracy of my curve
simplification:
http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm

Thanks
Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reducing a list evenly when deleting elements by index

2012-09-18 Thread Pete O'Connell
Thanks Stephen. That looks like nice clean code too!

Cheers
Pete

On Tue, Sep 18, 2012 at 9:59 AM, Stephen Haywood <
step...@averagesecurityguy.info> wrote:

> Someone has already tried. https://github.com/sebleier/RDP
>
>
> On Mon, Sep 17, 2012 at 5:50 PM, Pete O'Connell 
> wrote:
>
>>  When I have a bit more time I am going to try to implement the
>> Ramer–Douglas–Peucker algorithm to improve the accuracy of my curve
>> simplification:
>>
>> http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
>>
>>
>>
> --
> Stephen Haywood
> Information Security Consultant
> CISSP, GPEN, OSCP
> T: @averagesecguy
> W: averagesecurityguy.info
>
>


-- 
-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Connecting Py 2.7 with visa

2015-05-15 Thread Wilson, Pete
Greetings I am trying to write a test executive program using python 2.7 on a 
windows 7 computer. I want to connect to  a Keithley 2100 voltmeter using 
National Instruments VISA. I am having trouble installing pyvisa. All the 
documentation refers to using 'pip' and a command line "$ pip install pyvisa" . 
What interface or console is this? "$" prompt looks like a Linux command line. 
How do we do this with windows?

Do I have to do this? Or can I use the native visa module in Python 2.7? Using 
the help() and dir() features I can get some basic information about visa, but 
the functions have changed, like visa.ResourceManager.open_resource is not 
working. I really liked this function... Are there any examples of how to use 
this new visa? I have some working code below that uses pyvisa, can it be 
converted?

def update_current():
import visa

rm = visa.ResourceManager()
rm.list_resources()

current_1_ma = ""
exe_check = "PASS"

try:
dut_data = open("dut_data.txt", "w")
except:
exe_check = "FAIL"

try:
ki2100 = rm.open_resource('USB0::0x05E6::0x2100::1148525::INSTR')
device_id = ki2100.query("*IDN?")
except:
exe_check = "FAIL"

try:
dut_current_amps = (float(ki2100.query("MEASure:CURRent:DC?")))
dut_current_ma = dut_current_amps * 1000.0
current_1_ma = "%6G" % dut_current_ma
except:
exe_check = "FAIL"

new_line = "Litepoint_Data_Format" + "\r\n"
dut_data.write(new_line)
new_line = "CURRENT_1_MA=" + current_1_ma + "\r\n"
dut_data.write(new_line)
new_line = "EXE_CHECK=" + exe_check + "\r\n"
dut_data.write(new_line)

dut_data.close()

return


if __name__ == "__main__":

update_current()

print "Dumping dut_data.txt"

with open('dut_data.txt') as dut_data:

for line in dut_data:
print line,
   if 'str' in line:
break

dut_data.close()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Connecting Py 2.7 with visa

2015-05-19 Thread Wilson, Pete
Pip-for-windows worked great! Bob's your uncle! Case closed.

Pete

> -Original Message-
> From: Tutor [mailto:tutor-bounces+pete.wilson=atmel@python.org] On
> Behalf Of Mark Lawrence
> Sent: Friday, May 15, 2015 7:15 PM
> To: tutor@python.org
> Subject: Re: [Tutor] Connecting Py 2.7 with visa
> 
> On 15/05/2015 23:12, Wilson, Pete wrote:
> > Greetings I am trying to write a test executive program using python
> 2.7 on a windows 7 computer. I want to connect to  a Keithley 2100
> voltmeter using National Instruments VISA. I am having trouble
> installing pyvisa. All the documentation refers to using 'pip' and a
> command line "$ pip install pyvisa" . What interface or console is
> this? "$" prompt looks like a Linux command line. How do we do this
> with windows?
> >
> 
> It's a Windows command line prompt.  You can get this by hitting the
> Windows logo key with 'R' and then entering cmd.  However the
> simplest way for you I think is to download this
> https://sites.google.com/site/pydatalog/python/pip-for-windows
> 
> > Do I have to do this? Or can I use the native visa module in Python
> 2.7? Using the help() and dir() features I can get some basic
> information about visa, but the functions have changed, like
> visa.ResourceManager.open_resource is not working. I really liked this
> function... Are there any examples of how to use this new visa? I have
> some working code below that uses pyvisa, can it be converted?
> >
> 
> Please help us to help you.  Stating "is not working" is less than
> useless, please show us exactly what happens.  Cut and paste any
> output, don't rely on typing it as this often results in further errors
> that just confuse the issue.
> 
> > def update_current():
> >  import visa
> >
> >  rm = visa.ResourceManager()
> >  rm.list_resources()
> >
> >  current_1_ma = ""
> >  exe_check = "PASS"
> >
> >  try:
> >  dut_data = open("dut_data.txt", "w")
> >  except:
> >  exe_check = "FAIL"
> 
> Don't use bare excepts as it's asking for trouble.  Much better to
> leave out the error handling to start with and just let your
> programming errors bubble up as stack traces.  Then add in appropriate
> things to catch.  In the above FileNotFoundError amongst others seems
> suitable.
> 
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to load python code only after program startup?

2012-11-19 Thread Pete O'Connell
Hi I use a compositing program called Nuke which loads my custom modules on
start up. So if I have an error in my python code somewhere, Nuke won't
open and it throws a typical error which is easy enough to fix.
The problem I am running into is that when others on my network are using
an older version of Nuke, some of my code causes their older version to not
open. For example, recently I started using gnuplot.py for graphical feed
back which the older version of Nuke doesn't like.
So my question is:
What is the best way to wrap all my custom code so that it isn't read on
startup, but rather only after I invoke a "loadMyCustomModules.py" module.

Help please:)!

Pete
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Registering callbacks and .DLL

2014-10-17 Thread Wilson, Pete
Hi AG. I guess the attachment py_cb.7z was stripped. The statement below is 
correct, I am trying to register a Python function as the Callback 
(Confirmation)
For a C++ function in the .dll. When I register the call-back with 
prod_bat_vol_read_request I should get a machine readable 'token' from 
send_serial_data.tx_data. I send this token to the battery tester to start it. 
When the test is done, my_conf_func should be triggered.
The code is in line below. 

Section 15.17.1.17 is in the 2.6/library docs see the URL in the comments 
below. 

I tried several different ways without success. 

Thanks for your advice. 

Pete


#*
''' bat_read_15_17_1_17
this program is attempting ot register a call back function the 
ProductionTest.dll
using the techniques outlined in section 15.17.1.17 of the python docs
https://docs.python.org/2.6/library/ctypes.html#module-ctypes
'''

'''open and register SendSerialData
this part looks like it's working
'''

from ctypes import *
pt_dll = pt_dll = cdll.LoadLibrary("c:/py_stuff/ProductionTest.dll")
reg_send_serial_data = pt_dll.RegSendSerialData
class SendSerialData_t(Structure):
_fields_ = [("tx_data", c_void_p),
("size", c_uint8)]
send_serial_data = SendSerialData_t()
try:
reg_send_serial_data(send_serial_data)
except:
print "reg_send_serial_data fail"

print send_serial_data.tx_data
print send_serial_data.size

'''set up ProdBatVolRequest and register my_conf_func as the call back
this is NOT working.
'''

prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest

#MY_FUNC = CFUNCTYPE(None, POINTER(c_uint16),POINTER(c_uint8))
#MY_FUNC = CFUNCTYPE(None, c_uint16,c_uint8)
MY_FUNC = CFUNCTYPE(c_uint16,c_uint8)
#MY_FUNC = WINFUNCTYPE(c_uint16,c_uint8)
#MY_FUNC = WINFUNCTYPE(None, POINTER(c_uint16),POINTER(c_uint8))

def my_conf_func(bat_vol, status):
print "my_conf_func", bat_vol, status
#return 0

conf_func = MY_FUNC(my_conf_func)

print "breakpoint"

#prod_bat_vol_read_request(conf_func)

#prod_bat_vol_read_request(my_conf_func)

#prod_bat_vol_read_request(POINTER(my_conf_func)) 

#**


> -Original Message-
> From: Tutor [mailto:tutor-bounces+pete.wilson=atmel@python.org] On
> Behalf Of Alan Gauld
> Sent: Thursday, October 16, 2014 5:24 PM
> To: tutor@python.org
> Subject: Re: [Tutor] Registering callbacks and .DLL
> 
> On 17/10/14 00:35, Wilson, Pete wrote:
> > I'm having problems registering a call-back with a .DLL. Maybe
> someone
> > has some advice on registering call-backs from .dlls.
> >
> > I'm using Windows 7 and Python 2.6 (32-bits). The .DLL was written in
> > C++ is working with C++ apps calling it.
> 
> OK As I understand it, You are trying to register a Python function as
> a call back on a C++ function in a DLL is that correct?
> 
> Do you have any code? I don't see any attachment. Many lists/servers
> strip out binary attachments as potential viruses so it may not have
> made it through. Code is best included inline as text provided its not
> too long, or alternatively, in a pastebin.
> 
> > I tried the methods in section 15.17.1.17 with the qsort() and
> > CFUNCTYPE, but it is not working.  My code and the .dll are attached.
> 
> Section 15.17.1.17 of what? Not the tutorial, not the library reference
> and not the language reference. So what?
> 
> This question may be a tad too complex for the tutor list which is
> targeted to those learning Python, you probably want the main list for
> this. But you are going to have to provide some code and a more
> detailed description of what you are trying to do.
> 
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Registering callbacks and .DLL

2014-10-21 Thread Wilson, Pete
This worked. Yeah! Thank you so much. Pete

> -Original Message-
> From: eryksun [mailto:eryk...@gmail.com]
> Sent: Friday, October 17, 2014 8:53 PM
> To: Wilson, Pete
> Cc: tutor@python.org
> Subject: Re: [Tutor] Registering callbacks and .DLL
> 
> On Thu, Oct 16, 2014 at 6:35 PM, Wilson, Pete 
> wrote:
> >
> > The .DLL was written in C++ is working with C++ apps calling it.
> 
> ctypes doesn't support the platform C++ ABI (I don't think the VC++ ABI
> is even stable), classes, STL containers, or exceptions [*]. It isn't
> "cpptypes". To work with ctypes, a C++ library needs an extern "C"
> interface. The library you're using probably qualifies, but try a
> simple test program in C before jumping into ctypes.
> 
> [*] On Windows ctypes has limited support for Structured Exception
> Handling (SEH), a Microsoft extension of ANSI C. Inadvertently it also
> handles any exception raised by Win32 RaiseException, such as VC++
> exceptions. The code for VC++ exceptions is 0xE06D7363, i.e. "\xE0"
> "msc". ctypes isn't looking for this code and doesn't delve deeper to
> get the C++ exception type, so the OSError it raises is almost useless.
> Pretend this doesn't exist. SEH support is only implemented for the few
> cases ctypes handles explicitly such as access violations.
> 
> > I tried the methods in section 15.17.1.17 with the qsort() and
> > CFUNCTYPE, but it is not working.  My code and the .dll are attached.
> >
> > from ctypes import *
> >
> > pt_dll = cdll.LoadLibrary("c:/py_stuff/ProductionTest.dll")
> 
> You can use CDLL instead. It's fewer keystrokes.
> 
> from ctypes import *
> 
> pt_dll = CDLL("c:/py_stuff/ProductionTest.dll")
> 
> If the functions use the stdcall convention, substitute WinDLL for
> CDLL. If there's a mix of calling conventions you can simply load the
> library twice, once as CDLL and again as WinDLL. They'll each have the
> same _handle attribute.
> 
> You can also define prototypes manually via CFUNCTYPE and WINFUNCTYPE.
> Then instantiate them with a 2-tuple (name_or_ordinal, library), e.g.
> 
> libc = CDLL('msvcr100')
> 
> atoi_t = CFUNCTYPE(c_int, c_char_p)
> atoi = atoi_t(('atoi', libc))
> 
> >>> atoi(b'42')
> 42
> 
> FYI, 64-bit Windows has a single calling convention, so if you switch
> to 64-bit Python you don't have to worry about cdecl vs stdcall.
> 
> http://en.wikipedia.org/wiki/X86_calling_conventions
> 
> > reg_send_serial_data = pt_dll.RegSendSerialData
> >
> > class SendSerialData_t(Structure):
> > _fields_ = [("tx_data", c_void_p),
> >("size", c_uint8)]
> >
> > send_serial_data = SendSerialData_t()
> 
> SendSerialData_t is a function pointer type, not a data structure.
> Here are the C prototypes from the attached PDF:
> 
> typedef void (*SendSerialData_t) (uint8_t *tx_data, uint8_t size);
> 
> void RegSendSerialData(SendSerialData_t SendSerialData);
> 
> A SenedSerialData_t function takes two parameters (uint8_t *, uint8_t)
> and returns nothing (void).
> 
> ctypes declarations:
> 
> SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8)
> 
> reg_send_serial_data = pt_dll.RegSendSerialData
> reg_send_serial_data.argtypes = [SendSerialData_t]
> reg_send_serial_data.restype = None
> 
> The first argument to CFUNCTYPE is the return type. Use None for void.
> 
> Next define the Python callback.
> 
> def send_serial_data(tx_data, size):
> # testing
> print tx_data, size
> print tx_data[:size]
> 
> cb_send_serial_data = SendSerialData_t(send_serial_data)
> 
> Finally, register the callback with the library:
> 
> reg_send_serial_data(cb_send_serial_data)
> 
> It's vital that you keep a reference to cb_send_serial_data (as a
> global, an instance attribute, in a container, etc). This prevents the
> callback from being deallocated while it's possible the library can
> call it. Otherwise at best you'll get an  access violation (or segfault
> on POSIX systems), but probably a less obvious error.
> 
> Next your test code sets up ProdBatVolRequest, which is prototyped as
> follows:
> 
> typedef void (*BatVolReadRequest_cb)(uint16_t bat_vol, uint8_t
> status);
> 
> typedef struct {
> BatVolReadRequest_cb BatVolReadConf;
> } BatVolReadRequest_t;
> 
> void ProdBatVolReadRequest(BatVolReadRequest_t BatVolReadParam);
> 
> ProdB

[Tutor] Passing Data to .DLL

2014-10-22 Thread Wilson, Pete
I am having problems passing a pointer and uint8 to a .DLL. I have been 
successful registering call -backs with this .DLL. So it is all 32-bit and 
ctypes are working. Everything is working up to the line #set-up 
ProcessingIncomingSerialData. I tried the direct approach and then morphing the 
call-back style but yummy_thing is not so yummy... The python code is below..

The definition from the .DLL header is
[cid:image002.png@01CFED51.0C6D7E70]

# Python code starts here.

''' read_bat.py
'''

from serial import *
from string import *
from time import *

system_state = "G2G"

''' --- REMOVED Set-up the COMM port 
print("Enter COM port number")
user_comm_port = raw_input()

try:
dut_serial_port = Serial(port="COM"+user_comm_port, baudrate=115200, 
timeout=1)
except:
system_state = "FUBAR"
print "Serial Port Problem"

try:
dut_serial_port.isOpen()
print("COM" + user_comm_port + " is open")
except:
print "Serial Port Problem"
system_state = "FUBAR"

---'''

#Set-up and register cb_send_serial_data

from ctypes import *

pt_dll = CDLL("c:/py_stuff/ProductionTest.dll")

SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8)

reg_send_serial_data = pt_dll.RegSendSerialData
reg_send_serial_data.argtypes = [SendSerialData_t]
reg_send_serial_data.restype = None


global new_serial_tx_data
global new_serial_size

def send_serial_data(tx_data, size):
# testing
print "tx_data = ", tx_data
print "size = ", size
print "tx_data[:size] = ", tx_data[:size]

global new_serial_tx_data
new_serial_tx_data = tx_data[:size]

global new_serial_size
new_serial_size = size

cb_send_serial_data = SendSerialData_t(send_serial_data)
global cb_send_serial_data

reg_send_serial_data(cb_send_serial_data)

print "reg_send_serial_data done"

#Set-up and register cb_bat_vol_read
#this triggers send_serial_data when it is registered.


BatVolReadRequest_t = CFUNCTYPE(None, c_uint16, c_uint8)

prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest
prod_bat_vol_read_request.argtypes = [BatVolReadRequest_t]
prod_bat_vol_read_request.restype = None

def bat_vol_read(bat_vol, status):
print "bat_vol_read()"
# testing
print bat_vol, status

cb_bat_vol_read = BatVolReadRequest_t(bat_vol_read)

prod_bat_vol_read_request(cb_bat_vol_read)

print "prod_bat_vol_read_request done"


''' REMOVED serial access for example -
#push new_serial_tx_data  out the serial port to the DUT.

print new_serial_tx_data

dut_serial_port.write(new_serial_tx_data)
dut_serial_port.write("\r \n")
sleep(1)


#and check to see the DUT's reply...


global new_serial_rx_data
#new_serial_rx_data =""
global rx_buf
rx_buf = []

try:
string_buf = dut_serial_port.readline()
except:
system_state = "FUBAR"
print "serial read problem"

rx_buf = list(string_buf)
print "string_buf = ", string_buf
print "rx_buf = ", rx_buf

---'''

#set-up ProcessingIncomingSerialData

print "breakpoint"

class rx_data_t:
def _init_(self):
#self.ret = None
self.data = []
self.size = ''

fake_rx_data = rx_data_t()

fake_rx_data.data = ['\x01', '\x05', '\x00', '\x1c', '\x00', '\x99', '\x0c', 
'\x04']
fake_rx_data.size = 8

print "fake_rx_data.data = ", fake_rx_data.data
print "fake_rx_data.size = ", fake_rx_data.size

ProcessIncomingSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint16)

process_incoming_serial_data = pt_dll.ProcessIncomingSerialData
process_incoming_serial_data.argtypes = [ProcessIncomingSerialData_t]
#process_incoming_serial_data.argtypes = [POINTER(c_uint8), c_uint16]
process_incoming_serial_data.restype = None

yummy_thing = ProcessIncomingSerialData_t(fake_rx_data)passing pointers to

process_incoming_serial_data(yummy_thing)
#process_incoming_serial_data(fake_rx_data)

print "Done."
print "system_state = ", system_state
#print "Closing COM port", user_comm_port
#dut_serial_port.close()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Passing Data to .DLL

2014-10-23 Thread Wilson, Pete
It's working!

I don't understand the line
rx_buf = (c_uint8 * rx_buf_size).from_buffer_copy(string_buf)
or where .from_buffer_copy() came from but it works...

I promise I will not knowingly pass Python strings to C. 

Thanks.

> -Original Message-
> From: eryksun [mailto:eryk...@gmail.com]
> Sent: Wednesday, October 22, 2014 4:16 AM
> To: Wilson, Pete
> Cc: tutor@python.org
> Subject: Re: [Tutor] Passing Data to .DLL
> 
> On Tue, Oct 21, 2014 at 7:04 PM, Wilson, Pete 
> wrote:
> >
> > ProcessIncomingSerialData_t = CFUNCTYPE(None, POINTER(c_uint8),
> > c_uint16) process_incoming_serial_data =
> > pt_dll.ProcessIncomingSerialData
> process_incoming_serial_data.argtypes
> > = [ProcessIncomingSerialData_t]
> 
> ProcessIncomingSerialData takes two parameters: uint8_t *rx_buf and
> uint16_t size.
> 
> process_incoming_serial_data.argtypes = [POINTER(c_uint8),
> c_uint16]
> process_incoming_serial_data.restype = None
> 
> The buffer parameter isn't const, so pass a copy of string_buf.
> 
> size = len(string_buf)
> rx_buf = (c_uint8 * size).from_buffer_copy(string_buf)
> process_incoming_serial_data(rx_buf, size)
> 
> I'm going to meander off topic a bit to give you a warning...
> 
> Python strings are immutable by contract, so don't pass them directly
> to C functions that might modify them. String immutability makes
> interning possible, sometimes just locally in the code object and
> sometimes globally in the interpreter. Here's an example of both in
> CPython 2.7:
> 
> import abc
> from ctypes import *
> 
> def test():
> s = 'abc'
> a = cast(s, POINTER(c_char * 3))[0]
> a[:] = 'cba'
> print 'abc'
> 
> >>> test.__code__.co_consts
> (None, 'abc', 3, 0, 'cba')
> 
> Notice that the 2nd constant in co_consts is 'abc'. This gets stored to
> s and later printed. In between I use ctypes to reverse it. So what
> gets printed? If you guessed "cba", you're right.
> 
> >>> test()
> cba
> 
> Look at the constants now:
> 
> >>> test.__code__.co_consts
> (None, 'cba', 3, 0, 'cba')
> 
> So how about referencing the abc module?
> 
> >>> abc
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'abc' is not defined
> 
> OK, but do you think we can use cba instead?
> 
> >>> cba
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'cba' is not defined
> 
> This is yet another problem with mucking with the internal state of an
> immutable object. Equal objects are supposed to hash the same.  'cba'
> is now equal to the interned 'abc' string that I reversed, but it
> probes to a different slot in the dict's hash table.
> 
> >>> test.__code__.co_consts[1] == 'cba'
> True
> >>> hash(test.__code__.co_consts[1]) == hash('cba')
> False
> 
> OTOH, a new 'abc' string probes to the same slot in the hash table, but
> it's no longer equal (i.e. it gets handled as a hash collision).
> 
> >>> test.__code__.co_consts[1] == 'abc'
> False
> >>> hash(test.__code__.co_consts[1]) == hash('abc')
> True
> 
> This breaks dict lookup unless we use the interned string itself:.
> 
> >>> globals()[test.__code__.co_consts[1]]
> 
> 
> So the moral is only pass Python strings to C functions that promise
> (scout's honor) to not modify them. If the parameter isn't const, err
> on the side of caution; copy the string to a ctypes buffer.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Passing Data to .DLL

2014-10-23 Thread Wilson, Pete
No worries, if I could spell I would have been a Lawyer. Pete

> -Original Message-
> From: eryksun [mailto:eryk...@gmail.com]
> Sent: Wednesday, October 22, 2014 5:36 PM
> To: Wilson, Pete
> Cc: tutor@python.org
> Subject: Re: [Tutor] Passing Data to .DLL
> 
> On Wed, Oct 22, 2014 at 6:05 PM, eryksun  wrote:
> > from_buffer_copy is similar, accept instead of sharing the buffer
> 
> That should be ex-cept (conjunction for an exception clause), not ac-
> cept (verb, to receive). I missed that in my initial proofread. It
> takes a while to clear my mental buffer enough for a fresh look.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor