From alan.gauld at btinternet.com  Sun Nov  1 01:05:23 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 1 Nov 2009 00:05:23 -0000
Subject: [Tutor] Adding Value to CSV
References: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>
Message-ID: <hcijc2$ojk$1@ger.gmane.org>

"Paras K." <paras80 at gmail.com> wrote

> When I have the line that has been read I need to add another value at 
> the
> end of it, or write the information into another csv file

If you are dealing with csv fioles you should look at using the csv module.

> for line in fh.readlines():
>            readline = line
>            ipline = readline

the readline and ipline variables are not needed. just use line!

>            ip = ipline.split(' ')[0]
>            split_ip = ip.split('.')
>            if ((split_ip[0] == '"152')):

you don't need the parentheses here, its not C.
And you definitely don't need two sets of them!

>                ff.write(readline)
>                totalcount +=1
>
> I need to add another piece, how can I add another field at the end of
> ff.write(line)

just add it to the end of line before writing it using any of the
normal string addition operations, eg:

line += newfield    or
line = "%s%s" % (line,newfield)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From davea at ieee.org  Sun Nov  1 13:02:24 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 01 Nov 2009 07:02:24 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
Message-ID: <4AED78D0.2040105@ieee.org>

Vincent Davis wrote:
> I ask this question in part because of a fee remarks from another question I
> ask "class attribute to initiate more classes"
>
> Basically I am simulation the process of applicants to schools and trying to
> ask/answer some questions like "what conditions do you need to have an
> optimal solution" Oh and to learn python. I basically had it working as a
> script rather than using a class structure but it was very inflexible and I
> still needed to learn about classes.
>
> What I have these classes
> class Applicant: has lots of attributes (self.gpa = random.gauss(50, 10)
> about the Applicant and def() defining how/why an applicant applies to an
> institution,
>
> class Institution: Lots of attributes (self.quality = random.gauss(50,
> 10)) about the Institution and def() defining how the institution considers
> the applicant.
>
> class Match: this defines the interaction of the population of Applicants
> and Institutions, i.e. the rules of the game and returns the outcome i.e.
> which Applicants went to which Institutions.
>
> As of now I have been running 1 Match at a time. Which is to say generate
> 8000 instances of Applicant and 300 instances of Institution and then run
> the match Match(app_list, inst_list) and I do this with a short script.
>
> So now I need to implement my monte-carlo. By that I mean that i want to set
> some of the initial condition such as GPA, and Quality and basically re-run
> the what I descried above, (generate applicant, institutions, match them)
>  Then save the results.
>
> So my plan way to make a new class. This class would define the Applicant
> characteristics "self.gpa = random.gauss(mean, SD)" and the
> institutions self.quality = random.gauss(mean, sd)
>
> so it would look something like this
>
>
> class RepeatMatch:
>     def __int__(self, app_mean, app_sd, inst_mean, inst_sd, match_ repeat)
>         self.app_mean = app_mean
>         ??..
>         self.match_repeat = match_repeat
>
>    def makeApplicants():
>
>    def makeInstitutions():
>
>    def runMatches(self)
>    # runs the match match_repeat number of times, saves results
>
> # then I calculate some characteristics of the results
>
>     def ratio_dist():
>          # returns the Mean and sd of GPA/Quality
> END OF CODE
>
> Does it make sense to do it this way? Is there a better/alternative way of
> thinking about this. In the end I want to compare the results of repeated
> simulations "RepeatMatch(50,2?.) Compared to RepeatMatch(50,15?.)"
> This is way I had ask the earlier question "class attribute to initiate more
> classes"
>
> Thanks
> Vincent Davis
>
>   
I worried that you might find my responses too complex, but nobody else 
has responded, and it's been almost a day.

I don't see anything wrong with your approach.  Since you're going to do 
multiple sets of data, it makes sense for an instance of a class 
(RepeatMatch) to hold the data for one such run.  In your original 
sample, it didn't make sense to me, but of course I didn't know where 
you were heading.  So I would add in instance attributes such as  
self.applicants=[]  to your __init__() method of RepeatMatch.  (I 
suspect you're planning to do exactly that)

I would caution you that each instance of RepeatMatch will then hold 
lots of the other members, so keeping them around could be expensive. So 
when run_matches() finishes its analysis, it might want to delete its 
lists of raw data (eg. self.applicants).  But other choices exist, and 
you can decide that when you see how the whole thing fits together.  
Perhaps you'll only have one such instance at a time.  But if you're 
going to do multiple things with the results, you may want this object 
to hang onto the results, but throw away the raw data when all the 
necessary results have been calculated.

Are you sure that the only distribution you're going to use is 
random.gauss() ?  If so, then you only need to pass mean and stddev to 
the RepeatMatch constructor, as you're doing.  But if you might need to 
compare that distribution with a different one, then you might want to 
use a function object, as I tried to describe earlier.


HTH
DaveA

From davea at ieee.org  Sun Nov  1 13:16:24 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 01 Nov 2009 07:16:24 -0500
Subject: [Tutor] Adding Value to CSV
In-Reply-To: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>
References: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>
Message-ID: <4AED7C18.4030502@ieee.org>

Paras K. wrote:
> I have some code that is going through a number of test.
>
> When I have the line that has been read I need to add another value at the
> end of it, or write the information into another csv file
>
> Example of my code:
>
> for line in fh.readlines():
>             readline = line
>             ipline = readline
>             ip = ipline.split(' ')[0]
>             split_ip = ip.split('.')
>             if ((split_ip[0] == '"152')):
>                 ff.write(readline)
>                 totalcount +=1
>
>
> I need to add another piece, how can I add another field at the end of
> ff.write(readline)
>
>
> Any assistance would be greatly appreciated. Thank You.
>
>   
If your program is correct so far, then you could add it simply with:
             ff.write(readline + " " + newdata)

Although your message subject says CSV, it looks like you're breaking 
the line up by spaces.  So if in fact each field is constrained not to 
have a space within, and there is a single space between fields, then 
the above will work.

If, on the other hand, you have to deal with fields that can contain the 
delimiter, perhaps escaped or quoted, then things get much more complicated.

DaveA

From kent37 at tds.net  Sun Nov  1 14:32:44 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 1 Nov 2009 09:32:44 -0400
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
Message-ID: <1c2a2c590911010532v219c9053pbc33a5dfc5ac1aff@mail.gmail.com>

On Sat, Oct 31, 2009 at 12:54 PM, Vincent Davis
<vincent at vincentdavis.net> wrote:

> So my plan way to make a new class. This class would define the Applicant
> characteristics "self.gpa = random.gauss(mean, SD)" and the
> institutions?self.quality = random.gauss(mean, sd)
> so it would look something like this
>
> class RepeatMatch:
> ?? ?def __int__(self, app_mean, app_sd, inst_mean, inst_sd, match_ repeat)
> ?? ? ? ?self.app_mean = app_mean
> ?? ? ? ???..
> ?? ? ? ?self.match_repeat = match_repeat
> ?? def makeApplicants():
> ?? def makeInstitutions():
> ?? def runMatches(self)
> ?? # runs the match match_repeat number of times, saves results
> # then I calculate some characteristics of the results
> ?? ?def ratio_dist():
> ?? ? ? ? # returns the Mean and sd of GPA/Quality
> END OF CODE
> Does it make sense to do it this way? Is there a better/alternative way of
> thinking about this. In the end I want to compare the results of repeated
> simulations "RepeatMatch(50,2?.) Compared to?RepeatMatch(50,15?.)"
> This is way I had ask the earlier question "class attribute to initiate more
> classes"

This class has a lot of responsibilities:
- create applicants
- create institutions
- run a single match
- run multiple matches
- calculate statistics on the result of multiple matches

A principle of object-oriented design is that a class should have a
single responsibility. I would break your class up a bit using
multiple classes, perhaps
MatchParameters - holds app_mean, etc
Match - create applicants and institutions and run a single match,
yielding a MatchResult
MatchResult - the result of running a single match
RepeatMatch - run multiple matches and accumulate results, yielding a
RepeatResults
RepeatResults - the result of running multiple matches - knows how to
compute stats on itself

You may think of better names, or a different way to organize it, but
the idea is, don't shove everything into one class. Some of these may
be just built-in data structures or very simple classes, such as
MatchParameters which might be a dict or a collections.namedtuple.

Kent

From vincent at vincentdavis.net  Sun Nov  1 15:47:04 2009
From: vincent at vincentdavis.net (Vincent Davis)
Date: Sun, 1 Nov 2009 08:47:04 -0600
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <4AED78D0.2040105@ieee.org>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
Message-ID: <77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>

Kent Johsnon writes
"This class has a lot of responsibilities:
- create applicants
- create institutions
- run a single match
- run multiple matches
- calculate statistics on the result of multiple matches
A principle of object-oriented design is that a class should have a
single responsibility. I would break your class up a bit using
multiple classes, perhaps."

I am trying to do what you recomend, as much as makes sense to me, but that
is why I ask the question so I should consider your answer.
This is what I hear you saying, (I don't mean to represent them as
sub-classes but more how they would operate on each other) Should I consider
making Institutions) a subclass of (Make Institutions)? I can't think of
anything that would make sense to inherit.

class Simulation:
    class Create Institutions:
        class Institutions:
    class create Applicants:
        class Applicants:
    class Match:
    class Multi Match:

I add I am thinking

class Simulation:
    def__init__:(self, results, stats.....repeat..)
    def create_applicants():
        class Applicants
    def creat_institutions():
        class Institutions
    def one_simulation(): # one or more
        create_applicants()
        create_institutions()
        class Match()
        class Results
    def repeat_simulation()
        repeat one_simulations
        class Results

After writing this out I now think you are right, more classes. Which means
I really need to play with function objects to understand how they are
passed down the layers

Simulation(GPA = random.gauss(50, 10), repeat = 100.....)
    MakeApplicants(GPA)
        Applicants(GPA)  # I need to make sure the GPA is calculated at each
applicant not back at Simulation.

DaveA
ask if it will always be random.gauss? No, I would like it to be anything
from a constant to a much more complex function)

DaveA
"I would caution you that each instance of RepeatMatch will then hold lots
of the other members, so keeping them around could be expensive"

This means they stay in memory? Is there a way to know how much room a
instance takes up, in memory or hard drive?
I could serialize them a save them to a sqlite when done with a simulation
correct? How is a questions for later.

"worried that you might find my responses too complex"
I am kinda working in a vacuum, with respect to python programing. Complex
and difficult are ok, The challenge is not knowing what I don't know. I need
to practice with function objects and run a few experiments still to make
sureI understand them.

DaveA and Kent Thanks for all your help, Vincent
To add a little more to what I am doing I am using CherryPy to host this
simulation. So the GPA function and other simulation variables can be
entered in a html form and passed to the Simulation class. Then I am
calculating results as well as using matplotlib to make some plots, these
results and plots then get show. I think I have most of the CherryPy stuff
figured out once I have the Simulations class setup to do the whole thing.
The only part with CherryPy I am still working on is displaying progress on
the browser as the simulations runs, it can take several minutes.

Thanks
Vincent Davis
720-301-3003


On Sun, Nov 1, 2009 at 6:02 AM, Dave Angel <davea at ieee.org> wrote:

> Vincent Davis wrote:
>
>> I ask this question in part because of a fee remarks from another question
>> I
>> ask "class attribute to initiate more classes"
>>
>> Basically I am simulation the process of applicants to schools and trying
>> to
>> ask/answer some questions like "what conditions do you need to have an
>> optimal solution" Oh and to learn python. I basically had it working as a
>> script rather than using a class structure but it was very inflexible and
>> I
>> still needed to learn about classes.
>>
>> What I have these classes
>> class Applicant: has lots of attributes (self.gpa = random.gauss(50, 10)
>> about the Applicant and def() defining how/why an applicant applies to an
>> institution,
>>
>> class Institution: Lots of attributes (self.quality = random.gauss(50,
>> 10)) about the Institution and def() defining how the institution
>> considers
>> the applicant.
>>
>> class Match: this defines the interaction of the population of Applicants
>> and Institutions, i.e. the rules of the game and returns the outcome i.e.
>> which Applicants went to which Institutions.
>>
>> As of now I have been running 1 Match at a time. Which is to say generate
>> 8000 instances of Applicant and 300 instances of Institution and then run
>> the match Match(app_list, inst_list) and I do this with a short script.
>>
>> So now I need to implement my monte-carlo. By that I mean that i want to
>> set
>> some of the initial condition such as GPA, and Quality and basically
>> re-run
>> the what I descried above, (generate applicant, institutions, match them)
>>  Then save the results.
>>
>> So my plan way to make a new class. This class would define the Applicant
>> characteristics "self.gpa = random.gauss(mean, SD)" and the
>> institutions self.quality = random.gauss(mean, sd)
>>
>> so it would look something like this
>>
>>
>> class RepeatMatch:
>>    def __int__(self, app_mean, app_sd, inst_mean, inst_sd, match_ repeat)
>>        self.app_mean = app_mean
>>        ??..
>>        self.match_repeat = match_repeat
>>
>>   def makeApplicants():
>>
>>   def makeInstitutions():
>>
>>   def runMatches(self)
>>   # runs the match match_repeat number of times, saves results
>>
>> # then I calculate some characteristics of the results
>>
>>    def ratio_dist():
>>         # returns the Mean and sd of GPA/Quality
>> END OF CODE
>>
>> Does it make sense to do it this way? Is there a better/alternative way of
>> thinking about this. In the end I want to compare the results of repeated
>> simulations "RepeatMatch(50,2?.) Compared to RepeatMatch(50,15?.)"
>> This is way I had ask the earlier question "class attribute to initiate
>> more
>> classes"
>>
>> Thanks
>> Vincent Davis
>>
>>
>>
> I worried that you might find my responses too complex, but nobody else has
> responded, and it's been almost a day.
>
> I don't see anything wrong with your approach.  Since you're going to do
> multiple sets of data, it makes sense for an instance of a class
> (RepeatMatch) to hold the data for one such run.  In your original sample,
> it didn't make sense to me, but of course I didn't know where you were
> heading.  So I would add in instance attributes such as  self.applicants=[]
>  to your __init__() method of RepeatMatch.  (I suspect you're planning to do
> exactly that)
>
> I would caution you that each instance of RepeatMatch will then hold lots
> of the other members, so keeping them around could be expensive. So when
> run_matches() finishes its analysis, it might want to delete its lists of
> raw data (eg. self.applicants).  But other choices exist, and you can decide
> that when you see how the whole thing fits together.  Perhaps you'll only
> have one such instance at a time.  But if you're going to do multiple things
> with the results, you may want this object to hang onto the results, but
> throw away the raw data when all the necessary results have been calculated.
>
> Are you sure that the only distribution you're going to use is
> random.gauss() ?  If so, then you only need to pass mean and stddev to the
> RepeatMatch constructor, as you're doing.  But if you might need to compare
> that distribution with a different one, then you might want to use a
> function object, as I tried to describe earlier.
>
>
> HTH
> DaveA
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091101/64689948/attachment-0001.htm>

From kent37 at tds.net  Sun Nov  1 16:58:49 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 1 Nov 2009 10:58:49 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
Message-ID: <1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>

On Sun, Nov 1, 2009 at 10:47 AM, Vincent Davis <vincent at vincentdavis.net> wrote:
> Kent Johsnon writes
> "This class has a lot of responsibilities:
> - create applicants
> - create institutions
> - run a single match
> - run multiple matches
> - calculate statistics on the result of multiple matches
> A principle of object-oriented design is that a class should have a
> single responsibility. I would break your class up a bit using
> multiple classes, perhaps."
> I am trying to do what you recomend, as much as makes sense to me, but that
> is why I ask the question so I should consider your answer.
> This is what I hear you saying, (I don't mean to represent them as
> sub-classes but more how they would operate on each other) Should I consider
> making Institutions)?a subclass of (Make Institutions)? I can't think of
> anything that would make sense to inherit.
> class Simulation:
> ?? ?class Create Institutions:
> ?? ? ? ?class Institutions:
> ?? ?class create Applicants:
> ?? ? ? ?class Applicants:
> ?? ?class Match:
> ?? ?class Multi Match:
> I add I am thinking
> class Simulation:
> ?? ?def__init__:(self, results, stats.....repeat..)
> ?? ?def create_applicants():
> ?? ? ? ?class Applicants
> ?? ?def creat_institutions():
> ?? ? ? ?class Institutions
> ?? ?def one_simulation(): # one or more
> ?? ? ? ?create_applicants()
> ?? ? ? ?create_institutions()
> ?? ? ? ?class Match()
> ?? ? ? ?class Results
> ?? ?def repeat_simulation()
> ?? ? ? ?repeat one_simulations
> ?? ? ? ?class Results
> After writing this out I now think you are right, more classes.

Now you are getting too complicated. You don't need to use inheritance
or nested classes, and you can use simple methods (not classes) to
create applicants and institutions. You already have Applicant,
Institution and Match classes that run a single match. Now make a
RepeatMatch class that uses the Match class to run multiple
simulations.

Kent

From vincent at vincentdavis.net  Sun Nov  1 17:15:33 2009
From: vincent at vincentdavis.net (Vincent Davis)
Date: Sun, 1 Nov 2009 10:15:33 -0600
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
Message-ID: <77e831100911010815j54fe546drb7a15bfdd53a9260@mail.gmail.com>

Just to be clear,or try, given a set of applicants and institutions the
Match will always have the same result. So when I am repeating the Match
this only makes sense to do is I am also making new applicants and
institutions. So I am sampling Match results drawn from a process that is
initiated with a distributions set at the applicant and institution level.

Thanks
Vincent Davis


On Sun, Nov 1, 2009 at 9:58 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Sun, Nov 1, 2009 at 10:47 AM, Vincent Davis <vincent at vincentdavis.net>
> wrote:
> > Kent Johsnon writes
> > "This class has a lot of responsibilities:
> > - create applicants
> > - create institutions
> > - run a single match
> > - run multiple matches
> > - calculate statistics on the result of multiple matches
> > A principle of object-oriented design is that a class should have a
> > single responsibility. I would break your class up a bit using
> > multiple classes, perhaps."
> > I am trying to do what you recomend, as much as makes sense to me, but
> that
> > is why I ask the question so I should consider your answer.
> > This is what I hear you saying, (I don't mean to represent them as
> > sub-classes but more how they would operate on each other) Should I
> consider
> > making Institutions) a subclass of (Make Institutions)? I can't think of
> > anything that would make sense to inherit.
> > class Simulation:
> >     class Create Institutions:
> >         class Institutions:
> >     class create Applicants:
> >         class Applicants:
> >     class Match:
> >     class Multi Match:
> > I add I am thinking
> > class Simulation:
> >     def__init__:(self, results, stats.....repeat..)
> >     def create_applicants():
> >         class Applicants
> >     def creat_institutions():
> >         class Institutions
> >     def one_simulation(): # one or more
> >         create_applicants()
> >         create_institutions()
> >         class Match()
> >         class Results
> >     def repeat_simulation()
> >         repeat one_simulations
> >         class Results
> > After writing this out I now think you are right, more classes.
>
> Now you are getting too complicated. You don't need to use inheritance
> or nested classes, and you can use simple methods (not classes) to
> create applicants and institutions. You already have Applicant,
> Institution and Match classes that run a single match. Now make a
> RepeatMatch class that uses the Match class to run multiple
> simulations.
>
> Kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091101/0221efc2/attachment.htm>

From kent37 at tds.net  Sun Nov  1 18:46:14 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 1 Nov 2009 12:46:14 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <77e831100911010815j54fe546drb7a15bfdd53a9260@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
	<77e831100911010815j54fe546drb7a15bfdd53a9260@mail.gmail.com>
Message-ID: <1c2a2c590911010946uef9716bj2a29736eb942845d@mail.gmail.com>

On Sun, Nov 1, 2009 at 11:15 AM, Vincent Davis <vincent at vincentdavis.net> wrote:
> Just to be clear,or try, given a set of applicants and institutions the
> Match will always have the same result.

Yes. You have to create a new Match, with new Applicants and
Institutions, for each run of the simulation.

> So when I am repeating the Match
> this only makes sense to do is I am also making new applicants and
> institutions. So I am sampling Match results drawn from a process that is
> initiated with a distributions set at the applicant and institution level.

I don't know what you mean by this. A Match can be created with rules
for its distributions. The Match then creates Applicants and
Institutions for its run, does the simulation and returns the results.
A Simulation also has the distribution rules, so it can create Matches
using those rules.

Kent

From davea at ieee.org  Mon Nov  2 02:32:52 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 01 Nov 2009 20:32:52 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>	
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
Message-ID: <4AEE36C4.8040501@ieee.org>

(This is too hard to follow, so I'm just going to respond to Kent's 
subsequent email.  If I missed anything from here, please quote it more 
reasonably)

Vincent Davis wrote:
> Kent Johsnon writes
> "This class has a lot of responsibilities:
> - create applicants
> - create institutions
> - run a single match
> - run multiple matches
> - calculate statistics on the result of multiple matches
> A principle of object-oriented design is that a class should have a
> single responsibility. I would break your class up a bit using
> multiple classes, perhaps."
>
> I am trying to do what you recomend, as much as makes sense to me, but that
> is why I ask the question so I should consider your answer.
> This is what I hear you saying, (I don't mean to represent them as
> sub-classes but more how they would operate on each other) Should I consider
> making Institutions) a subclass of (Make Institutions)? I can't think of
> anything that would make sense to inherit.
>
> class Simulation:
>     class Create Institutions:
>         class Institutions:
>     class create Applicants:
>         class Applicants:
>     class Match:
>     class Multi Match:
>
> I add I am thinking
>
> class Simulation:
>     def__init__:(self, results, stats.....repeat..)
>     def create_applicants():
>         class Applicants
>     def creat_institutions():
>         class Institutions
>     def one_simulation(): # one or more
>         create_applicants()
>         create_institutions()
>         class Match()
>         class Results
>     def repeat_simulation()
>         repeat one_simulations
>         class Results
>
> After writing this out I now think you are right, more classes. Which means
> I really need to play with function objects to understand how they are
> passed down the layers
>
> Simulation(GPA = random.gauss(50, 10), repeat = 100.....)
>     MakeApplicants(GPA)
>         Applicants(GPA)  # I need to make sure the GPA is calculated at each
> applicant not back at Simulation.
>
> DaveA
> ask if it will always be random.gauss? No, I would like it to be anything
> from a constant to a much more complex function)
>
> DaveA
> "I would caution you that each instance of RepeatMatch will then hold lots
> of the other members, so keeping them around could be expensive"
>
> This means they stay in memory? Is there a way to know how much room a
> instance takes up, in memory or hard drive?
> I could serialize them a save them to a sqlite when done with a simulation
> correct? How is a questions for later.
>
> "worried that you might find my responses too complex"
> I am kinda working in a vacuum, with respect to python programing. Complex
> and difficult are ok, The challenge is not knowing what I don't know. I need
> to practice with function objects and run a few experiments still to make
> sureI understand them.
>
> DaveA and Kent Thanks for all your help, Vincent
> To add a little more to what I am doing I am using CherryPy to host this
> simulation. So the GPA function and other simulation variables can be
> entered in a html form and passed to the Simulation class. Then I am
> calculating results as well as using matplotlib to make some plots, these
> results and plots then get show. I think I have most of the CherryPy stuff
> figured out once I have the Simulations class setup to do the whole thing.
> The only part with CherryPy I am still working on is displaying progress on
> the browser as the simulations runs, it can take several minutes.
>
> Thanks
> Vincent Davis
> 720-301-3003
>
>   
<snip>


From davea at ieee.org  Mon Nov  2 02:34:56 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 01 Nov 2009 20:34:56 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>	
	<4AED78D0.2040105@ieee.org>	
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
Message-ID: <4AEE3740.5090202@ieee.org>

Kent Johnson wrote:
> On Sun, Nov 1, 2009 at 10:47 AM, Vincent Davis <vincent at vincentdavis.net> wrote:
>   
>> Kent Johsnon writes
>> "This class has a lot of responsibilities:
>> - create applicants
>> - create institutions
>> - run a single match
>> - run multiple matches
>> - calculate statistics on the result of multiple matches
>> A principle of object-oriented design is that a class should have a
>> single responsibility. I would break your class up a bit using
>> multiple classes, perhaps."
>> I am trying to do what you recomend, as much as makes sense to me, but that
>> is why I ask the question so I should consider your answer.
>> This is what I hear you saying, (I don't mean to represent them as
>> sub-classes but more how they would operate on each other) Should I consider
>> making Institutions) a subclass of (Make Institutions)? I can't think of
>> anything that would make sense to inherit.
>> class Simulation:
>>     class Create Institutions:
>>         class Institutions:
>>     class create Applicants:
>>         class Applicants:
>>     class Match:
>>     class Multi Match:
>> I add I am thinking
>> class Simulation:
>>     def__init__:(self, results, stats.....repeat..)
>>     def create_applicants():
>>         class Applicants
>>     def creat_institutions():
>>         class Institutions
>>     def one_simulation(): # one or more
>>         create_applicants()
>>         create_institutions()
>>         class Match()
>>         class Results
>>     def repeat_simulation()
>>         repeat one_simulations
>>         class Results
>> After writing this out I now think you are right, more classes.
>>     
>
> Now you are getting too complicated. You don't need to use inheritance
> or nested classes, and you can use simple methods (not classes) to
> create applicants and institutions. You already have Applicant,
> Institution and Match classes that run a single match. Now make a
> RepeatMatch class that uses the Match class to run multiple
> simulations.
>
> Kent
>
>   
I mostly agree with Kent, but I apparently disagree about which classes 
are actually needed.  Think what things will have actual instances that 
will last long enough to be worth formally defining.  So you need 
Applicant, and Institution, and Simulation.  Notice they're all 
singular.  I'm assuming one simulation is a single set of test data, 
with its results.  Then you create as many instances of Simulation as 
you need for comparison purposes, and perhaps keep a list of them.  It's 
not clear that list needs any further structure than the built-in list 
type provides.

You don't need a class for creating an Applicant, that's just a line or 
two in a loop in the Simulation class.  Similarly for Institution.

And if I understand it correctly, you don't need very many different 
methods in Simulation either.  You need the __init__ to save enough 
information to "tag" this particular simulation (call it a label, it's 
probably just a string).  If __init__ is too complex, you may want to 
break it into several phases.  But they'll always be called in direct 
succession, so there may not be any point.  Then you need something that 
triggers an analysis, and something that queries for particular 
results.  That last will then be called from plotting or charting routines.

But you probably don't need anything special for a collection of 
Simulation objects.  A list will probably be fine.

And from what you said earlier, you WILL need function objects, probably 
as parameters to the Simulation constructor.  So each instance of 
Simulation will be given several function objects to specify the 
distribution functions for the parameters to be used when creating 
Applicants and Institutions.

DaveA



From vincent at vincentdavis.net  Mon Nov  2 02:53:03 2009
From: vincent at vincentdavis.net (Vincent Davis)
Date: Sun, 1 Nov 2009 18:53:03 -0700
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <4AEE3740.5090202@ieee.org>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
	<4AEE3740.5090202@ieee.org>
Message-ID: <77e831100911011753l6ead32d5kc5d6b7f8eabcc3d1@mail.gmail.com>

Thanks again for all your help Kent and Dave.
I think you won't here from me for a week or more as I digest your advise
and work on my project.

Thanks
Vincent Davis
720-301-3003


On Sun, Nov 1, 2009 at 6:34 PM, Dave Angel <davea at ieee.org> wrote:

> Kent Johnson wrote:
>
>> On Sun, Nov 1, 2009 at 10:47 AM, Vincent Davis <vincent at vincentdavis.net>
>> wrote:
>>
>>
>>> Kent Johsnon writes
>>> "This class has a lot of responsibilities:
>>> - create applicants
>>> - create institutions
>>> - run a single match
>>> - run multiple matches
>>> - calculate statistics on the result of multiple matches
>>> A principle of object-oriented design is that a class should have a
>>> single responsibility. I would break your class up a bit using
>>> multiple classes, perhaps."
>>> I am trying to do what you recomend, as much as makes sense to me, but
>>> that
>>> is why I ask the question so I should consider your answer.
>>> This is what I hear you saying, (I don't mean to represent them as
>>> sub-classes but more how they would operate on each other) Should I
>>> consider
>>> making Institutions) a subclass of (Make Institutions)? I can't think of
>>> anything that would make sense to inherit.
>>> class Simulation:
>>>    class Create Institutions:
>>>        class Institutions:
>>>    class create Applicants:
>>>        class Applicants:
>>>    class Match:
>>>    class Multi Match:
>>> I add I am thinking
>>> class Simulation:
>>>    def__init__:(self, results, stats.....repeat..)
>>>    def create_applicants():
>>>        class Applicants
>>>    def creat_institutions():
>>>        class Institutions
>>>    def one_simulation(): # one or more
>>>        create_applicants()
>>>        create_institutions()
>>>        class Match()
>>>        class Results
>>>    def repeat_simulation()
>>>        repeat one_simulations
>>>        class Results
>>> After writing this out I now think you are right, more classes.
>>>
>>>
>>
>> Now you are getting too complicated. You don't need to use inheritance
>> or nested classes, and you can use simple methods (not classes) to
>> create applicants and institutions. You already have Applicant,
>> Institution and Match classes that run a single match. Now make a
>> RepeatMatch class that uses the Match class to run multiple
>> simulations.
>>
>> Kent
>>
>>
>>
> I mostly agree with Kent, but I apparently disagree about which classes are
> actually needed.  Think what things will have actual instances that will
> last long enough to be worth formally defining.  So you need Applicant, and
> Institution, and Simulation.  Notice they're all singular.  I'm assuming one
> simulation is a single set of test data, with its results.  Then you create
> as many instances of Simulation as you need for comparison purposes, and
> perhaps keep a list of them.  It's not clear that list needs any further
> structure than the built-in list type provides.
>
> You don't need a class for creating an Applicant, that's just a line or two
> in a loop in the Simulation class.  Similarly for Institution.
>
> And if I understand it correctly, you don't need very many different
> methods in Simulation either.  You need the __init__ to save enough
> information to "tag" this particular simulation (call it a label, it's
> probably just a string).  If __init__ is too complex, you may want to break
> it into several phases.  But they'll always be called in direct succession,
> so there may not be any point.  Then you need something that triggers an
> analysis, and something that queries for particular results.  That last will
> then be called from plotting or charting routines.
>
> But you probably don't need anything special for a collection of Simulation
> objects.  A list will probably be fine.
>
> And from what you said earlier, you WILL need function objects, probably as
> parameters to the Simulation constructor.  So each instance of Simulation
> will be given several function objects to specify the distribution functions
> for the parameters to be used when creating Applicants and Institutions.
>
> DaveA
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091101/5e6fa32e/attachment.htm>

From kent37 at tds.net  Mon Nov  2 03:22:31 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 1 Nov 2009 21:22:31 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <4AEE3740.5090202@ieee.org>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
	<4AEE3740.5090202@ieee.org>
Message-ID: <1c2a2c590911011822m27036af3ub26c9202775cdd52@mail.gmail.com>

On Sun, Nov 1, 2009 at 8:34 PM, Dave Angel <davea at ieee.org> wrote:
> I mostly agree with Kent, but I apparently disagree about which classes are
> actually needed. ?Think what things will have actual instances that will
> last long enough to be worth formally defining. ?So you need Applicant, and
> Institution, and Simulation. ?Notice they're all singular. ?I'm assuming one
> simulation is a single set of test data, with its results. ?Then you create
> as many instances of Simulation as you need for comparison purposes, and
> perhaps keep a list of them. ?It's not clear that list needs any further
> structure than the built-in list type provides.

That's pretty much what I have been suggesting. I think you may be
disagreeing with Vincent's interpretation of my suggestion :-) It
might be worth having a results class for the simulation to hold the
code that computes statistics on the results. I think it's probably
worth having a class to run the multiple simulations but that may just
be a function.

> You don't need a class for creating an Applicant, that's just a line or two
> in a loop in the Simulation class. ?Similarly for Institution.

Right.
>
> And if I understand it correctly, you don't need very many different methods
> in Simulation either. ?You need the __init__ to save enough information to
> "tag" this particular simulation (call it a label, it's probably just a
> string). ?If __init__ is too complex, you may want to break it into several
> phases. ?But they'll always be called in direct succession, so there may not
> be any point. ?Then you need something that triggers an analysis, and
> something that queries for particular results. ?That last will then be
> called from plotting or charting routines.

Kent.

From davea at ieee.org  Mon Nov  2 03:25:42 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 01 Nov 2009 21:25:42 -0500
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <4AEE3740.5090202@ieee.org>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>		<4AED78D0.2040105@ieee.org>		<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
	<4AEE3740.5090202@ieee.org>
Message-ID: <4AEE4326.9030802@ieee.org>

Dave Angel wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Kent 
> Johnson wrote:
>> On Sun, Nov 1, 2009 at 10:47 AM, Vincent Davis 
>> <vincent at vincentdavis.net> wrote:
>>  
>>> Kent Johsnon writes
>>> "This class has a lot of responsibilities:
>>> - create applicants
>>> - create institutions
>>> - run a single match
>>> - run multiple matches
>>> - calculate statistics on the result of multiple matches
>>> A principle of object-oriented design is that a class should have a
>>> single responsibility. I would break your class up a bit using
>>> multiple classes, perhaps."
>>> I am trying to do what you recomend, as much as makes sense to me, 
>>> but that
>>> is why I ask the question so I should consider your answer.
>>> This is what I hear you saying, (I don't mean to represent them as
>>> sub-classes but more how they would operate on each other) Should I 
>>> consider
>>> making Institutions) a subclass of (Make Institutions)? I can't 
>>> think of
>>> anything that would make sense to inherit.
>>> class Simulation:
>>>     class Create Institutions:
>>>         class Institutions:
>>>     class create Applicants:
>>>         class Applicants:
>>>     class Match:
>>>     class Multi Match:
>>> I add I am thinking
>>> class Simulation:
>>>     def__init__:(self, results, stats.....repeat..)
>>>     def create_applicants():
>>>         class Applicants
>>>     def creat_institutions():
>>>         class Institutions
>>>     def one_simulation(): # one or more
>>>         create_applicants()
>>>         create_institutions()
>>>         class Match()
>>>         class Results
>>>     def repeat_simulation()
>>>         repeat one_simulations
>>>         class Results
>>> After writing this out I now think you are right, more classes.
>>>     
>>
>> Now you are getting too complicated. You don't need to use inheritance
>> or nested classes, and you can use simple methods (not classes) to
>> create applicants and institutions. You already have Applicant,
>> Institution and Match classes that run a single match. Now make a
>> RepeatMatch class that uses the Match class to run multiple
>> simulations.
>>
>> Kent
>>
>>   
> I mostly agree with Kent, but I apparently disagree about which 
> classes are actually needed.  Think what things will have actual 
> instances that will last long enough to be worth formally defining.  
> So you need Applicant, and Institution, and Simulation.  Notice 
> they're all singular.  I'm assuming one simulation is a single set of 
> test data, with its results.  Then you create as many instances of 
> Simulation as you need for comparison purposes, and perhaps keep a 
> list of them.  It's not clear that list needs any further structure 
> than the built-in list type provides.
>
> You don't need a class for creating an Applicant, that's just a line 
> or two in a loop in the Simulation class.  Similarly for Institution.
>
> And if I understand it correctly, you don't need very many different 
> methods in Simulation either.  You need the __init__ to save enough 
> information to "tag" this particular simulation (call it a label, it's 
> probably just a string).  If __init__ is too complex, you may want to 
> break it into several phases.  But they'll always be called in direct 
> succession, so there may not be any point.  Then you need something 
> that triggers an analysis, and something that queries for particular 
> results.  That last will then be called from plotting or charting 
> routines.
>
> But you probably don't need anything special for a collection of 
> Simulation objects.  A list will probably be fine.
>
> And from what you said earlier, you WILL need function objects, 
> probably as parameters to the Simulation constructor.  So each 
> instance of Simulation will be given several function objects to 
> specify the distribution functions for the parameters to be used when 
> creating Applicants and Institutions.
>
> DaveA
>
Upon rereading, I think I have to disagree with myself.  Not being that 
acquainted with Monte Carlo simulations, I forgot that you would be 
creating many simulations with one set of function objects, then moving 
on to a different set of function objects.  So you do need some form of 
collection class.  At this point, I'm lost without something more 
concrete, so I'll try to bow out in favor of Kent and his ideas.

DaveA


From cwitts at compuscan.co.za  Mon Nov  2 07:55:22 2009
From: cwitts at compuscan.co.za (Christian Witts)
Date: Mon, 02 Nov 2009 08:55:22 +0200
Subject: [Tutor] python zlib problem
In-Reply-To: <da81a0a80910302151k708c7b34wcde5147c7d623dd4@mail.gmail.com>
References: <da81a0a80910302151k708c7b34wcde5147c7d623dd4@mail.gmail.com>
Message-ID: <4AEE825A.2040707@compuscan.co.za>

Amit Sethi wrote:
> Hi , For some weird reason or a huge screw up I did my python zlib 
> library has been removed . This is part of standard lib(i think!!!) 
> and it is used by setuptools and many other libraries .What is the 
> safe way in which I can recover this library on ubuntu. My previous 
> try somehow lead to removal of ubuntu-desktop...
>
>
> If this is not the list I should be asking this I am sorry
>
> -- 
> A-M-I-T S|S
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
You can take a look at this link and see if it can help you
http://www.1stbyte.com/2005/06/26/configure-and-compile-python-with-zlib/

-- 
Kind Regards,
Christian Witts



From paras80 at gmail.com  Mon Nov  2 10:14:36 2009
From: paras80 at gmail.com (Paras K.)
Date: Mon, 2 Nov 2009 04:14:36 -0500
Subject: [Tutor] Adding Value to CSV
In-Reply-To: <4AED7C18.4030502@ieee.org>
References: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>
	<4AED7C18.4030502@ieee.org>
Message-ID: <d32532fc0911020114p1b88b326p1e937fab9e90ecfd@mail.gmail.com>

What I am trying to do is as I am writing the row to the CSV file, I want to
add the string base on a few other checks that I still need to write.

Ex.

readline = '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23
15:40:33"\r\n'

At the end of this based on my checks I want to be able to write a string
like

Part of DHCP Pool or Part of VPN Pool

So the final line should be something like this written to the CSV file:

'"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 15:40:33",
"Part of DHCP Pool"

Thanx in advance for the help.

On Sun, Nov 1, 2009 at 7:16 AM, Dave Angel <davea at ieee.org> wrote:

> Paras K. wrote:
>
>> I have some code that is going through a number of test.
>>
>> When I have the line that has been read I need to add another value at the
>> end of it, or write the information into another csv file
>>
>> Example of my code:
>>
>> for line in fh.readlines():
>>            readline = line
>>            ipline = readline
>>            ip = ipline.split(' ')[0]
>>            split_ip = ip.split('.')
>>            if ((split_ip[0] == '"152')):
>>                ff.write(readline)
>>                totalcount +=1
>>
>>
>> I need to add another piece, how can I add another field at the end of
>> ff.write(readline)
>>
>>
>> Any assistance would be greatly appreciated. Thank You.
>>
>>
>>
> If your program is correct so far, then you could add it simply with:
>            ff.write(readline + " " + newdata)
>
> Although your message subject says CSV, it looks like you're breaking the
> line up by spaces.  So if in fact each field is constrained not to have a
> space within, and there is a single space between fields, then the above
> will work.
>
> If, on the other hand, you have to deal with fields that can contain the
> delimiter, perhaps escaped or quoted, then things get much more complicated.
>
> DaveA
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091102/d2e3fd36/attachment.htm>

From denis.spir at free.fr  Mon Nov  2 10:35:29 2009
From: denis.spir at free.fr (spir)
Date: Mon, 2 Nov 2009 10:35:29 +0100
Subject: [Tutor] Structure of my simulation / monte-carlo
In-Reply-To: <4AEE3740.5090202@ieee.org>
References: <77e831100910310954h49e2f1b7h61d270e872db8892@mail.gmail.com>
	<4AED78D0.2040105@ieee.org>
	<77e831100911010647t57cc7b69u865d50141815394e@mail.gmail.com>
	<1c2a2c590911010758h5791ea12oad451cb57dc2ccbb@mail.gmail.com>
	<4AEE3740.5090202@ieee.org>
Message-ID: <20091102103529.452c7aa8@o>

Le Sun, 01 Nov 2009 20:34:56 -0500,
Dave Angel <davea at ieee.org> s'exprima ainsi:

> And from what you said earlier, you WILL need function objects, probably 
> as parameters to the Simulation constructor.  So each instance of 
> Simulation will be given several function objects to specify the 
> distribution functions for the parameters to be used when creating 
> Applicants and Institutions.

A note on "function objects" (because you --Vincent-- seem to regard this notion as impressive, but maybe I'm wrong).
In python, function objects are functions, no more; all functions (and methods) are objects. Simply, the fact that they are objects, indeed, is revealed in the cases where you bind them to a name like any other object.

to an ordinary variable:

if random_choice == GAUSS:
    randomFunc = gaussRandom

or to a func parameter:

def produceDistribution(self, randomFunc, more_param):
    ...
    random_results = randomFunc(more_param)
    self.distrib = Distribution(results)	# if you have type for distrib

This is not so often needed, but your case in the good one.

The feature is present in most high-level languages. But this has not been true for a long time, especially for mainstream languages of the "imperative" field (while it was more common, even required, for functional languages). So, this feature has kept a kind of special prestige and "functions are first-class objects" often comes early in a list of language features. But there is nothing exceptional in this for a language like python that basically treats data as objects, id est that accesses data through references.

Denis
------
la vita e estrany



From cwitts at compuscan.co.za  Mon Nov  2 11:34:20 2009
From: cwitts at compuscan.co.za (Christian Witts)
Date: Mon, 02 Nov 2009 12:34:20 +0200
Subject: [Tutor] Adding Value to CSV
In-Reply-To: <d32532fc0911020114p1b88b326p1e937fab9e90ecfd@mail.gmail.com>
References: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>	<4AED7C18.4030502@ieee.org>
	<d32532fc0911020114p1b88b326p1e937fab9e90ecfd@mail.gmail.com>
Message-ID: <4AEEB5AC.30206@compuscan.co.za>

Paras K. wrote:
> What I am trying to do is as I am writing the row to the CSV file, I 
> want to add the string base on a few other checks that I still need to 
> write.
>
> Ex.
>
> readline = '"152.88.91.98","BitTorrent Client 
> Activity","1","2009-09-23 15:40:33"\r\n'
>
> At the end of this based on my checks I want to be able to write a 
> string like
>
> Part of DHCP Pool or Part of VPN Pool
>
> So the final line should be something like this written to the CSV file:
>
> '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 
> 15:40:33", "Part of DHCP Pool"
>
> Thanx in advance for the help.
>
> On Sun, Nov 1, 2009 at 7:16 AM, Dave Angel <davea at ieee.org 
> <mailto:davea at ieee.org>> wrote:
>
>     Paras K. wrote:
>
>         I have some code that is going through a number of test.
>
>         When I have the line that has been read I need to add another
>         value at the
>         end of it, or write the information into another csv file
>
>         Example of my code:
>
>         for line in fh.readlines():
>                    readline = line
>                    ipline = readline
>                    ip = ipline.split(' ')[0]
>                    split_ip = ip.split('.')
>                    if ((split_ip[0] == '"152')):
>                        ff.write(readline)
>                        totalcount +=1
>
>
>         I need to add another piece, how can I add another field at
>         the end of
>         ff.write(readline)
>
>
>         Any assistance would be greatly appreciated. Thank You.
>
>          
>
>     If your program is correct so far, then you could add it simply with:
>                ff.write(readline + " " + newdata)
>
>     Although your message subject says CSV, it looks like you're
>     breaking the line up by spaces.  So if in fact each field is
>     constrained not to have a space within, and there is a single
>     space between fields, then the above will work.
>
>     If, on the other hand, you have to deal with fields that can
>     contain the delimiter, perhaps escaped or quoted, then things get
>     much more complicated.
>
>     DaveA
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
Use .strip() to remove the end-of-line characters and then add the 
string you want to the end of the line including end-of-line character.

-- 
Kind Regards,
Christian Witts



From davea at ieee.org  Mon Nov  2 12:00:58 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 02 Nov 2009 06:00:58 -0500
Subject: [Tutor] Adding Value to CSV
In-Reply-To: <d32532fc0911020114p1b88b326p1e937fab9e90ecfd@mail.gmail.com>
References: <d32532fc0910311123j1bf95b59g87b77af107f97fda@mail.gmail.com>	
	<4AED7C18.4030502@ieee.org>
	<d32532fc0911020114p1b88b326p1e937fab9e90ecfd@mail.gmail.com>
Message-ID: <4AEEBBEA.8020009@ieee.org>

(Please don't top-post on a newsgroup that has the convention of adding 
new content after quoted text.)

Paras K. wrote:
> What I am trying to do is as I am writing the row to the CSV file, I want to
> add the string base on a few other checks that I still need to write.
>
> Ex.
>
> readline = '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23
> 15:40:33"\r\n'
>
> At the end of this based on my checks I want to be able to write a string
> like
>
> Part of DHCP Pool or Part of VPN Pool
>
> So the final line should be something like this written to the CSV file:
>
> '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 15:40:33",
> "Part of DHCP Pool"
>
> Thanx in advance for the help.
>
> On Sun, Nov 1, 2009 at 7:16 AM, Dave Angel <davea at ieee.org> wrote:
>
>   
>> Paras K. wrote:
>>
>>     
>>> I have some code that is going through a number of test.
>>>
>>> When I have the line that has been read I need to add another value at the
>>> end of it, or write the information into another csv file
>>>
>>> Example of my code:
>>>
>>> for line in fh.readlines():
>>>            readline = line
>>>            ipline = readline
>>>            ip = ipline.split(' ')[0]
>>>            split_ip = ip.split('.')
>>>            if ((split_ip[0] == '"152')):
>>>                ff.write(readline)
>>>                totalcount +=1
>>>
>>>
>>> I need to add another piece, how can I add another field at the end of
>>> ff.write(readline)
>>>
>>>
>>> Any assistance would be greatly appreciated. Thank You.
>>>
>>>
>>>
>>>       
>> If your program is correct so far, then you could add it simply with:
>>            ff.write(readline + " " + newdata)
>>
>> Although your message subject says CSV, it looks like you're breaking the
>> line up by spaces.  So if in fact each field is constrained not to have a
>> space within, and there is a single space between fields, then the above
>> will work.
>>
>> If, on the other hand, you have to deal with fields that can contain the
>> delimiter, perhaps escaped or quoted, then things get much more complicated.
>>
>> DaveA
>>
>>     
>
>   

Now that your input format is entirely different than what your code can 
parse, my answer would have to be different as well.  If you were still 
doing it by hand, then you'd need to use strip() to remove the trailing 
newline, then concatenate with a comma, some quotes, and another newline.

But you probably ought to be using the csv module, since you're likely 
to come up with some fields having embedded commas in them, and split() 
cannot handle that.

Once you've got a csv.reader() and a csv.writer(), all you're looking 
for is  mydata.append(newdata)  to add newdata field to the end of a 
record.  The reader reads into a list, and the write writes from a list.

DaveA


From sanelson at gmail.com  Mon Nov  2 12:38:52 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 2 Nov 2009 11:38:52 +0000
Subject: [Tutor] CSS Minification
Message-ID: <b6131fdc0911020338h48f6eb55g139c5b51d87b20c4@mail.gmail.com>

Is there a Python CSS and/or javascript minifier available?

I've got to convert some ant scripts to python, and ant has a minifier
plugin that I need to replicate.

Maybe Beautiful Soup can do this?

S.

-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From samusjack at yahoo.com  Mon Nov  2 06:37:49 2009
From: samusjack at yahoo.com (Sam Stout)
Date: Sun, 1 Nov 2009 21:37:49 -0800 (PST)
Subject: [Tutor] Can't find modules at command line
Message-ID: <513099.91923.qm@web43143.mail.sp1.yahoo.com>

I'm using Python on Mac OS X.  I usually run IDLE and get this result:

IDLE 1.2      
>>> import gun
Bang!
>>> 

However, when I try this from Terminal:

Jacks-Mac:~ Sam$ python
ActivePython 2.5.0.0 (ActiveState Software Inc.) based on
Python 2.5 (r25:51908, Mar  9 2007, 17:40:37) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gun
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named gun
>>> 

This file is at [/Users/sam/Documents/gun.py].  What should I do to make it visible to Python?


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091101/70c1d763/attachment.htm>

From cwitts at compuscan.co.za  Mon Nov  2 12:55:32 2009
From: cwitts at compuscan.co.za (Christian Witts)
Date: Mon, 02 Nov 2009 13:55:32 +0200
Subject: [Tutor] Can't find modules at command line
In-Reply-To: <513099.91923.qm@web43143.mail.sp1.yahoo.com>
References: <513099.91923.qm@web43143.mail.sp1.yahoo.com>
Message-ID: <4AEEC8B4.1040109@compuscan.co.za>

Sam Stout wrote:
> I'm using Python on Mac OS X.  I usually run IDLE and get this result:
>
> IDLE 1.2      
> >>> import gun
> Bang!
> >>> 
>
> However, when I try this from Terminal:
>
> Jacks-Mac:~ Sam$ python
> ActivePython 2.5.0.0 (ActiveState Software Inc.) based on
> Python 2.5 (r25:51908, Mar  9 2007, 17:40:37) 
> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import gun
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named gun
> >>> 
>
> This file is at [/Users/sam/Documents/gun.py].  What should I do to 
> make it visible to Python?
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
In the console when you start it up you appear to be in your home 
directory `~` but your script is in the Documents folder so you will 
need to add Documents to your path for eg.
from sys import path
path.append('/path/to/script/to/be/imported')
import gun

-- 
Kind Regards,
Christian Witts



From kent37 at tds.net  Mon Nov  2 13:03:59 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 2 Nov 2009 07:03:59 -0500
Subject: [Tutor] CSS Minification
In-Reply-To: <b6131fdc0911020338h48f6eb55g139c5b51d87b20c4@mail.gmail.com>
References: <b6131fdc0911020338h48f6eb55g139c5b51d87b20c4@mail.gmail.com>
Message-ID: <1c2a2c590911020403j76cee07oe54dbf83ad7ceebf@mail.gmail.com>

On Mon, Nov 2, 2009 at 6:38 AM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
> Is there a Python CSS and/or javascript minifier available?

cssutils can minify CSS:
http://cthedot.de/cssutils/

jsmin has been ported to Python:
http://www.crockford.com/javascript/jsmin.py.txt

Kent

From the_only_katala at verizon.net  Mon Nov  2 00:37:50 2009
From: the_only_katala at verizon.net (Katt)
Date: Sun, 01 Nov 2009 15:37:50 -0800
Subject: [Tutor] Retrieving information from a plain text file
 (WinXP/py2.6.2/Beginner)
References: <mailman.15.1257073202.23443.tutor@python.org>
Message-ID: <307953F1474F43AB84A8CB004C6F0237@COMPUTER01>

Hello all,

Thank you all for your help.  I appreciate it alot.

I have been trying to work with file IO alot recently and would like to 
improve my little program so that I no longer use a hard coded list, but a 
text file that I can edit easily.

The text file is three lines long and looks exactly like this:

Reminder1,2009_10_28
Reminder2,2009_11_01
Reminder3,2009_11_15

My program consists of the following code:
============
#]------------------[import modules]------------------[
from time import strftime, mktime, localtime
from WConio import textcolor
#]--------------------------------------------------------[
#]------------------[define functions]------------------[
def read_reminders():
    print "\nReading text file into program: reminders.txt"
    text_file = open("reminders.txt","r")
    reminders = [line.strip().split("'") for line in text_file]
    text_file.close()
    print reminders
#
def get_computer_date():
    #Get today's date from the computer
    todays_date = strftime("%Y_%m_%d")
    return todays_date
#
def color_print(strings):
    #Change the text color in the WinXP dos shell
    #The way to use:
    #color_print([("string",color number),\
    #(str(variable),color number),(etc)])
    for string in strings:
        textcolor(string[1])
        print string[0],
#
def change_to_julian(reminder_date):
    #Receives the year, month, and day
    #in the form of a single string (2009_10_15)
    #and changes it into three different int
    #variables.  Then take those three variables
    #and append six zeros and change into a
    #julian date.
    date = []
    date = reminder_date.split("_")
    year = int(date[0])
    month = int(date[1])
    day = int(date[2])
    timetuple = (year, month, day) + ( (0,) * 6 )
    unixtime = mktime(timetuple)
    timetuple = localtime(unixtime)
    print days_left(timetuple[7])
    # [7] is the number of julian-date field of
    #the unixtime tuple.
    return days_left(timetuple[7])
#
def days_left(julian_date):
    #This function calculates the days left
    #until a reminder.  If the days left are
    #greater than 0 it will print normally.
    #If it is -1 then it will print differently.
    #Also if it is greater than -1 it will print
    #yet again differently.
    days_until_reminder = julian_date - localtime().tm_yday
    if days_until_reminder > 0:
        color_print ([("There are",7),(str(days_until_reminder),4),("days 
left until this reminder.",7),("\n",7)])
    elif days_until_reminder == -1:
        color_print ([("\tYou have missed this reminder 
by",4),(str(days_until_reminder*-1),4),("day!",4),("\n",7)])
        color_print 
[("  ------------------------------------------------------------------------",4),("\n",7)])
    else:
        color_print ([("\tYou have missed this reminder 
by",4),(str(days_until_reminder*-1),4),("days!",4),("\n",7)])
        color_print 
[("  ------------------------------------------------------------------------",4),("\n",7)])
print
#
def compare_reminders(todays_date):
    #This function compares the reminders
    #to the computer date.
    #It has three different paths:
    # 1.Matches today's date
    # 2.The reminder date has already
    #  passed by
    # 3.The reminder date is yet to
    #  come.
    #After determining which it is it will
    #access the change_to_julian and
    #days_left functions.
    #reminders.sort()
    color_print ([(" 
[-------------------------------------------------------------------------]",4),("\n",7)])
    index = 0
    while index < len(reminders):
        if todays_date == reminders[index][1]:
            print
            color_print 
[("  ------------------------------------------------------------------------",4),("\n",7)])
            print "Today's reminder is: 
",reminders[index][0],"on",reminders[index][1]
            color_print ([("\t\tTake care of this reminder 
immediately",2),("\n",7)])
        elif todays_date > reminders[index][1]:
            print
            print "Whoops, you missed the following 
reminder.",reminders[index][0],"on",reminders[index][1]
            change_to_julian(reminders[index][1])
        else:
            print
            print "Your upcoming reminders are: 
",reminders[index][0],"on",reminders[index][1]
            change_to_julian(reminders[index][1])
        index = index + 1
    color_print ([(" 
[-------------------------------------------------------------------------]",4),("\n",7)])
#]--------------------------------------------------------[
#]-------------------[Main Program]-------------------[
read_reminders()
print reminders
compare_reminders(get_computer_date())
pause_it = raw_input("Press a key to end: ")
#]--------------------------------------------------------[
============
Could someone explain to me why my read_reminders function retrieves the 
information, but cannot process that information?

When I try and run the program I get the following error message:
============
Reading text file into program: reminders.txt
[['Reminder1,2010_10_15'], ['Reminder2,2010_11_01'], 
['Reminder3,2010_11_15']]
Traceback (most recent call last):
    File "reminders.py", line 182, in <module>
        print reminders
NameError: name 'reminders' is not defined
============

Thanks in advance for your help,

Katt 


From vinces1979 at gmail.com  Mon Nov  2 20:30:34 2009
From: vinces1979 at gmail.com (vince spicer)
Date: Mon, 2 Nov 2009 13:30:34 -0600
Subject: [Tutor] Retrieving information from a plain text file
	(WinXP/py2.6.2/Beginner)
In-Reply-To: <307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
References: <mailman.15.1257073202.23443.tutor@python.org>
	<307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
Message-ID: <1e53c510911021130g55c5b561re0fe3ff85789a8f4@mail.gmail.com>

On Sun, Nov 1, 2009 at 5:37 PM, Katt <the_only_katala at verizon.net> wrote:

> Hello all,
>
> Thank you all for your help.  I appreciate it alot.
>
> I have been trying to work with file IO alot recently and would like to
> improve my little program so that I no longer use a hard coded list, but a
> text file that I can edit easily.
>
> The text file is three lines long and looks exactly like this:
>
> Reminder1,2009_10_28
> Reminder2,2009_11_01
> Reminder3,2009_11_15
>
> My program consists of the following code:
> ============
> #]------------------[import modules]------------------[
> from time import strftime, mktime, localtime
> from WConio import textcolor
> #]--------------------------------------------------------[
> #]------------------[define functions]------------------[
> def read_reminders():
>   print "\nReading text file into program: reminders.txt"
>   text_file = open("reminders.txt","r")
>   reminders = [line.strip().split("'") for line in text_file]
>   text_file.close()
>   print reminders
> #
> def get_computer_date():
>   #Get today's date from the computer
>   todays_date = strftime("%Y_%m_%d")
>   return todays_date
> #
> def color_print(strings):
>   #Change the text color in the WinXP dos shell
>   #The way to use:
>   #color_print([("string",color number),\
>   #(str(variable),color number),(etc)])
>   for string in strings:
>       textcolor(string[1])
>       print string[0],
> #
> def change_to_julian(reminder_date):
>   #Receives the year, month, and day
>   #in the form of a single string (2009_10_15)
>   #and changes it into three different int
>   #variables.  Then take those three variables
>   #and append six zeros and change into a
>   #julian date.
>   date = []
>   date = reminder_date.split("_")
>   year = int(date[0])
>   month = int(date[1])
>   day = int(date[2])
>   timetuple = (year, month, day) + ( (0,) * 6 )
>   unixtime = mktime(timetuple)
>   timetuple = localtime(unixtime)
>   print days_left(timetuple[7])
>   # [7] is the number of julian-date field of
>   #the unixtime tuple.
>   return days_left(timetuple[7])
> #
> def days_left(julian_date):
>   #This function calculates the days left
>   #until a reminder.  If the days left are
>   #greater than 0 it will print normally.
>   #If it is -1 then it will print differently.
>   #Also if it is greater than -1 it will print
>   #yet again differently.
>   days_until_reminder = julian_date - localtime().tm_yday
>   if days_until_reminder > 0:
>       color_print ([("There are",7),(str(days_until_reminder),4),("days
> left until this reminder.",7),("\n",7)])
>   elif days_until_reminder == -1:
>       color_print ([("\tYou have missed this reminder
> by",4),(str(days_until_reminder*-1),4),("day!",4),("\n",7)])
>       color_print [("
>  ------------------------------------------------------------------------",4),("\n",7)])
>   else:
>       color_print ([("\tYou have missed this reminder
> by",4),(str(days_until_reminder*-1),4),("days!",4),("\n",7)])
>       color_print [("
>  ------------------------------------------------------------------------",4),("\n",7)])
> print
> #
> def compare_reminders(todays_date):
>   #This function compares the reminders
>   #to the computer date.
>   #It has three different paths:
>   # 1.Matches today's date
>   # 2.The reminder date has already
>   #  passed by
>   # 3.The reminder date is yet to
>   #  come.
>   #After determining which it is it will
>   #access the change_to_julian and
>   #days_left functions.
>   #reminders.sort()
>   color_print ([("
> [-------------------------------------------------------------------------]",4),("\n",7)])
>   index = 0
>   while index < len(reminders):
>       if todays_date == reminders[index][1]:
>           print
>           color_print [("
>  ------------------------------------------------------------------------",4),("\n",7)])
>           print "Today's reminder is:
> ",reminders[index][0],"on",reminders[index][1]
>           color_print ([("\t\tTake care of this reminder
> immediately",2),("\n",7)])
>       elif todays_date > reminders[index][1]:
>           print
>           print "Whoops, you missed the following
> reminder.",reminders[index][0],"on",reminders[index][1]
>           change_to_julian(reminders[index][1])
>       else:
>           print
>           print "Your upcoming reminders are:
> ",reminders[index][0],"on",reminders[index][1]
>           change_to_julian(reminders[index][1])
>       index = index + 1
>   color_print ([("
> [-------------------------------------------------------------------------]",4),("\n",7)])
> #]--------------------------------------------------------[
> #]-------------------[Main Program]-------------------[
> read_reminders()
> print reminders
> compare_reminders(get_computer_date())
> pause_it = raw_input("Press a key to end: ")
> #]--------------------------------------------------------[
> ============
> Could someone explain to me why my read_reminders function retrieves the
> information, but cannot process that information?
>
> When I try and run the program I get the following error message:
> ============
> Reading text file into program: reminders.txt
> [['Reminder1,2010_10_15'], ['Reminder2,2010_11_01'],
> ['Reminder3,2010_11_15']]
> Traceback (most recent call last):
>   File "reminders.py", line 182, in <module>
>       print reminders
> NameError: name 'reminders' is not defined
> ============
>
> Thanks in advance for your help,
>
> Katt
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


reminders is a local variable which is only available within you
read_reminders function

you can return the results instead to be used elsewhere

EX:

def read_reminders():
  print "\nReading text file into program: reminders.txt"
  text_file = open("reminders.txt","r")
  reminders = [line.strip().split("'") for line in text_file]
  text_file.close()
  return reminders

#]-------------------[Main Program]-------------------[
reminders = read_reminders()
print reminders
compare_reminders(get_computer_date())
pause_it = raw_input("Press a key to end: ")
#]--------------------------------------------------------[



Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091102/f54b06b8/attachment.htm>

From zstumgoren at gmail.com  Mon Nov  2 20:43:43 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Mon, 2 Nov 2009 14:43:43 -0500
Subject: [Tutor] Retrieving information from a plain text file
	(WinXP/py2.6.2/Beginner)
In-Reply-To: <307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
References: <mailman.15.1257073202.23443.tutor@python.org>
	<307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
Message-ID: <cadf44510911021143v30e970e3x5ca249462aec31ea@mail.gmail.com>

Hi Katt,

It appears you did not return the list of reminders that you extracted
in the "read_reminders" function, but simply printed them from inside
that function.

If you modify your code as below to store the list in a variable
called "reminders", you  should be able to access the list in your
global namespace.

> def read_reminders():
> ? print "\nReading text file into program: reminders.txt"
> ? text_file = open("reminders.txt","r")
> ? reminders = [line.strip().split("'") for line in text_file]
> ? text_file.close()
> ? print reminders
     return reminders

Also, on a side note, you can greatly improve the readability of your
code by using the triple-quote style for multi-line docstrings inside
functions (rather than the hash comment marks). I tend to use hash
marks for one-line/inline comments, since they can really become an
eyesore (at least IMHO) when used too liberally.

Also, Python's whitespace and code formatting conventions can handle a
lot of the "documentation" for you. For instance,  module imports are
typically always performed at the top of a script, so it's reasonable
to expect that others reading your code will understand you're
importing some modules.

Much of this spelled out in PEP's 8 (style guide) and 257 (doc strings):

http://www.python.org/dev/peps/pep-0008/
http://www.python.org/dev/peps/pep-0257/

HTH!

Serdar

From davea at ieee.org  Mon Nov  2 21:10:28 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 02 Nov 2009 15:10:28 -0500
Subject: [Tutor] Retrieving information from a plain text
	file	(WinXP/py2.6.2/Beginner)
In-Reply-To: <307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
References: <mailman.15.1257073202.23443.tutor@python.org>
	<307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
Message-ID: <4AEF3CB4.2050302@ieee.org>

(Looks like maybe you hijacked another thread, instead of just creating 
a new message, with new topic, for the list)

Katt wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Hello all,
>
> Thank you all for your help.  I appreciate it alot.
>
> I have been trying to work with file IO alot recently and would like 
> to improve my little program so that I no longer use a hard coded 
> list, but a text file that I can edit easily.
>
> The text file is three lines long and looks exactly like this:
>
> Reminder1,2009_10_28
> Reminder2,2009_11_01
> Reminder3,2009_11_15
>
> My program consists of the following code:
> ============
> #]------------------[import modules]------------------[
> from time import strftime, mktime, localtime
> from WConio import textcolor
> #]--------------------------------------------------------[
> #]------------------[define functions]------------------[
> def read_reminders():
>    print "\nReading text file into program: reminders.txt"
>    text_file = open("reminders.txt","r")
>    reminders = [line.strip().split("'") for line in text_file]
>    text_file.close()
>    print reminders
> #
> def get_computer_date():
>    #Get today's date from the computer
>    todays_date = strftime("%Y_%m_%d")
>    return todays_date
> #
> def color_print(strings):
>    #Change the text color in the WinXP dos shell
>    #The way to use:
>    #color_print([("string",color number),\
>    #(str(variable),color number),(etc)])
>    for string in strings:
>        textcolor(string[1])
>        print string[0],
> #
> def change_to_julian(reminder_date):
>    #Receives the year, month, and day
>    #in the form of a single string (2009_10_15)
>    #and changes it into three different int
>    #variables.  Then take those three variables
>    #and append six zeros and change into a
>    #julian date.
>    date = []
>    date = reminder_date.split("_")
>    year = int(date[0])
>    month = int(date[1])
>    day = int(date[2])
>    timetuple = (year, month, day) + ( (0,) * 6 )
>    unixtime = mktime(timetuple)
>    timetuple = localtime(unixtime)
>    print days_left(timetuple[7])
>    # [7] is the number of julian-date field of
>    #the unixtime tuple.
>    return days_left(timetuple[7])
> #
> def days_left(julian_date):
>    #This function calculates the days left
>    #until a reminder.  If the days left are
>    #greater than 0 it will print normally.
>    #If it is -1 then it will print differently.
>    #Also if it is greater than -1 it will print
>    #yet again differently.
>    days_until_reminder = julian_date - localtime().tm_yday
>    if days_until_reminder > 0:
>        color_print ([("There 
> are",7),(str(days_until_reminder),4),("days left until this 
> reminder.",7),("\n",7)])
>    elif days_until_reminder == -1:
>        color_print ([("\tYou have missed this reminder 
> by",4),(str(days_until_reminder*-1),4),("day!",4),("\n",7)])
>        color_print [("  
> ------------------------------------------------------------------------",4),("\n",7)]) 
>
>    else:
>        color_print ([("\tYou have missed this reminder 
> by",4),(str(days_until_reminder*-1),4),("days!",4),("\n",7)])
>        color_print [("  
> ------------------------------------------------------------------------",4),("\n",7)]) 
>
> print
> #
> def compare_reminders(todays_date):
>    #This function compares the reminders
>    #to the computer date.
>    #It has three different paths:
>    # 1.Matches today's date
>    # 2.The reminder date has already
>    #  passed by
>    # 3.The reminder date is yet to
>    #  come.
>    #After determining which it is it will
>    #access the change_to_julian and
>    #days_left functions.
>    #reminders.sort()
>    color_print ([(" 
> [-------------------------------------------------------------------------]",4),("\n",7)]) 
>
>    index = 0
>    while index < len(reminders):
>        if todays_date == reminders[index][1]:
>            print
>            color_print [("  
> ------------------------------------------------------------------------",4),("\n",7)]) 
>
>            print "Today's reminder is: 
> ",reminders[index][0],"on",reminders[index][1]
>            color_print ([("\t\tTake care of this reminder 
> immediately",2),("\n",7)])
>        elif todays_date > reminders[index][1]:
>            print
>            print "Whoops, you missed the following 
> reminder.",reminders[index][0],"on",reminders[index][1]
>            change_to_julian(reminders[index][1])
>        else:
>            print
>            print "Your upcoming reminders are: 
> ",reminders[index][0],"on",reminders[index][1]
>            change_to_julian(reminders[index][1])
>        index = index + 1
>    color_print ([(" 
> [-------------------------------------------------------------------------]",4),("\n",7)]) 
>
> #]--------------------------------------------------------[
> #]-------------------[Main Program]-------------------[
> read_reminders()
> print reminders
> compare_reminders(get_computer_date())
> pause_it = raw_input("Press a key to end: ")
> #]--------------------------------------------------------[
> ============
> Could someone explain to me why my read_reminders function retrieves 
> the information, but cannot process that information?
>
> When I try and run the program I get the following error message:
> ============
> Reading text file into program: reminders.txt
> [['Reminder1,2010_10_15'], ['Reminder2,2010_11_01'], 
> ['Reminder3,2010_11_15']]
> Traceback (most recent call last):
>    File "reminders.py", line 182, in <module>
>        print reminders
> NameError: name 'reminders' is not defined
> ============
>
> Thanks in advance for your help,
>
> Katt
>
>
The function read_reminders() doesn't return anything, so once it's 
done, those reminder items are gone.  It printed them, then forgot them.

Similarly, compare_reminders() tries to work on reminders, when it was 
not passed the data either.

You need to add a return statement to read_reminders(), and when you 
call it, you need to save it somewhere.  Then you can pass it to the 
compare_reminders() function so it has something to compare with.

Once you get that sorted out, another bug that's already apparent is 
that you're trying to split the line on quotes, when it uses commas 
between fields on each line.

DaveA


From oberoc at gmail.com  Mon Nov  2 21:52:43 2009
From: oberoc at gmail.com (Tino Dai)
Date: Mon, 2 Nov 2009 15:52:43 -0500
Subject: [Tutor] Can we unroll a loop?
Message-ID: <2ac5d4850911021252v41754e53l892e017ac04ac006@mail.gmail.com>

Hi Everybody,

     I am wondering about a better approach to doing this:

     for obj in groups:
       rVs = rVs + Event.objects.get(group=obj)
    rVs.sort()

     Instead what I'm looking for is to have a construct that would expand
out to this:

    rVs =  Event.objects.get(group=groups[0]) |
Event.objects.get(group=groups[1]) \
            | ... | Event.objects.get(group=groups[n-1]) |
Event.objects.get(group=groups[n])

     I don't know if this is even possible. I have looked at the built in
map function as well as
some of the itertools functions, but nothing seems to fit the bill. Is this
even possible and
if so, how?

Thanks in advance!
-Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091102/d4a7d909/attachment.htm>

From kent37 at tds.net  Mon Nov  2 22:32:27 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 2 Nov 2009 16:32:27 -0500
Subject: [Tutor] Can we unroll a loop?
In-Reply-To: <2ac5d4850911021252v41754e53l892e017ac04ac006@mail.gmail.com>
References: <2ac5d4850911021252v41754e53l892e017ac04ac006@mail.gmail.com>
Message-ID: <1c2a2c590911021332q7dab0ce8pc59440a233eb6c25@mail.gmail.com>

On Mon, Nov 2, 2009 at 3:52 PM, Tino Dai <oberoc at gmail.com> wrote:
> Hi Everybody,
>
> ???? I am wondering about a better approach to doing this:
>
> ???? for obj in groups:
> ??? ?? rVs = rVs + Event.objects.get(group=obj)
> ??? rVs.sort()

Assuming rVs is a list and Event.objects.get(group=obj) is also a
list, you can use
rVs = sum((Event.objects.get(group=obj) for obj in groups), [])

Kent

From eduardo.susan at gmail.com  Tue Nov  3 01:28:30 2009
From: eduardo.susan at gmail.com (Eduardo Vieira)
Date: Mon, 2 Nov 2009 17:28:30 -0700
Subject: [Tutor] Question about time.gmtime
In-Reply-To: <9356b9f30909290811had7e186q338b0aa745b948b@mail.gmail.com>
References: <9356b9f30909290811had7e186q338b0aa745b948b@mail.gmail.com>
Message-ID: <9356b9f30911021628l5518e3c2k87b56b71b0095b09@mail.gmail.com>

On Tue, Sep 29, 2009 at 8:11 AM, Eduardo Vieira <eduardo.susan at gmail.com> wrote:
> Hello, I had a problem with a script yesterday that made me puzzled.
> My time zone is US Mountain Time. This script was running nice last
> week, but yesterday it reported the date of today instead
> So, yesterday at 5:20pm this line:
> hoje = time.strftime("%a, %b %d, %Y", time.gmtime())
>
> Gave me this: "Tue, Sep 29, 2009" instead of "Mon, Sep 28, 2009"
> What's going on?
> Minutes ago, while I was investigating this I had this output:
>
>>>> time.gmtime()
> time.struct_time(tm_year=2009, tm_mon=9, tm_mday=29, tm_hour=14,
> tm_min=48, tm_sec=49, tm_wday=1, tm_yday=272, tm_isdst=0)
>>>> time.localtime()
> time.struct_time(tm_year=2009, tm_mon=9, tm_mday=29, tm_hour=8,
> tm_min=50, tm_sec=28, tm_wday=1, tm_yday=272, tm_isdst=1)
>
> I see there are 6 hours difference, but I'm sure the script ran before 6pm
> Should I simply modify my code to use localtime, instead? Why did it
> happen only yesterday?
>
> I'm using Windows XP Professional, and my clock says it's Tuesday,
> Sept. 29, 2009 and 9:10 AM
>
> Regards,
>
> Eduardo
>

Some time ago I experienced that. And today again. I wonder if it's
not related to the fact that I did some system restore operations
today?

From rdmoores at gmail.com  Tue Nov  3 09:09:27 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 00:09:27 -0800
Subject: [Tutor] trouble using 2to3.py
Message-ID: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>

I'm in the process to learning Python 3.1, and need to convert a bunch
of handy functions I wrote and stole over several years. They are all
written in 2.x, and are together in one file, mycalc.py.

I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .

Then I tried
E:\Python31\temp>2to3.py -w mycalc.py

and got

Traceback (most recent call last):
  File "E:\Python31\temp\2to3.py", line 2, in <module>
    from lib2to3.main import main
  File "E:\Python31\temp\lib2to3\main.py", line 33
    except os.error as err:
                     ^
SyntaxError: invalid syntax

How have others dealt with this?

Dick Moores

From rabidpoobear at gmail.com  Tue Nov  3 10:50:57 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Tue, 3 Nov 2009 03:50:57 -0600
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
Message-ID: <dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>

Are you sure you're using the 3.1 version of Python to run the script?
Welcome back, btw.  haven't heard from you in a while.

On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores <rdmoores at gmail.com>wrote:

> I'm in the process to learning Python 3.1, and need to convert a bunch
> of handy functions I wrote and stole over several years. They are all
> written in 2.x, and are together in one file, mycalc.py.
>
> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\  .
>
> Then I tried
> E:\Python31\temp>2to3.py -w mycalc.py
>
> and got
>
> Traceback (most recent call last):
>  File "E:\Python31\temp\2to3.py", line 2, in <module>
>    from lib2to3.main import main
>  File "E:\Python31\temp\lib2to3\main.py", line 33
>    except os.error as err:
>                     ^
> SyntaxError: invalid syntax
>
> How have others dealt with this?
>
> Dick Moores
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/4fa7833a/attachment.htm>

From amit.pureenergy at gmail.com  Tue Nov  3 11:03:00 2009
From: amit.pureenergy at gmail.com (Amit Sethi)
Date: Tue, 3 Nov 2009 15:33:00 +0530
Subject: [Tutor] java for python programmers
Message-ID: <da81a0a80911030203w8013659u1d0baf7115596b6c@mail.gmail.com>

Hi their is a book , python for java programmers is their a book on java for
python programmers for some one who wants to move to java from python

-- 
A-M-I-T S|S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/0045d3cc/attachment.htm>

From rdmoores at gmail.com  Tue Nov  3 11:15:11 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 02:15:11 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
Message-ID: <d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>

Well, here's the same try again, followed by starting the shell:

=========================================
E:\Python31\temp>2to3.py -w mycalc.py
Traceback (most recent call last):
  File "E:\Python31\temp\2to3.py", line 2, in <module>
    from lib2to3.main import main
  File "E:\Python31\temp\lib2to3\main.py", line 33
    except os.error as err:
                     ^
SyntaxError: invalid syntax

E:\Python31\temp>python
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
==========================================

Otherwise, how can I tell?

BTW I'm doing this on my old desktop, which runs XP SP2 32-bit.

And thanks, Luke, I took some time off to get back to my piano playing
with a new teacher, my 6th IIRC. Now its time to do both.

Dick

On Tue, Nov 3, 2009 at 01:50, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> Are you sure you're using the 3.1 version of Python to run the script?
> Welcome back, btw.? haven't heard from you in a while.
>
> On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores <rdmoores at gmail.com>
> wrote:
>>
>> I'm in the process to learning Python 3.1, and need to convert a bunch
>> of handy functions I wrote and stole over several years. They are all
>> written in 2.x, and are together in one file, mycalc.py.
>>
>> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
>> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\ ?.
>>
>> Then I tried
>> E:\Python31\temp>2to3.py -w mycalc.py
>>
>> and got
>>
>> Traceback (most recent call last):
>> ?File "E:\Python31\temp\2to3.py", line 2, in <module>
>> ? ?from lib2to3.main import main
>> ?File "E:\Python31\temp\lib2to3\main.py", line 33
>> ? ?except os.error as err:
>> ? ? ? ? ? ? ? ? ? ? ^
>> SyntaxError: invalid syntax
>>
>> How have others dealt with this?
>>
>> Dick Moores
>> _______________________________________________
>> Tutor maillist ?- ?Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>

From rdmoores at gmail.com  Tue Nov  3 13:12:37 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 04:12:37 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
Message-ID: <d71c7ed60911030412j4160958el1d60637d5bd1451a@mail.gmail.com>

And here's the same try (without the -w switch, to keep it simple),
but this time on my laptop (64-bit Vista SP1). The result is a bit
different:

=====================================
C:\Python31\temp>2to3.py mycalc.py
Traceback (most recent call last):
  File "C:\Python31\temp\2to3.py", line 2, in <module>
    from lib2to3.main import main
  File "C:\Python31\temp\lib2to3\main.py", line 121
    print("At least one file or directory argument required.", file=sys.stderr)
                                                                   ^
SyntaxError: invalid syntax

C:\Python31\temp>python
Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/4
0.25
>>>
==========================================

Dick

On Tue, Nov 3, 2009 at 02:15, Richard D. Moores <rdmoores at gmail.com> wrote:
> Well, here's the same try again, followed by starting the shell:
>
> =========================================
> E:\Python31\temp>2to3.py -w mycalc.py
> Traceback (most recent call last):
> ?File "E:\Python31\temp\2to3.py", line 2, in <module>
> ? ?from lib2to3.main import main
> ?File "E:\Python31\temp\lib2to3\main.py", line 33
> ? ?except os.error as err:
> ? ? ? ? ? ? ? ? ? ? ^
> SyntaxError: invalid syntax
>
> E:\Python31\temp>python
> Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>>
> ==========================================
>
> Otherwise, how can I tell?
>
> BTW I'm doing this on my old desktop, which runs XP SP2 32-bit.
>
> And thanks, Luke, I took some time off to get back to my piano playing
> with a new teacher, my 6th IIRC. Now its time to do both.
>
> Dick
>
> On Tue, Nov 3, 2009 at 01:50, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
>> Are you sure you're using the 3.1 version of Python to run the script?
>> Welcome back, btw.? haven't heard from you in a while.
>>
>> On Tue, Nov 3, 2009 at 2:09 AM, Richard D. Moores <rdmoores at gmail.com>
>> wrote:
>>>
>>> I'm in the process to learning Python 3.1, and need to convert a bunch
>>> of handy functions I wrote and stole over several years. They are all
>>> written in 2.x, and are together in one file, mycalc.py.
>>>
>>> I created a folder E:\Python31\temp\ (my OS is Vista SP1 64-bit), and
>>> in it I placed copies of mycalc.py, 2to3.py, and a folder, lib2to3\ ?.
>>>
>>> Then I tried
>>> E:\Python31\temp>2to3.py -w mycalc.py
>>>
>>> and got
>>>
>>> Traceback (most recent call last):
>>> ?File "E:\Python31\temp\2to3.py", line 2, in <module>
>>> ? ?from lib2to3.main import main
>>> ?File "E:\Python31\temp\lib2to3\main.py", line 33
>>> ? ?except os.error as err:
>>> ? ? ? ? ? ? ? ? ? ? ^
>>> SyntaxError: invalid syntax
>>>
>>> How have others dealt with this?
>>>
>>> Dick Moores
>>> _______________________________________________
>>> Tutor maillist ?- ?Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>

From davea at ieee.org  Tue Nov  3 14:30:43 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 03 Nov 2009 08:30:43 -0500
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
Message-ID: <4AF03083.3070803@ieee.org>

(Erasing entire history, since you both top-posted, and it's too confusing)

When you run a xxx.py on Windows, it locates the python.exe using the 
registry entries set up by  assoc and ftype.  But when you run "python" 
it looks for python.exe (or python.bat, or ...) on the PATH.  So there's 
not necessarily any conection between your tests.  You're probably 
running two different versions.

Easiest way to know which python is being used is to run it same way:

e:\python31\temp> python 2to3.py -w mycalc.py

Or the way I do it, have python31.bat and python26.bat, which launch the 
exact pythons that I want.

DaveA


From rdmoores at gmail.com  Tue Nov  3 15:53:06 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 06:53:06 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
Message-ID: <d71c7ed60911030653w7c51e657ybf44617b35ee91f2@mail.gmail.com>

After much fiddling around, I got 2to3 to work! And it did a fine, if
not perfect job on mycalc.py (which has about 120 functions and 1993
lines).

What I did that worked, was to use the E:\Python26\Tools\Scripts, and
not Python31. E:\Python26\Tools\Scripts has 2to3.py. I added the
folder lib2to3\ and mycalc.py to E:\Python26\Tools\Scripts, and did a

E:\Python26\Tools\Scripts>2to3 -w mycalc.py

The -w switch first backed up mycalc.py as mycalc.py.bak, and then
2to3.py went to work. The only problems it couldn't handle are shown
below. They are all of the same sort, involving a unicode error. I
simply remmed out the problem causing functions, and now it seems
mycalc.py is good to go with Python 3.1.  I'll look into the "unicode"
errors later -- much later.

>>> from mycalc import *
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 172
    """
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 33-34: malformed \N character escape
>>> from mycalc import *
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 186
    """
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 198-199: malformed \N character escape
>>> from mycalc import *
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    from mycalc import *
  File "E:\Python31\lib\site-packages\mycalc.py", line 207
    file = open('E:\PythonWork\Nutshell\\words.txt', 'r')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 13-14: malformed \N character escape
>>> from mycalc import *
>>>

I also ran 2to3 again and this time captured the output as what
appears to be a diff file:

==================================
E:\Python26\Tools\Scripts>2to3 -w mycalc.py > 2to3_at_work.txt
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Files that were modified:
RefactoringTool: mycalc.py

E:\Python26\Tools\Scripts>
==================================

You can see the file produced at <http://www.rcblue.com/Misc/2to3_at_work.txt>

Dick

From rdmoores at gmail.com  Tue Nov  3 16:18:14 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 07:18:14 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <4AF03083.3070803@ieee.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
Message-ID: <d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>

On Tue, Nov 3, 2009 at 05:30, Dave Angel <davea at ieee.org> wrote:
> (Erasing entire history, since you both top-posted, and it's too confusing)
>
> When you run a xxx.py on Windows, it locates the python.exe using the
> registry entries set up by ?assoc and ftype. ?But when you run "python" it
> looks for python.exe (or python.bat, or ...) on the PATH. ?So there's not
> necessarily any conection between your tests. ?You're probably running two
> different versions.
>
> Easiest way to know which python is being used is to run it same way:
>
> e:\python31\temp> python 2to3.py -w mycalc.py
>
> Or the way I do it, have python31.bat and python26.bat, which launch the
> exact pythons that I want.

Thanks, Dave. I'm not familiar with .bat files. Could you give me a
look at one of them?

Dick

From bermanrl at cfl.rr.com  Tue Nov  3 16:20:06 2009
From: bermanrl at cfl.rr.com (Robert Berman)
Date: Tue, 03 Nov 2009 10:20:06 -0500
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a tuple
 that may be an unknown 'thing'.
Message-ID: <1257261606.29483.23.camel@bermanrl-desktop>


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call
last)

/home/bermanrl/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the
index [2][0]. I think it is the index but if that is the case why does
l1[2][0] produce the value assigned to that location and not the same
error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I
must know the value of both items in l1[2] before I change either value.
I guess the correct question is how do I change or set the value of
l1[0][1] when I specifically mean the second item of an element of a 2D
array?

I have read numerous explanations of this problem thanks to Google; but
no real explanation of setting of one element of the pair without
setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.

Robert

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/eda4bbdb/attachment.htm>

From mail at timgolden.me.uk  Tue Nov  3 16:47:05 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Tue, 03 Nov 2009 15:47:05 +0000
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
Message-ID: <4AF05079.20006@timgolden.me.uk>

Richard D. Moores wrote:
> On Tue, Nov 3, 2009 at 05:30, Dave Angel <davea at ieee.org> wrote:
>> (Erasing entire history, since you both top-posted, and it's too confusing)
>>
>> When you run a xxx.py on Windows, it locates the python.exe using the
>> registry entries set up by  assoc and ftype.  But when you run "python" it
>> looks for python.exe (or python.bat, or ...) on the PATH.  So there's not
>> necessarily any conection between your tests.  You're probably running two
>> different versions.
>>
>> Easiest way to know which python is being used is to run it same way:
>>
>> e:\python31\temp> python 2to3.py -w mycalc.py
>>
>> Or the way I do it, have python31.bat and python26.bat, which launch the
>> exact pythons that I want.
> 
> Thanks, Dave. I'm not familiar with .bat files. Could you give me a
> look at one of them?

As an alternative, I've created python26.exe, python31.exe etc. in 
my c:\tools directory (which is on my PATH wherever I am) and which 
are hardlinks to their corresponding c:\python26\python.exe etc.
Same as the batch file, really, only without a batch file!

TJG

From waynejwerner at gmail.com  Tue Nov  3 16:53:13 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 3 Nov 2009 09:53:13 -0600
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
	tuple that may be an unknown 'thing'.
In-Reply-To: <1257261606.29483.23.camel@bermanrl-desktop>
References: <1257261606.29483.23.camel@bermanrl-desktop>
Message-ID: <333efb450911030753k40105a72xb6382578a66035a9@mail.gmail.com>

On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman <bermanrl at cfl.rr.com> wrote:

>
>   In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>

This calls the element at index 2 which is:
(0,0) - a tuple, then calls element [0] from that tuple, which is 0

when you try to assign an item into a tuple, you get the same problem:

In [1]: x = (1,2,3)

In [2]: type(x)
Out[2]: <type 'tuple'>

In [3]: x[0]
Out[3]: 1

In [4]: x[0] = 0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/wayne/Desktop/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

And from your example:
In [6]: l1 = [(0,0)] *4

In [7]: type(l1[2])
Out[7]: <type 'tuple'>


> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the index
> [2][0]. I think it is the index but if that is the case why does l1[2][0]
> produce the value assigned to that location and not the same error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value. I
> guess the correct question is how do I change or set the value of l1[0][1]
> when I specifically mean the second item of an element of a 2D array?
>

When you use l1[2] = 3,1 it converts the right hand side to a tuple by
implication - putting a comma between values:

In [8]: x = 3,1

 In [9]: type(x)
Out[9]: <type 'tuple'>

so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to the
list element at l1[2]" which is perfectly fine, because lists are mutable
and tuples are not.



>
> I have read numerous explanations of this problem thanks to Google; but no
> real explanation of setting of one element of the pair without setting the
> second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.
>

Hopefully this helps,
Wayne

p.s. If you want to be able to change individual elements, you can try this:
In [21]: l1 = [[0,0] for x in xrange(4)]

In [22]: l1
Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [23]: l1[2][0] = 3

In [24]: l1
Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]


I don't know if there's a better way to express line 21, but you can't do it
the other way or you'll just have the same list in your list 4 times:

In [10]: l1 = [[0,0]]*4

In [11]: l1
Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [12]: l1[2][0] = 3

In [13]: l1
Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/d9616c76/attachment.htm>

From emile at fenx.com  Tue Nov  3 16:55:15 2009
From: emile at fenx.com (Emile van Sebille)
Date: Tue, 03 Nov 2009 07:55:15 -0800
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
 tuple that may be an unknown 'thing'.
In-Reply-To: <1257261606.29483.23.camel@bermanrl-desktop>
References: <1257261606.29483.23.camel@bermanrl-desktop>
Message-ID: <hcpjlh$4en$1@ger.gmane.org>

On 11/3/2009 7:20 AM Robert Berman said...
> 
> In [69]: l1=[(0,0)] * 4
> 
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
> 
> In [71]: l1[2][0]
> Out[71]: 0
> 
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> 
> /home/bermanrl/<ipython console> in <module>()
> 
> TypeError: 'tuple' object does not support item assignment

Start out slower...

a = (1,2)
a[1]=3

You can't change a tuple.  You can create a new tuple, or you can change 
the content of an item help in a tuple, but you can't change the content 
of the tuple itself.  Try the following then see if it helps answer your 
questions...

b = [1]

a = (1,b)

b[0]=2

print a

a[1][0]=3

print a

Emile

> 
> First question, is the error referring to the assignment (3) or the 
> index [2][0]. I think it is the index but if that is the case why does 
> l1[2][0] produce the value assigned to that location and not the same 
> error message.
> 
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I 
> must know the value of both items in l1[2] before I change either value. 
> I guess the correct question is how do I change or set the value of 
> l1[0][1] when I specifically mean the second item of an element of a 2D 
> array?
> 
> I have read numerous explanations of this problem thanks to Google; but 
> no real explanation of setting of one element of the pair without 
> setting the second element of the pair as well.
> 
> For whatever glimmers of clarity anyone can offer. I thank you.
> 
> Robert
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


From andreengels at gmail.com  Tue Nov  3 16:53:49 2009
From: andreengels at gmail.com (Andre Engels)
Date: Tue, 3 Nov 2009 16:53:49 +0100
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
	tuple that may be an unknown 'thing'.
In-Reply-To: <1257261606.29483.23.camel@bermanrl-desktop>
References: <1257261606.29483.23.camel@bermanrl-desktop>
Message-ID: <6faf39c90911030753n5b833504x53a54917750989a4@mail.gmail.com>

On Tue, Nov 3, 2009 at 4:20 PM, Robert Berman <bermanrl at cfl.rr.com> wrote:

>
>   In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the index
> [2][0]. I think it is the index but if that is the case why does l1[2][0]
> produce the value assigned to that location and not the same error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value. I
> guess the correct question is how do I change or set the value of l1[0][1]
> when I specifically mean the second item of an element of a 2D array?
>
> I have read numerous explanations of this problem thanks to Google; but no
> real explanation of setting of one element of the pair without setting the
> second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.
>

Tuples are immutable types. Thus it is not possible to change one of the
values of a tuple (or even of changing both of them). The only thing you can
do, is create a new tuple, and put that in the same place in the list. In
your example, when you do l1[2][0] = 3, you try to change the tuple l1[2],
which is impossible.

To do what you want to do, you have to create a new array with the same
second but different first value, and put that array in l1[2], that is:

l1[2] = (3, l1[2,1])




-- 
Andr? Engels, andreengels at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/2e92beb3/attachment.htm>

From metolone+gmane at gmail.com  Tue Nov  3 17:01:56 2009
From: metolone+gmane at gmail.com (Mark Tolonen)
Date: Tue, 3 Nov 2009 08:01:56 -0800
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
	tuple that may be an unknown 'thing'.
References: <1257261606.29483.23.camel@bermanrl-desktop>
Message-ID: <hcpk5l$6hk$1@ger.gmane.org>


"Robert Berman" <bermanrl at cfl.rr.com> wrote in message 
news:1257261606.29483.23.camel at bermanrl-desktop...
>
> In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the
> index [2][0]. I think it is the index but if that is the case why does
> l1[2][0] produce the value assigned to that location and not the same
> error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value.
> I guess the correct question is how do I change or set the value of
> l1[0][1] when I specifically mean the second item of an element of a 2D
> array?
>
> I have read numerous explanations of this problem thanks to Google; but
> no real explanation of setting of one element of the pair without
> setting the second element of the pair as well.

Tuples are read-only, so you can't change just one element of a tuple:

    >>> x=0,0
    >>> x
    (0, 0)
    >>> x[0]=1
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment

You can, however, replace the whole thing, as you found:

    >>> x=1,1
    >>> x
    (1, 1)

To do what you want, you a list of lists, not a list of tuples, but there is 
a gotcha.  This syntax:

    >>> L=[[0,0]]*4
    >>> L
    [[0, 0], [0, 0], [0, 0], [0, 0]]

Produces a list of the *same* list object, so modifying one modifies all:

    >>> L[2][0]=1
    >>> L
    [[1, 0], [1, 0], [1, 0], [1, 0]]

Use a list comprehension to create lists of lists, where each list is a 
*new* list:

    >>> L = [[0,0] for i in range(4)]
    >>> L
    [[0, 0], [0, 0], [0, 0], [0, 0]]
    >>> L[2][0] = 1
    >>> L
    [[0, 0], [0, 0], [1, 0], [0, 0]]

-Mark



From bermanrl at cfl.rr.com  Tue Nov  3 17:12:21 2009
From: bermanrl at cfl.rr.com (Robert Berman)
Date: Tue, 03 Nov 2009 11:12:21 -0500
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
 tuple that may be an unknown 'thing'.
In-Reply-To: <333efb450911030753k40105a72xb6382578a66035a9@mail.gmail.com>
References: <1257261606.29483.23.camel@bermanrl-desktop>
	<333efb450911030753k40105a72xb6382578a66035a9@mail.gmail.com>
Message-ID: <1257264741.29483.36.camel@bermanrl-desktop>

Thank you for your explanations and especially your clear examples of a
phenomenon(when list elements are tuples) which takes a few moments of
study to truly grasp.

Robert

On Tue, 2009-11-03 at 09:53 -0600, Wayne Werner wrote:
> On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman <bermanrl at cfl.rr.com>
> wrote:
> 
>         
>         
>         In [69]: l1=[(0,0)] * 4
>         
>         In [70]: l1
>         Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>         
>         In [71]: l1[2][0]
>         Out[71]: 0
>         
> 
> 
> This calls the element at index 2 which is:
> (0,0) - a tuple, then calls element [0] from that tuple, which is 0
> 
> 
> when you try to assign an item into a tuple, you get the same problem:
> 
> 
> In [1]: x = (1,2,3)
> 
> 
> In [2]: type(x)
> Out[2]: <type 'tuple'>
> 
> 
> In [3]: x[0]
> Out[3]: 1
> 
> 
> In [4]: x[0] = 0
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
> 
> 
> /home/wayne/Desktop/<ipython console> in <module>()
> 
> 
> TypeError: 'tuple' object does not support item assignment
> 
> 
> And from your example:
> In [6]: l1 = [(0,0)] *4
> 
> 
> In [7]: type(l1[2])
> Out[7]: <type 'tuple'>
> 
> 
>         
>         In [72]: l1[2][0] = 3
>         ---------------------------------------------------------------------------
>         TypeError                                 Traceback (most
>         recent call last)
>         
>         /home/bermanrl/<ipython console> in <module>()
>         
>         TypeError: 'tuple' object does not support item assignment
>         
>         First question, is the error referring to the assignment (3)
>         or the index [2][0]. I think it is the index but if that is
>         the case why does l1[2][0] produce the value assigned to that
>         location and not the same error message.
>         
>         Second question, I do know that l1[2] = 3,1 will work. Does
>         this mean I must know the value of both items in l1[2] before
>         I change either value. I guess the correct question is how do
>         I change or set the value of l1[0][1] when I specifically mean
>         the second item of an element of a 2D array?
>         
> 
> 
> When you use l1[2] = 3,1 it converts the right hand side to a tuple by
> implication - putting a comma between values:
> 
> 
> In [8]: x = 3,1
> 
> 
>  In [9]: type(x)
> Out[9]: <type 'tuple'>
> 
> 
> so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to
> the list element at l1[2]" which is perfectly fine, because lists are
> mutable and tuples are not. 
> 
> 
>  
>         
>         I have read numerous explanations of this problem thanks to
>         Google; but no real explanation of setting of one element of
>         the pair without setting the second element of the pair as
>         well.
>         
>         For whatever glimmers of clarity anyone can offer. I thank
>         you.
> 
> 
> Hopefully this helps,
> Wayne
> 
> 
> p.s. If you want to be able to change individual elements, you can try
> this:
> In [21]: l1 = [[0,0] for x in xrange(4)]
> 
> 
> In [22]: l1
> Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]
> 
> 
> In [23]: l1[2][0] = 3
> 
> 
> In [24]: l1
> Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]
> 
> 
> 
> 
> I don't know if there's a better way to express line 21, but you can't
> do it the other way or you'll just have the same list in your list 4
> times:
> 
> 
> In [10]: l1 = [[0,0]]*4
> 
> 
> In [11]: l1
> Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]
> 
> 
> In [12]: l1[2][0] = 3
> 
> 
> In [13]: l1
> Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]
> 
> 
> 
> 
> 
> 
> 
> 
>  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/884e489a/attachment.htm>

From rabidpoobear at gmail.com  Tue Nov  3 19:10:59 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Tue, 3 Nov 2009 12:10:59 -0600
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <4AF03083.3070803@ieee.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
Message-ID: <dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>

On Tue, Nov 3, 2009 at 7:30 AM, Dave Angel <davea at ieee.org> wrote:

> (Erasing entire history, since you both top-posted, and it's too confusing)
>
I was under the impression that you were supposed to top-post unless you
were referring to a specific thing someone said, in which case you quoted
it, much like you do on a forum.
Is that not how it works?
I'd personally rather people top-posted unless they're specifically
referencing something, otherwise there's a huge tree of quotiness that I
have to scroll past before I can see what they're trying to say.
Maybe that's just me, though.  No one else seems to mind having to hit
"reply to all" to send a message to the list either.
Hopefully Google will succeed in their mission to replace e-mail with Wave
and all messages will be in context and there will be no need for quoting
one way or the other.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/0bc5e866/attachment.htm>

From kent37 at tds.net  Tue Nov  3 19:45:22 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 3 Nov 2009 13:45:22 -0500
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
Message-ID: <1c2a2c590911031045m463308a3r5bf1434876980836@mail.gmail.com>

On Tue, Nov 3, 2009 at 1:10 PM, Luke Paireepinart
<rabidpoobear at gmail.com> wrote:
> I was under the impression that you were supposed to top-post unless you
> were referring to a specific thing someone said, in which case you quoted
> it, much like you do on a forum.
> Is that not how it works?

Presumably you are replying to *something* in the previous post, yes?

> I'd personally rather people top-posted unless they're specifically
> referencing something, otherwise there's a huge tree of quotiness that I
> have to scroll past before I can see what they're trying to say.

You (or whoever) should delete the part of the "huge tree of
quotiness" that is not relevant to the reply.

> Hopefully Google will succeed in their mission to replace e-mail with Wave
> and all messages will be in context and there will be no need for quoting
> one way or the other.

Yes, that would help.

Kent

From sirgnip at gmail.com  Tue Nov  3 20:18:48 2009
From: sirgnip at gmail.com (Scott Nelson)
Date: Tue, 3 Nov 2009 13:18:48 -0600
Subject: [Tutor] PyWin32 - Library of functions to interact with windows?
In-Reply-To: <2682ac9b0910141126l92789ft20314ad2d73d9013@mail.gmail.com>
References: <mailman.5514.1255438063.2806.tutor@python.org>
	<5C9D64AAA86343A697750478F3B9E095@COMPUTER01>
	<hb52ao$blq$1@ger.gmane.org>
	<2682ac9b0910141126l92789ft20314ad2d73d9013@mail.gmail.com>
Message-ID: <2682ac9b0911031118y1c8b3423m806c62f16f7e07c7@mail.gmail.com>

Scott Nelson <sirgnip at gmail.com> wrote:
>
>  It *is* possible to color console text with Python and pywin.  But, it is
> tricky and not obvious.  I've been wondering how to do this myself and I
> recently found some C code on the web [2] that does this and I translated
> that into to Python and pywin.  It can be done in about 4 lines of Python.
>

For the sake of posterity (and the archives), I figured I'd list the "4
lines of Python" I mentioned above:

<code>
import win32console
handle = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE)
handle.SetConsoleTextAttribute(win32console.FOREGROUND_BLUE)
print 'blue text'
</code>

Beginning with this snippet, there are a number of other things you can do,
such as set the cursor position (handle.SetConsoleCursorPosition) and get
the properties of the console itself (handle.GetConsoleScreenBufferInfo).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/a27218e7/attachment-0001.htm>

From alan.gauld at btinternet.com  Tue Nov  3 20:50:22 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 3 Nov 2009 19:50:22 -0000
Subject: [Tutor] java for python programmers
References: <da81a0a80911030203w8013659u1d0baf7115596b6c@mail.gmail.com>
Message-ID: <hcq1i0$pg8$1@ger.gmane.org>


"Amit Sethi" <amit.pureenergy at gmail.com> wrote

> Hi their is a book , python for java programmers is their a book on java 
> for
> python programmers for some one who wants to move to java from python

Not that I know of but you could try the Jython book for starters to play 
with
the Java class library which is half the battle of  using Java.

Then try Bruce Eckel's Thinking in Java or the O'Reilly Learning Java,
both are good basic intros if you already know a language - like Python
say - IMHO. And they are not too basic - ala Java for Dummies...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Tue Nov  3 21:33:27 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 3 Nov 2009 20:33:27 -0000
Subject: [Tutor] trouble using 2to3.py
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com><dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com><d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com><4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
Message-ID: <hcq42o$2pu$1@ger.gmane.org>


"Richard D. Moores" <rdmoores at gmail.com> wrote

>> Or the way I do it, have python31.bat and python26.bat, which launch the
>> exact pythons that I want.
>
> Thanks, Dave. I'm not familiar with .bat files. Could you give me a
> look at one of them?

A bat file is just a file of "DOS" commands and ends in .bat

so python31.bat

will contain the single line

C:\path\ro\python\31\python.exe %*



The %* is a placeholder for any commandline arguments you give
when calling the batch file -  like sys.argv in python.

You then call it with

python31 myscript.py


There is a brief discussion and example of a bat file in the intro
to my tutor, in the What is Programming topic.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




From bermanrl at cfl.rr.com  Tue Nov  3 22:12:30 2009
From: bermanrl at cfl.rr.com (Robert Berman)
Date: Tue, 03 Nov 2009 16:12:30 -0500
Subject: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble using
	2to3.py
In-Reply-To: <dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
Message-ID: <1257282750.29483.56.camel@bermanrl-desktop>

The war between bottom posters and top posters has been long, arduous,
and most often incredibly silly. There have been written group rules
mandating one over the other. Oft times these rules have doomed the
group. 

I have used both approaches most often favoring top posting only because
my mail program is set to reply from the top. True, it can be set to
reply from the bottom and I can pick and choose as I see fit, but I
usually let the program dictate.

The reality is, bottom posters are not blessed by god and top posters
are not bottom feeders. 

I simply propose that the only requirement to communications here is
that replies and questions be well formulated, courteous, and reasonably
intelligent. We are not 'Facebooks' and we hopefully do not condone cute
rudeness and sophmoric attempts at vast intellect. 

I must say that the answers to my questions have almost always been
incredibly well constructed  and concisely and clearly presented. I
almost never notice their placement within the post. 

If there are going to be arguments pertaining to the posts, let them be
over content and not placement.

Thanks to all who participate in this group.

Robert Berman


On Tue, 2009-11-03 at 12:10 -0600, Luke Paireepinart wrote:
> 
> 
> On Tue, Nov 3, 2009 at 7:30 AM, Dave Angel <davea at ieee.org> wrote:
>         (Erasing entire history, since you both top-posted, and it's
>         too confusing)
> I was under the impression that you were supposed to top-post unless
> you were referring to a specific thing someone said, in which case you
> quoted it, much like you do on a forum.
> Is that not how it works?
> I'd personally rather people top-posted unless they're specifically
> referencing something, otherwise there's a huge tree of quotiness that
> I have to scroll past before I can see what they're trying to say.
> Maybe that's just me, though.  No one else seems to mind having to hit
> "reply to all" to send a message to the list either.
> Hopefully Google will succeed in their mission to replace e-mail with
> Wave and all messages will be in context and there will be no need for
> quoting one way or the other.
> -Luke
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


From davea at ieee.org  Tue Nov  3 22:16:35 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 03 Nov 2009 16:16:35 -0500
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
Message-ID: <4AF09DB3.3070109@ieee.org>

Richard D. Moores wrote:
> On Tue, Nov 3, 2009 at 05:30, Dave Angel <davea at ieee.org> wrote:
>   
>> <snip>
>>
>> Easiest way to know which python is being used is to run it same way:
>>
>> e:\python31\temp> python 2to3.py -w mycalc.py
>>
>> Or the way I do it, have python31.bat and python26.bat, which launch the
>> exact pythons that I want.
>>     
>
> Thanks, Dave. I'm not familiar with .bat files. Could you give me a
> look at one of them?
>
> Dick
>
>   
A batch file is Windows (much dummyfied) version of Unix script files.  
It's a "program" interpreted by CMD.EXE, the shell processor for a 
"Command Prompt".   Anyway, it's usually not much more than a list of 
programs to run, with their arguments.  In our case, we just want to run 
a particular python.exe in a particular path, with whatever arguments 
were passed to the batch file.  So mine are one line each, and look like:

python26.bat
-------------------
c:\progfiles\python26\python.exe %*


python31.bat
-------------------
c:\progfiles\python31\python.exe %*


Obviously, yours will be different, unless you happened to pick the same 
install path as I did.  The %* magic just means copy the rest of the 
original command line.

You run these something like:

m:\myscripts\ready >  python31  myprogram.py

to explicitly run python31 on the python script, myprogram.py

DaveA


From modulok at gmail.com  Tue Nov  3 22:35:44 2009
From: modulok at gmail.com (Modulok)
Date: Tue, 3 Nov 2009 14:35:44 -0700
Subject: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble
	using 2to3.py
In-Reply-To: <1257282750.29483.56.camel@bermanrl-desktop>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
	<1257282750.29483.56.camel@bermanrl-desktop>
Message-ID: <64c038660911031335p5fba67c3pbc3078c3b2b9050a@mail.gmail.com>

[snip]
> The war between bottom posters and top posters has been long, arduous,
> and most often incredibly silly. There have been written group rules
> mandating one over the other. Oft times these rules have doomed the
> group.
>

Two words come to mind: Bikeshed color.

> I simply propose that the only requirement to communications here is
> that replies and questions be well formulated, courteous, and reasonably
> intelligent.

Yup.
-Modulok-

From alan.gauld at btinternet.com  Tue Nov  3 22:52:12 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 3 Nov 2009 21:52:12 -0000
Subject: [Tutor] Retrieving information from a plain text
	file(WinXP/py2.6.2/Beginner)
References: <mailman.15.1257073202.23443.tutor@python.org><307953F1474F43AB84A8CB004C6F0237@COMPUTER01>
	<cadf44510911021143v30e970e3x5ca249462aec31ea@mail.gmail.com>
Message-ID: <hcq8md$inv$1@ger.gmane.org>


"Serdar Tumgoren" <zstumgoren at gmail.com> wrote 

> Also, on a side note, you can greatly improve the readability of your
> code by using the triple-quote style for multi-line docstrings inside
> functions (rather than the hash comment marks). I tend to use hash
> marks for one-line/inline comments, since they can really become an
> eyesore (at least IMHO) when used too liberally.

I'd second that suggestion with the added benefit that if you use 
docstrings they will be detected by Pythons help() function.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From alan.gauld at btinternet.com  Tue Nov  3 23:04:09 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 3 Nov 2009 22:04:09 -0000
Subject: [Tutor] Can't find modules at command line
References: <513099.91923.qm@web43143.mail.sp1.yahoo.com>
	<4AEEC8B4.1040109@compuscan.co.za>
Message-ID: <hcq9cq$l6g$1@ger.gmane.org>

"Christian Witts" <cwitts at compuscan.co.za> wrote
>> >>> import gun
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> ImportError: No module named gun
>> >>> 
>>
>> This file is at [/Users/sam/Documents/gun.py].  What should I do to 
>> make it visible to Python?
>>   
> In the console when you start it up you appear to be in your home 
> directory `~` but your script is in the Documents folder so you will 
> need to add Documents to your path for eg.
> from sys import path
> path.append('/path/to/script/to/be/imported')
> import gun

Or you can create/edit your PYTHONPATH environment variable
to include /Users/sam/Documents.

You do that in your ~/.bash_profile file with

export PYTHONPATH=$PYTHONPATH:/Users/sam/Documents

Note the dot before the filename...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




From alan.gauld at btinternet.com  Tue Nov  3 23:19:02 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 3 Nov 2009 22:19:02 -0000
Subject: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble
	using2to3.py
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com><dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com><d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com><4AF03083.3070803@ieee.org><dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
	<1257282750.29483.56.camel@bermanrl-desktop>
Message-ID: <hcqa8o$o22$1@ger.gmane.org>

"Robert Berman" <bermanrl at cfl.rr.com> wrote

> The war between bottom posters and top posters has been long, arduous,
> and most often incredibly silly.

It has, I agree but there is a very real difference in that gratuitous top
posting does actually become unreadable if not done very carefully.
And it also encorages people to leave the whole previous thread in
place which is very naughty for those who pay for ther internet
access by the byte! (Or those whose mail server implements
space quotas!)

But if a general comment is being made that does not need to refer
to specific quotations from the earlier rtext then I have no problem
with top posting. Provided all but a small context quote is left below.
But please don't send me 3 pages of text to just add two sentences
at the top!!!

> I simply propose that the only requirement to communications here is
> that replies and questions be well formulated, courteous, and reasonably
> intelligent.

Ah but there's the rub, what is "well formatted"? :-)

> If there are going to be arguments pertaining to the posts, let them be
> over content and not placement.

When you are trying to respond to hundreds of emails a day a
few top posted messages can cause severe headaches. Personally I
just ignore anything that has top posting that is hard to read, I don't 
have
time to unscramble it nor to waste time asking the poster to desist.
If they post scrambled mail it doesn't get answered...

So its not quite a case of the "color of the bikeshed" because this one
does make a very real difference to the usability of the medium and
to the success of the message in reaching its full audience.

In most cases it doesn't hurt much but in longer threads it does.
So, please folks, be sensitive to your readers.

Alan G.



From davea at ieee.org  Tue Nov  3 23:29:18 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 03 Nov 2009 17:29:18 -0500
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <4AF05079.20006@timgolden.me.uk>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	<4AF03083.3070803@ieee.org>	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<4AF05079.20006@timgolden.me.uk>
Message-ID: <4AF0AEBE.3080501@ieee.org>

Tim Golden wrote:
> Richard D. Moores wrote:
>> On Tue, Nov 3, 2009 at 05:30, Dave Angel <davea at ieee.org> wrote:
>>> <snip>
>>> Or the way I do it, have python31.bat and python26.bat, which launch 
>>> the
>>> exact pythons that I want.
>>
>> Thanks, Dave. I'm not familiar with .bat files. Could you give me a
>> look at one of them?
>
> As an alternative, I've created python26.exe, python31.exe etc. in my 
> c:\tools directory (which is on my PATH wherever I am) and which are 
> hardlinks to their corresponding c:\python26\python.exe etc.
> Same as the batch file, really, only without a batch file!
>
> TJG
I've thought of using hard links (using fsutil.exe, in case anyone else 
is interested), but I keep my scripts and batch files, as well as small 
utilities on a separate drive  partition from the one that has my OS and 
installed programs.  And hard links don't work across separate partitions.

But tell me, how does python.exe find its "home" directory, to set 
initial sys.path and suchlike?  I assumed it was done relative to the 
python.exe's full path location.  But if you hard link, nobody knows 
where the real executable image resides (and in fact the concept has no 
meaning, since the two directory entries equally own the content).

I once downloaded a program (which worked on many python scripts, but 
not all) which was written as a standin for python.exe, loading the 
pythonNN.dll and executing the script.  The intentional difference was 
that instead of getting the script name from the commandline, it used 
its own name as the name of the script, and searched in the same 
directory for that script.  So effectively if you made a copy of this 
program and called it  doit.exe, when it ran it would find doit.py and 
run that script.

One advantage it conferred is that the tasklist now identifies your 
program by name, instead of just having an anonymous python.exe running 
one (or six) instances.

I don't remember what it was called, but I'd love to play with it 
further.  And now I'd like to know how it could know where the lib files 
and suchlike are, as well.

DaveA


From rdmoores at gmail.com  Wed Nov  4 00:29:27 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 15:29:27 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <hcq42o$2pu$1@ger.gmane.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
Message-ID: <d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>

My sincere thanks to Dave Angel and Alan Gauld for their clear
explanations of .bat files and how to use them. I've already made
several useful ones.

Dick Moores

From bgailer at gmail.com  Wed Nov  4 01:06:05 2009
From: bgailer at gmail.com (bob gailer)
Date: Tue, 03 Nov 2009 19:06:05 -0500
Subject: [Tutor] Being beaten up by a tuple that's an integer thats a
 tuple that may be an unknown 'thing'.
In-Reply-To: <1257261606.29483.23.camel@bermanrl-desktop>
References: <1257261606.29483.23.camel@bermanrl-desktop>
Message-ID: <4AF0C56D.1050409@gmail.com>

Robert Berman wrote:
>
> In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call 
> last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the 
> index [2][0]. I think it is the index but if that is the case why does 
> l1[2][0] produce the value assigned to that location and not the same 
> error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean 
> I must know the value of both items in l1[2] before I change either 
> value. I guess the correct question is how do I change or set the 
> value of l1[0][1] when I specifically mean the second item of an 
> element of a 2D array?
>
> I have read numerous explanations of this problem thanks to Google; 
> but no real explanation of setting of one element of the pair without 
> setting the second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.

Tuples are immutable. Replace them with lists and voila. l1=[[0,0]] * 4

But also realize that you are creating a list with 4 copies of one 
object [0,0]. Assigning to one changes all!


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From alan.gauld at btinternet.com  Wed Nov  4 01:46:42 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 4 Nov 2009 00:46:42 -0000
Subject: [Tutor] trouble using 2to3.py
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com><dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com><d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com><4AF03083.3070803@ieee.org><d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com><hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
Message-ID: <hcqitn$jmb$1@ger.gmane.org>


"Richard D. Moores" <rdmoores at gmail.com> wrote 

> My sincere thanks to Dave Angel and Alan Gauld for their clear
> explanations of .bat files and how to use them. I've already made
> several useful ones.

Start the cmd shell in windows and run the help command to 
get a list of things you can put in to spruce them up. It will explain 
how to use IF statements and FOR loops to make them much 
more powerful. But in practice if you know Python you will 
usually find it easier with that! :-)

Alan G


From rdmoores at gmail.com  Wed Nov  4 02:21:59 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 17:21:59 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <hcqitn$jmb$1@ger.gmane.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
Message-ID: <d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>

On Tue, Nov 3, 2009 at 16:46, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Richard D. Moores" <rdmoores at gmail.com> wrote
>>
>> My sincere thanks to Dave Angel and Alan Gauld for their clear
>> explanations of .bat files and how to use them. I've already made
>> several useful ones.
>
> Start the cmd shell in windows and run the help command to get a list of
> things you can put in to spruce them up.

Sorry, put in where?

> It will explain how to use IF
> statements and FOR loops to make them much more powerful. But in practice if
> you know Python you will usually find it easier with that! :-)

Wow, the "online help utility" for 3.1 looks really useful and
informative. One question. Many of the articles are LONG. When I've
got what wanted out of one, I want to quit, but don't know how to quit
More. How to?

Another is, although I've seen this before in older Python docs, I
never really understood things like

for_stmt ::= "for" target_list "in" expression_list ":" suite
             ["else" ":" suite]

help> ::=
no Python documentation found for '::='

In particular, what exactly does "::=" mean?

Thanks,

Dick

From rdmoores at gmail.com  Wed Nov  4 03:24:22 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Tue, 3 Nov 2009 18:24:22 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
	<d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
Message-ID: <d71c7ed60911031824y4f88a375l9fd7c97dceb837aa@mail.gmail.com>

On Tue, Nov 3, 2009 at 17:21, Richard D. Moores <rdmoores at gmail.com> wrote:
> Another is, although I've seen this before in older Python docs, I
> never really understood things like
>
> for_stmt ::= "for" target_list "in" expression_list ":" suite
> ? ? ? ? ? ? ["else" ":" suite]
>
> help> ::=
> no Python documentation found for '::='
>
> In particular, what exactly does "::=" mean?

Found it. See <http://docs.python.org/3.1/reference/introduction.html#notation>

Dick

From amit.pureenergy at gmail.com  Wed Nov  4 07:49:02 2009
From: amit.pureenergy at gmail.com (Amit Sethi)
Date: Wed, 4 Nov 2009 12:19:02 +0530
Subject: [Tutor] idiomatic way of merging two dictionaries
Message-ID: <da81a0a80911032249x10b9b6fbseb5db213b3e72218@mail.gmail.com>

Hi , I am trying to merge(I am not sure if that is the right term)
dictionaries like this

dict1 ={'a':4,'b':3,'v':7,'h':4}
dict2={'a':5,'v':4,'k':3}
dict3 would be {'a':[4,5],'b':[3,4],'v':[7,4],'k':[0,3],'h':[4,0]}

-- 
A-M-I-T S|S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091104/d4617228/attachment.htm>

From alan.gauld at btinternet.com  Wed Nov  4 09:31:10 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Wed, 4 Nov 2009 08:31:10 +0000 (GMT)
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
	<d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
Message-ID: <653323.80143.qm@web86705.mail.ird.yahoo.com>



> >> My sincere thanks to Dave Angel and Alan Gauld for their clear
> >> explanations of .bat files and how to use them. I've already made
> >
> > Start the cmd shell in windows and run the help command to get a list of
> > things you can put in to spruce them up.
>
> Sorry, put in where?


In bat files, that's what you were discussing :-)

> Wow, the "online help utility" for 3.1 looks really useful and


Yes, but I was talking about the DOS help utility not the Python one...

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091104/fd537061/attachment.htm>

From alan.gauld at btinternet.com  Wed Nov  4 09:41:38 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 4 Nov 2009 08:41:38 -0000
Subject: [Tutor] idiomatic way of merging two dictionaries
References: <da81a0a80911032249x10b9b6fbseb5db213b3e72218@mail.gmail.com>
Message-ID: <hcreo4$idb$1@ger.gmane.org>


"Amit Sethi" <amit.pureenergy at gmail.com> wrote

> Hi , I am trying to merge(I am not sure if that is the right term)
> dictionaries like this
> 
> dict1 ={'a':4,'b':3,'v':7,'h':4}
> dict2={'a':5,'v':4,'k':3}
> dict3 would be {'a':[4,5],'b':[3,4],'v':[7,4],'k':[0,3],'h':[4,0]}

I don't know how "idiomatic" it is but I'd probably just go for a for loop

dict3 = {}
for key in set(dict1.keys()+dict2.keys()):    # get the unique keys
     dict3[key] = [dict1.get(key,0),dict2.get(key,0)]

I suspect that in Python 3 you could use the new dict notation 
to do it slightly more neatly.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From lie.1296 at gmail.com  Wed Nov  4 12:18:55 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Wed, 04 Nov 2009 22:18:55 +1100
Subject: [Tutor] idiomatic way of merging two dictionaries
In-Reply-To: <da81a0a80911032249x10b9b6fbseb5db213b3e72218@mail.gmail.com>
References: <da81a0a80911032249x10b9b6fbseb5db213b3e72218@mail.gmail.com>
Message-ID: <hcrnvv$dtm$1@ger.gmane.org>

Amit Sethi wrote:
> Hi , I am trying to merge(I am not sure if that is the right term) 
> dictionaries like this
> 
> dict1 ={'a':4,'b':3,'v':7,'h':4}
> dict2={'a':5,'v':4,'k':3}
> dict3 would be {'a':[4,5],'b':[3,4],'v':[7,4],'k':[0,3],'h':[4,0]}
> 

I can't see what you're trying to do there. is 'b' supposed to be 3,4 
or 0,3?

And is th ordering between the list (i.e. 'a':[4, 5] and 'a':[5, 4]) 
significant?


From garry.willgoose at newcastle.edu.au  Wed Nov  4 13:16:17 2009
From: garry.willgoose at newcastle.edu.au (Garry Willgoose)
Date: Wed, 4 Nov 2009 23:16:17 +1100
Subject: [Tutor] why is os.path.walk so slow?
Message-ID: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>

I need to synchronize the files on my home and office machine and have  
been using someone else's code for this to date but have been  
frustrated by how slow it is in getting the information on files for  
the mounted drive from my office machine so I thought I'd experiment  
with a python facility for this. The code I've experimented with is as  
below

def visitfunc(arg,dirname,names):
   global filelist
   import os.path
   for name in names:
     if not os.path.isdir(dirname+'/'+name):
       fullname=dirname+'/'+name
       filelist.append([fullname,os.path.getmtime(fullname)])
   return()

def check(dir):
   global filelist
   import os.path
   filelist=[]
   os.path.walk(dir,visitfunc,'')
   print filelist
   return()

This is very fast for a directory on my local machine but  
significantly slower on the remote machine. Not surprising but I would  
have expected that the run time for the remote directory would be  
limited by my broadband speed but when I look at upload/download in  
real time it's less than 10% of maximum. Is this just par for the  
course or is there something I can do that better utilizes my  
broadband bandwidth?

====================================================================
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:  garry.willgoose at newcastle.edu.au; g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
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
====================================================================






From mail at timgolden.me.uk  Wed Nov  4 16:41:41 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Wed, 04 Nov 2009 15:41:41 +0000
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <4AF0AEBE.3080501@ieee.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	<4AF03083.3070803@ieee.org>	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>	<4AF05079.20006@timgolden.me.uk>
	<4AF0AEBE.3080501@ieee.org>
Message-ID: <4AF1A0B5.1090506@timgolden.me.uk>

Dave Angel wrote:
> I've thought of using hard links (using fsutil.exe, in case anyone else 
> is interested), but I keep my scripts and batch files, as well as small 
> utilities on a separate drive  partition from the one that has my OS and 
> installed programs.  And hard links don't work across separate partitions.

That would be a problem. I just use a c:\tools wherever I go
where I dump anything useful.

> But tell me, how does python.exe find its "home" directory, to set 
> initial sys.path and suchlike?  I assumed it was done relative to the 
> python.exe's full path location.  But if you hard link, nobody knows 
> where the real executable image resides (and in fact the concept has no 
> meaning, since the two directory entries equally own the content).

Well, I haven't bothered to look at the machinery in question,
but before I did it, I just made sure it would work:

<dump>
H:\>python31 -c "import sys; print (sys.executable, '\n'.join (sys.path))"
c:\tools\python31.exe
C:\WINDOWS\system32\python31.zip
C:\Python31\Lib
C:\Python31\DLLs
H:\
c:\tools
C:\Python31
c:\work_in_progress\python-projects\wmi\trunk
c:\python31-site-packages
C:\Python31\lib\site-packages
C:\Python31\lib\site-packages\win32
C:\Python31\lib\site-packages\win32\lib
C:\Python31\lib\site-packages\Pythonwin
</dump>

TJG

From waynejwerner at gmail.com  Wed Nov  4 16:56:53 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Wed, 4 Nov 2009 09:56:53 -0600
Subject: [Tutor] why is os.path.walk so slow?
In-Reply-To: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>
References: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>
Message-ID: <333efb450911040756j214a77d1gc793dba4568059f4@mail.gmail.com>

On Wed, Nov 4, 2009 at 6:16 AM, Garry Willgoose <
garry.willgoose at newcastle.edu.au> wrote:

> <snip>
>
> This is very fast for a directory on my local machine but significantly
> slower on the remote machine. Not surprising but I would have expected that
> the run time for the remote directory would be limited by my broadband speed
> but when I look at upload/download in real time it's less than 10% of
> maximum. Is this just par for the course or is there something I can do that
> better utilizes my broadband bandwidth?


I'm not sure if there's a correlation, but there probably is. What OS are
you (and the remote system) using? What service are you using to connect?

By way of disclosure, I don't have a lot of experience in this category, but
I would venture that whatever service you're using has to send/receive
requests for each file/dir that os.walk checks.

I don't know precisely how os.walk works, but I'm guessing the differences
are as follows:

Local Machine:
python->os.walk->local system calls

Remote Machine:
python->os.walk->call to client->data through the tubes->remote host->system
calls

Even if that's not completely correct, you're still going to have extra
steps as you walk through the remote system, and the bottleneck will
probably not be on the internet connection (and your tests seem to verify
this).

It would work better, I think, if you were able to run a script on the
remote system and return the results.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091104/033228ea/attachment.htm>

From hugo.yoshi at gmail.com  Wed Nov  4 17:30:47 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Wed, 4 Nov 2009 17:30:47 +0100
Subject: [Tutor] why is os.path.walk so slow?
In-Reply-To: <333efb450911040756j214a77d1gc793dba4568059f4@mail.gmail.com>
References: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au> 
	<333efb450911040756j214a77d1gc793dba4568059f4@mail.gmail.com>
Message-ID: <29179d160911040830h721ad866xbc840fbcf5651389@mail.gmail.com>

On Wed, Nov 4, 2009 at 4:56 PM, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Wed, Nov 4, 2009 at 6:16 AM, Garry Willgoose
> <garry.willgoose at newcastle.edu.au> wrote:
>>
>> <snip>
>>
>> This is very fast for a directory on my local machine but significantly
>> slower on the remote machine. Not surprising but I would have expected that
>> the run time for the remote directory would be limited by my broadband speed
>> but when I look at upload/download in real time it's less than 10% of
>> maximum. Is this just par for the course or is there something I can do that
>> better utilizes my broadband bandwidth?
>
> I'm not sure if there's a correlation, but there probably is. What OS are
> you (and the remote system) using? What service are you using to connect?
> By way of disclosure, I don't have a lot of experience in this category, but
> I would venture that whatever service you're using has to send/receive
> requests for each file/dir that os.walk checks.
>
> <snip>

I'm taking a stab in the dark here, but maybe latency is the
bottleneck here. The process is sending a request for each
file/directory, waiting for the answer, and only then sending the next
request. All those little waits add up, even though the consumed
bandwidth is negligible.

Running the script on the remote server should be the solution if this
is the case, since you can request the data locally then transmit it
in one go, eliminating most of the waiting.

Hugo

From denis.spir at free.fr  Wed Nov  4 17:38:48 2009
From: denis.spir at free.fr (spir)
Date: Wed, 4 Nov 2009 17:38:48 +0100
Subject: [Tutor] PolyRange -- iterator
Message-ID: <20091104173848.0b9dbc1b@o>

Hello,

as I was crawling in endless complications with unadequate ranges, I decided to create a "PolyRange" type able to hold arbitrary many "sub-range"; which means finally a big range with wholes in it. This whole stuff again to cope with unicode -- a poly-range would be able to hold a range for a char class (eg 'letter') which spans over several ordinal ranges. (Viva unicode consortium!)

So, it works as needed. It is even able to "stretch" over addictional sub-ranges or to "unite" with a poly-range brother (sister, too). See code below.

Now, I need it to be properly iterable if needed -- the main use beeing indeed the 'in' operator -- but who knows. So, I tried to implement __iter__ and next (by the way, why isin't it called __next__ instead, looks strange for a python builtin?). Seems to work, but the result looks veeery heavy to my eyes. As I had never written an iterator before, would be nice and criticize the code?

Thank you,
Denis
-------
la vita e estrany



==========================================================
class PolyRange(object):
    def __init__(self, *bounds):
        ''' Build list of ranges from bounds.
            * bounds must sequence of (n1,n2) pairs. '''
        self.bounds = list(bounds)
        self.ranges = [xrange(n1,n2) for (n1,n2) in bounds]
        # iteration
    def __contains__(self, item):
        ''' x in self '''
        for range in self.ranges:
            if item in range:
                return True
    def __or__(self, other):
        ''' PolyRange union '''
        bounds = self.bounds + other.bounds
        return PolyRange(*bounds)
    def __add__(self, range):
        ''' PolyRange with new range added '''
        n1 = range[0] ; n2 = n1+len(range)
        bounds = self.bounds + [(n1,n2)]
        return PolyRange(*bounds)
    def __iter__(self):
        self.isEmpty = False
        (self.range_index, self.item_index) = (0,0)
        self.iter = self.ranges[0].__iter__()
        return self
    def next(self):
        # case already emptied
        if self.isEmpty:
            raise StopIteration
        # throw next item
        try:
            item = self.iter.next()
        except StopIteration:
            self.range_index += 1
            self.item_index = 0
        else:
            self.item_index += 1
            return item
        # next range
        if len(self.ranges) > self.range_index:
            self.iter = self.ranges[self.range_index].__iter__()
        else:
            self.empty = True
            raise StopIteration
        # throw item
        try:
            item = self.iter.next()
        except StopIteration:
            self.empty = True
            raise StopIteration
        else:
            self.item_index += 1
            return item
    def __str__(self):
        return "(%s)" %'U'.join("[%s,%s)" %(n1,n2) for (n1,n2) in self.bounds)
    def __repr__(self):
        return "PolyRange(%s)" %', '.join(str(b) for b in self.bounds)
        return False

def testPolyRange():
    print "=== base case -- test in/contains"
    pr = PolyRange((1,3),(5,7),(9,11))
    print "%s\t%s" %(pr,repr(pr))
    print 2 in pr, 4 in pr

    print "=== union"
    pr = PolyRange((1,3),(5,7)) | PolyRange((9,11))
    print pr

    print "=== addition, iteration"
    r = xrange(3,9)
    pr0 = PolyRange((1,3),(5,7))
    pr1 = pr0 + r
    print pr1
    for i in pr1: print i,
testPolyRange()



From hugo.yoshi at gmail.com  Wed Nov  4 18:17:21 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Wed, 4 Nov 2009 18:17:21 +0100
Subject: [Tutor] PolyRange -- iterator
In-Reply-To: <20091104173848.0b9dbc1b@o>
References: <20091104173848.0b9dbc1b@o>
Message-ID: <29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com>

On Wed, Nov 4, 2009 at 5:38 PM, spir <denis.spir at free.fr> wrote:
> Hello,
>
> as I was crawling in endless complications with unadequate ranges, I decided to create a "PolyRange" type able to hold arbitrary many "sub-range"; which means finally a big range with wholes in it. This whole stuff again to cope with unicode -- a poly-range would be able to hold a range for a char class (eg 'letter') which spans over several ordinal ranges. (Viva unicode consortium!)
>
> So, it works as needed. It is even able to "stretch" over addictional sub-ranges or to "unite" with a poly-range brother (sister, too). See code below.
>
> Now, I need it to be properly iterable if needed -- the main use beeing indeed the 'in' operator -- but who knows. So, I tried to implement __iter__ and next (by the way, why isin't it called __next__ instead, looks strange for a python builtin?). Seems to work, but the result looks veeery heavy to my eyes. As I had never written an iterator before, would be nice and criticize the code?
>

You're right, next() should really be called __next__(), and this is
actually fixed in python 3 (don't know why it wasn't originally done
this way).

Now, the code. If you write __iter__ as a generator, you won't have to
write a next method at all. It simplifies The thing a whole lot:

def __iter__(self):
    for range in self.ranges:
        for item in range:
            yield item

That's it. Alternatively, you could turn the whole thing into a
one-liner and just return a generator expression from __iter__:

def __iter__(self):
    return (item for r in self.ranges for item in r)

It's not as clear though, and it doesn't save that much space. I like
the first one slightly better.

python documentation on generators:
http://docs.python.org/tutorial/classes.html#generators

Hugo

From stefan at lsd.co.za  Wed Nov  4 19:25:44 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Wed, 4 Nov 2009 20:25:44 +0200
Subject: [Tutor] PolyRange -- iterator
In-Reply-To: <29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com>
References: <20091104173848.0b9dbc1b@o>
	<29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com>
Message-ID: <5cb309e70911041025t58441ad1u2350408ac196e4@mail.gmail.com>

On Wed, Nov 4, 2009 at 7:17 PM, Hugo Arts <hugo.yoshi at gmail.com> wrote:
> On Wed, Nov 4, 2009 at 5:38 PM, spir <denis.spir at free.fr> wrote:
>> Hello,
>>
>> as I was crawling in endless complications with unadequate ranges, I decided to create a "PolyRange" type able to hold arbitrary many "sub-range"; which means finally a big range with wholes in it. This whole stuff again to cope with unicode -- a poly-range would be able to hold a range for a char class (eg 'letter') which spans over several ordinal ranges. (Viva unicode consortium!)
>>
>> So, it works as needed. It is even able to "stretch" over addictional sub-ranges or to "unite" with a poly-range brother (sister, too). See code below.
>>
>> Now, I need it to be properly iterable if needed -- the main use beeing indeed the 'in' operator -- but who knows. So, I tried to implement __iter__ and next (by the way, why isin't it called __next__ instead, looks strange for a python builtin?). Seems to work, but the result looks veeery heavy to my eyes. As I had never written an iterator before, would be nice and criticize the code?
>>
>
> You're right, next() should really be called __next__(), and this is
> actually fixed in python 3 (don't know why it wasn't originally done
> this way).
>
> Now, the code. If you write __iter__ as a generator, you won't have to
> write a next method at all. It simplifies The thing a whole lot:
>
> def __iter__(self):
> ? ?for range in self.ranges:
> ? ? ? ?for item in range:
> ? ? ? ? ? ?yield item
>
> That's it. Alternatively, you could turn the whole thing into a
> one-liner and just return a generator expression from __iter__:
>
> def __iter__(self):
> ? ?return (item for r in self.ranges for item in r)
>
> It's not as clear though, and it doesn't save that much space. I like
> the first one slightly better.
>
> python documentation on generators:
> http://docs.python.org/tutorial/classes.html#generators

I dont know much about generators, but this link was posted earlier
this week and i learnt alot from it (actually some basics, like what
happens when you iterate over different objects!)

http://www.dabeaz.com/generators-uk/

(its the PDF presentation)

Stefan

From rdmoores at gmail.com  Wed Nov  4 19:32:34 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Wed, 4 Nov 2009 10:32:34 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <653323.80143.qm@web86705.mail.ird.yahoo.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
	<d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
	<653323.80143.qm@web86705.mail.ird.yahoo.com>
Message-ID: <d71c7ed60911041032s3c9af8fcte7474121b7ed67a3@mail.gmail.com>

On Wed, Nov 4, 2009 at 00:31, ALAN GAULD <alan.gauld at btinternet.com> wrote:
>> > Start the cmd shell in windows and run the help command to get a list of
>> > things you can put in to spruce them up.
>>
>> Sorry, put in where?
>
> In bat files, that's what you were discussing :-)
>> Wow, the "online help utility" for 3.1 looks really useful and
>
> Yes, but I was talking about the DOS help utility not the Python one...

Lots of info there, but for a .bat beginner it's hard to put it
together. I've put a couple of old books  on batch files on hold at my
local library, and also found the XP Command-line reference A-Z at
<http://technet.microsoft.com/en-us/library/bb490890.aspx>. Any
suggestions?

Thanks,

Dick

From alan.gauld at btinternet.com  Wed Nov  4 20:32:35 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Wed, 4 Nov 2009 11:32:35 -0800 (PST)
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <d71c7ed60911041032s3c9af8fcte7474121b7ed67a3@mail.gmail.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
	<d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
	<653323.80143.qm@web86705.mail.ird.yahoo.com>
	<d71c7ed60911041032s3c9af8fcte7474121b7ed67a3@mail.gmail.com>
Message-ID: <116861.5413.qm@web86706.mail.ird.yahoo.com>



> > Yes, but I was talking about the DOS help utility not the Python one...
>
> Lots of info there, but for a .bat beginner it's hard to put it
> together. I've put a couple of old books  on batch files on hold at my
> local library, and also found the XP Command-line reference A-Z at
> <http://technet.microsoft.com/en-us/library/bb490890.aspx>. Any
> suggestions?


You can do some pretty clever stuff with BAT files but frankly I wouldn't bother.
Python can do almost all of the same things more elegantly and in most 
cases faster using fewer resources.

Trying out a few loops and if/else type statements and writing a couple 
of test files may be an interesting diversion for an evening but the only 
real use of BAT files nowadays is to run a sequence of command 
line tools in one go. For anything else, use Python.

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091104/9e599f5e/attachment.htm>

From rdmoores at gmail.com  Wed Nov  4 20:52:08 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Wed, 4 Nov 2009 11:52:08 -0800
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <116861.5413.qm@web86706.mail.ird.yahoo.com>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com>
	<hcq42o$2pu$1@ger.gmane.org>
	<d71c7ed60911031529t4788d3e0mf042dd972b91b924@mail.gmail.com>
	<hcqitn$jmb$1@ger.gmane.org>
	<d71c7ed60911031721k5de5766fj8ac509285488363b@mail.gmail.com>
	<653323.80143.qm@web86705.mail.ird.yahoo.com>
	<d71c7ed60911041032s3c9af8fcte7474121b7ed67a3@mail.gmail.com>
	<116861.5413.qm@web86706.mail.ird.yahoo.com>
Message-ID: <d71c7ed60911041152q153eed38mb97e0e7ad4e9e857@mail.gmail.com>

On Wed, Nov 4, 2009 at 11:32, ALAN GAULD <alan.gauld at btinternet.com> wrote:
> You can do some pretty clever stuff with BAT files but frankly I wouldn't
> bother.
> Python can do almost all of the same things more elegantly and in most
> cases faster using fewer resources.
> Trying out a few loops and if/else type statements and writing a couple
> of test files may be an interesting diversion for an evening but the only
> real use of BAT files nowadays is to run a sequence of command
> line tools in one go. For anything else, use Python.

OK.

Dick

From modulok at gmail.com  Wed Nov  4 23:15:56 2009
From: modulok at gmail.com (Modulok)
Date: Wed, 4 Nov 2009 15:15:56 -0700
Subject: [Tutor] why is os.path.walk so slow?
In-Reply-To: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>
References: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>
Message-ID: <64c038660911041415p56f7d064i8f43fa0a8c9f0a80@mail.gmail.com>

[snip]
> I need to synchronize the files on my home and office machine and have
> been using someone else's code for this to date but have been
> frustrated by how slow it is in getting the information on files for
> the mounted drive from my office machine...
[/snip]

Not to cut your coding project short, and it may not even be
applicable, but have you looked into rsync? They kind of wrote the
book on efficiency in regards to synchronization of files.

Just a thought.
-Modulok-

From rh00667 at gmail.com  Wed Nov  4 23:07:52 2009
From: rh00667 at gmail.com (rh00667 at gmail.com)
Date: Wed, 4 Nov 2009 23:07:52 +0100
Subject: [Tutor] probando el email / mail test
Message-ID: <200911042207.nA4M7q40028314@bet.esrf.fr>

--- MENSAJE AUTOMATICO ---
Estoy probando mi lista de contactos. Pueden ignorar este msg.
Mis mejores deseos para todos.

--- AUTOMATIC MESSAGE ---
I'm testing my contact list. Please ignore this msg.
My best wishes for all.

From Garry.Willgoose at newcastle.edu.au  Thu Nov  5 00:36:15 2009
From: Garry.Willgoose at newcastle.edu.au (Garry Willgoose)
Date: Thu, 05 Nov 2009 10:36:15 +1100
Subject: [Tutor] why is os.path.walk so slow?
In-Reply-To: <64c038660911041415p56f7d064i8f43fa0a8c9f0a80@mail.gmail.com>
References: <0AB853B1-88A1-4479-9391-84E9F4292FD8@newcastle.edu.au>
	<64c038660911041415p56f7d064i8f43fa0a8c9f0a80@mail.gmail.com>
Message-ID: <EC8384BD-2471-4BF0-9D3D-78A7A5E0674B@newcastle.edu.au>


> [snip]
>> I need to synchronize the files on my home and office machine and  
>> have
>> been using someone else's code for this to date but have been
>> frustrated by how slow it is in getting the information on files for
>> the mounted drive from my office machine...
> [/snip]
>
> Not to cut your coding project short, and it may not even be
> applicable, but have you looked into rsync? They kind of wrote the
> book on efficiency in regards to synchronization of files.
>
> Just a thought.
> -Modulok-

It looks like rsync is ideal for what I want ... thanks for pointing  
this out. One less project to work on ;-)

To the other replies:

1. Both local and remote systems for the testing are OSX 10.5 and CPU  
was not limiting during the test.  Python was the Enthought binary  
distribution. Internet service is ADSL 1500/512.
2. I neglected to mention that I did comment out the code inside  
visitfunc() with the same result so the issue is inside walk().

I guess now I know abut rsync the pressing need for this is gone (I  
might still write a nice GUI for rsync ;-) but I'm still intrigued by  
the problem and whether its a latency vs bandwidth problem. The idea  
of writing a script to run on the remote end is good, and something I  
might use for another project.

From alan.gauld at btinternet.com  Thu Nov  5 01:38:05 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 5 Nov 2009 00:38:05 -0000
Subject: [Tutor] trouble using 2to3.py
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	<4AF03083.3070803@ieee.org>	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com><4AF05079.20006@timgolden.me.uk>
	<4AF0AEBE.3080501@ieee.org>
Message-ID: <hct6pg$1b4$1@ger.gmane.org>


"Dave Angel" <davea at ieee.org> wrote 

> But tell me, how does python.exe find its "home" directory, to set 
> initial sys.path and suchlike?  

Doesn't it look in the Registry?
I haven't checked but there are a bunch of registry entries for Python 
so I assume it checks those on startup.

Alan G


From wilcoxwork at gmail.com  Thu Nov  5 02:08:49 2009
From: wilcoxwork at gmail.com (Kristin Wilcox)
Date: Wed, 4 Nov 2009 17:08:49 -0800
Subject: [Tutor] parameters vs arguments
Message-ID: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>

I'm just starting to learn Python, and some of the vocabulary has me a bit
confused. I was hoping someone could help me out. People were so helpful
last time!

I'm reading stuff from multiple sources, and it seems to me like the words
"parameters" and "arguments" are used interchangeably. But I'm not sure if
this is a wrong perception, due to my lack of understanding, or if these
terms are truly synonyms.

When I define, say, function example like this...
def example(x,y):

are x and y arguments? or parameters?

And when I call the function and pass it values

example(32,17) are those values arguments or parameters? I *thought* this
was called 'passing arguments'...

I actually thought x and y would be referred to as arguments in both cases,
but then sometimes I think I hear them called parameters.

Help? Am I missing some nuances here?

Thanks for reading!

-Kris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091104/c635ceba/attachment.htm>

From kent37 at tds.net  Thu Nov  5 02:42:56 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 4 Nov 2009 20:42:56 -0500
Subject: [Tutor] parameters vs arguments
In-Reply-To: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
References: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
Message-ID: <1c2a2c590911041742u7c236ff0s91fb0a9f265b1621@mail.gmail.com>

On Wed, Nov 4, 2009 at 8:08 PM, Kristin Wilcox <wilcoxwork at gmail.com> wrote:
> I'm reading stuff from multiple sources, and it seems to me like the words
> "parameters" and "arguments" are used interchangeably. But I'm not sure if
> this is a wrong perception, due to my lack of understanding, or if these
> terms are truly synonyms.

Wikipedia has a nice description:
http://en.wikipedia.org/wiki/Parameter_%28computer_science%29#Parameters_and_arguments

Short view - technically, parameters are the variables in the function
and arguments are the values given to the variables at the point of
call. So outside the function, it is more common to talk about
arguments. Inside the function, you can really talk about either.
> When I define, say, function example like this...
> def example(x,y):
>
> are x and y arguments? or parameters?

Parameters that will take on the value of the arguments passed to the function.

> And when I call the function and pass it values
>
> example(32,17) are those values arguments or parameters? I *thought* this
> was called 'passing arguments'...

Arguments.

> I actually thought x and y would be referred to as arguments in both cases,
> but then sometimes I think I hear them called parameters.
>
> Help? Am I missing some nuances here?

Not really, it is not an important distinction.

Kent

From modulok at gmail.com  Thu Nov  5 05:58:15 2009
From: modulok at gmail.com (Modulok)
Date: Wed, 4 Nov 2009 21:58:15 -0700
Subject: [Tutor] parameters vs arguments
In-Reply-To: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
References: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
Message-ID: <64c038660911042058q3b9476eel9062f11a8537902d@mail.gmail.com>

[snip]
> When I define, say, function example like this...
> def example(x,y):
>
> are x and y arguments? or parameters?
>
> And when I call the function and pass it values
>
> example(32,17) are those values arguments or parameters? I *thought* this
> was called 'passing arguments'...
>
> I actually thought x and y would be referred to as arguments in both cases,
> but then sometimes I think I hear them called parameters.
>
> Help? Am I missing some nuances here?
[/snip]

For all practical intents and purposes, they're synonymous. But there
*is* a difference if you want to get technical about it.

As far as the terminology goes, it can depend on the language you're
using. Since a lot of programmers use more than one language, a lot of
terminology gets borrowed across language lines. Strictly speaking the
'parameter' is the variable which stores arbitrary data, that is
addressable within the function body. The specific data being passed,
is the 'argument'. For example:

def foo(arg):
   print arg

foo("spam and eggs")

The variable 'arg' in the function definition would the the
'parameter' and the value "spam and eggs" in the function call would
be an 'argument' to that parameter. ...strictly speaking. In reality,
some language texts may have a preference for one term over the other,
due to the author's background and previous languages they've used.
For all practical intents and purposes, they're synonymous and
frequently used interchangeably.

-Modulok-

From alan.gauld at btinternet.com  Thu Nov  5 08:08:18 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 5 Nov 2009 07:08:18 -0000
Subject: [Tutor] parameters vs arguments
References: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
Message-ID: <hcttl5$gdf$1@ger.gmane.org>


"Kristin Wilcox" <wilcoxwork at gmail.com> wrote

> I'm just starting to learn Python, and some of the vocabulary has me a 
> bit
> confused.

FWIW I take great care in my tutor to highlight and explain the terminology
of programming in my tutor. I italicise new words and explain them on first
use. So it might be useful taking a look there if you find areas of 
confusion.

> I'm reading stuff from multiple sources, and it seems to me like the 
> words
> "parameters" and "arguments" are used interchangeably.

This is a confusing one and people often do get them mixed up.
Basically a parameter is what you call the thing used in the function 
definition.
An argument is whats used  when you call the function.
Thus, at invocation, the parameter takes on the value of the argument.

This is confused futher by some text books using the term "formal argument"
instead of parameter. When you see "formal argument" it does just mean a
parameter. Fortunately this usage is rare nowadays.

> When I define, say, function example like this...
> def example(x,y):
>
> are x and y arguments? or parameters?

parameters

> And when I call the function and pass it values
>
> example(32,17) are those values arguments or parameters? I *thought* this
> was called 'passing arguments'...

arguments

HTH


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From denis.spir at free.fr  Thu Nov  5 10:39:09 2009
From: denis.spir at free.fr (spir)
Date: Thu, 5 Nov 2009 10:39:09 +0100
Subject: [Tutor] PolyRange -- iterator -- PS
In-Reply-To: <29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com>
References: <20091104173848.0b9dbc1b@o>
	<29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com>
Message-ID: <20091105103909.62eccacb@o>

Le Wed, 4 Nov 2009 18:17:21 +0100,
Hugo Arts <hugo.yoshi at gmail.com> s'exprima ainsi:

> Now, the code. If you write __iter__ as a generator, you won't have to
> write a next method at all. It simplifies The thing a whole lot:
> 
> def __iter__(self):
>     for range in self.ranges:
>         for item in range:
>             yield item
> 
> That's it. Alternatively, you could turn the whole thing into a
> one-liner and just return a generator expression from __iter__:
> 
> def __iter__(self):
>     return (item for r in self.ranges for item in r)
> 
> It's not as clear though, and it doesn't save that much space. I like
> the first one slightly better.

Thank you very much! That's exactly what I expected. Was sure my code was uselessly heavy. Actually, when reading the doc about iteration, I had wrongly understood that next() is required, too.

Now, I agree with you on your last comment... except that (.. for .. in ..) is precisely the syntax for generator expression (which is much more familiar to me than the use of generator funcs). Still, I will use the first idiom for clarity.

Two additional questions (relative to things manually implemented in my original code):
* What about memorization of "emptyness", meaning the last item is already reached, and following calls will all fail. This is automatic for generators, but...
* Then how do you restart it? With a decoupling of __iter__() and next(), it's possible to have both failure when empty for the same iterator (= call to next()), and 
a new iterator returned by __iter__(), typically for a new "for" statement. Below after a bug correction (attributes needing initialisation):
=======================
def testPolyRange():
    .......
    for i in pr1: print i,
    try:
        print pr1.next()
    except StopIteration,e:
        print "StopIteration"
    for i in pr1: print i,
==>
1 2 5 6 3 4 5 6 7 8 StopIteration
1 2 5 6 3 4 5 6 7 8
=======================

PS: Just checked and works as expected with generator.

Thank you again, Denis.
------
la vita e estrany



From sanelson at gmail.com  Thu Nov  5 11:06:47 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Thu, 5 Nov 2009 10:06:47 +0000
Subject: [Tutor] Making http posts
Message-ID: <b6131fdc0911050206j29cea9f7laaecbd86079f7d66@mail.gmail.com>

Hello,

I want to make an HTTP POST which will require authentication.

This is because we are using a web tool without an API, and we want a
programatic way of getting content into the tool.  Tech support of the
web tool have said we can just make a POST to the http endpoint.

>From the below source, it looks like I just post some text to
/project/4254/stories, with the ID of addStoryForm.

Is the best way to do this just a simple urllib script?

What's the best way to find out from the browser exactly what is being
posted when I use the web interface?  Tcpdump?  Or is the a better
tool?

S.

<a id="addStoryHandle" class="handle" href="#" onclick="return false"></a>
<div id="addStoryShade" class="shade">
	<div class="shadeHeader">
		<span id="actionHideAddStories">&laquo; Add Story</span>
	</div>
	<form id="addStoryForm" method="post" action="/project/4254/stories">
		<input type="hidden" name="hangImmediately" value="false"/>

		<input type="hidden" name="returnView" value="card"/>
		<div class="shadeSection">
			Text <span class="help">(1,024 characters max)</span>
			<textarea name="text" id="addStoryText" class="required"
maxlength="1024"></textarea>
		</div>
		<div class="shadeSection">
			Details <span class="help">(optional, supports <a
href="http://daringfireball.net/projects/markdown/syntax"
target="_blank">Markdown</a>)</span>

			<textarea name="details" id="addStoryDetails"></textarea>
		</div>
		<div class="shadeSection">
			Size <span class="help">(optional, 16 characters max)</span>
			<input type="text" name="size" maxlength="16"/>
		</div>
		<div class="shadeSection">

			Tags <span class="help">(separate by commas)</span>
			<input type="text" name="tags"/>
		</div>
		<div class="shadeSection">
			<button id="addStoryToBacklog" type="button">
				<img src="/content/images/icons/add.png"/>
				Add to Backlog
			</button>

			<button id="addStoryToBoard" type="button">
				<img src="/content/images/icons/pin.png"/>
				Hang on Board
			</button>
		</div>
	</form>
</div>

From hugo.yoshi at gmail.com  Thu Nov  5 12:19:28 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Thu, 5 Nov 2009 12:19:28 +0100
Subject: [Tutor] PolyRange -- iterator -- PS
In-Reply-To: <20091105103909.62eccacb@o>
References: <20091104173848.0b9dbc1b@o>
	<29179d160911040917v409c4fe5h8d6f2a052ae71c25@mail.gmail.com> 
	<20091105103909.62eccacb@o>
Message-ID: <29179d160911050319j3cb11596p74aa18e1f69fc8ad@mail.gmail.com>

On Thu, Nov 5, 2009 at 10:39 AM, spir <denis.spir at free.fr> wrote:
>
> Thank you very much! That's exactly what I expected. Was sure my code was uselessly heavy. Actually, when reading the doc about iteration, I had wrongly understood that next() is required, too.
>

This is actually correct. An iterator still requires a next method.
The nice thing about generator functions is that calling them creates
a generator object, which supplies the next method for you
automatically. As below:

>>> a = PolyRange((10, 20), (30, 40))
>>> iter(a)
<generator object at 0x7fe8092c38c0>
>>> b = iter(a)
>>> b.next()
10
>>> b.next()
11

etc. A generator expression does essentially the same thing.


> Two additional questions (relative to things manually implemented in my original code):
> * What about memorization of "emptyness", meaning the last item is already reached, and following calls will all fail. This is automatic for generators, but...
> * Then how do you restart it? With a decoupling of __iter__() and next(), it's possible to have both failure when empty for the same iterator (= call to next()), and
> a new iterator returned by __iter__(), typically for a new "for" statement. Below after a bug correction (attributes needing initialisation):
>
> <snip example>
>
> PS: Just checked and works as expected with generator.
>

Yes, every time you call iter(), a new generator object is created,
which works independently of other generators.

As an aside, I just thought of an even shorter implementation that
does not sacrifice clarity, using the itertools module:

#this imported at the top of the file
import itertools

def __iter__(self):
    return itertools.chain(*self.ranges)

HTH,
Hugo

From kent37 at tds.net  Thu Nov  5 13:33:52 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 5 Nov 2009 07:33:52 -0500
Subject: [Tutor] Making http posts
In-Reply-To: <b6131fdc0911050206j29cea9f7laaecbd86079f7d66@mail.gmail.com>
References: <b6131fdc0911050206j29cea9f7laaecbd86079f7d66@mail.gmail.com>
Message-ID: <1c2a2c590911050433g2aae1150g682a5a2d40af7001@mail.gmail.com>

On Thu, Nov 5, 2009 at 5:06 AM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
> Hello,
>
> I want to make an HTTP POST which will require authentication.

What kind of authentication? Basic auth is easy to use from Python;
form-based auth is a bit tricker.

> This is because we are using a web tool without an API, and we want a
> programatic way of getting content into the tool. ?Tech support of the
> web tool have said we can just make a POST to the http endpoint.
>
> >From the below source, it looks like I just post some text to
> /project/4254/stories, with the ID of addStoryForm.

IIRC the form ID is not part of the post. The text has to be formatted
as name=value pairs.

> Is the best way to do this just a simple urllib script?

urllib2. See my writeup here:
http://personalpages.tds.net/~kent37/kk/00010.html

> What's the best way to find out from the browser exactly what is being
> posted when I use the web interface? ?Tcpdump? ?Or is the a better
> tool?

There are Firefox addons for this. It's been a while so I'm not sure
which ones but I think either Firebug or Tamper Data will do it.

Kent

From mmadlavana at gmail.com  Thu Nov  5 14:16:47 2009
From: mmadlavana at gmail.com (Mkhanyisi Madlavana)
Date: Thu, 5 Nov 2009 15:16:47 +0200
Subject: [Tutor] Evaluating a string expression
Message-ID: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>

Hi Everyone,

I would like to know how would I evaluate a string expression in python.
For example, if i say:
>>> a = "3*2"
I want to do something to evaluate the variable 'a' to give me 6. How
can I do this?

Thanks
Mkhanyisi

From zstumgoren at gmail.com  Thu Nov  5 14:21:15 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Thu, 5 Nov 2009 08:21:15 -0500
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
Message-ID: <cadf44510911050521v7d940ee8jcedbd407ee716536@mail.gmail.com>

> I would like to know how would I evaluate a string expression in python.
> For example, if i say:
>>>> a = "3*2"
> I want to do something to evaluate the variable 'a' to give me 6. How
> can I do this?
>

I think the "eval" built-in function is what you're after:

>>> a = "3*2"
>>> eval(a)
6

http://docs.python.org/library/functions.html

From mail at timgolden.me.uk  Thu Nov  5 14:25:12 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Thu, 05 Nov 2009 13:25:12 +0000
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
Message-ID: <4AF2D238.3080209@timgolden.me.uk>

Mkhanyisi Madlavana wrote:
> Hi Everyone,
> 
> I would like to know how would I evaluate a string expression in python.
> For example, if i say:
>>>> a = "3*2"
> I want to do something to evaluate the variable 'a' to give me 6. How
> can I do this?

I'm afraid that your question doesn't pass the basic "Have I Googled it?"
test. I stuck "evaluate a string expression in python" into Google and got:

http://www.google.co.uk/#q=evaluate+a+string+expression+in+python

Does any of that help?

TJG

From mmadlavana at gmail.com  Thu Nov  5 14:22:49 2009
From: mmadlavana at gmail.com (Mkhanyisi Madlavana)
Date: Thu, 5 Nov 2009 15:22:49 +0200
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <cadf44510911050521v7d940ee8jcedbd407ee716536@mail.gmail.com>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
	<cadf44510911050521v7d940ee8jcedbd407ee716536@mail.gmail.com>
Message-ID: <52d606870911050522u556e4af7x12d815cffba72e74@mail.gmail.com>

Thanks!

2009/11/5 Serdar Tumgoren <zstumgoren at gmail.com>
>
> > I would like to know how would I evaluate a string expression in python.
> > For example, if i say:
> >>>> a = "3*2"
> > I want to do something to evaluate the variable 'a' to give me 6. How
> > can I do this?
> >
>
> I think the "eval" built-in function is what you're after:
>
> >>> a = "3*2"
> >>> eval(a)
> 6
>
> http://docs.python.org/library/functions.html

From zstumgoren at gmail.com  Thu Nov  5 14:28:12 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Thu, 5 Nov 2009 08:28:12 -0500
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <4AF2D238.3080209@timgolden.me.uk>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
	<4AF2D238.3080209@timgolden.me.uk>
Message-ID: <cadf44510911050528k20e14413j774b81fbde215a83@mail.gmail.com>

> I'm afraid that your question doesn't pass the basic "Have I Googled it?"
> test. I stuck "evaluate a string expression in python" into Google and got:
>
> http://www.google.co.uk/#q=evaluate+a+string+expression+in+python
>
That search turns up a useful tutorial by effbot:

http://effbot.org/zone/librarybook-core-eval.htm

It covers the basics of how to safely use eval if the data is coming
from an untrusted source. Might be worth a look depending on your use
case.

From davea at ieee.org  Thu Nov  5 15:58:07 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 05 Nov 2009 09:58:07 -0500
Subject: [Tutor] trouble using 2to3.py
In-Reply-To: <hct6pg$1b4$1@ger.gmane.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>	<4AF03083.3070803@ieee.org>	<d71c7ed60911030718l66051f50s7cd17599b82b2387@mail.gmail.com><4AF05079.20006@timgolden.me.uk>	<4AF0AEBE.3080501@ieee.org>
	<hct6pg$1b4$1@ger.gmane.org>
Message-ID: <4AF2E7FF.8020501@ieee.org>

Alan Gauld wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">
> "Dave Angel" <davea at ieee.org> wrote
>> But tell me, how does python.exe find its "home" directory, to set 
>> initial sys.path and suchlike?  
>
> Doesn't it look in the Registry?
> I haven't checked but there are a bunch of registry entries for Python 
> so I assume it checks those on startup.
>
> Alan G
>
>
>
Well, if it does, I must say I'm disappointed.  Perhaps there's still a 
simple explanation.  I expected the usual registry cruft left behind by 
the install process itself, and of course Windows requires various 
entries for things like uninstall.  And Windows adds things like LRU's 
in Explorer and elsewhere.  But looking through the registry I see 
hundreds of entries recording the various install directories for 
present and past python installs.

But I had hoped, given its portable roots, that Python's own registry 
use would be limited to things like assoc and ftype, which are used to 
find the associated python.exe, and that other dependencies would be 
resolved by python.exe itself, using its knowledge of its own location.


I have multiple installs of Python, and even  multiple installs of 2.6, 
so I suspect I'll be in trouble sooner or later, when it looks in the 
wrong directory tree for the version I thought I was running.  I 
expected the pythonNN.dll to be a conflict, but that's no problem; I use 
the latest one.  But what if I have ActivePython and Python.org installs 
of the exact same version?   I want to be able to switch back and forth 
for testing purposes, to know for sure whether I'm using features only 
available on the ActivePython version.

I tried once before, and again today, to search out information on just 
how this is managed.  But it seems all the docs I can find just describe 
how to use it, not how it's intended to work.

http://effbot.org/zone/python-register.htm
     uses sys.prefix    apparently to get the installation path of the 
currently running python

So, when I use that, it seems to track reliably the particular EXE being 
run, as I'd originally assumed.  But I haven't tried hardlinks, and I 
can't see how they'd preserve this behavior.

I hate not knowing. Too much "magic" going on.

DaveA

From zebra05 at gmail.com  Thu Nov  5 19:56:31 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Thu, 5 Nov 2009 20:56:31 +0200
Subject: [Tutor] Stolen thread: Bottom folk vs. toppers was trouble
	using2to3.py
In-Reply-To: <hcqa8o$o22$1@ger.gmane.org>
References: <d71c7ed60911030009o3af055dey2886f833abe56734@mail.gmail.com>
	<dfeb4470911030150h2bc135b9qb1f7d21e9c7b1a56@mail.gmail.com>
	<d71c7ed60911030215o1ef6e0efi47e3d92b612a7bad@mail.gmail.com>
	<4AF03083.3070803@ieee.org>
	<dfeb4470911031010y6552051em9048185e7e72d979@mail.gmail.com>
	<1257282750.29483.56.camel@bermanrl-desktop>
	<hcqa8o$o22$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911051056xe678dbdyd8665ff174dd4475@mail.gmail.com>

I'm afraid you guys are witty! Good on you. Rob, you're right - and Alan,
you're right too. BRA-VO.

On Wed, Nov 4, 2009 at 12:19 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> "Robert Berman" <bermanrl at cfl.rr.com> wrote
>
>  The war between bottom posters and top posters has been long, arduous,
>> and most often incredibly silly.
>>
>
> It has, I agree but there is a very real difference in that gratuitous top
> posting does actually become unreadable if not done very carefully.
> And it also encorages people to leave the whole previous thread in
> place which is very naughty for those who pay for ther internet
> access by the byte! (Or those whose mail server implements
> space quotas!)
>
> But if a general comment is being made that does not need to refer
> to specific quotations from the earlier rtext then I have no problem
> with top posting. Provided all but a small context quote is left below.
> But please don't send me 3 pages of text to just add two sentences
> at the top!!!
>
>
>  I simply propose that the only requirement to communications here is
>> that replies and questions be well formulated, courteous, and reasonably
>> intelligent.
>>
>
> Ah but there's the rub, what is "well formatted"? :-)
>
>
>  If there are going to be arguments pertaining to the posts, let them be
>> over content and not placement.
>>
>
> When you are trying to respond to hundreds of emails a day a
> few top posted messages can cause severe headaches. Personally I
> just ignore anything that has top posting that is hard to read, I don't
> have
> time to unscramble it nor to waste time asking the poster to desist.
> If they post scrambled mail it doesn't get answered...
>
> So its not quite a case of the "color of the bikeshed" because this one
> does make a very real difference to the usability of the medium and
> to the success of the message in reaching its full audience.
>
> In most cases it doesn't hurt much but in longer threads it does.
> So, please folks, be sensitive to your readers.
>
> Alan G.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091105/86a5d05c/attachment.htm>

From binto at triplegate.net.id  Fri Nov  6 01:22:13 2009
From: binto at triplegate.net.id (BINTO)
Date: Fri, 6 Nov 2009 07:22:13 +0700
Subject: [Tutor] parameters vs arguments
In-Reply-To: <hcttl5$gdf$1@ger.gmane.org>
References: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
	<hcttl5$gdf$1@ger.gmane.org>
Message-ID: <007601ca5e77$302f8420$908e8c60$@net.id>

Parameter is what used to give/call value in function.
Argument is what used to give/call value in command line interface.

Am I correct??...

Binto

-----Original Message-----
From: tutor-bounces+binto=triplegate.net.id at python.org
[mailto:tutor-bounces+binto=triplegate.net.id at python.org] On Behalf Of Alan
Gauld
Sent: Thursday, November 05, 2009 2:08 PM
To: tutor at python.org
Subject: Re: [Tutor] parameters vs arguments


"Kristin Wilcox" <wilcoxwork at gmail.com> wrote

> I'm just starting to learn Python, and some of the vocabulary has me a 
> bit
> confused.

FWIW I take great care in my tutor to highlight and explain the terminology
of programming in my tutor. I italicise new words and explain them on first
use. So it might be useful taking a look there if you find areas of 
confusion.

> I'm reading stuff from multiple sources, and it seems to me like the 
> words
> "parameters" and "arguments" are used interchangeably.

This is a confusing one and people often do get them mixed up.
Basically a parameter is what you call the thing used in the function 
definition.
An argument is whats used  when you call the function.
Thus, at invocation, the parameter takes on the value of the argument.

This is confused futher by some text books using the term "formal argument"
instead of parameter. When you see "formal argument" it does just mean a
parameter. Fortunately this usage is rare nowadays.

> When I define, say, function example like this...
> def example(x,y):
>
> are x and y arguments? or parameters?

parameters

> And when I call the function and pass it values
>
> example(32,17) are those values arguments or parameters? I *thought* this
> was called 'passing arguments'...

arguments

HTH


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 


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


From modulok at gmail.com  Fri Nov  6 01:54:31 2009
From: modulok at gmail.com (Modulok)
Date: Thu, 5 Nov 2009 17:54:31 -0700
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
Message-ID: <64c038660911051654k25348bc3r25b6e14055dc7988@mail.gmail.com>

[snip]
> I would like to know how would I evaluate a string expression in python.
> For example, if i say:
>>>> a = "3*2"
> I want to do something to evaluate the variable 'a' to give me 6. How
> can I do this?
[/snip]

The eval() function can do this:

   eval("3*2")

WARNING: Long winded security rant below...

Be *very* careful what strings you pass to eval(). It is executing
code! If you're doing this in a controlled environment it's not a
problem. If this is part of a bigger program which is going to be used
by other people, perhaps even online, this is a potentially *huge*
security risk. You will either have to very carefully parse the users
input to control what they can and cannot do, or better, strictly
control what the kernel permits the process to do. This includes what
hardware resources (memory/processor time) the process is allowed.
This way, even if (when) the process is hijacked, the damage will be
very limited.

Such a feat is accomplished by having the program execute as a user
who has very limited permissions. This is something best (only?) done
on UNIX/Linux/BSD flavored systems. This could be done via a setuid
binary, or a *carefully written* root process which immediately
demotes its privilege level upon execution/spawning of children. (Such
a model is employed by programs like apache's httpd server, where one
process is root owned and does nothing but spawn lesser privileged
processes to handle untrusted data.) If this is something you're
interested in, the os module features functions like, 'setuid()',
'setgid()', and notably 'chroot()'. For further security yet, you
might look into isolating a process from the rest of the system, as is
the case with FreeBSD's jails.

These are really big topics and in the end, it really depends on what
'untrusted source' constitutes, and your operating environment.
Writing bulletproof code in regards to security is challenging. It is
a very specialized topic worthy of further study. But in most
situations executing code from an untrusted source is a *really* bad
idea, even with precautions as those outlined in the example URL
provided by one of the other responses.
(http://effbot.org/zone/librarybook-core-eval.htm)

Sorry for all the lecture. I'll shut up now. :p
-Modulok-

From the_only_katala at verizon.net  Fri Nov  6 03:02:14 2009
From: the_only_katala at verizon.net (Katt)
Date: Thu, 05 Nov 2009 18:02:14 -0800
Subject: [Tutor] Retrieving information from a plain text
 file(WinXP/py2.6.2/Beginner)
References: <mailman.10273.1257192641.2806.tutor@python.org>
Message-ID: <975F7AD0774D4C98938E4501891E7A9D@COMPUTER01>


>> Hello all,
>>
>> Thank you all for your help.  I appreciate it alot.
>>
>> I have been trying to work with file IO alot recently and would like to
>> improve my little program so that I no longer use a hard coded list, but 
>> a
>> text file that I can edit easily.
>>
>> The text file is three lines long and looks exactly like this:
>>
>> Reminder1,2009_10_28
>> Reminder2,2009_11_01
>> Reminder3,2009_11_15
>>
>> My program consists of the following code:
>> ============
>> #]------------------[import modules]------------------[
>> from time import strftime, mktime, localtime
>> from WConio import textcolor
>> #]--------------------------------------------------------[
>> #]------------------[define functions]------------------[
>> def read_reminders():
>>   print "\nReading text file into program: reminders.txt"
>>   text_file = open("reminders.txt","r")
>>   reminders = [line.strip().split("'") for line in text_file]
>>   text_file.close()
>>   print reminders
>> #
>> def get_computer_date():
>>   #Get today's date from the computer
>>   todays_date = strftime("%Y_%m_%d")
>>   return todays_date
>> #
>> def color_print(strings):
>>   #Change the text color in the WinXP dos shell
>>   #The way to use:
>>   #color_print([("string",color number),\
>>   #(str(variable),color number),(etc)])
>>   for string in strings:
>>       textcolor(string[1])
>>       print string[0],
>> #
>> def change_to_julian(reminder_date):
>>   #Receives the year, month, and day
>>   #in the form of a single string (2009_10_15)
>>   #and changes it into three different int
>>   #variables.  Then take those three variables
>>   #and append six zeros and change into a
>>   #julian date.
>>   date = []
>>   date = reminder_date.split("_")
>>   year = int(date[0])
>>   month = int(date[1])
>>   day = int(date[2])
>>   timetuple = (year, month, day) + ( (0,) * 6 )
>>   unixtime = mktime(timetuple)
>>   timetuple = localtime(unixtime)
>>   print days_left(timetuple[7])
>>   # [7] is the number of julian-date field of
>>   #the unixtime tuple.
>>   return days_left(timetuple[7])
>> #
>> def days_left(julian_date):
>>   #This function calculates the days left
>>   #until a reminder.  If the days left are
>>   #greater than 0 it will print normally.
>>   #If it is -1 then it will print differently.
>>   #Also if it is greater than -1 it will print
>>   #yet again differently.
>>   days_until_reminder = julian_date - localtime().tm_yday
>>   if days_until_reminder > 0:
>>       color_print ([("There are",7),(str(days_until_reminder),4),("days
>> left until this reminder.",7),("\n",7)])
>>   elif days_until_reminder == -1:
>>       color_print ([("\tYou have missed this reminder
>> by",4),(str(days_until_reminder*-1),4),("day!",4),("\n",7)])
>>       color_print [("
>>  ------------------------------------------------------------------------",4),("\n",7)])
>>   else:
>>       color_print ([("\tYou have missed this reminder
>> by",4),(str(days_until_reminder*-1),4),("days!",4),("\n",7)])
>>       color_print [("
>>  ------------------------------------------------------------------------",4),("\n",7)])
>> print
>> #
>> def compare_reminders(todays_date):
>>   #This function compares the reminders
>>   #to the computer date.
>>   #It has three different paths:
>>   # 1.Matches today's date
>>   # 2.The reminder date has already
>>   #  passed by
>>   # 3.The reminder date is yet to
>>   #  come.
>>   #After determining which it is it will
>>   #access the change_to_julian and
>>   #days_left functions.
>>   #reminders.sort()
>>   color_print ([("
>> [-------------------------------------------------------------------------]",4),("\n",7)])
>>   index = 0
>>   while index < len(reminders):
>>       if todays_date == reminders[index][1]:
>>           print
>>           color_print [("
>>  ------------------------------------------------------------------------",4),("\n",7)])
>>           print "Today's reminder is:
>> ",reminders[index][0],"on",reminders[index][1]
>>           color_print ([("\t\tTake care of this reminder
>> immediately",2),("\n",7)])
>>       elif todays_date > reminders[index][1]:
>>           print
>>           print "Whoops, you missed the following
>> reminder.",reminders[index][0],"on",reminders[index][1]
>>           change_to_julian(reminders[index][1])
>>       else:
>>           print
>>           print "Your upcoming reminders are:
>> ",reminders[index][0],"on",reminders[index][1]
>>           change_to_julian(reminders[index][1])
>>       index = index + 1
>>   color_print ([("
>> [-------------------------------------------------------------------------]",4),("\n",7)])
>> #]--------------------------------------------------------[
>> #]-------------------[Main Program]-------------------[
>> read_reminders()
>> print reminders
>> compare_reminders(get_computer_date())
>> pause_it = raw_input("Press a key to end: ")
>> #]--------------------------------------------------------[
>> ============
>> Could someone explain to me why my read_reminders function retrieves the
>> information, but cannot process that information?
>>
>> When I try and run the program I get the following error message:
>> ============
>> Reading text file into program: reminders.txt
>> [['Reminder1,2010_10_15'], ['Reminder2,2010_11_01'],
>> ['Reminder3,2010_11_15']]
>> Traceback (most recent call last):
>>   File "reminders.py", line 182, in <module>
>>       print reminders
>> NameError: name 'reminders' is not defined
>> ============
>>
>> Thanks in advance for your help,
>>
>> Katt
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>
> #]-------------------[Main Program]-------------------[
> reminders = read_reminders()

This is the item that I was forgeting.  Originally I tried to use the 
"return" in my function, but left out what I was returning.  However if I 
did not notice this one line I would have continued to be stuck.  I guess I 
thought all you would have to do is call the function and it would know to 
make the return information into a global list.

===================================================
> It appears you did not return the list of reminders that you extracted
> in the "read_reminders" function, but simply printed them from inside
> that function.

Yes, originally I did this to find out how python would read the 
information.  Kind of an error check type thing.

> called "reminders", you  should be able to access the list in your
> global namespace.

If I hadn't paid attention to the end of Vince's post I would have not 
understood this as I am unfamiliar with some of the vocabulary (i.e.: global 
namespace).

> Also, on a side note, you can greatly improve the readability of your
> code by using the triple-quote style for multi-line docstrings inside
> functions (rather than the hash comment marks). I tend to use hash
> marks for one-line/inline comments, since they can really become an
> eyesore (at least IMHO) when used too liberally.
>
> Also, Python's whitespace and code formatting conventions can handle a
> lot of the "documentation" for you. For instance,  module imports are
> typically always performed at the top of a script, so it's reasonable
> to expect that others reading your code will understand you're
> importing some modules.
>
> Much of this spelled out in PEP's 8 (style guide) and 257 (doc strings):
>
> http://www.python.org/dev/peps/pep-0008/
> http://www.python.org/dev/peps/pep-0257/

I will make sure to check these out soon so that my comments are more 
readable.

==============================================

> (Looks like maybe you hijacked another thread, instead of just creating
> a new message, with new topic, for the list)

Sorry, about that.  I got lazy and just replied to a tutor message I had in 
my inbox.  Will make sure not to let that happen again.

> Once you get that sorted out, another bug that's already apparent is
> that you're trying to split the line on quotes, when it uses commas
> between fields on each line.

Missed that one.  Once I changed this everything clicked into place.

 Thank you Alan G.,Vince S., Serdar T., and Dave A. for your help.  I don't 
think I woud have got this on my own.  It is true that sometimes you can't 
see the answer when it is staring you in the face.

Thanks again,

Katt


From the_only_katala at verizon.net  Fri Nov  6 03:44:06 2009
From: the_only_katala at verizon.net (Katt)
Date: Thu, 05 Nov 2009 18:44:06 -0800
Subject: [Tutor] Change a text string from a list and change it into an
 integer number.(WinXP/py2.6.2/Beginner)
Message-ID: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>

Hello all,

I was wondering if it was possible to split a string that is seperated by 
the "_" character and changing the text into an integer?

My current code is as follows:

date = "cyear_11_05"
date2 = date.split("_")
check_year = date2[0]
if check_year == "cyear":
    year = localtime().tm_year
else:
    year = int(date2[0])
print year

So my goal here is for python to check at the value of "date".  If the value 
of "date[0]" is cyear then I want it to get the current year from the 
computer.  If the value of date[0] is a number then I want to just change it 
into an integer.

Currently the above code does not work unless I change the "if" statement to 
say:
 "if check_year == "c".

Did I do the slice incorrectly?  I thought that when you take the first 
location (0) of a list then it would take the "cyear" in stead of just the 
"c".

All input is appreciated.

Thanks in advance,

Katt


From binto at triplegate.net.id  Fri Nov  6 04:06:04 2009
From: binto at triplegate.net.id (BINTO)
Date: Fri, 6 Nov 2009 10:06:04 +0700
Subject: [Tutor] parameters vs arguments
In-Reply-To: <935630.15344.qm@web86706.mail.ird.yahoo.com>
References: <de4393b40911041708k1fc90c54gf7d8c1a800607c33@mail.gmail.com>
	<hcttl5$gdf$1@ger.gmane.org>
	<007501ca5e77$1275ce30$37616a90$@net.id>
	<935630.15344.qm@web86706.mail.ird.yahoo.com>
Message-ID: <000001ca5e8e$1436e7b0$3ca4b710$@net.id>

I understand now?

 

Thanks

Binto

 

From: ALAN GAULD [mailto:alan.gauld at btinternet.com] 
Sent: Friday, November 06, 2009 7:41 AM
To: BINTO
Subject: Re: [Tutor] parameters vs arguments

 

 

> Parameter is what used to give/call value in function.
> Argument is what used to give/call value in command line interface.
>
> Am I correct??...

No.

parameters are what you use when you create the function definition.

For example

 

def f(x):

    return 2*x

 

x is the parameter.

 

Now when I call f() I must pass in a value (or variable with a value). 

The thing I pass in when I call f() is the argument.

 

f(2)

 

2 becomes the argument for this particular call to f.

The parameter x of f takes on the value 2 during this particular invocation.

 

z = 7

f(z)

 

z is now the argument of f for this invocation.

The parameter x takes on the value 7, which is the value of the argument, z

 

x is always the parameter of f() but the argument can change 

each time you call f()

 

HTH,

 

Alan G.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/5c142ae0/attachment.htm>

From waynejwerner at gmail.com  Fri Nov  6 04:34:31 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Thu, 5 Nov 2009 21:34:31 -0600
Subject: [Tutor] Change a text string from a list and change it into an
	integer number.(WinXP/py2.6.2/Beginner)
In-Reply-To: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
References: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
Message-ID: <333efb450911051934x7839c69ek64f8435ae8ec6e99@mail.gmail.com>

On Thu, Nov 5, 2009 at 8:44 PM, Katt <the_only_katala at verizon.net> wrote:

> <snip>
> Currently the above code does not work unless I change the "if" statement
> to say:
> "if check_year == "c".
>
> Did I do the slice incorrectly?  I thought that when you take the first
> location (0) of a list then it would take the "cyear" in stead of just the
> "c".
>
>
It works correctly for me. Try modifying your code:

date = "cyear_11_05"
date2 = date.split("_")
check_year = date2[0]
print check_year

what does that do for you?
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091105/cbc14542/attachment.htm>

From anand.shashwat at gmail.com  Fri Nov  6 04:36:02 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Fri, 6 Nov 2009 09:06:02 +0530
Subject: [Tutor] Change a text string from a list and change it into an
	integer number.(WinXP/py2.6.2/Beginner)
In-Reply-To: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
References: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
Message-ID: <d4ab53de0911051936h2213d09eq59dd2bbc9637a519@mail.gmail.com>

What do you want to say exactly ?
is 'cyear' an integer ?
let's say date1 = "1984_11_05"

Then of course you can change it to an integer using following
list-comprehension,
>>> date1 = "1984_11_05"
>>> date1_list = [int(i) for i in date1.split("_")]
>>> date1_list
[1984, 11, 5]
or alternatively,
>>> date1_list_alternate=map(int,date1.split("_"))
>>> date1_list_alternate
[1984, 11, 5]


also your code seems to work on my system.

On Fri, Nov 6, 2009 at 8:14 AM, Katt <the_only_katala at verizon.net> wrote:

> Hello all,
>
> I was wondering if it was possible to split a string that is seperated by
> the "_" character and changing the text into an integer?
>
> My current code is as follows:
>
> date = "cyear_11_05"
> date2 = date.split("_")
> check_year = date2[0]
> if check_year == "cyear":
>   year = localtime().tm_year
> else:
>   year = int(date2[0])
> print year
>
> So my goal here is for python to check at the value of "date".  If the
> value of "date[0]" is cyear then I want it to get the current year from the
> computer.  If the value of date[0] is a number then I want to just change it
> into an integer.
>
> Currently the above code does not work unless I change the "if" statement
> to say:
> "if check_year == "c".
>
> Did I do the slice incorrectly?  I thought that when you take the first
> location (0) of a list then it would take the "cyear" in stead of just the
> "c".
>
> All input is appreciated.
>
> Thanks in advance,
>
> Katt
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/fb4e1059/attachment.htm>

From anand.shashwat at gmail.com  Fri Nov  6 04:57:46 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Fri, 6 Nov 2009 09:27:46 +0530
Subject: [Tutor] Change a text string from a list and change it into an
	integer number.(WinXP/py2.6.2/Beginner)
In-Reply-To: <d4ab53de0911051936h2213d09eq59dd2bbc9637a519@mail.gmail.com>
References: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
	<d4ab53de0911051936h2213d09eq59dd2bbc9637a519@mail.gmail.com>
Message-ID: <d4ab53de0911051957l7484deb8kef68fe3575c32ee1@mail.gmail.com>

import time

def katt(d):
    date0 = d.split("_")[0]
    if date0 == "cyear":
        return int(time.strftime("%Y"))
    else:
        return int(date0)

print katt("cyear_11_05")
print katt("1984_11_05")

l0nwlf-Arena:l0nwlf$ python katt.py
2009
1984

http://codepad.org/RBjKmNcA


Hope this helps !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/46ade018/attachment-0001.htm>

From alan.gauld at btinternet.com  Fri Nov  6 09:34:33 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 6 Nov 2009 08:34:33 -0000
Subject: [Tutor] Evaluating a string expression
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
	<64c038660911051654k25348bc3r25b6e14055dc7988@mail.gmail.com>
Message-ID: <hd0n2s$sbi$1@ger.gmane.org>

"Modulok" <modulok at gmail.com> wrote

>> I would like to know how would I evaluate a string expression in python.
>> For example, if i say:
>>>>> a = "3*2"
>> I want to do something to evaluate the variable 'a' to give me 6. How
>> can I do this?
> [/snip]
>
> The eval() function can do this:
>
>   eval("3*2")
>
> WARNING: Long winded security rant below...

And these are valid warnings which begs the question what are the 
alternatives?

If your string forms a well defined pattern you can parse the string into
its components - an arithmetic calculation in the example and execute it 
that way.
There are Python modules/tools available to help create such parsers and if
you are dealing with well defined input that is probably the safest 
approach.

Use eval() only if you know that the input cannot be malicious (or 
accidentally bad)
code.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Fri Nov  6 09:40:43 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 6 Nov 2009 08:40:43 -0000
Subject: [Tutor] Retrieving information from a plain text
	file(WinXP/py2.6.2/Beginner)
References: <mailman.10273.1257192641.2806.tutor@python.org>
	<975F7AD0774D4C98938E4501891E7A9D@COMPUTER01>
Message-ID: <hd0nef$tcl$1@ger.gmane.org>


"Katt" <the_only_katala at verizon.net> wrote

>> (Looks like maybe you hijacked another thread, instead of just creating
>> a new message, with new topic, for the list)
>
> Sorry, about that.  I got lazy and just replied to a tutor message I had 
> in my inbox.  Will make sure not to let that happen again.

Looks like it happened again :-)
This post shows up in my newsreader as being part of the thread about
Evaluating a String Expression.

In this case you should have used Reply tonyour original message
then it would have been added to that thread.

The reason this is important (from the posters point of view) is that if
your message goes into the back of an existing thread many readers
may not bother looking at it since they have lost interest in the older
thread. If you want your message to be seen post a new thread for a
new topic, and reply to an existing thread when deakling with that topic
(so  that only those following the thread need to read that one).
Its a win-win scenario :-)

Alan G 



From alan.gauld at btinternet.com  Fri Nov  6 09:44:51 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 6 Nov 2009 08:44:51 -0000
Subject: [Tutor] Change a text string from a list and change it into an
	integer number.(WinXP/py2.6.2/Beginner)
References: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
Message-ID: <hd0nm7$u4k$1@ger.gmane.org>


"Katt" <the_only_katala at verizon.net> wrote

> date = "cyear_11_05"
> date2 = date.split("_")
> check_year = date2[0]
> if check_year == "cyear":
>    year = localtime().tm_year
> else:
>    year = int(date2[0])
> print year
>
> Did I do the slice incorrectly?  I thought that when you take the first 
> location (0) of a list then it would take the "cyear" in stead of just 
> the "c".

When debugging this kind of thing insert some print statements
to check what date2 and check_year really look like. Or try using
the >>> prompt to experiment until you are happy with the behaviour
of the function with differernt sample data.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From davea at ieee.org  Fri Nov  6 14:28:45 2009
From: davea at ieee.org (Dave Angel)
Date: Fri, 06 Nov 2009 08:28:45 -0500
Subject: [Tutor] Change a text string from a list and change it into an
 integer number.(WinXP/py2.6.2/Beginner)
In-Reply-To: <hd0nm7$u4k$1@ger.gmane.org>
References: <E029571AB27D44579BFC78BDCFF04EFD@COMPUTER01>
	<hd0nm7$u4k$1@ger.gmane.org>
Message-ID: <4AF4248D.3070202@ieee.org>

Alan Gauld wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">
> "Katt" <the_only_katala at verizon.net> wrote
>
>> date = "cyear_11_05"
>> date2 = date.split("_")
>> check_year = date2[0]
>> if check_year == "cyear":
>>    year = localtime().tm_year
>> else:
>>    year = int(date2[0])
>> print year
>>
>> Did I do the slice incorrectly?  I thought that when you take the 
>> first location (0) of a list then it would take the "cyear" in stead 
>> of just the "c".
>
> When debugging this kind of thing insert some print statements
> to check what date2 and check_year really look like. Or try using
> the >>> prompt to experiment until you are happy with the behaviour
> of the function with differernt sample data.
>
> HTH,
>
>
Several things I'd add.

1) You forgot to include the line
      from time import localtime

2) You don't specify the python version or OS environment you're running 
on (though I don't think it matters here)

3) The example "works" as it is, meaning the localtime() function is 
called, and year is set to 2009 (when I run it today with CPython 
2.6.2)  So I'm guessing you retyped the example into your message.  
Always use copy/paste, and if practical, show the printed output results 
you got, or the error/traceback if it got an error.

4) You use the word "slice" in your query, but there are no slices in 
the program.  The line that binds check_year has a "subscript" 
operation, which is similar to a slice, but not the same.  The syntax is 
different, in that there's no colon in the square brackets.  And of 
course the meaning is different.



If I had to guess, I'd say that somewhere in your real code, you have a 
second subscript going on.  If you use a subscript on a list of strings, 
you get a string.  If you use a subscript on that, you get another 
string, consisting of a single character.


So if
date2 == ["cyear", "11", "05"]
date2[0] == "cyear"
date2[0][0] == "c"


DaveA

From kent37 at tds.net  Fri Nov  6 16:53:31 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 6 Nov 2009 10:53:31 -0500
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <hd0n2s$sbi$1@ger.gmane.org>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
	<64c038660911051654k25348bc3r25b6e14055dc7988@mail.gmail.com>
	<hd0n2s$sbi$1@ger.gmane.org>
Message-ID: <1c2a2c590911060753h65fa6db2g91f52cabfc574342@mail.gmail.com>

On Fri, Nov 6, 2009 at 3:34 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "Modulok" <modulok at gmail.com> wrote
>
>>> I would like to know how would I evaluate a string expression in python.
>>> For example, if i say:
>>>>>>
>>>>>> a = "3*2"
>>>
>>> I want to do something to evaluate the variable 'a' to give me 6. How
>>> can I do this?
>>
>> [/snip]
>>
>> The eval() function can do this:
>>
>> ?eval("3*2")
>>
>> WARNING: Long winded security rant below...
>
> And these are valid warnings which begs the question what are the
> alternatives?

Python 2.6 includes the ast.literal_eval() function which will
evaluate literal expressions:
http://docs.python.org/library/ast.html#ast.literal_eval

This is a bit too limited for the OP however.

The Python Cookbook has several examples of safe eval functions that
work by parsing an expression and evaluating the parse tree, only
allowing specific types of nodes. For example this one which does
allow arithmetic expressions:
http://code.activestate.com/recipes/286134/

Kent

From anand.shashwat at gmail.com  Fri Nov  6 17:30:40 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Fri, 6 Nov 2009 22:00:40 +0530
Subject: [Tutor] can time.time() be reversed so as to get date?
Message-ID: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>

If I have been given the number of seconds from midnight 1970.01.01 GMT, can
I calculate the date, month and time in the following format : 'Fri Nov 6
9:58:16 2009' ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/9691fe67/attachment.htm>

From anand.shashwat at gmail.com  Fri Nov  6 18:37:10 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Fri, 6 Nov 2009 23:07:10 +0530
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
Message-ID: <d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>

Problem resolved.

time.ctime(no_of_seconds - 330*60) does it.
330*60, because my time-zone is GMT+5:30 and time.ctime() gives the local
time while I wanted the GMT.

On Fri, Nov 6, 2009 at 10:00 PM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> If I have been given the number of seconds from midnight 1970.01.01 GMT,
> can I calculate the date, month and time in the following format : 'Fri Nov
> 6 9:58:16 2009' ?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/66fbe463/attachment.htm>

From timomlists at gmail.com  Fri Nov  6 18:53:05 2009
From: timomlists at gmail.com (Timo)
Date: Fri, 06 Nov 2009 18:53:05 +0100
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
Message-ID: <4AF46281.2040300@gmail.com>

Shashwat Anand schreef:
> If I have been given the number of seconds from midnight 1970.01.01 
> GMT, can I calculate the date, month and time in the following format 
> : 'Fri Nov 6 9:58:16 2009' ?
Yes, I think it can be done with the datetime module.

Timo

> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   


From vinces1979 at gmail.com  Fri Nov  6 18:55:15 2009
From: vinces1979 at gmail.com (vince spicer)
Date: Fri, 6 Nov 2009 11:55:15 -0600
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
Message-ID: <1e53c510911060955m78495ceue29f3a37da5ec418@mail.gmail.com>

On Fri, Nov 6, 2009 at 10:30 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> If I have been given the number of seconds from midnight 1970.01.01 GMT,
> can I calculate the date, month and time in the following format : 'Fri Nov
> 6 9:58:16 2009' ?
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Sure,

   you can get datetime from a teimstamp

EX:


from datetime import datetime
import time

now = time.time()

date = datetime.fromtimestamp(now)

print date.strftime("%a %b %d %H:%M:%S %Y")



Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/28a9904b/attachment.htm>

From emailkgnow at gmail.com  Fri Nov  6 18:56:06 2009
From: emailkgnow at gmail.com (Khalid Al-Ghamdi)
Date: Fri, 6 Nov 2009 20:56:06 +0300
Subject: [Tutor] problem importing the PIL and cPickle module
Message-ID: <dfac564f0911060956i58ac8253l19b3877589eafe2c@mail.gmail.com>

Hi,
I'm currently using python 3.1 and I tried to import PIL and cPickle as per
tutorial i was following, but both gave me errors. I tried to download PIL
from its respective site, but it only installed on the python 2.6
directory.
As for cPickle, I couldn't find it, but I found some kind of reference to
_pickle in the python documentation.

my questions:
is there a way to import PIL to python 3?
is _pickle the same as cPickle?

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/2267f89b/attachment-0001.htm>

From cdhallman at gmail.com  Fri Nov  6 20:37:16 2009
From: cdhallman at gmail.com (chris hallman)
Date: Fri, 6 Nov 2009 14:37:16 -0500
Subject: [Tutor] Python ICMP
Message-ID: <ecde30f10911061137y12ee6cc2pf137bf744cb886da@mail.gmail.com>

I need to create a class that sends ICMP packets from either a Windows or
Linux host. I found a few, but I like this one the best:


#!/usr/bin/env python


"""
    A pure python ping implementation using raw socket.



    Note that ICMP messages can only be sent from processes running as root.


    Derived from ping.c distributed in Linux's netkit. That code is
    copyright (c) 1989 by The Regents of the University of California.


    That code is in turn derived from code written by Mike Muuss of the
    US Army Ballistic Research Laboratory in December, 1983 and
    placed in the public domain. They have my thanks.

    Bugs are naturally mine. I'd be glad to hear about them. There are


    certainly word - size dependenceies here.

    Copyright (c) Matthew Dixon Cowles, <http://www.visi.com/~mdc/
<http://www.visi.com/%7Emdc/>>.
    Distributable under the terms of the GNU General Public License


    version 2. Provided with no warranties of any sort.

    Original Version from Matthew Dixon Cowles:
      -> ftp://ftp.visi.com/users/mdc/ping.py



    Rewrite by Jens Diemer:
      -> http://www.python-forum.de/post-69122.html#69122


    Revision history
    ~~~~~~~~~~~~~~~~



    May 30, 2007
    little rewrite by Jens Diemer:
     -  change socket asterisk import to a normal import
     -  replace time.time() with time.clock()
     -  delete "return None" (or change to "return" only)


     -  in checksum() rename "str" to "source_string"

    November 22, 1997
    Initial hack. Doesn't do much, but rather than try to guess
    what features I (or others) will want in the future, I've only


    put in what I need now.

    December 16, 1997
    For some reason, the checksum bytes are in the wrong order when
    this is run under Solaris 2.X for SPARC but it works right under
    Linux x86. Since I don't know just what's wrong, I'll swap the


    bytes always and then do an htons().

    December 4, 2000
    Changed the struct.pack() calls to pack the checksum and ID as
    unsigned. My thanks to Jerome Poincheval for the fix.


    Last commit info:


    ~~~~~~~~~~~~~~~~~
    $LastChangedDate: $
    $Rev: $
    $Author: $
"""


import os, sys, socket, struct, select, time



# From /usr/include/linux/icmp.h; your milage may vary.
ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris.




def checksum(source_string):
    """


    I'm not too confident that this is right but testing seems
    to suggest that it gives the same answers as in_cksum in ping.c
    """
    sum = 0


    countTo = (len(source_string)/2)*2


    count = 0
    while count<countTo:
        thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])


        sum = sum + thisVal
        sum = sum & 0xffffffff # Necessary?


        count = count + 2

    if countTo<len(source_string):


        sum = sum + ord(source_string[len(source_string) - 1])


        sum = sum & 0xffffffff # Necessary?



    sum = (sum >> 16)  +  (sum & 0xffff)


    sum = sum + (sum >> 16)


    answer = ~sum
    answer = answer & 0xffff

    # Swap bytes. Bugger me if I know why.


    answer = answer >> 8 | (answer << 8 & 0xff00)



    return answer


def receive_one_ping(my_socket, ID, timeout):


    """
    receive the ping from the socket.
    """
    timeLeft = timeout
    while True:


        startedSelect = time.clock()
        whatReady = select.select([my_socket], [], [], timeLeft)


        howLongInSelect = (time.clock() - startedSelect)


        if whatReady[0] == []: # Timeout


            return

        timeReceived = time.clock()


        recPacket, addr = my_socket.recvfrom(1024)
        icmpHeader = recPacket[20:28]


        type, code, checksum, packetID, sequence = struct.unpack(


            "bbHHh", icmpHeader
        )
        if packetID == ID:


            bytesInDouble = struct.calcsize("d")


            timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0]


            return timeReceived - timeSent

        timeLeft = timeLeft - howLongInSelect
        if timeLeft <= 0:


            return


def send_one_ping(my_socket, dest_addr, ID):


    """
    Send one ping to the given >dest_addr<.
    """
    dest_addr  =  socket.gethostbyname(dest_addr)



    # Header is type (8), code (8), checksum (16), id (16), sequence (16)
    my_checksum = 0



    # Make a dummy heder with a 0 checksum.
    header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)


    bytesInDouble = struct.calcsize("d")


    data = (192 - bytesInDouble) * "Q"


    data = struct.pack("d", time.clock()) + data



    # Calculate the checksum on the data and the dummy header.
    my_checksum = checksum(header + data)



    # Now that we have the right checksum, we put that in. It's just easier
    # to make up a new header than to stuff it into the dummy.


    header = struct.pack(
        "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1


    )
    packet = header + data
    my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1




def do_one(dest_addr, timeout):
    """


    Returns either the delay (in seconds) or none on timeout.
    """
    icmp = socket.getprotobyname("icmp")


    try:
        my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)


    except socket.error, (errno, msg):


        if errno == 1:
            # Operation not permitted


            msg = msg + (
                " - Note that ICMP messages can only be sent from processes"
                " running as root."


            )
            raise socket.error(msg)


        raise # raise the original error

    my_ID = os.getpid() & 0xFFFF



    send_one_ping(my_socket, dest_addr, my_ID)
    delay = receive_one_ping(my_socket, my_ID, timeout)



    my_socket.close()
    return delay




def verbose_ping(dest_addr, timeout = 2, count = 4):


    """
    Send >count< ping to >dest_addr< with the given >timeout< and display
    the result.
    """
    for i in xrange(count):


        print "ping %s..." % dest_addr,
        try:


            delay  =  do_one(dest_addr, timeout)
        except socket.gaierror, e:


            print "failed. (socket error: '%s')" % e[1]


            break

        if delay  ==  None:


            print "failed. (timeout within %ssec.)" % timeout


        else:
            delay  =  delay * 1000
            print "get ping in %0.4fms" % delay


    print


if __name__ == '__main__':


    verbose_ping("heise.de")
    verbose_ping("google.com")


    verbose_ping("a-test-url-taht-is-not-available.com")


    verbose_ping("192.168.1.1")


However, there is one small problem. It works on Windows, but not on Linux.
When I run this on Linux, the host sends the requests but never gets a
response. Not one. I've compared the output from both hosts in Wireshark and
the only difference I see is the identification field in the IP header.
Packets frmo Linux show 0 but packets from Windows have an incrementing
number.

I'd like to have this working on both. I prefer this code since it's pure
Python, doesn't shell out (popen) and I can call/import it into another
program.

Any ideas?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/7bb11f0c/attachment-0001.htm>

From taylan at nokta.com  Fri Nov  6 21:03:10 2009
From: taylan at nokta.com (Taylan Karaoglu)
Date: Fri, 06 Nov 2009 22:03:10 +0200
Subject: [Tutor] Python ICMP
In-Reply-To: <ecde30f10911061137y12ee6cc2pf137bf744cb886da@mail.gmail.com>
References: <ecde30f10911061137y12ee6cc2pf137bf744cb886da@mail.gmail.com>
Message-ID: <1257537790.16099.9.camel@iltaren-desktop>

Do you testing it with root account ? Linux wants root permission to
receive and normally send to icmp packets.

On Fri, 2009-11-06 at 14:37 -0500, chris hallman wrote:
> 
> I need to create a class that sends ICMP packets from either a Windows
> or Linux host. I found a few, but I like this one the best:
> 
> 
> #!/usr/bin/env python
> 
> 
>  
> """
>     A pure python ping implementation using raw socket.
>  
> 
>  
>     Note that ICMP messages can only be sent from processes running as root.
>  
>  
>     Derived from ping.c distributed in Linux's netkit. That code is
>     copyright (c) 1989 by The Regents of the University of California.
> 
> 
> 
>     That code is in turn derived from code written by Mike Muuss of the
>     US Army Ballistic Research Laboratory in December, 1983 and
>     placed in the public domain. They have my thanks.
>  
>     Bugs are naturally mine. I'd be glad to hear about them. There are
> 
> 
> 
>     certainly word - size dependenceies here.
>  
>     Copyright (c) Matthew Dixon Cowles, <http://www.visi.com/~mdc/>.
>     Distributable under the terms of the GNU General Public License
> 
> 
> 
>     version 2. Provided with no warranties of any sort.
>  
>     Original Version from Matthew Dixon Cowles:
>       -> ftp://ftp.visi.com/users/mdc/ping.py
> 
> 
>  
> 
>     Rewrite by Jens Diemer:
>       -> http://www.python-forum.de/post-69122.html#69122
>  
>  
>     Revision history
>     ~~~~~~~~~~~~~~~~
> 
> 
>  
> 
>     May 30, 2007
>     little rewrite by Jens Diemer:
>      -  change socket asterisk import to a normal import
>      -  replace time.time() with time.clock()
>      -  delete "return None" (or change to "return" only)
> 
> 
> 
>      -  in checksum() rename "str" to "source_string"
>  
>     November 22, 1997
>     Initial hack. Doesn't do much, but rather than try to guess
>     what features I (or others) will want in the future, I've only
> 
> 
> 
>     put in what I need now.
>  
>     December 16, 1997
>     For some reason, the checksum bytes are in the wrong order when
>     this is run under Solaris 2.X for SPARC but it works right under
>     Linux x86. Since I don't know just what's wrong, I'll swap the
> 
> 
> 
>     bytes always and then do an htons().
>  
>     December 4, 2000
>     Changed the struct.pack() calls to pack the checksum and ID as
>     unsigned. My thanks to Jerome Poincheval for the fix.
>  
>  
>     Last commit info:
> 
> 
> 
>     ~~~~~~~~~~~~~~~~~
>     $LastChangedDate: $
>     $Rev: $
>     $Author: $
> """
>  
>  
> import os, sys, socket, struct, select, time
> 
> 
> 
>  
> # From /usr/include/linux/icmp.h; your milage may vary.
> ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris.
> 
> 
> 
>  
>  
> def checksum(source_string):
>     """
> 
> 
> 
>     I'm not too confident that this is right but testing seems
>     to suggest that it gives the same answers as in_cksum in ping.c
>     """
>     sum = 0
> 
> 
> 
>     countTo = (len(source_string)/2)*2
> 
> 
> 
>     count = 0
>     while count<countTo:
>         thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
> 
> 
> 
>         sum = sum + thisVal
>         sum = sum & 0xffffffff # Necessary?
> 
> 
> 
>         count = count + 2
>  
>     if countTo<len(source_string):
> 
> 
> 
>         sum = sum + ord(source_string[len(source_string) - 1])
> 
> 
> 
>         sum = sum & 0xffffffff # Necessary?
> 
> 
> 
>  
>     sum = (sum >> 16)  +  (sum & 0xffff)
> 
> 
> 
>     sum = sum + (sum >> 16)
> 
> 
> 
>     answer = ~sum
>     answer = answer & 0xffff
>  
>     # Swap bytes. Bugger me if I know why.
> 
> 
> 
>     answer = answer >> 8 | (answer << 8 & 0xff00)
> 
> 
> 
>  
>     return answer
>  
>  
> def receive_one_ping(my_socket, ID, timeout):
> 
> 
> 
>     """
>     receive the ping from the socket.
>     """
>     timeLeft = timeout
>     while True:
> 
> 
> 
>         startedSelect = time.clock()
>         whatReady = select.select([my_socket], [], [], timeLeft)
> 
> 
> 
>         howLongInSelect = (time.clock() - startedSelect)
> 
> 
> 
>         if whatReady[0] == []: # Timeout
> 
> 
> 
>             return
>  
>         timeReceived = time.clock()
> 
> 
> 
>         recPacket, addr = my_socket.recvfrom(1024)
>         icmpHeader = recPacket[20:28]
> 
> 
> 
>         type, code, checksum, packetID, sequence = struct.unpack(
> 
> 
> 
>             "bbHHh", icmpHeader
>         )
>         if packetID == ID:
> 
> 
> 
>             bytesInDouble = struct.calcsize("d")
> 
> 
> 
>             timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0]
> 
> 
> 
>             return timeReceived - timeSent
>  
>         timeLeft = timeLeft - howLongInSelect
>         if timeLeft <= 0:
> 
> 
> 
>             return
>  
>  
> def send_one_ping(my_socket, dest_addr, ID):
> 
> 
> 
>     """
>     Send one ping to the given >dest_addr<.
>     """
>     dest_addr  =  socket.gethostbyname(dest_addr)
> 
> 
> 
>  
>     # Header is type (8), code (8), checksum (16), id (16), sequence (16)
>     my_checksum = 0
>  
> 
> 
> 
>     # Make a dummy heder with a 0 checksum.
>     header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
> 
> 
> 
>     bytesInDouble = struct.calcsize("d")
> 
> 
> 
>     data = (192 - bytesInDouble) * "Q"
> 
> 
> 
>     data = struct.pack("d", time.clock()) + data
> 
> 
> 
>  
>     # Calculate the checksum on the data and the dummy header.
>     my_checksum = checksum(header + data)
> 
> 
> 
>  
>     # Now that we have the right checksum, we put that in. It's just easier
>     # to make up a new header than to stuff it into the dummy.
> 
> 
> 
>     header = struct.pack(
>         "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
> 
> 
> 
>     )
>     packet = header + data
>     my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
> 
> 
> 
>  
>  
> def do_one(dest_addr, timeout):
>     """
> 
> 
> 
>     Returns either the delay (in seconds) or none on timeout.
>     """
>     icmp = socket.getprotobyname("icmp")
> 
> 
> 
>     try:
>         my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
> 
> 
> 
>     except socket.error, (errno, msg):
> 
> 
> 
>         if errno == 1:
>             # Operation not permitted
> 
> 
> 
>             msg = msg + (
>                 " - Note that ICMP messages can only be sent from processes"
>                 " running as root."
> 
> 
> 
>             )
>             raise socket.error(msg)
> 
> 
> 
>         raise # raise the original error
>  
>     my_ID = os.getpid() & 0xFFFF
> 
> 
> 
>  
>     send_one_ping(my_socket, dest_addr, my_ID)
>     delay = receive_one_ping(my_socket, my_ID, timeout)
> 
> 
> 
>  
>     my_socket.close()
>     return delay
> 
> 
>  
> 
>  
> def verbose_ping(dest_addr, timeout = 2, count = 4):
> 
> 
> 
>     """
>     Send >count< ping to >dest_addr< with the given >timeout< and display
>     the result.
>     """
>     for i in xrange(count):
> 
> 
> 
>         print "ping %s..." % dest_addr,
>         try:
> 
> 
> 
>             delay  =  do_one(dest_addr, timeout)
>         except socket.gaierror, e:
> 
> 
> 
>             print "failed. (socket error: '%s')" % e[1]
> 
> 
> 
>             break
>  
>         if delay  ==  None:
> 
> 
> 
>             print "failed. (timeout within %ssec.)" % timeout
> 
> 
> 
>         else:
>             delay  =  delay * 1000
>             print "get ping in %0.4fms" % delay
> 
> 
> 
>     print
>  
>  
> if __name__ == '__main__':
> 
> 
> 
>     verbose_ping("heise.de")
>     verbose_ping("google.com")
> 
> 
> 
>     verbose_ping("a-test-url-taht-is-not-available.com")
> 
> 
> 
>     verbose_ping("192.168.1.1")
> 
> However, there is one small problem. It works on Windows, but not on
> Linux. When I run this on Linux, the host sends the requests but never
> gets a response. Not one. I've compared the output from both hosts in
> Wireshark and the only difference I see is the identification field in
> the IP header. Packets frmo Linux show 0 but packets from Windows have
> an incrementing number.
> 
> I'd like to have this working on both. I prefer this code since it's
> pure Python, doesn't shell out (popen) and I can call/import it into
> another program.
> 
> Any ideas?
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



From davea at ieee.org  Fri Nov  6 21:14:05 2009
From: davea at ieee.org (Dave Angel)
Date: Fri, 06 Nov 2009 15:14:05 -0500
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
	<d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
Message-ID: <4AF4838D.1060609@ieee.org>

Shashwat Anand wrote:
> Problem resolved.
>
> time.ctime(no_of_seconds - 330*60) does it.
> 330*60, because my time-zone is GMT+5:30 and time.ctime() gives the local
> time while I wanted the GMT.
>
> On Fri, Nov 6, 2009 at 10:00 PM, Shashwat Anand <anand.shashwat at gmail.com>wrote:
>
>   
>> If I have been given the number of seconds from midnight 1970.01.01 GMT,
>> can I calculate the date, month and time in the following format : 'Fri Nov
>> 6 9:58:16 2009' ?
>>
>>     
You need to study the wording on the actual assignment.  There's no 
timezone conversion needed as stated, since the seconds are given in 
GMT, and you want GMT for the final date & time.  If you do convert to 
local time and back, you risk getting two kinds of errors:
    1) the time zone known to the system may differ from the one you 
have using the magic number 330, especially when the system changes to 
daylight savings time, and you forget to adjust your value.
    2) There is an hour of time in the fall or spring (I think it's in 
the fall) when a UTC time cannot be unambiguously represented as local 
time.  So if you convert to local and back, you'll get a different 
answer.  That's when the clocks get adjusted for daylight savings time.

So if you indeed want to go from epoch seconds GMT to printable time 
GMT, use the combination of  time.gmtime() and time.asctime().

No further comments, since you haven't posted any code.


DaveA

From qbits143 at gmail.com  Fri Nov  6 21:15:49 2009
From: qbits143 at gmail.com (Ajith Gopinath)
Date: Sat, 7 Nov 2009 01:45:49 +0530
Subject: [Tutor] pattern searching
Message-ID: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>

Hi,

How to find out all the occuerence of a particular pattern like  in a long
text where a capital letter in between two small letters ('aBa','dAd' etc..)
|| a j i t ||
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/38e74465/attachment.html>

From modulok at gmail.com  Fri Nov  6 21:35:17 2009
From: modulok at gmail.com (Modulok)
Date: Fri, 6 Nov 2009 13:35:17 -0700
Subject: [Tutor] Evaluating a string expression
In-Reply-To: <1c2a2c590911060753h65fa6db2g91f52cabfc574342@mail.gmail.com>
References: <52d606870911050516x4588746ewc537b9a9176faa24@mail.gmail.com>
	<64c038660911051654k25348bc3r25b6e14055dc7988@mail.gmail.com>
	<hd0n2s$sbi$1@ger.gmane.org>
	<1c2a2c590911060753h65fa6db2g91f52cabfc574342@mail.gmail.com>
Message-ID: <64c038660911061235g76d8e942i5200d8789a087afd@mail.gmail.com>

[snip]
>>>> I would like to know how would I evaluate a string expression in python.
>>>> For example, if i say:
>>>>>>>
>>>>>>> a = "3*2"
>>>>
>>>> I want to do something to evaluate the variable 'a' to give me 6. How
>>>> can I do this?
>>>
>>> The eval() function can do this:
>>>
>>>  eval("3*2")
>>>
>>> WARNING: Long winded security rant below...
...

>> And these are valid warnings which begs the question what are the
>> alternatives?
>
> Python 2.6 includes the ast.literal_eval() function which will
> evaluate literal expressions:
> http://docs.python.org/library/ast.html#ast.literal_eval
>
> This is a bit too limited for the OP however.
>
> The Python Cookbook has several examples of safe eval functions that
> work by parsing an expression and evaluating the parse tree, only
> allowing specific types of nodes. For example this one which does
> allow arithmetic expressions:
> http://code.activestate.com/recipes/286134/
>
> Kent
[/snip]

>From the article: http://code.activestate.com/recipes/286134/

"Also, it should be noted that a malicious user can still for example
cause the expression to take vast amounts of memory by inputting
something like '100100100100100**100...'. There is no way to really
prevent this from within Python, without making the expression
limitations too restrictive."

Just thinking aloud here for a moment: I wonder if it would be
reasonably possible to put the eval() step into a sub-process, with
the dispatcher process timing execution and killing the subprocess if
it consumes too much time/memory. ...of course the problem there, is
the sub-process runs at the same permission level, so if it is
hijacked it could potentially kill its parent first :S I think the
root-owned dispatcher, spawning lesser privileged processes, is the
only 'secure' way in regards to protecting the system from a denial of
service attack through an infinite variety of simply expressed, but
computationally intractable, expressions. The war between security and
ease of use (implementation in this case) wages onward.

-Modulok-

From modulok at gmail.com  Fri Nov  6 21:56:15 2009
From: modulok at gmail.com (Modulok)
Date: Fri, 6 Nov 2009 13:56:15 -0700
Subject: [Tutor] Python ICMP
In-Reply-To: <1257537790.16099.9.camel@iltaren-desktop>
References: <ecde30f10911061137y12ee6cc2pf137bf744cb886da@mail.gmail.com>
	<1257537790.16099.9.camel@iltaren-desktop>
Message-ID: <64c038660911061256l1e494f4cld31350432f69038a@mail.gmail.com>

[snip]
...
(code from OP)
...
[/snip]

> However, there is one small problem. It works on Windows, but not on Linux.
> When I run this on Linux, the host sends the requests but never gets a
> response. Not one. I've compared the output from both hosts in Wireshark and
> the only difference I see is the identification field in the IP header.
> Packets frmo Linux show 0 but packets from Windows have an incrementing
> number.
>
> I'd like to have this working on both. I prefer this code since it's pure
> Python, doesn't shell out (popen) and I can call/import it into another
> program.
>
> Any ideas?

I haven't read through the code, but something you might try if you
haven't already just to rule out other potential problems:

- Try to use the system command ping(8) from a command shell like bash
or tcsh, as the the same userID as your program will be running at:

   (tcsh shell prompt)> ping -c 3 www.google.com

If that doesn't get a response, you likely have a firewall issue.

Just a thought.
-Modulok-

From modulok at gmail.com  Fri Nov  6 21:58:57 2009
From: modulok at gmail.com (Modulok)
Date: Fri, 6 Nov 2009 13:58:57 -0700
Subject: [Tutor] pattern searching
In-Reply-To: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
Message-ID: <64c038660911061258k4d089371w216ff3adfb20517b@mail.gmail.com>

See the 're' module in the standard library. To quote the docs:

"This module ('re') provides regular expression matching operations
similar to those found in Perl. Both patterns and strings to be
searched can be Unicode strings as well as 8-bit strings."

You can find more information here: http://docs.python.org/library/re.html
-Modulok-

On 11/6/09, Ajith Gopinath <qbits143 at gmail.com> wrote:
> Hi,
>
> How to find out all the occuerence of a particular pattern like  in a long
> text where a capital letter in between two small letters ('aBa','dAd' etc..)
> || a j i t ||
>

From anand.shashwat at gmail.com  Fri Nov  6 22:52:06 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sat, 7 Nov 2009 03:22:06 +0530
Subject: [Tutor] pattern searching
In-Reply-To: <64c038660911061258k4d089371w216ff3adfb20517b@mail.gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
	<64c038660911061258k4d089371w216ff3adfb20517b@mail.gmail.com>
Message-ID: <d4ab53de0911061352w5111039bk8ce2a41f0899e855@mail.gmail.com>

Also you can try doing it using strings and this naive method:

>>> s = "aAabbnDeF"
>>> for i in range(1, len(s) - 1):
...     if s[i] in string.ascii_uppercase and s[i - 1] and s[i + 1] in
string.ascii_lowercase:
...         print "".join([s[i - 1], s[i], s[i + 1]])
...
aAa
nDe

It simple look for all capital letters and verify whether it adjacent
letters are small and print it.

On Sat, Nov 7, 2009 at 2:28 AM, Modulok <modulok at gmail.com> wrote:

> See the 're' module in the standard library. To quote the docs:
>
> "This module ('re') provides regular expression matching operations
> similar to those found in Perl. Both patterns and strings to be
> searched can be Unicode strings as well as 8-bit strings."
>
> You can find more information here: http://docs.python.org/library/re.html
> -Modulok-
>
> On 11/6/09, Ajith Gopinath <qbits143 at gmail.com> wrote:
> > Hi,
> >
> > How to find out all the occuerence of a particular pattern like  in a
> long
> > text where a capital letter in between two small letters ('aBa','dAd'
> etc..)
> > || a j i t ||
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/cff4db35/attachment.htm>

From rabidpoobear at gmail.com  Fri Nov  6 22:55:25 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Fri, 6 Nov 2009 15:55:25 -0600
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
	<d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
Message-ID: <dfeb4470911061355l2680cde4t43c88f0f67564c55@mail.gmail.com>

On Fri, Nov 6, 2009 at 11:37 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> Problem resolved.
>
> time.ctime(no_of_seconds - 330*60) does it.
> 330*60, because my time-zone is GMT+5:30 and time.ctime() gives the local
> time while I wanted the GMT.
>

Where do they use time zones that aren't a multiple of an hour?  That must
be incredibly confusing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/797b18f2/attachment.htm>

From anand.shashwat at gmail.com  Fri Nov  6 22:59:30 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sat, 7 Nov 2009 03:29:30 +0530
Subject: [Tutor] can time.time() be reversed so as to get date?
In-Reply-To: <dfeb4470911061355l2680cde4t43c88f0f67564c55@mail.gmail.com>
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com>
	<d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
	<dfeb4470911061355l2680cde4t43c88f0f67564c55@mail.gmail.com>
Message-ID: <d4ab53de0911061359i5a488c45hb826f85f4d1a5b25@mail.gmail.com>

Off-Topic :

http://en.wikipedia.org/wiki/Time_zone
7 / 33 time zones are not the multiple of hours.

On Sat, Nov 7, 2009 at 3:25 AM, Luke Paireepinart <rabidpoobear at gmail.com>wrote:

>
>
> On Fri, Nov 6, 2009 at 11:37 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:
>
>> Problem resolved.
>>
>> time.ctime(no_of_seconds - 330*60) does it.
>> 330*60, because my time-zone is GMT+5:30 and time.ctime() gives the local
>> time while I wanted the GMT.
>>
>
> Where do they use time zones that aren't a multiple of an hour?  That must
> be incredibly confusing.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/bb61102a/attachment.htm>

From kent37 at tds.net  Fri Nov  6 23:08:31 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 6 Nov 2009 17:08:31 -0500
Subject: [Tutor] problem importing the PIL and cPickle module
In-Reply-To: <dfac564f0911060956i58ac8253l19b3877589eafe2c@mail.gmail.com>
References: <dfac564f0911060956i58ac8253l19b3877589eafe2c@mail.gmail.com>
Message-ID: <1c2a2c590911061408p2fa9af97y1fd5742afd1bb044@mail.gmail.com>

On Fri, Nov 6, 2009 at 12:56 PM, Khalid Al-Ghamdi <emailkgnow at gmail.com> wrote:
> Hi,
> I'm currently using python 3.1 and I tried to import PIL and cPickle as per
> tutorial i was following, but both gave me errors. I tried to download PIL
> from its respective site, but it only installed on the python 2.6
> directory.

If you are using a Python 2 tutorial you will find it much easier to
use Python 2.6 instead of 3.1.

> As for cPickle, I couldn't find it, but I found some kind of reference to
> _pickle in the python documentation.
> my questions:
> is there a way to import PIL to python 3?

No, PIL is not available for python 3

> is _pickle the same as cPickle?

More or less, but it is used automatically when it is available. You
should just use pickle in Python 3.

Kent

From alan.gauld at btinternet.com  Sat Nov  7 01:54:33 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 00:54:33 -0000
Subject: [Tutor] can time.time() be reversed so as to get date?
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com><d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
	<4AF4838D.1060609@ieee.org>
Message-ID: <hd2ggd$m2g$1@ger.gmane.org>

"Dave Angel" <davea at ieee.org> wrote

> GMT, and you want GMT for the final date & time.  If you do convert to 
> local time and back, you risk getting two kinds of errors:
>    1) the time zone known to the system may differ from the one you 
> have using the magic number 330, especially when the system changes to 
> daylight savings time, and you forget to adjust your value.
>    2) There is an hour of time in the fall or spring (I think it's in 
> the fall) when a UTC time cannot be unambiguously represented as local 
> time.  

There are a myriad of problems involved in working with time zones.
I once had an undergraduate student on summer vacation do a study 
on it for me and he produced a paper of over 100 pages documenting 
something like 30 different issues he had uncovered. Most of those 
issues are still valid today some 15 years later.

If you are working in one timezone its not too bad (although that 
depends on which one!) but even then there are issues like the 
daylight savings switchover - when 1:30am can come after 1:45am!

And if you include local geographical and political factors it 
gets impossible. There is somewhere in the Pacific where the same 
street has two different timezones and the local government there
have chosen a different date to implement DST from everywhere 
else in either of the timezones! I once went on hioliday to a small 
European country where they delayed DST by a week because 
it would have spoiled the skiing! This was announced on public radio 
on the Wednesday before the clocks were due to change! How is a 
computer system supposeed to deal with that kind of arbitrary behaviour?!

And finally we represent times using a discrete numbering system 
but it is an essentially analog quantity with an arbitrary baseline, so 
there are always opportunities for error and inexactitude, especially 
with small values.

rant over,

Alan G.


From alan.gauld at btinternet.com  Sat Nov  7 01:59:07 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 00:59:07 -0000
Subject: [Tutor] can time.time() be reversed so as to get date?
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com><d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com>
	<dfeb4470911061355l2680cde4t43c88f0f67564c55@mail.gmail.com>
Message-ID: <hd2gp0$mis$1@ger.gmane.org>

"Luke Paireepinart" <rabidpoobear at gmail.com> wrote

> Where do they use time zones that aren't a multiple of an hour?  That 
> must
> be incredibly confusing.

Mainly Asia Pacidfic region. In fact I think even in Australia there is a 
difference
of 30 minutes between one of the northern states and one in the south.
Lance Hunt, is that still the case?

The scale of the problem is indicated by the fact that there are, I think,
37 different "official" timezones.

But there are also places which switch timezones rather than implement
a DST scheme. And other places with dual political oversight which have
dual timezones based on whichever political power you are aligned to.
I've never been there but I'm told places like railway stations have two
sets of clocks etc...

Alan G. 



From alan.gauld at btinternet.com  Sat Nov  7 02:00:43 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 01:00:43 -0000
Subject: [Tutor] can time.time() be reversed so as to get date?
References: <d4ab53de0911060830r6cf3bf38u501f5a26ff544e8d@mail.gmail.com><d4ab53de0911060937g3cd6f08en53bfc4a065a3274b@mail.gmail.com><dfeb4470911061355l2680cde4t43c88f0f67564c55@mail.gmail.com>
	<d4ab53de0911061359i5a488c45hb826f85f4d1a5b25@mail.gmail.com>
Message-ID: <hd2grv$muf$1@ger.gmane.org>

"Shashwat Anand" <anand.shashwat at gmail.com> wrote 

> http://en.wikipedia.org/wiki/Time_zone
> 7 / 33 time zones are not the multiple of hours.

OK, 33 not 37. The 37 may be the total we came up with to include 
all the abberations we uncovered!

Alan G.


From bgailer at gmail.com  Sat Nov  7 02:50:50 2009
From: bgailer at gmail.com (bob gailer)
Date: Fri, 06 Nov 2009 20:50:50 -0500
Subject: [Tutor] pattern searching
In-Reply-To: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
Message-ID: <4AF4D27A.5080802@gmail.com>

Ajith Gopinath wrote:
> Hi,
>
> How to find out all the occuerence of a particular pattern like  in a 
> long text where a capital letter in between two small letters 
> ('aBa','dAd' etc..)

The other proposals are all good. However if performance is a concern 
then I'd use string.maketran to create a translation table, then apply 
it to the text using translate, such that all lower case letters are 
translated to 'l', all upper case letters to 'u', then look for 'lul'.

import string
translationTable = string.maketrans(string.ascii_uppercase + 
string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
  start = translatedText.find('lul', start)
  if start >= 0:
    print text[start:start+3]
  else:
    break

Translate and find are both very fast.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From anand.shashwat at gmail.com  Sat Nov  7 03:12:34 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sat, 7 Nov 2009 07:42:34 +0530
Subject: [Tutor] pattern searching
In-Reply-To: <4AF4D27A.5080802@gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
	<4AF4D27A.5080802@gmail.com>
Message-ID: <d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com>

@Bob: the solution seems promising, and it's fast. Thanks for the
improvement. However I would like to do a minor change to the code to
prevent it going to infinite loop.

import string

text = raw_input()
translationTable = string.maketrans(string.ascii_uppercase +
string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
 start = translatedText.find('lul', start)
 if start >= 0:
  print text[start:start+3]
  start += 1
 else:
  break


On Sat, Nov 7, 2009 at 7:20 AM, bob gailer <bgailer at gmail.com> wrote:

> Ajith Gopinath wrote:
>
>> Hi,
>>
>> How to find out all the occuerence of a particular pattern like  in a long
>> text where a capital letter in between two small letters ('aBa','dAd' etc..)
>>
>
> The other proposals are all good. However if performance is a concern then
> I'd use string.maketran to create a translation table, then apply it to the
> text using translate, such that all lower case letters are translated to
> 'l', all upper case letters to 'u', then look for 'lul'.
>
> import string
> translationTable = string.maketrans(string.ascii_uppercase +
> string.ascii_lowercase, 'u'*26 + 'l'*26)
> translatedText = text.translate(translationTable)
> start = 0
> while True:
>  start = translatedText.find('lul', start)
>  if start >= 0:
>   print text[start:start+3]
>  else:
>   break
>
> Translate and find are both very fast.
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/2e4bdfe7/attachment.htm>

From bgailer at gmail.com  Sat Nov  7 03:19:59 2009
From: bgailer at gmail.com (bob gailer)
Date: Fri, 06 Nov 2009 21:19:59 -0500
Subject: [Tutor] pattern searching
In-Reply-To: <d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>	
	<4AF4D27A.5080802@gmail.com>
	<d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com>
Message-ID: <4AF4D94F.40704@gmail.com>

Shashwat Anand wrote:
> @Bob: the solution seems promising, and it's fast. Thanks for the 
> improvement. However I would like to do a minor change to the code to 
> prevent it going to infinite loop.
>
> import string
>
> text = raw_input()
> translationTable = string.maketrans(string.ascii_uppercase + 
> string.ascii_lowercase, 'u'*26 + 'l'*26)
> translatedText = text.translate(translationTable)
> start = 0
> while True:
>  start = translatedText.find('lul', start)
>  if start >= 0:
>   print text[start:start+3]
>   start += 1
>  else:
>   break
>
Good catch. My bad!

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From bibsmendez at gmail.com  Sat Nov  7 05:03:21 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Sat, 7 Nov 2009 07:03:21 +0300
Subject: [Tutor] Local vs global
Message-ID: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>

Hi gang,

Although i have read up quite a lot about local and global scope in
functions i still 'suffer' understanding it, i'm afraid. I have this
dragon's realm code modified to my liking or the way i know it. In the
original ebook there is no global variable but i have one on mine. I tried
removing it but i get error variable undeclared or something. It is in the
function choose_Cave.

I know that variables inside a function are local in scope and is gone after
the lifetime of the function, i mean after that function is called. That is
why there is the return statement. What i understand you can access whatever
that function did through the return statement.

I tried assigning the function to a variable outside the function definition
but still it doesnt work. Something like:

x = choose_Cave()
checkCave(x)

I'm slow in this programming thing haha but i want to learn. There are lots
to know in python and how i wish i have all the time. Unfortunately i have
my day job too and first things first. So I'm just managing my time. Below
is the code. Thanks.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Tutorial book Invent Your Own Computer Games with Python 2nd Edition
Author: Al Sweigart
Chapter 6: Dragon Realm
Filename: dragon.py
Created: 02-Nov-2009
edited: 06-Nov-2009; added functions
'''

import random
import time

location = '/home/bboymen/pyscripts/invent-with-Python/intro.txt'
file = open(location)
intro = file.read()
file.close()

def choose_Cave():
    global choose
    choose = raw_input('choose a cave! (1 or 2): ')
    while choose != '1' and choose != '2':  #(a)
        print('enter 1 or 2 only.')
        choose = raw_input('try again: ')
    return choose

def checkCave(chosenCave):
    print('you approach the cave...')
    time.sleep(2)
    print('it is dark and spooky...')
    time.sleep(2)
    print('a large dragon jumps over you! he open his jaws and....')
    time.sleep(2)
    friendlyCave = random.randint(1, 2)
    if chosenCave == str(friendlyCave):
        print('gives you his treasure!')
    else:
        print('gobbles you down in one bite!')

print(intro)
choose_Cave()
checkCave(choose)
while True:
    ask = raw_input('play again? (y/[N])')
    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
    if ask.lower() in reply:
        print "\n", intro
        choose_Cave()
        checkCave(choose)
    else:
        break

print('thanks for playing, goodbye!')


#(a) boolean operator `and` will evaluate 2 boolean values (to its
left and right)
#    and return a single boolean value



-- 
Best Regards,
bibimidi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/5f864cb4/attachment-0001.htm>

From qbits143 at gmail.com  Sat Nov  7 09:13:23 2009
From: qbits143 at gmail.com (Ajith Gopinath)
Date: Sat, 7 Nov 2009 13:43:23 +0530
Subject: [Tutor] pattern searching
In-Reply-To: <4AF4D94F.40704@gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com> 
	<4AF4D27A.5080802@gmail.com>
	<d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com> 
	<4AF4D94F.40704@gmail.com>
Message-ID: <f23da4ca0911070013x79064997x5478cb3351aa4188@mail.gmail.com>

Thanks folks.

|| a j i t ||


On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer at gmail.com> wrote:

> Shashwat Anand wrote:
>
>> @Bob: the solution seems promising, and it's fast. Thanks for the
>> improvement. However I would like to do a minor change to the code to
>> prevent it going to infinite loop.
>>
>> import string
>>
>> text = raw_input()
>> translationTable = string.maketrans(string.ascii_uppercase +
>> string.ascii_lowercase, 'u'*26 + 'l'*26)
>> translatedText = text.translate(translationTable)
>> start = 0
>> while True:
>>  start = translatedText.find('lul', start)
>>  if start >= 0:
>>  print text[start:start+3]
>>  start += 1
>>  else:
>>  break
>>
>>  Good catch. My bad!
>
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/07aaea55/attachment.htm>

From tmatsumoto at gmx.net  Sat Nov  7 09:18:34 2009
From: tmatsumoto at gmx.net (C.T. Matsumoto)
Date: Sat, 07 Nov 2009 09:18:34 +0100
Subject: [Tutor] Classes that do operator overloading
Message-ID: <4AF52D5A.1060107@gmx.net>

Hello All,

I'm reading Learning Python's section 'Operator Overloading' and I was 
wondering why
class headers that implement and overload are lowercase?

Cheers,

T

From alan.gauld at btinternet.com  Sat Nov  7 10:16:37 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 09:16:37 -0000
Subject: [Tutor] Local vs global
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
Message-ID: <hd3dtq$dj1$1@ger.gmane.org>


"bibi midi" <bibsmendez at gmail.com> wrote in

> Although i have read up quite a lot about local and global scope in
> functions i still 'suffer' understanding it, ... I tried
> removing it but i get error variable undeclared or something. It is in 
> the
> function choose_Cave.

We need to see the exact error text, the whole error.
Python error messages are very informative and usually
tell you exactly what is wrong and where (once you get used to them).

> I know that variables inside a function are local in scope and is gone 
> after
> the lifetime of the function, i mean after that function is called. That 
> is
> why there is the return statement. What i understand you can access 
> whatever
> that function did through the return statement.

Specifically you can access the values that the return exposes,
you cannot access anything else from the function body.

> I tried assigning the function to a variable outside the function 
> definition
> but still it doesnt work. Something like:
>
> x = choose_Cave()
> checkCave(x)

That should work
So what happened? How did the program run and what error did you get?

> location = '/home/bboymen/pyscripts/invent-with-Python/intro.txt'
> file = open(location)
> intro = file.read()
> file.close()
>
> def choose_Cave():
>    global choose
>    choose = raw_input('choose a cave! (1 or 2): ')
>    while choose != '1' and choose != '2':  #(a)
>        print('enter 1 or 2 only.')
>        choose = raw_input('try again: ')
>    return choose

You can lose the global line at the top, you are no longer
modifying the global value you are returning the local one.

> def checkCave(chosenCave):
>    print('you approach the cave...')
>    time.sleep(2)
>    print('it is dark and spooky...')
>    time.sleep(2)
>    print('a large dragon jumps over you! he open his jaws and....')
>    time.sleep(2)
>    friendlyCave = random.randint(1, 2)
>    if chosenCave == str(friendlyCave):
>        print('gives you his treasure!')
>    else:
>        print('gobbles you down in one bite!')

> print(intro)
> choose_Cave()

you are not assigning the return to a value

choose=choose_Cave()

> checkCave(choose)
> while True:
>    ask = raw_input('play again? (y/[N])')
>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>    if ask.lower() in reply:
>        print "\n", intro
>        choose_Cave()
>        checkCave(choose)
>    else:
>        break

You could tidy that up by moving the first lines inside the loop:

> while True:
>    print "\n", intro
>    choose = choose_Cave()
>    checkCave(choose)
>    ask = raw_input('play again? (y/[N])')
>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>    if ask.lower() not in reply:
>          break

HTH,

Alan G. 



From alan.gauld at btinternet.com  Sat Nov  7 10:17:55 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 09:17:55 -0000
Subject: [Tutor] Classes that do operator overloading
References: <4AF52D5A.1060107@gmx.net>
Message-ID: <hd3e08$dn8$1@ger.gmane.org>


"C.T. Matsumoto" <tmatsumoto at gmx.net> wrote 

> I'm reading Learning Python's section 'Operator Overloading' and I was 
> wondering why class headers that implement and overload are lowercase?

I'm not sure what you mean by class headers?
Can you post an example?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From tmatsumoto at gmx.net  Sat Nov  7 10:39:37 2009
From: tmatsumoto at gmx.net (C.T. Matsumoto)
Date: Sat, 07 Nov 2009 10:39:37 +0100
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <hd3e08$dn8$1@ger.gmane.org>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org>
Message-ID: <4AF54059.6000000@gmx.net>

yes,

class Foo: # the book says this is a class header
    pass

As for my question it looks like the convention is if a class only has
operator overloading then the class receives a lowercase class name.
If the class has a mix, operator overloading and a normal method then
the class name gets starts with a capital.

It's just a detail, but I wanted to know.

T
Alan Gauld wrote:
>
> "C.T. Matsumoto" <tmatsumoto at gmx.net> wrote
>> I'm reading Learning Python's section 'Operator Overloading' and I 
>> was wondering why class headers that implement and overload are 
>> lowercase?
>
> I'm not sure what you mean by class headers?
> Can you post an example?
>
>


From hugo.yoshi at gmail.com  Sat Nov  7 12:16:20 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Sat, 7 Nov 2009 12:16:20 +0100
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <4AF54059.6000000@gmx.net>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org> 
	<4AF54059.6000000@gmx.net>
Message-ID: <29179d160911070316ja27b22eu3bd215a683c75c75@mail.gmail.com>

On Sat, Nov 7, 2009 at 10:39 AM, C.T. Matsumoto <tmatsumoto at gmx.net> wrote:
> yes,
>
> class Foo: # the book says this is a class header
> ? pass
>
> As for my question it looks like the convention is if a class only has
> operator overloading then the class receives a lowercase class name.
> If the class has a mix, operator overloading and a normal method then
> the class name gets starts with a capital.
>
> It's just a detail, but I wanted to know.
>

class names should always be capitalized, no matter what kind of
methods they have.
The exceptions to this are the built-in types (int, str, list, dict,
etc.). But if you're writing a class yourself,
capitalize it.

quoting PEP 8:

      Almost without exception, class names use the CapWords convention.
      Classes for internal use have a leading underscore in addition.

if you want to know something, anything at all about style
conventions, read PEP 8. It's the definitive python styleguide.

http://www.python.org/dev/peps/pep-0008/

Hugo

From tmatsumoto at gmx.net  Sat Nov  7 12:25:12 2009
From: tmatsumoto at gmx.net (C.T. Matsumoto)
Date: Sat, 07 Nov 2009 12:25:12 +0100
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <29179d160911070316ja27b22eu3bd215a683c75c75@mail.gmail.com>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org>
	<4AF54059.6000000@gmx.net>
	<29179d160911070316ja27b22eu3bd215a683c75c75@mail.gmail.com>
Message-ID: <4AF55918.30502@gmx.net>

Thanks Hugo,

Do methods like __add__, __del__, count as built-in types? I'm aware of the
rule you explained and use it and that's why when  I saw:

class indexer():
     def ___getitem__(self, index):
         return index ** 2

I thought I was missing some special style, or rule. The class above is take
from Learning Python, and there are several other examples too.

Thanks,

T
Hugo Arts wrote:
> On Sat, Nov 7, 2009 at 10:39 AM, C.T. Matsumoto <tmatsumoto at gmx.net> wrote:
>   
>> yes,
>>
>> class Foo: # the book says this is a class header
>>   pass
>>
>> As for my question it looks like the convention is if a class only has
>> operator overloading then the class receives a lowercase class name.
>> If the class has a mix, operator overloading and a normal method then
>> the class name gets starts with a capital.
>>
>> It's just a detail, but I wanted to know.
>>
>>     
>
> class names should always be capitalized, no matter what kind of
> methods they have.
> The exceptions to this are the built-in types (int, str, list, dict,
> etc.). But if you're writing a class yourself,
> capitalize it.
>
> quoting PEP 8:
>
>       Almost without exception, class names use the CapWords convention.
>       Classes for internal use have a leading underscore in addition.
>
> if you want to know something, anything at all about style
> conventions, read PEP 8. It's the definitive python styleguide.
>
> http://www.python.org/dev/peps/pep-0008/
>
> Hugo
>
>   


From hugo.yoshi at gmail.com  Sat Nov  7 12:35:47 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Sat, 7 Nov 2009 12:35:47 +0100
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <4AF55918.30502@gmx.net>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org> 
	<4AF54059.6000000@gmx.net>
	<29179d160911070316ja27b22eu3bd215a683c75c75@mail.gmail.com> 
	<4AF55918.30502@gmx.net>
Message-ID: <29179d160911070335y102d7142h8b156d1592378187@mail.gmail.com>

On Sat, Nov 7, 2009 at 12:25 PM, C.T. Matsumoto <tmatsumoto at gmx.net> wrote:
> Thanks Hugo,
>
> Do methods like __add__, __del__, count as built-in types? I'm aware of the
> rule you explained and use it and that's why when ?I saw:
>

Built-in types are only those classes 'built in' to the python
interpreter. They include int, float, str, list, dict, tuple, and
others. Every class you define yourself (and also most classes in the
standard library) should have a Capitalized class name, no matter what
kind of methods you define for it.

> class indexer():
> ? ?def ___getitem__(self, index):
> ? ? ? ?return index ** 2
>
> I thought I was missing some special style, or rule. The class above is take
> from Learning Python, and there are several other examples too.
>

following the PEP 8 guidelines, that class should have been named Indexer.

From mail at timgolden.me.uk  Sat Nov  7 12:46:21 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Sat, 07 Nov 2009 11:46:21 +0000
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <29179d160911070335y102d7142h8b156d1592378187@mail.gmail.com>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org>
	<4AF54059.6000000@gmx.net>	<29179d160911070316ja27b22eu3bd215a683c75c75@mail.gmail.com>
	<4AF55918.30502@gmx.net>
	<29179d160911070335y102d7142h8b156d1592378187@mail.gmail.com>
Message-ID: <4AF55E0D.9030902@timgolden.me.uk>

Hugo Arts wrote:
> On Sat, Nov 7, 2009 at 12:25 PM, C.T. Matsumoto <tmatsumoto at gmx.net> wrote:
>> class indexer():
>>    def ___getitem__(self, index):
>>        return index ** 2
>>
>> I thought I was missing some special style, or rule. The class above is take
>> from Learning Python, and there are several other examples too.
>>
> 
> following the PEP 8 guidelines, that class should have been named Indexer.


It's worth stressing that word *guideline*: if you're contributing to
the Python core, PEP 8 is de rigueur; another library or project might
have different standards, more or less derived from PEP 8. If you're
writing your own code, you can do whatever you like. The advantage of
following PEP 8 is that there's one less barrier for someone (including
you, later) following your code. But if you fancy lowercase classes,
use lowercase classses. :)

TJG

From davea at ieee.org  Sat Nov  7 13:26:15 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 07 Nov 2009 07:26:15 -0500
Subject: [Tutor] Local vs global
In-Reply-To: <hd3dtq$dj1$1@ger.gmane.org>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org>
Message-ID: <4AF56767.5080205@ieee.org>

Alan Gauld wrote:
> "bibi midi" <bibsmendez at gmail.com> wrote in
>
>> Although i have read up quite a lot about local and global scope in
>> functions i still 'suffer' understanding it, ... I tried
>> removing it but i get error variable undeclared or something. It is 
>> in the
>> function choose_Cave.
>
> We need to see the exact error text, the whole error.
> Python error messages are very informative and usually
> tell you exactly what is wrong and where (once you get used to them).
>
>> I know that variables inside a function are local in scope and is 
>> gone after
>> the lifetime of the function, i mean after that function is called. 
>> That is
>> why there is the return statement. What i understand you can access 
>> whatever
>> that function did through the return statement.
>
> Specifically you can access the values that the return exposes,
> you cannot access anything else from the function body.
>
>> I tried assigning the function to a variable outside the function 
>> definition
>> but still it doesnt work. Something like:
>>
>> x = choose_Cave()
>> checkCave(x)
>
> That should work
> So what happened? How did the program run and what error did you get?
>
>> location = '/home/bboymen/pyscripts/invent-with-Python/intro.txt'
>> file = open(location)
>> intro = file.read()
>> file.close()
>>
>> def choose_Cave():
>>    global choose
>>    choose = raw_input('choose a cave! (1 or 2): ')
>>    while choose != '1' and choose != '2':  #(a)
>>        print('enter 1 or 2 only.')
>>        choose = raw_input('try again: ')
>>    return choose
>
> You can lose the global line at the top, you are no longer
> modifying the global value you are returning the local one.
>
>> def checkCave(chosenCave):
>>    print('you approach the cave...')
>>    time.sleep(2)
>>    print('it is dark and spooky...')
>>    time.sleep(2)
>>    print('a large dragon jumps over you! he open his jaws and....')
>>    time.sleep(2)
>>    friendlyCave = random.randint(1, 2)
>>    if chosenCave == str(friendlyCave):
>>        print('gives you his treasure!')
>>    else:
>>        print('gobbles you down in one bite!')
>
>> print(intro)
>> choose_Cave()
>
> you are not assigning the return to a value
>
> choose=choose_Cave()
>
>> checkCave(choose)
>> while True:
>>    ask = raw_input('play again? (y/[N])')
>>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>>    if ask.lower() in reply:
>>        print "\n", intro
>>        choose_Cave()
>>        checkCave(choose)
>>    else:
>>        break
>
> You could tidy that up by moving the first lines inside the loop:
>
>> while True:
>>    print "\n", intro
>>    choose = choose_Cave()
>>    checkCave(choose)
>>    ask = raw_input('play again? (y/[N])')
>>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>>    if ask.lower() not in reply:
>>          break
>
> HTH,
>
> Alan G.
>
>
Alan - Excellent comments, as usual.

bibimidi - I would point out that after you remove the 'global choose' line as Alan said, you should rename the global variable you're using to store the return value.  They're allowed to have the same name, but it's confusing to a beginner if they do, since you might assume they somehow share the value.

So I'd do something like:

while True:
    ....
    my_cave = choose_Cave()
    checkCave(my_cave)
    ....



Incidentally, as a matter of terminology, you have lots of 'global 
variables' in this code.  location, file, intro, ask and reply are all 
global.  If it were my code, I'd probably move all that logic to one 
more function, call it main().  And the only top-level code would be a 
call to main().

One more important thing.  You used 'file' as a variable name, but it 
already has a meaning in Python, as a built-in type for file 
operations.  No problem in this script, but it's a good practice to 
avoid names in built-ins, as well as names used by stdlib imports  (os, 
sys, math, random, time, ...)

DaveA


From alan.gauld at btinternet.com  Sat Nov  7 15:06:15 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 7 Nov 2009 14:06:15 -0000
Subject: [Tutor] Classes that do operator overloading
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org>
	<4AF54059.6000000@gmx.net>
Message-ID: <hd3uta$nda$1@ger.gmane.org>

"C.T. Matsumoto" <tmatsumoto at gmx.net> wrote 

> class Foo: # the book says this is a class header
>    pass

Hmm, that's pretty dubious usage of header in my view. 
Its the class definition and there is nothing "header" about it.

> As for my question it looks like the convention is if a class only has
> operator overloading then the class receives a lowercase class name.

Hugo and Tim have answered that. Pep 8 says no, but local 
standards may over-rule that.

It is worth pointing out however that classes which only have 
operator overloads in them are pretty rare! Especially ones 
that don't inherit from an existing type or class.

The Indexer example is not very typical, a pseudo list 
where the content that is apparently always twice the index.
I can see it being used to save space where the 
alternative would be a list comprehension like:

myIndexer = [2*n for n in range(bugnum)]

It would also guarantee that you never had an index out of range.

But personally even there I'd generalise it by passing in a function
so that the Indexer could return any function of the index:

class Indexer:
     def __init__(self, func = lambda n: n):
          self.func = func
     def __getitem__(self, index)
          return self.func(index)

doubles = Indexer(lambda n: n*2)
squares = Indexer(lambda n: n*n)
plus3s = Indexer(lambda n: n+3)

etc.

But its certainly not exactly a typical scenario.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From surjitkhakh at gmail.com  Sat Nov  7 01:24:57 2009
From: surjitkhakh at gmail.com (surjit khakh)
Date: Fri, 6 Nov 2009 16:24:57 -0800
Subject: [Tutor] can anyone help me in solving this problem this is urgent
Message-ID: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>

Write a python program to read a text file named ?text.txt? and show the
number
of times each article is found in the file. Articles in the English language
are the
words ?a?, ?an?, and ?the?.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/80bcf50e/attachment.htm>

From anand.shashwat at gmail.com  Sat Nov  7 17:20:20 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sat, 7 Nov 2009 21:50:20 +0530
Subject: [Tutor] can anyone help me in solving this problem this is
	urgent
In-Reply-To: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>
References: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>
Message-ID: <d4ab53de0911070820k22892e58l9fe6c26360b277ec@mail.gmail.com>

naive and unoptimized method:

>>> file = open("text.txt", 'r')
>>> s = file.read()
>>> s
'an elephant jump across the room\nhe met a guy\nthe guy was an moron\n'
>>> s = " "+s.replace('\n', ' ')+" "
>>> s.count(' a ')
1
>>> s.count(' an ')
2
>>> s.count(' the ')
2


On Sat, Nov 7, 2009 at 5:54 AM, surjit khakh <surjitkhakh at gmail.com> wrote:

> Write a python program to read a text file named ?text.txt? and show the
> number
> of times each article is found in the file. Articles in the English
> language are the
> words ?a?, ?an?, and ?the?.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/b0a28c3a/attachment.htm>

From waynejwerner at gmail.com  Sat Nov  7 18:10:21 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Sat, 7 Nov 2009 11:10:21 -0600
Subject: [Tutor] class Knights vs class Knights(object)
Message-ID: <333efb450911070910w58c4b720u73ee5ed85d07986a@mail.gmail.com>

Hi,

For class definitions I've always used

class Knights:

but I've just seen an example using:

class Knights(object):

So I did a quick little test and see this:

>>> a = Knights()
>>> b = Knights2()
>>> a
<__main__.Knights instance at 0xb7e12bec>
>>> b
<__main__.Knights2 object at 0xb7e12b2c>

and my question is what is the difference between the two? Is there a
difference other than one is an object the other is an instance? I googled
"python object vs. instance" and didn't find anything terribly useful.

Thanks,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091107/37926312/attachment.htm>

From emile at fenx.com  Sat Nov  7 19:04:31 2009
From: emile at fenx.com (Emile van Sebille)
Date: Sat, 07 Nov 2009 10:04:31 -0800
Subject: [Tutor] can anyone help me in solving this problem this is
	urgent
In-Reply-To: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>
References: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>
Message-ID: <hd4co1$a1h$1@ger.gmane.org>

On 11/6/2009 4:24 PM surjit khakh said...
> Write a python program to read a text file named ?text.txt? and show the 
> number
> of times each article is found in the file. Articles in the English 
> language are the
> words ?a?, ?an?, and ?the?.
> 

Sounds like you're taking a python class.  Great!  It's probably the 
best programming language to start with.

First, it helps when asking questions if you mention what version of the 
language you're using.  Some features and options are newer.  In 
particular, there's a string method 'count' that isn't available in 
older pythons, while the replace method has been around at least ten years.

If you haven't already, the tutorial at 
http://docs.python.org/tutorial/index.html is a great place to start. 
Pay particular attention to section 3's string introduction at 
http://docs.python.org/tutorial/introduction.html#strings and section 7 
starting with 
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
on files.

Implicit in this problem is identifying words in the text file.  This is 
tough because you need to take punctuation into account.  There's a neat 
tool in newer pythons such that, assuming you've read the file contents 
into a variable txt, allows you to say set(txt) to get all the letters, 
numbers, punctuation marks, and any other whitespace type characters 
embedded in the content.  You'll need to know these so that you can 
recognize the word regardless of adjacent punctuation.  In this specific 
case, as articles in English always precede nouns you'll always find 
whitespace following an article.  It would be a space except, of course, 
when the article ends the line and line wrap characters are included in 
the text file.

For example, consider the following text:

"""
SECTION 1.4. COUNTY PLANNING COMMISSION.

a. The County Planning Commission shall consist of five members. Each 
member of the Board of Supervisors shall recommend that a resident of 
his district be appointed to the Commission; provided, however, the 
appointments to the Commission shall require the affirmative vote of not 
less than a majority of the entire membership of the Board.
"""

Any a's, an's or the's in the paragraph body can be easily counted with 
the string count method once you properly prepared the text.

I expect the an's and the's are the easy ones to count.  Consider 
however the paragraph identifier -- "a." -- this is not an article but 
would likely be counted as one in most solutions.  There may also be a 
subsequent reference to this section (eg, see a above) or range of 
sections (eg, see a-n above) that further make this a harder problem. 
One possible approach may involve confirming the a noun follows the 
article.  There are dictionaries you can access, or word lists that can 
help.  The WordNet database from Princeton appears fairly complete with 
117k entries, but even there it's easy to find exceptions: "A 20's style 
approach"; "a late bus"; or "a fallen hero".

So, frankly, I expect that solutions to this problem will range from the 
naive through the reasonably complete to the impossible without human 
confirmation of complex structure and context.

For your homework, showing you can read in the file, strip out any 
punctuation, count the resulting occurances, and report the results 
should do it.

Emile


From patrick.just4fun at gmail.com  Sat Nov  7 21:35:36 2009
From: patrick.just4fun at gmail.com (Patrick Sabin)
Date: Sat, 07 Nov 2009 21:35:36 +0100
Subject: [Tutor] class Knights vs class Knights(object)
In-Reply-To: <333efb450911070910w58c4b720u73ee5ed85d07986a@mail.gmail.com>
References: <333efb450911070910w58c4b720u73ee5ed85d07986a@mail.gmail.com>
Message-ID: <4AF5DA18.9050202@gmail.com>

Wayne Werner wrote:

> and my question is what is the difference between the two? Is there a 
> difference other than one is an object the other is an instance? I 
> googled "python object vs. instance" and didn't find anything terribly 
> useful.

Yes there is a difference. One class inherits from object, the other 
doesn't. You may want to try to check this, e.g.:

issubclass(Knight, object)

So the first keyword you may want to google for is inheritance.

The second keyword is old/new-style classes.

Python 2 changed the way how classes work, but to be backward compatible 
the old mechanism still remained. If you want new style classes inherit
from object.

If you want to understand the details you may want to look up what 
metaclasses - your third keyword - are, old-style classes have the 
metaclass classobj, new-style classes type. You can check this using the 
builtin type-function.

Beware that this is python 2 stuff. In python 3 class X: and class 
X(object): are the same.

- Patrick

From alan.gauld at btinternet.com  Sun Nov  8 02:17:20 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 8 Nov 2009 01:17:20 -0000
Subject: [Tutor] can anyone help me in solving this problem this is
	urgent
References: <e98d364d0911061624o51327a7q1c07b61f157ab366@mail.gmail.com>
Message-ID: <hd59fm$en5$1@ger.gmane.org>

"surjit khakh" <surjitkhakh at gmail.com> wrote

> Write a python program to read a text file named ?text.txt? and show the
> number of times each article is found in the file.

In general we don't provide answers to homework questions on this list.
But we will try to point you in the right direction. But it helps if you 
show
us what you've tried so far and tell us what went wrong. Include any error
messages too.

But the point of homework is to learn by doing, and you won't learn
anything if we just tell you the answer.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From beachkid at insightbb.com  Sun Nov  8 03:53:46 2009
From: beachkid at insightbb.com (Ken G.)
Date: Sat, 07 Nov 2009 21:53:46 -0500
Subject: [Tutor] Printing Sideway...
Message-ID: <4AF632BA.3050304@insightbb.com>

It is possible to print letters sideway in Python? 

Instead of printing from left to right on the long side of
a #10 envelope, I wish to print sideway, printing from the
left short edge of envelope to its right short edge.

Thanks,

Ken

From bibsmendez at gmail.com  Sun Nov  8 04:20:48 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Sun, 8 Nov 2009 06:20:48 +0300
Subject: [Tutor] Local vs global
In-Reply-To: <88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>
	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>
	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>
Message-ID: <f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>

On Sat, Nov 7, 2009 at 3:26 PM, Dave Angel <davea at ieee.org> wrote:
> Alan Gauld wrote:
>>
> Alan - Excellent comments, as usual.
>
> bibimidi - I would point out that after you remove the 'global choose'
line
> as Alan said, you should rename the global variable you're using to store
> the return value.  They're allowed to have the same name, but it's
confusing
> to a beginner if they do, since you might assume they somehow share the
> value.
>

Thank you all for the helpful insights. I will keep in mind the naming
rules in Python, so as not to have it in collision with Python's
builtins.

The mention to tidy up e.g. "compartmentalize" code is also noted. I
agree definitely.

I'm reworking my code and will post back.




--
Best Regards

Marie von Ebner-Eschenbach  - "Even a stopped clock is right twice a
day." -
http://www.brainyquote.com/quotes/authors/m/marie_von_ebnereschenbac.html



-- 
Best Regards,
bibimidi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/4f422aea/attachment.htm>

From bgailer at gmail.com  Sun Nov  8 04:25:25 2009
From: bgailer at gmail.com (bob gailer)
Date: Sat, 07 Nov 2009 22:25:25 -0500
Subject: [Tutor] Printing Sideway...
In-Reply-To: <4AF632BA.3050304@insightbb.com>
References: <4AF632BA.3050304@insightbb.com>
Message-ID: <4AF63A25.1060204@gmail.com>

Ken G. wrote:
> It is possible to print letters sideway in Python?

Python is a programming language, not a printer driver.

So this is not the best place to ask the question.

But tell us more -

  what OS? (Windows or what)
  what printer? (Make and model)
  what are you doing in Python to print the envelope now?

> Instead of printing from left to right on the long side of
> a #10 envelope, I wish to print sideway, printing from the
> left short edge of envelope to its right short edge.


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From prachit915 at gmail.com  Sun Nov  8 05:08:09 2009
From: prachit915 at gmail.com (Prasad Mehendale)
Date: Sun, 8 Nov 2009 09:38:09 +0530
Subject: [Tutor] can anyone help me in solving this problem this is
	urgent
In-Reply-To: <mailman.196.1257646419.2872.tutor@python.org>
References: <mailman.196.1257646419.2872.tutor@python.org>
Message-ID: <200911080938.09971.microteacher@gmail.com>

On Sunday 08 November 2009 07:43 am, tutor-request at python.org wrote:

>
> In general we don't provide answers to homework questions on this list.
> But we will try to point you in the right direction. But it helps if you
> show  us what you've tried so far and tell us what went wrong. Include any 
error
> messages too.
>
> But the point of homework is to learn by doing, and you won't learn
> anything if we just tell you the answer.

I fully agree with Alan....however, I was impressed by the way Emile has shown 
the direction. Thanks.

--prasad mehendale
================================================================

From bgailer at gmail.com  Sun Nov  8 05:23:01 2009
From: bgailer at gmail.com (bob gailer)
Date: Sat, 07 Nov 2009 23:23:01 -0500
Subject: [Tutor] Printing Sideway...
In-Reply-To: <4AF63E83.6020705@insightbb.com>
References: <4AF632BA.3050304@insightbb.com> <4AF63A25.1060204@gmail.com>
	<4AF63E83.6020705@insightbb.com>
Message-ID: <4AF647A5.4070700@gmail.com>

Please always reply-all so a copy goes to the list. I am ccing this to 
the lists for you.

Ken G. wrote:
> I am using Ubuntu 9.04 as my primary OS.  I have Windows XP
> installed in the first partition (dual boot).  I have Python
> 2.6.2 installed on both OS.  The printer is a Cannon MX300.
> To print on the envelope, I use Windows Works v4.5 which is
> easy to use.  I just learned how to print via Open Office
> Writer 3.0.1 which took several hours to do properly.
>
> If it is not possible to print sideways, I understand.

That depends on the software (Windows Works / Open Office) and the printer.

How do you communicate from Python to these programs?
>
>>
>>> Instead of printing from left to right on the long side of
>>> a #10 envelope, I wish to print sideway, printing from the
>>> left short edge of envelope to its right short edge.
>>
>>
>


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From denis.spir at free.fr  Sat Nov  7 19:01:29 2009
From: denis.spir at free.fr (spir)
Date: Sat, 7 Nov 2009 19:01:29 +0100
Subject: [Tutor] operator overloading considered harmful ;-)
Message-ID: <20091107190129.45fd1fd7@o>

Hello,

I jump on the opportunity offered by the previous thread to tell a little story (serious only people disgard). Guess this is a candidate for Bug of the Year.

I was filtering (parse) results to keep only nodes from a given set of types:

for child in node:
    if child.pattern in item_types:
        items.append(child)
    else:       # recursion
        ...

For a weird reason, _all_ child nodes were collected. So, I printed out node types at start of loop, but all was ok. After a while wondering, I printed patterns in the if branch, too -- which seemed to myself completely irrational:

for child in node:
    print child.pattern
    if child.pattern in item_types:
        print "*** type OK\t%s" %child.pattern
        items.append(child)
    else:       # recursion
        ...

This gave me results such as:
digit::[0..9] 
*** type OK     section::header body footer

The second expression is the one of another pattern in play during this test. Well, had a hard time finding out how the pattern, or its expression, could change between both prints. Actually, the reasons were (hope you're confortable):
* "section" is a recursive pattern (some items in the body can be sections)
* I had overloaded "==" in the top Pattern type as a syntactic sugar to name patterns.

Both together mean that, when the "in" test is performed, any child is (1) renamed and (2)returned (for the renaming to work), like a false result to the "==" comparison, so that the assertion always seems True.
[I have long considered this rule of python logic a flaw, but it's only me]
Actually, the logical relation is a bit obscured because, for me, patterns where compared for identity, not equality.

The funny joke here is that this trick for naming patterns is not necessary in normal use, only handy for testing. So that, probably, I'm the only one who could step on the bug.
For the little story, I did not find the solution, rather it fell into my head from who-knows-where while walking in the street on the way to market ;-)

Denis
------
la vita e estrany



From denis.spir at free.fr  Sat Nov  7 19:12:15 2009
From: denis.spir at free.fr (spir)
Date: Sat, 7 Nov 2009 19:12:15 +0100
Subject: [Tutor] Classes that do operator overloading
In-Reply-To: <hd3uta$nda$1@ger.gmane.org>
References: <4AF52D5A.1060107@gmx.net> <hd3e08$dn8$1@ger.gmane.org>
	<4AF54059.6000000@gmx.net> <hd3uta$nda$1@ger.gmane.org>
Message-ID: <20091107191215.12ccb566@o>

Le Sat, 7 Nov 2009 14:06:15 -0000,
"Alan Gauld" <alan.gauld at btinternet.com> s'exprima ainsi:

> "C.T. Matsumoto" <tmatsumoto at gmx.net> wrote 
> 
> > class Foo: # the book says this is a class header
> >    pass  
> 
> Hmm, that's pretty dubious usage of header in my view. 
> Its the class definition and there is nothing "header" about it.

English is terrible. From wiktionary (abstracts)/

head
	The topmost, foremost, or leading part. 

header
	The upper portion of a page (or other) layout.
	Text, or other visual information, used to mark off a quantity of text, often titling or summarizing it.
	Text, or other visual information, that goes at the top of a column of information in a table.

headline
	A heading or title of an article.

heading
	The title or topic of a document, article, chapter etc.


Denis
------
la vita e estrany



From alan.gauld at btinternet.com  Sun Nov  8 09:59:12 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 8 Nov 2009 08:59:12 -0000
Subject: [Tutor] Classes that do operator overloading
References: <4AF52D5A.1060107@gmx.net>
	<hd3e08$dn8$1@ger.gmane.org><4AF54059.6000000@gmx.net>
	<hd3uta$nda$1@ger.gmane.org> <20091107191215.12ccb566@o>
Message-ID: <hd6195$oih$1@ger.gmane.org>


"spir" <denis.spir at free.fr> wrote

> English is terrible. From wiktionary (abstracts)/

Indeed. and the fact that computer Scientists often abuse English
makes it worse!

> header
> Text, or other visual information, used to mark off a quantity of text, 
> often titling or summarizing it.

This is probaly the closest to the usual CS definition.

A class header in C++, Delphi, Objective C and some other
languages is separate to its definition and provides a summary
or statement of the class interface. The implementation of the
class is then done elsewhere (although possibly in the same file).
Thats why I don;t think the class name definition is sufficient to
constitute a "header", it doesn't give any clue about the operations
or attributes - no summary in other words.

A class name and well written docstring could just about be called
a header, but its not common usage in Python and its accuracy
would depend entirely on the quality of the docstring!

All IMHO of course! :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Sun Nov  8 10:07:01 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 8 Nov 2009 09:07:01 -0000
Subject: [Tutor] Printing Sideway...
References: <4AF632BA.3050304@insightbb.com>
	<4AF63A25.1060204@gmail.com><4AF63E83.6020705@insightbb.com>
	<4AF647A5.4070700@gmail.com>
Message-ID: <hd61nr$pm0$1@ger.gmane.org>


> Ken G. wrote:
>> I am using Ubuntu 9.04 as my primary OS.  I have Windows XP
>> installed in the first partition (dual boot).  I have Python
>> 2.6.2 installed on both OS.  The printer is a Cannon MX300.
>> To print on the envelope, I use Windows Works v4.5 which is
>> easy to use.  I just learned how to print via Open Office
>> Writer 3.0.1 which took several hours to do properly.
>>
>> If it is not possible to print sideways, I understand.

It is possible but the solution has nothing to do with Python. 
That is the point Bob is making. How you print sideways depends 
on the printer driver, the application software you are using, and 
the OS.

ON Unix you can print sideways by doing things like sending 
print formatting codes through any of the print formatting programs 
in Unix - groff being a good example. Or you could create an 
HTML document containing your text and then print that via a browser.
Or you can format the output normally and then programmatically 
set the printer driver options to print in a different orientation.

All of these could be done from Python or via manual intervention.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From bibsmendez at gmail.com  Sun Nov  8 11:18:19 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Sun, 8 Nov 2009 13:18:19 +0300
Subject: [Tutor] Local vs global
In-Reply-To: <f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>
	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>
	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>
	<f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
Message-ID: <f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>

On Sun, Nov 8, 2009 at 6:20 AM, bibi midi <bibsmendez at gmail.com> wrote:


> Thank you all for the helpful insights. I will keep in mind the naming
> rules in Python, so as not to have it in collision with Python's
> builtins.
>
> The mention to tidy up e.g. "compartmentalize" code is also noted. I
> agree definitely.
>
> I'm reworking my code and will post back.
>

For reason unknown to me the script dont repeat itself
when asked if to play again or not no matter what juggling of the
while/if conditionals i do, the result is the same. What am i
missing?

Also, I'm not sure I'm right in the assignment ans = choose_Cave.
I code it like so because the assignment ans = choose_Cave()
the prompt 'Choose a cave' is repeated when the script is run.
Dont want this. I tested this when run in ipython.

I dont know if there is a way to just retrieve the return value to a
variable AND not run the whole function i mean in the case of
ans = choose_Cave(). Would love to hear your expert insights by return.

http://paste.debian.net/51004/


-- 
Best Regards,
bibimidi


On Sat, Nov 7, 2009 at 3:26 PM, Dave Angel <davea at ieee.org> wrote:
> Alan Gauld wrote:
>>
> Alan - Excellent comments, as usual.
>
> bibimidi - I would point out that after you remove the 'global choose'
line
> as Alan said, you should rename the global variable you're using to store
> the return value.  They're allowed to have the same name, but it's
confusing
> to a beginner if they do, since you might assume they somehow share the
> value.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/3ce9bb13/attachment.htm>

From chakrabarti.somnath at gmail.com  Sun Nov  8 12:18:36 2009
From: chakrabarti.somnath at gmail.com (somnath chakrabarti)
Date: Sun, 8 Nov 2009 06:18:36 -0500
Subject: [Tutor] TimeOut in
Message-ID: <b134f2450911080318xe729fc1je10aa8e1a0162428@mail.gmail.com>

Hi Python Tutor members,

I have a requirement where I am trying to give a solution for Magic Square
puzzle for sizes ranging from 3 to 5 and using different Solvers. I have the
below MS class derived from Problem class which is defined in constraint.py.
The Problem class has a method called getSolution() which I am overriding in
derived class MS. In MS.getSolution(solver), I am trying to start a
threading.Timer object that will execute MS.onTimeout() method when time
exceeds 100 secs i.e. if there is delay more than 100 secs for the
Problem.getSolution() method, the timer thread will execute onTimeout and
cancel the timer thread.
Once the timer thread is canceled, the control must return back to the
magic_square(n,solver=BacktrackingSolver()) function at the control point
ms.p.join() and it would print the time taken. Finally, it will close the
thread in magic_square.

Below is the code that I have written to do the timeout implementation. But
I am getting error as follows. Can anybody please help me where I am going
wrong?

%%%%%%Error Message %%%%%%%%%%%%%%%%%%%%%%%%%%%

Traceback (most recent call last):
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\csc671.py",
line 125, in <module>
<constraint.BacktrackingSolver object at 0x02845F50>
    ms_t.magic_square(size,func_solver())
Magic Square:
None
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 65, in magic_square
    print ms.getSolution(solver)
 Time Exceeded Limit.None
Time Exceeded Limit.
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 39, in getSolution
    self.p = threading.Timer(100, self.onTimeout())
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 52, in onTimeout
    if self.p.isAlive():
AttributeError: 'MS' object has no attribute 'p'
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner
    self.run()
  File "C:\Python26\lib\threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 39, in getSolution
    self.p = threading.Timer(100, self.onTimeout())
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 52, in onTimeout
    if self.p.isAlive():
AttributeError: 'MS' object has no attribute 'p'
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Below is the code -->


from __future__ import generators
from constraint import *
import time
#import thread
import threading
#import sys
#import signal
global t
class MS(Problem):
    start = 0
    end = 0
    def __init__(self,solver=None,size=3):
        Problem.__init__(self,solver)
        Problem.addVariables(self,range(0,size*size),range(1,size*size+1))
        self.addConstraints(size)
        self.solution = {}
    def addConstraints(self,size):
        magic_sum = (size*(size*size + 1))/2

Problem.addConstraint(self,AllDifferentConstraint(),range(0,size*size))
        Problem.addConstraint(self,ExactSumConstraint(magic_sum),[i for i in
range(0,size*size,size+1)])
        Problem.addConstraint(self,ExactSumConstraint(magic_sum),[i for i in
range(size-1,size*(size-1)+1,size-1)])
        for row in range(size):

Problem.addConstraint(self,ExactSumConstraint(magic_sum),[row*size+i for i
in range(size)])
        for col in range(size):

Problem.addConstraint(self,ExactSumConstraint(magic_sum),[col+size*i for i
in range(size)])
    def getSolution(self,solver):
        Problem.setSolver(self,solver)
        self.p = threading.Timer(100, self.onTimeout())
        self.start=time.time()
        self.p.start()
        try:
            self.solution=Problem.getSolution(self)
        finally:
            self.end=time.time()
        self.p.cancel()
    # dummy signal handler for timeout
    def onTimeout(self):
        print None
        print 'Time Exceeded Limit.'
        if self.p.isAlive():
            self.p.cancel()

def magic_square(n,solver=BacktrackingSolver()):
        ms=MS(solver,n)
        print solver
        print 'Magic Square:'
        t = threading.Thread(None,ms.getSolution,None,(solver,),{})
        t.start()
        print ms.getSolution(solver)
        ms.p.join()
        print 'Time taken: %1.3f' % (ms.end-ms.start)
        t.cancel()



-- 
Thanks and regards,
Somnath Chakrabarti.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/8b14ff57/attachment.htm>

From beachkid at insightbb.com  Sun Nov  8 12:31:30 2009
From: beachkid at insightbb.com (Ken G.)
Date: Sun, 08 Nov 2009 06:31:30 -0500
Subject: [Tutor] Printing Sideway...
In-Reply-To: <hd61nr$pm0$1@ger.gmane.org>
References: <4AF632BA.3050304@insightbb.com> <4AF63A25.1060204@gmail.com>
	<4AF63E83.6020705@insightbb.com> <4AF647A5.4070700@gmail.com>
	<hd61nr$pm0$1@ger.gmane.org>
Message-ID: <4AF6AC12.8020106@insightbb.com>

Okay, thanks.  Understood.  It's not a big thing here.  Thought I would 
ask.

Ken

Alan Gauld wrote:
>
>> Ken G. wrote:
>>> I am using Ubuntu 9.04 as my primary OS.  I have Windows XP
>>> installed in the first partition (dual boot).  I have Python
>>> 2.6.2 installed on both OS.  The printer is a Cannon MX300.
>>> To print on the envelope, I use Windows Works v4.5 which is
>>> easy to use.  I just learned how to print via Open Office
>>> Writer 3.0.1 which took several hours to do properly.
>>>
>>> If it is not possible to print sideways, I understand.
>
> It is possible but the solution has nothing to do with Python. That is 
> the point Bob is making. How you print sideways depends on the printer 
> driver, the application software you are using, and the OS.
>
> ON Unix you can print sideways by doing things like sending print 
> formatting codes through any of the print formatting programs in Unix 
> - groff being a good example. Or you could create an HTML document 
> containing your text and then print that via a browser.
> Or you can format the output normally and then programmatically set 
> the printer driver options to print in a different orientation.
>
> All of these could be done from Python or via manual intervention.
>
> HTH,
>

From erimendz at gmail.com  Sun Nov  8 04:16:13 2009
From: erimendz at gmail.com (Eri Mendz)
Date: Sun, 8 Nov 2009 06:16:13 +0300
Subject: [Tutor] Local vs global
In-Reply-To: <4AF56767.5080205@ieee.org>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>
Message-ID: <88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>

On Sat, Nov 7, 2009 at 3:26 PM, Dave Angel <davea at ieee.org> wrote:
> Alan Gauld wrote:
>>
> Alan - Excellent comments, as usual.
>
> bibimidi - I would point out that after you remove the 'global choose' line
> as Alan said, you should rename the global variable you're using to store
> the return value. ?They're allowed to have the same name, but it's confusing
> to a beginner if they do, since you might assume they somehow share the
> value.
>

Thank you all for the helpful insights. I will keep in mind the naming
rules in Python, so as not to have it in collision with Python's
builtins.

The mention to tidy up e.g. "compartmentalize" code is also noted. I
agree definitely.

I'm reworking my code and will post back.




-- 
Best Regards

Marie von Ebner-Eschenbach  - "Even a stopped clock is right twice a
day." - http://www.brainyquote.com/quotes/authors/m/marie_von_ebnereschenbac.html

From davea at ieee.org  Sun Nov  8 14:38:01 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 08 Nov 2009 08:38:01 -0500
Subject: [Tutor] Local vs global
In-Reply-To: <f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>	
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>	
	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>	
	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>	
	<f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
	<f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
Message-ID: <4AF6C9B9.1020406@ieee.org>

bibi midi wrote:
> On Sun, Nov 8, 2009 at 6:20 AM, bibi midi <bibsmendez at gmail.com> wrote:
>
>
>   
>> Thank you all for the helpful insights. I will keep in mind the naming
>> rules in Python, so as not to have it in collision with Python's
>> builtins.
>>
>> The mention to tidy up e.g. "compartmentalize" code is also noted. I
>> agree definitely.
>>
>> I'm reworking my code and will post back.
>>
>>     
>
> For reason unknown to me the script dont repeat itself
> when asked if to play again or not no matter what juggling of the
> while/if conditionals i do, the result is the same. What am i
> missing?
>
> Also, I'm not sure I'm right in the assignment ans = choose_Cave.
> I code it like so because the assignment ans = choose_Cave()
> the prompt 'Choose a cave' is repeated when the script is run.
> Dont want this. I tested this when run in ipython.
>
> I dont know if there is a way to just retrieve the return value to a
> variable AND not run the whole function i mean in the case of
> ans = choose_Cave(). Would love to hear your expert insights by return.
>
> http://paste.debian.net/51004/
>
>
>   
You have the following line at top-level:

    if ask.lower not in reply:

But you're not calling the method str.lower(), you're just creating a 
function object from it.  You need those parentheses.

    if ask.lower() not in reply:


You have the following fragment in main():

    print(intro)
    choose_Cave()
    ans = choose_Cave    # use: pass function by reference
    checkCave(ans)


which has two references to choose_Cave().  The first one calls it, but 
throws away the return value.  The second does not call it at all, but 
merely creates a function object and stores it in ans.  You need to 
combine your two intents in one line:

    print(intro)
    ans = choose_Cave()    # use: pass function by reference
    checkCave(ans)

Hope that helps.  Remember, that unlike Pascal, in Python you don't call 
the function unless you include the parentheses.

DaveA




From alan.gauld at btinternet.com  Sun Nov  8 17:07:24 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Sun, 8 Nov 2009 08:07:24 -0800 (PST)
Subject: [Tutor] Local vs global
In-Reply-To: <4AF6C9B9.1020406@ieee.org>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>
	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>
	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>
	<f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
	<f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
	<4AF6C9B9.1020406@ieee.org>
Message-ID: <518653.5199.qm@web86705.mail.ird.yahoo.com>



> Remember, that unlike Pascal, in Python you don't call the 
> function unless you include the parentheses.

And since you are unlikely to be old enough to have programmed 
in Pascal(!), I'll add unlike VB you don't call the function unless 
you include parentheses.

:-)

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/b1d356a2/attachment-0001.htm>

From alan.gauld at btinternet.com  Sun Nov  8 18:17:41 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 8 Nov 2009 17:17:41 -0000
Subject: [Tutor] TimeOut in
References: <b134f2450911080318xe729fc1je10aa8e1a0162428@mail.gmail.com>
Message-ID: <hd6ufr$4ig$1@ger.gmane.org>


"somnath chakrabarti" <chakrabarti.somnath at gmail.com> wrote

> Below is the code that I have written to do the timeout implementation. 
> But
> I am getting error as follows. Can anybody please help me where I am 
> going
> wrong?

I have no idea how this framework is upposed to work but my
guess is that the problem lies here:

>    def getSolution(self,solver):
>        Problem.setSolver(self,solver)
>        self.p = threading.Timer(100, self.onTimeout())

Thios looks like you should be providing a callback
function self.onTimeout but you are calling the function
instead of referencing it. This calls the method before
self.p has been defined.

You need to remove the parentheses after Timeout.

I think...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From lie.1296 at gmail.com  Sun Nov  8 19:27:23 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Mon, 09 Nov 2009 05:27:23 +1100
Subject: [Tutor] Local vs global
In-Reply-To: <f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>	<hd3dtq$dj1$1@ger.gmane.org>
	<4AF56767.5080205@ieee.org>	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>	<f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
	<f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
Message-ID: <hd72jt$g3m$1@ger.gmane.org>

bibi midi wrote:
> 
> 
> On Sun, Nov 8, 2009 at 6:20 AM, bibi midi <bibsmendez at gmail.com 
> <mailto:bibsmendez at gmail..com>> wrote:
> 
> 
>     Thank you all for the helpful insights. I will keep in mind the naming
>     rules in Python, so as not to have it in collision with Python's
>     builtins.
> 
>     The mention to tidy up e.g. "compartmentalize" code is also noted.. I
>     agree definitely.
> 
>     I'm reworking my code and will post back.
> 
> 
> For reason unknown to me the script dont repeat itself
> when asked if to play again or not no matter what juggling of the
> 
> while/if conditionals i do, the result is the same. What am i 
> missing?

You don't check for whether the answer is 'no'. A single if-statement 
branches the code into TWO line of execution, you wanted to have THREE 
line of execution (i.e. a 'yes' part, a 'no' part, and an invalid answer 
part).

> Also, I'm not sure I'm right in the assignment ans = choose_Cave.
> I code it like so because the assignment ans = choose_Cave()

In:
def choose_Cave():
     global choose
     ...

you declared that `choose` is a global variable.

You should remove this as it adds an unnecessary global variable. Even 
if you wish to retain it as a global, you probably should choose a 
better name (e.g. current_cave) since the name `choose` has no meaning 
outside choose_Cave function.

Also, at the end:

def choose_Cave():
     ....
     return choose


you returned a global variable. Although in some cases it may be 
necessary or wanted to return a global variable, you most likely don't 
need it since you can always access a global variable from anywhere in a 
script. You should make `choose` a local and assign the return value of 
choose_Cave() to another name (e.g. current_cave).

> the prompt 'Choose a cave' is repeated when the script is run.
> Dont want this. I tested this when run in ipython.
> 
> I dont know if there is a way to just retrieve the return value to a
> variable AND not run the whole function i mean in the case of
> 
> ans = choose_Cave(). Would love to hear your expert insights by return.

A function can be run multiple times, and its return value may be 
different every time it is run (in fact if the return value is always 
the same there is no point in using function). "accessing the return 
value and not run the function" doesn't make sense as it is unclear what 
you're wanting to access. What you wanted is to access the latest return 
value of choose_Cave, which means you want to store the return value of 
choose_Cave in a variable (i.e. current_cave = choose_Cave() or ans = 
choose_Cave())

An alternative would be to store the latest return value in a global 
variable, but I strongly recommend against this as global variable is 
harmful.


From lie.1296 at gmail.com  Sun Nov  8 19:30:13 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Mon, 09 Nov 2009 05:30:13 +1100
Subject: [Tutor] pattern searching
In-Reply-To: <f23da4ca0911070013x79064997x5478cb3351aa4188@mail.gmail.com>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>
	<4AF4D27A.5080802@gmail.com>	<d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com>
	<4AF4D94F.40704@gmail.com>
	<f23da4ca0911070013x79064997x5478cb3351aa4188@mail.gmail.com>
Message-ID: <hd72p1$g3m$2@ger.gmane.org>

Ajith Gopinath wrote:
> Thanks folks.
> 
> || a j i t ||
> 
> 
> On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer at gmail..com 
> <mailto:bgailer at gmail.com>> wrote:
> 
>     Shashwat Anand wrote:
> 
>         @Bob: the solution seems promising, and it's fast. Thanks for
>         the improvement. However I would like to do a minor change to
>         the code to prevent it going to infinite loop.
> 

Be mindful that if the original string contains 'lul' you might miscount.


From emmanuel.ruellan at laposte.net  Sun Nov  8 20:10:49 2009
From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan)
Date: Sun, 8 Nov 2009 20:10:49 +0100
Subject: [Tutor] SQLite database not update correctly
Message-ID: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>

Hi tutors,

I've got a functions that should update an sqlite database, among other
things.  However the database doesn't get updated. When used in isolation,
the update statement works fine. What am I doing wrong?

Below is the function. The whole script can be found at
http://pastebin.com/m53978ffa

def update_DB_chi_square():
    global stopwords
    conn = sqlite3.connect(os.path.join('.', 'collocations_base.sqlite'))
    c = conn.cursor()
    c.execute('select * from word_couples')
    couples_and_nbrs = c.fetchall()
    for couple_and_nbr in couples_and_nbrs:
        w1, w2, o11, chi_square = couple_and_nbr
        if o11 < 5 or w1 in stopwords or w2 in stopwords:
            continue
        o12 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 = ? and w2 != ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        o21 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 != ? and w2 = ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        o22 = c.execute("""select sum(nbr_occ)
                        from word_couples
                        where w1 != ? and w2 != ?
                        """, (w1, w2)).fetchall()[0][0] or 0
        n = c.execute("select sum(nbr_occ) from
word_couples").fetchall()[0][0]
        chi_square = float((n * (o11 * o22 - o12 * o21) ** 2))/ ((o11 + o12)
* (o11 + o21) * (o12 + o22) * (o21 + o22))
        c.execute('update word_couples set chi_square = ? where w1 = ? and
w2 = ?', (chi_square, w1, w2))
        print 'check:', '\t'.join((w1, w2, str(chi_square)))
    c.close()
    conn.close()

Best regards,
Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/f25b9dcf/attachment.htm>

From chakrabarti.somnath at gmail.com  Sun Nov  8 20:09:11 2009
From: chakrabarti.somnath at gmail.com (somnath chakrabarti)
Date: Sun, 8 Nov 2009 14:09:11 -0500
Subject: [Tutor] TimeOut in
In-Reply-To: <hd6ufr$4ig$1@ger.gmane.org>
References: <b134f2450911080318xe729fc1je10aa8e1a0162428@mail.gmail.com>
	<hd6ufr$4ig$1@ger.gmane.org>
Message-ID: <b134f2450911081109g5fa74a6egfdd8a931bfc02589@mail.gmail.com>

Hi Alan,

I am importing the constraint library package in the beginning that is
having classes Problem, Solver and all the methods that I am calling. Now,
as you said after making the change from

        self.p = threading.Timer(100, self.onTimeout)

I am getting the below error. I am also attaching the constraint.py
package that my code is using

- Somnath


%%%%%%% Error Message %%%%%%%%%%%%%%%%%%%%

<constraint.BacktrackingSolver object at 0x02846F10>
Magic Square:
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner
    self.run()
  File "C:\Python26\lib\threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 43, in getSolution
    self.solution=Problem.getSolution(self)
  File
"C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
215, in getSolution
    return self._solver.getSolution(domains, constraints, vconstraints)
  File
"C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
524, in getSolution
    return iter.next()
  File
"C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
506, in getSolutionIter
    pushdomains):
  File
"C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
1173, in __call__
    domain.hideValue(value)
  File
"C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
787, in hideValue
    list.remove(self, value)
ValueError: list.remove(x): x not in list
None
Time taken: 0.047

Traceback (most recent call last):
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\csc671.py",
line 125, in <module>
    ms_t.magic_square(size,func_solver())
  File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
line 68, in magic_square
    t.cancel()
AttributeError: 'Thread' object has no attribute 'cancel'
None
Time Exceeded Limit.


On Sun, Nov 8, 2009 at 12:17 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "somnath chakrabarti" <chakrabarti.somnath at gmail.com> wrote
>
>
> Below is the code that I have written to do the timeout implementation. But
>> I am getting error as follows. Can anybody please help me where I am going
>> wrong?
>>
>
> I have no idea how this framework is upposed to work but my
> guess is that the problem lies here:
>
>
>   def getSolution(self,solver):
>>       Problem.setSolver(self,solver)
>>       self.p = threading.Timer(100, self.onTimeout())
>>
>
> Thios looks like you should be providing a callback
> function self.onTimeout but you are calling the function
> instead of referencing it. This calls the method before
> self.p has been defined.
>
> You need to remove the parentheses after Timeout.
>
> I think...
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Thanks and regards,
Somnath Chakrabarti.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/8e433d23/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: constraint.py
Type: application/octet-stream
Size: 50556 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/8e433d23/attachment-0001.obj>

From pine508 at hotmail.com  Sun Nov  8 21:15:08 2009
From: pine508 at hotmail.com (Che M)
Date: Sun, 8 Nov 2009 15:15:08 -0500
Subject: [Tutor] SQLite database not update correctly
In-Reply-To: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>
References: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>
Message-ID: <SNT127-W5B9916A16DF60949BECC7E0AD0@phx.gbl>










> I've got a functions that should update an sqlite database, among other things.  However
>  the database doesn't get updated. When used in isolation, the update statement works 
> fine. What am I doing wrong?

> Below is the function. The whole script can be found at http://pastebin.com/m53978ffa

I think it's because you don't commit the changes to the database.  Use c.commit() after
you make the changes.

 		 	   		  
_________________________________________________________________
Find the right PC with Windows 7 and Windows Live. 
http://www.microsoft.com/Windows/pc-scout/laptop-set-criteria.aspx?cbid=wl&filt=200,2400,10,19,1,3,1,7,50,650,2,12,0,1000&cat=1,2,3,4,5,6&brands=5,6,7,8,9,10,11,12,13,14,15,16&addf=4,5,9&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen2:112009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/607cec5a/attachment.htm>

From emmanuel.ruellan at laposte.net  Sun Nov  8 21:28:07 2009
From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan)
Date: Sun, 8 Nov 2009 21:28:07 +0100
Subject: [Tutor] SQLite database not update correctly
In-Reply-To: <SNT127-W16A3EE29208E2F52923903E0AD0@phx.gbl>
References: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>
	<SNT127-W16A3EE29208E2F52923903E0AD0@phx.gbl>
Message-ID: <7296745c0911081228y11bf7423g9538e33e25520bff@mail.gmail.com>

Thanks a lot, Che! It's working fine now.

On Sun, Nov 8, 2009 at 9:13 PM, Che M <pine508 at hotmail.com> wrote:

>  > I've got a functions that should update an sqlite database, among other
> things.  However
> >  the database doesn't get updated. When used in isolation, the update
> statement works
> > fine. What am I doing wrong?
>
> > Below is the function. The whole script can be found at
> http://pastebin.com/m53978ffa
>
> I think it's because you don't commit the changes to the database.  Use
> c.commit() after
> you make the changes.
>
>
>
>
>
> ------------------------------
> Find the right PC with Windows 7 and Windows Live. Learn more.<http://www.microsoft.com/Windows/pc-scout/laptop-set-criteria.aspx?cbid=wl&filt=200,2400,10,19,1,3,1,7,50,650,2,12,0,1000&cat=1,2,3,4,5,6&brands=5,6,7,8,9,10,11,12,13,14,15,16&addf=4,5,9&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen2:112009>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/b10a0e34/attachment.htm>

From emmanuel.ruellan at laposte.net  Sun Nov  8 21:33:41 2009
From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan)
Date: Sun, 8 Nov 2009 21:33:41 +0100
Subject: [Tutor] SQLite database not update correctly
In-Reply-To: <SNT127-W5B9916A16DF60949BECC7E0AD0@phx.gbl>
References: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>
	<SNT127-W5B9916A16DF60949BECC7E0AD0@phx.gbl>
Message-ID: <7296745c0911081233x56112233g5f4b847d77abda1b@mail.gmail.com>

It's working fine now, but actually I didn't write exactly what you
suggested. The "commit" method belongs to the connection, not to the cursor.
Therefore, in my script it should be conn.commit().

On Sun, Nov 8, 2009 at 9:15 PM, Che M <pine508 at hotmail.com> wrote:

>
> > I've got a functions that should update an sqlite database, among other
> things.  However
> >  the database doesn't get updated. When used in isolation, the update
> statement works
> > fine. What am I doing wrong?
>
> > Below is the function. The whole script can be found at
> http://pastebin.com/m53978ffa
>
> I think it's because you don't commit the changes to the database.  Use
> c.commit() after
> you make the changes.
>
>
> <http://www.microsoft.com/Windows/pc-scout/laptop-set-criteria.aspx?cbid=wl&filt=200,2400,10,19,1,3,1,7,50,650,2,12,0,1000&cat=1,2,3,4,5,6&brands=5,6,7,8,9,10,11,12,13,14,15,16&addf=4,5,9&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen2:112009>
> ------------------------------
> Find the right PC with Windows 7 and Windows Live. Learn more.<http://www.microsoft.com/Windows/pc-scout/laptop-set-criteria.aspx?cbid=wl&filt=200,2400,10,19,1,3,1,7,50,650,2,12,0,1000&cat=1,2,3,4,5,6&brands=5,6,7,8,9,10,11,12,13,14,15,16&addf=4,5,9&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen2:112009>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/c9875207/attachment.htm>

From chakrabarti.somnath at gmail.com  Mon Nov  9 02:09:54 2009
From: chakrabarti.somnath at gmail.com (somnath chakrabarti)
Date: Sun, 8 Nov 2009 20:09:54 -0500
Subject: [Tutor] TimeOut in between threads
Message-ID: <b134f2450911081709h3e334b8fs9559ce076a8c6cd8@mail.gmail.com>

Hi Python Tutor members,

I am learning the concept of threads in Python and trying to write a Python
code that will start a thread (say t1) from main or any other thread and
then t1 will call a certain function f1(). If the execution of function f1()
takes more than a specified time (say 1000 secs), t1 will be canceled from
the calling thread. The requirement is sort of implementing timeout using
threads. I am writing using Python GUI (IDLE) in Windows and tried using the
signal package but it didnot work in Windows.

Below is the code that I have written. Thread t calls timer thread p. But
even after p is canceled, the function hello() keeps on printing 'Hello
World' for sometime. Any idea of what I am missing or what is wrong? I feel
like there should be more graceful solution for implementing timeouts in
Python? Any help will be appreciated.

Here is my code -->

import threading
import time
"""Example of an implementation of threading.Thread
   with threading.Timer"""
global p
def onTimeout():
    p.cancel()
    print 'Time Exceeded.'
p = threading.Timer(5,onTimeout)
def hello():
    p.start()
    j = 0
    while j < 1000:
        if p.isAlive():
            print 'Hello World'
            j += 1
        else:
            break
    if p.isAlive():
        p.cancel()

class TestThread(threading.Thread):
    def __init__(self,group=None,target=None,name=None,args=(),kwargs={}):
        threading.Thread.__init__(self,group,target,name,args,kwargs)

global t
t = TestThread(None,hello,None,(),{})
start = time.time()
t.start()
end = time.time()
t.join()
print 'Time Taken: %1.3f' % (end-start)
print 'Back from Time Out.'


Here is result -->

Hello World
...
Hello World
Time Exceeded.Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Time Taken: 0.000
Back from Time Out.

-- 
Thanks and regards,
Somnath Chakrabarti.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091108/b557aff6/attachment.htm>

From bgailer at gmail.com  Mon Nov  9 04:13:43 2009
From: bgailer at gmail.com (bob gailer)
Date: Sun, 08 Nov 2009 22:13:43 -0500
Subject: [Tutor] pattern searching
In-Reply-To: <hd72p1$g3m$2@ger.gmane.org>
References: <f23da4ca0911061215u7f6066b8h976817c48158bce3@mail.gmail.com>	<4AF4D27A.5080802@gmail.com>	<d4ab53de0911061812x650d1383nca53b950f90d0fe5@mail.gmail.com>	<4AF4D94F.40704@gmail.com>	<f23da4ca0911070013x79064997x5478cb3351aa4188@mail.gmail.com>
	<hd72p1$g3m$2@ger.gmane.org>
Message-ID: <4AF788E7.6000407@gmail.com>

Lie Ryan wrote:
> Ajith Gopinath wrote:
>> Thanks folks.
>>
>> || a j i t ||
>>
>>
>> On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer at gmail..com 
>> <mailto:bgailer at gmail.com>> wrote:
>>
>>     Shashwat Anand wrote:
>>
>>         @Bob: the solution seems promising, and it's fast. Thanks for
>>         the improvement. However I would like to do a minor change to
>>         the code to prevent it going to infinite loop.
>>
>
> Be mindful that if the original string contains 'lul' you might miscount.

I disagree. My algorithm works on a translation of the original. 'lul' 
will be translated to 'lll'.



-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From bibsmendez at gmail.com  Mon Nov  9 06:03:08 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Mon, 9 Nov 2009 00:03:08 -0500
Subject: [Tutor] Local vs global
In-Reply-To: <88d91f010911082057m7ac71230td3becfe590b5c569@mail.gmail.com>
References: <f16f1f8c0911062003qb6b28a7nba1282109dc285a9@mail.gmail.com>
	<hd3dtq$dj1$1@ger.gmane.org> <4AF56767.5080205@ieee.org>
	<88d91f010911071916l66419561w44e2fd39b7cdd851@mail.gmail.com>
	<88d91f010911071917q9696001wd842efe75b0ce86c@mail.gmail.com>
	<f16f1f8c0911071920y7ed491adu6ed77ab9e414fddf@mail.gmail.com>
	<f16f1f8c0911080218u7be40d81xd07adc30e58543d7@mail.gmail.com>
	<4AF6C9B9.1020406@ieee.org>
	<88d91f010911082057m7ac71230td3becfe590b5c569@mail.gmail.com>
Message-ID: <f16f1f8c0911082103h49b8bf8cndf92e62e8de30104@mail.gmail.com>

On Sun, Nov 8, 2009 at 8:38 AM, Dave Angel <davea at ieee.org> wrote:
>
> You have the following line at top-level:
>
>   if ask.lower not in reply:
>
> But you're not calling the method str.lower(), you're just creating a
> function object from it.  You need those parentheses.
>
>   if ask.lower() not in reply:
>
>
> You have the following fragment in main():
>
>   print(intro)
>   choose_Cave()
>   ans = choose_Cave    # use: pass function by reference
>   checkCave(ans)
>
>
> which has two references to choose_Cave().  The first one calls it, but
> throws away the return value.  The second does not call it at all, but
> merely creates a function object and stores it in ans.  You need to
combine
> your two intents in one line:
>
>   print(intro)
>   ans = choose_Cave()    # use: pass function by reference
>   checkCave(ans)
>
> Hope that helps.  Remember, that unlike Pascal, in Python you don't call
the
> function unless you include the parentheses.

Hi Dave and all,

My eyes failed me spotting the bugs :-) They are just staring at me
yet i miss to catch them. I guess thats the net result when one take
it too seriously haha (joke).

I fully agree function is not called when the parenthesis is missing,
you only call the object. About Pascal or VB i cant comment on them
:-)

The code works as expected now. I guess i have to move on to the other
examples.

Will be back here to bug you guys with questions :-) Thank you for so
helpful.


--
Best Regards

Jonathan Swift  - "May you live every day of your life." -
http://www.brainyquote.com/quotes/authors/j/jonathan_swift.html



-- 
Best Regards,
bibimidi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/2ff4031c/attachment.htm>

From sanelson at gmail.com  Mon Nov  9 06:41:12 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 05:41:12 +0000
Subject: [Tutor] Logfile Manipulation
Message-ID: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>

I've got a large amount of data in the form of 3 apache and 3 varnish
logfiles from 3 different machines.  They are rotated at 0400.  The
logfiles are pretty big - maybe 6G per server, uncompressed.

I've got to produce a combined logfile for 0000-2359 for a given day,
with a bit of filtering (removing lines based on text match, bit of
substitution).

I've inherited a nasty shell script that does this but it is very slow
and not clean to read or understand.

I'd like to reimplement this in python.

Initial questions:

* How does Python compare in performance to shell, awk etc in a big
pipeline?  The shell script kills the CPU
* What's the best way to extract the data for a given time, eg 0000 -
2359 yesterday?

Any advice or experiences?

S.
-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From wilcoxwork at gmail.com  Mon Nov  9 09:47:44 2009
From: wilcoxwork at gmail.com (Kristin Wilcox)
Date: Mon, 9 Nov 2009 00:47:44 -0800
Subject: [Tutor] CGI Script Fails on import cgi
Message-ID: <de4393b40911090047g47db778blafce4085b8788467@mail.gmail.com>

Hello,

As I've mentioned on the list before, I'm very new to Python and
programming in general. Now I'm trying my hand at writing very very
simple CGI scripts.  I basically wanted to try play with cgi scripting
as I feel my way around Python. I'm uploading to my account on my
university's server.

Unfortunately, I error out whenever I do 'import cgi'. I'm hoping to
receive some advice! Thanks in advance for reading all this.

Facts:
1. From what I can tell, the server is Red Hat Enterprise Linux with
Apache 1.3.33.

2. I have successfully executed .cgi scripts that only included
printing text or html.

3. I've tried adding form validation with cgi.FieldStorage() and it
would run in the server command line okay as .py, without syntax
errors.

4. However, these same scripts including cgi.FieldStorage() would fail
to execute when uploaded as a cgi script. I get this error:
"CGI Script Failure
The web page you attempted to access seems to have a problem: the
script failed before sending any headers. "

4. I tested by adding only 'import cgi' to one of my html printing
files that worked, to confirm that it was the sole problem. That
script failed - so I knew the module import was it.

5. I then used try: import cgi, except: traceback.format_exc(None)
I received this:
Traceback (most recent call last): File "cgi_errors.cgi",
line 9, in  import cgi File
"/usr/local/depot/python.1250025008/lib/python2.6/cgi.py",
line 40, in  import urllib File
"/usr/local/depot/python.1250025008/lib/python2.6/urllib.py",
line 26, in  import socket File
"/usr/local/depot/python.1250025008/lib/python2.6/socket.py",
line 46, in import _socket
ImportError: ld.so.1: python: fatal: relocation error: file
/usr/local/depot/python.1250025008/lib/python2.6/lib-dynload/_socket.so:
symbol inet_aton: referenced symbol not found


6. From the command line I typed python2.6, and then asked for
sys.path, sys.modules["cgi"], sys.modules["socket"], and
sys.modules["urllib"], and sys.modules["_socket"] and this is what it
gave me:

'/usr/local/depot/python.1250025008/lib/python26.zip',
'/usr/local/depot/python.1250025008/lib/python2.6',
'/usr/local/depot/python.1250025008/lib/python2.6/plat-linux2',
'/usr/local/depot/python.1250025008/lib/python2.6/lib-tk',
'/usr/local/depot/python.1250025008/lib/python2.6/lib-old',
'/usr/local/depot/python.1250025008/lib/python2.6/lib-dynload',
'/usr/local/depot/python.1250025008/lib/python2.6/site-packages',
'/afs/umbc.edu/common/python26/i386_linux26/lib/python2.6/site-packages'
<module 'cgi' from '/usr/local/depot/python.1250025008/lib/python2.6/cgi.pyc'>
<module 'urllib' from
'/usr/local/depot/python.1250025008/lib/python2.6/urllib.pyc'>
<module 'socket' from
'/usr/local/depot/python.1250025008/lib/python2.6/socket.pyc'>
<module '_socket' from
'/usr/local/depot/python.1250025008/lib/python2.6/lib-dynload/_socket.so'>

Any advice would be much appreciated!

- Kris

From alan.gauld at btinternet.com  Mon Nov  9 09:47:52 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 9 Nov 2009 08:47:52 -0000
Subject: [Tutor] Logfile Manipulation
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
Message-ID: <hd8kvv$4v0$1@ger.gmane.org>

"Stephen Nelson-Smith" <sanelson at gmail.com> wrote

> * How does Python compare in performance to shell, awk etc in a big
> pipeline?  The shell script kills the CPU

Python should be significantly faster than the typical shell script
and it should consume less resources, although it will probably
still use a fair bit of CPU unless you nice it.

> * What's the best way to extract the data for a given time, eg 0000 -
> 2359 yesterday?

I'm not familiar with Apache log files so I'll let somebody else answer,
but I suspect you can either use string.split() or a re.findall(). You 
might
even be able to use csv. Or if they are in XML you could use ElementTree.
It all depends on the data!

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From sanelson at gmail.com  Mon Nov  9 09:58:29 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 08:58:29 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <hd8kvv$4v0$1@ger.gmane.org>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
Message-ID: <b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>

On Mon, Nov 9, 2009 at 8:47 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:

> I'm not familiar with Apache log files so I'll let somebody else answer,
> but I suspect you can either use string.split() or a re.findall(). You might
> even be able to use csv. Or if they are in XML you could use ElementTree.
> It all depends on the data!

An apache logfile entry looks like this:

89.151.119.196 - - [04/Nov/2009:04:02:10 +0000] "GET
/service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
HTTP/1.1" 200 50 "-" "-"

I want to extract 24 hrs of data based timestamps like this:

[04/Nov/2009:04:02:10 +0000]

I also need to do some filtering (eg I actually don't want anything
with service.php), and I also have to do some substitutions - that's
trivial other than not knowing the optimum place to do it?  IE should
I do multiple passes?  Or should I try to do all the work at once,
only viewing each line once?  Also what about reading from compressed
files?  The data comes in as 6 gzipped logfiles which expand to 6G in
total.

S.

From alan.gauld at btinternet.com  Mon Nov  9 10:10:57 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Mon, 9 Nov 2009 01:10:57 -0800 (PST)
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
Message-ID: <294205.69710.qm@web86705.mail.ird.yahoo.com>



> An apache logfile entry looks like this:
>
>89.151.119.196 - - [04/Nov/2009:04:02:10 +0000] "GET
> /service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
> HTTP/1.1" 200 50 "-" "-"
>
>I want to extract 24 hrs of data based timestamps like this:
>
> [04/Nov/2009:04:02:10 +0000]


OK It looks like you could use a regex to extract the first 
thing you find between square brackets. Then convert that to a time.

> I also need to do some filtering (eg I actually don't want anything
> with service.php), 

That's easy enough to detect.

> and I also have to do some substitutions - that's
> trivial other than not knowing the optimum place to do it?  

Assuming they are trivial then...

> I do multiple passes?  Or should I try to do all the work at once,

I'd opt for doing it all in one pass. With such large files you really 
want to minimise the amount of time spent reading the file. 
Plus with such large files you will need/want to process them 
line by line anyway rather than reading the whole thing into memory.

> Also what about reading from compressed files?  
> The data comes in as 6 gzipped logfiles 

Python has a module for that but I've never used it.

BTW A quick google reveals that there are several packages  
for handling Apache log files. That is probably worth investigating 
before you start writing lots of code...

Examples:

Scratchy - The Apache Log Parser and HTML Report Generator for Python
Scratchy is an Apache Web Server log parser and HTML report generator written in Python. Scratchy was created by Phil Schwartz ...
scratchy.sourceforge.net/ - Cached - Similar - 


Loghetti: an apache log file filter in Python - O'Reilly ONLamp Blog
18 Mar 2008 ... Loghetti: an apache log file filter in Python ... This causes loghetti to parsethe query string, and return lines where the query parameter ...
www.oreillynet.com/.../blog/.../loghetti_an_apache_log_file_fi.html -
HTH


Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/4b9b0f67/attachment-0001.htm>

From sanelson at gmail.com  Mon Nov  9 10:36:21 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 09:36:21 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
Message-ID: <b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>

Sorry - forgot to include the list.

On Mon, Nov 9, 2009 at 9:33 AM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
> On Mon, Nov 9, 2009 at 9:10 AM, ALAN GAULD <alan.gauld at btinternet.com> wrote:
>>
>>> An apache logfile entry looks like this:
>>>
>>>89.151.119.196 - - [04/Nov/2009:04:02:10 +0000] "GET
>>> /service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
>>> HTTP/1.1" 200 50 "-" "-"
>>>
>>>I want to extract 24 hrs of data based timestamps like this:
>>>
>>> [04/Nov/2009:04:02:10 +0000]
>>
>> OK It looks like you could use a regex to extract the first
>> thing you find between square brackets. Then convert that to a time.
>
> I'm currently thinking I can just use a string comparison after the
> first entry for the day - that saves date arithmetic.
>
>> I'd opt for doing it all in one pass. With such large files you really
>> want to minimise the amount of time spent reading the file.
>> Plus with such large files you will need/want to process them
>> line by line anyway rather than reading the whole thing into memory.
>
> How do I handle concurrency? ?I have 6 log files which I need to turn
> into one time-sequenced log.
>
> I guess I need to switch between each log depending on whether the
> next entry is the next chronological entry between all six. ?Then on a
> per line basis I can also reject it if it matches the stuff I want to
> throw out, and substitute it if I need to, then write out to the new
> file.
>
> S.
>



-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From martin at linux-ip.net  Mon Nov  9 10:38:02 2009
From: martin at linux-ip.net (Martin A. Brown)
Date: Mon, 9 Nov 2009 10:38:02 +0100
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
Message-ID: <alpine.LNX.2.00.0911091002470.6072@localhost.localdomain>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

 : An apache logfile entry looks like this:
 : 
 : 89.151.119.196 - - [04/Nov/2009:04:02:10 +0000] "GET
 : /service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
 : HTTP/1.1" 200 50 "-" "-"
 : 
 : I want to extract 24 hrs of data based timestamps like this:
 : 
 : [04/Nov/2009:04:02:10 +0000]
 : 
 : I also need to do some filtering (eg I actually don't want 
 : anything with service.php), and I also have to do some 
 : substitutions - that's trivial other than not knowing the optimum 
 : place to do it?  IE should I do multiple passes?

I wouldn't.  Then, you spend decompression CPU, line matching CPU 
and I/O several times.  I'd do it all at once.


 : Or should I try to do all the work at once, only viewing each 
 : line once?  Also what about reading from compressed files?  The 
 : data comes in as 6 gzipped logfiles which expand to 6G in total.

There are standard modules for handling compressed data (gzip and 
bz2).  I'd imagine that the other pythonistas on this list will give 
you more detailed (and probably better) advice, but here's a sample 
of how to use the gzip module and how to skip the lines containing 
the '/service.php' string, and to extract an epoch timestamp from 
the datestamp field(s).  You would pass the filenames to operate on 
as arguments to this script.

  See optparse if you want fancier capabilities for option handling.
  
  See re if you want to match multiple patterns to ignore.
  
  See time (and datetime) for mangling time and date strings.  Be
    forewarned, time zone issues will probably be a massive headache.
    Many others have been here before [0].
  
  Look up itertools (and be prepared for some study) if you want the
    output from the log files from your different servers sorted in 
    the output.

Note that the below snippet is a toy and makes no attempt to trap 
(try/except) any error conditions.  

If you are looking for a weblog analytics package once you have 
reassambled the files into a whole, perhaps you could just start 
there (e.g. webalizer, analog are two old-school packages that come 
to mind for processing logging that has been produced in a Common 
Log Format).

I will echo Alan Gauld's sentiments of a few minutes ago and note 
that there are a probably many different Apache log parsers out 
there which can accomplish what you hope to accomplish.  On the 
other hand, you may be using this as an excuse to learn a bit of 
python.

Good luck,

- -Martin

 [0] http://seehuhn.de/blog/52

Sample:

  import sys, time, gzip
  
  files = sys.argv[1:]
  
  for file in files:
    print >>sys.stderr, "About to open %s" % ( file )
    f = gzip.open( file )
    for line in f:
      if line.find('/service.php') > 0:
        continue
      fields = line.split()
      # -- ignoring time zone; you are logging in UTC, right?
      #    tz = fields[4]
      d = int( time.mktime( time.strptime(fields[3], "[%d/%b/%Y:%H:%M:%S") ) )
      print d, line,


- -- 
Martin A. Brown
http://linux-ip.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/)

iD8DBQFK9+MGHEoZD1iZ+YcRAhITAKCLGF6GnEMYr50bgk4vAw3YMRZjuACg2VUg
I7/Vrw6KKjwqfxG0qfr10lo=
=oi6X
-----END PGP SIGNATURE-----

From kent37 at tds.net  Mon Nov  9 12:54:58 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 9 Nov 2009 06:54:58 -0500
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
Message-ID: <1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>

On Mon, Nov 9, 2009 at 4:36 AM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
>>>>I want to extract 24 hrs of data based timestamps like this:
>>>>
>>>> [04/Nov/2009:04:02:10 +0000]
>>>
>>> OK It looks like you could use a regex to extract the first
>>> thing you find between square brackets. Then convert that to a time.
>>
>> I'm currently thinking I can just use a string comparison after the
>> first entry for the day - that saves date arithmetic.

As long as the times are all in the same time zone.

>> How do I handle concurrency? ?I have 6 log files which I need to turn
>> into one time-sequenced log.
>>
>> I guess I need to switch between each log depending on whether the
>> next entry is the next chronological entry between all six. ?Then on a
>> per line basis I can also reject it if it matches the stuff I want to
>> throw out, and substitute it if I need to, then write out to the new
>> file.

If you create iterators from the files that yield (timestamp, entry)
pairs, you can merge the iterators using one of these recipes:
http://code.activestate.com/recipes/491285/
http://code.activestate.com/recipes/535160/

Kent

From waynejwerner at gmail.com  Mon Nov  9 13:37:29 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 9 Nov 2009 06:37:29 -0600
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
Message-ID: <333efb450911090437h59d7a1del5422a50ae464e6c8@mail.gmail.com>

On Sun, Nov 8, 2009 at 11:41 PM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:

> I've got a large amount of data in the form of 3 apache and 3 varnish
> logfiles from 3 different machines.  They are rotated at 0400.  The
> logfiles are pretty big - maybe 6G per server, uncompressed.
>
> I've got to produce a combined logfile for 0000-2359 for a given day,
> with a bit of filtering (removing lines based on text match, bit of
> substitution).
>
> I've inherited a nasty shell script that does this but it is very slow
> and not clean to read or understand.
>
> I'd like to reimplement this in python.
>
> Initial questions:
>
> * How does Python compare in performance to shell, awk etc in a big
> pipeline?  The shell script kills the CPU
> * What's the best way to extract the data for a given time, eg 0000 -
> 2359 yesterday?
>
> Any advice or experiences?
>
>
go here and download the pdf!
http://www.dabeaz.com/generators-uk/

Someone posted this the other day, and I went and read through it and played
around a bit and it's exactly what you're looking for - plus it has one vs.
slide of python vs. awk.

I think you'll find the pdf highly useful and right on.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/6c0dfea7/attachment.htm>

From c.t.matsumoto at gmail.com  Mon Nov  9 13:33:33 2009
From: c.t.matsumoto at gmail.com (C.T. Matsumoto)
Date: Mon, 9 Nov 2009 13:33:33 +0100
Subject: [Tutor] class initialization with a lot of parameters
Message-ID: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>

Hello All,

I'm making a class and the parameters I'm feeding the class is getting quite
large. I'm up
to 8 now. Is there any rules of thumb for classes with a lot of parameters?
I was thinking
to put the parameters into a tuple and then in the __init__ of the class,
iterate over the tuple
and assign attributes.

Right now my class basically looks like this:

class Foo(object):
    def __init__(self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8):
        ...

Cheers

T
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/95782f40/attachment.htm>

From sanelson at gmail.com  Mon Nov  9 14:02:35 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 13:02:35 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <333efb450911090437h59d7a1del5422a50ae464e6c8@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<333efb450911090437h59d7a1del5422a50ae464e6c8@mail.gmail.com>
Message-ID: <b6131fdc0911090502y6e515044j9ea95ee5af18536d@mail.gmail.com>

Hi,

>> Any advice or experiences?
>>
>
> go here and download the pdf!
> http://www.dabeaz.com/generators-uk/
> Someone posted this the other day, and I went and read through it and played
> around a bit and it's exactly what you're looking for - plus it has one vs.
> slide of python vs. awk.
> I think you'll find the pdf highly useful and right on.

Looks like generators are a really good fit.  My biggest question
really is how to multiplex.

I have 6 logs per day, so I don't know how which one will have the
next consecutive entry.

I love teh idea of making a big dictionary, but with 6G of data,
that's going to run me out of memory, isn't it

S.

From grflanagan at gmail.com  Mon Nov  9 14:20:41 2009
From: grflanagan at gmail.com (Gerard Flanagan)
Date: Mon, 09 Nov 2009 13:20:41 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090502y6e515044j9ea95ee5af18536d@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>	<333efb450911090437h59d7a1del5422a50ae464e6c8@mail.gmail.com>
	<b6131fdc0911090502y6e515044j9ea95ee5af18536d@mail.gmail.com>
Message-ID: <4AF81729.70401@gmail.com>

Stephen Nelson-Smith wrote:
> Hi,
>
>   
>>> Any advice or experiences?
>>>
>>>       
>> go here and download the pdf!
>> http://www.dabeaz.com/generators-uk/
>> Someone posted this the other day, and I went and read through it and played
>> around a bit and it's exactly what you're looking for - plus it has one vs.
>> slide of python vs. awk.
>> I think you'll find the pdf highly useful and right on.
>>     
>
> Looks like generators are a really good fit.  My biggest question
> really is how to multiplex.
>
> I have 6 logs per day, so I don't know how which one will have the
> next consecutive entry.
>
> I love teh idea of making a big dictionary, but with 6G of data,
> that's going to run me out of memory, isn't it
>
>   

Perhaps "lookahead" generators could help? Though that would be getting 
into advanced territory:

    
http://stackoverflow.com/questions/1517862/using-lookahead-with-generators



From sanelson at gmail.com  Mon Nov  9 14:35:56 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 13:35:56 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
Message-ID: <b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>

Hi,

> If you create iterators from the files that yield (timestamp, entry)
> pairs, you can merge the iterators using one of these recipes:
> http://code.activestate.com/recipes/491285/
> http://code.activestate.com/recipes/535160/

Could you show me how I might do that?

So far I'm at the stage of being able to produce loglines:

#! /usr/bin/env python
import gzip
class LogFile:
  def __init__(self, filename, date):
   self.f=gzip.open(filename,"r")
   for logline in self.f:
     self.line=logline
     self.stamp=" ".join(self.line.split()[3:5])
     if self.stamp.startswith(date):
       break

  def getline(self):
    ret=self.line
    self.line=self.f.readline()
    self.stamp=" ".join(self.line.split()[3:5])
    return ret

logs=[LogFile("a/access_log-20091105.gz","[05/Nov/2009"),LogFile("b/access_log-20091105.gz","[05/Nov/2009"),LogFile("c/access_log-20091105.gz","[05/Nov/2009")]
while True:
  print [x.stamp for x in logs]
  nextline=min((x.stamp,x) for x in logs)
  print nextline[1].getline()


-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From sanelson at gmail.com  Mon Nov  9 14:46:30 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 13:46:30 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
	<b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>
Message-ID: <b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>

And the problem I have with the below is that I've discovered that the
input logfiles aren't strictly ordered - ie there is variance by a
second or so in some of the entries.

I can sort the biggest logfile (800M) using unix sort in about 1.5
mins on my workstation.  That's not really fast enough, with
potentially 12 other files....

Hrm...

S.

On Mon, Nov 9, 2009 at 1:35 PM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
> Hi,
>
>> If you create iterators from the files that yield (timestamp, entry)
>> pairs, you can merge the iterators using one of these recipes:
>> http://code.activestate.com/recipes/491285/
>> http://code.activestate.com/recipes/535160/
>
> Could you show me how I might do that?
>
> So far I'm at the stage of being able to produce loglines:
>
> #! /usr/bin/env python
> import gzip
> class LogFile:
> ?def __init__(self, filename, date):
> ? self.f=gzip.open(filename,"r")
> ? for logline in self.f:
> ? ? self.line=logline
> ? ? self.stamp=" ".join(self.line.split()[3:5])
> ? ? if self.stamp.startswith(date):
> ? ? ? break
>
> ?def getline(self):
> ? ?ret=self.line
> ? ?self.line=self.f.readline()
> ? ?self.stamp=" ".join(self.line.split()[3:5])
> ? ?return ret
>
> logs=[LogFile("a/access_log-20091105.gz","[05/Nov/2009"),LogFile("b/access_log-20091105.gz","[05/Nov/2009"),LogFile("c/access_log-20091105.gz","[05/Nov/2009")]
> while True:
> ?print [x.stamp for x in logs]
> ?nextline=min((x.stamp,x) for x in logs)
> ?print nextline[1].getline()
>
>
> --
> Stephen Nelson-Smith
> Technical Director
> Atalanta Systems Ltd
> www.atalanta-systems.com
>



-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From waynejwerner at gmail.com  Mon Nov  9 16:15:37 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 9 Nov 2009 09:15:37 -0600
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com> 
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com> 
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com> 
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com> 
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com> 
	<b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com> 
	<b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
Message-ID: <333efb450911090715l14c23a2alef8064563d20f949@mail.gmail.com>

On Mon, Nov 9, 2009 at 7:46 AM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:

> And the problem I have with the below is that I've discovered that the
> input logfiles aren't strictly ordered - ie there is variance by a
> second or so in some of the entries.
>

Within a given set of 10 lines, is the first line and last line "in order" -
i.e.

1
2
4
3
5
8
7
6
9
10


> I can sort the biggest logfile (800M) using unix sort in about 1.5
> mins on my workstation.  That's not really fast enough, with
> potentially 12 other files....
>

If that's the case, then I'm pretty sure you can create sort of a queue
system, and it should probably cut down on the sorting time. I don't know
what the default python sorting algorithm is on a list, but AFAIK you'd be
looking at a constant O(log 10) time on each insertion by doing something
such as this:


log_generator = (d for d in logdata)
mylist = # first ten values

while True:
    try:
        mylist.sort()
        nextdata = mylist.pop(0)
        mylist.append(log_generator.next())
    except StopIteration:
        print 'done'

    #Do something with nextdata

Or now that I look, python has a priority queue (
http://docs.python.org/library/heapq.html ) that you could use instead. Just
push the next value into the queue and pop one out - you give it some
initial qty - 10 or so, and then it will always give you the smallest value.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/80124e34/attachment.htm>

From alan.gauld at btinternet.com  Mon Nov  9 16:23:13 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Mon, 9 Nov 2009 07:23:13 -0800 (PST)
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
	<b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>
	<b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
Message-ID: <42539.57294.qm@web86710.mail.ird.yahoo.com>



> I can sort the biggest logfile (800M) using unix sort in about 1.5
> mins on my workstation.  That's not really fast enough, with
> potentially 12 other files....

You won't beat sort with Python.
You have to be realistic, these are very big files!

Python should be faster overall but for specific tasks the Unix 
tools written in C will be faster.

But if you are merging multiple files into one then sorting 
them before processing will probably help. However if you expect 
to be pruning out more lines than you keep it might be easier just 
to throw all the data you want into a single file and then sort that 
at the end. It all depends on the data.

HTH,

Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/0ee80364/attachment.htm>

From vick_keshav at hotmail.com  Mon Nov  9 16:34:43 2009
From: vick_keshav at hotmail.com (Keshav Inderjeet)
Date: Mon, 9 Nov 2009 15:34:43 +0000
Subject: [Tutor] Cramer's Rule
Message-ID: <SNT125-W17217F7021D172786B3DED85AC0@phx.gbl>



Hello everyone I have been tryin to convert the 
following C- codes in python but always receiving error messages. Please Help. 
The program is about Cramer's rule 
 
 
#include<stdio.h>
 
#double finddet(double a1,double a2, double 
a3,double b1, double b2,double b3, double c1, double c2, double c3); 

 
void main():
{
     double a1, a2, a3, b1, 
b2, b3, c1, c2, c3, d1, d2, d3,det, detx, dety, detz;
     
     
printf("\n a1?"); /*Input Coefficients*/
     scanf("%lf",&a1);
     
printf("\n b1?");
     scanf("%lf",&b1);
     printf("\n 
c1?");
     scanf("%lf",&c1);
     printf("\n d1?");
     
scanf("%lf",&d1);
     printf("\n a2?");
     
scanf("%lf",&a2);
     printf("\n b2?");
     
scanf("%lf",&b2);
     printf("\n c2?");
     
scanf("%lf",&c2);
     printf("\n d2?");
     
scanf("%lf",&d2);
     printf("\n a3?");
     
scanf("%lf",&a3);
     printf("\n b3?");
     
scanf("%lf",&b3);
     printf("\n c3?");
     
scanf("%lf",&c3);
     printf("\n d3?");
     
scanf("%lf",&d3);
 
     det=finddet(a1,a2,a3,b1,b2,b3,c1,c2,c3);   
/*Find determinants*/
     detx=finddet(d1,d2,d3,b1,b2,b3,c1,c2,c3);
     
dety=finddet(a1,a2,a3,d1,d2,d3,c1,c2,c3);
     
detz=finddet(a1,a2,a3,b1,b2,b3,d1,d2,d3);
     
     if(d1==0 && 
d2==0 && d3==0 && det==0)
          printf("\n Infinite 
Solutions\n ");
     else if(d1==0 && d2==0 && d3==0 
&& det!=0)   /*Print Answers depending on various 
conditions*/
          printf("\n x=0\n y=0, \n z=0\n ");
     else 
if(det!=0)
          printf("\n x=%lf\n y=%lf\n z=%lf\n", (detx/det), 
(dety/det), (detz/det));
     else if(det==0 && detx==0 && 
dety==0 && detz==0)
          printf("\n Infinite Solutions\n 
");
     else
          printf("No Solution\n ");
 
}
 
double finddet(double a1,double a2, double 
a3,double b1, double b2,double b3, double c1, double c2, double c3)
{
     
return ((a1*b2*c3)-(a1*b3*c2)-(a2*b1*c3)+(a3*b1*c2)+(a2*b3*c1)-(a3*b2*c1)); 
/*expansion of a 3x3 determinant*/
}
 
KESH at V INDERJEET

 

            


 		 	   		  
_________________________________________________________________
Keep your friends updated?even when you?re not signed in.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/018f287c/attachment.htm>

From alan.gauld at btinternet.com  Mon Nov  9 16:53:27 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 9 Nov 2009 15:53:27 -0000
Subject: [Tutor] Cramer's Rule
References: <SNT125-W17217F7021D172786B3DED85AC0@phx.gbl>
Message-ID: <hd9du2$peb$1@ger.gmane.org>


"Keshav Inderjeet" <vick_keshav at hotmail.com> wrote 

> Hello everyone I have been tryin to convert the 
> following C- codes in python but always receiving error 
> messages. Please Help. 

So give us a clue.
What does your code look like?
What errors are you getting?

What do you think the problem might be?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From alan.gauld at btinternet.com  Mon Nov  9 16:59:27 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 9 Nov 2009 15:59:27 -0000
Subject: [Tutor] CGI Script Fails on import cgi
References: <de4393b40911090047g47db778blafce4085b8788467@mail.gmail.com>
Message-ID: <hd9e99$qnp$1@ger.gmane.org>


"Kristin Wilcox" <wilcoxwork at gmail.com> wrote

> Unfortunately, I error out whenever I do 'import cgi'. I'm hoping to
> receive some advice! Thanks in advance for reading all this.

My guess is that your web server user id is not set up to find the 
Python modules. You probably need the admin to do some 
web config

But that is just a guess.

Alan G.


From alan.gauld at btinternet.com  Mon Nov  9 16:56:16 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 9 Nov 2009 15:56:16 -0000
Subject: [Tutor] class initialization with a lot of parameters
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
Message-ID: <hd9e37$q1i$1@ger.gmane.org>


"C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote

> I'm making a class and the parameters I'm feeding the class is getting 
> quite
> large. I'm up to 8 now. Is there any rules of thumb for classes with a 
> lot of
> parameters?

There are no rules as such. Some of the Tkinter classes for excample
take a lot of arguments.

But you can usually simplify it by using default values and named 
parameters.
Also can any of the input values be grouped together into an object?
OOP means programming with objects, and that includes passing
whole objects aropund as arguments.

> class Foo(object):
>    def __init__(self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8):

But with only a generic example to guide us we can only offer limited help.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From sanelson at gmail.com  Mon Nov  9 17:10:44 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 9 Nov 2009 16:10:44 +0000
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <333efb450911090715l14c23a2alef8064563d20f949@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
	<b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>
	<b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
	<333efb450911090715l14c23a2alef8064563d20f949@mail.gmail.com>
Message-ID: <b6131fdc0911090810j726a805ar7e22104a1808c78d@mail.gmail.com>

On Mon, Nov 9, 2009 at 3:15 PM, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Mon, Nov 9, 2009 at 7:46 AM, Stephen Nelson-Smith <sanelson at gmail.com>
> wrote:
>>
>> And the problem I have with the below is that I've discovered that the
>> input logfiles aren't strictly ordered - ie there is variance by a
>> second or so in some of the entries.
>
> Within a given set of 10 lines, is the first line and last line "in order" -

On average, in a sequence of 10 log lines, one will be out by one or
two seconds.

Here's a random slice:

05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:36
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:36
05/Nov/2009:01:41:37
05/Nov/2009:01:41:37
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:37
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:37
05/Nov/2009:01:41:38
05/Nov/2009:01:41:36
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:38
05/Nov/2009:01:41:39
05/Nov/2009:01:41:38
05/Nov/2009:01:41:39
05/Nov/2009:01:41:39
05/Nov/2009:01:41:39
05/Nov/2009:01:41:39
05/Nov/2009:01:41:40
05/Nov/2009:01:41:40
05/Nov/2009:01:41:41
> I don't know
> what the default python sorting algorithm is on a list, but AFAIK you'd be
> looking at a constant O(log 10)

I'm not a mathematician - what does this mean, in layperson's terms?

> log_generator = (d for d in logdata)
> mylist = # first ten values

OK

> while True:
> ?? ?try:
> ?? ? ? ?mylist.sort()

OK - sort the first 10 values.

> ?? ? ? ?nextdata = mylist.pop(0)

So the first value...

> ?? ? ? ?mylist.append(log_generator.next())

Right, this will add another one value?

> ?? ?except StopIteration:
> ?? ? ? ?print 'done'

> Or now that I look, python has a priority queue (
> http://docs.python.org/library/heapq.html ) that you could use instead. Just
> push the next value into the queue and pop one out - you give it some
> initial qty - 10 or so, and then it will always give you the smallest value.

That sounds very cool - and I see that one of the activestate recipes
Kent suggested uses heapq too.  I'll have a play.

S.
-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From alan.gauld at btinternet.com  Mon Nov  9 17:17:02 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Mon, 9 Nov 2009 08:17:02 -0800 (PST)
Subject: [Tutor] CGI Script Fails on import cgi
In-Reply-To: <SNT125-W3743AFBC65BF35B43DDD8085AC0@phx.gbl>
References: <de4393b40911090047g47db778blafce4085b8788467@mail.gmail.com>
	<SNT125-W3743AFBC65BF35B43DDD8085AC0@phx.gbl>
Message-ID: <268358.66265.qm@web86701.mail.ird.yahoo.com>

Please use ReplyAll when replying to the list.
And please do not hijack other threads, it confuses 
threaded mail tools/newsreaders.



From: Keshav Inderjeet <vick_keshav at hotmail.com>
To: alan.gauld at btinternet.com
Sent: Monday, 9 November, 2009 16:10:30
Subject: RE: [Tutor] CGI Script Fails on import cgi

 
> In fact these codes will work just on C but I need them to work on python. 
> Is there any way to convert em?? 


Not automatically, you need to translate them into Python yourself. 
The good news is that its probably going to be shorter than the C code.

But we won't do it for you, we will help you to do it for yourself.

How much Python do you know?
Did you try to translate it? How far did you get?
What happened? What is preventing you from fixing it?

Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/7203a4d8/attachment.htm>

From alan.gauld at btinternet.com  Mon Nov  9 17:21:43 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Mon, 9 Nov 2009 08:21:43 -0800 (PST)
Subject: [Tutor] Logfile Manipulation
In-Reply-To: <b6131fdc0911090810j726a805ar7e22104a1808c78d@mail.gmail.com>
References: <b6131fdc0911082141m78c14f6fybc5fcdabebd16796@mail.gmail.com>
	<hd8kvv$4v0$1@ger.gmane.org>
	<b6131fdc0911090058w2fd5e409s4c3c75fb0ab9ac5d@mail.gmail.com>
	<294205.69710.qm@web86705.mail.ird.yahoo.com>
	<b6131fdc0911090133v5f5ea2beh42b9c38fb711366d@mail.gmail.com>
	<b6131fdc0911090136k53f4c33cp5cea03343d5aaeb3@mail.gmail.com>
	<1c2a2c590911090354j550e5ae2qdcdcd0e9ff9c5f8f@mail.gmail.com>
	<b6131fdc0911090535x67fd106dn1bd751dba88582e1@mail.gmail.com>
	<b6131fdc0911090546w75972826v37d01d9d09cb048e@mail.gmail.com>
	<333efb450911090715l14c23a2alef8064563d20f949@mail.gmail.com>
	<b6131fdc0911090810j726a805ar7e22104a1808c78d@mail.gmail.com>
Message-ID: <201797.11603.qm@web86702.mail.ird.yahoo.com>



> > what the default python sorting algorithm is on a list, but AFAIK you'd be
> > looking at a constant O(log 10)
>
> I'm not a mathematician - what does this mean, in layperson's terms?


O(log10) is a way of expressing the efficiency of an algorithm.
Its execution time is proportional (in the Order of) log10.
That is the time to process 100 items will be about twice the time to 
process 10 (rather than 100 times), since log(100) is 2 and log(10) is 1 

These measures are indicative only, but the bottom line is that it will 
scale to high volumes better than a linear algorithm would.

HTH,

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/b0b7f7b0/attachment-0001.htm>

From pine508 at hotmail.com  Mon Nov  9 18:49:04 2009
From: pine508 at hotmail.com (Che M)
Date: Mon, 9 Nov 2009 12:49:04 -0500
Subject: [Tutor] SQLite database not update correctly
In-Reply-To: <7296745c0911081233x56112233g5f4b847d77abda1b@mail.gmail.com>
References: <7296745c0911081110y255ed178xdd9c0aaa980216c0@mail.gmail.com>
	<SNT127-W5B9916A16DF60949BECC7E0AD0@phx.gbl>
Message-ID: <SNT127-W65FC97F67B3C95C5F37E31E0AC0@phx.gbl>



> It's working fine now, but actually I didn't write exactly what you suggested. 
> The "commit" method belongs to the connection, not to the cursor. Therefore, 
> in my script it should be conn.commit().

Whoops, you're quite right.  Went a little too fast there.  :D

Che
 		 	   		  
_________________________________________________________________
Windows 7: Unclutter your desktop.
http://go.microsoft.com/?linkid=9690331&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/d1fd3703/attachment.htm>

From davea at ieee.org  Tue Nov 10 01:05:26 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 09 Nov 2009 19:05:26 -0500
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
Message-ID: <4AF8AE46.7070405@ieee.org>

C.T. Matsumoto wrote:
> Hello All,
>
> I'm making a class and the parameters I'm feeding the class is getting quite
> large. I'm up
> to 8 now. Is there any rules of thumb for classes with a lot of parameters?
> I was thinking
> to put the parameters into a tuple and then in the __init__ of the class,
> iterate over the tuple
> and assign attributes.
>
> <snip>
>
>   
Don't do it.  Putting the parameters into a tuple only makes sense if 
they're somehow related to each other.  And if all the parameters are in 
the tuple, all you've done is to add another parenthesis pair around the 
argument list.

Now, if the parameters are related to each other (like the coordinates 
of an n-dimensional point), then it makes sense to group them.  As Alan 
said, a class can be good for that.  So can tuples, but only if there's 
some connection, or if they already were being treated as a tuple.

Note that if you want to declare your parameters as a tuple, you can use 
the * notation in the parameter list.  And if you want to pass the 
arguments as a tuple, you can use the * notation in the argument list.  
Or both.  But you need to have a reason, other than "too many parameters."


DaveA


From the_only_katala at verizon.net  Tue Nov 10 04:42:22 2009
From: the_only_katala at verizon.net (Katt)
Date: Mon, 09 Nov 2009 19:42:22 -0800
Subject: [Tutor] Tutor Digest, Vol 69,
 Issue 21 - Change a text string from a list and change it into an
 integer(WinXP/py2.6.2/Beginner)
References: <mailman.10978.1257479867.2806.tutor@python.org>
Message-ID: <4FA1C5B8E50B42AC806463F2836F4391@COMPUTER01>

First I want to thank the following people for your help: Wayne W., Shashwat 
A., and Alan G.  I appreciate the input/criticism as I continually learn 
more and more.

It seems that I am not sure if I am posting correctly to the right thread 
and am not sure if it has to do with the fact that I am getting the posts in 
a Digest manner.  Let me know if you have any suggestions.

Now back to message at hand:

> Message: 3
> Date: Thu, 5 Nov 2009 21:34:31 -0600
> From: Wayne Werner <waynejwerner at gmail.com>
> To: Katt <the_only_katala at verizon.net>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] Change a text string from a list and change it
> into an integer number.(WinXP/py2.6.2/Beginner)

>> <snip>
>> Currently the above code does not work unless I change the "if" statement
>> to say:
>> "if check_year == "c".
>>
> It works correctly for me. Try modifying your code:
>
> date = "cyear_11_05"
> date2 = date.split("_")
> check_year = date2[0]
> print check_year
>
> what does that do for you?
> -Wayne

For me it prints to the screen the following : cyear

> Message: 4
> Date: Fri, 6 Nov 2009 09:06:02 +0530
> From: Shashwat Anand <anand.shashwat at gmail.com>
> To: Katt <the_only_katala at verizon.net>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] Change a text string from a list and change it
> into an integer number.(WinXP/py2.6.2/Beginner)

> What do you want to say exactly ?
> is 'cyear' an integer ?
> let's say date1 = "1984_11_05"

Here cyear is just part of a text string which would be in the following: 
"cyear_11_05"

My goal(which recent I accomplished with the help of Shashwat A.'s function) 
was:
1. Split the string into a list. [cyear,11,5]
2. If date0 == "cyear" then it would replace cyear with the current year 
based on the computer's date.
    Then it would change this information into an integer and return it.
3. If date0 != "cyear" it would then change the number into an integer and 
return the information.

find code that I used below at end of this message.

> Then of course you can change it to an integer using following
> list-comprehension,
>>>> date1 = "1984_11_05"
>>>> date1_list = [int(i) for i in date1.split("_")]
>>>> date1_list
> [1984, 11, 5]
> or alternatively,
>>>> date1_list_alternate=map(int,date1.split("_"))
>>>> date1_list_alternate
> [1984, 11, 5]

I am still learning how to work with list comprehensions so I appreciate the 
multiple examples using the "for" and "map"
choices.

> also your code seems to work on my system.
>> date = "cyear_11_05"
>> date2 = date.split("_")
>> check_year = date2[0]
>> if check_year == "cyear":
>>   year = localtime().tm_year
>> else:
>>   year = int(date2[0])
>> print year

Found that I had not imported localtime from time.  It worked once I 
discovered this little oversight.


> Message: 5
> Date: Fri, 6 Nov 2009 09:27:46 +0530
> From: Shashwat Anand <anand.shashwat at gmail.com>
> To: Katt <the_only_katala at verizon.net>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] Change a text string from a list and change it
> into an integer number.(WinXP/py2.6.2/Beginner)
>
> import time
>
> def katt(d):
>    date0 = d.split("_")[0]
>    if date0 == "cyear":
>        return int(time.strftime("%Y"))
>    else:
>        return int(date0)
>
> print katt("cyear_11_05")
>print katt("1984_11_05")
>
> http://codepad.org/RBjKmNcA
>
>
> Hope this helps !

Thanks this helps.  I actually changed it a little so that I could include 
it into another function rather than its own seperate function.

My code is as follows:

if year_check == "cyear":
    year = int(strftime("%Y"))
else:
    year = int(year_check)
if month_check == "cmonth":
    month = int(strftime("%m"))
else:
    month = int(month_check)

I of course made sure to include the strftime in my import calls.  I may 
change the int(strftime("%Y")) for localtime().tm_year because I think I 
heard it returns the value as an integer, but will have to experiment.

Thanks again for the inspiration on this section of code.
Thanks again to all.

Katt 


From cspears2002 at yahoo.com  Tue Nov 10 04:48:21 2009
From: cspears2002 at yahoo.com (Christopher Spears)
Date: Mon, 9 Nov 2009 19:48:21 -0800 (PST)
Subject: [Tutor] parsing XML
Message-ID: <722256.79647.qm@web51602.mail.re2.yahoo.com>

Hi!

I need to parse several XML documents into a Python dictionary.  Is there a module that would be particularly good for this?  I heard beginners should start with ElementTree.  However, SAX seems to make a little more sense to me.  Any suggestions?


From anand.shashwat at gmail.com  Tue Nov 10 05:09:57 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 10 Nov 2009 09:39:57 +0530
Subject: [Tutor] Tutor Digest, Vol 69,
	Issue 21 - Change a text string from a list and change it into an
	integer(WinXP/py2.6.2/Beginner)
In-Reply-To: <4FA1C5B8E50B42AC806463F2836F4391@COMPUTER01>
References: <mailman.10978.1257479867.2806.tutor@python.org>
	<4FA1C5B8E50B42AC806463F2836F4391@COMPUTER01>
Message-ID: <d4ab53de0911092009l3c88b5c6p103aa87acaee05ca@mail.gmail.com>

On Tue, Nov 10, 2009 at 9:12 AM, Katt <the_only_katala at verizon.net> wrote:

> First I want to thank the following people for your help: Wayne W.,
> Shashwat A., and Alan G.  I appreciate the input/criticism as I continually
> learn more and more.
>
> It seems that I am not sure if I am posting correctly to the right thread
> and am not sure if it has to do with the fact that I am getting the posts in
> a Digest manner.  Let me know if you have any suggestions.
>

On http://mail.python.org/mailman/listinfo/tutor there is an option which
says - Would you like to receive list mail batched in a daily digest? May be
you checked it. Anyways you can try changing your settings to help remove
the Digest manner

<snip>


> I of course made sure to include the strftime in my import calls.  I may
> change the int(strftime("%Y")) for localtime().tm_year because I think I
> heard it returns the value as an integer, but will have to experiment.
>

Yes, it does. You can always check the type though.
>>> type(time.localtime().tm_year)
<type 'int'>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091110/c35f8f8a/attachment.htm>

From warpcat at gmail.com  Tue Nov 10 05:15:50 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Mon, 9 Nov 2009 20:15:50 -0800
Subject: [Tutor] parsing XML
In-Reply-To: <722256.79647.qm@web51602.mail.re2.yahoo.com>
References: <722256.79647.qm@web51602.mail.re2.yahoo.com>
Message-ID: <23cba4bf0911092015r53d83a9bjfef7634e321350bf@mail.gmail.com>

On Mon, Nov 9, 2009 at 7:48 PM, Christopher Spears <cspears2002 at yahoo.com>wrote:

> Hi!
>
> I need to parse several XML documents into a Python dictionary.  Is there a
> module that would be particularly good for this?  I heard beginners should
> start with ElementTree.  However, SAX seems to make a little more sense to
> me.  Any suggestions?
>

I'd recommend ElementTree.  I started out with minidom and wanted to rip my
face off.  Ok, exaggerating, but ElementTree made a lot more sense.
2c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091109/da0d9c8b/attachment-0001.htm>

From alan.gauld at btinternet.com  Tue Nov 10 06:53:39 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 10 Nov 2009 05:53:39 -0000
Subject: [Tutor] parsing XML
References: <722256.79647.qm@web51602.mail.re2.yahoo.com>
Message-ID: <hdav5b$cbs$1@ger.gmane.org>


"Christopher Spears" <cspears2002 at yahoo.com> wrote 

> I need to parse several XML documents into a Python dictionary.  
> Is there a module that would be particularly good for this?  
> I heard beginners should start with ElementTree.  
> However, SAX seems to make a little more sense to me.  

XML parsers fall into 2 groups. Those that parse the whole 
structure and create a tree of objects - usually accessed like 
a dictionary, and those that parse line by line looking for patterns. 
ElementTree is of the former, sax of the latter.

The former approach is usually slightly slower and more resource 
hungry but is much more flexible. SAX is fast but generally best 
if you only want to read something specific out of the XML.

If SAX makes sense for you and meets your needs go with it.

But ElementTree is worth persevering with if you need to do 
more complex editing of the XML. Its certainly easier than minidom.
(The other standard tree parser in Python)

Alan G.


From lie.1296 at gmail.com  Tue Nov 10 07:58:42 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Tue, 10 Nov 2009 17:58:42 +1100
Subject: [Tutor] Tutor Digest, Vol 69,
 Issue 21 - Change a text string from a list and change it into an
 integer(WinXP/py2.6.2/Beginner)
In-Reply-To: <4FA1C5B8E50B42AC806463F2836F4391@COMPUTER01>
References: <mailman.10978.1257479867.2806.tutor@python.org>
	<4FA1C5B8E50B42AC806463F2836F4391@COMPUTER01>
Message-ID: <hdb30m$k6a$1@ger.gmane.org>

Katt wrote:
> First I want to thank the following people for your help: Wayne W., 
> Shashwat A., and Alan G.  I appreciate the input/criticism as I 
> continually learn more and more.
> 
> It seems that I am not sure if I am posting correctly to the right 
> thread and am not sure if it has to do with the fact that I am getting 
> the posts in a Digest manner.  Let me know if you have any suggestions.

If you're using the digest version, you should copy the subject line and 
simply add RE: in front of it (don't add "Tutor Digest, Vol. 69, Issue 
21 - ") to preserve threading in a threaded newsreader. If you want to 
change the subject line but keep the threading (often discussion drifts 
away from the original topic), you could copy the Message ID header 
(better idea, don't change the subject line unless you're using a 
threaded newsreaded)

>> Message: 5
>> Date: Fri, 6 Nov 2009 09:27:46 +0530
>> From: Shashwat Anand <anand.shashwat at gmail.com>
>> To: Katt <the_only_katala at verizon.net>
>> Cc: tutor <tutor at python.org>
>> Subject: Re: [Tutor] Change a text string from a list and change it
>> into an integer number.(WinXP/py2.6.2/Beginner)
>>
>> import time
>>
>> def katt(d):
>>    date0 = d.split("_")[0]
>>    if date0 == "cyear":
>>        return int(time.strftime("%Y"))
>>    else:
>>        return int(date0)
>>
>> print katt("cyear_11_05")
>> print katt("1984_11_05")
>>
>> http://codepad.org/RBjKmNcA
>>
>>
>> Hope this helps !
> 
> Thanks this helps.  I actually changed it a little so that I could 
> include it into another function rather than its own seperate function.
> 
> My code is as follows:
> 
> if year_check == "cyear":
>    year = int(strftime("%Y"))
> else:
>    year = int(year_check)
> if month_check == "cmonth":
>    month = int(strftime("%m"))
> else:
>    month = int(month_check)

an alternative could be to us str.replace:

from time import strftime

current_year = strftime("%Y")
current_month = strftime("%m")
current_day = strftme("%d")

def katt(date):
     date = date.replace("cyear", current_year)
     date = date.replace("cmonth", current_month)
     date = date.replace("cday", current_day)
     return map(int, date.split("_"))

> 
> I of course made sure to include the strftime in my import calls.  I may 
> change the int(strftime("%Y")) for localtime().tm_year because I think I 
> heard it returns the value as an integer, but will have to experiment.
> 
> Thanks again for the inspiration on this section of code.
> Thanks again to all.
> 
> Katt
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


From sanelson at gmail.com  Tue Nov 10 11:04:16 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 10:04:16 +0000
Subject: [Tutor] Logfile multiplexing
Message-ID: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>

I have the following idea for multiplexing logfiles (ultimately into heapq):

import gzip

class LogFile:
    def __init__(self, filename, date):
        self.logfile = gzip.open(filename, 'r')
        for logline in self.logfile:
            self.line = logline
            self.stamp = self.timestamp(self.line)
            if self.stamp.startswith(date):
                break

    def timestamp(self, line):
        return " ".join(self.line.split()[3:5])

    def getline(self):
        nextline = self.line
        self.line = self.logfile.readline()
        self.stamp = self.timestamp(self.line)
        return nextline

The idea is that I can then do:

logs = [("log1", "[Nov/05/2009"), ("log2", "[Nov/05/2009"), ("log3",
"[Nov/05/2009"), ("log4", "[Nov/05/2009")]

I've tested it with one log (15M compressed, 211M uncompressed), and
it takes about 20 seconds to be ready to roll.

However, then I get unexpected behaviour:

~/system/tools/magpie $ python
Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import magpie
>>>magpie.l
<magpie.LogFile instance at 0x2b8045765bd8>
>>> magpie.l.stamp
'[05/Nov/2009:04:02:07 +0000]'
>>> magpie.l.getline()
89.151.119.195 - - [05/Nov/2009:04:02:07 +0000] "GET
/service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
HTTP/1.1" 200 50 "-" "-"

'89.151.119.195 - - [05/Nov/2009:04:02:07 +0000] "GET
/service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
HTTP/1.1" 200 50 "-" "-"\n'
>>> magpie.l.stamp
''
>>> magpie.l.getline()

''
>>>

I expected to be able to call getline() and get more lines...

a) What have I done wrong?
b) Is this an ok implementation?  What improvements could be made?
c) Is 20secs a reasonable time, or am I choosing a slow way to do this?

S.

From stefan_ml at behnel.de  Tue Nov 10 11:19:26 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Tue, 10 Nov 2009 11:19:26 +0100
Subject: [Tutor] parsing XML
In-Reply-To: <hdav5b$cbs$1@ger.gmane.org>
References: <722256.79647.qm@web51602.mail.re2.yahoo.com>
	<hdav5b$cbs$1@ger.gmane.org>
Message-ID: <hdbenf$vvg$1@ger.gmane.org>

Alan Gauld, 10.11.2009 06:53:
> "Christopher Spears" <cspears2002 at yahoo.com> wrote
>> I need to parse several XML documents into a Python dictionary.  Is
>> there a module that would be particularly good for this?  I heard
>> beginners should start with ElementTree.  However, SAX seems to make a
>> little more sense to me.  

Note that ElementTree provides both a SAX-like interface (look for the
'target' property of parsers) and an incremental parser (iterparse). So the
question is not "ElementTree or SAX?", it's more like "how much time do I
have to implement, run and maintain the code?".


> XML parsers fall into 2 groups. Those that parse the whole structure and
> create a tree of objects - usually accessed like a dictionary, and those
> that parse line by line looking for patterns.

Except that parsing XML is not about lines but about bytes in a stream.


> The former approach is usually slightly slower and more resource hungry

I'd better leave the judgement about this statement to a benchmark.


> If SAX makes sense for you and meets your needs go with it.

I'd change this to:

Unless you really know what you are doing and you have proven in benchmarks
that SAX is substantially faster for the problem at hand, don't use SAX.

Stefan


From c.t.matsumoto at gmail.com  Tue Nov 10 08:32:03 2009
From: c.t.matsumoto at gmail.com (C.T. Matsumoto)
Date: Tue, 10 Nov 2009 08:32:03 +0100
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <4AF8AE46.7070405@ieee.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
	<4AF8AE46.7070405@ieee.org>
Message-ID: <8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>

This reply is also to Alan's suggestion to provide more context.

The situation concerns databases, where in one schema table I've got a
'compare list'.
This list provides defines 2 tables that need to be paired and then
compared. Before
any comparing happens I 'filter' the compare list doing several validation
tests. Once
all the test succeed (exists, with the necessary things to compare) I'm left
with the
information describing the 2 tables:

reference_table_name
reference_dburi
reference_rows
test_table_name
test_dburi
test_rows
keys

It is with this information that I wanted to create a class object. Filling
a 'master list'
of 'CompareItem' objects.

Cheers,

T

On Tue, Nov 10, 2009 at 1:05 AM, Dave Angel <davea at ieee.org> wrote:

> C.T. Matsumoto wrote:
>
>> Hello All,
>>
>> I'm making a class and the parameters I'm feeding the class is getting
>> quite
>> large. I'm up
>> to 8 now. Is there any rules of thumb for classes with a lot of
>> parameters?
>> I was thinking
>> to put the parameters into a tuple and then in the __init__ of the class,
>> iterate over the tuple
>> and assign attributes.
>>
>> <snip>
>>
>>
>>
> Don't do it.  Putting the parameters into a tuple only makes sense if
> they're somehow related to each other.  And if all the parameters are in the
> tuple, all you've done is to add another parenthesis pair around the
> argument list.
>
> Now, if the parameters are related to each other (like the coordinates of
> an n-dimensional point), then it makes sense to group them.  As Alan said, a
> class can be good for that.  So can tuples, but only if there's some
> connection, or if they already were being treated as a tuple.
>
> Note that if you want to declare your parameters as a tuple, you can use
> the * notation in the parameter list.  And if you want to pass the arguments
> as a tuple, you can use the * notation in the argument list.  Or both.  But
> you need to have a reason, other than "too many parameters."
>
>
> DaveA
>
>


-- 
Todd Matsumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091110/6cb6dcbf/attachment.htm>

From kent37 at tds.net  Tue Nov 10 13:49:55 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 10 Nov 2009 07:49:55 -0500
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
Message-ID: <1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>

On Tue, Nov 10, 2009 at 5:04 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> I have the following idea for multiplexing logfiles (ultimately into heapq):
>
> import gzip
>
> class LogFile:
> ? ?def __init__(self, filename, date):
> ? ? ? ?self.logfile = gzip.open(filename, 'r')
> ? ? ? ?for logline in self.logfile:
> ? ? ? ? ? ?self.line = logline
> ? ? ? ? ? ?self.stamp = self.timestamp(self.line)
> ? ? ? ? ? ?if self.stamp.startswith(date):
> ? ? ? ? ? ? ? ?break
>
> ? ?def timestamp(self, line):
> ? ? ? ?return " ".join(self.line.split()[3:5])
>
> ? ?def getline(self):
> ? ? ? ?nextline = self.line
> ? ? ? ?self.line = self.logfile.readline()
> ? ? ? ?self.stamp = self.timestamp(self.line)
> ? ? ? ?return nextline

One error is that the initial line will be the same as the first
response from getline(). So you should call getline() before trying to
access a line. Also you may need to filter all lines - what if there
is jitter at midnight, or the log rolls over before the end.

More important, though, you are pretty much writing your own iterator
without using the iterator protocol. I would write this as:

class LogFile:
   def __init__(self, filename, date):
       self.logfile = gzip.open(filename, 'r')
       self.date = date

   def __iter__(self)
       for logline in self.logfile:
           stamp = self.timestamp(logline)
           if stamp.startswith(date):
               yield (stamp, logline)

   def timestamp(self, line):
       return " ".join(self.line.split()[3:5])


Then you could use this as
l = LogFile("log1", "[Nov/05/2009")
for stamp, line in l:
    print stamp, line

or use one of the merging recipes I pointed to earlier.

> The idea is that I can then do:
>
> logs = [("log1", "[Nov/05/2009"), ("log2", "[Nov/05/2009"), ("log3",
> "[Nov/05/2009"), ("log4", "[Nov/05/2009")]

or
logs = [LogFile("log1", "[Nov/05/2009"), LogFile("log2",
"[Nov/05/2009"), LogFile("log3",
"[Nov/05/2009"), LogFile("log4", "[Nov/05/2009")]

> I've tested it with one log (15M compressed, 211M uncompressed), and
> it takes about 20 seconds to be ready to roll.
>
> However, then I get unexpected behaviour:
>
> ~/system/tools/magpie $ python
> Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
> [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import magpie
>>>>magpie.l
> <magpie.LogFile instance at 0x2b8045765bd8>
>>>> magpie.l.stamp
> '[05/Nov/2009:04:02:07 +0000]'
>>>> magpie.l.getline()
> 89.151.119.195 - - [05/Nov/2009:04:02:07 +0000] "GET
> /service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
> HTTP/1.1" 200 50 "-" "-"
>
> '89.151.119.195 - - [05/Nov/2009:04:02:07 +0000] "GET
> /service.php?s=nav&arg[]=&arg[]=home&q=ubercrumb/node%2F20812
> HTTP/1.1" 200 50 "-" "-"\n'
>>>> magpie.l.stamp
> ''
>>>> magpie.l.getline()
>
> ''
>>>>
>
> I expected to be able to call getline() and get more lines...

You are reading through the entire file on load because your timestamp
check is failing. You are filtering out the whole file and returning
just the last line. Check the dates you are supplying vs the actual
data - they don't match.

Kent

From sanelson at gmail.com  Tue Nov 10 14:25:55 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 13:25:55 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
Message-ID: <b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>

Hi Kent,

> One error is that the initial line will be the same as the first
> response from getline(). So you should call getline() before trying to
> access a line. Also you may need to filter all lines - what if there
> is jitter at midnight, or the log rolls over before the end.

Well ultimately I definitely have to filter two logfiles per day, as
logs rotate at 0400.  Or do you mean something else?

> More important, though, you are pretty much writing your own iterator
> without using the iterator protocol. I would write this as:
>
> class LogFile:
> ? def __init__(self, filename, date):
> ? ? ? self.logfile = gzip.open(filename, 'r')
> ? ? ? self.date = date
>
> ? def __iter__(self)
> ? ? ? for logline in self.logfile:
> ? ? ? ? ? stamp = self.timestamp(logline)
> ? ? ? ? ? if stamp.startswith(date):
> ? ? ? ? ? ? ? yield (stamp, logline)
>
> ? def timestamp(self, line):
> ? ? ? return " ".join(self.line.split()[3:5])

Right - I think I understand that.

>From here I get:

import gzip

class LogFile:
    def __init__(self, filename, date):
        self.logfile = gzip.open(filename, 'r')
        self.date = date

    def __iter__(self):
        for logline in self.logfile:
            stamp = self.timestamp(logline)
            if stamp.startswith(date):
                yield (stamp, logline)

    def timestamp(self, line):
        return " ".join(self.line.split()[3:5])

l = LogFile("/home/stephen/access_log-20091105.gz", "[04/Nov/2009")

I get:

Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import kent
>>> kent.l
<kent.LogFile instance at 0x2afb05142bd8>
>>> dir(kent.l)
['__doc__', '__init__', '__iter__', '__module__', 'date', 'logfile',
'timestamp']
>>> for line in kent.l:
...   print line
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "kent.py", line 10, in __iter__
    stamp = self.timestamp(logline)
  File "kent.py", line 15, in timestamp
    return " ".join(self.line.split()[3:5])
AttributeError: LogFile instance has no attribute 'line'
>>> for stamp,line in kent.l:
...   print stamp,line
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "kent.py", line 10, in __iter__
    stamp = self.timestamp(logline)
  File "kent.py", line 15, in timestamp
    return " ".join(self.line.split()[3:5])
AttributeError: LogFile instance has no attribute 'line'
>>> for stamp,logline in kent.l:
...   print stamp,logline
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "kent.py", line 10, in __iter__
    stamp = self.timestamp(logline)
  File "kent.py", line 15, in timestamp
    return " ".join(self.line.split()[3:5])
AttributeError: LogFile instance has no attribute 'line'


> You are reading through the entire file on load because your timestamp
> check is failing. You are filtering out the whole file and returning
> just the last line. Check the dates you are supplying vs the actual
> data - they don't match.

Yes, I found that out in the end!  Thanks!

S.

From hugo.yoshi at gmail.com  Tue Nov 10 14:35:16 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Tue, 10 Nov 2009 14:35:16 +0100
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com> 
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com> 
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
Message-ID: <29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>

On Tue, Nov 10, 2009 at 2:25 PM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
><snip>
>
>From here I get:
>
> import gzip
>
> class LogFile:
>    def __init__(self, filename, date):
>        self.logfile = gzip.open(filename, 'r')
>        self.date = date
>
>    def __iter__(self):
>        for logline in self.logfile:
>            stamp = self.timestamp(logline)
>            if stamp.startswith(date):
>                yield (stamp, logline)
>
>    def timestamp(self, line):
>        return " ".join(self.line.split()[3:5])
>
> l = LogFile("/home/stephen/access_log-20091105.gz", "[04/Nov/2009")
>
> I get:
>
> Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
> [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import kent
>>>> kent.l
> <kent.LogFile instance at 0x2afb05142bd8>
>>>> dir(kent.l)
> ['__doc__', '__init__', '__iter__', '__module__', 'date', 'logfile',
> 'timestamp']
>>>> for line in kent.l:
> ...   print line
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>>>> for stamp,line in kent.l:
> ...   print stamp,line
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>>>> for stamp,logline in kent.l:
> ...   print stamp,logline
> ...
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>  File "kent.py", line 10, in __iter__
>    stamp = self.timestamp(logline)
>  File "kent.py", line 15, in timestamp
>    return " ".join(self.line.split()[3:5])
> AttributeError: LogFile instance has no attribute 'line'
>

probably that line should have been " ".join(line.split()[3:5]), i.e.
no self. The line variable is a supplied argument.

Hugo

From sanelson at gmail.com  Tue Nov 10 14:42:21 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 13:42:21 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
Message-ID: <b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>

Hi,

> probably that line should have been " ".join(line.split()[3:5]), i.e.
> no self. The line variable is a supplied argument.

Now I get:


Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import kent
>>> kent.l
<kent.LogFile instance at 0x2b375eac0b90>
>>> for a, b in kent.l
  File "<stdin>", line 1
    for a, b in kent.l
                     ^
SyntaxError: invalid syntax
>>> for a, b in kent.l:
...   print a, b
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "kent.py", line 11, in __iter__
    if stamp.startswith(date):
NameError: global name 'date' is not defined

How does __iter__ know about date?  Should that be self.date?

S.

From rabidpoobear at gmail.com  Tue Nov 10 15:00:00 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Tue, 10 Nov 2009 08:00:00 -0600
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
Message-ID: <dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>

> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>   File "kent.py", line 11, in __iter__
>    if stamp.startswith(date):
> NameError: global name 'date' is not defined
>
> How does __iter__ know about date?  Should that be self.date?
>

Yes.  self.date is set in the constructor.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091110/414809be/attachment.htm>

From lie.1296 at gmail.com  Tue Nov 10 15:26:07 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Wed, 11 Nov 2009 01:26:07 +1100
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
Message-ID: <hdbt7j$tjo$1@ger.gmane.org>

C.T. Matsumoto wrote:
> Hello All,
> 
> I'm making a class and the parameters I'm feeding the class is getting 
> quite large. I'm up
> to 8 now. Is there any rules of thumb for classes with a lot of 
> parameters? I was thinking
> to put the parameters into a tuple and then in the __init__ of the 
> class, iterate over the tuple
> and assign attributes.
> 
> Right now my class basically looks like this:
> 
> class Foo(object):
>     def __init__(self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8):
>         ...
> 

There are several "tricks":

1. pass a tuple

class Foo(object):
     def __init__(self, args):
         ....
Foo(("blah", 1, 2, 3))

bad, just adds more parentheses and make argument unpacking more 
complex, as Dave Angel said, an exception would be if the values are 
related like coordinates

2. use default value and named argument

class Foo(object):
     def __init__(self, arg0="", arg1=1, arg2=2, arg3=3):
         ....
Foo("blah")

simplifies the caller, but the function signature is a bit complex. You 
might want to split the signature into lines:
     def __init__(self,
                  arg0="",
                  arg1=1,
                  arg2=2,
                  arg3=3):

3. use *args and/or **kwargs

class Foo(object):
     def __init__(self, *args):
         ....

unpacking argument becomes complex

4. your class might be doing too much! Look for ways to split it into 
several smaller classes


From sanelson at gmail.com  Tue Nov 10 16:48:43 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 15:48:43 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
Message-ID: <b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>

Hello,

On Tue, Nov 10, 2009 at 2:00 PM, Luke Paireepinart
<rabidpoobear at gmail.com> wrote:
>
>> Traceback (most recent call last):
>> ?File "<stdin>", line 1, in ?
>> ?File "kent.py", line 11, in __iter__
>> ? ?if stamp.startswith(date):
>> NameError: global name 'date' is not defined
>>
>> How does __iter__ know about date? ?Should that be self.date?
>
> Yes.? self.date is set in the constructor.

OK, so now i've given it the full load of logs:

>>> for time, entry in kent.logs:
...   print time, entry
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: too many values to unpack

How do I get around this?!

S.

From sanelson at gmail.com  Tue Nov 10 16:59:59 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 15:59:59 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
Message-ID: <b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>

On Tue, Nov 10, 2009 at 3:48 PM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:

> OK, so now i've given it the full load of logs:
>
>>>> for time, entry in kent.logs:
> ... ? print time, entry
> ...
> Traceback (most recent call last):
> ?File "<stdin>", line 1, in ?
> ValueError: too many values to unpack
>
> How do I get around this?!

Erm, and now it's failing with only one logfile...

Code here:

http://pastebin.ca/1665013

S.

From sanelson at gmail.com  Tue Nov 10 17:25:13 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 10 Nov 2009 16:25:13 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
Message-ID: <b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>

On Tue, Nov 10, 2009 at 3:59 PM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> On Tue, Nov 10, 2009 at 3:48 PM, Stephen Nelson-Smith
> <sanelson at gmail.com> wrote:
>
>> OK, so now i've given it the full load of logs:
>>
>>>>> for time, entry in kent.logs:
>> ... ? print time, entry
>> ...
>> Traceback (most recent call last):
>> ?File "<stdin>", line 1, in ?
>> ValueError: too many values to unpack
>>
>> How do I get around this?!
>
> Erm, and now it's failing with only one logfile...
>
> Code here:
>
> http://pastebin.ca/1665013

OK - me being dumb.

So what I want to do is be able to multiplex the files - ie read the
next line of all 12 files at once, filter them accordingly, and then
write them out to one combined file.

My old code did this;

min((x.stamp, x) for x in logs)

What's the best way to do this now I'm using an iterable LogFile class?

S.

From kent37 at tds.net  Tue Nov 10 19:03:56 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 10 Nov 2009 13:03:56 -0500
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
Message-ID: <1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>

On Tue, Nov 10, 2009 at 11:25 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:

> So what I want to do is be able to multiplex the files - ie read the
> next line of all 12 files at once, filter them accordingly, and then
> write them out to one combined file.
>
> My old code did this;
>
> min((x.stamp, x) for x in logs)
>
> What's the best way to do this now I'm using an iterable LogFile class?

See the Python Cookbook recipes I referenced earlier.
http://code.activestate.com/recipes/491285/
http://code.activestate.com/recipes/535160/

Note they won't fix up the jumbled ordering of your files but I don't
think they will break from it either...

Kent

From davea at ieee.org  Tue Nov 10 20:21:18 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 10 Nov 2009 14:21:18 -0500
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>	
	<4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
Message-ID: <4AF9BD2E.9020601@ieee.org>

(Removing out of sequence history)

DaveA

instances of that class.  Better than using tuples.
makes sense to make a class to hold the seven parameters, and pass two
If you're passing two sets of 7 parameters to the same function, it probably




From davea at ieee.org  Tue Nov 10 20:25:16 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 10 Nov 2009 14:25:16 -0500
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
Message-ID: <4AF9BE1C.80906@ieee.org>

Stephen Nelson-Smith wrote:
> <snip>
> NameError: global name 'date' is not defined
>
> How does __iter__ know about date?  Should that be self.date?
>
> S.
>
>   
Yes.



From alan.gauld at btinternet.com  Tue Nov 10 22:12:46 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 10 Nov 2009 21:12:46 -0000
Subject: [Tutor] class initialization with a lot of parameters
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com><4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
Message-ID: <hdcl0n$r17$1@ger.gmane.org>


"C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote 

> This list provides defines 2 tables that need to be paired and then
> compared. 

So two instances of a TestTable object maybe?

> reference_table_name
> reference_dburi
> reference_rows
> test_table_name
> test_dburi
> test_rows
> keys

Looks like two TestTable objects and a set of keys?
And maybe TestTable has a comparison method that compares 
one table to another? Or maybe can index a row based on a key?
Maybe the Rows are objects too and they can compare themselves?

Lots of possibilities depending on your problem.


Just a thought.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From rabidpoobear at gmail.com  Tue Nov 10 22:23:03 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Tue, 10 Nov 2009 15:23:03 -0600
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <4AF9BD2E.9020601@ieee.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
	<4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
	<4AF9BD2E.9020601@ieee.org>
Message-ID: <dfeb4470911101323w726aac85j6365538c71bc08be@mail.gmail.com>

On Tue, Nov 10, 2009 at 1:21 PM, Dave Angel <davea at ieee.org> wrote:

> (Removing out of sequence history)
>
> DaveA
>
> instances of that class.  Better than using tuples.
> makes sense to make a class to hold the seven parameters, and pass two
> If you're passing two sets of 7 parameters to the same function, it
> probably
>
> I'm sorry Dave but I'm not really sure what's going on with this e-mail.
Did it get truncated / reordered at some point or am I reading it wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091110/cf9690e1/attachment.htm>

From davea at ieee.org  Tue Nov 10 22:43:08 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 10 Nov 2009 16:43:08 -0500
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <dfeb4470911101323w726aac85j6365538c71bc08be@mail.gmail.com>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>	
	<4AF8AE46.7070405@ieee.org>	
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>	
	<4AF9BD2E.9020601@ieee.org>
	<dfeb4470911101323w726aac85j6365538c71bc08be@mail.gmail.com>
Message-ID: <4AF9DE6C.7060403@ieee.org>

Luke Paireepinart wrote:
> On Tue, Nov 10, 2009 at 1:21 PM, Dave Angel <davea at ieee.org> wrote:
>
>   
>> (Removing out of sequence history)
>>
>> DaveA
>>
>> instances of that class.  Better than using tuples.
>> makes sense to make a class to hold the seven parameters, and pass two
>> If you're passing two sets of 7 parameters to the same function, it
>> probably
>>
>> I'm sorry Dave but I'm not really sure what's going on with this e-mail.
>>     
> Did it get truncated / reordered at some point or am I reading it wrong?
>
>   
Perhaps it was too subtle.  I was trying to show what top-posting feels 
like to me.  When the messages are out of order, it takes extra effort 
to interpret them.  So I was doing the same with the lines of my message.



From alan.gauld at btinternet.com  Wed Nov 11 00:09:16 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 10 Nov 2009 23:09:16 -0000
Subject: [Tutor] parsing XML
References: <722256.79647.qm@web51602.mail.re2.yahoo.com><hdav5b$cbs$1@ger.gmane.org>
	<hdbenf$vvg$1@ger.gmane.org>
Message-ID: <hdcrr4$isk$1@ger.gmane.org>

"Stefan Behnel" <stefan_ml at behnel.de> wrote

> Note that ElementTree provides both a SAX-like interface (look for the
> 'target' property of parsers) and an incremental parser (iterparse).

Interesting, I didn't realise that.
I've only ever used it to build a tree.

>> XML parsers fall into 2 groups. Those that parse the whole structure and
>> create a tree of objects - usually accessed like a dictionary, and those
>> that parse line by line looking for patterns.
>
> Except that parsing XML is not about lines but about bytes in a stream.

Indeed, I should probably have said element by element.

>> The former approach is usually slightly slower and more resource hungry
>
> I'd better leave the judgement about this statement to a benchmark.

It depends on what you are doing obviously. If you need to parse the whole
message therer will be very little difference, but a sax style parser often
can complete its job after reading a short section of the document.
Tree parsers generally require the whole document to be completed
to finish building the tree.

>> If SAX makes sense for you and meets your needs go with it.
>
> I'd change this to:
>
> Unless you really know what you are doing and you have proven in 
> benchmarks
> that SAX is substantially faster for the problem at hand, don't use SAX.

Even if speed is not the critical factor, if sax makes more sense to you
that ElementTree, and it will do what you want use sax. Theer are plenty 
industrial
strength applications using sax parsers, and if the requirement is simple 
it is
no harder to maintain than a poorly understood ElementTree implementation!

Personally I find ElementTree easier to work with, but if the OP prefers 
sax
and can make it work for him then there is nothing wrong with using it.

And familiarity with sax style parsers is arguably a more transferrable
skill than ElementTree should he need to work with C or Pascal - or
even Java etc

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



From warpcat at gmail.com  Wed Nov 11 02:12:02 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Tue, 10 Nov 2009 17:12:02 -0800
Subject: [Tutor] Querying a module's package path?
Message-ID: <23cba4bf0911101712s4fac201el4b813f90d36334c9@mail.gmail.com>

Presume I have a package 'a' like this:

   - /pystuff  (added to sys.path)
   - /a  (start of my package)
      - __init__.py
         - /b
            - __init__.py
            - module.py

to import module.py:
import *a.b.module*

What I'm trying to find is a way to query exactly what I typed above (in
bold):  Inside of module.py, can it query it's path back to the package
root?  Printing __file__ inside of module.py is close:
c:/pystuff/a/b/module.py

But I just want '*a.b.module'*
I can search sys.path for paths, remove them from __file__, switch slashes
to dots, strip extensions, etc.  But I've learned that there's usually
something easier in Python that I'm just missing.  I thought maybe the
inspect module, but I didn't have any luck there, nor did my Google queries.

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091110/480a9196/attachment.htm>

From lie.1296 at gmail.com  Wed Nov 11 08:15:13 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Wed, 11 Nov 2009 18:15:13 +1100
Subject: [Tutor] Querying a module's package path?
In-Reply-To: <23cba4bf0911101712s4fac201el4b813f90d36334c9@mail.gmail.com>
References: <23cba4bf0911101712s4fac201el4b813f90d36334c9@mail.gmail.com>
Message-ID: <hddobt$li9$1@ger.gmane.org>

Eric Pavey wrote:
> Presume I have a package 'a' like this:
> 
>     * /pystuff  (added to sys.path)
>           o /a  (start of my package)
>                 + __init__.py
>                 + /b  
>                       # __init__.py
>                       # module.py
> 
> to import module.py:
> import *a.b.module*
> 
> What I'm trying to find is a way to query exactly what I typed above (in 
> bold):  Inside of module.py, can it query it's path back to the package 
> root?  

Why would you want that? Can you describe the problem you're having now 
that requires you to know the module's package path? Right now, you're 
just describing a solution to your problem, not the problem itself. 
Maybe we can find another solution that doesn't involve knowing the 
module's package path.


From c.t.matsumoto at gmail.com  Wed Nov 11 08:40:12 2009
From: c.t.matsumoto at gmail.com (C.T. Matsumoto)
Date: Wed, 11 Nov 2009 08:40:12 +0100
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <hdcl0n$r17$1@ger.gmane.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
	<4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
	<hdcl0n$r17$1@ger.gmane.org>
Message-ID: <8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com>

Thanks for the ideas,

I see I still don't have the hang of this context thing! I still haven't
provided enough
context. So here goes again, to show the entire chain. This might change the
discussion to be about design practice but it will give overview of how I'm
using
the class in question.

First I've got a db class

class DB(object):
    """ This does all the database handling.
    """
    ...

Then I've got a class to filter a list of potential tables to be compared.
These
tables need to be tested.

class CompareTableList(DB):
    """ Make a master list, from a source list of compare tables
         that need to be compared.
         If the tables in the list pass the tests make a CompareItem.
         Store all the CompareItems in a compare_list.
    """
    ...

CompareItem is the class in question.

CompareItem(object):
    def __init__(self, ref_table, ref_dburi, ref_rows, test_table,
test_dburi, test_rows, keys):
        ...
    ...

This would be your suggested TestTable only it already has the pair and is
ready to be
compared. Rather than a method compare is a class.

Compare(CompareTableList):
    """ Foreach CompareItem in the CompareTableList.compare_list. Workout
         the differences and store difference in something that can format
         a report.
    """
    ...

Cheers,

T

On Tue, Nov 10, 2009 at 10:12 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote
>
>> This list provides defines 2 tables that need to be paired and then
>> compared.
>>
>
> So two instances of a TestTable object maybe?
>
>
>  reference_table_name
>> reference_dburi
>> reference_rows
>> test_table_name
>> test_dburi
>> test_rows
>> keys
>>
>
> Looks like two TestTable objects and a set of keys?
> And maybe TestTable has a comparison method that compares one table to
> another? Or maybe can index a row based on a key?
> Maybe the Rows are objects too and they can compare themselves?
>
> Lots of possibilities depending on your problem.
>
>
> Just a thought.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Todd Matsumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/b5b7e326/attachment.htm>

From alan.gauld at btinternet.com  Wed Nov 11 10:28:54 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 11 Nov 2009 09:28:54 -0000
Subject: [Tutor] class initialization with a lot of parameters
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com><4AF8AE46.7070405@ieee.org><8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com><hdcl0n$r17$1@ger.gmane.org>
	<8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com>
Message-ID: <hde04u$b2u$1@ger.gmane.org>

"C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote

> First I've got a db class
>
> class DB(object):
>    """ This does all the database handling.
>    """

That's fine.

> Then I've got a class to filter a list of potential tables to be 
> compared.
> These tables need to be tested.

OK, Could that be a method of your database?

> class CompareTableList(DB):
>    """ Make a master list, from a source list of compare tables
>         that need to be compared.
>         If the tables in the list pass the tests make a CompareItem.
>         Store all the CompareItems in a compare_list.
>    """

I still think that from an OOP sense it would be m,ore logical to
have a Table cklass that knows hhow to compare itself to another table.

You are trying to build an object which is a function, thats rarely
a good idea. You wind up with the worst of procedural and OOP
worlds coming together.

> CompareItem is the class in question.
>
> CompareItem(object):
>    def __init__(self, ref_table, ref_dburi, ref_rows, test_table,
> test_dburi, test_rows, keys):
>        ...
>    ...
>
> This would be your suggested TestTable only it already has the pair and 
> is
> ready to be compared. Rather than a method compare is a class.

I suspect thats your problem. cComparing things is normally an
operation of a class not a class in its own right. A compare class
would need to inspect the internal details of at least 2 objects.
But objects should manage their own data not expose it to third parties.
So either you have to pass in all the object attributes you want to know
about - long parameter  lists or you pass in two objects and violate data
hiding within the comparison.

> Compare(CompareTableList):
>    """ Foreach CompareItem in the CompareTableList.compare_list. Workout
>         the differences and store difference in something that can format
>         a report.
>    """

Again this looks like a method of your database.

db.compare(tables)
     foreach table in tables:
           if table.compare(masterTable?):
                 storeDifferences()

or

db.compare(table_sets):
     for table1, table2 in table_sets:
           if table1.compare(table2)
                storeDifferences()

Or similar.

By trying to create objects which are functions instead of making
the function a method of the objects that they act on you are
complicating your code.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From sanelson at gmail.com  Wed Nov 11 10:46:24 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Wed, 11 Nov 2009 09:46:24 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
	<1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
Message-ID: <b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>

Hi Kent,

> See the Python Cookbook recipes I referenced earlier.
> http://code.activestate.com/recipes/491285/
> http://code.activestate.com/recipes/535160/
>
> Note they won't fix up the jumbled ordering of your files but I don't
> think they will break from it either...

That's exactly the problem.  I do need the end product to be in order.
 The problem is that on my current design I'm still getting stuff out
of sync.  What I do at present is this:

Each of these columns is a log file (logfile A, B C D), with a number
of entries, slightly out of order.

1      1      1      1
2      2      2      2
3      3      3      3
A      B     C      D       ...

I currently take a slice through all (12) logs, and stick them in a
priority queue, and pop them off in order.  The problem comes that the
next slice could easily contain timestamps before the entries in  the
previous slice.  So I either need some kind of lookahead capability,
or I need to be feeding the queue one at a time, and hope the queue is
of sufficient size to cover the delta between the various logs.  It
all feels a bit brittle and wrong.

I don't really want to admit defeat and have a cron job sort the logs
before entry.  Anyone got any other ideas?

Thanks all - I'm really learning a lot.

S.

From alan.gauld at btinternet.com  Wed Nov 11 11:05:09 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 11 Nov 2009 10:05:09 -0000
Subject: [Tutor] Logfile multiplexing
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com><1c2a2c590911100449i4861bac8ib2ae41f11b1c63fe@mail.gmail.com><b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com><29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com><b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com><dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com><b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com><b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com><b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com><1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
	<b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>
Message-ID: <hde28u$ig2$1@ger.gmane.org>

"Stephen Nelson-Smith" <sanelson at gmail.com> wrote 

> I don't really want to admit defeat and have a cron job sort the logs
> before entry.  Anyone got any other ideas?

Why would that be admitting defeat?
Its normal when processing large data volumes to break the 
process into discrete steps that can be done in bulk and 
in parallel - thats how mainframes have worked for years.

Its a perfectly valid approach to preprocess your input data
so that your  main processing can be more efficient. The trick 
is to spread the load where the task is repeatable (eg sorting 
a file - and even the filteriing of your php's) and maximise 
the efficiency where it is not (ie merging).

So it would be valid to have a set of batch jobs removing 
the phps, followed by another job to sort each file, then finally 
merge the reduced files. The initial filtering and sorting can 
both be done on a per file basis in parallel.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From c.t.matsumoto at gmail.com  Wed Nov 11 11:08:25 2009
From: c.t.matsumoto at gmail.com (C.T. Matsumoto)
Date: Wed, 11 Nov 2009 11:08:25 +0100
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <hde04u$b2u$1@ger.gmane.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
	<4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
	<hdcl0n$r17$1@ger.gmane.org>
	<8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com>
	<hde04u$b2u$1@ger.gmane.org>
Message-ID: <8c5079cb0911110208k16ba2278w9daa1c5fe1cd0e07@mail.gmail.com>

Great,

I do see my objects working as functions so my OOP understanding needs
development. I've got to roll this around to come up with a design which
will be more OOP centered and change the code accordingly. To start
I could move CompareTableList into DB. This will make a list of tables
that need to retrieved.

The Table object you described I find more complicated if each table stands
on its own it is decoupled from its compare partner. I suppose a function
that
pairs the tables, feeding a Table object to its partner Table.compare
method.

Hmm ... This has got to sink in.

T

On Wed, Nov 11, 2009 at 10:28 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> "C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote
>
>  First I've got a db class
>>
>> class DB(object):
>>   """ This does all the database handling.
>>   """
>>
>
> That's fine.
>
>
>  Then I've got a class to filter a list of potential tables to be compared.
>> These tables need to be tested.
>>
>
> OK, Could that be a method of your database?
>
>
>  class CompareTableList(DB):
>>   """ Make a master list, from a source list of compare tables
>>        that need to be compared.
>>        If the tables in the list pass the tests make a CompareItem.
>>        Store all the CompareItems in a compare_list.
>>   """
>>
>
> I still think that from an OOP sense it would be m,ore logical to
> have a Table cklass that knows hhow to compare itself to another table.
>
> You are trying to build an object which is a function, thats rarely
> a good idea. You wind up with the worst of procedural and OOP
> worlds coming together.
>
>
>  CompareItem is the class in question.
>>
>> CompareItem(object):
>>   def __init__(self, ref_table, ref_dburi, ref_rows, test_table,
>> test_dburi, test_rows, keys):
>>       ...
>>   ...
>>
>> This would be your suggested TestTable only it already has the pair and is
>> ready to be compared. Rather than a method compare is a class.
>>
>
> I suspect thats your problem. cComparing things is normally an
> operation of a class not a class in its own right. A compare class
> would need to inspect the internal details of at least 2 objects.
> But objects should manage their own data not expose it to third parties.
> So either you have to pass in all the object attributes you want to know
> about - long parameter  lists or you pass in two objects and violate data
> hiding within the comparison.
>
>
>  Compare(CompareTableList):
>>   """ Foreach CompareItem in the CompareTableList.compare_list. Workout
>>        the differences and store difference in something that can format
>>        a report.
>>   """
>>
>
> Again this looks like a method of your database.
>
> db.compare(tables)
>    foreach table in tables:
>          if table.compare(masterTable?):
>                storeDifferences()
>
> or
>
> db.compare(table_sets):
>    for table1, table2 in table_sets:
>          if table1.compare(table2)
>               storeDifferences()
>
> Or similar.
>
> By trying to create objects which are functions instead of making
> the function a method of the objects that they act on you are
> complicating your code.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Todd Matsumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/da0d7689/attachment-0001.htm>

From sanelson at gmail.com  Wed Nov 11 11:34:32 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Wed, 11 Nov 2009 10:34:32 +0000
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <hde28u$ig2$1@ger.gmane.org>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
	<1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
	<b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>
	<hde28u$ig2$1@ger.gmane.org>
Message-ID: <b6131fdc0911110234i3b1d6a22s2bc42af9b83aa0b0@mail.gmail.com>

Hi,

On Wed, Nov 11, 2009 at 10:05 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "Stephen Nelson-Smith" <sanelson at gmail.com> wrote
>>
>> I don't really want to admit defeat and have a cron job sort the logs
>> before entry. ?Anyone got any other ideas?
>
> Why would that be admitting defeat?

Well, it mean admitting defeat on solving the problem in python.  Yes
in practical terms, I should probably preprocess the data, but as a
programming exercise, learning how to sort a number of files into one
is something I'd like to crack.

Maybe the real lesson here is knowing which battles to fight, and a
good developer uses the right tools for the job.

S.

From jamiebbbb at hotmail.com  Wed Nov 11 07:33:28 2009
From: jamiebbbb at hotmail.com (furblender)
Date: Tue, 10 Nov 2009 22:33:28 -0800 (PST)
Subject: [Tutor] Re ading List from File
Message-ID: <26296541.post@talk.nabble.com>


Hello everyone,

I to had the same problem and it pestered me to the nth degree. I had that
many problems I went to the python site and copied an example and used that
to test why it wasn't working -see below example and traceback report. I
wasted a lot of time trying to figure my issue out. Then it dawned on me - I
had called the main program csv.py. But even when I changed the name of the
file and ran the program I was still getting the same problem. Why? because
I had in the directory a file called csv.py which is exactly the file name
for the csv file that is used in the import function. I deleted all the
references of csv.py from the directory that I was executing the command
from. Magically it worked. I do feel stupid but I hope this helps others who
maybe experiencing the same issue. Check for erroneous version of files that
are called cvs.py. 

import csv
reader = csv.reader(open("test.csv", "rb"))
for row in reader:
    print row

Traceback (most recent call last):
  File "H:/csv.py", line 1, in <module>
    import csv
  File "H:/csv.py", line 2, in <module>
    reader=csv.reader(open(test.csv, "rb"))
AttributeError: 'module' object has no attribute 'reader'


Samir-16 wrote:
> 
> Hi Everyone,
> 
> I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from a
> text
> file and assign those values to a list, x, such that:
> 
> x = ["aaa", "bbb", "ccc"]
> 
> The code that I have come up with looks like this:
> 
>>>> x = []
>>>> f = open(r'c:\test.txt', 'r')
>>>> x.extend(f.readlines())
>>>> x
> ['"aaa","bbb","ccc"']
> 
> If you look closely, there is an extra pair of single quotes (') that
> encapsulates the string.  Therefore, len(x) returns 1, instead of 3.  Is
> there a function to "separate" this list out?  I hope my question makes
> sense.
> 
> Thanks in advance.
> 
> Samir
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 
View this message in context: http://old.nabble.com/Reading-List-from-File-tp18754202p26296541.html
Sent from the Python - tutor mailing list archive at Nabble.com.


From hayalok at gmail.com  Wed Nov 11 13:15:03 2009
From: hayalok at gmail.com (Alok Mishra)
Date: Wed, 11 Nov 2009 04:15:03 -0800 (PST)
Subject: [Tutor] Alok Mishra wants to stay in touch on LinkedIn
Message-ID: <1622189730.6627540.1257941703857.JavaMail.app@ech3-cdn10.prod>

LinkedIn
------------

Alok Mishra requested to add you as a connection on LinkedIn:
------------------------------------------

Valdemaras,

I'd like to add you to my professional network on LinkedIn.

- Alok Mishra

Accept invitation from Alok Mishra
http://www.linkedin.com/e/IZPPfNL6humFkJadIejjoAnrWJn/blk/I418904846_3/6lColZJrmZznQNdhjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPdvdzgUd30Ve34QiiYPtSsQoD99hOYVd3oPdj4QdjwLrCBxbOYWrSlI/EML_comm_afe/

View invitation from Alok Mishra
http://www.linkedin.com/e/IZPPfNL6humFkJadIejjoAnrWJn/blk/I418904846_3/0PnPoQe3gMejwNd4ALqnpPbOYWrSlI/svi/

------------------------------------------

Why might connecting with Alok Mishra be a good idea?

Have a question? Alok Mishra's network will probably have an answer:
You can use LinkedIn Answers to distribute your professional questions to Alok Mishra and your extended network. You can get high-quality answers from experienced professionals.

http://www.linkedin.com/e/ash/inv19_ayn/

 
------
(c) 2009, LinkedIn Corporation

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/62fb1c1e/attachment.htm>

From kent37 at tds.net  Wed Nov 11 13:51:00 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 11 Nov 2009 07:51:00 -0500
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<b6131fdc0911100525t31323746n54cb473810210342@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
	<1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
	<b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>
Message-ID: <1c2a2c590911110451q7f3d95fftf2cce474a50f356c@mail.gmail.com>

On Wed, Nov 11, 2009 at 4:46 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> Hi Kent,
>
>> See the Python Cookbook recipes I referenced earlier.
>> http://code.activestate.com/recipes/491285/
>> http://code.activestate.com/recipes/535160/
>>
>> Note they won't fix up the jumbled ordering of your files but I don't
>> think they will break from it either...
>
> That's exactly the problem. ?I do need the end product to be in order.

You could read many items from each log into your priority queue. If
you can confidently say that, for example, the 100th entry in the log
always occurs after the first, then you could initialize the queue
with 100 items from each log. Or if you are sure that the jitter is
never more than one second, each time you read a log you could read
until the time is two seconds after the initial time. Either of these
could probably be done as a modification of the heapq merge sort
recipe.

If you can't confidently make any claims about the locality of the
jitter, then you probably have no choice but to sort the logs first
(or sort the result when you are done, if you are filtering a lot of
items that might be faster).

Kent

From sanelson at gmail.com  Wed Nov 11 13:54:34 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Wed, 11 Nov 2009 12:54:34 +0000
Subject: [Tutor] Iterator Merging
Message-ID: <b6131fdc0911110454l56b47308kb81bf006ef52a9f6@mail.gmail.com>

So, following Kent and Alan's advice, I've preprocessed my data, and
have code that produces 6 LogFile iterator objects:

>>> import magpie
>>> magpie.logs[1]
<magpie.LogFile instance at 0x2b8235debc20>
>>> dir(magpie.logs[1])
['__doc__', '__init__', '__iter__', '__module__', 'date', 'logfile',
'timestamp']

>>> for timestamp, entry in itertools.islice(magpie.logs[1], 3):
...   print timestamp, entry
...
[05/Nov/2009:04:02:13 +0000] 192.168.41.107 - - [05/Nov/2009:04:02:13
+0000] "GET http://sekrit.com/taxonomy/term/27908?page=111&item_884=1&year=66&form_id=dynamic_build_learning_objectives_form&level=121
HTTP/1.1" 200 - "-" "Mozilla/5.0 (compatible; Googlebot/2.1;
+http://www.google.com/bot.html)"
[05/Nov/2009:04:02:13 +0000] 66.249.165.22 - - [05/Nov/2009:04:02:13
+0000] "GET /taxonomy/term/27908?page=111&item_884=1&year=66&form_id=objectives_form&level=121
HTTP/1.1" 200 28736 "-" "Mozilla/5.0 (compatible; Googlebot/2.1;
+http://www.google.com/bot.html)"
[05/Nov/2009:04:02:15 +0000] 162.172.185.126 - - [05/Nov/2009:04:02:15
+0000] "GET http://sekrit.com/sites/all/themes/liszt/images/backgrounds/grad_nav_5_h3.gif
HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible;)"

This is great.

So I have a list of 6 of these iterator objects.

Kent mentioned feeding them into an iterator merger.  I've got the
iterator merger in place too:

>>> from imerge import imerge
>>> imerge
<function imerge at 0x2b8235df6050>
>>> imerge([1,3,4],[2,7])
<generator object at 0x2b8235debf38>
>>> list(imerge([1,3,4],[2,7]))
[1, 2, 3, 4, 7]

What I'm trying to work out is how to feed the data I have - 6 streams
of timestamp, entry into imerge.

How can I do this?

S.

-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From anand.shashwat at gmail.com  Wed Nov 11 14:09:56 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Wed, 11 Nov 2009 18:39:56 +0530
Subject: [Tutor] Re ading List from File
In-Reply-To: <26296541.post@talk.nabble.com>
References: <26296541.post@talk.nabble.com>
Message-ID: <d4ab53de0911110509l5721e2bfhe1dfca61a50fc078@mail.gmail.com>

<snip>


Samir-16 wrote:
> >
> > Hi Everyone,
> >
> > I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from a
> > text
> > file and assign those values to a list, x, such that:
> >
> > x = ["aaa", "bbb", "ccc"]
> >
> > The code that I have come up with looks like this:
> >
> >>>> x = []
> >>>> f = open(r'c:\test.txt', 'r')
> >>>> x.extend(f.readlines())
> >>>> x
> > ['"aaa","bbb","ccc"']
> >
> > If you look closely, there is an extra pair of single quotes (') that
> > encapsulates the string.  Therefore, len(x) returns 1, instead of 3.  Is
> > there a function to "separate" this list out?  I hope my question makes
> > sense.
>

Simply use split().
I appended these lines at the end of your piece of code,

>>> y = x[0].split(',')
>>> y
['"aaa"', '"bbb"', '"ccc"']
>>> len(y)
3
the reason for extra quotes is due to the fact that string is "aaa" and not
aaa and strings are encapsulated with "" and hence the extra quote.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/f4d7ba67/attachment.htm>

From davea at ieee.org  Wed Nov 11 14:43:39 2009
From: davea at ieee.org (Dave Angel)
Date: Wed, 11 Nov 2009 08:43:39 -0500
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911110234i3b1d6a22s2bc42af9b83aa0b0@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>	<1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>	<b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>	<hde28u$ig2$1@ger.gmane.org>
	<b6131fdc0911110234i3b1d6a22s2bc42af9b83aa0b0@mail.gmail.com>
Message-ID: <4AFABF8B.90104@ieee.org>

Stephen Nelson-Smith wrote:
> Hi,
>
> On Wed, Nov 11, 2009 at 10:05 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>   
>> "Stephen Nelson-Smith" <sanelson at gmail.com> wrote
>>     
>>> I don't really want to admit defeat and have a cron job sort the logs
>>> before entry.  Anyone got any other ideas?
>>>       
>> Why would that be admitting defeat?
>>     
>
> Well, it mean admitting defeat on solving the problem in python.  Yes
> in practical terms, I should probably preprocess the data, but as a
> programming exercise, learning how to sort a number of files into one
> is something I'd like to crack.
>
> Maybe the real lesson here is knowing which battles to fight, and a
> good developer uses the right tools for the job.
>
> S.
>
>   
That's certainly a valuable lesson.  On the other hand, I don't think 
you've run out of useful python options

The problem as I see it is that your input files are *mostly* sorted, 
and have an occasional out-of-order item.  And further that you're not 
sure how far out-of-order that can get;  you're not willing to pick a 
constant "10" and say that no item will have to be moved more than 10 
items.  Would you feel safe with 100 ?  Or 1000 ?

If you give it to "a cron job" the code will be very generalized, and 
perhaps not very efficient at sorting a list which is almost in order 
already.  So perhaps you can do better.  You might want to time it to be 
sure, or you might just decide that "good enough" doesn't need measuring.

Back to the fundamental problem.  Apparently the max amount of jitter is 
one second.  So let's say you're processing all the 10:01 events, and 
you encounter a 10:02 one.  You don't know whether you've really got all 
the 10:02 ones till you see your first 10:03.  So you need a priority 
queue combined with a couple of threshold values, in this case 10:01 and 
10:03.  Fill the queue till you hit the upper threshold, letting the 
queue grow as needed.  So you have all the 10:01 events.  Now you feed 
out your queue into the merge logic, and don't replace items as they are 
removed, until you have removed the last one at your lower threshold.  
At this point raise the thresholds by one, and fill it again as before.

Such a routine can be done as a generator function, with the two 
thresholds simply being local variables in the function.  And the 
generator can then be used in a merge function, just as any other 
iterator producing a sorted list would be used.

And of course you would need to carefully test such a generator, both 
with worst-case data, and against the live data.  If our assumptions 
about worst case jitter is wrong, the thresholds might need changing.

Finally, consider the possibility that if you pick a fixed jitter size 
that statistically nearly always works (like 1000 items).then you could 
just continuously compare the output data as it's being generated.  If 
once every 30 days it's out of order, then do a massive sort on the 
result file that day, and figure a speedup the other 29 days, with 
simpler code, was worth it.  In this case, the iterator is simply a 
priority queue of fixed size.

DaveA


From kent37 at tds.net  Wed Nov 11 16:12:38 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 11 Nov 2009 10:12:38 -0500
Subject: [Tutor] Iterator Merging
In-Reply-To: <b6131fdc0911110454l56b47308kb81bf006ef52a9f6@mail.gmail.com>
References: <b6131fdc0911110454l56b47308kb81bf006ef52a9f6@mail.gmail.com>
Message-ID: <1c2a2c590911110712w302c8e64ye685c8a8295d7143@mail.gmail.com>

On Wed, Nov 11, 2009 at 7:54 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> So, following Kent and Alan's advice, I've preprocessed my data, and
> have code that produces 6 LogFile iterator objects:
>
>>>> import magpie
>>>> magpie.logs[1]
>
>>>> for timestamp, entry in itertools.islice(magpie.logs[1], 3):
> ... ? print timestamp, entry
> ...

> This is great.
>
> So I have a list of 6 of these iterator objects.
>
> Kent mentioned feeding them into an iterator merger. ?I've got the
> iterator merger in place too:
>
>>>> from imerge import imerge
>>>> imerge
> <function imerge at 0x2b8235df6050>
>>>> imerge([1,3,4],[2,7])
> <generator object at 0x2b8235debf38>
>>>> list(imerge([1,3,4],[2,7]))
> [1, 2, 3, 4, 7]
>
> What I'm trying to work out is how to feed the data I have - 6 streams
> of timestamp, entry into imerge.

merged = imerge(*magpie.logs)

for timestamp, entry in itertools.islice(merged, 3):
   print timestamp, entry

Kent

From alan.gauld at btinternet.com  Wed Nov 11 16:47:39 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Wed, 11 Nov 2009 07:47:39 -0800 (PST)
Subject: [Tutor] Logfile multiplexing
In-Reply-To: <b6131fdc0911110234i3b1d6a22s2bc42af9b83aa0b0@mail.gmail.com>
References: <b6131fdc0911100204h1cecbe9at1774a2539ccef923@mail.gmail.com>
	<29179d160911100535o33a0c0b3k7de5401bd6e20da3@mail.gmail.com>
	<b6131fdc0911100542x30a5f1f9o45a948eb0e2131e9@mail.gmail.com>
	<dfeb4470911100600l1cd44fa3xce3fc041392b81c2@mail.gmail.com>
	<b6131fdc0911100748r30114f32xc725b5c3f1eabdf9@mail.gmail.com>
	<b6131fdc0911100759v3272a6f5yd5c952804336312c@mail.gmail.com>
	<b6131fdc0911100825n21dc12bcp2b251878bb3c939@mail.gmail.com>
	<1c2a2c590911101003r5bd2e118h80cf4d7522a09e2a@mail.gmail.com>
	<b6131fdc0911110146w3fd93e82tb8c5c8076c137f94@mail.gmail.com>
	<hde28u$ig2$1@ger.gmane.org>
	<b6131fdc0911110234i3b1d6a22s2bc42af9b83aa0b0@mail.gmail.com>
Message-ID: <647396.63558.qm@web86712.mail.ird.yahoo.com>



> Why would that be admitting defeat?
> 
> Well, it mean admitting defeat on solving the problem in python.  Yes

You could still use Python to do the sorting. But it breaks the problem 
into two separate and simpler processes. One simply sorts a file given 
a particular data layout and sort algorithm optimised for sorting where
a) the volumes dictate doing it out of memory, and
b) the data is almost sorted already.

and  the other merges pre-sorted files into one, filtering out unwanted 
lines as you go.

cron can be used to launch the sorting jobs - one per file if that 
is a constant, or via a shell script that checks the files and launches 
the python sort per file found as a background process. (You could 
replace the shell with Python too but shell is designed for this kind 
of role).

cron can also be used to then launch the merge job which can check 
that the sorting has completed, with no errors and merge the results.

> programming exercise, learning how to sort a number of files into one
> is something I'd like to crack.

The problem hasn't changed, nor has the solution technology, its how 
you architect the solution that is different. Just because its Python
doesn't mean it all has to be in a single program.

> Maybe the real lesson here is knowing which battles to fight, and a
> good developer uses the right tools for the job.

Absolutely.


Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/320dba1c/attachment.htm>

From warpcat at gmail.com  Wed Nov 11 17:12:03 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Wed, 11 Nov 2009 08:12:03 -0800
Subject: [Tutor] Querying a module's package path?
In-Reply-To: <hddobt$li9$1@ger.gmane.org>
References: <23cba4bf0911101712s4fac201el4b813f90d36334c9@mail.gmail.com>
	<hddobt$li9$1@ger.gmane.org>
Message-ID: <23cba4bf0911110812ue2ff2d5k90dd6e4919a83e6a@mail.gmail.com>

On Tue, Nov 10, 2009 at 11:15 PM, Lie Ryan <lie.1296 at gmail.com> wrote:

> Eric Pavey wrote:
>
>> Presume I have a package 'a' like this:
>>
>>    * /pystuff  (added to sys.path)
>>          o /a  (start of my package)
>>
>>                + __init__.py
>>                + /b                        # __init__.py
>>                      # module.py
>>
>> to import module.py:
>> import *a.b.module*
>>
>> What I'm trying to find is a way to query exactly what I typed above (in
>> bold):  Inside of module.py, can it query it's path back to the package
>> root?
>>
>
> Why would you want that? Can you describe the problem you're having now
> that requires you to know the module's package path? Right now, you're just
> describing a solution to your problem, not the problem itself. Maybe we can
> find another solution that doesn't involve knowing the module's package
> path.
>
>
I know the feeling: "why would he ever want *that*...?"   :)

I'm building a high-level character rigging system in Autodesk Maya for the
next game I'm on, which uses Python as the scripting language.  Each piece
of the animation rig (which are a collection of 'physical nodes in Maya') is
authored via a Python module, and I want to store on the nodes which code
created which piece.  So during the execution of a module, it creates a
variety of rigging nodes in Maya that perform their function inside that
software.  I want to embed the name of the module, relative to the path,
into the node itself, so later unrelated code can query up what built it,
and call to additional Python modules based on that.

In a simpler sense it could be abstracted like this:  I have a module that
creates a text file.  In that text file, I want to write out the name of the
module, with package, that made it, so later I know what code created it.

I could completely hand-code this info in each module, but I wanted to make
it automated so it's easier for other people to use my code and not screw it
up :P  Plus my package is a bit in flux as I develop it, so things might
move around, and I don't want to have to remember to go back and change a
bunch of hard-coded paths.

I have some hacky code now that does what I need, but this is screaming for
some built-in system that returns it, IMO.

thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/c0969ad5/attachment.htm>

From zebra05 at gmail.com  Wed Nov 11 18:22:22 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Wed, 11 Nov 2009 19:22:22 +0200
Subject: [Tutor] TimeOut in
In-Reply-To: <b134f2450911081109g5fa74a6egfdd8a931bfc02589@mail.gmail.com>
References: <b134f2450911080318xe729fc1je10aa8e1a0162428@mail.gmail.com>
	<hd6ufr$4ig$1@ger.gmane.org>
	<b134f2450911081109g5fa74a6egfdd8a931bfc02589@mail.gmail.com>
Message-ID: <c7c6f3bc0911110922s33581916x6847b8895f8029cc@mail.gmail.com>

Hi Somnath,

I think the exception simply means that the thread object or class does not
have a cancel function. maybe read docs on Python threads to see what
functions can be called on those?

On Sun, Nov 8, 2009 at 9:09 PM, somnath chakrabarti <
chakrabarti.somnath at gmail.com> wrote:

> Hi Alan,
>
> I am importing the constraint library package in the beginning that is
> having classes Problem, Solver and all the methods that I am calling. Now,
> as you said after making the change from
>
>         self.p = threading.Timer(100, self.onTimeout)
>
> I am getting the below error. I am also attaching the constraint.py
> package that my code is using
>
> - Somnath
>
>
> %%%%%%% Error Message %%%%%%%%%%%%%%%%%%%%
>
> <constraint.BacktrackingSolver object at 0x02846F10>
> Magic Square:
>
> Exception in thread Thread-1:
> Traceback (most recent call last):
>   File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner
>     self.run()
>   File "C:\Python26\lib\threading.py", line 477, in run
>     self.__target(*self.__args, **self.__kwargs)
>   File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
> line 43, in getSolution
>
>     self.solution=Problem.getSolution(self)
>   File
> "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
> 215, in getSolution
>     return self._solver.getSolution(domains, constraints, vconstraints)
>   File
> "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
> 524, in getSolution
>     return iter.next()
>   File
> "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
> 506, in getSolutionIter
>     pushdomains):
>   File
> "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
> 1173, in __call__
>     domain.hideValue(value)
>   File
> "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line
> 787, in hideValue
>     list.remove(self, value)
> ValueError: list.remove(x): x not in list
> None
> Time taken: 0.047
>
> Traceback (most recent call last):
>   File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\csc671.py",
> line 125, in <module>
>     ms_t.magic_square(size,func_solver())
>   File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py",
> line 68, in magic_square
>     t.cancel()
> AttributeError: 'Thread' object has no attribute 'cancel'
> None
> Time Exceeded Limit.
>
>
> On Sun, Nov 8, 2009 at 12:17 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>>
>> "somnath chakrabarti" <chakrabarti.somnath at gmail.com> wrote
>>
>>
>> Below is the code that I have written to do the timeout implementation.
>>> But
>>> I am getting error as follows. Can anybody please help me where I am
>>> going
>>> wrong?
>>>
>>
>> I have no idea how this framework is upposed to work but my
>> guess is that the problem lies here:
>>
>>
>>   def getSolution(self,solver):
>>>       Problem.setSolver(self,solver)
>>>       self.p = threading.Timer(100, self.onTimeout())
>>>
>>
>> Thios looks like you should be providing a callback
>> function self.onTimeout but you are calling the function
>> instead of referencing it. This calls the method before
>> self.p has been defined.
>>
>> You need to remove the parentheses after Timeout.
>>
>> I think...
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Thanks and regards,
> Somnath Chakrabarti.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/19871165/attachment-0001.htm>

From alan.gauld at btinternet.com  Wed Nov 11 19:19:17 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 11 Nov 2009 18:19:17 -0000
Subject: [Tutor] class initialization with a lot of parameters
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com><4AF8AE46.7070405@ieee.org><8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com><hdcl0n$r17$1@ger.gmane.org><8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com><hde04u$b2u$1@ger.gmane.org>
	<8c5079cb0911110208k16ba2278w9daa1c5fe1cd0e07@mail.gmail.com>
Message-ID: <hdev7e$1dl$1@ger.gmane.org>


"C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote

> The Table object you described I find more complicated if each table 
> stands
> on its own it is decoupled from its compare partner. I suppose a function
> that pairs the tables, feeding a Table object to its partner 
> Table.compare
> method.

Kind of.

Think about something simpler. Like numbers.

when we do

if number1 == number2:

what we actually do in Python is

if number1.__eq__(number2):


In other words we call the special method __eq__() of number1 passing
in number2.

So == is actually a method of the object on the left hand side.

Similarly if you want to compare tables for equality you can define a class
Table and provide an __eq__() method and you will be able to write

if table1 == table2

and it will work.

And your __eq__() method can be a completely bespoke algorithm for
determining what equality means for your table class. It could mean that
every value of every field is the same or it could mean that the 3rd field
in number1 is twice the value of the 5th field in number2. Its entirely up 
to you.
But the ability to compare two things of the same class is an operation
of the class.

The same applies to >, <, >=, <= etc. They all have special methods
that you can override to make object comparisons work the way you
want them to.

> Hmm ... This has got to sink in.

Its quite a big jump from traditional programming but one of the areas
where OOP can dramatically simplify your code.  Once you build the
objects to act like the built in types your code that uses those objects
suddenly becomes much more readable

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Wed Nov 11 19:25:26 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 11 Nov 2009 18:25:26 -0000
Subject: [Tutor] Iterator Merging
References: <b6131fdc0911110454l56b47308kb81bf006ef52a9f6@mail.gmail.com>
	<1c2a2c590911110712w302c8e64ye685c8a8295d7143@mail.gmail.com>
Message-ID: <hdevj0$2l0$1@ger.gmane.org>


"Kent Johnson" <kent37 at tds.net> wrote 

> > What I'm trying to work out is how to feed the data I have - 6 streams
> > of timestamp, entry into imerge.
>
> merged = imerge(*magpie.logs)
>
> for timestamp, entry in itertools.islice(merged, 3):
>    print timestamp, entry

And stating the obvious, test it with smaller test data first 
or you might wait an awful long time before discovering 
you have a bug! :-)


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From warpcat at gmail.com  Wed Nov 11 19:26:20 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Wed, 11 Nov 2009 10:26:20 -0800
Subject: [Tutor] Querying a module's package path?
In-Reply-To: <23cba4bf0911110812ue2ff2d5k90dd6e4919a83e6a@mail.gmail.com>
References: <23cba4bf0911101712s4fac201el4b813f90d36334c9@mail.gmail.com>
	<hddobt$li9$1@ger.gmane.org>
	<23cba4bf0911110812ue2ff2d5k90dd6e4919a83e6a@mail.gmail.com>
Message-ID: <23cba4bf0911111026n3bc299e0w4effa2c848c95318@mail.gmail.com>

>
> Eric Pavey wrote:
>>
>>> Presume I have a package 'a' like this:
>>>
>>>    * /pystuff  (added to sys.path)
>>>          o /a  (start of my package)
>>>
>>>                + __init__.py
>>>                + /b                        # __init__.py
>>>                      # module.py
>>>
>>> to import module.py:
>>> import *a.b.module*
>>>
>>> What I'm trying to find is a way to query exactly what I typed above (in
>>> bold):  Inside of module.py, can it query it's path back to the package
>>> root?
>>>
>>
>> Why would you want that? Can you describe the problem you're having now
>> that requires you to know the module's package path? Right now, you're just
>> describing a solution to your problem, not the problem itself. Maybe we can
>> find another solution that doesn't involve knowing the module's package
>> path.
>>
>>
> I know the feeling: "why would he ever want *that*...?"   :)
>
> I'm building a high-level character rigging system in Autodesk Maya for the
> next game I'm on, which uses Python as the scripting language.  Each piece
> of the animation rig (which are a collection of 'physical nodes in Maya') is
> authored via a Python module, and I want to store on the nodes which code
> created which piece.  So during the execution of a module, it creates a
> variety of rigging nodes in Maya that perform their function inside that
> software.  I want to embed the name of the module, relative to the path,
> into the node itself, so later unrelated code can query up what built it,
> and call to additional Python modules based on that.
>
> In a simpler sense it could be abstracted like this:  I have a module that
> creates a text file.  In that text file, I want to write out the name of the
> module, with package, that made it, so later I know what code created it.
>
> I could completely hand-code this info in each module, but I wanted to make
> it automated so it's easier for other people to use my code and not screw it
> up :P  Plus my package is a bit in flux as I develop it, so things might
> move around, and I don't want to have to remember to go back and change a
> bunch of hard-coded paths.
>
> I have some hacky code now that does what I need, but this is screaming for
> some built-in system that returns it, IMO.
>
> thanks!
>

Found the answer, at least relative to a class, which is good enough :)

# whereAmI.py, in the data package
class Spam(object):
    def __init__(self):
        print self.__module__



>>> import data.whereAmI
>>> eggs = whereAmI.Spam()

data.whereAmI
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/6994c5d8/attachment.htm>

From wilcoxwork at gmail.com  Wed Nov 11 22:11:00 2009
From: wilcoxwork at gmail.com (Kristin Wilcox)
Date: Wed, 11 Nov 2009 13:11:00 -0800
Subject: [Tutor] module name completion suggestions drop down?
Message-ID: <de4393b40911111311l7fbc82a3tf58c5edc230a3e3@mail.gmail.com>

Hello,

I was wondering about something I've seen in people's video tutorials
-- these people are using IDLE like I am, but they get these drop down
suggestions for module names that I'm not experiencing.

The user starts typing a built-in method and suggestions of possible
methods that start with those characters drop down at their cursor.
The user is not in the Shell - they're in the edit window thingy.

I have attached a screen print showing what I'm talking about, in case
I'm describing this too vaguely.

Do any of you know something about this --> Is there a setting I don't
have switched on? Or is there something I can download? I've done a
bunch of google searches, but came up empty. (possibly I just don't
know the right terminology to search for)

FYI: I am running Windows XP, and have python 2.6 and 3.1 installed,
with wxPython installed on 2.6

Thanks for your help!
-kris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dropexample2.png
Type: image/png
Size: 6698 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20091111/80629cd8/attachment-0001.png>

From sander.sweers at gmail.com  Wed Nov 11 22:47:00 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Wed, 11 Nov 2009 22:47:00 +0100
Subject: [Tutor] module name completion suggestions drop down?
In-Reply-To: <de4393b40911111311l7fbc82a3tf58c5edc230a3e3@mail.gmail.com>
References: <de4393b40911111311l7fbc82a3tf58c5edc230a3e3@mail.gmail.com>
Message-ID: <1257976020.5904.2.camel@infirit.homelinux.org>

On Wed, 2009-11-11 at 13:11 -0800, Kristin Wilcox wrote:
> I was wondering about something I've seen in people's video tutorials
> -- these people are using IDLE like I am, but they get these drop down
> suggestions for module names that I'm not experiencing.

This is done by holding ctrl and pressing the spacebar. It is also under
the edit menu and called "show completions".

Greets
Sander


From emile at fenx.com  Wed Nov 11 23:30:26 2009
From: emile at fenx.com (Emile van Sebille)
Date: Wed, 11 Nov 2009 14:30:26 -0800
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <hdev7e$1dl$1@ger.gmane.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com><4AF8AE46.7070405@ieee.org><8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com><hdcl0n$r17$1@ger.gmane.org><8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com><hde04u$b2u$1@ger.gmane.org>	<8c5079cb0911110208k16ba2278w9daa1c5fe1cd0e07@mail.gmail.com>
	<hdev7e$1dl$1@ger.gmane.org>
Message-ID: <hdfdua$n4p$1@ger.gmane.org>

On 11/11/2009 10:19 AM Alan Gauld said...
> what we actually do in Python is
> 
> if number1.__eq__(number2):
> 
> 
> In other words we call the special method __eq__() of number1 passing
> in number2.
> 
> So == is actually a method of the object on the left hand side.

... and sometimes the right hand side.  Consider:

 >>> class A:
...   def __eq__(self,other):
...     print 'in A.__eq__'
...     return True
...
 >>> class C:
...   pass
...
 >>> c = C()
 >>> a == c
in A.__eq__
True
 >>> c == a
in A.__eq__
True
 >>>


Emile


From alan.gauld at btinternet.com  Thu Nov 12 01:03:35 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 12 Nov 2009 00:03:35 -0000
Subject: [Tutor] module name completion suggestions drop down?
References: <de4393b40911111311l7fbc82a3tf58c5edc230a3e3@mail.gmail.com>
Message-ID: <hdfjd0$7jo$1@ger.gmane.org>


"Kristin Wilcox" <wilcoxwork at gmail.com> wrote

> I was wondering about something I've seen in people's video tutorials
> -- these people are using IDLE like I am, but they get these drop down
> suggestions for module names that I'm not experiencing.

Try hitting Control-Space while typing a method name...
Does that work?

Alan G.


From emailkgnow at gmail.com  Thu Nov 12 04:34:09 2009
From: emailkgnow at gmail.com (Khalid Al-Ghamdi)
Date: Thu, 12 Nov 2009 06:34:09 +0300
Subject: [Tutor] os.environ question
Message-ID: <dfac564f0911111934k1c55dae3p481c2d3558eb07a@mail.gmail.com>

hi,

can anyone tell me why on python 2.6 i can enter *os.environ* and then get
all the items that that pertain to the os while on python 3 you just get the
following:

<os._Environ object at 0x01B18D90>

with no items?

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091112/aab57d99/attachment.htm>

From andrefsp at gmail.com  Thu Nov 12 05:10:29 2009
From: andrefsp at gmail.com (=?ISO-8859-1?Q?andr=E9?= palma)
Date: Thu, 12 Nov 2009 04:10:29 +0000
Subject: [Tutor] django python Version 1.1.1 - Internacionalization
Message-ID: <1257999029.5714.0.camel@andrefsp-eeepc>

Hi all! 
I'm developing a django web application but i'm having a trouble with
the internationalization.

The problem is that translation somehow doesn't work. 

i followed these steps: 

- on my project folder (./translation ) i run the command:
         "django-admin makemessages -l en-US" 

and it created the follow file with the right content:
        ./mysite/locale/en-US/LC_MESSAGES/django.po
inside the file it shows the messagesId's and the related messages
strings to fill with the translation. I was glad to see gettext is
installed.
========================
        #: views.py:12
        msgid "benvindo"
        msgstr "welcome"
========================

this messageID is related with this view: 
=================================================
from django.utils.translation import ugettext as _
def translate1(request):
    output = _("benvindo")
    return HttpResponse(output)

#("code from djangobook")
=================================================


- then i run "django-admin compilemessages" and so it has created the
binary file ./mysite/locale/en-US/LC_MESSAGES/django.mo, as i expected.


I think i already have my settings.py file configured and it looks like
this: 
=======================================================================

LANGUAGE_CODE = 'en-US'

# gettext settings for translation
ugettext = lambda s: s
LOCALE_PATHS = (
    '/home/andrefsp/django/mysite/locale',
)

LANGUAGES = (
    ('pt-PT', ugettext('Portuguese')),
    ('en-US', ugettext('English')),
)



USE_I18N = True


TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
========================================================================
        
And it still isn't working =( 

I'm out of ideas... I've already googled it everywhere and i couldn't
find the solution. 



From andrefsp at gmail.com  Thu Nov 12 05:09:03 2009
From: andrefsp at gmail.com (=?ISO-8859-1?Q?andr=E9?= palma)
Date: Thu, 12 Nov 2009 04:09:03 +0000
Subject: [Tutor] django-python Version 1.1.1 - Internacionalization
Message-ID: <1257998943.3983.26.camel@andrefsp-eeepc>

Hi all! 
I'm developing a django web application but i'm having a trouble with
the internationalization.

The problem is that translation somehow doesn't work. 

i followed these steps: 

- on my project folder (./translation ) i run the command:
	 "django-admin makemessages -l en-US" 

and it created the follow file with the right content:
	./mysite/locale/en-US/LC_MESSAGES/django.po
inside the file it shows the messagesId's and the related messages
strings to fill with the translation. I was glad to see gettext is
installed.
========================
	#: views.py:12
	msgid "benvindo"
	msgstr "welcome"
========================

this messageID is related with this view: 
=================================================
from django.utils.translation import ugettext as _
def translate1(request):
    output = _("benvindo")
    return HttpResponse(output)

#("code from djangobook")
=================================================


- then i run "django-admin compilemessages" and so it has created the
binary file ./mysite/locale/en-US/LC_MESSAGES/django.mo, as i expected.


I think i already have my settings.py file configured and it looks like
this: 
=======================================================================

LANGUAGE_CODE = 'en-US'

# gettext settings for translation
ugettext = lambda s: s
LOCALE_PATHS = (
    '/home/andrefsp/django/mysite/locale',
)

LANGUAGES = (
    ('pt-PT', ugettext('Portuguese')),
    ('en-US', ugettext('English')),
)



USE_I18N = True


TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
========================================================================
	
And it still isn't working =( 

I'm out of ideas... I've already googled it everywhere and i couldn't
find the solution. 




From waynejwerner at gmail.com  Thu Nov 12 07:05:22 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Thu, 12 Nov 2009 00:05:22 -0600
Subject: [Tutor] django python Version 1.1.1 - Internacionalization
In-Reply-To: <1257999029.5714.0.camel@andrefsp-eeepc>
References: <1257999029.5714.0.camel@andrefsp-eeepc>
Message-ID: <333efb450911112205y37795c2bp151d0d8d149e9f5a@mail.gmail.com>

On Wed, Nov 11, 2009 at 10:10 PM, andr? palma <andrefsp at gmail.com> wrote:

> Hi all!
> I'm developing a django web application but i'm having a trouble with
> the internationalization.


I don't know much about Django, and I suspect that's more the norm here -
you may have better luck on the Django users list:

http://groups.google.com/group/django-users?pli=1

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091112/52014a34/attachment-0001.htm>

From cspears2002 at yahoo.com  Thu Nov 12 08:05:38 2009
From: cspears2002 at yahoo.com (Christopher Spears)
Date: Wed, 11 Nov 2009 23:05:38 -0800 (PST)
Subject: [Tutor] moving directories into directories
Message-ID: <672976.1568.qm@web51608.mail.re2.yahoo.com>

I'm trying to move a bunch of files and directories into another directory.  Unfortunately, I keep getting an error message:

Traceback (most recent call last):
  File "checkDeviceType.py", line 46, in ?
    cdt.moveFiles(barcodeList)
  File "checkDeviceType.py", line 40, in moveFiles
    shutil.move(f,dst)
  File "/usr/lib/python2.4/shutil.py", line 190, in move
    copytree(src, dst, symlinks=True)
  File "/usr/lib/python2.4/shutil.py", line 111, in copytree
    os.mkdir(dst)
OSError: [Errno 17] File exists: './TAPE_ARCHIVES'

The problem seems to happen when I try to move a directory into another directory:

>>> import shutil
>>> shutil.move("test_dir","stuff")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/shutil.py", line 190, in move
    copytree(src, dst, symlinks=True)
  File "/usr/lib/python2.4/shutil.py", line 111, in copytree
    os.mkdir(dst)
OSError: [Errno 17] File exists: 'stuff'

Any idea how to pull this off?


From c.t.matsumoto at gmail.com  Thu Nov 12 08:32:44 2009
From: c.t.matsumoto at gmail.com (C.T. Matsumoto)
Date: Thu, 12 Nov 2009 08:32:44 +0100
Subject: [Tutor] class initialization with a lot of parameters
In-Reply-To: <hdev7e$1dl$1@ger.gmane.org>
References: <8c5079cb0911090433w2547ac22y1d0b43275130fea4@mail.gmail.com>
	<4AF8AE46.7070405@ieee.org>
	<8c5079cb0911092332u776932d3se816078c2c5f498f@mail.gmail.com>
	<hdcl0n$r17$1@ger.gmane.org>
	<8c5079cb0911102340y6020a63crac964ec1ba1deb71@mail.gmail.com>
	<hde04u$b2u$1@ger.gmane.org>
	<8c5079cb0911110208k16ba2278w9daa1c5fe1cd0e07@mail.gmail.com>
	<hdev7e$1dl$1@ger.gmane.org>
Message-ID: <8c5079cb0911112332g503c2c4ch6ca7177285c7249a@mail.gmail.com>

Hello Alan,

I see a new way to look at this design so I'm pretty excited to refactor the
code.
I've also been looking for an example to use 'overloading operators' as the
Learn
Python book calls it.

I think its time to close this discussion because the parameters question
has gotten
much advice, and this discussion seems to changing to objectness.

Thanks,

T

On Wed, Nov 11, 2009 at 7:19 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "C.T. Matsumoto" <c.t.matsumoto at gmail.com> wrote
>
>  The Table object you described I find more complicated if each table
>> stands
>> on its own it is decoupled from its compare partner. I suppose a function
>> that pairs the tables, feeding a Table object to its partner Table.compare
>> method.
>>
>
> Kind of.
>
> Think about something simpler. Like numbers.
>
> when we do
>
> if number1 == number2:
>
> what we actually do in Python is
>
> if number1.__eq__(number2):
>
>
> In other words we call the special method __eq__() of number1 passing
> in number2.
>
> So == is actually a method of the object on the left hand side.
>
> Similarly if you want to compare tables for equality you can define a class
> Table and provide an __eq__() method and you will be able to write
>
> if table1 == table2
>
> and it will work.
>
> And your __eq__() method can be a completely bespoke algorithm for
> determining what equality means for your table class. It could mean that
> every value of every field is the same or it could mean that the 3rd field
> in number1 is twice the value of the 5th field in number2. Its entirely up
> to you.
> But the ability to compare two things of the same class is an operation
> of the class.
>
> The same applies to >, <, >=, <= etc. They all have special methods
> that you can override to make object comparisons work the way you
> want them to.
>
>
>  Hmm ... This has got to sink in.
>>
>
> Its quite a big jump from traditional programming but one of the areas
> where OOP can dramatically simplify your code.  Once you build the
> objects to act like the built in types your code that uses those objects
> suddenly becomes much more readable
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Todd Matsumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091112/f392ad57/attachment.htm>

From jra at nella.org  Thu Nov 12 10:03:58 2009
From: jra at nella.org (Jeff R. Allen)
Date: Thu, 12 Nov 2009 10:03:58 +0100
Subject: [Tutor] Unexpected iterator
Message-ID: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>

Hello,

I am working my way through the tutorial, and I like trying
variations, just to see what expected errors look like, and other ways
things could be written.

I tried a, b = 0, 0 and that worked.

Then I tried this to (maybe) set both a and b to 0:

>>> a, b = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

I understand why it doesn't work, but I don't understand the wording
of the exception. Could someone explain how I accidentally introduced
iteration into the picture with my syntax? I have a feeling there's an
interesting lesson hidden in this example...

Thanks.

  -jeff

From alan.gauld at btinternet.com  Thu Nov 12 10:10:37 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 12 Nov 2009 09:10:37 -0000
Subject: [Tutor] moving directories into directories
References: <672976.1568.qm@web51608.mail.re2.yahoo.com>
Message-ID: <hdgjen$frf$1@ger.gmane.org>


"Christopher Spears" <cspears2002 at yahoo.com> wrote

> I'm trying to move a bunch of files and directories into another 
> directory.
> Unfortunately, I keep getting an error message:

> OSError: [Errno 17] File exists: './TAPE_ARCHIVES'
>
> The problem seems to happen when I try to move a directory into another 
> directory:

I think what is happening is that you are trying to copy something that
has an equivalently named item already existing in the target. When
you do this at a UI the OS asks if you want to replace it, but in a program
it can't do that so it throws the exception. You need to handle that case 
by
catching the exception and ignoring it, or stopping or whatever.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From patrick.just4fun at gmail.com  Thu Nov 12 10:38:28 2009
From: patrick.just4fun at gmail.com (Patrick Sabin)
Date: Thu, 12 Nov 2009 10:38:28 +0100
Subject: [Tutor] Unexpected iterator
In-Reply-To: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
Message-ID: <4AFBD794.3020101@gmail.com>

Jeff R. Allen wrote:

>>>> a, b = 0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
> 
> I understand why it doesn't work, but I don't understand the wording
> of the exception. Could someone explain how I accidentally introduced
> iteration into the picture with my syntax? I have a feeling there's an
> interesting lesson hidden in this example...

To upack your variables a and b you need an iterable object on the right 
side, which returns you exactly 2 variables, e.g you could also write

a, b = range(2)

or create your own class

class X:
	def __iter__(self):
		yield 0
		yield 0

a,b = X()

- Patrick

From modulok at gmail.com  Thu Nov 12 11:31:54 2009
From: modulok at gmail.com (Modulok)
Date: Thu, 12 Nov 2009 03:31:54 -0700
Subject: [Tutor] How to call a method with a print statement?
Message-ID: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>

List,

How do I get a print statement to call a class method? I'm using
python 2.5. I did it long ago, but can't remember how, or even where I
learned it from. Something like:

class Foo():
   def __init__(self):
      pass

   def  ***part I can't remember goes here***
      print "hello world!"

 # Now use it:
bar = Foo()
print bar
hello world! #<-- Magic!


If any of this makes sense, any pointers would be great!
-Modulok-

From alan.plum at uni-koeln.de  Thu Nov 12 11:38:06 2009
From: alan.plum at uni-koeln.de (Alan Plum)
Date: Thu, 12 Nov 2009 11:38:06 +0100
Subject: [Tutor] os.environ question
In-Reply-To: <dfac564f0911111934k1c55dae3p481c2d3558eb07a@mail.gmail.com>
References: <dfac564f0911111934k1c55dae3p481c2d3558eb07a@mail.gmail.com>
Message-ID: <1258022286.3156.1.camel@kallisti>

Ahoy!

On Do, 2009-11-12 at 06:34 +0300, Khalid Al-Ghamdi wrote:

> can anyone tell me why on python 2.6 i can enter os.environ and then
> get all the items that that pertain to the os while on python 3 you
> just get the following:
> <os._Environ object at 0x01B18D90>
> with no items?

Seems like os.environ has changed a little since Python 2.x. Try this:

>>> import os
>>> for env in os.environ:
...    print('%s: %s' % (env, os.environ[env]))

That should do the trick.

Cheers,

Alan



From sander.sweers at gmail.com  Thu Nov 12 12:25:47 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Thu, 12 Nov 2009 12:25:47 +0100
Subject: [Tutor] Unexpected iterator
In-Reply-To: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
Message-ID: <b65fbb130911120325p7f4d630die6e06932691f76cf@mail.gmail.com>

2009/11/12 Jeff R. Allen <jra at nella.org>:
> Then I tried this to (maybe) set both a and b to 0:
>
>>>> a, b = 0
> Traceback (most recent call last):
> ?File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable

I think you are looking for.

>>> a = b = c = 300

Greets
Sander

From kent37 at tds.net  Thu Nov 12 12:28:40 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 12 Nov 2009 06:28:40 -0500
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
Message-ID: <1c2a2c590911120328j3b78df3chfc91eb07b52b97d9@mail.gmail.com>

On Thu, Nov 12, 2009 at 5:31 AM, Modulok <modulok at gmail.com> wrote:
> List,
>
> How do I get a print statement to call a class method? I'm using
> python 2.5. I did it long ago, but can't remember how, or even where I
> learned it from. Something like:
>
> class Foo():
> ? def __init__(self):
> ? ? ?pass
>
> ? def ?***part I can't remember goes here***
> ? ? ?print "hello world!"

You want the __str__() special method. Note that it returns a string,
it doesn't itself print:
def __str__(self):
  return "hello world"

Kent

From jra at nella.org  Thu Nov 12 12:29:27 2009
From: jra at nella.org (Jeff R. Allen)
Date: Thu, 12 Nov 2009 12:29:27 +0100
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
Message-ID: <bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>

You are looking for the __str__ method. See
http://docs.python.org/reference/datamodel.html#object.__str__

class Foo():
? def __init__(self):
? ? ?pass

? def ?__str__(self)
? ? ?return "hello world!"

  -jeff

From rabidpoobear at gmail.com  Thu Nov 12 12:35:21 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Thu, 12 Nov 2009 05:35:21 -0600
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
	<bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>
Message-ID: <dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com>

On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen <jra at nella.org> wrote:

> You are looking for the __str__ method. See
> http://docs.python.org/reference/datamodel.html#object.__str__
>
> Can't you also implement __repr__?

(bottom-posted for Dave)
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091112/f980d0ea/attachment.htm>

From kent37 at tds.net  Thu Nov 12 12:55:22 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 12 Nov 2009 06:55:22 -0500
Subject: [Tutor] os.environ question
In-Reply-To: <dfac564f0911111934k1c55dae3p481c2d3558eb07a@mail.gmail.com>
References: <dfac564f0911111934k1c55dae3p481c2d3558eb07a@mail.gmail.com>
Message-ID: <1c2a2c590911120355q33b43d79v993a283f5f3bf491@mail.gmail.com>

On Wed, Nov 11, 2009 at 10:34 PM, Khalid Al-Ghamdi <emailkgnow at gmail.com> wrote:
> hi,
> can anyone tell me why on python 2.6 i can enter os.environ and then get all
> the items that that pertain to the os while on python 3 you just get the
> following:
> <os._Environ object at 0x01B18D90>
> with no items?

I think this is perhaps an oversight in Python 3.1. I have filed a bug
with an explanation (if you do want to know why) at
http://bugs.python.org/issue7310

Kent

From kent37 at tds.net  Thu Nov 12 13:00:41 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 12 Nov 2009 07:00:41 -0500
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
	<bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>
	<dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com>
Message-ID: <1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>

On Thu, Nov 12, 2009 at 6:35 AM, Luke Paireepinart
<rabidpoobear at gmail.com> wrote:
>
>
> On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen <jra at nella.org> wrote:
>>
>> You are looking for the __str__ method. See
>> http://docs.python.org/reference/datamodel.html#object.__str__
>>
> Can't you also implement __repr__?

Yes, in fact if you are only going to implement one of __str__ and
__repr__, arguably __repr__ is a better choice. __repr__() is called
by the interactive interpreter when it displays an object. __str__ is
called by print, and if you don't define __str__ it will call
__repr__. So defining only __str__ will not give a custom
representation unless you print:

In [1]: class Foo():
   ...:     def __str__(self):
   ...:         return "I'm a Foo"

In [2]: f = Foo()

In [3]: f
Out[3]: <__main__.Foo instance at 0x1433468>

In [4]: print f
I'm a Foo


Defining __repr__ will give the custom representation when you just
give the name of the object:

In [5]: class Foo2():
   ...:     def __repr__(self):
   ...:         return "I'm a Foo2"
   ...:
   ...:

In [6]: f2=Foo2()

In [7]: f2
Out[7]: I'm a Foo2

In [8]: print f2
I'm a Foo2

Kent

From waynejwerner at gmail.com  Thu Nov 12 14:17:39 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Thu, 12 Nov 2009 07:17:39 -0600
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com> 
	<bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com> 
	<dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com> 
	<1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>
Message-ID: <333efb450911120517y2603a5aby98eae79eeb03e54f@mail.gmail.com>

On Thu, Nov 12, 2009 at 6:00 AM, Kent Johnson <kent37 at tds.net> wrote:

> <snip>
>
> Defining __repr__ will give the custom representation when you just
> give the name of the object:
>
> In [5]: class Foo2():
>   ...:     def __repr__(self):
>   ...:         return "I'm a Foo2"
>   ...:
>   ...:
>
> In [6]: f2=Foo2()
>
> In [7]: f2
> Out[7]: I'm a Foo2
>
> In [8]: print f2
> I'm a Foo2
>
>
Which can be surprisingly useful in certain cases (I've used it for
debugging).

class Foo2:
    def __init__(self):
        self.name = 'This is my name'
        self.value = 'This is my value'

    def __repr__(self):
        return "Name: %s\nValue: %s" % (self.name, self.value)

In [2]: a = Foo2()

In [3]: a
Out[3]:
Name: This is my name
Value: This is my value

In [4]: print a
------> print(a)
Name: This is my name
Value: This is my value

HTH,
Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091112/901f5547/attachment-0001.htm>

From stefan at lsd.co.za  Thu Nov 12 15:17:37 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Thu, 12 Nov 2009 16:17:37 +0200
Subject: [Tutor] importing variables
Message-ID: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>

Hi guys,

Im trying to do something and hit a bit of a wall, potentially im
going about this the wrong way. Essentially the problem is:

features file contains
rt='''text'''

import features

a = 'rt'
print features.rt  #this works
print features.a  #this fails

I need to use features.a as i am iterating through a list and a would
be the different features i want to check if they exist, and then use
the variable.

I hope that makes sense, or if anyone has any suggestion on how
properly to do this!

Thanks in advance

stefan

From alan.gauld at btinternet.com  Thu Nov 12 15:38:12 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 12 Nov 2009 14:38:12 -0000
Subject: [Tutor] importing variables
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>
Message-ID: <hdh6kt$f8c$1@ger.gmane.org>


"Stefan Lesicnik" <stefan at lsd.co.za> wrote

> features file contains
> rt='''text'''
> 
> import features
> 
> a = 'rt'
> print features.rt  #this works
> print features.a  #this fails

Because you defined a in your current file.
You need to define it in features.

> I need to use features.a as i am iterating through a list and a would
> be the different features i want to check if they exist, and then use
> the variable.

So you need to define a in features.
Note that if you define a in features to be an empty list you can 
add/delete items to that list from the importing module.

########## features.py##########
a = []

###########main.py##########

import features
print features.a

features.a.append(66)
print features.a

features.a.remove(66)
print features.a


Does that help?

NB. I don't really recommend this approach, but it is a way 
to do what I think you want...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From stefan at lsd.co.za  Thu Nov 12 16:03:04 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Thu, 12 Nov 2009 17:03:04 +0200
Subject: [Tutor] importing variables
In-Reply-To: <hdh6kt$f8c$1@ger.gmane.org>
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com> 
	<hdh6kt$f8c$1@ger.gmane.org>
Message-ID: <5cb309e70911120703q717d1251x69546b72c1b8ed65@mail.gmail.com>

On Thu, Nov 12, 2009 at 4:38 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Stefan Lesicnik" <stefan at lsd.co.za> wrote
>
>> features file contains
>> rt='''text'''
>>
>> import features
>>
>> a = 'rt'
>> print features.rt ?#this works
>> print features.a ?#this fails
>
> Because you defined a in your current file.
> You need to define it in features.
>
>> I need to use features.a as i am iterating through a list and a would
>> be the different features i want to check if they exist, and then use
>> the variable.
>
> So you need to define a in features.
> Note that if you define a in features to be an empty list you can add/delete
> items to that list from the importing module.
>
> ########## features.py##########
> a = []
>
> ###########main.py##########
>
> import features
> print features.a
>
> features.a.append(66)
> print features.a
>
> features.a.remove(66)
> print features.a
>
>
> Does that help?
>
> NB. I don't really recommend this approach, but it is a way to do what I
> think you want...
>

Thanks Alan & Christian,  but i think I probably didnt communicate
what i was trying to do. Maybe a sample of the actual code will help


#!/usr/bin/python

import fileinput
import re
import features

client = open('clients/bob.txt', 'r')
template = open('temp.txt', 'w')

#Read all client data to variable dictionary
data = {}
for line in client:
    line = line.strip()
    line = line.split('=')
    data[line[0]] = line[1]

requestedfeatures = data['features'].split(',')
client.close()

#Write template and modify all relevant sections
for line in fileinput.input('template/noc-template.txt'):
    line = line.strip()
    templatereplace = re.match(r'.*\%\%(.*)\%\%.*', line)
    featurereplace = re.match(r'.*\$\$(.*)\$\$.*', line)
    if templatereplace:
        newline = re.sub('\%\%' + templatereplace.group(1) + '\%\%',
data[templatereplace.group(1)], line)
        print newline
        template.write(newline + '\n')
    elif featurereplace:
        if featurereplace.group(1) in requestedfeatures:
            newline = re.sub('\$\$' + featurereplace.group(1) +
'\$\$', features.rt, line)
            print newline
            template.write(newline + '\n')
    else:
        print line
        template.write(line + '\n')
template.close()



So.  At the end of the day, i want to generate a page that will be
read in a wiki.  The requestedfeatures is a variable of features. So
the end generated file will have certain extra fields depending on
what features were selected. So the noc-template.txt is the main
template which looks like this

========= %%clientname%% =========

this is the report

tech contact: %%technical_contact%%

$$rt$$


So my idea was to replace %%clientname%% with the dictionary
and replace $$rt$$ with features from the features.py.  The issue i
have is with the featurereplace. and this line --newline =
re.sub('\$\$' + featurereplace.group(1) + '\$\$', features.rt,
line)--.  Using features.rt works, but i essentially want to use
features.featurereplace.group(1)  which should be features.rt , but
saying features.featurereplace.group(1) seems to look for that
variable and doesnt substitute.  I've tried things like features. +
featuresreplace.group(1) etc but that doesnt work.

Furthermore, i think what im trying to do here is a mix of templating,
but i looked at cheetah and mako and cant work it out from the docs
:(.

I am probably doing this completely the wrong way round and appreciate
better ideas on 'how to do things (tm)'.

thanks!!

:)

From davea at ieee.org  Thu Nov 12 16:33:41 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 12 Nov 2009 10:33:41 -0500
Subject: [Tutor] Unexpected iterator
In-Reply-To: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
Message-ID: <4AFC2AD5.6030308@ieee.org>



Jeff R. Allen wrote:
> Hello,
>
> I am working my way through the tutorial, and I like trying
> variations, just to see what expected errors look like, and other ways
> things could be written.
>
> I tried a, b = 0, 0 and that worked.
>
> Then I tried this to (maybe) set both a and b to 0:
>
>   
>>>> a, b = 0
>>>>         
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'int' object is not iterable
>
> I understand why it doesn't work, but I don't understand the wording
> of the exception. Could someone explain how I accidentally introduced
> iteration into the picture with my syntax? I have a feeling there's an
> interesting lesson hidden in this example...
>
> Thanks.
>
>   -jeff
>
>   
What you've really got going across the = sign is a list, or something 
like it.  On the left side, you have exactly two items, so on the right 
side you need a list or tuple of length 2.  In your first example, you 
have a tuple, because of the comma.

The generalization implied above is an iterable.  Lists and tuples are 
iterables, but you could also use a generator, for example.  Anything 
that you could iterate over, such as with a for statement.

a, b, c, d = xrange(4)

Anyway, in your second example, the system is trying to interpret the 
right side as an iterable, in order to get two items from it.

DaveA


From davea at ieee.org  Thu Nov 12 16:45:43 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 12 Nov 2009 10:45:43 -0500
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>	<bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>	<dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com>
	<1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>
Message-ID: <4AFC2DA7.8050807@ieee.org>



Kent Johnson wrote:
> On Thu, Nov 12, 2009 at 6:35 AM, Luke Paireepinart
> <rabidpoobear at gmail.com> wrote:
>   
>> On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen <jra at nella.org> wrote:
>>     
>>> You are looking for the __str__ method. See
>>> http://docs.python.org/reference/datamodel.html#object.__str__
>>>
>>>       
>> Can't you also implement __repr__?
>>     
>
> Yes, in fact if you are only going to implement one of __str__ and
> __repr__, arguably __repr__ is a better choice. __repr__() is called
> by the interactive interpreter when it displays an object. __str__ is
> called by print, and if you don't define __str__ it will call
> __repr__. So defining only __str__ will not give a custom
> representation unless you print:
>
> In [1]: class Foo():
>    ...:     def __str__(self):
>    ...:         return "I'm a Foo"
>
> In [2]: f = Foo()
>
> In [3]: f
> Out[3]: <__main__.Foo instance at 0x1433468>
>
> In [4]: print f
> I'm a Foo
>
>
> Defining __repr__ will give the custom representation when you just
> give the name of the object:
>
> In [5]: class Foo2():
>    ...:     def __repr__(self):
>    ...:         return "I'm a Foo2"
>    ...:
>    ...:
>
> In [6]: f2=Foo2()
>
> In [7]: f2
> Out[7]: I'm a Foo2
>
> In [8]: print f2
> I'm a Foo2
>
> Kent
>
>   
And one other important place that uses __repr__() is the printing of 
containers.  So if you have a list of Foo2 objects, and you want to just say
    print mylist

it's better to have __repr__().



From bgailer at gmail.com  Thu Nov 12 17:26:39 2009
From: bgailer at gmail.com (bob gailer)
Date: Thu, 12 Nov 2009 11:26:39 -0500
Subject: [Tutor] importing variables
In-Reply-To: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>
Message-ID: <4AFC373F.4070405@gmail.com>

Stefan Lesicnik wrote:
> Hi guys,
>
> Im trying to do something and hit a bit of a wall, potentially im
> going about this the wrong way. Essentially the problem is:
>
> features file contains
> rt='''text'''
>
> import features
>
> a = 'rt'
> print features.rt  #this works
> print features.a  #this fails
>
> I need to use features.a as i am iterating through a list and a would
> be the different features i want to check if they exist, and then use
> the variable.
>
> I hope that makes sense, or if anyone has any suggestion on how
> properly to do this!
>   

Even though Alan missed your point, I understood it. So your explanation 
was OK (for me).

print getattr(features, a)



-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From stefan at lsd.co.za  Thu Nov 12 17:34:56 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Thu, 12 Nov 2009 18:34:56 +0200
Subject: [Tutor] importing variables
In-Reply-To: <4AFC373F.4070405@gmail.com>
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com> 
	<4AFC373F.4070405@gmail.com>
Message-ID: <5cb309e70911120834q756cbc62n474f4852107ad42@mail.gmail.com>

On Thu, Nov 12, 2009 at 6:26 PM, bob gailer <bgailer at gmail.com> wrote:
> Stefan Lesicnik wrote:
>>
>> Hi guys,
>>
>> Im trying to do something and hit a bit of a wall, potentially im
>> going about this the wrong way. Essentially the problem is:
>>
>> features file contains
>> rt='''text'''
>>
>> import features
>>
>> a = 'rt'
>> print features.rt ?#this works
>> print features.a ?#this fails
>>
>> I need to use features.a as i am iterating through a list and a would
>> be the different features i want to check if they exist, and then use
>> the variable.
>>
>> I hope that makes sense, or if anyone has any suggestion on how
>> properly to do this!
>>
>
> Even though Alan missed your point, I understood it. So your explanation was
> OK (for me).
>
> print getattr(features, a)

Thanks Bob!

That works. I hadn't heard about getattr

Much appreciated
stefan

From andrefsp at gmail.com  Thu Nov 12 17:23:42 2009
From: andrefsp at gmail.com (=?ISO-8859-1?Q?andr=E9?= palma)
Date: Thu, 12 Nov 2009 16:23:42 +0000
Subject: [Tutor] django python Version 1.1.1 - Internacionalization
In-Reply-To: <333efb450911112205y37795c2bp151d0d8d149e9f5a@mail.gmail.com>
References: <1257999029.5714.0.camel@andrefsp-eeepc>
	<333efb450911112205y37795c2bp151d0d8d149e9f5a@mail.gmail.com>
Message-ID: <1258043022.3691.9.camel@andrefsp-eeepc>




On Thu, 2009-11-12 at 00:05 -0600, Wayne Werner wrote:
> 
> 
> On Wed, Nov 11, 2009 at 10:10 PM, andr? palma <andrefsp at gmail.com>
> wrote:
>         Hi all!
>         I'm developing a django web application but i'm having a
>         trouble with
>         the internationalization.
> 
> 
> I don't know much about Django, and I suspect that's more the norm
> here - you may have better luck on the Django users list:
> 
> 
> http://groups.google.com/group/django-users?pli=1
>  
> HTH,
> Wayne


Tanks a lot for your help. But after a try a few many times i finally
figured out where the problem was coming from. 

I don't know exactly why but it seems that you really have to use your
manage.py file to create those translation files. 

I was using "django-admin makemessages -l <language>" command in my
project folder( like it is on django-book ) and i couldn't get the
translations working. So i did solved this issue by using the following
on my project folder.

::: python manage.py makemessages -l <language> 


It worked for me and i found that it might be useful for someone in the
same situation. 




From alan.gauld at btinternet.com  Thu Nov 12 18:34:57 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 12 Nov 2009 17:34:57 -0000
Subject: [Tutor] importing variables
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>
	<4AFC373F.4070405@gmail.com>
Message-ID: <hdhh0b$kv5$1@ger.gmane.org>


"bob gailer" <bgailer at gmail.com> wrote

>> I need to use features.a as i am iterating through a list and a would
>> be the different features i want to check if they exist, and then use
>> the variable.
>>
>> I hope that makes sense, or if anyone has any suggestion on how
>> properly to do this!
>
> Even though Alan missed your point, I understood it. So your explanation 
> was OK (for me).
>
> print getattr(features, a)

And now that I see Bob's solution I understand the question! :-)

Alan G. 



From davea at ieee.org  Thu Nov 12 20:08:29 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 12 Nov 2009 14:08:29 -0500
Subject: [Tutor] importing variables
In-Reply-To: <5cb309e70911120834q756cbc62n474f4852107ad42@mail.gmail.com>
References: <5cb309e70911120617j77ea4341s9756957b2d18ba46@mail.gmail.com>
	<4AFC373F.4070405@gmail.com>
	<5cb309e70911120834q756cbc62n474f4852107ad42@mail.gmail.com>
Message-ID: <4AFC5D2D.4080105@ieee.org>

Stefan Lesicnik wrote:
> On Thu, Nov 12, 2009 at 6:26 PM, bob gailer <bgailer at gmail.com> wrote:
>   
>> Stefan Lesicnik wrote:
>>     
>>> Hi guys,
>>>
>>> Im trying to do something and hit a bit of a wall, potentially im
>>> going about this the wrong way. Essentially the problem is:
>>>
>>> features file contains
>>> rt='text'''
>>>
>>> import features
>>>
>>> a =rt'
>>> print features.rt  #this works
>>> print features.a  #this fails
>>>
>>> I need to use features.a as i am iterating through a list and a would
>>> be the different features i want to check if they exist, and then use
>>> the variable.
>>>
>>> I hope that makes sense, or if anyone has any suggestion on how
>>> properly to do this!
>>>
>>>       
>> Even though Alan missed your point, I understood it. So your explanation was
>> OK (for me).
>>
>> print getattr(features, a)
>>     
>
> Thanks Bob!
>
> That works. I hadn't heard about getattr
>
> Much appreciated
> stefan
>
>   
1) Bob's suggestion is what I would have said as well, but using a 3rd 
argument to avoid getting exceptions:
    thisfeature = getattr(features, a, None)

But now that I see it, I'd mention other possibilities.

2)  Since the features.py is being generated, why not just make sure it 
has all the required attributes, perhaps using None as a flag to 
indicate that the feature is disabled.

3) Alternatively, let features.py generate an instance of a class you 
define in your (non-generated) code.  That class can have default 
behaviors to handle missing features, as well as interrelated features 
(eg. if feature3 is enabled, then disable feature4, even if the user 
wants it).

    import features
    myfeatures = features.factory()

and now  myfeatures.rt is guaranteed to exist, because the class makes 
sure of it.


General principle, avoid indexing by string if all the strings could be 
known in advance - that's what regular attribute notation is for.  And 
if there's a part that's out of your control (say data from bob.txt), 
isolate knowledge of that from the rest of the code.

Avoiding code generation:
I don't know cheetah or mako, and can't say I really understand your 
environment.   But if it's CGI stuff for example, I'd beware of 
generating code, since you then run the risk of multiple clients 
colliding with each other.  If you can do things entirely in data 
structures, things can be safely made reusable with no risk of two 
separate instances of the interpreter interfering with each other.



From yashwinkanchan at gmail.com  Fri Nov 13 01:41:44 2009
From: yashwinkanchan at gmail.com (Yashwin Kanchan)
Date: Fri, 13 Nov 2009 06:11:44 +0530
Subject: [Tutor] COM server: cannot assign property
Message-ID: <eae1591c0911121641t5d478415h104c15cf2fdc5541@mail.gmail.com>

Hi Guys

I am trying to create a simple test COM server , but am have trouble
assigning any property value to it through excel VBA.

Please point out where i am going wrong.


#COM server
class test(object):

    _reg_clsid_ = "{B5901450-F9A1-4F76-8FCF-2BFFA96ED210}"
    _reg_progid_ = "Python.Test"
    _public_methods_ = ["arg"]
    _public_attrs_ = ["t"]

    def __init__(self):
        self._t=0

    def arg(self,s,r):
        return (s,r)

    def get_t(self):
        return self._t

    def set_t(self,value):
        self._t = str(value)

    t = property(get_t,set_t)

if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(test)
    print "done"

VBA Code:

Sub Button1_Click()
    Set test = CreateObject("Python.Test")
    test.arg 2, 5
    test.t = "hello"
    MsgBox test.t
End Sub


Error; "Object doesnt support this property or method" at test.t = "hello"

Thanks
Yashwin Kanchan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091113/aac66077/attachment.htm>

From alan.gauld at btinternet.com  Fri Nov 13 02:29:08 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 13 Nov 2009 01:29:08 -0000
Subject: [Tutor] COM server: cannot assign property
References: <eae1591c0911121641t5d478415h104c15cf2fdc5541@mail.gmail.com>
Message-ID: <hdicpe$brk$1@ger.gmane.org>

"Yashwin Kanchan" <yashwinkanchan at gmail.com> wrote

> I am trying to create a simple test COM server , but am have trouble
> assigning any property value to it through excel VBA.

I've never tried using properties via COM.
Does it work if you disable the property aspect and expose
the set/get methods directly? Can you access those methods
successfully from VB?

If that works it suggests the problem lies in the property
definition stuff...

HTH,

Alan G

> Please point out where i am going wrong.
>
>
> #COM server
> class test(object):
>
>    _reg_clsid_ = "{B5901450-F9A1-4F76-8FCF-2BFFA96ED210}"
>    _reg_progid_ = "Python.Test"
>    _public_methods_ = ["arg"]
>    _public_attrs_ = ["t"]
>
>    def __init__(self):
>        self._t=0
>
>    def arg(self,s,r):
>        return (s,r)
>
>    def get_t(self):
>        return self._t
>
>    def set_t(self,value):
>        self._t = str(value)
>
>    t = property(get_t,set_t)
>
> if __name__=='__main__':
>    import win32com.server.register
>    win32com.server.register.UseCommandLine(test)
>    print "done"
>
> VBA Code:
>
> Sub Button1_Click()
>    Set test = CreateObject("Python.Test")
>    test.arg 2, 5
>    test.t = "hello"
>    MsgBox test.t
> End Sub
>
>
> Error; "Object doesnt support this property or method" at test.t = 
> "hello"
>
> Thanks
> Yashwin Kanchan
>


--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 



From bibsmendez at gmail.com  Fri Nov 13 14:17:57 2009
From: bibsmendez at gmail.com (biboy mendz)
Date: Fri, 13 Nov 2009 16:17:57 +0300
Subject: [Tutor] Question re: hangman.py
Message-ID: <4AFD5C85.7090606@gmail.com>

http://inventwithpython.com

chapter 8: hangman.py

expression is: print(letter, end=' ')

it explained:
end keyword argument in print() call makes the print() function put a space
character at the end of the string instead of a newline.

however when run it gives error: SyntaxError: invalid syntax.

What gives? This is the first time i saw such expression inside print
function. Is this version-specific of python? I'm running version 2.5.4.

-- 
Regards,
bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 ????? - kde-full - (200907141427) ]
www.sidux.com


From hugo.yoshi at gmail.com  Fri Nov 13 14:36:57 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Fri, 13 Nov 2009 14:36:57 +0100
Subject: [Tutor] Question re: hangman.py
In-Reply-To: <4AFD5C85.7090606@gmail.com>
References: <4AFD5C85.7090606@gmail.com>
Message-ID: <29179d160911130536r14e7e4bcya086c811e11195f2@mail.gmail.com>

On Fri, Nov 13, 2009 at 2:17 PM, biboy mendz <bibsmendez at gmail.com> wrote:
> http://inventwithpython.com
>
> chapter 8: hangman.py
>
> expression is: print(letter, end=' ')
>
> it explained:
> end keyword argument in print() call makes the print() function put a space
> character at the end of the string instead of a newline.
>
> however when run it gives error: SyntaxError: invalid syntax.
>
> What gives? This is the first time i saw such expression inside print
> function. Is this version-specific of python? I'm running version 2.5.4.
>

Yes, the print function is a new feature of python 3. The equivalent
statement in python 2.5 is probably something like this:

print letter, ' ',

The print function is a statement in python before 3.0. It takes a
comma-separated list of things to print. The trailing comma prevents
python from appending a newline.

Hugo

From modulok at gmail.com  Fri Nov 13 17:35:39 2009
From: modulok at gmail.com (Modulok)
Date: Fri, 13 Nov 2009 09:35:39 -0700
Subject: [Tutor] How to call a method with a print statement?
In-Reply-To: <4AFC2DA7.8050807@ieee.org>
References: <64c038660911120231k74a84f88p2354d1d8320282b4@mail.gmail.com>
	<bccc8ac80911120329i723e3e9g788883f8be6e7f24@mail.gmail.com>
	<dfeb4470911120335s3b920346y4eac0a9ade469717@mail.gmail.com>
	<1c2a2c590911120400p426bf243yaf27448b5182fc01@mail.gmail.com>
	<4AFC2DA7.8050807@ieee.org>
Message-ID: <64c038660911130835w42351f89n1adabe62b47a454d@mail.gmail.com>

List,

 __repr__() is exactly what I was looking for :)

You guys rock! Thank you.
-Modulok-

On 11/12/09, Dave Angel <davea at ieee.org> wrote:
>
>
> Kent Johnson wrote:
>> On Thu, Nov 12, 2009 at 6:35 AM, Luke Paireepinart
>> <rabidpoobear at gmail.com> wrote:
>>
>>> On Thu, Nov 12, 2009 at 5:29 AM, Jeff R. Allen <jra at nella.org> wrote:
>>>
>>>> You are looking for the __str__ method. See
>>>> http://docs.python.org/reference/datamodel.html#object.__str__
>>>>
>>>>
>>> Can't you also implement __repr__?
>>>
>>
>> Yes, in fact if you are only going to implement one of __str__ and
>> __repr__, arguably __repr__ is a better choice. __repr__() is called
>> by the interactive interpreter when it displays an object. __str__ is
>> called by print, and if you don't define __str__ it will call
>> __repr__. So defining only __str__ will not give a custom
>> representation unless you print:
>>
>> In [1]: class Foo():
>>    ...:     def __str__(self):
>>    ...:         return "I'm a Foo"
>>
>> In [2]: f = Foo()
>>
>> In [3]: f
>> Out[3]: <__main__.Foo instance at 0x1433468>
>>
>> In [4]: print f
>> I'm a Foo
>>
>>
>> Defining __repr__ will give the custom representation when you just
>> give the name of the object:
>>
>> In [5]: class Foo2():
>>    ...:     def __repr__(self):
>>    ...:         return "I'm a Foo2"
>>    ...:
>>    ...:
>>
>> In [6]: f2=Foo2()
>>
>> In [7]: f2
>> Out[7]: I'm a Foo2
>>
>> In [8]: print f2
>> I'm a Foo2
>>
>> Kent
>>
>>
> And one other important place that uses __repr__() is the printing of
> containers.  So if you have a list of Foo2 objects, and you want to just say
>     print mylist
>
> it's better to have __repr__().
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From alan.gauld at btinternet.com  Fri Nov 13 18:49:58 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 13 Nov 2009 17:49:58 -0000
Subject: [Tutor] Question re: hangman.py
References: <4AFD5C85.7090606@gmail.com>
Message-ID: <hdk68h$e6r$1@ger.gmane.org>


"biboy mendz" <bibsmendez at gmail.com> wrote

> chapter 8: hangman.py
>
> expression is: print(letter, end=' ')
>
> it explained:
> end keyword argument in print() call makes the print() function put a 
> space
> character at the end of the string instead of a newline.
>
> however when run it gives error: SyntaxError: invalid syntax.
>
> What gives? This is the first time i saw such expression inside print
> function. Is this version-specific of python? I'm running version 2.5.4.

Your tutorial is using version 3.
In 2.6 simply put a comma after the string:

print letter,

to do the same thing. 



From alan.gauld at btinternet.com  Fri Nov 13 18:51:40 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 13 Nov 2009 17:51:40 -0000
Subject: [Tutor] Question re: hangman.py
References: <4AFD5C85.7090606@gmail.com>
	<29179d160911130536r14e7e4bcya086c811e11195f2@mail.gmail.com>
Message-ID: <hdk6bn$eh8$1@ger.gmane.org>


"Hugo Arts" <hugo.yoshi at gmail.com> wrote 

> print letter, ' ',
> 

You don't need the space, Python automatically inserts 
a space instead of the newline when you use the comma.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/


From sanelson at gmail.com  Fri Nov 13 18:58:30 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Fri, 13 Nov 2009 17:58:30 +0000
Subject: [Tutor] Iterable Understanding
Message-ID: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>

I think I'm having a major understanding failure.

So having discovered that my Unix sort breaks on the last day of the
month, I've gone ahead and implemented a per log search, using heapq.

I've tested it with various data, and it produces a sorted logfile, per log.

So in essence this:

logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ),
         LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ),
         LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ]

Gives me a list of LogFiles - each of which has a getline() method,
which returns a tuple.

I thought I could merge iterables using Kent's recipe, or just with
heapq.merge()

But how do I get from a method that can produce a tuple, to some
mergable iterables?

for log in logs:
  l = log.getline()
  print l

This gives me three loglines.  How do I get more?  Other than while True:

Of course tuples are iterables, but that doesn't help, as I want to
sort on timestamp... so a list of tuples would be ok....  But how do I
construct that, bearing in mind I am trying not to use up too much
memory?

I think there's a piece of the jigsaw I just don't get.  Please help!

The code in full is here:

import gzip, heapq, re

class LogFile:
   def __init__(self, filename, date):
       self.logfile = gzip.open(filename, 'r')
       for logline in self.logfile:
           self.line = logline
           self.stamp = self.timestamp(self.line)
           if self.stamp.startswith(date):
               break
       self.initialise_heap()

   def timestamp(self, line):
       stamp = re.search(r'\[(.*?)\]', line).group(1)
       return stamp

   def initialise_heap(self):
       initlist=[]
       self.heap=[]
       for x in xrange(10):
           self.line=self.logfile.readline()
           self.stamp=self.timestamp(self.line)
           initlist.append((self.stamp,self.line))
       heapq.heapify(initlist)
       self.heap=initlist


   def getline(self):
       self.line=self.logfile.readline()
       stamp=self.timestamp(self.line)
       heapq.heappush(self.heap, (stamp, self.line))
       pop = heapq.heappop(self.heap)
       return pop

logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ),
         LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ),
         LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ]

From emile at fenx.com  Fri Nov 13 19:37:25 2009
From: emile at fenx.com (Emile van Sebille)
Date: Fri, 13 Nov 2009 10:37:25 -0800
Subject: [Tutor] COM server: cannot assign property
In-Reply-To: <eae1591c0911121641t5d478415h104c15cf2fdc5541@mail.gmail.com>
References: <eae1591c0911121641t5d478415h104c15cf2fdc5541@mail.gmail.com>
Message-ID: <hdk91e$nbk$1@ger.gmane.org>

On 11/12/2009 4:41 PM Yashwin Kanchan said...
> Hi Guys
> 
> I am trying to create a simple test COM server , but am have trouble 
> assigning any property value to it through excel VBA.
> 
> Please point out where i am going wrong.
> 
> 
> #COM server
> class test(object):
> 
>     _reg_clsid_ = "{B5901450-F9A1-4F76-8FCF-2BFFA96ED210}"
>     _reg_progid_ = "Python.Test"
>     _public_methods_ = ["arg"]

My bet is that the problem is here.  Do you need to expose t as well?

I wrote one of these six or seven years ago -- here're the critical bits 
(where EMSpecs is a class defined in the same module):

class fenxUtilities:
     _public_methods_ = [ 'FirstPartInsp' ]
     _reg_progid_ = "fenxDCom.Util"
     _reg_clsid_ = "{3EAD7AB4-2978-4360-8F7D-33FB36E9E146}"
     def FirstPartInsp(self, nomDiam, numFlutes, nomOAL, nomLOC):
         return EMSpecs(nomDiam, numFlutes, nomOAL, nomLOC).retvals


if __name__=='__main__':
     print "Registering COM server..."
     import win32com.server.register
     win32com.server.register.UseCommandLine(fenxUtilities)

HTH,

Emile


>     _public_attrs_ = ["t"]
>    
>     def __init__(self):
>         self._t=0
>    
>     def arg(self,s,r):
>         return (s,r)
>        
>     def get_t(self):
>         return self._t
>    
>     def set_t(self,value):
>         self._t = str(value)
>        
>     t = property(get_t,set_t)
>     
> if __name__=='__main__':
>     import win32com.server.register
>     win32com.server.register.UseCommandLine(test)
>     print "done"
> 
> VBA Code:
> 
> Sub Button1_Click()
>     Set test = CreateObject("Python.Test")
>     test.arg 2, 5
>     test.t = "hello"
>     MsgBox test.t
> End Sub
> 
> 
> Error; "Object doesnt support this property or method" at test.t = "hello"
> 
> Thanks
> Yashwin Kanchan
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


From bibsmendez at gmail.com  Fri Nov 13 19:37:41 2009
From: bibsmendez at gmail.com (biboy mendz)
Date: Fri, 13 Nov 2009 21:37:41 +0300
Subject: [Tutor] Question re: hangman.py
In-Reply-To: <hdk68h$e6r$1@ger.gmane.org>
References: <4AFD5C85.7090606@gmail.com> <hdk68h$e6r$1@ger.gmane.org>
Message-ID: <4AFDA775.2030707@gmail.com>

thanks a lot for the clarification Alan and all.

-- 
Regards,
bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 ????? - kde-full - (200907141427) ]
www.sidux.com



Alan Gauld wrote:
>
> "biboy mendz" <bibsmendez at gmail.com> wrote
>
>> chapter 8: hangman.py
>>
>> expression is: print(letter, end=' ')
>>
>> it explained:
>> end keyword argument in print() call makes the print() function put a 
>> space
>> character at the end of the string instead of a newline.
>>
>> however when run it gives error: SyntaxError: invalid syntax.
>>
>> What gives? This is the first time i saw such expression inside print
>> function. Is this version-specific of python? I'm running version 2.5.4.
>
> Your tutorial is using version 3.
> In 2.6 simply put a comma after the string:
>
> print letter,
>
> to do the same thing.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From denis.spir at free.fr  Fri Nov 13 20:48:30 2009
From: denis.spir at free.fr (spir)
Date: Fri, 13 Nov 2009 20:48:30 +0100
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
Message-ID: <20091113204830.2187bedb@o>

Le Fri, 13 Nov 2009 17:58:30 +0000,
Stephen Nelson-Smith <sanelson at gmail.com> s'exprima ainsi:

> I think I'm having a major understanding failure.
> 
> So having discovered that my Unix sort breaks on the last day of the
> month, I've gone ahead and implemented a per log search, using heapq.
> 
> I've tested it with various data, and it produces a sorted logfile, per log.
> 
> So in essence this:
> 
> logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ]
> 
> Gives me a list of LogFiles - each of which has a getline() method,
> which returns a tuple.
> 
> I thought I could merge iterables using Kent's recipe, or just with
> heapq.merge()
> 
> But how do I get from a method that can produce a tuple, to some
> mergable iterables?
> 
> for log in logs:
>   l = log.getline()
>   print l
> 
> This gives me three loglines.  How do I get more?  Other than while True:

I'm not 100% sure to understand your needs and intention; just have a try. Maybe what you want actually is rather:

for log in logs:
  for line in log:
    print l

Meaning your log objects need be iterable. To do this, you must have an __iter__ method that would surely simply return the object's getline (or maybe replace it alltogether). Then when walking the log with for...in, python will silently call getline until error. This means getline must raise StopIteration when the log is "empty" and __iter__ must "reset" it.
Another solution may be to subtype "file", for a file is precisely an iterator over lines; and you really get your data from a file. Simply (sic), there must some job done about this issue of time stamps (haven't studied in details). Still, i guess this track may be worth an little study.
Once you get logs iterable, you may subtype list for your overall log collection and set it an __iter__ method like:

    for log in self:
        for line in log:
            yield line

(The trick is not from me.)
Then you can write:
    for line in my_log_collection

> Of course tuples are iterables, but that doesn't help, as I want to
> sort on timestamp... so a list of tuples would be ok....  But how do I
> construct that, bearing in mind I am trying not to use up too much
> memory?
> 
> I think there's a piece of the jigsaw I just don't get.  Please help!
> 
> The code in full is here:
> 
> import gzip, heapq, re
> 
> class LogFile:
>    def __init__(self, filename, date):
>        self.logfile = gzip.open(filename, 'r')
>        for logline in self.logfile:
>            self.line = logline
>            self.stamp = self.timestamp(self.line)
>            if self.stamp.startswith(date):
>                break
>        self.initialise_heap()
> 
>    def timestamp(self, line):
>        stamp = re.search(r'\[(.*?)\]', line).group(1)
>        return stamp
> 
>    def initialise_heap(self):
>        initlist=[]
>        self.heap=[]
>        for x in xrange(10):
>            self.line=self.logfile.readline()
>            self.stamp=self.timestamp(self.line)
>            initlist.append((self.stamp,self.line))
>        heapq.heapify(initlist)
>        self.heap=initlist
> 
> 
>    def getline(self):
>        self.line=self.logfile.readline()
>        stamp=self.timestamp(self.line)
>        heapq.heappush(self.heap, (stamp, self.line))
>        pop = heapq.heappop(self.heap)
>        return pop
> 
> logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ]
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From cspears2002 at yahoo.com  Sat Nov 14 07:14:09 2009
From: cspears2002 at yahoo.com (Christopher Spears)
Date: Fri, 13 Nov 2009 22:14:09 -0800 (PST)
Subject: [Tutor] parsing XML into a python dictionary
Message-ID: <548447.35613.qm@web51601.mail.re2.yahoo.com>

I've been working on a way to parse an XML document and convert it into a python dictionary.  I want to maintain the hierarchy of the XML.  Here is the sample XML I have been working on:

<collection>
  <comic title="Sandman" number='62'>
    <writer>Neil Gaiman</writer>
    <penciller pages='1-9,18-24'>Glyn Dillon</penciller>
    <penciller pages="10-17">Charles Vess</penciller>
  </comic>
</collection>

This is my first stab at this:

#!/usr/bin/env python

from lxml import etree

def generateKey(element):
    if element.attrib:
        key = (element.tag, element.attrib)
    else:
	key = element.tag
    return key	

class parseXML(object):
    def __init__(self, xmlFile = 'test.xml'):
        self.xmlFile = xmlFile
	
    def parse(self):
        doc = etree.parse(self.xmlFile)
	root = doc.getroot()
	key = generateKey(root)
	dictA = {}
	for r in root.getchildren():
	    keyR = generateKey(r)
	    if r.text:
	        dictA[keyR] = r.text
	    if r.getchildren():
	        dictA[keyR] = r.getchildren()
		
	newDict = {}
	newDict[key] = dictA
	return newDict
	        	
if __name__ == "__main__":
    px = parseXML()
    newDict = px.parse()
    print newDict
	
This is the output:
163>./parseXML.py
{'collection': {('comic', {'number': '62', 'title': 'Sandman'}): [<Element writer at -482193f4>, <Element penciller at -482193cc>, <Element penciller at -482193a4>]}}

The script doesn't descend all of the way down because I'm not sure how to hand a XML document that may have multiple layers.  Advice anyone?  Would this be a job for recursion?

Thanks!

From emailkgnow at gmail.com  Sat Nov 14 09:40:32 2009
From: emailkgnow at gmail.com (Khalid Al-Ghamdi)
Date: Sat, 14 Nov 2009 11:40:32 +0300
Subject: [Tutor] getting python 3 to run from the command line
Message-ID: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>

Hi,

for some reason I haven't been able to get python 3 to work from the the
command line. I've added it to the the path in the the system variables but
still no luck. when I try to run python 2.6 it works flawlessly, though,
bearing in mind that it wasn't added to the system variables!

i'm running windows vista and have python 30 ,31 and 26 installed.

all of this , by the way, was just to get easy install to work so i can get
nose working to do some practice of unit testing!

can anyone shed some light on the issue?

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091114/e9cf4a0d/attachment.htm>

From alan.gauld at btinternet.com  Sat Nov 14 09:50:19 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 14 Nov 2009 08:50:19 -0000
Subject: [Tutor] parsing XML into a python dictionary
References: <548447.35613.qm@web51601.mail.re2.yahoo.com>
Message-ID: <hdlr0n$bhd$1@ger.gmane.org>

"Christopher Spears" <cspears2002 at yahoo.com> wrote

> I've been working on a way to parse an XML document and
> convert it into a python dictionary.  I want to maintain the hierarchy of 
> the XML.

> Here is the sample XML I have been working on:
>
> <collection>
>  <comic title="Sandman" number='62'>
>    <writer>Neil Gaiman</writer>
>    <penciller pages='1-9,18-24'>Glyn Dillon</penciller>
>    <penciller pages="10-17">Charles Vess</penciller>
>  </comic>
> </collection>
>
> This is my first stab at this:
>
> #!/usr/bin/env python
>
> from lxml import etree
>
> def generateKey(element):
>    if element.attrib:
>        key = (element.tag, element.attrib)
>    else:
> key = element.tag
>    return key

So how are you handling multiple identical tags? It looks from your code
that you will replace the content of the previous tag with the content of 
the
last found tag? I would expect your keys to have some reference to
either the parse depth or a sequuence count. In your sample XML the
problem never arises and maybe in your real data it will never happen
either, but in the general case it is quite common for the same tag
and attribute pair to be used multiple times in a document.


> class parseXML(object):
>    def __init__(self, xmlFile = 'test.xml'):
>        self.xmlFile = xmlFile
>
>    def parse(self):
>        doc = etree.parse(self.xmlFile)
> root = doc.getroot()
> key = generateKey(root)
> dictA = {}
> for r in root.getchildren():
>     keyR = generateKey(r)
>     if r.text:
>         dictA[keyR] = r.text
>     if r.getchildren():
>         dictA[keyR] = r.getchildren()
>
> The script doesn't descend all of the way down because I'm
> not sure how to hand a XML document that may have multiple layers.
> Advice anyone?  Would this be a job for recursion?

Recursion is the classic way to deal with tree structures so
yes you could use there. provided your tree never exceeds
Pythons recursion depth limit (I think its still 1000 levels).

I'm not sure how converting etree's tree structure into a dictionary
will help you however. It seems like a lot of work for a small gain.

hth,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From metolone+gmane at gmail.com  Sat Nov 14 13:03:41 2009
From: metolone+gmane at gmail.com (Mark Tolonen)
Date: Sat, 14 Nov 2009 04:03:41 -0800
Subject: [Tutor] getting python 3 to run from the command line
References: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
Message-ID: <hdm6ao$8dt$1@ger.gmane.org>


"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote in message 
news:dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf at mail.gmail.com...
> Hi,
>
> for some reason I haven't been able to get python 3 to work from the the
> command line. I've added it to the the path in the the system variables 
> but
> still no luck. when I try to run python 2.6 it works flawlessly, though,
> bearing in mind that it wasn't added to the system variables!
>
> i'm running windows vista and have python 30 ,31 and 26 installed.
>
> all of this , by the way, was just to get easy install to work so i can 
> get
> nose working to do some practice of unit testing!
>
> can anyone shed some light on the issue?

How are you starting python?  If you type 'python' and C:\Python31 is the 
first thing in your path, you should see Python 3.1 execute.  If you type 
'somefile.py' the Windows command line uses the registered program for the 
'.py' extension to run the script.  It doesn't use the PATH.  You can see 
the default program for an extension from the command line with the 'assoc' 
and 'ftype' commands:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Python26\python.exe" "%1" %*

-Mark



From kent37 at tds.net  Sat Nov 14 14:03:52 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sat, 14 Nov 2009 08:03:52 -0500
Subject: [Tutor] parsing XML into a python dictionary
In-Reply-To: <548447.35613.qm@web51601.mail.re2.yahoo.com>
References: <548447.35613.qm@web51601.mail.re2.yahoo.com>
Message-ID: <1c2a2c590911140503la8c2048p3324c57183d74741@mail.gmail.com>

On Sat, Nov 14, 2009 at 1:14 AM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
> I've been working on a way to parse an XML document and convert it into a python dictionary. ?I want to maintain the hierarchy of the XML. ?Here is the sample XML I have been working on:
>
> <collection>
> ?<comic title="Sandman" number='62'>
> ? ?<writer>Neil Gaiman</writer>
> ? ?<penciller pages='1-9,18-24'>Glyn Dillon</penciller>
> ? ?<penciller pages="10-17">Charles Vess</penciller>
> ?</comic>
> </collection>

> This is the output:
> 163>./parseXML.py
> {'collection': {('comic', {'number': '62', 'title': 'Sandman'}): [<Element writer at -482193f4>, <Element penciller at -482193cc>, <Element penciller at -482193a4>]}}

This seems an odd format. How are you going to use it? How is this
better than the native ElementTree structure?

> The script doesn't descend all of the way down because I'm not sure how to hand a XML document that may have multiple layers. ?Advice anyone? ?Would this be a job for recursion?

Yes. Here is an example that might be helpful:
http://code.activestate.com/recipes/410469/

Kent

From kent37 at tds.net  Sat Nov 14 14:07:17 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sat, 14 Nov 2009 08:07:17 -0500
Subject: [Tutor] getting python 3 to run from the command line
In-Reply-To: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
References: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
Message-ID: <1c2a2c590911140507g4c33a959sc5db6efe24a31697@mail.gmail.com>

On Sat, Nov 14, 2009 at 3:40 AM, Khalid Al-Ghamdi <emailkgnow at gmail.com> wrote:
> Hi,
> for some reason I haven't been able to get python 3 to work from the the
> command line. I've added it to the the path in the the system variables but
> still no luck. when I try to run python 2.6 it works flawlessly, though,
> bearing in mind that it wasn't added to the system variables!
> i'm running windows vista and have python 30 ,31 and 26 installed.
> all of this , by the way, was just to get easy install to work so i can get
> nose working to do some practice of unit testing!

I don't think setuptools supports Python 3. There is a fork of
setuptools called distribute that does support Python 3.

Kent

From sanelson at gmail.com  Sat Nov 14 14:34:09 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sat, 14 Nov 2009 13:34:09 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com>
Message-ID: <b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com>

Hi,

>> for log in logs:
>> ?l = log.getline()
>> ?print l
>>
>> This gives me three loglines. ?How do I get more? ?Other than while True:
>>
> I presume that what you want is to get all lines from each log.

Well... what I want to do is create a single, sorted list by merging a
number of other sorted lists.

> for log in logs:
> ??? for line in log.getlines():
> ??????? print l

This gives me three lines.

S.

From davea at ieee.org  Sat Nov 14 14:40:49 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 14 Nov 2009 08:40:49 -0500
Subject: [Tutor] getting python 3 to run from the command line
In-Reply-To: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
References: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
Message-ID: <4AFEB361.4010205@ieee.org>

Khalid Al-Ghamdi wrote:
> Hi,
>
> for some reason I haven't been able to get python 3 to work from the the
> command line. I've added it to the the path in the the system variables but
> still no luck. when I try to run python 2.6 it works flawlessly, though,
> bearing in mind that it wasn't added to the system variables!
>
> i'm running windows vista and have python 30 ,31 and 26 installed.
>
> all of this , by the way, was just to get easy install to work so i can get
> nose working to do some practice of unit testing!
>
> can anyone shed some light on the issue?
>
> thanks
>
>   
We need more details to really diagnose the problem, although you've 
given enough for us to guess.  If you really want to help us help you, 
explain what you mean by "haven't been able to get..."    Just what do 
you try, and what happens when you try it?  What does your PATH look 
like, and where is the python installation you're trying to run.

I'll make some guesses anyway.  You've got Vista, and you're running 
from a command prompt.  When you try the following:
   c:\programming\sources\project\ >  python myscript.py

the system runs Python 2.6 on your script (which is in the project 
directory).  Now you want it to run Python 3.1 instead.  I have no clue 
what you've tried.

There are four variables I know of which help determine whether you can 
run a python script in various ways, and using various installations of 
python.  Two of these are in the registry, and two are in the 
environment (environment variables).  Keywords are  assoc, ftype, 
PATHEXT, and PATH

I'll start with the environment variable PATH.  This determines how the 
system interprets word "python" on the command above.  If you have 
multiple python.exe files, you can choose between them in two different 
ways:  you can change the PATH, or you can change what you type.  My 
preference is as follows:

Make a batch directory  (Mine is called  m:\t\bat ) and use it to put 
all your batch files for various purposes.  Add that directory to your 
PATH to point there (by using Control panel, so it will still work on 
reboots).  Close any existing command lines (DOS boxes), because they 
won't have the new PATH value.  Start a new one, and check PATH to make 
sure it's there, and spelled right.

For each installation of python.exe, create a pythonNN.bat which knows 
how to run that particular version.  So I might have a python26.bat 
which has the following two lines in it:

    Echo  Running Python version 2.6
    c:\python26\python.exe %*

When everything's working, you can take out the Echo line, and add a '@' 
symbol in front of the other line.


The other three things I mentioned only affect you if you're trying to 
run a python script "directly", without typing "python" in front of its 
name.


One other thing:  It's useful to have a simple script called  
version.py, which looks something like:

   import sys
   print (sys.version)

(but don't indent the lines, naturally.  For that matter, don't indent 
them in the bat files above either.)

HTH,
DaveA


From alan.gauld at btinternet.com  Sat Nov 14 14:50:56 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 14 Nov 2009 13:50:56 -0000
Subject: [Tutor] getting python 3 to run from the command line
References: <dfac564f0911140040y27e0bee5ub02aa2de2b02fdbf@mail.gmail.com>
Message-ID: <hdmckc$oov$1@ger.gmane.org>


"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote

> for some reason I haven't been able to get python 3 to work from the the
> command line. I've added it to the the path in the the system variables 
> but
> still no luck. when I try to run python 2.6 it works flawlessly, though,
> bearing in mind that it wasn't added to the system variables!

Can you print the output from PATH?
That should tell you what is included in your path, so your Python30
or python31 folder should show up.

> i'm running windows vista and have python 30 ,31 and 26 installed.

How are you calling Python from the command line?

I assume you start a CMD window and then just type python?
If so it will execute whichever python it finds first.
You might want to create aliases for python30, python31
and python26 that execute the right python.exe
In fact if you dreate batch files you an also set the PYTHONPATH
to the right folders too if there are different folders for pytghon 3
and python 2 files - which is usually necessary!

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/ 



From waynejwerner at gmail.com  Sat Nov 14 15:18:34 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Sat, 14 Nov 2009 08:18:34 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com> 
	<40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com> 
	<b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com>
Message-ID: <333efb450911140618u266749c6p6ee462dade58fc5e@mail.gmail.com>

On Sat, Nov 14, 2009 at 7:34 AM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:

>
> Well... what I want to do is create a single, sorted list by merging a
> number of other sorted lists.
>
>
Just write your own merge:
(simplified and probably inefficient and first thing off the top of my head)

newlist = []
for x, y, z in zip(list1, list2, list3):
    if y > x < z:
        newlist.append(x)
    elif x > y < z:
        newlist.append(y)
    elif x > z < y:
        newlist.append(z)

I'm pretty sure that should work although it's untested.

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091114/3b7f8a3c/attachment.htm>

From sanelson at gmail.com  Sat Nov 14 15:40:32 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sat, 14 Nov 2009 14:40:32 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911140543l11c2413ag93161f6fb878fb42@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<20091113204830.2187bedb@o>
	<b6131fdc0911140543l11c2413ag93161f6fb878fb42@mail.gmail.com>
Message-ID: <b6131fdc0911140640k7fa93274lc5d963d0361d75ad@mail.gmail.com>

Gah! Failed to reply to all again!

On Sat, Nov 14, 2009 at 1:43 PM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> Hi,
>> I'm not 100% sure to understand your needs and intention; just have a try. Maybe what you want actually is rather:
>>
>> for log in logs:
>> ?for line in log:
>> ? ?print l
>
> Assuming you meant print line. ?This also gives me just three lines.
>
>> Meaning your log objects need be iterable. To do this, you must have an __iter__ method that would surely simply return the object's getline (or maybe replace it alltogether).
>
> I'm not sure I fully understand how that works, but yes, I created an
> __iter__ method:
>
> ? def __iter__(self):
> ? ? ?self.line=self.logfile.readline()
> ? ? ?stamp=self.timestamp(self.line)
> ? ? ?heapq.heappush(self.heap, (stamp, self.line))
> ? ? ?pop = heapq.heappop(self.heap)
> ? ? ?yield pop
>
> But I still don't see how I can iterate over it... I must be missing something.
>
> I thought that perhaps I could make a generator function:
>
> singly = ((x.stamp, x.line) for x in logs)
> for l in singly:
> ?print
>
> But this doesn't seem to help either.
>
>
>> Then when walking the log with for...in, python will silently call getline until error. This means getline must raise StopIteration when the log is "empty" and __iter__ must "reset" it.
>
> Yes, but for how long? ?Having added the __iter__ method, if I now do:
>
> for log in logs:
> ? for line in log:
> ? ? ?print line
>
> I still get only three results.
>
>> Another solution may be to subtype "file", for a file is precisely an iterator over lines; and you really get your data from a file.
>
> I'm very sorry - I'm not sure I understand. ?I get that a file is
> iterable by definition, but I'm not sure how subtyping it helps.
>
>> Simply (sic), there must some job done about this issue of time stamps (haven't studied in details). Still, i guess this track may be worth an little study.
>
> Sorry for not understanding :(
>
>> Once you get logs iterable, you may subtype list for your overall log collection and set it an __iter__ method like:
>>
>> ? ?for log in self:
>> ? ? ? ?for line in log:
>> ? ? ? ? ? ?yield line
>>
>> (The trick is not from me.)
>
> OK - I make the logs iterable by giving them an __iter__ method - I
> get that. ?I just don't know what you mean by 'subtype list'.
>
>> Then you can write:
>> ? ?for line in my_log_collection
>
> That sounds useful....
>
> S.
>



-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From sanelson at gmail.com  Sat Nov 14 15:49:58 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sat, 14 Nov 2009 14:49:58 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <333efb450911140618u266749c6p6ee462dade58fc5e@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com>
	<b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com>
	<333efb450911140618u266749c6p6ee462dade58fc5e@mail.gmail.com>
Message-ID: <b6131fdc0911140649i61f38725ma9d22385d013baa0@mail.gmail.com>

Hi Wayne,

> Just write your own merge:
> (simplified and probably inefficient and first thing off the top of my head)
> newlist = []
> for x, y, z in zip(list1, list2, list3):

I think I need something like izip_longest don't I, since the list wil
be of varied length?

Also, where do these lists come from?  They can't go in memory -
they're much too big.  This is why I felt using some kind if generator
was the right way - I can produce 3 (or 12) sets of tuples... i just
need to work out how to merge them.

> ?? ?if y > x < z:
> ?? ? ? ?newlist.append(x)
> ?? ?elif x > y < z:
> ?? ? ? ?newlist.append(y)
> ?? ?elif x > z < y:
> ?? ? ? ?newlist.append(z)
> I'm pretty sure that should work although it's untested.

Well, no it won't work.  The lists are in time order, but they won't
match up.  One log may have entries at the same numerical position (ie
the 10th log entry) but earlier than the entries on the previous
lines.  To give a simple example:

List 1        List 2        List 3
(1, cat)      (2, fish)     (1, cabbage)
(4, dog)     (5, pig)      (2, ferret)
(5, phone)  (6, horse)  (3, sausage)

Won't this result in the lowest number *per row* being added to the
new list?  Or am I misunderstanding how it works?

S.

From waynejwerner at gmail.com  Sat Nov 14 16:19:25 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Sat, 14 Nov 2009 09:19:25 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911140649i61f38725ma9d22385d013baa0@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com> 
	<40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com> 
	<b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com> 
	<333efb450911140618u266749c6p6ee462dade58fc5e@mail.gmail.com> 
	<b6131fdc0911140649i61f38725ma9d22385d013baa0@mail.gmail.com>
Message-ID: <333efb450911140719ua82bbe5p35ee33361adf9f60@mail.gmail.com>

On Sat, Nov 14, 2009 at 8:49 AM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:

> Hi Wayne,
>
> > Just write your own merge:
> > (simplified and probably inefficient and first thing off the top of my
> head)
> > newlist = []
> > for x, y, z in zip(list1, list2, list3):
>
> I think I need something like izip_longest don't I, since the list wil
> be of varied length?
>

Yes, probably


>
> Also, where do these lists come from?  They can't go in memory -
> they're much too big.  This is why I felt using some kind if generator
> was the right way - I can produce 3 (or 12) sets of tuples... i just
> need to work out how to merge them.
>

you can zip generators, too:
In [10]: for x, y in zip(xrange(10), xrange(5)):
   ....:     print x, y
   ....:
   ....:
0 0
1 1
2 2
3 3
4 4

In [38]: for x,y in itertools.izip_longest(xrange(10), xrange(5)):
   ....:     print x,y
   ....:
   ....:
0 0
1 1
2 2
3 3
4 4
5 None
6 None
7 None
8 None
9 None

>     if y > x < z:
> >         newlist.append(x)
> >     elif x > y < z:
> >         newlist.append(y)
> >     elif x > z < y:
> >         newlist.append(z)
> > I'm pretty sure that should work although it's untested.
>
> Well, no it won't work.  The lists are in time order, but they won't
> match up.  One log may have entries at the same numerical position (ie
> the 10th log entry) but earlier than the entries on the previous
> lines.  To give a simple example:
>
> List 1        List 2        List 3
> (1, cat)      (2, fish)     (1, cabbage)
> (4, dog)     (5, pig)      (2, ferret)
> (5, phone)  (6, horse)  (3, sausage)
>
> Won't this result in the lowest number *per row* being added to the
> new list?  Or am I misunderstanding how it works?


I forgot to add the rest of them. But it appears that you'll have to come up
with some better logic.

I would use some type of priority queue-style implementation. I don't know
if the heapq datatype would be sufficient, or if you'd need to roll your
own. The way I would do it is check the top value of each, whichever has the
smallest value, pop it off and add that to the new list (which could also be
a generator[there's some other term for it but I forget ATM] writing out to
a file) then check across the top of all 3 again.

Make sense?
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091114/fd7ab812/attachment.htm>

From emailkgnow at gmail.com  Sat Nov 14 17:54:49 2009
From: emailkgnow at gmail.com (Khalid Al-Ghamdi)
Date: Sat, 14 Nov 2009 19:54:49 +0300
Subject: [Tutor] getting python 3 to run from the command line (version 2)
Message-ID: <dfac564f0911140854v42fa4e0ehe5868517a50ef8bb@mail.gmail.com>

hi all,

I realize my question was previous question was phrased, well, vaguely, as I
learn from your responses so here's version 2 of my question:

i'm running windows vista and have python 30 ,31 and 26 installed.!

when I try to run python form the command line by printing *python *to
whichever path i have there (in my case:C:\users\KE>) it runs python 30, but
when i try to code something it just gives me a new line without any
processing of the code. (for example:2+2 returns a new line)

when I tried to change the path to the directory that contains python 31 and
enter *python (c:\python31>)*,  it just crashes and gives me the following
message:


*fatal python error: Py_Initialize: can't initialize sys standard streams
> *

*LookupError: unknown encoding: cp720*
*
*
*This application has requested the Runtime to terminate it in an unusual
way. *
*Please contact teh application's support team for more Information.*
*
*
When i change the directory to c:\python26 and then enter it works ok.

so can anyone tell me why this is happening?

PS: I learn from Alan Gauld's response that windows defaults to python30
when i enter python because in the system variables python30 was entered
first and I confirmed that that was true, but still python 30 doesn't
process my code and python 31 crashes.

thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091114/3584f25f/attachment-0001.htm>

From modulok at gmail.com  Sat Nov 14 18:43:47 2009
From: modulok at gmail.com (Modulok)
Date: Sat, 14 Nov 2009 10:43:47 -0700
Subject: [Tutor] OT: Writing code while tired, counterproductive?
Message-ID: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>

List,

This is kind off topic, but:

Does anyone else find, writing code while tired to be counterproductive?

It just seems like when I push myself to stay up late finishing a
project, I sorely regret it the following day. Granted, Python is a
fairly forgiving language and doesn't take the mental attention that
some others require (like C++ ...or someone else's C++ ....or someone
else's poorly written C++), but for me, writing code while tired
usually ends up being a bad thing - In any language. But then there's
the guilt of taking the day off and not meeting deadlines. *grumble*
Just wondering if this is a common occurrence among the masses.

Anyone?
-Modulok-

From cspears2002 at yahoo.com  Sat Nov 14 19:47:17 2009
From: cspears2002 at yahoo.com (Christopher Spears)
Date: Sat, 14 Nov 2009 10:47:17 -0800 (PST)
Subject: [Tutor] parsing XML into a python dictionary
Message-ID: <772235.39513.qm@web51612.mail.re2.yahoo.com>

Thanks!? I have a lot of XML files at work that users search through.? I want to parse the XML into a python dictionary and then read the dictionary into a database that users can use to search through the thousands of files.

Basically, the user would submit a query like "Neil Gaiman" and then the program would return the name of the files in which the words "Neil Gaiman" appears.  

I thought I might be able to use the tags to speed up the search.  For example, maybe the program will only look at the "writer" tags, or I can ask the program to show me everything under the "comic" tag.


--- On Sat, 11/14/09, Kent Johnson <kent37 at tds.net> wrote:

> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] parsing XML into a python dictionary
> To: "Christopher Spears" <cspears2002 at yahoo.com>
> Cc: tutor at python.org
> Date: Saturday, November 14, 2009, 5:03 AM
> On Sat, Nov 14, 2009 at 1:14 AM,
> Christopher Spears
> <cspears2002 at yahoo.com>
> wrote:
> > I've been working on a way to parse an XML document
> and convert it into a python dictionary. ?I want to
> maintain the hierarchy of the XML. ?Here is the sample XML
> I have been working on:
> >
> > <collection>
> > ?<comic title="Sandman" number='62'>
> > ? ?<writer>Neil Gaiman</writer>
> > ? ?<penciller pages='1-9,18-24'>Glyn
> Dillon</penciller>
> > ? ?<penciller pages="10-17">Charles
> Vess</penciller>
> > ?</comic>
> > </collection>
> 
> > This is the output:
> > 163>./parseXML.py
> > {'collection': {('comic', {'number': '62', 'title':
> 'Sandman'}): [<Element writer at -482193f4>,
> <Element penciller at -482193cc>, <Element
> penciller at -482193a4>]}}
> 
> This seems an odd format. How are you going to use it? How
> is this
> better than the native ElementTree structure?
> 
> > The script doesn't descend all of the way down because
> I'm not sure how to hand a XML document that may have
> multiple layers. ?Advice anyone? ?Would this be a job for
> recursion?
> 
> Yes. Here is an example that might be helpful:
> http://code.activestate.com/recipes/410469/
> 
> Kent
>


From alan.gauld at btinternet.com  Sat Nov 14 19:48:51 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 14 Nov 2009 18:48:51 -0000
Subject: [Tutor] Iterable Understanding
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com><40af687b0911131124s3bc688b4t1f859ce0f0079175@mail.gmail.com><b6131fdc0911140534s71275c53u97885db3ca0c3416@mail.gmail.com><333efb450911140618u266749c6p6ee462dade58fc5e@mail.gmail.com>
	<b6131fdc0911140649i61f38725ma9d22385d013baa0@mail.gmail.com>
Message-ID: <hdmu2v$be7$1@ger.gmane.org>


"Stephen Nelson-Smith" <sanelson at gmail.com> wrote

> List 1        List 2        List 3
> (1, cat)      (2, fish)     (1, cabbage)
> (4, dog)     (5, pig)      (2, ferret)
> (5, phone)  (6, horse)  (3, sausage)
>
> Won't this result in the lowest number *per row* being added to the
> new list?  Or am I misunderstanding how it works?

You are right.

I think the algorithm you need goes something like

- read the first item from each flie
- find the lowest item add it to the result
- keep taking items from that file until you find one greater than
- the next lowest item that you first found, put that  cvalue in place
  of the original lowest value
- repeat for the new lowest value until it matches the next lowest value
  and so on until all files are empty

To taker the trivial case of 2 files containing the odd and every other 
even
numbers respectively

A = [1, 3,  5,  7,...
B = [2, 6, 10...

lowA = 1
low2 = 2
load lowA into result, get next  from A -> 3
lowA becomes 3
load next lowest (lowB=2) into result, get next from B -> 6
lowB becomes 6
load next lowest (lowA=3) into result, get next from A -> 5
load 5 in result and get next from A -> 7
load 7 in lowA
load next lowest (lowB=6) into result, get next from B -> 10
load next lowest(lowA=7) into result, get next from A -> 9
etc until all files are empty


Obviously you can create a dictionary or list of your lowest values
and the matching file. You can simplify the algorithm by always
loading the latest value into lowX and alwayss checking for the
lowest value before storing it, but for high volumes the extra checks
will mount up.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Sat Nov 14 19:56:04 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 14 Nov 2009 18:56:04 -0000
Subject: [Tutor] getting python 3 to run from the command line (version
	2)
References: <dfac564f0911140854v42fa4e0ehe5868517a50ef8bb@mail.gmail.com>
Message-ID: <hdmugg$cia$1@ger.gmane.org>


"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote

> when i try to code something it just gives me a new line without any
> processing of the code. (for example:2+2 returns a new line)

You mean you get to the >>> prompt?
And you type 2+2 you get this:

>>> 2+2

>>>

With just a newline between your input and the next >>> prompt?

> when I tried to change the path to the directory that contains python 31 
> and
> enter *python (c:\python31>)*,

I'm not surprised it doesn't work, that should have python trying to start
executing a folder. But....

> *fatal python error: Py_Initialize: can't initialize sys standard streams

I get a very different message:

C:\Documents and Settings\Alan Gauld>python (C:\Python31)
python: can't open file '(C:\Python31)': [Errno 22] Invalid argument

> When i change the directory to c:\python26 and then enter it works ok.
> so can anyone tell me why this is happening?

Nope, sorry, I don't understand how the 2.6 version works if
you are passing in a folder as you did for 3.1

Do you know how to cut n paste from a cmd window>
It would probably help if you pasted in the actuall sessions
into your mail.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Sat Nov 14 19:57:14 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 14 Nov 2009 18:57:14 -0000
Subject: [Tutor] Writing code while tired, counterproductive?
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
Message-ID: <hdmuil$cof$1@ger.gmane.org>


"Modulok" <modulok at gmail.com> wrote

> Does anyone else find, writing code while tired to be counterproductive?

Yes. Doing anything that is mentally taxing is usually a bad 
idea when tired!

Alan G


From metolone+gmane at gmail.com  Sat Nov 14 20:25:53 2009
From: metolone+gmane at gmail.com (Mark Tolonen)
Date: Sat, 14 Nov 2009 11:25:53 -0800
Subject: [Tutor] getting python 3 to run from the command line (version
	2)
References: <dfac564f0911140854v42fa4e0ehe5868517a50ef8bb@mail.gmail.com>
Message-ID: <hdn07r$h7u$1@ger.gmane.org>


"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote in message 
news:dfac564f0911140854v42fa4e0ehe5868517a50ef8bb at mail.gmail.com...
> hi all,
>
> I realize my question was previous question was phrased, well, vaguely, as 
> I
> learn from your responses so here's version 2 of my question:
>
> i'm running windows vista and have python 30 ,31 and 26 installed.!
>
> when I try to run python form the command line by printing *python *to
> whichever path i have there (in my case:C:\users\KE>) it runs python 30, 
> but
> when i try to code something it just gives me a new line without any
> processing of the code. (for example:2+2 returns a new line)
>
> when I tried to change the path to the directory that contains python 31 
> and
> enter *python (c:\python31>)*,  it just crashes and gives me the following
> message:
>
>
> *fatal python error: Py_Initialize: can't initialize sys standard streams
>> *
>
> *LookupError: unknown encoding: cp720*
> *
> *
> *This application has requested the Runtime to terminate it in an unusual
> way. *
> *Please contact teh application's support team for more Information.*
> *
> *
> When i change the directory to c:\python26 and then enter it works ok.
>
> so can anyone tell me why this is happening?
>
> PS: I learn from Alan Gauld's response that windows defaults to python30
> when i enter python because in the system variables python30 was entered
> first and I confirmed that that was true, but still python 30 doesn't
> process my code and python 31 crashes.

My version of Python and Windows doesn't understand cp720.  According to 
http://en.wikipedia.org/wiki/Code_page_720, it is a OEM Arabic code page. 
The Windows-equivalent is 'cp1256'.  I have Python 2.6 and it doesn't have a 
codec for cp720, but does for cp1256.

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> u'abc'.encode('cp720')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: cp720
>>> u'abc'.encode('cp1256')
'abc'

If you are running on an Arabic version of Windows, you might change the 
console code page to 1256 and see if that works.  Run 'chcp 1256' before 
running 'python'.

-Mark



From kent37 at tds.net  Sat Nov 14 20:31:23 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sat, 14 Nov 2009 14:31:23 -0500
Subject: [Tutor] OT: Writing code while tired, counterproductive?
In-Reply-To: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
Message-ID: <1c2a2c590911141131v41d7cad7vf57949b1b945cf47@mail.gmail.com>

On Sat, Nov 14, 2009 at 12:43 PM, Modulok <modulok at gmail.com> wrote:

> Does anyone else find, writing code while tired to be counterproductive?

I definitely find that there is a point of diminishing returns, where
my productivity and the quality of code decline to the point where it
is no longer worth it to continue.

Kent

From ljmamoreira at gmail.com  Sat Nov 14 21:07:57 2009
From: ljmamoreira at gmail.com (Jose Amoreira)
Date: Sat, 14 Nov 2009 20:07:57 +0000
Subject: [Tutor] OT: Writing code while tired, counterproductive?
In-Reply-To: <1c2a2c590911141131v41d7cad7vf57949b1b945cf47@mail.gmail.com>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
	<1c2a2c590911141131v41d7cad7vf57949b1b945cf47@mail.gmail.com>
Message-ID: <200911142007.57981.ljmamoreira@gmail.com>

When I get really tired, I sort of slip into an half awaken state. I still 
have my eyes open, I still look at the code, I still think about the problem, 
but suddenly the lines come alive and start arguing with each other (and some 
times arguing with me!), or setting up dates, or singing in choir, all sorts 
of crazy things. That's definitely the time to STOP!, and get some real sleep.

Cheers,
Jose Amoreira

From sanelson at gmail.com  Sat Nov 14 21:49:52 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sat, 14 Nov 2009 20:49:52 +0000
Subject: [Tutor] Should a beginner learn Python 3.x
Message-ID: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>

My brother in law is learning python.  He's downloaded 3.1 for
Windows, and is having a play.  It's already confused him that print
"hello world" gives a syntax error....

He's an absolute beginner with no programming experience at all.  I
think he might be following 'Python Programming for the Absolute
Beginner", or perhaps some online guides.  Should I advise him to
stick with 2.6 for a bit, since most of the material out  there will
be for 2.x?  Or since he's learning from scratch, should he jump
straight to 3.x  In which case what can you recommend for him to work
through - I must stress he has absolutely no clue at all about
programming, no education beyond 16 yrs old, but is keen to learn.

S.

-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From alan.plum at uni-koeln.de  Sat Nov 14 22:10:23 2009
From: alan.plum at uni-koeln.de (Alan Plum)
Date: Sat, 14 Nov 2009 22:10:23 +0100
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
Message-ID: <1258233023.4517.8.camel@kallisti>

Ahoy!

On Sa, 2009-11-14 at 20:49 +0000, Stephen Nelson-Smith wrote:
> He's an absolute beginner with no programming experience at all.  I
> think he might be following 'Python Programming for the Absolute
> Beginner", or perhaps some online guides.  Should I advise him to
> stick with 2.6 for a bit, since most of the material out  there will
> be for 2.x?  Or since he's learning from scratch, should he jump
> straight to 3.x  In which case what can you recommend for him to work
> through - I must stress he has absolutely no clue at all about
> programming, no education beyond 16 yrs old, but is keen to learn.

It's too early for Python 3.x.

He should probably learn Python 2.6 for now, possibly with future
imports (though they probably take a bit longer to explain).

3.x will probably cause too many problems for him for now. A lot of the
reference material he can find on the web expects 2.x and many of the
"cool" libraries aren't quite ported to 3.x yet. That's just confusing.

He'll learn about the differences between 2.x and 3.x eventually, but he
shouldn't have to worry about it for the time being.


Cheers

Alan


From alan.gauld at btinternet.com  Sun Nov 15 01:16:57 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 15 Nov 2009 00:16:57 -0000
Subject: [Tutor] Should a beginner learn Python 3.x
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
Message-ID: <hdnha5$sgr$1@ger.gmane.org>

"Stephen Nelson-Smith" <sanelson at gmail.com> wrote

>  Beginner", or perhaps some online guides.  Should I advise him to
> stick with 2.6 for a bit, since most of the material out  there will
> be for 2.x?  Or since he's learning from scratch, should he jump
> straight to 3.x

Version 3 support is getting there but I still don't think it's fully 
suitable
for beginners yet. I'd say stick with version 2.6 (or 2.7!) [Actually v3 is
fine for beginners the problem is as soon as they try to do anything
beyond beginner they will likely run into support issues...]

That having been said my tutorial for V3 is making slow but steady
progress and the basics section is complete and I'm slowly ticking
off the advanced topics so he could try that. (see this sig's url)

But personally I'd still go with v2

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/ 



From mjekl at iol.pt  Sun Nov 15 01:49:27 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Sun, 15 Nov 2009 00:49:27 +0000
Subject: [Tutor] opening a file directly from memory
Message-ID: <20091115004927.ukv176t344kks0os@webmail.iol.pt>

I'm wondering if I must save a file to memory before opening it. By  
opening I mean displaying it to the user.

I have a BLOB field in a db and I have managed to read the blob into a  
binary fileobject. I've also managed to write it to disk and then I  
open it by doubleclicking on it. But I was wondering:

1. How to open the file directly from code (and not by double clicking):
I'm aware of os.startfile on Win by I'm on a Mac now, so I rather have  
a cross-platform way of accomplishing this.

2. If there is any Python module that takes care of saving and  
cleaning temp files in an OS transparent way?

Txs,
Miguel

________________________________________________________________________________
COFIDIS Maxicredito. Ate' ?10.000 sem burocracias. Resposta on-line!
Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273

From anand.shashwat at gmail.com  Sun Nov 15 02:35:38 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sun, 15 Nov 2009 07:05:38 +0530
Subject: [Tutor] Find Integer co-ordinates lying on a circle
Message-ID: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>

How to find all possible integer co-ordinates lying on a circle of given
radius 'r'.
If given the upper bound of 'r', I want to calculate all given co-ordinates
lying for 0 <= r <= n

Let's say the upper bound of radius is 5
All possible results are:
radius 'r' - (x, y)
0 - (0, 0)
1 - (1, 0), (0, 1), (-1, 0), (0, -1)
2 - (2, 0), (0, 2), (-2, 0), (0, -2)
3 - (3, 0), (0, 3), (-3, 0), (0, -3)
4 - (4, 0), (0, 4), (-4, 0), (0, -4)
5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
4), (-4, 3), (-3, -4), (-4, -3)

Also for a particular radius, lots of possible combination of (x, y) is
possible so best datastructure could be defaultdict for further operations
IMHO.
So my desired output is:
defaultdict(<type 'list'>, {0 : [(0, 0)], 1: [(1, 0), (0, 1), (-1, 0), (0,
-1)], 2: [(2, 0), (0, 2), (-2, 0), (0, -2)], 3: [(3, 0), (0, 3), (-3, 0),
(0, -3)], 4: [(4, 0), (0, 4), (-4, 0), (0, -4)], 5: [(5, 0), (0, 5), (-5,
0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4),
(-4, -3)]})

My approach using pythagorean triplets:
>>> d = collections.defaultdict(list)
>>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v
in range(u+1, 10)]))
>>> [d[k].append(v) for k,v in s]
However it sure is wrong and fails in my case.

Any suggestions as to how can I reach my desired goal, or are there any
other options to tackle this problem?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/4cecac70/attachment-0001.htm>

From davea at ieee.org  Sun Nov 15 03:39:59 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 14 Nov 2009 21:39:59 -0500
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <20091115004927.ukv176t344kks0os@webmail.iol.pt>
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt>
Message-ID: <4AFF69FF.1040008@ieee.org>



mjekl at iol.pt wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm 
> wondering if I must save a file to memory before opening it. By 
> opening I mean displaying it to the user.
>
> I have a BLOB field in a db and I have managed to read the blob into a 
> binary fileobject. I've also managed to write it to disk and then I 
> open it by doubleclicking on it. But I was wondering:
>
> 1. How to open the file directly from code (and not by double clicking):
> I'm aware of os.startfile on Win by I'm on a Mac now, so I rather have 
> a cross-platform way of accomplishing this.
>
> 2. If there is any Python module that takes care of saving and 
> cleaning temp files in an OS transparent way?
>
> Txs,
> Miguel
>

You don't say what this binary data is.  Is there a specific program 
that should be launched to "display it to the user" ?  Or do you have to 
keep this general?

If you know what the necessary program is, you could use subprocess 
module to launch it.  But I don't know enough about the Mac to know how 
to do the Mac equivalent of  os.startfile

As for avoiding the use of a file, that depends entirely on the program 
you're launching.  Some programs can be told to get their data from 
stdin.  If that's the case, there's a way to provide stdin directly from 
Python, using subprocess.

As for temporary files, consider tempfile module. I haven't used it, but 
it looks promising. 

HTH,
DaveA

From davea at ieee.org  Sun Nov 15 03:59:40 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 14 Nov 2009 21:59:40 -0500
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
Message-ID: <4AFF6E9C.7050302@ieee.org>



Shashwat Anand wrote:
> How to find all possible integer co-ordinates lying on a circle of given
> radius 'r'.
> If given the upper bound of 'r', I want to calculate all given co-ordinates
> lying for 0 <= r <= n
>
> Let's say the upper bound of radius is 5
> All possible results are:
> radius 'r' - (x, y)
> 0 - (0, 0)
> 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
> 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
> 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
> 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
> 4), (-4, 3), (-3, -4), (-4, -3)
>
> Also for a particular radius, lots of possible combination of (x, y) is
> possible so best datastructure could be defaultdict for further operations
> IMHO.
> So my desired output is:
> defaultdict(<type 'list'>, {0 : [(0, 0)], 1: [(1, 0), (0, 1), (-1, 0), (0,
> -1)], 2: [(2, 0), (0, 2), (-2, 0), (0, -2)], 3: [(3, 0), (0, 3), (-3, 0),
> (0, -3)], 4: [(4, 0), (0, 4), (-4, 0), (0, -4)], 5: [(5, 0), (0, 5), (-5,
> 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4),
> (-4, -3)]})
>
> My approach using pythagorean triplets:
>   
>>>> d = collections.defaultdict(list)
>>>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v
>>>>         
> in range(u+1, 10)]))
>   
>>>> [d[k].append(v) for k,v in s]
>>>>         
> However it sure is wrong and fails in my case.
>
> Any suggestions as to how can I reach my desired goal, or are there any
> other options to tackle this problem?
>
>   
Your "all possible results" isn't even close to all the points that 
match the 0<= r <= 5.  And I don't know how either spec justifies the 
set logic you quoted.

So I have to start over.  I think you're trying to find all integer 
co-ordinates which lie on or within a circle of given radius (sample 5).

Taking only the first quadrant to keep the number of values smaller, I see:

( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 )
( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 )
( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 )
( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 )
( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 )
( 5 , 0 )

To find them, just write a doubly nested loop, each with range from 
-radius to radius, checking x*x + y*y <= radius*radius.  Add to the 
result set, each value that meets the comparison.


There are some optimizations you can do, but first get it working in the 
form you want.


DaveA

From anand.shashwat at gmail.com  Sun Nov 15 04:41:16 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sun, 15 Nov 2009 09:11:16 +0530
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <4AFF6E9C.7050302@ieee.org>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
	<4AFF6E9C.7050302@ieee.org>
Message-ID: <d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>

>
> Your "all possible results" isn't even close to all the points that match
> the 0<= r <= 5.  And I don't know how either spec justifies the set logic
> you quoted.
>

> So I have to start over.  I think you're trying to find all integer
> co-ordinates which lie on or within a circle of given radius (sample 5).
>
> No, I'm trying to find all integer co-ordinates which lies on a circle.
Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0),
(0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 = 5**2 (as
Origin is the centre of the circle.)
Now I want a table of these points for say r = 0 to upper bound.
So for r = 0, the only point lying on it is (0,0)
For r = 1, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1,
0), (0, 1), (-1, 0), (0, -1)]
And so forth.

Thus the table shows the points (x,y) which are lying on the circle of
radius 'r'.

radius 'r' - (x, y)
0 - (0, 0)
1 - (1, 0), (0, 1), (-1, 0), (0, -1)
2 - (2, 0), (0, 2), (-2, 0), (0, -2)
3 - (3, 0), (0, 3), (-3, 0), (0, -3)
4 - (4, 0), (0, 4), (-4, 0), (0, -4)
5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
4), (-4, 3), (-3, -4), (-4, -3)

Which is correct. I'm not trying to find all integer co-ordinates within a
circle but trying to create a table of points lying on the circle of radius
'r', varying the radius from '0' to upper bound 'r'.
The bruteforce can be done as:

#R = upper bound
for r in range(0, R+1):
    for x in range(0, R+1):
        for y in range(0, R+1):
            if x**2 + y**2 == r**2:
                #store them

However the complexity reaches O(n**3) and it's not optimized at all. So I
though of using pythagorean triplets.

Taking only the first quadrant to keep the number of values smaller, I see:
>
> ( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 )
> ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 )
> ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 )
> ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 )
> ( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 )
> ( 5 , 0 )
>
> To find them, just write a doubly nested loop, each with range from -radius
> to radius, checking x*x + y*y <= radius*radius.  Add to the result set, each
> value that meets the comparison.
>
>
> There are some optimizations you can do, but first get it working in the
> form you want.
>
>
> The bruteforce code is working for me but that's an unacceptable option. So
I tried pythagorean triplets. But I'm not sure as to where am I doing
mistake.
>>> d = collections.defaultdict(list)
I created a dictionary.
>>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v
in range(u+1, 10)]))
for u = (0,9) and v > u, the triplets are u**2+v**2, v**2 - u**2, 2*u*v. So,
I stored them. However it looks for only half of positive quadrant. set
logic is to remove  duplicate elements. Now for each value (p, q) there will
be seven more possible values i.e. (q, p), (q, -p), (-q, p), (-q, -p), (p,
-q), (-p, q), (-p, -q). In case of either of p or q value = 0, the values
are reduced to 3.
>>> [d[k].append(v) for k,v in s]
I stored all the values.

Hope I did not made this post really messy. I think I'm unable to apply my
logic. How to implement / Can there be better logic, optimizations and
implementations ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/71f1e1f6/attachment.htm>

From mjekl at iol.pt  Sun Nov 15 05:02:33 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Sun, 15 Nov 2009 04:02:33 +0000
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <4AFF69FF.1040008@ieee.org>
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt>
	<4AFF69FF.1040008@ieee.org>
Message-ID: <20091115040233.ih6k6wpwiok0s4ow@webmail.iol.pt>

davea at ieee.org wrote:
mjekl at iol.pt wrote:
<div class="moz-text-flowed" style="font-family: -moz-fixed">I'm  
wondering if I must
save a file to memory before opening it. By opening I mean displaying  
it to the user.

I have a BLOB field in a db and I have managed to read the blob into a binary
fileobject. I've also managed to write it to disk and then I open it  
by doubleclicking
on it. But I was wondering:

1. How to open the file directly from code (and not by double clicking):
I'm aware of os.startfile on Win by I'm on a Mac now, so I rather have  
a cross-platform
way of accomplishing this.

2. If there is any Python module that takes care of saving and  
cleaning temp files in
an OS transparent way?

Txs,
Miguel
You don't say what this binary data is.  Is there a specific program  
that should be
launched to "display it to the user" ?  Or do you have to keep this general?

If you know what the necessary program is, you could use subprocess  
module to launch it.
But I don't know enough about the Mac to know how to do the Mac equivalent of
os.startfile

As for avoiding the use of a file, that depends entirely on the program you're
launching.  Some programs can be told to get their data from stdin.   
If that's the case,
there's a way to provide stdin directly from Python, using subprocess.

As for temporary files, consider tempfile module. I haven't used it,  
but it looks
promising. HTH,
DaveA
I know what the binary data is from the database. Typically it would  
be some file the OS
knows how to open.
Chiefly I'm trying to avoid making the user save the file on some  
location and then go
and double click it to open it. This makes no sense, since the user  
has already made the
decision to open the file.

Txs Dave,
Miguel

________________________________________________________________________________
COFIDIS Maxicredito. Ate' ?10.000 sem burocracias. Resposta on-line!
Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273

From davea at ieee.org  Sun Nov 15 05:16:54 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 14 Nov 2009 23:16:54 -0500
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>	
	<4AFF6E9C.7050302@ieee.org>
	<d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
Message-ID: <4AFF80B6.7070104@ieee.org>

Shashwat Anand wrote:
>> Your "all possible results" isn't even close to all the points that match
>> the 0<= r <= 5.  And I don't know how either spec justifies the set logic
>> you quoted.
>>
>>     
>
>   
>> So I have to start over.  I think you're trying to find all integer
>> co-ordinates which lie on or within a circle of given radius (sample 5).
>>
>> No, I'm trying to find all integer co-ordinates which lies on a circle.
>>     
> Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0),
> (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
> 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 = 5**2 (as
> Origin is the centre of the circle.)
>   

In other words  r = n,   not 0 <= r <= n  as you originally said.  You 
want all points for which x*x + y*y = n*n

Upon reading your new message, it appears that you are not using r in 
the usual sense of polar coordinates
    (where r = sqrt( x*x + y*y)  and  theta = arctan(x/y)    or maybe 
it's y/x )

Instead you're using r as the radius of one circle you're trying to do, 
in a loop that is trying different radii up to n (or R)
> Now I want a table of these points for say r = 0 to upper bound.
> So for r = 0, the only point lying on it is (0,0)
> For r = 1, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1,
> 0), (0, 1), (-1, 0), (0, -1)]
> And so forth.
>
> Thus the table shows the points (x,y) which are lying on the circle of
> radius 'r'.
>
> radius 'r' - (x, y)
> 0 - (0, 0)
> 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
> 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
> 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
> 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
> 4), (-4, 3), (-3, -4), (-4, -3)
>
> Which is correct. I'm not trying to find all integer co-ordinates within a
> circle but trying to create a table of points lying on the circle of radius
> 'r', varying the radius from '0' to upper bound 'r'.
> The bruteforce can be done as:
>
> #R = upper bound
> for r in range(0, R+1):
>     for x in range(0, R+1):
>         for y in range(0, R+1):
>             if x**2 + y**2 == r**2:
>                 #store them
>
>   
Care to define r and R ?  I think the outer loop here belongs 
elsewhere.  Let's solve it for one circle.  Then you can run that inside 
a loop which iterates through possible radii.

The outer loop isn't our concern.  And the innermost loop can be 
replaced by a calculation. Why not simply
   y = math.sqrt(r*r - x*x)

and check if y is an int. ??
> However the complexity reaches O(n**3) and it's not optimized at all. So I
> though of using pythagorean triplets.
>
> Taking only the first quadrant to keep the number of values smaller, I see:
>   
>> ( 0 , 0 ) ( 0 , 1 ) ( 0 , 2 ) ( 0 , 3 ) ( 0 , 4 ) ( 0 , 5 )
>> ( 1 , 0 ) ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 )
>> ( 2 , 0 ) ( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 )
>> ( 3 , 0 ) ( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 )
>> ( 4 , 0 ) ( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 )
>> ( 5 , 0 )
>>
>> To find them, just write a doubly nested loop, each with range from -radius
>> to radius, checking x*x + y*y <= radius*radius.  Add to the result set, each
>> value that meets the comparison.
>>
>>
>> There are some optimizations you can do, but first get it working in the
>> form you want.
>>
>>
>> The bruteforce code is working for me but that's an unacceptable option. So
>>     
> I tried pythagorean triplets. But I'm not sure as to where am I doing
> mistake.
>   
>>>> d = collections.defaultdict(list)
>>>>         
> I created a dictionary.
>   
>>>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v
>>>>         
> in range(u+1, 10)]))
> for u = (0,9) and v > u, the triplets are u**2+v**2, v**2 - u**2, 2*u*v. So,
> I stored them. However it looks for only half of positive quadrant. set
> logic is to remove  duplicate elements. Now for each value (p, q) there will
> be seven more possible values i.e. (q, p), (q, -p), (-q, p), (-q, -p), (p,
> -q), (-p, q), (-p, -q). In case of either of p or q value = 0, the values
> are reduced to 3.
>   
>>>> [d[k].append(v) for k,v in s]
>>>>         
> I stored all the values.
>
> Hope I did not made this post really messy. I think I'm unable to apply my
> logic. How to implement / Can there be better logic, optimizations and
> implementations ?
>
>   


From davea at ieee.org  Sun Nov 15 05:19:36 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 14 Nov 2009 23:19:36 -0500
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <20091115040053.ik0yf738g0sw88ks@webmail.iol.pt>
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt>	<4AFF69FF.1040008@ieee.org>
	<20091115040053.ik0yf738g0sw88ks@webmail.iol.pt>
Message-ID: <4AFF8158.1020905@ieee.org>

(You forgot to send this message to the list, so I'm forwarding it)

mjekl at iol.pt wrote:
>> davea at ieee.org wrote:
>> mjekl at iol.pt wrote:
>>> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm 
>>> wondering if I must save a file to memory before opening it. By 
>>> opening I mean displaying it to the user.
>>>
>>> I have a BLOB field in a db and I have managed to read the blob into 
>>> a binary fileobject. I've also managed to write it to disk and then 
>>> I open it by doubleclicking on it. But I was wondering:
>>>
>>> 1. How to open the file directly from code (and not by double 
>>> clicking):
>>> I'm aware of os.startfile on Win by I'm on a Mac now, so I rather 
>>> have a cross-platform way of accomplishing this.
>>>
>>> 2. If there is any Python module that takes care of saving and 
>>> cleaning temp files in an OS transparent way?
>>>
>>> Txs,
>>> Miguel
>>>
>>
>> You don't say what this binary data is.  Is there a specific program 
>> that should be launched to "display it to the user" ?  Or do you have 
>> to keep this general?
>>
>> If you know what the necessary program is, you could use subprocess 
>> module to launch it.  But I don't know enough about the Mac to know 
>> how to do the Mac equivalent of  os.startfile
>>
>> As for avoiding the use of a file, that depends entirely on the 
>> program you're launching.  Some programs can be told to get their 
>> data from stdin.  If that's the case, there's a way to provide stdin 
>> directly from Python, using subprocess.
>>
>> As for temporary files, consider tempfile module. I haven't used it, 
>> but it looks promising. HTH,
>> DaveA
>>
>
> I know what the binary data is from the database. Typically it would 
> be some file the OS knows how to open.
> Chiefly I'm trying to avoid making the user save the file on some 
> location and then go and double click it to open it. This makes no 
> sense, since the user has already made the decision to open the file.
> Miguel
>
> Txs Dave.
>
You forgot to answer the question.  You say "The OS knows how to open".  
Does *your* *program* know what program is needed, to open this 
particular binary data?



From wilcoxwork at gmail.com  Sun Nov 15 05:25:14 2009
From: wilcoxwork at gmail.com (Kristin Wilcox)
Date: Sat, 14 Nov 2009 20:25:14 -0800
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
Message-ID: <de4393b40911142025j4348c082tde87fabdfc401ee3@mail.gmail.com>

I started as an absolute beginner to with programming with Python 3. I
remain a beginner but I've now installed 2.6 as well, because I found that
some of the modules I wanted to use weren't available for 3.x.

My personal experience was that the available literature/tutorials wasn't
really a problem. I quickly figured out that I should copy the list on the
python site of all the 2x-3x changes, and I picked up a couple other lists
written with different wording saying the same thing, and kept it as a quick
reference on my computer. But honestly, as a beginner you only run into a
handful of differences. Other than print(), input(), <>, xrange(), the
dictionary has_key, interkeys(), a bit of other dictionary stuff, tkinter,
and renamed http modules, you're not going to run into much that varies
between the versions. I just learned the very basic stuff to watch out for -
compared to all the other new knowledge you're putting in your brain, it's
nothing.

I personally really prefer print() to print - it just made more sense to me
when learning, and I like that you get a set literal like you get with lists
and dictionaries (it felt like the same logic was being applied in similar
situations, which is good when you're just starting out). But these are
small conveniences and I switched to 2.x because of compatibility issues.

I don't think it's a problem to initially learn on 3.1, but I do think it's
inevitable that he will have to learn both - and not just for compatibility
with the cool toys. If he's going to be using random online tutorials and
reading references from everywhere, he's going to run into stuff written for
both 3.x and 2.x and he's going to have to know the little differences to
compensate for when trying out the practice code. For instance, 2.x users
that grab the new Head First Programming book by O'Reilly that's coming out
in Dec (teaching beginning programming using Python 3.1) will have issues
converting backwards.. so it's not all one way.

Just my mostly ignorant 2 cents.

-Kris

On Sat, Nov 14, 2009 at 12:49 PM, Stephen Nelson-Smith
<sanelson at gmail.com>wrote:

> My brother in law is learning python.  He's downloaded 3.1 for
> Windows, and is having a play.  It's already confused him that print
> "hello world" gives a syntax error....
>
> He's an absolute beginner with no programming experience at all.  I
> think he might be following 'Python Programming for the Absolute
> Beginner", or perhaps some online guides.  Should I advise him to
> stick with 2.6 for a bit, since most of the material out  there will
> be for 2.x?  Or since he's learning from scratch, should he jump
> straight to 3.x  In which case what can you recommend for him to work
> through - I must stress he has absolutely no clue at all about
> programming, no education beyond 16 yrs old, but is keen to learn.
>
> S.
>
> --
> Stephen Nelson-Smith
> Technical Director
> Atalanta Systems Ltd
> www.atalanta-systems.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091114/31fae58f/attachment.htm>

From mjekl at iol.pt  Sun Nov 15 06:05:30 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Sun, 15 Nov 2009 05:05:30 +0000
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <4AFF8158.1020905@ieee.org>
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt>
	<4AFF69FF.1040008@ieee.org>
	<20091115040053.ik0yf738g0sw88ks@webmail.iol.pt>
	<4AFF8158.1020905@ieee.org>
Message-ID: <20091115050530.fzgj9vjklc0g00ww@webmail.iol.pt>

davea at ieee.org wrote:
> (You forgot to send this message to the list, so I'm forwarding it)
>
> mjekl at iol.pt wrote:
>>> davea at ieee.org wrote:
>>> mjekl at iol.pt wrote:
>>>> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm  
>>>> wondering if I must save a file to memory before opening it. By  
>>>> opening I mean displaying it to the user.
>>>>
>>>> I have a BLOB field in a db and I have managed to read the blob  
>>>> into a binary fileobject. I've also managed to write it to disk  
>>>> and then I open it by doubleclicking on it. But I was wondering:
>>>>
>>>> 1. How to open the file directly from code (and not by double clicking):
>>>> I'm aware of os.startfile on Win by I'm on a Mac now, so I rather  
>>>> have a cross-platform way of accomplishing this.
>>>>
>>>> 2. If there is any Python module that takes care of saving and  
>>>> cleaning temp files in an OS transparent way?
>>>>
>>>> Txs,
>>>> Miguel
>>>>
>>>
>>> You don't say what this binary data is.  Is there a specific  
>>> program that should be launched to "display it to the user" ?  Or  
>>> do you have to keep this general?
>>>
>>> If you know what the necessary program is, you could use  
>>> subprocess module to launch it.  But I don't know enough about the  
>>> Mac to know how to do the Mac equivalent of  os.startfile
>>>
>>> As for avoiding the use of a file, that depends entirely on the  
>>> program you're launching.  Some programs can be told to get their  
>>> data from stdin.  If that's the case, there's a way to provide  
>>> stdin directly from Python, using subprocess.
>>>
>>> As for temporary files, consider tempfile module. I haven't used  
>>> it, but it looks promising. HTH,
>>> DaveA
>>>
>>
>> I know what the binary data is from the database. Typically it  
>> would be some file the OS knows how to open.
>> Chiefly I'm trying to avoid making the user save the file on some  
>> location and then go and double click it to open it. This makes no  
>> sense, since the user has already made the decision to open the file.
>> Miguel
>>
>> Txs Dave.
>>
> You forgot to answer the question.  You say "The OS knows how to  
> open".  Does *your* *program* know what program is needed, to open  
> this particular binary data?
>
Yes. My program knows. A database column stores the complete file name  
(including extension), and I can be certain the applications will be  
available to run the file.
Miguel

________________________________________________________________________________
COFIDIS Maxicredito. Ate' ?10.000 sem burocracias. Resposta on-line!
Clique aqui para saber mais http://www.iol.pt/correio/rodape.php?dst=0802273

From wescpy at gmail.com  Sun Nov 15 08:37:26 2009
From: wescpy at gmail.com (wesley chun)
Date: Sat, 14 Nov 2009 23:37:26 -0800
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <de4393b40911142025j4348c082tde87fabdfc401ee3@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
	<de4393b40911142025j4348c082tde87fabdfc401ee3@mail.gmail.com>
Message-ID: <78b3a9580911142337p768e5a5bh6a3dc711a0da4ce9@mail.gmail.com>

>> My brother in law is learning python.  He's downloaded 3.1 for
>> Windows, and is having a play.  It's already confused him that print
>> "hello world" gives a syntax error....
>>
>> He's an absolute beginner with no programming experience at all.  I
>> think he might be following 'Python Programming for the Absolute
>> Beginner", or perhaps some online guides.  Should I advise him to
>> stick with 2.6 for a bit, since most of the material out  there will
>> be for 2.x?  Or since he's learning from scratch, should he jump
>> straight to 3.x


good question, and already well-answered by most. i'll chime in with a
few remarks too. basically, if he is really starting from scratch,
i.e., no preexisting codebase, not using it for work, etc., then
there's no harm in starting using 3.x as long as you give the caveat
that most tutorials and source out there is still 2.x. 3.x has not
gained widespread adoption yet because not all of the lower-level (nor
higher-level) libraries, packages, and modules have been ported to 3.x
yet.

i gave a talk recently about this very topic (
http://siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227 )
and will repeat it again at PyCon 2010 in Atlanta (
http://us.pycon.org/2010/conference/talks -- see session #48 ).

i get asked this question a lot, esp. when it pertains to my book,
"Core Python Programming." which should i learn? is your book
obsolete? etc. i basically tell them that even though they are
backwards-incompatible, it's not like Python 2 and 3 are sooooo
different that you wouldn't recognize the language anymore! as Kris
has said, there are just a handful of noticeable difference that you
have to just keep in mind. finally, the next edition of the book will
definitely be BOTH Python 2 and 3. Python 2 isn't EOL'd and will be
around for awhile longer -- the most important evidence of this being
that both 2.x and 3.x are being developed in parallel.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com

From bibsmendez at gmail.com  Sun Nov 15 08:51:55 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Sun, 15 Nov 2009 10:51:55 +0300
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <78b3a9580911142337p768e5a5bh6a3dc711a0da4ce9@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
	<de4393b40911142025j4348c082tde87fabdfc401ee3@mail.gmail.com>
	<78b3a9580911142337p768e5a5bh6a3dc711a0da4ce9@mail.gmail.com>
Message-ID: <f16f1f8c0911142351k40689a2frf4e840bd36b3cc13@mail.gmail.com>

On Sun, Nov 15, 2009 at 10:37 AM, wesley chun <wescpy at gmail.com> wrote:

> >> My brother in law is learning python.  He's downloaded 3.1 for
> >> Windows, and is having a play.  It's already confused him that print
> >> "hello world" gives a syntax error....
> >>
> >> He's an absolute beginner with no programming experience at all.  I
> >> think he might be following 'Python Programming for the Absolute
> >> Beginner", or perhaps some online guides.  Should I advise him to
> >> stick with 2.6 for a bit, since most of the material out  there will
> >> be for 2.x?  Or since he's learning from scratch, should he jump
> >> straight to 3.x
>
>
> good question, and already well-answered by most. i'll chime in with a
> few remarks too. basically, if he is really starting from scratch,
> i.e., no preexisting codebase, not using it for work, etc., then
> there's no harm in starting using 3.x as long as you give the caveat
> that most tutorials and source out there is still 2.x. 3.x has not
> gained widespread adoption yet because not all of the lower-level (nor
> higher-level) libraries, packages, and modules have been ported to 3.x
> yet.
>
> i gave a talk recently about this very topic (
> http://siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227 )
> and will repeat it again at PyCon 2010 in Atlanta (
> http://us.pycon.org/2010/conference/talks -- see session #48 ).
>
> i get asked this question a lot, esp. when it pertains to my book,
> "Core Python Programming." which should i learn? is your book
> obsolete? etc. i basically tell them that even though they are
> backwards-incompatible, it's not like Python 2 and 3 are sooooo
> different that you wouldn't recognize the language anymore! as Kris
> has said, there are just a handful of noticeable difference that you
> have to just keep in mind. finally, the next edition of the book will
> definitely be BOTH Python 2 and 3. Python 2 isn't EOL'd and will be
> around for awhile longer -- the most important evidence of this being
> that both 2.x and 3.x are being developed in parallel.
>
> hope this helps!
> -- wesley
>


I just ordered your great book 2nd edition. I dont know if i should get
worried using a dated version. All i want is to learn the language. The
transition process (i think) should just follow normally once you learn the
language. So far I'm just a newbie trying to learn.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/7f61fa89/attachment.htm>

From alan.gauld at btinternet.com  Sun Nov 15 10:35:28 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 15 Nov 2009 09:35:28 -0000
Subject: [Tutor] opening a file directly from memory
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt><4AFF69FF.1040008@ieee.org><20091115040053.ik0yf738g0sw88ks@webmail.iol.pt><4AFF8158.1020905@ieee.org>
	<20091115050530.fzgj9vjklc0g00ww@webmail.iol.pt>
Message-ID: <hdoi1c$mjb$1@ger.gmane.org>

<mjekl at iol.pt> wrote
>> You forgot to answer the question.  You say "The OS knows how to  
>> open".  Does *your* *program* know what program is needed, to open  
>> this particular binary data?
>>
> Yes. My program knows. A database column stores the complete file name  
> (including extension), and I can be certain the applications will be  
> available to run the file.

You still haven't answered the question.

We have established that
a) The OS knows what program is associated with the file
b) your program knows the file name and location

But does your program - or more specifically you as the author - know 
what program the OS will use to open the file? If you do you can call it 
explicitly, but if you do not then you need to find a way of getting the 
OS to tell you, or to leave it to the OS.

For example a txt file could be opened by any of hundreds of text 
editors, depending on what the user selected, but a Photoshop 
file will typically only be opened by photoshop.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From mwalsh at mwalsh.org  Sun Nov 15 10:11:48 2009
From: mwalsh at mwalsh.org (Martin Walsh)
Date: Sun, 15 Nov 2009 03:11:48 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
Message-ID: <4AFFC5D4.6000304@mwalsh.org>

Stephen Nelson-Smith wrote:
> I think I'm having a major understanding failure.

Perhaps this will help ...
http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/

<snip>
> So in essence this:
> 
> logs = [ LogFile( "/home/stephen/qa/ded1353/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1408/quick_log.gz", "04/Nov/2009" ),
>          LogFile( "/home/stephen/qa/ded1409/quick_log.gz", "04/Nov/2009" ) ]
> 
> Gives me a list of LogFiles - each of which has a getline() method,
> which returns a tuple.
> 
> I thought I could merge iterables using Kent's recipe, or just with
> heapq.merge()

But, at this point are your LogFile instances even iterable? AFAICT, the
answer is no, and I think you should want them to be in order to use
heapq.merge. Have a look at the documentation
(http://docs.python.org/library/stdtypes.html#iterator-types) and then
re-read Kent's advice, in your previous thread ('Logfile multiplexing'),
about "using the iterator protocol" (__iter__).

And, judging by the heapq docs
(http://docs.python.org/library/heapq.html#heapq.merge) ...

"""
Merge multiple sorted inputs into a single sorted output (for example,
merge timestamped entries from multiple log files). Returns an iterator
over the sorted values.
"""

... using heapq.merge appears to be a reasonable approach.

You might also be interested to know, that while heapq.merge is(was) new
in 2.6, it's implementation is very similar (read: nearly identical) to
the one of the cookbook recipes referenced by Kent.

It's unclear from your previous posts (to me at least) -- are the
individual log files already sorted, in chronological order? I'd imagine
they are, being log files. But, let's say you were to run your
hypothetical merge script against only one file -- would the output to
be identical to the input? If not, then you'll want to sort the inputs
first.

> 
> But how do I get from a method that can produce a tuple, to some
> mergable iterables?
> 

I'm going to re-word this question slightly to "How can I modify the
LogFile class, for instances to be usable by heapq.merge?" and make an
attempt to answer. The following borrows heavily from Kent's iterator
example, but removes your additional line filtering (if
self.stamp.startswith(date), etc) to, hopefully, make it clearer.

import time, gzip, heapq

def timestamp(line):
    # replace with your own timestamp function
    # this appears to work with the sample logs I chose
    stamp = ' '.join(line.split(' ', 3)[:-1])
    return time.strptime(stamp, '%b %d %H:%M:%S')

class LogFile(object):
    def __init__(self, filename):
        self.logfile = gzip.open(filename, 'r')

    def __iter__(self):
        for logline in self.logfile:
            yield (timestamp(logline), logline)

logs = [
    LogFile("/home/stephen/qa/ded1353/quick_log.gz"),
    LogFile("/home/stephen/qa/ded1408/quick_log.gz"),
    LogFile("/home/stephen/qa/ded1409/quick_log.gz")
]

merged = heapq.merge(*logs)
with open('/tmp/merged_log', 'w') as output:
    for stamp, line in merged:
        output.write(line)

Will it be fast enough? I have no clue.

Good luck!
Marty




From davea at ieee.org  Sun Nov 15 11:32:55 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 15 Nov 2009 05:32:55 -0500
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <20091115050530.fzgj9vjklc0g00ww@webmail.iol.pt>
References: <20091115004927.ukv176t344kks0os@webmail.iol.pt>	<4AFF69FF.1040008@ieee.org>	<20091115040053.ik0yf738g0sw88ks@webmail.iol.pt>	<4AFF8158.1020905@ieee.org>
	<20091115050530.fzgj9vjklc0g00ww@webmail.iol.pt>
Message-ID: <4AFFD8D7.6010503@ieee.org>



mjekl at iol.pt wrote:
> <div class="moz-text-flowed" style="font-family: 
> -moz-fixed">davea at ieee.org wrote:
>> (You forgot to send this message to the list, so I'm forwarding it)
>>
>> mjekl at iol.pt wrote:
>>>> davea at ieee.org wrote:
>>>> mjekl at iol.pt wrote:
>>>>> <div class=oz-text-flowed" style="font-family: -moz-fixed">I'm 
>>>>> wondering if I must save a file to memory before opening it. By 
>>>>> opening I mean displaying it to the user.
>>>>>
>>>>> I have a BLOB field in a db and I have managed to read the blob 
>>>>> into a binary fileobject. I've also managed to write it to disk 
>>>>> and then I open it by doubleclicking on it. But I was wondering:
>>>>>
>>>>> 1. How to open the file directly from code (and not by double 
>>>>> clicking):
>>>>> I'm aware of os.startfile on Win by I'm on a Mac now, so I rather 
>>>>> have a cross-platform way of accomplishing this.
>>>>>
>>>>> 2. If there is any Python module that takes care of saving and 
>>>>> cleaning temp files in an OS transparent way?
>>>>>
>>>>> Txs,
>>>>> Miguel
>>>>>
>>>>
>>>> You don't say what this binary data is.  Is there a specific 
>>>> program that should be launched to "display it to the user" ?  Or 
>>>> do you have to keep this general?
>>>>
>>>> If you know what the necessary program is, you could use subprocess 
>>>> module to launch it.  But I don't know enough about the Mac to know 
>>>> how to do the Mac equivalent of  os.startfile
>>>>
>>>> As for avoiding the use of a file, that depends entirely on the 
>>>> program you're launching.  Some programs can be told to get their 
>>>> data from stdin.  If that's the case, there's a way to provide 
>>>> stdin directly from Python, using subprocess.
>>>>
>>>> As for temporary files, consider tempfile module. I haven't used 
>>>> it, but it looks promising. HTH,
>>>> DaveA
>>>>
>>>
>>> I know what the binary data is from the database. Typically it would 
>>> be some file the OS knows how to open.
>>> Chiefly I'm trying to avoid making the user save the file on some 
>>> location and then go and double click it to open it. This makes no 
>>> sense, since the user has already made the decision to open the file.
>>> Miguel
>>>
>>> Txs Dave.
>>>
>> You forgot to answer the question.  You say "The OS knows how to 
>> open".  Does *your* *program* know what program is needed, to open 
>> this particular binary data?
>>
> Yes. My program knows. A database column stores the complete file name 
> (including extension), and I can be certain the applications will be 
> available to run the file.
> Miguel
>
>
Then just use the subprocess module to invoke the program, passing it 
the filename as an argument.  No need for any double-clicking.

DaveA

From grflanagan at gmail.com  Sun Nov 15 12:06:09 2009
From: grflanagan at gmail.com (Gerard Flanagan)
Date: Sun, 15 Nov 2009 11:06:09 +0000
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
Message-ID: <hdonb1$2fn$1@ger.gmane.org>

Shashwat Anand wrote:
 > How to find all possible integer co-ordinates lying on a circle of 
given radius 'r'.
 > If given the upper bound of 'r', I want to calculate all given 
co-ordinates lying for 0 <= r <= n
 >
 > Let's say the upper bound of radius is 5
 > All possible results are:
 > radius 'r' - (x, y)
 > 0 - (0, 0)
 > 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
 > 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
 > 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
 > 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
 > 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, 
-3), (-3, 4), (-4, 3), (-3, -4), (-4, -3)
 >
 > Also for a particular radius, lots of possible combination of (x, y) 
is possible so best datastructure could be defaultdict for further 
operations IMHO.
 > So my desired output is:
 > defaultdict(<type 'list'>, {0 : [(0, 0)], 1: [(1, 0), (0, 1), (-1, 
0), (0, -1)], 2: [(2, 0), (0, 2), (-2, 0), (0, -2)], 3: [(3, 0), (0, 3), 
(-3, 0), (0, -3)], 4: [(4, 0), (0, 4), (-4, 0), (0, -4)], 5: [(5, 0), 
(0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 
3), (-3, -4), (-4, -3)]})
 >

I think you only need to consider the first quadrant (positive  x and 
y). So if you find one result you can determine the other seven (or 
three for a point on the axis). One approach might be to use complex 
numbers:


m = complex(0, 1).__mul__

rng4 = range(4)

def rotate_and_mirror(z):
    for _ in rng4:
        yield z
        yield z.conjugate()
        z = m(z)

or:

def rotate_and_mirror2(x, y):
    z = complex(x, y)
    for _ in rng4:
        yield z
        yield z.conjugate()
        z = m(z)

for z in rotate_and_mirror2(3, 4):
    print z

(3+4j)
(3-4j)
(-4+3j)
(-4-3j)
(-3-4j)
(-3+4j)
(4-3j)
(4+3j)


 > My approach using pythagorean triplets:
 > >>> d = collections.defaultdict(list)
 > >>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in 
range(10) for v in range(u+1, 10)]))
 > >>> [d[k].append(v) for k,v in s]
 > However it sure is wrong and fails in my case.
 >
 > Any suggestions as to how can I reach my desired goal, or are there 
any other options to tackle this problem?
 >

I think there are formulas for finding triples, eg.

def primitive_triples(N):
    for i in xrange(1, N):
        m = 2 * i
        h = m * (i+1)
        yield m+1, h, h+1

So it seems the problem is finding primitive triples and multiples of 
those triples. ie. (3, 4) is on the circle with radius 5, so (6, 8) is 
on the circle with radius 10 etc.

That's the best I've got - my "maths brain" is much atrophied! Good luck.

Gerard



From sanelson at gmail.com  Sun Nov 15 12:42:24 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sun, 15 Nov 2009 11:42:24 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <4AFFC5D4.6000304@mwalsh.org>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<4AFFC5D4.6000304@mwalsh.org>
Message-ID: <b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>

Hi Martin,

Thanks for a very detailed response.  I'm about to head out, so I
can't put your ideas into practice yet, or get down to studying for a
while.

However, I had one thing I felt I should respond to.

> It's unclear from your previous posts (to me at least) -- are the
> individual log files already sorted, in chronological order?

Sorry if I didn't make this clear.  No they're not.  They are *nearly*
sorted - ie they're out by a few seconds, every so often, but they are
in order at the level of minutes, or even in the order of a few
seconds.

It was precisely because of this that I decided, following Alan's
advice, to pre-filter the data.  I compiled a unix sort command to do
this, and had a solution I was happy with, based on Kent's iterator
example, fed into heapq.merge.

However, I've since discovered that the unix sort isn't reliable on
the last and first day of the month.  So, I decided I'd need to sort
each logfile first.  The code at the start of *this* thread does this
- it uses a heapq per logfile and is able to produce a tuple of
timestamp, logline, which will be in exact chronological order.  What
I want to do is merge this output into a file.

I think I probably have enough to be getting on with, but I'll be sure
to return if I still have questions after studying the links you
provided, and trying the various suggestions people have made.

Thanks so very much!

S.

From timomlists at gmail.com  Sun Nov 15 13:39:00 2009
From: timomlists at gmail.com (Timo List)
Date: Sun, 15 Nov 2009 13:39:00 +0100
Subject: [Tutor] nul file in Windows
Message-ID: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>

For my program I disable the py2exe log feature by routing output to the
nul-file.
Code:

        if win32 and py2exe:
            sys.stdout = open("nul", "w")
            sys.stderr = open("nul", "w")

This always worked fine.

Today, I received an email from a user with the following error:
IOError: [Errno 2] No such file or directory: 'nul'

Now, I thought the nul-file always existed, shouldn't it?
Is there another way to disable output, if this one fails?

Cheers,
Timo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/0e4be0e1/attachment.htm>

From denis.spir at free.fr  Sat Nov 14 19:31:28 2009
From: denis.spir at free.fr (spir)
Date: Sat, 14 Nov 2009 19:31:28 +0100
Subject: [Tutor] OT: Writing code while tired, counterproductive?
In-Reply-To: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
Message-ID: <20091114193128.516dd062@o>

Le Sat, 14 Nov 2009 10:43:47 -0700,
Modulok <modulok at gmail.com> s'exprima ainsi:

> List,
> 
> This is kind off topic, but:
> 
> Does anyone else find, writing code while tired to be counterproductive?
> 
> It just seems like when I push myself to stay up late finishing a
> project, I sorely regret it the following day. Granted, Python is a
> fairly forgiving language and doesn't take the mental attention that
> some others require (like C++ ...or someone else's C++ ....or someone
> else's poorly written C++), but for me, writing code while tired
> usually ends up being a bad thing - In any language. But then there's
> the guilt of taking the day off and not meeting deadlines. *grumble*
> Just wondering if this is a common occurrence among the masses.
> 
> Anyone?
> -Modulok-

100% agree



denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From stefan_ml at behnel.de  Sun Nov 15 14:45:55 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Sun, 15 Nov 2009 14:45:55 +0100
Subject: [Tutor] parsing XML into a python dictionary
In-Reply-To: <772235.39513.qm@web51612.mail.re2.yahoo.com>
References: <772235.39513.qm@web51612.mail.re2.yahoo.com>
Message-ID: <hdp0mj$r6p$1@ger.gmane.org>

Christopher Spears, 14.11.2009 19:47:
> Thanks! I have a lot of XML files at work that users search through. I
> want to parse the XML into a python dictionary and then read the dictionary
> into a database that users can use to search through the thousands of files.

I think "database" is the right keyword here. Depending on how large your
"thousands of files" are and what the actual content of each file is, a
full-text search engine (e.g. pylucene) or an XML database might be the
right tool, instead of trying to write something up yourself.

If you want to use something that's in Python's standard library, consider
parsing the XML files as a stream instead of a document tree (look for the
iterparse() function in lxml.etree or the xml.etree.ElementTree package),
and safe the extracted data into a sqlite3 database.

You can also use such a database as a kind of cache that stores relevant
information for each file, and update that information whenever you notice
that a file has been modified.

Stefan


From waynejwerner at gmail.com  Sun Nov 15 14:52:35 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Sun, 15 Nov 2009 07:52:35 -0600
Subject: [Tutor] nul file in Windows
In-Reply-To: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>
References: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>
Message-ID: <333efb450911150552p46d2b669ncf5b0a710db27d0b@mail.gmail.com>

On Sun, Nov 15, 2009 at 6:39 AM, Timo List <timomlists at gmail.com> wrote:

> For my program I disable the py2exe log feature by routing output to the
> nul-file.
> Code:
>
>         if win32 and py2exe:
>             sys.stdout = open("nul", "w")
>             sys.stderr = open("nul", "w")
>
> This always worked fine.
>
> Today, I received an email from a user with the following error:
> IOError: [Errno 2] No such file or directory: 'nul'
>
> Now, I thought the nul-file always existed, shouldn't it?
> Is there another way to disable output, if this one fails?
>

Just try/except:

try:
   #put your code here
except IOError:
   #don't save, print a message, whatever you want to do
   #if you want to propagate the error, uncomment next line
   #raise

HTH,
Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/54432597/attachment-0001.htm>

From davea at ieee.org  Sun Nov 15 15:12:38 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 15 Nov 2009 09:12:38 -0500
Subject: [Tutor] nul file in Windows
In-Reply-To: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>
References: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>
Message-ID: <4B000C56.8090205@ieee.org>

Timo List wrote:
> For my program I disable the py2exe log feature by routing output to the
> nul-file.
> Code:
>
>         if win32 and py2exe:
>             sys.stdout = open("nul", "w")
>             sys.stderr = open("nul", "w")
>
> This always worked fine.
>
> Today, I received an email from a user with the following error:
> IOError: [Errno 2] No such file or directory: 'nul'
>
> Now, I thought the nul-file always existed, shouldn't it?
> Is there another way to disable output, if this one fails?
>
> Cheers,
> Timo
>
>   
All you need is an object that behaves like a file, but does nothing 
with the data sent to it.  That's what duck-typing is all about.

I haven't tried it, but I'd start by making a new class:

class  NullFile(object):
    def __init__(self, *arg, **kwarg):
             pass
    def write(self, data, *arg, **kwarg):
            pass
     def close(self, *arg, **kwarg):
           pass

and just say
    sys.stdout = NullFile()

If you get any exceptions, you could add new methods to this file, 
accordingly.


DaveA


From sanelson at gmail.com  Sun Nov 15 16:12:40 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Sun, 15 Nov 2009 15:12:40 +0000
Subject: [Tutor] Unexpected iterator
In-Reply-To: <4AFBD794.3020101@gmail.com>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
	<4AFBD794.3020101@gmail.com>
Message-ID: <b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>

> To upack your variables a and b you need an iterable object on the right
> side, which returns you exactly 2 variables

What does 'unpack' mean?  I've seen a few Python errors about packing
and unpacking.  What does it mean?

S.

From alan.plum at uni-koeln.de  Sun Nov 15 16:52:06 2009
From: alan.plum at uni-koeln.de (Alan Plum)
Date: Sun, 15 Nov 2009 16:52:06 +0100
Subject: [Tutor] Unexpected iterator
In-Reply-To: <b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
	<4AFBD794.3020101@gmail.com>
	<b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>
Message-ID: <1258300326.3860.11.camel@kallisti>

On So, 2009-11-15 at 15:12 +0000, Stephen Nelson-Smith wrote:
> > To upack your variables a and b you need an iterable object on the right
> > side, which returns you exactly 2 variables
> 
> What does 'unpack' mean?  I've seen a few Python errors about packing
> and unpacking.  What does it mean?

Unpacking refers to taking a collection of values and assigning it to a
collection of variables.

(a,b) = (1,2,3) # this won't work

Let's see what it actually means (in pseudo-code):

(a,b)[0] = (1,2,3)[0] # a = 1
(a,b)[1] = (1,2,3)[1] # b = 2
(a,b)[2] = (1,2,3)[2] # undefined! = 3

Same goes for the inverse:

(a,b,c) = (1,2) # this won't work either

in pseudo code:

(a,b,c)[0] = (1,2)[0] # a = 1
(a,b,c)[1] = (1,2)[1] # b = 2
(a,b,c)[2] = (1,2)[2] # c = undefined!

Basically, just make sure your len(variables) == len(values) before
trying to use the unpacking syntax:

(a,b,c) = (1,2,3) # this works
(a,b) = (1,2) # this works, too


Cheers

Alan


From mwalsh at mwalsh.org  Sun Nov 15 18:44:24 2009
From: mwalsh at mwalsh.org (Martin Walsh)
Date: Sun, 15 Nov 2009 11:44:24 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>	
	<4AFFC5D4.6000304@mwalsh.org>
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
Message-ID: <4B003DF8.9070909@mwalsh.org>

Stephen Nelson-Smith wrote:
>> It's unclear from your previous posts (to me at least) -- are the
>> individual log files already sorted, in chronological order?
> 
> Sorry if I didn't make this clear.  No they're not.  They are *nearly*
> sorted - ie they're out by a few seconds, every so often, but they are
> in order at the level of minutes, or even in the order of a few
> seconds.
> 
> It was precisely because of this that I decided, following Alan's
> advice, to pre-filter the data.  I compiled a unix sort command to do
> this, and had a solution I was happy with, based on Kent's iterator
> example, fed into heapq.merge.
> 
> However, I've since discovered that the unix sort isn't reliable on
> the last and first day of the month.  So, I decided I'd need to sort
> each logfile first.  The code at the start of *this* thread does this
> - it uses a heapq per logfile and is able to produce a tuple of
> timestamp, logline, which will be in exact chronological order.  What
> I want to do is merge this output into a file.

Well, you haven't described the unreliable behavior of unix sort so I
can only guess, but I assume you know about the --month-sort (-M) flag?

I did misunderstand your intent for this thread, so thanks for
clarifying. The fact remains that if you are interested in using
heapq.merge, then you need to pass it iterable objects. And, I don't see
any reason to avoid adapting your approach to fit heapq.merge. How about
something like the following (completely untested) ...

import gzip
from heapq import heappush, heappop, merge

def timestamp(line):
    # replace with your own timestamp function
    # this appears to work with the sample logs I chose
    stamp = ' '.join(line.split(' ', 3)[:-1])
    return time.strptime(stamp, '%b %d %H:%M:%S')

class LogFile(object):
    def __init__(self, filename, jitter=10):
        self.logfile = gzip.open(filename, 'r')
        self.heap = []
        self.jitter = jitter

    def __iter__(self):
        while True:
            for logline in self.logfile:
                heappush(self.heap, (timestamp(logline), logline))
                if len(self.heap) >= self.jitter:
                    break
            try:
                yield heappop(self.heap)
            except IndexError:
                raise StopIteration

logs = [
    LogFile("/home/stephen/qa/ded1353/quick_log.gz"),
    LogFile("/home/stephen/qa/ded1408/quick_log.gz"),
    LogFile("/home/stephen/qa/ded1409/quick_log.gz")
]

merged_log = merge(*logs)
with open('/tmp/merged_log', 'w') as output:
    for stamp, line in merged_log:
        output.write(line)


... which probably won't preserve the order of log entries that have the
same timestamp, but if you need it to -- should be easy to accommodate.

HTH,
Marty

From wescpy at gmail.com  Sun Nov 15 19:11:35 2009
From: wescpy at gmail.com (wesley chun)
Date: Sun, 15 Nov 2009 10:11:35 -0800
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <f16f1f8c0911142351k40689a2frf4e840bd36b3cc13@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
	<de4393b40911142025j4348c082tde87fabdfc401ee3@mail.gmail.com>
	<78b3a9580911142337p768e5a5bh6a3dc711a0da4ce9@mail.gmail.com>
	<f16f1f8c0911142351k40689a2frf4e840bd36b3cc13@mail.gmail.com>
Message-ID: <78b3a9580911151011h137271des22cd8376598f24cf@mail.gmail.com>

>> i get asked this question a lot, esp. when it pertains to my book,
>> "Core Python Programming." which should i learn? is your book
>> obsolete? etc. i basically tell them that even though they are
>> backwards-incompatible, it's not like Python 2 and 3 are sooooo
>> different that you wouldn't recognize the language anymore! as Kris
>> has said, there are just a handful of noticeable difference that you
>> have to just keep in mind. finally, the next edition of the book will
>> definitely be BOTH Python 2 and 3. Python 2 isn't EOL'd and will be
>> around for awhile longer -- the most important evidence of this being
>> that both 2.x and 3.x are being developed in parallel.
>
> I just ordered your great book 2nd edition. I dont know if i should get
> worried using a dated version. All i want is to learn the language. The
> transition process (i think) should just follow normally once you learn the
> language. So far I'm just a newbie trying to learn.


you see? that's *exactly* what i'm talking about. :-)  my stance for
all my books and my courses is that i focus on teaching you the core
fundamentals of the Python language. i'm less interested in the minor
differences between releases that newbies will not likely use anyway.

however, i cannot ignore the fact that 3.x is backwards-incompatible
with older releases, so those are the opportune times to mention the
differences. the next edition of the book will not exist for several
years, and in that book, it will be a combo of Python 2 and 3, not
purely just 3.

in the most recent printing of the 2nd ed., the publishers have let me
add 2 new appendices, one on Python 3 as the next generation of the
language, and another on Python 2.6 and the remaining 2.x releases and
their transition role. these should be able to tie y'all over until
3rd ed. :-)

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2009
    http://withdjango.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com

From denis.spir at free.fr  Sun Nov 15 19:49:27 2009
From: denis.spir at free.fr (spir)
Date: Sun, 15 Nov 2009 19:49:27 +0100
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
Message-ID: <20091115194927.246a33eb@o>

Le Sat, 14 Nov 2009 20:49:52 +0000,
Stephen Nelson-Smith <sanelson at gmail.com> s'exprima ainsi:

> My brother in law is learning python.  He's downloaded 3.1 for
> Windows, and is having a play.  It's already confused him that print
> "hello world" gives a syntax error....
> 
> He's an absolute beginner with no programming experience at all.  I
> think he might be following 'Python Programming for the Absolute
> Beginner", or perhaps some online guides.  Should I advise him to
> stick with 2.6 for a bit, since most of the material out  there will
> be for 2.x?  Or since he's learning from scratch, should he jump
> straight to 3.x  In which case what can you recommend for him to work
> through - I must stress he has absolutely no clue at all about
> programming, no education beyond 16 yrs old, but is keen to learn.
> 
> S.
> 

(While I'm aware this answer may launch sharp replies from some of you, I'll be bold and say what I mean;-)

I guess python is no more a language especially suited for absolute beginners for a while already. It has grown too many features, too much complication and several layers of abstraction. So, my answers would not be py2.6 or py3.1, rather py1.5.

Lua is certainly great in comparison to present python in this respect --except if you think builtin OO is a must. But lua is "prepared" for OO anyway (its tables are dict-like objects), & several libraries provide it. Lua + OO framework is close to python 1.5 from my point of view, except less builtin sweeties, and even cleaner syntax (half-way between python and pascal, but unfortunately no indented structure).

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From denis.spir at free.fr  Sun Nov 15 20:16:40 2009
From: denis.spir at free.fr (spir)
Date: Sun, 15 Nov 2009 20:16:40 +0100
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
	<4AFF6E9C.7050302@ieee.org>
	<d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
Message-ID: <20091115201640.57bfe0eb@o>

Le Sun, 15 Nov 2009 09:11:16 +0530,
Shashwat Anand <anand.shashwat at gmail.com> s'exprima ainsi:

> > No, I'm trying to find all integer co-ordinates which lies on a circle.  
> Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0),
> (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
> 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 = 5**2 (as
> Origin is the centre of the circle.)
> Now I want a table of these points for say r = 0 to upper bound.
> So for r = 0, the only point lying on it is (0,0)
> For r = 1, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1,
> 0), (0, 1), (-1, 0), (0, -1)]
> And so forth.
> 
> Thus the table shows the points (x,y) which are lying on the circle of
> radius 'r'.
> 
> radius 'r' - (x, y)
> 0 - (0, 0)
> 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
> 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
> 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
> 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
> 4), (-4, 3), (-3, -4), (-4, -3)
> 
> Which is correct. I'm not trying to find all integer co-ordinates within a
> circle but trying to create a table of points lying on the circle of radius
> 'r', varying the radius from '0' to upper bound 'r'.
> The bruteforce can be done as:
> 
> #R = upper bound
> for r in range(0, R+1):
>     for x in range(0, R+1):
>         for y in range(0, R+1):
>             if x**2 + y**2 == r**2:
>                 #store them
> 
> However the complexity reaches O(n**3) and it's not optimized at all. So I
> though of using pythagorean triplets.

Interesting! As some said previously, you only need to find point for a quadrant, then extrapolate.
I can see two approaches to find _integer_ coordinate pairs on a circle of given radius:

* Walk the circle itself. I mean start with a point on it (0,r), then find an algorithm to walk step by step (unit by unit) while keeping as close as possible to the circle. This means rounding to next int. Eg for r=2, you would step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether x? + y? = 2?.

* Solve the above equation in integer domain, again by an approaching algorithm, maybe taking profit of know points (where either x or y is 0).

I guess such approaches can  be interesting, compared to brute force, only in case of very big number of radii or very big radii.

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From alan.gauld at btinternet.com  Sun Nov 15 20:31:37 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Sun, 15 Nov 2009 19:31:37 +0000 (GMT)
Subject: [Tutor] Fw: getting python 3 to run from the command line (version
	2)
In-Reply-To: <371510.75427.qm@web86708.mail.ird.yahoo.com>
References: <dfac564f0911140854v42fa4e0ehe5868517a50ef8bb@mail.gmail.com>
	<hdmugg$cia$1@ger.gmane.org>
	<dfac564f0911142311j43089ee4r4f5e4cca7aef3c76@mail.gmail.com>
	<371510.75427.qm@web86708.mail.ird.yahoo.com>
Message-ID: <339296.66934.qm@web86707.mail.ird.yahoo.com>

Forwarding to the tutor list with cut n paste sessions.
 
It looks to me like the code page issue somebody else 
referred to is the problem but the behaviour seems a 
bit extreme, I'd have thought it might have chosen 
a default value or something...

But I'm not sure what causes it to select cp720 in 
the first place. Where does Py_Initialize get its values?
Are they set in the environment somewhere?

And why is 2.6 OK? I know 3.0 did some changes around 
locale handling but I can't recall what they were. 
I assume this is a side effect of those changes?
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/




----- Forwarded Message ----
From: Khalid Al-Ghamdi <emailkgnow at gmail.com>
To: ALAN GAULD <alan.gauld at btinternet.com>
Sent: Sunday, 15 November, 2009 18:02:25
Subject: Re: [Tutor] getting python 3 to run from the command line (version 2)


Hi,
I'm really sorry for this hassle!

Python31 (gives error):

C:\Users\KE>python
Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: cp720

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

C:\Users\KE>cd c:\python31

c:\Python31>python
Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: cp720

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Python30 (doesn't respond):

c:\Python31>cd c:\python30

c:\Python30>python
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
>>> 

Python26(works ok):

C:\Users\KE>cd c:\python26

c:\Python26>python
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>>

Thanks very much













On Sun, Nov 15, 2009 at 8:16 PM, ALAN GAULD <alan.gauld at btinternet.com> wrote:

Unfortunately the image didn't get to me.
>
>
>To copy/paste from a cmd window click the small icon 
>at the left hand end of the title bar.
>
>
>From that select Edit->Mark
>Use the mouse to select the text you want to copy
>Use the menu again to do Edit->Copy
>
>
>Now paste into your mail program.
>
>
>You can make this easier by opening the properties 
>dialog from the same menu, and on the Options tag 
>tick QuickEdit. This will allow you to select with 
>the mouse and copy by hitting return after selecting.
>
>
>This is very useful when sending python examples 
>to the
> tutor list! :-) 
>
>
>
>Alan Gauld
>Author of the Learn To Program website
>
>http://www.alan-g.me.uk/
>
>
>
>
>
>
________________________________
 From: Khalid Al-Ghamdi <emailkgnow at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com>
>Sent: Sunday, 15 November, 2009 7:11:06
>Subject: Re: [Tutor] getting python 3 to run from the command line (version 2)
>
>
>
>Hi Alan,
>
>
>this is how the problem looks from
> my work pc (which is xp. at home i use vista). and yes i don't know how copy and paste from the cmd so here is an image:
>
>
>
>
>
>
>
>As you can see when I try to access python31 it gives me the error above. When I use python 26 it works fine.
>Now, I don't have python30 installed at my work PC, but what happens is when I enter c:\python30\python it initiates and gives me a blank prompt(>>>) then when i enter something (2+2 for example) it returns (>>>) as if i just pressed enter without entering any code. It just doesn't process the code for me. 
>
>
>I hope you can be of help.
>
>
>thanks 
>
>
>On Sat, Nov 14, 2009 at 9:56 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
>>>>"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote
>>
>>
>>
>>>>>when i try to code something it just gives me a new line without any
>>>>>>processing of the code. (for example:2+2 returns a new line)
>>>
>>
>>You mean you get to the >>> prompt?
>>>>And you type 2+2 you get this:
>>
>>
>>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>2+2
>>>>>
>>
>>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>With just a newline between your input and the next >>> prompt?
>>
>>
>>>>>when I tried to change the path to the directory that contains python 31 and
>>>
>>>enter *python (c:\python31>)*,
>>>
>>>>I'm not surprised it doesn't work, that should have python trying to start
>>>>executing a folder. But....
>>
>>
>>>>>*fatal python error: Py_Initialize: can't initialize sys standard streams
>>>
>>>>I get a very different message:
>>
>>>>C:\Documents and Settings\Alan Gauld>python (C:\Python31)
>>>>python: can't open file '(C:\Python31)': [Errno 22] Invalid argument
>>
>>
>>
>>>>>When i change the directory to c:\python26 and then enter it works ok.
>>>>>>so can anyone tell me why this is happening?
>>>
>>
>>Nope, sorry, I don't understand how the 2.6 version works if
>>>>you are passing in a folder as you did for 3.1
>>
>>>>Do you know how to cut n paste from a cmd window>
>>>>It would probably help if you pasted in the actuall sessions
>>>>into your mail.
>>
>>>>HTH,
>>
>>
>>>>-- 
>>>>Alan Gauld
>>
>>>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/ 
>>
>>
>>>>_______________________________________________
>>>>Tutor maillist  -  Tutor at python.org
>>>>To unsubscribe or change subscription options:
>>http://mail.python.org/mailman/listinfo/tutor
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/b5d8105d/attachment-0001.htm>

From alan.gauld at btinternet.com  Sun Nov 15 20:23:33 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 15 Nov 2009 19:23:33 -0000
Subject: [Tutor] Unexpected iterator
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com><4AFBD794.3020101@gmail.com>
	<b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>
Message-ID: <hdq0aq$p1c$1@ger.gmane.org>

"Stephen Nelson-Smith" <sanelson at gmail.com> wrote

>> To upack your variables a and b you need an iterable object on the right
>> side, which returns you exactly 2 variables
>
> What does 'unpack' mean?  I've seen a few Python errors about packing
> and unpacking.  What does it mean?

It has a coup[le of uses, the one being referred to here is where we
take a sequence of values and assign each value to a corresponding
variable on the other side of the assignment.

x,y = (val1, val2)

which is equivalent to

x = val1; y = val2

or

j,k = [3,4]

j=3; k=4

You can also pack a set of variables into a single value:

t = 3,4,5

or

s = 4,5,6

But this is really just tuple assignment without the parentheses! :-)

Pack/Unpack can also refer to an operation of the struct module,
which does a similar thing with binary data.

So if we have a set of bytes b we can extract the bytes into a
set of variables using a format string and the struct.unpack()
operation. Conversely you use struct.pack to encode a sequence
of values into a sequence of bytes.

But thats a more specialised usage, not seen so often.

HTH

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From sanelson at gmail.com  Mon Nov 16 01:11:07 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 00:11:07 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <4B003DF8.9070909@mwalsh.org>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<4AFFC5D4.6000304@mwalsh.org>
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
	<4B003DF8.9070909@mwalsh.org>
Message-ID: <b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>

Hi Marty,

Thanks for a very lucid reply!

> Well, you haven't described the unreliable behavior of unix sort so I
> can only guess, but I assume you know about the --month-sort (-M) flag?

Nope - but I can look it up.  The problem I have is that the source
logs are rotated at 0400 hrs, so I need two days of logs in order to
extract 24 hrs from 0000 to 2359 (which is the requirement).  At
present, I preprocess using sort, which works fine as long as the
month doesn't change.

> import gzip
> from heapq import heappush, heappop, merge

Is this a preferred method, rather than just 'import heapq'?

> def timestamp(line):
> ? ?# replace with your own timestamp function
> ? ?# this appears to work with the sample logs I chose
> ? ?stamp = ' '.join(line.split(' ', 3)[:-1])
> ? ?return time.strptime(stamp, '%b %d %H:%M:%S')

I have some logfie entries with multiple IP addresses, so I can't
split using whitespace.

> class LogFile(object):
> ? ?def __init__(self, filename, jitter=10):
> ? ? ? ?self.logfile = gzip.open(filename, 'r')
> ? ? ? ?self.heap = []
> ? ? ? ?self.jitter = jitter
>
> ? ?def __iter__(self):
> ? ? ? ?while True:
> ? ? ? ? ? ?for logline in self.logfile:
> ? ? ? ? ? ? ? ?heappush(self.heap, (timestamp(logline), logline))
> ? ? ? ? ? ? ? ?if len(self.heap) >= self.jitter:
> ? ? ? ? ? ? ? ? ? ?break

Really nice way to handle the batching of the initial heap - thank you!

> ? ? ? ? ? ?try:
> ? ? ? ? ? ? ? ?yield heappop(self.heap)
> ? ? ? ? ? ?except IndexError:
> ? ? ? ? ? ? ? ?raise StopIteration
>
> logs = [
> ? ?LogFile("/home/stephen/qa/ded1353/quick_log.gz"),
> ? ?LogFile("/home/stephen/qa/ded1408/quick_log.gz"),
> ? ?LogFile("/home/stephen/qa/ded1409/quick_log.gz")
> ]
>
> merged_log = merge(*logs)
> with open('/tmp/merged_log', 'w') as output:
> ? ?for stamp, line in merged_log:
> ? ? ? ?output.write(line)

Oooh, I've never used 'with' before.  In fact I am currently
restricted to 2.4 on the machine on whch this will run.  That wasn't a
problem for heapq.merge, as I was just able to copy the code from the
2.6 source.  Or I could use Kent's recipe.

> ... which probably won't preserve the order of log entries that have the
> same timestamp, but if you need it to -- should be easy to accommodate.

I don't think  that is necessary, but I'm curious to know how...

Now... this is brilliant.  What it doesn't do that mine does, is
handle date - mine checks for whether it starts with the appropriate
date, so we can extract 24 hrs of data.  I'll need to try to include
that.  Also, I need to do some filtering and gsubbing, but I think I'm
firmly on the right path now, thanks to you.

> HTH,

Very much indeed.

S.

From davea at ieee.org  Mon Nov 16 02:11:57 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 15 Nov 2009 20:11:57 -0500
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <20091115201640.57bfe0eb@o>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>	<4AFF6E9C.7050302@ieee.org>	<d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
	<20091115201640.57bfe0eb@o>
Message-ID: <4B00A6DD.60403@ieee.org>

spir wrote:
> Le Sun, 15 Nov 2009 09:11:16 +0530,
> Shashwat Anand <anand.shashwat at gmail.com> s'exprima ainsi:
>
>   
>>> No, I'm trying to find all integer co-ordinates which lies on a circle.  
>>>       
>> Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5, 0),
>> (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
>> 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 =**2 (as
>> Origin is the centre of the circle.)
>> Now I want a table of these points for say r = to upper bound.
>> So for r =, the only point lying on it is (0,0)
>> For r =, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1,
>> 0), (0, 1), (-1, 0), (0, -1)]
>> And so forth.
>>
>> Thus the table shows the points (x,y) which are lying on the circle of
>> radius 'r'.
>>
>> radius 'r' - (x, y)
>> 0 - (0, 0)
>> 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
>> 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
>> 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
>> 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
>> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
>> 4), (-4, 3), (-3, -4), (-4, -3)
>>
>> Which is correct. I'm not trying to find all integer co-ordinates within a
>> circle but trying to create a table of points lying on the circle of radius
>> 'r', varying the radius from '0' to upper bound 'r'.
>> The bruteforce can be done as:
>>
>> #R =pper bound
>> for r in range(0, R+1):
>>     for x in range(0, R+1):
>>         for y in range(0, R+1):
>>             if x**2 + y**2 =r**2:
>>                 #store them
>>
>> However the complexity reaches O(n**3) and it's not optimized at all. So I
>> though of using pythagorean triplets.
>>     
>
> Interesting! As some said previously, you only need to find point for a quadrant, then extrapolate.
> I can see two approaches to find _integer_ coordinate pairs on a circle of given radius:
>
> * Walk the circle itself. I mean start with a point on it (0,r), then find an algorithm to walk step by step (unit by unit) while keeping as close as possible to the circle. This means rounding to next int. Eg for r= you would step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether x? + y? = 2?.
>
> * Solve the above equation in integer domain, again by an approaching algorithm, maybe taking profit of know points (where either x or y is 0).
>
> I guess such approaches can  be interesting, compared to brute force, only in case of very big number of radii or very big radii.
>
> Denis
>
>   
What you don't know is that the OP's original unstated (and probably at 
that time unknown) requirement included circles of non-integer radius, 
as long as three of the points on such a circle land on integer 
vertices.  For example, the points  (8, 1), (1, -8), (-4, 7).  That 
little tidbit never made it into the thread.

DaveA

From marc.tompkins at gmail.com  Mon Nov 16 02:57:41 2009
From: marc.tompkins at gmail.com (Marc Tompkins)
Date: Sun, 15 Nov 2009 17:57:41 -0800
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<4AFFC5D4.6000304@mwalsh.org>
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
	<4B003DF8.9070909@mwalsh.org>
	<b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
Message-ID: <40af687b0911151757q34c35ce4j1b952f673eb16903@mail.gmail.com>

On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:

> > import gzip
> > from heapq import heappush, heappop, merge
>
> Is this a preferred method, rather than just 'import heapq'?
>
> It has a couple of advantages:
-  convenience: if you "import heapq", then to do a push you need to type
"heapq.heappush"; if you do "from heapq import heappush", then you can
simply type "heappush" to use it.
-  efficiency: you import only what you actually need to use.  Importing all
of a gigantic package to use one or two methods is wasteful; on the other
hand, if a package contains five methods and you're using four of them, this
might not be such a big deal.
Remember, this is Python... there's always going to be more than one way to
do it, and you'll get an argument on this list for every one of them.

What you should NEVER do, though: "from package import *"
-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/b00db83b/attachment.htm>

From metolone+gmane at gmail.com  Mon Nov 16 03:09:53 2009
From: metolone+gmane at gmail.com (Mark Tolonen)
Date: Sun, 15 Nov 2009 18:09:53 -0800
Subject: [Tutor] getting python 3 to run from the command line (version2)
References: <dfac564f0911140854v42fa4e0ehe5868517a50ef8bb@mail.gmail.com><hdmugg$cia$1@ger.gmane.org><dfac564f0911142311j43089ee4r4f5e4cca7aef3c76@mail.gmail.com><371510.75427.qm@web86708.mail.ird.yahoo.com>
	<339296.66934.qm@web86707.mail.ird.yahoo.com>
Message-ID: <hdqc9a$opv$1@ger.gmane.org>

Forgive the top-posting, but when in Rome...

Running 'chcp' at the command line will show the default code page.  Judging 
from the OP's name it is probably an Arabic version of Windows.

Since Python 2.6 works it probably is falling back to something besides 
cp720.  Try:
   import sys
   print sys.stdout.encoding
to find out what.

Python 3.0 probably has a bug if it runs but doesn't work correctly as 
described by the OP below.

Python 3.1 "refuses to guess" and displays an error.  Since Python 3.X uses 
Unicode for strings it really needs to know the encoding of the terminal to 
decode stdin and encode to stdout.  Implementing a cp720 codec would likely 
fix the problem.

-Mark



"ALAN GAULD" <alan.gauld at btinternet.com> wrote in message 
news:339296.66934.qm at web86707.mail.ird.yahoo.com...
Forwarding to the tutor list with cut n paste sessions.

It looks to me like the code page issue somebody else
referred to is the problem but the behaviour seems a
bit extreme, I'd have thought it might have chosen
a default value or something...

But I'm not sure what causes it to select cp720 in
the first place. Where does Py_Initialize get its values?
Are they set in the environment somewhere?

And why is 2.6 OK? I know 3.0 did some changes around
locale handling but I can't recall what they were.
I assume this is a side effect of those changes?
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/




----- Forwarded Message ----
From: Khalid Al-Ghamdi <emailkgnow at gmail.com>
To: ALAN GAULD <alan.gauld at btinternet.com>
Sent: Sunday, 15 November, 2009 18:02:25
Subject: Re: [Tutor] getting python 3 to run from the command line (version 
2)


Hi,
I'm really sorry for this hassle!

Python31 (gives error):

C:\Users\KE>python
Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: cp720

This application has requested the Runtime to terminate it in an unusual 
way.
Please contact the application's support team for more information.

C:\Users\KE>cd c:\python31

c:\Python31>python
Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: cp720

This application has requested the Runtime to terminate it in an unusual 
way.
Please contact the application's support team for more information.

Python30 (doesn't respond):

c:\Python31>cd c:\python30

c:\Python30>python
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] 
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
>>>

Python26(works ok):

C:\Users\KE>cd c:\python26

c:\Python26>python
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] 
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>>

Thanks very much













On Sun, Nov 15, 2009 at 8:16 PM, ALAN GAULD <alan.gauld at btinternet.com> 
wrote:

Unfortunately the image didn't get to me.
>
>
>To copy/paste from a cmd window click the small icon
>at the left hand end of the title bar.
>
>
>From that select Edit->Mark
>Use the mouse to select the text you want to copy
>Use the menu again to do Edit->Copy
>
>
>Now paste into your mail program.
>
>
>You can make this easier by opening the properties
>dialog from the same menu, and on the Options tag
>tick QuickEdit. This will allow you to select with
>the mouse and copy by hitting return after selecting.
>
>
>This is very useful when sending python examples
>to the
> tutor list! :-)
>
>
>
>Alan Gauld
>Author of the Learn To Program website
>
>http://www.alan-g.me.uk/
>
>
>
>
>
>
________________________________
 From: Khalid Al-Ghamdi <emailkgnow at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com>
>Sent: Sunday, 15 November, 2009 7:11:06
>Subject: Re: [Tutor] getting python 3 to run from the command line (version 
>2)
>
>
>
>Hi Alan,
>
>
>this is how the problem looks from
> my work pc (which is xp. at home i use vista). and yes i don't know how 
> copy and paste from the cmd so here is an image:
>
>
>
>
>
>
>
>As you can see when I try to access python31 it gives me the error above. 
>When I use python 26 it works fine.
>Now, I don't have python30 installed at my work PC, but what happens is 
>when I enter c:\python30\python it initiates and gives me a blank 
>prompt(>>>) then when i enter something (2+2 for example) it returns (>>>) 
>as if i just pressed enter without entering any code. It just doesn't 
>process the code for me.
>
>
>I hope you can be of help.
>
>
>thanks
>
>
>On Sat, Nov 14, 2009 at 9:56 PM, Alan Gauld <alan.gauld at btinternet.com> 
>wrote:
>
>
>>>>"Khalid Al-Ghamdi" <emailkgnow at gmail.com> wrote
>>
>>
>>
>>>>>when i try to code something it just gives me a new line without any
>>>>>>processing of the code. (for example:2+2 returns a new line)
>>>
>>
>>You mean you get to the >>> prompt?
>>>>And you type 2+2 you get this:
>>
>>
>>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>2+2
>>>>>
>>
>>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>With just a newline between your input and the next >>> prompt?
>>
>>
>>>>>when I tried to change the path to the directory that contains python 
>>>>>31 and
>>>
>>>enter *python (c:\python31>)*,
>>>
>>>>I'm not surprised it doesn't work, that should have python trying to 
>>>>start
>>>>executing a folder. But....
>>
>>
>>>>>*fatal python error: Py_Initialize: can't initialize sys standard 
>>>>>streams
>>>
>>>>I get a very different message:
>>
>>>>C:\Documents and Settings\Alan Gauld>python (C:\Python31)
>>>>python: can't open file '(C:\Python31)': [Errno 22] Invalid argument
>>
>>
>>
>>>>>When i change the directory to c:\python26 and then enter it works ok.
>>>>>>so can anyone tell me why this is happening?
>>>
>>
>>Nope, sorry, I don't understand how the 2.6 version works if
>>>>you are passing in a folder as you did for 3.1
>>
>>>>Do you know how to cut n paste from a cmd window>
>>>>It would probably help if you pasted in the actuall sessions
>>>>into your mail.
>>
>>>>HTH,
>>
>>
>>>>-- 
>>>>Alan Gauld
>>
>>>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>
>>
>>>>_______________________________________________
>>>>Tutor maillist  -  Tutor at python.org
>>>>To unsubscribe or change subscription options:
>>http://mail.python.org/mailman/listinfo/tutor
>>
>



--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 



From anand.shashwat at gmail.com  Mon Nov 16 03:20:53 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Mon, 16 Nov 2009 07:50:53 +0530
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <4B00A6DD.60403@ieee.org>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
	<4AFF6E9C.7050302@ieee.org>
	<d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
	<20091115201640.57bfe0eb@o> <4B00A6DD.60403@ieee.org>
Message-ID: <d4ab53de0911151820l6f8b52b4mb44732129c0dd19a@mail.gmail.com>

@DaveA: thanks for pointing it out.

For a origin-centre circle x**2 + y**2 = r**2, I assumed r to be integer,
however it was r**2 which was integer. A mistake on my part.

On Mon, Nov 16, 2009 at 6:41 AM, Dave Angel <davea at ieee.org> wrote:

> spir wrote:
>
>> Le Sun, 15 Nov 2009 09:11:16 +0530,
>> Shashwat Anand <anand.shashwat at gmail.com> s'exprima ainsi:
>>
>>
>>
>>> No, I'm trying to find all integer co-ordinates which lies on a circle.
>>>>
>>>>
>>> Say for a circle of radius 5 the co-ordinates are [(5, 0), (0, 5), (-5,
>>> 0),
>>> (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
>>> 4), (-4, 3), (-3, -4), (-4, -3)] which lies on circle x**2 + y**2 =**2
>>> (as
>>>
>>> Origin is the centre of the circle.)
>>> Now I want a table of these points for say r = to upper bound.
>>> So for r =, the only point lying on it is (0,0)
>>> For r =, the points lying on them (the circle x**2 + y**2 = 1**2) is [(1,
>>>
>>> 0), (0, 1), (-1, 0), (0, -1)]
>>> And so forth.
>>>
>>> Thus the table shows the points (x,y) which are lying on the circle of
>>> radius 'r'.
>>>
>>> radius 'r' - (x, y)
>>> 0 - (0, 0)
>>> 1 - (1, 0), (0, 1), (-1, 0), (0, -1)
>>> 2 - (2, 0), (0, 2), (-2, 0), (0, -2)
>>> 3 - (3, 0), (0, 3), (-3, 0), (0, -3)
>>> 4 - (4, 0), (0, 4), (-4, 0), (0, -4)
>>> 5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3),
>>> (-3,
>>> 4), (-4, 3), (-3, -4), (-4, -3)
>>>
>>> Which is correct. I'm not trying to find all integer co-ordinates within
>>> a
>>> circle but trying to create a table of points lying on the circle of
>>> radius
>>> 'r', varying the radius from '0' to upper bound 'r'.
>>> The bruteforce can be done as:
>>>
>>> #R =pper bound
>>>
>>> for r in range(0, R+1):
>>>    for x in range(0, R+1):
>>>        for y in range(0, R+1):
>>>            if x**2 + y**2 =r**2:
>>>                #store them
>>>
>>> However the complexity reaches O(n**3) and it's not optimized at all. So
>>> I
>>> though of using pythagorean triplets.
>>>
>>>
>>
>> Interesting! As some said previously, you only need to find point for a
>> quadrant, then extrapolate.
>> I can see two approaches to find _integer_ coordinate pairs on a circle of
>> given radius:
>>
>> * Walk the circle itself. I mean start with a point on it (0,r), then find
>> an algorithm to walk step by step (unit by unit) while keeping as close as
>> possible to the circle. This means rounding to next int. Eg for r= you would
>> step on (0,2), (1,1), (2,0), (1,-1),... For each pair, simply check whether
>> x? + y? = 2?.
>>
>> Got an algorithm used for antialiasing and plotting circle known as
bresanham's algorithm (
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm) but hadn't actually
worked on it.

>
>> * Solve the above equation in integer domain, again by an approaching
>> algorithm, maybe taking profit of know points (where either x or y is 0).
>>
>> I guess such approaches can  be interesting, compared to brute force, only
>> in case of very big number of radii or very big radii.
>>
>> Denis
>>
>>
>>
> What you don't know is that the OP's original unstated (and probably at
> that time unknown) requirement included circles of non-integer radius, as
> long as three of the points on such a circle land on integer vertices.  For
> example, the points  (8, 1), (1, -8), (-4, 7).  That little tidbit never
> made it into the thread.
>
> DaveA
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/a196e935/attachment.htm>

From anand.shashwat at gmail.com  Mon Nov 16 03:38:43 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Mon, 16 Nov 2009 08:08:43 +0530
Subject: [Tutor] Find Integer co-ordinates lying on a circle
In-Reply-To: <d4ab53de0911151820l6f8b52b4mb44732129c0dd19a@mail.gmail.com>
References: <d4ab53de0911141735m5ad084edibf98fe337f2f1ae2@mail.gmail.com>
	<4AFF6E9C.7050302@ieee.org>
	<d4ab53de0911141941r363831d8ycef6b6bac8e67f56@mail.gmail.com>
	<20091115201640.57bfe0eb@o> <4B00A6DD.60403@ieee.org>
	<d4ab53de0911151820l6f8b52b4mb44732129c0dd19a@mail.gmail.com>
Message-ID: <d4ab53de0911151838x5c9b82beoa91db16c188c798f@mail.gmail.com>

regarding Integer co-ordinates within a circle got a nice link :
http://mathworld.wolfram.com/GausssCircleProblem.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/af21b288/attachment.htm>

From kent37 at tds.net  Mon Nov 16 04:01:44 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 15 Nov 2009 22:01:44 -0500
Subject: [Tutor] Iterable Understanding
In-Reply-To: <40af687b0911151757q34c35ce4j1b952f673eb16903@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<4AFFC5D4.6000304@mwalsh.org>
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
	<4B003DF8.9070909@mwalsh.org>
	<b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
	<40af687b0911151757q34c35ce4j1b952f673eb16903@mail.gmail.com>
Message-ID: <1c2a2c590911151901r296cb451h1e3b952a045e85aa@mail.gmail.com>

On Sun, Nov 15, 2009 at 8:57 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:
> On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith <sanelson at gmail.com>
> wrote:
>>
>> > import gzip
>> > from heapq import heappush, heappop, merge
>>
>> Is this a preferred method, rather than just 'import heapq'?
>>
> It has a couple of advantages:
...
> -? efficiency: you import only what you actually need to use.? Importing all
> of a gigantic package to use one or two methods is wasteful;

I can't think of any way in which
  import heapq
is "wasteful" compared to
  from heapq import heappush, heappop, merge

other than a tiny efficiency in name lookup (it's faster to lookup
just heappush than heapq.heappush).

In either case, the entire module is loaded. The only difference is in
which names are introduced into the importing module's namespace.

Kent

From davea at ieee.org  Mon Nov 16 04:31:11 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 15 Nov 2009 22:31:11 -0500
Subject: [Tutor] Iterable Understanding
In-Reply-To: <40af687b0911151757q34c35ce4j1b952f673eb16903@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>	<4AFFC5D4.6000304@mwalsh.org>	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>	<4B003DF8.9070909@mwalsh.org>	<b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
	<40af687b0911151757q34c35ce4j1b952f673eb16903@mail.gmail.com>
Message-ID: <4B00C77F.1010008@ieee.org>



Marc Tompkins wrote:
> On Sun, Nov 15, 2009 at 4:11 PM, Stephen Nelson-Smith <sanelson at gmail.com>wrote:
>
>   
>>> import gzip
>>> from heapq import heappush, heappop, merge
>>>       
>> Is this a preferred method, rather than just 'import heapq'?
>>
>> It has a couple of advantages:
>>     
> -  convenience: if you "import heapq", then to do a push you need to type
> "heapq.heappush"; if you do "from heapq import heappush", then you can
> simply type "heappush" to use it.
> -  efficiency: you import only what you actually need to use.  Importing all
>   
from xx import yy, zz   gets the whole module xx, not just the 
functions  yy and zz.  It just doesn't add the module name to the 
present global namespace.  That changes the way you reference yy and zz, 
but it doesn't decrease the load time or the memory used.  And if you 
want to cheat, you can reference the rest of the module with something like:
    sys.modules["heapq"].heapify()

or even
heapq = sys.modules["heapq"]

which then gets you where you would have been had you just imported the 
module the first place.
> of a gigantic package to use one or two methods is wasteful; on the other
> hand, if a package contains five methods and you're using four of them, this
> might not be such a big deal.
>   
Now you switch from talking about modules (heapq) to packages.  I don't 
believe packages are loaded monolithically, but just modules.
> Remember, this is Python... there's always going to be more than one way to
> do it, and you'll get an argument on this list for every one of them.
>
> What you should NEVER do, though: "from package import *"
>   
Definite agreement on that one.

Consider the following two lines:

from hashlib import md5
q3 = sys.modules["hashlib"].sha1

Without the first line, the second one gets an error.  So the import is 
indeed getting more than just the md5 constructor.


DaveA


From denis.spir at free.fr  Mon Nov 16 06:17:27 2009
From: denis.spir at free.fr (spir)
Date: Mon, 16 Nov 2009 06:17:27 +0100
Subject: [Tutor] Unexpected iterator
In-Reply-To: <hdq0aq$p1c$1@ger.gmane.org>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
	<4AFBD794.3020101@gmail.com>
	<b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>
	<hdq0aq$p1c$1@ger.gmane.org>
Message-ID: <20091116061727.7102cfa5@o>

Le Sun, 15 Nov 2009 19:23:33 -0000,
"Alan Gauld" <alan.gauld at btinternet.com> s'exprima ainsi:

> What does 'unpack' mean?  I've seen a few Python errors about packing
> and unpacking.  What does it mean?  

Unpacking is rarely needed. It matches some kind of problems.
Imagine you parse "codes" each made of name-sep-number. Then when walking through the result you can write:
for code in codes:
	(name,sep,number) = code

(parens not needed) It's just an elegant manner to avoid indexing -- right?

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From zebra05 at gmail.com  Mon Nov 16 07:57:50 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Mon, 16 Nov 2009 08:57:50 +0200
Subject: [Tutor] Writing code while tired, counterproductive?
In-Reply-To: <hdmuil$cof$1@ger.gmane.org>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
	<hdmuil$cof$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911152257l23e5de3dk5ba9fdb01828b025@mail.gmail.com>

>From first-hand experience, i would concur :)

A refreshed mind will perform much better than an over-exerted one.

On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Modulok" <modulok at gmail.com> wrote
>
>  Does anyone else find, writing code while tired to be counterproductive?
>>
>
> Yes. Doing anything that is mentally taxing is usually a bad idea when
> tired!
>
> Alan G
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/bc121147/attachment.htm>

From stefan at lsd.co.za  Mon Nov 16 07:58:25 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Mon, 16 Nov 2009 08:58:25 +0200
Subject: [Tutor] I love python / you guys :)
Message-ID: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>

hi,

Although not a question, i just want to tell you guys how awesome you are!

I am not a programmer, i can do a bit of bash. I have never officially
learnt programming, but numerous times looked at some perl, c, java
and never really gotten past the beginning stages of it. That all
changed when i picked up python. Although my style and use of python
is probably barbaric at best, I really enjoy it when you can make
things work. Python was just amazing from a readability / logical
point of view. If i can think of something, there is a way to do it in
python. After learning the simple data structures it seems i can
really do anything i want to.

I really appreciate all the effort the knowledgeable guys put into
this list (you know who you are! you post answers all the time!). I've
asked a couple of questions and the answers have always been excellent
and i've learnt so much.

So i guess i just wanted to say thanks for supporting a great
language, and helping me as i learn to shape my thinking into better
and better ways to do things.

stefan

From wescpy at gmail.com  Mon Nov 16 08:08:02 2009
From: wescpy at gmail.com (wesley chun)
Date: Sun, 15 Nov 2009 23:08:02 -0800
Subject: [Tutor] Should a beginner learn Python 3.x
In-Reply-To: <20091115194927.246a33eb@o>
References: <b6131fdc0911141249v604c293q8da02a765fb92ba0@mail.gmail.com>
	<20091115194927.246a33eb@o>
Message-ID: <78b3a9580911152308s5e97d1f8v8770fafec0ec3780@mail.gmail.com>

>>?Should I advise him to
>> stick with 2.6 for a bit, since most of the material out ?there will
>> be for 2.x? ?Or since he's learning from scratch, should he jump
>> straight to 3.x ?In which case what can you recommend for him to work
>> through - I must stress he has absolutely no clue at all about
>> programming, no education beyond 16 yrs old, but is keen to learn.
>
> (While I'm aware this answer may launch sharp replies from some of you, I'll be bold and say what I mean;-)
>
> I guess python is no more a language especially suited for absolute beginners for a while already. It has grown too many features, too much complication and several layers of abstraction. So, my answers would not be py2.6 or py3.1, rather py1.5.


very bold indeed. if learning purely for being introduced to
programming, etc., not work-related, etc., you have an interesting
idea. but even so, you don't have to learn *every* feature of 2.x or
3.x to learn programming. you can just learn the 1.5 syntax.
basically, i'm not going to give a sharp reply, however, it is an
unusual suggestion, but it isn't mind-blowing as there are some
systems out there that are built using 1.5, e.g., the Red Hat
installer, http://www.scons.org ... etc. it also helps keep the 1st
edition of my book "Core Python Programming" alive -- that edition was
written against 1.5.2, 1.6, and 2.0 back in 2000-2001. :-)

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2009
    http://withdjango.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com

From zebra05 at gmail.com  Mon Nov 16 09:12:24 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Mon, 16 Nov 2009 10:12:24 +0200
Subject: [Tutor] I love python / you guys :)
In-Reply-To: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
References: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
Message-ID: <c7c6f3bc0911160012t5795f726veaef2a1d740e8b7c@mail.gmail.com>

Stefan, you echoed my thinking too :)

On Mon, Nov 16, 2009 at 8:58 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:

> hi,
>
> Although not a question, i just want to tell you guys how awesome you are!
>
> I am not a programmer, i can do a bit of bash. I have never officially
> learnt programming, but numerous times looked at some perl, c, java
> and never really gotten past the beginning stages of it. That all
> changed when i picked up python. Although my style and use of python
> is probably barbaric at best, I really enjoy it when you can make
> things work. Python was just amazing from a readability / logical
> point of view. If i can think of something, there is a way to do it in
> python. After learning the simple data structures it seems i can
> really do anything i want to.
>
> I really appreciate all the effort the knowledgeable guys put into
> this list (you know who you are! you post answers all the time!). I've
> asked a couple of questions and the answers have always been excellent
> and i've learnt so much.
>
> So i guess i just wanted to say thanks for supporting a great
> language, and helping me as i learn to shape my thinking into better
> and better ways to do things.
>
> stefan
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/3678ceb1/attachment.htm>

From fomcl at yahoo.com  Mon Nov 16 09:23:04 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Mon, 16 Nov 2009 00:23:04 -0800 (PST)
Subject: [Tutor] Apologies for sent spam
Message-ID: <179330.98920.qm@web110710.mail.gq1.yahoo.com>

Hi,
?
I just noticed that a spam message was sent with my email address. Apologies.
I have no idea what caused that. I'll check my windows machine for viruses. Or could?it have some other cause? Perhaps I should also change my password.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/72db124d/attachment.htm>

From rabidpoobear at gmail.com  Mon Nov 16 09:56:37 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Mon, 16 Nov 2009 02:56:37 -0600
Subject: [Tutor] Writing code while tired, counterproductive?
In-Reply-To: <c7c6f3bc0911152257l23e5de3dk5ba9fdb01828b025@mail.gmail.com>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
	<hdmuil$cof$1@ger.gmane.org>
	<c7c6f3bc0911152257l23e5de3dk5ba9fdb01828b025@mail.gmail.com>
Message-ID: <dfeb4470911160056p38a52b8eh89be83400efa467b@mail.gmail.com>

I hate to be the odd one out here, but I actually find that I am extremely
productive when I'm tired.  It's easier for me to commit completely to the
code, when I'm well-rested I dream about running through fields of
sunflowers and such, get distracted more easily.  The code I write when I'm
tired is usually of marginally worse quality, but it's usually easy to audit
it when I'm rested and fix any problems.  Although if I'm trying to solve
something particularly difficult, especially something I've never done
before, sometimes I won't be able to do it until I wake up a bit.  Note that
this is for being tired, not exhausted.  If I'm exhausted I write a whole
lot of awful code that I have to completely rewrite when I wake up.

On Mon, Nov 16, 2009 at 12:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:

> >From first-hand experience, i would concur :)
>
> A refreshed mind will perform much better than an over-exerted one.
>
>
> On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>>
>> "Modulok" <modulok at gmail.com> wrote
>>
>>  Does anyone else find, writing code while tired to be counterproductive?
>>>
>>
>> Yes. Doing anything that is mentally taxing is usually a bad idea when
>> tired!
>>
>> Alan G
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Regards,
> Lloyd
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/554409c0/attachment-0001.htm>

From timomlists at gmail.com  Mon Nov 16 10:03:04 2009
From: timomlists at gmail.com (Timo)
Date: Mon, 16 Nov 2009 10:03:04 +0100
Subject: [Tutor] nul file in Windows
In-Reply-To: <4B000C56.8090205@ieee.org>
References: <d0ed98100911150439u169630sf605f6cbf8021e0b@mail.gmail.com>
	<4B000C56.8090205@ieee.org>
Message-ID: <4B011548.7000102@gmail.com>

Dave Angel schreef:
> Timo List wrote:
>> For my program I disable the py2exe log feature by routing output to the
>> nul-file.
>> Code:
>>
>>         if win32 and py2exe:
>>             sys.stdout = open("nul", "w")
>>             sys.stderr = open("nul", "w")
>>
>> This always worked fine.
>>
>> Today, I received an email from a user with the following error:
>> IOError: [Errno 2] No such file or directory: 'nul'
>>
>> Now, I thought the nul-file always existed, shouldn't it?
>> Is there another way to disable output, if this one fails?
>>
>> Cheers,
>> Timo
>>
>>   
> All you need is an object that behaves like a file, but does nothing 
> with the data sent to it.  That's what duck-typing is all about.
>
> I haven't tried it, but I'd start by making a new class:
>
> class  NullFile(object):
>    def __init__(self, *arg, **kwarg):
>             pass
>    def write(self, data, *arg, **kwarg):
>            pass
>     def close(self, *arg, **kwarg):
>           pass
>
> and just say
>    sys.stdout = NullFile()
Thanks, this is what I was looking for.

>
> If you get any exceptions, you could add new methods to this file, 
> accordingly.
I already catch exceptions with my own function:
    sys.excepthook = self.exception_hook

Disabling stdout and stderr is to prevent py2exe from generating it's 
own logreport on exceptions.

Cheers,
Timo

>
>
> DaveA
>


From alan.gauld at btinternet.com  Mon Nov 16 10:18:59 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 16 Nov 2009 09:18:59 -0000
Subject: [Tutor] Unexpected iterator
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com><4AFBD794.3020101@gmail.com><b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com><hdq0aq$p1c$1@ger.gmane.org>
	<20091116061727.7102cfa5@o>
Message-ID: <hdr5eg$fhs$1@ger.gmane.org>


"spir" <denis.spir at free.fr> wrote

> Unpacking is rarely needed. It matches some kind of problems.
...
> (parens not needed) It's just an elegant manner to avoid indexing --  
> right?

Regarding tuple unpacking...
Absolutely, its a convenience.

Regarding struct unpacking, its an absolute necessity in getting data
out of a binary stream.

Alan G 



From sanelson at gmail.com  Mon Nov 16 11:58:30 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 10:58:30 +0000
Subject: [Tutor] GzipFile has no attribute '__exit__'
Message-ID: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>

I'm trying to write a gzipped file on the fly:

merged_log = merge(*logs)

with gzip.open('/tmp/merged_log.gz', 'w') as output:
    for stamp, line in merged_log:
        output.write(line)

But I'm getting:

Traceback (most recent call last):
  File "./magpie.py", line 72, in <module>
    with gzip.open('/tmp/merged_log.gz', 'w') as output:
AttributeError: GzipFile instance has no attribute '__exit__'

What am I doing wrong, and how do I put it right?

S.

From kent37 at tds.net  Mon Nov 16 12:34:54 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 06:34:54 -0500
Subject: [Tutor] Unexpected iterator
In-Reply-To: <20091116061727.7102cfa5@o>
References: <bccc8ac80911120103r439889a3qcb4a8e8ac9610@mail.gmail.com>
	<4AFBD794.3020101@gmail.com>
	<b6131fdc0911150712o76902612v6f7e5815d62d0ac6@mail.gmail.com>
	<hdq0aq$p1c$1@ger.gmane.org> <20091116061727.7102cfa5@o>
Message-ID: <1c2a2c590911160334q66954a18hd227893d40781686@mail.gmail.com>

On Mon, Nov 16, 2009 at 12:17 AM, spir <denis.spir at free.fr> wrote:
> Le Sun, 15 Nov 2009 19:23:33 -0000,
> "Alan Gauld" <alan.gauld at btinternet.com> s'exprima ainsi:
>
>> What does 'unpack' mean? ?I've seen a few Python errors about packing
>> and unpacking. ?What does it mean?
>
> Unpacking is rarely needed. It matches some kind of problems.

Not needed, I suppose, since there is another way to write the code,
but using tuple unpacking can greatly reduce the length and improve
the readability of your code.

> Imagine you parse "codes" each made of name-sep-number. Then when walking through the result you can write:
> for code in codes:
> ? ? ? ?(name,sep,number) = code

Or even
  for name, sep, number in codes:
    # Do something with name, sep, number

> It's just an elegant manner to avoid indexing -- right?

It avoids indexing and gives meaningful names to values. Compare the
above with the alternatives:
  for code in codes:
    name = code[0]
    sep = code[1]
    number = code[2]
    # Do something with name, sep, number

or

  for code in codes:
    # Do something with code[0], code[1], code[2]

The first alternative is much more verbose while the second one is
much harder to understand.

Kent

From bibsmendez at gmail.com  Mon Nov 16 12:36:35 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Mon, 16 Nov 2009 06:36:35 -0500
Subject: [Tutor] I love python / you guys :)
In-Reply-To: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
References: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
Message-ID: <f16f1f8c0911160336w2801f228q4f8f1c766c85b24f@mail.gmail.com>

On Mon, Nov 16, 2009 at 1:58 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:

> hi,
>
> Although not a question, i just want to tell you guys how awesome you are!
>
> I am not a programmer, i can do a bit of bash. I have never officially
> learnt programming, but numerous times looked at some perl, c, java
> and never really gotten past the beginning stages of it. That all
> changed when i picked up python. Although my style and use of python
> is probably barbaric at best, I really enjoy it when you can make
> things work. Python was just amazing from a readability / logical
> point of view. If i can think of something, there is a way to do it in
> python. After learning the simple data structures it seems i can
> really do anything i want to.
>


Hi Stefan,

Your message mirrored my current state. There's TONS to learn in linux like
bash, perl, vim, sed, awk, python, etc. Too much to put in your head and in
the end makes you half-baked e.g. good at start but fades away in the end.
Anyway i hope to follow your lead and be one among the guys here
knowledgeable of the language.

In the same lines of if-you-can-think-of-anything-python-can-do-it i got
inspired to ask a question for the gurus:

When i use our company's LAN i set my proxy variable by hand in .bashrc.
There are 4 files to insert proxy variable:

in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.

The last one is actually rename e.g. mv to apt.conf to activate proxy and mv
to apt.conf.bak to deactivate. The proxy variable is something like this

export http_proxy=http://username:password at proxy:port
ftp_proxy=$http_proxy

To activate i uncomment them then source .bashrc. To deactivate i put back
the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
apt.conf see rename above. I deactivate because i have another internet
connection option via 3G usb modem. But thats another story.

I will do this myself in python so please show me the way. Surely this can
be done.



-- 
Best Regards,
bibimidi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/288c84ea/attachment.htm>

From kent37 at tds.net  Mon Nov 16 12:41:38 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 06:41:38 -0500
Subject: [Tutor] GzipFile has no attribute '__exit__'
In-Reply-To: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
References: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
Message-ID: <1c2a2c590911160341v338f2f5axfc7afd0dab4e2b99@mail.gmail.com>

On Mon, Nov 16, 2009 at 5:58 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> I'm trying to write a gzipped file on the fly:
>
> merged_log = merge(*logs)
>
> with gzip.open('/tmp/merged_log.gz', 'w') as output:
> ? ?for stamp, line in merged_log:
> ? ? ? ?output.write(line)
>
> But I'm getting:
>
> Traceback (most recent call last):
> ?File "./magpie.py", line 72, in <module>
> ? ?with gzip.open('/tmp/merged_log.gz', 'w') as output:
> AttributeError: GzipFile instance has no attribute '__exit__'
>
> What am I doing wrong, and how do I put it right?

gzip.open() doesn't support the context manager protocol needed for it
to be used in a 'with' statement. Just do this old-style:

output = gzip.open('/tmp/merged_log.gz', 'w')
try:
   for stamp, line in merged_log:
       output.write(line)
finally:
  output.close()

See PEP 343 this explanation of context managers if you want to know
why you got this specific error:
effbot.org/zone/python-with-statement.htm
http://www.python.org/dev/peps/pep-0343/

Kent

From davea at ieee.org  Mon Nov 16 12:54:49 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 16 Nov 2009 06:54:49 -0500
Subject: [Tutor] GzipFile has no attribute '__exit__'
In-Reply-To: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
References: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
Message-ID: <4B013D89.60409@ieee.org>

Stephen Nelson-Smith wrote:
> I'm trying to write a gzipped file on the fly:
>
> merged_log = merge(*logs)
>
> with gzip.open('/tmp/merged_log.gz', 'w') as output:
>     for stamp, line in merged_log:
>         output.write(line)
>
> But I'm getting:
>
> Traceback (most recent call last):
>   File "./magpie.py", line 72, in <module>
>     with gzip.open('/tmp/merged_log.gz', 'w') as output:
> AttributeError: GzipFile instance has no attribute '__exit__'
>
> What am I doing wrong, and how do I put it right?
>
> S.
>
>   
(I get the same result in CPython 2.6, but you should have told your 
python version and OS, just so people can tell you version-specific 
problems.)

In order to use the "with" syntax with an object of a particular class, 
that class has to support the Context Manager protocol, which includes 
both __enter__() and __exit__() methods.  I don't know why the system 
checks first for __exit__(), but there you have it.

GzipFile() class doesn't have such methods (in version 2.6), and 
therefore doesn't support the Context Manager.  So you have to do it the 
"old fashioned" way, with explicit close() method, and a 
try/except/finally.  And unlike regular text files, I don't expect 
you'll find a new file at all usable, if it wasn't properly closed.


Alternatively, you could subclass it, and write your own.  At a minimum, 
the __exit__() method should close() the stream.

DaveA


From stefan at lsd.co.za  Mon Nov 16 12:57:17 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Mon, 16 Nov 2009 13:57:17 +0200
Subject: [Tutor] proxy switcher - was Re:  I love python / you guys :)
Message-ID: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>

On Mon, Nov 16, 2009 at 1:36 PM, bibi midi <bibsmendez at gmail.com> wrote:
>
>
> On Mon, Nov 16, 2009 at 1:58 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:
>>
>> hi,
>>
>> Although not a question, i just want to tell you guys how awesome you are!
>>
>> I am not a programmer, i can do a bit of bash. I have never officially
>> learnt programming, but numerous times looked at some perl, c, java
>> and never really gotten past the beginning stages of it. That all
>> changed when i picked up python. Although my style and use of python
>> is probably barbaric at best, I really enjoy it when you can make
>> things work. Python was just amazing from a readability / logical
>> point of view. If i can think of something, there is a way to do it in
>> python. After learning the simple data structures it seems i can
>> really do anything i want to.
>
>
> Hi Stefan,
>
> Your message mirrored my current state. There's TONS to learn in linux like
> bash, perl, vim, sed, awk, python, etc. Too much to put in your head and in
> the end makes you half-baked e.g. good at start but fades away in the end.
> Anyway i hope to follow your lead and be one among the guys here
> knowledgeable of the language.
>
> In the same lines of if-you-can-think-of-anything-python-can-do-it i got
> inspired to ask a question for the gurus:
>
> When i use our company's LAN i set my proxy variable by hand in .bashrc.
> There are 4 files to insert proxy variable:
>
> in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
>
> The last one is actually rename e.g. mv to apt.conf to activate proxy and mv
> to apt.conf.bak to deactivate. The proxy variable is something like this
>
> export http_proxy=http://username:password at proxy:port
> ftp_proxy=$http_proxy
>
> To activate i uncomment them then source .bashrc. To deactivate i put back
> the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
> apt.conf see rename above. I deactivate because i have another internet
> connection option via 3G usb modem. But thats another story.
>
> I will do this myself in python so please show me the way. Surely this can
> be done.
>

Heys,

I do something similair with apt.conf. Below is an older version of an
app i wrote (the new version i have written integrates into network
manager, so when it detects a certain network, it switches all the
corresponding settings!).

This older version has some functions that you might be interested.
Maybe something below helps you.



#!/usr/bin/python

import sys
import fileinput
import re
import netifaces
import glob
import string

from netaddr import IP, CIDR

networks = {}
networks["home"] = {"ip":"192.168.1.0/24","url":"http://192.168.1.2:3142/"}
#networks["home"] =
{"ip":"192.168.1.0/24","url":"http://ecstacy.lsd.co.za:3142/"}
networks["office"] =
{"ip":"172.30.16.16/20","url":"http://ecstacy.lsd.co.za:3142/"}

#Things to keep in sync
sync=["/etc/apt/sources.list","/etc/apt/sources.list.d/*.list"]
#sync=["/etc/apt/sources.list"]

#Search interfaces for IP and compare against variable list
def netcheck():
	for interface in netifaces.interfaces():
		iface = netifaces.ifaddresses(interface)
		if netifaces.AF_INET in iface:
			networkaddr = '%s/%s' %
(netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'],
netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask'])
			networkaddr = IP(networkaddr).cidr()
			print 'networkaddr: %s' % networkaddr
			for name, values in networks.iteritems():
				network = IP(values['ip']).cidr()
				print 'network: %s' % network
				if networkaddr in network:
					location = networks[name]['url']
					print 'Location \'%s\' found. Setting proxy to \'%s\'' % (name,location)
			
#Function to hash out mirrors based on regex - may be useful for
enabling/disabling src package downloads
#Not currently used
def hash():
    for line in fileinput.input("test.list", inplace=1):
        line = line.strip()
        if re.search('^deb', line):
            print '#%s' % line
        else:
            print line

#Function to check if sync list actually exists (maybe should check
for writable?)
def checksync():
    for file in sync:
        if not glob.glob(file):
            print ("Error: %s does not exist" %file)
            sys.exit(1)

#Function to replace mirror
def replace(location=False):
    for file in sync:
        for line in fileinput.input(glob.glob(file), inplace=1):
            line = line.strip()

            if location:
                if re.search('^deb.*http://(.*):3142/', line):
                    print re.sub('http://(.*):3142/', location, line)
                elif re.search('^deb.*http://', line):
		    print re.sub('http://', location, line)
	        else:
		    print line
            elif re.search('^deb.*http://(.*):3142/', line):
	        print re.sub('http://(.*):3142/', 'http://', line)
	    else:
	        print line
	
checksync()
replace('http://ecstacy.lsd.co.za:3142/')
#replace()

From sander.sweers at gmail.com  Mon Nov 16 13:15:32 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Mon, 16 Nov 2009 13:15:32 +0100
Subject: [Tutor] GzipFile has no attribute '__exit__'
In-Reply-To: <4B013D89.60409@ieee.org>
References: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
	<4B013D89.60409@ieee.org>
Message-ID: <b65fbb130911160415n1861860bif36d6afcee3ad918@mail.gmail.com>

2009/11/16 Dave Angel <davea at ieee.org>:
> Alternatively, you could subclass it, and write your own. ?At a minimum, the
> __exit__() method should close() the stream.

This triggered my to dig into this a bit. This is not fixed untill
python 3.1 but seems easilly added to the ZipFile class. My attempt to
backport this from python 3.1's gzip.py below seems to work.

Greets
Sander

import gzip

class myGzipFile(gzip.GzipFile):
    def __enter__(self):
        if self.fileobj is None:
            raise ValueError("I/O operation on closed GzipFile object")
        return self

    def __exit__(self, *args):
        self.close()

zfilepath = r'C:\test.gz'
s = 'This is a test'

with myGzipFile(zfilepath,'w') as output:
    output.write(s)

From fomcl at yahoo.com  Mon Nov 16 13:17:51 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Mon, 16 Nov 2009 04:17:51 -0800 (PST)
Subject: [Tutor] Writing code while tired, counterproductive?
In-Reply-To: <dfeb4470911160056p38a52b8eh89be83400efa467b@mail.gmail.com>
Message-ID: <940960.30630.qm@web110706.mail.gq1.yahoo.com>

I find that switching to a completely different approach or strategy becomes more difficult when tired. Depending on the context, that could be called persistence of perseverence (begin good or bad, respectively).?However, in my opinion, not being able to view things from a different angle is usually counterproductive.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 11/16/09, Luke Paireepinart <rabidpoobear at gmail.com> wrote:


From: Luke Paireepinart <rabidpoobear at gmail.com>
Subject: Re: [Tutor] Writing code while tired, counterproductive?
To: "OkaMthembo" <zebra05 at gmail.com>
Cc: tutor at python.org
Date: Monday, November 16, 2009, 9:56 AM


I hate to be the odd one out here, but I actually find that I am extremely productive when I'm tired.? It's easier for me to commit completely to the code, when I'm well-rested I dream about running through fields of sunflowers and such, get distracted more easily.? The code I write when I'm tired is usually of marginally worse quality, but it's usually easy to audit it when I'm rested and fix any problems.? Although if I'm trying to solve something particularly difficult, especially something I've never done before, sometimes I won't be able to do it until I wake up a bit.? Note that this is for being tired, not exhausted.? If I'm exhausted I write a whole lot of awful code that I have to completely rewrite when I wake up.


On Mon, Nov 16, 2009 at 12:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:

>From first-hand experience, i would concur :)

A refreshed mind will perform much better than an over-exerted one.





On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:


"Modulok" <modulok at gmail.com> wrote


Does anyone else find, writing code while tired to be counterproductive?

Yes. Doing anything that is mentally taxing is usually a bad idea when tired!

Alan G

_______________________________________________
Tutor maillist ?- ?Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



-- 
Regards,
Lloyd

_______________________________________________
Tutor maillist ?- ?Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



-----Inline Attachment Follows-----


_______________________________________________
Tutor maillist? -? Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/aaf52501/attachment.htm>

From rabidpoobear at gmail.com  Mon Nov 16 13:25:34 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Mon, 16 Nov 2009 06:25:34 -0600
Subject: [Tutor] I love python / you guys :)
In-Reply-To: <dfeb4470911160424p4aa36063m9f9cfbc5f3efb742@mail.gmail.com>
References: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
	<f16f1f8c0911160336w2801f228q4f8f1c766c85b24f@mail.gmail.com>
	<dfeb4470911160424p4aa36063m9f9cfbc5f3efb742@mail.gmail.com>
Message-ID: <dfeb4470911160425y72f0a251mbe83771a324b8168@mail.gmail.com>

Accidental off-list reply.
On Mon, Nov 16, 2009 at 5:36 AM, bibi midi <bibsmendez at gmail.com> wrote:

>
> In the same lines of if-you-can-think-of-anything-python-can-do-it i got
> inspired to ask a question for the gurus:
>
> When i use our company's LAN i set my proxy variable by hand in .bashrc.
> There are 4 files to insert proxy variable:
>
> in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
>
> The last one is actually rename e.g. mv to apt.conf to activate proxy and
> mv to apt.conf.bak to deactivate. The proxy variable is something like this
>
> export http_proxy=http://username:password at proxy:port
> ftp_proxy=$http_proxy
>
> To activate i uncomment them then source .bashrc. To deactivate i put back
> the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
> apt.conf see rename above. I deactivate because i have another internet
> connection option via 3G usb modem. But thats another story.
>
> I will do this myself in python so please show me the way. Surely this can
> be done.
>
> Sure it can.

This is actually fairly easy to do.  I would just use regular expressions to
match the lines and comment/uncomment them  depending on if you want to
enable/disable.


####################
Spoiler alert! don't read if you want to solve it yourself.
####################

#### Remove/Add comments to any line that matches a specified regular
expression.
##    comments are assumed to be the '#' symbol.
#### Warning - completely untested code.

import re
regex =  # remove leading spaces and the # comment symbol from a line, if it
exists.

def uncomment(filename, regex, newfile):
    remove_comments_regex = ' *#*(.*$)'
    for line in open(filename):
        if re.match(regex, line): # if we have a matching line we should
remove the comment.
            newfile.write(re.match(remove_comments_regex, line).groups()[0])

def comment(filename, regex, newfile):
    for line in open(filename):
        if re.match(regex, line):
            #avoid dual-commenting
            if line.strip().startswith("#"):
                newfile.write(line)
            else:
                newfile.write('#' + line)

####################
End of spoiler
####################


As for renaming, look into the os module, there's an easy function for
renaming in there.  Just add a check around the rename function for an
os.path.exists(...) so you can ensure the file exists before you attempt to
rename it, otherwise you might get an exception (or you could just try
renaming it and just catch the exception, either way.)   Then just make a
.py script that can enable and one that can disable, and make sure tehre's
no harm if you run either one multiple times consecutively.

Hope that helps, and yes, we know you guys appreciate the help, that's why
we do it!  We certainly don't get paid anything.  It's nice to hear you say
it anyway though, so thanks!
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/b59758f5/attachment.htm>

From kent37 at tds.net  Mon Nov 16 13:39:33 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 07:39:33 -0500
Subject: [Tutor] GzipFile has no attribute '__exit__'
In-Reply-To: <b65fbb130911160415n1861860bif36d6afcee3ad918@mail.gmail.com>
References: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
	<4B013D89.60409@ieee.org>
	<b65fbb130911160415n1861860bif36d6afcee3ad918@mail.gmail.com>
Message-ID: <1c2a2c590911160439g30a5892etca96926b3c9f64a8@mail.gmail.com>

On Mon, Nov 16, 2009 at 7:15 AM, Sander Sweers <sander.sweers at gmail.com> wrote:

> This triggered my to dig into this a bit. This is not fixed untill
> python 3.1 but seems easilly added to the ZipFile class. My attempt to
> backport this from python 3.1's gzip.py below seems to work.
>
> Greets
> Sander
>
> import gzip
>
> class myGzipFile(gzip.GzipFile):
> ? ?def __enter__(self):
> ? ? ? ?if self.fileobj is None:
> ? ? ? ? ? ?raise ValueError("I/O operation on closed GzipFile object")
> ? ? ? ?return self
>
> ? ?def __exit__(self, *args):
> ? ? ? ?self.close()

You might want to report this as a bug against 2.6 and submit this
change as a proposed fix. It's easy to do, see
http://www.python.org/dev/patches/

You could submit your change right in the body of the report if you
don't want to make a diff file. You could suggest an update to the
docs as well.

Kent

From ma_development at hotmail.com  Mon Nov 16 13:51:38 2009
From: ma_development at hotmail.com (Mario Cavett)
Date: Mon, 16 Nov 2009 07:51:38 -0500
Subject: [Tutor] Tutor Digest, Vol 69, Issue 73
In-Reply-To: <mailman.1880.1258375178.2872.tutor@python.org>
References: <mailman.1880.1258375178.2872.tutor@python.org>
Message-ID: <SNT106-W204DE5CBDCB25C858906C4EFA50@phx.gbl>


I'm sorry I haven't been able to reply topython tutor, I was wondering couldyou guys send me some beginner issuesthat get straight to  basic problems. Beginners have when starting pythonand using it as a scripting language. And nothing else because I'm thinking thats the only way to use it when game designing.please reply
> From: tutor-request at python.org
> Subject: Tutor Digest, Vol 69, Issue 73
> To: tutor at python.org
> Date: Mon, 16 Nov 2009 13:39:38 +0100
> 
> Send Tutor mailing list submissions to
> 	tutor at python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> 	tutor-request at python.org
> 
> You can reach the person managing the list at
> 	tutor-owner at python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>    1. Re: GzipFile has no attribute '__exit__' (Sander Sweers)
>    2. Re: Writing code while tired, counterproductive?
>       (Albert-Jan Roskam)
>    3. Re: I love python / you guys :) (Luke Paireepinart)
>    4. Re: GzipFile has no attribute '__exit__' (Kent Johnson)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Mon, 16 Nov 2009 13:15:32 +0100
> From: Sander Sweers <sander.sweers at gmail.com>
> To: Dave Angel <davea at ieee.org>
> Cc: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] GzipFile has no attribute '__exit__'
> Message-ID:
> 	<b65fbb130911160415n1861860bif36d6afcee3ad918 at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
> 
> 2009/11/16 Dave Angel <davea at ieee.org>:
> > Alternatively, you could subclass it, and write your own. ?At a minimum, the
> > __exit__() method should close() the stream.
> 
> This triggered my to dig into this a bit. This is not fixed untill
> python 3.1 but seems easilly added to the ZipFile class. My attempt to
> backport this from python 3.1's gzip.py below seems to work.
> 
> Greets
> Sander
> 
> import gzip
> 
> class myGzipFile(gzip.GzipFile):
>     def __enter__(self):
>         if self.fileobj is None:
>             raise ValueError("I/O operation on closed GzipFile object")
>         return self
> 
>     def __exit__(self, *args):
>         self.close()
> 
> zfilepath = r'C:\test.gz'
> s = 'This is a test'
> 
> with myGzipFile(zfilepath,'w') as output:
>     output.write(s)
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Mon, 16 Nov 2009 04:17:51 -0800 (PST)
> From: Albert-Jan Roskam <fomcl at yahoo.com>
> To: OkaMthembo <zebra05 at gmail.com>, Luke Paireepinart
> 	<rabidpoobear at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Writing code while tired, counterproductive?
> Message-ID: <940960.30630.qm at web110706.mail.gq1.yahoo.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> I find that switching to a completely different approach or strategy becomes more difficult when tired. Depending on the context, that could be called persistence of perseverence (begin good or bad, respectively).?However, in my opinion, not being able to view things from a different angle is usually counterproductive.
> 
> Cheers!!
> Albert-Jan
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In the face of ambiguity, refuse the temptation to guess.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> --- On Mon, 11/16/09, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> 
> 
> From: Luke Paireepinart <rabidpoobear at gmail.com>
> Subject: Re: [Tutor] Writing code while tired, counterproductive?
> To: "OkaMthembo" <zebra05 at gmail.com>
> Cc: tutor at python.org
> Date: Monday, November 16, 2009, 9:56 AM
> 
> 
> I hate to be the odd one out here, but I actually find that I am extremely productive when I'm tired.? It's easier for me to commit completely to the code, when I'm well-rested I dream about running through fields of sunflowers and such, get distracted more easily.? The code I write when I'm tired is usually of marginally worse quality, but it's usually easy to audit it when I'm rested and fix any problems.? Although if I'm trying to solve something particularly difficult, especially something I've never done before, sometimes I won't be able to do it until I wake up a bit.? Note that this is for being tired, not exhausted.? If I'm exhausted I write a whole lot of awful code that I have to completely rewrite when I wake up.
> 
> 
> On Mon, Nov 16, 2009 at 12:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> 
> >From first-hand experience, i would concur :)
> 
> A refreshed mind will perform much better than an over-exerted one.
> 
> 
> 
> 
> 
> On Sat, Nov 14, 2009 at 8:57 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> 
> 
> "Modulok" <modulok at gmail.com> wrote
> 
> 
> Does anyone else find, writing code while tired to be counterproductive?
> 
> Yes. Doing anything that is mentally taxing is usually a bad idea when tired!
> 
> Alan G
> 
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> -- 
> Regards,
> Lloyd
> 
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> -----Inline Attachment Follows-----
> 
> 
> _______________________________________________
> Tutor maillist? -? Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
>       
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/aaf52501/attachment-0001.htm>
> 
> ------------------------------
> 
> Message: 3
> Date: Mon, 16 Nov 2009 06:25:34 -0600
> From: Luke Paireepinart <rabidpoobear at gmail.com>
> To: "[tutor python]" <tutor at python.org>
> Subject: Re: [Tutor] I love python / you guys :)
> Message-ID:
> 	<dfeb4470911160425y72f0a251mbe83771a324b8168 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Accidental off-list reply.
> On Mon, Nov 16, 2009 at 5:36 AM, bibi midi <bibsmendez at gmail.com> wrote:
> 
> >
> > In the same lines of if-you-can-think-of-anything-python-can-do-it i got
> > inspired to ask a question for the gurus:
> >
> > When i use our company's LAN i set my proxy variable by hand in .bashrc.
> > There are 4 files to insert proxy variable:
> >
> > in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
> >
> > The last one is actually rename e.g. mv to apt.conf to activate proxy and
> > mv to apt.conf.bak to deactivate. The proxy variable is something like this
> >
> > export http_proxy=http://username:password at proxy:port
> > ftp_proxy=$http_proxy
> >
> > To activate i uncomment them then source .bashrc. To deactivate i put back
> > the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
> > apt.conf see rename above. I deactivate because i have another internet
> > connection option via 3G usb modem. But thats another story.
> >
> > I will do this myself in python so please show me the way. Surely this can
> > be done.
> >
> > Sure it can.
> 
> This is actually fairly easy to do.  I would just use regular expressions to
> match the lines and comment/uncomment them  depending on if you want to
> enable/disable.
> 
> 
> ####################
> Spoiler alert! don't read if you want to solve it yourself.
> ####################
> 
> #### Remove/Add comments to any line that matches a specified regular
> expression.
> ##    comments are assumed to be the '#' symbol.
> #### Warning - completely untested code.
> 
> import re
> regex =  # remove leading spaces and the # comment symbol from a line, if it
> exists.
> 
> def uncomment(filename, regex, newfile):
>     remove_comments_regex = ' *#*(.*$)'
>     for line in open(filename):
>         if re.match(regex, line): # if we have a matching line we should
> remove the comment.
>             newfile.write(re.match(remove_comments_regex, line).groups()[0])
> 
> def comment(filename, regex, newfile):
>     for line in open(filename):
>         if re.match(regex, line):
>             #avoid dual-commenting
>             if line.strip().startswith("#"):
>                 newfile.write(line)
>             else:
>                 newfile.write('#' + line)
> 
> ####################
> End of spoiler
> ####################
> 
> 
> As for renaming, look into the os module, there's an easy function for
> renaming in there.  Just add a check around the rename function for an
> os.path.exists(...) so you can ensure the file exists before you attempt to
> rename it, otherwise you might get an exception (or you could just try
> renaming it and just catch the exception, either way.)   Then just make a
> .py script that can enable and one that can disable, and make sure tehre's
> no harm if you run either one multiple times consecutively.
> 
> Hope that helps, and yes, we know you guys appreciate the help, that's why
> we do it!  We certainly don't get paid anything.  It's nice to hear you say
> it anyway though, so thanks!
> -Luke
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/b59758f5/attachment-0001.htm>
> 
> ------------------------------
> 
> Message: 4
> Date: Mon, 16 Nov 2009 07:39:33 -0500
> From: Kent Johnson <kent37 at tds.net>
> To: Sander Sweers <sander.sweers at gmail.com>
> Cc: Python Tutor mailing list <tutor at python.org>, Dave Angel
> 	<davea at ieee.org>
> Subject: Re: [Tutor] GzipFile has no attribute '__exit__'
> Message-ID:
> 	<1c2a2c590911160439g30a5892etca96926b3c9f64a8 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> On Mon, Nov 16, 2009 at 7:15 AM, Sander Sweers <sander.sweers at gmail.com> wrote:
> 
> > This triggered my to dig into this a bit. This is not fixed untill
> > python 3.1 but seems easilly added to the ZipFile class. My attempt to
> > backport this from python 3.1's gzip.py below seems to work.
> >
> > Greets
> > Sander
> >
> > import gzip
> >
> > class myGzipFile(gzip.GzipFile):
> > ? ?def __enter__(self):
> > ? ? ? ?if self.fileobj is None:
> > ? ? ? ? ? ?raise ValueError("I/O operation on closed GzipFile object")
> > ? ? ? ?return self
> >
> > ? ?def __exit__(self, *args):
> > ? ? ? ?self.close()
> 
> You might want to report this as a bug against 2.6 and submit this
> change as a proposed fix. It's easy to do, see
> http://www.python.org/dev/patches/
> 
> You could submit your change right in the body of the report if you
> don't want to make a diff file. You could suggest an update to the
> docs as well.
> 
> Kent
> 
> 
> ------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> End of Tutor Digest, Vol 69, Issue 73
> *************************************
 		 	   		  
_________________________________________________________________
Windows 7: It works the way you want. Learn more.
http://www.microsoft.com/Windows/windows-7/default.aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009v2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/09a5076a/attachment-0001.htm>

From sanelson at gmail.com  Mon Nov 16 14:35:49 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 13:35:49 +0000
Subject: [Tutor] I love python / you guys :)
In-Reply-To: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
References: <5cb309e70911152258q1644b8f1gb30e58dc9c2bda86@mail.gmail.com>
Message-ID: <b6131fdc0911160535p529c1c59mecdc32f944d4446b@mail.gmail.com>

Hello all,

On Mon, Nov 16, 2009 at 6:58 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:
> hi,
>
> Although not a question, i just want to tell you guys how awesome you are!

+1

I've been a happy member of this list for years, even though I've
taken a 3 year Ruby sabbatical!

I've always found it to be full of invaluable advice, and is, in my
opinion, a real gem in Python's crown.  It's one of the reasons I feel
so confident in recommending python to anyone - they can be guaranteed
a friendly and informational welcome on this list.

Thank you very much - it is greatly appreciated.

S.

From sanelson at gmail.com  Mon Nov 16 15:30:42 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 14:30:42 +0000
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
Message-ID: <b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>

Hi,

>> When i use our company's LAN i set my proxy variable by hand in .bashrc.
>> There are 4 files to insert proxy variable:
>>
>> in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
>>
>> The last one is actually rename e.g. mv to apt.conf to activate proxy and mv
>> to apt.conf.bak to deactivate. The proxy variable is something like this
>>
>> export http_proxy=http://username:password at proxy:port
>> ftp_proxy=$http_proxy
>>
>> To activate i uncomment them then source .bashrc. To deactivate i put back
>> the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
>> apt.conf see rename above. I deactivate because i have another internet
>> connection option via 3G usb modem. But thats another story.
>>
>> I will do this myself in python so please show me the way. Surely this can
>> be done.

Here's what I knocked up over lunch.  It doesn't cover the moving of
the file, I don't like that it's deep-nested, and I've not tested it,
but I welcome criticism and feedback:

files = ['file1', 'file2', 'file3', 'file4']
settings = ['export http_proxy=', 'ftp_proxy=']

for file in files:
  with open(file, 'rw') as file:
    for line in file:
      for setting in settings:
        if setting in line:
          if line[0] == '#':
            line = line[1:]
          else:
            line =  '#' + line
        output.write(line)

S.

From rabidpoobear at gmail.com  Mon Nov 16 15:52:34 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Mon, 16 Nov 2009 08:52:34 -0600
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
Message-ID: <dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>

> Here's what I knocked up over lunch.  It doesn't cover the moving of
> the file, I don't like that it's deep-nested, and I've not tested it,
> but I welcome criticism and feedback:
>
> files = ['file1', 'file2', 'file3', 'file4']
> settings = ['export http_proxy=', 'ftp_proxy=']
>
> for file in files:
>  with open(file, 'rw') as file:
>

Does this actually work?
I didn't think you could open files as reading and writing?

also you're shadowing the 'file' var, which might be confusing to beginners,
as it's not the same file as in the outer for loop.
Also, 'with' is a 3.0 keyword, right?  That's pretty interesting syntax,
I've never seen it before, but I haven't used 3.x yet.


>    for line in file:
>      for setting in settings:
>        if setting in line:
>          if line[0] == '#':
>            line = line[1:]
>          else:
>            line =  '#' + line
>        output.write(line)
>

Also, you didn't define 'output' anywhere.  Is this an implicit declaration
via the 'with' syntax?

Thanks,
-Luke

>
>
S.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/1d1cec20/attachment.htm>

From mrholtsr at sbcglobal.net  Mon Nov 16 16:56:07 2009
From: mrholtsr at sbcglobal.net (Ray Holt)
Date: Mon, 16 Nov 2009 10:56:07 -0500
Subject: [Tutor] Help on finding the 1000th prime
Message-ID: <8E81A526E0444D1DAC483D86D354C680@ray>

I have posted this on other lists, but have just discovered this one. Can
someone give me help on writing the code necessary to find the 1000th. prime
number. I know I will have to use a while loop, but I can't seem to get the
body of the code to function rightly. I would appreciate any help. I am
taking the online course Introduction to Computer Science and Programming
from the MIT website and this is one of their assignments. The on campus
students are able to collaborate with one another, but I live in Ohio.
Thanks, Ray 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/09eabad1/attachment.htm>

From christopher.henk at allisontransmission.com  Mon Nov 16 18:28:09 2009
From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com)
Date: Mon, 16 Nov 2009 12:28:09 -0500
Subject: [Tutor] Help on finding the 1000th prime
In-Reply-To: <8E81A526E0444D1DAC483D86D354C680@ray>
Message-ID: <OF158C4FB8.CDE4CFD4-ON85257670.005F6048-85257670.005FEDF2@mail.ati.int>

mrholtsr at sbcglobal.net wrote on 11/16/2009 10:56:07 AM:

> I have posted this on other lists, but have just discovered this one. 

        Welcome to the list.  I am sure you will find plenty of folks here 
who will be happy to help you.

> Can someone give me help on writing the code 
> necessary to find the 1000th. prime number.

        I seem to recall this problem was recently discussed on the list. 
If you do a quick search you should be able to find a few pointers on one 
way to solve it. 

> I know I will have to use a while loop, but I can't seem to get the body 

> of the code to function rightly. I would appreciate any help. 

        The way this list works you will have to give us something to work 
from.  Show us what you tried, how it failed (any error messages, etc), 
what you re thinking, and where you are stuck.  We won't do the work for 
you but try and lead you to the answers. 


> I am taking the online course Introduction to Computer 
> Science and Programming from the MIT website and this is one of their 
assignments. The on campus students are able to 
> collaborate with one another, but I live in Ohio. Thanks, Ray 
_______________________________________________

        Good luck with the class, and feel free to ask questions here.

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/7d911a42/attachment.htm>

From kent37 at tds.net  Mon Nov 16 18:33:08 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 12:33:08 -0500
Subject: [Tutor] Help on finding the 1000th prime
In-Reply-To: <8E81A526E0444D1DAC483D86D354C680@ray>
References: <8E81A526E0444D1DAC483D86D354C680@ray>
Message-ID: <1c2a2c590911160933r40d6e990md1cbe1423694996a@mail.gmail.com>

On Mon, Nov 16, 2009 at 10:56 AM, Ray Holt <mrholtsr at sbcglobal.net> wrote:
> I have posted this on other lists, but have just discovered this one. Can
> someone give me help on writing the code necessary to find the 1000th. prime
> number. I know I will have to use a while loop, but I can't seem to get the
> body of the code to function rightly.

What have you tried so far? What don't you understand?

Kent

From davea at ieee.org  Mon Nov 16 19:30:19 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 16 Nov 2009 13:30:19 -0500
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
	<dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>
Message-ID: <4B019A3B.5010100@ieee.org>

Luke Paireepinart wrote:
>> Here's what I knocked up over lunch.  It doesn't cover the moving of
>> the file, I don't like that it's deep-nested, and I've not tested it,
>> but I welcome criticism and feedback:
>>
>> files = ['file1', 'file2', 'file3', 'file4']
>> settings = ['export http_proxy=', 'ftp_proxy=']
>>
>> for file in files:
>>  with open(file, 'rw') as file:
>>
>>     
>
> Does this actually work?
> I didn't think you could open files as reading and writing?
>
>   
Yes, you can, but not this way.  I'm guessing the op was changing his 
mind back and forth, between having two files, one for reading and one 
for writing, and trying to do it in place.  The code does neither/both.

Take a look at fileinput.FileInput()  with the inplace option.  It makes 
it convenient to update text files "in place" by handling all the temp 
file copying and such.  It even handles iterating through the list of files.
> also you're shadowing the 'file' var, which might be confusing to beginners,
> as it's not the same file as in the outer for loop.
> Also, 'with' is a 3.0 keyword, right?  That's pretty interesting syntax,
> I've never seen it before, but I haven't used 3.x yet.
>
>   
Good point about 'file' as its a built-in name.  If the code ever has to 
use the std meaning, you have a problem.  Worse, it's unreadable as is.

With was introduced in 2.6, and does work nicely, at least in CPython.
>   
>>    for line in file:
>>      for setting in settings:
>>        if setting in line:
>>          if line[0] == '#':
>>            line = line[1:]
>>          else:
>>            line =  '#' + line
>>        output.write(line)
>>
>>     
>
> Also, you didn't define 'output' anywhere.  Is this an implicit declaration
> via the 'with' syntax?
>
> Thanks,
> -Luke
>
>   
No, output is an undefined global at present.  As I implied earlier, he 
was probably intending to have a separate file for writing.
>>     
> S.
>   
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>     
>
>   
Another potential  bug with the code is if more than one "setting" could 
appear in a line. It would change the line for an odd number, and not 
for an even number of matches.

Also, the nesting of output.write() is wrong, because file position 
isn't preserved, and random access in a text file isn't a good idea 
anyway.  But there's not much point in debugging that till the OP 
decides how he's going to handle the updates, via new files and copying 
or renaming, or via inputfile.


DaveA


From sanelson at gmail.com  Mon Nov 16 19:46:28 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 18:46:28 +0000
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <4B019A3B.5010100@ieee.org>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
	<dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>
	<4B019A3B.5010100@ieee.org>
Message-ID: <b6131fdc0911161046l1c9510d2g75dad5e2f3ea8e1c@mail.gmail.com>

Evening,
> Yes, you can, but not this way. ?I'm guessing the op was changing his mind
> back and forth, between having two files, one for reading and one for
> writing, and trying to do it in place. ?The code does neither/both.

Well, just neither I think!  I didn't check if 'rw' was possible.  My
plan was to read the file and write to the same file as the change was
made, to avoid having to use temporary files and os.move.  But I
wasn't near a machine with python on it, so it was untested.

> Take a look at fileinput.FileInput() ?with the inplace option. ?It makes it
> convenient to update text files "in place" by handling all the temp file
> copying and such. ?It even handles iterating through the list of files.

Will definitely look into that.

> Good point about 'file' as its a built-in name. ?If the code ever has to use
> the std meaning, you have a problem. ?Worse, it's unreadable as is.

Thanks for that hint.  In what way is it unreadable?  Because the
intent is not clear because of the ambiguity of the name?

>> Also, you didn't define 'output' anywhere. ?Is this an implicit
>> declaration

No, just a dumb mistake.

> Another potential ?bug with the code is if more than one "setting" could
> appear in a line. It would change the line for an odd number, and not for an
> even number of matches.

Not sure I follow that.  From the OPs description, it appeared he
would be entering these lines in.  I figured it was safe to trust the
OP not to put in duplicate data.  Maybe defensively I should check for
it anyway?

> Also, the nesting of output.write() is wrong, because file position isn't
> preserved, and random access in a text file isn't a good idea anyway.

Could you expand on this?

> ?But
> there's not much point in debugging that till the OP decides how he's going
> to handle the updates, via new files and copying or renaming, or via
> inputfile.

I'll look up inputfile, and try again :)

S.

From kb1pkl at aim.com  Mon Nov 16 20:18:52 2009
From: kb1pkl at aim.com (kb1pkl at aim.com)
Date: Mon, 16 Nov 2009 14:18:52 -0500
Subject: [Tutor] Unexpected Result in Test Sequence
Message-ID: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>

Hello Tutor list.
I'm running a test to find what the experimental average of a d20 is, 
and came across a strange bug in my code.
import random
list1 = []
def p():
    d = 0
    for number in range(1,1000):
        t = random.randrange(1,19)
        list1.append(t)
    for value in list1:
        d+=value
    print d/1000
    d = 0
for value in range(1,100):
    p()

It works, but I have a logic error somewhere. It runs, and the results 
have a pattern :
9
19
28
37
47
56
66
75
85
94
104
113
...
...
It just adds 10, and every second result, subtracts 1, till it gets to 
0, and then starts again with 9 in singles, and whatever in the 10's, 
etc.
What is causing this?

From jra at nella.org  Mon Nov 16 20:40:53 2009
From: jra at nella.org (Jeff R. Allen)
Date: Mon, 16 Nov 2009 20:40:53 +0100
Subject: [Tutor] Unexpected Result in Test Sequence
In-Reply-To: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <bccc8ac80911161140v37f36e9cm6b9f816834d2a495@mail.gmail.com>

When you declare list1 before "def p()" you are making it global. That
means it will keep its values between invocations of p(). When you
start function p, you don't reset list1 to empty. You divide each time
by 1000, and but your list1 list is growing and growing and growing.
That's why the total is growing, not giving the answer you were
expecting.

Either you need to reset list1 to empty each time p() starts, or (if
you want to accumulate the results between calls to p()) you need to
divide by the true number of items in the list, not by 1000. So
replace /1000 with /len(list1).

However, that brings me to something else interesting. You are doing
an integer divide. That means you'll get integer results, which for
your purposes are much less interesting than floating point ones.
Replace the /1000 with /1000.0 and see what you get. (Move the
declaration of list1 into p() also.)

Another thing... play in the interpreter with range(1,5) and
range(0,5). Can you see another little bug your program has? It is not
doing precisely what you think it is yet...

 -jeff

From vinces1979 at gmail.com  Mon Nov 16 20:42:08 2009
From: vinces1979 at gmail.com (vince spicer)
Date: Mon, 16 Nov 2009 13:42:08 -0600
Subject: [Tutor] Unexpected Result in Test Sequence
In-Reply-To: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <1e53c510911161142t6b731d4ch93c91c70450f0000@mail.gmail.com>

On Mon, Nov 16, 2009 at 1:18 PM, <kb1pkl at aim.com> wrote:

> Hello Tutor list.
> I'm running a test to find what the experimental average of a d20 is, and
> came across a strange bug in my code.
> import random
> list1 = []
> def p():
>   d = 0
>   for number in range(1,1000):
>       t = random.randrange(1,19)
>       list1.append(t)
>   for value in list1:
>       d+=value
>   print d/1000
>   d = 0
> for value in range(1,100):
>   p()
>
> It works, but I have a logic error somewhere. It runs, and the results have
> a pattern :
> 9
> 19
> 28
> 37
> 47
> 56
> 66
> 75
> 85
> 94
> 104
> 113
> ...
> ...
> It just adds 10, and every second result, subtracts 1, till it gets to 0,
> and then starts again with 9 in singles, and whatever in the 10's, etc.
> What is causing this?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


I think that one problem you are seeing is caused by the use of integers
When you are dividing sum of the random number by 1000, python is rounding
your output.

Also your list, never gets cleared and you keep adding to the original list,
and you are then dividing by the original 1000


A simple fix is to use floats (PS: cleaned up a bit):

from random import randrange
def p():
    mylist = [randrange(1,19) for x in range(1, 1000)]
    d = sum(list)
    print float(d) / len(mylist)

for value in range(1,100):
    p()


Hope this point you in a better direction

Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/5e394103/attachment-0001.htm>

From ljmamoreira at gmail.com  Mon Nov 16 20:42:53 2009
From: ljmamoreira at gmail.com (Jose Amoreira)
Date: Mon, 16 Nov 2009 19:42:53 +0000
Subject: [Tutor] Unexpected Result in Test Sequence
In-Reply-To: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <200911161942.53809.ljmamoreira@gmail.com>

Hi!
Everytime your program calls function p, list1 is appended to. It keeps on 
getting bigger and bigger. The function adds all the elements of the list, but 
the total is divided by 1000, even if the list is already much longer that 
1000!

And there's another thing. When var1 and var2 are integer, var1/var2 returns 
the integer division, that is, for instance 3/2=1, 9/10=0, etc (try it!).

I bet that if you divide the sum of the values in list1 by len(list1) 
(otherwise reset the list, so that it remains only 1000 element long) and 
replace the line 'print d/1000' with 'print (d*1.0)/len(list1)' (or 'print 
d/1000.0', if you reset the list every time you call p) your problem is over.

Hope this helps!
Cheers
Jos? Amoreira


On Monday 16 November 2009 07:18:52 pm kb1pkl at aim.com wrote:
> Hello Tutor list.
> I'm running a test to find what the experimental average of a d20 is,
> and came across a strange bug in my code.
> import random
> list1 = []
> def p():
>     d = 0
>     for number in range(1,1000):
>         t = random.randrange(1,19)
>         list1.append(t)
>     for value in list1:
>         d+=value
>     print d/1000
>     d = 0
> for value in range(1,100):
>     p()
>
> It works, but I have a logic error somewhere. It runs, and the results
> have a pattern :
> 9
> 19
> 28
> 37
> 47
> 56
> 66
> 75
> 85
> 94
> 104
> 113
> ...
> ...
> It just adds 10, and every second result, subtracts 1, till it gets to
> 0, and then starts again with 9 in singles, and whatever in the 10's,
> etc.
> What is causing this?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


From tim.peters at gmail.com  Mon Nov 16 20:44:57 2009
From: tim.peters at gmail.com (Tim Peters)
Date: Mon, 16 Nov 2009 14:44:57 -0500
Subject: [Tutor] Unexpected Result in Test Sequence
In-Reply-To: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <1f7befae0911161144j47510fe6mb9168886916eaecd@mail.gmail.com>

[kb1pkl at aim.com]
> I'm running a test to find what the experimental average of a d20 is,

I don't know what "a d20" is, but your code is picking integers
uniformly at random between 1 and 18 inclusive.  The expected value is
therefore (1+18)/2.0 = 9.5.

> and came across a strange bug in my code.
>
> import random
> list1 = []

Note that you never clear this list.  It just keeps growing, and
growing, and growing ...

> def p():
> ? d = 0
> ? for number in range(1,1000):
> ? ? ? t = random.randrange(1,19)
> ? ? ? list1.append(t)

On average, the sum of the 1000 integers you just added is 9.5 * 1000 = 9500.

> ? for value in list1:
> ? ? ? d+=value

So the sum of the entire list (which you never clear) will be
approximately 9500 times the number of times you've called p().

> ? print d/1000

Here you're dividing by 1000 no matter how long the list is,  The
first time you call p(), it does have 1000 integers by the time you
get here.  The second time you call p(), it will have 2000 integers by
the time you get here.  And so on.  So what I expect this to print is
approximately

    9500 * the number of times you've called p() / 1000 =
    approximately 9.5 times the number of times you've called p()

However, you're using truncating integer division, not floating
division, so the truth of that is obscured.


> ? d = 0

This line didn't do anything for you.

> for value in range(1,100):
> ? p()
>
> It works, but I have a logic error somewhere. It runs, and the results have
> a pattern :
> 9

As above, approximately 9.5 times 1.

> 19

Approximately 9.5 times 2.

> 28

Approximately 9.5 times 3.

> 37

Approximately 9.5 times 4.

> 47
> 56
> 66
> 75
> 85
> 94
> 104
> 113
> ...

Etc.


> It just adds 10, and every second result, subtracts 1, till it gets to 0,
> and then starts again with 9 in singles, and whatever in the 10's, etc.
> What is causing this?

A couple things:  you're never clearing list1, and you're using
truncating integer division to compute the average.  I /suspect/ you
didn't really intend "randrange(1, 19)" either, but that depends on
what "a d20" means.

Try this instead ;-)

import random
def p():
  list1 = [random.randrange(1, 19) for dummy in xrange(1000)]
  print float(sum(list1)) / len(list1)

for value in range(1,100):
  p()

From emmanuel.ruellan at laposte.net  Mon Nov 16 20:48:40 2009
From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan)
Date: Mon, 16 Nov 2009 20:48:40 +0100
Subject: [Tutor] Unexpected Result in Test Sequence
In-Reply-To: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <7296745c0911161148w74ca9820g4a1705331faf40f9@mail.gmail.com>

In your code, list list1 never gets emptied.

There is another problem : the doc for random.randrange() says it "choose[s]
a random item from range(start, stop[, step])" and range(1, 19) goes from 1
included to 19 _not_ included. Your 'd20' is not a 20-sided dice.

On Mon, Nov 16, 2009 at 8:18 PM, <kb1pkl at aim.com> wrote:

>
>
> Hello Tutor list.
> I'm running a test to find what the experimental average of a d20 is, and
> came across a strange bug in my code.
> import random
> list1 = []
> def p():
>   d = 0
>   for number in range(1,1000):
>       t = random.randrange(1,19)
>       list1.append(t)
>   for value in list1:
>       d+=value
>   print d/1000
>   d = 0
> for value in range(1,100):
>   p()
>
> It works, but I have a logic error somewhere. It runs, and the results have
> a pattern :
> 9
> 19
> 28
> 37
> 47
> 56
> 66
> 75
> 85
> 94
> 104
> 113
> ...
> ...
> It just adds 10, and every second result, subtracts 1, till it gets to 0,
> and then starts again with 9 in singles, and whatever in the 10's, etc.
> What is causing this?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/51b032fa/attachment.htm>

From davea at ieee.org  Mon Nov 16 20:56:15 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 16 Nov 2009 14:56:15 -0500
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <b6131fdc0911161046l1c9510d2g75dad5e2f3ea8e1c@mail.gmail.com>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>	
	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>	
	<dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>	
	<4B019A3B.5010100@ieee.org>
	<b6131fdc0911161046l1c9510d2g75dad5e2f3ea8e1c@mail.gmail.com>
Message-ID: <4B01AE5F.4010300@ieee.org>

(Multiple people on this thread have been quoting earlier people without 
keeping the attribution lines  (e.g.   xxxx yyy wrote: )
    so I'm not sure who said what any more.  I recognize my own words, 
of course, but for anybody else, your guess is
    better than mine)

Stephen Nelson-Smith wrote:
> Evening,
>   
>> Yes, you can, but not this way.  I'm guessing the op was changing his mind
>> back and forth, between having two files, one for reading and one for
>> writing, and trying to do it in place.  The code does neither/both.
>>     
>
> Well, just neither I think!  I didn't check if 'rw' was possible.  My
> plan was to read the file and write to the same file as the change was
> made, to avoid having to use temporary files and os.move.  But I
> wasn't near a machine with python on it, so it was untested.
>
>   
The string to use is "r+b"   The  r  says you're going to read it.  The 
+ says you're also going to write it.  And the  b  says you don't want 
any character translation going on.  Text files should only be written 
in one direction, or appended, but not updated in-place, except in very 
specific circumstances.

If you're both reading and writing, you need to do a seek() in between.  
So you'll be doing two seeks per loop. And since each line is a variable 
size, you'd have to keep track of that size.  And if you were on a 
Windows machine, the size of the string you see may not match the size 
on the file ( 0d0a becomes 0a when reading a text file).  Finally, even 
if you get all that right, the specific example is trying to replace 
lines with new lines of a different length.  Not in this world.
>> Take a look at fileinput.FileInput()  with the inplace option.  It makes it
>> convenient to update text files "in place" by handling all the temp file
>> copying and such.  It even handles iterating through the list of files.
>>     
>
> Will definitely look into that.
>
>   
>> Good point about 'file' as its a built-in name.  If the code ever has to use
>> the std meaning, you have a problem.  Worse, it's unreadable as is.
>>     
>
> Thanks for that hint.  In what way is it unreadable?  Because the
> intent is not clear because of the ambiguity of the name?
>
>   
Exactly.  In that short fragment, there are three meanings for the name 
file.  It's a built-in type until the first time you change it.  Then it 
becomes a string.  And after the with statement it becomes a file 
object.  If you had to check its type, that would be quite difficult.  
And if you needed its name, you've clobbered it.

>>> Also, you didn't define 'output' anywhere.  Is this an implicit
>>> declaration
>>>       
>
> No, just a dumb mistake.
>
>   
>> Another potential  bug with the code is if more than one "setting" could
>> appear in a line. It would change the line for an odd number, and not for an
>> even number of matches.
>>     
>
> Not sure I follow that.  From the OPs description, it appeared he
> would be entering these lines in.  I figured it was safe to trust the
> OP not to put in duplicate data.  Maybe defensively I should check for
> it anyway?
>
>   
I'm not sure where these "settings" values come from.  But if these are 
arbitrary command lines, it's plausible that one command works on a file 
whose name looks like one of the other commands.   I'd feel better with 
something like "startswith()"  instead of "in" .  But just as a general 
precaution, I'd put a break in the for loop whenever one of them matches
>> Also, the nesting of output.write() is wrong, because file position isn't
>> preserved, and random access in a text file isn't a good idea anyway.
>>     
>
> Could you expand on this?
>
>   
As it stands, it only writes the line when it needs to change.  But that 
implies that you've been keeping track of sizes so you can put a seek 
before and after the line, and of course that the size doesn't change.  
Since none of these is true (or practical), my comment was assuming that 
you're using a different "output" file.  So you need to write every 
line, not just the ones that change.
>>  But
>> there's not much point in debugging that till the OP decides how he's going
>> to handle the updates, via new files and copying or renaming, or via
>> inputfile.
>>     
>
> I'll look up inputfile, and try again :)
>
> S.
>
>   

With inputfile, you'll basically copy the file from the supplied 
iterator to stdout (probably using print), doing any conversions on the 
line as you go.

DaveA

From sander.sweers at gmail.com  Mon Nov 16 20:59:08 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Mon, 16 Nov 2009 20:59:08 +0100
Subject: [Tutor] GzipFile has no attribute '__exit__'
In-Reply-To: <1c2a2c590911160439g30a5892etca96926b3c9f64a8@mail.gmail.com>
References: <b6131fdc0911160258h1f429321occ4fe40b011c0c7e@mail.gmail.com>
	<4B013D89.60409@ieee.org>
	<b65fbb130911160415n1861860bif36d6afcee3ad918@mail.gmail.com>
	<1c2a2c590911160439g30a5892etca96926b3c9f64a8@mail.gmail.com>
Message-ID: <b65fbb130911161159p6ce0694el41e9b931fac9306@mail.gmail.com>

2009/11/16 Kent Johnson <kent37 at tds.net>:
> You might want to report this as a bug against 2.6 and submit this
> change as a proposed fix. It's easy to do, see

I poked around in the bug tracker and found [1]. It will be in 2.7 and
was *not* accepted for 2.6.

If anyone wants to add this in themselves see [2].

Greets
Sander

[1] http://bugs.python.org/issue3860
[2] http://svn.python.org/view?view=rev&revision=68484

From mrholtsr at sbcglobal.net  Mon Nov 16 20:34:56 2009
From: mrholtsr at sbcglobal.net (Ray Holt)
Date: Mon, 16 Nov 2009 14:34:56 -0500
Subject: [Tutor] A way to search archives?
Message-ID: <37034D4120664049BEFC2D998278F417@ray>

Is there a way to search the archives?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/d1b7d04e/attachment.htm>

From bgailer at gmail.com  Mon Nov 16 21:34:24 2009
From: bgailer at gmail.com (bob gailer)
Date: Mon, 16 Nov 2009 15:34:24 -0500
Subject: [Tutor] Help on finding the 1000th prime
In-Reply-To: <8E81A526E0444D1DAC483D86D354C680@ray>
References: <8E81A526E0444D1DAC483D86D354C680@ray>
Message-ID: <4B01B750.908@gmail.com>

Ray Holt wrote:
> find the 1000th. prime number.
Break this down into 2 separate problems. (I assume 1 is the first prime 
number)
1 - determining the next prime number
2 - repeating that 1000 times.

A while loop is a way to accomplish 2.

How do you determine the next prime number? There are many algorithms 
for this - you might consult Wikipedia for ideas.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From zstumgoren at gmail.com  Mon Nov 16 21:37:25 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Mon, 16 Nov 2009 15:37:25 -0500
Subject: [Tutor] A way to search archives?
In-Reply-To: <37034D4120664049BEFC2D998278F417@ray>
References: <37034D4120664049BEFC2D998278F417@ray>
Message-ID: <cadf44510911161237u1e8ff834w6abd14334635a7cb@mail.gmail.com>

> Is there a way to search the archives?

Not sure if someone's developed an official search resource, but I've
always found Google search to work well:

<search keywords> site:http://mail.python.org/pipermail/tutor/

From modulok at gmail.com  Mon Nov 16 21:54:26 2009
From: modulok at gmail.com (Modulok)
Date: Mon, 16 Nov 2009 13:54:26 -0700
Subject: [Tutor] Do you use unit testing?
Message-ID: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>

List,

A general question:

How many of you guys use unit testing as a development model, or at
all for that matter?

 I just starting messing around with it and it seems painfully slow to
have to write a test for everything you do. Thoughts, experiences,
pros, cons?

Just looking for input and different angles on the matter, from the
Python community.
-Modulok-

From kent37 at tds.net  Mon Nov 16 21:59:46 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 15:59:46 -0500
Subject: [Tutor] Fwd:  Help on finding the 1000th prime
In-Reply-To: <20264FC967B24CA38BF4D903F05AED6C@ray>
References: <8E81A526E0444D1DAC483D86D354C680@ray>
	<1c2a2c590911160933r40d6e990md1cbe1423694996a@mail.gmail.com>
	<20264FC967B24CA38BF4D903F05AED6C@ray>
Message-ID: <1c2a2c590911161259x4ad53840nd179c0c4a22cfaf2@mail.gmail.com>

Please use reply all to reply to the list


---------- Forwarded message ----------
From: Ray Holt <mrholtsr at sbcglobal.net>
Date: Mon, Nov 16, 2009 at 1:34 PM
Subject: RE: [Tutor] Help on finding the 1000th prime
To: Kent Johnson <kent37 at tds.net>


Basically I can't figure out how to loop through the possible candidates. I
am using version 2.5.6. I creat the varibles :
primeCount = ?0
Prime Candidate = 2
While primeCount < 1000
isPrime = True
? ? ? ?primeCanditate = primeCandidate + 2
That's as far as I have gotten. I can't figure out how to loop through the
possible candidates to the 1000th prime.

-----Original Message-----
From: kent3737 at gmail.com [mailto:kent3737 at gmail.com] On Behalf Of Kent
Johnson
Sent: Monday, November 16, 2009 12:33 PM
To: Ray Holt
Cc: tutor at python.org
Subject: Re: [Tutor] Help on finding the 1000th prime

On Mon, Nov 16, 2009 at 10:56 AM, Ray Holt <mrholtsr at sbcglobal.net> wrote:
> I have posted this on other lists, but have just discovered this one.
> Can someone give me help on writing the code necessary to find the
> 1000th. prime number. I know I will have to use a while loop, but I
> can't seem to get the body of the code to function rightly.

What have you tried so far? What don't you understand?

Kent

From kent37 at tds.net  Mon Nov 16 22:00:02 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 16:00:02 -0500
Subject: [Tutor] Fwd:  Help on finding the 1000th prime
In-Reply-To: <D3A79352DF4246D782D5BE7074F3B71F@ray>
References: <8E81A526E0444D1DAC483D86D354C680@ray>
	<1c2a2c590911160933r40d6e990md1cbe1423694996a@mail.gmail.com>
	<D3A79352DF4246D782D5BE7074F3B71F@ray>
Message-ID: <1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>

---------- Forwarded message ----------
From: Ray Holt <mrholtsr at sbcglobal.net>
Date: Mon, Nov 16, 2009 at 1:55 PM
Subject: RE: [Tutor] Help on finding the 1000th prime
To: Kent Johnson <kent37 at tds.net>


I hit the send button before I was ready. Here is the code that doesn't
work.
primeCount = 0
primeCandidate = 2
While primeCount <= 1000:
isPrime = True
? ? ? ?primeCandidate = primeCandidate + 2
? ? ? ?for x in range(2, primeCandidate):
? ? ? ? ? ? ? ?if primeCandidate % x == 0:
? ? ? ?print primeCandidate, "equals", x, "*", primeCandidate/x
? ? ? ?isPrime = False
Print primeCandidate
? ? ? ?break
If isPrime:
? ? ? ?primeCount = primeCount + 2
Print primeCandidate


-----Original Message-----
From: kent3737 at gmail.com [mailto:kent3737 at gmail.com] On Behalf Of Kent
Johnson
Sent: Monday, November 16, 2009 12:33 PM
To: Ray Holt
Cc: tutor at python.org
Subject: Re: [Tutor] Help on finding the 1000th prime

On Mon, Nov 16, 2009 at 10:56 AM, Ray Holt <mrholtsr at sbcglobal.net> wrote:
> I have posted this on other lists, but have just discovered this one.
> Can someone give me help on writing the code necessary to find the
> 1000th. prime number. I know I will have to use a while loop, but I
> can't seem to get the body of the code to function rightly.

What have you tried so far? What don't you understand?

Kent

From zstumgoren at gmail.com  Mon Nov 16 22:31:38 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Mon, 16 Nov 2009 16:31:38 -0500
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
Message-ID: <cadf44510911161331q6856ca46y3cdae59680fa4ad0@mail.gmail.com>

> How many of you guys use unit testing as a development model, or at
> all for that matter?

> ?I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do. Thoughts, experiences,
> pros, cons?
>
I just started with Test-Driven Development myself and am also having
some growing pains. I think the urge to write a test for everything
stems from fuzziness about what types of things one should test for
(at least that's the area I'm stumbling on).

Dive Into Python has a great step-by-step tutorial with lots of code
samples for those looking to get started with unit testing, but I've
yet to find a good, high-level overview of use cases for unit testing.

Are there any rules of thumb or perhaps even testing patterns out there?

Would love to hear what others on the list have to say on this.

Serdar

From bgailer at gmail.com  Mon Nov 16 22:33:59 2009
From: bgailer at gmail.com (bob gailer)
Date: Mon, 16 Nov 2009 16:33:59 -0500
Subject: [Tutor] Fwd:  Help on finding the 1000th prime
In-Reply-To: <1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>
References: <8E81A526E0444D1DAC483D86D354C680@ray>	<1c2a2c590911160933r40d6e990md1cbe1423694996a@mail.gmail.com>	<D3A79352DF4246D782D5BE7074F3B71F@ray>
	<1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>
Message-ID: <4B01C547.7090708@gmail.com>


> Here is the code that doesn't work.
>   

Thanks for posting the code. What "doesn't work" mean?
error messages?
unexpected results?
or what?

There are several problems in your code.
 - The candidates you are testing. Walk thru the program and list a few 
of them and see if you can determine that problem.
 - Counting primes as you discover them. Same advice as above.
 - indentation is screwed up. Please use only spaces - no tabs.
> primeCount = 0
> primeCandidate = 2
> While primeCount <= 1000:
> isPrime = True
>        primeCandidate = primeCandidate + 2
>        for x in range(2, primeCandidate):
>                if primeCandidate % x == 0:
>        print primeCandidate, "equals", x, "*", primeCandidate/x
>        isPrime = False
> Print primeCandidate
>        break
> If isPrime:
>        primeCount = primeCount + 2
> Print primeCandidate
>
>   

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From modulok at gmail.com  Mon Nov 16 22:47:37 2009
From: modulok at gmail.com (Modulok)
Date: Mon, 16 Nov 2009 14:47:37 -0700
Subject: [Tutor] Fwd: Help on finding the 1000th prime
In-Reply-To: <4B01C547.7090708@gmail.com>
References: <8E81A526E0444D1DAC483D86D354C680@ray>
	<1c2a2c590911160933r40d6e990md1cbe1423694996a@mail.gmail.com>
	<D3A79352DF4246D782D5BE7074F3B71F@ray>
	<1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>
	<4B01C547.7090708@gmail.com>
Message-ID: <64c038660911161347j12f190dbh112a11336345fa67@mail.gmail.com>

Also watch things like letter case on stuff like 'Print' and 'While',
they should instead be 'print' and 'while', (all lowercase).

-Modulok-

On 11/16/09, bob gailer <bgailer at gmail.com> wrote:
>
>> Here is the code that doesn't work.
>>
>
> Thanks for posting the code. What "doesn't work" mean?
> error messages?
> unexpected results?
> or what?
>
> There are several problems in your code.
>  - The candidates you are testing. Walk thru the program and list a few
> of them and see if you can determine that problem.
>  - Counting primes as you discover them. Same advice as above.
>  - indentation is screwed up. Please use only spaces - no tabs.
>> primeCount = 0
>> primeCandidate = 2
>> While primeCount <= 1000:
>> isPrime = True
>>        primeCandidate = primeCandidate + 2
>>        for x in range(2, primeCandidate):
>>                if primeCandidate % x == 0:
>>        print primeCandidate, "equals", x, "*", primeCandidate/x
>>        isPrime = False
>> Print primeCandidate
>>        break
>> If isPrime:
>>        primeCount = primeCount + 2
>> Print primeCandidate
>>
>>
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From kent37 at tds.net  Mon Nov 16 22:59:47 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 16 Nov 2009 16:59:47 -0500
Subject: [Tutor] A way to search archives?
In-Reply-To: <37034D4120664049BEFC2D998278F417@ray>
References: <37034D4120664049BEFC2D998278F417@ray>
Message-ID: <1c2a2c590911161359h4cbc3727o88f7c1edb6080042@mail.gmail.com>

On Mon, Nov 16, 2009 at 2:34 PM, Ray Holt <mrholtsr at sbcglobal.net> wrote:
> Is there a way to search the archives?

At http://mail.python.org/mailman/listinfo/tutor you will find links to:
http://dir.gmane.org/gmane.comp.python.tutor
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor

Kent

From sanelson at gmail.com  Mon Nov 16 23:03:32 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 16 Nov 2009 22:03:32 +0000
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
Message-ID: <b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>

Great question,

I learned TDD with PyUnit, but since moved to using Ruby, and have
been spoilt by rspec and even cucumber.  My instincts are to write
things test first, but so far I'm not finding PyUnit easy enough to
get going.

Once I get into the groove, I find it's a wonderful way to work - and
avoids some of the drivel I've come up with in the last few days.

As a discipline - work out what we want to test, write the test, watch
it fail, make it pass - I find this a very productive way to think and
work.

S.

On Mon, Nov 16, 2009 at 8:54 PM, Modulok <modulok at gmail.com> wrote:
> List,
>
> A general question:
>
> How many of you guys use unit testing as a development model, or at
> all for that matter?
>
> ?I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do. Thoughts, experiences,
> pros, cons?
>
> Just looking for input and different angles on the matter, from the
> Python community.
> -Modulok-
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From worminater at gmail.com  Mon Nov 16 23:15:41 2009
From: worminater at gmail.com (Chris Mueller)
Date: Mon, 16 Nov 2009 17:15:41 -0500
Subject: [Tutor] "Snack" python-newt version incompatibilities.. (anaconda
	kickstart)
Message-ID: <940dc7890911161415o3d43fcf1vd076f859af36c6dc@mail.gmail.com>

Hello all.

I'm working on a CentOS 5.2 kickstart interface; and have been happily using

    %pre --interpreter /usr/bin/python

to do my management for awhile now with the snack module.  My problem
comes in when I am attempting to use the snack.EntryWindow function;
and setting default values.

The simplified version of my code is below...

    (default1,default2,default3,default4)=('1','2','3','4')
    #These are all of my prompts.
    prompts = [('Static1',default1), ('Static2',
default2),('Static3',default3),('Static4',default4)]
    ok, params = snack.EntryWindow(screen, "This is my title",
                                   "This is my text at top of box",
                                   prompts,
                                   help = "")

On my desktop, I have...
$>python -V
Python 2.5.2
The defualt python in CentOS5.2 is..
$>python -V
Python 2.4.3

The snack window behaves as expected on my desktop (default values all
populate and change values as I want/expect), but when I try and run
on the 2.4 python, i get the following(output cleaned up a little bit
as it splays the entire window)...

    <my file and pointing to the snack.EntryWindow call>
    File "/usr/lib/python2.4/site-packages/snack.py", line 796, in EntryWindow
    sg.setField(e, 1, count, anchorLeft = 1)
    File "/usr/lib/python2.4/site-packages/snack.py", line 388, in setField
        (what.__dict__.has_key('g')):
    AttributeError: 'str' object has no attribute '__dict__' selects

With some googling, I managed to find references to a bug in the
python snack module that was fixed, but I was unable to find a working
format of the "prompts" variable to work in my Python 2.4.3.

I tried the above code, as well the suggestion from the link below of

    prompts = [Entry(20, 'ham'), Entry(20, 'egg', hidden=True)]

But to no avail. Any help would be greatly appreciated...

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=340366

Thanks,
    Chris

From jra at nella.org  Mon Nov 16 23:17:25 2009
From: jra at nella.org (Jeff R. Allen)
Date: Mon, 16 Nov 2009 23:17:25 +0100
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com> 
	<b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>
Message-ID: <bccc8ac80911161417p3ff386b2h32a1bef45f2f65fc@mail.gmail.com>

> As a discipline - work out what we want to test, write the test, watch
> it fail, make it pass - I find this a very productive way to think and
> work.

There's an emotional aspect too. Keeping a positive aspect keeps up
flow, and seeing tests go from failing to passing feels really good.
Making a change you're not completely confident in and seeing tests
start failing immediately is also quite nice .. you immediately feel
how close to correct the change in question is, and if it's worth
fixing up or is leading you the wrong direction and should be reverted
allowing you to take another stab at it.

  -jeff

From zstumgoren at gmail.com  Mon Nov 16 23:22:43 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Mon, 16 Nov 2009 17:22:43 -0500
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
	<b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>
Message-ID: <cadf44510911161422v5bb57842ncba3d17090d354ce@mail.gmail.com>

> I learned TDD with PyUnit, but since moved to using Ruby, and have
> been spoilt by rspec and even cucumber. ?My instincts are to write
> things test first, but so far I'm not finding PyUnit easy enough to
> get going.
>

The Dive Into Python chapter walks the process of writing/thinking
through "requirements" as a precursor to writing tests.

http://diveintopython.org/unit_testing/index.html

These aren't high-level abstract requirements per se, but lower-level
requirements based on observations about how a function, method or
class should work. In the Roman numerals example it uses, here's one
of the requirements:

    "There is no way to represent negative numbers in Roman numerals."

Based on those observations, he codes up the unit tests, and then
finally the actual functions and methods.

That approach makes sense, but again, it's always tougher when you're
working on your own project....

From christopher.henk at allisontransmission.com  Mon Nov 16 23:05:44 2009
From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com)
Date: Mon, 16 Nov 2009 17:05:44 -0500
Subject: [Tutor] Fwd:  Help on finding the 1000th prime
In-Reply-To: <1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>
Message-ID: <OF4B591556.B1954053-ON85257670.0077EA31-85257670.00793D93@mail.ati.int>

Kent Johnson <kent37 at tds.net> wrote on 11/16/2009 04:00:02 PM:

> ---------- Forwarded message ----------
> From: Ray Holt <mrholtsr at sbcglobal.net>
> Date: Mon, Nov 16, 2009 at 1:55 PM
> Subject: RE: [Tutor] Help on finding the 1000th prime
> To: Kent Johnson <kent37 at tds.net>
> 
> 
> I hit the send button before I was ready. Here is the code that doesn't
> work.
        Not to nit pick, but try and copy the code exactly as you have it. 
 The code below has capitalization where it doesn't belong and indenting 
is not consistent, so there should be plenty of syntax errors if you tried 
to run it.  This makes it hard to figure out where your particular problem 
is.  Not sure if its a syntax problem or a algorithm problem.  Indentation 
is key in Python so without showing exactly what you have loops and 
branching is hard or impossible to follow.  This is also why it is good to 
include exactly what error you are experiencing.

> primeCount = 0
> primeCandidate = 2

        You might find it easier to start with a different primeCandidate 
and handle 2 as a special case.

> While primeCount <= 1000:
> isPrime = True
>        primeCandidate = primeCandidate + 2

        primeCandidate is going to be 4,6,8,10,12,14,16,18,...
        which won't yield many primes. see comment above

>        for x in range(2, primeCandidate):
>                if primeCandidate % x == 0:
>        print primeCandidate, "equals", x, "*", primeCandidate/x
>        isPrime = False
> Print primeCandidate
>        break
> If isPrime:
>        primeCount = primeCount + 2
> Print primeCandidate
> 

        It is hard to follow what is in which loop with the rest of the 
code, so I can't say much more on where you are having problems.  It looks 
like you have the parts there just a question of what goes where.

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/d877dc3c/attachment-0001.htm>

From markersng at gmail.com  Mon Nov 16 23:24:08 2009
From: markersng at gmail.com (MARCUS NG)
Date: Mon, 16 Nov 2009 17:24:08 -0500
Subject: [Tutor] search the folder for the 2 files and extract out only the
	version numbers 1.0 and 2.0.
Message-ID: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>

 Hi all,
I am really new to python and I am trying to create a script that looks at
file versions of a particular file
I have 4 files in a folder. One called myTest1.0.zip, myTest2.0.zip,
test3.txt and test4.zip in a directory/folder called allFiles on my desktop.
so the file structure is as such

C:\Users\blueman\Desktop\allFiles\myTest1.0.zip
and
C:\Users\blueman\Desktop\allFiles\myTest2.0.zip

Aim:
what is the best way to look into the allFiles directory, and only search
the folder for the 2 files myTest1.0.zip and myTest2.0.zip to extract out
only the version numbers 1.0 and 2.0.

what i have for my script so far is
############################
import os
#path of  scripts folder
path="C:/Users/blueman/Desktop/allFiles/"
dirList=os.listdir(path)
for fname in dirList:
print fname
############################
so I did think of listing the file names in an array only to realise that I
wouldn't know which index to query if this script is going to be automated.

what is the best way to obtain the version numbers of the 2 files?
thank you for your time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/048c7f47/attachment.htm>

From zstumgoren at gmail.com  Tue Nov 17 00:21:09 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Mon, 16 Nov 2009 18:21:09 -0500
Subject: [Tutor] search the folder for the 2 files and extract out only
	the version numbers 1.0 and 2.0.
In-Reply-To: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>
References: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>
Message-ID: <cadf44510911161521w498470e0h36ea5db5ea0b543a@mail.gmail.com>

> Aim:
> what is the best way to look into the allFiles directory, and only search
> the folder for the 2 files myTest1.0.zip and myTest2.0.zip to extract out
> only the version numbers 1.0 and 2.0.
>
Based on the above file-naming convention, you could get at the
version numbers with a combo of string methods and regular
expressions:

>>> import re  #import the regular expression module
>>> file_list = ['myTest1.0.zip', 'myTest2.0.zip']
>>> for f in file_list:
...         if f.startswith("myTest") and f.endswith("zip"):
...             version = re.search(r'\d+\.\d+', f).group()
...             print version
...
1.0
2.0
>>>

The above regular expression pattern searches the string for one or
more digits, followed by a dot (which has been escaped), followed by
another sequence of one or more digits. The group method returns the
match.

You could also skip the string methods (startswith/endswith) entirely
and just use a regular expression:

>>> for f in file_list:
...         version = re.match(r'myTest(\d+\.\d+)\.zip', f).group(1)
...         print version
...
1.0
2.0
>>>

You can tweak the regex pattern above if you need more
flexibility/fuzziness in the types of file names you're trying to
match.

No doubt others on the list will have more elegant solutions.
Meantime, this site provides a gentle intro to regular expressions:

http://www.amk.ca/python/howto/regex/

HTH!

Serdar

From cflo2382 at hotmail.com  Mon Nov 16 23:52:29 2009
From: cflo2382 at hotmail.com (Carlos Flores)
Date: Mon, 16 Nov 2009 17:52:29 -0500
Subject: [Tutor] (no subject)
Message-ID: <BLU141-W2687CB491B149F3ECD707BC2A50@phx.gbl>


Dear Python,

I am trying to learn Python to experience my
computer programming knowledge. I am working through the online
textbook How to Think Like A Computer Scientist. I have made it through
chapters 1-5 and completed the exercises at the end. Now I am on
chapter 6 and I am having a lot of trouble and its only going to get
harder from here on. Is there any sources that help with these
exercises that are at the end of each chapter? Its tough not knowing if
I am doing these exercises correctly without seeing the actual answers.


Thanks. 		 	   		   		 	   		  
_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
http://clk.atdmt.com/GBL/go/171222986/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/1fbe8bdb/attachment.htm>

From anand.shashwat at gmail.com  Tue Nov 17 02:10:36 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 17 Nov 2009 06:40:36 +0530
Subject: [Tutor] Fwd: Help on finding the 1000th prime
In-Reply-To: <OF4B591556.B1954053-ON85257670.0077EA31-85257670.00793D93@mail.ati.int>
References: <1c2a2c590911161300l605f5315kb49f62d89dfd8e93@mail.gmail.com>
	<OF4B591556.B1954053-ON85257670.0077EA31-85257670.00793D93@mail.ati.int>
Message-ID: <d4ab53de0911161710v36bfda70le0ab565db2a6d70f@mail.gmail.com>

You'r trying naive algorithm. Finding prime is one of basic elements of
maths and programming. You may look at sieve of
eratosthenes<http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>and
sieve
of atkin <http://en.wikipedia.org/wiki/Sieve_of_atkin>

On Tue, Nov 17, 2009 at 3:35 AM,
<christopher.henk at allisontransmission.com>wrote:

>
>
> Kent Johnson <kent37 at tds.net> wrote on 11/16/2009 04:00:02 PM:
>
>
> > ---------- Forwarded message ----------
> > From: Ray Holt <mrholtsr at sbcglobal.net>
> > Date: Mon, Nov 16, 2009 at 1:55 PM
> > Subject: RE: [Tutor] Help on finding the 1000th prime
> > To: Kent Johnson <kent37 at tds.net>
> >
> >
> > I hit the send button before I was ready. Here is the code that doesn't
> > work.
>        Not to nit pick, but try and copy the code exactly as you have it.
>  The code below has capitalization where it doesn't belong and indenting is
> not consistent, so there should be plenty of syntax errors if you tried to
> run it.  This makes it hard to figure out where your particular problem is.
>  Not sure if its a syntax problem or a algorithm problem.  Indentation is
> key in Python so without showing exactly what you have loops and branching
> is hard or impossible to follow.  This is also why it is good to include
> exactly what error you are experiencing.
>
> > primeCount = 0
> > primeCandidate = 2
>
>        You might find it easier to start with a different primeCandidate
> and handle 2 as a special case.
>
> > While primeCount <= 1000:
> > isPrime = True
> >        primeCandidate = primeCandidate + 2
>
>         primeCandidate is going to be 4,6,8,10,12,14,16,18,...
>         which won't yield many primes. see comment above
>
> >        for x in range(2, primeCandidate):
> >                if primeCandidate % x == 0:
> >        print primeCandidate, "equals", x, "*", primeCandidate/x
> >        isPrime = False
> > Print primeCandidate
> >        break
> > If isPrime:
> >        primeCount = primeCount + 2
> > Print primeCandidate
> >
>
>         It is hard to follow what is in which loop with the rest of the
> code, so I can't say much more on where you are having problems.  It looks
> like you have the parts there just a question of what goes where.
>
> Chris
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091117/610f38a0/attachment.htm>

From waynejwerner at gmail.com  Tue Nov 17 02:57:55 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 16 Nov 2009 19:57:55 -0600
Subject: [Tutor] (no subject)
In-Reply-To: <BLU141-W2687CB491B149F3ECD707BC2A50@phx.gbl>
References: <BLU141-W2687CB491B149F3ECD707BC2A50@phx.gbl>
Message-ID: <333efb450911161757pa1459b5ndddd704fc00325bd@mail.gmail.com>

On Mon, Nov 16, 2009 at 4:52 PM, Carlos Flores <cflo2382 at hotmail.com> wrote:

>  Dear Python,
>
> I am trying to learn Python to experience my computer programming
> knowledge.
>

Welcome to the wonderful world of Python - we think it's a good extension of
anyone's knowledge


>  I am working through the online textbook How to Think Like A Computer
> Scientist. I have made it through chapters 1-5 and completed the exercises
> at the end. Now I am on chapter 6 and I am having a lot of trouble and its
> only going to get harder from here on. Is there any sources that help with
> these exercises that are at the end of each chapter?
>

I'm not aware of any... if you're worried that you're doing something wrong,
you can always use a pastebin to post your code and have us look at it. If
you're as respectful as you've been so far, you'll probably find someone
willing to take a look to see if your code does what you think it should be
doing.


> Its tough not knowing if I am doing these exercises correctly without
> seeing the actual answers.
>

That's very true! But there are a lot of smart folks on this list who are
happy to help when you get stuck somewhere.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/27e17229/attachment-0001.htm>

From mwalsh at mwalsh.org  Tue Nov 17 05:33:47 2009
From: mwalsh at mwalsh.org (Martin Walsh)
Date: Mon, 16 Nov 2009 22:33:47 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>	
	<4AFFC5D4.6000304@mwalsh.org>	
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>	
	<4B003DF8.9070909@mwalsh.org>
	<b6131fdc0911151611h164522d0h53442be283d37a6e@mail.gmail.com>
Message-ID: <4B0227AB.6040903@mwalsh.org>

Stephen Nelson-Smith wrote:
> Nope - but I can look it up.  The problem I have is that the source
> logs are rotated at 0400 hrs, so I need two days of logs in order to
> extract 24 hrs from 0000 to 2359 (which is the requirement).  At
> present, I preprocess using sort, which works fine as long as the
> month doesn't change.

Still not sure without more detail, but IIRC from your previous posts,
your log entry timestamps are formatted with the abbreviated month name
instead of month number. Without the -M flag, the sort command will ...
well, erm ... sort the month names alphabetically. With the -M
(--month-sort) flag, they are sorted chronologically.

Just a guess, of course. I suppose this is drifting a bit off topic, in
any case, but it may still serve to demonstrate the importance of
converting your string based timestamps into something that can be
sorted accurately by your python code -- the most obvious being time or
datetime objects, IMHO.

<snip>
>> class LogFile(object):
>>    def __init__(self, filename, jitter=10):
>>        self.logfile = gzip.open(filename, 'r')
>>        self.heap = []
>>        self.jitter = jitter
>>
>>    def __iter__(self):
>>        while True:
>>            for logline in self.logfile:
>>                heappush(self.heap, (timestamp(logline), logline))
>>                if len(self.heap) >= self.jitter:
>>                    break
> 
> Really nice way to handle the batching of the initial heap - thank you!
> 
>>            try:
>>                yield heappop(self.heap)
>>            except IndexError:
>>                raise StopIteration
<snip>
>> ... which probably won't preserve the order of log entries that have the
>> same timestamp, but if you need it to -- should be easy to accommodate.
> 
> I don't think  that is necessary, but I'm curious to know how...

I'd imagine something like this might work ...

class LogFile(object):
    def __init__(self, filename, jitter=10):
        self.logfile = open(filename, 'r')
        self.heap = []
        self.jitter = jitter

    def __iter__(self):
        line_count = 0
        while True:
            for logline in self.logfile:
                line_count += 1
                heappush(self.heap,
                   ((timestamp(logline), line_count), logline))
                if len(self.heap) >= self.jitter:
                    break
            try:
                yield heappop(self.heap)
            except IndexError:
                raise StopIteration

The key concept is to pass additional unique data to heappush, something
related to the order of lines from input. So, you could probably do
something with file.tell() also. But beware, it seems you can't reliably
tell() a file object opened in 'r' mode, used as an iterator[1] -- and
in python 3.x attempting to do so raises an IOError.

[1] http://mail.python.org/pipermail/python-list/2008-November/156865.html

HTH,
Marty

From timomlists at gmail.com  Tue Nov 17 08:00:21 2009
From: timomlists at gmail.com (Timo)
Date: Tue, 17 Nov 2009 08:00:21 +0100
Subject: [Tutor] search the folder for the 2 files and extract out only
 the version numbers 1.0 and 2.0.
In-Reply-To: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>
References: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>
Message-ID: <4B024A05.7080205@gmail.com>

I find regular expressions alwys hard to get, so here is my solution:

 >>> import glob
 >>> 
glob.glob('C:\\Users\\blueman\\Desktop\\allFiles\\MyTest[0-9].[0-9].zip')


Cheers,
Timo


MARCUS NG schreef:
> Hi all,
> I am really new to python and I am trying to create a script that 
> looks at file versions of a particular file
> I have 4 files in a folder. One called myTest1.0.zip, myTest2.0.zip, 
> test3.txt and test4.zip in a directory/folder called allFiles on my 
> desktop.
> so the file structure is as such
>
> C:\Users\blueman\Desktop\allFiles\myTest1.0.zip
> and
> C:\Users\blueman\Desktop\allFiles\myTest2.0.zip
>
> Aim:
> what is the best way to look into the allFiles directory, and only 
> search the folder for the 2 files myTest1.0.zip and myTest2.0.zip to 
> extract out only the version numbers 1.0 and 2.0.
>
> what i have for my script so far is
> ############################
> import os
> #path of  scripts folder
> path="C:/Users/blueman/Desktop/allFiles/"
> dirList=os.listdir(path)
> for fname in dirList:
> print fname
> ############################
> so I did think of listing the file names in an array only to realise 
> that I wouldn't know which index to query if this script is going to 
> be automated.
>
> what is the best way to obtain the version numbers of the 2 files?
> thank you for your time.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   


From rdole1 at cogeco.ca  Tue Nov 17 07:51:24 2009
From: rdole1 at cogeco.ca (rick)
Date: Tue, 17 Nov 2009 01:51:24 -0500
Subject: [Tutor] pyce, scripts complete too fast
Message-ID: <1258440684.14510.12.camel@rick-desktop>

Perhaps the wrong list, but I'll ask anyway.  So I'm in the middle of
starting, yet again, to learn some programming.   I have 2.6.4 and 3.1
installed on the desktop (Ubuntu 9.10), 2.6.4 installed on the netbook
(UNR 9.10).   

I was thinking to be ultra portable, I'd put python on the pocket pc.
Now I'm back to the problem that a Windows newbie would have, the
scripts complete before I can see what happened!  I can get right to the
interactive mode, or I can run a script, but not sure where I'd find the
terminal.

Thanks Alan, I was going through your old tutorial, the pointer to the
new was appreciated.

Rick


From waynejwerner at gmail.com  Tue Nov 17 08:49:32 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 17 Nov 2009 01:49:32 -0600
Subject: [Tutor] pyce, scripts complete too fast
In-Reply-To: <1258440684.14510.12.camel@rick-desktop>
References: <1258440684.14510.12.camel@rick-desktop>
Message-ID: <333efb450911162349t28cf3a39x862bd5a7cc74f816@mail.gmail.com>

On Tue, Nov 17, 2009 at 12:51 AM, rick <rdole1 at cogeco.ca> wrote:

> I was thinking to be ultra portable, I'd put python on the pocket pc.
> Now I'm back to the problem that a Windows newbie would have, the
> scripts complete before I can see what happened!  I can get right to the
> interactive mode, or I can run a script, but not sure where I'd find the
> terminal.
>

Add "raw_input('<enter to continue>')" or some such to the end of your
program!

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091117/a9a9aab8/attachment.htm>

From alan.gauld at btinternet.com  Tue Nov 17 09:01:32 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 08:01:32 -0000
Subject: [Tutor] Do you use unit testing?
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
Message-ID: <hdtl9b$vid$1@ger.gmane.org>


"Modulok" <modulok at gmail.com> wrote

> How many of you guys use unit testing as a development model, or at
> all for that matter?

Personally I use unit testing for anything that I'm getting paid for....
Occasionally I use it for something I'm doing for myself, but only
if its quite big - several modules and over say, 500 lines of code.
So for small single-file utilities etc that I only use myself I wouldn't 
bother.

> I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do.

Its a lot more painful and slow to have to debug subtle faults in
code you wrote 6 months ago and that don't throw exceptions
but just quietly corrupt your data...

The real  benefits of unit tests are:
a) You test stuff as you write it so you know where the bug must lie
b) You can go back and run the tests again 6 months later,
c) if you have to add a new test its easy to make sure you don't break
     what used to work (regression testing).

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Tue Nov 17 09:04:58 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 08:04:58 -0000
Subject: [Tutor] Do you use unit testing?
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
	<b6131fdc0911161403r35f9e25bq1ef064262609ffe0@mail.gmail.com>
Message-ID: <hdtlfp$d5$1@ger.gmane.org>


"Stephen Nelson-Smith" <sanelson at gmail.com> wrote 

> As a discipline - work out what we want to test, write the test, watch
> it fail, make it pass - I find this a very productive way to think and
> work.

Warning:
It can be seductively addictive and lead to very bad code structure.
You start focussing on just passing the test rather than the overall 
structure and design of the solution. Make sure that once it passes 
the tests you go back and refactor the code to be clean and maintainable.

Then test it again of course!

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From alan.gauld at btinternet.com  Tue Nov 17 09:13:01 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 08:13:01 -0000
Subject: [Tutor] pyce, scripts complete too fast
References: <1258440684.14510.12.camel@rick-desktop>
Message-ID: <hdtlur$1kl$1@ger.gmane.org>


"rick" <rdole1 at cogeco.ca> wrote

> Now I'm back to the problem that a Windows newbie would have, the
> scripts complete before I can see what happened!  I can get right to the
> interactive mode, or I can run a script, but not sure where I'd find the
> terminal.

Have a look at the "Note for Windows Users" in the Topic Add a little style
in my tutorial. It offers several ways of dealing with this issue.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From denis.spir at free.fr  Tue Nov 17 09:15:08 2009
From: denis.spir at free.fr (spir)
Date: Tue, 17 Nov 2009 09:15:08 +0100
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
Message-ID: <20091117091508.6676aca6@o>

Le Mon, 16 Nov 2009 13:54:26 -0700,
Modulok <modulok at gmail.com> stated:

> List,
> 
> A general question:
> 
> How many of you guys use unit testing as a development model, or at
> all for that matter?


I do. Systematically. Has become a reflex.
I would rather ask the opposite question: how do know a part of your code actually works if you don't test it?

What I do not do is writing the tests before developping. Probably because my style of programming is rather "exploratory", or incremental: I discover and expand while developping, so that I cannot predict what a relevant test may be. Test-driven development relies on full, accurate, detailed specification even before design; actually pre-development test code forms a kind of specif.

>  I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do. Thoughts, experiences,
> pros, cons?

Yes, you're right: "it seems painfully slow". It's an illusion. Just like the illusion that writing "user_account" rather than "usr_acnt" will slow your development pace. (I read once that developpers spend less than 1% of their time actually typing.) There's something wrong in humain brains ;-)

Writing a test func for a bit of code lets you:
* understand your code better
* find flaws and bugs before even testing
* find remaining flaws and bugs (but not all)
* validate your code (mentally, if only for yourself)
* validate changes (possibly in other code parts, related to this one)

It's much faster and better than letting unfinished code with "bombs" everywhere, that will randomly explode. Especially because numerous bugs won't look like beeing related to their actual source.

> Just looking for input and different angles on the matter, from the
> Python community.
> -Modulok-

Good question, imo!

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From alan.gauld at btinternet.com  Tue Nov 17 09:10:41 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 08:10:41 -0000
Subject: [Tutor] search the folder for the 2 files and extract out only
	theversion numbers 1.0 and 2.0.
References: <5d2f2e630911161424l5cbb1396rb5f9e78f07bb5273@mail.gmail.com>
Message-ID: <hdtlqf$197$1@ger.gmane.org>


"MARCUS NG" <markersng at gmail.com> wrote

> what is the best way to look into the allFiles directory, and only search
> the folder for the 2 files myTest1.0.zip and myTest2.0.zip to extract out
> only the version numbers 1.0 and 2.0.

You probably want to use the glob.glob() function to search using
a pattern like myTest*.zip.

Then you can iterate over the list of names returned and examoine the
version number using normal sring functions (or reguar expressions)

> so I did think of listing the file names in an array only to realise that 
> I
> wouldn't know which index to query if this script is going to be 
> automated.

In Python you hardly ever need to query the index to extract items
from a list, instead you use a for loop to look at each item in turn.

for filename in glob.glob(pattern):
     version = extractVersion(filename)
    # etc...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Tue Nov 17 09:23:37 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 08:23:37 -0000
Subject: [Tutor] Unexpected Result in Test Sequence
References: <8CC3508C448035D-10F0-7E67@webmail-m053.sysops.aol.com>
Message-ID: <hdtmio$390$1@ger.gmane.org>


<kb1pkl at aim.com> wrote in message

> I'm running a test to find what the experimental average of a d20 is, and 
> came across a strange bug in my code.
> import random
> list1 = []
> def p():
>    d = 0
>    for number in range(1,1000):
>        t = random.randrange(1,19)
>        list1.append(t)
>    for value in list1:
>        d+=value
>    print d/1000
>    d = 0
> for value in range(1,100):
>    p()
>
> It works, but I have a logic error somewhere. It runs, and the results 
> have a pattern :

> It just adds 10, and every second result, subtracts 1, till it gets to 0, 
> and then starts again with 9 in singles, and whatever in the 10's, etc.

I'm not exactly sure what you expect but given you are finding the
average of a set of numbers between 1,19 I'd expect the result to be 9.5.
In other words the integer value will vary between 9 and 10, which is what
is happening.

Can you show us a segment of data that manifests the problem
more clearly than the set you have included?

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From denis.spir at free.fr  Tue Nov 17 09:56:23 2009
From: denis.spir at free.fr (spir)
Date: Tue, 17 Nov 2009 09:56:23 +0100
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <hdtl9b$vid$1@ger.gmane.org>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
	<hdtl9b$vid$1@ger.gmane.org>
Message-ID: <20091117095623.2dbff814@o>

Le Tue, 17 Nov 2009 08:01:32 -0000,
"Alan Gauld" <alan.gauld at btinternet.com> stated:

Hello, Alan,

> The real  benefits of unit tests are:
> a) You test stuff as you write it so you know where the bug must lie

Yes! (tried to say the same much clearly ;-)

> b) You can go back and run the tests again 6 months later,
> c) if you have to add a new test its easy to make sure you don't break
>      what used to work (regression testing).

Do not understand this point. Would you be kind and expand on this topic? Have never header of regression testing (will google it, but would appreciate you words, too.) (I guess it may be relevant for incremental development.)

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From denis.spir at free.fr  Tue Nov 17 09:58:05 2009
From: denis.spir at free.fr (spir)
Date: Tue, 17 Nov 2009 09:58:05 +0100
Subject: [Tutor] Do you use unit testing? PS
In-Reply-To: <20091117095623.2dbff814@o>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
	<hdtl9b$vid$1@ger.gmane.org> <20091117095623.2dbff814@o>
Message-ID: <20091117095805.4a86a131@o>

Le Tue, 17 Nov 2009 09:56:23 +0100,
spir <denis.spir at free.fr> stated:


> > b) You can go back and run the tests again 6 months later,
> > c) if you have to add a new test its easy to make sure you don't break
> >      what used to work (regression testing).
> 
> Do not understand this point. Would you be kind and expand on this topic?
> Have never header of regression testing (will google it, but would
> appreciate you words, too.) (I guess it may be relevant for incremental
> development.)

Sorry, sent my post too fast:
http://en.wikipedia.org/wiki/Regression_testing 

Denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From davea at ieee.org  Tue Nov 17 14:27:58 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 17 Nov 2009 08:27:58 -0500
Subject: [Tutor] pyce, scripts complete too fast
In-Reply-To: <1258440684.14510.12.camel@rick-desktop>
References: <1258440684.14510.12.camel@rick-desktop>
Message-ID: <4B02A4DE.2050308@ieee.org>

rick wrote:
> Perhaps the wrong list, but I'll ask anyway.  So I'm in the middle of
> starting, yet again, to learn some programming.   I have 2.6.4 and 3.1
> installed on the desktop (Ubuntu 9.10), 2.6.4 installed on the netbook
> (UNR 9.10).   
>
> I was thinking to be ultra portable, I'd put python on the pocket pc.
> Now I'm back to the problem that a Windows newbie would have, the
> scripts complete before I can see what happened!  I can get right to the
> interactive mode, or I can run a script, but not sure where I'd find the
> terminal.
>
> Thanks Alan, I was going through your old tutorial, the pointer to the
> new was appreciated.
>
> Rick
>
>
>   
On Windows, you can get a terminal window (aka DOS box)  by running 
"Command Prompt" from your Start menu (In recent versions it seems to be 
in Start->Accessories).   You can copy that shortcut elsewhere, of 
course.  Or you can run CMD.EXE from anywhere, like the Run menu or 
another DOS box.

Once you have a DOS box, you can set local environment variables, run 
batch files, or do other preparations before beginning your python 
program.  And of course when the python program finishes you can still 
see the results (with some amount of scrolling, buffer size can be 
adjusted).    And once you're in a DOS box, you can get two independent 
ones by running Start from the first.   The new one inherits environment 
variables and current directory from the first, but does not block the 
first one from taking new commands.


You can also create a shortcut that starts a new DOS box, AND runs your 
program.  I haven't done this in so long that I forget the syntax.  But 
it's something like
        cmd.exe  /k  python.exe   script.py

By using the right switch on cmd.exe, the DOS box doesn't go away when 
the python program exits.

By the way, you can get rid of a DOS box by typing EXIT at its prompt, 
or by clicking the X on the upper right corner of the window.

DaveA

DaveA


From sanelson at gmail.com  Tue Nov 17 14:39:07 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Tue, 17 Nov 2009 13:39:07 +0000
Subject: [Tutor] Use of 'or'
Message-ID: <b6131fdc0911170539i21c10167p617ae273f200d3af@mail.gmail.com>

A friend of mine mentioned what he called the 'pythonic' idiom of:

print a or b

Isn't this a 'clever' kind or ternary - an if / else kind of thing?

I don't warm to it... should I?

S.

From mail at timgolden.me.uk  Tue Nov 17 14:52:06 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Tue, 17 Nov 2009 13:52:06 +0000
Subject: [Tutor] Use of 'or'
In-Reply-To: <b6131fdc0911170539i21c10167p617ae273f200d3af@mail.gmail.com>
References: <b6131fdc0911170539i21c10167p617ae273f200d3af@mail.gmail.com>
Message-ID: <4B02AA86.10406@timgolden.me.uk>

Stephen Nelson-Smith wrote:
> A friend of mine mentioned what he called the 'pythonic' idiom of:
> 
> print a or b
> 
> Isn't this a 'clever' kind or ternary - an if / else kind of thing?

I would say it's perfectly idiomatic in Python, but
not as a ternary. If you want a ternary use the
(relatively) recently-introduced:

a if <cond> else b

eg:

things = [1, 2 ,3]
print "%d thing%s found" % (len (things), "" if len (things) == 1 else "s")

Before this operator came along, people used to use
more-or-less clever-clever tricks with boolean operators
or using the fact that bool / int are interchangeable:

['s', ''][len (things) == 1]

The "a or b" idiom is most commonly used for default-type
situations:

def f (a, b=None):
  print "a", b or "<Unknown>"


> I don't warm to it... should I?

Up to you :) But I regard it as a useful idiom.

TJG

From toni at muybien.org  Tue Nov 17 17:58:08 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Tue, 17 Nov 2009 16:58:08 +0000
Subject: [Tutor] Introduction - log exercise
Message-ID: <20091117165639.GA3411@cateto>

Hi everybody,

This is my first post here. I have started learning python and I am new to
programing, just some bash scripting, no much. 
Thank you for the kind support and help that you provide in this list.

This is my problem: I've got a log file that is filling up very quickly, this
log file is made of blocks separated by a blank line, inside these blocks there
is a line "foo", I want to discard blocks with that line inside it, and create a
new log file, without those blocks, that will reduce drastically the size of the
log file. 

The log file is gziped, so I am going to use gzip module, and I am going to pass
the log file as an argument, so sys module is required as well.

I will read lines from file, with the 'for loop', and then I will check them for
'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
list, and if there is no matches for foo, I will append line to the list. When I
get to a blank line (end of block), write myList to an external file. And start
with another line.

I am stuck with defining 'blank line', I don't manage to get throught the while
loop, any hint here I will really appreciate it.
I don't expect the solution, as I think this is a great exercise to get wet
with python, but if anyone thinks that this is the wrong way of solving the
problem, please let me know.


#!/usr/bin/python

import sys
import gzip

myList = []

# At the moment not bother with argument part as I am testing it with a
# testing log file
#fileIn = gzip.open(sys.argv[1])

fileIn = gzip.open('big_log_file.gz', 'r')
fileOut = open('outputFile', 'a')

for line in fileIn:
    while line != 'blank_line':
        if line == 'foo':
            Somehow re-initialise myList
	    break
        else:
            myList.append(line)
    fileOut.writelines(myList)


Somehow rename outputFile with big_log_file.gz

fileIn.close()
fileOut.close()

-------------------------------------------------------------

The log file will be fill with:


Tue Nov 17 16:11:47 GMT 2009
	bladi bladi bla
	tarila ri la
	patatin pataton
	tatati tatata

Tue Nov 17 16:12:58 GMT 2009
	bladi bladi bla
	tarila ri la
	patatin pataton
	foo
	tatati tatata

Tue Nov 17 16:13:42 GMT 2009
	bladi bladi bla
	tarila ri la
	patatin pataton
	tatati tatata


etc, etc ,etc
..............................................................

Again, thank you.

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------


From waynejwerner at gmail.com  Tue Nov 17 18:41:25 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 17 Nov 2009 11:41:25 -0600
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117165639.GA3411@cateto>
References: <20091117165639.GA3411@cateto>
Message-ID: <333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>

On Tue, Nov 17, 2009 at 10:58 AM, Antonio de la Fuente <toni at muybien.org>wrote:

> Hi everybody,
>
> This is my first post here. I have started learning python and I am new to
> programing, just some bash scripting, no much.
> Thank you for the kind support and help that you provide in this list.
>

You're welcome!


>
> This is my problem: I've got a log file that is filling up very quickly,
> this
> log file is made of blocks separated by a blank line, inside these blocks
> there
> is a line "foo", I want to discard blocks with that line inside it, and
> create a
> new log file, without those blocks, that will reduce drastically the size
> of the
> log file.
>
> The log file is gziped, so I am going to use gzip module, and I am going to
> pass
> the log file as an argument, so sys module is required as well.
>
> I will read lines from file, with the 'for loop', and then I will check
> them for
> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> list, and if there is no matches for foo, I will append line to the list.
> When I
> get to a blank line (end of block), write myList to an external file. And
> start
> with another line.
>
> I am stuck with defining 'blank line', I don't manage to get throught the
> while
> loop, any hint here I will really appreciate it.
>

Let me start out by saying this; I'm very impressed with the thought you've
put into the problem and the way you've presented it.

The first thing that pops into my mind is to simply strip whitespace from
the line and check if the line == ''. Upon further experiment there's the
"isspace" method:

In [24]: x = '   \n\n\r\t\t'

In [25]: x.isspace()
Out[25]: True

x contains a bunch of spaces, newlines, and tab chars. From the docs:

" Return True if all characters in S are whitespace
    and there is at least one character in S, False otherwise."



> I don't expect the solution, as I think this is a great exercise to get wet
> with python, but if anyone thinks that this is the wrong way of solving the
> problem, please let me know.
> <snip some code>
> for line in fileIn:
>    while line != 'blank_line':
>        if line == 'foo':
>            Somehow re-initialise myList
>            break
>        else:
>            myList.append(line)
>    fileOut.writelines(myList)

 <snip some more>


Rather than using a while, you can use an if statement with the space method
(and join your statement with an:

if not line.isspace() and not line == 'foo':
    fileOut.write(line)

then you can get rid of the whole myList. Based on what you've written,
there's really no need to have a list, it's more efficient to just write the
line straight to the file.

for the renaming part, take a look at the shutil module.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091117/3a809265/attachment-0001.htm>

From nick at stinemates.org  Tue Nov 17 19:30:51 2009
From: nick at stinemates.org (Nick Stinemates)
Date: Tue, 17 Nov 2009 13:30:51 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117165639.GA3411@cateto>
References: <20091117165639.GA3411@cateto>
Message-ID: <20091117183051.GA20117@stinemates.org>

> I will read lines from file, with the 'for loop', and then I will check them for
> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> list, and if there is no matches for foo, I will append line to the list. When I
> get to a blank line (end of block), write myList to an external file. And start

Can you please explain what you mean by _re-initialize the list_ ?

> with another line.
> 
> I am stuck with defining 'blank line', I don't manage to get throught the while
> loop, any hint here I will really appreciate it.
> I don't expect the solution, as I think this is a great exercise to get wet
> with python, but if anyone thinks that this is the wrong way of solving the
> problem, please let me know.
> 
> 

Can you explain why the following won't work?


#!/usr/bin/python
 
import sys
import gzip
# At the moment not bother with argument part as I am testing it with a
# testing log file
#fileIn = gzip.open(sys.argv[1])

fileIn = gzip.open('big_log_file.gz', 'r')
fileOut = open('outputFile', 'a')

for line in fileIn:
    while line != 'blank_line':
        if 'foo' not in line:
            fileOut.write(line)
> 
> 
> Somehow rename outputFile with big_log_file.gz
> 
> fileIn.close()
> fileOut.close()
> 
> -------------------------------------------------------------
> 
> The log file will be fill with:
> 
> 
> Tue Nov 17 16:11:47 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
> 
> Tue Nov 17 16:12:58 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	foo
> 	tatati tatata
> 
> Tue Nov 17 16:13:42 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
> 
> 
> etc, etc ,etc
> ..............................................................
> 
> Again, thank you.
> 
> -- 
> -----------------------------
> Antonio de la Fuente Mart?nez
> E-mail: toni at muybien.org
> -----------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

From mhw at doctors.net.uk  Tue Nov 17 19:57:21 2009
From: mhw at doctors.net.uk (mhw at doctors.net.uk)
Date: Tue, 17 Nov 2009 18:57:21 +0000
Subject: [Tutor] Processing rows from CSV
Message-ID: <545517726-1258484248-cardhu_decombobulator_blackberry.rim.net-1199407271-@bda061.bisx.produk.on.blackberry>

Dear Tutors,

A rather general question, I'm afraid. I have found myself writing some python code to handle some CSV data, using the csv. DictReader that generates a dict for each row with the key as the column heading and the value in the file as the item. Most operations involve code of the form: (Apologies for incorrect caps)

For row in reader:
    If row['foo'] == 'something' :
        do this etc.

Typically I'm checking some form of consistency, or adding an element to the row based on something in the row.

I know about the existence of awk/ sed etc. Which could do some of this, but I ran into some problems with date manipulation, etc that they don't seem to handle very well.

I wanted to ask if anyone knew of anything similar, as I'm worried about re-inventing the wheel. One option would be to use (e.g.) sqlite and then use select/ insert commands, but personally I'd much rather write conditions in python than sql.

My other question was about how I do this more efficiently: I don't want to read the whole thing into memory at once, but (AFAIK) csv.DictReader doesn't support something like readline() to deliver it one at a time.

Any comments gratefully received.

Matt
Sent from my BlackBerry? wireless device

From mjekl at iol.pt  Tue Nov 17 19:37:08 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Tue, 17 Nov 2009 18:37:08 +0000
Subject: [Tutor] opening a file directly from memory
Message-ID: <20091117183708.gp7so8yqs40o4ook@webmail.iol.pt>

> Alan Gauld wrote:
>> <mjekl at iol.pt> wrote
>>> Yes. My program knows. A database column stores the complete file  
>>> name  (including extension), and I can be certain the applications  
>>> will be  available to run the file.
>>
>> You still haven't answered the question.
>>
>> We have established that
>> a) The OS knows what program is associated with the file
>> b) your program knows the file name and location
>>
>> But does your program - or more specifically you as the author -  
>> know what program the OS will use to open the file?

Yes. My program knows which program to use (that will be installed).
>> If you do you can call it explicitly, but if you do not then you  
>> need to find a way of getting the OS to tell you, or to leave it to  
>> the OS.
I'm interested in nthis for the sake of generalizing (which is  
better). How can I get the OS to tell me which program to use. Or  
alternatively, how to tell the OS to open it - assuming that since the  
os knows which program to use it will just use it (perhaps too big an  
assumption ;-)

>> For example a txt file could be opened by any of hundreds of text  
>> editors, depending on what the user selected, but a Photoshop file  
>> will typically only be opened by photoshop.
>>
>> HTH,

Txs,
Miguel


________________________________________________________________________________
VIVA os SEUS SONHOS com o Cr?dito Pessoal Capital Mais.
Pe?a aqui at? 15.000 Euros: http://www.iol.pt/correio/rodape.php?dst=0901052

From toni at muybien.org  Tue Nov 17 21:23:15 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Tue, 17 Nov 2009 20:23:15 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>
References: <20091117165639.GA3411@cateto>
	<333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>
Message-ID: <20091117202315.GA25231@cateto>

* Wayne Werner <waynejwerner at gmail.com> [2009-11-17 11:41:25 -0600]:

> Date: Tue, 17 Nov 2009 11:41:25 -0600
> From: Wayne Werner <waynejwerner at gmail.com>
> To: Antonio de la Fuente <toni at muybien.org>
> Cc: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> Message-ID: <333efb450911170941g709e7ea3l4b4316044be0990d at mail.gmail.com>
> 
> On Tue, Nov 17, 2009 at 10:58 AM, Antonio de la Fuente <toni at muybien.org>wrote:
> 
> > Hi everybody,
> >
> > This is my first post here. I have started learning python and I am new to
> > programing, just some bash scripting, no much.
> > Thank you for the kind support and help that you provide in this list.
> >
> 
> You're welcome!
> 
> 
> >
> > This is my problem: I've got a log file that is filling up very quickly,
> > this
> > log file is made of blocks separated by a blank line, inside these blocks
> > there
> > is a line "foo", I want to discard blocks with that line inside it, and
> > create a
> > new log file, without those blocks, that will reduce drastically the size
> > of the
> > log file.
> >
[...] 
> 
> Let me start out by saying this; I'm very impressed with the thought you've
> put into the problem and the way you've presented it.
> 

Thank you, it took me some time, but it did help me to understand
better the problem.

> The first thing that pops into my mind is to simply strip whitespace from
> the line and check if the line == ''. Upon further experiment there's the
> "isspace" method:
> 
> In [24]: x = '   \n\n\r\t\t'
> 
> In [25]: x.isspace()
> Out[25]: True
> 
> x contains a bunch of spaces, newlines, and tab chars. From the docs:
> 
> " Return True if all characters in S are whitespace
>     and there is at least one character in S, False otherwise."
> 

So, I could use it like this:
while not line.isspace()
      if line == 'foo':
           Somehow re-initialise myList 
	   break
	   [and the rest]

> 
> 
> > I don't expect the solution, as I think this is a great exercise to get wet
> > with python, but if anyone thinks that this is the wrong way of solving the
> > problem, please let me know.
> > <snip some code>
> > for line in fileIn:
> >    while line != 'blank_line':
> >        if line == 'foo':
> >            Somehow re-initialise myList
> >            break
> >        else:
> >            myList.append(line)
> >    fileOut.writelines(myList)
> 
>  <snip some more>
> 
> 
> Rather than using a while, you can use an if statement with the space method
> (and join your statement with an:
> 
> if not line.isspace() and not line == 'foo':
>     fileOut.write(line)
> 
But then, the new log file will have all the blocks, even the ones that
had 'foo' on it, even if the foo lines weren't there anymore. No? or
is there anything that I don't get?

> then you can get rid of the whole myList. Based on what you've written,
> there's really no need to have a list, it's more efficient to just write the
> line straight to the file.
My idea of using a list was that I can put the block in the list, and
if block does not content line with 'foo' then write it to file,
otherwise discard block.
> 
> for the renaming part, take a look at the shutil module.
> 
> HTH,
> Wayne
Thank you Wayne.
-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

Dios mira las manos limpias, no las llenas.
		-- Publio Siro. 

From bgailer at gmail.com  Tue Nov 17 21:26:20 2009
From: bgailer at gmail.com (bob gailer)
Date: Tue, 17 Nov 2009 15:26:20 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117165639.GA3411@cateto>
References: <20091117165639.GA3411@cateto>
Message-ID: <4B0306EC.8000105@gmail.com>

Antonio de la Fuente wrote:
> Hi everybody,
>
> This is my first post here. I have started learning python and I am new to
> programing, just some bash scripting, no much. 
> Thank you for the kind support and help that you provide in this list.
>
> This is my problem: I've got a log file that is filling up very quickly, this
> log file is made of blocks separated by a blank line, inside these blocks there
> is a line "foo", I want to discard blocks with that line inside it, and create a
> new log file, without those blocks, that will reduce drastically the size of the
> log file. 
>
> The log file is gziped, so I am going to use gzip module, and I am going to pass
> the log file as an argument, so sys module is required as well.
>
> I will read lines from file, with the 'for loop', and then I will check them for
> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> list, and if there is no matches for foo, I will append line to the list. When I
> get to a blank line (end of block), write myList to an external file. And start
> with another line.
>
> I am stuck with defining 'blank line', I don't manage to get throught the while
> loop, any hint here I will really appreciate it.
> I don't expect the solution, as I think this is a great exercise to get wet
> with python, but if anyone thinks that this is the wrong way of solving the
> problem, please let me know.
>
>
> #!/usr/bin/python
>
> import sys
> import gzip
>
> myList = []
>
> # At the moment not bother with argument part as I am testing it with a
> # testing log file
> #fileIn = gzip.open(sys.argv[1])
>
> fileIn = gzip.open('big_log_file.gz', 'r')
> fileOut = open('outputFile', 'a')
>
> for line in fileIn:
>     while line != 'blank_line':
>         if line == 'foo':
>             Somehow re-initialise myList
> 	    break
>         else:
>             myList.append(line)
>     fileOut.writelines(myList)
>   
Observations:
0 - The other responses did not understand your desire to drop any  
paragraph containing 'foo'.
1 - The while loop will run forever, as it keeps processing the same line.
2 - In your sample log file the line with 'foo' starts with a tab. line 
== 'foo' will always be false.
3 - Is the first line in the file Tue Nov 17 16:11:47 GMT 2009 or blank?
4 - Is the last line blank?

Better logic:

# open files
paragraph = []
keep = True
for line in fileIn:
  if line.isspace(): # end of paragraph
    if keep:
      outFile.writelines(paragraph)
    paragraph = []
    keep = True
  else:
    if keep:
      if line == '\tfoo':
        keep = False
      else:
        paragraph.append(line)
# anticipating last line not blank, write last paragraph
if keep:
   outFile.writelines(paragraph)

# use shutil to rename


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From waynejwerner at gmail.com  Tue Nov 17 21:32:47 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 17 Nov 2009 14:32:47 -0600
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117202315.GA25231@cateto>
References: <20091117165639.GA3411@cateto>
	<333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com> 
	<20091117202315.GA25231@cateto>
Message-ID: <333efb450911171232l4fe91abbhc121a02172bc368f@mail.gmail.com>

On Tue, Nov 17, 2009 at 2:23 PM, Antonio de la Fuente <toni at muybien.org>wrote:

> But then, the new log file will have all the blocks, even the ones that
> had 'foo' on it, even if the foo lines weren't there anymore. No? or
> is there anything that I don't get?
>

Ah yes, I forgot about that part.

So you should probably keep the list.

-Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091117/3f1f0667/attachment.htm>

From toni at muybien.org  Tue Nov 17 21:36:40 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Tue, 17 Nov 2009 20:36:40 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117183051.GA20117@stinemates.org>
References: <20091117165639.GA3411@cateto>
	<20091117183051.GA20117@stinemates.org>
Message-ID: <20091117203640.GB25231@cateto>

* Nick Stinemates <nick at stinemates.org> [2009-11-17 13:30:51 -0500]:

> Date: Tue, 17 Nov 2009 13:30:51 -0500
> From: Nick Stinemates <nick at stinemates.org>
> To: Antonio de la Fuente <toni at muybien.org>
> Cc: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> Mail-Followup-To: Antonio de la Fuente <toni at muybien.org>,
> 	Python Tutor mailing list <tutor at python.org>
> User-Agent: Mutt/1.5.20 (2009-06-14)
> Message-ID: <20091117183051.GA20117 at stinemates.org>
> 
> > I will read lines from file, with the 'for loop', and then I will check them for
> > 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> > list, and if there is no matches for foo, I will append line to the list. When I
> > get to a blank line (end of block), write myList to an external file. And start
> 
> Can you please explain what you mean by _re-initialize the list_ ?
>

By re-initialize the list I was thinking to get rid of the lines that
have been put into it, until it has been found one with 'foo', if not
it will contaminated the new log file, with lines from blocks that had
'foo' lines.

> > with another line.
> > 
> > I am stuck with defining 'blank line', I don't manage to get throught the while
> > loop, any hint here I will really appreciate it.
> > I don't expect the solution, as I think this is a great exercise to get wet
> > with python, but if anyone thinks that this is the wrong way of solving the
> > problem, please let me know.
> > 
> > 
> 
> Can you explain why the following won't work?
>

Because I don't know how to define a blank line, that will allow me to
differentiate between blocks.

> 
> #!/usr/bin/python
>  
> import sys
> import gzip
> # At the moment not bother with argument part as I am testing it with a
> # testing log file
> #fileIn = gzip.open(sys.argv[1])
> 
> fileIn = gzip.open('big_log_file.gz', 'r')
> fileOut = open('outputFile', 'a')
> 
> for line in fileIn:
>     while line != 'blank_line':
>         if 'foo' not in line:
>             fileOut.write(line)
[...] 

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

El que con ni?os se acuesta, mojado se levanta. 

From kent37 at tds.net  Tue Nov 17 21:50:49 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 17 Nov 2009 15:50:49 -0500
Subject: [Tutor] Processing rows from CSV
In-Reply-To: <545517726-1258484248-cardhu_decombobulator_blackberry.rim.net-1199407271-@bda061.bisx.produk.on.blackberry>
References: <545517726-1258484248-cardhu_decombobulator_blackberry.rim.net-1199407271-@bda061.bisx.produk.on.blackberry>
Message-ID: <1c2a2c590911171250t73a4300ete3063d7bd9746442@mail.gmail.com>

On Tue, Nov 17, 2009 at 1:57 PM,  <mhw at doctors.net.uk> wrote:

> A rather general question, I'm afraid. I have found myself writing some python code to handle some CSV data, using the csv. DictReader that generates a dict for each row with the key as the column heading and the value in the file as the item. Most operations involve code of the form: (Apologies for incorrect caps)
>
> For row in reader:
> ? ?If row['foo'] == 'something' :
> ? ? ? ?do this etc.

> My other question was about how I do this more efficiently: I don't want to read the whole thing into memory at once, but (AFAIK) csv.DictReader doesn't support something like readline() to deliver it one at a time.

The loop above does deliver one line worth of data at a time.

Kent

From davea at ieee.org  Tue Nov 17 22:30:43 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 17 Nov 2009 16:30:43 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117165639.GA3411@cateto>
References: <20091117165639.GA3411@cateto>
Message-ID: <4B031603.4000306@ieee.org>

Antonio de la Fuente wrote:
> Hi everybody,
>
> This is my first post here. I have started learning python and I am new to
> programing, just some bash scripting, no much. 
> Thank you for the kind support and help that you provide in this list.
>
> This is my problem: I've got a log file that is filling up very quickly, this
> log file is made of blocks separated by a blank line, inside these blocks there
> is a line "foo", I want to discard blocks with that line inside it, and create a
> new log file, without those blocks, that will reduce drastically the size of the
> log file. 
>
> The log file is gziped, so I am going to use gzip module, and I am going to pass
> the log file as an argument, so sys module is required as well.
>
> I will read lines from file, with the 'for loop', and then I will check them for
> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> list, and if there is no matches for foo, I will append line to the list. When I
> get to a blank line (end of block), write myList to an external file. And start
> with another line.
>
> I am stuck with defining 'blank line', I don't manage to get throught the while
> loop, any hint here I will really appreciate it.
> I don't expect the solution, as I think this is a great exercise to get wet
> with python, but if anyone thinks that this is the wrong way of solving the
> problem, please let me know.
>
>
> #!/usr/bin/python
>
> import sys
> import gzip
>
> myList =]
>
> # At the moment not bother with argument part as I am testing it with a
> # testing log file
> #fileIn =zip.open(sys.argv[1])
>
> fileIn =zip.open('big_log_file.gz', 'r')
> fileOut =pen('outputFile', 'a')
>
> for line in fileIn:
>     while line !=blank_line':
>         if line ='foo':
>             Somehow re-initialise myList
> 	    break
>         else:
>             myList.append(line)
>     fileOut.writelines(myList)
>
>
> Somehow rename outputFile with big_log_file.gz
>
> fileIn.close()
> fileOut.close()
>
> -------------------------------------------------------------
>
> The log file will be fill with:
>
>
> Tue Nov 17 16:11:47 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
>
> Tue Nov 17 16:12:58 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	foo
> 	tatati tatata
>
> Tue Nov 17 16:13:42 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
>
>
> etc, etc ,etc
> ..............................................................
>
> Again, thank you.
>
>   
You've got some good ideas, and I'm going to give you hints, rather than 
just writing it for you, as you suggested.

First, let me point out that there are advanced features in Python that 
could make a simple program that'd be very hard for a beginner to 
understand.  I'll give you the words, but recommend that you not try it 
at this time.  If you were to wrap the file in a generator that returned 
you a "paragraph" at a time, the same way as it's now returning a line 
at a time, then the loop would be simply a for-loop on that generator, 
checking each paragraph for whether it contained "foo" and if so, 
writing it to the output.


But you can also do it without using advanced features, and that's what 
I'm going to try to outline.

Two things you'll be testing each line for:  is it blank, and is it "foo".
   if line.isspace()  will test if a line is whitespace only, as Wayne 
pointed out.
   if line == "foo" will test if a line has exactly "foo" in it.  But if 
you apparently have leading whitespace, and
trailing newlines, and if they're irrelevant, then you might want
   if line.strip() == "foo"

I would start by just testing for blank lines.   Try replacing all blank 
lines with "***** blank line ****"  and print each
line. See whether the output makes sense.  if it does, go on to the next 
step.
   for line in ....
         if line-is-blank
               line-is-fancy-replacement
        print line

Now, instead of just printing the line, add it to a list object.  Create 
an object called paragraph(rather than a file) as an empty list object, 
before the for loop.
Inside the for loop, if the line is non-empty, add it to the paragraph.  
If the line is empty, then print the paragraph (with something before 
and after it,
so you can see what came from each print stmt).  Then blank it (outlist 
= []).
Check whether this result looks good, and if so, continue on.

Next version of the code: whenever you have a non-blank line, in 
addition to adding it to the list, also check it for whether it's  
equal-foo.
If so, set a flag.  When printing the outlist, skip the printing if the 
flag is set.  Remember that you'll have to clear this flag each time you 
blank
the mylist, both before the loop, and in the middle of the loop.

Once this makes sense, you can worry about actually writing the output 
to a real file, maybe compressing it, maybe doing deletes and renames
as appropriate.   You probably don't need shutil module,  os module 
probably has enough functions for this.

At any of these stages, if you get stuck, call for help.  But your code 
will be only as complex as that stage needs, so we can find one bug at a 
time.

DaveA


From kent37 at tds.net  Tue Nov 17 22:32:03 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 17 Nov 2009 16:32:03 -0500
Subject: [Tutor] Do you use unit testing?
In-Reply-To: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com>
Message-ID: <1c2a2c590911171332w41fd15dcq96eed9adba854d02@mail.gmail.com>

On Mon, Nov 16, 2009 at 3:54 PM, Modulok <modulok at gmail.com> wrote:
> List,
>
> A general question:
>
> How many of you guys use unit testing as a development model, or at
> all for that matter?
>
> ?I just starting messing around with it and it seems painfully slow to
> have to write a test for everything you do. Thoughts, experiences,
> pros, cons?

In my opinion TDD is awesome :-) Why?

- It gives you high confidence that your code does what you think it
does, and that you can rely on it
- It can be a lot of fun to write a test, then make it pass,
repeat...each time you make the test pass is a little pat on the back
- good work!
- It forces you to think as a client of the code that you are writing
- It puts pressure on your design, to write code that is
  - testable (duh)
  - loosely coupled
  - reusable (to some extent), because you start with two clients

That's just the benefit when you initially write the code. Just as
important is the benefit downstream, when you want to make a change.
Having a good test suite allows you to make changes to the code (for
example refactoring) with confidence that if you break anything, you
will find out quickly.

This is a profound shift. On projects without tests, there tends to be
code whose function is not clear, or whose structure is tangled, which
no one wants to touch for fear of breaking something. Generally there
is an undercurrent of fear of breakage that promotes code rot.

On projects with tests, fear is replaced with confidence. Messes can
be cleaned up, dead code stripped out, etc. So tests promote a healthy
codebase over time.

There is a cost but it is smaller than it looks from the outside. Yes,
you have to learn to use a test framework, but that is a one-time
cost. I use unittest because it follows the xUnit pattern so it is
familiar across Python, Java, C#, etc. There are alternatives such as
doctest, nose and py.test that some people prefer. I have written a
brief intro to unittest here, with links to comparison articles:
http://personalpages.tds.net/~kent37/kk/00014.html

You will occasionally have to stop and figure out how to test
something new and perhaps write some test scaffolding. That time will
pay off hugely as you write tests.

You have to write the tests, but what were you doing before? How do
you know your code works? You must be doing some kind of tests. If you
write your tests as automated unit tests they are preserved and useful
in the future, and probably more comprehensive than what you would do
by hand. If you test by hand, your tests are lost when you finish.

Finally I confess that GUI unit tests are very difficult. I have often
omitted them, instead trying to write a thin GUI over a testable layer
of functionality. In my current (WinForms, C#) project we have found a
workable way to write GUI tests using NUnitForms so in the future
maybe I will be writing more GUI unit tests.

More advocacy:
http://c2.com/cgi/wiki?TestDrivenDevelopment
http://c2.com/cgi/wiki?CodeUnitTestFirst

Kent

From alan.gauld at btinternet.com  Tue Nov 17 22:33:46 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 21:33:46 -0000
Subject: [Tutor] opening a file directly from memory
References: <20091117183708.gp7so8yqs40o4ook@webmail.iol.pt>
Message-ID: <hdv4s9$52d$1@ger.gmane.org>

<mjekl at iol.pt> wrote 

>>> If you do you can call it explicitly, but if you do not then you  
>>> need to find a way of getting the OS to tell you, or to leave it to  
>>> the OS.

> I'm interested in nthis for the sake of generalizing (which is  
> better). How can I get the OS to tell me which program to use. 
> alternatively, how to tell the OS to open it - assuming that since the  
> os knows which program to use it will just use it 

This is where it gets messy.

The solution depends on the OS. If its Windows you can use Startfile.
If its MacOS you can interrogate the package manifest.
If its another Unix you can use any of several options depending on the 
flavour. The simplest is, I think, the file command, then if its a 
text file you can check the shebang line at the top of the file. But 
modern Unices, like Linux have file association tables - but these 
are often associated with the desktop environment - KDE, Gnome etc.
Finally for text files you should check the EDITOR and VISUAL 
environment variables - although these are increasingly not 
used or respected nowadays.

So you could write a program that checked the OS and then tried 
all of these options to identify the host application. But even then 
you are not guaranteed to succeed!

Finally you can try just running the file via os.system or the 
subprocess module and see what happens!

But there is no foolproof way of doing it on all OS. That's why 
its easier if you either know what app to use or create a 
config file such that the user can specify the app at install time.
On unix that would traditionally be called .myapprc aand be 
stored in each users home directory.

On Windows it would either be a registry entry or a myapp.ini file,
usually stored in the Windows directory or (better IMHO but against 
MS guidelines) in the app directory.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From alan.plum at uni-koeln.de  Tue Nov 17 22:50:03 2009
From: alan.plum at uni-koeln.de (Alan Plum)
Date: Tue, 17 Nov 2009 22:50:03 +0100
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <hdv4s9$52d$1@ger.gmane.org>
References: <20091117183708.gp7so8yqs40o4ook@webmail.iol.pt>
	<hdv4s9$52d$1@ger.gmane.org>
Message-ID: <1258494603.3397.5.camel@kallisti>

Dammit. Meant to send this over the list. Sorry, Alan.

On Di, 2009-11-17 at 21:33 +0000, Alan Gauld wrote:
> Unices, like Linux have file association tables - but these 
> are often associated with the desktop environment - KDE, Gnome etc.
> Finally for text files you should check the EDITOR and VISUAL 
> environment variables - although these are increasingly not 
> used or respected nowadays.

I find mimemagic to be quite reliable. File can be silly at times (it
routinely diagnoses my python scripts as Java source files). In my
gopher server I rely on mimemagic to make an educated guess and only use
file(1) as a last resort to find out whether it's a binary file or ASCII
text (some gopher clients don't support UTF8).

I'm not sure whether mimemagic is also available on other OSes, tho.


Cheers

Alan



From kent37 at tds.net  Tue Nov 17 22:56:52 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 17 Nov 2009 16:56:52 -0500
Subject: [Tutor] opening a file directly from memory
In-Reply-To: <20091117183708.gp7so8yqs40o4ook@webmail.iol.pt>
References: <20091117183708.gp7so8yqs40o4ook@webmail.iol.pt>
Message-ID: <1c2a2c590911171356id5d1797re6f4930ab756262e@mail.gmail.com>

On Tue, Nov 17, 2009 at 1:37 PM,  <mjekl at iol.pt> wrote:
>> Alan Gauld wrote:
>>>
>>> <mjekl at iol.pt> wrote

>>> If you do you can call it explicitly, but if you do not then you need to
>>> find a way of getting the OS to tell you, or to leave it to the OS.
>
> I'm interested in nthis for the sake of generalizing (which is better). How
> can I get the OS to tell me which program to use. Or alternatively, how to
> tell the OS to open it - assuming that since the os knows which program to
> use it will just use it (perhaps too big an assumption ;-)

The "desktop" package provides a portable open() function:
http://pypi.python.org/pypi/desktop

It came out of this discussion (extensive, with links):
http://bugs.python.org/issue1301512

Kent

From alan.gauld at btinternet.com  Tue Nov 17 23:11:39 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 17 Nov 2009 22:11:39 -0000
Subject: [Tutor] Introduction - log exercise
References: <20091117165639.GA3411@cateto><333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>
	<20091117202315.GA25231@cateto>
Message-ID: <hdv73a$c9e$1@ger.gmane.org>


"Antonio de la Fuente" <toni at muybien.org> wrote 

> > if not line.isspace() and not line == 'foo':
> >     fileOut.write(line)
> 
> But then, the new log file will have all the blocks, even the ones that
> had 'foo' on it, even if the foo lines weren't there anymore. No? or
> is there anything that I don't get?

I think the test should be:

if not line.isspace and 'foo' not in line:
     fileOut.write(line)

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From toni at muybien.org  Tue Nov 17 23:38:26 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Tue, 17 Nov 2009 22:38:26 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <4B0306EC.8000105@gmail.com>
References: <20091117165639.GA3411@cateto>
 <4B0306EC.8000105@gmail.com>
Message-ID: <20091117223826.GC25231@cateto>

* bob gailer <bgailer at gmail.com> [2009-11-17 15:26:20 -0500]:

> Date: Tue, 17 Nov 2009 15:26:20 -0500
> From: bob gailer <bgailer at gmail.com>
> To: Antonio de la Fuente <toni at muybien.org>
> CC: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
> Message-ID: <4B0306EC.8000105 at gmail.com>
> 
> Antonio de la Fuente wrote:
> >Hi everybody,
> >
> >This is my first post here. I have started learning python and I am new to
> >programing, just some bash scripting, no much. Thank you for the
> >kind support and help that you provide in this list.
> >
> >This is my problem: I've got a log file that is filling up very quickly, this
> >log file is made of blocks separated by a blank line, inside these blocks there
> >is a line "foo", I want to discard blocks with that line inside it, and create a
> >new log file, without those blocks, that will reduce drastically the size of the
> >log file.
> >
> >The log file is gziped, so I am going to use gzip module, and I am going to pass
> >the log file as an argument, so sys module is required as well.
> >
> >I will read lines from file, with the 'for loop', and then I will check them for
> >'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> >list, and if there is no matches for foo, I will append line to the list. When I
> >get to a blank line (end of block), write myList to an external file. And start
> >with another line.
> >
> >I am stuck with defining 'blank line', I don't manage to get throught the while
> >loop, any hint here I will really appreciate it.
> >I don't expect the solution, as I think this is a great exercise to get wet
> >with python, but if anyone thinks that this is the wrong way of solving the
> >problem, please let me know.
> >
> >
> >#!/usr/bin/python
> >
> >import sys
> >import gzip
> >
> >myList = []
> >
> ># At the moment not bother with argument part as I am testing it with a
> ># testing log file
> >#fileIn = gzip.open(sys.argv[1])
> >
> >fileIn = gzip.open('big_log_file.gz', 'r')
> >fileOut = open('outputFile', 'a')
> >
> >for line in fileIn:
> >    while line != 'blank_line':
> >        if line == 'foo':
> >            Somehow re-initialise myList
> >	    break
> >        else:
> >            myList.append(line)
> >    fileOut.writelines(myList)
> Observations:
> 0 - The other responses did not understand your desire to drop any
> paragraph containing 'foo'.

Yes, paragraph == block, that's it

> 1 - The while loop will run forever, as it keeps processing the same line.

Because the tabs in the line with foo?!

> 2 - In your sample log file the line with 'foo' starts with a tab.
> line == 'foo' will always be false.

So I need first to get rid of those tabs, right? I can do that with
line.strip(), but then I need the same formatting for the fileOut.

> 3 - Is the first line in the file Tue Nov 17 16:11:47 GMT 2009 or blank?

First line is Tue Nov 17 16:11:47 GMT 2009

> 4 - Is the last line blank?

last line is blank.

> 
> Better logic:
> 
I would have never thought this way of solving the problem. Interesting.
> # open files
> paragraph = []
> keep = True
> for line in fileIn:
>  if line.isspace(): # end of paragraph 

Aha! finding the blank line

>    if keep:
>      outFile.writelines(paragraph)
>    paragraph = []

This is what I called re-initialising the list.

>    keep = True
>  else:
>    if keep:
>      if line == '\tfoo':
>        keep = False
>      else:
>        paragraph.append(line)
> # anticipating last line not blank, write last paragraph
> if keep:
>   outFile.writelines(paragraph)
> 
> # use shutil to rename
> 
Thank you.

> 
> -- 
> Bob Gailer
> Chapel Hill NC
> 919-636-4239

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

The problem with people who have no vices is that generally you can
be pretty sure they're going to have some pretty annoying virtues.
		-- Elizabeth Taylor

From toni at muybien.org  Wed Nov 18 01:31:56 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Wed, 18 Nov 2009 00:31:56 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <4B031603.4000306@ieee.org>
References: <20091117165639.GA3411@cateto>
 <4B031603.4000306@ieee.org>
Message-ID: <20091118003156.GA4478@cateto>

* Dave Angel <davea at ieee.org> [2009-11-17 16:30:43 -0500]:

> Date: Tue, 17 Nov 2009 16:30:43 -0500
> From: Dave Angel <davea at ieee.org>
> To: Antonio de la Fuente <toni at muybien.org>
> CC: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
> Message-ID: <4B031603.4000306 at ieee.org>
> 
> Antonio de la Fuente wrote:
> >Hi everybody,
> >

[...] 

> >This is my problem: I've got a log file that is filling up very quickly, this
> >log file is made of blocks separated by a blank line, inside these blocks there
> >is a line "foo", I want to discard blocks with that line inside it, and create a
> >new log file, without those blocks, that will reduce drastically the size of the
> >log file.
> >

[...] 

> You've got some good ideas, and I'm going to give you hints, rather
> than just writing it for you, as you suggested.
> 
Much appreciated, really.

> First, let me point out that there are advanced features in Python
> that could make a simple program that'd be very hard for a beginner
> to understand.  I'll give you the words, but recommend that you not
> try it at this time.  If you were to wrap the file in a generator
> that returned you a "paragraph" at a time, the same way as it's now
> returning a line at a time, then the loop would be simply a for-loop
> on that generator, checking each paragraph for whether it contained
> "foo" and if so, writing it to the output.
> 
> 
> But you can also do it without using advanced features, and that's
> what I'm going to try to outline.
> 

[...] 

> Inside the for loop, if the line is non-empty, add it to the
> paragraph.  If the line is empty, then print the paragraph (with
> something before and after it,
> so you can see what came from each print stmt).  Then blank it
> (outlist = []).
> Check whether this result looks good, and if so, continue on.

for line in fileIn:                                                                                      
    if line.isspace():                                                                                   
        print "***** blank line ****"                                                                    
        print myList                                                                                     
	print "***** fancy blank line ****"
        myList = []                                                                                      
    else:                                                                                                
        myList.append(line)       

I think is what i expect, but confuse me that is in this format:

['Tue Nov 17 16:11:47 GMT 2009\n'], '\tbladi bladi bla', '\ttarila ri la\n', '\tpatatin pataton\n', '\ttatati tatata\n', '\tfoo\n']
***** fancy blank line ****
***** blank line ****

with linefeeds and tabs all over, I see why everybody calls it
paragraph.
Once I write to a file from the list, it will comeback the initial
format of the file?

> 
> Next version of the code: whenever you have a non-blank line, in
> addition to adding it to the list, also check it for whether it's
> equal-foo.
> If so, set a flag.  When printing the outlist, skip the printing if
> the flag is set.  Remember that you'll have to clear this flag each
> time you blank
> the mylist, both before the loop, and in the middle of the loop.

I am a bit lost with the flags, is it what Bob Gailer was calling keep =
True, keep = False, right?

> 
> Once this makes sense, you can worry about actually writing the
> output to a real file, maybe compressing it, maybe doing deletes and
> renames
> as appropriate.   You probably don't need shutil module,  os module
> probably has enough functions for this.
> 
> At any of these stages, if you get stuck, call for help.  But your
> code will be only as complex as that stage needs, so we can find one
> bug at a time.
> 
> DaveA
> 
Thank you, it has been very helpful.

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

Power, like a desolating pestilence,
Pollutes whate'er it touches...
		-- Percy Bysshe Shelley

From bgailer at gmail.com  Wed Nov 18 02:22:18 2009
From: bgailer at gmail.com (bob gailer)
Date: Tue, 17 Nov 2009 20:22:18 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <hdv73a$c9e$1@ger.gmane.org>
References: <20091117165639.GA3411@cateto><333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>	<20091117202315.GA25231@cateto>
	<hdv73a$c9e$1@ger.gmane.org>
Message-ID: <4B034C4A.1080109@gmail.com>

Alan Gauld wrote:
>
> "Antonio de la Fuente" <toni at muybien.org> wrote
>> > if not line.isspace() and not line == 'foo':
>> >     fileOut.write(line)
>>
>> But then, the new log file will have all the blocks, even the ones that
>> had 'foo' on it, even if the foo lines weren't there anymore. No? or
>> is there anything that I don't get?
>
> I think the test should be:
>
> if not line.isspace and 'foo' not in line:
>     fileOut.write(line)
>

No - that misses the objective of eliminating blocks containing 'foo'


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From bgailer at gmail.com  Wed Nov 18 02:24:37 2009
From: bgailer at gmail.com (bob gailer)
Date: Tue, 17 Nov 2009 20:24:37 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117223826.GC25231@cateto>
References: <20091117165639.GA3411@cateto> <4B0306EC.8000105@gmail.com>
	<20091117223826.GC25231@cateto>
Message-ID: <4B034CD5.6070507@gmail.com>

Antonio de la Fuente wrote:
> * bob gailer <bgailer at gmail.com> [2009-11-17 15:26:20 -0500]:
>
>   
>> Date: Tue, 17 Nov 2009 15:26:20 -0500
>> From: bob gailer <bgailer at gmail.com>
>> To: Antonio de la Fuente <toni at muybien.org>
>> CC: Python Tutor mailing list <tutor at python.org>
>> Subject: Re: [Tutor] Introduction - log exercise
>> User-Agent: Thunderbird 2.0.0.23 (Windows/20090812)
>> Message-ID: <4B0306EC.8000105 at gmail.com>
>>
>> Antonio de la Fuente wrote:
>>     
>>> Hi everybody,
>>>
>>> This is my first post here. I have started learning python and I am new to
>>> programing, just some bash scripting, no much. Thank you for the
>>> kind support and help that you provide in this list.
>>>
>>> This is my problem: I've got a log file that is filling up very quickly, this
>>> log file is made of blocks separated by a blank line, inside these blocks there
>>> is a line "foo", I want to discard blocks with that line inside it, and create a
>>> new log file, without those blocks, that will reduce drastically the size of the
>>> log file.
>>>
>>> The log file is gziped, so I am going to use gzip module, and I am going to pass
>>> the log file as an argument, so sys module is required as well.
>>>
>>> I will read lines from file, with the 'for loop', and then I will check them for
>>> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
>>> list, and if there is no matches for foo, I will append line to the list. When I
>>> get to a blank line (end of block), write myList to an external file. And start
>>> with another line.
>>>
>>> I am stuck with defining 'blank line', I don't manage to get throught the while
>>> loop, any hint here I will really appreciate it.
>>> I don't expect the solution, as I think this is a great exercise to get wet
>>> with python, but if anyone thinks that this is the wrong way of solving the
>>> problem, please let me know.
>>>
>>>
>>> #!/usr/bin/python
>>>
>>> import sys
>>> import gzip
>>>
>>> myList = []
>>>
>>> # At the moment not bother with argument part as I am testing it with a
>>> # testing log file
>>> #fileIn = gzip.open(sys.argv[1])
>>>
>>> fileIn = gzip.open('big_log_file.gz', 'r')
>>> fileOut = open('outputFile', 'a')
>>>
>>> for line in fileIn:
>>>    while line != 'blank_line':
>>>        if line == 'foo':
>>>            Somehow re-initialise myList
>>> 	    break
>>>        else:
>>>            myList.append(line)
>>>    fileOut.writelines(myList)
>>>       
>> Observations:
>> 0 - The other responses did not understand your desire to drop any
>> paragraph containing 'foo'.
>>     
>
> Yes, paragraph == block, that's it
>
>   
>> 1 - The while loop will run forever, as it keeps processing the same line.
>>     
>
> Because the tabs in the line with foo?!
>   

No - because within the loop there is nothing reading the next line of 
the file!
>   
>> 2 - In your sample log file the line with 'foo' starts with a tab.
>> line == 'foo' will always be false.
>>     
>
> So I need first to get rid of those tabs, right? I can do that with
> line.strip(), but then I need the same formatting for the fileOut.
>
>   
>> 3 - Is the first line in the file Tue Nov 17 16:11:47 GMT 2009 or blank?
>>     
>
> First line is Tue Nov 17 16:11:47 GMT 2009
>
>   
>> 4 - Is the last line blank?
>>     
>
> last line is blank.
>
>   
>> Better logic:
>>
>>     
> I would have never thought this way of solving the problem. Interesting.
>   
>> # open files
>> paragraph = []
>> keep = True
>> for line in fileIn:
>>  if line.isspace(): # end of paragraph 
>>     
>
> Aha! finding the blank line
>
>   
>>    if keep:
>>      outFile.writelines(paragraph)
>>    paragraph = []
>>     
>
> This is what I called re-initialising the list.
>
>   
>>    keep = True
>>  else:
>>    if keep:
>>      if line == '\tfoo':
>>        keep = False
>>      else:
>>        paragraph.append(line)
>> # anticipating last line not blank, write last paragraph
>> if keep:
>>   outFile.writelines(paragraph)
>>
>> # use shutil to rename
>>
>>     
> Thank you.
>
>   
>> -- 
>> Bob Gailer
>> Chapel Hill NC
>> 919-636-4239
>>     
>
>   


-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From davea at ieee.org  Wed Nov 18 03:28:58 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 17 Nov 2009 21:28:58 -0500
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091118003156.GA4478@cateto>
References: <20091117165639.GA3411@cateto> <4B031603.4000306@ieee.org>
	<20091118003156.GA4478@cateto>
Message-ID: <4B035BEA.7010608@ieee.org>



Antonio de la Fuente wrote:
> * Dave Angel <davea at ieee.org> [2009-11-17 16:30:43 -0500]:
>
>   
>> <snip>
>>     
> for line in fileIn:                                                                                      
>     if line.isspace():                                                                                   
>         print "***** blank line ****"                                                                    
>         print myList                                                                                     
> 	print "***** fancy blank line ****"
>         myList =]                                                                                      
>     else:                                                                                                
>         myList.append(line)       
>
> I think is what i expect, but confuse me that is in this format:
>
> ['Tue Nov 17 16:11:47 GMT 2009\n'], '\tbladi bladi bla', '\ttarila ri la\n', '\tpatatin pataton\n', '\ttatati tatata\n', '\tfoo\n']
> ***** fancy blank line ****
> ***** blank line ****
>
> with linefeeds and tabs all over, I see why everybody calls it
> paragraph.
> Once I write to a file from the list, it will comeback the initial
> format of the file?
>
>   
No, when you want to write it to the file, you'll need to loop through 
the list.  There is a shortcut, however.  If you simply do:
        stdout.write(  "".join(myList)  )

it will join all the strings in the list together into one big string 
and then send it to stdout.  You can do that because each line still has 
its newline at the end.

BTW, the list you quoted above is malformed.  You really need to 
copy&paste code and data into the message, so information isn't lost.  
You have an extra right bracket and a missing \n in there.


>> Next version of the code: whenever you have a non-blank line, in
>> addition to adding it to the list, also check it for whether it's
>> equal-foo.
>> If so, set a flag.  When printing the outlist, skip the printing if
>> the flag is set.  Remember that you'll have to clear this flag each
>> time you blank
>> the mylist, both before the loop, and in the middle of the loop.
>>     
>
> I am a bit lost with the flags, is it what Bob Gailer was calling keep True, keep = False, right?
>
>   
You can certainly call it 'keep'   The point is, it'll tell you whether 
to output a particular paragraph or not.
>> Once this makes sense, you can worry about actually writing the
>> output to a real file, maybe compressing it, maybe doing deletes and
>> renames
>> as appropriate.   You probably don't need shutil module,  os module
>> probably has enough functions for this.
>>
>> At any of these stages, if you get stuck, call for help.  But your
>> code will be only as complex as that stage needs, so we can find one
>> bug at a time.
>>
>> DaveA
>>
>>     
> Thank you, it has been very helpful.
>
>   
You're very welcome.

DaveA

From bibsmendez at gmail.com  Wed Nov 18 05:12:09 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Wed, 18 Nov 2009 07:12:09 +0300
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <4B019A3B.5010100@ieee.org>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
	<dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>
	<4B019A3B.5010100@ieee.org>
Message-ID: <f16f1f8c0911172012q73ae78c8v8325f184a29a1798@mail.gmail.com>

Hi guys!

Thank you all for the brainstorming. As much as i love to follow all your
tips but sorry I got lost in the sea of discussion :-) Therefore i thought i
start to roll out my own and i hope you guys can chime in.

The print lines in my code are just for debugging. Of course they can be
omitted but for a newbie like me I helps to see what I'm doing is what I'm
expected to do.

I would like to improve my line search with the module re but cant find
something to get me started. I appreciate if you can show me how. The
'proxy' search works but I'm sure with re module the search will be definite
i mean wont yield 'false positive' result, sort-of.

http://pastebin.ca/1675865


-- 
Best Regards,
bibimidi

Sent from Dhahran, 04, Saudi Arabia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091118/0da6cd34/attachment.htm>

From alan.gauld at btinternet.com  Wed Nov 18 09:38:23 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 18 Nov 2009 08:38:23 -0000
Subject: [Tutor] Introduction - log exercise
References: <20091117165639.GA3411@cateto><333efb450911170941g709e7ea3l4b4316044be0990d@mail.gmail.com>	<20091117202315.GA25231@cateto><hdv73a$c9e$1@ger.gmane.org>
	<4B034C4A.1080109@gmail.com>
Message-ID: <he0bqe$vp9$1@ger.gmane.org>


"bob gailer" <bgailer at gmail.com> wrote

>> "Antonio de la Fuente" <toni at muybien.org> wrote
>>> > if not line.isspace() and not line == 'foo':
>>> >     fileOut.write(line)
>>>
>>> But then, the new log file will have all the blocks, even the ones that
>>> had 'foo' on it, even if the foo lines weren't there anymore. No? or
>>> is there anything that I don't get?
>>
>> I think the test should be:
>>
>> if not line.isspace and 'foo' not in line:
>>     fileOut.write(line)
>>
>
> No - that misses the objective of eliminating blocks containing 'foo'

Yeah, I was assuming "block" referred to a line not a paragraph.
I was simply extending the original post to remove the line if it
contained 'foo' rather than just being 'foo'. But it has since become
obvious that the OP needs more than that!

Alan G. 



From amit.pureenergy at gmail.com  Wed Nov 18 11:11:23 2009
From: amit.pureenergy at gmail.com (Amit Sethi)
Date: Wed, 18 Nov 2009 15:41:23 +0530
Subject: [Tutor] python wsgi
Message-ID: <da81a0a80911180211q52aa075el8c5dc7ef8a33c524@mail.gmail.com>

Hi ,

How do I run a python script using wsgi? I am not using any web
framework I just wish to try out running a simple script.

-- 
A-M-I-T S|S

From mjekl at iol.pt  Wed Nov 18 15:54:34 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Wed, 18 Nov 2009 14:54:34 +0000
Subject: [Tutor] Open Source database software
Message-ID: <20091118145434.4edcr66dwokwccck@webmail.iol.pt>

Kent Johnson wrote:
> On Mon, Nov 24, 2008 at 7:19 PM, Mike Meisner <mikem at blazenetme.net> wrote:
>
>> > 3.  A good GUI front end for creating the database, creating  
>> forms for user
>> > data input, queries, reports, etc.
>
> For this you might look at Dabo:
> http://dabodev.com/
>
> I haven't worked with it myself but some people like it a lot.
>
> PostgreSQL has pgAdmin which is very nice for basic admin but not for
> user applications.
>

This would also be my best advice.
The framework is very solid (core as been stable for a few years know).
Documentation exists, although it is lacking and scatered the mailing  
list is low traffic and very responsive.
I would not focus on using the visual tools, although a lot can  
already be accomplished with them (if you dig or ask you will find  
info on hand coding forms).
It is based on wxPython, and you can drop to that level any time. But  
in general you rather use the dabo interface as it will provide you  
with a much cleaner and productive implementation (quite marvelous),  
and with very good databinding.
Database access is abstracted so you can develop for/with SQLite and  
then just change the backend to one of a few flavours of choice (such  
as PostgreSQL, Firebird and other commercial options as well).

In a nutshelll :-)
Miguel

________________________________________________________________________________
VIVA os SEUS SONHOS com o Cr?dito Pessoal Capital Mais.
Pe?a aqui at? 15.000 Euros: http://www.iol.pt/correio/rodape.php?dst=0901052

From mjekl at iol.pt  Wed Nov 18 15:44:34 2009
From: mjekl at iol.pt (mjekl at iol.pt)
Date: Wed, 18 Nov 2009 14:44:34 +0000
Subject: [Tutor] opening a file directly from memory
Message-ID: <20091118144434.ty2402urqc480880@webmail.iol.pt>

Humm. Most enlighting.

For my case the solution is clearly to have an initialization file. In  
case the specified extension is not known then I'll prompt the user to  
save the file and pass on the responsibility.

Txs everyone,
Miguel


________________________________________________________________________________
Junte todos os seus cr?ditos no ?nico da Capital Mais...
...reduza as suas despesas mensais.
Saiba mais em http://www.iol.pt/correio/rodape.php?dst=0901051

From kent37 at tds.net  Wed Nov 18 16:56:42 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 18 Nov 2009 10:56:42 -0500
Subject: [Tutor] python wsgi
In-Reply-To: <da81a0a80911180211q52aa075el8c5dc7ef8a33c524@mail.gmail.com>
References: <da81a0a80911180211q52aa075el8c5dc7ef8a33c524@mail.gmail.com>
Message-ID: <1c2a2c590911180756m5fc5b45cj29d73ee7bafed146@mail.gmail.com>

On Wed, Nov 18, 2009 at 5:11 AM, Amit Sethi <amit.pureenergy at gmail.com> wrote:
> Hi ,
>
> How do I run a python script using wsgi? I am not using any web
> framework I just wish to try out running a simple script.

You need to run a WSGI-compliant server and configure it to host your
application. The std lib contains a simple reference server in
wsgiref, the make_server() example shows how to use it to run a demo
app:
http://docs.python.org/library/wsgiref.html#wsgiref.simple_server.make_server

If you want something more than that, look at the list here:
http://www.wsgi.org/wsgi/Servers

If I were to pick from that list for simple testing I would try CherryPy.

Kent

From goodpotatoes at yahoo.com  Wed Nov 18 22:51:35 2009
From: goodpotatoes at yahoo.com (GoodPotatoes)
Date: Wed, 18 Nov 2009 13:51:35 -0800 (PST)
Subject: [Tutor] Faster list searching?
Message-ID: <998415.64762.qm@web51808.mail.re2.yahoo.com>

I'm dealing with bigger lists than I have been, and noticed this is getting really slow.  Is there a faster way to do this?

for x in list1:
    if x not in list2:
        list3.append(x)

My search is taking up to 5 minutes to complete.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091118/753ca19a/attachment.htm>

From wescpy at gmail.com  Wed Nov 18 23:50:37 2009
From: wescpy at gmail.com (wesley chun)
Date: Wed, 18 Nov 2009 14:50:37 -0800
Subject: [Tutor] Faster list searching?
In-Reply-To: <998415.64762.qm@web51808.mail.re2.yahoo.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
Message-ID: <78b3a9580911181450h14409d04rfd99a5ed18b42903@mail.gmail.com>

On Wed, Nov 18, 2009 at 1:51 PM, GoodPotatoes <goodpotatoes at yahoo.com> wrote:
> I'm dealing with bigger lists than I have been, and noticed this is getting
> really slow.  Is there a faster way to do this?
>
> for x in list1:
>     if x not in list2:
>         list3.append(x)
>
> My search is taking up to 5 minutes to complete.


greetings! hopefully this isn't a homework problem as we cannot help
with those. can you give us an example of the lists that you're using
(not the entire things, but just tell us what they contain)? also, use
the timeit module to show us some numbers to confirm what you're
seeing.

my suggestion would be to use sets if possible instead of lists since
those lookups are way faster (hash vs. sequence). if you're using
Python 3, i think you can even build the solution set using set
comprehensions.

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com

From kent37 at tds.net  Wed Nov 18 23:59:56 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 18 Nov 2009 17:59:56 -0500
Subject: [Tutor] Faster list searching?
In-Reply-To: <998415.64762.qm@web51808.mail.re2.yahoo.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
Message-ID: <1c2a2c590911181459j55d4b6cm285bac1fed99301@mail.gmail.com>

On Wed, Nov 18, 2009 at 4:51 PM, GoodPotatoes <goodpotatoes at yahoo.com> wrote:
> I'm dealing with bigger lists than I have been, and noticed this is getting
> really slow.? Is there a faster way to do this?
>
> for x in list1:
> ??? if x not in list2:
> ??? ??? list3.append(x)
>
> My search is taking up to 5 minutes to complete.

If you can, use a set instead of list2. "x not in list2" does a linear
search of list2 for x, which takes time proportional to the length of
list2. Searching a set takes constant time - it doesn't depend on the
size of the set. Also you can use a list comprehension to speed up the
outer loop a little:

set_of_list2 = set(list2)
list3 = [ x for x in list1 if x not in set_of_list2 ]

Note this will only work if the elements of list2 are hashable
(useable as dict keys).

Kent

From toni at muybien.org  Thu Nov 19 00:06:26 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Wed, 18 Nov 2009 23:06:26 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <4B03A7EB.9@compuscan.co.za>
References: <20091117165639.GA3411@cateto>
 <4B03A7EB.9@compuscan.co.za>
Message-ID: <20091118230626.GB3372@cateto>

* Christian Witts <cwitts at compuscan.co.za> [2009-11-18 09:53:15 +0200]:

> Date: Wed, 18 Nov 2009 09:53:15 +0200
> From: Christian Witts <cwitts at compuscan.co.za>
> To: Antonio de la Fuente <toni at muybien.org>
> CC: Python Tutor mailing list <tutor at python.org>
> Subject: Re: [Tutor] Introduction - log exercise
> User-Agent: Thunderbird 2.0.0.23 (X11/20090817)
> Message-ID: <4B03A7EB.9 at compuscan.co.za>
> 
> How I would process it:
> 
> from sys import argv, exit
> import gzip
> 
> if __name__ == '__main__':
>    try:
>        fIn = gzip.open(argv[1])
>    except IOError:
>        exit('Cannot open file specified.\n')
> 
>    fOut = open('outputFile', 'a')
> 
>    datablock = []
>    discard = False
>    for line in fIn:
>        if line.strip():
>            if line.strip() == 'foo':
>                discard = True
>            else:
>                datablock.append(line)
>        else:
>            if discard:
>                datablock = []
>                discard = False
>            else:
>                fOut.write(''.join(datablock))
> 
>    fIn.close()
>    fOut.close()

I've borrowed a couple of things from your script.
It is educative for me to see different ways of solving the problem.
Thank you.
> 
> -- 
> Kind Regards,
> Christian Witts
> Business Intelligence
> 
> C o m p u s c a n | Confidence in Credit
> 
> Telephone: +27 21 888 6000
> National Cell Centre: 0861 51 41 31
> Fax: +27 21 413 2424
> E-mail: cwitts at compuscan.co.za
> 
> NOTE:  This e-mail (including attachments )is subject to the disclaimer published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
> If you cannot access the disclaimer, request it from email.disclaimer at compuscan.co.za or 0861 514131.
> 
> National Credit Regulator Credit Bureau Registration No. NCRCB6
> 
> 

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

... ich bin in einem dusenjet ins jahr 53 vor chr ... ich lande im
antiken Rom ...  einige gladiatoren spielen scrabble ... ich rieche
PIZZA ...

From bill at celestial.net  Thu Nov 19 00:03:22 2009
From: bill at celestial.net (Bill Campbell)
Date: Wed, 18 Nov 2009 15:03:22 -0800
Subject: [Tutor] Faster list searching?
In-Reply-To: <998415.64762.qm@web51808.mail.re2.yahoo.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
Message-ID: <20091118230322.GA30918@ayn.mi.celestial.com>

On Wed, Nov 18, 2009, GoodPotatoes wrote:
>I'm dealing with bigger lists than I have been, and noticed this is getting really slow.  Is there a faster way to do this?
>
>for x in list1:
>    if x not in list2:
>        list3.append(x)
>
>My search is taking up to 5 minutes to complete.

When I have had to deal with large lists, I have found that using
an intermediate dictionary can save huge amounts of time.
Something like:

dict2 = {}.fromkeys(list2)
for x in list1:
	if x not in dist2:
		dict2[x] = True

list2 = dict2.keys()

Bill
-- 
INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Intaxication: Euphoria at getting a refund from the IRS, which lasts until
you realize it was your money to start with.

From rabidpoobear at gmail.com  Thu Nov 19 00:54:13 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Wed, 18 Nov 2009 17:54:13 -0600
Subject: [Tutor] Faster list searching?
In-Reply-To: <20091118230322.GA30918@ayn.mi.celestial.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
	<20091118230322.GA30918@ayn.mi.celestial.com>
Message-ID: <dfeb4470911181554t3c8bbd82k64f0bbbf65a6580f@mail.gmail.com>

On Wed, Nov 18, 2009 at 5:03 PM, Bill Campbell <bill at celestial.net> wrote:

>
> When I have had to deal with large lists, I have found that using
> an intermediate dictionary can save huge amounts of time.
> Something like:
>
> dict2 = {}.fromkeys(list2)
> for x in list1:
>        if x not in dist2:
>                dict2[x] = True
>
> list2 = dict2.keys()
>
> This is really just a round-about way of using sets.
I don't really want to give a code-sample unless he's confirmed he's not
doing this as homework, but the set version is much more simple (shorter
code that makes more sense) and extremely quick as well.  If you're
interested in it, Bill, reply to me off-list and I'll send it to you.

-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091118/224b227d/attachment.htm>

From rabidpoobear at gmail.com  Thu Nov 19 00:55:03 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Wed, 18 Nov 2009 17:55:03 -0600
Subject: [Tutor] Faster list searching?
In-Reply-To: <dfeb4470911181554t3c8bbd82k64f0bbbf65a6580f@mail.gmail.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
	<20091118230322.GA30918@ayn.mi.celestial.com>
	<dfeb4470911181554t3c8bbd82k64f0bbbf65a6580f@mail.gmail.com>
Message-ID: <dfeb4470911181555t1f6885a8j60927362377ec575@mail.gmail.com>

On Wed, Nov 18, 2009 at 5:54 PM, Luke Paireepinart
<rabidpoobear at gmail.com>wrote:

> This is really just a round-about way of using sets.
> I don't really want to give a code-sample unless he's confirmed he's not
> doing this as homework, but the set version is much more simple (shorter
> code that makes more sense) and extremely quick as well.  If you're
> interested in it, Bill, reply to me off-list and I'll send it to you.
>
> Never mind about this, Kent already gave basically the same code sample I
was going to.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091118/05a6a6e3/attachment.htm>

From toni at muybien.org  Thu Nov 19 01:01:19 2009
From: toni at muybien.org (Antonio de la Fuente)
Date: Thu, 19 Nov 2009 00:01:19 +0000
Subject: [Tutor] Introduction - log exercise
In-Reply-To: <20091117165639.GA3411@cateto>
References: <20091117165639.GA3411@cateto>
Message-ID: <20091119000119.GC3372@cateto>

* Antonio de la Fuente <toni at muybien.org> [2009-11-17 16:58:08 +0000]:

> Date: Tue, 17 Nov 2009 16:58:08 +0000
> From: Antonio de la Fuente <toni at muybien.org>
> To: Python Tutor mailing list <tutor at python.org>
> Subject: [Tutor] Introduction - log exercise
> Organization: (muybien.org)
> User-Agent: Mutt/1.5.20 (2009-06-14)
> Message-ID: <20091117165639.GA3411 at cateto>
> 
> Hi everybody,
> 
> This is my first post here. I have started learning python and I am new to
> programing, just some bash scripting, no much. 
> Thank you for the kind support and help that you provide in this list.
> 
> This is my problem: I've got a log file that is filling up very quickly, this
> log file is made of blocks separated by a blank line, inside these blocks there
> is a line "foo", I want to discard blocks with that line inside it, and create a
> new log file, without those blocks, that will reduce drastically the size of the
> log file. 
> 
> The log file is gziped, so I am going to use gzip module, and I am going to pass
> the log file as an argument, so sys module is required as well.
> 
> I will read lines from file, with the 'for loop', and then I will check them for
> 'foo' matches with a 'while loop', if matches I (somehow) re-initialise the
> list, and if there is no matches for foo, I will append line to the list. When I
> get to a blank line (end of block), write myList to an external file. And start
> with another line.
> 
> I am stuck with defining 'blank line', I don't manage to get throught the while
> loop, any hint here I will really appreciate it.
> I don't expect the solution, as I think this is a great exercise to get wet
> with python, but if anyone thinks that this is the wrong way of solving the
> problem, please let me know.
> 
> 
> #!/usr/bin/python
> 
> import sys
> import gzip
> 
> myList = []
> 
> # At the moment not bother with argument part as I am testing it with a
> # testing log file
> #fileIn = gzip.open(sys.argv[1])
> 
> fileIn = gzip.open('big_log_file.gz', 'r')
> fileOut = open('outputFile', 'a')
> 
> for line in fileIn:
>     while line != 'blank_line':
>         if line == 'foo':
>             Somehow re-initialise myList
> 	    break
>         else:
>             myList.append(line)
>     fileOut.writelines(myList)
> 
> 
> Somehow rename outputFile with big_log_file.gz
> 
> fileIn.close()
> fileOut.close()
> 
> -------------------------------------------------------------
> 
> The log file will be fill with:
> 
> 
> Tue Nov 17 16:11:47 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
> 
> Tue Nov 17 16:12:58 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	foo
> 	tatati tatata
> 
> Tue Nov 17 16:13:42 GMT 2009
> 	bladi bladi bla
> 	tarila ri la
> 	patatin pataton
> 	tatati tatata
> 
> 
> etc, etc ,etc
> ..............................................................
> 
> Again, thank you.
> 
This is how, with your help, finally, I wrote the script.
The way I compress the file at the end of the script, opening files
again, didn't feel right, but couldn't make it work other way, and I
was very tired at the time.
First test, seems to work, but I will do a more deep test tomorrow.
Thank you all.

#!/usr/bin/python                                                                                                                                                                         
                                                                                                                                                                                          # Importing modules that I'm going to need
import sys                                                                                                                                                                                
import gzip                                                                                                                                                                               
import os                                                                                                                                                                                 
																							  # Initialising paragraph list
paragraph = []                                                                                                                                                                            
# Flag to signal which paragraphs to keep and which one to discard.
keep = True                                                                                                                                                                               
# Getting file name, without extension, from parameter pass to script,
# to rename file.
renameFile, ignored = os.path.splitext(sys.argv[1])                                                                                                                                       
# Opening argument file.
fileIn = gzip.open(sys.argv[1])                                                                                                                                                           
fileOut = open('outputFile', 'a')                                                                                                                                                         

# Only one argument pass to script, gzip file.
if len(sys.argv) != 2:                                                                                                                                                                    
    print 'Usage: log_exercise01.py <gzip logfile>'                                                                                                                                       
    sys.exit(1)                                                                                                                                                                           

# Main loop
for line in fileIn:                                                                                                                                                                       
    # If a blank line in log file
    if line.isspace():                                                                                                                                                                    
    # I append a blank line to list to keep the format
        paragraph.append('\n')                                                                                                                                                            
	# If true append line to file, keeping formating with the
	# "".join trick
        if keep:                                                                                                                                                                          
            fileOut.write(  "".join(paragraph)  )                                                                                                                                         
	# Re-initialise list
        paragraph = []                                                                                                                                                                    
        keep = True                                                                                                                                                                       
    # Else append line to paragraph list and if stripping the line from
    # the initial tab is 'foo' then set flag keep to false, to discard
    # paragraph.
    else:                                                                                                                                                                                 
        paragraph.append(line)                                                                                                                                                            
        if line.strip() == 'foo':                                                                                                                           
            keep = False                                                                                                                                                                  

# Compressing file that has been created
f_in = open('outputFile', 'r')                                                                                                                                                           
f_out = gzip.open(sys.argv[1], 'w')                                                                                                                                                      
f_out.writelines(f_in)                                                                                                                                                                    

# Renaming the file with the same name that parameter passed and
# deleting the file created by the script.
os.rename('outputFile', renameFile)                                                                                                                                                       
os.remove(renameFile)                                                                                                                                                                     

f_in.close()                                                                                                                                                                              
f_out.close()                                                                                                                                                                             
fileIn.close()                                                                                                                                                                            
fileOut.close()             

-- 
-----------------------------
Antonio de la Fuente Mart?nez
E-mail: toni at muybien.org
-----------------------------

En una organizaci?n jer?rquica, cuanto m?s alto es el nivel, mayor es la
confusi?n.
		-- Ley de Dow. 

From tim.peters at gmail.com  Thu Nov 19 01:03:30 2009
From: tim.peters at gmail.com (Tim Peters)
Date: Wed, 18 Nov 2009 19:03:30 -0500
Subject: [Tutor] Faster list searching?
In-Reply-To: <dfeb4470911181555t1f6885a8j60927362377ec575@mail.gmail.com>
References: <998415.64762.qm@web51808.mail.re2.yahoo.com>
	<20091118230322.GA30918@ayn.mi.celestial.com> 
	<dfeb4470911181554t3c8bbd82k64f0bbbf65a6580f@mail.gmail.com> 
	<dfeb4470911181555t1f6885a8j60927362377ec575@mail.gmail.com>
Message-ID: <1f7befae0911181603j50e4e15eq4a94f31f79db0bfc@mail.gmail.com>

[Luke Paireepinart]
>> This is really just a round-about way of using sets.
>> I don't really want to give a code-sample unless he's confirmed he's not
>> doing this as homework, but the set version is much more simple (shorter
>> code that makes more sense) and extremely quick as well.? If you're
>> interested in it, Bill, reply to me off-list and I'll send it to you.

[also Luke Paireepinart]
> Never mind about this, Kent already gave basically the same code sample I
> was going to.

So long as the cat's out of the list, may as well do it "the obvious"
;-) way too:

result = set(list1) - set(list2)

Of course the result is a set then.  Maybe that will work fine in
context, maybe not.  I leave it as an exercise to figure out how to
change it back into a list (hint:  try the obvious way first ;-) ).

From alan.gauld at btinternet.com  Thu Nov 19 01:49:15 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 19 Nov 2009 00:49:15 -0000
Subject: [Tutor] Faster list searching?
References: <998415.64762.qm@web51808.mail.re2.yahoo.com><20091118230322.GA30918@ayn.mi.celestial.com>
	<dfeb4470911181554t3c8bbd82k64f0bbbf65a6580f@mail.gmail.com>
	<dfeb4470911181555t1f6885a8j60927362377ec575@mail.gmail.com>
	<1f7befae0911181603j50e4e15eq4a94f31f79db0bfc@mail.gmail.com>
Message-ID: <he24mr$3sl$1@ger.gmane.org>


"Tim Peters" <tim.peters at gmail.com> wrote

> result = set(list1) - set(list2)
>
> Of course the result is a set then.  

Which means that if there were duplicates in list1 you only get 
one copy in the result. As Tim says, whether that is good, bad 
or irrelevant depends on the problem context.

> Maybe that will work fine in context, maybe not.  
> I leave it as an exercise to figure out how to
> change it back into a list (hint:  try the obvious way first ;-) ).

But that won't replace any missing duplicates.
If they are significant you'll probably need to stick with Kent's 
list comprehension approach.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



From sanelson at gmail.com  Thu Nov 19 07:14:31 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Thu, 19 Nov 2009 06:14:31 +0000
Subject: [Tutor] Readable date arithmetic
Message-ID: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>

I have the following method:

def get_log_dates(the_date_we_want_data_for):
  t = time.strptime(the_date_we_want_data_for, '%Y%m%d')
  t2 = datetime.datetime(*t[:-2])
  extra_day = datetime.timedelta(days=1)
  t3 = t2 + extra_day
  next_log_date = t3.strftime('%Y%m%d')
  return (the_date_we_want_data_for, next_log_date)

Quite apart from not much liking the t[123] variables, does date
arithmetic really need to be this gnarly?

How could I improve the above, especially from a readability
perspective?  Or is it ok?

S.

From roadierich at googlemail.com  Thu Nov 19 07:27:21 2009
From: roadierich at googlemail.com (Rich Lovely)
Date: Thu, 19 Nov 2009 06:27:21 +0000
Subject: [Tutor] Making http posts
In-Reply-To: <1c2a2c590911050433g2aae1150g682a5a2d40af7001@mail.gmail.com>
References: <b6131fdc0911050206j29cea9f7laaecbd86079f7d66@mail.gmail.com>
	<1c2a2c590911050433g2aae1150g682a5a2d40af7001@mail.gmail.com>
Message-ID: <f0b4202b0911182227o35b8f8e0qdd002d1d64a6ec8@mail.gmail.com>

2009/11/5 Kent Johnson <kent37 at tds.net>:
> On Thu, Nov 5, 2009 at 5:06 AM, Stephen Nelson-Smith <sanelson at gmail.com> wrote:
>> Hello,
>>
>> I want to make an HTTP POST which will require authentication.
>
> What kind of authentication? Basic auth is easy to use from Python;
> form-based auth is a bit tricker.
>
>> This is because we are using a web tool without an API, and we want a
>> programatic way of getting content into the tool. ?Tech support of the
>> web tool have said we can just make a POST to the http endpoint.
>>
>> >From the below source, it looks like I just post some text to
>> /project/4254/stories, with the ID of addStoryForm.
>
> IIRC the form ID is not part of the post. The text has to be formatted
> as name=value pairs.
>
>> Is the best way to do this just a simple urllib script?
>
> urllib2. See my writeup here:
> http://personalpages.tds.net/~kent37/kk/00010.html
>
>> What's the best way to find out from the browser exactly what is being
>> posted when I use the web interface? ?Tcpdump? ?Or is the a better
>> tool?
>
> There are Firefox addons for this. It's been a while so I'm not sure
> which ones but I think either Firebug or Tamper Data will do it.
>
> Kent
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

If you're really stuck, look into mechanize:  it links together
urllib, beautiful soup and a form handler.  To log in to a form, all
you need to do is:

br = mechanize.Browser()
br.open("http://path.to.form.com/form")
#br.select_form(...) if it's not the first form on the page - see docs
br['name'] = "username"
br['password'] = "password"
response = br.submit()

then response is a beautifulsoup object of the returned page.  The
browser object also updates to act on the new page as well, and has
methods to click on links and so on, exactly as you would in a
browser.  Basically, if you can do it with a keyboard and your
prefered webbrowser, you can do it with mechanize, and a few things
beside.  (I've bypassed a (copypasta) javascript md5 function on a
forum login before now...)
-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.

From sanelson at gmail.com  Thu Nov 19 09:24:09 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Thu, 19 Nov 2009 08:24:09 +0000
Subject: [Tutor] Why are these results different?
Message-ID: <b6131fdc0911190024j652e79can6137ab197adf062e@mail.gmail.com>

I'm seeing different behaviour between code that looks to be the same.
 It obviously isn't the same, so I've misunderstood something:


>>> log_names
('access', 'varnish')
>>> log_dates
('20091105', '20091106')
>>> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % (source_dir, log, date)) for log in log_names for date in log_dates)
>>> for log in logs:
...   print log
...
/Volumes/UNTITLED 1/ded1/access_log-20091105.gz
/Volumes/UNTITLED 1/ded2/access_log-20091105.gz
/Volumes/UNTITLED 1/ded3/access_log-20091105.gz
/Volumes/UNTITLED 1/ded1/access_log-20091106.gz
/Volumes/UNTITLED 1/ded2/access_log-20091106.gz
/Volumes/UNTITLED 1/ded3/access_log-20091106.gz
/Volumes/UNTITLED 1/ded1/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded2/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded3/varnishncsa.log-20091105.gz
/Volumes/UNTITLED 1/ded1/varnishncsa.log-20091106.gz
/Volumes/UNTITLED 1/ded2/varnishncsa.log-20091106.gz
/Volumes/UNTITLED 1/ded3/varnishncsa.log-20091106.gz

However:

for date in log_dates:
  for log in log_names:
     logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
% (source_dir, log, date)))

Gives me one character at a time when I iterate over logs.

Why is this?

And how, then, can I make the first more readable?

S.

From alan.gauld at btinternet.com  Thu Nov 19 09:57:30 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 19 Nov 2009 08:57:30 -0000
Subject: [Tutor] Why are these results different?
References: <b6131fdc0911190024j652e79can6137ab197adf062e@mail.gmail.com>
Message-ID: <he31aa$vtm$1@ger.gmane.org>


"Stephen Nelson-Smith" <sanelson at gmail.com> wrote


> I'm seeing different behaviour between code that looks to be the same.
> It obviously isn't the same, so I've misunderstood something:

In the first instance the two for-loops are inside the chain() call.
In the second you apply the chain inside the loops, so it only
applies to one item at a time.

>>>> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % 
>>>> (source_dir, log, date)) for log in log_names for date in log_dates)
>>>> for log in logs:
> ...   print log
> ...
> /Volumes/UNTITLED 1/ded1/access_log-20091105.gz
> /Volumes/UNTITLED 1/ded2/access_log-20091105.gz

> However:
>
> for date in log_dates:
>  for log in log_names:
>     logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
> % (source_dir, log, date)))
>
> Gives me one character at a time when I iterate over logs.

The final result is whatever chain() evaluated for the final loop 
iteration.
Apparently a string.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From seena_blk at yahoo.com  Thu Nov 19 11:28:02 2009
From: seena_blk at yahoo.com (Ali Sina)
Date: Thu, 19 Nov 2009 02:28:02 -0800 (PST)
Subject: [Tutor] TypeError: Need Help
Message-ID: <583784.96930.qm@web45909.mail.sp1.yahoo.com>

>>I have Python 3.0 and I'm trying to learn from a pdf document. I followed its instructions >>but I've encountered a problem in its Pickling chapter. This is the code I've written:

import pickle, shelve

print('Pickling list.')
variety=['sweet','hot','dill']
shape=['whole','spear','chip']
brand=['Claussen','Heinz','Vlassic']

pickle_file=open('pickles1.dat','w')
pickle.dump(variety, pickle_file)
pickle.dump(shape, pickle_file)
pickle.dump(brand, pickle_file)
pickle_file.close()

print('\nUnpickling list.')
pickle_file=open('pickles1.dat','r')
variety=pickle.load(pickle_file)
shape=pickle.load(pickle_file)
brand=pickle.load(pickle_file)
print(variety,'\n',shape,'\n',brand)
pickle_file.close()

>>But it gives this error:

Traceback (most recent call last):
? File "E:/Python/pickle it.py", line 11, in <module>
??? pickle.dump(variety, pickle_file)
? File "C:\Python31\lib\pickle.py", line 1354, in dump
??? Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: write() argument 1 must be str, not bytes

>>I read Python Docs but it didn't help me either. I hoping anyone of you could guide me or >>send the revised code itself. Thank you.

>>Regards



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/38c76e68/attachment.htm>

From bibsmendez at gmail.com  Thu Nov 19 12:40:34 2009
From: bibsmendez at gmail.com (bibi midi)
Date: Thu, 19 Nov 2009 06:40:34 -0500
Subject: [Tutor] proxy switcher - was Re: I love python / you guys :)
In-Reply-To: <f16f1f8c0911172012q73ae78c8v8325f184a29a1798@mail.gmail.com>
References: <5cb309e70911160357m2a617e19tff6c839b47dc98c4@mail.gmail.com>
	<b6131fdc0911160630q5bca682et4d954fe2917246b@mail.gmail.com>
	<dfeb4470911160652v42e28335l98538d27e817e14d@mail.gmail.com>
	<4B019A3B.5010100@ieee.org>
	<f16f1f8c0911172012q73ae78c8v8325f184a29a1798@mail.gmail.com>
Message-ID: <f16f1f8c0911190340w1839f0dene1ed84bc5a07db18@mail.gmail.com>

---------- Forwarded message ----------
From: bibi midi <bibsmendez at gmail.com>
Date: Tue, Nov 17, 2009 at 11:12 PM
Subject: Re: [Tutor] proxy switcher - was Re: I love python / you guys :)
To: Dave Angel <davea at ieee.org>
Cc: Luke Paireepinart <rabidpoobear at gmail.com>, tutor <tutor at python.org>


Hi guys!

Thank you all for the brainstorming. As much as i love to follow all your
tips but sorry I got lost in the sea of discussion :-) Therefore i thought i
start to roll out my own and i hope you guys can chime in.

The print lines in my code are just for debugging. Of course they can be
omitted but for a newbie like me I helps to see what I'm doing is what I'm
expected to do.

I would like to improve my line search with the module re but cant find
something to get me started. I appreciate if you can show me how. The
'proxy' search works but I'm sure with re module the search will be definite
i mean wont yield 'false positive' result, sort-of.

http://pastebin.ca/1675865


-- 
Best Regards,
bibimidi

Sent from Dhahran, 04, Saudi Arabia



-- 
Best Regards,
bibimidi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/b032f834/attachment.htm>

From kent37 at tds.net  Thu Nov 19 12:41:40 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 19 Nov 2009 06:41:40 -0500
Subject: [Tutor] Readable date arithmetic
In-Reply-To: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>
References: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>
Message-ID: <1c2a2c590911190341tbe9c450pcf8c812c79008ec5@mail.gmail.com>

On Thu, Nov 19, 2009 at 1:14 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> I have the following method:
>
> def get_log_dates(the_date_we_want_data_for):
> ?t = time.strptime(the_date_we_want_data_for, '%Y%m%d')
> ?t2 = datetime.datetime(*t[:-2])

Use datetime.datetime.strptime() to save a step:
t2 = datetime.datetime.strptime(the_date_we_want_data_for, '%Y%m%d')

> ?extra_day = datetime.timedelta(days=1)
> ?t3 = t2 + extra_day

t2 += datetime.timedelta(days=1)

> ?next_log_date = t3.strftime('%Y%m%d')
> ?return (the_date_we_want_data_for, next_log_date)
>
> Quite apart from not much liking the t[123] variables, does date
> arithmetic really need to be this gnarly?

Yes, pretty much. Convert the string to a date-aware object, increment
the date, convert back to a string.

> How could I improve the above, especially from a readability
> perspective?

Above changes improve readability IMO.

Kent

From kent37 at tds.net  Thu Nov 19 12:52:43 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 19 Nov 2009 06:52:43 -0500
Subject: [Tutor] Why are these results different?
In-Reply-To: <b6131fdc0911190024j652e79can6137ab197adf062e@mail.gmail.com>
References: <b6131fdc0911190024j652e79can6137ab197adf062e@mail.gmail.com>
Message-ID: <1c2a2c590911190352m548fea09l2a689e7073ccbe1b@mail.gmail.com>

On Thu, Nov 19, 2009 at 3:24 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> I'm seeing different behaviour between code that looks to be the same.
> ?It obviously isn't the same, so I've misunderstood something:
>
>
>>>> log_names
> ('access', 'varnish')
>>>> log_dates
> ('20091105', '20091106')
>>>> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' % (source_dir, log, date)) for log in log_names for date in log_dates)
>>>> for log in logs:
> ... ? print log

Here the argument to from_iterable() is a sequence of lists.
from_iterable() iterates each list in the sequence.

> However:
>
> for date in log_dates:
> ?for log in log_names:
> ? ? logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
> % (source_dir, log, date)))

> Gives me one character at a time when I iterate over logs.
>
Here the argument to from_iterable() is a list of strings, i.e. a
sequence of strings. from_iterable() iterates each string in the
sequence. Iterating a string yields each character in the string in
turn.

By the way do you know that the second version loops in a different
order than the first?

> Why is this?
>
> And how, then, can I make the first more readable?

Break out the argument to from_iterable() into a separate variable.
If you like spelling it out as separate loops, but you want a single
sequence, use the second form but put it in a generator function:
def log_file_names(log_names, log_dates):
  for date in log_dates:
   for log in log_names:
      for file_name in glob.glob('%sded*/%s*%s.gz' % (source_dir, log, date)):
        yield file_name

Then your client code can say
for file_name in log_file_names(log_names, log_dates):
    print file_name

If log_names is a constant you can put it into log_file_names()
instead of passing it as a parameter.

Kent

From fomcl at yahoo.com  Thu Nov 19 16:23:50 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Thu, 19 Nov 2009 07:23:50 -0800 (PST)
Subject: [Tutor] elementtree question
Message-ID: <647808.3974.qm@web110704.mail.gq1.yahoo.com>

Hi,
?
I have an elementtree question that probably reflects my inexperience with xml processing (and/or with Python). The xml file is a stream of the Spss Clementine program. Each stream consists of, among others, nodes. Each nodes has properties, among which "tooltiptext" and "label". I want to replace the contents of "label" to "tooltiptext".
?
Below is what I've cooked up till now. Could anyone give me some pointers? Thanks a lot in advance!
?
from elementtree import ElementTree as ET
"""
Replace the empty text of the tooltipText tag?with the text of the label tag
Relevant part of the tree: stream/diagram/nodes/node/properties
Within properties, the tooltiptext tag is listed before the label tag.
"""
in_tree = ET.ElementTree(file="d:/jib/test.xml")
parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
def xml_read(parent_map):
??? for c, p in parent_map.iteritems():
??????? if p.tag == "properties" and c.tag == "label":
??????????? yield c.text
##newnames = xml_read(parent_map)
##for newname in newnames:
##??? print newname
?
def xml_write(parent_map, in_tree):
??? newnames = xml_read(parent_map)
??? for c, p in parent_map.iteritems():
??????? if p.tag == "properties" and c.tag == "toolTipText":
??????????? for newname in newnames:
??????????????? print newname
??????????????? c.text = newname
??? in_tree.write("d:/jib/out_xml.xml")
xml_write(parent_map, in_tree)


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/d211d1be/attachment-0001.htm>

From grflanagan at gmail.com  Thu Nov 19 17:10:12 2009
From: grflanagan at gmail.com (Gerard Flanagan)
Date: Thu, 19 Nov 2009 16:10:12 +0000
Subject: [Tutor] elementtree question
In-Reply-To: <647808.3974.qm@web110704.mail.gq1.yahoo.com>
References: <647808.3974.qm@web110704.mail.gq1.yahoo.com>
Message-ID: <4B056DE4.1070800@gmail.com>

Albert-Jan Roskam wrote:
> Hi,
>  
> I have an elementtree question that probably reflects my inexperience 
> with xml processing (and/or with Python). The xml file is a stream of 
> the Spss Clementine program. Each stream consists of, among others, 
> nodes. Each nodes has properties, among which "tooltiptext" and 
> "label". I want to replace the contents of "label" to "tooltiptext".
>  
> Below is what I've cooked up till now. Could anyone give me some 
> pointers? Thanks a lot in advance!
>  
> from elementtree import ElementTree as ET
> """
> Replace the empty text of the tooltipText tag with the text of the 
> label tag
> Relevant part of the tree: stream/diagram/nodes/node/properties
> Within properties, the tooltiptext tag is listed before the label tag.
> """
> in_tree = ET.ElementTree(file="d:/jib/test.xml")
> parent_map = dict((c, p) for p in in_tree.getiterator() for c in p)
> def xml_read(parent_map):
>     for c, p in parent_map.iteritems():
>         if p.tag == "properties" and c.tag == "label":
>             yield c.text
> ##newnames = xml_read(parent_map)
> ##for newname in newnames:
> ##    print newname
>  
> def xml_write(parent_map, in_tree):
>     newnames = xml_read(parent_map)
>     for c, p in parent_map.iteritems():
>         if p.tag == "properties" and c.tag == "toolTipText":
>             for newname in newnames:
>                 print newname
>                 c.text = newname
>     in_tree.write("d:/jib/out_xml.xml")
> xml_write(parent_map, in_tree)
>
>

That looks a bit over-thought. If I've understood what you want, 
something like below should do the job. It replaces the value of the id 
attribute with the value of the colour attribute.

ATTR_TEST_STRING = '''
<root>
    <title lang="en" encoding="utf-8">Document Title</title>
    <category id="123" code="A">
        <item id="A001" colour="red">Item A1</item>
        <item id="A002" colour="blue">Item A2</item>
        <item id="A003" colour="yellow">Item A3</item>
    </category>
    <category id="456" code="B">
        <item id="B001" colour="pink">Item B1</item>
        <item id="B002" colour="blue">Item B2</item>
        <item id="B003" >Item B3</item>
    </category>
    <category id="789" code="C">
        <item id="C001" colour="pink">Item C1</item>
        <item id="C002" colour="orange">Item C2</item>
        <item id="C003" colour="blue">Item C3</item>
    </category>
</root>'''

from xml.etree import ElementTree as ET

xml = ET.fromstring(ATTR_TEST_STRING)

print('-------- before -------')
ET.dump(xml)

for elem in xml.getiterator():
    if 'id' in elem.attrib and 'colour' in elem.attrib:
        elem.set('id', elem.get('colour', ''))

print('-------- after -------')
ET.dump(xml)



From davea at ieee.org  Thu Nov 19 17:53:49 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 19 Nov 2009 11:53:49 -0500
Subject: [Tutor] TypeError: Need Help
In-Reply-To: <583784.96930.qm@web45909.mail.sp1.yahoo.com>
References: <583784.96930.qm@web45909.mail.sp1.yahoo.com>
Message-ID: <4B05781D.1040805@ieee.org>



Ali Sina wrote:
>>> I have Python 3.0 and I'm trying to learn from a pdf document. I followed its instructions >>but I've encountered a problem in its Pickling chapter. This is the code I've written:
>>>       
>
> import pickle, shelve
>
> print('Pickling list.')
> variety=['sweet','hot','dill']
> shape=['whole','spear','chip']
> brand=['Claussen','Heinz','Vlassic']
>
> pickle_file=open('pickles1.dat','w')
> pickle.dump(variety, pickle_file)
> pickle.dump(shape, pickle_file)
> pickle.dump(brand, pickle_file)
> pickle_file.close()
>
> print('\nUnpickling list.')
> pickle_file=open('pickles1.dat','r')
> variety=pickle.load(pickle_file)
> shape=pickle.load(pickle_file)
> brand=pickle.load(pickle_file)
> print(variety,'\n',shape,'\n',brand)
> pickle_file.close()
>
>   
>>> But it gives this error:
>>>       
>
> Traceback (most recent call last):
>   File "E:/Python/pickle it.py", line 11, in <module>
>     pickle.dump(variety, pickle_file)
>   File "C:\Python31\lib\pickle.py", line 1354, in dump
>     Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
> TypeError: write() argument 1 must be str, not bytes
>
>   
>>> I read Python Docs but it didn't help me either. I hoping anyone of you could guide me or >>send the revised code itself. Thank you.
>>>       
>
>   
>>> Regards
>>>       
>
>   
Three guesses, then a suggestion:

I'd guess that the pdf document you're learning from is aimed at Python 
2.x, not Python 3.x

In Python 3, all strings are in Unicode, and there's a distinct type, 
bytes, which serves to store non-Unicode versions of strings.  How that 
fits, I don't know.

I'd upgrade to Python 3.1,  Python 3.0 has a lot of known problems.

But I think your real problem is:
     the Pickler constructor expects a binary file, so you'll want "wb" 
instead of "w" on the open().

With that change, it works for me:
Unpickling list.
['sweet', 'hot', 'dill']
 ['whole', 'spear', 'chip']
 ['Claussen', 'Heinz', 'Vlassic']


DaveA



From davea at ieee.org  Thu Nov 19 18:06:03 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 19 Nov 2009 12:06:03 -0500
Subject: [Tutor] Readable date arithmetic
In-Reply-To: <1c2a2c590911190341tbe9c450pcf8c812c79008ec5@mail.gmail.com>
References: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>
	<1c2a2c590911190341tbe9c450pcf8c812c79008ec5@mail.gmail.com>
Message-ID: <4B057AFB.5010702@ieee.org>

Kent Johnson wrote:
> On Thu, Nov 19, 2009 at 1:14 AM, Stephen Nelson-Smith
> <sanelson at gmail.com> wrote:
>   
>> I have the following method:
>>
>> def get_log_dates(the_date_we_want_data_for):
>>  t =ime.strptime(the_date_we_want_data_for, '%Y%m%d')
>>  t2 =atetime.datetime(*t[:-2])
>>     
>
> Use datetime.datetime.strptime() to save a step:
> t2 =atetime.datetime.strptime(the_date_we_want_data_for, '%Y%m%d')
>
>   
>>  extra_day =atetime.timedelta(days=1)
>>  t3 =2 + extra_day
>>     
>
> t2 +=atetime.timedelta(days=1)
>
>   
>>  next_log_date =3.strftime('%Y%m%d')
>>  return (the_date_we_want_data_for, next_log_date)
>>
>> Quite apart from not much liking the t[123] variables, does date
>> arithmetic really need to be this gnarly?
>>     
>
> Yes, pretty much. Convert the string to a date-aware object, increment
> the date, convert back to a string.
>
>   
>> How could I improve the above, especially from a readability
>> perspective?
>>     
>
> Above changes improve readability IMO.
>
> Kent
>
>   
My opinion is that these three things don't usually belong in the same 
function.  You should be converting incoming dates to datetime format, 
and only convert back when you're ready to output something that has to 
be human-readable text.  That way you avoid double-conversions, and can 
localize problems such as dealing with multiple timezones or daylight 
savings.  When you're passing dates around the program, use a simple, 
self-describing format, such as datetime.

YMMV,
DaveA


From geoff.dutton at noaa.gov  Thu Nov 19 23:47:45 2009
From: geoff.dutton at noaa.gov (Geoff Dutton)
Date: Thu, 19 Nov 2009 15:47:45 -0700
Subject: [Tutor] Handling missing positional arguments
Message-ID: <7a9810630911191447v1ad3e452g5bfbfa08e6a45602@mail.gmail.com>

Hi Group,

I have been following the Tutor group for awhile but this is my first
post....

Do you have recommendations for handling missing positional arguments?  I'm
a huge fan of OptionParser module and use it in several programs to handle
options, but there must be an eligant way of handling missing arguments and
alerting the user.  Here is how I have done it ...

import sys
from subprocess import call
from optparse import OptionParser

if __name__=='__main__':

    opt = OptionParser(
        usage = "usage: %prog [options] site year",
        description = "Count number of ITX files from a station for a given
year."
    )
    opt.add_option("-e", action="store",
            dest="email_add", help="send table as email")
    opt.add_option("-l", '--lenght', action="store", default="30",
            dest="len", help="Number of lines in table.")

    (options, args) = opt.parse_args()

    if len(args) != 2:
        call(["countinj.py", "-h"])
        sys.exit()

    (site, year) = args

    <snip>


I like the "help" that OptionParser creates for my program when called with
the -h or --help option. Thus when len(args) is not correct I call the
program using the call function in the module subprocess.  But.... Do I
really need go to this trouble?  Is there a better way?

Thanks,
Geoff

-- 
NOAA Earth System Research Laboratory
Global Monitoring Division
325 Broadway, R/GMD1
Boulder, CO 80305
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/a26034da/attachment.htm>

From lauren at protopc.com  Fri Nov 20 00:02:41 2009
From: lauren at protopc.com (lauren at protopc.com)
Date: Thu, 19 Nov 2009 16:02:41 -0700
Subject: [Tutor] Can a method be added to dictionary?
Message-ID: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>

Hey Gang,

Can a function/method be added to a dictionary like so:

myDictionary = {"string":processString(parameter),
                "string2":processString2(parameter),
                "string3":processString3(parameter)
               }

I am basically interested in doing this with a Combobx Event.
When the user selects an option in my dropdown box (event.GetString())
that matches the string in my dictionary...say "string2" then the method:
processString2(parameter) is executed.

Currently when I get the value of: myDictionary[string2] it is None.

Thanks for your help!
Lauren



From john at fouhy.net  Fri Nov 20 01:22:48 2009
From: john at fouhy.net (John Fouhy)
Date: Fri, 20 Nov 2009 13:22:48 +1300
Subject: [Tutor] Can a method be added to dictionary?
In-Reply-To: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>
References: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>
Message-ID: <5e58f2e40911191622g250574fi7bc9d3832dfd264c@mail.gmail.com>

2009/11/20  <lauren at protopc.com>:
> Hey Gang,
>
> Can a function/method be added to a dictionary like so:
>
> myDictionary = {"string":processString(parameter),
> ? ? ? ? ? ? ? ?"string2":processString2(parameter),
> ? ? ? ? ? ? ? ?"string3":processString3(parameter)
> ? ? ? ? ? ? ? }
>
> I am basically interested in doing this with a Combobx Event.
> When the user selects an option in my dropdown box (event.GetString())
> that matches the string in my dictionary...say "string2" then the method:
> processString2(parameter) is executed.
>
> Currently when I get the value of: myDictionary[string2] it is None.
>
> Thanks for your help!
> Lauren

Hi Lauren,

What happens here is that "processString2(parameter)" executes at the
point you define the dictionary.  So myDictionary[string2] is None
because that's what your method returns.

What you need to do is put an actual function in there, instead of a
function call.

Here's a toy example:
>>> def say_hello():
...     print 'Hello world!'
...
>>> myDict = { 1:say_hello }  # note: no () here!
>>> myDict[1]
<function say_hello at 0x4638f0>
>>> myDict[1]()               # now I use () to call the function.
Hello world!

Hope this helps!

-- 
John.

From lauren at protopc.com  Fri Nov 20 02:39:01 2009
From: lauren at protopc.com (lauren at protopc.com)
Date: Thu, 19 Nov 2009 18:39:01 -0700
Subject: [Tutor] Can a method be added to dictionary?
In-Reply-To: <5e58f2e40911191622g250574fi7bc9d3832dfd264c@mail.gmail.com>
References: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>
	<5e58f2e40911191622g250574fi7bc9d3832dfd264c@mail.gmail.com>
Message-ID: <0538412a861f4830b4090fcb2f1c1c92.squirrel@www.protopc.com>

John,

Thank you so much for your help! -- Problem SOLVED!!! -- Your explanation
and example was extremely helpful. I am very grateful.

Lauren :-)



From beachkid at insightbb.com  Fri Nov 20 02:53:36 2009
From: beachkid at insightbb.com (Ken G.)
Date: Thu, 19 Nov 2009 20:53:36 -0500
Subject: [Tutor] Outputting Data to Printer
Message-ID: <4B05F6A0.8040106@insightbb.com>

Is there a Python command to send data to printer? 

I have a Canon MX300 hooked up by USB.   I can print from Firefox and 
Thunderbird.  I am using Ubuntu 9.04 and Python 2.6.2.

I could print to a file and then use gedit to print out the content of 
the file but I was wondering if there was an easily way to do this.

Thanks,

Ken
 



From warpcat at gmail.com  Fri Nov 20 03:31:27 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Thu, 19 Nov 2009 18:31:27 -0800
Subject: [Tutor] Querying a packages modules?
Message-ID: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>

Say I have this package layout

   - \myPackage
   - __init__.py
      - moduleA.py
      - moduleB.py

Is there a way (and I'm sure there is...) to query, for a given package
level, which modules live under it?
I thought I could do it like so:

import myPackage
goodQualityInfo = dir(myPackage)

Thinking that he modules are just attributes of the package.  And while this
prints some nice stuff, it has no knowledge of moduleA & B.  I feel like
missing\forgetting some fundamental command or something ;)

If you're wondering, I'm authoring a tool that will take whatever modules
are placed in the package (authored to a certain standard) and execute them
based on a passed-in dataset to each.  But first I've got to get that list
of modules.  I've thought of some hacky-ways in my head, but I'd like to see
if there is something a bit more elegant...

thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/c493c981/attachment.htm>

From markersng at gmail.com  Fri Nov 20 07:54:48 2009
From: markersng at gmail.com (MARCUS NG)
Date: Fri, 20 Nov 2009 01:54:48 -0500
Subject: [Tutor] copy zip file from source folder to destination and unzip
	all files within destination
Message-ID: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>

Hey all,
I have been searching online for ways to copy a zip file to a destination
and extract the zip file with python.
Currently nothing works due to my limited understanding.
I am wondering if my syntax is wrong or am I missing anything?
the code is as such. also if there is a better code, I am definitely open to
it. If it is good, can you also explain steps?
###########

import shutil, zipfile
shutil.copyfile('C:\Users\blueman\Desktop\myTest1.0.zip',
'C:\Users\blueman\Desktop\allFiles')

zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
extractiondir='C:\Users\blueman\Desktop\allFiles\test'

def extract(zipfilepath, extractiondir):
    zip = zipfile(zipfilepath)
    zip.extractall(path=extractiondir)
    print 'it works'

extract()

###########
thank you so much for your time and help in advance!!!


,
Marcus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/ce7064c3/attachment.htm>

From alan.gauld at btinternet.com  Fri Nov 20 09:43:06 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 20 Nov 2009 08:43:06 -0000
Subject: [Tutor] Outputting Data to Printer
References: <4B05F6A0.8040106@insightbb.com>
Message-ID: <he5kra$f6l$1@ger.gmane.org>

"Ken G." <beachkid at insightbb.com> wrote

> Is there a Python command to send data to printer? 
> 
> I have a Canon MX300 hooked up by USB.   I can print from Firefox and 
> Thunderbird.  I am using Ubuntu 9.04 and Python 2.6.2.

There is no universal easy way to print stuff unfortunately.
In console mode on Unix its not too bad, you can send data
to lpr, just as you would from the shell.

You can also format your data using fmt or groff and send that 
to lpr as you would from the shell.

Finally, the route I tend to use nowadays is to format 
the output as html and print that via the browser.

If you are working inside a GUI and want to preserve 
formatting then it gets a whole lot harder and reaklly 
depends on the GUI framework that you are using.

> I could print to a file and then use gedit to print out the content of 
> the file but I was wondering if there was an easily way to do this.

That sounds like your needs are fairly simple so the 
lpr route is probably your best bet.

HTH,

Alan G.


From alan.gauld at btinternet.com  Fri Nov 20 10:35:26 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 20 Nov 2009 09:35:26 -0000
Subject: [Tutor] copy zip file from source folder to destination and
	unzipall files within destination
References: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
Message-ID: <he5nte$oc2$1@ger.gmane.org>

"MARCUS NG" <markersng at gmail.com> wrote

> Currently nothing works due to my limited understanding.

That's a pretty vague description of the problem.
What exactly happens?
Does the file get copied?
Do you get error messages? If so what?

> I am wondering if my syntax is wrong or am I missing anything?

If the syntax is wrong Python should tell you.
Does it?

The more information you give us the better able we
are to diagnose the problem.

> the code is as such. also if there is a better code, I am definitely open 
> to
> it. If it is good, can you also explain steps?

Without any more info I'll pass along a couple of observations.

> ###########
>
> import shutil, zipfile
> shutil.copyfile('C:\Users\blueman\Desktop\myTest1.0.zip',
> 'C:\Users\blueman\Desktop\allFiles')

The first thing is the use of DOS style path names.
You need to do one of the following:

1) Use raw strings - prefix the string with an r
 r'C:\Users\blueman\Desktop\allFiles')

2) Escape the backslashes
 'C:\\Users\\blueman\\Desktop\\allFiles')

3) Use Unix style slashes:
 'C:/Users/blueman/Desktop/allFiles')

Otherwise Python sees the \ as an escape character
and tries to interpret \U, \b \D and \a as special characters
like \n etc

> zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
> extractiondir='C:\Users\blueman\Desktop\allFiles\test'

Same here

> def extract(zipfilepath, extractiondir):
>    zip = zipfile(zipfilepath)
>    zip.extractall(path=extractiondir)
>    print 'it works'

While puuting things in functions is usuually a good idea, in this case
you only call it once so I wouldn't bother - at least not until I had the
program working

> extract()

You don't pas any arguments but you defined the functin to
take 2 - Python should tell you about this.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From lie.1296 at gmail.com  Fri Nov 20 10:35:42 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 20 Nov 2009 20:35:42 +1100
Subject: [Tutor] copy zip file from source folder to destination and
 unzip all files within destination
In-Reply-To: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
References: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
Message-ID: <he5o03$ojo$1@ger.gmane.org>

MARCUS NG wrote:
> Hey all,
> I have been searching online for ways to copy a zip file to a 
> destination and extract the zip file with python.
> Currently nothing works due to my limited understanding.
> I am wondering if my syntax is wrong or am I missing anything?
> the code is as such. also if there is a better code, I am definitely 
> open to it. If it is good, can you also explain steps?

It is probably good if you went through some beginner python tutorials...

anyways, there are many reasons why your script doesn't work...


> import shutil, zipfile
> shutil.copyfile('C:\Users\blueman\Desktop\myTest1..0.zip', 
> 'C:\Users\blueman\Desktop\allFiles')
> 
> zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
> extractiondir='C:\Users\blueman\Desktop\allFiles\test'
> 

note that you're defining 'extract' with two required arguments here:

> def extract(zipfilepath, extractiondir):

you're calling a module, which doesn't make sense. What you want is to 
call zipfile.ZipFile(zipfilepath)

>     zip = zipfile(zipfilepath)
>     zip.extractall(path=extractiondir)
>     print 'it works'
> 

remember that you defined 'extract' with two required arguments earlier? 
Here you're calling 'extract' without any arguments. You should pass two 
arguments to extract: extract(something, something_else)

> extract()
> 
> ###########
> thank you so much for your time and help in advance!!!
> 
> 
> ,
> Marcus
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


From kent37 at tds.net  Fri Nov 20 13:03:43 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 20 Nov 2009 07:03:43 -0500
Subject: [Tutor] Handling missing positional arguments
In-Reply-To: <7a9810630911191447v1ad3e452g5bfbfa08e6a45602@mail.gmail.com>
References: <7a9810630911191447v1ad3e452g5bfbfa08e6a45602@mail.gmail.com>
Message-ID: <1c2a2c590911200403o2548c55fx929760eea3a53bb9@mail.gmail.com>

On Thu, Nov 19, 2009 at 5:47 PM, Geoff Dutton <geoff.dutton at noaa.gov> wrote:
> Do you have recommendations for handling missing positional arguments?? I'm
> a huge fan of OptionParser module and use it in several programs to handle
> options, but there must be an eligant way of handling missing arguments and
> alerting the user.? Here is how I have done it ...
>
> import sys
> from subprocess import call
> from optparse import OptionParser
>
> if __name__=='__main__':
>
> ??? opt = OptionParser(
> ??? ??? usage = "usage: %prog [options] site year",
> ??? ??? description = "Count number of ITX files from a station for a given
> year."
> ??? )
> ??? opt.add_option("-e", action="store",
> ??? ??? ??? dest="email_add", help="send table as email")
> ??? opt.add_option("-l", '--lenght', action="store", default="30",
> ??? ??? ??? dest="len", help="Number of lines in table.")
>
> ??? (options, args) = opt.parse_args()
>
> ??? if len(args) != 2:
> ??? ??? call(["countinj.py", "-h"])
> ??? ??? sys.exit()

opt.print_help() seems to be what you want here.

Kent

>
> ??? (site, year) = args
>
> ??? <snip>
>
>
> I like the "help" that OptionParser creates for my program when called with
> the -h or --help option. Thus when len(args) is not correct I call the
> program using the call function in the module subprocess.? But.... Do I
> really need go to this trouble?? Is there a better way?
>
> Thanks,
> Geoff
>
> --
> NOAA Earth System Research Laboratory
> Global Monitoring Division
> 325 Broadway, R/GMD1
> Boulder, CO 80305
>
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

From kent37 at tds.net  Fri Nov 20 13:09:56 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 20 Nov 2009 07:09:56 -0500
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
Message-ID: <1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>

On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
> Say I have this package layout
>
> \myPackage
>
> __init__.py
> moduleA.py
> moduleB.py
>
> Is there a way (and I'm sure there is...) to query, for a given package
> level, which modules live under it?
> I thought I could do it like so:
>
> import myPackage
> goodQualityInfo = dir(myPackage)

One way to do this is to include an __all__ attribute in __init__.p]:
__all__ = ['moduleA', 'moduleB']

Then instead of dir(myPackage) use myPackage.__all__.

The name is standard though it is usually used for a slightly different purpose:
http://docs.python.org/tutorial/modules.html#importing-from-a-package

Kent

From kent37 at tds.net  Fri Nov 20 13:11:59 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 20 Nov 2009 07:11:59 -0500
Subject: [Tutor] copy zip file from source folder to destination and
	unzip all files within destination
In-Reply-To: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
References: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
Message-ID: <1c2a2c590911200411n745d0992t2bd46cc6f6eec23@mail.gmail.com>

On Fri, Nov 20, 2009 at 1:54 AM, MARCUS NG <markersng at gmail.com> wrote:
> Hey all,
> I have been searching online for ways to copy a zip file to a destination
> and extract the zip file with python.
> Currently nothing works due to my limited understanding.
> I am wondering if my syntax is wrong or am I missing anything?

You don't say how it fails but you should at least escape the \ in the
paths as \\ or use raw strings (r'C:\Users...')

The \char codes are escape codes so your paths are not correct.

Kent

> the code is as such. also if there is a better code, I am definitely open to
> it. If it is good, can you also explain steps?
> ###########
>
> import shutil, zipfile
> shutil.copyfile('C:\Users\blueman\Desktop\myTest1.0.zip',
> 'C:\Users\blueman\Desktop\allFiles')
>
> zipfilepath='C:\Users\blueman\Desktop\allFiles\myTest1.0.zip'
> extractiondir='C:\Users\blueman\Desktop\allFiles\test'
>
> def extract(zipfilepath, extractiondir):
> ??? zip = zipfile(zipfilepath)
> ??? zip.extractall(path=extractiondir)
> ??? print 'it works'
>
> extract()
>
> ###########
> thank you so much for your time and help in advance!!!
>
>
> ,
> Marcus
>
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

From jfabiani at yolo.com  Fri Nov 20 13:26:09 2009
From: jfabiani at yolo.com (John)
Date: Fri, 20 Nov 2009 04:26:09 -0800
Subject: [Tutor] how to access deep classes
Message-ID: <200911200426.09966.jfabiani@yolo.com>

Hi,
I'm not to sure I can explain myself.  But I need to ask because I do not 
understand how it works or what is possible.

class A (wx.Panel);
   def__init__(...)

class B(wx.PyPanel):

   def __init__(..):
     self.pages = A(...)

class C (B)
  def __init__(...)

I can't change the code in either class A or class B.  But I want to add a 
property to class A in class C.  Is that possible?

Something like

wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize, None, '')

Or is there another way to get the same thing done?

Johnf

   

From lie.1296 at gmail.com  Fri Nov 20 13:43:27 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 20 Nov 2009 23:43:27 +1100
Subject: [Tutor] Can a method be added to dictionary?
In-Reply-To: <0538412a861f4830b4090fcb2f1c1c92.squirrel@www.protopc.com>
References: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>	<5e58f2e40911191622g250574fi7bc9d3832dfd264c@mail.gmail.com>
	<0538412a861f4830b4090fcb2f1c1c92.squirrel@www.protopc.com>
Message-ID: <he6304$qkn$1@ger.gmane.org>

lauren at protopc.com wrote:
> John,
> 
> Thank you so much for your help! -- Problem SOLVED!!! -- Your explanation
> and example was extremely helpful. I am very grateful.
> 
> Lauren :-)

if you want to pass an argument, you can "curry" the function with 
functools.partial()

# currying the function, don't execute yet..
d = {'abc': functools.partial(func, arg1, arg2, arg3),
     }

# execute the curried function now
d['abc']()


From lie.1296 at gmail.com  Fri Nov 20 13:48:59 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 20 Nov 2009 23:48:59 +1100
Subject: [Tutor] how to access deep classes
In-Reply-To: <200911200426.09966.jfabiani@yolo.com>
References: <200911200426.09966.jfabiani@yolo.com>
Message-ID: <he63ag$rnj$1@ger.gmane.org>

John wrote:
> Hi,
> I'm not to sure I can explain myself.  But I need to ask because I do not 
> understand how it works or what is possible.
> 
> class A (wx.Panel);
>    def__init__(...)
> 
> class B(wx.PyPanel):
> 
>    def __init__(..):
>      self.pages = A(...)
> 
> class C (B)
>   def __init__(...)
> 
> I can't change the code in either class A or class B.  But I want to add a 
> property to class A in class C.  Is that possible?
> 
> Something like
> 
> wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize, None, '')
> 
> Or is there another way to get the same thing done?
> 

Is this what you want?

class C(B):
     @property
     def wxpanelFontSize(self):
         return self.pages.wxpanelFontSize
     @wxpanelFontSize.setter
     def wxpanelFontSize(self, value):
         self.pages.wxpanelFontSize = value


From sanelson at gmail.com  Fri Nov 20 14:08:11 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Fri, 20 Nov 2009 13:08:11 +0000
Subject: [Tutor] Replace try: except: finally:
Message-ID: <b6131fdc0911200508q30808e8cm1643c1a1dc27e898@mail.gmail.com>

I need to make some code Python 2.4 compliant... the only thing I see
is use of try: except: finally:

To make this valid, I think I need to do a try: finally: and next try:
except: inside.  Is this correct?

The code has;

try:
  ...
  ...
  ...
except SystemExit:
        raise
except KeyboardInterrupt:
        if state.output.status:
            print >> sys.stderr, "\nStopped."
        sys.exit(1)
except:
        sys.excepthook(*sys.exc_info())
        sys.exit(1)
finally:
        for key in connections.keys():
            if state.output.status:
                print "Disconnecting from %s..." % denormalize(key),
            connections[key].close()
            if state.output.status:
                print "done."

How should I replace this?

S.



-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com

From jfabiani at yolo.com  Fri Nov 20 14:17:19 2009
From: jfabiani at yolo.com (John)
Date: Fri, 20 Nov 2009 05:17:19 -0800
Subject: [Tutor] how to access deep classes
In-Reply-To: <he63ag$rnj$1@ger.gmane.org>
References: <200911200426.09966.jfabiani@yolo.com> <he63ag$rnj$1@ger.gmane.org>
Message-ID: <200911200517.19445.jfabiani@yolo.com>

On Friday 20 November 2009 04:48:59 am Lie Ryan wrote:
> Is this what you want?
>
> class C(B):
> ? ? ?@property
> ? ? ?def wxpanelFontSize(self):
> ? ? ? ? ?return self.pages.wxpanelFontSize
> ? ? ?@wxpanelFontSize.setter
> ? ? ?def wxpanelFontSize(self, value):
> ? ? ? ? ?self.pages.wxpanelFontSize = value

Maybe? @property is called a decorator.  But from what I'm reading I don't see 
how it's different from what a normal property def is like:
wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize, None, '')

I'm trying to set the font size of a text control in a class that provides no 
way to set the font size.  But in the back of my head I think all wx text 
items have FontSize.  Maybe I'm thinking about this incorrectly.  I think 
your saying that from class C I can set the font size if I have access to the 
underlying wx text control.  

Johnf


From beachkid at insightbb.com  Fri Nov 20 14:41:54 2009
From: beachkid at insightbb.com (Ken G.)
Date: Fri, 20 Nov 2009 08:41:54 -0500
Subject: [Tutor] Outputting Data to Printer
In-Reply-To: <he5kra$f6l$1@ger.gmane.org>
References: <4B05F6A0.8040106@insightbb.com> <he5kra$f6l$1@ger.gmane.org>
Message-ID: <4B069CA2.1000302@insightbb.com>

Alan Gauld wrote:
> "Ken G." <beachkid at insightbb.com> wrote
>
>> Is there a Python command to send data to printer?
>> I have a Canon MX300 hooked up by USB.   I can print from Firefox and 
>> Thunderbird.  I am using Ubuntu 9.04 and Python 2.6.2.
>
> There is no universal easy way to print stuff unfortunately.
> In console mode on Unix its not too bad, you can send data
> to lpr, just as you would from the shell.
>
> You can also format your data using fmt or groff and send that to lpr 
> as you would from the shell.
>
>
That is a surprise!  I was so use to using lprint as in Basic.  Oh 
well.  I will have to study up on fmt and/or groff.

Would those two commands (fmt, groff) send data to lpr from shell?  Is 
lpr the same hookup for USB port?

Thanks,

Ken

From alan.gauld at btinternet.com  Fri Nov 20 16:08:34 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Fri, 20 Nov 2009 15:08:34 +0000 (GMT)
Subject: [Tutor] Outputting Data to Printer
In-Reply-To: <4B069CA2.1000302@insightbb.com>
References: <4B05F6A0.8040106@insightbb.com> <he5kra$f6l$1@ger.gmane.org>
	<4B069CA2.1000302@insightbb.com>
Message-ID: <115552.73076.qm@web86701.mail.ird.yahoo.com>



> > There is no universal easy way to print stuff unfortunately.
> > In console mode on Unix its not too bad, you can send data
> > to lpr, just as you would from the shell.


> That is a surprise!  I was so use to using lprint as in Basic.  

BASIC ran on a predefined platform so could be sure where the 
printer lived and minimium set of print codes to use.

Because Unix has multile different ways to connect printrs etc 
its much harder to do.

> Oh well.  I will have to study up on fmt and/or groff.


They are formatting commands. fmt converts plain text into 
nicely aligned paragraphs etc. groff is similar to LaTeX but 
more programmer friendly (IMHO). man pages are formatted with groff.

> Would those two commands (fmt, groff) send data to lpr from shell?  

No, you usually use them as part of a pipeline, senduing the ouput 
to lpr (or maybe lp on Linux?)

> Is lpr the same hookup for USB port?

Should be, because it uses the installed printer driver.

HTH,

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/ecbe520f/attachment.htm>

From beachkid at insightbb.com  Fri Nov 20 16:36:06 2009
From: beachkid at insightbb.com (Ken G.)
Date: Fri, 20 Nov 2009 10:36:06 -0500
Subject: [Tutor] Outputting Data to Printer
In-Reply-To: <115552.73076.qm@web86701.mail.ird.yahoo.com>
References: <4B05F6A0.8040106@insightbb.com> <he5kra$f6l$1@ger.gmane.org>
	<4B069CA2.1000302@insightbb.com>
	<115552.73076.qm@web86701.mail.ird.yahoo.com>
Message-ID: <4B06B766.8070407@insightbb.com>

ALAN GAULD wrote:
>
> > > There is no universal easy way to print stuff unfortunately.
> > > In console mode on Unix its not too bad, you can send data
> > > to lpr, just as you would from the shell.
>
> > That is a surprise!  I was so use to using lprint as in Basic.  
>
> BASIC ran on a predefined platform so could be sure where the 
> printer lived and minimium set of print codes to use.
>
> Because Unix has multile different ways to connect printrs etc 
> its much harder to do.
>
> > Oh well.  I will have to study up on fmt and/or groff.
>
> They are formatting commands. fmt converts plain text into 
> nicely aligned paragraphs etc. groff is similar to LaTeX but 
> more programmer friendly (IMHO). man pages are formatted with groff.
>
> > Would those two commands (fmt, groff) send data to lpr from shell?  
>
> No, you usually use them as part of a pipeline, senduing the ouput 
> to lpr (or maybe lp on Linux?)
>
> > Is lpr the same hookup for USB port?
>
> Should be, because it uses the installed printer driver.
>
> HTH,
>
> Alan G.
>
THANKS!  Can't say enough for your excellent help. 

I do recommend your Learning to Program website.

Ken


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/50f7d1a1/attachment.htm>

From geoff.dutton at noaa.gov  Fri Nov 20 16:40:54 2009
From: geoff.dutton at noaa.gov (Geoff Dutton)
Date: Fri, 20 Nov 2009 08:40:54 -0700
Subject: [Tutor] Handling missing positional arguments
In-Reply-To: <1c2a2c590911200403o2548c55fx929760eea3a53bb9@mail.gmail.com>
References: <7a9810630911191447v1ad3e452g5bfbfa08e6a45602@mail.gmail.com>
	<1c2a2c590911200403o2548c55fx929760eea3a53bb9@mail.gmail.com>
Message-ID: <7a9810630911200740r453ab88ap60a0ce72ad2173e9@mail.gmail.com>

On Fri, Nov 20, 2009 at 5:03 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Thu, Nov 19, 2009 at 5:47 PM, Geoff Dutton <geoff.dutton at noaa.gov>
> wrote:
> > Do you have recommendations for handling missing positional arguments?
> I'm
> > a huge fan of OptionParser module and use it in several programs to
> handle
> > options, but there must be an eligant way of handling missing arguments
> and
> > alerting the user.  Here is how I have done it ...
> >
> > import sys
> > from subprocess import call
> > from optparse import OptionParser
> >
> > if __name__=='__main__':
> >
> >     opt = OptionParser(
> >         usage = "usage: %prog [options] site year",
> >         description = "Count number of ITX files from a station for a
> given
> > year."
> >     )
> >     opt.add_option("-e", action="store",
> >             dest="email_add", help="send table as email")
> >     opt.add_option("-l", '--lenght', action="store", default="30",
> >             dest="len", help="Number of lines in table.")
> >
> >     (options, args) = opt.parse_args()
> >
> >     if len(args) != 2:
> >         call(["countinj.py", "-h"])
> >         sys.exit()
>
> opt.print_help() seems to be what you want here.
>
> Kent
>
>
Exactly!  Thank you.
Geoff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/4300fef1/attachment-0001.htm>

From kent37 at tds.net  Fri Nov 20 16:47:04 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 20 Nov 2009 10:47:04 -0500
Subject: [Tutor] Replace try: except: finally:
In-Reply-To: <b6131fdc0911200508q30808e8cm1643c1a1dc27e898@mail.gmail.com>
References: <b6131fdc0911200508q30808e8cm1643c1a1dc27e898@mail.gmail.com>
Message-ID: <1c2a2c590911200747m53eac514p3430c68f5d2a98c4@mail.gmail.com>

On Fri, Nov 20, 2009 at 8:08 AM, Stephen Nelson-Smith
<sanelson at gmail.com> wrote:
> I need to make some code Python 2.4 compliant... the only thing I see
> is use of try: except: finally:
>
> To make this valid, I think I need to do a try: finally: and next try:
> except: inside. ?Is this correct?

Yes, the backwards-compatible replacement for try: except: finally: is

try:
  try:
    ...
  except:
    ...
finally:
  ...

Kent

From warpcat at gmail.com  Fri Nov 20 17:07:13 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Fri, 20 Nov 2009 08:07:13 -0800
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
Message-ID: <23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>

On Fri, Nov 20, 2009 at 4:09 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
> > Say I have this package layout
> >
> > \myPackage
> >
> > __init__.py
> > moduleA.py
> > moduleB.py
> >
> > Is there a way (and I'm sure there is...) to query, for a given package
> > level, which modules live under it?
> > I thought I could do it like so:
> >
> > import myPackage
> > goodQualityInfo = dir(myPackage)
>
> One way to do this is to include an __all__ attribute in __init__.p]:
> __all__ = ['moduleA', 'moduleB']
>
> Then instead of dir(myPackage) use myPackage.__all__.
>
> The name is standard though it is usually used for a slightly different
> purpose:
> http://docs.python.org/tutorial/modules.html#importing-from-a-package
>
> Kent
>

Thanks.  I was considering that too, but I want to be able to have people
drop modules in that dir and "be done with it":  Not also have them also
need to update __all__ in __init__.py
Appreciate the suggestion though.
My current hacky plan is just query the location on disk of the imported
package, then do a dir search for .py files in that dir, and process those.
Seems clunky though.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/f2705eab/attachment.htm>

From rdole1 at cogeco.ca  Fri Nov 20 18:11:48 2009
From: rdole1 at cogeco.ca (rick)
Date: Fri, 20 Nov 2009 12:11:48 -0500
Subject: [Tutor] Tutor Digest, Vol 69, Issue 96
In-Reply-To: <mailman.2726.1258731655.2872.tutor@python.org>
References: <mailman.2726.1258731655.2872.tutor@python.org>
Message-ID: <1258737108.27275.10.camel@rick-desktop>

On Fri, 2009-11-20 at 16:40 +0100, tutor-request at python.org wrote:
> That is a surprise!  I was so use to using lprint as in Basic.  Oh 
> well.  I will have to study up on fmt and/or groff.
> 
> Would those two commands (fmt, groff) send data to lpr from shell?
> Is 
> lpr the same hookup for USB port?
> 
> Thanks,
> 
> Ken 

well, for a long time, lpr didn't work, as all the distros had moved
away from the old lpd and gone with cups, but there was the old lpr
command that still expected lpd to be set up.   I just tried it on my
system (Ubuntu 9.10) and I find that lpr is yet again working, it has
been updated and is now a frontend for cups.

suggest you read the man pages for fmt and groff to see what those
commands will do to your text.  By default, they both output to stdout,
so you would have to redirect their output in some way to cups.

Rick


From beachkid at insightbb.com  Fri Nov 20 18:02:18 2009
From: beachkid at insightbb.com (Ken G.)
Date: Fri, 20 Nov 2009 12:02:18 -0500
Subject: [Tutor] Breaking out of loop...
Message-ID: <4B06CB9A.5000304@insightbb.com>

I am trying to break out of a loop posted below.  When asked for 
monthdate, it should break out if I entered the number zero and it does 
not.  GRRR.  Been working on this for almost an hour.

    monthdate = 999

    while monthdate <> 0:
        monthdate = raw_input('Enter the month and date in two digit 
format each: ')           
        month = monthdate[0:2]
        date = monthdate[2:4]
        year = ('2009')
        print month, date, year

    print "Finished!"

Thanking you all in advance for your help.

Ken

   

From alan.gauld at btinternet.com  Fri Nov 20 18:37:51 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 20 Nov 2009 17:37:51 -0000
Subject: [Tutor] Breaking out of loop...
References: <4B06CB9A.5000304@insightbb.com>
Message-ID: <he6k60$paq$1@ger.gmane.org>


"Ken G." <beachkid at insightbb.com> wrote

>I am trying to break out of a loop posted below.  When asked for 
>monthdate, it should break out if I entered the number zero and it does 
>not.  GRRR.  Been working on this for almost an hour.
>
>    monthdate = 999
>
>    while monthdate <> 0:

You are comparing monthdate with a number but raw_input returns a string.

Also <> is deprecated, change it to !=

Try

while monthdate != "0":

and it should work.

Except....
>        monthdate = raw_input('Enter the month and date in two digit 
> format each: ')           month = monthdate[0:2]
>        date = monthdate[2:4]
>        year = ('2009')
>        print month, date, year

With this code you always process monthdate so if the user
types zero it will fail.

The idiomatic Python way to do this is:

while True:   # loop "forever"
        monthdate = raw_input('Enter the month and date in two digit 
format each: ')
        if monthdate == "0":
               break    # exit the loop
        month = monthdate[0:2]
        date = monthdate[2:4]
        year = ('2009')
        print month, date, year

That change means you can get rid of the initial assignment to 999 too.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Fri Nov 20 18:48:38 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 20 Nov 2009 17:48:38 -0000
Subject: [Tutor] how to access deep classes
References: <200911200426.09966.jfabiani@yolo.com>
Message-ID: <he6kq7$rft$1@ger.gmane.org>

"John" <jfabiani at yolo.com> wrote

> class A (wx.Panel);
>   def__init__(...)
>
> class B(wx.PyPanel):
>
>   def __init__(..):
>     self.pages = A(...)
>
> class C (B)
>  def __init__(...)
>
> I can't change the code in either class A or class B.  But I want to add 
> a
> property to class A in class C.  Is that possible?

You need to distinguish between classes and instances thereof.

You can add attributes to instances of A, and you can access the
instance because it is stored in self.pages of C. (You can actually add
attributes and methods to the class too, after it is defined but I wouldn't
recommend that since it gets  very confusing if you duplicate a name
added to an instance elsewhere!)

So you can, within C's methods do

self.pages.foo = 66

which adds a new foo attribute to the instance of A stored in pages.
Just be careful you don't have any code anywhere that relies on the
content of A instances (eg pickle/unpickle type things).

> wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize, 
> None, '')

Sorry, the connection to A is not clear.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From beachkid at insightbb.com  Fri Nov 20 18:53:05 2009
From: beachkid at insightbb.com (Ken G.)
Date: Fri, 20 Nov 2009 12:53:05 -0500
Subject: [Tutor] Breaking out of loop...
In-Reply-To: <he6k60$paq$1@ger.gmane.org>
References: <4B06CB9A.5000304@insightbb.com> <he6k60$paq$1@ger.gmane.org>
Message-ID: <4B06D781.2020203@insightbb.com>



Alan Gauld wrote:
>
> "Ken G." <beachkid at insightbb.com> wrote
>
>> I am trying to break out of a loop posted below.  When asked for 
>> monthdate, it should break out if I entered the number zero and it 
>> does not.  GRRR.  Been working on this for almost an hour.
>>
>>    monthdate = 999
>>
>>    while monthdate <> 0:
>
> You are comparing monthdate with a number but raw_input returns a string.
>
> Also <> is deprecated, change it to !=
>
> Try
>
> while monthdate != "0":
>
> and it should work.
>
> Except....
>>        monthdate = raw_input('Enter the month and date in two digit 
>> format each: ')           month = monthdate[0:2]
>>        date = monthdate[2:4]
>>        year = ('2009')
>>        print month, date, year
>
> With this code you always process monthdate so if the user
> types zero it will fail.
>
> The idiomatic Python way to do this is:
>
> while True:   # loop "forever"
>        monthdate = raw_input('Enter the month and date in two digit 
> format each: ')
>        if monthdate == "0":
>               break    # exit the loop
>        month = monthdate[0:2]
>        date = monthdate[2:4]
>        year = ('2009')
>        print month, date, year
>
> That change means you can get rid of the initial assignment to 999 too.
>
> HTH,
>
Truly amazing!  Thanks.

Ken

From fomcl at yahoo.com  Fri Nov 20 18:55:32 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Fri, 20 Nov 2009 09:55:32 -0800 (PST)
Subject: [Tutor] Breaking out of loop...
In-Reply-To: <4B06CB9A.5000304@insightbb.com>
Message-ID: <666357.38208.qm@web110701.mail.gq1.yahoo.com>

Hi!

Slightly different (extented) than your original question, but here's how I'd do this (although the program doesn't really do very much): 

import time, random

def is_valid_date():
??? while True:
??????? prompt = 'Enter the date in ISO format (YYYY-MM-DD), or 0 to exit: '
??????? date = raw_input(prompt)
??????? try:
??????????? time.strptime(date, "%Y-%m-%d")
??????????? print "You've entered: %s" % date
??????????? return True
??????? except ValueError:
??????????? if date == str(0):
??????????????? print "Finished!"
??????????????? break
??????????? else:
??????????????? print "Invalid date or invalid date format (%s)" % date
??????????? return False

def do_something():
??? for i in range(10):
??????? print "%05d" % random.randint(0, 10**4)

if is_valid_date():
??? do_something()

Cheers!!

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the face of ambiguity, refuse the temptation to guess.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 11/20/09, Ken G. <beachkid at insightbb.com> wrote:

From: Ken G. <beachkid at insightbb.com>
Subject: [Tutor] Breaking out of loop...
To: tutor at python.org
Date: Friday, November 20, 2009, 6:02 PM

I am trying to break out of a loop posted below.? When asked for monthdate, it should break out if I entered the number zero and it does not.? GRRR.? Been working on this for almost an hour.

???monthdate = 999

???while monthdate <> 0:
? ? ???monthdate = raw_input('Enter the month and date in two digit format each: ')? ? ? ? ? ? ? ? ? month = monthdate[0:2]
? ? ???date = monthdate[2:4]
? ? ???year = ('2009')
? ? ???print month, date, year

???print "Finished!"

Thanking you all in advance for your help.

Ken

? _______________________________________________
Tutor maillist? -? Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/af589f0b/attachment.htm>

From rabidpoobear at gmail.com  Fri Nov 20 19:03:08 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Fri, 20 Nov 2009 12:03:08 -0600
Subject: [Tutor] Can a method be added to dictionary?
In-Reply-To: <he6304$qkn$1@ger.gmane.org>
References: <0a9a2a47d4f4409aed9aaaa87d85042f.squirrel@www.protopc.com>
	<5e58f2e40911191622g250574fi7bc9d3832dfd264c@mail.gmail.com>
	<0538412a861f4830b4090fcb2f1c1c92.squirrel@www.protopc.com>
	<he6304$qkn$1@ger.gmane.org>
Message-ID: <dfeb4470911201003g40f5c4eauf65ab300cfa39beb@mail.gmail.com>

Does the partial just do a lambda in the background? It's a neat
example, thanks! I've never seen this used before. Sorry if reply is
top-posted or otherwise weird, posting from mobile.

On 11/20/09, Lie Ryan <lie.1296 at gmail.com> wrote:
> lauren at protopc.com wrote:
>> John,
>>
>> Thank you so much for your help! -- Problem SOLVED!!! -- Your explanation
>> and example was extremely helpful. I am very grateful.
>>
>> Lauren :-)
>
> if you want to pass an argument, you can "curry" the function with
> functools.partial()
>
> # currying the function, don't execute yet..
> d = {'abc': functools.partial(func, arg1, arg2, arg3),
>      }
>
> # execute the curried function now
> d['abc']()
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

-- 
Sent from my mobile device

From davea at ieee.org  Fri Nov 20 19:40:13 2009
From: davea at ieee.org (Dave Angel)
Date: Fri, 20 Nov 2009 13:40:13 -0500
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
Message-ID: <4B06E28D.1000103@ieee.org>

Eric Pavey wrote:
> On Fri, Nov 20, 2009 at 4:09 AM, Kent Johnson <kent37 at tds.net> wrote:
>
>   
>> On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
>>     
>>> Say I have this package layout
>>>
>>> \myPackage
>>>
>>> __init__.py
>>> moduleA.py
>>> moduleB.py
>>>
>>> Is there a way (and I'm sure there is...) to query, for a given package
>>> level, which modules live under it?
>>> I thought I could do it like so:
>>>
>>> import myPackage
>>> goodQualityInfo = dir(myPackage)
>>>       
>> One way to do this is to include an __all__ attribute in __init__.p]:
>> __all__ = ['moduleA', 'moduleB']
>>
>> Then instead of dir(myPackage) use myPackage.__all__.
>>
>> The name is standard though it is usually used for a slightly different
>> purpose:
>> http://docs.python.org/tutorial/modules.html#importing-from-a-package
>>
>> Kent
>>
>>     
>
> Thanks.  I was considering that too, but I want to be able to have people
> drop modules in that dir and "be done with it":  Not also have them also
> need to update __all__ in __init__.py
> Appreciate the suggestion though.
> My current hacky plan is just query the location on disk of the imported
> package, then do a dir search for .py files in that dir, and process those.
> Seems clunky though.
>
>   
Doesn't seem clunky to me.  And if you do it in __init__.py, you can use 
the __file__ attribute to get your base directory.  Just put the results 
in __all__ and you combine both ideas.

DaveA

From warpcat at gmail.com  Fri Nov 20 19:58:57 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Fri, 20 Nov 2009 10:58:57 -0800
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <4B06E28D.1000103@ieee.org>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
	<4B06E28D.1000103@ieee.org>
Message-ID: <23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>

On Fri, Nov 20, 2009 at 10:40 AM, Dave Angel <davea at ieee.org> wrote:

> Eric Pavey wrote:
>
>> On Fri, Nov 20, 2009 at 4:09 AM, Kent Johnson <kent37 at tds.net> wrote:
>>
>>
>>
>>> On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
>>>
>>>
>>>> Say I have this package layout
>>>>
>>>> \myPackage
>>>>
>>>> __init__.py
>>>> moduleA.py
>>>> moduleB.py
>>>>
>>>> Is there a way (and I'm sure there is...) to query, for a given package
>>>> level, which modules live under it?
>>>> I thought I could do it like so:
>>>>
>>>> import myPackage
>>>> goodQualityInfo = dir(myPackage)
>>>>
>>>>
>>> One way to do this is to include an __all__ attribute in __init__.p]:
>>> __all__ = ['moduleA', 'moduleB']
>>>
>>> Then instead of dir(myPackage) use myPackage.__all__.
>>>
>>> The name is standard though it is usually used for a slightly different
>>> purpose:
>>> http://docs.python.org/tutorial/modules.html#importing-from-a-package
>>>
>>> Kent
>>>
>>
>> Thanks.  I was considering that too, but I want to be able to have people
>> drop modules in that dir and "be done with it":  Not also have them also
>> need to update __all__ in __init__.py
>> Appreciate the suggestion though.
>> My current hacky plan is just query the location on disk of the imported
>> package, then do a dir search for .py files in that dir, and process
>> those.
>> Seems clunky though.
>>
>>  Doesn't seem clunky to me.  And if you do it in __init__.py, you can use
> the __file__ attribute to get your base directory.  Just put the results in
> __all__ and you combine both ideas.
>
> DaveA
>

Starting to like that more ;)
thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/fb2a2be9/attachment.htm>

From warpcat at gmail.com  Fri Nov 20 20:14:19 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Fri, 20 Nov 2009 11:14:19 -0800
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
	<4B06E28D.1000103@ieee.org>
	<23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>
Message-ID: <23cba4bf0911201114k46eb0451m8d460ac7a6989279@mail.gmail.com>

On Fri, Nov 20, 2009 at 10:58 AM, Eric Pavey <warpcat at gmail.com> wrote:

> On Fri, Nov 20, 2009 at 10:40 AM, Dave Angel <davea at ieee.org> wrote:
>
>> Eric Pavey wrote:
>>
>>> On Fri, Nov 20, 2009 at 4:09 AM, Kent Johnson <kent37 at tds.net> wrote:
>>>
>>>
>>>
>>>> On Thu, Nov 19, 2009 at 9:31 PM, Eric Pavey <warpcat at gmail.com> wrote:
>>>>
>>>>
>>>>> Say I have this package layout
>>>>>
>>>>> \myPackage
>>>>>
>>>>> __init__.py
>>>>> moduleA.py
>>>>> moduleB.py
>>>>>
>>>>> Is there a way (and I'm sure there is...) to query, for a given package
>>>>> level, which modules live under it?
>>>>> I thought I could do it like so:
>>>>>
>>>>> import myPackage
>>>>> goodQualityInfo = dir(myPackage)
>>>>>
>>>>>
>>>> One way to do this is to include an __all__ attribute in __init__.p]:
>>>> __all__ = ['moduleA', 'moduleB']
>>>>
>>>> Then instead of dir(myPackage) use myPackage.__all__.
>>>>
>>>> The name is standard though it is usually used for a slightly different
>>>> purpose:
>>>> http://docs.python.org/tutorial/modules.html#importing-from-a-package
>>>>
>>>> Kent
>>>>
>>>
>>> Thanks.  I was considering that too, but I want to be able to have people
>>> drop modules in that dir and "be done with it":  Not also have them also
>>> need to update __all__ in __init__.py
>>> Appreciate the suggestion though.
>>> My current hacky plan is just query the location on disk of the imported
>>> package, then do a dir search for .py files in that dir, and process
>>> those.
>>> Seems clunky though.
>>>
>>>  Doesn't seem clunky to me.  And if you do it in __init__.py, you can use
>> the __file__ attribute to get your base directory.  Just put the results in
>> __all__ and you combine both ideas.
>>
>> DaveA
>>
>
> Starting to like that more ;)
> thanks


Results.  Thanks all.

#---------------------------
# __init__.py
import os
import glob

__all__ = []
packageDir = os.path.split(__file__)[0]
modules = glob.glob( os.path.join(packageDir, "*.py") )
for m in modules:
    justModule = os.path.splitext(os.path.split(m)[-1])[0]
    # Don't add itself to the list:
    if justModule != '__init__':
        __all__.append(justModule)
#---------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/82ac621c/attachment-0001.htm>

From davea at ieee.org  Fri Nov 20 21:05:01 2009
From: davea at ieee.org (Dave Angel)
Date: Fri, 20 Nov 2009 15:05:01 -0500
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <23cba4bf0911201137y5787ddben734c1c692f7941a9@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>	
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>	
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>	
	<4B06E28D.1000103@ieee.org>	
	<23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>	
	<23cba4bf0911201114k46eb0451m8d460ac7a6989279@mail.gmail.com>
	<23cba4bf0911201137y5787ddben734c1c692f7941a9@mail.gmail.com>
Message-ID: <4B06F66D.6020502@ieee.org>

Eric Pavey wrote:
> On Fri, Nov 20, 2009 at 11:14 AM, Eric Pavey <warpcat at gmail.com> wrote:
>
>   
>> On Fri, Nov 20, 2009 at 10:58 AM, Eric Pavey <warpcat at gmail.com> wrote:
>>
>>     
>>> On Fri, Nov 20, 2009 at 10:40 AM, Dave Angel <davea at ieee.org> wrote:
>>>
>>>       
>>> <snip>
> lol, in usual fashion, after I hack through it, while in the docs, I find
> the 'fancy' solution:
>
> import pkgutil
> import myPackage
> modules = pkgutil.iter_modules(myPackage.__path__)
> for p in modules:
>     print p[1]
>
> moduleA
> moduleB
>
>   

Thanks much for that.  It's apparently in the code, but it's not in the 
(Windows) help doc.    Is that a doc bug?


DaveA

From mgsharky619 at aim.com  Fri Nov 20 20:42:25 2009
From: mgsharky619 at aim.com (mark guinder)
Date: Fri, 20 Nov 2009 14:42:25 -0500
Subject: [Tutor] (no subject)
Message-ID: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com>

ok, all i know is that i have python installed on my computer(mac),  
and i know how to open up "IDLE", how do i write my first program,  
what do i use , how do i open that, and how to i save it as a prograam? 

From kent37 at tds.net  Fri Nov 20 21:28:34 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 20 Nov 2009 15:28:34 -0500
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <4B06F66D.6020502@ieee.org>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
	<4B06E28D.1000103@ieee.org>
	<23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>
	<23cba4bf0911201114k46eb0451m8d460ac7a6989279@mail.gmail.com>
	<23cba4bf0911201137y5787ddben734c1c692f7941a9@mail.gmail.com>
	<4B06F66D.6020502@ieee.org>
Message-ID: <1c2a2c590911201228q68071f24h11f4f601921011bf@mail.gmail.com>

On Fri, Nov 20, 2009 at 3:05 PM, Dave Angel <davea at ieee.org> wrote:
> Eric Pavey wrote:

>> lol, in usual fashion, after I hack through it, while in the docs, I find
>> the 'fancy' solution:
>>
>> import pkgutil
>> import myPackage
>> modules = pkgutil.iter_modules(myPackage.__path__)
>> for p in modules:
>> ? ?print p[1]
>>
>> moduleA
>> moduleB

> Thanks much for that. ?It's apparently in the code, but it's not in the
> (Windows) help doc. ? ?Is that a doc bug?

Seems to be. The pkgutil docs are quite sparse:
http://docs.python.org/library/pkgutil.html

Kent

From josephfennellster at gmail.com  Fri Nov 20 21:51:25 2009
From: josephfennellster at gmail.com (Joseph Fennell)
Date: Fri, 20 Nov 2009 14:51:25 -0600
Subject: [Tutor] Global Variables
Message-ID: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>

Quick introduction,  I'm Joseph Fennell, and I've been playing with python
for the past 6 years (primarily in the interpreter) and have finally gotten
started on a full project and I've run into a small problem.  I have defined
some modules, and when I import them into the page I want to use them on
after I run the program it tells me the variable I'm trying to pass through
them is not defined.

I assume that it's because the variable is considered local and the imported
functions are not technically local to that document.

The code is to pass the contents of a database through to filter by name and
quantity and as follows

=================================================

cHandler.execute("SELECT * FROM table")

#sets results from above to nl

nl = cHandler.fetchall()

dn = raw_input("User input: ")

nl2 = []

dname()
########
def dname():
    global nl
    for ndc in nl:
        if ndc[1] == dn:
            nl2.append(ndc,)
        else:
            continue
#########
tuple(nl2) = nl2

qtysrch()
#########
def qtysrch():
    global nl2
    for ndc in nl2:
        if ndc[7] == 0:
            continue
        else:
            print ndc


Any help I can get with this problem would be greatly appreciated.


Thanks



Joseph L. Fennell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/bf6e78f8/attachment.htm>

From waynejwerner at gmail.com  Fri Nov 20 22:27:52 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Fri, 20 Nov 2009 15:27:52 -0600
Subject: [Tutor] (no subject)
In-Reply-To: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com>
References: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com>
Message-ID: <333efb450911201327p63b1c3f2q69aa3031f8145772@mail.gmail.com>

On Fri, Nov 20, 2009 at 1:42 PM, mark guinder <mgsharky619 at aim.com> wrote:

> ok, all i know is that i have python installed on my computer(mac), and i
> know how to open up "IDLE", how do i write my first program, what do i use ,
> how do i open that, and how to i save it as a prograam?


Try taking a look at http://docs.python.org/ - Click the tutorial link that
says "Start here". You also might want to type "Python programming
tutorials" into your favourite search engine (Google, Yahoo, etc.)

That's really the best way to go about finding information.

Also, please read this:
http://catb.org/~esr/faqs/smart-questions.html

HTH,
-Wayne
-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/486a3e9f/attachment.htm>

From rabidpoobear at gmail.com  Fri Nov 20 22:52:18 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Fri, 20 Nov 2009 15:52:18 -0600
Subject: [Tutor] Global Variables
In-Reply-To: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
References: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
Message-ID: <dfeb4470911201352v78c13739ve68659b303312a9b@mail.gmail.com>

On Fri, Nov 20, 2009 at 2:51 PM, Joseph Fennell <josephfennellster at gmail.com
> wrote:

> Quick introduction,  I'm Joseph Fennell, and I've been playing with python
> for the past 6 years (primarily in the interpreter) and have finally gotten
> started on a full project and I've run into a small problem.  I have defined
> some modules, and when I import them into the page I want to use them on
> after I run the program it tells me the variable I'm trying to pass through
> them is not defined.
>
> I assume that it's because the variable is considered local and the
> imported functions are not technically local to that document.
>
> The code is to pass the contents of a database through to filter by name
> and quantity and as follows
>

The example code you provided is very confusing.

>
> =================================================
>
> cHandler.execute("SELECT * FROM table")
>
> #sets results from above to nl
>
> nl = cHandler.fetchall()
>
> dn = raw_input("User input: ")
>
> nl2 = []
>
> dname()
> ########
> def dname():
>     global nl
>     for ndc in nl:
>         if ndc[1] == dn:
>             nl2.append(ndc,)
>         else:
>             continue
> #########
> tuple(nl2) = nl2
>
> qtysrch()
> #########
> def qtysrch():
>     global nl2
>     for ndc in nl2:
>         if ndc[7] == 0:
>             continue
>         else:
>             print ndc
>

Are you saying these are in separate files?

Please repost the code in a more logical fashion.  Eg.

-------------------------------
~~~ somemodule.py
-------------------------------
def foobar():
    print "foobar!"

-------------------------------
~~~ main.py
-------------------------------

import somemodule
somemodule.foobar()
-------------------------------


This will make it much easier to understand your question, because I don't
see how the code sample you provided applies to your question whatsoever.
Also, if you have a lot of code post it on pastebin (I wouldn't consider
this a lot of code).  Just for future reference.
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/c0933984/attachment-0001.htm>

From warpcat at gmail.com  Fri Nov 20 23:10:08 2009
From: warpcat at gmail.com (Eric Pavey)
Date: Fri, 20 Nov 2009 14:10:08 -0800
Subject: [Tutor] Querying a packages modules?
In-Reply-To: <1c2a2c590911201228q68071f24h11f4f601921011bf@mail.gmail.com>
References: <23cba4bf0911191831l4c26c75bs42cd190380f32004@mail.gmail.com>
	<1c2a2c590911200409u7a746772ncaf35eca3e1901bb@mail.gmail.com>
	<23cba4bf0911200807v196649a3p89607fdbdfcae786@mail.gmail.com>
	<4B06E28D.1000103@ieee.org>
	<23cba4bf0911201058w54c7d365g9d7a3cf39b585ba2@mail.gmail.com>
	<23cba4bf0911201114k46eb0451m8d460ac7a6989279@mail.gmail.com>
	<23cba4bf0911201137y5787ddben734c1c692f7941a9@mail.gmail.com>
	<4B06F66D.6020502@ieee.org>
	<1c2a2c590911201228q68071f24h11f4f601921011bf@mail.gmail.com>
Message-ID: <23cba4bf0911201410o69074c2fqae1696252944fce1@mail.gmail.com>

On Fri, Nov 20, 2009 at 12:28 PM, Kent Johnson <kent37 at tds.net> wrote:

> On Fri, Nov 20, 2009 at 3:05 PM, Dave Angel <davea at ieee.org> wrote:
> > Eric Pavey wrote:
>
> >> lol, in usual fashion, after I hack through it, while in the docs, I
> find
> >> the 'fancy' solution:
> >>
> >> import pkgutil
> >> import myPackage
> >> modules = pkgutil.iter_modules(myPackage.__path__)
> >> for p in modules:
> >>    print p[1]
> >>
> >> moduleA
> >> moduleB
>
> > Thanks much for that.  It's apparently in the code, but it's not in the
> > (Windows) help doc.    Is that a doc bug?
>
> Seems to be. The pkgutil docs are quite sparse:
> http://docs.python.org/library/pkgutil.html
>
> Kent


I'd seen a reference to that module online, looked in the docs, didn't see
much.  But I imported it into my IDE anyway, which exposes all the methods
and classes, and I realized it did a lot more than the docs said.  So I
opened the module itself and poked around until I found iter_modules() and
after some trial and error got it workin'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/f3f07b05/attachment.htm>

From jfabiani at yolo.com  Fri Nov 20 23:14:45 2009
From: jfabiani at yolo.com (John)
Date: Fri, 20 Nov 2009 14:14:45 -0800
Subject: [Tutor] how to access deep classes
In-Reply-To: <he6kq7$rft$1@ger.gmane.org>
References: <200911200426.09966.jfabiani@yolo.com> <he6kq7$rft$1@ger.gmane.org>
Message-ID: <200911201414.45230.jfabiani@yolo.com>

On Friday 20 November 2009 09:48:38 am Alan Gauld wrote:
> "John" <jfabiani at yolo.com> wrote
>
> > class A (wx.Panel);
> >   def__init__(...)
> >
> > class B(wx.PyPanel):
> >
> >   def __init__(..):
> >     self.pages = A(...)
> >
> > class C (B)
> >  def __init__(...)
> >
> > I can't change the code in either class A or class B.  But I want to add
> > a
> > property to class A in class C.  Is that possible?
>
> You need to distinguish between classes and instances thereof.
>
> You can add attributes to instances of A, and you can access the
> instance because it is stored in self.pages of C. (You can actually add
> attributes and methods to the class too, after it is defined but I wouldn't
> recommend that since it gets  very confusing if you duplicate a name
> added to an instance elsewhere!)
>
> So you can, within C's methods do
>
> self.pages.foo = 66
>
> which adds a new foo attribute to the instance of A stored in pages.
> Just be careful you don't have any code anywhere that relies on the
> content of A instances (eg pickle/unpickle type things).
>
> > wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize,
> > None, '')
>
> Sorry, the connection to A is not clear.

Thanks - for your help.

Johnf

From bgailer at gmail.com  Sat Nov 21 02:10:00 2009
From: bgailer at gmail.com (bob gailer)
Date: Fri, 20 Nov 2009 20:10:00 -0500
Subject: [Tutor] Readable date arithmetic
In-Reply-To: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>
References: <b6131fdc0911182214u78f04d7dhcb6ba83816455432@mail.gmail.com>
Message-ID: <4B073DE8.4020300@gmail.com>

Stephen Nelson-Smith wrote:
> I have the following method:
>
> def get_log_dates(the_date_we_want_data_for):
>   t = time.strptime(the_date_we_want_data_for, '%Y%m%d')
>   t2 = datetime.datetime(*t[:-2])
>   extra_day = datetime.timedelta(days=1)
>   t3 = t2 + extra_day
>   next_log_date = t3.strftime('%Y%m%d')
>   return (the_date_we_want_data_for, next_log_date)
>
> Quite apart from not much liking the t[123] variables, does date
> arithmetic really need to be this gnarly?
>
> How could I improve the above, especially from a readability
> perspective?  Or is it ok?
>   

Please in future provide a specification of the algorithm. What is is to 
do. Saves me a lot of time trying to decipher the code.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From alan.gauld at btinternet.com  Sat Nov 21 02:20:08 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 21 Nov 2009 01:20:08 -0000
Subject: [Tutor] (no subject)
References: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com>
Message-ID: <he7f8p$bn6$1@ger.gmane.org>


"mark guinder" <mgsharky619 at aim.com> wrote 

> ok, all i know is that i have python installed on my computer(mac),  
> and i know how to open up "IDLE", how do i write my first program,  
> what do i use , how do i open that, and how to i save it as a prograam? 

That's slightly more than I assume for beginners in my tutorial...
So you  could try that.

Or any of the other non-programmers tutorials linked on the 
python web site

http://www.python.org

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From alan.gauld at btinternet.com  Sat Nov 21 02:31:34 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sat, 21 Nov 2009 01:31:34 -0000
Subject: [Tutor] Global Variables
References: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
Message-ID: <he7fu7$d11$1@ger.gmane.org>

"Joseph Fennell" <josephfennellster at gmail.com> wrote

> I assume that it's because the variable is considered local and the 
> imported
> functions are not technically local to that document.

global in Python means "in the same module" not global across all modules

You are best to pass data into functions as arguments and to pass
it back out as return values.

> cHandler.execute("SELECT * FROM table")
> nl = cHandler.fetchall()
>
> dn = raw_input("User input: ")

> nl2 = []
>
> dname()
> ########
> def dname():
>    global nl

This looks for or creates an nl in your module.
But you don't need the statement because you are not
assigning to nl, merely reading it so Python would find
the global variable if it existed.

>    for ndc in nl:
>        if ndc[1] == dn:
>            nl2.append(ndc,)
>        else:
>            continue

You don't need the else: continue. Python will do that
by default.

You could abbreviate your code usuing a list comprehension:

def dname(nl, dn):
     return [ ndc for ndc in nl if ncd[1] == dn ]

and call it with

nl2 = dname(nl, dn)

> #########
> tuple(nl2) = nl2

I think you are confused here. You could(should?) just do

nl2 = tuple(nl2)

Assuming you want to convert the list in nl2 into a tuple and
store the result in nl2?

If not then I am confused!

> qtysrch()
> #########
> def qtysrch():
>    global nl2
>    for ndc in nl2:
>        if ndc[7] == 0:
>            continue
>        else:
>            print ndc

Again you don't need the global since you are only rading nl2.
And again it would be better practice to pass nl2 into the function
as an argument.

> Any help I can get with this problem would be greatly appreciated.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From rabidpoobear at gmail.com  Sat Nov 21 02:33:31 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Fri, 20 Nov 2009 19:33:31 -0600
Subject: [Tutor] Global Variables
In-Reply-To: <30176b160911201444j20fa6d14ra29242ad8c91aa5a@mail.gmail.com>
References: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
	<dfeb4470911201352v78c13739ve68659b303312a9b@mail.gmail.com>
	<30176b160911201444j20fa6d14ra29242ad8c91aa5a@mail.gmail.com>
Message-ID: <dfeb4470911201733l640f2969se262676f5e555043@mail.gmail.com>

On Fri, Nov 20, 2009 at 4:44 PM, Joseph Fennell <josephfennellster at gmail.com
> wrote:

> My apologies yes these are in seperate files,
> --------------
> main.py
> ---------------
>  cHandler.execute("SELECT * FROM table")
>
> #sets results from above to nl
>
> nl = cHandler.fetchall()
>
> dn = raw_input("User input: ")
>
> nl2 = []
> ------------
> modules.py
> ----------
> dname()
> ########
> def dname():
>     global nl
>     for ndc in nl:
>         if ndc[1] == dn:
>             nl2.append(ndc,)
>         else:
>             continue
> #########
> ------------
> main.py
> ------------
> tuple(nl2) = nl2
>

Just FYI, this line does nothing.
In fact I think that it is invalid syntax.
What you probably mean to say is
nl2 = tuple(nl2)

I.E. turn nl2 into a tuple and assign it back to nl2.

>
> ----------------
> modules.py
> ----------------
> qtysrch()
> #########
> def qtysrch():
>     global nl2
>     for ndc in nl2:
>         if ndc[7] == 0:
>             continue
>         else:
>             print ndc
>
> and the major problem I'm having is in both modules when the code is ran,
> an error is returned when it attempts to pass 'dname()' that 'nl' is not
> defined.
>
>
Yes, that is because this is not how you are supposed to code in Python.

In general, using global variables should be avoided.  There are specific
cases where it's acceptable, but you should assume your case is not one of
them.

You should read through a tutorial such as Alan Gauld's tutorial, so you can
understand the 'functionality' of functions (pardon the pun.)

Essentially, what you're doing now is using functions to modify variables
DIRECTLY.  What you need to to is use functions  that take in a value or
values, and output a value or values, but do not modify variables in the
global scope.  Also, if a function prints anything or modifies any outside
sources, it should not have a return value.  For example, list.sort()
returns None because it sorts the list in-place.  However, list.split()
returns the splitted list, and does not modify the original.

You should try to come up with more descriptive variable names.  It should
be clear to me what your code does even though I have never seen it before.
What do 'nl' and 'ndc' and 'nl2' refer to?  I've changed your variable names
to more descriptive versions, but it may not be completely accurate as I'm
not sure of your code intent.

Here's a cheat sheet of how you should rewrite these functions (also take
note of the way I modified your 'if' statements.  Your 'continue' branches
are not necessary):

------------------
order.py
------------------
def display_qtys(order):
    for lineitem in order:
        if lineitem[7] != 0: #check for a nonzero quantity
            print lineitem

def search(order, query):
    results = []
    for lineitem in order:
        if lineitem [1] == query:
            results.append(lineitem)
    return results
------------------

Note that I personally would actually write the search function like this:
------------------
def search(order, query):
    return [lineitem for lineitem in order if lineitem[1] == query]
------------------

List comprehensions can be a bit scary when you're first starting out.  Once
you get used to them, you really start to like them though.

Then your main program will look like this:

-------------------
main.py
------------------
import order
# some import for the cHandler routine, wherever that comes from...

cHandler.execute("SELECT * FROM table")
results = cHandler.fetchall()

query = raw_input("Please enter item to search for: ")
filtered_results = order.search(results, query)

#display the results
order.display_qtys(filtered_results)
------------------


I strongly recommend reading more tutorials about proper function
definition, in the long run (and whenever you have a large scale project to
work on, or even a medium-scale one, at that) handling values between
functions in global variables is going to soon become a huge headache.

Hope that helps, and let me know if anything is unclear!
-Luke


P.S. please use the REPLY-ALL because when you use regular reply it does not
send to the list as well, it just sends it directly to me.

Sorry I can't be more specific with the exact error returned as I'm not near
> a machine that I have python on.
>
>
> Thanks.
>
>
>
> On Fri, Nov 20, 2009 at 3:52 PM, Luke Paireepinart <rabidpoobear at gmail.com
> > wrote:
>
>>
>>
>>  On Fri, Nov 20, 2009 at 2:51 PM, Joseph Fennell <
>> josephfennellster at gmail.com> wrote:
>>
>>> Quick introduction,  I'm Joseph Fennell, and I've been playing with
>>> python for the past 6 years (primarily in the interpreter) and have finally
>>> gotten started on a full project and I've run into a small problem.  I have
>>> defined some modules, and when I import them into the page I want to use
>>> them on after I run the program it tells me the variable I'm trying to pass
>>> through them is not defined.
>>>
>>> I assume that it's because the variable is considered local and the
>>> imported functions are not technically local to that document.
>>>
>>> The code is to pass the contents of a database through to filter by name
>>> and quantity and as follows
>>>
>>
>> The example code you provided is very confusing.
>>
>>>
>>> =================================================
>>>
>>> cHandler.execute("SELECT * FROM table")
>>>
>>> #sets results from above to nl
>>>
>>> nl = cHandler.fetchall()
>>>
>>> dn = raw_input("User input: ")
>>>
>>> nl2 = []
>>>
>>> dname()
>>> ########
>>> def dname():
>>>     global nl
>>>     for ndc in nl:
>>>         if ndc[1] == dn:
>>>             nl2.append(ndc,)
>>>         else:
>>>             continue
>>> #########
>>> tuple(nl2) = nl2
>>>
>>> qtysrch()
>>> #########
>>> def qtysrch():
>>>     global nl2
>>>     for ndc in nl2:
>>>         if ndc[7] == 0:
>>>             continue
>>>         else:
>>>             print ndc
>>>
>>
>> Are you saying these are in separate files?
>>
>> Please repost the code in a more logical fashion.  Eg.
>>
>> -------------------------------
>> ~~~ somemodule.py
>> -------------------------------
>> def foobar():
>>     print "foobar!"
>>
>> -------------------------------
>> ~~~ main.py
>> -------------------------------
>>
>> import somemodule
>> somemodule.foobar()
>> -------------------------------
>>
>>
>> This will make it much easier to understand your question, because I don't
>> see how the code sample you provided applies to your question whatsoever.
>> Also, if you have a lot of code post it on pastebin (I wouldn't consider
>> this a lot of code).  Just for future reference.
>> -Luke
>>
>
>
>
> --
> "I swear - by my life and my love of it - that I will not live my life for
> another man, nor ask another man to live for mine."
> - John Galt (Anti-hero from Ayn Rand's Atlas Shrugged)
> <br>
> <a href="http://playogg.org"><img src "
> http://www.fsf.org/resources/formats/playogg/ogg_data/play_ogg_medium
> "></a>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/ad49ca1a/attachment-0001.htm>

From kent37 at tds.net  Sat Nov 21 14:32:38 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sat, 21 Nov 2009 08:32:38 -0500
Subject: [Tutor] copy zip file from source folder to destination and
	unzip all files within destination
In-Reply-To: <5d2f2e630911201109m1973d093k321d500b95c4d342@mail.gmail.com>
References: <5d2f2e630911192254x71a5ba84i679c5287bdb7ac8b@mail.gmail.com>
	<1c2a2c590911200411n745d0992t2bd46cc6f6eec23@mail.gmail.com>
	<5d2f2e630911201109m1973d093k321d500b95c4d342@mail.gmail.com>
Message-ID: <1c2a2c590911210532wc027d6br8b50f7cf2c218d77@mail.gmail.com>

On Fri, Nov 20, 2009 at 2:09 PM, MARCUS NG <markersng at gmail.com> wrote:
> Kent,
>
> 1)I am not sure what you meant by "escape the \ "
> can you show me an example.

See Alan's email for examples.

> 2)how may I reply to tutor so that the thread continues?

Reply All

Kent

From jammer10000 at gmail.com  Sat Nov 21 17:09:30 2009
From: jammer10000 at gmail.com (Joe Bennett)
Date: Sat, 21 Nov 2009 10:09:30 -0600
Subject: [Tutor] Processing rows from CSV
In-Reply-To: <545517726-1258484248-cardhu_decombobulator_blackberry.rim.net-1199407271-@bda061.bisx.produk.on.blackberry>
References: <545517726-1258484248-cardhu_decombobulator_blackberry.rim.net-1199407271-@bda061.bisx.produk.on.blackberry>
Message-ID: <dfd7bbde0911210809h4e1262d7mad3ae21f9624e0ef@mail.gmail.com>

Doing this a lot in my daily activities... your example

> For row in reader:
>    If row['foo'] == 'something' :
>        do this etc.

 'row' will return a row within 'reader'... You can then pick through
the headers like "foo" and see what is there... I have some compare
scripts that do exactly what you are doing... Iterate by row, then by
column comparing the data in each field. They mostly compare
everything as a string. If I need to float something in a known data
set, I just specifically pull that data out and change it... There are
probably more efficient ways of doing it, but it only takes the script
about 15 seconds to do its thing, so what is saving another second or
two? :)





On Tue, Nov 17, 2009 at 12:57 PM,  <mhw at doctors.net.uk> wrote:
> Dear Tutors,
>
> A rather general question, I'm afraid. I have found myself writing some python code to handle some CSV data, using the csv. DictReader that generates a dict for each row with the key as the column heading and the value in the file as the item. Most operations involve code of the form: (Apologies for incorrect caps)
>
> For row in reader:
> ? ?If row['foo'] == 'something' :
> ? ? ? ?do this etc.
>
> Typically I'm checking some form of consistency, or adding an element to the row based on something in the row.
>
> I know about the existence of awk/ sed etc. Which could do some of this, but I ran into some problems with date manipulation, etc that they don't seem to handle very well.
>
> I wanted to ask if anyone knew of anything similar, as I'm worried about re-inventing the wheel. One option would be to use (e.g.) sqlite and then use select/ insert commands, but personally I'd much rather write conditions in python than sql.
>
> My other question was about how I do this more efficiently: I don't want to read the whole thing into memory at once, but (AFAIK) csv.DictReader doesn't support something like readline() to deliver it one at a time.
>
> Any comments gratefully received.
>
> Matt
> Sent from my BlackBerry? wireless device
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From bgailer at gmail.com  Sat Nov 21 23:56:26 2009
From: bgailer at gmail.com (bob gailer)
Date: Sat, 21 Nov 2009 17:56:26 -0500
Subject: [Tutor] Global Variables
In-Reply-To: <he7fu7$d11$1@ger.gmane.org>
References: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
	<he7fu7$d11$1@ger.gmane.org>
Message-ID: <4B08701A.2020508@gmail.com>

Alan Gauld wrote:
>>
>>
>> dname()
>> ########
>> def dname():
>>    global nl
>
> This looks for or creates an nl in your module. 
Beg to differ- global will not create it.

def f():global a
...
 >>> a
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'a' is not defined

What it does is alert the compiler that assignment to the variable will 
make it global.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From alan.gauld at btinternet.com  Sun Nov 22 00:01:25 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Sat, 21 Nov 2009 23:01:25 +0000 (GMT)
Subject: [Tutor] Global Variables
In-Reply-To: <4B08701A.2020508@gmail.com>
References: <30176b160911201251l3fa7aac7v436bef3d9eae2ea4@mail.gmail.com>
	<he7fu7$d11$1@ger.gmane.org> <4B08701A.2020508@gmail.com>
Message-ID: <495919.37160.qm@web86703.mail.ird.yahoo.com>



> > This looks for or creates an nl in your module. 
> Beg to differ- global will not create it.
>
> def f():global a


> What it does is alert the compiler that assignment to the 
> variable will make it global.

Thats what I meant, that an assignment to a variable marked 
as global inside a function will create the variable in the 
global namespace rather than in the local one, if it does not 
already exist.

But I didn't explain it very well...

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091121/a03f6a68/attachment.htm>

From jeffpeery at yahoo.com  Sun Nov 22 00:30:24 2009
From: jeffpeery at yahoo.com (Jeff Peery)
Date: Sat, 21 Nov 2009 15:30:24 -0800 (PST)
Subject: [Tutor] numpy and py2exe
Message-ID: <674144.12875.qm@web43132.mail.sp1.yahoo.com>

Hello I have?a python app that uses wxPython, numpy, and I'm trying to package it with py2exe. I get the below error. I tried putting a dummy __init__.py file in the distutils.tests directory but this did not solve the problem. how may I fix this?
?

*** searching for required modules ***
Traceback (most recent call last):
? File "C:\Users\Jeff\Documents\Working\App\setup.py", line 298, in <module>
??? cmdclass = {"py2exe": build_installer},
? File "C:\Python26\lib\distutils\core.py", line 152, in setup
??? dist.run_commands()
? File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
??? self.run_command(cmd)
? File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
??? cmd_obj.run()
? File "C:\Users\Jeff\Documents\Working\App\setup.py", line 273, in run
??? py2exe.run(self)
? File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 243, in run
??? self._run()
? File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 296, in _run
??? self.find_needed_modules(mf, required_files, required_modules)
? File "C:\Python26\lib\site-packages\py2exe\build_exe.py", line 1342, in find_n
eeded_modules
??? mf.import_hook(package, None, ["*"])
? File "C:\Python26\lib\site-packages\py2exe\mf.py", line 719, in import_hook
??? return Base.import_hook(self,name,caller,fromlist,level)
? File "C:\Python26\lib\site-packages\py2exe\mf.py", line 137, in import_hook
??? m = self.load_tail(q, tail)
? File "C:\Python26\lib\site-packages\py2exe\mf.py", line 217, in load_tail
??? raise ImportError, "No module named " + mname
ImportError: No module named numpy.distutils.tests
?
?
thanks,
Jeff


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091121/6d9251f8/attachment.htm>

From anand.shashwat at gmail.com  Sun Nov 22 09:30:02 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sun, 22 Nov 2009 14:00:02 +0530
Subject: [Tutor] multiple assignment on one line
Message-ID: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>

Ok, this is silly but i want to know is multiple assignment is possible in
one line using "+=, -=, *= etc" operators on python 2.6.4

like a,b = 0,1 which is equal to
a = 0
b = 1
on seperate lines.
similarly a = b = 0 which is equal to
a = 0
b = 0
on seperate lines.

Can we pack these two statements in one line ?
a += k
b += k
Both a,b are incremented by same value 'k'
ofcourse a,b +=k will not work but is there any such option available?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/6a4f4daa/attachment-0001.htm>

From alan.gauld at btinternet.com  Sun Nov 22 10:08:31 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 22 Nov 2009 09:08:31 -0000
Subject: [Tutor] multiple assignment on one line
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
Message-ID: <heav31$7gq$1@ger.gmane.org>


"Shashwat Anand" <anand.shashwat at gmail.com> wrote

> Can we pack these two statements in one line ?
> a += k
> b += k

> of course a,b +=k will not work but is there any such option available?

Not that I'm aware. And when I tried it just to see what happens 
it killed IDLE completely - Window disappeared  and everything!

You could of course do it in one line using a semicolon but 
it is no more concise than using two lines!

a += k; b += k

And you could use a generator expression, but thats even more typing!

a,b = (n+k for n in [a,b])

Sometimes simplest is best...

HTH,

Alan G


From anand.shashwat at gmail.com  Sun Nov 22 10:12:38 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sun, 22 Nov 2009 14:42:38 +0530
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <heav31$7gq$1@ger.gmane.org>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
	<heav31$7gq$1@ger.gmane.org>
Message-ID: <d4ab53de0911220112y225623b7q14e1852e7ce55cde@mail.gmail.com>

thanks Alan, i was just curious, thats it.
And yeah, simplest is best :)

On Sun, Nov 22, 2009 at 2:38 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Shashwat Anand" <anand.shashwat at gmail.com> wrote
>
>
>  Can we pack these two statements in one line ?
>> a += k
>> b += k
>>
>
>  of course a,b +=k will not work but is there any such option available?
>>
>
> Not that I'm aware. And when I tried it just to see what happens it killed
> IDLE completely - Window disappeared  and everything!
>
> You could of course do it in one line using a semicolon but it is no more
> concise than using two lines!
>
> a += k; b += k
>
> And you could use a generator expression, but thats even more typing!
>
> a,b = (n+k for n in [a,b])
>
> Sometimes simplest is best...
>
> HTH,
>
> Alan G
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/edf56dc4/attachment.htm>

From michele.alzetta at gmail.com  Sun Nov 22 10:12:47 2009
From: michele.alzetta at gmail.com (Michele Alzetta)
Date: Sun, 22 Nov 2009 10:12:47 +0100
Subject: [Tutor] About classes and databases
Message-ID: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>

Hallo all!

Long time since I last dabbled in python :-(

I was thinking of a possible solution to a problem I have: I could make a
Class which contains, amongst other things, a couple of datetime.datetime
objects.
The class also will have to contain an element taken from a Tuple and
various elements taken from a series of dictionaries.

First problem: when creating each instance of the class I have to pass it
the information necessary to create the correct datetime objects - how would
I do this?
Second problem: I would find myself to be working with something in the
order of 500 instances of this class contemporaneously, how do I create them
"automatically" from a series of data and once I've created them how do I
grab the correct one when I need it?

Or do you think my approach is all wrong, and it would be better for me not
to use classes at all and to store the data in a database?
But is it possible to store datetime objects and dictionaries in a database?

Thanks for any comments.

Michele
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/ded252a4/attachment.htm>

From denis.spir at free.fr  Sat Nov 14 19:31:28 2009
From: denis.spir at free.fr (spir)
Date: Sat, 14 Nov 2009 19:31:28 +0100
Subject: [Tutor] OT: Writing code while tired, counterproductive?
In-Reply-To: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
References: <64c038660911140943u2c5581b8j71e18b832447ffbe@mail.gmail.com>
Message-ID: <20091114193128.516dd062@o>

Le Sat, 14 Nov 2009 10:43:47 -0700,
Modulok <modulok at gmail.com> s'exprima ainsi:

> List,
> 
> This is kind off topic, but:
> 
> Does anyone else find, writing code while tired to be counterproductive?
> 
> It just seems like when I push myself to stay up late finishing a
> project, I sorely regret it the following day. Granted, Python is a
> fairly forgiving language and doesn't take the mental attention that
> some others require (like C++ ...or someone else's C++ ....or someone
> else's poorly written C++), but for me, writing code while tired
> usually ends up being a bad thing - In any language. But then there's
> the guilt of taking the day off and not meeting deadlines. *grumble*
> Just wondering if this is a common occurrence among the masses.
> 
> Anyone?
> -Modulok-

100% agree



denis
--------------------------------
* la vita e estrany *

http://spir.wikidot.com/




From lie.1296 at gmail.com  Sun Nov 22 10:31:45 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Sun, 22 Nov 2009 20:31:45 +1100
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
Message-ID: <heb0gp$and$1@ger.gmane.org>

Shashwat Anand wrote:
> Ok, this is silly but i want to know is multiple assignment is possible 
> in one line using "+=, -=, *= etc" operators on python 2.6.4
> 
> like a,b = 0,1 which is equal to
> a = 0
> b = 1
> on seperate lines.
> similarly a = b = 0 which is equal to
> a = 0
> b = 0
> on seperate lines.
> 
> Can we pack these two statements in one line ?
> a += k
> b += k

Can we pack them into one line? Sort of...

a += k; b += k

I was a bit surprised this doesn't work though:

a, b += k, k

> Both a,b are incremented by same value 'k'
> ofcourse a,b +=k will not work but is there any such option available?


From lie.1296 at gmail.com  Sun Nov 22 10:34:58 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Sun, 22 Nov 2009 20:34:58 +1100
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <heav31$7gq$1@ger.gmane.org>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
	<heav31$7gq$1@ger.gmane.org>
Message-ID: <heb0mo$and$2@ger.gmane.org>

Alan Gauld wrote:
> 
> "Shashwat Anand" <anand.shashwat at gmail.com> wrote
> 
>> Can we pack these two statements in one line ?
>> a += k
>> b += k
> 
>> of course a,b +=k will not work but is there any such option available?
> 
> Not that I'm aware. And when I tried it just to see what happens it 
> killed IDLE completely - Window disappeared  and everything!
> 

FYI, that's #7050: http://bugs.python.org/issue7050


From rabidpoobear at gmail.com  Sun Nov 22 12:56:25 2009
From: rabidpoobear at gmail.com (Luke Paireepinart)
Date: Sun, 22 Nov 2009 05:56:25 -0600
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
Message-ID: <dfeb4470911220356wd8a1f29y523feb24a6d5f4ab@mail.gmail.com>

On Sun, Nov 22, 2009 at 2:30 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> Ok, this is silly but i want to know is multiple assignment is possible in
> one line using "+=, -=, *= etc" operators on python 2.6.4
>
> like a,b = 0,1 which is equal to
> a = 0
> b = 1
> on seperate lines.
> similarly a = b = 0 which is equal to
> a = 0
> b = 0
> on seperate lines.
>

Please note this is only the same because you happen to be using an
immutable type!
using the 'on separate lines' version is preferable because you won't
accidentally do something like this:
>>> a = b = []
>>> a.append('foo')
>>> a
['foo']
>>> b
['foo']
>>>
if you forget your type is mutable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/fdfe607a/attachment.htm>

From davea at ieee.org  Sun Nov 22 13:05:56 2009
From: davea at ieee.org (Dave Angel)
Date: Sun, 22 Nov 2009 07:05:56 -0500
Subject: [Tutor] About classes and databases
In-Reply-To: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>
References: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>
Message-ID: <4B092924.4090907@ieee.org>

Michele Alzetta wrote:
> Hallo all!
>
> Long time since I last dabbled in python :-(
>
> I was thinking of a possible solution to a problem I have: I could make a
> Class which contains, amongst other things, a couple of datetime.datetime
> objects.
> The class also will have to contain an element taken from a Tuple and
> various elements taken from a series of dictionaries.
>
> First problem: when creating each instance of the class I have to pass it
> the information necessary to create the correct datetime objects - how would
> I do this?
> Second problem: I would find myself to be working with something in the
> order of 500 instances of this class contemporaneously, how do I create them
> "automatically" from a series of data and once I've created them how do I
> grab the correct one when I need it?
>
> Or do you think my approach is all wrong, and it would be better for me not
> to use classes at all and to store the data in a database?
> But is it possible to store datetime objects and dictionaries in a database?
>
> Thanks for any comments.
>
> Michele
>
>   
You don't supply enough information to get more than generic answers.  
And your wording is confusing to me as well. It reminds me of the kind 
of homework assignment that goes:   

Question: Use "confused" in a sentence. 
Response:  "I can't figure out a way to use "confused" in a sentence, so 
I wrote this"

It's not at all clear whether there will be a tuple and some 
dictionaries for each instance, or whether these are to be collection 
objects that your class' constructor will consult.

Problem 1:   Lots of ways to create a datetime object.  Which one you 
should use depends on what information you're starting with.  For 
example, if you're starting with text you probably want strptime(), 
while if you have a timestamp, you might use utcfromtimestamp().

Problem 2:  500 instances isn't that many, unless the object is very 
complex.  So keeping them in memory is quite practical.  But you have to 
decide the type of container, and that depends mainly on how you plan to 
access them.  Built-in choices are a list and a dict.  Dict is quite 
useful if you're always going to find them based on a single type of 
key, and if that key is unique.

But suppose these objects represent people, and you sometimes have to 
find a particular one based on name, and sometimes based on age, and 
...   Then a single dictionary doesn't work well for more than the one case.

The other tradeoff that needs to be made is whether the collection gets 
changed dynamically, or just gets built once.  And whether it needs to 
persist through multiple runs of the program.  And whether building it 
takes a long time.  And how often the collection will be accessed by a 
single (primary) key, versus multiple ones.  And so on.



From kent37 at tds.net  Sun Nov 22 14:41:26 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 22 Nov 2009 08:41:26 -0500
Subject: [Tutor] About classes and databases
In-Reply-To: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>
References: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>
Message-ID: <1c2a2c590911220541t97bdd78uaf133dab9b4bdeed@mail.gmail.com>

On Sun, Nov 22, 2009 at 4:12 AM, Michele Alzetta
<michele.alzetta at gmail.com> wrote:
> Hallo all!
>
> Long time since I last dabbled in python :-(
>
> I was thinking of a possible solution to a problem I have: I could make a
> Class which contains, amongst other things, a couple of datetime.datetime
> objects.
> The class also will have to contain an element taken from a Tuple and
> various elements taken from a series of dictionaries.
>
> First problem: when creating each instance of the class I have to pass it
> the information necessary to create the correct datetime objects - how would
> I do this?

What information do you have? You could pass that to the constructor.
Or perhaps a better approach is to create the datetime itself and pass
that to the constructor.

> Second problem: I would find myself to be working with something in the
> order of 500 instances of this class contemporaneously, how do I create them
> "automatically" from a series of data and once I've created them how do I
> grab the correct one when I need it?

Create the classes in a loop and put them in a data structure such as
list or dict. How do you know which is the "right" one? That will
determine what data structure to use.

> Or do you think my approach is all wrong, and it would be better for me not
> to use classes at all and to store the data in a database?

Your approach may be fine, a database may also work. You haven't given
enough information for us to know.

> But is it possible to store datetime objects and dictionaries in a database?

You can store dates and timestamps in a database and you can store
key-value pairs.

Kent

From alan.gauld at btinternet.com  Sun Nov 22 17:22:30 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 22 Nov 2009 16:22:30 -0000
Subject: [Tutor] multiple assignment on one line
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
	<heb0gp$and$1@ger.gmane.org>
Message-ID: <hebogp$8cn$1@ger.gmane.org>


"Lie Ryan" <lie.1296 at gmail.com> wrote

 I was a bit surprised this doesn't work though:
>
> a, b += k, k
>

What would you expect?
Remember that

a,b = c,d

is just tuple assignment without using parens (and parens are not part of 
the tuple definition)

So using tuples

a,b += k,k

would unwrap as

a,b = (a,b) + (k,k)   -> (a,b,k,k)

which should give an error since we are then unpacking 4 values
into two...

It doesn't really make sense.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From alan.gauld at btinternet.com  Sun Nov 22 17:30:08 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Sun, 22 Nov 2009 16:30:08 -0000
Subject: [Tutor] About classes and databases
References: <8079280b0911220112p731e9681n6edef472593538b8@mail.gmail.com>
Message-ID: <hebov3$9p5$1@ger.gmane.org>


"Michele Alzetta" <michele.alzetta at gmail.com> wrote

> First problem: when creating each instance of the class I have to pass it
> the information necessary to create the correct datetime objects - how 
> would
> I do this?

Same way you do any otrher kind of initialisation as arguments to the init 
method.
In fact you could create the atetime objects first and just pass them as a 
single value.
Or am I missing your point?

> Second problem: I would find myself to be working with something in the
> order of 500 instances of this class contemporaneously, how do I create 
> them
> "automatically" from a series of data and once I've created them how do I
> grab the correct one when I need it?

You probably need to use a loop and a collection.
Either a list accessed via index or a dictionary using some set of key
values from the object(ID or Name etc)

> Or do you think my approach is all wrong, and it would be better for me 
> not
> to use classes at all and to store the data in a database?

With 500 instances you are probably getting close to the point where a
database would be better. Its not impossible in memory but if you need to 
do
searches to select certain subsets etc then a database and SQL are probably
the way to go. OTOH if your problem is more about performing complex 
algorithms
using the objects them memory might be more suitable.

If you expect the problem to scale up much bigeger than 500 instances I'd
definitely go for a database.

> But is it possible to store datetime objects and dictionaries in a 
> database?

A data base table can be thought of as a dictionary.
Andmost databases have their own concept of a data object that does
most of what datetime does in Python.

If you go the database route you will need to consider the partitioning of
the application between pure python and SQL. Bulk operations should
be in SQL, operations on a single object could be on either and long 
running
calculations are probably best in Python.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From anand.shashwat at gmail.com  Sun Nov 22 18:01:48 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Sun, 22 Nov 2009 22:31:48 +0530
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <hebogp$8cn$1@ger.gmane.org>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
	<heb0gp$and$1@ger.gmane.org> <hebogp$8cn$1@ger.gmane.org>
Message-ID: <d4ab53de0911220901x20417331kdc2da725d06c709@mail.gmail.com>

Ohh, I never thought it this way. Thanks for enlightening.

On Sun, Nov 22, 2009 at 9:52 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Lie Ryan" <lie.1296 at gmail.com> wrote
>
>
> I was a bit surprised this doesn't work though:
>
>>
>> a, b += k, k
>>
>>
> What would you expect?
> Remember that
>
> a,b = c,d
>
> is just tuple assignment without using parens (and parens are not part of
> the tuple definition)
>
> So using tuples
>
> a,b += k,k
>
> would unwrap as
>
> a,b = (a,b) + (k,k)   -> (a,b,k,k)
>
> which should give an error since we are then unpacking 4 values
> into two...
>
> It doesn't really make sense.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/7844b700/attachment-0001.htm>

From lie.1296 at gmail.com  Sun Nov 22 19:33:33 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Mon, 23 Nov 2009 05:33:33 +1100
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <hebogp$8cn$1@ger.gmane.org>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>	<heb0gp$and$1@ger.gmane.org>
	<hebogp$8cn$1@ger.gmane.org>
Message-ID: <hec08l$vru$1@ger.gmane.org>

Alan Gauld wrote:
> 
> "Lie Ryan" <lie.1296 at gmail.com> wrote
> 
> I was a bit surprised this doesn't work though:
>>
>> a, b += k, k
>>
> 
> What would you expect?

I expect
a, b += c, d
to be
a += c; b += d

just like
a, b = c, d
to be
a = c; b = d

> Remember that
> 
> a,b = c,d
> 
> is just tuple assignment without using parens (and parens are not part 
> of the tuple definition)
> 
> So using tuples
> 
> a,b += k,k
> 
> would unwrap as
> 
> a,b = (a,b) + (k,k)   -> (a,b,k,k)
> 
> which should give an error since we are then unpacking 4 values
> into two...
> 
> It doesn't really make sense.

That makes sense.

I agree "a, b += k, k" is rather ambiguous.

I disagree with the finer points of the explanation though, because in:
a, b += c, d
a, b is not really a tuple, but rather ltuple (as in a += b; a is the 
lvalue and be is the value). ltuple could (and probably should) have a 
different semantic for in-place assignment rather than the (current) 
non-sensical appending the ltuple with the value tuple. No?


From bgailer at gmail.com  Sun Nov 22 21:02:00 2009
From: bgailer at gmail.com (bob gailer)
Date: Sun, 22 Nov 2009 15:02:00 -0500
Subject: [Tutor] multiple assignment on one line
In-Reply-To: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
References: <d4ab53de0911220030r46771b00s3b7cf457dbeabb1f@mail.gmail.com>
Message-ID: <4B0998B8.7060401@gmail.com>

Shashwat Anand wrote:
> Ok, this is silly but i want to know is multiple assignment is 
> possible in one line using "+=, -=, *= etc" operators on python 2.6.4

Read the manual: 6.3.1 Augmented assignment statements

"target augop expression_list
An augmented assignment evaluates the target (which, unlike normal 
assignment statements, cannot be an unpacking) and the expression list, 
performs the binary operation specific to the type of assignment on the 
two operands, and assigns the result to the original target."

-- 
Bob Gailer
Chapel Hill NC
919-636-4239

From beachkid at insightbb.com  Mon Nov 23 02:27:29 2009
From: beachkid at insightbb.com (Ken G.)
Date: Sun, 22 Nov 2009 20:27:29 -0500
Subject: [Tutor] Sorting Data in Databases
Message-ID: <4B09E501.70905@insightbb.com>

Greeting: Can someone give me a brief outline of sorting data in 
databases?  I have five databases with numeric values for each item.  To 
be more precise, they are sorted by date and if necessary, by time.  
Currently, I am using Ubuntu 9.04 in the language of Linux and Python is 
my choice of programming language.

The way I use to do it in another programming language (Liberty Basic in 
Windows) was:

1. Count number of data entry in database.

2. Dimension or create an array to equal the maximum number of entry.

3. Copy data entry from database into array.

4. Use the SORT command on array, ascending or descending sequence.

5. Copy sorted data from array into database.

 From what I know of Python so far, is it possible to sort the database 
as it or do I have to copy data into either a list, tuple or dictionary 
and sort there and then, after sorting copy sorted entry back into the 
database.

An outline of what needed to be done will be fine so I can study and 
learn the proper way of doing this in Python.  FWIW, I just finished 
Alan's tutorial on list, tuple and dictionary.  Whew!!!  Quite 
comprehensive.

Again, TIA,

Ken

Thanks, gentleman.

Ken



From lie.1296 at gmail.com  Mon Nov 23 05:17:03 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Mon, 23 Nov 2009 15:17:03 +1100
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <4B09E501.70905@insightbb.com>
References: <4B09E501.70905@insightbb.com>
Message-ID: <hed2eq$n6n$1@ger.gmane.org>

Ken G. wrote:
> The way I use to do it in another programming language (Liberty Basic in 
> Windows) was:
> 
> 1. Count number of data entry in database.
> 2. Dimension or create an array to equal the maximum number of entry.
> 3. Copy data entry from database into array.
> 4. Use the SORT command on array, ascending or descending sequence.
> 5. Copy sorted data from array into database.

In any programming language, that was the wrong algorithm, except for 
very low-volume purpose. You should ask the DBMS system to sort the 
database whenever possible instead of doing the sorting yourself.

>  From what I know of Python so far, is it possible to sort the database 
> as it or do I have to copy data into either a list, tuple or dictionary 
> and sort there and then, after sorting copy sorted entry back into the 
> database.

Question time:
1. What DBMS are you using?
2. What python interface to the DBMS you are using?
3. Why do you think you need to sort the database? There is no advantage 
(that I know of) of having a sorted database. A decent DBMS should 
determine how best to store the data (it's their problem, not yours). If 
your DBMS can't do this, you should consider using another DBMS.
4. Can't you simply sort the queried data? An SQL-based DBMS should 
support ORDER BY command to sort the SELECT-ed rows. That should be 
faster than sorting the whole database. You can sort the returned tuple 
yourself, but it would be very difficult to beat the DBMS's ORDER BY.

> An outline of what needed to be done will be fine so I can study and 
> learn the proper way of doing this in Python.  FWIW, I just finished 
> Alan's tutorial on list, tuple and dictionary.  Whew!!!  Quite 
> comprehensive.


From beachkid at insightbb.com  Mon Nov 23 06:03:12 2009
From: beachkid at insightbb.com (Ken G.)
Date: Mon, 23 Nov 2009 00:03:12 -0500
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <hed2eq$n6n$1@ger.gmane.org>
References: <4B09E501.70905@insightbb.com> <hed2eq$n6n$1@ger.gmane.org>
Message-ID: <4B0A1790.40609@insightbb.com>

I have not use the DBMS as I am unaware of them in both languages.   

Lie Ryan wrote:
> Ken G. wrote:
>> The way I use to do it in another programming language (Liberty Basic 
>> in Windows) was:
>>
>> 1. Count number of data entry in database.
>> 2. Dimension or create an array to equal the maximum number of entry.
>> 3. Copy data entry from database into array.
>> 4. Use the SORT command on array, ascending or descending sequence.
>> 5. Copy sorted data from array into database.
>
> In any programming language, that was the wrong algorithm, except for 
> very low-volume purpose. You should ask the DBMS system to sort the 
> database whenever possible instead of doing the sorting yourself.
>
>>  From what I know of Python so far, is it possible to sort the 
>> database as it or do I have to copy data into either a list, tuple or 
>> dictionary and sort there and then, after sorting copy sorted entry 
>> back into the database.
>
> Question time:
> 1. What DBMS are you using?
> 2. What python interface to the DBMS you are using?
> 3. Why do you think you need to sort the database? There is no 
> advantage (that I know of) of having a sorted database. A decent DBMS 
> should determine how best to store the data (it's their problem, not 
> yours). If your DBMS can't do this, you should consider using another 
> DBMS.
> 4. Can't you simply sort the queried data? An SQL-based DBMS should 
> support ORDER BY command to sort the SELECT-ed rows. That should be 
> faster than sorting the whole database. You can sort the returned 
> tuple yourself, but it would be very difficult to beat the DBMS's 
> ORDER BY.
>
>> An outline of what needed to be done will be fine so I can study and 
>> learn the proper way of doing this in Python.  FWIW, I just finished 
>> Alan's tutorial on list, tuple and dictionary.  Whew!!!  Quite 
>> comprehensive.
> As stated, I am unaware of any DBMS in both languages.  In Basic and 
> now in Python, I wrote up a program to input data to the datafile and 
> a program to output the content of the database.  Very primitive and 
> basic.

All of the databases I spoke of are arranged by date and if necessary, 
time.  One contains my blood glucose reading and it is now, the newest 
entry is at the bottom of the database.  I need to arrange it in 
descending order whereas I can print out a report of the last 55 entries 
for my doctor.  Some of the entries in the other databases are not in 
sequenced order and I wish them to be, either ascending or descending 
sequence.  The data are numerical, ranging in the length of 20, 22 and 
44 long with no spaces.

Thank you for letting me look a little further into DBMS.

Ken

From sanelson at gmail.com  Mon Nov 23 09:57:16 2009
From: sanelson at gmail.com (Stephen Nelson-Smith)
Date: Mon, 23 Nov 2009 08:57:16 +0000
Subject: [Tutor] Iterable Understanding
In-Reply-To: <4B003DF8.9070909@mwalsh.org>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>
	<4AFFC5D4.6000304@mwalsh.org>
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>
	<4B003DF8.9070909@mwalsh.org>
Message-ID: <b6131fdc0911230057j23281749i40fdf8fa55e028bf@mail.gmail.com>

Martin,

> ? ?def __iter__(self):
> ? ? ? ?while True:
> ? ? ? ? ? ?for logline in self.logfile:
> ? ? ? ? ? ? ? ?heappush(self.heap, (timestamp(logline), logline))
> ? ? ? ? ? ? ? ?if len(self.heap) >= self.jitter:
> ? ? ? ? ? ? ? ? ? ?break
> ? ? ? ? ? ?try:
> ? ? ? ? ? ? ? ?yield heappop(self.heap)
> ? ? ? ? ? ?except IndexError:
> ? ? ? ? ? ? ? ?raise StopIteration

In this __iter__ method, why are we wrapping a for loop in a while True?

S.

From denis.spir at free.fr  Mon Nov 23 10:06:33 2009
From: denis.spir at free.fr (spir)
Date: Mon, 23 Nov 2009 10:06:33 +0100
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <4B0A1790.40609@insightbb.com>
References: <4B09E501.70905@insightbb.com> <hed2eq$n6n$1@ger.gmane.org>
	<4B0A1790.40609@insightbb.com>
Message-ID: <20091123100633.0c0020ba@o>

"Ken G." <beachkid at insightbb.com> wrote:

> I have not use the DBMS as I am unaware of them in both languages.   

DBMS is short for Data Base Management System, see wikipedia entry on the topic.
Actually, the term is a bit ambiguous:
* From a user point of view, a BDMS will be an application allowing managing data sets in a hopefully meaningful and easy manner.
* From a programmer point of view, it will be a system for wich a binding exists in one's favorite programming language, meaning a library that that can be imported.

> Lie Ryan wrote:
> > Ken G. wrote:
> >> The way I use to do it in another programming language (Liberty Basic 
> >> in Windows) was:
> >>
> >> 1. Count number of data entry in database.
> >> 2. Dimension or create an array to equal the maximum number of entry.
> >> 3. Copy data entry from database into array.
> >> 4. Use the SORT command on array, ascending or descending sequence.
> >> 5. Copy sorted data from array into database.
> >
> > In any programming language, that was the wrong algorithm, except for 
> > very low-volume purpose. You should ask the DBMS system to sort the 
> > database whenever possible instead of doing the sorting yourself.
> >
> >>  From what I know of Python so far, is it possible to sort the 
> >> database as it or do I have to copy data into either a list, tuple or 
> >> dictionary and sort there and then, after sorting copy sorted entry 
> >> back into the database.
> >
> > Question time:
> > 1. What DBMS are you using?
> > 2. What python interface to the DBMS you are using?
> > 3. Why do you think you need to sort the database? There is no 
> > advantage (that I know of) of having a sorted database. A decent DBMS 
> > should determine how best to store the data (it's their problem, not 
> > yours). If your DBMS can't do this, you should consider using another 
> > DBMS.
> > 4. Can't you simply sort the queried data? An SQL-based DBMS should 
> > support ORDER BY command to sort the SELECT-ed rows. That should be 
> > faster than sorting the whole database. You can sort the returned 
> > tuple yourself, but it would be very difficult to beat the DBMS's 
> > ORDER BY.
> >
> >> An outline of what needed to be done will be fine so I can study and 
> >> learn the proper way of doing this in Python.  FWIW, I just finished 
> >> Alan's tutorial on list, tuple and dictionary.  Whew!!!  Quite 
> >> comprehensive.
> > As stated, I am unaware of any DBMS in both languages.  In Basic and 
> > now in Python, I wrote up a program to input data to the datafile and 
> > a program to output the content of the database.  Very primitive and 
> > basic.
> 
> All of the databases I spoke of are arranged by date and if necessary, 
> time.  One contains my blood glucose reading and it is now, the newest 
> entry is at the bottom of the database.  I need to arrange it in 
> descending order whereas I can print out a report of the last 55 entries 
> for my doctor.  Some of the entries in the other databases are not in 
> sequenced order and I wish them to be, either ascending or descending 
> sequence.  The data are numerical, ranging in the length of 20, 22 and 
> 44 long with no spaces.

I'm not sure of what order is in the file, and what order you want. Seems the data is ordered by date/time, so maybe you would like to sort last n entries by glucose rate? but it's not such clear for me.

What is clear:
* The data files are "hand-made" data recordings.
* You don't need a complicated (relational) DBMS.

If you have a bit of free time, I would advise to explore both:
* Sorting the data from files as are now (you will need to sort "records" on the proper column).
* Using the simplest possible DBMS available from python, namely I guess SQLite (correct me if I'm wrong), to replace your data storing system.

> Thank you for letting me look a little further into DBMS.
> 
> Ken




________________________________

la vita e estrany

http://spir.wikidot.com/



From zebra05 at gmail.com  Mon Nov 23 13:00:25 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Mon, 23 Nov 2009 14:00:25 +0200
Subject: [Tutor] (no subject)
In-Reply-To: <he7f8p$bn6$1@ger.gmane.org>
References: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com>
	<he7f8p$bn6$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911230400r6fce6ccfm7361eee846fbe0ad@mail.gmail.com>

Hi Mark,

When i started off i had pretty much the same questions. I think you need to
start with the Python tutorial as it will show you the basics (from the
Python shell and how to write and run Python files, to language syntax +
keywords and how to define and use functions, classes, modules etc). From
there you could approach any good tutorial to reiterate on that as you will
have a good basic grasp.


On Sat, Nov 21, 2009 at 3:20 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "mark guinder" <mgsharky619 at aim.com> wrote
>
>> ok, all i know is that i have python installed on my computer(mac),  and i
>> know how to open up "IDLE", how do i write my first program,  what do i use
>> , how do i open that, and how to i save it as a prograam?
>>
>
> That's slightly more than I assume for beginners in my tutorial...
> So you  could try that.
>
> Or any of the other non-programmers tutorials linked on the python web site
>
> http://www.python.org
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/c526dbbc/attachment.htm>

From mwalsh at mwalsh.org  Mon Nov 23 15:17:42 2009
From: mwalsh at mwalsh.org (Martin Walsh)
Date: Mon, 23 Nov 2009 08:17:42 -0600
Subject: [Tutor] Iterable Understanding
In-Reply-To: <b6131fdc0911230057j23281749i40fdf8fa55e028bf@mail.gmail.com>
References: <b6131fdc0911130958t5b372e3csbb8231644db15fd9@mail.gmail.com>	
	<4AFFC5D4.6000304@mwalsh.org>	
	<b6131fdc0911150342rf0c0639l16a5f29a5664284b@mail.gmail.com>	
	<4B003DF8.9070909@mwalsh.org>
	<b6131fdc0911230057j23281749i40fdf8fa55e028bf@mail.gmail.com>
Message-ID: <4B0A9986.1070605@mwalsh.org>

Stephen Nelson-Smith wrote:
> Martin,
> 
>>    def __iter__(self):
>>        while True:
>>            for logline in self.logfile:
>>                heappush(self.heap, (timestamp(logline), logline))
>>                if len(self.heap) >= self.jitter:
>>                    break
>>            try:
>>                yield heappop(self.heap)
>>            except IndexError:
>>                raise StopIteration
> 
> In this __iter__ method, why are we wrapping a for loop in a while True?
> 
> S.

Are you more interested in one of the loops over the other? Do you have
a more specific question?

The for loop is a means to populate the heap to a predefined size before
this iterator yields any values. You may have noticed that the first
time through the heap is empty so it will loop a number of times, as
defined by self.jitter, while subsequent entries into the loop will
break after only one execution. Perhaps not the most efficient approach
-- not sure either way, you'll have to test.

The while True loop is where values are yielded, until the heap is empty
again (heappop raises an IndexError) -- a fairly common pattern for
generators in general, I think.

The way I see it (not a programmer by trade or training, FWIW), both
loops have something like a do-until style.

HTH,
Marty

From rdmoores at gmail.com  Mon Nov 23 19:28:26 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Mon, 23 Nov 2009 10:28:26 -0800
Subject: [Tutor] Python 3.1: How to print a very large number to n
	significant digits?
Message-ID: <d71c7ed60911231028g45bdcd2bm134eb5165a31ab07@mail.gmail.com>

Can't find the answer in the docs for 3.1

To print 123**34.6 to 5 sig digits,

print("%.*e" % (4, 123**34.6))

will do the job:

>>> print("%.*e" % (4, 123**34.6))
2.0451e+72

However, if the number is 123**346, using

print("%.*e" % (4, 123**346))

gets me

>>> print("%.*e" % (5, 123**346))
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    print("%.*e" % (5, 123**346))
OverflowError: Python int too large to convert to C double

So how to do it?

Thanks,

Dick Moores

From waynejwerner at gmail.com  Mon Nov 23 21:23:54 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 23 Nov 2009 14:23:54 -0600
Subject: [Tutor] Python 3.1: How to print a very large number to n
	significant digits?
In-Reply-To: <d71c7ed60911231028g45bdcd2bm134eb5165a31ab07@mail.gmail.com>
References: <d71c7ed60911231028g45bdcd2bm134eb5165a31ab07@mail.gmail.com>
Message-ID: <333efb450911231223j49f219c1l9e1ec489f0e1f946@mail.gmail.com>

On Mon, Nov 23, 2009 at 12:28 PM, Richard D. Moores <rdmoores at gmail.com>wrote:

> Can't find the answer in the docs for 3.1
>
> To print 123**34.6 to 5 sig digits,
>
> print("%.*e" % (4, 123**34.6))
>
> will do the job:
>
> >>> print("%.*e" % (4, 123**34.6))
> 2.0451e+72
>
> However, if the number is 123**346, using
>
> print("%.*e" % (4, 123**346))
>
> gets me
>
> >>> print("%.*e" % (5, 123**346))
> Traceback (most recent call last):
>  File "<pyshell#28>", line 1, in <module>
>    print("%.*e" % (5, 123**346))
> OverflowError: Python int too large to convert to C double
>

Try the decimal module:

http://docs.python.org/library/decimal.html

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/526f37bd/attachment.htm>

From rdmoores at gmail.com  Mon Nov 23 21:56:42 2009
From: rdmoores at gmail.com (Richard D. Moores)
Date: Mon, 23 Nov 2009 12:56:42 -0800
Subject: [Tutor] Python 3.1: How to print a very large number to n
	significant digits?
In-Reply-To: <333efb450911231223j49f219c1l9e1ec489f0e1f946@mail.gmail.com>
References: <d71c7ed60911231028g45bdcd2bm134eb5165a31ab07@mail.gmail.com>
	<333efb450911231223j49f219c1l9e1ec489f0e1f946@mail.gmail.com>
Message-ID: <d71c7ed60911231256p2b4b1b10p5e8ef17d47dd035@mail.gmail.com>

On Mon, Nov 23, 2009 at 12:23, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Mon, Nov 23, 2009 at 12:28 PM, Richard D. Moores <rdmoores at gmail.com>
> wrote:
>>
>> Can't find the answer in the docs for 3.1
>>
>> To print 123**34.6 to 5 sig digits,
>>
>> print("%.*e" % (4, 123**34.6))
>>
>> will do the job:
>>
>> >>> print("%.*e" % (4, 123**34.6))
>> 2.0451e+72
>>
>> However, if the number is 123**346, using
>>
>> print("%.*e" % (4, 123**346))
>>
>> gets me
>>
>> >>> print("%.*e" % (5, 123**346))
>> Traceback (most recent call last):
>> ?File "<pyshell#28>", line 1, in <module>
>> ? ?print("%.*e" % (5, 123**346))
>> OverflowError: Python int too large to convert to C double
>
> Try the decimal module:
> http://docs.python.org/library/decimal.html
> HTH,
> Wayne

Yes, I just found this function in my collection of functions. I
believe I wrote this one myself, but I can't remember for sure:

=======================================
def numberRounding(n, significantDigits):
    """
    Rounds a string in the form of a string number
    (float or integer, negative or positive) to any number of
    significant digits. If an integer, there is no limitation on it's size.
    Safer to always have n be a string.
    """
    import decimal
    def d(x):
        return decimal.Decimal(str(x))
    decimal.getcontext().prec = significantDigits
    s = str(d(n)/1)
    s = s.lstrip('0')
    return s
=======================================

>>> numberRounding(123**346,5)
'1.2799E+723'
>>> numberRounding(123**34.6,5)
'2.0451E+72'
>>> numberRounding(12345**6789,3)
'1.36E+27777'
>>> numberRounding(3.4**-12.9,6)
'1.39291E-7'

Dick

From beachkid at insightbb.com  Tue Nov 24 03:15:20 2009
From: beachkid at insightbb.com (Ken G.)
Date: Mon, 23 Nov 2009 21:15:20 -0500
Subject: [Tutor] =?iso-8859-1?q?R=E9p=2E_=3A__Sorting_Data_in_Databases?=
In-Reply-To: <970896.38379.qm@web30906.mail.mud.yahoo.com>
References: <970896.38379.qm@web30906.mail.mud.yahoo.com>
Message-ID: <4B0B41B8.7090407@insightbb.com>


Luhmann wrote:
> I'd suggest you load all your data into a list of lists, then use the 
> .sort() method to sort it.
> The sort method takes a key= argument, which should be a function that 
> does some transformation to your data, then returns something to be 
> compared.
>
> for instance:
>
> >>> l=[[1,2,3],[2,3,4],[0,9,1]]
>
> >>> def cp(a):
>     return a[2]
>
> >>> l
> [[0, 9, 1], [1, 2, 3], [2, 3, 4]]
>
> >>> def cp(a):
>     return a[1]
>
> >>> l.sort(key=cp)
> >>> l
> [[1, 2, 3], [2, 3, 4], [0, 9, 1]]
> >>> def cp(a):
>     return a[0]
>
> >>> l.sort(key=cp)
> >>> l
> [[0, 9, 1], [1, 2, 3], [2, 3, 4]]
> >>>
>
Actually, I was fooling around with that today by adding to the list 
from the file and then sort.  I was using the following simplify method:

newlist = []

newlist = [
    456, 54, 8, 158, 878
    ]
(above data read from file)

newlist.sort()

print newlist
[8, 54, 158,  456, 878]

(still working adding to new file]

The actual sorted list has, as I recall, an apostrophe at the beginning 
and an \n at the end of every number I had.  I will be working on that. 

Thanks for your suggestion.

Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/fb03a325/attachment-0001.htm>

From anand.shashwat at gmail.com  Tue Nov 24 03:15:25 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 24 Nov 2009 07:45:25 +0530
Subject: [Tutor] understanding the behavious of parameter 'key' in sort
Message-ID: <d4ab53de0911231815l14a669dfld4fc10ab9e582bb6@mail.gmail.com>

I intended to sort a list which sorts according to user-defined custom
sorting-order.
For example: If sorting-order is "zabc...wxy", then the output will be in
lexicographically sorted order but z will always be given priority over rest
others.
as a test case i took sorting order as reverse of normal sorting, hence i
defined user_key as string.ascii_lowercase. It should sort in reverse manner
but I'm not getting expected output.
(Even though it could have just been sorted as reverse=True, but my
intention is to generalize it for this is just a test-case). I'm not able to
find where the bug lies nor am i exactly sure how the key function works,
even though i use it in a regular fashion. Can you guys help me out ?

here is my code:

import string

def userdef_sort(l, user_key):
    tr = string.maketrans("".join(sorted(user_key)), user_key)
    return [i for i in sorted(l, key = lambda x:x.translate(tr), reverse =
False)]

#user_key = raw_input()
user_key = string.ascii_lowercase[::-1]
l = ['a', 'aa', 'ab', 'abc', 'cba', 'cab']

print userdef_sort(l, user_key)

my output: ['cba', 'cab', 'a', 'ab', 'abc', 'aa']
expected output: ['cba', 'cab', 'abc', 'ab', 'aa', 'a']
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/e0f5a437/attachment.htm>

From asweigart at gmail.com  Tue Nov 24 03:54:41 2009
From: asweigart at gmail.com (Albert Sweigart)
Date: Mon, 23 Nov 2009 18:54:41 -0800
Subject: [Tutor] R?p. : Sorting Data in Databases
Message-ID: <716dd5b60911231854x4bbd46cbm286ee36b2dda04b6@mail.gmail.com>

Ken,

You should probably use the sorting functionality that your DBMS
provides. However, if you have a list of strings that end with a new
line and start with an apostrophe, you can use list comprehensions to
remove them:

newlist = [x[1:-1] for x in newlist]

You can look at the following links to find out more about list
comprehensions here:

http://www.secnetix.de/olli/Python/list_comprehensions.hawk
http://www.network-theory.co.uk/docs/pytut/ListComprehensions.html

-Al
You should check out my free beginner's Python book here:
http://inventwithpython.com


> Actually, I was fooling around with that today by adding to the list
> from the file and then sort. ?I was using the following simplify method:
>
> newlist = []
>
> newlist = [
> ? ?456, 54, 8, 158, 878
> ? ?]
> (above data read from file)
>
> newlist.sort()
>
> print newlist
> [8, 54, 158, ?456, 878]
>
> (still working adding to new file]
>
> The actual sorted list has, as I recall, an apostrophe at the beginning
> and an \n at the end of every number I had. ?I will be working on that.
>
> Thanks for your suggestion.
>
> Ken
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/fb03a325/attachment.htm>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 69, Issue 105
> **************************************
>

From lie.1296 at gmail.com  Tue Nov 24 04:30:46 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Tue, 24 Nov 2009 14:30:46 +1100
Subject: [Tutor] understanding the behavious of parameter 'key' in sort
In-Reply-To: <d4ab53de0911231815l14a669dfld4fc10ab9e582bb6@mail.gmail.com>
References: <d4ab53de0911231815l14a669dfld4fc10ab9e582bb6@mail.gmail.com>
Message-ID: <hefk44$an6$1@ger.gmane.org>

Shashwat Anand wrote:
> I intended to sort a list which sorts according to user-defined custom 
> sorting-order.
> For example: If sorting-order is "zabc...wxy", then the output will be 
> in lexicographically sorted order but z will always be given priority 
> over rest others.
> as a test case i took sorting order as reverse of normal sorting, hence 
> i defined user_key as string.ascii_lowercase. It should sort in reverse 
> manner but I'm not getting expected output.
> (Even though it could have just been sorted as reverse=True, but my 
> intention is to generalize it for this is just a test-case). I'm not 
> able to find where the bug lies nor am i exactly sure how the key 
> function works, even though i use it in a regular fashion. Can you guys 
> help me out ?

Your code is not wrong. It's your expected output (or your need) that's 
different from a typical definition of "lexicographical sorting". In a 
typical lexicographical sorting "a" comes before "ab" since "a" is 
shorter than "ab".


So, if you want this:
> expected output: ['cba', 'cab', 'abc', 'ab', 'aa', 'a']
you must use a custom cmp= argument to reverse the shorter substring case:


like this:

import string

def my_cmp(s1, s2):
     if s1.startswith(s2):
         return -1
     elif s2.startswith(s1):
         return 1
     else:
         return cmp(s1, s2)

def userdef_sort(l, user_key):
     table = string.maketrans("".join(sorted(user_key)), user_key)
     trans = lambda x: x.translate(table)
     return sorted(l, cmp=my_cmp, key=trans)

#user_key = raw_input()
user_key = string.ascii_lowercase[::-1]
l = ['a', 'aa', 'ab', 'abc', 'cba', 'cab']

print userdef_sort(l, user_key)


From pine508 at hotmail.com  Tue Nov 24 05:37:51 2009
From: pine508 at hotmail.com (Che M)
Date: Mon, 23 Nov 2009 23:37:51 -0500
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <4B09E501.70905@insightbb.com>
References: <4B09E501.70905@insightbb.com>
Message-ID: <SNT127-W12F7EE1369CC0C237DA2DFE09D0@phx.gbl>



Ken,



I would also recommend trying out databases, if you have an interest. 
I found them a fun new aspect of using Python.  I would recommend using
SQLite, which very conveniently comes with Python.  Alan Gauld's
tutorial that you've read part of has a nice section on Working with Databases, and it uses
SQLite as the example database management system.  

Understanding a bit
about how to use SQL (the Structured Query Language that most databases
use, but which is surprisingly close to English) is also a good thing
to learn as you learn about programming generally.  I thought it was going to be harder than it turned out to be.  It's not bad.  Another good helper is http://sqlzoo.net/.  What you will want to "Google up on" once you are ready to try to sort your database is "ORDER BY" in the context of SQLite.



Good luck,

Che 		 	   		  
_________________________________________________________________
Hotmail: Trusted email with powerful SPAM protection.
http://clk.atdmt.com/GBL/go/177141665/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/996a7eaf/attachment.htm>

From beachkid at insightbb.com  Tue Nov 24 06:22:27 2009
From: beachkid at insightbb.com (Ken G.)
Date: Tue, 24 Nov 2009 00:22:27 -0500
Subject: [Tutor] R?p. : Sorting Data in Databases
In-Reply-To: <716dd5b60911231854x4bbd46cbm286ee36b2dda04b6@mail.gmail.com>
References: <716dd5b60911231854x4bbd46cbm286ee36b2dda04b6@mail.gmail.com>
Message-ID: <4B0B6D93.5050405@insightbb.com>

Albert Sweigart wrote:
> Ken,
>
> You should probably use the sorting functionality that your DBMS
> provides. However, if you have a list of strings that end with a new
> line and start with an apostrophe, you can use list comprehensions to
> remove them:
>
> newlist = [x[1:-1] for x in newlist]
>
> You can look at the following links to find out more about list
> comprehensions here:
>
> http://www.secnetix.de/olli/Python/list_comprehensions.hawk
> http://www.network-theory.co.uk/docs/pytut/ListComprehensions.html
>
> -Al
> You should check out my free beginner's Python book here:
> http://inventwithpython.com
>
>   
>
Thanks for the tip.  I really don't have a DBMS here.  It is just 
something I don't use.  I will look into it.

Again, thanks.  I also appreciate the links.

Ken

From beachkid at insightbb.com  Tue Nov 24 06:26:08 2009
From: beachkid at insightbb.com (Ken G.)
Date: Tue, 24 Nov 2009 00:26:08 -0500
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <SNT127-W12F7EE1369CC0C237DA2DFE09D0@phx.gbl>
References: <4B09E501.70905@insightbb.com>
	<SNT127-W12F7EE1369CC0C237DA2DFE09D0@phx.gbl>
Message-ID: <4B0B6E70.5080604@insightbb.com>


Che M wrote:
>
> Ken,
>
> I would also recommend trying out databases, if you have an interest.  
> I found them a fun new aspect of using Python.  I would recommend 
> using SQLite, which very conveniently comes with Python.  Alan Gauld's 
> tutorial that you've read part of has a nice section on Working with 
> Databases, and it uses SQLite as the example database management system. 
>
> Understanding a bit about how to use SQL (the Structured Query 
> Language that most databases use, but which is surprisingly close to 
> English) is also a good thing to learn as you learn about programming 
> generally.  I thought it was going to be harder than it turned out to 
> be.  It's not bad.  Another good helper is http://sqlzoo.net/.  What 
> you will want to "Google up on" once you are ready to try to sort your 
> database is "ORDER BY" in the context of SQLite.
>
> Good luck,
> Che
> ------------------------------------------------------------------------
>
That is a surprise to me.  I did not know that Python would work with 
SQLite.   I will look into Alan's tutorial on DB.  I am getting more and 
more surprised of what Python can do.  Very comprehensive.   Thanks all.

Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/ec272c48/attachment-0001.htm>

From anand.shashwat at gmail.com  Tue Nov 24 11:14:44 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 24 Nov 2009 15:44:44 +0530
Subject: [Tutor] understanding the behavious of parameter 'key' in sort
In-Reply-To: <hefk44$an6$1@ger.gmane.org>
References: <d4ab53de0911231815l14a669dfld4fc10ab9e582bb6@mail.gmail.com>
	<hefk44$an6$1@ger.gmane.org>
Message-ID: <d4ab53de0911240214m5ec125f3kba3413b4a5954bfb@mail.gmail.com>

it was a bit tricky, thanks :)

On Tue, Nov 24, 2009 at 9:00 AM, Lie Ryan <lie.1296 at gmail.com> wrote:

> Shashwat Anand wrote:
>
>> I intended to sort a list which sorts according to user-defined custom
>> sorting-order.
>> For example: If sorting-order is "zabc...wxy", then the output will be in
>> lexicographically sorted order but z will always be given priority over rest
>> others.
>> as a test case i took sorting order as reverse of normal sorting, hence i
>> defined user_key as string.ascii_lowercase. It should sort in reverse manner
>> but I'm not getting expected output.
>> (Even though it could have just been sorted as reverse=True, but my
>> intention is to generalize it for this is just a test-case). I'm not able to
>> find where the bug lies nor am i exactly sure how the key function works,
>> even though i use it in a regular fashion. Can you guys help me out ?
>>
>
> Your code is not wrong. It's your expected output (or your need) that's
> different from a typical definition of "lexicographical sorting". In a
> typical lexicographical sorting "a" comes before "ab" since "a" is shorter
> than "ab".
>
>
> So, if you want this:
>
>  expected output: ['cba', 'cab', 'abc', 'ab', 'aa', 'a']
>>
> you must use a custom cmp= argument to reverse the shorter substring case:
>
>
> like this:
>
> import string
>
> def my_cmp(s1, s2):
>    if s1.startswith(s2):
>        return -1
>    elif s2.startswith(s1):
>        return 1
>    else:
>        return cmp(s1, s2)
>
>
> def userdef_sort(l, user_key):
>    table = string.maketrans("".join(sorted(user_key)), user_key)
>    trans = lambda x: x.translate(table)
>    return sorted(l, cmp=my_cmp, key=trans)
>
>
> #user_key = raw_input()
> user_key = string.ascii_lowercase[::-1]
> l = ['a', 'aa', 'ab', 'abc', 'cba', 'cab']
>
> print userdef_sort(l, user_key)
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/f9eb73e5/attachment.htm>

From anand.shashwat at gmail.com  Tue Nov 24 11:26:31 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 24 Nov 2009 15:56:31 +0530
Subject: [Tutor] Alternatives to get IP address of a computer : which one
	should I use ?
Message-ID: <d4ab53de0911240226q15112195r58540edd6f0e7653@mail.gmail.com>

I was going through a python scrip woof (
http://www.home.unix-ag.org/simon/woof ) and there was a portion of the code
dedicated to get IP address

def find_ip ():
   if sys.platform == "cygwin":
      ipcfg = os.popen("ipconfig").readlines()
      for l in ipcfg:
         try:
            candidat = l.split(":")[1].strip()
            if candidat[0].isdigit():
               break
         except:
            pass
      return candidat

   os.environ["PATH"] = "/sbin:/usr/sbin:/usr/local/sbin:" + os.environ["PATH"]
   platform = os.uname()[0];

   if platform == "Linux":
      netstat = commands.getoutput ("LC_MESSAGES=C netstat -rn")
      defiface = [i.split ()[-1] for i in netstat.split ('\n')
                                    if i.split ()[0] == "0.0.0.0"]
   elif platform in ("Darwin", "FreeBSD", "NetBSD"):
      netstat = commands.getoutput ("LC_MESSAGES=C netstat -rn")
      defiface = [i.split ()[-1] for i in netstat.split ('\n')
                                    if len(i) > 2 and i.split ()[0] ==
"default"]
   elif platform == "SunOS":
      netstat = commands.getoutput ("LC_MESSAGES=C netstat -arn")
      defiface = [i.split ()[-1] for i in netstat.split ('\n')
                                    if len(i) > 2 and i.split ()[0] ==
"0.0.0.0"]
   else:
      print >>sys.stderr, "Unsupported platform; please add support
for your platform in find_ip().";
      return None

   if not defiface:
      return None

   if platform == "Linux":
      ifcfg = commands.getoutput ("LC_MESSAGES=C ifconfig "
                                  + defiface[0]).split ("inet addr:")
   elif platform in ("Darwin", "FreeBSD", "SunOS", "NetBSD"):
      ifcfg = commands.getoutput ("LC_MESSAGES=C ifconfig "
                                  + defiface[0]).split ("inet ")

   if len (ifcfg) != 2:
      return None
   ip_addr = ifcfg[1].split ()[0]

   # sanity check
   try:
      ints = [ i for i in ip_addr.split (".") if 0 <= int(i) <= 255]
      if len (ints) != 4:
         return None
   except ValueError:
      return None

   return ip_addr


It gets OS name, run netstat -rn, gets the interface name via it ('en'
in my case i.e. Darwin, and then run ifconfig and split it via 'inet '
and gets the IP and do a check. Nice !!
However if I want to get my IP I can get it via:
>>>socket.gethostbyname(socket.gethostname())

I want to know why the above approach is followed, is it so because of
a check via network ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/07e9ba9b/attachment.htm>

From waynejwerner at gmail.com  Tue Nov 24 11:58:06 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 24 Nov 2009 04:58:06 -0600
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <4B0B6E70.5080604@insightbb.com>
References: <4B09E501.70905@insightbb.com>
	<SNT127-W12F7EE1369CC0C237DA2DFE09D0@phx.gbl> 
	<4B0B6E70.5080604@insightbb.com>
Message-ID: <333efb450911240258u1c254b99j30dfce986358992c@mail.gmail.com>

On Mon, Nov 23, 2009 at 11:26 PM, Ken G. <beachkid at insightbb.com> wrote:

> I am getting more and more surprised of what Python can do.  Very
> comprehensive.


It's a much safer bet to assume that Python can do (
http://xkcd.com/353/ )<http://xkcd.com/353/> anything
( http://xkcd.com/413/ ).

You just have to install the right libraries ;)

-Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/727e4818/attachment.htm>

From anand.shashwat at gmail.com  Tue Nov 24 12:47:06 2009
From: anand.shashwat at gmail.com (Shashwat Anand)
Date: Tue, 24 Nov 2009 17:17:06 +0530
Subject: [Tutor] value of 'e'
Message-ID: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>

Followed by this discussion on Hacker
News<http://news.ycombinator.com/item?id=958323>I checked this
link <http://www.isotf.org/?page_value=13223> and was wondering how to
calculate value of 'e' <http://mathworld.wolfram.com/e.html> to a large
extent

as e = 1/0! + 1/1! +1/2! .... and so on...
so i wrote this:
>>> sum(1.0 / math.factorial(i) for i in range(100))
2.7182818284590455

It was not giving the precision that I wanted so I tried decimal module of
which I was not much aware of.

>>> decimal.getcontext().prec = 100
>>> sum(decimal.Decimal(str(1./math.factorial(decimal.Decimal(i)))) for i in
range(100))
Decimal('2.718281828459409995603699925637255290043107782360218523330012825771122202286299367023903783933889309')

Until now no problem
I matched the value of 'e' from
here<http://dl.dropbox.com/u/59605/ten_million_e.txt>
which claims it have 10 million digits of e
the first few digits of 'e' from there which doesn't match with the result I
got:
2.71828182845904523536028747135266249775724709369995957496696762772407

so i tried,
>>> sum(decimal.Decimal(str(1./math.factorial(decimal.Decimal(i)))) for i in
range(1000))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 2, in <genexpr>
OverflowError: long int too large to convert to float

And then i went clueless !!
How can it be done ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/6e6cda7e/attachment.htm>

From waynejwerner at gmail.com  Tue Nov 24 13:01:46 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Tue, 24 Nov 2009 06:01:46 -0600
Subject: [Tutor] value of 'e'
In-Reply-To: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
Message-ID: <333efb450911240401g11542111jc34e76e7d39e870@mail.gmail.com>

On Tue, Nov 24, 2009 at 5:47 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

>
> And then i went clueless !!
> How can it be done ?
>

Well, upon inspection it seems that "The math module consists mostly of thin
wrappers around the platform C math library functions" - one would presume
those are accurate, but I don't know to how many places. You might try
writing your own factorial function that works with the decimal type and
compare with the result you get from using the math library.

If you find a discrepancy I'm sure there are places to file a bug report.

HTH,
Wayne

(Of course it's also possible that your source who claims to have the
correct digits is faulty! See if you can verify by other sources)
-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/276babc4/attachment.htm>

From reply_to_is_valid at nowhere.com.invalid  Tue Nov 24 11:06:24 2009
From: reply_to_is_valid at nowhere.com.invalid (Nick)
Date: Tue, 24 Nov 2009 10:06:24 +0000 (UTC)
Subject: [Tutor] Is pydoc the right API docs?
Message-ID: <hegb70$5sj$1@ger.gmane.org>

I'm not sure I'm using pydoc correctly.  I only seem to get abbreviated 
help rather than full documentation.  It happens often enough that I think 
I'm doing something wrong.

For example, I want to upgrade my scripts to use .format() from using %.

   $ pydoc format
   Help on built-in function format in module __builtin__:

   format(...)
   format(value[, format_spec]) -> string
    
   Returns value.__format__(format_spec)
   format_spec defaults to ""

Well, that just tells me that there is an entity called format_spec.  I 
want to know what format_spec actually IS so I can use it.  I try:

   $ pydoc -k format_spec
   $

Nothing.

I found the answer using google, but that won't work if I'm offline.  Am I 
using pydoc correctly?  Or is there a more complete language spec?


From lie.1296 at gmail.com  Tue Nov 24 13:19:03 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Tue, 24 Nov 2009 23:19:03 +1100
Subject: [Tutor] value of 'e'
In-Reply-To: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
Message-ID: <hegj2k$pi6$1@ger.gmane.org>

Shashwat Anand wrote:
> How can it be done ?

 >>> import decimal, math
 >>> D = decimal.Decimal
 >>> decimal.getcontext().prec = 100
 >>> sum(D(1) / D(math.factorial(i)) for i in range(1000))
Decimal('2.718281828459045235360287471352662497757247093699959574966967627724076
630353547594571382178525166428')


From kent37 at tds.net  Tue Nov 24 13:26:57 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 07:26:57 -0500
Subject: [Tutor] value of 'e'
In-Reply-To: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
Message-ID: <1c2a2c590911240426t66e3a90dh19407e5ab0cf0abf@mail.gmail.com>

On Tue, Nov 24, 2009 at 6:47 AM, Shashwat Anand
<anand.shashwat at gmail.com> wrote:
> Followed by this discussion on Hacker News I checked this link and was
> wondering how to calculate value of 'e' to a large extent
>
> as e = 1/0! + 1/1! +1/2! .... and so on...
> so i wrote this:
>>>> sum(1.0 / math.factorial(i) for i in range(100))
> 2.7182818284590455
>
> It was not giving the precision that I wanted so I tried decimal module of
> which I was not much aware of.
>
>>>> decimal.getcontext().prec = 100
>>>> sum(decimal.Decimal(str(1./math.factorial(decimal.Decimal(i)))) for i in
>>>> range(100))

You are using floating point division here. The argument to
math.factorial() is an integer, so the conversion of i to Decimal is
not doing anything - it is converted back to an integer. Then you
compute 1./<some large integer>. This will use floating point math and
will fail when the factorial is too large to represent in floating
point.

You should convert the result of factorial() to Decimal and compute
Decimal(1.0)/Decimal(<factorial>). This should give you additional
precision as well.

Kent

From kent37 at tds.net  Tue Nov 24 13:33:11 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 07:33:11 -0500
Subject: [Tutor] value of 'e'
In-Reply-To: <hegj2k$pi6$1@ger.gmane.org>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
	<hegj2k$pi6$1@ger.gmane.org>
Message-ID: <1c2a2c590911240433n6e554a51pfac1cc8f048bb33b@mail.gmail.com>

On Tue, Nov 24, 2009 at 7:19 AM, Lie Ryan <lie.1296 at gmail.com> wrote:
> Shashwat Anand wrote:
>>
>> How can it be done ?
>
>>>> import decimal, math
>>>> D = decimal.Decimal
>>>> decimal.getcontext().prec = 100
>>>> sum(D(1) / D(math.factorial(i)) for i in range(1000))
> Decimal('2.718281828459045235360287471352662497757247093699959574966967627724076
> 630353547594571382178525166428')

And with the correct arithmetic, as above, there is no need to compute
so many terms.
You just have to get to a value of i for which i! > 10^100. i=80 works:

In [8]: sum(D(1) / D(math.factorial(i)) for i in range(80))
Decimal('2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166428')

Kent

From kent37 at tds.net  Tue Nov 24 13:35:51 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 07:35:51 -0500
Subject: [Tutor] Is pydoc the right API docs?
In-Reply-To: <hegb70$5sj$1@ger.gmane.org>
References: <hegb70$5sj$1@ger.gmane.org>
Message-ID: <1c2a2c590911240435l61a00dbel6ef8f1a61128b279@mail.gmail.com>

On Tue, Nov 24, 2009 at 5:06 AM, Nick
<reply_to_is_valid at nowhere.com.invalid> wrote:
> I'm not sure I'm using pydoc correctly. ?I only seem to get abbreviated
> help rather than full documentation. ?It happens often enough that I think
> I'm doing something wrong.

> I found the answer using google, but that won't work if I'm offline. ?Am I
> using pydoc correctly? ?Or is there a more complete language spec?

pydoc just shows the docstrings. It does not include the full text of
the documentation. For that see
http://python.org/doc/

You can download the docs for offline use, see the above link.

Kent

From kent37 at tds.net  Tue Nov 24 13:37:13 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 07:37:13 -0500
Subject: [Tutor] value of 'e'
In-Reply-To: <333efb450911240401g11542111jc34e76e7d39e870@mail.gmail.com>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
	<333efb450911240401g11542111jc34e76e7d39e870@mail.gmail.com>
Message-ID: <1c2a2c590911240437r421071djc2500c3751ee968f@mail.gmail.com>

On Tue, Nov 24, 2009 at 7:01 AM, Wayne Werner <waynejwerner at gmail.com> wrote:

> Well, upon inspection it seems that "The math module consists mostly of thin
> wrappers around the platform C math library functions" - one would presume
> those are accurate, but I don't know to how many places. You might try
> writing your own factorial function that works with the decimal type and
> compare with the result you get from using the math library.

I don't think there is anything wrong with math.factorial(). The
problem is that he is using floating-point (limited precision)
division.

Kent

From lie.1296 at gmail.com  Tue Nov 24 13:38:28 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Tue, 24 Nov 2009 23:38:28 +1100
Subject: [Tutor] value of 'e'
In-Reply-To: <333efb450911240401g11542111jc34e76e7d39e870@mail.gmail.com>
References: <d4ab53de0911240347s657159f9t2de2282931dc4fda@mail.gmail.com>
	<333efb450911240401g11542111jc34e76e7d39e870@mail.gmail.com>
Message-ID: <hegk6u$slt$1@ger.gmane.org>

Wayne Werner wrote:
> You
> might try writing your own factorial function that works with the 
> decimal type and compare with the result you get from using the math 
> library.

There is no need for that, math.factorial will use python int/long 
object instead of the platform's integer as necessary.



From jammer10000 at gmail.com  Tue Nov 24 17:27:05 2009
From: jammer10000 at gmail.com (Joe Bennett)
Date: Tue, 24 Nov 2009 10:27:05 -0600
Subject: [Tutor] Class understanding
Message-ID: <dfd7bbde0911240827p3ea78ad5x87af5b68c69c9d0@mail.gmail.com>

Hi all... Have been attempting to understand classes... Been getting
along without them for a while now and feel it's time to jump in....

What I want to do it start a log with the logging module... I have
this working without classes, but want to try... Here is a snippet of
the code that I am hacking on:


class logger():





                        import logging
                        logging.basicConfig(level=logging.INFO,
                                            format='%(asctime)s -
%(name)-12s - %(levelname)-8s - %(message)s',
                                            #format='%(asctime)s
%(levelname)s %(message)s',
                                            filename='T2Notify.log',
                                            filemode='a')
                        logging_output =
logging.getLogger('logging_output.core')
                        print "Log set up"

                        def write2log(log_info):

                                    logging_output.info("log started")
                                    print "written to log"
                                    return()


logger()
logger.write2log(log_info)



What I want to do it be able to set up the log, but have something
outside the class be able to write log updates to write2log under the
logger class... the logger.write2log() is not working :)... Any ideas,
encouragement, or pointers to good docs would be helpful... I've done
a lot of searching via Google on classes, and it's all confusing to
me...




-Joe

From roadierich at googlemail.com  Tue Nov 24 17:43:50 2009
From: roadierich at googlemail.com (Rich Lovely)
Date: Tue, 24 Nov 2009 16:43:50 +0000
Subject: [Tutor] Is pydoc the right API docs?
In-Reply-To: <1c2a2c590911240435l61a00dbel6ef8f1a61128b279@mail.gmail.com>
References: <hegb70$5sj$1@ger.gmane.org>
	<1c2a2c590911240435l61a00dbel6ef8f1a61128b279@mail.gmail.com>
Message-ID: <f0b4202b0911240843i45ac9fe1o2aef31ac704ba600@mail.gmail.com>

11/24 Kent Johnson <kent37 at tds.net>:
> On Tue, Nov 24, 2009 at 5:06 AM, Nick
> <reply_to_is_valid at nowhere.com.invalid> wrote:
>> I'm not sure I'm using pydoc correctly. ?I only seem to get abbreviated
>> help rather than full documentation. ?It happens often enough that I think
>> I'm doing something wrong.
>
>> I found the answer using google, but that won't work if I'm offline. ?Am I
>> using pydoc correctly? ?Or is there a more complete language spec?
>
> pydoc just shows the docstrings. It does not include the full text of
> the documentation. For that see
> http://python.org/doc/
>
> You can download the docs for offline use, see the above link.
>
> Kent
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Although, iirc the online docs are generated by pydoc.

It tells you that it calls value.__format__, so try $pydoc some_value.__format__

For example:

$pydoc str.__format__

I don't have python installed here, so can't check it, but it might
give you some more information.   The same will happen if you try
looking at the pydocs for, for example, str or repr:  they are
wrappers for magic methods on the object it is called with.

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.

From kent37 at tds.net  Tue Nov 24 18:03:19 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 12:03:19 -0500
Subject: [Tutor] Is pydoc the right API docs?
In-Reply-To: <f0b4202b0911240843i45ac9fe1o2aef31ac704ba600@mail.gmail.com>
References: <hegb70$5sj$1@ger.gmane.org>
	<1c2a2c590911240435l61a00dbel6ef8f1a61128b279@mail.gmail.com>
	<f0b4202b0911240843i45ac9fe1o2aef31ac704ba600@mail.gmail.com>
Message-ID: <1c2a2c590911240903i3199bc2o31ea7e2a13ed79f8@mail.gmail.com>

On Tue, Nov 24, 2009 at 11:43 AM, Rich Lovely <roadierich at googlemail.com> wrote:
> 11/24 Kent Johnson <kent37 at tds.net>:

>> pydoc just shows the docstrings. It does not include the full text of
>> the documentation. For that see
>> http://python.org/doc/

> Although, iirc the online docs are generated by pydoc.

No, they are created with Sphinx from reStructuredText source. Click
the Show Source link in the sidebar of any docs page to see.

The online docs do include much of the same text as the doc strings
but they are separately written and generated.

Kent

From pine508 at hotmail.com  Tue Nov 24 18:04:42 2009
From: pine508 at hotmail.com (Che M)
Date: Tue, 24 Nov 2009 12:04:42 -0500
Subject: [Tutor] Sorting Data in Databases
In-Reply-To: <4B0B6E70.5080604@insightbb.com>
References: <4B09E501.70905@insightbb.com>
	<SNT127-W12F7EE1369CC0C237DA2DFE09D0@phx.gbl>,
	<4B0B6E70.5080604@insightbb.com>
Message-ID: <SNT127-W53A6B88D01507F23A0B6FE09D0@phx.gbl>



> That is a surprise to me.  I did not know that Python would work with
SQLite.   

Sure, as someone else said, Python comes with a LOT of libraries built right
in when you download Python.  This is known as "batteries included", that is,
what comes with the standard distribution of Python.  

> I will look into Alan's tutorial on DB. 

It is of course thorough and will really provide understanding.  But just to
emphasize how simple creating a SQLite database in Python is, I recommend
you do these 4 simple steps:

1. Download and install the very nice SQLite Database Browser application, 
from here:
http://sourceforge.net/projects/sqlitebrowser/

It's simple and good.  

2. Now open IDLE (which also comes with Python), do File > New Window,
and paste this simple Python code into that window:

#--------------------------------------
#get SQLite into Python...it's that simple!
import sqlite3

#Make a connection to a database...if it doesn't exist yet, we'll create it.
conn = sqlite3.connect('my_database.db')   

#Create a "cursor", a kind of "pen" that writes into the database.
cur = conn.cursor()

#Write a table, called here MyTable, into the database, and give it two fields,
# name and address.
cur.execute('''CREATE TABLE if not exists MyTable (name, address)''')

#Now actually write some data into the table you made:
cur.execute('INSERT INTO MyTable VALUES(?,?)',('John','Chicago'))

#Always have to commit any changes--or they don't "stick"!
conn.commit()

#You're done!
#------------------------------------------

Without the comments, (which explain a bit about why it is written
as it is) this is just this small an amount of Python code--6 lines:

import sqlite3

conn = sqlite3.connect('my_database.db')   

cur = conn.cursor()

cur.execute('''CREATE TABLE if not exists MyTable (name, address)''')

cur.execute('INSERT INTO MyTable VALUES(?,?)',('John','Chicago'))

conn.commit()


3. Run your program in IDLE (Run > Run Module...or just hit F5).  Save
it to your Desktop.

4. Now view your handiwork in the SQLite Database Browser.  Open
it and then do File > Open Database, then find a file on your Desktop
called mydatabase.db.  Open it.  Now you are looking at the database
you just made.  Click on the Browse Data tab and you are now seeing
that John lives in Chicago.

It's that simple to at least get started.  Thanks, Python.

Che










 I am getting more
and more surprised of what Python can do.  Very comprehensive.   Thanks
all.




Ken 
 		 	   		  
_________________________________________________________________
Bing brings you maps, menus, and reviews organized in one place.
http://www.bing.com/search?q=restaurants&form=MFESRP&publ=WLHMTAG&crea=TEXT_MFESRP_Local_MapsMenu_Resturants_1x1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/c71d58a0/attachment-0001.htm>

From pine508 at hotmail.com  Tue Nov 24 18:25:27 2009
From: pine508 at hotmail.com (Che M)
Date: Tue, 24 Nov 2009 12:25:27 -0500
Subject: [Tutor] Class understanding
In-Reply-To: <dfd7bbde0911240827p3ea78ad5x87af5b68c69c9d0@mail.gmail.com>
References: <dfd7bbde0911240827p3ea78ad5x87af5b68c69c9d0@mail.gmail.com>
Message-ID: <SNT127-W48DB8B684EBC4BBC467DE0E09D0@phx.gbl>




> Date: Tue, 24 Nov 2009 10:27:05 -0600
> From: jammer10000 at gmail.com
> To: tutor at python.org
> Subject: [Tutor] Class understanding
> 
> Hi all... Have been attempting to understand classes... Been getting
> along without them for a while now and feel it's time to jump in....
> 
> What I want to do it start a log with the logging module... I have
> this working without classes, but want to try... Here is a snippet of
> the code that I am hacking on:

I'm sure the better explainers will jump in presently, but let me try
a few tips...

> class logger():

The convention in Python is to make class names capitalized.  It is
not necessary, but it is a good habit to get into, so class Logger().

>                         import logging

Imports are traditionally done at the top of a Python file, not within
a class. 

> logger()

This calls the class but doesn't create a name for an instance of
the class, so you won't be able to access it later.  Instead, try
(assuming you rename logger() to Logger() ),

logger_instance = Logger()

Now you have a name for that instance of the class, and so
can access the goodies inside the class.  

> logger.write2log(log_info)

So that would now be:

logger_instance.write2log(log_info)

> encouragement, or pointers to good docs would be helpful... I've done
> a lot of searching via Google on classes, and it's all confusing to
> me...

Keep trying.  There have to be tons of good tutorials on classes.
They fall under the heading of "Object Oriented Programming".   I tend
to think of a class as a "container" that has all the stuff you will need
to do a certain set of actions.  It can contain data (facts) and it can 
contain methods (functions).  You can create one or more "instances"
of any class (a traditional example being that Dog() is a class whereas
fluffy is an instance of a dog, and therefore has all the traditional dog
methods, like bark(), wag(), etc.)

CM







 		 	   		  
_________________________________________________________________
Windows 7: It works the way you want. Learn more.
http://www.microsoft.com/Windows/windows-7/default.aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009v2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/4a6d72e4/attachment.htm>

From zstumgoren at gmail.com  Tue Nov 24 20:02:34 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Tue, 24 Nov 2009 14:02:34 -0500
Subject: [Tutor] the art of testing
Message-ID: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>

Hi everyone,
The list recently discussed the virtues of unit testing, and I was
hoping someone could offer some high-level advice and further
resources as I try to apply the TDD methodology.

I'm trying to develop an application that regularly downloads some
government data (in XML), parses the data and then updates a database.
Simple enough in theory, but the problem I'm hitting is where to begin
with tests on data that is ALL over the place.

The agency in question performs little data validation, so a given
field can have a huge range of possible inputs (e.g. - a Boolean field
should be 0 or 1, but might be blank, have a negative number or even
strings like the word 'None').

In such a case, should I be writing test cases for *expected* inputs
and then coding the the parser portion of my program to handle the
myriad of possible "bad" data?

Or given the range of possible inputs, should I simply skip testing
for valid data at the parser level, and instead worry about flagging
(or otherwise handling) invalid input at the database-insertion layer
(and therefore write tests at that layer)?

Or should I not be testing data values at all, but rather the results
of actions performed on that data?

It seems like these questions must be a subset of the issues in the
realm of testing. Can anyone recommend a resource on the types of
tests that should be applied to the various tasks and stages of the
development process?

A friend recommended The Art of Software Testing -- is that the type
of book that covers these issues? If so, can anyone recommend a
suitable alternative that costs less than $100?

As always, I appreciate the advice.

Regards,
Serdar

From alan.gauld at btinternet.com  Tue Nov 24 21:08:45 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Tue, 24 Nov 2009 20:08:45 -0000
Subject: [Tutor] (no subject)
References: <E7811A1F-D08F-475C-84EB-E500C5EA2F6C@aim.com><he7f8p$bn6$1@ger.gmane.org>
	<c7c6f3bc0911230400r6fce6ccfm7361eee846fbe0ad@mail.gmail.com>
Message-ID: <heheh1$rlh$1@ger.gmane.org>

"OkaMthembo" <zebra05 at gmail.com> wrote

> When i started off i had pretty much the same questions. I think you need 
> to
> start with the Python tutorial as it will show you the basics

Unfortunately it won't if the OP is a complete beginner - and from his 
email it
sounds like he is. The standard tutorial assumes quite a lot of knowledge
about programming, assuming you know at least one other language.

Thats why there are several "absolute beginners" tutorials - because for
many python programmers it is their first exposure and the standard 
tutorial
is not ideal for them.

OTOH, If you have ever done any programming before then the standard
tutorial is excellent.

> keywords and how to define and use functions, classes, modules etc).

This is a good example. The standard tutorial assumes readers know
what a function is and why you'd want to use one.
The section on classes starts with a fauirly detailed description of
namespaces and scopes and the fine differences between
them - completely meaningless to a complete beginner.

And of course it doesn't describe IDLE - which is what the OP says
he has available.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



From kent37 at tds.net  Tue Nov 24 21:21:26 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 15:21:26 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
Message-ID: <1c2a2c590911241221w7ae44dadp4bec44d7f1cf5b64@mail.gmail.com>

On Tue, Nov 24, 2009 at 2:02 PM, Serdar Tumgoren <zstumgoren at gmail.com> wrote:
> Hi everyone,
> The list recently discussed the virtues of unit testing, and I was
> hoping someone could offer some high-level advice and further
> resources as I try to apply the TDD methodology.
>
> I'm trying to develop an application that regularly downloads some
> government data (in XML), parses the data and then updates a database.
> Simple enough in theory, but the problem I'm hitting is where to begin
> with tests on data that is ALL over the place.
>
> The agency in question performs little data validation, so a given
> field can have a huge range of possible inputs (e.g. - a Boolean field
> should be 0 or 1, but might be blank, have a negative number or even
> strings like the word 'None').
>
> In such a case, should I be writing test cases for *expected* inputs
> and then coding the the parser portion of my program to handle the
> myriad of possible "bad" data?

Yes. The parser needs to handle the bad data in some appropriate way,
unless you are filtering out the bad data before it reaches the
parser. The tests should cover the range of expected inputs, both good
and bad data. If you want to test the parser, you should write tests
that ensure that it behaves appropriately for the full range of
expected data. So your tests should include the full range of good
data and some sample bad data.

The book "Pragmatic Unit Testing" has a lot of guidelines about what
to test. The examples are in Java (or C#) but JUnit and Python's
unittest are pretty similar and the ideas certainly apply to any
language.

Kent

From lie.1296 at gmail.com  Tue Nov 24 21:20:07 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Wed, 25 Nov 2009 07:20:07 +1100
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
Message-ID: <hehf8i$tif$1@ger.gmane.org>

Serdar Tumgoren wrote:
> Hi everyone,
> The list recently discussed the virtues of unit testing, and I was
> hoping someone could offer some high-level advice and further
> resources as I try to apply the TDD methodology.

TDD is different from data validation. TDD ensures program correctness. 
Data validation ensures input correctness.

> In such a case, should I be writing test cases for *expected* inputs
> and then coding the the parser portion of my program to handle the
> myriad of possible "bad" data?

Yes, the parser should handle all bad data and respond in appropriate 
manner (raise an error or flag for manual check by programmer). Input 
should be sanitized as early as possible.

If you want to apply TDD here; you will be checking that the parser 
correctly normalize all "bad" data into the proper form (e.g. all 0, 
None, False, <empty></empty> in the boolean field is properly normalized 
to False (I assume there is no difference between each different 
representation of False?)).


From zstumgoren at gmail.com  Tue Nov 24 21:41:04 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Tue, 24 Nov 2009 15:41:04 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <hehf8i$tif$1@ger.gmane.org>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
Message-ID: <cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>

Lie and Kent,

Thanks for the quick replies.

I've started writing some "requirements", and combined with your
advice, am starting to feel a bit more confident on how to approach
this project.

Below is an excerpt of my requirements -- basically what I've learned
from reviewing the raw data using ElementTree at the command line.

Are these the types of "requirements" that are appropriate for the
problem at hand? Or am I not quite hitting the mark for the data
validation angle?

I figured once I write down these low-level rules about my input, I
can start coding up the test cases...Is that correct?

<< requirements snippet>>

Root node of every XML file is PublicFiling
Every PublicFiling node must contain at least one Filing node
Every Filing must contain 'Type' attribute
Every Filing must contain 'Year' attribute, etc.
Filing node must be either a Registration or activity Report
Filing is a Registration when 'Type' attribute equals 'Registration'
or 'Registration Amendment'
Registration must not have an 'Amount' attribute
Registration must not have an 'is_state_or_local_attrib'

<< end requirements>>

From davea at ieee.org  Tue Nov 24 22:48:08 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 24 Nov 2009 16:48:08 -0500
Subject: [Tutor] Class understanding
In-Reply-To: <SNT127-W48DB8B684EBC4BBC467DE0E09D0@phx.gbl>
References: <dfd7bbde0911240827p3ea78ad5x87af5b68c69c9d0@mail.gmail.com>
	<SNT127-W48DB8B684EBC4BBC467DE0E09D0@phx.gbl>
Message-ID: <4B0C5498.2080706@ieee.org>



Che M wrote:
>
>   
>> Date: Tue, 24 Nov 2009 10:27:05 -0600
>> From: jammer10000 at gmail.com
>> To: tutor at python.org
>> Subject: [Tutor] Class understanding
>>
>> Hi all... Have been attempting to understand classes... Been getting
>> along without them for a while now and feel it's time to jump in....
>>
>> What I want to do it start a log with the logging module... I have
>> this working without classes, but want to try... Here is a snippet of
>> the code that I am hacking on:
>>     
>
> I'm sure the better explainers will jump in presently, but let me try
> a few tips...
>
>   
>> class logger():
>>     
>
> The convention in Python is to make class names capitalized.  It is
> not necessary, but it is a good habit to get into, so class Logger().
>
>   
>>                         import logging
>>     
>
> Imports are traditionally done at the top of a Python file, not within
> a class. 
>
>   
>> logger()
>>     
>
> This calls the class but doesn't create a name for an instance of
> the class, so you won't be able to access it later.  Instead, try
> (assuming you rename logger() to Logger() ),
>
> logger_instance = Logger()
>
> Now you have a name for that instance of the class, and so
> can access the goodies inside the class.  
>
>   
>> logger.write2log(log_info)
>>     
>
> So that would now be:
>
> logger_instance.write2log(log_info)
>
>   
>> encouragement, or pointers to good docs would be helpful... I've done
>> a lot of searching via Google on classes, and it's all confusing to
>> me...
>>     
>
> Keep trying.  There have to be tons of good tutorials on classes.
> They fall under the heading of "Object Oriented Programming".   I tend
> to think of a class as a "container" that has all the stuff you will need
> to do a certain set of actions.  It can contain data (facts) and it can 
> contain methods (functions).  You can create one or more "instances"
> of any class (a traditional example being that Dog() is a class whereas
> fluffy is an instance of a dog, and therefore has all the traditional dog
> methods, like bark(), wag(), etc.)
>
> CM
>
>
>
>   
For my first class, I'd have picked something self-contained, and 
probably something dumb & simple, so as not to be confused between the 
stuff in the imports and the problems in understanding how class 
instances, methods, and attributes work.  Anyway, you probably 
understand the logging module better than I;  you certainly couldn't 
understand less.

Also, probably because you used tabs, the current code is heavily 
indented, and pretty hard to follow.  The def line is indented about 26 
columns, where I'd expect four.

CM has pointed out several important things. 

In addition, I need to point out that you need a "self" parameter on 
your method(s). 

And that if you use the same name for the argument as you used in the 
parameter, you can get confused as to who is doing what. 

Also, you want to derive new classes from object, for reasons that 
probably won't matter now, but when they do, it's easier if you've 
already got the habit. 

And finally I don't think you were planning to return an empty tuple.  
Probably you used syntax from other languages.  In Python, to return 
nothing, use one of three forms:  1) fall off the end of the 
function/method  2) return with no argument 3) return None


So your code would become:


import logging

class Logger:
    ... some initialization logic, which I don't know about...
    def write2log(self, log_msg):
        print "writing to log", log_msg
        ... some logging stuff...
        return

inst = Logger()
log_info = "This is first msg"
inst.write2log(log_info)

I'm not sure why this is a class, unless you want to be able to have 
multiple loggers (instances of Logger).  And in that case, you 
presumably would need another method, the Python constructor, which is 
called __init__()


DaveA

From timgoddardsemail at gmail.com  Tue Nov 24 22:53:02 2009
From: timgoddardsemail at gmail.com (Tim Goddard)
Date: Tue, 24 Nov 2009 15:53:02 -0600
Subject: [Tutor] Difficulty with csv files - line breaks
Message-ID: <ed76920d0911241353g6d38cc88l1ad70fbd9829d4be@mail.gmail.com>

What I'm trying to do is store a bunch of information into a .csv
file.  Each row will contain a date, webpage, etc of a job
application.

My difficulty is that it seems something I am doing is not recording
the line breaks.  I've read that \r\n are default in the csv module
but so far I can not seem to use them successfully..So every time I
enter a new list of data it just gets appended to the end of the last
row in the .csv file.

It should read:
"date", "company", "title", "site", "site url", "ref_ID"
"date", "company", "title", "site", "site url", "ref_ID"

but instead it does this:
"date", "company", "title", "site", "site url", "ref_ID""date",
"company", "title", "site", "site url", "ref_ID" and so forth....

Here's the segment of code responsible for my troubles.

import csv

                date = raw_input("Enter the date applied: ")
                company = raw_input("Enter the company applied to: ")
                job_title = raw_input("Enter the job title: ")
                site = raw_input("Enter the website used: ")
                site_url = raw_input("Paste the URL here: ")
                ref_ID = raw_input("Enter the reference ID: ")
                entry_list = [date, company, job_title, site, site_url, ref_ID]
                print "Are you sure you want to add\n",
                for entry in entry_list:
                    print entry
                print "to the file?"
                answer = yes_and_no()
                if answer == "y":
                    append_file(entry_list,filename)

def append_file(list,filename):
    text_file = open(filename, "a")
    writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC)
    writer.writerow(list)
    text_file.close()

From kent37 at tds.net  Tue Nov 24 23:03:27 2009
From: kent37 at tds.net (Kent Johnson)
Date: Tue, 24 Nov 2009 17:03:27 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
Message-ID: <1c2a2c590911241403p20769a2au73b6901ede127cdc@mail.gmail.com>

On Tue, Nov 24, 2009 at 3:41 PM, Serdar Tumgoren <zstumgoren at gmail.com> wrote:

> I've started writing some "requirements", and combined with your
> advice, am starting to feel a bit more confident on how to approach
> this project.
>
> Below is an excerpt of my requirements -- basically what I've learned
> from reviewing the raw data using ElementTree at the command line.
>
> Are these the types of "requirements" that are appropriate for the
> problem at hand? Or am I not quite hitting the mark for the data
> validation angle?

I'm not really sure where you are going with this? This looks like a
data specification, but you said the data is poorly specified and not
under your control. So is this a specification of a data validator?

> I figured once I write down these low-level rules about my input, I
> can start coding up the test cases...Is that correct?

Yes...but I'm not really clear what it is you want to test. What does
your code do? What if a Filing does not have a 'Type' attribute?

Kent
>
> << requirements snippet>>
>
> Root node of every XML file is PublicFiling
> Every PublicFiling node must contain at least one Filing node
> Every Filing must contain 'Type' attribute
> Every Filing must contain 'Year' attribute, etc.
> Filing node must be either a Registration or activity Report
> Filing is a Registration when 'Type' attribute equals 'Registration'
> or 'Registration Amendment'
> Registration must not have an 'Amount' attribute
> Registration must not have an 'is_state_or_local_attrib'
>
> << end requirements>>
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From davea at ieee.org  Tue Nov 24 23:15:24 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 24 Nov 2009 17:15:24 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
Message-ID: <4B0C5AFC.8050108@ieee.org>

Serdar Tumgoren wrote:
> Lie and Kent,
>
> Thanks for the quick replies.
>
> I've started writing some "requirements", and combined with your
> advice, am starting to feel a bit more confident on how to approach
> this project.
>
> Below is an excerpt of my requirements -- basically what I've learned
> from reviewing the raw data using ElementTree at the command line.
>
> Are these the types of "requirements" that are appropriate for the
> problem at hand? Or am I not quite hitting the mark for the data
> validation angle?
>
> I figured once I write down these low-level rules about my input, I
> can start coding up the test cases...Is that correct?
>
> << requirements snippet>>
>
> Root node of every XML file is PublicFiling
> Every PublicFiling node must contain at least one Filing node
> Every Filing must contain 'Type' attribute
> Every Filing must contain 'Year' attribute, etc.
> Filing node must be either a Registration or activity Report
> Filing is a Registration when 'Type' attribute equals 'Registration'
> or 'Registration Amendment'
> Registration must not have an 'Amount' attribute
> Registration must not have an 'is_state_or_local_attrib'
>
> << end requirements>>
>
>   
That's a good start.  You're missing one requirement that I think needs 
to be explicit.  Presumably you're requiring that the XML be 
well-formed.  This refers to things like matching <xxx>  and </xxx> 
nodes, and proper use of quotes and escaping within strings.  Most DOM 
parsers won't even give you a tree if the file isn't well-formed.

In addition, you want to state just how flexible each field is.  You 
mentioned booleans could be 0, 1, blank, ...  You might want ranges on 
numerics, or validation on specific fields such as Year, month, day, 
where if the month is 2, the day cannot be 30.

But most importantly, you can divide the rules where you say "if the 
data looks like XXXX" the file is rejected.   Versus "if the data looks 
like YYYY, we'll pretend it's actually ZZZZ, and keep going.  An example 
of that last might be what to do if somebody specifies March 35.  You 
might just pretend March 31, and keep going.

Don't forget that errors and warnings for the input data need to be 
output in a parseable form, at least if you expect more than one or two 
cases per run.

DaveA


From zstumgoren at gmail.com  Tue Nov 24 23:40:37 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Tue, 24 Nov 2009 17:40:37 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <1c2a2c590911241403p20769a2au73b6901ede127cdc@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
	<1c2a2c590911241403p20769a2au73b6901ede127cdc@mail.gmail.com>
Message-ID: <cadf44510911241440o619e9b63g8aa2bc1ece301b4a@mail.gmail.com>

> I'm not really sure where you are going with this? This looks like a
> data specification, but you said the data is poorly specified and not
> under your control. So is this a specification of a data validator?
>
The short answer -- yes, these are specs for a data validator.

And I should have been more specific about my problem domain. I'm
cobbling together a specification using various government manuals and
a *very* limited data definition.

For instance, the agency states that a lobbyist's status must be
either active (0), terminated (1) or administratively terminated (2)
or undetermined (3). So I know the expected inputs for that field.
However, the agency does not validate that data and it's possible for
that field to be blank or even contain gobbledygook strings such as a
'<Enter Lobbyist Status>' (residue from software built atop the
agency's automated filing service).

In other cases, based on working with the raw data, I've ascertained
that every Filing has at least a unique ID, and seems to have a Year,
etc. So it's a mish-mash of pre-defined "specs" (as best as I can
ascertain from the government), and patterns I'm discerning in the
data.

>
> Yes...but I'm not really clear what it is you want to test. What does
> your code do? What if a Filing does not have a 'Type' attribute?
>
At this stage, I'm just trying to perform some basic validation on the input.

'Type' is an attribute that I'd expect every filing to contain (and if
it does not, my code would have to log the record for human review).

From zstumgoren at gmail.com  Tue Nov 24 23:58:30 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Tue, 24 Nov 2009 17:58:30 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <4B0C5AFC.8050108@ieee.org>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
	<4B0C5AFC.8050108@ieee.org>
Message-ID: <cadf44510911241458t10165713o353228ac71b9d405@mail.gmail.com>

> That's a good start. ?You're missing one requirement that I think needs to
> be explicit. ?Presumably you're requiring that the XML be well-formed. ?This
> refers to things like matching <xxx> ?and </xxx> nodes, and proper use of
> quotes and escaping within strings. ?Most DOM parsers won't even give you a
> tree if the file isn't well-formed.

I actually hadn't been checking for well-formedness on the assumption
that ElementTree's parse method did that behind the scenes. Is that
not correct?

(I didn't see any specifics on that subject in the docs:
http://docs.python.org/library/xml.etree.elementtree.html)

> But most importantly, you can divide the rules where you say "if the data
> looks like XXXX" the file is rejected. ? Versus "if the data looks like
> YYYY, we'll pretend it's actually ZZZZ, and keep going. ?An example of that
> last might be what to do if somebody specifies March 35. ?You might just
> pretend March 31, and keep going.

Ok, so if I'm understanding -- I should convert invalid data to
sensible defaults where possible (like setting blank fields to 0);
otherwise if the data is clearly invalid and the default is
unknowable, I should flag the field for editing, deletion or some
other type of handling.

From asweigart at gmail.com  Wed Nov 25 00:01:38 2009
From: asweigart at gmail.com (Albert Sweigart)
Date: Tue, 24 Nov 2009 15:01:38 -0800
Subject: [Tutor] Difficulty with csv files - line breaks
Message-ID: <716dd5b60911241501y57db5c62r358b1a9859a3ae3a@mail.gmail.com>

Tim,

I've checked your code and it seems to work as far as using newlines
for the line terminator. The default line terminator is \r\n, which
might not show up correctly in some text editors.

Otherwise, try checking to see if you've specified a blank line for
the line terminator. You can set it explicitly when you create your
csv.writer:

writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC,
lineterminator='\r\n')


-Al
You should check out my free beginner's Python book here:
http://inventwithpython.com

From denis.spir at free.fr  Wed Nov 25 00:07:13 2009
From: denis.spir at free.fr (spir)
Date: Wed, 25 Nov 2009 00:07:13 +0100
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
Message-ID: <20091125000713.522c31fc@o>

Serdar Tumgoren <zstumgoren at gmail.com> wrote:

> Lie and Kent,
> 
> Thanks for the quick replies.
> 
> I've started writing some "requirements", and combined with your
> advice, am starting to feel a bit more confident on how to approach
> this project.
> 
> Below is an excerpt of my requirements -- basically what I've learned
> from reviewing the raw data using ElementTree at the command line.
> 
> Are these the types of "requirements" that are appropriate for the
> problem at hand? Or am I not quite hitting the mark for the data
> validation angle?
> 
> I figured once I write down these low-level rules about my input, I
> can start coding up the test cases...Is that correct?
> 
> << requirements snippet>>
> 
> Root node of every XML file is PublicFiling
> Every PublicFiling node must contain at least one Filing node
> Every Filing must contain 'Type' attribute
> Every Filing must contain 'Year' attribute, etc.
> Filing node must be either a Registration or activity Report
> Filing is a Registration when 'Type' attribute equals 'Registration'
> or 'Registration Amendment'
> Registration must not have an 'Amount' attribute
> Registration must not have an 'is_state_or_local_attrib'
> 
> << end requirements>>

This is a semantic schema (see wikipedia), meaning the specification of data structures describing something meaningfully.
It seems the major part of your program's task is checking correctness of parsed data (semantic validation).
Then the specification of your program should be the description of what it is supposed to do when processing valid and (most importantly) invalid data of all sorts. From this, you can directly write tests: in a sense, tests are a rewriting of a program's specification (*).

Denis

(*) The reason why, when a program is fully specified, one can write tests before starting coding. But afaik this works only for trivial apps, or inside a limited domain we know well. For we usually discover the app better as we develop it, which in turn changes its definition, often even dramatically.
________________________________

la vita e estrany

http://spir.wikidot.com/



From eike.welk at gmx.net  Tue Nov 24 23:44:27 2009
From: eike.welk at gmx.net (Eike Welk)
Date: Tue, 24 Nov 2009 23:44:27 +0100
Subject: [Tutor] Alternatives to get IP address of a computer : which
 one should I use ?
In-Reply-To: <d4ab53de0911240226q15112195r58540edd6f0e7653@mail.gmail.com>
References: <d4ab53de0911240226q15112195r58540edd6f0e7653@mail.gmail.com>
Message-ID: <200911242344.28271.eike.welk@gmx.net>

On Tuesday 24 November 2009, Shashwat Anand wrote:
On my openSuse 11.0 machine your method doesn't work as intended:


eike at lixie:~> python
Python 2.5.2 (r252:60911, Dec  1 2008, 18:10:01)
[GCC 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]] on 
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname(socket.gethostname())
'127.0.0.2'
>>>


It's a valid IP of my computer, but not the one you wanted. You could 
have written this one from memory (well I would have written 
127.0.0.1, which is also valid).  

Parsing the output from ifconfig would work for my computer. 


Kind regards,
Eike.

From chombee at lavabit.com  Wed Nov 25 00:47:15 2009
From: chombee at lavabit.com (chombee)
Date: Tue, 24 Nov 2009 23:47:15 +0000
Subject: [Tutor] How to get new messages from maildir?
Message-ID: <20091124234714.GB3563@compaq-nx7000-laptop>

I'm using the standard mailbox module to read a maildir, but it seems to 
be quite difficult to do some simple things. Is there any way to 
identify a message as new, unread, unseen or something similar? What 
about finding the most recent message?

My aim is to write a program that will print out the From: and Subject: 
headers of new (or unread, or unseen, whatever I can get) messages, in 
chronological order. Or failing that, just print out all messages in 
chronological order.

As far as I can tell there's no way to do the first, and to do the 
second you would have to use the date strings in the messages, 
converting them to datetimes with strptime first, although on my system 
there doesn't seem to be a valid strftime format for python that matches 
the date strings in my emails. They end like "+0000 (GMT)", which I 
believe is "%z (%Z)" in strftime, but python will not accept the %z in 
the strftime pattern.


From alan.gauld at btinternet.com  Wed Nov 25 02:24:36 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 25 Nov 2009 01:24:36 -0000
Subject: [Tutor] the art of testing
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
Message-ID: <hei118$i5g$1@ger.gmane.org>

"Serdar Tumgoren" <zstumgoren at gmail.com> wrote 

> Simple enough in theory, but the problem I'm hitting is where to begin
> with tests on data that is ALL over the place.

I've just spent the last 2 days in a workshop with >30 of our company's 
end to end test team. These guys are professional testers, all they do 
is test software systems. Mostly they work on large scale systems 
comprising many millions of lines of code on multiple OS and physical 
servers/networks. It was very interesting working with them and learning 
more about the techniques they use. Some of these involve very esoteric
(and expensive!) tools. However much of it is applicable to smaller 
systems.

Two key principles they apply:

1) Define the "System Under Test" (SUT) and treat it as a black box.
This involves your test boundary and working out what all the inputs 
are and all the outputs. Then you create a matrix mapping every set 
of inputs (the input vector) to every corresponding set ouf outputs
(The output vector). The set of functions which maps the input to 
output is known as the transfor function  matrix and if you can define 
it mathematically it becomes possible to automate a compete test 
cycle. Unfortunately its virtually never definable in real terms so we 
come to point 2...

2) Use risk based testing
This means look at what is most likely to break and focus effort on those 
areas. Common breakage areas are poor data quality and faulty 
interfaces. So test the inputs and outputs thoroughly.

> In such a case, should I be writing test cases for *expected* inputs
> and then coding the the parser portion of my program to handle the
> myriad of possible "bad" data?
> 
> Or given the range of possible inputs, should I simply skip testing
> for valid data at the parser level, and instead worry about flagging
> (or otherwise handling) invalid input at the database-insertion layer
> (and therefore write tests at that layer)?

The typical way of testing inputs with ranges is to test
just below the lower boundary, just above the boundary, the mid point, 
just below the upper boundary, just above the boundary known invalid 
values, "wildy implausible" values.

Thus for an input that can accept values between 1 and 100 you 
would test 0,1,50,100,101, -50 and 'five' say

Its not exhaustive but it covers a range of valid and invalid data points.
You could also test very large data values such as
12165231862471893479073407147235801787789578917897
Which will check for buffer and overflow type problems

But the point is you applyintelligence to determine the most likely 
forms of data error and test those values, not every possible value.

> Or should I not be testing data values at all, but rather the results
> of actions performed on that data?

Data errors are a high risk area therefore should always be tested.
Look to automate the testing if at all possible and write a program 
to generate the data sets used and ideally to generate the expected 
output data too - but thats hard since presumably you need the SUT 
to do that!

> It seems like these questions must be a subset of the issues in the
> realm of testing. Can anyone recommend a resource on the types of
> tests that should be applied to the various tasks and stages of the
> development process?
> 
> A friend recommended The Art of Software Testing -- is that the type
> of book that covers these issues? 

Yes and is one of the stabndard texts.
But most general software engineering texts cover testing issues.
For example try Somerville, Pressman, McConell etc.

> suitable alternative that costs less than $100?

Most of the mentioned authors have at least 1 chapter on testing.

HTH,

PS. It was also interesting to hear how testing has moved on from 
the days I spent as a tester at the beginning of my career in 
software engineering. In particular the challenges of Agile techniques 
for E2E testing and the move away from blanket testing to risk based 
testing, as well as the change in emphasis from "try to break it" - the 
dominant advice in my day - to "check it breaks cleanly under real loads"

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From boli1285 at gmail.com  Tue Nov 24 23:42:07 2009
From: boli1285 at gmail.com (Bo Li)
Date: Tue, 24 Nov 2009 16:42:07 -0600
Subject: [Tutor] Nested loop of I/O tasks
Message-ID: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>

Dear Python

I am new to Python and having questions about its usage. Currently I have to
read two .csv files INCT and INMRI which are similar to this

INCT
      NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1
1 1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35
1 1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0  Nz 121.57
34.71 14.81 1.35 1 1 1  Reye 91.04 57.59 6.98 1.35 0 1 0
INMRI
    NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1 1
1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35 1
1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0
My job is to match the name on the two files and combine the first three
attributes together. So far I tried to read two files. But when I tried to
match the pattern using nested loop, but Python stops me after 1 iteration.
Here is what I got so far.

INCT = open(' *.csv')
INMRI = open(' *.csv')

for row in INCT:
    name, x, y, z, a, b, c, d = row.split(",")
    print aaa,
    for row2 in INMRI:
        NAME, X, Y, Z, A, B, C, D = row2.split(",")
        if name == NAME:
            print aaa


The results are shown below

"NONAME" "NONAME" "Cella " "NONAME" "Chiasm" "NONAME" "FMagnum" "NONAME"
"Inion" "NONAME" "LEAM" "NONAME" "LTM" "NONAME" "Leye" "NONAME" "Nose"
"NONAME" "Nz" "NONAME" "REAM" "NONAME" "RTM" "NONAME" "Reye" "Cella"
"Chiasm" "FMagnum" "Iz" "LEAM" "LEAM" "LPAF" "LTM" "Leye" "Nz" "Reye"


I was a MATLAB user and am really confused by what happens with me. I wish
someone could help me with this intro problem and probably indicate a
convenient way for pattern matching. Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/143f9773/attachment.htm>

From davea at ieee.org  Wed Nov 25 04:41:32 2009
From: davea at ieee.org (Dave Angel)
Date: Tue, 24 Nov 2009 22:41:32 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241458t10165713o353228ac71b9d405@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>	<hehf8i$tif$1@ger.gmane.org>	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>	<4B0C5AFC.8050108@ieee.org>
	<cadf44510911241458t10165713o353228ac71b9d405@mail.gmail.com>
Message-ID: <4B0CA76C.2080900@ieee.org>



Serdar Tumgoren wrote:
>> That's a good start.  You're missing one requirement that I think needs to
>> be explicit.  Presumably you're requiring that the XML be well-formed.  This
>> refers to things like matching <xxx>  and </xxx> nodes, and proper use of
>> quotes and escaping within strings.  Most DOM parsers won't even give you a
>> tree if the file isn't well-formed.
>>     
>
> I actually hadn't been checking for well-formedness on the assumption
> that ElementTree's parse method did that behind the scenes. Is that
> not correct?
>
> (I didn't see any specifics on that subject in the docs:
> http://docs.python.org/library/xml.etree.elementtree.html)
>
>   
I also would assume that ElementTree would do the check.  But the point 
is:  it's part of the spec, and needs to be explicitly handled in your 
list of errors:
     file xxxxyyy.xml  was rejected because .....

I am not saying you need to separately test for it in your validator, 
but effectively it's the second test you'll be doing.  (The first is:  
the file exists and is readable)
>> But most importantly, you can divide the rules where you say "if the data
>> looks like XXXX" the file is rejected.   Versus "if the data looks like
>> YYYY, we'll pretend it's actually ZZZZ, and keep going.  An example of that
>> last might be what to do if somebody specifies March 35.  You might just
>> pretend March 31, and keep going.
>>     
>
> Ok, so if I'm understanding -- I should convert invalid data to
> sensible defaults where possible (like setting blank fields to 0);
> otherwise if the data is clearly invalid and the default is
> unknowable, I should flag the field for editing, deletion or some
> other type of handling.
>
>   

Exactly.  As you said in one of your other messages, human intervention 
required.  Then the humans may decide to modify the spec to reduce the 
number of cases needing human intervention.  So I see the spec and the 
validator as a matched pair that will evolve.

Note that none of this says anything about testing your code.  You'll 
need a controlled suite of test data to help with that.  The word "test" 
is heavily overloaded (and heavily underdone) in our industry.

DaveA


From reply_to_is_valid at nowhere.com.invalid  Wed Nov 25 04:41:29 2009
From: reply_to_is_valid at nowhere.com.invalid (Nick)
Date: Wed, 25 Nov 2009 03:41:29 +0000 (UTC)
Subject: [Tutor] the art of testing
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
Message-ID: <hei918$5sj$2@ger.gmane.org>

As well as the other replies, consider that you are doing "unit" testing:
http://en.wikipedia.org/wiki/Unit_test

One method is black-box testing, which is where the thing (class, 
function, module) you are testing is treated as a black box, something 
that takes input and returns output, and how it does it are irrelevant 
from the tester's perspective.  You remove the implementation of the thing 
from the test of the thing, which allows you to focus on the tests.  In 
fact, you can write the tests before you code up the black box, which is a 
valid development technique that has many proponents.

You throw all sorts of data at the black box, particularly invalid, 
boundary and corner-case data, and test that the black box returns 
consistent, valid output in all cases, and that it never crashes or 
triggers an exception (unless part of the design).

Once you have run your tests and have produced a set of test failures, you 
have some leads on what within the black box is broken, and you take off 
your tester's hat, put on your developer hat, and go fix them.  Repeat.  A 
pre-built test suite makes this easier and gives consistency.  You know 
that your input data is the same as it was before your changes and you can 
test for consistency of output over your development cycle so that you 
know you haven't inadvertently introduced new bugs.  This process is 
regression testing.  

Clearly, ensuring your test data covers all possible cases is important.  
This is one reason for designing the test suite before building the black 
box.  You won't have inadvertently tainted your mind-set with preconceived 
notions on what the data contains, since you haven't examined it yet.  
(You've only examined the specs to find out what it *should* contain; your 
black box implementation will handle everything else gracefully, returning 
output when it should and triggering an exception when it should.)  This 
frees you up to create all sorts of invalid, i.e. non-specification, and 
boundary test data, without preconceived ideas.

Once you are passing your test data, throw lots of samples of real-life 
data at it.  If your test data was comprehensive, real-life data should be 
a piece of cake.

Obviously I'm a fan of unit-testing. Sure, the first time they're a bit of 
work to build up, but you'll find you can re-use them over and over with a 
small amount of editing.  Many conditions are the same for any program, 
such as (off the top of my head) file-not-found, file-has-one-record-only, 
file-has-a-trillion-records, string-where-int-expected, int-out-of-
expected-range, and so on.


From cwitts at compuscan.co.za  Wed Nov 25 07:38:09 2009
From: cwitts at compuscan.co.za (Christian Witts)
Date: Wed, 25 Nov 2009 08:38:09 +0200
Subject: [Tutor] Nested loop of I/O tasks
In-Reply-To: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
References: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
Message-ID: <4B0CD0D1.2000307@compuscan.co.za>

Bo Li wrote:
> Dear Python
>
> I am new to Python and having questions about its usage. Currently I 
> have to read two .csv files INCT and INMRI which are similar to this
>
> INCT
> NONAME 	121.57 	34.71 	14.81 	1.35 	0 	0 	1
> Cella 	129.25 	100.31 	27.25 	1.35 	1 	1 	1
> Chiasm 	130.3 	98.49 	26.05 	1.35 	1 	1 	1
> FMagnum 	114.89 	144.94 	-15.74 	1.35 	1 	1 	1
> Iz 	121.57 	198.52 	30.76 	1.35 	1 	1 	1
> LEAM 	160.53 	127.6 	-1.14 	1.35 	1 	1 	1
> LEAM 	55.2 	124.66 	12.32 	1.35 	1 	1 	1
> LPAF 	180.67 	128.26 	-9.05 	1.35 	1 	1 	1
> LTM 	77.44 	124.17 	15.95 	1.35 	1 	1 	1
> Leye 	146.77 	59.17 	-2.63 	1.35 	1 	0 	0
> Nz 	121.57 	34.71 	14.81 	1.35 	1 	1 	1
> Reye 	91.04 	57.59 	6.98 	1.35 	0 	1 	0
>
>
> INMRI
> NONAME 	121.57 	34.71 	14.81 	1.35 	0 	0 	1
> Cella 	129.25 	100.31 	27.25 	1.35 	1 	1 	1
> Chiasm 	130.3 	98.49 	26.05 	1.35 	1 	1 	1
> FMagnum 	114.89 	144.94 	-15.74 	1.35 	1 	1 	1
> Iz 	121.57 	198.52 	30.76 	1.35 	1 	1 	1
> LEAM 	160.53 	127.6 	-1.14 	1.35 	1 	1 	1
> LEAM 	55.2 	124.66 	12.32 	1.35 	1 	1 	1
> LPAF 	180.67 	128.26 	-9.05 	1.35 	1 	1 	1
> LTM 	77.44 	124.17 	15.95 	1.35 	1 	1 	1
> Leye 	146.77 	59.17 	-2.63 	1.35 	1 	0 	0
>
>
> My job is to match the name on the two files and combine the first 
> three attributes together. So far I tried to read two files. But when 
> I tried to match the pattern using nested loop, but Python stops me 
> after 1 iteration. Here is what I got so far.
>
> INCT = open(' *.csv')
> INMRI = open(' *.csv')
>
> for row in INCT:
>     name, x, y, z, a, b, c, d = row.split(",")
>     print aaa,
>     for row2 in INMRI:
>         NAME, X, Y, Z, A, B, C, D = row2.split(",")
>         if name == NAME:
>             print aaa
>
>
> The results are shown below
>
> "NONAME" "NONAME" "Cella " "NONAME" "Chiasm" "NONAME" "FMagnum" 
> "NONAME" "Inion" "NONAME" "LEAM" "NONAME" "LTM" "NONAME" "Leye" 
> "NONAME" "Nose" "NONAME" "Nz" "NONAME" "REAM" "NONAME" "RTM" "NONAME" 
> "Reye" "Cella" "Chiasm" "FMagnum" "Iz" "LEAM" "LEAM" "LPAF" "LTM" 
> "Leye" "Nz" "Reye"
>
>
> I was a MATLAB user and am really confused by what happens with me. I 
> wish someone could help me with this intro problem and probably 
> indicate a convenient way for pattern matching. Thanks!
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
What's happening is you are iterating over the first file and on the 
first line on that file you start iterating over the second file.  Once 
the second file has been completely looped through it is 'empty' so your 
further iterations over file 1 can't loop through file 2.

If your output is going to be sorted like that so you know NONAME will 
be on the same line in both files what you can do is

INCT = open('something.csv', 'r')
INMRI = open('something_else.csv', 'r')

rec_INCT = INCT.readline()
rec_INMRI = INMRI.readline()

while rec_INCT and rec_INMRI:
    name, x, y, z, a, b, c, d = rec_INCT.split(',')
    NAME, X, Y, Z, A, B, C, D = rec.INMRI.split(',')

    if name == NAME:
        print 'Matches'

    rec_INCT = INCT.readline()
    rec_INMRI = INMRI.readline()

INCT.close()
INMRI.close()

What will happen is that you open the files, read the first line of each 
and then start with the while loop.  It will only run the while as long 
as both the INCT and INMRI files have more lines to read, if one of them 
runs out then it will exit the loop.  It then does the splitting, checks 
to see if it matches at which point you can do your further processing 
and after that read another line of each file.

Of course if the files are not sorted then you would have to process it 
a little differently.  If the file sizes are small you can use one of 
the files to build a dictionary, key being the `name` and value being 
the rest of your data, and then iterate over the second file checking to 
see if the name is in dictionary.  It would also work for this scenario 
of perfect data as well.

Hope that helps.  

-- 
Kind Regards,
Christian Witts



From wescpy at gmail.com  Wed Nov 25 08:04:56 2009
From: wescpy at gmail.com (wesley chun)
Date: Tue, 24 Nov 2009 23:04:56 -0800
Subject: [Tutor] Nested loop of I/O tasks
In-Reply-To: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
References: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
Message-ID: <78b3a9580911242304r17255830ob18eba359f70eac9@mail.gmail.com>

On Tue, Nov 24, 2009 at 2:42 PM, Bo Li <boli1285 at gmail.com> wrote:
>
> I am new to Python and having questions about its usage. Currently I have to read two .csv files INCT and INMRI which are similar to this:
> [...]
> I was a MATLAB user and am really confused by what happens with me. I wish someone could help me with this intro problem and probably indicate a convenient way for pattern matching. Thanks!


greetings and welcome to Python!

the problem you are experiencing is due to the fact that you do not
read in and cache your data first. you are iterating over the data in
both files once, which is what enables your first pass to work.

however, on the second pass, INMRI does not return any more data
because you have already exhausted all lines of the file on the first
pass. if you intend on reiterating over the file, then you must read
in all of the data first and just use that data structure rather than
the actual file as you have.

hope this helps!
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
? ?http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com

From davea at ieee.org  Wed Nov 25 08:25:26 2009
From: davea at ieee.org (Dave Angel)
Date: Wed, 25 Nov 2009 02:25:26 -0500
Subject: [Tutor] Nested loop of I/O tasks
In-Reply-To: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
References: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
Message-ID: <4B0CDBE6.90302@ieee.org>



Bo Li wrote:
> Dear Python
>
> I am new to Python and having questions about its usage. Currently I have to
> read two .csv files INCT and INMRI which are similar to this
>
> INCT
>       NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1
> 1 1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35
> 1 1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
> LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
> 77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0  Nz 121.57
> 34.71 14.81 1.35 1 1 1  Reye 91.04 57.59 6.98 1.35 0 1 0
> INMRI
>     NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1 1
> 1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35 1
> 1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
> LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
> 77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0
> My job is to match the name on the two files and combine the first three
> attributes together. So far I tried to read two files. But when I tried to
> match the pattern using nested loop, but Python stops me after 1 iteration.
> Here is what I got so far.
>
> INCT = open(' *.csv')
> INMRI = open(' *.csv')
>
> for row in INCT:
>     name, x, y, z, a, b, c, d = row.split(",")
>     print aaa,
>     for row2 in INMRI:
>         NAME, X, Y, Z, A, B, C, D = row2.split(",")
>         if name == NAME:
>             print aaa
>
>
> The results are shown below
>
> "NONAME" "NONAME" "Cella " "NONAME" "Chiasm" "NONAME" "FMagnum" "NONAME"
> "Inion" "NONAME" "LEAM" "NONAME" "LTM" "NONAME" "Leye" "NONAME" "Nose"
> "NONAME" "Nz" "NONAME" "REAM" "NONAME" "RTM" "NONAME" "Reye" "Cella"
> "Chiasm" "FMagnum" "Iz" "LEAM" "LEAM" "LPAF" "LTM" "Leye" "Nz" "Reye"
>
>
> I was a MATLAB user and am really confused by what happens with me. I wish
> someone could help me with this intro problem and probably indicate a
> convenient way for pattern matching. Thanks!
>
>   
I'm wondering how Christian's quote of your message was formatted so 
much better.  Your csv contents are word-wrapped when I see your email.  
Did you perhaps send it using html mail, instead of text?

The other thing I note (and this is the same with Christian's version of 
your message), is that the code you show wouldn't run, and also wouldn't 
produce the output you supplied, so you must have retyped it instead of 
copy/pasting it.  That makes the job harder, for anybody trying to help.

Christian's analysis of your problem was spot-on.  Files can only be 
iterated once, and thus the inner loop will fail the second time through 
the outer loop.  However, there are two possible fixes that are both 
closer to what you have, and therefore perhaps more desirable.

Simplest change is to do a readlines() on the second file.  This means 
you have to have enough memory for the whole file, stored as a list.

INCT = open('file1.csv')
INMRIlist = open('file2.csv').readlines()

for row in INCT:
    name, x, y, z, a, b, c, d = row.split(",")
    print name,
    for row2 in INMRIlist:
        NAME, X, Y, Z, A, B, C, D = row2.split(",")
        print NAME,
        if name == NAME:
            print "---matched---"



The other choice, somewhat slower, but saving of memory, is


INCT = open('file1.csv')
#INMRI = open('file2.csv')

for row in INCT:
    name, x, y, z, a, b, c, d = row.split(",")
    print name,
    for row2 in open('file2.csv'):
        NAME, X, Y, Z, A, B, C, D = row2.split(",")
        print NAME,
        if name == NAME:
            print "---matched---"

There are many other things I would change (probably eventually going to 
the dictionary that Christian mentioned), but these are the minimum 
changes to let you continue down the path you've envisioned.


(all code untested, I just typed it directly into the email, assuming 
Python2.6)


DaveA


From alan.plum at uni-koeln.de  Wed Nov 25 09:02:29 2009
From: alan.plum at uni-koeln.de (Alan Plum)
Date: Wed, 25 Nov 2009 09:02:29 +0100
Subject: [Tutor] How to get new messages from maildir?
In-Reply-To: <20091124234714.GB3563@compaq-nx7000-laptop>
References: <20091124234714.GB3563@compaq-nx7000-laptop>
Message-ID: <1259136149.3043.12.camel@kallisti>

Ahoy!

On Di, 2009-11-24 at 23:47 +0000, chombee wrote:
> I'm using the standard mailbox module to read a maildir, but it seems to 
> be quite difficult to do some simple things. Is there any way to 
> identify a message as new, unread, unseen or something similar? What 
> about finding the most recent message?

AFAIR, the new (i.e. unread or unseen -- not sure which) messages should
be in Maildir/new rather than Maildir/cur. Flags are a bit trickier,
because they're simply appended to the file name, so you'd have to check
each name in the directory to figure that out. Don't get me started on
how subfolders are implemented in Maildir.

> My aim is to write a program that will print out the From: and Subject: 
> headers of new (or unread, or unseen, whatever I can get) messages, in 
> chronological order. Or failing that, just print out all messages in 
> chronological order.

The easiest(!) way would be to iterate over the folder, read the headers
(i.e. everything up to \r\n\r\n) of each message and parse them. MUAs
(Thunderbird, Evolution, Outlook etc) often don't rely on maildirs to
store that kind of meta data, they actually parse the maildir once and
then store the parsed data locally in a more usable format (i.e. some
kind of flat file database).

> As far as I can tell there's no way to do the first, and to do the 
> second you would have to use the date strings in the messages, 
> converting them to datetimes with strptime first, although on my system 
> there doesn't seem to be a valid strftime format for python that matches 
> the date strings in my emails. They end like "+0000 (GMT)", which I 
> believe is "%z (%Z)" in strftime, but python will not accept the %z in 
> the strftime pattern.

Dare I say "regular expressions"? Normally, %z should work in strftime,
though. Maybe the problem is the " (%Z)" bit -- Python might be using
different names than the MDA.


Cheers,

Alan


From denis.spir at free.fr  Wed Nov 25 10:22:59 2009
From: denis.spir at free.fr (spir)
Date: Wed, 25 Nov 2009 10:22:59 +0100
Subject: [Tutor] file questions
Message-ID: <20091125102259.5ac7a671@o>

Hello,

1) encoding guess
From the library manual:
file.encoding
    The encoding that this file uses. When Unicode strings are written to a file, they will be converted to byte strings using this encoding. In addition, when the file is connected to a terminal, the attribute gives the encoding that the terminal is likely to use (that information might be incorrect if the user has misconfigured the terminal). The attribute is read-only and may not be present on all file-like objects. It may also be None, in which case the file uses the system default encoding for converting Unicode strings.
How can python do that?

2) time stamp
How to read it (found no attr in "file" type)? (If possible in platform independant manner, and as datetime object.)

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/



From alan.gauld at btinternet.com  Wed Nov 25 11:04:59 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 25 Nov 2009 10:04:59 -0000
Subject: [Tutor] file questions
References: <20091125102259.5ac7a671@o>
Message-ID: <heivh0$mlj$1@ger.gmane.org>


"spir" <denis.spir at free.fr> wrote

> 1) encoding guess
> How can python do that?

I'll let someone else try that one :-)

> 2) time stamp
> How to read it (found no attr in "file" type)? (If possible in platform 
> independant manner, and as datetime object.)

Which timestamp? There are several.

To read them use the os.path functions getctime() and getmtime() etc.
You can also use the os.stat function to return a tuple of file 
characteristics
including creation, modification and last open times.

See my tutorial topic on Using the OS to see some examples.


HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From kent37 at tds.net  Wed Nov 25 12:25:22 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 25 Nov 2009 06:25:22 -0500
Subject: [Tutor] Nested loop of I/O tasks
In-Reply-To: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
References: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
Message-ID: <1c2a2c590911250325k7e3829afp7a76af523419154a@mail.gmail.com>

On Tue, Nov 24, 2009 at 5:42 PM, Bo Li <boli1285 at gmail.com> wrote:

> Dear Python
>
> I am new to Python and having questions about its usage. Currently I have
> to read two .csv files INCT and INMRI which are similar to this
> ...
>


> My job is to match the name on the two files and combine the first three
> attributes together. So far I tried to read two files. But when I tried to
> match the pattern using nested loop, but Python stops me after 1 iteration.


That is because you can only iterate an open file once. You iterate INMRI in
a loop; only the first iteration will give any rows.

A simple fix is to read INMRI into a list using readlines(). Lists can be
iterated repeatedly.


> Here is what I got so far.
>
> INCT = open(' *.csv')
> INMRI = open(' *.csv')
>
INMRI = open(' *.csv').readlines()


>
> for row in INCT:
>     name, x, y, z, a, b, c, d = row.split(",")
>     print aaa,
>

What is aaa?


>     for row2 in INMRI:
>         NAME, X, Y, Z, A, B, C, D = row2.split(",")
>         if name == NAME:
>             print aaa
>

For short lists this is adequate. If both lists are long the nested loop
will be slow because it has to look through all of INMRI for each line of
INCT. In this case a better solution is to put the data from INMRI in a
dictionary keyed by NAME; then you can easily test for membership.

Kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091125/938d2eca/attachment.htm>

From kent37 at tds.net  Wed Nov 25 12:33:21 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 25 Nov 2009 06:33:21 -0500
Subject: [Tutor] Nested loop of I/O tasks
In-Reply-To: <4B0CD0D1.2000307@compuscan.co.za>
References: <4a7eba590911241442jad00e76t6ef0e010de0ca67f@mail.gmail.com>
	<4B0CD0D1.2000307@compuscan.co.za>
Message-ID: <1c2a2c590911250333r17434424y24d31bb7dc522e54@mail.gmail.com>

On Wed, Nov 25, 2009 at 1:38 AM, Christian Witts <cwitts at compuscan.co.za> wrote:

> If your output is going to be sorted like that so you know NONAME will be on
> the same line in both files what you can do is
>
> INCT = open('something.csv', 'r')
> INMRI = open('something_else.csv', 'r')
>
> rec_INCT = INCT.readline()
> rec_INMRI = INMRI.readline()
>
> while rec_INCT and rec_INMRI:
> ? name, x, y, z, a, b, c, d = rec_INCT.split(',')
> ? NAME, X, Y, Z, A, B, C, D = rec.INMRI.split(',')
>
> ? if name == NAME:
> ? ? ? print 'Matches'
>
> ? rec_INCT = INCT.readline()
> ? rec_INMRI = INMRI.readline()
>
> INCT.close()
> INMRI.close()

For this to work the files must have the same names in the same order.
It's not enough that they be sorted, they have to have the same
entries.

Kent

From kent37 at tds.net  Wed Nov 25 12:35:42 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 25 Nov 2009 06:35:42 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
Message-ID: <1c2a2c590911250335s11c038f3tdca5a79914daa7a6@mail.gmail.com>

On Tue, Nov 24, 2009 at 3:41 PM, Serdar Tumgoren <zstumgoren at gmail.com> wrote:

> << requirements snippet>>
>
> Root node of every XML file is PublicFiling
> Every PublicFiling node must contain at least one Filing node
> Every Filing must contain 'Type' attribute
> Every Filing must contain 'Year' attribute, etc.
> Filing node must be either a Registration or activity Report
> Filing is a Registration when 'Type' attribute equals 'Registration'
> or 'Registration Amendment'
> Registration must not have an 'Amount' attribute
> Registration must not have an 'is_state_or_local_attrib'
>
> << end requirements>>

Many of these requirements could be written as a DTD or schema for the
input. If you had a DTD or schema, a validating XML parser would check
the requirements for you. If you just want to reject non-conforming
documents this may be the simplest approach.

Kent

From fomcl at yahoo.com  Wed Nov 25 14:44:24 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Wed, 25 Nov 2009 05:44:24 -0800 (PST)
Subject: [Tutor] UnicodeEncodeError
Message-ID: <333759.33332.qm@web110716.mail.gq1.yahoo.com>

Hi,
?
I'm parsing an xml file using elementtree, but it seems to get stuck on certain non-ascii characters (for example: "?"). I'm using Python 2.4. Here's the relevant code fragment:
?
# CODE:
for element in doc.getiterator():
? try:
????m = re.match(search_text, str(element.text))
? except UnicodeEncodeError:
??? raise # I want to get rid of this exception.

# PRINTBACK:
????m = re.match(search_text, str(element.text))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 4: ordinal not in range(128)
?
How can I get rid of this unicode encode error. I tried:
s = str(element.text)
s.encode("utf-8")
(and then feeding it into the regex)
?
The xml file is in UTF-8. Somehow I need to tell the program not to use ascii but utf-8, right?
?
Thanks in advance!

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091125/32965c58/attachment.htm>

From denis.spir at free.fr  Wed Nov 25 15:12:55 2009
From: denis.spir at free.fr (spir)
Date: Wed, 25 Nov 2009 15:12:55 +0100
Subject: [Tutor] UnicodeEncodeError
In-Reply-To: <333759.33332.qm@web110716.mail.gq1.yahoo.com>
References: <333759.33332.qm@web110716.mail.gq1.yahoo.com>
Message-ID: <20091125151255.0b0c25d8@o>

Albert-Jan Roskam <fomcl at yahoo.com> wrote:

> # CODE:
> for element in doc.getiterator():
> ? try:
> ????m = re.match(search_text, str(element.text))
> ? except UnicodeEncodeError:
> ??? raise # I want to get rid of this exception.


First, you should separate both actions done in a single statement to isolate the source of error:
for element in doc.getiterator():
? try:
????source = str(element.text)
? except UnicodeEncodeError:
??? raise # I want to get rid of this exception.
  else:
????m = re.match(search_text, source)

I guess
   source = unicode(element;text, "utf8")
should do the job if, actually, you know elements are utf8 encoded (else try latin1, or better get proper information on origin of you doc files).

PS: I just discovered python's builtin attribute file.encoding that should give you the proper encoding to pass to unicode(..., encoding).
PPS: You should in fact decode the whole source before parsing it, no? (meaning parsing a unicode object, not encoded text)

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/



From zstumgoren at gmail.com  Wed Nov 25 16:23:25 2009
From: zstumgoren at gmail.com (Serdar Tumgoren)
Date: Wed, 25 Nov 2009 10:23:25 -0500
Subject: [Tutor] the art of testing
In-Reply-To: <1c2a2c590911250335s11c038f3tdca5a79914daa7a6@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
	<1c2a2c590911250335s11c038f3tdca5a79914daa7a6@mail.gmail.com>
Message-ID: <cadf44510911250723t417029caqdbc7468da49058a5@mail.gmail.com>

Thanks to everyone for the detailed and varied responses. They really
help get me situated in this unfamiliar world of testing.

And the more I hear from you folks, the more I start thinking that my
confusion arose because I don't have a formal set of requirements and
therefore have to wear multiple hats: requirements writer, coder and
tester. It's clear that I also have plenty to learn about the *types*
of tests one should conduct on a given project, and how to determine
what those tests should be based on risk assessments of a specific
project.

So it seems I have quite a bit of work to do before I return to unit
testing. I'm planning to gather more details about my raw input data,
and then resume writing requirements. And then, when I have a clearer
sense of the project needs, will return to the unit tests and program
code.

Hopefully that's a sensible plan.  Please correct me if I've
misunderstood the lessons you've tried to relate!

Meantime, many thanks again for the excellent guidance!

Regards,
Serdar

From timgoddardsemail at gmail.com  Wed Nov 25 16:31:39 2009
From: timgoddardsemail at gmail.com (Tim Goddard)
Date: Wed, 25 Nov 2009 09:31:39 -0600
Subject: [Tutor] Difficulty with csv files - line breaks
Message-ID: <ed76920d0911250731x212bf9aft37597097fff0c9e4@mail.gmail.com>

> Date: Tue, 24 Nov 2009 15:01:38 -0800
> From: Albert Sweigart <asweigart at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Difficulty with csv files - line breaks
> Message-ID:
> ? ? ? ?<716dd5b60911241501y57db5c62r358b1a9859a3ae3a at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Tim,
>
> I've checked your code and it seems to work as far as using newlines
> for the line terminator. The default line terminator is \r\n, which
> might not show up correctly in some text editors.
>
> Otherwise, try checking to see if you've specified a blank line for
> the line terminator. You can set it explicitly when you create your
> csv.writer:
>
> writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC,
> lineterminator='\r\n')
>
>
> -Al
> You should check out my free beginner's Python book here:
> http://inventwithpython.com
>
>
> ------------------------------

Al, That helped out a lot.  I had worked around this problem by
writing a "\n" after writing the csv information.

text_file.write("\n")

Setting the lineterminator explicitly fixes it and cleans up my code.

From denis.spir at free.fr  Wed Nov 25 17:15:39 2009
From: denis.spir at free.fr (spir)
Date: Wed, 25 Nov 2009 17:15:39 +0100
Subject: [Tutor] the art of testing
In-Reply-To: <cadf44510911250723t417029caqdbc7468da49058a5@mail.gmail.com>
References: <cadf44510911241102w54ad1c50lfa435c87a5dbbc5f@mail.gmail.com>
	<hehf8i$tif$1@ger.gmane.org>
	<cadf44510911241241j684f59eeieda71df7002c608f@mail.gmail.com>
	<1c2a2c590911250335s11c038f3tdca5a79914daa7a6@mail.gmail.com>
	<cadf44510911250723t417029caqdbc7468da49058a5@mail.gmail.com>
Message-ID: <20091125171539.38734d39@o>

Serdar Tumgoren <zstumgoren at gmail.com> wrote:

> And the more I hear from you folks, the more I start thinking that my
> confusion arose because I don't have a formal set of requirements and
> therefore have to wear multiple hats

I think a base confusion was between your app's own job (validation), on one hand, and testing your app itself, on the other hand: both are kinds of checking. From my point of view, your words seemed to show you were confusing both levels. I may be wrong, indeed.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/



From denis.spir at free.fr  Wed Nov 25 17:11:05 2009
From: denis.spir at free.fr (spir)
Date: Wed, 25 Nov 2009 17:11:05 +0100
Subject: [Tutor] python time
Message-ID: <20091125171105.480f4963@o>

Hello,

How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?)

Thank you,
Denis
________________________________

la vita e estrany

http://spir.wikidot.com/



From kent37 at tds.net  Wed Nov 25 17:55:32 2009
From: kent37 at tds.net (Kent Johnson)
Date: Wed, 25 Nov 2009 11:55:32 -0500
Subject: [Tutor] UnicodeEncodeError
In-Reply-To: <333759.33332.qm@web110716.mail.gq1.yahoo.com>
References: <333759.33332.qm@web110716.mail.gq1.yahoo.com>
Message-ID: <1c2a2c590911250855x52533b3dpecc16cb1e5423e25@mail.gmail.com>

On Wed, Nov 25, 2009 at 8:44 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:

> Hi,
>
> I'm parsing an xml file using elementtree, but it seems to get stuck on
> certain non-ascii characters (for example: "?"). I'm using Python 2.4.
> Here's the relevant code fragment:
>
> # CODE:
> for element in doc.getiterator():
>   try:
>     m = re.match(search_text, str(element.text))
>   except UnicodeEncodeError:
>     raise # I want to get rid of this exception.
> # PRINTBACK:
>     m = re.match(search_text, str(element.text))
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in
> position 4: ordinal not in range(128)
>

You can't convert element.text to a str because it contains non-ascii
characters. Why are you converting it? re.match() will accept a unicode
string as its argument.

>
> How can I get rid of this unicode encode error. I tried:
> s = str(element.text)
> s.encode("utf-8")
> (and then feeding it into the regex)
>

This fails because it is the str() that won't work. To get UTF-8 use
  s = element.text.encode('utf-8')
 but I don't think this is the correct solution.


> The xml file is in UTF-8. Somehow I need to tell the program not to use
> ascii but utf-8, right?
>
> No, just pass Unicode to re.match().

Kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091125/0cd4e1a8/attachment.htm>

From luhmann_br at yahoo.com  Wed Nov 25 20:15:30 2009
From: luhmann_br at yahoo.com (Luhmann)
Date: Wed, 25 Nov 2009 11:15:30 -0800 (PST)
Subject: [Tutor] Is there any way to move the print cursor in IDLE?
Message-ID: <804754.71743.qm@web30905.mail.mud.yahoo.com>

I wanted to display a counter for my loops like this:
>>> for a in range(int(1e+307)):... 	a=str(a)... 	b='\b'*len(a)... 	print(b,a, sep='',end='')
But this only works on the command prompt, in IDLE it will flood the screen. How do I do it?



      D?couvrez les photos les plus int?ressantes du jour.
http://www.flickr.com/explore/interesting/7days/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091125/95f5b4cf/attachment.htm>

From alan.gauld at btinternet.com  Wed Nov 25 20:40:52 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 25 Nov 2009 19:40:52 -0000
Subject: [Tutor] python time
References: <20091125171105.480f4963@o>
Message-ID: <hek18q$bsq$1@ger.gmane.org>


"spir" <denis.spir at free.fr> wrote 

> How does python get the time in microseconds? 

The underlying OS API generally provides that.

> (In other words, how would I get it if python (like some other 
> languages) would provide time in whole seconds only?)

You would have to call the OS routines directly from Python 
yourself.

Alan G


From alan.gauld at btinternet.com  Wed Nov 25 20:44:03 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Wed, 25 Nov 2009 19:44:03 -0000
Subject: [Tutor] Is there any way to move the print cursor in IDLE?
References: <804754.71743.qm@web30905.mail.mud.yahoo.com>
Message-ID: <hek1ep$cgh$1@ger.gmane.org>


"Luhmann" <luhmann_br at yahoo.com> wrote

>>> for a in range(int(1e+307)):... a=str(a)... b='\b'*len(a)... print(b,a, 
>>> sep='',end='')
> But this only works on the command prompt, in IDLE it will flood the 
> screen. How do I do it?


I don't think you can since IDLEs output window does not obey the
terminal control codes of the console.

You would need to create a GUI display window and place the
output there. EasyGUI may provide a suitable mechanism for doing it.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



From lie.1296 at gmail.com  Thu Nov 26 06:36:27 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Thu, 26 Nov 2009 16:36:27 +1100
Subject: [Tutor] python time
In-Reply-To: <20091125171105.480f4963@o>
References: <20091125171105.480f4963@o>
Message-ID: <hel47q$49f$1@ger.gmane.org>

spir wrote:
> Hello,
> 
> How does python get the time in microseconds? 

time.time() should provide fractional second accuracy if the system 
provides them. Did the system clock actually record fractional seconds?


From davea at ieee.org  Thu Nov 26 12:51:02 2009
From: davea at ieee.org (Dave Angel)
Date: Thu, 26 Nov 2009 06:51:02 -0500
Subject: [Tutor] python time
In-Reply-To: <20091125171105.480f4963@o>
References: <20091125171105.480f4963@o>
Message-ID: <4B0E6BA6.5080206@ieee.org>

spir wrote:
> Hello,
>
> How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?)
>
> Thank you,
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
>
>
>   
You need to supply more information about your environment, and maybe 
the reason for asking.  Since you don't, I'll make some guesses.

If you're trying to do extremely fine delta-time measurements, there is 
a Pentium instruction to fetch the number of clock cycles.  You'd get 
this in assembler language, or with _asm in C code.

If you're on Windows, there is a NtQuerySystemTime() function exported 
by ntdll.dll.  It fetches a large integer specifying the number of 
100-ns intervals since some epoch time in 1601.  It's been deprecated, 
however, in favor of GetSystemTimeAsFileTime(), available in Win2k and 
later, and exported from kernel32.dll

There are others, but I can't find them at the moment.



From fomcl at yahoo.com  Thu Nov 26 14:13:06 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Thu, 26 Nov 2009 05:13:06 -0800 (PST)
Subject: [Tutor] UnicodeEncodeError
In-Reply-To: <1c2a2c590911250855x52533b3dpecc16cb1e5423e25@mail.gmail.com>
Message-ID: <52299.67517.qm@web110706.mail.gq1.yahoo.com>

OK, thanks a lot Spir and Kent for your replies. I converted element.text to str because some of the element.text were integers and these caused TypeErrors later on in the program. I don't have the program here (it's in the office) so I can't tell you the exact details. It's a search-and-replace program where users can enter a search text (or regex pattern) and a replace text. The source file is an xml file. Currently, strings with non-ascii letters still need to be inputted in unicode format, eg. u'enqu\xeate' instead of "enqu?te". Kinda ugly. I'll try to fix that later. Thanks again!

Cheers!!

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the face of ambiguity, refuse the temptation to guess.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 11/25/09, Kent Johnson <kent37 at tds.net> wrote:

From: Kent Johnson <kent37 at tds.net>
Subject: Re: [Tutor] UnicodeEncodeError
To: "Albert-Jan Roskam" <fomcl at yahoo.com>
Cc: "tutor at python.org tutor at python.org tutor at python.org" <tutor at python.org>
Date: Wednesday, November 25, 2009, 5:55 PM

On Wed, Nov 25, 2009 at 8:44 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:


Hi,
?
I'm parsing an xml file using elementtree, but it seems to get stuck on certain non-ascii characters (for example: "?"). I'm using Python 2.4. Here's the relevant code fragment:
?
# CODE:
for element in doc.getiterator():
? try:
????m = re.match(search_text, str(element.text))
? except UnicodeEncodeError:
??? raise # I want to get rid of this exception.

# PRINTBACK:
????m = re.match(search_text, str(element.text))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 4: ordinal not in range(128)

You can't convert element.text to a str because it contains non-ascii characters. Why are you converting it? re.match() will accept a unicode string as its argument.



?
How can I get rid of this unicode encode error. I tried:
s = str(element.text)
s.encode("utf-8")
(and then feeding it into the regex)
This fails because it is the str() that won't work. To get UTF-8 use
? s = element.text.encode('utf-8')
?but I don't think this is the correct solution.

?

The xml file is in UTF-8. Somehow I need to tell the program not to use ascii but utf-8, right?

No, just pass Unicode to re.match().

Kent 






      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/6ed8a80e/attachment.htm>

From tvsm at hotmail.com  Thu Nov 26 11:49:55 2009
From: tvsm at hotmail.com (Travis Murphy)
Date: Thu, 26 Nov 2009 05:49:55 -0500
Subject: [Tutor] quick question
Message-ID: <BAY126-W68DE4D48C26C56E6C019BD39B0@phx.gbl>


i am a first year student taking a python developers course. i wanted to know if from time to time in the future, is there somebody there i can speak to for support?
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/3d706d24/attachment.htm>

From zebra05 at gmail.com  Thu Nov 26 14:29:15 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Thu, 26 Nov 2009 15:29:15 +0200
Subject: [Tutor] quick question
In-Reply-To: <BAY126-W68DE4D48C26C56E6C019BD39B0@phx.gbl>
References: <BAY126-W68DE4D48C26C56E6C019BD39B0@phx.gbl>
Message-ID: <c7c6f3bc0911260529x53bb3b83se968112ec2fedb6f@mail.gmail.com>

Hi Travis,

Welcome to the python mailing list. You have come to the right place for
support and advice pertaining to python; it is what this list is about.


On Thu, Nov 26, 2009 at 12:49 PM, Travis Murphy <tvsm at hotmail.com> wrote:

>  i am a first year student taking a python developers course. i wanted to
> know if from time to time in the future, is there somebody there i can speak
> to for support?
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/9bc51af4/attachment.htm>

From zebra05 at gmail.com  Thu Nov 26 14:30:07 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Thu, 26 Nov 2009 15:30:07 +0200
Subject: [Tutor] quick question
In-Reply-To: <c7c6f3bc0911260529x53bb3b83se968112ec2fedb6f@mail.gmail.com>
References: <BAY126-W68DE4D48C26C56E6C019BD39B0@phx.gbl>
	<c7c6f3bc0911260529x53bb3b83se968112ec2fedb6f@mail.gmail.com>
Message-ID: <c7c6f3bc0911260530r1df4d87sfb56dadc1908a6cd@mail.gmail.com>

By the way, when asking a question or answering, you only ever need to send
the message to tutor at python.org :)

On Thu, Nov 26, 2009 at 3:29 PM, OkaMthembo <zebra05 at gmail.com> wrote:

> Hi Travis,
>
> Welcome to the python mailing list. You have come to the right place for
> support and advice pertaining to python; it is what this list is about.
>
>
> On Thu, Nov 26, 2009 at 12:49 PM, Travis Murphy <tvsm at hotmail.com> wrote:
>
>>  i am a first year student taking a python developers course. i wanted to
>> know if from time to time in the future, is there somebody there i can speak
>> to for support?
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
> --
> Regards,
> Lloyd
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/c0ef5f64/attachment-0001.htm>

From kent37 at tds.net  Thu Nov 26 14:33:20 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 26 Nov 2009 08:33:20 -0500
Subject: [Tutor] python time
In-Reply-To: <20091125171105.480f4963@o>
References: <20091125171105.480f4963@o>
Message-ID: <1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>

On Wed, Nov 25, 2009 at 11:11 AM, spir <denis.spir at free.fr> wrote:
> Hello,
>
> How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?)

Use the source...in particular, see floattime() in timemodule.c:
http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup

Kent

From fomcl at yahoo.com  Thu Nov 26 17:02:27 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Thu, 26 Nov 2009 08:02:27 -0800 (PST)
Subject: [Tutor] unicode mapping doesn't work
Message-ID: <612967.83315.qm@web110709.mail.gq1.yahoo.com>

Hi,

I want to substitute some letters with accents with theire non-accented equivalents. It should be easy, but it doesn't work. What am I doing wrong?

trans = {}
funnychars = u"????????????????????????"
asciichars = "eeeeooooaaaaEEEEOOOOAAAA"
for f, a in zip(funnychars, asciichars):
??? trans.update({f: a})
print funnychars.translate(trans) # why doesn't this return the letters without accents?

Cheers!!

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the face of ambiguity, refuse the temptation to guess.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/e2d08bee/attachment.htm>

From lie.1296 at gmail.com  Thu Nov 26 17:33:15 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 27 Nov 2009 03:33:15 +1100
Subject: [Tutor] unicode mapping doesn't work
In-Reply-To: <612967.83315.qm@web110709.mail.gq1.yahoo.com>
References: <612967.83315.qm@web110709.mail.gq1.yahoo.com>
Message-ID: <hemana$jub$1@ger.gmane.org>

Albert-Jan Roskam wrote:
> Hi,
> 
> I want to substitute some letters with accents with theire non-accented 
> equivalents. It should be easy, but it doesn't work. What am I doing wrong?
> 
> trans = {}
> funnychars = u"????????????????????????"
> asciichars = "eeeeooooaaaaEEEEOOOOAAAA"
> for f, a in zip(funnychars, asciichars):
>     trans.update({f: a})
> print funnychars.translate(trans) # why doesn't this return the letters 
> without accents?
> 

Perhaps the doc should be more obvious and perhaps unicode.translate is 
badly designed, but:

translate(...)
     S.translate(table) -> unicode

     Return a copy of the string S, where all characters have been mapped
     through the given translation table, which must be a mapping of
     Unicode ordinals to Unicode ordinals, Unicode strings or None.
     Unmapped characters are left untouched. Characters mapped to None
     are deleted.


it says "mapping of Unicode ordinals to Unicode ordinals, Unicode 
strings or None"

which reads as a mapping of (int) to (int, unicode, or None).

your translation table is a mapping of (unicode) to (str).

this works:
for f, a in zip(funnychars, asciichars):
     trans.update({ord(f): unicode(a)})

this as well:
for f, a in zip(funnychars, asciichars):
     trans.update({ord(f): ord(a)})


btw, you're updating the dict and creates a new dict for each character 
then immediately disposing it. Why not just:
for f, a in zip(funnychars, asciichars):
     trans[ord(f)] = ord(a)



From alan.gauld at btinternet.com  Thu Nov 26 18:58:34 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Thu, 26 Nov 2009 17:58:34 -0000
Subject: [Tutor] quick question
References: <BAY126-W68DE4D48C26C56E6C019BD39B0@phx.gbl>
Message-ID: <hemfka$3er$1@ger.gmane.org>


"Travis Murphy" <tvsm at hotmail.com> wrote

> i am a first year student taking a python developers course. 
> i wanted to know if from time to time in the future, is there 
> somebody there i can speak to for support?

Yes, a whole mailinglist of people.

The only caveat is that, as a matter of policy, we do not 
do homework for you. So if you are stuck on a homework 
problem show us what you have done, your expected 
approach and tell us what doesn't work or where you are stuck.

We will then supply hints, pointers or corrections as appropriate.

In general always tell us:
- Which OS you are on
- Which Python version
and
- Include short sample code to illustrate the problem
- Include full error text

Finally always use REPLY ALL in your mail tool to include 
the list as well as the individual to which you are replying.

Otherwise enjoy the tutor mailing list. :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From fomcl at yahoo.com  Thu Nov 26 20:50:26 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Thu, 26 Nov 2009 11:50:26 -0800 (PST)
Subject: [Tutor] unicode mapping doesn't work
In-Reply-To: <hemana$jub$1@ger.gmane.org>
Message-ID: <497887.26117.qm@web110701.mail.gq1.yahoo.com>

Thank you! Quite frankly, I didn't know about ordinals *blushes*.

Also, I thought that d.update({k: v}) was equivalent to d[k] = v, but apparently it's not. So thank you twice!

Cheers!!

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the face of ambiguity, refuse the temptation to guess.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 11/26/09, Lie Ryan <lie.1296 at gmail.com> wrote:

From: Lie Ryan <lie.1296 at gmail.com>
Subject: Re: [Tutor] unicode mapping doesn't work
To: tutor at python.org
Date: Thursday, November 26, 2009, 5:33 PM

Albert-Jan Roskam wrote:
> Hi,
> 
> I want to substitute some letters with accents with theire non-accented equivalents. It should be easy, but it doesn't work. What am I doing wrong?
> 
> trans = {}
> funnychars = u"????????????????????????"
> asciichars = "eeeeooooaaaaEEEEOOOOAAAA"
> for f, a in zip(funnychars, asciichars):
>? ???trans.update({f: a})
> print funnychars.translate(trans) # why doesn't this return the letters without accents?
> 

Perhaps the doc should be more obvious and perhaps unicode.translate is badly designed, but:

translate(...)
? ? S.translate(table) -> unicode

? ? Return a copy of the string S, where all characters have been mapped
? ? through the given translation table, which must be a mapping of
? ? Unicode ordinals to Unicode ordinals, Unicode strings or None.
? ? Unmapped characters are left untouched. Characters mapped to None
? ? are deleted.


it says "mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None"

which reads as a mapping of (int) to (int, unicode, or None).

your translation table is a mapping of (unicode) to (str).

this works:
for f, a in zip(funnychars, asciichars):
? ? trans.update({ord(f): unicode(a)})

this as well:
for f, a in zip(funnychars, asciichars):
? ? trans.update({ord(f): ord(a)})


btw, you're updating the dict and creates a new dict for each character then immediately disposing it. Why not just:
for f, a in zip(funnychars, asciichars):
? ? trans[ord(f)] = ord(a)


_______________________________________________
Tutor maillist? -? Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091126/acec9b3e/attachment.htm>

From lie.1296 at gmail.com  Fri Nov 27 01:41:27 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 27 Nov 2009 11:41:27 +1100
Subject: [Tutor] unicode mapping doesn't work
In-Reply-To: <497887.26117.qm@web110701.mail.gq1.yahoo.com>
References: <hemana$jub$1@ger.gmane.org>
	<497887.26117.qm@web110701.mail.gq1.yahoo.com>
Message-ID: <hen7am$4re$1@ger.gmane.org>

On 11/27/2009 10:43 AM, The Music Guy wrote:
 > Next thing is, I can't see logically how the path of the discussion of
 > the proposal lead to the proposal being rejected. It looked like a lot
 > of people really liked the idea--including Guido himself--and several
 > examples were given about how it could be useful. The final verdict on
 > the matter just doesn't make logical sense in the context of the
 > discussions.

Guido withdraws his support for the proposal due to many people 
declaiming that setattr, getattr, and hasattr are too rarely used to 
justify a new syntax (especially since nobody can agree to a syntax that 
looks pythonic). Many other followed Guido's lead to formally turn their 
"not vote"'s, +0s, and +1s to -1s for the reason.

"""
Guido wrote:
 > This seems to be the overwhelming feedback at this point, so I'm
 > withdrawing my support for the proposal. I hope that Ben can write up
 > a PEP and mark it rejected, to summarize the discussion; it's been a
 > useful lesson. Occasinoally, negative results are worth publishing!
 >
 > On 2/13/07, Barry Warsaw <barry at python.org> wrote:
 >> On Feb 13, 2007, at 7:24 PM, Greg Ewing wrote:
 >> > I'm still -1 on the basic idea, though, on the grounds of
 >> > YAGNIOE (You Aren't Going to Need It Often Enough).
 >>
 >> I can't really add much more than what's already be stated, but I
 >> echo Greg's sentiment.
"""


From alan.gauld at btinternet.com  Fri Nov 27 02:06:21 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 27 Nov 2009 01:06:21 -0000
Subject: [Tutor] unicode mapping doesn't work
References: <hemana$jub$1@ger.gmane.org><497887.26117.qm@web110701.mail.gq1.yahoo.com>
	<hen7am$4re$1@ger.gmane.org>
Message-ID: <hen8me$7ic$1@ger.gmane.org>

Huh?! Was this to the right place?
It doesn't seem to be related to the previous posts in the thread?

Confused....

Alan G.

"Lie Ryan" <lie.1296 at gmail.com> wrote in message 
news:hen7am$4re$1 at ger.gmane.org...
> On 11/27/2009 10:43 AM, The Music Guy wrote:
> > Next thing is, I can't see logically how the path of the discussion of
> > the proposal lead to the proposal being rejected. It looked like a lot
> > of people really liked the idea--including Guido himself--and several
> > examples were given about how it could be useful. The final verdict on
> > the matter just doesn't make logical sense in the context of the
> > discussions.
>
> Guido withdraws his support for the proposal due to many people 
> declaiming that setattr, getattr, and hasattr are too rarely used to 
> justify a new syntax (especially since nobody can agree to a syntax that 
> looks pythonic). Many other followed Guido's lead to formally turn their 
> "not vote"'s, +0s, and +1s to -1s for the reason.
>
> """
> Guido wrote:
> > This seems to be the overwhelming feedback at this point, so I'm
> > withdrawing my support for the proposal. I hope that Ben can write up
> > a PEP and mark it rejected, to summarize the discussion; it's been a
> > useful lesson. Occasinoally, negative results are worth publishing!
> >
> > On 2/13/07, Barry Warsaw <barry at python.org> wrote:
> >> On Feb 13, 2007, at 7:24 PM, Greg Ewing wrote:
> >> > I'm still -1 on the basic idea, though, on the grounds of
> >> > YAGNIOE (You Aren't Going to Need It Often Enough).
> >>
> >> I can't really add much more than what's already be stated, but I
> >> echo Greg's sentiment.
> """
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 



From modulok at gmail.com  Fri Nov 27 03:28:55 2009
From: modulok at gmail.com (Modulok)
Date: Thu, 26 Nov 2009 19:28:55 -0700
Subject: [Tutor] python time
In-Reply-To: <1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
Message-ID: <64c038660911261828g158b6634g9602b219839cbb7c@mail.gmail.com>

Doesn't time.time return a float?

>>> import time
>>> help(time.time)
Help on built-in function time in module time:

time(...)
    time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.


>>> time.time()
1259288538.576565

Right?
-Modulok-

On 11/26/09, Kent Johnson <kent37 at tds.net> wrote:
> On Wed, Nov 25, 2009 at 11:11 AM, spir <denis.spir at free.fr> wrote:
>> Hello,
>>
>> How does python get the time in microseconds? (In other words, how would I
>> get it if python (like some other languages) would provide time in whole
>> seconds only?)
>
> Use the source...in particular, see floattime() in timemodule.c:
> http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup
>
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

From kent37 at tds.net  Fri Nov 27 05:54:05 2009
From: kent37 at tds.net (Kent Johnson)
Date: Thu, 26 Nov 2009 23:54:05 -0500
Subject: [Tutor] python time
In-Reply-To: <64c038660911261828g158b6634g9602b219839cbb7c@mail.gmail.com>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
	<64c038660911261828g158b6634g9602b219839cbb7c@mail.gmail.com>
Message-ID: <1c2a2c590911262054yeda8bcx341f04c1118e222b@mail.gmail.com>

On Thu, Nov 26, 2009 at 9:28 PM, Modulok <modulok at gmail.com> wrote:
> Doesn't time.time return a float?

>>>> time.time()
> 1259288538.576565
>
> Right?

Yes.

I'm not at all sure I understand the original question, but one
plausible interpretation is, "How does python get the time to high
accuracy? I want to do that in my programs." That is the question I
was answering.

Kent

> -Modulok-
>
> On 11/26/09, Kent Johnson <kent37 at tds.net> wrote:
>> On Wed, Nov 25, 2009 at 11:11 AM, spir <denis.spir at free.fr> wrote:
>>> Hello,
>>>
>>> How does python get the time in microseconds? (In other words, how would I
>>> get it if python (like some other languages) would provide time in whole
>>> seconds only?)
>>
>> Use the source...in particular, see floattime() in timemodule.c:
>> http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup
>>
>> Kent
>> _______________________________________________
>> Tutor maillist ?- ?Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>

From stefan at lsd.co.za  Fri Nov 27 08:45:56 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Fri, 27 Nov 2009 09:45:56 +0200 (SAST)
Subject: [Tutor] socket timeout
Message-ID: <23225549.468.1259307956072.JavaMail.root@zimbra>


Hi guys,

Maybe someone can point me in the right direction, since i'm not sure why i cant get the timeout to work. Admittedly i don't know much about socket programming.

Im using the below code...

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.setdefaulttimeout(10000)
s.connect((proxy,port))

As the code is above, it works (with the timeout commented out), when i leave the uncomment the setdefaulttimeout, the code doesnt even seem to connect. I've played with various values (1, 5, 500 etc)

The problem essentially occurs when i connect to a server where a firewall drops the packet. It seems to wait really long before moving on, i would like the timeout to kick in after 10secs or so to continue. Im also not sure if this is a good way to check if a ip,port is available.

Thanks in advance

stefan

From stefan at lsd.co.za  Fri Nov 27 09:03:57 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Fri, 27 Nov 2009 10:03:57 +0200 (SAST)
Subject: [Tutor] Python best practices
Message-ID: <373240271.474.1259309037554.JavaMail.root@zimbra>

hihi!  (two in 1 day!)

Is there a document, pep, wiki etc that defines best practice for python code?  (maybe its more generic).

I keep stumbling on things I think, it would be nice if someone mentioned this. Some things ive seen

- keep you try blocks as small as possible, so you can isolate the error
- functions should return one value (im not 100% of this one)
- declare variable names in a certain way (place?)
- use the if __name__ == '__main__': main() convention

im sure there are tons more, that i only figure out after stumbling through them. does anyone know if its written down, or stumbling is just part of the learning process?

stefan

From lie.1296 at gmail.com  Fri Nov 27 09:41:53 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Fri, 27 Nov 2009 19:41:53 +1100
Subject: [Tutor] unicode mapping doesn't work
In-Reply-To: <hen8me$7ic$1@ger.gmane.org>
References: <hemana$jub$1@ger.gmane.org><497887.26117.qm@web110701.mail.gq1.yahoo.com>	<hen7am$4re$1@ger.gmane.org>
	<hen8me$7ic$1@ger.gmane.org>
Message-ID: <heo3fg$ooo$1@ger.gmane.org>

On 11/27/2009 12:06 PM, Alan Gauld wrote:
> Huh?! Was this to the right place?
> It doesn't seem to be related to the previous posts in the thread?
>
> Confused....
>
> Alan G.


whoops.. wrong thread...



From zebra05 at gmail.com  Fri Nov 27 10:57:46 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 11:57:46 +0200
Subject: [Tutor] Python on multicore machines
Message-ID: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>

Hi All,

Is there a python implementation that takes advantage of all cores on modern
multicore machines?

-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/e943bdcb/attachment.htm>

From sander.sweers at gmail.com  Fri Nov 27 11:26:15 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Fri, 27 Nov 2009 11:26:15 +0100
Subject: [Tutor] socket timeout
In-Reply-To: <23225549.468.1259307956072.JavaMail.root@zimbra>
References: <23225549.468.1259307956072.JavaMail.root@zimbra>
Message-ID: <b65fbb130911270226m1459d151i75a94755386ffdd3@mail.gmail.com>

2009/11/27 Stefan Lesicnik <stefan at lsd.co.za>:
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> #s.setdefaulttimeout(10000)
> s.connect((proxy,port))

I have never used socket but a quick look at the docs [1] my guess is
that you should use use s.settimeout() [2].

The docs say that setdefaulttimeout [3] will only be applied to *new*
socket object. You set the default after creating the socket object.

Greets
Sander

[1] http://docs.python.org/library/socket.html
[2] http://docs.python.org/library/socket.html#socket.socket.settimeout
[3] http://docs.python.org/library/socket.html#socket.setdefaulttimeout

From lie.1296 at gmail.com  Fri Nov 27 14:04:57 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Sat, 28 Nov 2009 00:04:57 +1100
Subject: [Tutor] Python on multicore machines
In-Reply-To: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
Message-ID: <heoit1$kdm$1@ger.gmane.org>

On 11/27/2009 8:57 PM, OkaMthembo wrote:
> Hi All,
>
> Is there a python implementation that takes advantage of all cores on
> modern multicore machines?

yes, Cpython if you used "multiprocessing". Ironpython and Jython 
doesn't have GIL. Unladen Swallow plans to remove GIL.

Many minor python branch have been attempted to remove GIL, just as many 
are rejected for merging back to the main trunk since they slow down 
single threading. You might be able to find these patches from the 
internet and download the appropriate python source, patch them, and get 
python minus GIL.


From waynejwerner at gmail.com  Fri Nov 27 14:08:03 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Fri, 27 Nov 2009 07:08:03 -0600
Subject: [Tutor] Python best practices
In-Reply-To: <373240271.474.1259309037554.JavaMail.root@zimbra>
References: <373240271.474.1259309037554.JavaMail.root@zimbra>
Message-ID: <333efb450911270508k4c03e31di5270f39790e71748@mail.gmail.com>

On Fri, Nov 27, 2009 at 2:03 AM, Stefan Lesicnik <stefan at lsd.co.za> wrote:

> hihi!  (two in 1 day!)
>
> Is there a document, pep, wiki etc that defines best practice for python
> code?  (maybe its more generic).
>
>
 This one is fairly comprehensive:
http://www.python.org/dev/peps/pep-0008/

HTH,
Wayne

I keep stumbling on things I think, it would be nice if someone mentioned
> this. Some things ive seen
>
> - keep you try blocks as small as possible, so you can isolate the error
> - functions should return one value (im not 100% of this one)
> - declare variable names in a certain way (place?)
> - use the if __name__ == '__main__': main() convention
>
> im sure there are tons more, that i only figure out after stumbling through
> them. does anyone know if its written down, or stumbling is just part of the
> learning process?
>
> stefan
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/645b8069/attachment.htm>

From kent37 at tds.net  Fri Nov 27 14:38:50 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 27 Nov 2009 08:38:50 -0500
Subject: [Tutor] Python on multicore machines
In-Reply-To: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
Message-ID: <1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>

On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> Hi All,
>
> Is there a python implementation that takes advantage of all cores on modern
> multicore machines?

Presumably you mean something like, "Is there a python implementation
that can run multiple compute-bound processes on multiple cores
concurrently."

Some options:
- the multiprocessing module in the std lib - here is an example of
using it with numpy:
http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf

- Jython and IronPython both have threading models that allow multiple
threads to run concurrently on multiple processors.

Kent

From zebra05 at gmail.com  Fri Nov 27 14:43:45 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 15:43:45 +0200
Subject: [Tutor] Python on multicore machines
In-Reply-To: <heoit1$kdm$1@ger.gmane.org>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<heoit1$kdm$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911270543p4ad5ac64h4cd563f06a1ebac2@mail.gmail.com>

Sorry to double -post, Lie. Didn;t "Reply to all"..

Thats interesting - is Unladen Swallow production-ready? I've been over the
site briefly, but i can't decide whether it's still in incubation or not.

On Fri, Nov 27, 2009 at 3:04 PM, Lie Ryan <lie.1296 at gmail.com> wrote:

> On 11/27/2009 8:57 PM, OkaMthembo wrote:
>
>> Hi All,
>>
>> Is there a python implementation that takes advantage of all cores on
>> modern multicore machines?
>>
>
> yes, Cpython if you used "multiprocessing". Ironpython and Jython doesn't
> have GIL. Unladen Swallow plans to remove GIL.
>
> Many minor python branch have been attempted to remove GIL, just as many
> are rejected for merging back to the main trunk since they slow down single
> threading. You might be able to find these patches from the internet and
> download the appropriate python source, patch them, and get python minus
> GIL.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/3b2e1065/attachment.htm>

From stefan_ml at behnel.de  Fri Nov 27 14:52:44 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Fri, 27 Nov 2009 14:52:44 +0100
Subject: [Tutor] Python on multicore machines
In-Reply-To: <heoit1$kdm$1@ger.gmane.org>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<heoit1$kdm$1@ger.gmane.org>
Message-ID: <heoljc$saa$1@ger.gmane.org>

Lie Ryan, 27.11.2009 14:04:
> Unladen Swallow plans to remove GIL.

Don't bet your money on it.

Stefan


From zebra05 at gmail.com  Fri Nov 27 14:57:23 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 15:57:23 +0200
Subject: [Tutor] Python on multicore machines
In-Reply-To: <1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
Message-ID: <c7c6f3bc0911270557u43b9e4dbse51bb2a984ff3651@mail.gmail.com>

Thanks, i must admit the concept of the GIL is cloudy to me - for example,
if the python interpreter on a single machine is
handling one process and locks until it is done, then on to the next one,
and so on - isn't that what causes speed issues?
I was wondering why python can't implicitly handle multiple processes at
once by using all machine cores (have many threads, each
invoking the interpreter and handling a process).

Maybe i should get up to speed on threads first to get the bigger picture?


On Fri, Nov 27, 2009 at 3:38 PM, Kent Johnson <kent37 at tds.net> wrote:

> On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> > Hi All,
> >
> > Is there a python implementation that takes advantage of all cores on
> modern
> > multicore machines?
>
> Presumably you mean something like, "Is there a python implementation
> that can run multiple compute-bound processes on multiple cores
> concurrently."
>
> Some options:
> - the multiprocessing module in the std lib - here is an example of
> using it with numpy:
> http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf
>
> - Jython and IronPython both have threading models that allow multiple
> threads to run concurrently on multiple processors.
>
> Kent
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/5dd3da95/attachment-0001.htm>

From stefan_ml at behnel.de  Fri Nov 27 14:59:16 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Fri, 27 Nov 2009 14:59:16 +0100
Subject: [Tutor] Python on multicore machines
In-Reply-To: <1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
Message-ID: <heolvk$tkh$1@ger.gmane.org>

Kent Johnson, 27.11.2009 14:38:
> On Fri, Nov 27, 2009 at 4:57 AM, OkaMthembo wrote:
>> Is there a python implementation that takes advantage of all cores on modern
>> multicore machines?
> 
> Presumably you mean something like, "Is there a python implementation
> that can run multiple compute-bound processes on multiple cores
> concurrently."
> 
> Some options:
> - the multiprocessing module in the std lib - here is an example of
> using it with numpy:
> http://folk.uio.no/sturlamo/python/multiprocessing-tutorial.pdf

In case this is about some kind of numeric computation that doesn't require
Python object types, another option is to use Cython and put the compute
intensive code into a "nogil" function that you can run multi-threaded.

If the above doesn't apply, it may be quite possible that the GIL is not
even the limiting factor. Unless the OP provides more insight, this is
impossible to answer.

Stefan


From kent37 at tds.net  Fri Nov 27 16:12:21 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 27 Nov 2009 10:12:21 -0500
Subject: [Tutor] Python on multicore machines
In-Reply-To: <c7c6f3bc0911270557u43b9e4dbse51bb2a984ff3651@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
	<c7c6f3bc0911270557u43b9e4dbse51bb2a984ff3651@mail.gmail.com>
Message-ID: <1c2a2c590911270712n5b2229ebn566bc8175942c5be@mail.gmail.com>

On Fri, Nov 27, 2009 at 8:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> Thanks, i must admit the concept of the GIL is cloudy to me - for example,
> if the python interpreter on a single machine is
> handling one process and locks until it is done, then on to the next one,
> and so on - isn't that what causes speed issues?

A single interpreter runs in a single process. Within that process,
only one thread can be interpreting Python byte codes at one time. If
a thread is blocked, for example waiting for I/O, another thread can
run. Some C extensions release the GIL so other threads can run
concurrently.

> I was wondering why python can't implicitly handle multiple processes at
> once by using all machine cores (have many threads, each
> invoking the interpreter and handling a process).

Because explicit is better than implicit?

The multiprocessing module allows you to control multiple processes
from Python and do pretty much what you describe above - a Python main
program can create multiple processes, each running a separate
interpreter, all working on a single problem.

> Maybe i should get up to speed on threads first to get the bigger picture?

At least make sure you understand the difference between threads and processes.

Kent

From zebra05 at gmail.com  Fri Nov 27 16:15:14 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 17:15:14 +0200
Subject: [Tutor] Python on multicore machines
In-Reply-To: <heolvk$tkh$1@ger.gmane.org>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
	<heolvk$tkh$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>

Thanks everyone,

I realise that my question is vague on a few fronts. To try and clarify,
supposing i had a box running a
Python web app on it, and the web server handles say, 10 concurrent
requests. Now i've read that only one
instance of the  Python interpreter can run on a single machine. Can that
interpreter be invoked by the web server
to run several requests at once, or do the requests get queued and handled
one at a time? If the interpreter cannot
handle concurrent requests by the web server, is it because of the GIL? I am
thinking that on multi-core machines
the interpreter should be able to run different processes on each core, thus
being able to serve more requests?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/8c276f11/attachment.htm>

From zebra05 at gmail.com  Fri Nov 27 16:30:23 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 17:30:23 +0200
Subject: [Tutor] Python on multicore machines
In-Reply-To: <1c2a2c590911270712n5b2229ebn566bc8175942c5be@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
	<c7c6f3bc0911270557u43b9e4dbse51bb2a984ff3651@mail.gmail.com>
	<1c2a2c590911270712n5b2229ebn566bc8175942c5be@mail.gmail.com>
Message-ID: <c7c6f3bc0911270730h71d9d1b5t77098812ebd3ab56@mail.gmail.com>

Thanks Kent, that did clear some of the smog.

>At least make sure you understand the difference between threads and
processes.

Will be taking that advice. I think it's the reason i struggle to ask the
question better.

On Fri, Nov 27, 2009 at 5:12 PM, Kent Johnson <kent37 at tds.net> wrote:

> On Fri, Nov 27, 2009 at 8:57 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> > Thanks, i must admit the concept of the GIL is cloudy to me - for
> example,
> > if the python interpreter on a single machine is
> > handling one process and locks until it is done, then on to the next one,
> > and so on - isn't that what causes speed issues?
>
> A single interpreter runs in a single process. Within that process,
> only one thread can be interpreting Python byte codes at one time. If
> a thread is blocked, for example waiting for I/O, another thread can
> run. Some C extensions release the GIL so other threads can run
> concurrently.
>
> > I was wondering why python can't implicitly handle multiple processes at
> > once by using all machine cores (have many threads, each
> > invoking the interpreter and handling a process).
>
> Because explicit is better than implicit?
>
> The multiprocessing module allows you to control multiple processes
> from Python and do pretty much what you describe above - a Python main
> program can create multiple processes, each running a separate
> interpreter, all working on a single problem.
>
> > Maybe i should get up to speed on threads first to get the bigger
> picture?
>
> At least make sure you understand the difference between threads and
> processes.
>
> Kent
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/a96f25a3/attachment.htm>

From stefan_ml at behnel.de  Fri Nov 27 16:42:08 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Fri, 27 Nov 2009 16:42:08 +0100
Subject: [Tutor] Python on multicore machines
In-Reply-To: <c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>	<heolvk$tkh$1@ger.gmane.org>
	<c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>
Message-ID: <heos0g$l5m$1@ger.gmane.org>

OkaMthembo, 27.11.2009 16:15:
> i've read that only one
> instance of the  Python interpreter can run on a single machine.

That's likely the source of your confusion then. You can run any number of
Python interpreters on a single machine (limited by system resources like
RAM, obviously), and in fact, many web servers will do that for you to
handle multiple requests in parallel.

Stefan


From zebra05 at gmail.com  Fri Nov 27 17:01:45 2009
From: zebra05 at gmail.com (OkaMthembo)
Date: Fri, 27 Nov 2009 18:01:45 +0200
Subject: [Tutor] Python on multicore machines
In-Reply-To: <heos0g$l5m$1@ger.gmane.org>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
	<heolvk$tkh$1@ger.gmane.org>
	<c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>
	<heos0g$l5m$1@ger.gmane.org>
Message-ID: <c7c6f3bc0911270801pc35417fhefcccdb3570f25f@mail.gmail.com>

> That's likely the source of your confusion then. You can run any number of
> Python interpreters on a single machine (limited by system resources like
> RAM, obviously), and in fact, many web servers will do that for you to
> handle multiple requests in parallel.

Thanks Stefan, i was  not aware of the fact.

On Fri, Nov 27, 2009 at 5:42 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:

> OkaMthembo, 27.11.2009 16:15:
> > i've read that only one
> > instance of the  Python interpreter can run on a single machine.
>
> That's likely the source of your confusion then. You can run any number of
> Python interpreters on a single machine (limited by system resources like
> RAM, obviously), and in fact, many web servers will do that for you to
> handle multiple requests in parallel.
>
> Stefan
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Lloyd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091127/6b36d54c/attachment-0001.htm>

From kent37 at tds.net  Fri Nov 27 17:09:30 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 27 Nov 2009 11:09:30 -0500
Subject: [Tutor] Python on multicore machines
In-Reply-To: <c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>
References: <c7c6f3bc0911270157l4a4624aeh227391e2559785b6@mail.gmail.com>
	<1c2a2c590911270538h75925a36sb6c2a931b5d0542a@mail.gmail.com>
	<heolvk$tkh$1@ger.gmane.org>
	<c7c6f3bc0911270715i484cbb44m79803956d5719ecd@mail.gmail.com>
Message-ID: <1c2a2c590911270809l73c4c4f9yb8ccdd635077200f@mail.gmail.com>

On Fri, Nov 27, 2009 at 10:15 AM, OkaMthembo <zebra05 at gmail.com> wrote:
> Thanks everyone,
>
> I realise that my question is vague on a few fronts. To try and clarify,
> supposing i had a box running a
> Python web app on it, and the web server handles say, 10 concurrent
> requests.

This is still pretty vague, as there are many ways to write a Python
web app and most of those have many options for deployment. But any of
the major Python web frameworks have deployment options that support
concurrent requests. For example Django deployment options:
http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index

Also, remember that a request handler will spend some of its time
blocked waiting for socket I/O or database access, and this is time
when other threads can run. Also, there are asynchronous request
handling models that do not rely on threads, for example Twisted.

Kent

From ghartshaw at gmail.com  Fri Nov 27 20:29:28 2009
From: ghartshaw at gmail.com (Garrett Hartshaw)
Date: Fri, 27 Nov 2009 14:29:28 -0500
Subject: [Tutor] AttributeError: Vector instance has no attribute 'Normalize'
Message-ID: <8b40e7de0911271129u52b7cb44v4e59180a28b395ee@mail.gmail.com>

The program I am writing gives the following error message when run.

Traceback (most recent call last):
? File "./space.py", line 177, in <module>
??? main()
? File "./space.py", line 173, in main
??? player = Ship("space/models/fighter.3ds",
"space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
? File "./space.py", line 131, in __init__
??? Object.__init__(self, obj, tex, rot, pos)
? File "./space.py", line 124, in __init__
??? self.model = Model(obj)
? File "./space.py", line 80, in __init__
??? self.CalcNormals()
? File "./space.py", line 90, in CalcNormals
??? vectB = (vectA - vectB).Normalize()
AttributeError: Vector instance has no attribute 'Normalize'

The definition for Vector is:

class Vector:
??? def __init__(self, x=0, y=0, z=0):
??? ??? self.x = x
??? ??? self.y = y
??? ??? self.z = z

??? def Normalize(self):
??? ??? return self / self.Length()

??? def __sub__(self, right):
??? ??? return Vector(self.x - right.x, self.y - right.y, self.z - right.z)

so I cannot figure out what the error is.

(o_
//\
V_/_

From bgailer at gmail.com  Fri Nov 27 20:41:05 2009
From: bgailer at gmail.com (bob gailer)
Date: Fri, 27 Nov 2009 14:41:05 -0500
Subject: [Tutor] AttributeError: Vector instance has no attribute
	'Normalize'
In-Reply-To: <8b40e7de0911271129u52b7cb44v4e59180a28b395ee@mail.gmail.com>
References: <8b40e7de0911271129u52b7cb44v4e59180a28b395ee@mail.gmail.com>
Message-ID: <4B102B51.30304@gmail.com>

Garrett Hartshaw wrote:
> The program I am writing gives the following error message when run.
>
> Traceback (most recent call last):
>   File "./space.py", line 177, in <module>
>     main()
>   File "./space.py", line 173, in main
>     player = Ship("space/models/fighter.3ds",
> "space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
>   File "./space.py", line 131, in __init__
>     Object.__init__(self, obj, tex, rot, pos)
>   File "./space.py", line 124, in __init__
>     self.model = Model(obj)
>   File "./space.py", line 80, in __init__
>     self.CalcNormals()
>   File "./space.py", line 90, in CalcNormals
>     vectB = (vectA - vectB).Normalize()
> AttributeError: Vector instance has no attribute 'Normalize'
>   

I get a different error:
  File "J:\pipelines\vector.py", line 8, in Normalize
    return self / self.Length()
AttributeError: Vector instance has no attribute 'Length'

> The definition for Vector is:
>
> class Vector:
>     def __init__(self, x=0, y=0, z=0):
>         self.x = x
>         self.y = y
>         self.z = z
>
>     def Normalize(self):
>         return self / self.Length()
>
>     def __sub__(self, right):
>         return Vector(self.x - right.x, self.y - right.y, self.z - right.z)
>
>
>   


-- 
Bob Gailer<br>
Chapel Hill NC<br>
919-636-4239

From denis.spir at free.fr  Thu Nov 26 18:55:23 2009
From: denis.spir at free.fr (spir)
Date: Thu, 26 Nov 2009 18:55:23 +0100
Subject: [Tutor] unicode mapping doesn't work
In-Reply-To: <hemana$jub$1@ger.gmane.org>
References: <612967.83315.qm@web110709.mail.gq1.yahoo.com>
	<hemana$jub$1@ger.gmane.org>
Message-ID: <20091126185523.6c8aced0@o>

Lie Ryan <lie.1296 at gmail.com> wrote:

> > funnychars = u"????????????????????????"
> > asciichars = "eeeeooooaaaaEEEEOOOOAAAA"
> >

In addition to Lie's reply, you will very probably need diacritic-free chars to be unicode, too. Otherwise prepare for later UnocideEn/De-codeError-s. As a rule of thumb, if you work with unicode thingies, make everything unicode in your whole program.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From denis.spir at free.fr  Fri Nov 27 09:27:25 2009
From: denis.spir at free.fr (spir)
Date: Fri, 27 Nov 2009 09:27:25 +0100
Subject: [Tutor] python time
In-Reply-To: <1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
Message-ID: <20091127092725.6ccd24d4@o>

Kent Johnson <kent37 at tds.net> wrote:

> On Wed, Nov 25, 2009 at 11:11 AM, spir <denis.spir at free.fr> wrote:
> > Hello,
> >
> > How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?)
> 
> Use the source...in particular, see floattime() in timemodule.c:
> http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup
> 
> Kent
> 

Thanks, Kent.
So, python  uses C's gettimeofday() func when available (microsecond), or ftime() (millisecond), else it has only plain second precision using time(). But there is no matching system call AFAIK except for time (via datetime in unix-like systems). So, does someone know of any solution via the system itself? How do C funcs get precise time, anyway?

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From denis.spir at free.fr  Fri Nov 27 21:59:24 2009
From: denis.spir at free.fr (spir)
Date: Fri, 27 Nov 2009 21:59:24 +0100
Subject: [Tutor] AttributeError: Vector instance has no attribute
 'Normalize'
In-Reply-To: <8b40e7de0911271129u52b7cb44v4e59180a28b395ee@mail.gmail.com>
References: <8b40e7de0911271129u52b7cb44v4e59180a28b395ee@mail.gmail.com>
Message-ID: <20091127215924.32a49433@o>

Garrett Hartshaw <ghartshaw at gmail.com> wrote:

> The program I am writing gives the following error message when run.
> 
> Traceback (most recent call last):
> ? File "./space.py", line 177, in <module>
> ??? main()
> ? File "./space.py", line 173, in main
> ??? player = Ship("space/models/fighter.3ds",
> "space/models/realistic.bmp", Quaternion(), Vector(0, 0, -30), 1, 0.1)
> ? File "./space.py", line 131, in __init__
> ??? Object.__init__(self, obj, tex, rot, pos)
> ? File "./space.py", line 124, in __init__
> ??? self.model = Model(obj)
> ? File "./space.py", line 80, in __init__
> ??? self.CalcNormals()
> ? File "./space.py", line 90, in CalcNormals
> ??? vectB = (vectA - vectB).Normalize()
> AttributeError: Vector instance has no attribute 'Normalize'
> 
> The definition for Vector is:
> 
> class Vector:
> ??? def __init__(self, x=0, y=0, z=0):
> ??? ??? self.x = x
> ??? ??? self.y = y
> ??? ??? self.z = z
> 
> ??? def Normalize(self):
> ??? ??? return self / self.Length()
> 
> ??? def __sub__(self, right):
> ??? ??? return Vector(self.x - right.x, self.y - right.y, self.z - right.z)
> 
> so I cannot figure out what the error is.

You may print out the given vector's attr list using dir to check, or just check it actually holds a Normalise method, exactly at the point you want its norm. Something like:
??? v = (vectA - vectB)
    print (hasattr(v, "Normalise")); print (v.__class__)
    vectB = v.Normalise()

The second print() is for this reason: I bet you have several instances (;-) of the Vector class on your system, and the one actually used is not the one you guess...

> (o_
> //\
> V_/_
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




________________________________

la vita e estrany

http://spir.wikidot.com/


From alan.gauld at btinternet.com  Fri Nov 27 22:10:20 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Fri, 27 Nov 2009 21:10:20 -0000
Subject: [Tutor] python time
References: <20091125171105.480f4963@o><1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
	<20091127092725.6ccd24d4@o>
Message-ID: <hepf7t$fm2$1@ger.gmane.org>

"spir" <denis.spir at free.fr> wrote

> So, python  uses C's gettimeofday() func when available 

It's not C's function, it's a Unix system call. 
It's been part of Unix since BSD 4.2

> ftime() (millisecond), 

Also Unix and predates BSD 4.2...

> else it has only plain second precision using time(). 

Which is an ANSI C function.

> But there is no matching system call AFAIK except for time 
> (via datetime in unix-like systems). 

time() is the lowest common denominator supported by any ANSI C system.
It returns a tm struct:

struct tm{
    int tm_sec;
    int tm_min;
    // etc up to
    int tm_yday;
    int tm_isdst;
}

So only seconds need to be supported for ANSI compliance.

gettimeofday() is the Unix system call that returns microseconds.

But to confuse things, on Unix there is also the time() library function
(not system call - subtle difference!) which returns a timeval structure:

struct timeval{
    long tv_sec;
    long tv_usec;
}

So even time() on Unix returns microsecs.

But on early DOS it only returns seconds.
(I may be wrong but I think time() on DOS 6 returned milliseconds?)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


From denis.spir at free.fr  Sat Nov 28 00:07:13 2009
From: denis.spir at free.fr (spir)
Date: Sat, 28 Nov 2009 00:07:13 +0100
Subject: [Tutor] python time
In-Reply-To: <hepf7t$fm2$1@ger.gmane.org>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
	<20091127092725.6ccd24d4@o> <hepf7t$fm2$1@ger.gmane.org>
Message-ID: <20091128000713.66c835ec@o>

"Alan Gauld" <alan.gauld at btinternet.com> wrote:

Thank you, Alan.

> "spir" <denis.spir at free.fr> wrote
> 
> > So, python  uses C's gettimeofday() func when available 
> 
> It's not C's function, it's a Unix system call. 
> It's been part of Unix since BSD 4.2
>
> > ftime() (millisecond), 
> 
> Also Unix and predates BSD 4.2...

I am confused here. That's what I first thought (there _must_ be a way to get time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find the proper manner to use these system calls. Even from the command-line directly. Certainly missing something _really_ obvious.

As for the code in timemodule.c, I read:

floattime(void)
{
	/* There are three ways to get the time:
	  (1) gettimeofday() -- resolution in microseconds
	  (2) ftime() -- resolution in milliseconds
	  (3) time() -- resolution in seconds
	  In all cases the return value is a float in seconds.
	  Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
	  fail, so we fall back on ftime() or time().
	  Note: clock resolution does not imply clock accuracy! */
#ifdef HAVE_GETTIMEOFDAY
	{
		struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
		if (gettimeofday(&t) == 0)
			return (double)t.tv_sec + t.tv_usec*0.000001;
#else /* !GETTIMEOFDAY_NO_TZ */
		if (gettimeofday(&t, (struct timezone *)NULL) == 0)
			return (double)t.tv_sec + t.tv_usec*0.000001;
#endif /* !GETTIMEOFDAY_NO_TZ */
	}

#endif /* !HAVE_GETTIMEOFDAY */
...

This let me think gettimeofday() and ftime() are C routines, but they may ne wrappers around system calls.

> 
> > else it has only plain second precision using time(). 
> 
> Which is an ANSI C function.
> 
> > But there is no matching system call AFAIK except for time 
> > (via datetime in unix-like systems). 
> 
> time() is the lowest common denominator supported by any ANSI C system.
> It returns a tm struct:
> 
> struct tm{
>     int tm_sec;
>     int tm_min;
>     // etc up to
>     int tm_yday;
>     int tm_isdst;
> }
> 
> So only seconds need to be supported for ANSI compliance.

Right. This is precisely the reason why some languages (eg lua) don't provide any way to get time in ms or us. And also the source of my original post on the topic.

> gettimeofday() is the Unix system call that returns microseconds.
> 
> But to confuse things, on Unix there is also the time() library function
> (not system call - subtle difference!) which returns a timeval structure:
> 
> struct timeval{
>     long tv_sec;
>     long tv_usec;
> }
> 
> So even time() on Unix returns microsecs.
> 
> But on early DOS it only returns seconds.
> (I may be wrong but I think time() on DOS 6 returned milliseconds?)
> 
> HTH,
 
So, I still have the issue of not beeing smart enough to access one of these systems-provided features. Would someone be nice & write an example call to use eg Unix's gettimeofday directly from python or from the command-line? (without using python's time module, indeed)

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From kent37 at tds.net  Sat Nov 28 00:54:37 2009
From: kent37 at tds.net (Kent Johnson)
Date: Fri, 27 Nov 2009 18:54:37 -0500
Subject: [Tutor] python time
In-Reply-To: <20091128000713.66c835ec@o>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
	<20091127092725.6ccd24d4@o> <hepf7t$fm2$1@ger.gmane.org>
	<20091128000713.66c835ec@o>
Message-ID: <1c2a2c590911271554h504750d2wa4aa7b13b1d94a71@mail.gmail.com>

On Fri, Nov 27, 2009 at 6:07 PM, spir <denis.spir at free.fr> wrote:

> I am confused here. That's what I first thought (there _must_ be a way to get time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find the proper manner to use these system calls. Even from the command-line directly. Certainly missing something _really_ obvious.

I think you have to call them from C or some other language that can
access the system calls.

>> So only seconds need to be supported for ANSI compliance.
>
> Right. This is precisely the reason why some languages (eg lua) don't provide any way to get time in ms or us. And also the source of my original post on the topic.

Some solutions for Lua here:
http://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds

Kent

From modulok at gmail.com  Sat Nov 28 07:06:49 2009
From: modulok at gmail.com (Modulok)
Date: Fri, 27 Nov 2009 23:06:49 -0700
Subject: [Tutor] python time
In-Reply-To: <20091127074916.315132a3@o>
References: <20091125171105.480f4963@o>
	<1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com>
	<64c038660911261828g158b6634g9602b219839cbb7c@mail.gmail.com>
	<20091127074916.315132a3@o>
Message-ID: <64c038660911272206i1ba04da6m79d1fd71c3b0d537@mail.gmail.com>

>> Doesn't time.time return a float?
>>
>> >>> import time
>> >>> help(time.time)
>> Help on built-in function time in module time:
>>
>> time(...)
>>     time() -> floating point number
>>
>>     Return the current time in seconds since the Epoch.
>>     Fractions of a second may be present if the system clock provides
>> them.
>>
>>
>> >>> time.time()
>> 1259288538.576565
>>
>> Right?
>> -Modulok-
>
> For sure, but this wasn't my question. I was precisely asking how python
> does that.

Clearly I have a mighty need to consume more coffee. My apologies for
misreading the original post.
-Modulok-

From ljmamoreira at gmail.com  Sat Nov 28 10:52:38 2009
From: ljmamoreira at gmail.com (Jose Amoreira)
Date: Sat, 28 Nov 2009 09:52:38 +0000
Subject: [Tutor] Iterating over two sequences in "parallel"
Message-ID: <200911280952.38453.ljmamoreira@gmail.com>

Hi!
I want to process corresponding elements of two lists, sequentially. Call the 
lists list1 and list2, and assume they have equal lengths. I can do something 
like

for index in range(len(list1)):
    process(list1[index], list2[index])

But I find it somehow rather ugly, because we generate yet another an list for 
the index, when we already have the two we want to process. I know we can use 
xrange, but still I find it awkward...

Instead of the above snippet, I am considering something like

while list1:
    process(list1.pop(), list2.pop())

But this has the side effect of emptying both lists, which may not be 
convenient. Of course we can make backup copies of the lists if needed, but we 
are then recovering the previous method ugliness...

Do you guys have any suggestions regarding this? Thanks
Jose Amoreira

From ljmamoreira at gmail.com  Sat Nov 28 12:03:30 2009
From: ljmamoreira at gmail.com (Jose Amoreira)
Date: Sat, 28 Nov 2009 11:03:30 +0000
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <000001ca7019$3f2541a0$bd6fc4e0$@johansson@math.umu.se>
References: <200911280952.38453.ljmamoreira@gmail.com>
	<000001ca7019$3f2541a0$bd6fc4e0$@johansson@math.umu.se>
Message-ID: <200911281103.30392.ljmamoreira@gmail.com>

Yes, Robert, that does it! Thanks a lot!
Have a nice weekend!
Jose

On Saturday 28 November 2009 10:54:56 am Robert Johansson wrote:
> Hi!
> I want to process corresponding elements of two lists, sequentially. Call
> the
> lists list1 and list2, and assume they have equal lengths. I can do
> something
> like
> 
> for index in range(len(list1)):
>     process(list1[index], list2[index])
> 
> But I find it somehow rather ugly, because we generate yet another an list
> for
> the index, when we already have the two we want to process. I know we can
> use
> xrange, but still I find it awkward...
> 
> Instead of the above snippet, I am considering something like
> 
> while list1:
>     process(list1.pop(), list2.pop())
> 
> But this has the side effect of emptying both lists, which may not be
> convenient. Of course we can make backup copies of the lists if needed, but
> we
> are then recovering the previous method ugliness...
> 
> Maybe enumerate is what you are looking for?
> 
> list1=range(10)
> list2=range(10,20)
> for index, element in enumerate(list1):
>     print element, list2[index]
> 
> /Robert
> 

From robert.johansson at math.umu.se  Sat Nov 28 11:54:56 2009
From: robert.johansson at math.umu.se (Robert Johansson)
Date: Sat, 28 Nov 2009 11:54:56 +0100
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <200911280952.38453.ljmamoreira@gmail.com>
References: <200911280952.38453.ljmamoreira@gmail.com>
Message-ID: <000001ca7019$3f2541a0$bd6fc4e0$@johansson@math.umu.se>

Hi!
I want to process corresponding elements of two lists, sequentially. Call
the 
lists list1 and list2, and assume they have equal lengths. I can do
something 
like

for index in range(len(list1)):
    process(list1[index], list2[index])

But I find it somehow rather ugly, because we generate yet another an list
for 
the index, when we already have the two we want to process. I know we can
use 
xrange, but still I find it awkward...

Instead of the above snippet, I am considering something like

while list1:
    process(list1.pop(), list2.pop())

But this has the side effect of emptying both lists, which may not be 
convenient. Of course we can make backup copies of the lists if needed, but
we 
are then recovering the previous method ugliness...

Maybe enumerate is what you are looking for?

list1=range(10)
list2=range(10,20)
for index, element in enumerate(list1):
    print element, list2[index]

/Robert


From lie.1296 at gmail.com  Sat Nov 28 13:13:35 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Sat, 28 Nov 2009 23:13:35 +1100
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <200911281103.30392.ljmamoreira@gmail.com>
References: <200911280952.38453.ljmamoreira@gmail.com>	<000001ca7019$3f2541a0$bd6fc4e0$@johansson@math.umu.se>
	<200911281103.30392.ljmamoreira@gmail.com>
Message-ID: <her48h$qgi$1@ger.gmane.org>

On 11/28/2009 10:03 PM, Jose Amoreira wrote:
> Yes, Robert, that does it! Thanks a lot!
> Have a nice weekend!
> Jose

don't forget zip() built-in function:

for x, y in zip(list1, list2):
     print x, y


From davea at ieee.org  Sat Nov 28 13:59:04 2009
From: davea at ieee.org (Dave Angel)
Date: Sat, 28 Nov 2009 07:59:04 -0500
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <200911280952.38453.ljmamoreira@gmail.com>
References: <200911280952.38453.ljmamoreira@gmail.com>
Message-ID: <4B111E98.1030108@ieee.org>

Jose Amoreira wrote:
> Hi!
> I want to process corresponding elements of two lists, sequentially. Call the 
> lists list1 and list2, and assume they have equal lengths. I can do something 
> like
>
> for index in range(len(list1)):
>     process(list1[index], list2[index])
>
> But I find it somehow rather ugly, because we generate yet another an list for 
> the index, when we already have the two we want to process. I know we can use 
> xrange, but still I find it awkward...
>
> Instead of the above snippet, I am considering something like
>
> while list1:
>     process(list1.pop(), list2.pop())
>
> But this has the side effect of emptying both lists, which may not be 
> convenient. Of course we can make backup copies of the lists if needed, but we 
> are then recovering the previous method ugliness...
>
> Do you guys have any suggestions regarding this? Thanks
> Jose Amoreira
>
>   
(following is untested, and from memory, so I may not have it perfect.  
But it's close)

This is exactly what zip() is for.
       zip(list1, list2)
yields a new list that consists of tuples taken in sequence from those 
two lists.  So
      for item1, item2 in zip(list1, list2):
             process(item1, item2)

And if the lists are large, use  itertools.izip() which works the same, 
but produces an iterator.

Note that if the lists are not the same length, I think it stops when 
the shorter one ends.

DaveA


From waynejwerner at gmail.com  Sat Nov 28 14:09:14 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Sat, 28 Nov 2009 07:09:14 -0600
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <4B111E98.1030108@ieee.org>
References: <200911280952.38453.ljmamoreira@gmail.com>
	<4B111E98.1030108@ieee.org>
Message-ID: <333efb450911280509v46c59da8h504bda1867befa84@mail.gmail.com>

On Sat, Nov 28, 2009 at 6:59 AM, Dave Angel <davea at ieee.org> wrote:

>
> And if the lists are large, use  itertools.izip() which works the same, but
> produces an iterator.
>
> Note that if the lists are not the same length, I think it stops when the
> shorter one ends.
>

But you can use izip_longest:

import itertools
list1 = [1,2,3,4]
list2 = [1,2]

lIn [32]: for x, y in itertools.izip_longest(list1, list2):
   ....:     print x,y
   ....:
   ....:
1 1
2 2
3 None
4 None

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091128/0030d4c8/attachment.htm>

From robert.johansson at math.umu.se  Sat Nov 28 17:33:41 2009
From: robert.johansson at math.umu.se (Robert Johansson)
Date: Sat, 28 Nov 2009 17:33:41 +0100
Subject: [Tutor] glpk
Message-ID: <000d01ca7048$8bed2050$a3c760f0$@johansson@math.umu.se>

Hi, I would like to use the GNU linear programming kit, preferably with
Python and under Windows. After some googling I found python-glpk and
ctypes-glpk. The later seems to be an alternative but I get a feeling that
the former is the one most people recommend (also, I'm not sure that it will
work under windows). Is there anyone who can give me an advice or a pointer
to more information?    

 

/Robert

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091128/1a4bd9a7/attachment.htm>

From stefan at lsd.co.za  Sat Nov 28 17:56:29 2009
From: stefan at lsd.co.za (Stefan Lesicnik)
Date: Sat, 28 Nov 2009 18:56:29 +0200 (SAST)
Subject: [Tutor] socket timeout
In-Reply-To: <b65fbb130911270226m1459d151i75a94755386ffdd3@mail.gmail.com>
Message-ID: <1789406756.824.1259427389726.JavaMail.root@zimbra>


----- "Sander Sweers" <sander.sweers at gmail.com> wrote:

> 2009/11/27 Stefan Lesicnik <stefan at lsd.co.za>:
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > #s.setdefaulttimeout(10000)
> > s.connect((proxy,port))
> 
> I have never used socket but a quick look at the docs [1] my guess is
> that you should use use s.settimeout() [2].
> 
> The docs say that setdefaulttimeout [3] will only be applied to *new*
> socket object. You set the default after creating the socket object.
> 

Thanks Sander,

The seemd to fix it up.  Funny how something so simple can plague you for so long :)

Appreciate it.

stefan

From roadierich at googlemail.com  Sat Nov 28 18:33:58 2009
From: roadierich at googlemail.com (Rich Lovely)
Date: Sat, 28 Nov 2009 17:33:58 +0000
Subject: [Tutor] Iterating over two sequences in "parallel"
In-Reply-To: <333efb450911280509v46c59da8h504bda1867befa84@mail.gmail.com>
References: <200911280952.38453.ljmamoreira@gmail.com>
	<4B111E98.1030108@ieee.org>
	<333efb450911280509v46c59da8h504bda1867befa84@mail.gmail.com>
Message-ID: <f0b4202b0911280933w48865b30g20f1f8554219e310@mail.gmail.com>

2009/11/28 Wayne Werner <waynejwerner at gmail.com>:
>
>
> On Sat, Nov 28, 2009 at 6:59 AM, Dave Angel <davea at ieee.org> wrote:
>>
>> And if the lists are large, use ?itertools.izip() which works the same,
>> but produces an iterator.
>>
>> Note that if the lists are not the same length, I think it stops when the
>> shorter one ends.
>
> But you can use izip_longest:
> import itertools
> list1 = [1,2,3,4]
> list2 = [1,2]
> lIn [32]: for x, y in itertools.izip_longest(list1, list2):
> ?? ....: ? ? print x,y
> ?? ....:
> ?? ....:
> 1 1
> 2 2
> 3 None
> 4 None
> HTH,
> Wayne
>
> --
> To be considered stupid and to be told so is more painful than being called
> gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
> every vice, has found its defenders, its rhetoric, its ennoblement and
> exaltation, but stupidity hasn?t. - Primo Levi
>
> _______________________________________________
> Tutor maillist ?- ?Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

You can also pass two lists to map:

>>> l1 = range(4)
>>> l2 = range(2)
>>> def p(x, y):
...     print x, y
...
>>> map(p, l1, l2)
0 0
1 1
2 None
3 None
[None, None, None, None]
>>> def p(x, y):
...     return (x or 0) + (y or 0)
...
>>> map(p, l1, l2)
[0, 2, 2, 3]

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.

From lie.1296 at gmail.com  Sat Nov 28 19:26:30 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Sun, 29 Nov 2009 05:26:30 +1100
Subject: [Tutor] Python best practices
In-Reply-To: <373240271.474.1259309037554.JavaMail.root@zimbra>
References: <373240271.474.1259309037554.JavaMail.root@zimbra>
Message-ID: <herq3q$k1n$1@ger.gmane.org>

On 11/27/2009 7:03 PM, Stefan Lesicnik wrote:
> hihi!  (two in 1 day!)
>
> Is there a document, pep, wiki etc that defines best practice for python code?  (maybe its more generic).

PEP 8 is the official style guideline.

> I keep stumbling on things I think, it would be nice if someone mentioned this. Some things ive seen
>
> - keep you try blocks as small as possible, so you can isolate the error
> - functions should return one value (im not 100% of this one)

I 100% disagree or with this one.

> - declare variable names in a certain way (place?)
> - use the if __name__ == '__main__': main() convention

Python specific best practices, among the most important ones are:
- use new-style classes (derive from object)
- avoid "for x in range(len(list)):", when you need the index prefer 
enumerate
- if you can think of a functionality, it is very likely it exist 
somewhere in the stdlib or at least it exists as a 3rd party lib 
http://xkcd.com/353/
- @property and attributes over getter and setter
- don't assume the use a reference counting GC, close file properly or 
use with block
- ''.join() when building large string
- don't manipulate path names, use os.path
- when writing GUI program, IDLE may mess things up

More general non-python-specific:
- write unittest for any serious code, preferrably write unittest before 
writing code
- it's easier to optimize correct code, than the correct optimized code
- when using nested loop (implicit or explicit), be wary of possible 
quadratic (or worse) behavior.


From jm_hannon at yahoo.com  Sun Nov 29 00:15:36 2009
From: jm_hannon at yahoo.com (Michael Hannon)
Date: Sat, 28 Nov 2009 15:15:36 -0800 (PST)
Subject: [Tutor] Example of use of (?P<name>) and (?P=name) in Python
	regular expressions?
Message-ID: <401609.96855.qm@web38805.mail.mud.yahoo.com>

Greetings.  While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using:

    (?P<name>...)

and then refer to them using:

    (?P=name)

I was able to get this to work in the following, nonsensical, example:

    >>> x = 'Free Fri Fro From'
    >>> y = re.sub(r'(?P<test>\bFro\b)', r'Frodo (--matched from \g<test>)', x)
    >>> y
    'Free Fri Frodo (--matched from Fro) From'
    >>> 

But, as you can see, to refer to the match I used the "\g" notation (that I found some place on the web).

I wasn't able to find a way to use the "P=" syntax, and I wasn't able to find any working examples of this syntax on the web.

If you have a working example of the use of the "P=" syntax, will you please send it to me?

Thanks.

-- Mike



      

From mwalsh at mwalsh.org  Sun Nov 29 02:26:15 2009
From: mwalsh at mwalsh.org (Martin Walsh)
Date: Sat, 28 Nov 2009 19:26:15 -0600
Subject: [Tutor] Example of use of (?P<name>) and (?P=name) in Python
 regular expressions?
In-Reply-To: <401609.96855.qm@web38805.mail.mud.yahoo.com>
References: <401609.96855.qm@web38805.mail.mud.yahoo.com>
Message-ID: <4B11CDB7.8030506@mwalsh.org>

Michael Hannon wrote:
> Greetings.  While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using:
> 
>     (?P<name>...)
> 
> and then refer to them using:
> 
>     (?P=name)

I'm not sure you've got that quite right. IIUC, the (?P=name) syntax is
used to match a previously defined group, "in the regular expression
itself." (http://docs.python.org/library/re.html)

<snip>

x = 'Free Fri Fro Fro From'
y = re.sub(
    r'(?P<test>Fro) (?P=test)',
    r'Frodo (--matched from \g<test>, twice in a row)', x
)
# y == 'Free Fri Frodo (--matched from Fro, twice in a row) From'

> But, as you can see, to refer to the match I used the "\g" notation (that I found some place on the web).

The \g notation is appropriate for re.sub.

HTH,
Marty


From kent37 at tds.net  Sun Nov 29 02:58:54 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sat, 28 Nov 2009 20:58:54 -0500
Subject: [Tutor] Example of use of (?P<name>) and (?P=name) in Python
	regular expressions?
In-Reply-To: <401609.96855.qm@web38805.mail.mud.yahoo.com>
References: <401609.96855.qm@web38805.mail.mud.yahoo.com>
Message-ID: <1c2a2c590911281758n45a68513v78c7520a3b837@mail.gmail.com>

On Sat, Nov 28, 2009 at 6:15 PM, Michael Hannon <jm_hannon at yahoo.com> wrote:
> Greetings. ?While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using:
>
> ? ?(?P<name>...)
>
> and then refer to them using:
>
> ? ?(?P=name)
>
> I was able to get this to work in the following, nonsensical, example:
>
> ? ?>>> x = 'Free Fri Fro From'
> ? ?>>> y = re.sub(r'(?P<test>\bFro\b)', r'Frodo (--matched from \g<test>)', x)
> ? ?>>> y
> ? ?'Free Fri Frodo (--matched from Fro) From'
> ? ?>>>
>
> But, as you can see, to refer to the match I used the "\g" notation (that I found some place on the web).

?P= is a matching pattern that you use within the regex to match a
previously seen (and named) pattern. \g is a substitution pattern used
in the replacement text. For (a not very interesting) example, to
replace from 'Fro' to the last 'Fro':

In [2]: x = 'Free Fri Fro From'

In [3]: re.sub(r'(?P<test>Fro).*(?P=test)', 'xx', x)
Out[3]: 'Free Fri xxm'

For aslightly more interesting example, here (?P<test>Fr\w\w) matches
the first 'Free' so (?P=test) matches the second 'Free':
In [6]: x = 'Free Fri Free From'

In [7]: re.sub(r'(?P<test>Fr\w\w).*(?P=test)', 'xx', x)
Out[7]: 'xx From'

Kent

From denis.spir at free.fr  Sun Nov 29 10:59:01 2009
From: denis.spir at free.fr (spir)
Date: Sun, 29 Nov 2009 10:59:01 +0100
Subject: [Tutor] Python best practices
In-Reply-To: <herq3q$k1n$1@ger.gmane.org>
References: <373240271.474.1259309037554.JavaMail.root@zimbra>
	<herq3q$k1n$1@ger.gmane.org>
Message-ID: <20091129105901.6885e9e1@o>

Lie Ryan <lie.1296 at gmail.com> dixit:

> > - functions should return one value (im not 100% of this one)  
> 
> I 100% disagree or with this one.

Could you explain this bit, Lie? I'm very interested.
I use multiple-value result myself, for it's so practicle in given cases. But it makes me uneasy; also have the impression (why?) it reveals wrong design.

About the same feeling as when I need a function both to have an effect (eg assignment outside the func scope) and return a value. I call this "action vs question" ; it more or less matches Bertrand Meyer's "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs function".
In the latter case I'm 99% sure the design smells but sometines I cannot find proper code reorganisation.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From alan.gauld at btinternet.com  Mon Nov 30 01:57:23 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 30 Nov 2009 00:57:23 -0000
Subject: [Tutor] Python best practices
References: <373240271.474.1259309037554.JavaMail.root@zimbra><herq3q$k1n$1@ger.gmane.org>
	<20091129105901.6885e9e1@o>
Message-ID: <hev59n$8ci$1@ger.gmane.org>

"spir" <denis.spir at free.fr> wrote

>> > - functions should return one value (im not 100% of this one)
>>
>> I 100% disagree or with this one.
>
> Could you explain this bit, Lie? I'm very interested.
> I use multiple-value result myself, for it's so practicle in given cases.

My take on this is that in Python its OK to return multiple values if it
is as a tuple - because a tuple is really a single value. Its like 
returning
a record in Pascal or a struct in C or a List in Lisp...

> But it makes me uneasy; also have the impression (why?) it
> reveals wrong design.

Its better than....

> a function both to have an effect (eg assignment outside the func scope) 
> and
> return a value.

Side effects in functions are nearly always bad news and are always
avoidable if you can return multiple values (or pass arguments by 
reference).

> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs 
> function".

You can have Command/Query separation without having side-effects.
A Query is a procedure or function that doesn't change anything but just
returns a result. A Command changes something, but it can be the thing
passed to it - or if a method of a class the internal data of the class.
Again a command can be a function or a procedure. Those are separate
concepts. (I very rarely write procedures in real programs - there is 
nearly
always something useful you can return - and in an object that's usually
a minimum of self! (Returning self is the default idiom in Smalltalk - it
allows chaining of methods)

HTH,

Alan G. 



From kent37 at tds.net  Mon Nov 30 02:22:35 2009
From: kent37 at tds.net (Kent Johnson)
Date: Sun, 29 Nov 2009 20:22:35 -0500
Subject: [Tutor] Python best practices
In-Reply-To: <herq3q$k1n$1@ger.gmane.org>
References: <373240271.474.1259309037554.JavaMail.root@zimbra>
	<herq3q$k1n$1@ger.gmane.org>
Message-ID: <1c2a2c590911291722t7b8f345bw1d7b4854ed3ececc@mail.gmail.com>

On Sat, Nov 28, 2009 at 1:26 PM, Lie Ryan <lie.1296 at gmail.com> wrote:
> On 11/27/2009 7:03 PM, Stefan Lesicnik wrote:

>> - functions should return one value (im not 100% of this one)
>
> I 100% disagree or with this one.

Me too. Actually I think it is meaningless - every Python function
returns one value, an instance of some class. Each of these functions
returns one value - a list, a tuple and a Foo instance:

def make_list():
  return [0, 1]

def make_tuple():
  return 0, 1

class Foo(object):
  def __init__(self, a, b):
    self.a = a
    self.b = b

def make_Foo():
  return Foo(0, 1)


BTW when I first saw this rule I mis-read it as "Functions should have
a single point of return" which is fairly common advice that I also
disagree with.

Kent

From lie.1296 at gmail.com  Mon Nov 30 06:42:28 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Mon, 30 Nov 2009 16:42:28 +1100
Subject: [Tutor] Python best practices
In-Reply-To: <20091129105901.6885e9e1@o>
References: <373240271.474.1259309037554.JavaMail.root@zimbra>	<herq3q$k1n$1@ger.gmane.org>
	<20091129105901.6885e9e1@o>
Message-ID: <hevm3b$8gh$1@ger.gmane.org>

On 11/29/2009 8:59 PM, spir wrote:
> Lie Ryan<lie.1296 at gmail.com>  dixit:
>
>>> - functions should return one value (im not 100% of this one)
>>
>> I 100% disagree or with this one.
>
> Could you explain this bit, Lie? I'm very interested.
> I use multiple-value result myself, for it's so practicle in given
 > cases. But it makes me uneasy; also have the impression (why?) it
 > reveals wrong design.

I guess this particular "advice" comes from languages like C/C++ where 
returning a struct means copying the whole struct, which could be a very 
expensive. In python, returning tuple is cheap; it's just copying 
references. Don't hesitate to return multiple values, whenever it is 
meaningful/useful (either tuple or list or dict or ...).


From fomcl at yahoo.com  Mon Nov 30 10:58:21 2009
From: fomcl at yahoo.com (Albert-Jan Roskam)
Date: Mon, 30 Nov 2009 01:58:21 -0800 (PST)
Subject: [Tutor] Python best practices
In-Reply-To: <hev59n$8ci$1@ger.gmane.org>
Message-ID: <697419.96737.qm@web110703.mail.gq1.yahoo.com>

I'm currently reading the book "Code Complete" (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of religion. There is no one best way to do it.
?
Re: functions, the author recommends that they have one purpose and one purpose only, and that a function returns only one result.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 11/30/09, Alan Gauld <alan.gauld at btinternet.com> wrote:


From: Alan Gauld <alan.gauld at btinternet.com>
Subject: Re: [Tutor] Python best practices
To: tutor at python.org
Date: Monday, November 30, 2009, 1:57 AM


"spir" <denis.spir at free.fr> wrote

>> > - functions should return one value (im not 100% of this one)
>> 
>> I 100% disagree or with this one.
> 
> Could you explain this bit, Lie? I'm very interested.
> I use multiple-value result myself, for it's so practicle in given cases.

My take on this is that in Python its OK to return multiple values if it
is as a tuple - because a tuple is really a single value. Its like returning
a record in Pascal or a struct in C or a List in Lisp...

> But it makes me uneasy; also have the impression (why?) it
> reveals wrong design.

Its better than....

> a function both to have an effect (eg assignment outside the func scope) and
> return a value.

Side effects in functions are nearly always bad news and are always
avoidable if you can return multiple values (or pass arguments by reference).

> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs function".

You can have Command/Query separation without having side-effects.
A Query is a procedure or function that doesn't change anything but just
returns a result. A Command changes something, but it can be the thing
passed to it - or if a method of a class the internal data of the class.
Again a command can be a function or a procedure. Those are separate
concepts. (I very rarely write procedures in real programs - there is nearly
always something useful you can return - and in an object that's usually
a minimum of self! (Returning self is the default idiom in Smalltalk - it
allows chaining of methods)

HTH,

Alan G. 

_______________________________________________
Tutor maillist? -? Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/7059ec93/attachment.htm>

From alan.gauld at btinternet.com  Mon Nov 30 11:06:14 2009
From: alan.gauld at btinternet.com (ALAN GAULD)
Date: Mon, 30 Nov 2009 10:06:14 +0000 (GMT)
Subject: [Tutor] Python best practices
In-Reply-To: <697419.96737.qm@web110703.mail.gq1.yahoo.com>
References: <697419.96737.qm@web110703.mail.gq1.yahoo.com>
Message-ID: <42453.82695.qm@web86706.mail.ird.yahoo.com>

Code Complete by Steve McConnell is an excellent book
and I agree with almost everything in it. Including 
the advice about functions.

But as I said earlier the concept of multiple return 
values in Python is similar to returning a struct in C.
The key thing is that the function has a single purpose 
so if returning multiple values or a struct the values 
within that group should be related to the single purpose

In other words don't write a function called

def getLengthOrContent(dataStream, flag)
    data = readData(dataAStream)      
    if flag:
       return len(data)
    else: 
       return data

Thats what I think is meant by bad practice in returning 
multiple values. The function returns two completely different 
things depending on some input flag.
 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/





________________________________
From: Albert-Jan Roskam <fomcl at yahoo.com>
To: tutor at python.org; Alan Gauld <alan.gauld at btinternet.com>
Sent: Monday, 30 November, 2009 9:58:21
Subject: Re: [Tutor] Python best practices


I'm currently reading the book "Code Complete" (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of religion. There is no one best way to do it.
 
Re: functions, the author recommends that they have one purpose and one purpose only, and that a function returns only one result.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 11/30/09, Alan Gauld <alan.gauld at btinternet.com> wrote:


>From: Alan Gauld <alan.gauld at btinternet.com>
>Subject: Re: [Tutor] Python best practices
>To: tutor at python.org
>Date: Monday, November 30, 2009, 1:57 AM
>
>
>"spir" <denis.spir at free.fr> wrote
>
>>> > - functions should return one value (im not 100% of this one)
>>> 
>>> I 100% disagree or with this one.
>> 
>> Could you explain this bit, Lie? I'm very interested.
>> I use multiple-value result myself, for it's so practicle in given cases.
>
>My take on this is that in Python its OK to return multiple values if it
>is as a tuple - because a tuple is really a single value. Its like returning
>a record in Pascal or a struct in C or a List in Lisp...
>
>> But it makes me uneasy; also have the impression (why?) it
>> reveals wrong design.
>
>Its better than....
>
>> a function both to have an effect (eg assignment outside the func scope) and
>> return a value.
>
>Side effects in functions are
> nearly always bad news and are always
>avoidable if you can return multiple values (or pass arguments by reference).
>
>> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs function".
>
>You can have Command/Query separation without having side-effects.
>A Query is a procedure or function that doesn't change anything but just
>returns a result. A Command changes something, but it can be the thing
>passed to it - or if a method of a class the internal data of the class.
>Again a command can be a function or a procedure. Those are separate
>concepts. (I very rarely write procedures in real programs - there is nearly
>always something useful you can return - and in an object that's usually
>a minimum of self! (Returning self is the default idiom in Smalltalk - it
>allows chaining of methods)
>
>HTH,
>
>Alan G. 
>
>_______________________________________________
>Tutor maillist  - 
> Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/b4fd4aba/attachment-0001.htm>

From denis.spir at free.fr  Mon Nov 30 11:24:45 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 11:24:45 +0100
Subject: [Tutor] python closures
Message-ID: <20091130112445.7451eaf6@o>

Hello,

Below startup definitions:

x = 1

def f():
  n = 1
  def g0(a):
    print (x + n + a) 
  return g0


I'm surprised the snippet below works as expected (py 2.6) without any trick:

g = f()
g(1)	# --> 3

This means a (real) closure is built for g0, or what?
Thought I would need instead to use the old trick of pseudo default-parameters:

def f():
  n = 1
  def g0(a, n=n, x=x):
    print (x + n + a) 
  return g0

to let the inner func g0 "remember" outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?

The bit below also works:

x = 2
...
g(1)	# --> 4

which seems to indicate python really embeds "symbolic references" (*) to outer *variables*, when creating a closure for g0. Not "pointer references" (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.
Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?

Denis

(*) by name, indirect access, second table lookup
(**) by address, direct access, no second lookup
________________________________

la vita e estrany

http://spir.wikidot.com/


From waynejwerner at gmail.com  Mon Nov 30 13:21:54 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 30 Nov 2009 06:21:54 -0600
Subject: [Tutor] python closures
In-Reply-To: <20091130112445.7451eaf6@o>
References: <20091130112445.7451eaf6@o>
Message-ID: <333efb450911300421y64468806k157fe291dd782bf3@mail.gmail.com>

On Mon, Nov 30, 2009 at 4:24 AM, spir <denis.spir at free.fr> wrote:

> which seems to indicate python really embeds "symbolic references" (*) to
> outer *variables*, when creating a closure for g0. Not "pointer references"
> (**), otherwise the replacement of x would not be seen by the closure --like
> in the case of default-parameter.
> Actually, I find this _Bad_. Obviously, the func's behaviour and result
> depend on arbitrary external values (referentially opaque). What do you
> think?
>

I'm not sure *why*/how this behaviour really works, other than it treats x
as a global variable... and probably treats n as something similar.

I don't know how bad I find it - you should be declaring the variables
you're planning to use in your function anyway... I'm sure there's *some*
case that it would end out problematic, but I can't think of one ATM.

-Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/fe56003f/attachment.htm>

From waynejwerner at gmail.com  Mon Nov 30 13:29:14 2009
From: waynejwerner at gmail.com (Wayne Werner)
Date: Mon, 30 Nov 2009 06:29:14 -0600
Subject: [Tutor] Python best practices
In-Reply-To: <42453.82695.qm@web86706.mail.ird.yahoo.com>
References: <697419.96737.qm@web110703.mail.gq1.yahoo.com> 
	<42453.82695.qm@web86706.mail.ird.yahoo.com>
Message-ID: <333efb450911300429j3903f29bn5493527b8243be9@mail.gmail.com>

On Mon, Nov 30, 2009 at 4:06 AM, ALAN GAULD <alan.gauld at btinternet.com>wrote:

>
> Thats what I think is meant by bad practice in returning
> multiple values. The function returns two completely different
> things depending on some input flag.
>

Now that's something I definitely agree with! I can't think of a single case
where that would make sense or be good practice. It would just create a ton
more work for the programmer/maintainer and obfuscate the code.

OTOH I can think of several reasons/times I've returned multiple related
values - for instance I was just writing a function for homework that found
the continuous subsequence with the greatest total that returned the start,
end, and total (more for my benefit than any other requirement... still, in
terms of time complexity and program complexity, it makes much more sense
than trying to break it up as "getMaxTotal" "getstart" and "getend"
functions - that would just be ridiculous!)

-Wayne


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn?t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/30d610b6/attachment.htm>

From bibsmendez at gmail.com  Mon Nov 30 14:00:03 2009
From: bibsmendez at gmail.com (biboy mendz)
Date: Mon, 30 Nov 2009 16:00:03 +0300
Subject: [Tutor] Equivalent exception of os.path.exists()
Message-ID: <4B13C1D3.6060305@gmail.com>

http://pastebin.ca/1693849

This is end-of-chapter3 exercise of the book Core Python Programming.

I'm reading/searching in the book and other materials but is 
unsuccessful. There are at least 50 exceptions listed but I can't find 
anything close.

I commented out my modified script to just what *should* be applicable. 
Can you please point me to the right direction? TIA.


-- 
Regards,
bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 ????? - kde-full - (200907141427) ]
www.sidux.com


From kent37 at tds.net  Mon Nov 30 16:16:05 2009
From: kent37 at tds.net (Kent Johnson)
Date: Mon, 30 Nov 2009 10:16:05 -0500
Subject: [Tutor] python closures
In-Reply-To: <20091130112445.7451eaf6@o>
References: <20091130112445.7451eaf6@o>
Message-ID: <1c2a2c590911300716r308918beic8ac796279f1f01f@mail.gmail.com>

On Mon, Nov 30, 2009 at 5:24 AM, spir <denis.spir at free.fr> wrote:
> Hello,
>
> Below startup definitions:
>
> x = 1
>
> def f():
> ?n = 1
> ?def g0(a):
> ? ?print (x + n + a)
> ?return g0
>
>
> I'm surprised the snippet below works as expected (py 2.6) without any trick:
>
> g = f()
> g(1) ? ?# --> 3
>
> This means a (real) closure is built for g0, or what?

Yes, Python has had real (read-only) closures since 2.1 when nested
scopes where introduced:
http://docs.python.org/dev/whatsnew/2.1.html#pep-227-nested-scopes

Python 3 introduces the 'nonlocal' keyword which allows assignment to
names in enclosing scopes, presumably ending at last the debate about
whether Python has 'real' closures:
http://www.python.org/dev/peps/pep-3104/

> Thought I would need instead to use the old trick of pseudo default-parameters:
>
> def f():
> ?n = 1
> ?def g0(a, n=n, x=x):
> ? ?print (x + n + a)
> ?return g0
>
> to let the inner func g0 "remember" outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?

That has not been needed since 2.1 though it is still useful when
closures are created in a loop (because closures are kind of late
bound - I'm not sure the exact technical explanation):
In [13]: def f():
   ....:     l = []
   ....:     for i in range(3):
   ....:         def g():
   ....:             print i
   ....:         l.append(g)
   ....:     return l

In [14]: for g in f(): g()
   ....:
2
2
2


But with the default argument it captures the value of i each time
through the loop:
In [15]: def f():
   ....:     l = []
   ....:     for i in range(3):
   ....:         def g(i=i):
   ....:             print i
   ....:         l.append(g)
   ....:     return l

In [16]: for g in f(): g()
   ....:
0
1
2

> The bit below also works:
>
> x = 2
> ...
> g(1) ? ?# --> 4
>
> which seems to indicate python really embeds "symbolic references" (*) to outer *variables*, when creating a closure for g0. Not "pointer references" (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.

In your first definition of f(), x is global and not included in the
closure. This is the same behaviour you would have in older versions.
In your second definition of f(), x is bound to a default argument and
changing the global x doesn't change the result of g().

> Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?

That is always the case when a function accesses globals. Globals are
_Bad_, yes.

Kent

From stefan_ml at behnel.de  Mon Nov 30 16:17:26 2009
From: stefan_ml at behnel.de (Stefan Behnel)
Date: Mon, 30 Nov 2009 16:17:26 +0100
Subject: [Tutor] python closures
In-Reply-To: <20091130112445.7451eaf6@o>
References: <20091130112445.7451eaf6@o>
Message-ID: <hf0nm7$dbj$1@ger.gmane.org>

spir, 30.11.2009 11:24:
> Below startup definitions:
> 
> x = 1
> 
> def f():
>   n = 1
>   def g0(a):
>     print (x + n + a) 
>   return g0
> 
> I'm surprised the snippet below works as expected (py 2.6) without any trick:
> 
> g = f()
> g(1)	# --> 3
> 
> This means a (real) closure is built for g0, or what?

Yes.


> Thought I would need instead to use the old trick of pseudo default-parameters:
> 
> def f():
>   n = 1
>   def g0(a, n=n, x=x):
>     print (x + n + a) 
>   return g0
> 
> to let the inner func g0 "remember" outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?

Different use case. The above uses default arguments for n and x that can
be overridden by callers, but that have a value if callers do not pass
them. Values in closures can only be modified by the owner(s) of the names
that participate in the closure (i.e. the function f in this case).


> The bit below also works:
> 
> x = 2
> ...
> g(1)	# --> 4

x is not in the closure, it's a global name.


> the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?

It's a matter of how you use it. Closures make a lot of sense for many
cases, but there are certainly also cases where using them feels like a
goto - just like global variables.

Stefan


From mail at timgolden.me.uk  Mon Nov 30 16:27:48 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Mon, 30 Nov 2009 15:27:48 +0000
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B13C1D3.6060305@gmail.com>
References: <4B13C1D3.6060305@gmail.com>
Message-ID: <4B13E474.8020007@timgolden.me.uk>

biboy mendz wrote:
> http://pastebin.ca/1693849
> 
> This is end-of-chapter3 exercise of the book Core Python Programming.
> 
> I'm reading/searching in the book and other materials but is 
> unsuccessful. There are at least 50 exceptions listed but I can't find 
> anything close.
> 
> I commented out my modified script to just what *should* be applicable. 
> Can you please point me to the right direction? TIA.


Ummm.. You're not actually trying to open the file.
raw_input just asks the user for a string. But it's
just a string. You could be going to use it for
anything. Try something like:

try:
  open (fname).close ()
except IOError, e:
  print "blah"


TJG

From lie.1296 at gmail.com  Mon Nov 30 16:24:33 2009
From: lie.1296 at gmail.com (Lie Ryan)
Date: Tue, 01 Dec 2009 02:24:33 +1100
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B13C1D3.6060305@gmail.com>
References: <4B13C1D3.6060305@gmail.com>
Message-ID: <hf0o6s$f47$1@ger.gmane.org>

On 12/1/2009 12:00 AM, biboy mendz wrote:
> http://pastebin.ca/1693849
>
> This is end-of-chapter3 exercise of the book Core Python Programming.
>
> I'm reading/searching in the book and other materials but is
> unsuccessful. There are at least 50 exceptions listed but I can't find
> anything close.
>
> I commented out my modified script to just what *should* be applicable.
> Can you please point me to the right direction? TIA.

You're trying to take exception from raw_input(); raw_input() merely 
receive input from the stdin. raw_input() doesn't have anything to do 
with the file. You should expect the exception to come from the open() 
function instead.

However, on a little bit of note; open('...', 'w') will (almost) always 
work since if the file doesn't exists, python will simply create it. The 
only time it wouldn't work is if you've have file edit permission but 
not create new file permission.


From hugo.yoshi at gmail.com  Mon Nov 30 17:19:20 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Mon, 30 Nov 2009 17:19:20 +0100
Subject: [Tutor] python closures
In-Reply-To: <20091130112445.7451eaf6@o>
References: <20091130112445.7451eaf6@o>
Message-ID: <29179d160911300819w50f3da6fo508d0162700edc3b@mail.gmail.com>

On Mon, Nov 30, 2009 at 11:24 AM, spir <denis.spir at free.fr> wrote:
> Hello,
>
> Below startup definitions:
>
> x = 1
>
> def f():
> ?n = 1
> ?def g0(a):
> ? ?print (x + n + a)
> ?return g0
>
>
> I'm surprised the snippet below works as expected (py 2.6) without any trick:
>
> g = f()
> g(1) ? ?# --> 3
>
> This means a (real) closure is built for g0, or what?
> Thought I would need instead to use the old trick of pseudo default-parameters:
>
> def f():
> ?n = 1
> ?def g0(a, n=n, x=x):
> ? ?print (x + n + a)
> ?return g0
>
> to let the inner func g0 "remember" outer values. Why is this idiom used, then? Has something changed, or do I miss a relevant point?

A real closure is indeed created, but you are missing something.
Consider this python session:

 >>> x = 0
 >>> def f():
		x = x + 1

 >>> f()

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    f()
  File "<pyshell#26>", line 2, in f
    x = x + 1
UnboundLocalError: local variable 'x' referenced before assignment

The python docs offers some insight:

The execution of a function introduces a new symbol table used for the
local variables of the function. More precisely, all variable
assignments in a function store the value in the local symbol table;
whereas variable references first look in the local symbol table, then
in the local symbol tables of enclosing functions, then in the global
symbol table, and finally in the table of built-in names. Thus, global
variables cannot be directly assigned a value within a function
(unless named in a global statement), although they may be referenced.

( from http://docs.python.org/tutorial/controlflow.html#defining-functions )

In short, the problem is that writing "x =" will create a new
(unbound) local name for x, hiding the global one. The following
reference to x will find the local unbound variable and start
complaining.

If you wanted to create a local variable, your idiom is the fix you
want. If you want to modify the global one, use the 'global x'
statement to tell the interpreter that explicitly.

> The bit below also works:
>
> x = 2
> ...
> g(1) ? ?# --> 4
>
> which seems to indicate python really embeds "symbolic references" (*) to outer *variables*, when creating a closure for g0. Not "pointer references" (**), otherwise the replacement of x would not be seen by the closure --like in the case of default-parameter.
> Actually, I find this _Bad_. Obviously, the func's behaviour and result depend on arbitrary external values (referentially opaque). What do you think?

I don't quite understand the point you're trying to make. The code
you're showing above seems like what is 'proper' behaviour to me. Can
you show an example demonstrating why it is bad?

Hugo

From hugo.yoshi at gmail.com  Mon Nov 30 17:38:57 2009
From: hugo.yoshi at gmail.com (Hugo Arts)
Date: Mon, 30 Nov 2009 17:38:57 +0100
Subject: [Tutor] python closures
In-Reply-To: <1c2a2c590911300716r308918beic8ac796279f1f01f@mail.gmail.com>
References: <20091130112445.7451eaf6@o>
	<1c2a2c590911300716r308918beic8ac796279f1f01f@mail.gmail.com>
Message-ID: <29179d160911300838v4e7020d0k5b4d1acb8ce2b1b9@mail.gmail.com>

On Mon, Nov 30, 2009 at 4:16 PM, Kent Johnson <kent37 at tds.net> wrote:

> That has not been needed since 2.1 though it is still useful when
> closures are created in a loop (because closures are kind of late
> bound - I'm not sure the exact technical explanation):
> In [13]: def f():
> ? ....: ? ? l = []
> ? ....: ? ? for i in range(3):
> ? ....: ? ? ? ? def g():
> ? ....: ? ? ? ? ? ? print i
> ? ....: ? ? ? ? l.append(g)
> ? ....: ? ? return l
>
> In [14]: for g in f(): g()
> ? ....:
> 2
> 2
> 2
>

This doesn't really have anything to do with closures specifically.
Variable lookup is done at runtime, not definition time. So when these
lookups for i are performed the value of i is indeed 2.

This wouldn't happen if closures used pointer references (as spir
called them). Then again, even with pointer references modifications
to mutable variables are still visible inside a closure.

Hugo

From denis.spir at free.fr  Mon Nov 30 17:34:34 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 17:34:34 +0100
Subject: [Tutor] Python best practices
In-Reply-To: <42453.82695.qm@web86706.mail.ird.yahoo.com>
References: <697419.96737.qm@web110703.mail.gq1.yahoo.com>
	<42453.82695.qm@web86706.mail.ird.yahoo.com>
Message-ID: <20091130173434.29ebf75f@o>

ALAN GAULD <alan.gauld at btinternet.com> dixit:

> But as I said earlier the concept of multiple return 
> values in Python is similar to returning a struct in C.
> The key thing is that the function has a single purpose 
> so if returning multiple values or a struct the values 
> within that group should be related to the single purpose

It's not enough I guess that returned values are "related to the [func's] single purpose".

The cases when I do not mind returning multiple values are precisely the ones when I could build the equivalent of a struct / record / (named) tuple / object. One that makes sense. In other words, when the set of values together describe *one single thing*. In some cases it's not worth building a "struct" if the caller will then just unpack it for further process.
Eg it can makes sense to 
   return (position, color)
instead of:
   return Point(position, color)
(provided there is a Point type defined)

But there are cases when I feel uneasy returning multiple values even if each one is clearly related to the (single) purpose the the func. The typical case for me is a match func that needs together:
1. tell about match outcome (success/failure)
2. move ahead the pointer position in source
3. return node (match result)
There are indeed numerous ways to do this, even to avoid returning more than one thing (have tried several). My latest method is to (1) raise exception in case of failure (2) have a source object that itself holds its pointer (3) then the node only is returned.
But these means are tricks (esp. unsing exception, which is _always_ a trick) to hide multiple actions and returns. 
Also note that a stuct composed of outcome+position+node does not make any sense (try to name it! ;-).
I guess the only proper solution is to build a real state machine (on which parse data is stored), but this makes parse tree construction extremely complicated in the general case, compared to having nodes directly returned by match funcs.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From eike.welk at gmx.net  Mon Nov 30 18:09:38 2009
From: eike.welk at gmx.net (Eike Welk)
Date: Mon, 30 Nov 2009 18:09:38 +0100
Subject: [Tutor] x is a global variable
In-Reply-To: <20091130112445.7451eaf6@o>
References: <20091130112445.7451eaf6@o>
Message-ID: <200911301809.39269.eike.welk@gmx.net>

Hello Spir!

On Monday 30 November 2009, spir wrote:
> which seems to indicate python really embeds "symbolic references"
> (*) to outer *variables*, when creating a closure for g0. Not
> "pointer references" (**), otherwise the replacement of x would not
> be seen by the closure --like in the case of default-parameter.
> Actually, I find this _Bad_. Obviously, the func's behaviour and
> result depend on arbitrary external values (referentially opaque).

If I understand you right, you are proposing that the inner function 
g0 should get a separate copy of the global namespace. The copy should 
be done when the function is defined. 

When this would be implemented, inner functions (g0) would be treated 
very differently from outer functions (f), with respect to global 
variables. I would not like this different treatment. When a global 
variable is changed all functions should see the change. 

I think currently there are three 'containers' where functions can 
find variables in Python:
- local variables 
- the closure, which is empty in outer functions (f.func_closure)
- global variables, this is probably f.__module__.__dict__.

The exact semantics of closures seem to be a bit tricky. Below is some 
behavior I find surprising. I had expected that a local variable 'i' 
is created in function 'inc'. Instead the function fails with an 
error. (Producing the error is IMHO surprising, but sane behavior.):

In [25]:def clos():
  i = 0
  def inc():
    i = i + 1
    print i
  def pri():
    print i
  return inc, pri

In [33]:inc, pri = clos()

In [34]:pri()
0

In [35]:inc()
-------------------------------------------------------------------
UnboundLocalError                 Traceback (most recent call last)

/home/eike/<ipython console> in <module>()
/home/eike/<ipython console> in inc()
UnboundLocalError: local variable 'i' referenced before assignment


---
Eike.


From denis.spir at free.fr  Mon Nov 30 18:08:18 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 18:08:18 +0100
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B13C1D3.6060305@gmail.com>
References: <4B13C1D3.6060305@gmail.com>
Message-ID: <20091130180818.10edde94@o>

biboy mendz <bibsmendez at gmail.com> dixit:

> http://pastebin.ca/1693849
> 
> This is end-of-chapter3 exercise of the book Core Python Programming.
> 
> I'm reading/searching in the book and other materials but is 
> unsuccessful. There are at least 50 exceptions listed but I can't find 
> anything close.
> 
> I commented out my modified script to just what *should* be applicable. 
> Can you please point me to the right direction? TIA.
> 
> 

What is your question?
If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:

  print os.path.exists("foo".bar)
  ==> False

The output beeing a logical value, there is no failure.
A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:

  f = file("foo.bar")
  ==>
Traceback (most recent call last):
  File "__essai__.py", line 10, in <module>
    f = file("foo.bar")
IOError: [Errno 2] No such file or directory: 'foo.bar'

In this case, the type is IOError.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From eike.welk at gmx.net  Mon Nov 30 18:36:52 2009
From: eike.welk at gmx.net (Eike Welk)
Date: Mon, 30 Nov 2009 18:36:52 +0100
Subject: [Tutor] python closures
In-Reply-To: <29179d160911300819w50f3da6fo508d0162700edc3b@mail.gmail.com>
References: <20091130112445.7451eaf6@o>
	<29179d160911300819w50f3da6fo508d0162700edc3b@mail.gmail.com>
Message-ID: <200911301836.52847.eike.welk@gmx.net>

On Monday 30 November 2009, Hugo Arts wrote:
> Consider this python session:
>  >>> x = 0
>  >>> def f():
>
> 		x = x + 1
>
>  >>> f()
>
> Traceback (most recent call last):
>   File "<pyshell#27>", line 1, in <module>
>     f()
>   File "<pyshell#26>", line 2, in f
>     x = x + 1
> UnboundLocalError: local variable 'x' referenced before assignment
Ah... what a pity I didn't try this. I used similar code in my 
response to Spir and thought it would be somehow connected to 
closures. Sending nonsense statements to the list again... 

>
> The python docs offers some insight:
>
> The execution of a function introduces a new symbol table used for
> the local variables of the function. More precisely, all variable
> assignments in a function store the value in the local symbol
> table; whereas variable references first look in the local symbol
> table, then in the local symbol tables of enclosing functions, then
> in the global symbol table, and finally in the table of built-in
> names. Thus, global variables cannot be directly assigned a value
> within a function (unless named in a global statement), although
> they may be referenced.
>
> ( from
> http://docs.python.org/tutorial/controlflow.html#defining-functions
> )
>
> In short, the problem is that writing "x =" will create a new
> (unbound) local name for x, hiding the global one. The following
> reference to x will find the local unbound variable and start
> complaining.
I find that behavior quite counterintuitive. I expect:
1. 'x' is looked up, global variable 'x' is found; 
2. the addition is performed; 
3. a local name 'x' is created and bound to the result of the 
addition.

Producing the error is not insane. Because referencing a global 
variable and shadowing it in the same statement is bad style. 


Eike.

From alan.gauld at btinternet.com  Mon Nov 30 18:38:12 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 30 Nov 2009 17:38:12 -0000
Subject: [Tutor] python closures
References: <20091130112445.7451eaf6@o>
Message-ID: <hf0vu9$c0u$1@ger.gmane.org>


"spir" <denis.spir at free.fr> wrote

> x = 1
>
> def f():
>  n = 1
>  def g0(a):
>    print (x + n + a)
>  return g0
>
>
> I'm surprised the snippet below works as expected (py 2.6) without any 
> trick:

I'm not sure how else it could work.
x is a global name so the function must reference it.
n is a local name so it musdt evaluate it (it will disappear otherwise)
a is a parameter delivered at run time.

> This means a (real) closure is built for g0, or what?
> Thought I would need instead to use the old trick of pseudo 
> default-parameters:
>
> def f():
>  n = 1
>  def g0(a, n=n, x=x):
>    print (x + n + a)
>  return g0

I did wonder if you would need n=n but I didn't think you would need x=x.

Its an interesting example and I confess I don't fully understand how 
Python's
naming/reference rules are working here.

> to let the inner func g0 "remember" outer values.
> Why is this idiom used, then? Has something changed, or do I miss
> a relevant point?

I thought you might need to do it if n had been a parameter of f()... but
having tried it no, it works as above.

I look forward to the explanation.

Alan G. 



From faisal.moledina at gmail.com  Mon Nov 30 19:47:32 2009
From: faisal.moledina at gmail.com (Faisal Moledina)
Date: Mon, 30 Nov 2009 13:47:32 -0500
Subject: [Tutor] numerical simulation + SQLite
Message-ID: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com>

Hey everyone,

I have a general issue that I'd like to discuss. I'm using Python to
run a numerical simulation where at each time step, I run a number of
operations and store the results before moving to the next timestep.
At first, I used a list to store a bunch of class instances, each of
which contained a bunch of data calculated at each time step. This
resulted in whopping memory usage (2.75 GB RAM, 3.75 GB VM).

So then I decided instead to use SQLite to store that information at
each timestep. This seems to work well, but it gets slow over time as
well. I've never used SQLite or Python before and I come from a
MATLAB-based engineering background rather than a programming one. I
was wondering if anyone had any tips for using SQLite efficiently.
Maybe a list of dos and don'ts.

I understand that specific help is impossible without a reduced sample
of code. Currently I'm looking for general guidelines and will come
back to this list for specific hangups. Thank you.

Faisal

From denis.spir at free.fr  Mon Nov 30 19:02:08 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 19:02:08 +0100
Subject: [Tutor] x is a global variable
In-Reply-To: <200911301809.39269.eike.welk@gmx.net>
References: <20091130112445.7451eaf6@o> <200911301809.39269.eike.welk@gmx.net>
Message-ID: <20091130190208.4c2545d3@o>

Hello Eike!

Eike Welk <eike.welk at gmx.net> dixit:

> Hello Spir!
> 
> On Monday 30 November 2009, spir wrote:
> > which seems to indicate python really embeds "symbolic references"
> > (*) to outer *variables*, when creating a closure for g0. Not
> > "pointer references" (**), otherwise the replacement of x would not
> > be seen by the closure --like in the case of default-parameter.
> > Actually, I find this _Bad_. Obviously, the func's behaviour and
> > result depend on arbitrary external values (referentially opaque).
> 
> If I understand you right, you are proposing that the inner function 
> g0 should get a separate copy of the global namespace. The copy should 
> be done when the function is defined. 
> 
> When this would be implemented, inner functions (g0) would be treated 
> very differently from outer functions (f), with respect to global 
> variables. I would not like this different treatment. When a global 
> variable is changed all functions should see the change. 
> 
> I think currently there are three 'containers' where functions can 
> find variables in Python:
> - local variables 
> - the closure, which is empty in outer functions (f.func_closure)
> - global variables, this is probably f.__module__.__dict__.
> 
> The exact semantics of closures seem to be a bit tricky. Below is some 
> behavior I find surprising. I had expected that a local variable 'i' 
> is created in function 'inc'. Instead the function fails with an 
> error. (Producing the error is IMHO surprising, but sane behavior.):
> 
> In [25]:def clos():
>   i = 0
>   def inc():
>     i = i + 1
>     print i
>   def pri():
>     print i
>   return inc, pri
> 
> In [33]:inc, pri = clos()
> 
> In [34]:pri()
> 0
> 
> In [35]:inc()
> -------------------------------------------------------------------
> UnboundLocalError                 Traceback (most recent call last)
> 
> /home/eike/<ipython console> in <module>()
> /home/eike/<ipython console> in inc()
> UnboundLocalError: local variable 'i' referenced before assignment

Well, this is certainly not specific to closures.

i = 0
def f():
  i = i+1
  print i
f()
==> UnboundLocalError

Imo, in this case, "i = i+1" is a kind of "paradoxal injonction" (lol! not sure of the exact idiom in english). You tell python both to create a local i (thus ignore any other scope to lookup for variables called 'i') and to use global i to define the local one.
If I were the victim of such a "paradoxal injonction" I would reply with a naughty word!

> ---
> Eike.
> 

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From bibsmendez at gmail.com  Mon Nov 30 21:04:52 2009
From: bibsmendez at gmail.com (biboy mendz)
Date: Mon, 30 Nov 2009 23:04:52 +0300
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <20091130180818.10edde94@o>
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
Message-ID: <4B142564.4020600@gmail.com>



spir wrote:
>
> What is your question?
> If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
>
>   print os.path.exists("foo".bar)
>   ==> False
>
>   
My question is: i'm looking for type of exception that more or less 
equivalent to os.path.exists attribute. I know for a fact that this 
method yields true or false. The exercise in the book says use 
try-except in place of os.path.exists(). That sure is (to me) quite 
difficult task. Instead of using the available function you're are 
tasked to do the alternative.

Lie and Tim's input are true that raw_input doesnt do anything or you 
cant catch exception error from it. And i'm wrong in placing the 
try-except clause, it should be on the fobj-open line. But im still 
looking for the exception that will be raised when i input a filename 
and that file already exists. I hope you get what i mean :-)





> The output beeing a logical value, there is no failure.
> A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
>
>   f = file("foo.bar")
>   ==>
> Traceback (most recent call last):
>   File "__essai__.py", line 10, in <module>
>     f = file("foo.bar")
> IOError: [Errno 2] No such file or directory: 'foo.bar'
>
> In this case, the type is IOError.
>
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>   

-- 
Regards,
bibs M.

Host/Kernel/OS  "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 
[sidux 2009-02 ????? - kde-full - (200907141427) ]
www.sidux.com


From mail at timgolden.me.uk  Mon Nov 30 21:11:32 2009
From: mail at timgolden.me.uk (Tim Golden)
Date: Mon, 30 Nov 2009 20:11:32 +0000
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B142564.4020600@gmail.com>
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
	<4B142564.4020600@gmail.com>
Message-ID: <4B1426F4.8090502@timgolden.me.uk>

biboy mendz wrote:
> Lie and Tim's input are true that raw_input doesnt do anything or you 
> cant catch exception error from it. And i'm wrong in placing the 
> try-except clause, it should be on the fobj-open line. But im still 
> looking for the exception that will be raised when i input a filename 
> and that file already exists. I hope you get what i mean :-)

There isn't one. The string you input which is a filename is just
a string. There's no reason for Python to check whether it is a
valid filename, any more than to check it is a valid integer or
a valid recipe on goodrecipes.com. You have to decide what you
want to do with the string.

Then you do that (try to open it as a file; try to convert it
to a number; try to use it as a URL to goodrecipes.com) and
deal with exceptions appropriately.

TJG

From davea at ieee.org  Mon Nov 30 21:46:46 2009
From: davea at ieee.org (Dave Angel)
Date: Mon, 30 Nov 2009 15:46:46 -0500
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <20091130180818.10edde94@o>
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
Message-ID: <4B142F36.5090501@ieee.org>

spir wrote:
> biboy mendz <bibsmendez at gmail.com> dixit:
>
>   
>> http://pastebin.ca/1693849
>>
>> This is end-of-chapter3 exercise of the book Core Python Programming.
>>
>> I'm reading/searching in the book and other materials but is 
>> unsuccessful. There are at least 50 exceptions listed but I can't find 
>> anything close.
>>
>> I commented out my modified script to just what *should* be applicable. 
>> Can you please point me to the right direction? TIA.
>>
>>
>>     
>
> What is your question?
> If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
>
>   print os.path.exists("foo".bar)
>   ==> False
>
> The output beeing a logical value, there is no failure.
> A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
>
>   f = file("foo.bar")
>   ==>
> Traceback (most recent call last):
>   File "__essai__.py", line 10, in <module>
>     f = file("foo.bar")
> IOError: [Errno 2] No such file or directory: 'foo.bar'
>
> In this case, the type is IOError.
>
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
>
>
>   
As others have pointed out, you need to spell out the question you're 
trying to answer.  Not all of us have a copy of that book, or have it 
handy in any case.

If you're trying to write a function that produces equivalent results to 
os.path.exists(), but uses exceptions to do so, you'll need to know 
first what that function does.  It checks if a path is valid, and refers 
to an existing file *or* *directory*.    So if you decide that the path 
doesn't specify a file, then you also have to check if it specifies a 
directory.

It's not actually correct to just use open(), even for the file portion, 
because open() may fail if the file is already open exclusively by 
another program.  For a little while, the file exists, but you may not 
open it.

I think if I couldn't use any of the os.path functions, I'd resort to 
using os.walk().  Once again, I wouldn't expect any exceptions.  I'd 
just walk through the whole tree, and see if the desired file was on any 
of the returned lists.  Don't forget to use case-insensitive comparison 
on Windows.

The problem with a poorly reworded question is there are answers that 
are perhaps useless to the original problem.  If I couldn't use 
os.path.exists(), then I'd substitute os.path.isfile() and 
os.path.isdir(), checking the one if the other fails.  But this wouldn't 
raise any exceptions either, so it wouldn't be the learning experience 
the author wants.

My suspicion is that the author just wants you to try to do an open() 
(readonly), inside a try/except block, and to successfully handle the 
exception.  Among other things, this requires you to find out which 
exception type to use.

DaveA


From denis.spir at free.fr  Mon Nov 30 22:00:12 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 22:00:12 +0100
Subject: [Tutor] python closures
In-Reply-To: <200911301836.52847.eike.welk@gmx.net>
References: <20091130112445.7451eaf6@o>
	<29179d160911300819w50f3da6fo508d0162700edc3b@mail.gmail.com>
	<200911301836.52847.eike.welk@gmx.net>
Message-ID: <20091130220012.736dd12d@o>

Eike Welk <eike.welk at gmx.net> dixit:

> On Monday 30 November 2009, Hugo Arts wrote:
> > Consider this python session:  
> >  >>> x = 0
> >  >>> def f():  
> >
> > 		x = x + 1
> >  
> >  >>> f()  
> >
> > Traceback (most recent call last):
> >   File "<pyshell#27>", line 1, in <module>
> >     f()
> >   File "<pyshell#26>", line 2, in f
> >     x = x + 1
> > UnboundLocalError: local variable 'x' referenced before assignment  
> Ah... what a pity I didn't try this. I used similar code in my 
> response to Spir and thought it would be somehow connected to 
> closures. Sending nonsense statements to the list again... 

My answer was stupid as well. Actually, guess I hadn't understood the real sense of Hugo's comment... until I tried to answer your post, Eike.

Denis

PS:
I just found by chance an article (actually a pair of) by Paul Graham (Lisp advocate) that really show how pitifully helpless python is (I'm half joking) compared to "Lisp/Perl/Smalltalk/Javascript".
http://www.paulgraham.com/icad.html (see end of article)
http://www.paulgraham.com/icadmore.html (search Paul Prescod's reply)

Quote:
<< I was actually surprised at how badly Python did. I had never realized, for example, that a Python lambda-expression  couldn't contain the same things as a named function, or that *variables from enclosing scopes are visible but not modifiable*. Neither Lisp nor Perl nor Smalltalk nor Javascript impose either restriction. >>
(I highlight)

Please don't take my quoting as an occasion for flamewar -- I'm just quoting to bring an external point of view into the thread. 

An interesting thing he points earlier is that (provided anonymous funcs were real funcs and changing external vars were possible) python,'s idiom for his example would not be:

def accum(n):
   lambda i: n += i

but instead:

def accum(n):
   return lambda i: return n += i

But actually he's wrong, I guess, it would even be:

def accum(n):
   return lambda i: n += i ; return n

(cannot return a statement) 

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


From sander.sweers at gmail.com  Mon Nov 30 22:14:54 2009
From: sander.sweers at gmail.com (Sander Sweers)
Date: Mon, 30 Nov 2009 22:14:54 +0100
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B142564.4020600@gmail.com>
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
	<4B142564.4020600@gmail.com>
Message-ID: <b65fbb130911301314h131f9f87x7a7b688241a014bd@mail.gmail.com>

2009/11/30 biboy mendz <bibsmendez at gmail.com>:
> clause, it should be on the fobj-open line. But im still looking for the
> exception that will be raised when i input a filename and that file already
> exists. I hope you get what i mean :-)

There is no exception to alert you a file already exists. Depending on
how you open the file (mode) it will either read, write or append to
the file. If the file exists and you open it in write mode it *will*
overwrite the file. See [1] for more info.

So what you need to do is check yourself if the file exists and make
the python script take appriate action. For example:

import os
fname = "C:\\testfile.txt"
if os.path.exists(fname):
    do something if exists
else:
   do something else if not exists

Greets
Sander

[1] http://docs.python.org/library/functions.html#open

From denis.spir at free.fr  Mon Nov 30 22:25:33 2009
From: denis.spir at free.fr (spir)
Date: Mon, 30 Nov 2009 22:25:33 +0100
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B142564.4020600@gmail.com>
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
	<4B142564.4020600@gmail.com>
Message-ID: <20091130222533.5344a707@o>

biboy mendz <bibsmendez at gmail.com> dixit:

> 
> 
> spir wrote:
> >
> > What is your question?
> > If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find:
> >
> >   print os.path.exists("foo".bar)
> >   ==> False
> >
> >   
> My question is: i'm looking for type of exception that more or less 
> equivalent to os.path.exists attribute. I know for a fact that this 
> method yields true or false. The exercise in the book says use 
> try-except in place of os.path.exists(). That sure is (to me) quite 
> difficult task. Instead of using the available function you're are 
> tasked to do the alternative.
> 
> Lie and Tim's input are true that raw_input doesnt do anything or you 
> cant catch exception error from it. And i'm wrong in placing the 
> try-except clause, it should be on the fobj-open line. But im still 
> looking for the exception that will be raised when i input a filename 
> and that file already exists. I hope you get what i mean :-)

Right. So, the exercise is about replacing an explicit check by try...except. But unlike what you seem to think above, you won't get any error from file *writing*.
Think at variables: python does not make any distinction between creation or change, so that you you won't get any error by "writing" a variable that already exist, even if your intention was to create a new one (and you wrote a wrong name). Because there is no syntactic difference between creation and change. Conversely, if you create a new variable when you intended to change an existing one, you won't get any nice error, it will be silently processed. Right?
The same with files. So you cannot have such errors at file writing, if the file exists, it will be overwritten. So, you can only use try...except for file *reading*.
See my previous post.


(*) Except in the case when you try to overwrite you don't have (as user) the right to change, or if you try to create a file in a folder you don't have writing right in.

> > The output beeing a logical value, there is no failure.
> > A general method to get a type of exception matching a particuliar kind of error is to provoke an error of this given kind, eg:
> >
> >   f = file("foo.bar")
> >   ==>
> > Traceback (most recent call last):
> >   File "__essai__.py", line 10, in <module>
> >     f = file("foo.bar")
> > IOError: [Errno 2] No such file or directory: 'foo.bar'
> >
> > In this case, the type is IOError.
> >
> > Denis



________________________________

la vita e estrany

http://spir.wikidot.com/


From christopher.henk at allisontransmission.com  Mon Nov 30 22:43:00 2009
From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com)
Date: Mon, 30 Nov 2009 16:43:00 -0500
Subject: [Tutor] Equivalent exception of os.path.exists()
In-Reply-To: <4B142564.4020600@gmail.com>
Message-ID: <OF9A103826.E6BDA9F5-ON8525767E.00746B83-8525767E.007742F5@mail.ati.int>

biboy mendz <bibsmendez at gmail.com> wrote on 11/30/2009 03:04:52 PM:

> 
> 
> spir wrote:
> >
> > What is your question?
> > If it's about the type of exception raised when os.path.exists fails, 
well, sure it's hard to find:
> >
> >   print os.path.exists("foo".bar)
> >   ==> False
> >
> > 
> My question is: i'm looking for type of exception that more or less 
> equivalent to os.path.exists attribute. I know for a fact that this 
> method yields true or false. The exercise in the book says use 
> try-except in place of os.path.exists(). That sure is (to me) quite 
> difficult task. Instead of using the available function you're are 
> tasked to do the alternative.
> 

Looking at my copy of Core Python the exercise says to follow the example 
3.2 (readTextFile.py).  There he uses the try except block around the open 
function call. 

This as mentioned could raise an IOError.  It will be triggered in a 
different manner then os.path.exist since you are now trying to open a 
file to write, instead of checking if it exist.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/eea64772/attachment.htm>

From alan.gauld at btinternet.com  Mon Nov 30 22:59:53 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 30 Nov 2009 21:59:53 -0000
Subject: [Tutor] Equivalent exception of os.path.exists()
References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o>
	<4B142F36.5090501@ieee.org>
Message-ID: <hf1f8u$1f4$1@ger.gmane.org>


"Dave Angel" <davea at ieee.org> wrote

> My suspicion is that the author just wants you to try to do an open() 
> (readonly), inside a try/except block, and to successfully handle the 
> exception.  

And the critical thing here is that it must be an open in read mode. 
As others pointed out if you use write mode it will (nearly) always 
succeed, even if that means overwriting an existing file.

Alan G.


From alan.gauld at btinternet.com  Mon Nov 30 23:05:50 2009
From: alan.gauld at btinternet.com (Alan Gauld)
Date: Mon, 30 Nov 2009 22:05:50 -0000
Subject: [Tutor] numerical simulation + SQLite
References: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com>
Message-ID: <hf1fk6$2p2$1@ger.gmane.org>

"Faisal Moledina" <faisal.moledina at gmail.com> wrote 
.
> At first, I used a list to store a bunch of class instances, each of
> which contained a bunch of data calculated at each time step. This
> resulted in whopping memory usage (2.75 GB RAM, 3.75 GB VM).
> 
> So then I decided instead to use SQLite to store that information at
> each timestep. This seems to work well, but it gets slow over time as
> well. 

You may need to be realistic in your expectations.
A database is writing to disk which will be slower than working in 
memory. And a 3GB file takes a while to read/traverse, even with 
indexes. It depends a lot on exactly what you are doing. If its mainly 
writing it should not be much slower than writing to a flat file. If you 
are doing a lot of reading - and you have used indexes - then it 
should be a lot faster than a file.

But RAM - if you have enough - will always be fastest, by about 100 times.
The problem is when you run out, you revert to using files and that's 
usually slower than a database...

But without details of your usage pattern and database schema 
and SQL code etc it is, as you say, impossible to be specific.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/