Python on Windows

2012-10-16 Thread graham


Downloaded and installed Python 2.7.3 for windows (an XP machine).

Entered the Python interactive interpreter/command line and typed the 
following:


>>>import feedparser

and I get the error message "No module named feedparser".

There is a feedparser.py file lurking around - so I suppose Python 
cannot find it.


Anyone: What to do?



GC

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


Re: Python on Windows

2012-10-19 Thread graham

On 16/10/2012 12:29, graham wrote:


Downloaded and installed Python 2.7.3 for windows (an XP machine).

Entered the Python interactive interpreter/command line and typed the
following:

 >>>import feedparser

and I get the error message "No module named feedparser".

There is a feedparser.py file lurking around - so I suppose Python
cannot find it.

Anyone: What to do?



GC



Thanks to everyone who replied.

Python was installed in the subdirectory C:\Python27 with the file 
feedparser.py residing in C:\Python27\Lib\email.


Setting the Windows environment variable (which did not previously 
exist) to C:\Python27\Lib\email allowed me to import feedparser 
successfully.


However, it seems that this feedparser module is not the module I wanted.

I'm trying to follow an introductory Python course from the magazine 
Linux Format (issue number 120 I think). The article includes the 
following lines:



import feedparser
url = “http://weather.yahooapis.com/forecastrss?p=UKXX0637&u=c”
data = feedparser.parse(url)


This is fine using Ubuntu (after installing the feedparser package) but 
now, running XP I get



data = feedparser.parse(url)

Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'parse'



So there seems to be at least 2 feedparser modules - the one I have does 
not include "parse". How can I identify the correct one? How do I


This is all confusing and frustrating.

Some searching suggests I need the 'universal feed parser' code. I can 
find documentation for this but no code/module file. Is it available 
only for Unix-like OS's?


GC






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


Re: Python on Windows

2012-10-19 Thread graham

On 19/10/2012 14:24, graham wrote:

On 16/10/2012 12:29, graham wrote:


Downloaded and installed Python 2.7.3 for windows (an XP machine).

Entered the Python interactive interpreter/command line and typed the
following:

 >>>import feedparser

and I get the error message "No module named feedparser".

There is a feedparser.py file lurking around - so I suppose Python
cannot find it.

Anyone: What to do?



GC



Thanks to everyone who replied.

Python was installed in the subdirectory C:\Python27 with the file
feedparser.py residing in C:\Python27\Lib\email.

Setting the Windows environment variable (which did not previously
exist) to C:\Python27\Lib\email allowed me to import feedparser
successfully.

However, it seems that this feedparser module is not the module I wanted.

I'm trying to follow an introductory Python course from the magazine
Linux Format (issue number 120 I think). The article includes the
following lines:


import feedparser
url = “http://weather.yahooapis.com/forecastrss?p=UKXX0637&u=c”
data = feedparser.parse(url)


This is fine using Ubuntu (after installing the feedparser package) but
now, running XP I get


data = feedparser.parse(url)

Traceback (most recent call last):
   File "", line 1, in 
AttributeError: 'module' object has no attribute 'parse'



So there seems to be at least 2 feedparser modules - the one I have does
not include "parse". How can I identify the correct one? How do I

This is all confusing and frustrating.

Some searching suggests I need the 'universal feed parser' code. I can
find documentation for this but no code/module file. Is it available
only for Unix-like OS's?

GC




Once again thanks to those that replied.

Since I posted, I found a version 4.1 on sourceforge.

I guessed/muddled my way through installation and it seems to work.

To Tom Golden,
  Thanks for the link to http://pypi.python.org/pypi/feedparser/ - I 
couldn't find it and I don't know how you did.


The version at http://pypi.python.org/pypi/feedparser/ is 5.1. Can I 
simply install 'over the top' of the previous one or do I need to 
uninstall V4.1?



GC







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


Re: how can I run python interactively?

2005-11-02 Thread Graham
You could use either:

s = raw_input("Please Hit a Key...")

s being the string they typed before hitting enter.

or  you could use

   print "Please hit a key..."
   s = sys.stdin.readline()

the only reason i recommend the second is because i believe i saw in a
PEP that raw_input was being depreciated at some point, however i may
be wrong on that. (correction anyone?)

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


Re: how to write a blog system with Python

2005-11-02 Thread Graham
Are you fresh to Python or just to this group?

There are quite a few python frameworks that provide functionality for
blogs.
You'll just have to decide which one you want to try out.

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


Class Variable Access and Assignment

2005-11-03 Thread Graham
This has to do with class variables and instances variables.

Given the following:



class _class:
var = 0
#rest of the class

instance_b = _class()

_class.var=5

print instance_b.var # -> 5
print _class.var # -> 5



Initially this seems to make sense, note the difference between to last
two lines, one is refering to the class variable 'var' via the class
while the other  refers to it via an instance.

However if one attempts the following:



instance_b.var = 1000 # -> _class.var = 5
_class.var =  # -> _class.var = 



An obvious error occurs. When attempting to assign the class variable
via the instance it instead creates a new entry in that instance's
__dict__ and gives it the value. While this is allowed because of
pythons ability to dynamically add attributes to a instance however it
seems incorrect to have different behavior for different operations.

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.

Many thanks to elpargo and coke. elpargo assisted in fleshing out the
best way to present this.

perhaps this was intended, i was just wondering if anyone else had
noticed it, and if so what form would you consider to be 'proper'
either referring to class variables via the class itself or via
instances of that class. Any response would be greatly appreciated.


Graham

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


Class Variable Access and Assignment

2005-11-03 Thread Graham
This has to do with class variables and instances variables.

Given the following:



class _class:
var = 0
#rest of the class

instance_b = _class()

_class.var=5

print instance_b.var # -> 5
print _class.var # -> 5



Initially this seems to make sense, note the difference between to last
two lines, one is refering to the class variable 'var' via the class
while the other  refers to it via an instance.

However if one attempts the following:



instance_b.var = 1000 # -> _class.var = 5
_class.var =  # -> _class.var = 



An obvious error occurs. When attempting to assign the class variable
via the instance it instead creates a new entry in that instance's
__dict__ and gives it the value. While this is allowed because of
pythons ability to dynamically add attributes to a instance however it
seems incorrect to have different behavior for different operations.

There are two possible fixes, either by prohibiting instance variables
with the same name as class variables, which would allow any reference
to an instance of the class assign/read the value of the variable. Or
to only allow class variables to be accessed via the class name itself.

Many thanks to elpargo and coke. elpargo assisted in fleshing out the
best way to present this.

perhaps this was intended, i was just wondering if anyone else had
noticed it, and if so what form would you consider to be 'proper'
either referring to class variables via the class itself or via
instances of that class. Any response would be greatly appreciated.


Graham

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


Re: Class Variable Access and Assignment

2005-11-03 Thread Graham
Thanks to all of you for your reply's i had no idea it would get this
sort of response,
i've read through most of the posts and it seems that its a active
topic.

My question remains however, i suppose i'm not familiar with how this
functions in
other languages, but what is the common way of referring to a class
variable.

is . the norm?
or . the norm.

I just seems to me that . shouldn't defer to the class
variable if
an instance variable of the same name does not exists, it should, at
least how i
understand it raise an exception.

Is my thinking way off here?

Graham

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


Re: Class Variable Access and Assignment

2005-11-03 Thread Graham
Many thanks your explaination cleared up many of the questions I had.
I know think i can understand the purpose, regardless of my opinion, i
do however think that one should be able to assign the value in the
same way it is accessed.
Given your previous example:

> class Counter(object):
>  "A mutable counter."
>  # implementation elided

> class A(object):
>  instance_count = Counter()
>  def __init__(self):
>  self.instance_count.increment()


if you changed
> class A(object):
>  instance_count = Counter()
>  def __init__(self):
>  self.instance_count.increment()

to

> class A(object):
>  instance_count = 0
>  def __init__(self):
>  self.instance_count = self.instance_count + 1

It would not work as planned. I understand all the reasons why this
occurs, but i dont understand why its implemented this way. Because it
acts in a different way than you expect. It seems to me that
self.instance_count should not create a new entry in the __dict__ if a
class variable of that name is already present anywhere in that objects
hierarchy.

Does that make sense?

Again thank you for explaination.

graham

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


Re: Class Variable Access and Assignment

2005-11-03 Thread Graham
Once again, many thanks, your explainations are very detailed and i
think i'm in full understanding of the what/when/why of it all.

And with further introspection i can see why its done this way from a
language processing point of view rather than programming one. I also
now realize that . is there so that you dont
have to type .__class__. all the time.

Once again many thanks, i hadn't expected nearly this type of response.


Graham.

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


Selective HTML doc generation

2005-02-23 Thread Graham
Hi. I'm looking for a documentation generation tool (such as pydoc,
epydoc, happydoc, etc.) that will allow me to filter what it includes
in
it's output.

I only want the reader to know about classes and methods in my package
if if the classes have docstrings. I've got a large package that is
used
by relatively non-technical users (and yet they write Python!) that
documents the public API with docstrings. I don't want to clutter their
view of the world with links to the package's internal classes and
documentation that covers things like __special__ methods.

Anybody know of anything that let's you do it? I realise I may end up
doing some hacking here but don't want to repeat work unnecessarily.

Cheers,

Graham

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


MS SQL Server/ODBC package for Python

2005-04-15 Thread Graham
I need a SQL Server or ODBC package for Python.  Can anyone help please?
Have search the Python packages under Database and there is no reference to
either SQL Server or ODBC.

Thanks
Graham


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


Re: MS SQL Server/ODBC package for Python

2005-04-15 Thread Graham
Thanks Simon
I have just installed mxODBC and tried one of their samples.
Am getting an error on trying to import mx.ODBC

ImportError: No module named mx.ODBC

After the install the mx folder has been place in my C:\Program Files\Plone
2\Python\lib\site-packages folder however when I browse the PythonPath and
look under Standard Python Library I can't see mx.

Any ideas what I mnay have done wrong?

many thanks
Graham


"Simon Brunning" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
On 4/15/05, Graham <[EMAIL PROTECTED]> wrote:
> I need a SQL Server or ODBC package for Python.  Can anyone help please?
> Have search the Python packages under Database and there is no reference
to
> either SQL Server or ODBC.

Top of the line is probably mxODBC
(<http://www.egenix.com/files/python/mxODBC.html>). It's the Rolls
Royce of DB-API modules. It's not free for commercial use, but it's
worth every penny/cent/groat/whatever.

For commercial work where I've not been able to justify spending money
(in my bosses opinion at least), adodbapi
(<http://adodbapi.sourceforge.net/>) works a treat.

--
Cheers,
Simon B,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/


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


Re: Clearing the screen

2006-02-15 Thread Graham
you could always use ANSI escape codes:

print "\\033[2J"

for a screen clear, or

print "\\022[2j \033[0;0H"

to clear and reset the way "os.system('clear')" would work.

check out http://www.termsys.demon.co.uk/vtansi.htm

Seems like all that mud programming came in handy after all.

Graham.

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


Safe Python Execution

2006-02-15 Thread Graham
I've been messing around with trying to get a small sandbox like
environment where i could execute python code in a "safe" way.
Basically what the old restricted execution module attempted to do.
I've written a small amount of code to get custom interpreter running,
but i'm not really sure if its safe.

The way i'm controlling functionality is with some games and exec, so
if 'code' was the text code you wanted to execute i run:

exec code in {'__builtins__':None"}

obviously this doesn't give you much to play with, but it does remove
file access and importing as far as i can tell. Can anyone think of a
hack around this? I assume if it was this easy it would be a module
already but i figured i would ask.

Graham.

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


Issues with python/libtcod

2012-11-07 Thread Graham Fielding




Hey, all. I'm trying to program a roguelike, using the wonderful tutorials 
written by João F. Henriques (a.k.a. Jotaf), but I've stumbled onto a bit of a 
problem setting up the game's inventory system, and I was hoping someone could 
help me out. Here's a code snippet, including the affected line. #def 
menu(header, options, width):
#if len(options) > 26: raise ValueError('Cannot have a menu with more than 
26 options.')
 
#calculate total height for the header (after auto-wrap) and one line per 
option
#header_height = libtcod.console_height_left_rect(con, 0, 0, width, 
SCREEN_HEIGHT, header)
#height = len(options) + header_height
In a nutshell: def menu() is an all-encompassing 'menu processor' function -- 
it contains a standardized menu layout that can be called, from anywhere in the 
stack, to create a generic menu. When I launch the game to check things out, 
everything runs smoothly until I try to open the inventory window -- at which 
point, IDLE  tosses up an AttributeError:
File "C:/Python Project/Roguelike.py", line 602, in 
player_action = handle_keys()
  File "C:/Python Project/Roguelike.py", line 531, in handle_keys
chosen_item = inventory_menu('Press the key next to an item to use it, or 
any other to cancel.\n')
  File "C:/Python Project/Roguelike.py", line 487, in inventory_menu
index = menu(header, options, INVENTORY_WIDTH)
  File "C:/Python Project/Roguelike.py", line 447, in menu
header_height = libtcod.console_height_left_rect(con, 0, 0, width, 
SCREEN_HEIGHT, header)
AttributeError: 'module' object has no attribute 'console_height_left_rect' 
I've tried moving the entire def menu() function to various sections of the 
stack, I've changed its arguments manually, and I've even removed the affected 
line entirely to see if that would help; nothing seems to work, and it's only 
when def menu() is called that this happens. Anyone got any ideas what could be 
going flooey?-- 
http://mail.python.org/mailman/listinfo/python-list


Writing game-state data...

2012-11-08 Thread Graham Fielding




Hey, folks, me again! I've been puzzling over this for a while now: I'm trying 
to write data to a file to save the state of my game using the following 
function: def save_game():
#open a new empty shelve (possibly overwriting an old one) to write the 
game data
file_object = open('savegame.sav', 'wb')
file['map'] = map
file['objects'] = objects
file['player_index'] = objects.index(player)  #index of player in objects 
list
file['inventory'] = inventory
file['game_msgs'] = game_msgs
file['game_state'] = game_state
file['stairs_index'] = objects.index(stairs)
file['dungeon_level'] = dungeon_level
file.close() However, while 'savegame.sav' is created in the directory I 
specify, the function dies on file['map'] = map.  This is the end of the stack 
trace: 
  File "C:\Python Project\Roguelike.py", line 966, in save_game
file['map'] = map
TypeError: 'type' object does not support item assignment Now, the map is 
randomly generated -- could that be an issue? Should I just scrap the current 
system and use pickle?-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Writing game-state data...

2012-11-09 Thread Graham Fielding


 > To: python-list@python.org
> From: breamore...@yahoo.co.uk
> Subject: Re: Writing game-state data...
> Date: Fri, 9 Nov 2012 07:37:56 +
> 
> On 09/11/2012 07:20, Graham Fielding wrote:
> >
> > Hey, folks, me again! I've been puzzling over this for a while now: I'm 
> > trying to write data to a file to save the state of my game using the 
> > following function: def save_game():
> >  #open a new empty shelve (possibly overwriting an old one) to write 
> > the game data
> >  file_object = open('savegame.sav', 'wb')
> >  file['map'] = map
> >  file['objects'] = objects
> >  file['player_index'] = objects.index(player)  #index of player in 
> > objects list
> >  file['inventory'] = inventory
> >  file['game_msgs'] = game_msgs
> >  file['game_state'] = game_state
> >  file['stairs_index'] = objects.index(stairs)
> >  file['dungeon_level'] = dungeon_level
> >  file.close() However, while 'savegame.sav' is created in the directory 
> > I specify, the function dies on file['map'] = map.  This is the end of the 
> > stack trace:
> >File "C:\Python Project\Roguelike.py", line 966, in save_game
> >  file['map'] = map
> > TypeError: 'type' object does not support item assignment Now, the map is 
> > randomly generated -- could that be an issue? Should I just scrap the 
> > current system and use pickle?  
> >
> 
> Please always give the complete stack trace, it's provided for a 
> purpose.  Here I'll grope around in the dark and guess that you need 
> file_object = shelve.open(...
> 
> -- 
> Cheers.
> 
> Mark Lawrence.
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list Here's the full stack 
> trace:   File "C:\Python Project\Roguelike.py", line 1048, in 
main_menu()
  File "C:\Python Project\Roguelike.py", line 1030, in main_menu
play_game()
  File "C:\Python Project\Roguelike.py", line 1007, in play_game
player_action = handle_keys()
  File "C:\Python Project\Roguelike.py", line 717, in handle_keys
quit_menu() #exit game
  File "C:\Python Project\Roguelike.py", line 698, in quit_menu
save_game()
  File "C:\Python Project\Roguelike.py", line 909, in save_game
file['map'] = map
TypeError: 'type' object does not support item assignment
>>>  What I'm trying to do is figure out a way to get the game to save its 
>>> state; I compiled it to an EXE using py2exe, but after that, the game 
>>> refuses to hold onto its data. Thanks for all the help! 
  -- 
http://mail.python.org/mailman/listinfo/python-list


Getting a seeded value from a list

2012-11-18 Thread Graham Fielding




Hello!  Clueless noob again! :) This time around, I'm trying to figure out the 
random.seed() function -- specifically, I'd like to retrieve the seeded values 
from a list (which I've called levelSeed), and use them in a random-dungeon 
generator.  The numbers are generating and storing properly, but I can't find 
any examples online that aren't specific to a particular purpose (such as 
mathematics, arrays, and the like). Would I use 'getstate' when the dungeon is 
generated, or is there another method that would be better suited to the task?  
 -- 
http://mail.python.org/mailman/listinfo/python-list


RE: Getting a seeded value from a list

2012-11-24 Thread Graham Fielding

Just a quick update: after enlisting some help from a friend, we managed to get 
things working :)  Thanks for all the help. In the area of items and monsters, 
they're both chosen from lists, based on some simple selection criteria (number 
per room, total per level, chance of appearance).  Now, the major task is to 
reconfigure the distribution system to let items and monsters stay in place 
between levels.
 > Date: Sat, 24 Nov 2012 09:32:46 +1100
> Subject: Re: Getting a seeded value from a list
> From: ros...@gmail.com
> To: python-list@python.org
> 
> On Sat, Nov 24, 2012 at 3:27 AM, Prasad, Ramit
>  wrote:
> > Steven D'Aprano wrote:
> >>
> >> On Wed, 21 Nov 2012 14:41:24 +1100, Chris Angelico wrote:
> >>
> >> > However, this still means that the player will see the exact same level
> >> > regenerated every time, absolutely fresh. As previously stated in this
> >> > thread, that's not usually a good thing for encounters, treasure, etc.
> >> > Once some nasty critter has been killed, he should STAY killed! :)
> >>
> >> Why? That isn't true in real life, why should it be true for games?
> >>
> >
> > It is not true in all games. I have seen games where treasures
> > regenerate in the same location except for key items. Same goes
> > for enemies (where only "bosses" do not regenerate). It really
> > just depends on the type of game you are playing--designing
> > in this case.
> 
> Perhaps they regenerate, but do they regenerate from the exact same
> random seed? For instance, in Murkon's Refuge, the maps are
> handcrafted and thus constant every time you enter a particular level
> - but go downstairs and upstairs, and the monsters and treasure
> regenerate, different from last time.
> 
> Of course, if the idea is that you're rewinding time, then it makes
> good sense for you to see the exact same pattern of enemies.
> 
> ChrisA
> -- 
> http://mail.python.org/mailman/listinfo/python-list
  -- 
http://mail.python.org/mailman/listinfo/python-list


RE: Is it bad style to override the built-in function `type`?

2012-11-24 Thread Graham Fielding

(This comes from my experience writing interactive fiction with the TADS 
workbench; YMMV) As a rule, it's generally unwise to modify built-in variables 
without thoroughly documenting your changes.  Your modifications might not hold 
up if the built-in definitions are revised (which would break your program) and 
you could introduce later version/compatibility issues with other things that 
use/need the 'standard' definitions. That said, if done cautiously, modifying 
the built-ins can have a net benefit; just be careful to indicate what you've 
changed so you can debug it properly, and -- if possible -- distribute yoru 
modified code along with a copy of the original, so that if issues arise, your 
users can restore the stable code without digging around in the guts of the 
software.
> > 
> > [...]
> > 
> > | I know it's a common beginner's mistake to incautiously override
> > 
> > | built-in functions. However, we put in a lot of research and have come to
> > 
> > | the conclusion that, if Python had not already defined it, `type` would
> > 
> > | be the best name. We are now trying to evaluate how bad the disadvantages
> > 
> > | you mention are in comparison to the advantage to having a name that is
> > 
> > | more intuitive to use in the problem domain.
> > 
> > | 
> > 
> > | Can you somehow relate to my explanations, or are your experiences
> > 
> > | with overwriting built-in variables so bad that you would advise to
> > 
> > | never ever do it?
> > 
> > 
> > 
> > My own experience says that it is a thing best avoiding without a truly
> > 
> > amazing reason not to.
> > 
> > 
> > 
> > I urge you not to: type(foo) is a very basic Python idiom and you're
> > 
> > breaking it. One day it _will_ bite you or your users. You will
> > 
> > understand, but I would give goods odds that some of your users will not
> > 
> > the day they go to examine the type of an object for perfectly normal
> > 
> > pythonic reasons.
> > 
> > 
> > 
> > Example: I have a module that stores "objects" and they have as a
> > 
> > primary key a "name" and a "type" - not Python types, just strings.
> > 
> > Accordingly I have a similar situation to yours: the desire to use the
> > 
> > word "type". Fortunately for me, as an attribute in (usually small) code
> > 
> > chunks I can usually go:
> > 
> > 
> > 
> >   t = foo.type
> > 
> >   ... work with t here ...
> > 
> > 
> > 
> > Where I must pass one as a parameter I use the common convention of
> > 
> > naming the parameter "type_" at the receiving end.
> > 
> > 
> > 
> > For the calling end, as in your case, you want to use:
> > 
> > 
> > 
> >   type(blah)
> > 
> > 
> > 
> > Is it at all possible to make all uses of your "type" function method
> > 
> > calls? Eg:
> > 
> > 
> > 
> >   something.type("text to type")
> > 
> > 
> > 
> > It avoids the overloading while keeping your desired name.
> > 
> > -- 
> > 
> > Cameron Simpson 
> > 
> > 
> > 
> > Wouldn't it be great if all emergency stopping situations occurred on your
> > 
> > favourite bit of road..you'd probably know about it before it happened
> > 
> > and would be able to take other evasive action.
> > 
> > - Neville Brabet
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
  -- 
http://mail.python.org/mailman/listinfo/python-list


Issue with seeded map generation

2012-12-08 Thread Graham Fielding




Hey, all! I've managed to get my project to a semi-playable state (everything 
functions, if not precisely the way I'd like it to).  One small issue is that 
when the player movs from one level to the next, the items and monsters in the 
previous level all 'reset' and return to the positions they had when the level 
was seeded. I've puzzled over (and attempted) quite a few workarounds, and had 
no success.  I don't want to pickle the entire level (that would be overkill 
for what I need), but I want to update the item/monster locations so the player 
can drop an item and come back to it later. Should I add something to the 
'drop_item' function, or call soemthing in make_map?
  -- 
http://mail.python.org/mailman/listinfo/python-list


RE: What are the minimum requirements to get a job in?

2012-12-13 Thread Graham Fielding


 
> Date: Thu, 13 Dec 2012 18:49:32 -0800
> Subject: What are the minimum requirements to get a job in?
> From: suresh.pinn...@gmail.com
> To: python-list@python.org
> 
> My aim is to get a job into google or cisco or facebok.
> I have basic knowledge in python,c,java and good in javascript,html,css, 
> database concepts.
> If i learn django and python. Shall I get my dream job?
> 
> Please suggest me
> -- 
> http://mail.python.org/mailman/listinfo/python-list
You'd need a fair bit of industry cred to get picked up at a place like Google, 
I'd imagine.  By all means, keep pursuing your dream, but temper your 
expectations.   -- 
http://mail.python.org/mailman/listinfo/python-list


Inserting Unicode chars in Entry widget

2012-12-29 Thread Alan Graham
Hello Python experts,

I want to insert Unicode chars in an Entry widget by pushing on buttons;
one for each Unicode character I need. I have made the Unicode buttons.
I just need a simple function that will send the Unicode character to
the Entry widget.
Is there a better approach?

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


Strange occasional marshal error

2011-03-02 Thread Graham Stratton
Hi,

I'm using Python with ZeroMQ to distribute data around an HPC cluster.
The results have been good apart from one issue which I am completely
stuck with:

We are using marshal for serialising objects before distributing them
around the cluster, and extremely occasionally a corrupted marshal is
produced. The current workaround is to serialise everything twice and
check that the serialisations are the same. On the rare occasions that
they are not, I have dumped the files for comparison. It turns out
that there are a few positions within the serialisation where
corruption tends to occur (these positions seem to be independent of
the data of the size of the complete serialisation). These are:

4 bytes starting at 548867 (0x86003)
4 bytes starting at 4398083 (0x431c03)
4 bytes starting at 17595395 (0x10c7c03)
4 bytes starting at 19794819 (0x12e0b83)
4 bytes starting at 22269171 (0x153ccf3)
2 bytes starting at 25052819 (0x17e4693)
3 bytes starting at 28184419 (0x1ae0f63)

I note that the ratio between the later positions is almost exactly
1.125. Presumably this has something to do with memory allocation
somewhere?

Some datapoints:

- The phenomenon has been observed in a single-threaded process
without ZeroMQ
- I think the phenomenon has been observed in pickled as well as
marshalled data
- The phenomenon has been observed on different hardware

Unfortunately after quite a lot of work I still haven't managed to
reproduce this error on a single machine. Hopefully the above is
enough information for someone to speculate as to where the problem
is.

Many thanks in advance for any help.

Regards,

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


Re: Strange occasional marshal error

2011-03-03 Thread Graham Stratton
On Mar 2, 3:01 pm, Graham Stratton  wrote:
> We are using marshal for serialising objects before distributing them
> around the cluster, and extremely occasionally a corrupted marshal is
> produced. The current workaround is to serialise everything twice and
> check that the serialisations are the same. On the rare occasions that
> they are not, I have dumped the files for comparison. It turns out
> that there are a few positions within the serialisation where
> corruption tends to occur (these positions seem to be independent of
> the data of the size of the complete serialisation). These are:
>
> 4 bytes starting at 548867 (0x86003)
> 4 bytes starting at 4398083 (0x431c03)
> 4 bytes starting at 17595395 (0x10c7c03)
> 4 bytes starting at 19794819 (0x12e0b83)
> 4 bytes starting at 22269171 (0x153ccf3)
> 2 bytes starting at 25052819 (0x17e4693)
> 3 bytes starting at 28184419 (0x1ae0f63)

I modified marshal.c to print when it extends the string used to write
the marshal to. This gave me these results:

>>> s = marshal.dumps(list((i, str(i)) for i in range(140)))
Resizing string from 50 to 1124 bytes
Resizing string from 1124 to 3272 bytes
Resizing string from 3272 to 7568 bytes
Resizing string from 7568 to 16160 bytes
Resizing string from 16160 to 33344 bytes
Resizing string from 33344 to 67712 bytes
Resizing string from 67712 to 136448 bytes
Resizing string from 136448 to 273920 bytes
Resizing string from 273920 to 548864 bytes
Resizing string from 548864 to 1098752 bytes
Resizing string from 1098752 to 2198528 bytes
Resizing string from 2198528 to 4398080 bytes
Resizing string from 4398080 to 8797184 bytes
Resizing string from 8797184 to 17595392 bytes
Resizing string from 17595392 to 19794816 bytes
Resizing string from 19794816 to 22269168 bytes
Resizing string from 22269168 to 25052814 bytes
Resizing string from 25052814 to 28184415 bytes
Resizing string from 28184415 to 31707466 bytes

Every corruption point occurs exactly three bytes above an extension
point (rounded to the nearest word for the last two). This clearly
isn't a coincidence, but I can't see where there could be a problem.
I'd be grateful for any pointers.

Thanks,

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


Re: Strange occasional marshal error

2011-03-03 Thread Graham Stratton
On Mar 3, 6:04 pm, Guido van Rossum  wrote:
> This bug report doesn't mention the Python version nor the platform --
> it could in theory be a bug in the platform compiler or memory
> allocator.

I've seen the problem with 2.6 and 2.7, on RHEL 4 (possibly with a
custom kernel, I can't check at the moment).

> It would also be nice to provide the test program that
> reproduces the issue.

I'm working on trying to reproduce it without the proprietary code
that uses it, but so far haven't managed it. There are some custom C
extensions in the system where this is observed, but since the code is
single-threaded I don't think they can have any effect during
marshalling.

Thanks,

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


Re: FBI wants public help solving encrypted notes from murder mystery

2011-03-30 Thread Graham Cooper
On Mar 31, 12:18 pm, "Stretto"  wrote:
> "Joe Snodgrass"  wrote in message
>
> news:c37e8e0b-a825-4ac5-9886-8828ab1fa...@x8g2000prh.googlegroups.com...
>
>
>
>
>
>
>
> > FBI cryptanalysis hasn’t decrypted notes from 1999 murder mystery
>
> >http://tinyurl.com/4d56zsz
>
> > The FBI is seeking the public's help in breaking the encrypted code
> > found in two notes discovered on the body of a murdered man in 1999.
>
> > The FBI says that officers in St. Louis, Missouri discovered the body
> > of 41-year-old Ricky McCormick on June 30, 1999 in a field and the
> > clues regarding the homicide were two encrypted notes found in the
> > victim's pants pockets.
>
> > The FBI says that despite extensive work by its Cryptanalysis and
> > Racketeering Records Unit (CRRU), and the American Cryptogram
> > Association, the meanings of those two coded notes remain a mystery
> > and McCormick's murderer has never been found. One has to wonder
> > though, if the FBI can't figure this out, who can? But I digress.
>
> > From the FBI: "The more than 30 lines of coded material use a
> > maddening variety of letters, numbers, dashes, and parentheses.
> > McCormick was a high school dropout, but he was able to read and write
> > and was said to be 'street smart.' According to members of his family,
> > McCormick had used such encrypted notes since he was a boy, but
> > apparently no one in his family knows how to decipher the codes, and
> > it's unknown whether anyone besides McCormick could translate his
> > secret language. Investigators believe the notes in McCormick's
> > pockets were written up to three days before his death."
>
> > "Standard routes of cryptanalysis seem to have hit brick walls," said
> > CRRU chief Dan Olson in a statement. To move the case forward,
> > examiners need another sample of McCormick's coded system-or a similar
> > one-that might offer context to the mystery notes or allow valuable
> > comparisons to be made. Or, short of new evidence, Olson said, "Maybe
> > someone with a fresh set of eyes might come up with a brilliant new
> > idea."
>
> > The FBI says it has always relied on public tips and other assistance
> > to solve crimes though breaking a code may represent a special
> > circumstance.
>
> > For larger images of the notes go here. [LINK]
>
> > If you have an idea how to break the code, have seen similar codes, or
> > have any information about the Ricky McCormick case, write to CRRU at
> > the following address:
>
> > FBI Laboratory
> > Cryptanalysis and Racketeering Records Unit
> > 2501 Investigation Parkway
> > Quantico, VA 22135
> > Attn: Ricky McCormick Case
>
> > There is no reward being offered, just the knowledge that you may be
> > solving an intriguing murder mystery, the FBI stated.
>
> No other information about the guy? It might help. If the note is of any use
> then people and places would be in it. If that is the case then it would
> help to know where he lived and some of the names of people he knows.
>
> The note seems like it may not be just encrypted but a sort of
> compression(or rather shorthand/jargon) was used. Was the guy a drug dealer?
> It could be a list of "clients" or information about where he sold drugs(the
> numbers look like street addresses or amounts.
>
> If these kinda notes were so common from this guy then surely the FBI should
> have many more?
>
> Seems like the FBI could do more if they wanted it really solved..



I can use my psychic powers to solve the crime!

I did a test on the Australian Channel 9 News website a few months
ago...

Given a sequence of photos tell who is a notorious criminal and who is
a professional surfer!

I got every one right!  My psychic channels were spot on, e.g. "had a
big magazine following" -> SURFER



G. Adam
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ssh or other python editor

2005-10-04 Thread Graham Fawcett
[EMAIL PROTECTED] wrote:
> >So you're using Putty to telenet/ssh into the FreeBSD server, but what
> >editor on you using on the FreeBSD server?
>
> I use pico for that.
> That Samba isn't available but I can install it.
>
> Or are there other editors for FreeBSD that I can run with putty ?

I use Emacs on my win32 workstation to edit files on my Linux servers
via SSH. There are at least two Emacs third-party modules ("modes") --
Tramp and Hobo -- that allow you to edit files over SSH.

I use Hobo (never tried Tramp, I think it's for XEmacs anyway). There
is a bit of setup. You'll need to use "pageant" and authenticate by
RSA/DSA key. See PuTTY's Web site; you may also need to do some
Googling to set up agent-based authentication. Note that pageant will
make PuTTY authenticate automatically as well, so it's easy to test
whether your pageant setup is correct, apart from any Emacs-related
issues.

You might also need to configure Hobo slightly; email me if you want my
configuration details (though I can't help you with the
pageant/authentication stuff, so get that done first :-).

Once you're done, you can load, edit and save files quite
transparently. Not everybody's cup of tea, but there you are. :-)

Graham

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


Re: MS Word Outline -> reStructuredText script?

2005-10-14 Thread Graham Fawcett
Jim wrote:
> Hi,
>
> I'm using reStructuredText as a format for some group documentation,
> and often my co-workers take notes during meetings in Word's outline
> mode.  Does anyone already have a python script that will convert from
> Word (or the Open Office file format version of a word document) to
> reStructured Text?

I don't have a script; but if you have to roll your own, you might
consider having your colleagues save their Word documents in XML
format. Office 2003, and perhaps slightly earlier versions, do this.
Then you can "simply" parse the XML and transform it into anything you
like, including a reST document.

If you cannot convince them, then you could also have your script
automate Word, via win32com.client.Dispatch('Word.Application'), and do
the save-as-XML yourself before parsing the generated XML. At the end
of the day, I think this would be easier than, e.g., trying to use
win32com to traverse the document's object model.

Graham

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


Re: write a loopin one line; process file paths

2005-10-24 Thread Graham Fawcett
Xah Lee wrote:
> Dear Peter Hansen,
> My messages speak themselfs. You and your cohorts's stamping of it does
> not change its nature. And if this is done with repetitiousness, it
> gives away your nature.

Taunt not the cohorts of Peter Hansen!

Graham

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


Re: Python and Lotus Notes

2005-11-02 Thread Graham Fawcett

Grzegorz Slusarek wrote:
> Hello everyone. I have to get data from Lotus Notes and i curious is it
> possible doing it with Python. I heard that Lotus Notes using COM, so
> the Python does so maybe it can be done? Anyone have any experiences
> doing that?
> Ane help will by apreciated

Yes, it's pretty simple. Quick example:

from win32com.client import Dispatch
session = Dispatch('Lotus.NotesSession')
session.Initialize(MY_NOTES_PASSWORD)

db = session.getDatabase(SERVER_NAME, DB_NAME)
...

The LotusScript reference guide is almost completely applicable to
Notes via COM.

It can be useful to define some helper functions to make working with
Notes a bit easier. For example, this is the (very clunky) way to
iterate all documents in a Notes database, without helper code:

view = db.getView('Important Docs')
doc = db.getFirstDocument()
while doc:
do_something_with(doc)
doc = view.getNextDocument(doc)

You probably recognize the pattern from any LotusScript you've written.
(It sure is easy to forget that last line...)

But Python gives you far better control structures, like generators.
It's worthwhile to define a few helper functions like this, in a common
module:

def iterateDocuments(docs):
doc = docs.getFirstDocument()
while doc:
yield doc
doc = docs.getNextDocument(doc)

Then you can write much cleaner code, like this:

for doc in iterateDocuments(view):
do_something_with(doc)

Similarly you can write iterator functions for traversing databases,
ACL entries, etc.

I also find these two defnitions useful:

def get(doc, attr):
return doc.getItemValue(attr)

def get1(doc, attr):
return get(doc, attr)[0]

because it's a lot easier to write:

user_name = get1(user_doc, 'FullName')

than:

user_name = user_doc.getItemValue('FullName')[0]

Note that the attribute-access style that LotusScript allows, e.g.
"user_doc.FullName(0)" does not work in Python/COM, hence you'll need
getItemValue(), replaceItemValue(), etc.. You could write a wrapper for
the NotesDocument class that would give you attributes like this.
Personally, I find that to be more trouble than it's worth, but your
needs be very different.

Last warning: once you've tried using Python and COM, you'll scream
whenever you have to write LotusScript, and you'll wish like crazy that
Notes/Domino supported Python as a native scripting language. :-)
Jython kind of works, but I never got it happily running for
server-side code, alas.

Graham

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


Re: ADT for restricted set of values

2005-11-03 Thread Graham Fawcett
Ben Finney wrote:
> Howdy all,
>
> I'd like to have an Abstract Data Type for a scalar value that is
> restricted to a small set of values. Like an Enum, I suppose.
>
> What I would like is to be able to use simple 'str' values in most of
> the code, but where the values are actually used in a semantically
> meaningful context, have them used as an ADT that will barf if the
> value isn't part of the set.

How about this:

class BaseEnum(str):
"""Base class for enumerations. You must subclass and provide
values for the _valid list."""

_valid = []

def __init__(self, v):
assert self in self._valid, 'value %r not in %r' % (self,
self._valid)

cass Gender(BaseEnum):
_valid = ['male', 'female', 'unknown']

Trying the following:

Gender('male')
Gender('female')
assert Gender('male') == 'male'
Gender('shemale')

The last will return an error:
AssertionError: value 'shemale' not in ['male', 'female', 'unknown']

Graham

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


Re: Python and Lotus Notes

2005-11-04 Thread Graham Fawcett
Marco Aschwanden wrote:
> The second line of your code is already a show stopper in my case:
>
>  from win32com.client import Dispatch
> session = Dispatch('Lotus.NotesSession')
> session.Initialize('my_secret_passwort')
>
> When started, ends:
> [snip]
> AttributeError: Lotus.NotesSession.Initialize
>
> It worked before though with Version 5.x of Notes. In Notes Version 6.X
> they introduced the session.Initialize() - that was the point, when I
> couldn't create an instance anymore. I found no hint on the net... Do you
> have any idea what is going wrong here?

I'm using 6.x here, so that's not the problem.

Is there any chance you have an old gen_py file in your path? E.g.,
does re-running the PythonWin "makepy" utility again make a difference?
Just a thought, but something might be "hard-coded" to your earlier 5.x
COM interface, and gen_py would be a likely suspect.

Also, If you have another language that you can use to do COM, e.g.
Visual Basic (in any of the MS Office apps, or in Visual Studio),
verify that you can create a Lotus.NotesSession using those
environments as well. It might be a lower-level (non-Python-related)
problem.

Graham

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


Re: python win32 and COM? for internet monitoring

2005-11-22 Thread Graham Fawcett
Matthew Thorley wrote:
> Greetings, I have a question I hope some one with more back ground can
> give me a little help with.
>
> I want to write a simple internet monitoring script for windows that
> watches out bound http traffic and keeps a list of all the site visited.
>
> I am thinking that I might be able to use pywin32 and COM to listen on
> open ports (e.g. 80) and watch for http headers. But I'm not sure if
> there is a better way to do it.

You might want to look into PCAP (a.k.a libpcap), which is the
network-sniffing libary used by Ethereal, among other programs.  Much
more portable than COM, and there are Python wrappers for it, IIRC.

You could also implement an HTTP proxy server in Python; Google should
turn up numerous existing implementations.

Graham

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


Re: Stealing focus: emacs, and PIL, in Windows

2005-11-26 Thread Graham Fawcett
[EMAIL PROTECTED] wrote:
> I'm using GNU Emacs 21.3.1 with python-mode 1.0alpha under Windows XP.
> Whenever I execute a command in an edit window (with
> py-execute-region), the output window steals the focus. How can I stop
> this happening?
[snip]
> I commented out the command
>   (pop-to-buffer (current-buffer))
> and now the command window no longer steals focus. But I don't know if
> this has any other side effects, or it there's a better way to prevent
> focus being stolen.

For someone who says he doesn't know Lisp, you seem to be doing just
fine. :-)

Not sure if there would be side-effects. Note that py-execute-string,
py-execute-buffer, py-execute-def-or-class all use py-execute-region.
Their behaviour may be altered by your change. But it would be easy to
test.

If you did encounter a problematic side-effect, you could define your
own "py-execute-region-custom" as a copy of py-execute-region but with
the (pop-to-buffer) call removed. Then use

  (require 'python-mode)  ;; if you're putting this in .emacs
  (define-key py-mode-map "\C-c|" 'py-execute-region-custom)

to redefine the keyboard mapping.  (The (require 'python-mode) call
ensures that the py-mode-map variable has been initialized.) You could
provide an alternate mapping for your new function, of course.

If you put the custom function and your define-key call both in your
.emacs file, then you can easily port it to other machines (perh.
easier than maintaining a patch for python-mode).

Graham

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


Re: Winsound doesn't play whole sound?

2005-11-30 Thread Graham Fawcett
Dieter Vanderelst wrote:
> Hello,
>
> I'm having a problem with playing WAV files using Winsound.
>
> If I use winsound to play a certain WAV files only the first few seconds
> of the file get played. I think this comes because these files contain
> some parts of silence. There winsound seems the stop playing the files
> (windows media player plays the sounds correctly).
[snip]
> Just as a reference, the code I use:
> winsound.PlaySound("My_wav.wav", winsound.SND_FILENAME)

This is totally a guess, but add the following to the end of your test
script:

import time
time.sleep(10)

Did the file play for ten seconds longer? I don't know anything about
winsound, but I'm guessing that the PlaySound call is non-blocking, and
your playback stops because your process is quitting before the sound
has played.

Graham

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


Re: When someone from Britain speaks, Americans hear a "British accent"...

2005-06-30 Thread Graham Fawcett
Steven D'Aprano wrote:
> Speaking as an Australia, ...
> [snip]
> But don't worry, there is one thing we all agree on throughout the
> English-speaking world: you Americans don't speak English.

And lest you feel Steven's observation don't bear much weight, keep in
mind that he is speaking as an entire continent. ;-)

But, speaking as Antarctica, I must disagree. I don't think the Keepers
of the Canon of the English Language(tm) would hold up either your
Strine or our Canadian regional accents as examples of Real English
Pronunciation(tm). But that's the kind of thing that canon-keepers
obsess about, while the rest of us just get along and communicate with
one another. (By "us", I mean "us people", not "us continents" -- I
stopped speaking as Antarctica a few lines back.)

keep-your-stick-on-the-ice'ly yours,

Graham

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


Re: Web App like Google

2005-07-12 Thread Graham Fawcett
In translating natural language to SQL, be sure you're not introducing
opportunities for SQL injection attacks. Code like

   sql = 'SELECT %s FROM %s' % (this, that)

is considered dangerous, because a well-crafted value for "that" can be
used to, e.g., delete rows from your tables, run system commands, etc.
You can save a lot of worry by using a database account with read-only
privileges, but you still have to be careful. My advice is to read up
on "sql injection" before going too public with your code.

Graham

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


Re: How to store "3D" data? (data structure question)

2005-07-20 Thread Graham Fawcett
Sebastian Bassi wrote:
> Hello,
>
> I have to parse a text file (was excel, but I translated to CSV) like
> the one below, and I am not sure how to store it (to manipulate it
> later).
>
> Here is an extract of the data:
>
[snip]

This looks a lot like 2D data (row/column), not 3D. What's the third
axis? It looks, too, that you're not really interested in storage, but
in analysis...

Since your "line" columns all have names, why not use them as keys in a
dictionary? The associated values would be lists, in which you could
keep references to matching rows, or parts of those rows (e.g. name and
allele). Count up the length of the row, and you have your "number of
matches".



import csv  # let Python do the grunt work

f = file('name-of-file.csv')
reader = csv.reader(f)

headers = reader.next() # read the first row
line_names = headers[2:]

results = {}# set up the dict
for lname in line_names:# each key is a line-name
results[lname] = []

for row in reader:  # iterate the data rows
row_name, allele = row[:2]
line_values = row[2:]   # get the line values.
# zip is your friend here. It lets you iterate
# across your line names and corresponding values
# in parallel.
for lname, value in zip(line_names, line_values):
if value == '*':
results[lname].append((row_name, allele))

# a quick look at the results.
for lname, matches in results.items():
print '%s %d' % (lname, len(matches))


Graham

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


Re: How to store "3D" data? (data structure question)

2005-07-20 Thread Graham Fawcett
Sebastian Bassi wrote:
> On 20 Jul 2005 10:47:50 -0700, Graham  Fawcett <[EMAIL PROTECTED]> wrote:
> > This looks a lot like 2D data (row/column), not 3D. What's the third
> > axis? It looks, too, that you're not really interested in storage, but
> > in analysis...
>
> I think it as 3D like this:
> 1st axis: [MARKER]Name, like TDF1, TDF2.
> 2nd axis: Allele, like 181, 188 and so on.
> 3rd axis: Line: RHA280, RHA801.
>
> I can have a star in MarkerName TDF1, Allele 181 and Line RHA280.
> I can have an empty (o none) in TDF1, Allele 181 and Line RHA801.

Okay. I think what will drive your data-structure question is the way
that you intend to use the data. Conceptually, it will always be 3D, no
matter how you model it, but trying to make a "3D data structure" is
probably not what is most efficient for your application.

If 90% of your searches are of the type, 'does TDF1/181/RHA280 have a
star?' then perhaps a dict using (name,allele,line) as a key makes most
sense:

  d = {('TDF1',181,'RHA280'):'*', ...}
  query = ('TDF1', 181, 'RHA280')
  assert query in d

Really, you don't need '*' as a value for this, just use None if you
like, since all the real useful info is in the keyspace of the dict.

If you're always querying based on line first, then something like my
earlier 'results' dict might make sense:

  d = {'RHA280':[('TDF1',181), ...], ...}
  for name, allele in d['RHA280']:
  if allele == 181:  # or some other "query" within
RHA280
  ...

You get the idea: model the data in the way that makes it most useable
to you, and/or most efficient (if this is a large data set).

But note that by picking a structure like this, you're making it easy
to do certain lookups, but possibly harder (and slower) to do ones you
hadn't thought of yet.

The general solution would be to drop it into a relational database and
use SQL queries. Multidimensional analysis is what relational DBs are
for, after all. A hand-written data structure is almost guaranteed to
be more efficient for a given task, but maybe the flexibility of a
relational db would help serve multiple needs, where a custom structure
may only be suitable for a few applications.

If you're going to roll your own structure, just keep in mind that
dict-lookups are very fast in Python, far more efficient than, e.g.,
checking for membership in a list.

Graham

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


Re: wxPython Notebook crash when pressing alt key

2005-09-22 Thread Graham Fawcett

Kreedz wrote:
> Could Windows version have anything to do with this?? Else I've got
> some really weird issue...
>
> I'm on Windows 2000 Professional

Yes, that definitely counts as a wierd issue. 

I couldn't reproduce the bug either.

C:\temp>python
ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
on win32
>>> import wx
>>> wx.__version__
'2.6.1.0'

It's the unicode build, in case that makes any difference.

Graham

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


Re: Selective HTML doc generation

2005-02-24 Thread Graham Ashton
Thanks Brian, much appreciated. Looks quite straightforward.

Graham

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


Re: Python mascot proposal

2004-12-13 Thread Graham Fawcett
Luis M. Gonzalez wrote:
> Hey Dimitri,
>
> I completely agree with you in that Python needs once for all a cool
> logo.
> I like your design very much, but I have a few thoughts about it:
>
> 1) I think that Python's logo should reflect its power.
> If we use a mascot as its image, we would be giving the wrong idea:
> that Python is a "toy" language, instead of a very good alternative
to
> other mainstream languages.

+1. But I think your logo would be a great identifier for Python when
teaching it to younger students. Perhaps the EDU-SIG/Python in
Education folks might be interested.

The American wrestler Hulk Hogan used to intimidate his opponents by
flexing the muscles of his arms, which he refered to as his "26-inch
Pythons". Now, there's a powerful image that would strike an awe-ful
chord in the hearts of many a thick-glassed geek! "Whatcha gonna do,
when the world's most powerful dynamic language comes after you?"
-- Graham

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


Re: Getting directory size

2005-03-21 Thread Graham Fawcett
Peter Hansen wrote:
> francisl wrote:
> > How can we get a full directory size (sum of all his data)?
> > like when we type `du -sh mydir`
> >
> > Because os.path.getsize('mydir') only give the size of the
directory
> > physical representation on the disk.
>
> os.popen('du -sh mydir') would be one approach.
>
> The harder way is to use os.walk('mydir') to scan all
> files in all subdirectories, and use os.stat() or
> os.path.getsize() to add up the sizes of each file.

With Orendorff's path module, this becomes a two-liner:

from path import path
dir_size = sum([f.size for f in path('mydir').walkfiles()])

With Python 2.4 genexps, you don't even need the square brackets...

http://www.jorendorff.com/articles/python/path/

Shoulda-been-added-to-the-standard-library'ly yours,

-- Graham

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


String manipulation

2005-04-13 Thread Nicholas Graham

I'm writing a program that requires some string manipulations.  Let's say 
I have a string

s='x'

Now, the ascii code for 'x' is 0x78.  I want to be able to perform 
operations on this number, and print the character corresponding to the 
results of the operation.  For example, the pseudo-code looks like:

-read in string s from external file (for simplicity, we'll keep s='x')

-determine the code for s (should be 0x78 or 120 in base 10), store it in 
variable c

-add a literal to c (let's say c=c+1)

-find out which character corresponds to the new code c=0x79

-store the character in a new string s2

At the end of this, I want s2='y'

Any suggestions?

NG

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


Using a particular python binary with venv

2015-06-01 Thread greenbay . graham
According to this https://docs.python.org/3.4/library/venv.html#module-venv 
'Each virtual environment has its own Python binary (allowing creation of 
environments with various Python versions)' 
So how would I create a virtual environment using the venv module that has a 
Python 2.7 binary?
tia
g
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using a particular python binary with venv

2015-06-01 Thread greenbay . graham
On Tuesday, 2 June 2015 09:43:37 UTC+12, Carl Meyer  wrote:
> On 06/01/2015 03:33 PM, orotau wrote:
> > According to this 
> > https://docs.python.org/3.4/library/venv.html#module-venv 'Each 
> > virtual environment has its own Python binary (allowing creation of 
> > environments with various Python versions)'
> >
> > So how would I create a virtual environment using the venv module 
> > that has a Python 2.7 binary?
> 
> You can't. The venv module only exists in and supports Python 3.3+.
> 
> If you need to support earlier Django versions, you'll need to use the
> older "virtualenv" library instead, which looks very similar from the
> end-user side. See https://virtualenv.pypa.io/en/latest/
> 
> Carl

Thanks Carl
I am guessing that the documentation needs to be altered then. I will submit a 
documentation bug...
cheers
g
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using a particular python binary with venv

2015-06-01 Thread greenbay . graham
http://bugs.python.org/issue24356
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: AttributeError: ' ' object has no attribute ' '

2013-02-23 Thread Graham Fielding

 > Date: Sat, 23 Feb 2013 10:22:54 -0800
> Subject: AttributeError:  '  ' object has no attribute '  '
> From: matt.doolittl...@gmail.com
> To: python-list@python.org
> 
> I am using Ubuntu 12.10, and Python 2.7.3, GNU Radio Companion v3.6.3.  I get 
> the this error in terminal: 
> 
>  in __init__
> self.wxgui_waterfallsink2_0.set_callback(wxgui_waterfallsink2_0_callback)
>   File "/usr/local/lib/python2.7/dist-packages/gnuradio/gr/hier_block2.py", 
> line 54, in __getattr__
> return getattr(self._hb, name)
> AttributeError: 'gr_hier_block2_sptr' object has no attribute 'set_callback'
> 
> I have been getting this error in multiple programs.  So what is stopping the 
> program here?  There is no Attribute .set_callback obviously. But where is 
> this attribute missing from exactly and how do i put it where it should be?  
> -- 
> http://mail.python.org/mailman/listinfo/python-list
That error means that 'gr_hier_block2_sptr' doesn't have enough information to 
proceed. If you look at the top of the defintion (for example, 'def 
gr_hier_block2_sptr, [attribute], [another attribute]'), that's where you 
should find .set_callback.  If it's not there, you'l just need to add it to the 
end; if it is there, then the attribute is probably given a different name 
somewhere else  (so you'll just need to find out its new name and update the 
existing entry). -- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parser or regex ?

2005-12-16 Thread Graham Fawcett
Fuzzyman wrote:
> Hello all,
>
> I'm writing a module that takes user input as strings and (effectively)
> translates them to function calls with arguments and keyword
> arguments.to pass a list I use a sort of 'list constructor' - so the
> syntax looks a bit like :
>
>checkname(arg1, "arg 2", 'arg 3', keywarg="value",
> keywarg2='value2', default=list("val1", 'val2'))
>
> Worst case anyway :-)

Since you've taken the care to use Python syntax, you could do worse
than using standard-library tools that are designed for parsing Python.

>>> import compiler
>>> from compiler import ast
>>> src = """
... checkname(arg1, "arg 2", 'arg 3', keywarg="value",
... keywarg2='value2', default=list("val1", 'val2'))
... """
>>> node = compiler.parse(src, mode='eval')
>>> node
Expression(CallFunc(Name('checkname'), [Name('arg1'), Const('arg 2'),
Const('arg 3'), Keyword('keywarg', Const('value')), Keyword('keywarg2',
Const('value2')), Keyword('default', CallFunc(Name('list'),
[Const('val1'), Const('val2')], None, None))], None, None))
>>> dir(node)
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'asList',
'getChildNodes', 'getChildren', 'node']

In this case, compiler.parse returns a compiler.ast.Expression object,
acting as the root node of an abstract syntax tree.

The compiler.visitor package may also be of help in writing tree-walker
code to extract the bits you want. With the example session above,

class KwVisitor(compiler.visitor.ASTVisitor):

def visitKeyword(self, node):
print '>>', node

compiler.visitor.walk(node, KwVisitor())

would output:

>> Keyword('keywarg', Const('value'))
>> Keyword('keywarg2', Const('value2'))
>> Keyword('default', CallFunc(Name('list'), [Const('val1'), Const('val2')], 
>> None, None))


Note that with a bit of black magic, you can modify the AST, and then
compile the transformed AST into a callable codeblock. Tools like
Quixote's PTL template language use this approach to introduce new
behaviours into Python-like code at compliation time.

Graham

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


Re: Guido at Google

2005-12-22 Thread Graham Fawcett
Cameron Laird wrote:
> In article <[EMAIL PROTECTED]>,
>  <[EMAIL PROTECTED]> wrote:
>   .
> >Well, this may be the CPython way of open source but I don't know if
> >that is "Open source" in general. Another way is that if someone(or
> >group) don't like the current state of a project, they fork. I don't
> >know if that is possible in the context of python, and programming
> >language in general. Can it still be called python ?
>   .
> While I don't understand the question, it might be pertinent to
> observe that, among open-source development projects, Python is
> unusual for the *large* number of "forks" or alternative imple-
> mentations it has supported through the years  http://phaseit.net/claird/comp.lang.python/python_varieties.html >.

...though not a lot of forks/variations that have persisted past the
early-alpha phase. Many of those projects are stale or defunct, alas.

Personally, I'd point out Scheme as an "open" HLL with a vast number of
implementations. But I guess it helps when the language itself is a
spec and there's no canonical implementation.

This all reminds me of one my favourite quotes from python-list of
yore:

 So python will fork if ActiveState starts
polluting it?

 I find it more relevant to speculate on whether
Python would fork if the merpeople start invading our cities
riding on the backs of giant king crabs. [1]

Merry _('Christmas') to all,
Graham


[1] http://mail.python.org/pipermail/python-list/2001-April/037142.html

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


Re: Guido at Google

2005-12-22 Thread Graham Fawcett
Steve Holden wrote:
> > Nicola Musatti wrote:
> > Of course, I'm going on vacation next week and there was talk
> > about a one-way ticket to Mexico. The real question is will they let me 
> > *back* in? :-)
> >
> I would be careful coming back across the border. I heard that the PSU
[suspicous premature end-of-sentence]

Steve, I hope that the PSU is just jamming your comms, and not holding
you captive over the holidays for your transgressions against the
cabal!

Graham

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


Re: Guido at Google

2005-12-22 Thread Graham Fawcett
Peter Hansen wrote:
> Graham Fawcett wrote:
> > Steve Holden wrote:
> >>>Nicola Musatti wrote:
> >>>Of course, I'm going on vacation next week and there was talk
> >>>about a one-way ticket to Mexico. The real question is will they let me 
> >>>*back* in? :-)
> >>I would be careful coming back across the border. I heard that the PSU
> >
> > [suspicous premature end-of-sentence]
> >
> > Steve, I hope that the PSU is just jamming your comms, and not holding
> > you captive over the holidays for your transgressions against the
> > cabal!
>
> At about the same instant that he sent that message to group, I was
> trying to call Steve on Google Talk and he suddenly went offline. I
> haven't seen him since.

There is no Steve Holden, and he has never been at war with Eurasia.
Remove the P, S and U keys from your keyboard immediately.

double-plus-good'ly yours, ...umm... doble-l-good'ly yor,

Graham

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


Re: COM automation, Internet Explorer, DocumentComplete event

2006-01-10 Thread Graham Fawcett
puff wrote:
> I'm able to catch IE's events including DocumentComplete with:
>
> def OnDocumentComplete(self, pDisp, URL):
>
> so i have pDisp.  Self is the object returned by:
>
> self.ie = DispatchWithEvents("InternetExplorer.Application",
> InternetExplorerEvents)
>
> that created the automation object.  How do I perform the test in
> Python?

This recipe might give you some clues; you can use the Busy attribute,
instead of a DocumentComplete handler:

http://herlock.com/ob/pythoncb/0596007973/chp-10-sect-15.html

Graham

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


Re: exec a string in an embedded environment

2006-01-11 Thread Graham Fawcett
Tommy R wrote:
> I need some way to execute a string and pass arguments to the functions
> inside the string. We have discussed a solution where we first load the
> string (containing some funcs) and then run something similar to
> Py_RunString("foo(1.0, 'str')");  We need to do this in a generic way
> so we can send in arbitrary arguments.

You could use

foo_ptr = Py_RunString("foo", Py_eval_input, globals_ptr,
locals_ptr)

to get a reference to the foo function, and then use

result_ptr = PyObject_Call(foo_ptr, args_ptr, kwargs_ptr)

to call it with arbitrary arguments and keyword arguments. Of course,
you're responsible for setting up the globals and locals, as well as
the args list and kwargs dictionary objects.

The PyObject_Call signature, from , is:

 PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
 PyObject *args, PyObject *kw);

Does this help?

Graham

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


Newbie..Needs Help

2006-07-28 Thread Graham Feeley
Hi this is a plea for some help.
I am enjoying a script that was written for me and its purpose is to collect 
data from a web site and puts it into a access database table.
It works fine, however it is a sports info table but now I need to collect 
the results of those races.

I simply can't keep up putting the results in manually.
I dont care if it is a access table or a text file ( whichever is easiest)
there are only 12 fields to extract
The person who wrote the script is not available as he is engrossed in 
another project which is talking all his time.
I hope someone has a little time on his hands willing to help me
Regards
Graham
 


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


Re: Newbie..Needs Help

2006-07-28 Thread Graham Feeley
Thanks Nick for the reply
Of course my first post was a general posting to see if someone would be 
able to help
here is the website which holds the data I require
http://www.aapracingandsports.com.au/racing/raceresultsonly.asp?storydate=27/07/2006&meetings=bdgo

The fields required are as follows
 NSW Tab
#  Win  Place
 2$4.60   $2.40
 5$2.70
 1$1.30
 Quin$23.00
 Tri  $120.70
Field names are
Date   ( not important )
Track= Bendigo
RaceNoon web page
Res1st...2
Res2nd..5
Res3rd..1
Div1..$4.60
DivPlc...$2.40
Div2..$2.70
Div3..$1.30
DivQuin.$23.00
DivTrif...$120.70
As you can see there are a total of 6 meetings involved and I would need to 
put in this parameter ( =bdgo) or (=gosf) these are the meeting tracks

Hope this more enlightening
Regards
graham

"Graham Feeley" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi this is a plea for some help.
> I am enjoying a script that was written for me and its purpose is to 
> collect data from a web site and puts it into a access database table.
> It works fine, however it is a sports info table but now I need to collect 
> the results of those races.
>
> I simply can't keep up putting the results in manually.
> I dont care if it is a access table or a text file ( whichever is easiest)
> there are only 12 fields to extract
> The person who wrote the script is not available as he is engrossed in 
> another project which is talking all his time.
> I hope someone has a little time on his hands willing to help me
> Regards
> Graham
>
>
> 


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


Re: Newbie..Needs Help

2006-07-29 Thread Graham Feeley
Well Well Well, Anthra you are a clever person, Are
nt you
I nearly fell over when i read your post.
Would it help if we used another web site to gather data
As you stated the tables are not all that well structured.
well I will give thisone  a go first and if there is anything I can do for 
you just ask and I will try my best.
I really appreciate what you have done.
Of course I will try to follow your code to see if any will fall on 
meLOL
Regards
Graham

"Anthra Norell" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>
> - Original Message -
> From: "Graham Feeley" <[EMAIL PROTECTED]>
> Newsgroups: comp.lang.python
> To: 
> Sent: Friday, July 28, 2006 5:11 PM
> Subject: Re: Newbie..Needs Help
>
>
>> Thanks Nick for the reply
>> Of course my first post was a general posting to see if someone would be
>> able to help
>> here is the website which holds the data I require
>> http://www.aapracingandsports.com.au/racing/raceresultsonly.asp?storydate=27/07/2006&meetings=bdgo
>>
>> The fields required are as follows
>>  NSW Tab
>> #  Win  Place
>>  2$4.60   $2.40
>>  5$2.70
>>  1$1.30
>>  Quin$23.00
>>  Tri  $120.70
>> Field names are
>> Date   ( not important )
>> Track= Bendigo
>> RaceNoon web page
>> Res1st...2
>> Res2nd..5
>> Res3rd..1
>> Div1..$4.60
>> DivPlc...$2.40
>> Div2..$2.70
>> Div3..$1.30
>> DivQuin.$23.00
>> DivTrif.......$120.70
>> As you can see there are a total of 6 meetings involved and I would need 
>> to
>> put in this parameter ( =bdgo) or (=gosf) these are the meeting tracks
>>
>> Hope this more enlightening
>> Regards
>> graham
>>
>
> Graham,
>
> Only a few days ago I gave someone a push who had a very similar problem. 
> I handed him code ready to run. I am doing it again for
> you.
>  The site you use is much harder to interpret than the other one was 
> and so I took the opportunity to experimentally stretch
> the envelope of a new brain child of mine: a stream editor called SE. It 
> is new and so I also take the opportunity to demo it.
>  One correspondent in the previous exchange was Paul McGuire, the 
> author of 'pyparse'. He made a good case for using 'pyparse'
> in situations like yours. Unlike a stream editor, a parser reads structure 
> in addition to data and can relate the data to its
> context.
>  Anlayzing the tables I noticed that they are poorly structured: The 
> first column contains both data and ids. Some records are
> shorter than others, so column ids have to be guessed and hard coded. 
> Missing data sometimes is a dash, sometimes nothing. The
> inconsistencies seem to be consistent, though, down the eight tables of 
> the page. So they can be formalized with some confidence
> that they are systematic. If Paul could spend some time on this, I'd be 
> much interested to see how he would handle the relative
> disorder.
>  Another thought: The time one invests in developing a program should 
> not exceed the time it can save overall (not talking
> about recreational programming). Web pages justify an extra measure of 
> caution, because they may change any time and when they do
> they impose an unscheduled priority every time the reader stops working 
> and requires a revision.
>
> So, here is your program. I write it so you can copy the whole thing to a 
> file. Next copy SE from the Cheese Shop. Unzip it and put
> both SE.PY and SEL.PY where your Python progams are. Then 'execfile' the 
> code in an IDLE window, call 'display_horse_race_data
> ('Bendigo', '27/07/2006') and see what happens. You'll have to wait ten 
> seconds or so.
>
> Regards
>
> Frederic
>
> ##
>
> TRACKS = { 'New Zealand' : '',
>   'Bendigo' : 'bdgo',
>   'Gosford' : 'gosf',
>   'Northam' : 'nthm',
>   'Port Augusta': 'pta',
>   'Townsville'  : 'town',
> }
>
>
> # This function does it all once all functions are loaded. If nothing 
> shows, the
> # page has not data.
>
> def display_horse_race_data (track, date, clip_summary = 100):
>
>   """
>  tracks: e.g. 'Be

Re: os.path.normpath

2006-08-16 Thread Graham Dumpleton
Hari Sekhon wrote ..
> [EMAIL PROTECTED] wrote:
> > [EMAIL PROTECTED] wrote:
> >   
> >> I am using a windows box and passing a string like "../foo/../foo2"
> to
> >> normpath which then returns "..\\foo2". But if this string is going
> >> into a webpage link it should really be "../foo".
> >>
> >> Is there any way to tell os.path.normpath to act like we are an a unix
> >> style box?
> >> 
> >
> > Use posixpath.normpath() instead.
> >
> >   
> I can't seem to find posixpath in the docs, but I can import posixpath
> and a dir shows it does indeed have a normpath.

Quoting:

  http://www.python.org/doc/2.3.5/lib/node750.html

it says:

  posixpath
  -- Implementation of os.path on POSIX.

What it provides is therefore documented by os.path module. The posixpath
module is still accessible on non POSIX platforms though.

Graham

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


Re: Python to use a non open source bug tracker?

2006-10-07 Thread James Graham
Steve Holden wrote:
> Giovanni Bajo wrote:
> [...]
>>
>> I understand your concerns, but I have to remember you that most bug 
>> reports
>> submitted by users go totally ignored for several years, or, better, 
>> forever. I
>> do not have a correct statistic for this, but I'm confident that at 
>> least 80%
>> of the RFE or patches filed every week is totally ignored, and 
>> probably at
>> least 50% of the bugs too. I think there is a much bigger problem here 
>> wrt QOS.
>>
>> So, you might prefer 6-10 people to activate a new tracker account 
>> faster than
>> light. I'd rather have 3-days delay in administrative issues because 
>> our single
>> administrator is sleeping or whatever, and then have 2-3 people doing 
>> regular
>> bug processing.
> 
> ... and if wishes were horses then beggars would ride.

FWIW, this situation (few administrators compared to the number of 
community members involved in triage) is basically the situation for the 
Mozilla project's bug database (which is a bugzilla install, of course), 
This was the case even before the corporation was founded so it's not a 
funding issue. My impression has always been that people who kept the 
bug database clean (moving things to the right component, hunting out 
duplicates, verifying fixes, and so on) are seen as vital and accorded 
appropriate respect by the Mozilla development community.

I don't think I have any specific point to make except, perhaps, that by 
making the right noises, it is quite possible to get a useful number of 
people helping with bug processing work.
-- 
http://mail.python.org/mailman/listinfo/python-list


WebScraping

2006-11-04 Thread Graham Feeley
Can someone steer me to scripts / modules etc on webscraping please???
Ultimately I would like someone to write a script for me.
However i am still searching for documentation on this subject
Thanks Graham



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


Re: mod_python installation problem ..severity High

2006-11-14 Thread Graham Dumpleton
boney wrote:
> On doing this and duly following the installation and testing
> instructions at www.modpython.org and then pointing the url as
> http://localhost/test/mptest.py, i get the following error on the
> server log file:
>
> [Tue Nov 14 15:17:47 2006] [error] make_obcallback: could not import
> mod_python.apache.\n
> [Tue Nov 14 15:17:47 2006] [error] make_obcallback: Python path being
> used "['C:Program FilesApache
> GroupApache2binpython24.zip', '.DLLs', '.lib',
> '.libplat-win', '.liblib-tk', 'C:Program
> FilesApache GroupApache2bin']".
> [Tue Nov 14 15:17:47 2006] [error] python_handler: no interpreter
> callback found.
> [Tue Nov 14 15:17:47 2006] [error] [client 127.0.0.1] python_handler:
> Can't get/create interpreter.

Your registry settings for Python do not include the site-packages
directory where mod_python typically gets installed. Thus, trying
updating the registry to add the site-packages directory. As a guide,
see the following post from the mod_python mailing list archive as a
guide.


http://www.modpython.org/pipermail/mod_python/2006-September/021979.html

This is for Python 2.5, but should give an indicator of what to look
for. Update the PythonPath entry.

For any mod_python help in the future, suggest you subscribe to the
mod_python mailing list and ask your question there as there is a
higher concentration of people there who are familiar with the
software. Details of the mod_python mailing list are on the mod_python
web site.

Graham

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


Re: RSS aggregator with curses and feedparser

2006-09-24 Thread James Graham
Roberto Bechtlufft wrote:
> And another thing: feedparser returns the result entries as
> dictionaries. What's the best approach to create my cache file? I see
> that the cache file in liferea is an xml file. Should I try to create
> my own xml file based on the results from feedparser?

Well you could do, using elementtree or whatever but there's no 
particular reason to use XML over anything else. It's semi-human 
readable which is nice but, if you're just serializing dicts some json 
library (e.g. [1]) might do all you need out of the box. Alternatively, 
if you don't care about the local format being human-readable, you could 
simply use the built-in pickle module to save your state.

> Thanks for your help.

(note that people tend to dislike top posting because, as you can see, 
it tends to screw up the order of replies).

> Roberto Bechtlufft wrote:
>
>> A question: how do I tell my program that a certain entry was/wasn't
>> downloaded yet? Should I use the date or link tag of the entry?
> 
Assuming the feed is atom, you want to look at the entry's GUID to 
determine whether you have already downloaded it. That may also work for 
RSS feeds although I'm not sure how well RSS feeds in the wild stick to 
the "Globally Unique" part of GUID... but this is more of a feed 
handling question than a python one.

[1] http://cheeseshop.python.org/pypi/python-json/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: WebScraping

2006-11-19 Thread Graham Feeley
Well I would like to publicly thank Bernard Chhun for actually writing this 
script and "pretting " it up for me.
He is truly a talented guy.
He used Beautifull Soup and Regex which i am still coming to terms trying to 
understand them any way Thanks again Bernard.
Regards
graham

"Graham Feeley" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Can someone steer me to scripts / modules etc on webscraping please???
> Ultimately I would like someone to write a script for me.
> However i am still searching for documentation on this subject
> Thanks Graham
>
>
> 


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


Re: Python, WSGI, legacy web application

2006-11-22 Thread Graham Dumpleton

Ben Finney wrote:
> Howdy all,
>
> I'm working on a web application that is starting to gain a lot of
> back-end code written in Python. However, all the current interface
> code is written in legacy PHP. I'd like to slowly introduce new
> features as Python WSGI programs.
>
> Is it possible to write a Python WSGI program that talks to a PHP
> program as its "back end"? Where can I find out how to do this,
> preferably with examples?
>
> The ideal here is to keep all the existing code as is, but write
> little or no new PHP code. Instead, iteratively change the interface,
> replacing pieces of the monolithic legacy PHP interface with modular
> WSGI programs over time.

Look at mod_python for Apache. If you use it correctly you can on a
page by page basis as need be, replace the existing PHP pages with
equivalents written using Python. You could do this by programming
right at the level of mod_python, or again, if done right by using WSGI
on top of mod_python. If you need to maintain compatibility of URLs,
you could even do things so that even though URLs use .php, that it
maps to Python code underneath, thus easing any transition.

If you are interested in this path, the mod_python mailing list may be
a good place to go to discuss the mod_python aspects of this. The
mod_python mailing list details are on the mod_python web site at
www.modpython.org.


Graham

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


Re: WSGI with mod_python (was: Python, WSGI, legacy web application)

2006-11-23 Thread Graham Dumpleton
Paul Boddie wrote:
> Rob De Almeida wrote:
> > Ben Finney wrote:
> > > I was under the impression that WSGI in mod_python was a rather kludgy
> > > way to do WSGI, but I don't know what the alternatives are. CGI?
> > > Python http server (e.g. CherryPy)? Something else?
> >
> > You can use FastCGI or SCGI too, with Apache, lighttpd or Cherokee.
>
> I think the motivation behind suggesting an Apache solution was that
> you'd be able to migrate the PHP resources you already have running in
> Apache (I assume, since PHP can run in other places these days) to
> mod_python whilst staying within the Apache environment, rather than
> having to maintain a number of different environments at the same time.
> In other words, you'd write your replacement resources using WSGI (or
> whatever) on mod_python (for performance), CGI (for relative
> simplicity), or some other connection technology, and then it'd just be
> a matter of changing the various directives and having Apache figure it
> out.

Correct.

As example, imagine you have written a mod_python handler which itself
interprets how to map a URL to something to implement the URL. This
might map to a WSGI application or to some system of basic mod_python
handlers.

Within the .htaccess file of the directory where all your PHP files
live you could then write:

  PythonHandler myphppagereplacementhandler | .php

At this point nothing will happen, but then one could do the following:

  
  SetHandler mod_python
  

For the one page called 'index.php' the mod_python handler would be
called instead of PHP. As a Python equivalent for each PHP page is
written, just need to trigger the mod_python handler to be used by
using the Files directive.

One could also have different handlers for each page and use Apache to
make the selection if wanted to:

  
  SetHandler mod_python
  PythonHandler myphppagereplacementshandler::index
  

Now I am sure that some will say it looks messy, but as far as trying
to do a progressive replacement of pages and maintain URLs, it is
probably the quickest way. It should be said that any progressive
migration like this is likely to be a bit messy.

Don't like this, then another way using Apache might be to use
mod_rewrite to remap URLs to new URLs which use Python code. Using
mod_rewrite can be a pain though. Yet another way may be to use
mod_proxy to selectively forward URLs through to a separate back end
web server if you are Apache phobic and want to use a web server
written in pure Python.

Overall, Apache/mod_python has a lot to offer, but from what I have
seen most Python web frameworks simply uses it as a jumping off point
and not much else.

Graham

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


Re: Mod_python vs. application server like CherryPy?

2006-12-06 Thread Graham Dumpleton

Vincent Delporte wrote:
> On 5 Dec 2006 17:05:06 -0800, "fumanchu" <[EMAIL PROTECTED]> wrote:
> >In a nutshell, mod_python gives you
> >access from Python to the Apache API, whereas CherryPy and friends give
> >you their own API.
>
> I didn't know Apache had an API of its own, or that it was even needed
> when writing a web application in Python. What does it provide in
> addition to Python/mod_python?

Although Apache does have a quite considerable underlying API of its
own, mod_python doesn't actually provide direct access to much of it
from Python code. What mod_python does provide is still adequate for
getting basic stuff done, but Apache could perhaps be better harnessed
if all the Apache API were exposed and available.

Where the power of Apache comes into play is the ability to compose
together the functionality of different Apache modules to build up an
application. That is, you aren't just doing everything in Python code.
That said though, this doesn't mean you have to go off and write code
in another language such as C. This is because the Apache modules are
glued together through the Apache configuration files with some
features also being able to be enabled from Python code running under
mod_python.

In some respects you need to see the whole of Apache as being a
platform for building a web application. Unfortunately, most Python web
application developers don't see that and simply use Apache as a
convenient hopping off point for the main content handling phase of a
request. Even where people do write stuff which makes use of mod_python
as more than just a hopping off point, more often than not they still
work within just mod_python and don't bring in other parts of Apache to
any degree.

For example, consider an extreme case such as WSGI. Through a goal of
WSGI being portability it effectively ignores practically everything
that Apache has to offer. Thus although Apache offers support for
authentication and authorisation, a WSGI user would have to implement
this functionality themselves or use a third party WSGI component that
does it for them. Another example is Apache's support for enabling
compression of content returned to a client. The WSGI approach is again
to duplicate that functionality. Similarly with other Apache features
such as URL rewriting, proxying, caching etc etc.

Although WSGI is an extreme case because of the level it pitches at,
other systems such as CherryPy and Django aren't much different as they
effectively duplicate a lot of stuff that could be achieved using more
basic functionality of Apache as well. Once one starts to make use of
the underlying Apache functionality, you are binding yourself to Apache
though and your stuff isn't portable to other web servers. Also, your
job in part becomes more about integrating stuff to come up with a
solution, rather than just writing pure Python code, something that
many Python coders possibly wouldn't find appealing. :-)

Graham

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


Re: Mod_python vs. application server like CherryPy?

2006-12-06 Thread Graham Dumpleton

Vincent Delporte wrote:
> On 6 Dec 2006 14:55:58 -0800, "Graham Dumpleton"
> <[EMAIL PROTECTED]> wrote:
> >Although WSGI is an extreme case because of the level it pitches at,
> >other systems such as CherryPy and Django aren't much different as they
> >effectively duplicate a lot of stuff that could be achieved using more
> >basic functionality of Apache as well.
>
> Mmm... So how can I use those goodies from Apache? Just through their
> configuration files, or do I have to somehow call them from Python?
>
> Is the fact that Python developers tend to ignore resources in Apach
> due to difficulties in making calls from Python, making the scripts
> unpythonic?

It depends on what you are doing. For example, if you need to do URL
rewriting, you use the mod_rewrite module for Apache, not that it is
the most pleasant thing to use. If you need to proxy through some
subset of requests to a downstream web server, use mod_proxy. If you
need to compress content going back to clients, use mod_deflate. If you
need to do caching you use mod_cache.

How to configure each of these from the Apache configuration files, you
need to look at the Apache httpd documentation at httpd.apache.org.

Some, like mod_proxy and mod_deflate can be triggered from within
mod_python although finding the magic required isn't always straight
forward. How you setup responses can also control mod_cache.

If anything, the reason that Python developers tend to ignore a lot of
what Apache has to offer is that it means understanding Apache. The
Apache documentation isn't always the easiest thing to understand and
for some things it even requires looking at the Apache source code to
work out how to do things. The mod_python documentation at the moment
doesn't help either, as it doesn't provide much in the way of recipes
for doing things. The new mod_python wiki will hopefully address that
over time, but right now the mod_python mailing lists are the best it
gets.

In terms of whether it is 'unpythonic', what should be more important
is whether it gets the job done in a way that makes best use of what is
available. If you want something to be 'pythonic', then using Apache as
a framework probably isn't what you want, as as I said previously it
becomes more about integrating things rather than writing pure Python
code.

Getting perhaps back to the answer you were seeking right back at the
start, that is if you are new to web application and development and
Python, then you may well be better of just using a higher level
framework as they will make it easier and isolate you from any pains in
understanding Apache and how to use it properly.

Graham

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


Re: apache & mod_python

2006-12-09 Thread Graham Dumpleton

Maxim Sloyko wrote:
> m.banaouas wrote:
>
> > Can i install and use  "Apache 2.2.3" & "mod_python 3.2.10" (most recent
> > versions) without facing any known major issue ?

Only that to use Apache 2.2 you must have mod_python 3.2.10 or later,
older versions of mod_python do not work with the more recent version
of Apache.

> Works fine for me.
> The only "known major issue" you can face is general non-threadsafety
> of Python interpreter. So, if you are using Apache MPM, you have to
> allow for it, or use framework that does it for you.

You answer here is a bit misleading. There are no issues with the
thread safety of the Python interpreter per-se. However, as with any
Python application, whether mod_python is used or not, if multiple
threads are being used your application code may have to be written so
as to cope with access to code/data from multiple threads at the same
time. This is not anything unique to mod_python.

Whether this will be an issue or not is dependent on which Apache MPM
is used, not that the Apache MPM is used as one will always be used.
The specific Apache MPMs where multithreading has to be taken into
consideration are "winnt" on Windows platforms and "worker" on UNIX
platforms. What this all means is that when these MPMs are used there
can be concurrent request handlers executing in distinct threads within
the same Apache processes. Thus, where common data is accessed, it has
to be adequately protected.

For further details on the Apache/mod_python process/interpreter/thread
model, see:


http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreterModel

That said, there are sometimes threading issues with mod_python but it
is more to do with the fact that mod_python can use multiple
interpreter instances within a single process. Also, the problems are
not a problem in mod_python, but result from third party C modules for
Python being used which take the simple path of using the simplified
thread API for working with the GIL. Such modules often will not work
properly when used from secondary Python interpreter instances. In
these cases it is a simple matter of telling mod_python to handle
requests which are dependent on those modules within the main Python
interpreter, ie., the very first one which gets created. Where third
party C modules use the full thread API for Python properly, it doesn't
matter which interpreter instance they are used from.

Graham

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


Re: Mod_python vs. application server like CherryPy?

2006-12-09 Thread Graham Dumpleton

Damjan wrote:
> > For example, consider an extreme case such as WSGI. Through a goal of
> > WSGI being portability it effectively ignores practically everything
> > that Apache has to offer. Thus although Apache offers support for
> > authentication and authorisation, a WSGI user would have to implement
> > this functionality themselves or use a third party WSGI component that
> > does it for them.
>
> OTOH
> WSGI auth middleware already supports more auth methods than apache2 itself.

A distinction needs to be made here. HTTP supports Basic and Digest
authentication mechanisms, both of which are in Apache by default. The
authentication mechanism though needs to be seen as distinct from the
auth provider, that is, who actually validates that a user is valid.
Apache 2.2 separates these two things with there being a pluggable auth
provider facility with backend implementations for such things as
passwd like files, dbm database, ldap and using mod_dbd various SQL
databases. Because it is pluggable it can be extended to support any
sort of auth provider implementation you want. It is even technically
possibly to write an auth provider which makes used of Python to
perform the check using some custom system, although mod_python itself
doesn't provide this yet, so you need to roll your own auth module to
do it.

Even as far as authentication mechanisms go, you aren't limited to just
those as the fact that you can provide a complete authentication and/or
authorisation handler means you can implement other as well. You might
for example build on top of SSL and use client certificates to control
access, or you could use HTTP forms based login and sessions. These
custom mechanisms could also use the auth provider plugin system so
that they aren't dependent on one particular user validation mechanism.

Now, when you say that WSGI already supports more auth methods than
Apache 2 itself, are you referring to how the login/authentication is
handled over HTTP, or how client validation is handled, ie., auth
providers.

I am not being critical here, asking more to build my knowledge of WSGI
and what capabilities it does have.

> > Similarly with other Apache features
> > such as URL rewriting, proxying, caching etc etc.
>
> Well, not everybody can use Apache ... and again there's already WSGI
> middleware that's more flexible than the Apache modules for most of the
> features you mention.
>
> It's not that I think mod_python doesn't have uses.. I just think it's not
> practical to make python web applications targeted solely to mod_python.

For what you are doing that may not be practical, but in a lot of other
places it would be a more than reasonable way of going about it. To
clarify though, I am not talking about building something which is
completed implemented within the context of mod_python alone and not
even something that is written in Python exclusively. What I have been
talking about is using Apache as a whole as the platform.

Thus, taking authentication as an example, for basic forms of
authentication it is best to use the standard mod_auth and auth
provider facilities of Apache to do it. For more complicated mechanisms
such as using HTTP form, more customisation is usually required and
this might be implemented using Python under mod_python but could just
as well be implemented in mod_perl. A main thing to note though is that
if Apache authentication handlers are written properly, it can be used
to span not just mod_python but apply to static files served by Apache,
as well as handlers which serve up content using other languages
modules such as PHP, mod_perl.

This is where I said it can be more about integration rather than
writing pure Python code, because if you use Apache and mod_python
properly, you don't end having to write code within a monoculture
consisting of one implementation language as you are if you use WSGI.
Instead, you can pick and choose the modules and languages which make
the most sense as far as allowing you to implement something in the
easiest way possible given what you have and the environment you are
operating in

All I can say is that since this is a Python language list, that many
here will be here because they want to program specifically in Python
and nothing else. Others though may well see the bigger picture and the
realities of working with big systems, especially in a corporate
environment, where one doesn't have a choice but to deal with code
developed over time and in different languages with a result that most
of the time you are writing more glue and than actual application code.
Thus, in an ideal world you might be able to write in Python and
nothing else, but it isn't always an ideal world.

Graham

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


Re: mod_python.so is garbled mod_python.so is garbled

2006-12-13 Thread Graham Dumpleton

blbmdsmith wrote:
> Has anyone seen the following error while starting httpd:
>
> Starting httpd: httpd: Syntax error on line 54 of
> /usr/local/apache2/conf/httpd.conf: API module structure
> `python_module' in file /usr/local/apache/modules/mod_python.so is
> garbled - perhaps this is not an Apache module DSO
>
> I am running python2.5 with apache server 2.2.3, using
> mod_python-3.2.10
> I ran mod_python configure with-apxs= /usr/local/apache/bin/apxs
> --with-python=/usr/bin/python
>
> I have configured httpd.conf with the follwing lines:
>
> LoadModule python_module /usr/local/apache/modules/mod_python.so
>
> 
> AllowOverride FileInfo
> AddHandler mod_python .py
> PythonHandler mptest
> PythonDebug On
> 
>
> Thanks,
> Bill
>
> I have posted the same message on the mod_python group.  Is this a
> better group to post his message?

The "mod_python" Google group doesn't get used. You should be
subscribing to and posting on the mod_python mailing list for best
chances of a response. Mailing list details are on the mod_python web
site.

Should you have perhaps used:

  --with-apxs=/usr/local/apache2/bin/apxs

Ie., is the version of Apache you compiled for actually the version you
are running it with?

Do you have multiple versions of Apache installed on your system?

Graham

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


Re: Apache 2.2.3 and mod_python 3.2.10

2006-12-19 Thread Graham Dumpleton

m.banaouas wrote:
> sorry, I give here the right paths:
>
> I installed Apache 2.2.3 and mod_python 3.2.10 on WinXP plateform
> I configured mod_python via httpd.conf:
> LoadModule python_module modules/mod_python.so
>
> but my script folder configuration doesn't work correctely:
>
> Alias /myfolder D:/myfolder
>
> 
>  Order allow,deny
>  Allow from all
>  AddHandler mod_python .py
>  PythonHandler mod_python.publisher
>  PythonDebug On
> 
>
> for test, this is a sample script d:\myfolder\test.py
> # test.py
> #
> from mod_python import apache
> #
> def hello(name=None):
>  if name:
>  return 'Hello, %s!' % name.capitalize()
>  else:
>  return 'Hello there!'
> #
> def handler(req):
>req.content_type = 'text/plain'
>req.write("from handler test, Hello World!")
>return apache.OK
>
> when I access to the url  http://localhost/myfolder/test.py,
> I obtain source test.py listing but not the rendering of handler or
> hello method.
>
> with url http://localhost/myfolder/test.py/hello, I obtain :
> =>The requested URL /myfolder/test.py/hello was not found on this server.
>
> It seems like something is missing ... but what ?
>
> thanks for any help

Get it working for a normal handler first and don't use
mod_python.publisher.

For a set of instructions on how to get a simple handler working,
including descriptions of common problems and how to debug it, see:


http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking

Once you have confirmed that your installation works, then consider
looking at mod_python.publisher.

I'd also suggest that you get yourself on the mod_python user mailing
list and seek help there as the mod_python community there is much
larger. Mailing list details are on the mod_python web site.

BTW, don't you mean:

  

if that is what Alias uses?

Also, don't mix normal handlers and mod_python.publisher functions in
the same module, and don't use files called 'test.py' as there is a
standard Python module by the same name and if things aren't set up
right you can be picking up the wrong module.

Graham

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


Re: Page layouts in mod_python?

2006-12-19 Thread Graham Dumpleton

Bruno Desthuilliers wrote:
> Michael a écrit :
> > Hey everyone,
> >
> > Is it possible to automatically insert headers/footers using
> > mod_python?
> > I will be not be using PSP's, so I cannot use the PSP/include solution.
> > Furthermore, the header will be dynamic; it won't be a static HTML
> > page.
> >
> > In short, I've been looking for a page layout facility using
> > mod_python.
> > (Similar to the layout capability in Ruby on Rails.)
>
> mod_python is mainly a librairy exposing the apache API to Python - not
> a full-stack db-to-web framework. What you want here is a HTML
> templating system. There are quite a lot available in Python - look for
> SimpleTAL, Genshi, Myghty, Jinja, Cheetah, etc...

Contrary to the above advise, you could actually use mod_python to do
what you want and you do not need a full stack to do it. Whether you
would want to use just mod_python is another question and the answer
would depend on a bit on what form the HTML files you want to modify
are in or what is generating them.

The first option available is to write an Apache output filter using
mod_python which takes the content of the page and searches for the
 and  tags and inserts at those points the appropriate
header/footer. Using this scheme, because you are looking for tags
already within the HTML page, you do not need to modify the original
HTML source files. Because it is an output filter, the technique will
work on both static files and on files which have been generated
dynamically by PHP, mod_perl, mod_python or any other Apache response
handler capable of generating dynamic pages.

For this first technique, you do not even have to use mod_python if you
didn't want to, as there are other Apache modules which have been
designed to specifically use this technique to insert headers and
footers in pages. These other modules are written in C and so are
probably going to be quicker as well as more flexible without you
having to do too much hard work.

The second option, although not strictly mod_python specific, is to
enable server side include processing for HTML files. For this to work
you would need to modify the original HTML source files or what is
generating them to add in appropriate SSI directives to include the
header/footer. Again, because SSI is performed as an output filter,
source files can be static or dynamically generated.

If using basic SSI, the include of the header/footer to get dynamic
information would be done as a virtual include. That is, it generates
an Apache subrequest to a handler which generates the header/footer.
This handler could be implemented as a CGI script, mod_python handler,
mod_perl handler or even PHP.

Alternatively, in mod_python 3.3 (soon to be released), one can use the
new ability to use Python code in conjunction with SSI. For dynamic
header/footer snippets, this would involve adding callouts to pages to
Python code setup using mod_python.

For an example of header/footer generation using Python code, see:


http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfServerSideIncludes

If you particularly want to use mod_python, for further discussion on
this topic and what may be best for what you want to do, I would
suggest you might take the conversation over to the mod_python user
mailing list. Details for the list are on the mod_python web site.

Graham

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


Re: ANNOUNCE: Mod_python 3.3.0b (Beta)

2006-12-25 Thread Graham Dumpleton

derekl00 wrote:
> Gregory (Grisha) Trubetskoy wrote:
> > The Apache Software Foundation and The Apache HTTP Server Project are
> > pleased to announce the 3.3.0b (Beta) release of mod_python.
>
> How long does it usually take for these things to make there way into
> the Fedora (or other distro) repositories?

Given that this is a beta, I would hope that the distro people don't
get on to it quickly or at all. It was a right pain the last time a
beta of mod_python was released and RPMs got distributed, as it took us
a lot longer to get rid of the beta version out of the pipeline when
the non beta was actually released.

Thus, if you really want to try out this beta, compile it from source
code, otherwise wait for the official non beta release. If you don't
like compiling from source, then you possibly aren't the sort of person
who should be using this beta version in the first place, especially
when compilation on different platforms is always a part of what we
want tested in releasing a beta.
 

Graham

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


Re: Mod_python

2006-12-27 Thread Graham Dumpleton

Maxim Sloyko wrote:
> Lad wrote:
> > In my web application I use Apache and mod_python.
> > I allow users to upload huge files( via HTTP FORM , using POST method)
> > I would like to store the file directly on a hard disk and not to
> > upload the WHOLE huge file into  server's memory first.
> > Can anyone suggest a solution?
>
> The only solution you need is Apache and mod_python :)
> I mean,  Apache won't load a huge POST request into its memory no
> matter what. All file uploads will be stored in temporary files. Under
> mod_python (provided you use FieldStorage) you'll need to deal only
> with 'file' objects.

Note though that the default behaviour of FieldStorage is to store
files in whatever directory the 'tempfile' module uses. This may not
always be acceptable, especially with very large files, as the user may
want to have the files actually reside elsewhere in some special
directory and having to make a copy of the file, if a different file
system, may in itself cause issues.

Thus, provided you are using a recent version of mod_python, you still
may have to customise how files are handled using the FieldStorage file
callbacks to have the file stored in the desired location from the
outside.

The other alternative to FieldStorage is to write a custom
Apache/mod_python input filter to process uploads. An example of this
is Tramline (http://www.infrae.com/products/tramline).

Graham

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


Re: per interpreter storage for C extensions

2006-12-28 Thread Graham Dumpleton

Chris Mellon wrote:
> I'm not that familiar with
> mod_python but I'm surely each python interpreter is in a different
> thread (if not process) than the others.

No.

In mod_python there can be multiple distinct interpreter instances in
each Apache child process. If using a multithreaded Apache MPM, there
can be multiple active threads in each Apache child process and more
than one thread can be executing within any one particular interpreter
instance within a process at the same time.

For more details about the Apache/mod_python process/interpreter models
read:


http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreterModel

Graham

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


Re: Question about the "new" module

2006-12-29 Thread Graham Dumpleton

[EMAIL PROTECTED] wrote:
> Gabriele> I'm using Python 2.5 to develop a simple MVC framework based
> Gabriele> on mod_python. To load my controllers, I create new modules
> Gabriele> using the "new" module like this:
>
> Gabriele> # 
> Gabriele> my_module = new.module("random_name")
> Gabriele> my_module.__file__ = module_path
> Gabriele> exec open(module_path, "r") in my_module.__dict__
>
> Gabriele> then I initialize the class defined inside the module and call
> Gabriele> a method of this class based on the HTTP request.
>
> Why use the new module?  Why not call __import__() or execfile()?  Details
> on their use are here:
>
> http://docs.python.org/dev/lib/built-in-funcs.html

Or why not use mod_python.apache.import_module() from mod_python
itself. It is designed as a way of users importing specific modules,
including the capability for modules to be reloaded if they have been
changed. See:


http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfModuleImporting

as well as the mod_python documentation.

Is recommended though to use mod_python 3.3 as soon as you can though
if you want the module reloading to always work. You can find
descriptions of problems with the module reloading in older versions of
mod_python at:


http://www.dscpl.com.au/wiki/ModPython/Articles/ModuleImportingIsBroken

Note that module importer in mod_python 3.3 has been enhanced some what
over prior versions in addition to fixing problems. An important new
feature is being able to load modules based on their path with modules
with same names in different directories being properly distinguished,
something which is quite useful where modules are used in web
applications to represent pages. You may want to look at documentation
for it at:


http://www.modpython.org/live/mod_python-3.3.0b/doc-html/pyapi-apmeth.html

Graham

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


[ANN] html5lib 0.2

2007-01-09 Thread James Graham
DESCRIPTION

HTML parsing library based on the WHATWG Web Applications 1.0 "HTML5"
specification[1]. The parser is designed to work with all existing 
flavors of HTML and implements well-defined error recovery that has been 
specified though analysis of the behavior of modern desktop web browsers.

html5lib currently allows parsing to both a custom "simpletree" format 
and to an ElementTree, if available. Future releases will include 
support for at least one DOM implementation, and it is possible to 
implement custom treebuilders although the API should not yet be 
considered stable.

DOWNLOAD

http://html5lib.googlecode.com/files/html5lib-0.2.zip

BUGS

This is the first release of html5lib and it is considered alpha quality 
software. However, it ships with over 230 passing unit tests covering 
most of the specified behavior. Bugs should be reported on the issue 
tracker [2]

KNOWN ISSUES

Error handling does not yet conform to the specification; not all errors 
are reported and the error messages are not informative.

PROJECT PAGE

More information about the project including documentation and 
information on getting involved is available on the project page:
http://code.google.com/p/html5lib/

[1] http://whatwg.org/specs/web-apps/current-work/
[2] http://code.google.com/p/html5lib/issues/list
-- 
http://mail.python.org/mailman/listinfo/python-list


injecting functions into a python sandbox within a python program

2007-01-09 Thread Graham Menhennitt
I have a large Python 2.5 program that I want my users to be able to 
"extend" using a Python script. However, I want their script to run in a 
sandbox within the overall program so that they only have access to the 
rest of the program via a single simple interface. Note that this is not 
meant to be a real anti-hacker type security sandbox - just "help them 
to avoid shooting themselves in the foot" type security.

So I created a single object that has the interface that I want them to 
access. I call their script via "exec" passing the single interface 
object in the "globals" parameter to exec. It (conceptually) looks like 
this:

i = Interface()
glob = { 'i': i }
exec script in glob

Then they can call i.whatever() from within their script. This all works 
fine.

Now, what I want to do is provide some "helper" functions for them to 
use in the script. These functions still only access the rest of the 
program via 'i'. They just wrap some of the interface functions to make 
life easier for the user. My current solution is to prepend these 
functions onto the start of the script. I.e.

helperFuncs = """
def f1(): i.whatever()
"""

exec helperFuncs + "\n" + script.read() in glob

This works but doesn't seem very elegant.

I've tried defining the helper funcions in my caller and passing them 
through the globals i.e.

def f1(): i.whatever()
glob = { 'i': i, 'f1': f1 }
exec script in glob

The problem here is that the functions don't have access to the global 
variable 'i'. I need to use that object instance since it has other 
functionality that is required to interface to the rest of the program.

I'm sure that this is simple to get around for a Python expert (which I 
am not!). Does anybody have any ideas? Any alternate approach?

Thanks in advance for any assistance,
Graham
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the problem wtih cgi

2005-05-27 Thread Graham Fawcett
chris patton wrote:
> Hi everyone.
>
> Has anyone seen that problem with running a python cgi script in a
> server?
> It takes you to myspace.com/redmartian or something. Anyway, does
> anyone know when this problem will be fixed?

It could be solved much sooner if you follow these steps:

(1) Carefully read http://www.catb.org/~esr/faqs/smart-questions.html
(2) consider how your original question reads to an audience that
doesn't know anything about your script or your server
(3) Based on (1) and (2), reformulate your question
(4) When you post your revised question, be sure to include your CGI
script's code.

-- Graham

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


Re: Multiple python interpreters within the same process

2007-06-09 Thread Graham Dumpleton
On Jun 10, 9:07 am, Josiah Carlson <[EMAIL PROTECTED]>
wrote:
> André wrote:
> > On Jun 9, 5:00 pm, "Marcin Kalicinski" <[EMAIL PROTECTED]> wrote:
> >> How do I use multiple Python interpreters within the same process?
>
> >> I know there's a function Py_NewInterpreter. However, how do I use 
> >> functions
> >> like Py_RunString etc. with it? They don't take any arguments that would
> >> tell on which interpreter to run the string...?
>
> >> Marcin
>
> > You may want to look at the code 
> > modulehttp://docs.python.org/lib/module-code.html
>
> That's completely unrelated.
>
> To answer Marcin's question, from what I understand, running multiple
> Python interpreters is not supported.  There are various issues with
> object sharing and refcounts, etc., and are unlikely to be fixed soon if
> ever.

I really don't know why people keep propagating this myth that you
can't run multiple Python interpreters. :-(

The Python C API has supported multiple Python interpreter instances
within a single process for a long time. If you write your C program
correctly there isn't a problem. The only real limitation is that
different Python interpreter instances can't use different versions of
a C extension module as they when loaded are shared across all Python
interpreter instances.

C extension modules can also cause other problems as well, but this
isn't the fault of Python but of the module writers. Specifically, if
a C extension module is written to only use the simplified API for GIL
locking it can only be used with the first Python interpreter instance
created. If they use the wider GIL locking API properly then there is
no problem with using it in other Python interpreter instances.
Another issue although one which you aren't likely to come across
unless you are doing really tricky stuff, is where a C extension
module was written so as to assume that once it is loaded into a
Python interpreter instance, that that interpreter will not be
destroyed. From what I have seen Pyrex generated C extension modules
possibly have this latter problem and will cause a process to crash if
a Python interpreter instance is destroyed and then a new one created
in its place.

As proof that all this stuff can work, have a look at mod_python and
mod_wsgi. Both make use of multiple Python interpreter instances in a
single process. The mod_wsgi documentation even points out the above
problems with C extension modules in latter sections of:

  http://code.google.com/p/modwsgi/wiki/ApplicationIssues

Graham

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

Re: How does mod_python know where's Python installed ?

2007-06-12 Thread Graham Dumpleton
On Jun 13, 7:03 am, arorap <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I recently setupmod_pythonsuccessfully and things work smooth.
> However, I do not remember tellingmod_pythonwhere to find Python
> installation. There's not environment variable which gives that
> information. As such how doesmod_pythonknow where to find Python ?
> When I print sys.path in my scripts it contains my python installation
> folder. I'm wondering how does Apache/mod_pythonget this
> information ?
>
> I'm running Apache 2.2 on windows.

Rather than duplicate here, for a long explanation see the comments in
the file:

  Modules/getpath.c

in the Python source code.

In short though, it searches for 'python' executable on PATH and from
that tries to determine where the corresponding 'lib' directory is for
the version of Python that mod_python is linked against.

Because it looks in PATH, this will not always work when people have
multiple versions of Python installed on their system with different
base directories. See:

  https://issues.apache.org/jira/browse/MODPYTHON-225

for further details in respect of that problem.

Graham


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


Re: cgi.FieldStorage() not working on Windows

2007-06-12 Thread Graham Dumpleton
On Jun 13, 1:17 am, arorap <[EMAIL PROTECTED]> wrote:
> I've mod_php installed with Apache 2.2. In one of my folders, I'm
> using the cgihandler as the PythonHandler as my target host runs
> python only as CGI. Here cgi.FieldStorage() doesn't seem to work. I
> can see the form data in sys.stdin but cgi.FieldStorage() returns an
> empty dictionary. Here's the code for the test script I am posting to
> -
>
> --
> #!/usr/bin/python
>
> import os
> import cgi
> import sys
>
> print "Content Type: text/plain\n\n"
> print "Hello CGI World !\n"
>
> for key in os.environ:
>   print key + "= " + os.environ[key]
>
> print cgi.FieldStorage()
>
> print sys.stdin.read()
> --
>
> And here's the output I see ..
>
> --
> Hello CGI World !
>
> HTTP_REFERER=http://learnpython/form.htm
> SERVER_SOFTWARE= Apache/2.2.4 (Win32)mod_python/3.3.1 Python/2.5.1
> SCRIPT_NAME= /mptest.py
> SERVER_SIGNATURE=
> REQUEST_METHOD= POST
> SERVER_PROTOCOL= HTTP/1.1
> QUERY_STRING= abc=ayz
> PATH= C:\Program Files\Internet Explorer;;C:\WINDOWS\system32;C:
> \WINDOWS;C:\WINDOWS\System32\Wbem;q:\bin;m:\cm\clearcase\bin;M:\PERL\NT
> \EXEC\BIN;m:\cm\clearcase\bin\nt;M:\Perl\NT\EXEC\BIN;m:\perl\nt\exec
> \bin;m:\cm\clearcase\utils;q:\bin;m:\opus;m:\tvcs;C:\highc331\bin;C:
> \Program Files\Rational\ClearCase\bin;C:\Program Files\Rational\common
> CONTENT_LENGTH= 86
> HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
> SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
> HTTP_CONNECTION= Keep-Alive
> SERVER_NAME= learnpython
> REMOTE_ADDR= 127.0.0.1
> PATHEXT= .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
> SERVER_PORT= 80
> SERVER_ADDR= 127.0.0.1
> DOCUMENT_ROOT= D:/Projects/LearnPython/www
> COMSPEC= C:\WINDOWS\system32\cmd.exe
> SCRIPT_FILENAME= D:/Projects/LearnPython/www/mptest.py
> SERVER_ADMIN= [EMAIL PROTECTED]
> HTTP_HOST= learnpython
> SystemRoot= C:\WINDOWS
> HTTP_CACHE_CONTROL= no-cache
> REQUEST_URI= /mptest.py?abc=ayz
> HTTP_ACCEPT= */*
> WINDIR= C:\WINDOWS
> GATEWAY_INTERFACE= Python-CGI/1.1
> REMOTE_PORT= 1081
> HTTP_ACCEPT_LANGUAGE= en-us
> CONTENT_TYPE= application/x-www-form-urlencoded
> HTTP_ACCEPT_ENCODING= gzip, deflate
>
> FieldStorage(None, None, [])
>
> firstName=puneet&address=hawaii
> --
>
> I am posting to this script using a form with two text fields named
> firstName and address.
>
> any clue where am I going wrong ?

You don't need mod_python/cgihandler to run CGI scripts. Rather than
bring mod_python into the picture and confuse things, set up Apache to
run your script as a traditional CGI script instead.

BTW, the fact that mod_python is loaded means that CGI scripts aren't
the only way of using Python available to you as you seem to think.
So, suggest you do some research as to what the differences are
between CGI and mod_python.

Graham

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


Re: cgi.FieldStorage() not working on Windows

2007-06-13 Thread Graham Dumpleton
On Jun 13, 12:58 pm, arorap <[EMAIL PROTECTED]> wrote:
> Thanks for your reply.
>
> The reason I want to run it as CGI (even though mod_php is available
> on my local computer

Why do you keep mentioning mod_php, surely you mean mod_python.

> is that the target machine to which I will
> finally be uploading my scripts runs CGI.
>
> cgihandler should work just like CGI.

I wouldn't rely on it being exactly the same. The way it works uses a
number of kludges. Also, the mod_python.cgihandler code in mod_python
doesn't really get much attention from mod_python developers anymore
and not sure if it was even specifically retested when mod_python 3.3
was released.

> Any clue why the
> cgi.FieldStorage()might not be working ?

Have no idea why it doesn't work as works as written on MacOS X even
when mod_python.cgihandler is used.

You'll have to get someone else who has Windows to try it. You might
be better off going to the mod_python mailing list to get help, or
just use plain old CGI instead since using mod_python isn't really
going to gain you much anyway.

Graham

> On Jun 12, 7:59 pm, Graham Dumpleton <[EMAIL PROTECTED]>
> wrote:
>
> > On Jun 13, 1:17 am,arorap<[EMAIL PROTECTED]> wrote:
>
> > > I've mod_php installed with Apache 2.2. In one of my folders, I'm
> > > using the cgihandler as the PythonHandler as my target host runs
> > > python only as CGI. Here cgi.FieldStorage() doesn't seem to work. I
> > > can see the form data in sys.stdin but cgi.FieldStorage() returns an
> > > empty dictionary. Here's the code for the test script I am posting to
> > > -
>
> > > --
> > > #!/usr/bin/python
>
> > > import os
> > > import cgi
> > > import sys
>
> > > print "Content Type: text/plain\n\n"
> > > print "Hello CGI World !\n"
>
> > > for key in os.environ:
> > >   print key + "= " + os.environ[key]
>
> > > print cgi.FieldStorage()
>
> > > print sys.stdin.read()
> > > --
>
> > > And here's the output I see ..
>
> > > --
> > > Hello CGI World !
>
> > > HTTP_REFERER=http://learnpython/form.htm
> > > SERVER_SOFTWARE= Apache/2.2.4 (Win32)mod_python/3.3.1 Python/2.5.1
> > > SCRIPT_NAME= /mptest.py
> > > SERVER_SIGNATURE=
> > > REQUEST_METHOD= POST
> > > SERVER_PROTOCOL= HTTP/1.1
> > > QUERY_STRING= abc=ayz
> > > PATH= C:\Program Files\Internet Explorer;;C:\WINDOWS\system32;C:
> > > \WINDOWS;C:\WINDOWS\System32\Wbem;q:\bin;m:\cm\clearcase\bin;M:\PERL\NT
> > > \EXEC\BIN;m:\cm\clearcase\bin\nt;M:\Perl\NT\EXEC\BIN;m:\perl\nt\exec
> > > \bin;m:\cm\clearcase\utils;q:\bin;m:\opus;m:\tvcs;C:\highc331\bin;C:
> > > \Program Files\Rational\ClearCase\bin;C:\Program Files\Rational\common
> > > CONTENT_LENGTH= 86
> > > HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
> > > SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
> > > HTTP_CONNECTION= Keep-Alive
> > > SERVER_NAME= learnpython
> > > REMOTE_ADDR= 127.0.0.1
> > > PATHEXT= .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
> > > SERVER_PORT= 80
> > > SERVER_ADDR= 127.0.0.1
> > > DOCUMENT_ROOT= D:/Projects/LearnPython/www
> > > COMSPEC= C:\WINDOWS\system32\cmd.exe
> > > SCRIPT_FILENAME= D:/Projects/LearnPython/www/mptest.py
> > > SERVER_ADMIN= [EMAIL PROTECTED]
> > > HTTP_HOST= learnpython
> > > SystemRoot= C:\WINDOWS
> > > HTTP_CACHE_CONTROL= no-cache
> > > REQUEST_URI= /mptest.py?abc=ayz
> > > HTTP_ACCEPT= */*
> > > WINDIR= C:\WINDOWS
> > > GATEWAY_INTERFACE= Python-CGI/1.1
> > > REMOTE_PORT= 1081
> > > HTTP_ACCEPT_LANGUAGE= en-us
> > > CONTENT_TYPE= application/x-www-form-urlencoded
> > > HTTP_ACCEPT_ENCODING= gzip, deflate
>
> > > FieldStorage(None, None, [])
>
> > > firstName=puneet&address=hawaii
> > > --
>
> > > I am posting to this script using a form with two text fields named
> > > firstName and address.
>
> > > any clue where am I going wrong ?
>
> > You don't need mod_python/cgihandler to run CGI scripts. Rather than
> > bring mod_python into the picture and confuse things, set up Apache to
> > run your script as a traditional CGI script instead.
>
> > BTW, the fact that mod_python is loaded means that CGI scripts aren't
> > the only way of using Python available to you as you seem to think.
> > So, suggest you do some research as to what the differences are
> > between CGI and mod_python.

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


Re: Trivial string substitution/parser

2007-06-17 Thread Graham Breed
Samuel wote:

> Thanks, however, turns out my specification of the problem was
> incomplete: In addition, the variable names are not known at compilation
> time.
> I just did it that way, this looks fairly easy already:
>
> ---
> import re
>
> def variable_sub_cb(match):
> prepend = match.group(1)
> varname = match.group(2)
> value   = get_variable(varname)
> return prepend + value
>
> string_re = re.compile(r'(^|[^\\])\$([a-z][\w_]+\b)', re.I)
>
> input  = r'In this string $variable1 is substituted,'
> input += 'while \$variable2 is not.'
>
> print string_re.sub(variable_sub_cb, input)
> ---

It gets easier:

import re

def variable_sub_cb(match):
return get_variable(match.group(1))

string_re = re.compile(r'(?http://mail.python.org/mailman/listinfo/python-list


Re: Trivial string substitution/parser

2007-06-18 Thread Graham Breed
Duncan Booth wote:

> Also, of course, vars just needs to be something which quacks like a dict:
> it can do whatever it needs to do such as looking up a database or querying
> a server to generate the value only when it needs it, or even evaluating
> the name as an expression; in the OP's case it could call get_variable.

And in case that sounds difficult, the code is

class VariableGetter:
def __getitem__(self, key):
return get_variable(key)

> Anyway, the question seems to be moot since the OP's definition of 'elegant
> and lazy' includes regular expressions and reinvented wheels.

Your suggestion of subclassing string.Template will also require a
regular expression -- and a fairly hairy one as far as I can work out
from the documentation.  There isn't an example and I don't think it's
the easiest way of solving this problem.  But if Samuel really wants
backslash escaping it'd be easier to do a replace('$$','') and
replace('\\$', '$$') (or replace('\\$','\\$$') if he really wants the
backslash to persist) before using the template.

Then, if he really does want to reject single letter variable names,
or names beginning with a backslash, he'll still need to subclass
Template and supply a regular expression, but a simpler one.

> ... and in another message Graham Breed wrote:
> > def get_variable(varname):
> > return globals()[varname]
>
> Doesn't the mere thought of creating global variables with unknown names
> make you shudder?

Not at all.  It works, it's what the shell does, and it's easy to test
interactively.  Obviously the application code wouldn't look like
that.


 Graham

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


Re: Trivial string substitution/parser

2007-06-19 Thread Graham Breed
Duncan Booth wote:

> If you must insist on using backslash escapes (which introduces the
> question of how you get backslashes into the output: do they have to be
> escaped as well?) then use string.Template with a custom pattern.

If anybody wants this, I worked out the following regular expression
which seems to work:

(?P\\)\$ | #backslash escape pattern
\$(?:
  (?P[_a-z][_a-z0-9]*)| # delimiter and Python identifier
  {(?P[_a-z][_a-z0-9]*)} | # delimiter and braced identifier
  (?P)  # Other ill-formed delimiter exprs
)

The clue is string.Template.pattern.pattern

So you compile that with verbose and case-insensitive flags and set it
to "pattern" in a string.Template subclass.  (In fact you don't have
to compile it, but that behaviour's undocumented.)  Something like

>>> regexp = """
... (?P)\\$ | # backslash escape pattern
... \$(?:
...   (?P[_a-z][_a-z0-9]*)| # delimiter and identifier
...   {(?P[_a-z][_a-z0-9]*)} | # ... and braced identifier
...   (?P)  # Other ill-formed delimiter exprs
... )
... """
>>> class BackslashEscape(Template):
... pattern = re.compile(regexp, re.I | re.X)
...


   Graham

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


Re: Text-To-Speech for the Mac (OS X)

2007-06-20 Thread Graham Dumpleton
On Jun 21, 9:41 am, Brian <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Does anyone know how to get Python to be able to perform text-to-speech
> abilities for the Mac (OS X)?  I have been searching Google, but have
> not found any helpful solutions or resources yet.

os.system('say read the man page for "say"')

:-)

Graham

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


Re: Using PSE under Win32

2007-06-24 Thread Graham Dumpleton
On Jun 24, 1:13 am, Eduardo Dobay <[EMAIL PROTECTED]> wrote:
> Hello, I've been playing around withmod_pythonthese days (using
> Publisher and PSP), and it has been working smoothly under Windows XP
> (using Apache 2.2). But when I installed PSE and went to use it 
> withmod_python, it didn't work. The error I get whenever I try to load a
> PSE page is:
>
> Traceback (most recent call last):
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 1537, in HandlerDispatch
> default=default_handler, arg=req, silent=hlist.silent)
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 1229, in _process_target
> result = _execute_target(config, req, object, arg)
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 1128, in _execute_target
> result = object(arg)
>
> TypeError: 'module' object is not callable
>
> I thought it could be some incompatibility issue between PSE andmod_python, 
> but I tried both installing the PSE binary and building
> the sources, and it didn't make a difference. Has anyone out there had
> success using PSE under Windows?
>
> (Just for the record, I did install matching versions, at least for
> Apache (2.2.3), Python (2.5) andmod_python(3.3.1). PSE doesn't seem
> to have a strict version requirement.)

What do you have PythonHandler set to and what does it identify? The
error above suggests that whatever you identify as the handler is not
a callable object like a function. Are you perhaps identifying some
sort of PSE template object as target when you shouldn't be? Post your
handler code so we can see it.

Graham

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


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-24 Thread Graham Breed
Steven D'Aprano wote:

> But if you really want declarations, you can have them.
>
> >>> import variables
> >>> variables.declare(x=1, y=2.5, z=[1, 2, 4])
> >>> variables.x = None
> >>> variables.w = 0
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "variables.py", line 15, in __setattr__
> raise self.DeclarationError("Variable '%s' not declared" % name)
> variables.DeclarationError: Variable 'w' not declared

Another way is to decorate functions with their local variables:

>>> from strict import my
>>> @my("item")
... def f(x=1, y=2.5, z=[1,2,4]):
... x = float(x)
... w = float(y)
... return [item+x-y for item in z]
...
Traceback (most recent call last):
  File "", line 2, in 
  File "strict.py", line 11, in dec
raise DeclarationError("No slot for %s"%varname)
strict.DeclarationError: No slot for w

and the implementation

import re

class DeclarationError(TypeError): pass

def my(slots=""):
tokens = slots.split()
def dec(func):
code = func.func_code
for varname in code.co_varnames[code.co_argcount:]:
if re.match('\w+$', varname) and varname not in tokens:
raise DeclarationError("No slot for %s"%varname)
return func
return dec


The best way to catch false rebindings is to stick a comment with the
word "rebound" after every statement where you think you're rebinding
a variable.  Then you can search your code for cases where there's a
"rebound" comment but no rebinding.  Assuming you're the kind of
person who knows that false rebindings can lead to perplexing bugs,
but doesn't check apparent rebindings in a paranoid way every time a
perplexing bug comes up, anyway.  (They aren't that common in modern
python code, after all.)  And that you remembered to add the comments
(like you would have remembered the let and set).  And you're also the
kind of person who's troubled by perplexing bugs but doesn't run a
fully fledged lint.  Maybe that's the kind of person who wouldn't put
up with anything short of a macro as in the original proposal.  All I
know is that it's the kind of person I don't want to second guess.


   Graham

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


Re: server wide variables

2007-06-25 Thread Graham Dumpleton
On Jun 26, 1:29 am, Jay Sonmez <[EMAIL PROTECTED]> wrote:
> I want to be able to save some server variables as long as Apache runs
> on the server (mod_python).
>
> How is that possible in Python?

It depends on whether you expect all Apache child processes for that
server to be able to access the data and for the data to be able to
survive the killing off and start up of individual Apache child
processes.

For background on some of the issues read:

  http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreterModel

Saving data in os.environ is not recommended even if they have to
survive just within the context of that specific interpreter within
that process, as os.environ access and update is not thread protected
and Apache child processes may be multithreaded.

So, read that referenced document and then perhaps explain you
expectations a bit better.

Graham

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


Re: server wide variables

2007-06-25 Thread Graham Dumpleton
On Jun 26, 10:29 am, Graham Dumpleton <[EMAIL PROTECTED]>
wrote:
> On Jun 26, 1:29 am, Jay Sonmez <[EMAIL PROTECTED]> wrote:
>
> > I want to be able to save some server variables as long as Apache runs
> > on the server (mod_python).
>
> > How is that possible in Python?
>
> It depends on whether you expect all Apache child processes for that
> server to be able to access the data and for the data to be able to
> survive the killing off and start up of individual Apache child
> processes.
>
> For background on some of the issues read:
>
>  http://www.dscpl.com.au/wiki/ModPython/Articles/TheProcessInterpreter...
>
> Saving data in os.environ is not recommended even if they have to
> survive just within the context of that specific interpreter within
> that process, as os.environ access and update is not thread protected
> and Apache child processes may be multithreaded.

Before someone decides to shoot me down, yes I know that dictionaries
themselves are thread safe and thus os.environ itself is thread safe.
In making that comment I am looking more at the higher level locking
aspects of a combined set of data, ie., the same as you might have for
global data in a module and what is required to protect that in the
presence of multiple threads. The os.environ dictionary being what it
is, people may not appreciate the need that locking may still be
required if one is going to use it as a means of holding data that can
change over time. In short, os.environ simply shouldn't be used for
that anyway.

Graham

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


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-26 Thread Graham Breed
Douglas Alan wote:
> Graham Breed <[EMAIL PROTECTED]> writes:
>
> > Another way is to decorate functions with their local variables:
>
> >>>> from strict import my
> >>>> @my("item")
> > ... def f(x=1, y=2.5, z=[1,2,4]):
> > ... x = float(x)
> > ... w = float(y)
> > ... return [item+x-y for item in z]
>
> Well, I suppose that's a bit better than the previous suggestion, but
> (1) it breaks the style rule of not declaring variables until you need
> them, and (2) it doesn't catch double initialization.

(1) is a style rule that many style guides explicitly violate.  What
is (2) and why would it be a problem?

A better way that I think is fine syntactically would be

from strict import norebind, set
@norebind
def f(x=1, y=2.5, z=[1.2.4]):
set(x=float(x))
set(w=float(y))
return [item+x-y for item in z]

It won't work because the Python semantics don't allow a function to
alter a nested namespace.  Or for a decorator to get at the locals of
the function it's decorating.  It's an example of Python restricting
flexibility, certainly.

> > The best way to catch false rebindings is to stick a comment with
> > the word "rebound" after every statement where you think you're
> > rebinding a variable.
>
> No, the best way to catch false rebindings is to have the computers
> catch such errors for you.  That's what you pay them for.

How does the computer know which rebindings are false unless you tell
it?

> > Then you can search your code for cases where there's a "rebound"
> > comment but no rebinding.
>
> And how do I easily do that?  And how do I know if I even need to in
> the face of sometimes subtle bugs?

In UNIX, you do it by putting this line in a batch file:

egrep -H 'rebound' $* | egrep -v '^[^:]+:[[:space:]]*([.[:alnum:]]+)
[[:space:]]*=(|.*[^."])\<\1\>'

You don't know you need to do it, of course.  Like you wouldn't know
you needed to use the let and set macros if that were possible.
Automated checks are only useful for problems you know you might have.

> > Assuming you're the kind of person who knows that false rebindings
> > can lead to perplexing bugs, but doesn't check apparent rebindings
> > in a paranoid way every time a perplexing bug comes up, anyway.
> > (They aren't that common in modern python code, after all.)
>
> They're not that uncommon, either.

The 300-odd line file I happened to have open had no examples of the
form x = f(x).  There was one rebinding of an argument, such as:

if something is None:
something = default_value

but that's not the case you were worried about.  If you've decided it
does worry you after all there may be a decorator/function pattern
that can check that no new variables have been declared up to a
certain point.

I also checked a 400-odd file which has one rebinding that the search
caught.  And also this line:

m, n = n, m%n

which isn't of the form I was searching for.  Neither would the set()
solution above be valid, or the substitution below.  I'm sure it can
be done with regular expressions, but they'd get complicated.  The
best way would be to use a parser, but unfortunately I don't
understand the current Python grammar for assignments.  I'd certainly
be interested to see how your proposed macros would handle this kind
of thing.

This is important because the Python syntax is complicated enough that
you have to be careful playing around with it.  Getting macros to work
the way you want with results acceptable to the general community
looks like a huge viper pit to me.  That may be why you're being so
vague about the implementation, and why no macro advocates have
managed to get a PEP together.  A preprocessor that can read in
modified Python syntax and output some form of real Python might do
what you want.  It's something you could work on as a third-party
extension and it should be able to do anything macros can.


That aside, the short code sample I give below does have a rebinding
of exactly the form you were worried about.  It's still idiomatic for
text substitutions and so code with a lot of text substitutions will
likely have a lot of rebindings.  You could give each substituted text
a different name.  I think that makes some sense because if you're
changing the text you should give it a name to reflect the changes.
But it's still error prone: you might use the wrong (valid) name
subsequently.  Better is to check for unused variables.

> I've certainly had it happen to me on several occasions, and sometimes
> they've been hard to find as I might not even see the mispeling even
> if I read the code 20 times.

With vim, all you have to do

Changing default output object for

2007-06-27 Thread Graham Dumpleton
I am sure someone has probably thought of this and there are good
reasons why it is a bad idea or not possible, but, is there any
technical reason why one couldn't somehow replace PRINT_ITEM byte
code, with combination of byte codes which inserted in a reference to
a file like object and then invoked PRINT_ITEM_TO byte code instead.

In other words, the aim is allow users code to use 'print' but change
where the output goes when no target file is provided.

I do know that one can replace sys.stdout with another file object in
the simple case, or in the more complicated case with a file like
object that determines where to redirect output based on the calling
thread identity or some over criteria, but wanted to see if some one
could do something without modifying sys.stdout.

One particular example where I thought this might be interesting is to
allow in a web templating system where Python code blocks can be
embedded in markup, the use of the 'print' statement without a user
having to know that they really need to direct output to a special
file object, eg. 'print >> response'. Procedure might thus be to load
Python code snippets and compile to byte code. Fixup byte code to
change PRINT_ITEM to PRINT_ITEM_TO with other byte codes added to
insert appropriate reference to file like object being supplied
through data only passed to code object when executed. Cache code for
ongoing use. Invoke code object as required, supplying reference to
file object for output.

In some template systems such as in web.py they replace sys.stdout
with a magic object which based on the identity of the thread making
calls to the methods of sys.stdout, will redirect the output back to a
registered response object, but instead of doing it this way, could
byte code manipulation provide another way.

Anyway, just curious as to whether it would be possible or not and if
someone has an example of doing it using byteplay or similar would
love to see what is required.

Graham

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


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-27 Thread Graham Breed
Dennis Lee Bieber wote:

>   But if these "macros" are supposed to allow one to sort of extend
> Python syntax, are you really going to code things like
>
>   macrolib1.keyword
>
> everywhere?

I don't see why that *shouldn't* work.  Or "from macrolib1 import
keyword as foo".  And to be truly Pythonic the keywords would have to
be scoped like normal Python variables.  One problem is that such a
system wouldn't be able to redefine existing keywords.

Lets wait for a concrete proposal before delving into this rats'
cauldron any further.


   Graham

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


Re: mod_python & doc file system layout

2007-07-04 Thread Graham Dumpleton
On Jul 5, 12:22 am, Jan Danielsson <[EMAIL PROTECTED]> wrote:
> Hello all,
>
>This is probably more of an apache question, but I'm guessing there
> will be other mod_python-beginners who are wondering the same thing.
>
>Let's say I have a web app called MyApp. It uses the usual images and
> style sheets. I keep it all in
> ~/projects/MyApp/{styles,images,index.py,.htaccess}. I configure up
> apache to use virtual hosts, and point it to MyApp.
>
>Now when I access myapp.local (which points to 127.0.0.1, obviously),
> my index.py is parsed properly. However, any access made to /images or
> /styles end up being handled by mod_python, which isn't good, because it
> won't load images or style sheets.
>
>I have solved this by moving index.py (and the associated .htaccess)
> to a subdirectory "main".
>
>I guess one solution would be to make an access to / on the virtual
> server redirect to main.
>
>Another solution, I guess, would be to make the python script load
> the images and style sheets.. A solution which I find to be particularly
> ugly. :-)
>
>Any mod_python users out there with any other solutions?

In the subdirectories containing your static files add a .htaccess
file containing:

  SetHandler None

Graham

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


Re: Client-side cookies on Python in Mac OSX

2007-07-12 Thread Graham Dumpleton
On Jul 13, 12:14 pm, Adrian Petrescu <[EMAIL PROTECTED]> wrote:
> Hi, all. I'm writing an app for OS X; therefore I'd prefer to use only
> the default python install that comes with Tiger. For the moment,
> however, this means:
>
> NaviOSX:~ adrianpetrescu$ python -V
> Python 2.3.5
>
> Therefore, I was not surprised to find out that cookielib did not
> exist here, since I knew that it was a 2.4+ feature.
>
> However, I *was* shocked to find out that ClientCookie, which I'd
> thought was a Python 2.0+ feature, also cannot be found:
>
> >>> import ClientCookie
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> ImportError: No module named ClientCookie
>
> Why would Apple go out of their way to remove this functionality from
> their shipped version of Python? More importantly, considering I need
> to programatically access a website that uses cookies for
> authentication, how can I do this in OSX's Python install? Do they
> provide some other library they'd prefer you to use?
>
> I'm sure SOMEONE in the world has used cookies on Macs so I'm hoping
> there is a solution for this...

They didn't remove it, it was never there in the first place.

The ClientCookie site says 'Python 2.0 or above is required'. It
doesn't say it is included with Python distributions. The package
still has to be installed separately. See:

  http://wwwsearch.sourceforge.net/ClientCookie/

Graham

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


Re: Installing mod_python on mac os 10.4.7

2007-07-14 Thread Graham Dumpleton
On Jul 15, 2:47 am, 7stud <[EMAIL PROTECTED]> wrote:
> Themod_pythonmanual says this under section 2.1 Prerequisites:
>
> --
> In order to compilemod_pythonyou will need to have the include files
> for both Apache and Python, as well as the Python library installed on
> your system. If you installed Python and Apache from source, then you
> already have everything needed. However, if you are using prepackaged
> software (e.g. Red Hat Linux RPM, Debian, or Solaris packages from
> sunsite, etc) then chances are, you have just the binaries and not the
> sources on your system. Often, the Apache and Python include files and
> libraries necessary to compilemod_pythonare part of separate
> ``development'' package. If you are not sure whether you have all the
> necessary files, either compile and install Python and Apache from
> source, or refer to the documentation for your system on how to get
> the development packages.
> -
>
> I installed Apache from source using these instructions:
>
> http://switch.richard5.net/isp-in-a-box-v2/installing-apache-on-mac-o...
>
> but I used a package to install python 2.4.  The package was from
> here:
>
> http://www.pythonmac.org/packages/
>
> and it's a "Universal binary version of Python that runs natively on
> PPC and Intel systems."
>
> But my mac came with Python 2.3.5 pre-installed, so I wonder if I
> already have the necessary "include files for both Apache and Python,
> as well as the Python library" already installed.

Have you actually tried to install mod_python? That would be the
quickest way of finding out.

Because you are using an alternate Apache than the OS supplied one,
you will need to use the --with-apxs option to configure when building
Python. Unless you really need Python 2.4, it is easier to use the OS
supplied version of Python. If you must use an alternate version, use
the --with-python option to configure for mod_python to tell it which
version. Depending on where that Python version is installed, you may
also have to fiddle the Apache 'envvars' file as well to get it to
work.

Graham

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


Re: Installing mod_python on mac os 10.4.7

2007-07-14 Thread Graham Dumpleton
On Jul 15, 10:06 am, Graham Dumpleton <[EMAIL PROTECTED]>
wrote:
> On Jul 15, 2:47 am, 7stud <[EMAIL PROTECTED]> wrote:
>
>
>
> > Themod_pythonmanual says this under section 2.1 Prerequisites:
>
> > --
> > In order to compilemod_pythonyou will need to have the include files
> > for both Apache and Python, as well as the Python library installed on
> > your system. If you installed Python and Apache from source, then you
> > already have everything needed. However, if you are using prepackaged
> > software (e.g. Red Hat Linux RPM, Debian, or Solaris packages from
> > sunsite, etc) then chances are, you have just the binaries and not the
> > sources on your system. Often, the Apache and Python include files and
> > libraries necessary to compilemod_pythonare part of separate
> > ``development'' package. If you are not sure whether you have all the
> > necessary files, either compile and install Python and Apache from
> > source, or refer to the documentation for your system on how to get
> > the development packages.
> > -
>
> > I installed Apache from source using these instructions:
>
> >http://switch.richard5.net/isp-in-a-box-v2/installing-apache-on-mac-o...
>
> > but I used a package to install python 2.4.  The package was from
> > here:
>
> >http://www.pythonmac.org/packages/
>
> > and it's a "Universal binary version of Python that runs natively on
> > PPC and Intel systems."
>
> > But my mac came with Python 2.3.5 pre-installed, so I wonder if I
> > already have the necessary "include files for both Apache and Python,
> > as well as the Python library" already installed.
>
> Have you actually tried to install mod_python? That would be the
> quickest way of finding out.
>
> Because you are using an alternate Apache than the OS supplied one,
> you will need to use the --with-apxs option to configure when building
> Python.

Whoops, --with-apxs option is to configure for mod_python, not Python.

> Unless you really need Python 2.4, it is easier to use the OS
> supplied version of Python. If you must use an alternate version, use
> the --with-python option to configure for mod_python to tell it which
> version. Depending on where that Python version is installed, you may
> also have to fiddle the Apache 'envvars' file as well to get it to
> work.
>
> Graham


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


  1   2   3   >