Re: [Tutor] Set Reply-To field to Tutor@python.org

2013-04-09 Thread DoanVietTrungAtGmail
FWIW, I'd like to reverse my answer, and now I agree with Oscar's 29 Jan
suggestion to make "Reply-to-tutor-list" the default.

Lately, several times a tutor had to forward to the list an email meant for
the list but sent to him only, by mistake. On the other hand, the
wrote-to-1-person-but-mistakenly-sent-to-list scenario that I had in mind
is rarer, therefore Oscar's suggestion is better than my previous reply.

Trung


On Wed, Jan 30, 2013 at 6:33 AM, Nick W  wrote:

> My personal opinion (with whatever limited weight that has on this list
> since I've only answered a few questions - and probably half of them I've
> accidentally only sent to the op)/how I read it is that RFC 2822 actually
> allows lists to set reply-to header; by my view the list software is
> forwarding to everyone else and therefor counts as the most recent
> sender/author. I admit that that is a somewhat different conclusion to
> others that I've read as to the meaning of 2822, but that seems logical to
> me and also my personal preference is for having the reply-to header be set
> to the list address.
> Nick
>
>
> On Tue, Jan 29, 2013 at 7:44 AM, Albert-Jan Roskam wrote:
>
>>
>>
>> >
>> > To summarize existing opinions on this matter:
>> >
>> > http://marc.merlins.org/netrants/listreplyto.html
>> >
>> > You might want to familiarize yourself with existing literature on the
>> > matter before starting a new flame war.
>>
>> Hmmm... False alarm?
>>
>> Page blocked
>>
>> The page you've been trying to access was blocked.
>> Reason: Access Denied! The requested URL is a Spyware site.
>> Transaction ID is 5107F01CFF920603D57F.
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Which databases allow lists as record fields?

2013-03-24 Thread DoanVietTrungAtGmail
I read your help (thanks, Peter and Alan) and thought that relational
database technology didn't naturally map to my problem domain. Here, at
each simulation timestep each entity has a run-time variable number of
pointers to other entities. Representing each entity as a table seems
unwieldy to me.

So, I searched further and now write about what I've learnt, in case
someone with a similar problem finds this thread in future.

One possible set of solutions is the family of "NoSQL
databases<http://en.wikipedia.org/wiki/NoSQL>".
Redis is one such DB, of "key-value" type, and in Pypi there is a package
called redis 2.7.2 <https://pypi.python.org/pypi/redis/2.7.2>. Another type
is "graph databases", and in Pypi there's a package called redis-graph
1.0<https://pypi.python.org/pypi/redis_graph/1.0>built on top of
Redis.

I've also just installed Scipy because Scipy handles sparse
matrices<http://www.scipy.org/SciPyPackages/Sparse>.
My simulation has N entities, I'll try representing it as an NxN sparse
matrix, each row stores pointers in some sparse format to the rows it talks
to.

And then there's also NxN bitarray <https://pypi.python.org/pypi/bitarray/>,
where on each row a bit is 1 if this row points to this column, to save
space there's "compressed bitarray".  I also read about "Judy sparse
array", touted as fast and memory efficient, but last night I read the
author of the package PyJudy
<http://stackoverflow.com/questions/550204/judy-array-for-managed-languages/550464#550464>writing
in 2009 that he hasn't maintained it for "a few years", that put me off.

Which one to use, I don't know yet. I am about to start experimenting with
Scipy sparse matrix.

Trung
==

On Thu, Mar 14, 2013 at 8:13 PM, Alan Gauld wrote:

> On 14/03/13 01:39, DoanVietTrungAtGmail wrote:
>
>> You don't. You create a second table to hold the list.
>> Then in the second table you include be reference back to the first.
>>
>> assuming I do it that way, how to deal with variable-length list? Most
>> lists have 10^3, but some can grow to perhaps 10^7 items. A fixed record
>> structure to fit the longest possible record would make the database
>> table sparse and, presumably, waste space.
>>
>
> I'm not sure what you mean here but the tables are not fixed size. Usually
> you are only limited by the size of your hard drive.
>
> To give an example if you have an object MyType with two of these list
> type attributes called foo and bar. We have two instances of MyType, A and
> B. A has 10 foos and 50 bars while B has 500 foos and 1000 bars.
>
> Create a table MyType and populate it with A and B
>
> Create a Table MyType_foo and populate it with 10 rows referencing A and
> 500 rows referencing B
>
> Create a table Mytype_bar and populate it with 50 rows referencing A and
> 1000 rows referencing bar
>
> Now to see all the foos for A use
>
> select value from foo where parent = A
>
> or for B use
>
> select value from foo whee parent = B
>
> and the same for bar...
>
> You can keep adding rows to foo or bar for as long as ypou like.
> Youb can add new MyType rows and then add more rows to MyType_foo and bar
> which reference thiose new MyTypes.
>
> There is no limit to how many items you add until your disk fills up!
>
>
>  An alternative I thought of was to use SQL Server fields for XML, or
>> varchar variable-length character strings containing 10^9 characters,
>> then to read the list I'd parse the XML or split the string into literal
>> integers then convert to int. I wanted to avoid this computation cost if
>> possible.
>>
>
> You can do this but you might as well just store everything in XML in a
> flat file. You lose the power of the database query language to
> search/filter the results and give yourself a huge
> development/test/maintenance task.
>
> The people who build databases are likely much better at this stuff that
> you or me. Use what they give you.
>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Which databases allow lists as record fields?

2013-03-13 Thread DoanVietTrungAtGmail
I saw your (Chris') reply after replying to Alan. So, the answer from both
of you is No, not native.

I've just read that MySQL is free and has a data type called LONGTEXT
storing up to 4x10^9 char. Hurrah! As to SQL Server, I've just read that it
"could take a small independent company´s budget and eat it for lunch".
That's scary.

Thanks Chris, thanks Alan!

Trung
=
On Thu, Mar 14, 2013 at 11:35 AM, Chris Fuller <
cfuller...@thinkingplanet.net> wrote:

>
> Use pickle to serialize the list, then use any DB that takes strings of
> arbitrary length.  You will probably be frustrated if you seek a (useful)
> DB
> that has native support for such things.
>
> Cheers
>
> On Wednesday, March 13, 2013, Dave Angel wrote:
> > I presume you meant this for the tutor list.  I don't have any answers
> > for the question.
> >
> >
> >  Original Message 
> > Subject: [Tutor] Which databases allow lists as record fields?
> > Date: Thu, 14 Mar 2013 10:59:27 +1100
> > From: DoanVietTrungAtGmail 
> > To: Dave Angel 
> >
> > Dear tutors
> >
> > To elaborate on my above question, I mean: Which database (which a Python
> > program can access) allows a record to have not just simple fields but
> also
> > fields that are variable-length lists?
> >
> > For my application, records in one database table might have 1 list,
> those
> > in another have,  say, 4. Each list has a variable number of integers, no
> > list contains embedded lists.
> >
> > The purpose of asking this question is to know which database to learn.
> >
> > Before asking this question, I looked at Alan Gauld's SQLite
> > tutorial<http://www.alan-g.me.uk/tutor/tutdbms.htm>and the list
> > of data types in W3school's SQL
> > tute<http://www.w3schools.com/sql/sql_datatypes.asp>,
> > plus a few Google searches.
> >
> > Trung Doan
> >
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Which databases allow lists as record fields?

2013-03-13 Thread DoanVietTrungAtGmail
..


> You don't. You create a second table to hold the list.
> Then in the second table you include be reference back to the first.
>

I thought about that but thought it seemed a roundabout way. But assuming I
do it that way, how to deal with variable-length list? Most lists have
10^3, but some can grow to perhaps 10^7 items. A fixed record structure to
fit the longest possible record would make the database table sparse and,
presumably, waste space.

An alternative I thought of was to use SQL Server fields for XML, or
varchar variable-length character strings containing 10^9 characters, then
to read the list I'd parse the XML or split the string into literal
integers then convert to int. I wanted to avoid this computation cost if
possible.

Trung


>
>> For my application, records in one database table might have 1 list, those
>> in another have,  say, 4. Each list has a variable number of integers, no
>> list contains embedded lists.
>>
>> The purpose of asking this question is to know which database to learn.
>>
>> Before asking this question, I looked at Alan Gauld's SQLite
>> tutorial>and
>> the list
>> of data types in W3school's SQL
>> tute
>> >,
>> plus a few Google searches.
>>
>> Trung Doan
>>
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A CSV field is a list of integers - how to read it as such?

2013-03-06 Thread DoanVietTrungAtGmail
> Once a csv file has been read by a csv reader (such as DictReader), it's
> no longer a csv file.


That was an "Aha!" moment for me. The file is on disk, each row of it is in
memory as a list or dict, and it's the list or dict that matters. It's so
obvious now. Thanks Dave.

>

a namedtuple is probably exactly what you want.


I read this as meaning that while tuples themselves are immutable, to
effectively modify it I simply delete it and replace it with a new tuple
with new values. Another 'Aha!'

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


Re: [Tutor] A CSV field is a list of integers - how to read it as such?

2013-03-03 Thread DoanVietTrungAtGmail
Don, Dave - Thanks for your help!

Don: Thanks! I've just browsed the AST documentation, much of it goes over
my head, but the ast.literal_eval helper function works beautifully for me.

Dave: Again, thanks! Also, you asked "More space efficient than what?" I
meant .csv versus dict, list, and objects. Specifically, if I read a
10-million row .csv file into RAM, how is its RAM footprint compared to a
list or dict containing 10M equivalent items, or to 10M equivalent class
instances living in RAM. I've just tested and learned that a .csv file has
very little overhead, in the order of bytes not KB. Presumably the same
applies when the file is read into RAM.

As to the RAM overheads of dict, list, and class instances, I've just found
some stackoverflow discussions.
One<http://stackoverflow.com/questions/2211965/python-memory-usage-loading-large-dictionaries-in-memory>says
that for large lists in CPython, "the
overallocation is 12.5 percent".

Trung Doan


On Mon, Mar 4, 2013 at 2:12 PM, Dave Angel  wrote:

> On 03/03/2013 09:24 PM, DoanVietTrungAtGmail wrote:
>
>> Dear tutors
>>
>> I am checking out csv as a possible data structure for my records. In each
>> record, some fields are an integer and some are a list of integers of
>> variable length. I use csv.DictWriter to write data. When reading out
>> using
>> csv.DictReader, each row is read as a string, per the csv module's
>> standard
>> behaviour. To get these columns as lists of integers, I can think of only
>> a
>> multi-step process: first, remove the brackets enclosing the string;
>> second, split the string into a list containing substrings; third, convert
>>   each substring into an integer. This process seems inelegant. Is there a
>> better way to get integers and lists of integers from a csv file?
>>
>> Or, is a csv file simply not the best data structure given the above
>> requirement?
>>
>
> Your terminology is very confusing.  A csv is not a data structure, it's a
> method of serializing lists of strings.  Or in this case dicts of strings.
>  If a particular dict value isn't a string, it'll get converted to one
> implicitly.  csv does not handle variable length records, so this is close
> to the best you're going to do.
>
>
>  Apart from csv, I considered using a dict or list, or using an
>
>> object to represent each row.
>>
>
> Objects don't exist in a file, so they don't persist between multiple runs
> of the program.  Likewise dict and list.  So no idea what you really meant.
>
>
>  I am being attracted to csv because csv means
>
>> serialisation is unnecessary, I just need to close and open the file to
>> stop and continue later (it's a simulation experiment).
>>
>
> Closing and opening don't do anything to persist data, but we can guess
> you must have meant to imply reading and writing as well.  And you've
> nicely finessed the serialization in the write step, but as you discovered,
> you'll have to handle the deserialization to get back to ints and list.
>
>
>  Also, I am guessing
>
>> but haven't checked, csv is more space efficient.
>>
>
> More space efficient than what?
>
>
>  Each row contains a few
>
>> integers plus a few lists containing hundreds of integers, and there will
>> be up to hundreds of millions of rows.
>>
>> CODE: My Python 2.7 code is below. It doesn't have the third step
>> (substring -> int).
>>
>> import csv
>>
>> record1 = {'id':1, 'type':1, 'level':1, 'ListInRecord':[2, 9]}
>> record2 = {'id':2, 'type':1, 'level':1, 'ListInRecord':[1, 9]}
>> record3 = {'id':3, 'type':2, 'level':1, 'ListInRecord':[2]}
>> record9 = {'id':9, 'type':3, 'level':0, 'ListInRecord':[]}
>> rows = [record1, record2, record3, record9]
>> header = ['id', 'type', 'level', 'ListInRecord']
>>
>> with open('testCSV.csv', 'wb') as f:
>>  fCSV = csv.DictWriter(f, header)
>>  fCSV.writeheader()
>>  fCSV.writerows(rows)
>>
>> with open('testCSV.csv', 'r') as f:
>>  fCSV = csv.DictReader(f)
>>  for row in fCSV:
>>
>
>  I'd add the deserialization here. For each item in row, if the value
> begins and ends with [ ]  then make it into a list, and if a digit or
> minus-sign, make it into an int.  Then for the lists, convert each element
> to an int.  You can use Do

[Tutor] timeit: 10million x 1 Vs 1million x 10

2013-02-27 Thread DoanVietTrungAtGmail
Dear tutors

My function below simply populates a large dict. When measured by timeit
populating 10 million items once, versus populating 1 million items ten
times, the times are noticeably different:

---
import timeit

N = 1000 # This constant's value is either 10 million or 1 million
testDict = {}
def writeDict(N):
for i in xrange(N):
testDict[i] = [i, [i + 1, i + 2], i + 3]
print timeit.Timer('f(N)', 'from __main__ import N, writeDict as
f').timeit(1) # the 'number' parameter is either 1 or 10

---
Result from 3 runs of 10 million x 1 time: 12.7655465891, 13.1248426525,
12.1611512459

Result from 3 runs of 1 million x 10 times:
14.3727692498, 14.3825673988, 14.4390314636

I ran Python 2.7 on Pycharm on Windows 7.

My guess is that this discrepancy is a result of either how some sort of
overhead in timeit, or of Python having to allocate memory space for a dict
10 times. What do you think, and how to find out for sure?

Second (for me, this question is more important), how to improve
performance? (I tried a tuple rather than a list for the dict values, it
was slightly faster, but I need dict items to be mutable)

Thanks

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


Re: [Tutor] Python Lover

2013-02-13 Thread DoanVietTrungAtGmail
I am also learning and have found it useful to learn from David Beazley's
"Python In Action", parts 1 and 2. Part 2 is tough for me because I don't
know other languages nor systems programming, but may be OK for you Brajesh.

The slidepacks are at
 http://www.slideshare.net/dabeaz/presentations

Trung Doan


On Thu, Feb 14, 2013 at 9:39 AM, Prasad, Ramit wrote:

> Brajesh wrote:
> > Hi ,
> >
> > I have just subscribed mailing list to learn python. I am programming in
> > java for nearly 8 months, I have developed a strong love towards python
> > and want to learn and start making the best use of it. I want to know
> > where to from as i know JAVA already so where to start from.
> >
> > Regards
> > Brajesh
>
> Since you are from a Java background, I think it will help
> you to read the following articles.
>
> http://dirtsimple.org/2004/12/python-is-not-java.html
> And the counter article
> http://dirtsimple.org/2004/12/java-is-not-python-either.html
>
>
> I also think Doug Hellman's Module of the Week (MOTW) is an
> excellent reference for the standard library.
> http://www.doughellmann.com/PyMOTW/index.html
>
>
> ~Ramit
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Set Reply-To field to Tutor@python.org

2013-01-29 Thread DoanVietTrungAtGmail
On Tue, Jan 29, 2013 at 9:12 PM, Oscar Benjamin
wrote:

> If you don't mind my asking, do you send many off-list messages as
> replies to on-list ones?
> Oscar
>

For this list, I have sent 1 public reply and 2 private replies (to thank
individual tutors). Both numbers are too small to have any significance.

Reading the Chip Rosenthal article that Steven referred to, I thought that
in the case of this Tutor list, his arguments are neither here nor there.
It probably comes down to personal preference, and that's why I stated mine.

The reason for my personal preference is that for other lists, I am
frequently annoyed by private-reply emails landing in my inbox, and
sometimes I absent-mindedly do the same thing, thus annoying others.

For most lists, group-reply ought to be deliberate. For a few lists it
doesn't matter either way. What if, as Oscar seems to say, this Tutor list
is in a category where private-reply ought to be deliberate? Most people
would only form 1 habit for all lists, rather than 1 for each of the
several lists they participate in. If so, I think the habit of deliberate
public-reply would serve us better in terms of etiquette.

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


Re: [Tutor] Set Reply-To field to Tutor@python.org

2013-01-28 Thread DoanVietTrungAtGmail
As a student user of this list, I prefer leaving the Reply-To field
unchanged. I like the fact that this means a deliberate  decision is
required to send public emails.

Trung

On Tue, Jan 29, 2013 at 12:17 PM, Steven D'Aprano wrote:

> On 29/01/13 09:04, Oscar Benjamin wrote:
>
>  One particular list that I receive has recently made an administrative
>> change so that, from now on, all emails have the "Reply-To" header set
>> to the list address. This means that the default behaviour when
>> replying to a message is that the reply goes to the list. I think that
>> this is the right thing to do by default since replying off-list is
>> much less common and more likely to be something that you are
>> consciously aware of when you do it.
>>
>> The change seems to have gone down well on the other list so I
>> wondered: could it be done for this list as well?
>>
>
>
> http://www.unicom.com/pw/**reply-to-harmful.html
> http://woozle.org/~neale/**papers/reply-to-still-harmful.**html
>
> versus
>
> http://www.betacantrips.com/**bits/reply_to_munging_**
> considered_harmful_considered_**infuriating/
> http://www.metasystema.net/**essays/reply-to.mhtml
>
>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list of references to object properties

2013-01-19 Thread DoanVietTrungAtGmail
Hi Jose, have you thought about shallow copying?

I am learning Python too, so I can be wrong here. But I have just tried the
idea and it worked for me:

class celestialBody(object):
def __init__(self, m, x, y, z):
self.mass = m
self.pos = [x, y, z]
def __str__(self):
return str(self.mass) + ' and ' + str(self.pos)

earth = celestialBody(1, 0, 0, 0)
jupiter = celestialBody(2, 2, 2, 2)
asteroid = celestialBody(0.1, 1, 1, 1)

planetSystem = [earth, jupiter]

positionVectors = [earth.pos, jupiter.pos]

print "object earth's mass and original position is", earth

# Now we make changes to earth's coordinates one by one
# not by manipulating earth but by manipulating the positionVectors
positionVectors[0][0] = positionVectors[0][0] + 0.01*asteroid.pos[0]
positionVectors[0][1] = positionVectors[0][1] + 0.01*asteroid.pos[1]
positionVectors[0][2] = positionVectors[0][2] + 0.01*asteroid.pos[2]

print "object earth's new position, as seen in positionVectors, is",
positionVectors[0]
print "object earth's mass and new position, as seen in earth object, is",
earth
print "object earth's mass and new position, as seen in planetSystem, is",
planetSystem[0]

>>> == RESTART ==
>>>
object earth's mass and original position is 1 and [0, 0, 0]
object earth's new position, as seen in positionVectors, is [0.01, 0.01,
0.01]
object earth's mass and new position, as seen in earth object, is 1 and
[0.01, 0.01, 0.01]
object earth's mass and new position, as seen in planetSystem, is 1 and
[0.01, 0.01, 0.01]
>>>

As to Alan's ideas below, I don't really understand them.

Trung Doan

On Sun, Jan 20, 2013 at 5:01 AM, Alan Gauld wrote:

> On 19/01/13 15:47, Jose Amoreira wrote:
>
>  motion. I defined a class, CelestialBody, that describes objects that
>> represent planets in my simulation. These objects have three attributes:
>> position, velocity and mass (the first two are 3D-vectors; as such, the
>> number of attributes is actually 7). The many-body system is represented
>> in the simulation by a list of CelestialBody objects.
>>
>
> OK, why not hold that list in the CelestialBody class?
>
> Then get the list to either hold a copy of the key values or generate it
> on demand. If the list of values is also in the class definition all the
> descendant types of body can update the class list at the same time as
> updating their own copy. It means duplicating data but it keeps the
> instances in control of the data while making it available (read-only, from
> the class) when needed. The overhead is creating the getter/setter methods
> to update the class list in parallel with the instance data.
>
> Alternatively store it once at class level and create properties of the
> instance that do all access in the class list. That way it looks like you
> are updating the instance but the instance delegates the storage to the
> class list. The instance can store its own  index into the class list as
> advised by the class on creation.
>
> There are ways of doing what you want that keep responsibility in the
> object. The choice will depend on how often you need to vary the instance
> values as opposed to using the class list.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why doesn't line n execute before line n+1?

2013-01-14 Thread DoanVietTrungAtGmail
Yes you are right, it was printed. I think my eyes are tired. I left the
PC, came back, and saw your reply plus the printed line. Thanks Mitya, and
I apologise to everyone about false alarm! The answer of 42 was staring at
me and I didn't see it.

Trung

On Tue, Jan 15, 2013 at 2:33 PM, Mitya Sirenef wrote:

> On Mon 14 Jan 2013 10:14:24 PM EST, DoanVietTrungAtGmail wrote:
>
>> Dear tutors
>>
>> In learning about the __call__ magic method, in the code below I
>> deliberately omitted __call__ and, as expected, I got the error
>> message "TypeError: 'Test' object is not callable". But I am surprised
>> that the print statement was not executed, even though the interpreter
>> sees it first. Why is that?
>>
>> I thought that the Python interpreter executes line by line. That is,
>> in the code below,:
>> -First, it executes the class definition because these 2 lines are
>> what it sees first
>> -Second, it creates an instance of the class Test, called test
>> -Third, it executes the print statement
>> -Only then would it encounter the error of calling the instance as if
>> it were callable
>>
>> class Test(object):
>> pass
>> test  = Test()
>> print "I am puzzled. Why isn't this line printed?"
>> test()
>>
>> Making the puzzle worse for me, when I tried adding another print
>> statement before the test = Test() line, the interpreter behaved as I
>> expected!
>>
>> Trung
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>
> It does print the line for both in 2.7 and 3.3.  - mitya
>
>
> --
> Lark's Tongue Guide to Python: http://lightbird.net/larks/
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Why doesn't line n execute before line n+1?

2013-01-14 Thread DoanVietTrungAtGmail
Dear tutors

In learning about the __call__ magic method, in the code below I
deliberately omitted __call__ and, as expected, I got the error message
"TypeError: 'Test' object is not callable". But I am surprised that the
print statement was not executed, even though the interpreter sees it
first. Why is that?

I thought that the Python interpreter executes line by line. That is, in
the code below,:
-First, it executes the class definition because these 2 lines are what it
sees first
-Second, it creates an instance of the class Test, called test
-Third, it executes the print statement
-Only then would it encounter the error of calling the instance as if it
were callable

class Test(object):
pass
test  = Test()
print "I am puzzled. Why isn't this line printed?"
test()

Making the puzzle worse for me, when I tried adding another print statement
before the test = Test() line, the interpreter behaved as I expected!

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


Re: [Tutor] Decorators: Are they good for checking inputs and outputs?

2013-01-06 Thread DoanVietTrungAtGmail
*.. Could you perhaps give a concrete example of a situation where
a decorator would be useful for checking the inputs to a function? .. Oscar*

Say I write a function that expects 5 positional arguments, and up to 4 **
arguments. Now I want to:
a- check that the first positional argument is a sorted list. If not a
list, raise an error. If an unsorted list, sort it and pass to the function
b- check that the 5th position argument is an 0 < int < 10
c- check that no more than 4 ** arguments are passed

In the decorators-make-my-life-simple scenario I hope for, I'll simply open
my box of decorators, pick 3 relevant decorators, and put the 3 @ lines
above my function. In the @ for the b- test, I'll pass a 5 to tell the
decorator to look at the 5th argument, and similarly a 4 for the decorator
checking c-.

Then I write another function, whose test requirements will be different,
I'll simply pick decorators relevant to it, then go on writing more code.

If this scenario works as expected, I can focus my mind on the concepts I'm
working on rather than having to interrupt the flow of thoughts to write
test code.

*.. See the recent discussion on Test Driven Development .. Alan*

Yes I followed the recent discussion about unit testing. I suppose that
decorators can't do everything that a comprehensive unit testing can, but
probably decorators plus a few doctests would suffice for my purpose.

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


Re: [Tutor] need help with python for counter

2012-12-18 Thread DoanVietTrungAtGmail
After incrementing for a little while, if the condition i == digit is not
met for the current character, count is reset by the else: branch. Your
count variable must feel frustrated like a dog which keeps being yanked
back by a cord of length 0 when it tries to get free.

Trung Doan
==

On Wed, Dec 19, 2012 at 4:40 PM, Brandon Merritt wrote:

> I feel silly, but I'm having the darndest time trying to figure out why
> this for counter won't work. I know that there is the count method for the
> string class, but I was just trying to do it the syntactical way to prove
> myself that I know the very basics. As of right now, my script is just
> returning 1 or 0, even if I very clearly make sure that I specify at least
> 4 instances of the digit in my number string:
>
> number = raw_input('Enter a 7-unit number: ')
>
> digit = raw_input('Enter a single digit: ')
>
> for i in number:
> count = 0
> if i == digit:
> count += 1
> else:
> count = 0
>
> print count
>
> Thanks,
> Brandon
>
>
> --
> *Brandon Merritt**
> (707) 481-1744*
> *
> *
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor