Re: [Tutor] Setting PYTHONPATH and other env vars dynamically

2009-08-18 Thread Douglas Philips

On 2009 Aug 18, at 7:57 PM, Jramak wrote:

Hello
We have developed three custom applications in Python. Each one of
these applications needs a different PYTHONPATH, in addition to
different environment variables to work. Instead of manually setting
the environment variables for each application, what would be the best
way to set PYTHONPATH and other environment variables for a specific
application? We only run one application at a time, not all of them.
We are running Python 2.5.2 and Python 2.4.1 on Win2K. Is a bash
script that sets the environment variables on the application start-up
way to go?

Any ideas? I was not aware of site.py until a co-worker bandied it
about - he says site.py is better than PYTHONPATH.


Ok, well, if you are on Windows and your talking about bash scripts,  
it sounds like you might be using cygwin? Typically "scripts" on  
Windows are .BAT files, so I am not sure what you're referring to here.

I think you have five-ish general options:

a) set variables by hand. run program(s) by hand. (not very much fun,  
and possibly error prone.)


b) create scripts (bash or .BAT or whatever, depending on your  
situation) that set the variables and runs your program(s).


c) install whatever your programs need under the site-packages  
directory of your python install.
This assumes that all the packages your programs need have unique  
names. I can't tell from your description if they are different  
pacakges or if they might just be different versions of the same  
package. If they are just different versions of the same package,  
probably this option wouldn't work, it all depends on the specific  
files. It also depends on where you have Python 2.5.2. and Python  
2.4.1 installed and if you use them at the same time. This option  
still requires another option for setting the other environment  
variables.


d) Set up your environments in each of your main programs. Here is the  
boilerplate you might use:
# This needs to be at the top of the program after the module doc  
string (if any) and before any other statements or imports:


import sys
sys.path.append("dir1/dir2")  # Add dir1/dir2 to your Python search  
path. Can be absolute path or relative.

sys.path.append("dir3/dir4")
# ...

import os
os.environ['MYVARIABLE'] = 'MyVALUE'
os.environ['ANOTHERONE'] = 'someothervalue'
# ...

This has the nice property that everything is self contained. But that  
also can be a downside if the values need to be changed by someone  
else, they'd have to go and edit .py files.


e) option d and a (seems unlikely), option d and b, option d and c.  
Depending on what you variables are, and how often they change, it  
might make sense to put some of them (Python path or environment) into  
the programs, and the rest in scripts.


Hope this helps,
--Doug

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


Re: [Tutor] handling a textfile continued

2009-08-20 Thread Douglas Philips

Hi!


Hi!

There's three first rows that belong to the same subject. And then  
next three rows belong to another subject and so on, to the end of  
the file.


Thanks for clarifying, you're input comes in lines, and the lines are  
grouped in threes.


What I need to do, is put the three rows that goes together and  
belong to certain subject, on a one single line in the output  
textfile.

And that goes with the rest of the data to the end of the new file.


A few suggestions have already been posted under those general  
assumptions, and you should find at least one of them is practically  
your solution.


One thing that wasn't addressed is what happens if your file isn't an  
exact multiple of three lines long? What if it is 700 lines, or 802  
lines?  What kind of error handling/recovery do you need?


Assuming perfect input, a not too bad solution would be:

parts_so_far = []
for line in open("my-file-name-here", "rU"):   # "rU" means open the  
file to read Universal new-lines.

parts_so_far.append(line.rstrip())
if len(parts_so_far) == 3:
   print "".join(parts_so_far)
   parts_so_far = []

There are lots of other ways, some better, but this should work (I  
tested it on some of my own local files).


If you need some text separating the lines (a space/tab/comma,  
whatever), you can put it in the quotes of the "".join line.


--Doug



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


Re: [Tutor] mapping/filtering a sequence

2009-09-05 Thread Douglas Philips

On or about 2009 Sep 5, at 10:45 AM, Martin A. Brown indited:

Have you discovered the map() builtin yet?

I would imagine that others on this list will have some even more
elegant and efficient solutions for you, but here's a possibility:

 def filt_seq( thing ):
   if thing == 38:
 thing = thing - 4
   elif thing == 40:
 thing = thing - 1
   return thing

 l = [ 1, 17, 12, 38, 4, 40, 17, 19 ]
 l = map( filt_seq, l )

Good luck,



I must have missed a message or two, because I don't understand why  
math is being done on constants like that.

Given my misunderstanding, I'd go with:

my_map = { 38: 34, 40: 39 }

def filter_item(item):
return my_map.get(item, item)

l = [ ... ]
l = map(filter_item, l)


-Doug

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


Re: [Tutor] mapping/filtering a sequence

2009-09-05 Thread Douglas Philips

On 2009 Sep 5, at 12:22 PM, Mark Tolonen wrote:

As a list comp:


L=range(30,41)
[{38:34,40:39}.get(n,n) for n in L]

[30, 31, 32, 33, 34, 35, 36, 37, 34, 39, 39]



True, that is terse, but IMHO has maintainability issues. The mapping  
data structure and the method of transformation (.get()) are tangled  
in with the transformation itself.
Again, IMHO, unless this is a very short script, the maintenance  
should outweigh raw terseness.


-Doug

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


Re: [Tutor] Pyduino

2009-09-08 Thread Douglas Philips

Check out:
http://us.pycon.org/2009/conference/schedule/event/73/
which has the video of the talk as well.
That was one of the many talks I didn't see in person, and is on my  
queue to watch. Hmmm, just got bumped a lot higher on the queue since  
I've also recently been bitten by the Arduino bug. :)


-Doug


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


Re: [Tutor] Pyduino

2009-09-08 Thread Douglas Philips

Check out:
http://us.pycon.org/2009/conference/schedule/event/73/
which has the video of the talk as well.
That was one of the many talks I didn't see in person, and is on my  
queue to watch. Hmmm, just got bumped a lot higher on the queue  
since I've also recently been bitten by the Arduino bug. :)


For another microcontroller python, there is PyMite:
http://us.pycon.org/2009/openspace/ForEmbeddedSystems/
http://code.google.com/p/python-on-a-chip/

--Doug

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


Re: [Tutor] working with multiple sets

2009-09-08 Thread Douglas Philips

On or about 2009 Sep 8, at 1:51 PM, Alan Gauld indited:

"kevin parks"  wrote
What would this look like if i want to use a straight up built-in   
dictionary type and not the collections.defaultdict.


Not too different:


import collections
def foo():
lookup = collections.defaultdict(list)

# Doug: lookup = dict()

x = range(10)
y = range(5, 15)
z = range(8, 22)
sets = {'x': set(x), 'y': set(y), 'z': set(z)}
for key, value in sets.items():
   for element in value:

lookup[element] = lookup.get(element, []).append(key)
# Doug: That doesn't do what you think it does, it won't insert the  
new list into the dictionary.
# Doug: I think what you want is lookup.setdefault(element,  
[]).append(key)

print "\n", lookup, "\n\n"
for x in lookup:
lookup[x].sort()
print x, lookup[x]
print "\n"


At least I think thats all you need here.



>>> help(dict.setdefault)
setdefault(...)
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

-Doug


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


Re: [Tutor] Getting list of attributes from list of objects

2009-09-10 Thread Douglas Philips

On or about 2009 Sep 10, at 11:36 AM, Serdar Tumgoren indited:

I think a list comprehension might be the most compact way to
accomplish what you're after:

objcts = [a, b, c]

titles = [obj.title for obj in objcts]


That probably is the best form.


For the sake of showing other options:

from operator import attrgetter
#...
titles = map(attrgetter("title"), objcts)


# I love placeholder for this kind of thing.
from placeholder import __
#...
titles = map(__.title, objcts)

--Doug

P.S. placeholder can be found at: http://pypi.python.org/pypi/placeholder
P.P.S. I have a version that composes, so you can say: map((__ + 3) *  
2, my_numbers) -- http://bitbucket.org/dgou/placeholder2/


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


Re: [Tutor] "if clause" in list comprehensions.

2009-10-19 Thread Douglas Philips

On or about 2009 Oct 19, at 3:57 PM, Sander Sweers indited:

I missed that the try: did not return anything. I was thinking more of
something like this.

def upperfy(item):
   try:
   item.upper()
   return item
   except AttributeError:
   return item

Thanks for correcting me!


Depending on what 'item' is, item.upper() might return an new thing,  
but the code looks as if you're expecting .upper() to modify item  
itself. If item is a string, item.upper() will return a new string and  
leave item alone (since strings are immutable in Python).


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