Re: [Tutor] Read active tab url from firefox

2008-08-25 Thread Alan Gauld

xbmuncher [EMAIL PROTECTED] wrote

I want to make a program in python that knows the current url of the 
active
tab in firefox. I want to be able to right click anywhere in my 
system,

notepad, firefox..etc.. and be able to see an added menu option.
Any pointers on doing this?


This is impossible, sorry.
You can get some of it to work, you might even get all of it to work
some of the time, but you can't get all of it to work all of the time.
You simply don't have that much control, nor should you!


Summary of goals:
1. retrieve url of current tab from firefox


That should be possible with some deep digging into the internals
of FireFox's COM model or, if necessary, into its window tree.

2. add right click option that displays anywhere I right click the 
mouse in

windows xp, notepad, a browser, desktop..wherever


This is the tricky bit. Every program has the option of creating
its own right-click menu and it may not expose that in such a
way that you can add items to it. Some older Windows apps
draw their own right click menus bypassing the Windows
widget set and for those you have no chance (or if you do you
will likely destroy the apps own menu system!). Even for those
that use the standard Windows mechanisms it might be tricky.
I think you will need to monitor the running task list and every
time a new task starts your code will have to attach its
menu item to that application. Even then things like X windows
under cygwin has its own menuing system quite separate to
Windows and I doubt it will be possible there without also
writing a cygwin/X version of your code.

Messing around with other people's applications is tricky and
trying to do it in a generic way is trickier still.

3. maybe I want to activate an action in windows when I press some 
key

combinations.. how can I do this with python?


This could be standard windows hot-key functionality.
You can assign hot keys in windows to bring up applications
via the properrties/shortcut dialog.

I don't kow your level of skill with Windows programming
or Python but this may be a lot harder than you think. Its
certainly not a project I'd recommend for a beginner in Python.

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] Python open of c:\ path Problem

2008-08-25 Thread Lie Ryan
 Message: 7
 Date: Sun, 24 Aug 2008 00:21:45 +0100
 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] Python open of c:\ path Problem
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
 reply-type=original
 
 
 Wayne Watson [EMAIL PROTECTED] wrote
 
 junkfile = open('c:\tmp\junkpythonfile','w')
  IOError: [Errno 2] No such file or directory: 
  'c:\tmp\\junkpythonfile'
 
  I suspect the problem is with the back slash. Comments?
 
 Correct. There are several ways round this, the simplest
 being to use forward slashes which are effectively portable
 across most OSs.
 
junkfile = open('c:/tmp/junkpythonfile','w')
 
 You could use a raw string by prefixing the string with r
 
 junkfile = open(r'c:\tmp\junkpythonfile','w')
 
 
 Or you could escape the backslash
 
junkfile = open('c:\\tmp\\junkpythonfile','w')
 
  BTW, how does one continue a long statement
  that has, say, a long path to a file?
 
 You can create a long string by adding the shorter string
 elements :
 
 f = open(
 a:/very/long/path/name/that/needs/a/whole/line/to./itself.py,
 w)
 
 becomes
 
 f = open(a:/very/long/path/name/ +
   that/needs/a/whole/ +
   line/to./itself.py,w)
 
 
 or by using a line continuation character.
 
 f = open(a:/very/long/path/name/ \
   that/needs/a/whole/ \
   line/to./itself.py, w)
 

You don't even need the line continuation character, you can use
implicit line continuation character (since it's inside a parentheses)

f = open(a:/very/long/path/name/
 that/needs/a/whole/
 line/to./itself.py, w)

 HTH,
 

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


Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Eric Abrahamsen
Okay I think I'm onto something, more iterator-related stuff. If I can  
make self.events an iterator, then run a for loop on it, breaking out  
of the loop when the events' date attributes get too high. Then on the  
next run through, that same for loop should pick up where it left off,  
right? Here's my next version of the function. It doesn't quite work  
right: when I test it each child instance receives the correct events,  
but when it passes those events onto its children, they get consumed  
(or something) and are never actually output. To test I'm  
instantiating a Month m, filling it with events, then looping like so:


for w in m:
print w.event_count()
for d in w:
print d.events

w.event_count() produces the right event count, but d.events is always  
empty. If anyone can see what's wrong with this function...


##

def _iter_children(self, child, require_events=False):

Iterate through an object's 'child' items.

If require_events == True, only return children with
events, otherwise return all children.

iterevents = iter(self.events)
while self.sentinel  self.stop:
c = child([], self.sentinel, self.start_attr,  
rolling=self.rolling, counts_only=self.counts_only)

for e in iterevents:
if getattr(e, self.start_attr)  c.stop:
c.events.append(e)
else:
break
self.sentinel += c.dt_range
if not require_events or c.has_events():
# if require_events == True, omit empty children.
yield c



On Aug 25, 2008, at 11:49 AM, Eric Abrahamsen wrote:



On Aug 24, 2008, at 7:20 PM, Kent Johnson wrote:

Forwarding to the list with my reply. Please use Reply All to reply  
to the list.


Grr, sorry, I keep forgetting...




On Sun, Aug 24, 2008 at 1:02 AM, Eric Abrahamsen
[EMAIL PROTECTED] wrote:


On Aug 23, 2008, at 11:22 PM, Kent Johnson wrote:


On Sat, Aug 23, 2008 at 6:47 AM, Eric Abrahamsen
[EMAIL PROTECTED] wrote:


At first I thought the bisect module was the way to go, but it  
is too
tightly tied to integer list indices, and works very awkwardly  
when

bisecting on datetime attributes.


I'm not sure what the problem is with bisect (to find the starting
point). Your list of model elements has integer indices. I think  
you
have to write a __cmp__ method for your model class that compares  
on

the datetime attribute, then it should work. You can also create a
list of (key, model) and sort/search that.
http://www.mail-archive.com/[EMAIL PROTECTED]/msg189443.html


The __cmp__ trick is very nice, and would do it, except I won't  
have access
to the events model classes. I could get bisect to work by feeding  
it a list
comprehension, but making the high and low parameters work  
required integer

indices, which seemed like one half-hack too many...


I don't understand the issue. *All* lists have integer indices. If  
you

make a list of (key, model) pairs, the keys only need to be
comparable, not integers.


The main problem is that I don't have any control over the list of  
models, and all I've got is the name of a datetime attribute to  
filter by. The only way I could get bisect to work was like this:


index = bisect([getattr(x, attr_name) for x in model_list],  
sentinel_date)


But that loops over the whole list, which is what I was trying to  
avoid. And the only way I could provide high and low parameters to  
bisect is by calling event_list.index(event), which doesn't work on  
django querysets. I also experimented with itertools.groupby to  
produce groups of events, but that turned out to be far slower than  
simply looping over the whole event list and extracting events which  
test true for c.start = getattr(event, attr_name)  c.stop.


Ideally I could create a kind of concurrent iterator, that steps  
through children's blocks of time and the object's event list in  
tandem, rather than going over the whole events list once for every  
child produced. Maybe that's not possible... I'm pasting the whole  
function down below, just for the hell of it.


Thanks again,
Eric


def _iter_children(self, child, require_events=False):
   
   Iterate through an object's 'child' items.

   If require_events == True, only return children with
   events, otherwise return all children.
   
   while self.sentinel  self.stop:
   c = child([], self.sentinel, self.start_attr,  
rolling=self.rolling, counts_only=self.counts_only)

   for e in self.events:
   if c.start = getattr(e,self.start_attr)  c.stop:
   c.events.append(e)
   self.sentinel += c.dt_range
   if not require_events or c.has_events():
   yield c
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org

Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Kent Johnson
I'm not following your code very well. I don't understand the
relationship between the first loop and the iter_children() function.

A couple of things that might help:
- Django QuerySets can be qualified with additional tests, so you
could have each of your month/week/etc classes have its own correctly
qualified QuerySet. This will result in one database query for each
event class, and multiple copies of the actual events.
- When you start iterating a QuerySet, it fetches all the model
instances into a list. I think you are trying to use iterators to
prevent this fetch but Django doesnt' work that way. You might as well
just use the list.
- Iterators can't be restarted, I think that is why your latest
iter_children() doesn't work as you expect.

Can you say some more about why you are doing this? Where do all the
initial constraints come from? Do you really have to be so careful to
protect against a 'madman' user? Perhaps you could set a limit on the
number of events you will retrieve and use a count() on the QuerySet
to ensure that not too many records have been fetched. Maybe you
should try a straightforward implementation and when you have
something working you can worry about making it better.

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


[Tutor] editmypage

2008-08-25 Thread Kirk Bailey
I wrote a password protected program to edit webpages via http. thiss iss 
in 2 files, one serves the editing page, one saves the edited page and 
shows the rssults.

http://www.freeholdmarketing.com/EditMyPage.py
http://www.freeholdmarketing.com/EditMyPage2.py

This take a link on the page to edit. This can be a universal link by using 
server side includes such as:
a href=/cgi-bin/EditMyPage.py?!--#echo var=DOCUMENT_URI --Edit 
this page/a


Just a hack to make my life a little simpler. It is free, feel free to rip 
me off.



--


Cheers!
  -Kirk D Bailey

  THINK
 +-+
  .*.| BOX |
  ..*+-+
  *** THINK
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] test

2008-08-25 Thread Kirk Bailey

is my posting getting through?
--


Cheers!
  -Kirk D Bailey

  THINK
 +-+
  .*.| BOX |
  ..*+-+
  *** THINK
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] editmypage

2008-08-25 Thread Kent Johnson
On Mon, Aug 25, 2008 at 12:18 PM, Kirk Bailey [EMAIL PROTECTED] wrote:
 I wrote a password protected program to edit webpages via http. thiss iss in
 2 files, one serves the editing page, one saves the edited page and shows
 the rssults.
 http://www.freeholdmarketing.com/EditMyPage.py
 http://www.freeholdmarketing.com/EditMyPage2.py

You might want to look into wikis like MoinMoin or Trac...

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


Re: [Tutor] test

2008-08-25 Thread W W
yup

On Mon, Aug 25, 2008 at 11:47 AM, Kirk Bailey [EMAIL PROTECTED]wrote:

 is my posting getting through?
 --


 Cheers!
  -Kirk D Bailey

  THINK
 +-+
  .*.| BOX |
  ..*+-+
  *** THINK
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] test

2008-08-25 Thread Robert Berman




Nope.

Kirk Bailey wrote:
is my
posting getting through?
  



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


Re: [Tutor] pass argument into running program *outside* of program

2008-08-25 Thread Ricardo Aráoz

Emile van Sebille wrote:

Lie Ryan wrote:


In a much simpler situation, even a communicating from a plain file
could be enough. In the daemon's program folder, there'll be two files:
input and output. You write to input to instruct the server and read the
response from output. This model is in respect to Unix's philosophy:
make program to handle text streams, because it's the universal
interface.



I've done this and it works well... one thing to watch out for though is 
snagging a file before it's completely written.  Setting up a semaphore 
or pausing to allow the file write to complete once seeing the file 
fixes it adequately.




If instead of an input/output file you use a directory this is easily 
solved. You write your request to a uniquely named file (with .in 
extension) and you read the response out of an equally named file with a 
.out extension. The server polls the directory for new .in files and 
processes them in order of creation date (you can even include a 
priorities scheme coded in the extension (e.g. .in1 to .in9 for the 
different priorities).




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


Re: [Tutor] Python Docs (Was: Reformatting phone number)

2008-08-25 Thread Ricardo Aráoz

Kent Johnson wrote:

On Fri, Aug 22, 2008 at 9:46 AM, Ricardo Aráoz [EMAIL PROTECTED] wrote:


What I find lacking in the docs are a link to a code example for every item
and a See Also link to other related items. With some modules I can't make
sense of the different bits and pieces till I see a code example, and I
think this would be the greatest improvement to the actually very good docs.


You might like Doug Hellman's Python Module of the Week pages. He
gives exhaustive examples of each module.
http://www.doughellmann.com/projects/PyMOTW/

Kent



Thanks Kent, yes, I knew the place. I didn't mean to say that I 
couldn't find code examples, after all google's my friend ;c). But I 
might also google for everything that I can find in the docs. What I 
meant is that the docs could be immensely improved by providing code 
examples IN them.



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


Re: [Tutor] pass argument into running program *outside* of program

2008-08-25 Thread Emile van Sebille

Ricardo Aráoz wrote:

Emile van Sebille wrote:
I've done this and it works well... one thing to watch out for though 
is snagging a file before it's completely written.  Setting up a 
semaphore or pausing to allow the file write to complete once seeing 
the file fixes it adequately.




If instead of an input/output file you use a directory this is easily 
solved. You write your request to a uniquely named file (with .in 
extension) 


But that doesn't solve the problem.  If you have a lot of data to be 
written to the '.in' file you need to wait for the file writes to 
complete before reading the contents on the 'other' side.  File 
existence isn't sufficient.  Perhaps writing to a '.tmp' and renaming it 
to '.in' after completely written might do it without the possibility of 
consuming the partial file before it's ready.



Emile

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


[Tutor] Why does the Hex builtin function in Python return a string ?

2008-08-25 Thread Py Hex
When I run this:

 type(hex(12))
type 'str'

I get a string type back, i.e, '0xC' not 0xC

On the other hand, if I use 0x with data, Python understands it is hex data and 
not a string value.

 e = 0xCD
 type(e)
type 'int'

Why does the Hex builtin function in Python return a string ?  How can I 
convert this string returned by hex builtin function to data with 0x prefixed ? 

Am I missing anything ? Is there a builtin in Python (I'm using Python 2.5) 
that does this conversion from int to hex without returning a string value?

It is a bit confusing. 

Thanks 
Ramses


  

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


[Tutor] Fwd: Why does the Hex builtin function in Python return a string ?

2008-08-25 Thread W W
Forgot to send to list...

On Mon, Aug 25, 2008 at 7:45 PM, Py Hex [EMAIL PROTECTED] wrote:

 When I run this:

  type(hex(12))
 type 'str'

 I get a string type back, i.e, '0xC' not 0xC

 On the other hand, if I use 0x with data, Python understands it is hex data
 and not a string value.

  e = 0xCD
  type(e)
 type 'int'


You missed trying something:

 e = 0xCD
 e
205
 type(e)
type 'int'

It doesn't store the value as hex data, it stores it as an integer.

I'm really not sure about anything else (i.e. converting the value to an
integer - I've tried and int(e) doesn't work when it's a hex string) though.

HTH, Wayne

p.s. After a quick Google, I discovered how to convert the other way:
int('0xCD', 0) will give you the integer value of your string (if it's hex.
If you're doing octal you'll want int(myOctal, 8) )

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does the Hex builtin function in Python return a string ?

2008-08-25 Thread John Fouhy
2008/8/26 Py Hex [EMAIL PROTECTED]:
 When I run this:

 type(hex(12))
 type 'str'

 I get a string type back, i.e, '0xC' not 0xC

 On the other hand, if I use 0x with data, Python understands it is hex data 
 and not a string value.

 e = 0xCD
 type(e)
 type 'int'

 Why does the Hex builtin function in Python return a string ?  How can I 
 convert this string returned by hex builtin function to data with 0x prefixed 
 ?

If you type 0xC, you get a number.  Try it:

 0xC
12

'12' is the default python representation of the integer 0xC.
Internally, it is (I guess) stored as a 4 byte chunk of memory; that
is, a 32-bit long binary.  There is _no difference_ between 0xC and
12:

 0xC is 12
True

The hex() function (and oct() too) provides you with a different
string representation from the default.  If you want to change python
to display integers in hex instead of decimal by default, I can't help
you.. (well, maybe you could subclass int, and change __repr__ and
__str__ to return hex strings)

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


Re: [Tutor] Why does the Hex builtin function in Python return a string ?

2008-08-25 Thread John Fouhy
2008/8/26 John Fouhy [EMAIL PROTECTED]:
 The hex() function (and oct() too) provides you with a different
 string representation from the default.  If you want to change python
 to display integers in hex instead of decimal by default, I can't help
 you.. (well, maybe you could subclass int, and change __repr__ and
 __str__ to return hex strings)

Actually, that was easier than I thought:

class HexInt(int):
def __repr__(self):
return hex(self)
def __str__(self):
return str(self)
def __add__(self, other):
return HexInt(int(self)+int(other))
def __sub__(self, other):
return HexInt(int(self)-int(other))
def __mul__(self, other):
return HexInt(int(self)*int(other))
def __div__(self, other):
return HexInt(int(self)/int(other))

 h1 = HexInt(13)
 h2 = HexInt(21)
 h1, h2
(0xd, 0x15)
 h1+h2
0x22
 h1-h2
-0x8
 h1*h2
0x111
 int(h1*h2)
273
 h1+16
0x1d

Of course, there's obvious problems if you want to mix this with
floats :-/  And I'm not sure what you'd gain, since as mentioned,
integers are integers, whatever they look like.

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


Re: [Tutor] Why does the Hex builtin function in Python return a string ?

2008-08-25 Thread Kent Johnson
On Mon, Aug 25, 2008 at 8:45 PM, Py Hex [EMAIL PROTECTED] wrote:
 When I run this:

 type(hex(12))
 type 'str'

 I get a string type back, i.e, '0xC' not 0xC

 On the other hand, if I use 0x with data, Python understands it is hex data 
 and not a string value.

 e = 0xCD
 type(e)
 type 'int'

 Why does the Hex builtin function in Python return a string ?  How can I 
 convert this string returned by hex builtin function to data with 0x prefixed 
 ?

I think you are confusing representation with value. 0xCD and 205 are
different representations of the same value. The interpreter
understands both but the values are the same. In fact if you ask
Python for the value of e in your example, it will say it is 205.

The base (10 or 16) is a property of the representation, not the
value. The hex() function gives you the string representation of the
value as a (base 16) hex string. The str() function gives the standard
(base 10) string representation of an integer.

Are you just curious or is there something specific you are trying to do?

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


Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-25 Thread Eric Abrahamsen
I do apologize for the large quantities of confusing description –  
articulating the problem here has helped me understand exactly what it  
is I'm after (though it hasn't improved my code!), and I've got a  
better grasp of the problem now than I did when I first asked.


It isn't so much that I need to protect against crazy users, but that  
the pattern I'm making is very flexible, and could be used in vastly  
different ways. I want to make sure that it operates on efficient  
principles, so that people will get the best performance out of it no  
matter how they use it.


So my test case: a Month has a 'child' attribute pointing at Week,  
which has a 'child' attribute pointing at Day, so they all know what  
kind of child instances iteration should produce. With nested loops, a  
Month produces one Week, that Week produces seven Days, then the next  
Week is produced, it makes seven more Days, etc. That much is easy.


Then there's self.events. My original code looped over all of  
self.events for each child produced. A Month loops over its events  
four times, a Week seven times. This was the straightforward  
implementation, but it seemed inefficient. (I also, as you point out,  
might have been wrong about the way django QuerySets work). My thought  
was that only one loop over self.events should be necessary, in  
theory, since they're sorted by date.


A for loop creates an iterator from a sequence and calls next() on it,  
and it creates an entirely new iterator each time you start a new for  
loop: each for loop starts from the beginning of the sequence. But if  
you create your own iterator from the sequence and run a for loop on  
it, then using break to jump out of the for loop should leave the  
iterator just where you left it, since it maintains state. Doing  
another for loop on it (the next time through the date-based while  
loop), should pick up where it left off. That's why the line  
iterevents = iter(self.events) is outside of the while loop: it is  
only created once, and the loops later in the function all make use of  
the same iterator, instead of creating a new one every time.


I'm pretty sure this works in theory, because calling event_count() on  
the Weeks as they come out returns the correct number of events. But,  
for some reason, those events are not making it into the Day children.


I had originally assumed that a QuerySet pulled objects out of the  
database in a rolling fashion – ie iterating on a Month would first  
draw a Week's worth of events from the database, then another Week,  
then two more. But if it loads them all at first access, then I might  
as well just call list() on the QuerySet at object instantiation, and  
save myself some trouble.


I hope that's a little clearer. My central issue is maintaining my  
place in the self.events loop, and only advancing it as far as the  
date-based while loop advances. Whether that's possible or not...



Thanks again,
Eric

On Aug 26, 2008, at 1:12 AM, Kent Johnson wrote:


I'm not following your code very well. I don't understand the
relationship between the first loop and the iter_children() function.

A couple of things that might help:
- Django QuerySets can be qualified with additional tests, so you
could have each of your month/week/etc classes have its own correctly
qualified QuerySet. This will result in one database query for each
event class, and multiple copies of the actual events.
- When you start iterating a QuerySet, it fetches all the model
instances into a list. I think you are trying to use iterators to
prevent this fetch but Django doesnt' work that way. You might as well
just use the list.
- Iterators can't be restarted, I think that is why your latest
iter_children() doesn't work as you expect.

Can you say some more about why you are doing this? Where do all the
initial constraints come from? Do you really have to be so careful to
protect against a 'madman' user? Perhaps you could set a limit on the
number of events you will retrieve and use a count() on the QuerySet
to ensure that not too many records have been fetched. Maybe you
should try a straightforward implementation and when you have
something working you can worry about making it better.

Kent


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