Python without wrapper script

2009-12-02 Thread eric.frederich
Is there a way to set up environment variables in python itself
without having a wrapper script.

The wrapper script is now something like

#!/bin/bash

export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"

export PATH="/some/thing/bin:$PATH"
export PATH="/another/thing/bin:$PATH"

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


Blocking code

2010-01-15 Thread eric.frederich
I am trying to write something that will watch directories without
poling them.
This is what FAM is fore.  Gamin is a re-implementation of FAM and it
has python bindings.

The problem is that when I call handle_one_event() it blocks until
there is an event to handle.
Pressing Ctrl-C does nothing here.
This seems to be the behavior of FAM so it was re-implemented in
Gamin.
I looked at another set of bindings for FAM in Java.
It came with an example that watched a directory for changes.  They
basically put the code that blocks in another thread and accepted
input on a second thread waiting for the user to press q and it would
kill the blocking thread.

I don't know how to do something like this in Python, I have never
used threads and I'm not sure if thats the way to go.

Someone else that complained about the blocking behavior of those
calls said that he found a solution since he was using OCaml.
Apparently OCaml has a way to say that you're entering a block of code
that blocks and you can still exit using Ctrl-C.
I don't think anything like this exists in Python does it?

So, my question here is How from Python can I call code that
blocks without losing the ability to use Ctrl-C?

Here is the code that is impenetrable to Ctrl-C.  You need to kill it
with another terminal

#!/usr/bin/env python

import gamin
import sys

directory = sys.argv[1]

def callback(path, event):
print "Got callback: %s, %s" % (path, event)

mon = gamin.WatchMonitor()
mon.watch_directory(directory, callback)
mon.event_pending()

while True:
mon.handle_one_event()
-- 
http://mail.python.org/mailman/listinfo/python-list


Split class across multiple files

2009-11-20 Thread eric.frederich
I have a class which holds a connection to a server and a bunch of
services.
In this class I have methods that need to work with that connection
and services.

Right now there are about 50 methods some of which can be quite long.
>From an organizational standpoint, I'd like to have method
implementations in their own files.

Is this possible?  It is recommended?  Should I just stop worrying
about it and have a 5,000 line class?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split class across multiple files

2009-11-20 Thread eric.frederich
Yeah... it does seem like a God Object.
My reasoning for putting them all into one big class is that the class
has references to all the required resources, connections, and
services.
The other option would be to have a simple object called Session which
did nothing but login and hold those references.
The problem then is that I'd have 50 methods all of which would need
to take that session object as an argument.
That is what I was trying to avoid, having a ton of methods all taking
the same first parameter.
In most cases there would only be one session.

Perhaps I could do a singleton approach and have each of these methods
implicitly use the single session object unless called with a session=
keyword argument.

How does this look?.


# Begin Session.py

class Session():

def __init__(self, host=None, username=None, password=None):
if host and username and password:
self.login(host, username, password)

def login(self, host, username, password):
self.host = host
self.username = username
self.password = password

self.connection = Something.login(host, username,
password)
self.someService= SomeService.getService(connection)
self.anotherService = AnotherService.getService(connection)

def logout(self):
return self.connection.logout()


defaultSession = Session()


# Begin MyLibrary.py

from session import defaultSession

def doSomethig(x, y, z, session=defaultSession):

return session.someService.doSomething(x, y, z)

def anotherFunction(x, y, session=defaultSession):
ret = 0
for item in session.anotherService.getStuff(x):
if session.someService.foo(item, y):
session.anotherService.bar(item, x)
ret += 1
return ret





On Nov 20, 11:31 am, "Diez B. Roggisch"  wrote:
> eric.frederich schrieb:
>
> > I have a class which holds a connection to a server and a bunch of
> > services.
> > In this class I have methods that need to work with that connection
> > and services.
>
> > Right now there are about 50 methods some of which can be quite long.
> > From an organizational standpoint, I'd like to have method
> > implementations in their own files.
>
> > Is this possible?  It is recommended?  Should I just stop worrying
> > about it and have a 5,000 line class?
>
> It is pretty easy to do, as python allows multiple inheritance. Just
> group together methods into a class, and create one that inherits from all.
>
> However, I'd say something that even splitted up is essentially one
> class with 5000 lines cries for a huge refactoring. Of course this
> depends on the use-case, but I for once don't have a single such beast.
> Looks like a god-object to me...
>
> http://en.wikipedia.org/wiki/God_object
>
> Diez

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