Receiving Output from os.system command

2009-01-19 Thread K-Dawg
I am really new to python and am trying to learn it to do some projects.  I
wanted to perform a simple task and am having some trouble with it.  I run
linux in a vm on a windows laptop for work.  I have my laptop screen and an
external monitor.  I move my Ubuntu VM back and forth depending on what I am
working on so I wanted to write a little python script that changed the
resolutions depending on what the current resolution is.

I have:

#! /usr/bin/python

import sys
import os
import re

re_currentResolution = re.compile(r'current \d{4}\sx\s\d{3,4}')
xrandr_output = os.popen('xrandr').readlines()
currentRes = re_currentResolution.search(xrandr_output)
print currentRes.group(0)

I just want to grab the xrandr output as a string and parse it with the
regex.  This will give me a string with the current resolution, for instance
"current 1024 x 768".  I would then split that up and call back to the
os.system('xrandr -s 1280x1024').  If the resolution was "current 1280 x
1024" then I would parse that and call back to os.system('xrandr -s
1024x768').

However, when I try the line (as in the code above):

currentRes = re_currentResolution.search(xrandr_output)

it is complaining that xrandr_output is not a string.  (however, if I do a
print xrandr_output it prints fine)

I instead get the following error:

Traceback (most recent call last):
  File "change_res.py", line 9, in 
currentRes = re_currentResolution.search(xrandr_output)
TypeError: expected string or buffer

What am I doing wrong?

Thanks.

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


Re: Receiving Output from os.system command

2009-01-19 Thread K-Dawg
Nevermind, I am an idiot.  I didn't realize what it was returning...

Please disregard.

Thanks.

Kevin


On Mon, Jan 19, 2009 at 2:14 PM, K-Dawg  wrote:

> I am really new to python and am trying to learn it to do some projects.  I
> wanted to perform a simple task and am having some trouble with it.  I run
> linux in a vm on a windows laptop for work.  I have my laptop screen and an
> external monitor.  I move my Ubuntu VM back and forth depending on what I am
> working on so I wanted to write a little python script that changed the
> resolutions depending on what the current resolution is.
>
> I have:
>
> #! /usr/bin/python
>
> import sys
> import os
> import re
>
> re_currentResolution = re.compile(r'current \d{4}\sx\s\d{3,4}')
> xrandr_output = os.popen('xrandr').readlines()
> currentRes = re_currentResolution.search(xrandr_output)
> print currentRes.group(0)
>
> I just want to grab the xrandr output as a string and parse it with the
> regex.  This will give me a string with the current resolution, for instance
> "current 1024 x 768".  I would then split that up and call back to the
> os.system('xrandr -s 1280x1024').  If the resolution was "current 1280 x
> 1024" then I would parse that and call back to os.system('xrandr -s
> 1024x768').
>
> However, when I try the line (as in the code above):
>
> currentRes = re_currentResolution.search(xrandr_output)
>
> it is complaining that xrandr_output is not a string.  (however, if I do a
> print xrandr_output it prints fine)
>
> I instead get the following error:
>
> Traceback (most recent call last):
>   File "change_res.py", line 9, in 
> currentRes = re_currentResolution.search(xrandr_output)
> TypeError: expected string or buffer
>
> What am I doing wrong?
>
> Thanks.
>
> Kevin
>
>
--
http://mail.python.org/mailman/listinfo/python-list


Beginner Question

2009-01-19 Thread K-Dawg
Please forgive my beginner question.  I have used python a little bit,
mainly as a scripting language to perform specific administrative tasks.  I
have trying to learn to use it to develop applications but there are a few
things I do not understand.

I come from more of a Java background.

I do no understand the underscore methods.  __main__  - is this just the
main method that is in the file that is actually executed?  I also see
__init__ a lot.  What is that for?  Is it like a constructor in Java or
totally different?

Thanks for clearing it up.  I am undertaking my first application
development effort in python and anticipate discussing this with all of you
a lot.  :)  Thanks for your support.

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


Overloading Methods

2009-01-20 Thread K-Dawg
Can you overload methods in Python?

Can I have multiple __inits__ with different parameters passed in?

Thanks.

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


Re: Overloading Methods

2009-01-20 Thread K-Dawg
Thank you for the explanation.  With my background in Java, I have to get
myself to think a little differently.
Kevin


On Tue, Jan 20, 2009 at 1:41 PM, Chris Rebert  wrote:

> On Tue, Jan 20, 2009 at 10:18 AM, MRAB  wrote:
> > K-Dawg wrote:
> >>
> >> Can you overload methods in Python?
> >>
> >> Can I have multiple __inits__ with different parameters passed in?
> >>
> > Simple answer: no.
>
> More complicated answer: Yes, with some caveats.
>
> You usually don't need to overload methods in Python since you can use
> default and keyword arguments instead. For instance:
>
> class Foo(object):
>def __init__(self, a, b=10, c=None):
>self.a = a
>self.b = b
>if c is None: c = []
>self.c = c
>
> #example use
> x = Foo("#", 4, [6,7])
> y = Foo("@")
> z = Foo("!", c=[1,2])
>
> Whereas in Java or C++ this would require several overloads, it can be
> succinctly expressed as a single method in Python.
>
> However, if you want the overloads to accept completely different
> types as parameters, then it arguably should expressed as distinct
> methods rather than "overloads". In the special case of __init__, you
> might want to make the alternate initializers classmethods or factory
> functions.
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...
> http://rebertia.com
>  --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Beating a Timeout

2009-01-21 Thread K-Dawg
Hi,

I am trying to write a python script that I can run to prevent a timeout of
webpage.  I have to use a system at work that keeps track of issues.  I use
this a couple of time an hour and it times out after 10 minutes.  The system
is really crummy and it rejects your username and password about 30-40 times
before it takes it.  It is supposed to integrate with Active Directory and I
don't think it does it very well.

So I have this open in Internet Explorer (its all in asp.net so it does not
work in any other browser).  I was hoping to have something simple like:

#c:\Python25\python

import urllib
import urllib2
import time

while True:
 result = urllib.urlopen("the_URL
")
 print "Retrieved Page"
 time.sleep(300)
But this is not working  The code on the page uses a hidden iFrame that
has a javascript file that monitors the time and intiates the timeout.  The
src of the hidden iFrame is what I keep trying to grab.

It is still timing out in IE.  Once it times out in IE I get an error in my
script saying:

IOError:  [Errno socket error] (10060, 'Operation timed out')

The system is terrible but I am stuck with it.  If it doesn't kick me out,
at least its bearable...

Thanks for any help you can provide.

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


Re: Beating a Timeout

2009-01-21 Thread K-Dawg
Also, the actual JS code that does this simply uses a

window.location.reload()

to keep the session active.  So I guess the only thing could be that eash
urllib.urlopen call is seen as a new session?  How can I make it part of the
same session I am using in IE?  Or am I a hundred miles off?

Thanks for any help.

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


Re: Beating a Timeout

2009-01-21 Thread K-Dawg
Sorry about the last mesasge, I accidentally replied directly to the
poster.  So I now have:


br = mechanize.Browser()
br.open("theURL")
while True:
 result = br.reload()
 print result
 print "Retrieved Page"
 time.sleep(300)

But it still does not appear to be working...  Shortly after I got the "Your
session is about to expire" pop up.  I will continue to monitor though.

Is mechanize starting another session separate from my IE?  Is there anyway
to get my python to interact with the IE session if so?

Thanks.

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


Mechanize hanging

2009-01-21 Thread K-Dawg
Hi,

I am trying to use mechanize to connect and log into Yahoo!

Here is my code:

#c:\Python25\python
import re
import urllib
import urllib2
import mechanize

print "print1"
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("https://login.yahoo.com/config/login?";)

It is hanging at the open.  Eventually I have to just kill it and I get
this:

__init__.py
Traceback (most recent call last):
  File "C:\Documents and
Settings\kholleran\workspace\PythonPractice\src\PythonP
ractice\__init__.py", line 10, in 
br.open("https://login.yahoo.com/config/login?";)
  File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_mecha
nize.py", line 206, in open
  File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_mecha
nize.py", line 232, in _mech_open
  File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_opene
r.py", line 192, in open
  File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_http.
py", line 570, in http_response
KeyboardInterrupt

Which i am sure is not very helpful but I don't want to leave anything out.

Thanks for any help.

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


Python Style Question

2009-01-22 Thread K-Dawg
I am trying to become more pythonic as I learn python and get my mind around
it instead of other languages I have used.

I have an app that has a series of classes for objects it uses.  From a
style perspective, which should be done:

Different py file for each class

or

One py file with all the classes

The classes are small with a constructor and a few methods, no more than a
couple, some with just one other method.

Which is more "pythonic"?

Thanks.

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


Re: Refreshing an IE Webpage

2009-02-02 Thread K-Dawg
I have also tried to do this with mechanize:

import mechanize
import time

br = mechanize.Browser()
br.open("URL")
while True:
 br.reload()
 time.sleep(300)

After a bunch of time, I get the following error:
C:\>SDE_KeepAlive-v2.py
Traceback (most recent call last):
 File "C:\SDE_KeepAlive-v2.py",
line 1
1, in 

 File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_mecha
nize.py", line 345, in reload
 File
"C:\Python25\lib\site-packages\mechanize-0.1.9-py2.5.egg\mechanize\_mecha
nize.py", line 257, in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 302: The HTTP server
retu
rned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found




And on my first code, trying to use the Win32 api, I get the following
error:

C:\> SDE_Keep_Alive.py
Traceback (most recent call last):
 File "C:\SDE_Keep_Alive.py",
line 8,
in 
   id=ie.Document.Script._oleobj_.GetIDsOfNames('window.location.reload')
pywintypes.com_error: (-2147352570, 'Unknown name.', None, None)



All I want to do is grab a page and refresh it every 5 minutes.

Thanks.

--
Kevin



On Mon, Feb 2, 2009 at 9:14 AM, K-Dawg  wrote:

> Hi,
>
> I am trying to get and then refresh a page in IE with a Python script.  I
> just want to get a page, then refresh it every 5 minutes.  Below is the code
> I am attempting.  It is erroring out on the
> id=ie.Document.Script._oleobj_.GetIDsOfNames('window.location.reload()')
> #tried with and without parens - reload & reload()
> line.  This is mostly a borrowed script I found.  The script was trying to
> call a written function on the page.  I just want to call the build in
> reload function to refresh the page.
>
> Thanks for any help.
>
> Kevin
>
>
> import win32com.client, pythoncom
> from time import sleep
>
> ie=win32com.client.Dispatch('internetexplorer.application')
> ie.Visible=1
> ie.Navigate('URL')
> sleep(5)
>
> id=ie.Document.Script._oleobj_.GetIDsOfNames('window.location.reload()')
> while True:
> res=ie.Document.Script._oleobj_.Invoke(id, 0,
> pythoncom.DISPATCH_METHOD, True)
> sleep(300)
>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Refreshing an IE Webpage

2009-02-02 Thread K-Dawg
Please disregard... I was making it harder than it had to be I think.

The following seems to be running fine.  Whether its doing what I want I
will know in a little bit if the page in my browser times out


import win32com.client, pythoncom
from time import sleep

ie=win32com.client.Dispatch('internetexplorer.application')

> ie.Visible=0
> ie.Navigate('URL')
> sleep(5)
>
> while True:
> sleep(240)

ie.Refresh()

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


Refreshing an IE Webpage

2009-02-02 Thread K-Dawg
Hi,

I am trying to get and then refresh a page in IE with a Python script.  I
just want to get a page, then refresh it every 5 minutes.  Below is the code
I am attempting.  It is erroring out on the
id=ie.Document.Script._oleobj_.GetIDsOfNames('window.location.reload()')
#tried with and without parens - reload & reload()
line.  This is mostly a borrowed script I found.  The script was trying to
call a written function on the page.  I just want to call the build in
reload function to refresh the page.

Thanks for any help.

Kevin


import win32com.client, pythoncom
from time import sleep

ie=win32com.client.Dispatch('internetexplorer.application')
ie.Visible=1
ie.Navigate('URL')
sleep(5)

id=ie.Document.Script._oleobj_.GetIDsOfNames('window.location.reload()')
while True:
res=ie.Document.Script._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_METHOD,
True)
sleep(300)
--
http://mail.python.org/mailman/listinfo/python-list


WIn32api

2009-02-06 Thread K-Dawg
Hi,

I have a python script that I want to run in the system tray and system tray
only (windows system).  I am looking at the win32gui_taskbar.py demo file
but am having trouble making sense of what parts do.  I understand what the
whole does but I want to actually learn what it is doing so I can apply it,
not just copy and paste parts to make it work the way I want.

I have searched for some kind of explanation or tutorial on this and have
come up empty.  Is there anything out there?

Thanks.

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


Re: WIn32api

2009-02-06 Thread K-Dawg
I have come up with what I need and will try tweaking some things that
hopefully will help me learn what some of this stuff does.  In the meantime,
I am having an issue:


class *KeepAlive*(threading.Thread):

def *__init__*(*self*):

*self*.count = 0

*self*.ie=win32com.client.Dispatch(*'internetexplorer.application'*)

*self*.ie.Visible=0

threading.Thread.__init__(*self*)

def *run*(*self*):

*self*.ie.Navigate(URL)

sleep(5)

while True:

sleep(1)

*self*.count += 1

*self*.timeLeft = str((300 - *self*.count)/60) + *":"* + str((
300 - *self*.count)%60) + *" until next refresh"
*

print *self*.timeLeft

if *self*.count == 300:

*self*.ie.Refresh()

*self*.count = 0



This works if I call run() specifically.  But when I try to initiate the
thread with .start() I get the following error

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
  File "C:\app.py", line 104, in run
self.ie.Navigate(URL)
  File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 500,
in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: internetexplorer.application.Navigate

What does this mean?  Why is it only happening when I am trying to do this
in a thread?

Thanks for any help.



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


URGENT! Changing IE PAC Settings with Python

2009-05-20 Thread K-Dawg
Hello,

Thanks for any response.  I am in a crisis where one of our networking guys
moved where our PAC file is housed.  There was a group policy set in Active
Directory that set the PAC file location in Internet Explorer to the new
location.

However, we have 100 remote centers that have about 3 to 4 machines that are
not on AD (from before an AD migration).  These machines did not receive the
update.

Is there a way I can write a python script to run from my machine to jump
out to a list
--
Kevin Holleran
Master of Science, Computer Information Systems
Grand Valley State University
Master of Business Administration
Western Michigan University
Completion December 2009
CCNA, ISA, MCSA, MCDST, MCP

"We are what we repeatedly do. Excellence, then, is not an act, but a
habit." - Aristotle

"A man flattened by an opponent can get up again. A man flattened by
conformity stays down for good. " - Thomas J. Watson
-- 
http://mail.python.org/mailman/listinfo/python-list


Urllib2 proxy settings

2009-06-01 Thread K-Dawg
Hello,

I am having trouble with an application running on a linux server.  It keeps
reverting to old proxy settings and messing up the web application.  I have
checked everything I can think of, and since the application is written in
Python and uses urllib to call a web service (which is where the error is
occurring) I thought that I could open up the interpreter, import URLLib2
and then see what it thinks the proxy is.

How can I just echo this in the interpreter?

Thanks.

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


Re: Distributing Python App

2008-07-18 Thread K-Dawg
On Fri, Jul 18, 2008 at 3:43 PM, Stephen Johnson <[EMAIL PROTECTED]> wrote:

> Look up py2app and py2exe, for OS X and Windows, respectively. They are
> extensively documented and easy to use.
> On Jul 18, 2008, at 3:35 PM, KDawg44 wrote:
>
> Hi,
>
> I am very new to Python but find it very interesting (from what I know
> of it) and am considering writing an application.  However, I have a
> question regarding distribution of the app once it is done.  Is there
> a way to package everything a user needs to run the app or will they
> have to install Python their machine?  This is probably an obvious
> question but most development I do is for administrative tasks on
> servers and such, not usually development of an app for someone else.
>
> Thanks for your help.
>
> Kevin
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
Thanks!  I'll look into that.

-- 
Kevin

"We are what we repeatedly do. Excellence, then, is not an act, but a
habit."
- Aristotle."
--
http://mail.python.org/mailman/listinfo/python-list