[Tutor] multiprocessing: Correct usage of pool & queue?

2009-09-03 Thread Allen Fowler
Hello,

I have a list of tasks/items that I want handed off to threads/processes to 
complete.  (I would like to stick with process if I could, since there is some 
CPU work here. )

Each task involves some calculations and a call to a remote server over 
urllib2/HTTP.  

The time to complete each task varies from 1 to 20 seconds depending on a 
number of factors including variable delay on the remote server.

I would like to:

1) Have a maximum of 20 "in-flight" tasks.  (thus worker processes?)

2) Not overload the external server that each task is calling.  No more than "3 
new tasks" per second. More "waiting" tasks may be OK, i need to test it. 

3) Certain tasks in my list must be processed in the correct order.  (I guess 
the asignment logic must somehow tag those to by done by the same worker?)


Do any of you have suggestions? Can someone point me in the direction of sample 
code for this?

Thank you,
:)


  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string.title(): correct?

2009-08-11 Thread Allen Fowler

Hello,

"He's a great guy".title()

Gives me:
 
"He'S A Great Guy"


I expected:

"He's A Great Guy"

Did i miss something here?

Thank you,
:)


  

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


[Tutor] Fixing "nearly ASCII" data file?

2009-08-10 Thread Allen Fowler

Hello,

I have some CSV data from Office / OpenOffice in "nearly ASCII" format.  This 
is just basic text but it it stored as UTF-8, and has curly quotes, etc.

Is there a way to easily read the file as ASCII by forcing these to the 
standard ASCII equivalents?

Thank you


  

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


Re: [Tutor] Configuaration files and paths?

2009-08-07 Thread Allen Fowler




> > 
> > FWIW:
> > 
> > When using relative paths I got extra ../../ terms, so I changed  
> join_relative() to:
> > 
> > def join_relative(base, path):
> > return os.path.normpath(os.path.join(script_dir(base), path))
> > 
> > 
> > Seems to work...
> 
> 
> Yeah, good catch ... looks great, and thanks for sharing your mod.
> 


Glad I could return the favor.  :)

Since then, I took it a bit further for use in my code...  I am not finished 
with it yet, but am curios what you think.  (I am not sure named things right 
or structured it logically)


###

import os
class HomePath(object):
"""For finding paths based on a home/install directory.

"""
 
def __init__(self, home_dir = None, home_file = None):
"""Must be called with either a path to a directory or, as a 
shortcut, a file in that directory.

"""

if home_file != None:
# Set home based on a path to a file in its directory
self.home = os.path.normpath(self.fix_path(home_file))

elif home_dir != None:
# Set home based on its path
self.home = os.path.normpath(self.get_dir(home_dir))

else:
raise Exception("Must call with either a path to a directory 
or, as a shortcut, a file in that directory.")

def abs(self, rel_from_home):
"""Return an absolute path when passed a path relative to home.

"""

return self.join_relative(self.home, rel_from_home)

def fix_path(self, base):
return os.path.realpath(os.path.abspath(base))

def get_dir(self, base):
return os.path.dirname(self.fix_path(base))

def join_relative(self, base, path):
return os.path.normpath(self.fix_path(os.path.join(self.home, 
path)))
#



  

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


Re: [Tutor] Configuaration files and paths?

2009-08-06 Thread Allen Fowler




> 
> Something like this ...
> 
> # lib/mypaths.py
> # --
> 
> import os
> 
> def script_path(base):
> return os.path.realpath(os.path.abspath(base))
> 
> def script_dir(base):
> return os.path.dirname(script_path(base))
> 
> def join_relative(base, path):
> return os.path.join(script_dir(base), path)
> 
> # app.py
> # --
> 
> ...
> 
> from lib.mypaths import join_relative
> print join_relative(__file__, 'config/prefs.ini')
> 
> # this may work when calling from
> # other modules, I suppose (untested)
> 
> import sys
> print join_relative(sys.argv[0], 'config/prefs.ini')
> 
> ...


FWIW:

When using relative paths I got extra ../../ terms, so I changed  
join_relative() to:

def join_relative(base, path):
return os.path.normpath(os.path.join(script_dir(base), path))


Seems to work...


  

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


Re: [Tutor] Configuaration files and paths?

2009-08-06 Thread Allen Fowler



> Martin Walsh 
>

> Allen Fowler wrote:
> > 
> 
> > 
> > As a follow-up question, how do give my modules stored under ./lib access 
> > to 
> the data in my ConfigParser object?  (For instance, database connection 
> string, 
> storage path, etc.)
> > 
> > I guess a global ConfigParser object would work, but that seems wrong.
> > 
> 
> And yet, to me it seems wrong to have more than one instance of config
> data floating around. Instead of using a global you can pass the config
> object, or just the appropriate attributes, around to your lib
> functions/classes, as necessary -- and keep the flow in your main
> script. For example (highly speculative, FWIW),
> 
> # app.py
> from database.connection import getcursor
> from lib.persist import Storage
> 
> def main():
> confp = ConfigParser()
> confp.read(join_relative(__file__, 'config/prefs.ini'))
> config = confp.defaults()
> 
> curs = getcursor(config[db_connection_string])
> 
> ...
> 
> storage = Storage(config[storage_path])
> 
>


I hear your point.  It makes sense.


The above sample code seems like the way to go... if I can structure my modules 
like that.

For an object that needs many settings, what about passing in an instance of 
ConfigParser?  (Or should I extract the settings to a dict, first?)

Thank you again,
:)


  

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


Re: [Tutor] Configuaration files and paths?

2009-08-06 Thread Allen Fowler




> > Assuming the application could be invoked in odd ways that may alter the 
> notion of the current working directory, how do I unambiguously find the 
> absolute path to the current python source file?  (So I can load the nearby 
> .ini)
> 
> I use a helper function that calculates the absolute path of my app
> script, then I am able to os.path.join relative elements to my heart's
> content. I haven't had a problem with this approach so far, which of
> course doesn't mean it's the right way, or even a correct implementation
> for that matter.
> 
> Something like this ...
> 
> # lib/mypaths.py
> # --
> 
> import os
> 
> def script_path(base):
> return os.path.realpath(os.path.abspath(base))
> 
> def script_dir(base):
> return os.path.dirname(script_path(base))
> 
> def join_relative(base, path):
> return os.path.join(script_dir(base), path)
> 
> # app.py
> # --
> 
> ...
> 
> from lib.mypaths import join_relative
> print join_relative(__file__, 'config/prefs.ini')
> 
> # this may work when calling from
> # other modules, I suppose (untested)
> 
> import sys
> print join_relative(sys.argv[0], 'config/prefs.ini')
> 


That is really great.  Thank you.  

I wonder why something like this is not in the standard library.  Seems like an 
often needed function.


  

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


Re: [Tutor] Configuaration files and paths?

2009-08-05 Thread Allen Fowler




> > What is the recommended way to configure my application find the various 
> database and/or configuration files it needs?
> 
> Recommemded by whom? A lot depends on the OS. Apple for example have one set 
> of 
> recommendations for MacOS, Windows has another and Linux has several to 
> choose 
> from!
> 


Thank you good point.

Planning on using a ConfigParser based .ini file located in the ./config folder.

> > For instance my folder layout:
> > 
> > /path_to_app/app.py
> > /path_to_app/lib/
> > /path_to_app/database/
> > /path_to_app/config/
> > /path_to_app/photos
> > 
> >  and so on.  I would like to make an .ini in the config folder 
> 
> Seems fair enough, however on a Unix system you should also consider allowing 
> the user to have their own personalised version in their home directory. Thus 
> at 
> startup you get the current user ID / home directory and look for a suitable 
> config file. If it exists read it, if not read the default one in your config 
> directory.
>
> > 1) How does my main app file find the config file in the first place?
> 
> Generally use a relative path so normally your app will run from its home 
> folder 
> so you can look in ./config. You might also set a system environment variable 
> - 
> for example the CLASSPATH or PYTHONPATH variables, or the ORACLE_HOME used by 
> Oracle for their database. If the environment var is not set then look in 
> ./config
>

Assuming the application could be invoked in odd ways that may alter the notion 
of the current working directory, how do I unambiguously find the absolute path 
to the current python source file?  (So I can load the nearby .ini)


As a follow-up question, how do give my modules stored under ./lib access to 
the data in my ConfigParser object?  (For instance, database connection string, 
storage path, etc.)

I guess a global ConfigParser object would work, but that seems wrong.


  

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


[Tutor] Configuaration files and paths?

2009-08-05 Thread Allen Fowler

Hello,

What is the recommended way to configure my application find the various 
database and/or configuration files it needs?


For instance my folder layout:

/path_to_app/app.py
/path_to_app/lib/
/path_to_app/database/
/path_to_app/config/
/path_to_app/photos

 and so on.  (app.py being the main file, and the lib folder containing my 
custom modules.)


I would like to make an .ini in the config folder that contains entries that 
point to the other folders.  Seems like a resonable idea.

So my questions are:

1) How does my main app file find the config file in the first place?

2) How should I point to those other folders in the ini file?


Thank you


  

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


Re: [Tutor] Suggested source code folder layout

2009-06-08 Thread Allen Fowler

> 

> My 2 cents...
> 
> 1) Since you are describing your source code layout, any virtual
> environment should be outside. A virtual environment (virtualenv) is
> part of deployment and not part of source. If you need to make a
> reproducible deployment environment, then you need a deployment system
> such as zc.buildout. You would store just the recipe for the
> environment in source, but not the resulting buildout. Your build
> recipe will typically go in the top of your source tree.
> 
> http://pypi.python.org/pypi/zc.buildout/
> 


Good point about keeping the virtualenv out of the source tree in favor of 
"recipe", and copying the modules to lib or site-packages upon deployment.

zc.buildout seems like a powerful tool, but I'm not at all clear on how to use 
it for this sort of task. (All the tutorials I can find to seem to zope 
related.)


Do you have any examples you can share?



  

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


[Tutor] Suggested source code folder layout

2009-06-02 Thread Allen Fowler

Hello,

I'm looking for some suggestions as to the filesystem source code layout for a 
new project.

Here is what I am thinking so far:

root_folder/
- app/ -- Code for our pylons/django/TG/etc web app
- web/ -- Public static web files (and wsgi / fastCGI connector files)
- db/ -- SQlite DB
- scripts/ -- Various custom programs that will also interact with the DB / 
app. (Some cron, some interactive.)

However, I am still wondering about a couple of items:

1) Where to create the virtualpython installation that will be used by both the 
app and the scripts. 

2) Where to put our in-house created python modules that will be imported by 
both the app and scripts.

Thank you,
:)



  

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


[Tutor] Spell checking source code?

2009-06-01 Thread Allen Fowler

Hello,

Are there any utilities to help "spell check" source code?  (Docstrings, etc) 

Thank you,
:)



  

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


[Tutor] Sample python file/module templates?

2009-05-31 Thread Allen Fowler

Hello,

In terms of in-code documentation of function / section headers, change logs, 
etc.  Are there well-done sample files I can use as inspiration?

Thank you,
:)



  

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


Re: [Tutor] Mapping to an equivalent / similar object?

2009-05-30 Thread Allen Fowler




> The difference between objects is measeured by their interfaces not their 
> data 
> so provided the supplier objects all use the same operations of a Flavor then 
> its not too bad. You can either create subclasses of Flavor for each 
> manufacturer that retuirns the requisite list of attributes  (but requires 
> ongoing changes for every new manufacturer - eek!) or you can write a method 
> of 
> Flavor that takes a list (or dictionary?) of required attributes and returns 
> the 
> values. It can also raise exceptions if asked for unsupported attributes, or 
> return None, in which case the Supplier object provides a suitable default.
> 

Hmm.  I'll have to read that a couple of times.



> This way you can limit the changes for a new supplier. You might also be able 
> to 
> data drive the Supplier object to make the required list of attributes 
> configurable(via a database or file?). Then you only need subclasses of 
> Supplier 
> for the actual interface types (EDI, v FTP, v http v email etc).
> 
> > How should the mapping between the CanonicalFlavor('Vanilla') object and 
> ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') objects be handled.
> 
> If the mapping is one to many I don;t think you need to maintain it - at 
> least 
> not in the objects. Just pass the Flavor object to the supplier object. Let 
> the 
> supplier query the Flavor for the data it needs.
> 
> If the choice of supplier was driven by Flavor characteristics then the 
> selection method would be in the Flavour and then you'd need to keep a list 
> of 
> suppliers in the Flavor. But since
> the supplier is chosen from external factors the Flavor doesn't need to know 
> about its supplier. And the supplier only needs to know about how to query a 
> flavour.
> 


What about just Flavor() objects and a customised "per-manufacture" SupplierA() 
/ SupplierB() objects that internally knows how to generate an order from a 
passed in Flavor() object?  (Which would also raise a CantMakeFlavor error if 
this manufature can't supply the required flavor.)


  

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


[Tutor] Mapping to an equivalent / similar object?

2009-05-28 Thread Allen Fowler

Hello,

Imagine the relationship between an Ice Cream retail store and the several 
manufactures that supply it's flavors.

The retail store has a single list of flavors and any given flavor can be made 
by one or more manufactures.  (Most are made by several.)

The store's stock monitoring system will generate a list of Flavor() objects 
that need to be ordered.  Say: Flavor('Vanilla'), or better yet, 
CanonicalFlavor('Vanilla')

The store's ordering system takes as input a CanonicalFlavor('Vanilla') object 
that needs to be purchased, and must route this to a manufacturer's ordering 
system.  The choice of which manufacture to use is based on external factors.

However, each manufacturer's ordering system is different, and each expects a 
radically different variation of a Flavor object.  (Different ingredients, 
parameters, etc. )

How should the mapping between the CanonicalFlavor('Vanilla') object and 
ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') objects be handled.


(Or is this the wrong way to do this?)


Thank you,
:)



  

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


Re: [Tutor] Shared FIFO?

2009-05-18 Thread Allen Fowler



> "Allen Fowler" wrote 
> > I have several CGI scripts that I would like coordinate via a "First In / 
> First Out" style buffer.That is, some processes are adding work units, 
> and 
> some take the oldest and start work on them.
> > 
> > What is the right way to do this?I suppose I could use an SQL server or 
> SQlite , but that seems very complex for just a FIFO.
> 
> Given how easy it is to use something like SqlLite from Python I don't think 
> its 
> an unreasonable approach and it avoids or eases some of the hidden 
> complexities 
> around using simple files (such as locking issues).
>

Yeah.  Now that I think about there are ton of small issues that using sqlite 
would take care of.

For clarification, this is not just for CGI scripts... there will be cron 
initiated processes and perhaps a TurboGears app accessing the same buffer.

Since this sounds like it's been done before, can anyone direct me to tutorial 
or how-to on the subject?

thank you,
:)



  

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


[Tutor] Shared FIFO?

2009-05-17 Thread Allen Fowler

Hello,

I have several CGI scripts that I would like coordinate via a "First In / First 
Out" style buffer.That is, some processes are adding work units, and some 
take the oldest and start work on them.

What is the right way to do this?I suppose I could use an SQL server or 
SQlite , but that seems very complex for just a FIFO.

Thank you,
:)



  

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


Re: [Tutor] Calling super classs __init__?

2008-03-19 Thread Allen Fowler

> Nowadays the best practice for invoking a method from all superclasses
> (yes, multiple inheritance) is this:
> 
> class SubClass(BaseClass):
> def __init__(self, t, *args, **kw):
> super(SubClass, self).__init__(*args, **kw)
> # do something with t
> 
> That way you let Python decide which superclasses your SubClass has,
> instead of hard-coding it in several places.
>

Excellent.  Thank you.  This seems far more logical.

Is there a proper way to handle the case when SubClass() is called using 
positional arguments, and you do not desire "t" to be at the beginning?

Thanks again,
:)





  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

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


Re: [Tutor] Calling super classs __init__?

2008-03-18 Thread Allen Fowler

> >  What's the most "pythonic" way to make this work?
> 
> class BaseClass(object):
>   def __init__(self, x, y, z, foo='foo'): # whatever
>  # etc
> 
> class SubClass(BaseClass):
>   def __init__(self, t, *args, **kw):
> BaseClass.__init__(self, *args, **kw)
> # do something with t
> 
> This does mean that the special sub class argument has to come before
> the base class arguments when you create instances.
> 
> Whether you call BaseCla

Thank you...  Excellent idea.  

I haven't tried it yet, but I suppose **kwarg.pop()'ing  and *args manipulation 
could help ease subclass instantiation call signature limitations. (?)

Thanks,
:)



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

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


Re: [Tutor] Calling super classs __init__?

2008-03-18 Thread Allen Fowler

> > class MySubClass(MySuperClass):
> > 
> > def __init__(self, just_a_sub_option):  # what about other args? **args?
> 
> I think I would go ahead and list the superclass parameters and put the 
> new one at the end:
>  def __init__(self, opt_1, opt_2, opt_3, opt_n, just_a_sub_option):
> 
> > MySuperClass.__init__()# Should this be first?  
> What args to use? **args?
> 
> MySuperClass.__init__(self, opt_1, opt_2, opt_3, opt_n)
> 
> John's method will also work but I prefer to add the new parameter at 
> the end of the argument list.


(Hmm.. I should have pointed out that I generally use keyword args.  )

Right. So, I can certainly call the MySuperClass.__init__() with a long list of 
kwargs, but that gets annoying quickly when the superclass is also under active 
development and it's call signature frequently changes. 






  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

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


[Tutor] kwargs to object proporties?

2008-03-18 Thread Allen Fowler
Hello,

What's the best way convert keyword arguments to object properties?

Basically, I have a defined list of valid object properties that I'd like to 
optionally specify in the call to the constructor.

I've got ideas about using __getattr__ but I'm not sure if that's the right 
way.  Plus, that kind of makes it hard to use tab completion of objects in 
iPython.

Thanks,
:)







  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

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


[Tutor] Calling super classs __init__?

2008-03-18 Thread Allen Fowler
Hello,

Now, perhaps this not the best way write code, but I have a few questions 
regrading calling the super classes constructor:

I have a super class that accepts many arguments to it's constructor, and a 
subclass that should define one additional argument.

What's the most "pythonic" way to make this work?

class MySuperClass(object):

def __init__(self, opt_1, opt_2, opt_3, opt_n):
   # stuff done here
pass


class MySubClass(MySuperClass):

def __init__(self, just_a_sub_option):  # what about other args? **args?
# do stuff with "just_a_sub_option"
MySuperClass.__init__()# Should this be first?  
What args to use? **args?
   pass


Basically, I'd like to avoid maintaining a verbose list of arguments in the 
subclass.

Thanks,
:)




  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

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


Re: [Tutor] Simple reg-ex syntax?

2008-03-13 Thread Allen Fowler
findall is great.  Thank you. :)

- Original Message 
> From: Chris Fuller <[EMAIL PROTECTED]>
> To: tutor@python.org
> Sent: Thursday, March 13, 2008 5:10:43 AM
> Subject: Re: [Tutor] Simple reg-ex syntax?
> 
> 
> How I would improve this:
> 
> compile the regular expression.  This is more efficient.
> self.digit_extractor = re.compile('(\d+)')
> 
> then, use the findall method:
> self.allNumbers = self.digit_extractor.findall(self.aString)
> which will even work with multiline strings, but doesn't convert to integers.
> 
> To convert, use a list comprehension:
> self.allNumbers = [int(i) for i in self.digit_extractor.findall(self.aString)]
> 
> Cheers
> 
> On Wednesday 12 March 2008 21:59, Allen Fowler wrote:
> > Hello,
> >
> > I have code that looks something like:
> >
> > self.aString = "abc123xyz"
> > self.theNumber = int(re.search('(\d+)',self.aString).group())
> >
> > Is there a more Pythonic way of doing this? (Both the reg-ex and the Int
> > coercion.)  How about when I will need to extract more than one substring?
> >
> > Thank you,
> >
> > :)
> >
> >  
> > ___
> >_ Never miss a thing.  Make Yahoo your home page.
> > http://www.yahoo.com/r/hs
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

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


Re: [Tutor] Correct way to call an outside program?

2008-03-13 Thread Allen Fowler
Thank you for the help.  :)

- Original Message 

simplest way to run external commands ! 

import os 
cmd="/usr/bin/ssh 10.0.0.20 uptime"
os.popen(cmd)

my cmd is just an example, use any cmd you want & its output will be displayed 
to you.
hope this helps

[SNIP]



subprocess.Popen().communicate() will do it:

In [1]: import subprocess
In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
In [10]: print x[0]


If you just want stdout and stderr of the subprocess to go to stdout and
stderr of the calling process you can omit those arguments to Popen().






  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Correct way to call an outside program?

2008-03-12 Thread Allen Fowler
Hello,

I need to call an external command line .exe utility from my Python script.

What is the best way to capture the output (if any) and (optionally) direct it 
to my normal standard output?

There seem to be many options...

Thank you,
:)







  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


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


[Tutor] Simple reg-ex syntax?

2008-03-12 Thread Allen Fowler
Hello,

I have code that looks something like:

self.aString = "abc123xyz"
self.theNumber = int(re.search('(\d+)',self.aString).group())

Is there a more Pythonic way of doing this? (Both the reg-ex and the Int 
coercion.)  How about when I will need to extract more than one substring?

Thank you,
:)
 



  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

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


Re: [Tutor] Video file metadata? (MP4/WMV)

2008-03-04 Thread Allen Fowler


I can't seem to find a simple description of the MP4 sepc... it might be 
because there is not one. :)

That being said, what is the correct way to parse binary files in Python?


- Original Message 
> From: Alan Gauld <[EMAIL PROTECTED]>
> To: tutor@python.org
> Sent: Tuesday, March 4, 2008 7:32:01 PM
> Subject: Re: [Tutor] Video file metadata? (MP4/WMV)
> 
> 
> "Allen Fowler"  wrote in 
> 
> > 1) Does these exist a python module I can use to 
> > programatically edit the metadata in MP4 
> > files? 
> 
> I don;t know of one but if you can find the spec it might 
> be easy enough to just edit the files using normal (binary) 
> file handling methods. I've certainly done that for MIDI files.
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> Temorarily at:
> http://uk.geocities.com/[EMAIL PROTECTED]/
> Normally:
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


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


[Tutor] Video file metadata? (MP4/WMV)

2008-03-04 Thread Allen Fowler
Hello,

 I have several hundred WMV video files with bad embedded author/title/date 
information. 
 
However, the correct information is correctly encoded in the file name.. i.e. 
"title-author-date.wmv" 
 
Seems like the a great job for Python. :)

Also, I am about to convert these fiiles to MP4 for use on an iPod. The video 
software I am using will, I think, transfer the metadata from the WMV to the 
new MP4 files. 
 
So, two questions: 
 
1) Does these exist a python module I can use to programatically edit the 
metadata in MP4 
 files? 
 
2) Failing that, is there a python module I can use to edit the 
 metadata in the WMV files? (hopeing the data makes it through the 
 conversion..)
 
-- Thank you 





  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


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


Re: [Tutor] run in "deamon" mode?

2008-01-10 Thread Allen Fowler
Thank you for all the great tips... I'll try a few and see what works.

I must say that I'm a bit surprised that the Python Std library does not have a 
module for this.  Are all python scripts expected to be small user-mode 
utilities?
 




  

Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs

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


[Tutor] run in "deamon" mode?

2008-01-09 Thread Allen Fowler
Hello,

How can a make a python script run in "deamon mode"? (on a linux box)

That is, I want to run the program via "python myfile.py" and have it drop me 
back to the command line.  The program should continue running until I kill it 
via it's PID, the machine shuts down, or the program itself decides to 
shutdown.   It should _not_  die when I simply log-out, etc.

Is there a standard library module to help with this?

-- Thank you





  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

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


[Tutor] Pythonic way to "try a few times, then raise exception"?

2007-10-26 Thread Allen Fowler
Hello,

I have a block of code buried deep in a module  that I expect to fail 
periodically. (Calls to other machines over slow network, and such.)

Generally, though,  trying it a second / third will work.

Is there clean way to write this on Python?

Thanks

 __
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getattr__(): Is this right? [w/ new code]

2007-10-15 Thread Allen Fowler
Umm... well. obviously I left out an __setattr__() call..

sigh.  

thanks anyway...

Allen Fowler <[EMAIL PROTECTED]> wrote: 


What did you try? What happened? What did you expect?

Kent

Narrowed things down a bit.

Given this class:
---
class sc(object):


def __init__(self, **kargs):

self.valid_props =  [ 'foo', 'bar', 'baz' ]

for prop in self.valid_props:
if kargs.has_key(prop):
self.__setattr__(prop, kargs[prop])

def  __getattr__(self,attr):
if attr in self.valid_props:
print "__getattr__ was called for %s" % attr
return 'n/a'
else:
raise AttributeError, attr

This is my in/output:
-
In [1]: some_string_a = 'foo' 

In [2]: some_string_b = 'FOO' 

In [3]: s = sc( foo=some_string_b )

In [4]: s.foo
Out[4]: 'FOO'

In [5]: eval('s.' + some_string_a)
Out[5]: 'FOO'

In [6]: s.__getattr__(some_string_a)
__getattr__ was called for foo
Out[6]: 'n/a'

In [7]: s.__getattr__('foo')
__getattr__ was called for foo
Out[7]: 'n/a'

In [8]: s.bar
__getattr__ was called for bar
__getattr__ was called  for bar
Out[8]: 'n/a'

In [9]: s.__getattr__('bar')
__getattr__ was called for bar
Out[9]: 'n/a'


Here is what I expected:
--
Outputs 4 and 5 are fine.
Outputs 6 and 7 should have returned 'FOO'
Outputs 8 and 9 are fine. (But why two prints on 8?)

Thank you all.




-
Don't let your dream ride pass you by.Make it a reality with Yahoo! Autos.  
 ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


   
-
Moody friends. Drama queens. Your life? Nope! - their life, your story.
 Play Sims Stories at Yahoo! Games. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getattr__(): Is this right?

2007-10-15 Thread Allen Fowler



> "Note that if the attribute is found through the normal mechanism, 
> __getattr__() is not called. (This is an intentional asymmetry between 
> __getattr__() and __setattr__().) This is done both for efficiency 
> reasons and because otherwise __setattr__() would have no way to 
> access other attributes of the instance."

There you go, you are right and I am not.  I guess because I don't 
actually set the attribute of self, I can intercept both the get and set.

"Learn one new thing every day".   Done.  :-)

Happy to help :)

You may be interested in:
__getattribute__
http://docs.python.org/ref/new-style-attribute-access.html

That will get ALL requests.
 Read the warnings, though.




   
-
Tonight's top picks. What will you watch tonight? Preview the hottest shows on 
Yahoo! TV.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getattr__(): Is this right? [w/ new code]

2007-10-15 Thread Allen Fowler



What did you try? What happened? What did you expect?

Kent

Narrowed things down a bit.

Given this class:
---
class sc(object):


def __init__(self, **kargs):

self.valid_props =  [ 'foo', 'bar', 'baz' ]

for prop in self.valid_props:
if kargs.has_key(prop):
self.__setattr__(prop, kargs[prop])

def __getattr__(self,attr):
if attr in self.valid_props:
print "__getattr__ was called for %s" % attr
return 'n/a'
else:
raise AttributeError, attr

This is my in/output:
-
In [1]: some_string_a = 'foo' 

In [2]: some_string_b = 'FOO' 

In [3]: s = sc( foo=some_string_b )

In [4]: s.foo
Out[4]: 'FOO'

In [5]: eval('s.' + some_string_a)
Out[5]: 'FOO'

In [6]: s.__getattr__(some_string_a)
__getattr__ was called for foo
Out[6]: 'n/a'

In [7]: s.__getattr__('foo')
__getattr__ was called for foo
Out[7]: 'n/a'

In [8]: s.bar
__getattr__ was called for bar
__getattr__ was called for bar
Out[8]: 'n/a'

In [9]: s.__getattr__('bar')
__getattr__ was called for bar
Out[9]: 'n/a'


Here is what I expected:
--
Outputs 4 and 5 are fine.
Outputs 6 and 7 should have returned 'FOO'
Outputs 8 and 9 are fine. (But why two prints on 8?)

Thank you all.



   
-
Don't let your dream ride pass you by.Make it a reality with Yahoo! Autos. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getattr__(): Is this right?

2007-10-15 Thread Allen Fowler


Eric Brunson <[EMAIL PROTECTED]> wrote: Allen Fowler wrote:
> I seem to be having an issue with __getattr__() being called even if 
> the proporite already exists... I thought that this was not supposed 
> to happen.

I think you've misunderstood.  __getattr__() should always be called, it
allows you to intercept and reimplement the behavior of a attribute
lookup to suit your needs.  Typically, my __getattrs__() look like this:



See:
http://docs.python.org/ref/attribute-access.html


This would lead me to think not that way...


"Note that if the attribute is found through the normal mechanism, 
__getattr__() is not called.  (This is an intentional asymmetry between 
__getattr__() and __setattr__().) This is done both for efficiency reasons and 
because otherwise __setattr__() would have no way to access other attributes of 
the instance."




   
-
Building a website is a piece of cake. 
Yahoo! Small Business gives you all the tools to get online.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] __getattr__(): Is this right?

2007-10-15 Thread Allen Fowler
I seem to be having an issue with __getattr__() being called even if the 
proporite already exists... I thought that this was not supposed to happen.

Is there a typo somewhere, or I do i misunderstand things?

class someclass(object):


def __init__(self, **kargs):
   
self.valid_props =  [ 'foo', 'bar', 'baz' ]

for prop in self.valid_props:
if kargs.has_key(prop):
self.__setattr__(prop, kargs[prop])

def __getattr__(self,attr):
if attr in self.valid_props:
# This print should throw an exception,
# but it does not. It shows the value.
print "Oh no.. This was not found: %s" % self.__dict__[attr]
return 'n/a' 
else:
raise AttributeError, attr



   
-
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Exceptions: Logging TB and local variables?

2007-10-09 Thread Allen Fowler
Hi, 
 My code looks like this: 
 
for item in bigset: 
   self.__sub1(item) 
   self.__sub2(item) 
   self.__sub3(item) 
 
# the subX functions, in turn, use various 3rd party modules. 
 
Now, I would like to do this: 
 
for item in bigset: 
   try: 
 self.__sub1(item) 
 self.__sub2(item) 
 self.__sub3(item) 
   except StandardError: 
 # Log error and continue to next item in set. 
 log_error_to_file() 
 
In the error log, I would like to record a stacktrace and various local 
variables that existed in subX at the time the Exception was thrown... (even 
though the actual exception may have been thrown from deep inside some 3rd 
party module that subX called) 
 
How should I do this? 
 
Am I barking up the right tree?

Thank you, 
 Allen 
   
-
Need a vacation? Get great deals to amazing places on Yahoo! Travel. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor