[Tutor] How to programmatically EDIT a python file using an editor of my choice ?

2009-04-12 Thread Dominique
Hi,

I'd like to edit and not run a .py file programmatically.
os.path(filename.py) runs the file using python.
 
What I'd like is to only edit the file using Idle or Notepad++ or Spe...
I could do this by making a new file with a .txt extension.
But in that case, it is the program associated with the .txt extension that
would open the file.
What I'd like is to open the file using an editor of my choice.

Is there an easy way to do this ?

Thanks in advance
Dominique

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


Re: [Tutor] How to programmatically EDIT a python file using an editorof my choice ?

2009-04-12 Thread Alan Gauld


"Dominique"  wrote


What I'd like is to only edit the file using Idle or Notepad++ or Spe...
What I'd like is to open the file using an editor of my choice.


How would you do this non programatically and not from the GUI?
In other words can you do it from an OS command line?

If so put that command in a call to subprocess.Popen.

If not is thre an API to the editor, for example Windows editors 
may have a COM interface that you could access.


But without being specific about the editor and the operating 
system it is hard to be specific about a solution. Generically,
if on Unix, you should probably respect the EDITOR or VISUAL 
environment variables since they reflect the users editor 
preferences so the code should look like:


ed = os.getenv('EDITOR')
vi = os.getenv('VISUAL')
editor = vi or ed
if editor:
  subprocess.Popen().call([editor, filename])

HTH,


--
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


Re: [Tutor] How to programmatically EDIT a python file using an editorof my choice ?

2009-04-12 Thread Alan Gauld

"Dominique"  wrote


I tried several other ways without any success:
subprocess.Popen(args = ["notepad++.exe", filename])
subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", 
filename])


I wonder if it is due to the location of the softwares.


In a way. I think it's because you are using single backslashes.
You need to make the command a raw string by putting an r in front.:

subprocess.Popen([r"C:\Program Files\Notepad++\notepad++.exe", filename])

Otherwise Python gets confused by \n and thinks its a newline character


If not is thre an API to the editor, for example Windows editors
may have a COM interface that you could access.


Do you mean that it is not always possible to open another software from 
python,

notably if the software doesn't have a COM interface ?


You can always open the program but some programs may not take
command line arguments to open a file. Thuis you need to drive them
via COM to effectively use the File->Open menu option from Python.

Fortunately most editors will accept a command line filename so its
only rarely an issue for editors. But things like statistics [packages,
graphics editors etc can be more difficult.


ed = os.getenv('EDITOR')
vi = os.getenv('VISUAL')


os.getenv is always None,


Presumably you don;t have either EDITOR or VISUAL defined.
What if you try

os.getenv('PATH')

That should return something.


which may be correct since I am on Windows ?


No, getenv() works on any OS, although Unix tends to use environment
variables much more than Windows, which favours the registry.

Alan G. 



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


Re: [Tutor] Make beautifulsoup show the data it has an issue with

2009-04-12 Thread Kent Johnson
On Sun, Apr 12, 2009 at 10:21 AM, Sander Sweers  wrote:
> 2009/4/10 Kent Johnson :
>> Or, catch
>> the exception, have the code find out where the error is and display
>> the bad line.
>
> This is what I was looking for. I know how to catch the exception but
> how do I make it display the bad line?

You had:

tsoup = BeautifulSoup(readPage('http://url.sanitized'))

and you got

HTMLParseError: malformed start tag, at line 167, column 73

so try something like this (assuming Python 2.x):

from HTMLParser import HTMLParseError
data = readPage('http://url.sanitized')
try:
  tsoup = BeautifulSoup(data)
except HTMLParseError, ex:
  lines = data.splitlines()
  bad_line = lines[ex.lineno]
  print ex
  print repr(bad_line)  # use repr() so non-printing chars will show

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


Re: [Tutor] How to programmatically EDIT a python file using an editorof my choice ?

2009-04-12 Thread Sander Sweers
2009/4/12 Dominique :
> I tried several other ways without any success:
> subprocess.Popen(args = ["notepad++.exe", filename])
> subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", 
> filename])

Try this:

subprocess.Popen(args = [r'C:\Program Files\Notepad++\notepad++.exe', filename])

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


Re: [Tutor] How to programmatically EDIT a python file usin g an editorof my choice ?

2009-04-12 Thread Dominique
Alan Gauld  btinternet.com> writes:




> 
> How would you do this non programatically and not from the GUI?
> In other words can you do it from an OS command line?

Hello Alan,

Thank you for your response.

I want to do it from a GUI (with wxPython).
I work with Windows XP SP3, python 2.5 and wxPython 2.8.9.2.

subprocess.Popen(args = ["notepad", filename]) works fine.
Same for subprocess.Popen(["C:\Program Files\Editra\Editra.exe", filename]).
It opens the file with notepad or Editra without problems.

subprocess.Popen("notepad") or subprocess.Popen("C:\Program
Files\Editra\Editra.exe") work also fine.

Conversely, subprocess.Popen("notepad++") doesn't work.

If I do subprocess.Popen(args = ["notepad++", filename]), I get an exception.
The exception is as follows:
Traceback (most recent call last):
  File "", line 1, in 
s.Popen(args = ["notepad++.exe", "c:\\Redirect.txt"])
  File "C:\Python25\lib\subprocess.py", line 594, in __init__
errread, errwrite)
  File "C:\Python25\lib\subprocess.py", line 816, in _execute_child
startupinfo)
WindowsError: [Error 2] Le fichier spécifié est introuvable (the specified file
cannot be found)

I tried several other ways without any success:
subprocess.Popen(args = ["notepad++.exe", filename])
subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", filename])

I wonder if it is due to the location of the softwares.
Notepad.exe is located in C:\WINDOWS while notepad++ is located in C:\Program
Files\Notepad++

>If not is thre an API to the editor, for example Windows editors 
>may have a COM interface that you could access.

Do you mean that it is not always possible to open another software from python,
notably if the software doesn't have a COM interface ?

> 
> ed = os.getenv('EDITOR')
> vi = os.getenv('VISUAL')

os.getenv is always None, which may be correct since I am on Windows ?


Thanks a lot  for your help.
Dominique


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


Re: [Tutor] Make beautifulsoup show the data it has an issue with

2009-04-12 Thread Sander Sweers
2009/4/10 Kent Johnson :
> Or, catch
> the exception, have the code find out where the error is and display
> the bad line.

This is what I was looking for. I know how to catch the exception but
how do I make it display the bad line?

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


Re: [Tutor] How to programmatically EDIT a python file usin g an editorof my choice ?

2009-04-12 Thread Dominique

OK. 

With r before the path of the software, it works perfectly.
subprocess.Popen([r"C:\Program Files\Editra\Editra.exe", filename])
I saw that once but I don't remember and cannot find the reason for the use of 
r.

I'll try not to forget to test with and without r in case a file access doesn't
work.

Thanks a lot to both of you Sander and Alan for your help.

Dominique

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


Re: [Tutor] How to programmatically EDIT a python file using an editorof my choice ?

2009-04-12 Thread Sander Sweers
2009/4/12 Dominique :
> With r before the path of the software, it works perfectly.
> subprocess.Popen([r"C:\Program Files\Editra\Editra.exe", filename])
> I saw that once but I don't remember and cannot find the reason for the use 
> of r.

The r means the string is a raw string.

> I'll try not to forget to test with and without r in case a file access 
> doesn't
> work.

The real issue is what Alan pointed out. The backlash is an escape
character and if you want to use it in a string should be escaped by a
backlash.Using a raw string will give the same result as 'C:\\Program
Files\\Editra\\Editra.exe'

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


Re: [Tutor] Make beautifulsoup show the data it has an issue with

2009-04-12 Thread Sander Sweers
2009/4/12 Kent Johnson :
> try:
>  tsoup = BeautifulSoup(data)
> except HTMLParseError, ex:

Aah, ex is the object/class which has the error data. This was the
missing piece for me.

Many thanks Kent!

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


[Tutor] Reading a "Raw "Image File

2009-04-12 Thread Wayne Watson

I can read a raw of 640x480 bytes with:

   raw_file=open('sent_internal.raw')
   raw_image=raw_file.read()

It likely contains a lot of 0 values, black=0. The result above is a
string of 242 characters. I'm guessing the 0's were ignored, and only >0
values were kept. If so, how do I get all 307200 bytes into a read?
Whoops ... Ah, I missed 'rb'; however, I want to continue.

I found the minimum value among the first 242 items to be 31 without the
'rb'. Among the 307200, I found it to be 150, which I believe is
correct. That is, there are pixels in the image that are pretty bright.
So what weirdness took place w/o the 'rb'? Why no 150?

One more question, when I tried (no 'wb') read(size=307200), why di read
balk at size, syntactically.

--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  All the neutrons, and protons in the human body occupy
  a cube whose is 5.52*10**-6 meters. That adds up to a
  150 pound person. It's not a surprise that we are mostly
  space. (Calculation by WTW)




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


Re: [Tutor] Reading a "Raw "Image File

2009-04-12 Thread Alan Gauld


"Wayne Watson"  wrote 


raw_image=raw_file.read()


One more question, when I tried (no 'wb') read(size=307200), 
why di read balk at size, syntactically.


It doesn't balk at the size syntactically it finds what it thinks is 
an End Of file character code in the data and stops reading. 
That's how text files are terminated and why you need to use 
rb to read non text data. 

Having read it as binary its then up to you to interpret the binary 
byte stream in whatever way you need, for example decoding 
it with the struct module or treating it as an image for PIL to 
manipulate or whatever.


HTH,


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/

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


Re: [Tutor] Reading a "Raw "Image File

2009-04-12 Thread Emile van Sebille

Wayne Watson wrote:

I can read a raw of 640x480 bytes with:

   raw_file=open('sent_internal.raw')


 raw_file=open('sent_internal.raw','rb')


   raw_image=raw_file.read()

It likely contains a lot of 0 values, black=0. The result above is a
string of 242 characters. I'm guessing the 0's were ignored, and only >0
values were kept. If so, how do I get all 307200 bytes into a read?
Whoops ... Ah, I missed 'rb'; however, I want to continue.


ah



I found the minimum value among the first 242 items to be 31 without the
'rb'. 


the first 32 characters in ascii are control characters


Among the 307200, I found it to be 150, which I believe is
correct. That is, there are pixels in the image that are pretty bright.
So what weirdness took place w/o the 'rb'? Why no 150?


not sure what this means...



One more question, when I tried (no 'wb') 


assuming you mean 'rb' above

read(size=307200), 
why di read

balk at size, syntactically.



read terminates upon first end on line character encountered

HTH,

Emile


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


Re: [Tutor] Reading a "Raw "Image File

2009-04-12 Thread Wayne Watson
I think I meant why didn't I find the value 150 (a high) in the the 242 
if I had correctly guessed only 242 non-zero pixels were the cause of 
the shrinkage. Instead I found  31 as the high. It makes sense though if 
it shortened to the first CR found. The CR was probably at 243.



Among the 307200, I found it to be 150, which I believe is
correct. That is, there are pixels in the image that are pretty bright.
So what weirdness took place w/o the 'rb'? Why no 150?


not sure what this means...

--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  

  All the neutrons, and protons in the human body occupy 
  a cube whose is 5.52*10**-6 meters. That adds up to a 
  150 pound person. It's not a surprise that we are mostly 
  space. (Calculation by WTW)



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


Re: [Tutor] Using Easygui in Python v3

2009-04-12 Thread Wayne Watson

Possibly contact Steve Ferg?

Alan Gauld wrote:

I'm currently updating my tutorial to cover Python V3.
I thought it miight be good to include some stuff on EasyGUI for the 
user input topic and on checking the web site it said Easygui was 
compatible with Python v3. However after downloading  and installing I 
get a "No module named StringIO" error under Python v3.


Before I start digging deeper I thought I'd ask if anyone else had the 
same problem and knows of a fix?





--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  

  All the neutrons, and protons in the human body occupy 
  a cube whose is 5.52*10**-6 meters. That adds up to a 
  150 pound person. It's not a surprise that we are mostly 
  space. (Calculation by WTW)



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


[Tutor] Tkinter and Antialiasing

2009-04-12 Thread Wayne Watson
If I draw a fairly slanted line across an image using Tkinter, it looks 
a bit jagged. Is this typical of Tkinter graphics, or is there an option 
or mechanism that will antialias?


Win XP, Python 2.5, Tkinter 8.4.

--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  

  All the neutrons, and protons in the human body occupy 
  a cube whose is 5.52*10**-6 meters. That adds up to a 
  150 pound person. It's not a surprise that we are mostly 
  space. (Calculation by WTW)



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


Re: [Tutor] Using Easygui in Python v3

2009-04-12 Thread Alan Gauld


"Wayne Watson"  wrote


Possibly contact Steve Ferg?


Yes I did that and Steve has included my fix in the latest version.

He also kindly gave a plug to my tutorial :-)

I've now included a section on EasyGui in my "Talking to the User"
topic in the Python v3 version.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/

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


Re: [Tutor] Tkinter and Antialiasing

2009-04-12 Thread Alan Gauld


"Wayne Watson"  wrote

If I draw a fairly slanted line across an image using Tkinter, it looks a 
bit jagged. Is this typical of Tkinter graphics, or is there an option or 
mechanism that will antialias?


I think that is a feature of the canvas and its underlying code.
I can't see any anti-aliasing features although you can of course
render it as a smooth line, and mess around with the splines settings,
but I don't think that will help in this case if it's a single straight 
line.


This might be an area where OpenGL or another toolkit might help.
If its part of a graphic then generating the graphic as an image and
loading the image into the canvas might work better.

HTH,


--
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


Re: [Tutor] Tkinter and Antialiasing

2009-04-12 Thread Wayne Watson
I can probably get around it in this case. What I was attempting to do 
is draw radial lines out from a center. Each maybe 15 degrees apart. 
Basically compass style. With the jaggies it looks a bit odd; however, I 
can probably do this with a graphics tool, and then superimpose it on 
the image I planned to use it for. Extra work, but it should do the 
trick. Then again I can probably draw the radial lines from near the 
perimeter to have it look more like a compass, and that'll probably look 
satisfactory. Each line being maybe 10-20 pixels. I tried PILs similar 
draw methods with the same results. Tkinter probably uses them anyway. 
Maybe there's hope when the new PIL is released.


Alan Gauld wrote:


"Wayne Watson"  wrote

If I draw a fairly slanted line across an image using Tkinter, it 
looks a bit jagged. Is this typical of Tkinter graphics, or is there 
an option or mechanism that will antialias?


I think that is a feature of the canvas and its underlying code.
I can't see any anti-aliasing features although you can of course
render it as a smooth line, and mess around with the splines settings,
but I don't think that will help in this case if it's a single 
straight line.


This might be an area where OpenGL or another toolkit might help.
If its part of a graphic then generating the graphic as an image and
loading the image into the canvas might work better.

HTH,




--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  

  All the neutrons, and protons in the human body occupy 
  a cube whose is 5.52*10**-6 meters. That adds up to a 
  150 pound person. It's not a surprise that we are mostly 
  space. (Calculation by WTW)



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


[Tutor] Using numpy Array Capabilities

2009-04-12 Thread Wayne Watson
I'm pretty sure numpy can manipulate arrays, but I'm barely familiar 
with it. Basically, what I would like to do is to take a list like:


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

and convert it to;
[['0x0', '0x1', '0x2', '0x3', '0x4'], ['0x5', '0x6', '0x7', '0x8', '0x9']]

That is, it becomes a matrix of 2 rows and 5 columns. I generated the 
above from a loop in Python, and generated hex equivalents in the last 
list.  Can numpy do this simply without resorting to loops? How would 
one code it?


I think numpy produces an array like object, but it's not necessarily 
the same as Python. However, the numpy arrays are going to be input into 
an object that expects numpy arrays.


--
  Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
 Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet  

  All the neutrons, and protons in the human body occupy 
  a cube whose is 5.52*10**-6 meters. That adds up to a 
  150 pound person. It's not a surprise that we are mostly 
  space. (Calculation by WTW)



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


[Tutor] FW: another newbie

2009-04-12 Thread j mercedes










hi, its me again jay, thanks alot everyone for your input on the last program i 
created that solved for the mode,mean, and average of a set of numbers it 
actually worked great...i used the import in a bit different method but still 
very valuable...if anyone can give me any ideas on this program i am doing now 
it would definetly be appreciated here it goes:import shelveimport 
stringUNKNOWN = 0HOME = 1WORK = 2FAX = 3CELL = 4class phoneentry:def 
__init__(self, name = 'Unknown', number = 'Unknown',type = UNKNOWN):
self.name = nameself.number = numberself.type = type#  create 
string representationdef __repr__(self):return('%s:%d' % ( self.name, 
self.type ))#  fuzzy compare or two itemsdef __cmp__(self, that):this = 
string.lower(str(self))that = string.lower(that)if string.find(this, 
that) >= 0:return(0)return(cmp(this, that))def showtype(self):  
  if self.type == UNKNOWN: return('Unknown')if self.type == HOME: 
return('Home')if self.type == WORK: return('Work')if self.type == FAX: 
return('Fax')if self.type == CELL: return('Cellular')class phonedb:def 
__init__(self, dbname = 'phonedata'):self.dbname = dbname;  
  self.shelve = shelve.open(self.dbname);def __del__(self):
self.shelve.close()self.shelve = Nonedef add(self, name, number, type = 
HOME):e = phoneentry(name, number, type)self.shelve[str(e)] = edef 
lookup(self, string):list = []for key in self.shelve.keys():e = 
self.shelve[key]if cmp(e, string) == 0:list.append(e)
return(list)#  if not being loaded as a module, run a small testif __name__ == 
'__main__':foo = phonedb()foo._add_('Sean Reifschneider', 
'970-555-', HOME)foo.add('Sean Reifschneider', '970-555-', CELL)
foo.add('Evelyn Mitchell', '970-555-', HOME)print 'First lookup:'for entry 
in foo.lookup('reifsch'):print '%-40s %s (%s)' % ( entry.name, 
entry.number, entry.showtype())print 'Second lookup:'for entry in 
foo.lookup('e'):print '%-40s %s (%s)' % ( entry.name, entry.number, 
entry.showtype() )___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] get bytes back from string representation?

2009-04-12 Thread Daniel
hi list,

I am using SUDS to talk with a web service written by C#. The service will
crawl a web page, then return its content as bytes.

it's something like:

>>> from suds.client import Client
>>> url = "http://WSServer/Service1.asmx?wsdl";
>>> client = Client(url)
>>> page = client.service.GetURLContent("http://www.google.cn";)
>>> print page
(CrawlResult){
   crawStatus = "SUCC"
   rawByte = "PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb2
... "

the problem is I need to convert the rawByte from string to bytes, then
explain it as text with encoding (like "UTF-8").

Any simple way to convert this string back to bytes? I am not clear with
that.

Thanks,
Daniel.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to programmatically EDIT a python file usin g an editorof my choice ?

2009-04-12 Thread Dominique
Sander Sweers  gmail.com> writes:

Hello Sander,

> 
> The r means the string is a raw string.
> 
> The real issue is what Alan pointed out. The backlash is an escape
> character and if you want to use it in a string should be escaped by a
> backlash.Using a raw string will give the same result as 'C:\\Program
> Files\\Editra\\Editra.exe'
> 
> Greets
> Sander

OK. I understand.

What surprises me is that before calling the subprocess.Popen() method, I
normalized the path using:
filename = os.path.normpath(filename).

Consequently, I thought the path was corrected from the backslash problems.

Anyway, Thank you very much for your help.

Dominique



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