Re: [Tutor] lst file

2008-01-24 Thread Remco Gerlich
Many things could be wrong; perhaps with reading the file, or the lines, or
with printing them... Debugging usually consists of what the problem is
exactly, where it occurs.

First thing I'd look at is seeing whether the lines do get read. I would
change "print line" into "print len(line)" and see if the lines are empty,
or filled with unprintable things.

Also, what sort of file is it?

Remco


On Jan 24, 2008 11:42 AM, SwartMumba snake <[EMAIL PROTECTED]> wrote:

> Hi
>
> I am trying to read from  a specific .lst file. When I read from it, it
> just prints out blank lines and I am sure that this file contains content.
>
> This is what I am using to read from the file:
>
> f = open("C:\\Users\\UserName\\Directory\\words.lst")
>
> for line in f:
> print line
>
> f.close()
>
> Do you guys know what is wrong?
>
> Thanks in advance.
>
> --
> Never miss a thing. Make Yahoo your 
> homepage.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a nested dictionary

2008-01-24 Thread Remco Gerlich
Of course, I forgot to mention that you can use tuples as dictionary keys.

A common idiom is:

pdb[(dataset, modulename, parametername, 'value')] = value.

And if that works for you, it's much simpler. But you lose the ability to
use, say, pdb[dataset][modulename] as a dictionary on its own.

Remco


On Jan 24, 2008 8:20 AM, Garry Willgoose <[EMAIL PROTECTED]>
wrote:

> Is there any easy way to create a nested dictionary. I want to be
> able to allocate like
>
> pdb[dataset][modulename][parametername]['value']=value
>
> where dataset, modulename, parametername are variables that are
> determined within loops nested 3 deep, and value comes from a
> database call. Prior to the nested loops I do not know what the
> values of dataset, modulename, parametername will range over, but I
> do know pdb needs to be nested 3 deep. What I'd like to do is
> something like this
>
> pdb={}
> for dataset in dataset_list:
>modulename_list=getmodules(dataset)
>for modulename in modulename_list:
>parametername_list=getparameters(dataset,modulename)
>for parametername in parametername_list:
>value=getvalue(dataset, modulename, parametername)
>
>  pdb[dataset][modulename][parametername]['value']=value
>
> What I'm currently doing is
>
> pdb={}
> for dataset in dataset_list:
>modulename_list=getmodules(dataset)
>moduledict={}
>for modulename in modulename_list:
>parametername_list=getparameters(dataset,modulename)
>valuedict={}
>for parametername in parametername_list:
>value=getvalue(dataset, modulename, parametername)
>valuedict['value']=value
> #  valuedict needs to be a dictionary because there is other stuff
>valuedict['otherstuff]=otherstuff
> ...
>parameterdict[parametername]=valuedict.copy()
>moduledict[modeulename]=copy.deepcopy(parameterdict)
>pdb[dataset]=copy.deepcopy(moduledict)
>
>
> Now I know the 2nd is not that much more complex but this is a pretty
> common construct in what I'm doing so I'm just wondering if there is
> a clear and simple shortcut ;-)
>
>
> 
> Prof Garry Willgoose,
> Australian Professorial Fellow in Environmental Engineering,
> Director, Centre for Climate Impact Management (C2IM),
> School of Engineering, The University of Newcastle,
> Callaghan, 2308
> Australia.
>
> Centre webpage: www.c2im.org.au
>
> Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574
> (Fri PM-Mon)
> FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal
> and Telluric)
> Env. Engg. Secretary: (International) +61 2 4921 6042
>
> email:  [EMAIL PROTECTED];
> [EMAIL PROTECTED]
> email-for-life: [EMAIL PROTECTED]
> personal webpage: www.telluricresearch.com/garry
> 
> "Do not go where the path may lead, go instead where there is no path
> and leave a trail"
>   Ralph Waldo Emerson
> 
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a nested dictionary

2008-01-24 Thread Remco Gerlich
Hi,

Stream of consciousness answer:

So, you want to set things in a dictionary with a statement like
d[a][b][c][d] = x,
 even though d[a] wasn't used before so it's not initialized yet (let alone
d[a][b]).

Of course, if d is a dict, the d[a] = x works. That's one level deep, and
non existing keys are just created for you. More levels is harder.

In the collections module, there is a defaultdict object. You can tell it
what to use as a default for some value if it doesn't exist yet. Other than
that, it works as a normal dict.
Say, if we do:

from collections import defaultdict
d = defaultdict(dict)

Then, if you use d[a] when it doesn't exist yet, it will call dict() to
create a new dictionary.

So now d[a][b] = x works.

But d[a][b][c] = x still doesn't, since the newly created dictionary isn't
_itself_ a default dictionary - it's a normal dict.

Instead of 'dict', we need to give defaultdict a function that takes no
arguments and returns a defaultdictionary object. Like

d = defaultdict(lambda: defaultdict(dict))

Now d[a][b][c] = x works. But, again, the last one is a dict, so we still
can't go deeper...

Now of course d = defaultdict(lambda: defaultdict(lambda:
defaultdict(dict))) is there. It solves your problem; you can now set
d[a][b][c][d] = x.

(importantly, you shouldn't mix depths; don't first set d[a][b][c] = 3 and
then try to set d[a][b][c][d]; since d[a][b][c] won't be a dictionary
anymore).

I can't think of a really good generalization, one that will work for all
depths. That's the sort of thing you use Lisp, combinators and lots of
coffee for. Perhaps later today.

For now though:

def deep_default_dict(level):
   if level < 1:
  raise ValueError()
   result = dict
   while level > 1:
  result = lambda: defaultdict(result)
  level -= 1
   return result

d = deep_default_dict(4)()

Should give a dictionary for level=1 and the appropriate defaultdict for the
number of levels you need.

But I can't test right now...

Remco


On Jan 24, 2008 8:20 AM, Garry Willgoose <[EMAIL PROTECTED]>
wrote:

> Is there any easy way to create a nested dictionary. I want to be
> able to allocate like
>
> pdb[dataset][modulename][parametername]['value']=value
>
> where dataset, modulename, parametername are variables that are
> determined within loops nested 3 deep, and value comes from a
> database call. Prior to the nested loops I do not know what the
> values of dataset, modulename, parametername will range over, but I
> do know pdb needs to be nested 3 deep. What I'd like to do is
> something like this
>
> pdb={}
> for dataset in dataset_list:
>modulename_list=getmodules(dataset)
>for modulename in modulename_list:
>parametername_list=getparameters(dataset,modulename)
>for parametername in parametername_list:
>value=getvalue(dataset, modulename, parametername)
>
>  pdb[dataset][modulename][parametername]['value']=value
>
> What I'm currently doing is
>
> pdb={}
> for dataset in dataset_list:
>modulename_list=getmodules(dataset)
>moduledict={}
>for modulename in modulename_list:
>parametername_list=getparameters(dataset,modulename)
>valuedict={}
>for parametername in parametername_list:
>value=getvalue(dataset, modulename, parametername)
>valuedict['value']=value
> #  valuedict needs to be a dictionary because there is other stuff
>valuedict['otherstuff]=otherstuff
> ...
>parameterdict[parametername]=valuedict.copy()
>moduledict[modeulename]=copy.deepcopy(parameterdict)
>pdb[dataset]=copy.deepcopy(moduledict)
>
>
> Now I know the 2nd is not that much more complex but this is a pretty
> common construct in what I'm doing so I'm just wondering if there is
> a clear and simple shortcut ;-)
>
>
> 
> Prof Garry Willgoose,
> Australian Professorial Fellow in Environmental Engineering,
> Director, Centre for Climate Impact Management (C2IM),
> School of Engineering, The University of Newcastle,
> Callaghan, 2308
> Australia.
>
> Centre webpage: www.c2im.org.au
>
> Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574
> (Fri PM-Mon)
> FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal
> and Telluric)
> Env. Engg. Secretary: (International) +61 2 4921 6042
>
> email:  [EMAIL PROTECTED];
> [EMAIL PROTECTED]
> email-for-life: [EMAIL PROTECTED]
> personal webpage: www.telluricresearch.com/garry
> 
> "Do not go where the path may lead, go instead where there is no path
> and leave a trail"
>   Ralph Waldo Emerson
> 
>
>
>
>
>
> ___
> Tutor maillis

Re: [Tutor] random.choice function

2008-01-19 Thread Remco Gerlich
Hi,

This line:

ch = "So good to see you!","How are you?","Everything good
today?","Glad you're here!".split(" ")

Creates a tuple with 4 elements:

1. "So good to see you!"
2. "How are you?"
3. "Everything good today?"
and
4. "Glad you're here!".split(" "), which is equal to ["Glad","you're",
"here"].

So it would seem that you just need to remove the .split(" "). It's probably
there from a previous try? What it does is break the string into a list,
splitting at every space.

Also, I'd recommend adding ( and ) around the tuple for readability, so
you'd get this:
ch = ("So good to see you!",
  "How are you?",
  "Everything good today?",
  "Glad you're here!",
)

Also a last ',' so it's easier to add new lines, move them around, delete
lines etc without accidently forgetting the comma. But these things are just
a matter of taste.

Remco


On Jan 19, 2008 12:37 PM, Cecilia Grahn <[EMAIL PROTECTED]> wrote:

> Hi,
> this is my first mail and I apologize if I got something wrong =)
> I am trying to make a script which returns a random string:
>
> #hello.py
> # Get the user's name and print a friendly hello
> import random, string
> name = raw_input("Your name please:")
> ch = "So good to see you!","How are you?","Everything good
> today?","Glad you're here!".split(" ")
> x = random.choice(ch)
> print "Hello", name, x
>
> everything works pretty much as I want it to, except when the last
> string is returned. It looks like this:
>
>  >>>  RESTART
> 
>  >>>
> Your name please:Cece
> Hello Cece ['Glad', "you're", 'here!']
>  >>>
>
> What is it I have missed or am I using  random.choice wrong?
>
>
> /CeCe
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] interview questions

2008-01-19 Thread Remco Gerlich
Hi.

I've interviewed people for a project in which we used Perl. Most of us
already on the team hadn't used Perl before that project, but we learned it
pretty quickly, so we knew that that was quite possible. Before the
interview we would have seen their CV, and decided we wanted to speak to
them, so any language specific questions in the interview (or programming
questions generally) would be to check if they really had the level they
claimed.

Some people said in their letter, I don't know Perl yet but I do know X and
Y, I can program, and I expect to be able to learn Perl. That was fine by
us, we did ask them things about how they'd do things in X and Y, and
general problems of Web programming, how would they go about learning a
language - but we didn't grill them on their Perl.

Then there were those who claimed they were good at Perl. Even though we
weren't specifically looking for great Perl programmers, if they claim to be
great... these very often failed at pretty simple stuff, they really were
just beginners. Not actually worse at Perl than the other candidates, but
they had claimed more.

So, be honest :-) Then it doesn't matter much what they ask, since you have
the level of Python knowledge that you claimed.

Remco Gerlich

On Jan 19, 2008 3:09 AM, Varsha Purohit <[EMAIL PROTECTED]> wrote:

> Hello All,
>I have an interview in python program development. Can i know some
> interview questions in python ? If you know any website where i can refer
> that would be helpful.
>
> thanks,
> --
> Varsha Purohit,
> Graduate Student
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2008-01-10 Thread Remco Gerlich
Hi,

A few days ago, someone posted a "daemon.py" to Reddit, that's supposed to
do everything needed. Haven't used it myself, but here it is:
http://hathawaymix.org/Software/Sketches/daemon.py

Remco

On Jan 10, 2008 6:41 AM, Allen Fowler <[EMAIL PROTECTED]> wrote:

> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to compare elements of 2 lists

2008-01-04 Thread Remco Gerlich
Hi,

a = [4,3,2,6,7,9]
b = [8,6,3,3,2,7]

You can turn this into a list of two element tuples with zip():
>>> zip(a,b)
[ (4,8),(3,6,),(2,3),(6,3),(7,2),(9,7) ]

Now you can loop through that and compare both elements, for instance I
believe this list comprehension is what you're looking for:

[ t[0] < t[1] for t in zip(a,b) ]

but of course we can write that more elegantly with

[ i wrote:

> sith . wrote:
> > Hi,
> > I've read the posts on comparing 2 lists and couldn't find the answer to
> > my question.
> > I have 2 lists
> > a = [4,3,2,6,7,9]
> > b = [8,6,3,3,2,7]
> > How can I determine if the elements in a are larger or smaller than the
> > elements in b.
> >
> > for i in a:
> > for u in b:
> > i > u
> > does not return the result I seek.
> >
> > In this example, 4 from a is compared to 8,6,3,3,2,7 then 3 from a is
> > compared to all the elements in b.
> > I'd like
> > 4 to 8,
> > 3 to 6,
> > 2 to 3 and so on; like 2 columns in excel, the third column would be a
> > new list of boolean values.
> > Can someone help please?  Thank you.
> >
> all(all(i>j for j in b) for i in a)
> HTH
>
>
> Hi,
> Thanks for your reply.  This is what I get:
> >>> a = [4,3,2,6,7,9]
> >>> b = [8,6,3,3,2,7]
> >>> all(all(i>j for j in b) for i in a)
> >>> all(all(i>j for j in b) for i in a)
> False
> How do I make it loop through the whole list?
>
> --
> Never miss a thing. Make Yahoo your 
> homepage.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Something I don't understand

2007-12-18 Thread Remco Gerlich
On Dec 18, 2007 2:44 AM, Jim Morcombe <[EMAIL PROTECTED]> wrote:

> Below, "student_seats" is a list of the class "student".


How do you create the list?

And yes, the naming is confusing, but I believe that only masks the class,
it shouldn't cause this problem.

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


Re: [Tutor] How to iterate and update subseqent items in list

2007-12-06 Thread Remco Gerlich
Hi,

In this case, I'd go for the simple old fashioned for loop with a boolean:

found = False
for thing in Things:
if thing.value > 0:
found = True
if found:
thing.value = 2

Remco Gerlich

On Dec 6, 2007 9:48 AM, ted b <[EMAIL PROTECTED]> wrote:

> Can you suggest a good way to iterate through the
> remainder of list and update them?
>
> Ex:
> Thing1.value = 0
> Thing2.value = 1
> Thing3.value = 0
> Thing4.value = 0
>
> Things = [Thing1, Thing2, Thing3, Thing4]
>
> I want to iterate through 'Things' and if
> 'Thing.value' > 0, then I want to set all values for
> that and the subsequent 'Things' in the list to 2
>
> So that i'd end up with
>
> Thing1.value = 0
> Thing2.value = 2
> Thing3.value = 2
> Thing4.value = 2
>
>
>
>  
> 
> 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best way of learning

2007-12-06 Thread Remco Gerlich
On Dec 5, 2007 11:43 PM, andy <[EMAIL PROTECTED]> wrote:

> So, after this long-winded introduction, I was hoping to pick the wisdom
> of this list to get some pointers of what to do/not to do to make the
> most effective use of the few hours I have to learn how to program using
> Python. So, any advice for someone in their mid-40s who would like to
> learn Python in a more methodical and effective manner?
>

In my opinion, the best way to learn _anything_ is to try to do it, and then
look around for tips every now and then in the mean time. You'll connect
much more easily with the information when it's something you've recently
struggled with. And practice is the most important part of any learning
anyway.

So, what do you want to do with Python? Any other hobbies you can connect it
with? Perhaps ideas for a dynamic web site or something?

In my experience, learning something just for abstract knowledge can be fun
for a while, but it's hard to stay committed. And there's nothing wrong with
that, there's only so many hours in a day, and the things you actually use
in life should probably take precedence :-)

So if you've written small tools, feel like expanding them? Used modules in
them that you don't entirely understand yet, perhaps dive into their docs?

Perhaps the Python Challenge ( http://www.pythonchallenge.com/ ) is
something for you? It's at least sideways a little into the hacking spirit,
it's fun, and it's a tour of what Python can do - but you'll have to find
the way yourself :-)

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


Re: [Tutor] Problems with List Server?

2007-12-06 Thread Remco Gerlich
It arrived.

Since you appear to be the only one reporting the problem, perhaps it's
something on your end?

Remco Gerlich

On Dec 3, 2007 11:51 PM, Tim Johnson <[EMAIL PROTECTED]> wrote:

> On Monday 03 December 2007, Tim Johnson wrote:
> > I appear to be having a weird problem with the List Server.
> > At first, email sent to this address did not appear at
> > all.
> > After contacting the ML maintainers only one email from
> > me to this address go through. When I replied to the
> > thread which the email started, it was not delivered.
> >
> > This is a very frustrating turn of events and I am hoping
> > that it will be corrected soon. I wonder if anyone else is
> > having similar problems?
> >
> > I suspect that this email will be delivered, but I might not
> > be able to send another on the same thread.
> >
> > Any ideas?
> > tim
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> If this is not delivered to the ML, then the problem
> persists.
> thanks
> Tim
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] opening a javascript window and reading its source

2007-11-27 Thread Remco Gerlich
Hi,

Python can't run Javascript functions. Note that "HTTPCookieProcessor"
is a HTTP thing, so it can handle  "http:" links, not "javascript:"
links.

Your best bet would probably be to download all the Javascript files
mentioned in the original HTML file, and try to find out what the
toggleBackpack() function does. If you can find out from that
Javascript function what the URL is that it opens in the new window,
then you can download that same URL from Python.

There is no such thing as a "Javascript window". Javascript can be
used to open perfectly normal windows, that contain normal HTML.

It's hard to give more help without knowing more about your problem.

Hope this helps a little,
Remco Gerlich

On Nov 27, 2007 1:10 AM, Chiar Nimeni <[EMAIL PROTECTED]> wrote:
> i basicly need python to open a certain javascript window ... then read it's
> source ... how do i do that?
>
> -
> import urllib2
> import cookielib
>
> cj = cookielib.CookieJar()
> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
>
> url = opener.open("The url that has a javascript window toggled on click")
> js = opener.open('javascript:toggleBackpack(null,2190551);')  # opening the
> JS window
> js2 = opener.open("javascript:toggleBackpack('orb',2190551);") # toggleing
> another section on the JS window
> source = js2.read()
> print source
>
>  
> Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to import modules using the input() command

2007-11-15 Thread Remco Gerlich
The input() function calls eval() on the value you give it. Eval() can
evaluate any _expression_. That's generally insecure and not
recommended - people could type in anything, as you're trying to do
:-)

But anyway, that is why 'import ...' directly doesn't work, import is
a statement, not an expression.

However, you can use the built-in function __import__(), that imports
the module and returns it. That loads it into memory, but it doesn't
add the module to the current namespace.

What do you need this for?

Remco Gerlich

On Nov 15, 2007 4:25 PM, Mihai Iacob <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I was wondering if there is a way to import modules
> using the input() command. If i try to do it directly
> it gives me an error:
>
> >>> input()
> import time
>
> Traceback (most recent call last):
>   File "", line 1, in 
> input()
>   File "", line 1
> import time
>  ^
> SyntaxError: invalid syntax
> >>>
>
>
> Is there another way in which i can import modules
> with the input() command?
>
>
>
>
>
>   
> 
> 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


Re: [Tutor] Obtaining image date of creation

2007-11-14 Thread Remco Gerlich
Hi,

I'm assuming you don't want the last changed date you see on the file
on your PC, but the creation date that the camera stored when the
picture was taken.

Now I don't know much about that, but I thought it was interesting, so
I did some Googling :-)

I don't know whether it's always stored, but many cameras do. There
are several formats. One of them is EXIF, no idea how universal that
is, but it seems to be pretty wide spread.

Unfortunately, the most common Python library for image stuff (PIL)
doesn't handle this.

However, some more Googling led me to the PyExif library (
http//pyexif.sourceforge.net/ ) and this post by someone who did the
exact thing you want to do:
http://www.leancrew.com/all-this/2007/08/photo_file_renaming_again.html

Hope this helps :-)

Remco

On Nov 14, 2007 1:04 PM, Roy Chen <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I would like to write a simple script that would rename a bunch of
> photos according to their creation date, i.e. the date which the
> photo was taken.
>
> Is there a way to use Python to obtain that information from the
> image file, or is that information not stored in the file?
>
> Thanks,
> Roy Chen
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor