Re: Question About Command line arguments

2011-06-10 Thread Kurt Smith
On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips
 wrote:
> How do I write my script so it picks up argument from the output of commands
> that pipe input into my script?

def main():
import sys
print sys.stdin.read()

if __name__ == '__main__':
main()

$ echo "fred" | python script.py
fred
$
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange set of errors

2007-08-03 Thread Kurt Smith
Sorry, forgot to "Reply to all."

On 8/3/07, Stephen Webb <[EMAIL PROTECTED]> wrote:
> Greetings all,

<>

> Also, I've been having trouble with the plot function in matplotlib. For
> example, I enter the following in the terminal:
>
>  >>> from pylab import *
> >>> plot([1,2,3])
> []
>

I can help you with your second problem: matplotlib is doing what it
should, all you need to do is tell it to show() the figure you created
(with the plot on it.)  Note -- if you call the show() function, you
will turn control over to the backend (in my case, TkAgg), and lose
the ability to issue interactive commands afterwards.  A solution is
to use the ion() (stands for "interactive-on") function call before
issuing plotting commands:

>>> from pylab import *
>>> ion()
>>> plot([1,2,3])
[]
>>> # image is shown here

See http://matplotlib.sourceforge.net/interactive.html for an
exhaustive explanation.

> Every time I run the plot([1,2,3]) I get a different ending number that
> seems to vary randomly.

The "[]" is what the
plot() function returns -- a list of instances of the Line2D class,
and it tells you their locations in memory (hence the hex number
starting with 0x).  The hex number can be used to uniquely identify
this object, as in the id(object) call.

Hope this helps,

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange set of errors

2007-08-03 Thread Kurt Smith
On 8/3/07, Stephen Webb <[EMAIL PROTECTED]> wrote:
> Greetings all,
>
> I've recently begun using Python to do scientific computation, and I wrote
> the following script to find approximate eigenvalues for a semi-infinite
> matrix:
>
>
>  from pylab import *
> from numpy import *
> from scipy import *
>
> def bandstructure(N,s):
>
> b = s/4.0
>
> jmax = 10 + N**2
>
> spectrum1 = [0]*2*N
> spectrum2 = [0]*2*N
> spectrum3 = [0]*2*N
>
>
> for k in arange(1, 2*N+1, 1):
>
> A = zeros( (jmax,jmax) )
>
> i = 0
> while i <= jmax-1:
> if i <= jmax-2:
> A[i,i+1] = b
> A[i+1,i] = b
> A[i,i] = ((k + 2.0*i*N)/N)**2
> i = i+1
> else:
> A[i,i] = ((k + 2.0*i*N)/N)**2
> i = i+1
>
> #This portion of the code builds a matrix twice as large to check
> against
>
> B = zeros( (2*jmax,2*jmax) )
>
> i = 0
> while i <= 2*jmax-1:
> if i <= 2*jmax-2:
> B[i,i+1] = b
> B[i+1,i] = b
> B[i,i] = ((k + 2.0*i*N)/N)**2
> i = i+1
> else:
> B[i,i] = ((k + 2.0*i*N)/N)**2
> i = i+1
>
> x = linalg.eigvals(A)
> y = linalg.eigvals(B)
>
> j = 1
> while j<=3:
> if abs(y[j]-x[j]) <= 10.0**(-5):
> j = j + 1
> else:
> print 'jmax not large enough to obtain accurate results'
>
> spectrum1[k-1] = x[0] + 0.5*s
> spectrum2[k-1] = x[1] + 0.5*s
> spectrum3[k-1] = x[2] + 0.5*s
>
> plot (k, spectrum1, k, spectrum2, k, spectrum3)
>
> xlabel('k (energy level)')
> ylabel('E/E_r')
> title('Finite Size Band Structure, N = %d, s = %f' % (N, s))
> grid(true)
>
>
> When I run this script, I get the following message, which I can't figure
> out:
>
>  Traceback (most recent call last):
>   File "", line 1, in 
>   File "bandstruc.py", line 61, in bandstructure
> plot (k, spectrum1, k, spectrum2, k, spectrum3)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pylab.py",
> line 2028, in plot
> ret =  gca().plot(*args, **kwargs)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py",
> line 2535, in plot
> for line in self._get_lines(*args, **kwargs):
>   File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py",
> line 437, in _grab_next_args
> for seg in self._plot_2_args(remaining[:2], **kwargs):
>   File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py",
> line 337, in _plot_2_args
> x, y, multicol = self._xy_from_xy(x, y)
>   File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py",
> line 266, in _xy_from_xy
> nrx, ncx = x.shape
>  ValueError: need more than 0 values to unpack
>


Basically, you're trying to plot spectrum[123] vs. k: the spectra are
lists, but k is an integer.  I made some modifications to get things
working more smoothly.  Note the ion() function call at the beginning,
and the use of "indx = arange(1,2*N+1)" instead of plotting vs. k.

There's probably some other things that could be cleaned up, but I'm
short on time right now; this will get your code working at least.

Hope this helps,
(From a fellow physicist)
Kurt

##

from pylab import *
from numpy import *

ion()

def bandstructure(N,s):

b = s/4.0

jmax = 10 + N**2

spectrum1 = [0]*2*N
spectrum2 = [0]*2*N
spectrum3 = [0]*2*N


for k in arange(1, 2*N+1, 1):

A = zeros( (jmax,jmax) )

i = 0
while i <= jmax-1:
if i <= jmax-2:
A[i,i+1] = b
A[i+1,i] = b
A[i,i] = ((k + 2.0*i*N)/N)**2
i = i+1
else:
A[i,i] = ((k + 2.0*i*N)/N)**2
i = i+1

#This portion of the code builds a matrix twice as large to
check against

B = zeros( (2*jmax,2*jmax) )

i = 0
while i <= 2*jmax-1:
if i <= 2*jmax-2:
B[i,i+1] = b
B[i+1,i] = b
B[i,i] = ((k + 2.0*i*N)/N)**2
i = i+1
else:
B[i,i] = ((k + 2.0*i*N)/N)**2
i = i+1

x = linalg.eigvals(A)
y = linalg.eigvals(B)

j = 1
while j<=3:
if abs(y[j]-x[j]) <= 10.0**(-5):
j = j + 1
else:
print 'jmax not large enough to obtain accurate results'

spectrum1[k-1] = x[0] + 0.5*s
spectrum2[k-1] = x[1] + 0.5*s
spectrum3[k-1] = x[2] + 0.5*s

indx = arange(1,2*N+1, 1)
plot (indx, spectr

Re: Drawing a graph

2007-08-13 Thread Kurt Smith
On 8/12/07, Ghirai <[EMAIL PROTECTED]> wrote:
> Hello list,
>
> I need to draw a graph, 2 axes, 2D, nothing fancy.
> One of the axes is time, the other one is a series of integers.
>
> I don't care much about the output format.
>
> Are there any specialized libraries for this, or should i use PIL?
>
> Thanks.
>
> --
> Regards,
> Ghirai.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Checkout matplotlib: http://matplotlib.sourceforge.net/

Powerful (esp. with IPython), clean implementation, very nice output,
IMHO.  It does depend on some other things; primarily numpy and some
sort of backend for displaying figures (usually can use a default
backend -- e.g. Tkinter).  Otherwise you can plot directly to file.
May not be what you're looking for if you want something quick.

HTH,

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to assign a function to another function

2007-09-17 Thread Kurt Smith
On 9/17/07, Stefano Esposito <[EMAIL PROTECTED]> wrote:
> Hi all
>
> what i'm trying to do is this:
>
> >>>def foo ():
> ... return None
> ...
> >>>def bar ():
> ... print "called bar"
> ...
> >>>def assigner ():
> ... foo = bar
> ...

You need to tell "assigner()" that foo doesn't belong to the local
(function) namespace, but rather comes from the global namespace:

In [1]: def foo():
   ...: return None
   ...:

In [2]: def bar():
   ...: print "called bar"
   ...:
   ...:

In [3]: def assigner():
   ...: global foo
   ...: foo = bar
   ...:
   ...:

In [4]: assigner()

In [5]: foo()
called bar

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What does the syntax [::-1] really mean?

2007-10-04 Thread Kurt Smith
On 10/4/07, Casey <[EMAIL PROTECTED]> wrote:
[snippage]
>
> Following the reference to section 3.2 provides a (non-rigorous)
> description of what a slice object is, in terms of the extended
> slicing semantics.  But it doesn't shed any additional light on the
> meaning of [::-1].
>
> >From this, I would expect that x[::-1] would be identical to x[n:0:-1]
> (n and 0 being the "end" values, with the order switched due to the
> negative step value).  But the clause that "(but never including j)"
> means that x[n:0:-1] excludes the 1st element of x, x[0].  A quick
> test in ipython confirms that "abc"[3:0:-1] => "cb", not "cba".
> Changing the "end" value  to x[n:-1:-1] results in an empty string.

Check it out:

>>> 'abc'[3:None:-1]
'cba'
>>>

The second argument to the slice object, if negative, will convert
this index to (len(x)+j), which is why 'abc'[3:-1:-1] == 'abc'[2:2:-1]
== ''.  If you pass in None for the 1st or second index, the slice
object will compute the right bounds based on the __len__ of the
object, and do the right thing.  When there is no value specified,
None is assumed.


>
> So my question is: "what exactly is [::-1] shorthand for"?  Or is it a
> special case, in which case why isn't it  defined as such in the
> library?

obj[::-1] == obj[None:None:-1] == obj[slice(None,None,-1)]

>>> 'abc'[::-1]
'cba'
>>> 'abc'[None:None:-1]
'cba'
>>> 'abc'[slice(None,None,-1)]
'cba'

Taking a look at the slice class:

 |  indices(...)
 |  S.indices(len) -> (start, stop, stride)
 |
 |  Assuming a sequence of length len, calculate the start and stop
 |  indices, and the stride length of the extended slice described by
 |  S. Out of bounds indices are clipped in a manner consistent with the
 |  handling of normal slices.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbi Q: Recursively reverse lists but NOT strings?

2007-10-15 Thread Kurt Smith
On 10/15/07, Gary Herron <[EMAIL PROTECTED]> wrote:
> Dmitri O.Kondratiev wrote:
> > Gary, thanks for lots of info!
> > Python strings are not lists! I got it now. That's a pity, I need two
> > different functions: one to reverse a list and one to reverse a string:
> True, they are not both lists, but they *are* both sequences, with some
> things in common.  In particular xs[::-1] will reverse both types of
> objects.  And even if you roll you own reversal function, you don't need
> two.  One will do.
>
> Gary Herron
>
> >
> > def reverseList(xs):
> > if xs == []:
> > return xs
> > else:
> > return (reverseList (xs[1:])) + [xs[0]]
> >
> > def reverseStr(str):
> > if str == "":
> > return str
> > else:
> > return (reverseStr (str[1:])) + str[0]
> >
> > Ok. Now regarding in-place reversal of a list:
> >
> > >>> l = [1,2,3]
> > >>> l
> > [1, 2, 3]
> > >>> l.reverse()
> > >>> l
> > [3, 2, 1]
> >
> > That was, as I expected. Good.
> >
> > Then why this ? :
> >
> > >>> ls = [1,2,3].reverse()
> > >>> ls
> > >>>
> > >>> print [1,2,3].reverse()
> > None
> > >>>
> > I mean, why ls is empty after assignment?
> >
> > Also, I couldn't find in the Python docs what this form of slicing means:
> > xs[::-1]  ?
> >
> > It works for creating a reversed copy of either a string or a list,
> > but what does '::-1' syntax means?

mylist[::-1] is interpreted as mylist[slice(None,None,-1)], and the
slice object has a method, 'indices' that computes the right endpoints
for what you want to do.

So:

>>> myseq = (1,2,3)
>>> myseq[::-1]
(3, 2, 1)
>>> myseq[slice(None,None,-1)]
(3, 2, 1)
>>> myseq[None:None:-1]
(3, 2, 1)

etc.



> >
> > Thanks,
> >
> > Dmitri O. Kondratiev
> > [EMAIL PROTECTED] 
> > http://www.geocities.com/dkondr
> >
> > On 10/15/07, *Gary Herron* < [EMAIL PROTECTED]
> > > wrote:
> >
> > Dmitri O.Kondratiev wrote:
> > >
> > > The function I wrote (below) reverses lists all right:
> > >
> > > def reverse(xs):
> > > if xs == []:
> > > return []
> > > else:
> > > return (reverse (xs[1:])) + [xs[0]]
> > >
> > >
> > > >>> reverse ([1,2,3])
> > > [3, 2, 1]
> > > >>>
> > >
> > >
> > > Yet when I try to reverse a string I  get:
> > >
> > > >>> reverse ("abc")
> > >
> > > ...
> > > ...
> > > ...
> > >
> > >   File "C:\wks\python-wks\reverse.py", line 5, in reverse
> > >
> > > return (reverse (xs[1:])) + [xs[0]]
> > >
> > >   File "C:\wks\python-wks\reverse.py", line 5, in reverse
> > >
> > > return (reverse (xs[1:])) + [xs[0]]
> > >
> > >   File "C:\wks\python-wks\reverse.py", line 2, in reverse
> > >
> > > if xs == []:
> > >
> > > RuntimeError: maximum recursion depth exceeded in cmp
> > >
> > > >>>
> > >
> > > What's wrong? Why recursion never stops?
> > >
> > If you are doing this as an python-learning exercise, then read
> > on.   If
> > you are doing this reversal for real code, then try:
> >
> >   xs.reverse() for in-place reversal of a list (but not a string), or
> >   result = xs[::-1] for creating a reversed copy of either a
> > string or a
> > list
> >
> >
> > Your recursion stops when xs == [], but when you're stripping
> > characters
> > off a string,  like 'abc', the remaining portion will be 'bc',
> > then 'c',
> > than '', but never [] so you 'll never stop.
> >
> > Try:
> >
> > if xs == []:
> > return []
> > elif xs == '':
> > return ''
> > else:
> > ...
> >
> >
> > Gary Herron
> >
> >
> > >
> > > Thanks,
> > > Dima
> >
> >
> >
> >
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


New subclass vs option in __init__

2007-12-06 Thread Kurt Smith
Hi List:

Class inheritance noob here.

For context, I have the following base class and subclass:

class Base(object):
def __init__(self, val):
self.val = val

class Derived1(Base):
def __init__(self, val):
super(Derived1, self).__init__(val)

I'm curious as to other's thoughts on the following: when
incorporating optional behavior differences for a subclass, do you a)
make a new subclass (e.g., 'Derived2') and override (and add new)
methods that would encapsulate the new behavior, or b) keep the same
subclass around (i.e., 'Derived1'), but add an initialization option
that would specify the different behavior, and check for the value of
this option in the different methods?

It would seem that there are cases where one would be preferable over
the other: a) when the new behavior would modify a large portion of
the existing subclass, making a new subclass would be ideal; b) when
the new behavior changes only slightly the existing subclass, perhaps
a simple default option in the subclass's __init__ method would be
best.  Where is the tipping point?  Since one cannot predict what
direction the new behavior might take things, should one usually err
on the side of a new subclass?  Is option b) just being lazy?  Is a)
too verbose in many situations?

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.popen function with quotes

2008-03-25 Thread Kurt Smith
On Wed, Mar 26, 2008 at 12:15 AM, skunkwerk <[EMAIL PROTECTED]> wrote:
> On Mar 25, 9:25 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
>  wrote:
>  > En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <[EMAIL PROTECTED]>
>  > escribió:
>
> >
>  > >>i'm trying to call subprocess.popen on the 'rename' function in
>  > >> linux.  When I run the command from the shell, like so:
>  >
>  > >> rename -vn 's/\.htm$/\.html/' *.htm
>  >
>  > >> it works fine... however when I try to do it in python like so:
>  > >> p = subprocess.Popen(["rename","-vn","'s/\.htm$/
>  > >> \.html/'","*.htm"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>  >
>  > >> print p.communicate()[0]
>  >
>  > >> nothing gets printed out (even for p.communicate()[1])
>  >
>
> > I'd try with:
>  >
>  > p = subprocess.Popen(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
>  >stdout=subprocess.PIPE, stderr=subprocess.PIPE,
>  >shell=True)
>  >
>  > (note that I added shell=True and I'm using a raw string to specify the
>  > reg.expr.)
>  >
>  > --
>  > Gabriel Genellina
>
>  Thanks Gabriel,
>I tried the new command and one with the raw string and single
>  quotes, but it is still giving me the same results (no output).  any
>  other suggestions?

I had similar problems passing quoted arguments to grep -- I don't
have rename on my system so I can't do an exact translation.

First version, passing argument to grep that would normally have to be
quoted if typed in shell:

In [1]: from subprocess import *

In [2]: p1 = Popen(['ls'], stdout=PIPE)

In [3]: p2 = Popen(['grep', '[0-9]\{7\}'], stdin=p1.stdout,
stdout=PIPE) # note that the grep regex isn't double quoted...

In [4]: output = p2.communicate()[0]

In [5]: print output
cur0046700.png
cur0046700_1.png
cur0046750.png
dendat0046700.png
dendat0046700_1.png
dendat0046750.png

And we see that everything is hunky dory.

Now, trying to pass grep a quoted argument:

In [10]: p1 = Popen(['ls'], stdout=PIPE)

In [11]: p2 = Popen(['grep', '"[0-9]\{7\}"'], stdin=p1.stdout, stdout=PIPE)

In [12]: output = p2.communicate()[0]

In [13]: print output


In [14]:

And we get nothing.  N.B. that's a single-quote double-quote string
argument to grep in the second example, not a triple single quote.

Incidentally, a triple quoted string will work as well.

Moral from this example:  when passing arguments that would normally
be quoted to be safe from the shell's expansion, etc,  don't.  Just
pass it using Popen(['cmd', 'arg-that-would-normally-be-quoted']) and
it will be passed directly to the function without the shell's
intervention.

Since the argument is passed directly to the command (rename in your
case, grep in this one) quoting to preserve special characters isn't
needed, and the quotes will be passed as part of the argument, giving
the null results you got above.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fortran array in python (f2py?)...

2009-02-14 Thread Kurt Smith
On Sat, Feb 14, 2009 at 2:06 PM, tripp  wrote:

> OK.  It sounds like it would be easiest for me, then, to dump the
> arrays to a binary file (much faster than dumping it to a text) from
> the fortran program.  Then use f2py to load a fortran module to read
> it.?.


I've done something similar and have written some wrapper functions (in pure
python) that read in fortran binary arrays and puts them into a numpy
array.  You have to deal with fortran records, which (for the fortran
compiler I'm using) puts a 4-byte record length indicator at the beginning
and end of each record, with the raw binary data between.  The issue is
mildly complicated if you have to deal with endianness incompatibilites
between computers.  IIRC, the format of the records is compiler dependent,
although I've found that gfortran, g77 and xlfortran are consistent with
each other.

If you like I'd be happy to send you the code.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: number theory libraries / project euler

2009-02-18 Thread Kurt Smith
On Wed, Feb 18, 2009 at 2:18 PM, eliben  wrote:
>
> Hello,
>
> What are some good & recommended number theory libs for Python (or
> accessible interfaces to C libs), for things like primes,
> factorization, etc. Naturally, speed is of utmost importance here.
>
> In other words, which Python libraries and tools to you use to help
> you solve Project Euler problems :-) ?

There's Sage: http://www.sagemath.org/ -- I believe it aims to do
everything that Mathematica can do and more, and I know it has some
number theory libs, too.  I haven't had the occasion to use it myself.

Much of the fun of project euler problems is 'rolling your own,' or
implementing a classic algorithm from a description of it on
Wikipedia.  A good sieve of eratosthenes in pure Python (that I tend
to use quite often) is here:

http://code.activestate.com/recipes/117119/

You can find some combinatorics in numpy, I believe.

Often the solutions are one-to-five liners, if you've thought the
problem through.  Too much dependence on external libraries robs the
project euler problems of their fun, IMO.

Best,

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regular expression bug?

2009-02-19 Thread Kurt Smith
On Thu, Feb 19, 2009 at 12:55 PM, Ron Garret  wrote:
> I'm trying to split a CamelCase string into its constituent components.
> This kind of works:
>
 re.split('[a-z][A-Z]', 'fooBarBaz')
> ['fo', 'a', 'az']
>
> but it consumes the boundary characters.  To fix this I tried using
> lookahead and lookbehind patterns instead, but it doesn't work:
>
 re.split('((?<=[a-z])(?=[A-Z]))', 'fooBarBaz')
> ['fooBarBaz']
>
> However, it does seem to work with findall:
>
 re.findall('(?<=[a-z])(?=[A-Z])', 'fooBarBaz')
> ['', '']
>
> So the regular expression seems to be doing the Right Thing.  Is this a
> bug in re.split, or am I missing something?

>From what I can tell, re.split can't split on zero-length boundaries.
It needs something to split on, like str.split.  Is this a bug?
Possibly.  The docs for re.split say:

Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings.

Note that it does not say that zero-length matches won't work.

I can work around the problem thusly:

re.sub(r'(?<=[a-z])(?=[A-Z])', '_', 'fooBarBaz').split('_')

Which is ugly.  I reckon you can use re.findall with a pattern that
matches the components and not the boundaries, but you have to take
care of the beginning and end as special cases.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: can multi-core improve single funciton?

2009-02-20 Thread Kurt Smith
On Fri, Feb 20, 2009 at 4:27 PM, Grant Edwards  wrote:
> On one hand, the upshot of that is that by finding an
> appropriate library module you might gain some of the same
> benefits as removing the GIL.
>
> On the other hand, that doesn't help if you're doing something
> original enough that nobody has written a library to handle
> large chunks of it.
>
> And on the grasping hand, I find that most of us vastly
> overestimate the originality of what we're doing.

+1 The Mote in God's Eye / The Gripping Hand reference!

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting object instead of string from dir()

2008-12-17 Thread Kurt Smith
On Wed, Dec 17, 2008 at 1:52 PM, Rominsky  wrote:

> On Dec 17, 10:59 am, Christian Heimes  wrote:
> > Rominsky schrieb:
> >
> > > I am trying to use dir to generate a list of methods, variables, etc.
> > > I would like to be able to go through the list and seperate the
> > > objects by type using the type() command, but the dir command returns
> > > a list of strings.  When I ask for the type of an element, the answer
> > > is always string.  How do I point at the variables themselves.  A
> > > quick example is:
> >
> > > a = 5
> > > b = 2.0
> > > c = 'c'
> >
> > > lst = dir()
> >
> > > for el in lst:
> > > print type(el)
> >
> > for name, obj in vars().iteritems():
> > print name, obj
> >
> > Christian
>
> I do have some understanding of the pythonic methodology of
> programming, though by far I still don't consider myself an expert.
> The problem at hand is that I am coming from a matlab world and trying
> to drag my coworkers with me.  I have gotten a lot of them excited
> about using python for this work, but the biggest gripe everytime is
> they want their matlab ide.  I am trying to experiment with making
> similar pieces of the ide, in particular I am working on the workspace
> window which lists all the current variables in the namespace, along
> with their type, size, value, etc  I am trying to create a python
> equivalent.  I can get dir to list all the variables names in a list
> of strings, but I am trying to get more info them.  hence the desire


Are you familiar with the ipython console?

http://ipython.scipy.org/moin/

It is quite powerful; in particular, the %who and %whos 'magic functions'
will do much of what you'd like:

[501]$ ipython
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?   -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help-> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: a = 'foo'

In [2]: b = 'bar'

In [3]: c = 5.234

In [4]: import os

In [5]: d = os

In [6]: whos
Variable   Type  Data/Info
--
a  str   foo
b  str   bar
c  float 5.234
d  module
os module

In [7]: import numpy as np

In [8]: aa = np.zeros(100)

In [9]: whos
Variable   Type   Data/Info
---
a  strfoo
aa ndarray100: 100 elems, type `float64`, 800 bytes
b  strbar
c  float  5.234
d  module 
np module ages/numpy/__init__.pyc'>
os module 

And I trust you've heard of numpy, scipy and matplotlib?

http://www.scipy.org/

http://matplotlib.sourceforge.net/

 http://numpy.scipy.org/

Cheers,

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: 'Hidden Features of Python'

2008-10-17 Thread Kurt Smith
On Fri, Oct 17, 2008 at 11:48 AM, Joe Strout <[EMAIL PROTECTED]> wrote:
> On Oct 17, 2008, at 10:35 AM, coldpizza wrote:
>
>> If you are using and IDE, such as Eclipse, PyScripter, etc, then CTR
>> +click on 'this' should do the trick.
>> In ipython you can do 'import this' and then type 'this??' Or if you
>> are *not* lazy, you could try locating the file in the Python tree.
>
> Oh!  They're actually talking about a module literally called 'this'!  I
> thought that was just a placeholder, like "foo".
>
> Interesting (and useful).
>
> As for examining the source though, it seems like it could be shortened up
> quite a bit now -- in fact all the source except the assignment to s could
> be replaced with the one-liner
>
>  print s.encode('rot13')

Methinks you miss the point -- read the Zen, and apply its principles
to the "this" module.  I think they managed to break pretty much all
of them, probably to illustrate a point, all in good fun.  Tim Peters
rocks!

For more fun with the Zen, see this thread:

http://mail.python.org/pipermail/python-bugs-list/2008-July/055857.html

Best,

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: "xxx.has_key(a)" vs "a in xxx"

2008-10-30 Thread Kurt Smith
On Thu, Oct 30, 2008 at 9:37 AM, Łukasz Ligowski <[EMAIL PROTECTED]>wrote:

> Hi,
>
> There is small inconsistency (or I don't understand it right) between
> python
> 2.5 docs and python 2.6 docs.
>
> 2.5 docs say that:
>  "a.has_key(k) Equivalent to k in a, use that form in new code"


Meaning: don't use 'a.has_key(k)'.


>
>
> 2.6 docs say that:
>  "dict.has_key(key) is equivalent to key in d, but deprecated."


Meaning, 'd.has_key(key)' is deprecated -- use 'key in d'.


>
>
> which is true?


Both.

[269]$ python2.6 -3
Python 2.6 (r26:66714, Oct  2 2008, 12:46:52)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> D = {'foo':'bar'}
>>> 'foo' in D
True
>>> D.has_key('foo')
__main__:1: DeprecationWarning: dict.has_key() not supported in 3.x; use the
in operator
True
>>>



>
>
> L
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Data Coding suggestions

2009-02-28 Thread Kurt Smith
On Sat, Feb 28, 2009 at 10:08 AM, steven.oldner  wrote:

>
> Thanks guys.  While shopping today I've thought of a few more columns
> for my data so my first item will be building the 3 DB tables and a
> way to populate them.  Since this was intended to automate what I do
> on a weekly basis, I didn't think about adding recipes since I know
> what I need for the family, but that's a good touch.
>
> Item 1. Build 3 db tables
> Item 2. Build app to populate tables.
> Item 3. Build app to read tables and print lists.
>
> Anything else?

You might take a look at the source code for the Gourmet Recipe Manager

http://grecipe-manager.sourceforge.net/

It's written in python, has a persistent database (not sure if using
sqlite3) and you might be able to adapt it to your needs.

We use it for our shopping lists here and it's great.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Indentations and future evolution of languages

2009-03-10 Thread Kurt Smith
On Tue, Mar 10, 2009 at 12:39 PM,   wrote:
>
>
>    John> The only complaint I have there is that mixing tabs and spaces for
>    John> indentation should be detected and treated as a syntax error.
>
> Guido's time machine strikes again (fixed in Python 3.x):
>
>    % python3.0 ~/tmp/mixed.py
>      File "/home/titan/skipm/tmp/mixed.py", line 3
>        print(a)
>               ^
>    TabError: inconsistent use of tabs and spaces in indentation

Or just use the '-tt' command line switch to force indentation consistency:

ksm...@work:~/tmp [366]$ python2.5 -tt mixed.py
  File "mixed.py", line 6
print a
  ^
TabError: inconsistent use of tabs and spaces in indentation


Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Memory efficient tuple storage

2009-03-13 Thread Kurt Smith
On Fri, Mar 13, 2009 at 10:59 AM, psaff...@googlemail.com
 wrote:
> I'm reading in some rather large files (28 files each of 130MB). Each
> file is a genome coordinate (chromosome (string) and position (int))
> and a data point (float). I want to read these into a list of
> coordinates (each a tuple of (chromosome, position)) and a list of
> data points.
>
> This has taught me that Python lists are not memory efficient, because
> if I use lists it gets through 100MB a second until it hits the swap
> space and I have 8GB physical memory in this machine. I can use Python
> or numpy arrays for the data points, which is much more manageable.
> However, I still need the coordinates. If I don't keep them in a list,
> where can I keep them?

Assuming your data is in a plaintext file something like
'genomedata.txt' below, the following will load it into a numpy array
with a customized dtype.  You can access the different fields by name
('chromo', 'position', and 'dpoint' -- change to your liking).  Don't
know if this works or not; might give it a try.

===

[186]$ cat genomedata.txt
gene1 120189 5.34849
gene2 84040 903873.1
gene3 300822 -21002.2020

[187]$ cat g2arr.py
import numpy as np

def g2arr(fname):
# the 'S100' should be modified to be large enough for your string field.
dt = np.dtype({'names': ['chromo', 'position', 'dpoint'],
'formats': ['S100', np.int, np.float]})
return np.loadtxt(fname, delimiter=' ', dtype=dt)

if __name__ == '__main__':
arr = g2arr('genomedata.txt')
print arr
print arr['chromo']
print arr['position']
print arr['dpoint']

=

Take a look at the np.loadtxt and np.dtype documentation.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Memory efficient tuple storage

2009-03-13 Thread Kurt Smith
On Fri, Mar 13, 2009 at 11:33 AM, Kurt Smith  wrote:
[snip OP]
>
> Assuming your data is in a plaintext file something like
> 'genomedata.txt' below, the following will load it into a numpy array
> with a customized dtype.  You can access the different fields by name
> ('chromo', 'position', and 'dpoint' -- change to your liking).  Don't
> know if this works or not; might give it a try.

To clarify -- I don't know if this will work for your particular
problem, but I do know that it will read in the array correctly and
cut down on memory usage in the final array size.

Specifically, if you use a dtype with 'S50', 'i4' and 'f8' (see the
numpy dtype docs) -- that's 50 bytes for your chromosome string, 4
bytes for the position and 8 bytes for the data point -- each entry
will use just 50 + 4 + 8 bytes, and the numpy array will have just
enough memory allocated for all of these records.  The datatypes
stored in the array will be a char array for the string, a C int and a
C double; it won't use the corresponding python datatypes which have a
bunch of other memory usage associated with them.

Hope this helps,

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Memory efficient tuple storage

2009-03-13 Thread Kurt Smith
On Fri, Mar 13, 2009 at 1:13 PM, psaff...@googlemail.com
 wrote:
> Thanks for all the replies.
>
[snip]
>
> The numpy solution does work, but it uses more than 1GB of memory for
> one of my 130MB files. I'm using
>
> np.dtype({'names': ['chromo', 'position', 'dpoint'], 'formats': ['S6',
> 'i4', 'f8']})
>
> so shouldn't it use 18 bytes per line? The file has 5832443 lines,
> which by my arithmetic is around 100MB...?

I made a mock up file with 5832443 lines, each line consisting of

abcdef 100 100.0

and ran the g2arr() function with 'S6' for the string.  While running
(which took really long), the memory usage spiked on my computer to
around 800MB, but once g2arr() returned, the memory usage went to
around 200MB.  The number of bytes consumed by the array is 105MB
(using arr.nbytes).  From looking at the loadtxt routine in numpy, it
looks like there are a zillion objects created (string objects for
splitting each line, temporary ints floats and strings for type
conversions, etc) while in the routine which are garbage collected
upon return.  I'm not well versed in Python's internal memory
managment system, but from what I understand, practically all that
memory is either returned to the OS or held onto by Python for future
use by other objects after the routine returns.  But the only memory
in use by the array is the ~100MB for the raw data.

Making 5 copies of the array (using numpy.copy(arr)) bumps total
memory usage (from top) up to 700MB, which is 117MB per array or so.
The total memory reported by summing the arr.nbytes is 630MB (105MB /
array), so there isn't that much memory wasted.  Basically, the numpy
solution will pack the data into an array of C structs with the fields
as indicated by the dtype parameter.

Perhaps a database solution as mentioned in other posts would suit you
better; if the temporary spike in memory usage is unacceptable you
could try to roll your own loadtxt function that would be leaner and
meaner.  I suggest the numpy solution for its ease and efficient use
of memory.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Books for learning how to write "big" programs

2008-05-22 Thread Kurt Smith
On Thu, May 22, 2008 at 10:55 AM, duli <[EMAIL PROTECTED]> wrote:
> Hi:
> I would like recommendations for books (in any language, not
> necessarily C++, C, python) which have walkthroughs for developing
> a big software project ? So starting from inception, problem
> definition, design, coding and final delivery on a single theme
> or application.

The bigger the project, the more likely it is that you'll have
documentation on how to use it (for a language or library, how to use
the features in your program) but to take the time to write up a
dead-tree book on the project's "inception, problem definition,
design, coding and final delivery" is not likely well spent.  Anyone
who has the expertise to write such a book would probably be spending
his time working on the next phase of the project itself.

Someone will probably respond with an amazon link to a book that does
exactly what you're asking, in which case, I will stand corrected.
But I'll be surprised.

>
> Most of the code I have written and books that I have read deal with
> toy programs and I am looking for something a bit more
> comprehensive.  For example, maybe a complete compiler written in C++
> for some language, or a complete web server or implementing
> .net libraries in some language (just a few examples of the scale of
> things I am interested in learning).

It seems to me the reason toy programs are so prevalent is because
they illustrate a (few) well defined ideas in a short amount of code.
A big project, necessarily, brings together all kinds of stuff, much
of which may not interest the author at all, and so doesn't motivate
him to write a book about it.

Compilers, web servers & .NET libraries are *widely* varying areas.
You may have interest in them all, but to significantly contribute to
any requires a fair amount of expertise and specialization.

The best route I've found to learn how to organize & program large
scale applications is this: find a cutting edge program that interests
you and that is open source.  Download its source, and read the code.
Diagram it.  Map it out.  Read the comments.  Join the mailing list
(probably the developer's list), lurk for a while, and ask questions
about why they organized things the way they did.  Get the overall big
picture and learn from it.  Better yet, find out what pitfalls they
found and avoided (or fell into).  Compare their approach &
organization with another competing project.  This is the wonder of
open source software -- you have access to everything, and can learn
from all the expertise the developers put into their opus.

You can learn the basics from books, but nothing beats analyzing a
species in the wild.

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Histogram of floating point values.

2008-07-25 Thread Kurt Smith
On Fri, Jul 25, 2008 at 5:02 PM, aditya shukla
<[EMAIL PROTECTED]> wrote:
> Hello folks,
>
> I have a list say
>
> data=[0.99,0.98,0.98,0.98,0.97,0.93,0.92,0.92,0.83,0.66,0.50,0.50]
>
> i am trying to plot histogram of these values
>
> i have installed numpy and matplotlib  and this is what i am doing*
>  import numpy
>  import pylab
>  from numpy import *
>  from pylab import *
>
> input_hist=array(data)
> pylab.hist(input_hist,bins=0.1)
> and this is the error that i am getting
>
> (array([], dtype=int32), array([ 0.5]), )
>
>
> does this mean that i cannot plot a histogram of floating point values ? or
> is there a way around

the 'bins' argument to pylab.hist() is supposed to be an integer or a
list of the bins' lower edges.  The default value is 10, more than
that gives smaller bins, as one would expect.  Take a look at the
pylab.hist documentation (you can do 'print pylab.hist.__doc__' from
the command interpreter).

You should have no problem plotting a hist of floats.  Try this:

import numpy
import pylab
from numpy import *
from pylab import *

data=[0.99,0.98,0.98,0.98,0.97,0.93,0.92,0.92,0.83,0.66,0.50,0.50]

input_hist=array(data)
pylab.hist(input_hist)
pylab.show()

The last line will display the actual histogram.  See the difference
pylab.show and pylab.ion functions.

In the future, it is advisable to post these questions to the
matplotlib or the numpy/scipy users mailing lists.

Kurt



>
> Thanks in advance
>
> Aditya
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Read and write binary data

2008-09-08 Thread Kurt Smith
On Sun, Sep 7, 2008 at 5:41 PM, Mars creature <[EMAIL PROTECTED]> wrote:
> Hi guys,
>  I am new to Python, and thinking about migrating to it from matlab
> as it is a really cool language. Right now, I am trying to figure out

If you're trying to migrate from matlab to python I'd take a look at numpy:

http://numpy.scipy.org/

And scipy which is built on top of numpy:

http://www.scipy.org/

There is a plotting/numerical computation package known as matplotlib
that does its best to parallel matlab commands:

http://matplotlib.sourceforge.net/

There is support for reading and writing binary data, even fortran records.

And last but not least, you can always take a look at the scipy and
numpy mailing lists.

Good luck,

Kurt

> how to control read and write binary data, like
> 'formatted','stream','big-endian','little-edian' etc.. as in fortran.
> I googled, but can not find a clear answer. Anyone has clue where can
> I learn it? Thanks!!
> Jinbo
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using jython to call python procedures/methods

2010-01-20 Thread Kurt Smith
On Wed, Jan 20, 2010 at 9:32 AM, KB  wrote:
> Hi there,
>
> I have an application that only publishes a Java API. I can use jython
> to access java classes, but jython currently (to the best of my
> knowledge) does not support numpy/scipy.
>
> Ideally I would like to have jython call a "native" python routine
> where I have the numpy/scipy procedures already written.
>
> Does anyone have any experience with this? Is it possible?

I have no experience with these technologies, and others can point you
to more detailed info, but you could try using XML-RPC (see the docs
for the xmlrpclib module in the standard library) or SOAP (Google it).

These would be better than rolling your own.

Perhaps there's a more Pythonic solution though?

Kurt

>
> I had toyed with the idea of having jython/java write the data to a
> file/database and then manually kick off the python process, but
> ideally I would like this as automated as possible.
>
> Thanks in advance.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Over(joy)riding

2010-02-17 Thread Kurt Smith
On Wed, Feb 17, 2010 at 9:08 AM, mk  wrote:
> Bruno Desthuilliers wrote:
>>
>> mk a écrit :
>>>
>>> P.S. Method resolution order in Python makes me want to kill small
>>> kittens.
>>
>> mro is only a "problem" when using MI.
>
> Oh sure! And I have the impression that multiple inheritance is not used all
> that often. What (some) Python code I've read in open source projects
> typically uses single inheritance.
>
> Nevertheless MI is there in Python, might be useful in some cases and Guido
> put it there for some reason, so Citizens Aspiring To Become Pythonistas
> like me work to learn it.

In case you're not familiar with it, MI allows you to have mixins &
traits.  They work very well if the mixin superclasses don't have any
clashes with the other superclasses, so each mixin adds its own unique
set of methods to the derived class.  Then you don't have to worry
about mro and all that (at least as much).

Here's an article on it, with examples:

http://www.linuxjournal.com/node/4540/print

Michele Simionato has some criticism of MI pitfalls and has come up
with the straits module to remedy it -- you might be interested.  He
goes into detail here:

http://www.artima.com/weblogs/viewpost.jsp?thread=246488

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-18 Thread Kurt Smith
On Thu, Feb 18, 2010 at 10:46 PM, Steve Howell  wrote:
> On Feb 18, 2:49 pm, Jonathan Gardner 
> wrote:
>> On Feb 18, 8:15 am, Steve Howell  wrote:
>>
>>
>>
>> >     def print_numbers()
>> >         [1, 2, 3, 4, 5, 6].map { |n|
>> >             [n * n, n * n * n]
>> >         }.reject { |square, cube|
>> >             square == 25 || cube == 64
>> >         }.map { |square, cube|
>> >             cube
>> >         }.each { |n|
>> >             puts n
>> >         }
>> >     end
>>
>> If this style of programming were useful, we would all be writing Lisp
>> today. As it turned out, Lisp is incredibly difficult to read and
>> understand, even for experienced Lispers. I am pleased that Python is
>> not following Lisp in that regard.
>>
>> for n in range(1,6):
>>     square = n*n
>>     cube = n*n*n
>>     if square == 25 or cube == 64: continue
>>     print cube
>
> There's definitely a cognitive dissonance between imperative
> programming and functional programming.  It's hard for programmers
> used to programming in an imperative style to appreciate a functional
> approach, because functional solutions often read "upside down" in the
> actual source code and common algebraic notation:
>
>    def compute_squares_and_cubes(lst):
>        return [(n * n, n * n * n) for n in lst]
>
>    def reject_bad_values(lst):
>        return [(square, cube) for (square, cube) \
>            in lst if not (square == 25 or cube == 64)]
>
>    def cubes_only(lst):
>        return [cube for square, cube in lst]
>
>    def print_results(lst):
>        # 1. compute_squares_and_cubes
>        # 2. reject_bad_values
>        # 3. take cubes_only
>        # 4. print values
>        for item in \
>            cubes_only( # 3
>                reject_bad_values( # 2
>                    compute_squares_and_cubes(lst))): # 1
>            print item # 4
>
> You can, of course, restore the natural order of operations to read
> top-down with appropriate use of intermediate locals:
>
>    def print_results(lst):
>        lst2 = compute_squares_and_cubes(lst)
>        lst3 = reject_bad_values(lst2)
>        lst4 = cubes_only(lst3)
>        for item in lst4:
>            print item
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

# sent the original to the wrong place -- resending to python-list.

Somewhat off topic, but only somewhat:  you could use coroutines to
get a pipeline effect.

#--8<-
# Shamelessly lifted from David Beazley's
#  http://www.dabeaz.com/coroutines/

def coroutine(co):
   def _inner(*args, **kwargs):
   gen = co(*args, **kwargs)
   gen.next()
   return gen
   return _inner

def squares_and_cubes(lst, target):
   for n in lst:
   target.send((n * n, n * n * n))

@coroutine
def reject_bad_values(target):
   while True:
   square, cube = (yield)
   if not (square == 25 or cube == 64):
   target.send((square, cube))

@coroutine
def cubes_only(target):
   while True:
   square, cube = (yield)
   target.send(cube)

@coroutine
def print_results():
   while True:
   print (yield)

squares_and_cubes(range(10),
   reject_bad_values(
   cubes_only(
   print_results()
   )
   )
   )
#--8<-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need advice on starting a Python group

2010-03-12 Thread Kurt Smith
On Thu, Mar 11, 2010 at 8:57 AM, gb345  wrote:
>
>
>
> I'm hoping to get advice from anyone with prior experience setting
> up a Python group.
>
> A friend of mine and I have been trying to start a
> scientific-programming-oriented Python group in our school (of
> medecine and bio research), with not much success.
>
> The main problem is attendance.  Even though a *ton* of people have
> told us that it's a great idea, that they're *very* interested,
> and have asked to be added to our mailing list, the attendance to
> our first few meeting has never been more than 5, including my
> friend and I.  Last time just he and I showed up.
>
> The second problem is getting content.  The format we'd envisioned
> for this group was centered around code review (though not limited
> to it).  The idea was that at every meeting a different member
> would show some code.  This could be for any of a number of reasons,
> such as, for example, 1) illustrate a cool module or technique; 2)
> present a scientific research problem and how they used Python to
> solve it, or get help solving it; 3) get general feedback (e.g. on
> code clarity, software usability, module architecture, etc.).  But
> in principle just about anything is OK: e.g. a talk on favorite
> Python resources, or a comparison of Python with some other language,
> or an overview of Python gotchas would all be fair game.
>
> Also, we stressed that the talks were not expected to be polished:
> no need for PowerPoint slides, etc.  Just project any old code onto
> the screen, and talk about it, or scribble stuff on the chalkboard.
>
> Still, we have a hard time finding volunteers.
>
> And even when we've had volunteers, hardly anyone shows up!
>
> Any suggestions would be appreciated.
>
> GB
>
> P.S.  There's a Python Meetup we could go to, but it does not fit
> the bill for us: it doesn't meet often enough, it's sort of out of
> the way, and has practically no one doing scientific programming.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

There's a general Scientific Computing interest group that gets
together here at the University of Wisconsin-Madison, and it has a
significant Python component & focus.  They put on a Python bootcamp
this January that was a huge success.

http://hackerwithin.org/cgi-bin/hackerwithin.fcgi/wiki

They have bi-weekly meetings, sometimes it's of the 'come and share on
X topic,' although many times its 'local guest speaker is coming to
speak about Y'.  My impression is that the latter meetings grabbed a
number of people around campus -- 'hey, I need to do Y, I'll see what
the speaker has to say,' and then they started coming for the
show-and-tell meetings.  My recommendation would be to provide
something of value every meeting, the more specific the better.
'Python' in this regard is a bit open ended.  You'd likely get more
involvement if you had meetings that focused on, e.g., parallel
computing (and have examples in python (mpi4py), and have someone come
and talk about MPI or something), or scientific data formats (with
examples of pyhdf5 or pytables...), or you could advertise a tutorial
on some scipy & numpy features and their advantages over using
matlab/octave/idl.

It's more work than show-and-tell meetings, but look at it as priming the pump.

There is much interest around here re: Python in science, but many
have only heard about it, some have dabbled but put it on the shelf,
others couldn't get it to work (they're scientists and used to
prepackaged software that works out of the box -- if it doesn't, it's
somebody else's problem), many others can't justify the time it would
take to learn it when they already have something else working.  Until
something with value comes along (like your meeting with specific
topics) to change their minds, an open-ended meeting won't appeal much
to them.

Just some thoughts, and an example of what's worked here.  Personally
I tend to make it to the meetings with a specific topic, and end up
skipping the ones that are more open-ended.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to decide between PHP and Python

2011-01-06 Thread Kurt Smith
On Thu, Jan 6, 2011 at 3:32 PM, Alan Meyer  wrote:
> On 1/5/2011 11:40 AM, Tomasz Rola wrote:
>>
>> On Tue, 4 Jan 2011, Roy Smith wrote:
>>
>>> There.  Now that I've tossed some gasoline on the language wars fire,
>>> I'll duck and run in the other direction :-)
>>
>> May I suggest a better strategy? Run first, duck next :-).
>
> Or more precisely:
>
>   ((run) duck)

If you're going to mock another language, you might as well get it right :-)

If that's Lisp code, it should be:

(funcall (run) duck)

see: http://hornbeck.wordpress.com/2009/07/05/lisp-1-vs-lisp-2/

It'll work unchanged for Scheme, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use Python well?

2011-02-16 Thread Kurt Smith
On Wed, Feb 16, 2011 at 12:35 PM, snorble  wrote:
> I use Python a lot, but not well. I usually start by writing a small
> script, no classes or modules. Then I add more content to the loops,
> and repeat. It's a bit of a trial and error learning phase, making
> sure I'm using the third party modules correctly, and so on. I end up
> with a working script, but by the end it looks messy, unorganized, and
> feels hacked together. I feel like in order to reuse it or expand it
> in the future, I need to take what I learned and rewrite it from
> scratch.
>
> If I peeked over a Python expert's shoulder while they developed
> something new, how would their habits differ? Do they start with
> classes from the start?
>
> I guess I'm looking for something similar to "Large Scale C++ Software
> Design" for Python. Or even just a walkthrough of someone competent
> writing something from scratch. I'm not necessarily looking for a
> finished product that is well written. I'm more interested in, "I have
> an idea for a script/program, and here is how I get from point A to
> point B."
>
> Or maybe I'm looking for is best practices for how to organize the
> structure of a Python program. I love Python and I just want to be
> able to use it well.

Try this:

http://www.refactoring.com/

Not a silver bullet, but a good place to start.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tool for browsing python code

2009-06-17 Thread Kurt Smith
On Tue, Jun 16, 2009 at 7:48 AM, Lucas P Melo wrote:
> Is there any tool for browsing python code? (I'm having a hard time trying
> to figure this out)
> Anything like cscope with vim would be great.

Check out pycscope:

http://pypi.python.org/pypi/pycscope/0.3

I use it myself, and it works fine.  Better than ctags/etags for python.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting Python code to C/C++

2009-06-23 Thread Kurt Smith
On Mon, Jun 22, 2009 at 9:49 PM, Andras
Pikler wrote:
> Hi!
>
>
>
> Short: I need to turn a Python program that I (mostly) wrote into C code,
> and I am at a loss.
>
>
>
> Long: I’m doing research/programming for a professor, and we are working
> with MIDI files (a type of simple music file). The research deals with
> generating variations from a musical melody; currently, my Python code uses
> a Python midi package I found online to read the notes in question from a
> midi file, about 350 lines of my own code to generate a variation based on
> these notes and the professor’s algorithms, and finally the package again to
> write the new melody to another midi file.
>
>
>
> Now, my professor would like to have this exact code in C/C++, as she
> believes C is more compatible with MATLAB, and wants the code to be
> available in multiple languages in case a programmer works for her in the
> future who knows C but not Python. While I know a tiny bit of C (emphasis on
> the tiny), I would much prefer if there were some sort of automatic compiler
> I could use to turn my Python code into C than taking a week or two or three
> to learn the minimum I need about C, find a way to access MIDI files in it,
> and rewrite all of my code.
>
>
>
> After some googling, I found and tried Shedskin, but it doesn’t work, as the
> Python midi package I’m using uses modules which Shedskin does not support.
> Otherwise, I haven’t found much. Is there anything out there to help me do
> this? If not, from anyone who has experience in this regard, how daunting
> should I expect this to be?


Taking on C from a cold start and being able to handle the ins and
outs of interfacing with Python isn't something that's feasible in
'two or three weeks'.  Here are a couple of options -- take 'em or
leave 'em:

1) Put the code in Cython: http://www.cython.org/  (full disclosure:
I'm doing a GSoC project with Cython).  It will convert pretty much
any python code into C code (even closures are supported in the most
recent version, I think), and the C code can then be compiled into an
extension module.

The only problem with the above is the C code isn't, at first blush,
easy to read.  Nor is it supposed to be changed by the user.  So that
leads us to option...

2) Write the core functionality in C yourself, and then wrap those C
functions in Cython.  You'll want to take a look at the documentation:

http://docs.cython.org/

and, more specifically on wrapping C code:

http://docs.cython.org/docs/external_C_code.html

I don't think you'll be able to avoid learning C, though.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not enforce four space indentations in version 3.x?

2009-07-10 Thread Kurt Smith
On Fri, Jul 10, 2009 at 2:22 PM, walterbyrd wrote:
> I believe Guido himself has said that all indentions should be four
> spaces - no tabs.
>
> Since backward compatibility is being thrown away anyway, why not
> enforce the four space rule?
>
> At least that way, when I get python code from somebody else, I would
> know what I am looking at, without having to do a hex dump, or
> something.

What you propose has already been (forcefully) rejected:

http://www.python.org/dev/peps/pep-0666/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler

2009-07-22 Thread Kurt Smith
On Wed, Jul 22, 2009 at 2:48 AM, Bearophile wrote:
> greg:
>> Posting benchmark times for Pyrex or Cython is pretty
>> meaningless without showing the exact code that was
>> used, since times can vary enormously depending on
>> how much you C-ify things.
>
> Was this link, shown by William, not enough?
> http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1

I took a stab at converting the recent psyco-optimized code to cython,
and got a speedup.

gcj4.3.31.39s
gcc4.3.31.55s
cython 11.2 1.91s
psyco   1.94s
javac  1.5.0_19 2.00s
python 2.5.4168.37s

It was just a matter of cdef-ing all the arrays & integers --
bearophile already did the hard work :-)

Here's the cython code; all the others are from the repo.

#
DEF NMOVES = 8
DEF SIDE = 5
DEF SQR_SIDE = SIDE * SIDE

cdef int circuit[SQR_SIDE]
cdef int nsolutions = 0

cdef int movex[NMOVES]
cdef int movey[NMOVES]
py_movex = [-1,-2,-2,-1,+1,+2,+2,+1]
py_movey = [-2,-1,+1,+2,+2,+1,-1,-2]
for i in range(NMOVES):
   movex[i] = py_movex[i]
   movey[i] = py_movey[i]
shift = [x * SIDE + y for x,y in zip(py_movex, py_movey)]
cdef int shift_0 = shift[0]
cdef int shift_1 = shift[1]
cdef int shift_2 = shift[2]
cdef int shift_3 = shift[3]
cdef int shift_4 = shift[4]
cdef int shift_5 = shift[5]
cdef int shift_6 = shift[6]
cdef int shift_7 = shift[7]

def showCircuit():
   print
   for x in xrange(SIDE):
   x_SIDE = x * SIDE
   for y in xrange(SIDE):
   if SQR_SIDE < 100:
   print "%02d " % circuit[x_SIDE + y],
   else:
   print "%03d " % circuit[x_SIDE + y],
   print

cdef void solve(int nb, int x, int y,
   int SIDE=SIDE, int SQR_SIDE=SQR_SIDE, int *circuit=circuit,
   int shift_0=shift_0,
   int shift_1=shift_1,
   int shift_2=shift_2,
   int shift_3=shift_3,
   int shift_4=shift_4,
   int shift_5=shift_5,
   int shift_6=shift_6,
   int shift_7=shift_7,
   ):
   global nsolutions

   cdef int newx, newy
   cdef int pos = x * SIDE + y
   circuit[pos] = nb
   if nb == SQR_SIDE:
   #showCircuit()
   nsolutions += 1
   circuit[pos] = 0
   return

   newx = x + -1
   if newx >= 0 and newx < SIDE:
   newy = y + -2
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_0]:
   solve(nb+1, newx, newy)

   newx = x + -2
   if newx >= 0 and newx < SIDE:
   newy = y + -1
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_1]:
   solve(nb+1, newx, newy)

   newx = x + -2
   if newx >= 0 and newx < SIDE:
   newy = y + 1
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_2]:
   solve(nb+1, newx, newy)

   newx = x + -1
   if newx >= 0 and newx < SIDE:
   newy = y + 2
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_3]:
   solve(nb+1, newx, newy)

   newx = x + 1
   if newx >= 0 and newx < SIDE:
   newy = y + 2
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_4]:
   solve(nb+1, newx, newy)

   newx = x + 2
   if newx >= 0 and newx < SIDE:
   newy = y + 1
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_5]:
   solve(nb+1, newx, newy)

   newx = x + 2
   if newx >= 0 and newx < SIDE:
   newy = y + -1
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_6]:
   solve(nb+1, newx, newy)

   newx = x + 1
   if newx >= 0 and newx < SIDE:
   newy = y + -2
   if newy >= 0 and newy < SIDE and not circuit[pos + shift_7]:
   solve(nb+1, newx, newy)

   circuit[pos] = 0

def main():
   print "Search for side=%d" % SIDE
   cdef int x,y
   for x in range(SIDE):
   for y in range(SIDE):
   solve(1, x, y);
   print "\n%dx%d case, %d solutions." % (SIDE, SIDE, nsolutions)

def run():
   import time
   s=time.time()
   main()
   print time.time()-s
#
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python/Fortran interoperability

2009-08-24 Thread Kurt Smith
On Sun, Aug 23, 2009 at 9:21 AM, Stefan Behnel wrote:
> n...@cam.ac.uk wrote:
>> I am interested in surveying people who want to interoperate between
>> Fortran and Python to find out what they would like to be able to do
>> more conveniently, especially with regard to types not supported for C
>> interoperability by the current Fortran standard.  Any suggestions as to
>> other ways that I could survey such people (Usenet is no longer as
>> ubiquitous as it used to be) would be welcomed.
>
> You might want to ask also on the Cython, NumPy and SciPy mailing lists.
> NumPy and SciPy have a rather large audience of scientific developers, and
> Cython has a running sub-project on providing better Fortran integration
> (which might be of interest to you anyway).

Thanks for the mention, Stefan.  For those who are interested, here's
my blog summarizing the status of 'fwrap,' a Fortran wrapper utility
for the C, Cython & Python languages.

http://fortrancython.wordpress.com/

Linked there are my talk slides & presentation at last week's SciPy
2009 conference, which gives a good overview.

Kurt
-- 
http://mail.python.org/mailman/listinfo/python-list