Re: [Tutor] ASCII Conversion

2012-01-31 Thread Russel Winder
On Tue, 2012-01-31 at 07:33 +0200, Christian Witts wrote:
> [...]o with 
> `type(y) == int`, and to get the ASCII value of a character you can use 
> `ord` like `ord('a') == 97`. And how to avoid your ValueError with a bad 
> conversion, do your type checking before hand.

isinstance ( y , int ) 

preferred?

Violates EAFP obviously but...

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@russel.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any book to study Python

2012-03-19 Thread Russel Winder
Xianming,

On Mon, 2012-03-19 at 18:38 +1100, Yan, Xianming wrote:
> Hello all,
> 
> I'm a new learning to python and does not know python anything at all.
> 
> Anyone can recommend a book about python to read? I prefer a paper-based 
> book, not online book. 
> 
> By the way, I'm from china, I hope the book is Chinese--:)--Reading in 
> Chinese is quicker~:)
> 
> Thanks
> Xianming

The book Sarah Mount, James Shuttleworth and I wrote "Python for
Rookies" is a book aimed to teach people who are not already programmers
programming using Python.  Not been translated to Chinese yet as far as
I know.  The book is Python 2.4/2.5, we are currently trying to work
with the publisher to get out a new edition that can be Python 2.7/3.2.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error handling

2012-03-25 Thread Russel Winder
Michael,

On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote:
[...]

It is perhaps worth noting that in Python 3, the syntax has changed:

> import os, errno
> try:
> 
> os.makedirs('a/b/c')
> except OSError, e:

except OSError as e :

> 
> if e.errno != errno.EEXIST:
> 
> raise

This "as" syntax works in 2.6 and 2.7 so is probably the syntax to use
unless you have to use very old versions of Python.  I think the "as"
syntax makes it clearer that e is a variable referring to the instance
of OSError that causes the except clause to execute if it does.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Barry,

On Fri, 2012-03-30 at 16:42 +0100, Barry Drake wrote:
[...]
> def getflag(thisflag, results):
>  if (thisflag == 2):
>  results[0] += 1
>  elif (thisflag == 1):
>  results[1] += 1
>  elif (thisflag == 0):
>  results[2] += 1
>  return(results)

Two thoughts spring to mind:

-- is it possible to arrange the data model such that the value of the
thisflag is the index into the sequence, then:

def getflag(thisflag, results):
results[thisflag] += 1
return results

-- seriously "overengineered" for this particular example but the
replacement for switch statement is a dictionary and function pointers.

from functools import partial

def alt(thisflag, results):
def setflag(x):
results[x] += 1
{
0: partial(setflag, 2),
1: partial(setflag, 1),
2: partial(setflag, 0),
}[thisflag]()
return results

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Ramit,

On Fri, 2012-03-30 at 16:22 +, Prasad, Ramit wrote:
[...]
> C switch is just a different way of doing an if/elif tree, I do not 
> really see any real difference. Although, if there is you can feel free 
> to enlighten me. :)
[...]

'fraid not -- though it depends on which compiler and how many cases.
For 3 or more cases compilers will generate a hardware supported jump
table: a switch statement is a jump table not a cascade of if
statements.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Barry,

On Fri, 2012-03-30 at 18:27 +0100, Barry Drake wrote:
> On 30/03/12 17:58, Mark Lawrence wrote:
> > The recipe here
> > http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/
> >  
> >
> > refers to several other recipes which you might want to take a look 
> > at, sorry I meant to mention this earlier.
> >
> 
> Oh, that's neat.  Not worth putting into my little project, but I've 
> bookmarked that on for when I need a lot of cases.  It also shows how to 
> define a class - that was something I had wondered about, but not yet 
> tackled.

Be aware of the sensible warnings though.  switch in C is generally O(1)
whereas this Python simulacrum remains O(n).  The people who comment
saying C switch is always O(n) have clearly never looked at any compiler
output.

Personally, I am in the camp that says: don't use a device that makes it
appear the performance is not what it is.  Thus I would prefer
if/elif/else cascade over this device since it is simpler and more
obvious that it is O(n).

But it is a nice example of what Python can achieve even though I would
not use it myself.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] .py vs .pyc

2012-04-19 Thread Russel Winder
On Thu, 2012-04-19 at 10:47 -0400, Max S. wrote:
> Could anyone tell me why I should use a .pyc file rather than a .py?  After
> doing some research, I have found that a .py file is first precompiled and
> then run, while a .pyc file is already precompiled and is simply run.  But
> unless I'm mistaken, it seems that a .pyc is no faster or better than a .py
> file.  When should I use a .py, and when should I use a .pyc?

pyc files are just internal PVM files.  Although they appear on the
filestore visible to the programmer, just leave management of them to
the PVM.  Humans deal only with .py files -- or possibly pyx if you are
using Cython.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Readabilty vs 80 characters

2012-04-19 Thread Russel Winder
On Thu, 2012-04-19 at 16:00 +0200, Peter Otten wrote:
[...]
> Seriously, learn that you don't need backslashes if you put the expression 
> into parens, precalculate parts of the expression and put them into 
> temporary variables -- and if that doesn't suffice to keep the code readable 
> and below the 80 char threshold reread the part of the PEP with the Emerson 
> quote ("A foolish consistency...").

I agree that where there is a backslash in a Python program, there is a
better layout that doesn't have it.  I wouldn't agree though that,
necessarily, decomposing long expression using temporary variables
improves anything. A good fluent API leads to function call chaining,
far fewer variables, and far more readable code. Much of the time, not
always, of course.

There is currently a definite trend (*) away from 1960s FORTRAN style
code structuring, to a 1980s Miranda (think Haskell and Scala) type
thinking about how to structure code.  There is experimental evidence in
psychology of programming that declarative expression leads to more
easily comprehensible code. This isn't advocacy research as much of
programming research has been. Even C++ is going to a more declarative
mode of expression, along with Java.  Python's list comprehensions fit
into this trend.


(*) Which may turn out just to be just the latest fashion ready to be
replaced in 2013. 

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] .py vs .pyc

2012-04-19 Thread Russel Winder
On Thu, 2012-04-19 at 10:57 -0400, Max S. wrote:
> Then if I understand correctly, I work with .py files and (should) run
> them as .pyc files?

No, you always run the py files, don't worry about the pyc files at all,
the PVM will do what it does.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why gtk.Entry does not give me right answers?

2012-04-23 Thread Russel Winder
On Tue, 2012-04-24 at 01:08 +1000, Steven D'Aprano wrote:
[...]
> 
> * Use the decimal module instead of floats. You still have finite precision,
>   but you can choose how many *decimal* places to store instead of having a
>   fixed number of *binary* places. (But decimal is much slower.)

Alternatively use http://packages.python.org/bigfloat/ or possibly
http://code.google.com/p/mpmath/
> 
> By the way, the code that you sent is unreadable. Please make sure that you
> send plain text email, and that indentation is not lost. Without indentation,
> it is impossible to understand your code.

Indeed. The lack of indentation was a barrier to contemplating looking
at the code. Especially since indentation is meaningful in Python!

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting started with PyGTK [Receiving Error]

2012-04-29 Thread Russel Winder
Santosh,

On Sun, 2012-04-29 at 04:18 +0530, Santosh Kumar wrote:
> System Information
> 
> Ubuntu 11.10
> Python 2.7.2
> 
> Problem
> 
> 
> I think my Ubuntu has PyGTK and GTK both already installed. But
> however when I am importing "gtk" in Python interactive mode then I am
> getting the following warning:
> 
> (.:4126): Gtk-WARNING **: Unable to locate theme engine in
> module_path: "pixmap",
> 
> (.:4126): Gtk-WARNING **: Unable to locate theme engine in
> module_path: "pixmap",
> 
> (.:4126): Gtk-WARNING **: Unable to locate theme engine in
> module_path: "pixmap",
> 
> (.:4126): Gtk-WARNING **: Unable to locate theme engine in
> module_path: "pixmap",
> 
> On the other side, importing "PyGTK" works well. What might be error?

This is an Ubuntu configuration error and nothing to do with Python or
PyGTK.  As Russell Smith notes you need to get
Aptitude/Apt-Get/Synaptic/...whatever package manager you use... to
ensure the pixmap engine is installed.  Of course one has to ask why you
are using that GTK engine, there are many other far better ones.

It is also worth noting that PyGTK is effectively deprecated. PyGTK only
works with GTK+2 it is not ported to GTK+3.  Instead PyGObject is the
way of working with GTK+3 from Python.

http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html


-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] threading mind set

2012-05-13 Thread Russel Winder
Steven,

On Sun, 2012-05-13 at 10:22 +1000, Steven D'Aprano wrote:
> carlo locci wrote:
> > Hello All,
> > I've started to study python a couple of month ago(and I truly love it :)),
> > however I'm having some problems understanding how to modify a sequential
> > script and make it multithreaded (I think it's because I'm not used to
> > think in that way), 
> 
> No, that's because multithreading and parallel processing is hard.

Shared memory multithreading may be hard due to locks, semaphores,
monitors, etc., but concurrency and parallelism need not be hard. Using
processes and message passing, using dataflow, actors or CSP,
parallelism and concurrency is far more straightforward. Not easy,
agreed, but then programming isn't easy.

> > as well as when it's best to use it(some say that
> > because of the GIL I won't get any real benefit from threading my script).
> 
> That depends on what your script does.
> 
> In a nutshell, if your program is limited by CPU processing, then using 
> threads in Python won't help. (There are other things you can do instead, 
> such 
> as launching new Python processes.)

The GIL in Python is a bad thing for parallelism. Using the
multiprocessing package or concurrent.futures gets over the problem.
Well sort of, these processes are a bit heavyweight compared to what can
be achieved on the JVM or with Erlang.

> If your program is limited by disk or network I/O, then there is a 
> possibility 
> you can speed it up with threads.

Or better still use an event based system, cf Twisted.

[...]
> 

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hello~

2012-05-13 Thread Russel Winder
On Mon, 2012-05-14 at 00:19 +0100, Mark Lawrence wrote:
[...]
> Sorry but it's unreadable to me.  Have you sent this in HTML when you 
> should have sent in plain text?

I think it is just line wrapping, email still is supposed to have no
lines greater that 78 characters (RFC 2822) and some email clients
enforce this on sending by amending what the author thought they sent.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] threading mind set

2012-05-13 Thread Russel Winder
On Mon, 2012-05-14 at 10:31 +1000, Steven D'Aprano wrote:
[...]
> No hard compared to what?

Compared to sequential programming.

[...]
> My argument is that once you move beyond the one-operation-after-another 
> programming model, almost any parallel processing problem is harder than the 
> equivalent sequential version, inherently due to the parallelism. Except 
> perhaps for "embarrassingly parallel" problems, parallelism adds complexity 
> even if your framework abstracts away most of the tedious detail like 
> semaphores.
> 
> http://en.wikipedia.org/wiki/Embarrassingly_parallel
> 
> Once you move beyond sequential execution, you have to think about issues 
> that 
> don't apply to sequential programs: how to divide the task up between 
> processes/threads/actors/whatever, how to manage their synchronization, 
> resource starvation (e.g. deadlocks, livelocks), etc.

Actor systems, dataflow systems and CSP (Communicating Sequential
Processes), do not guarantee lack of deadlock or livelock, but the whole
"processes communicating by passing messages not by sharing data" make
it hugely easier to reason about what is happening.

Moreover if like with CSP, your actors or dataflow systems enforce
sequential actors/operators then it gets even better.

The secret to parallel processing (in general, there are always
exception/corner cases) is to write sequential bits that then
communicate using queues or channels.

No semaphores. No locks. No monitors. These are tools for operating
systems folk and for folk creating actor, dataflow and CSP queues and
channels.

> We have linear minds and it doesn't take that many real-time parallel tasks 
> to 
> overwhelm the human brain. I'm not saying that people can't reason in 
> parallel, because we clearly can and do, but it's inherently harder than 
> sequential reasoning.

I think if you delve into the psychology of it, our minds are far from
linear. Certainly at the electro-chemical level the brain is a massively
parallel machine.

Over the last 50 years, we have enshrined single processor, single
memory into our entire thinking about computing and programming. Our
education systems enforce sequential programming for all but the final
parallel programming option. The main reason for parallel programming
being labelled hard is that we have the wrong tools for reasoning about
it. This is the beauty of the 1960s/1970s models of actors, dataflow and
CSP, you deconstruct the problem into small bits each of which are
sequential and comprehensible, then the overall behaviour of the system
is an emergent property of the interaction between these small
subsystems.

Instead of trying to reason about all the communications systems wide,
we just worry about what happens with a small subsystem.

The hard part is the decomposition. But then the hard part of software
has always been the algorithm.

You highlight "embarrassingly parallel" which is the simplest
decomposition possible, straight scatter/gather, aka map/reduce. More
often that not this is handled by a façade such as "parallel reduce".

It is perhaps worth noting that "Big Data" is moving to dataflow
processing in a "Big Way" :-) Data mining and the like has been
revolutionized by changing it's perception of algorithm and how to
decompose problems. 

[...]
> Python doesn't have a GIL. Some Python implementations do, most obviously 
> CPython, the reference implementation. But Jython and IronPython don't. If 
> the 
> GIL is a problem for your program, consider running it on Jython or 
> IronPython.

It is true that Python doesn't have a GIL, thanks for the correction.
CPython and (until recently) PyPy have a GIL. The PyPy folk are
experimenting with software transactional memory (STM) in the
interpreter to be able to remove the GIL. To date things are looking
very positive. PyPy will rock :-)

Although Guido had said (EuroPython 2010) he is happy to continue with
the GIL in CPython, there are subversive elements (notable the PyPy
folk) who are trying to show that STM will work with CPython as well.

Jython is sadly lagging behind in terms of versions of Python supported
and is increasingly becoming irrelevant -- unless someone does something
soon. Groovy, JRuby and Clojure are the dynamic languages of choice on
the JVM.

IronPython is an interesting option except that there is all the FUD
about use of the CLR and having to buy extortion^H^H^H^H^H^H^H^H^H
licencing money to Microsoft. Also Microsoft ceasing to fund IronPython
(and IronRuby) is a clear indicator that Microsoft have no intention of
supporting use of Python on CLR. Thus it could end up in the same state
as Jython.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...

Re: [Tutor] extracting lines between patterns.

2012-05-14 Thread Russel Winder
On Mon, 2012-05-14 at 09:31 +0200, Bala subramanian wrote:
> Friends,
> Could someone please give some hint on how to extract lines between two
> patterns in a file. I use the re module to compile my patterns but not able
> to figure out how i can use these patterns to extract the lines lying in
> between.

Without the source code you already have, it is difficult to provide any
constructive suggestions.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] extracting lines between patterns.

2012-05-14 Thread Russel Winder
On Mon, 2012-05-14 at 12:23 +0200, Bala subramanian wrote:
[...]
> mypat=re.compile(r'^[ atomtypes ]$[ moleculetype ]',re.MULTILINE)
[...]

mypat=re.compile(r'^\[ atomtypes \]$(.*)^\[ moleculetype \]$',re.MULTILINE | 
re.DOTALL)

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-15 Thread Russel Winder
On Mon, 2012-05-14 at 23:38 -0400, bob gailer wrote:
[...]
> I would set up a SQLite database with a table of 4 numeric columns: 
> year, month, rainfall, firearea
> Use SQL to select the desired date range and do the max and avg 
> calculations:
> select year, avg(firearea), max(rainfall) from table where year = 1973 
> and month between 6 and 8)
> 
> you can use dictionaries but that will be harder. Here a start 
> (untested). Assumes data are correct.

Clearly if the data is to be stored for a long time and have various
(currently unknown) queries passed over it then year a database it the
right thing -- though I would probably choose a non-SQL database.

If the issues is to just do quick calculations over the data in the file
format then nothing wrong with using dictionaries or parallel arrays à
la:

with open ( 'yearmonthrainfire.txt' ) as infile :
climateindexname = infile.readline ( ).split ( )
data = [ line.split ( ) for line in infile.readlines ( ) ]

years = sorted ( { item[0] for item in data } )
months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 
'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ]

dataByYear = { year : [ ( float ( item[2] ) , float ( item[3] ) ) for 
item in data if item[0] == year ] for year in years } 
dataByMonth = { month : [ ( float ( item[2] ) , float ( item[3] ) ) for 
item in data if item[1] == month ] for month in months }

averagesByYear = { year : ( sum ( dataByYear[year][0] ) / len ( 
dataByYear[year][0] ) , sum ( dataByYear[year][1] ) / len ( dataByYear[year][1] 
) ) for year in years }
averagesByMonth = { month : ( sum ( dataByMonth[month][0] ) / len ( 
dataByMonth[month][0] ) , sum ( dataByMonth[month][1] ) / len ( 
dataByMonth[month][1] ) ) for month in months }

for year in years :
print ( year , averagesByYear[year][0] , averagesByYear[year][1] )

for month in months :
print ( month , averagesByMonth[month][0] , 
averagesByMonth[month][1] )

The cost of the repetition in the code here is probably minimal compared
to the disc access costs. On the other hand this is a small data set so
time is probably not a big issue.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-16 Thread Russel Winder
On Tue, 2012-05-15 at 19:14 +0100, Alan Gauld wrote:
> On 15/05/12 10:36, Russel Winder wrote:
> > ...queries passed over it then year a database it the
> > right thing -- though I would probably choose a non-SQL database.
> 
> As a matter of interest why?

Because there are alternatives that need to be investigated on a per
problem basis for the best database.

SQL
MongoDB
CouchDB
Cassandra
Neo

etc. Python only has SQLite3 as standard but there are alternatives. I
have been using PyMongo quite successfully.

> And what kind of alternative would you use?

See above ;-)

> It seems to me that SQL is ideally suited(*) to this type of role. I'm 
> curious what the alternatives might be and why they would be preferred?
> 
> (*)Because: Flexible query language, wide set of tools including GUI 
> query builders, reporting tools etc. Plus easy integration with 
> programming environments, scaleability (less an issue here), 
> security(usually) etc.

It is not clear that the original table works better with the relational
model compared to one of the key-value stores or document stores. It
might. But I no longer always go to SQL for persistence as I used to a
few years ago.

There are various articles around the Web comparing and contrasting
these various models. Some of the articles are even reasonable :-)


-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Russel Winder
On Wed, 2012-05-16 at 12:57 -0400, Joel Goldstick wrote:
[...]
> I think the OP is just learning and this thread may have gotten of track.

I didn't realize discussion of immediate side issues and alternatives,
and allowing people to exchange information was OT in this mailing list.
Also of course, OP didn't mention databases, but was asking how to do it
with lists and dictionaries. I think there is irony somewhere in here.

> Here is some code to get started.  I decided to use sqlite3 since its
> easy to use with python -- no finding and learning to load packages.
> 
> 
> #!/usr/bin/env python
> 
> import sqlite3 as db
> 
> # Ideally this shouldn't be global, but in this short code snippet it
> gets the job done
> # here we create a database and get a cursor
> conn = db.connect('climate.db')
> cursor = conn.cursor()
> print cursor

I believe that there are more problems than just global data here. One
obvious thing is this code is not safe against exceptions. I appreciate
this is a trivially small program, but I think it important that sample
code presents good style or explicitly states what is wrong so as to
present what not to do. Your comment about global sits well with this,
but for me doesn't go far enough. Python introduced context managers and
the with statement exactly for this sort of situation, following the
lead of C++ with RAII. I think we should all get into the habit of using
the with statement automatically in this situation.

> # this will create a table for our data
> sql_create = """CREATE TABLE if not exists rain (
> id INTEGER PRIMARY KEY,
> year INTEGER,
> month TEXT(3),
> rainfall FLOAT,
> fire_area FLOAT
> )"""
> 
> # this will read the data file and put it in our database
> def populate_climate_table(file_name):
> """
> reads the file_name and insert data into sqlite table
> """
> sql_insert_string = "insert into rain (year, month, rainfall,
> fire_area) values (%d, '%s', %f, %f)"
> 
> f = open(file_name)

Same comment about context managers and with statement applies here:
this code is not exception safe.

> f.readline() # get rid of column headers
> for l in f.readlines():
> data_list = l.split()
> print data_list
> sql_insert = sql_insert_string % (int(data_list[0]),
> data_list[1], float(data_list[2]), float(data_list[3]))

Should we be promoting use of the format method in strings rather than
the % operator? % is deprecated now.

Although not an issue here, this sort of SQL string manipulation is at
the heart of SQL injection attacks and so should be frowned upon. Hence
SQLAlchemy's expression languages, which goes some way to avoiding the
whole issue.  At the expense of having to load an additional package.
With package management on Debian/Fedora/Ubuntu/MacPorts or the pip
command this is not difficult to add. 

> print sql_insert
> cursor.execute(sql_insert)
> conn.commit()
> 
> 
> if __name__ == '__main__':
> 
> print sql_create
> cursor.execute(sql_create)
> populate_climate_table('data.txt')
> 
> 
> So, I haven't solved all of the questions with this code.  The next
> thing to do is to read a little about sqlite select statements.
> for example: sqlite> select sum(rainfall)/count(*) from rain;
> 3.97352768125
> 
> This statement will give the average rainfall over the complete dataset.
> To get the ave rainfall for a given year do this:
> sqlite> select sum(rainfall)/count(*) from rain where year = 1983;
> 
> Come back with more questions

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Russel Winder
On Wed, 2012-05-16 at 16:03 +0100, Alan Gauld wrote:
[...]
> I agree, but in this case SQL seemed like the most likely fit of the 
> ones I knew. however:

Which raises the point that the best design of a given problem in a
given context is the one that is most comprehensible to the people
directly involved.

> >  SQL
> >  MongoDB
> 
> I know about these

I have it on good authority yesterday that MongDB is only properly
useful in a single store context, i.e. not a replicated cluster.  Along
withthis comes news that Riak is very good and has a Python API.

> 
> >  CouchDB
> >  Cassandra
> >  Neo
> 
> These are new to me.

CouchDB is an Erlang implemented system. Ubuntu One uses this for
example.  Cassandra is an Apache project, a JVM-based system. MongoDB,
CouchDB and Cassandra are "document stores". Neo4J is a graph repository
so of a very different architecture and performance characteristics. And
then there is Redis :-)

> 
> > etc. Python only has SQLite3 as standard but there are alternatives. I
> > have been using PyMongo quite successfully.
> 
> Python comes with several storage/access options including shelve, gdbm, 
> ldap, cobfig files, XML, in addition to SQL.

Indeed. The problem I have had with shelve for this sort of thing is
that is is critically dependent on the pickling algorithm and so
potentially Python version dependent.

[...]
> on flexiblity of data format. The OPs requirements suggested intelligent 
> filtering of a fixed record format which is one of the areas where SQL 
> works well. The other side of the coin is that the data is essentially 
> single table so the relationship management aspects of SQL would not be 
> needed. So I agree we don't have enough detail
> to be 100% sure that another option would not work as well or better.

The signpost here is that the table as is is likely not in third normal
form, and that if the problem currently being solved was actually a
small part of a bigger problem, this issue would need to be addressed.

> But most other options require learning new (often bespoke) query 
> languages and have limited user tools. All of these factors need to be 
> included too. Mongo et al tend to be better suited, in my experience, to 
> machine access applications rather than end user access.

Agreed. Unfortunately, vendor commercial issues often get in the way of
experimenting to find out where the NoSQL systems are genuinely better
than SQL ones. We will get there though.

Interesting, or not, the "Big Data" people are rapidly realizing that
data mining and SQL are mutually incompatible. The current trend is
towards streaming whole databases through dataflow programs. But Java
rather than Python is the default language in that world.

> > There are various articles around the Web comparing and contrasting
> > these various models. Some of the articles are even reasonable :-)
> 
> Wikipedia is my friend :-)

:-)

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-18 Thread Russel Winder
On Thu, 2012-05-17 at 19:35 +1000, Steven D'Aprano wrote:
> On Thu, May 17, 2012 at 08:27:07AM +0100, Russel Winder wrote:
> 
> > Should we be promoting use of the format method in strings rather than
> > the % operator? % is deprecated now.
> 
> It most certainly is not.
> 
> There are no plans to deprecate the string % operator any time in the 
> foreseeable future. It may, hypothetically, wither away from lack of use 

OK I am clearly wrong with the statement I made.  I had assumed the
statement (*) that it would be deprecated in 3.1 was carried through, I
had not actually checked the reality in the 3.1 and 3.2 differences
documents.  My apologies for misdirection, thanks for pulling me up on
this.


(*) There is no statement of when deprecation would happen in PEP 3101
itself http://www.python.org/dev/peps/pep-3101/ but there is an explicit
statement in
http://docs.python.org/release/3.0/whatsnew/3.0.html#pep-3101-a-new-approach-to-string-formatting
 which clearly didn't happen even though it led to a lot of people thinking it 
would.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Joining all strings in stringList into one string

2012-05-30 Thread Russel Winder
On Wed, 2012-05-30 at 12:21 -0400, Akeria Timothy wrote:
[...]
> def joinStrings(stringList):
>  string = []

indentation error in that the above line and the below line should have
the same indent level.  Also the above line and the following line are
both definitions of the variable string so the above is actually
redundant.

> for string in stringList:
> print ''.join(stringList)

Given the variable doesn't appear in the block I wonder if the code
reflects the intended algorithm?

> 
> def main():
> print joinStrings(['very', 'hot', 'day'])
> print joinStrings(['this', 'is', 'it'])
> print joinStrings(['1', '2', '3', '4', '5'])
> 
> main()

The above code, with the trivial indent fix, outputs:

veryhotday
veryhotday
veryhotday
None
thisisit
thisisit
thisisit
None
12345
12345
12345
12345
12345
None

Is this what was desired?  I am tempted to think that actually what was
desired was:

veryhotday
thisisit
12345

in which case I would suggest the code should perhaps read:

def main():
print ''.join(['very', 'hot', 'day'])
print ''.join(['this', 'is', 'it'])
print ''.join(['1', '2', '3', '4', '5'])

if __name__ == '__main__':
main()

but, mayhap, I am missing the intention.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Traffic Light

2013-01-20 Thread Russel Winder
On Sat, 2013-01-19 at 12:31 -0800, anthonym wrote:
> Hello All,
> 
> I am new to Python and taking a course now.  One of my exercises is to
> create a traffic light using tkinter.   The exercise also requires that I
> create radio buttons that allow the user to click on the color and the
> corresponding light will come on.

A side-note here rather than a direct contribution to your problem at
hand:  radio buttons are not a good way of implementing traffic lights
if internationalization is a factor. In the UK and other places, there
is a state where both red and yellow are on at the same time.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help!

2013-01-26 Thread Russel Winder
Following up on Jos Kerc's answer:
 
On Fri, 2013-01-18 at 07:56 -0500, Carpenter, Steven wrote:
[…]
> print(math.acos(((a**2)+(b**2)-(c**2))/(2(a*b

2(a*b) → 2 * (a * b)

> TypeError: 'int' object is not callable

Juxtaposition does not imply multiplication in Python as it does in
mathematics.
-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need a mentor, Any help would be great

2013-03-03 Thread Russel Winder
On Sun, 2013-03-03 at 08:29 +, Alan Gauld wrote:
[…]
> Once comfortable go to SourceForge and find something in Python you can 
> contribute too. Testing and Bug fixing is often a good place to start.

Probably should flag BitBucket and GitHub as well ?


-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need a mentor, Any help would be great

2013-03-03 Thread Russel Winder
On Sun, 2013-03-03 at 13:51 +0100, Kwpolska wrote:
> On Sun, Mar 3, 2013 at 10:55 AM, Russel Winder  wrote:
> > On Sun, 2013-03-03 at 08:29 +, Alan Gauld wrote:
> > […]
> >> Once comfortable go to SourceForge and find something in Python you can
> >> contribute too. Testing and Bug fixing is often a good place to start.
> >
> > Probably should flag BitBucket and GitHub as well ?
> 
> Not “as well”, “instead”.  Because SF is anywhere near human-friendly,
> and many awesome coders are on BitBucket (eg. the Pocoo team) and
> GitHub (eg. Kenneth Reitz), you can also find some good people on
> Launchpad.

Actually I was thinking "instead" :-)

I am less sure about Launchpad these days. For whatever reasons Bazaar
is having Canonical resource removed so has no development momentum. It
is a great DVCS but Mercurial and especially Git have the mindshare.

Although Python (and Java) are in Mercurial, there seems to be a trend
to say Git is the winner of the DVCS war. 

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] some web and python version questions

2013-03-04 Thread Russel Winder
On Mon, 2013-03-04 at 17:58 +, Alan Gauld wrote:
[…]
> There are so many web frameworks for Python that asking which is bet is 
> rather pointless. They are all fairly good, some are very good for 
> specific tasks, others are more general. The main thing to start with is 
> pick one and learn it. You can switch later once you understand the 
> concepts but stick with one until you do.

I think it probably is worth dividing the "full stack" frameworks from
the microframeworks since although the latter can be combined with other
frameworks to emulate the former, they can serve better for some web
applications that do not need big database support.

> My personal recommendation would be either to go direct to Django
> or try CherryPy. But there are plenty of advocates for the other options 
> too. It really is less important which one you choose, rather focus on 
> understanding the concepts of  translating urls to Python methods, using 
> templates to isolate code from presentation and using databases to store 
> data. The frameworks all facilitate these features in one way or another.

Many will argue that there are better full stack frameworks, but as of
today Django is  definitely a "not wrong" proposal. Principally because
the documentation is very extensive and there is an excellent tutorial
using TDD at http://www.tdd-django-tutorial.com/

For microframeworks, Bottle is getting a lot of good press. Flask also
except that it doesn't have a Python 3 presence as far as I know just
now.


-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I need a good resource for python Django

2013-08-19 Thread Russel Winder
On Sat, 2013-08-17 at 21:29 +0530, Arun Kumar wrote:
> Hi,
> 
> Can anyone suggest me a good resource for python Django. I've gone through
> the official website of Django but it is of limited use to me. Any help on
> this would be highly appreciated.

You have given no indication as to why you consider the website not to
have given you what you need so we have no information to try and direct
you to useful material.

Others have provided so URL of other material, but, for me, the best
"Let's Learn Django" website has to be Harry Percival's Test-Driven
Django Tutorial, it emphasizes TDD and tests where the Django site
rarely mentions them in the introductory material (a gross oversight).
http://www.tdd-django-tutorial.com/

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] learning recursion

2014-02-09 Thread Russel Winder
On Thu, 2014-02-06 at 18:06 -0500, Dave Angel wrote:
[…]
> 
> Code:
> def fib2(n):
>   if n==1:
>   return 1
> 
>   elif n==2:
>   return 1
>   else:
>   return fib2(n-2) +fib2(n-1)
[…]

I suggest it also be pointed out that this form is algorithmically
dreadful. Having transformed the maths to this first cut code, we then
need to consider it as code and shift to a tail recursive form,
iterative form, or at the very least memoize the function. Leaving
students of programming (in any current language) with the idea that
this is a good final solution is, I believe, a disservice. 

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] Python as Teaching Language

2014-02-09 Thread Russel Winder
On Mon, 2014-01-20 at 10:41 +, Oscar Benjamin wrote:
[…]
> f = open('myfile.txt')
> for line in f:
> print(line.upper())
> f.close()

I suggest we even see this as not good code due to the possibility of
I/O exceptions:

with open('myfile.txt') as f:
for line in f:
print(line.upper())

should, I argue, be the canonical idiom in modern Python.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] learning recursion

2014-02-10 Thread Russel Winder
On Sun, 2014-02-09 at 13:56 -0500, Dave Angel wrote:
[…]
> Not as bad as attributing that code to me.

Apologies for that implication, bad editing on my part, I should have
retained the link to the author.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] Python as Teaching Language

2014-02-10 Thread Russel Winder
On Sun, 2014-02-09 at 13:36 +, Oscar Benjamin wrote:
[…]
> I agree entirely, but what you've overlooked is that my examples are
> carefully targeted at a particular part of a tutorial-based class.
> We're talking about iteration so this is quite early in the course. At
> this stage I want my students to understand that closing a file is an
> explicit action that needs to occur. Later in the course I will teach
> exception handling and explain that the above should be rewritten as
> 
> f = open('myfile.txt')
> try:
> for line in f:
> print(line.upper())
> finally:
> f.close()
> 
> Shortly after that we will look at using (but not creating) context
> managers and I'll explain that file objects are also context managers.

Works for me. Personally I would ensure I put in forward signposts at
the time of covering earlier codes to ensure people realize there is a
progression and that the current code is initial not final.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] Python as Teaching Language

2014-02-10 Thread Russel Winder
On Mon, 2014-02-10 at 00:17 +, Alan Gauld wrote:
[…]
> And in my tutorial I deliberately don't teach many of the "standard" 
> Python idioms because I'm trying to teach programming rather than 
> Python. So if python has an insanely great way to do stuff but virtually 
> no other language has it I will ignore it. (Or more
> likely mention it as an aside/footnote.)

In the case of file handling and the with statement, indeed any resource
management, it is a standard idiom across languages so well worth
covering in Python: RAII in C++, ARM in Java, etc. 

> What's interesting (to me) is that I'm currently working on a new
> project aimed at beginners who have progressed beyond the first
> steps but are not confident in putting together a bigger program.
> That is allowing me to address many of the idiomatic aspects
> of Python that my first book didn't permit. It means that although there 
> is some overlap in coverage the style and content are quite different.
> 
> Context and target make a big difference in what and how you teach.

Definitely. Good luck with the new project.

-- 
Russel.
=========
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] Recommendation For A Complete Noob

2014-02-12 Thread Russel Winder
On Tue, 2014-02-11 at 23:25 +, Bob Williams wrote:
[…]
> I'm also a noob. In addition to all the excellent recommendations
> already posted, I would suggest setting yourself a task to solve in
> python. It's far more interesting than working through the examples in
> most books - the challenge is solving each problem as you come across
> them. Google is excellent.

Definitely. What you also need is to get feedback on your code from
another person. Knowing whether your solution is good, Pythonic, or
needs more work, is separate from does it pass its tests and does it do
what it should.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: [Tutor] c++ on python

2014-03-13 Thread Russel Winder
On Wed, 2014-03-12 at 22:05 +, Alan Gauld wrote:
> On 12/03/14 16:49, Stefan Behnel wrote:
> > Alan Gauld, 12.03.2014 10:11:
> >> If it were a library then you would have to call
> >> the individual C++ functions directly using
> >> something like ctypes, which is usually more
> >> complex.
> >
> > ctypes won't talk to C++, but Cython can do it quite easily.
> 
> I thought it would work provided the interface functions
> were declared as C functions? That might involve
> writing a wrapper around it but that is usually
> trivial if you have to compile the source anyway.

ctypes (and CFFI) talks quite happily to C++ functions as long as they
are declared with C linkage (so as to avoid any "name mangling"):

export "C" x f(…){…}

makes f accessible via ctypes if f is in a shared object/dynamic link
library.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] c++ on python

2014-03-13 Thread Russel Winder
On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote:
[…]
> The thing is: if you have to write your own wrapper anyway (trivial or
> not), then why not write it in Cython right away and avoid the intermediate
> plain C level?

If the task is two write an adapter (aka wrapper) then perhaps use SWIG
whcih is easier for this task than writing Cython code.

> It's usually much nicer to work with object oriented code on both sides
> (assuming you understand the languages on both sides), than to try to
> squeeze them through a C-ish API bottleneck in the middle.

It could be that "object oriented" is a red herring. Without details (*)
of what it is about the C++ code that is the connection between Python
and C++, it is difficult to generalize.

ctypes can be a real pain when trying to call C++ from Python using
argument values that are not primitive types. CFFI solves (currently
much, soon most) of this problem by addressing the adapter between
Python and C++ in a different way to that employed by ctypes. In both
cases, both are a lot easier than writing Cython code. 


(*) I may have just missed this detail in which case apologies.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing from python

2014-03-14 Thread Russel Winder
On Fri, 2014-03-14 at 11:40 +0100, Ulrich Goebel wrote:
> Hallo,
> 
> is there another way to pritt a PDF form python?
> 
> My problem is a PDF which is printed well by evince, but not with lp, 
> even not out of python using
> 
> os.system("lp some_options file.pdf")

Python is deprecating os.system in favour of using the subprocess
package.

subprocess.call('lp some_options file.pdf', shell=True)

or

subprocess.call(['lp', *some_options, 'file.pdf')

assuming some_options is a sequence of strings, on option per string.

> Later on I have to build the PDF in a python program (using reportlab) 
> and then print it, even from the python program.
> 
> So I look for a (nice documented) library which give access to the CUPS 
> API or something else to print the PDF from python.
> 
> Any help is welcome!

Debian Sid has packages python-cups and python-cupshelpers which are
Python 2 APIs for working with CUPS. Sadly there do not seem to be
Python 3 versions of these In Debian, but the package in PyPI is pycups
so can be made available to Pytho 3. Also: 

http://cyberelk.net/tim/software/pycups/

I have not used these yet myself, but I have trying them out on my
agenda for later in the spring.

-- 
Russel.
=====
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor