Re: [Tutor] text files

2013-01-16 Thread Alan Gauld

On 16/01/13 02:31, Gina wrote:


new_file = open("the_file_upper.txt", "w+")


In general consider the w+ file option to be for advanced use only.
Either open it for reading or writing, don't try to do both at the same 
time. It's a bit like sitting on a tree branch with a saw to cut it 
down. If you cut on the wrong side you get hurt...



new_file.write(*)
print(new_file.read())


This won't read anything because the file cursor is at the end of the 
string that you wrote (remember the branch illustration?)



new_file.close()

And this just closes the file with a short line of asterisks in it...
Not really what you want.

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

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


Re: [Tutor] Question regarding lists and manipulating items in lists.

2013-01-16 Thread Steven D'Aprano

On 16/01/13 11:23, Scurvy Scott wrote:


After playing with your example I keep being told that list has no
attribute int_to_note. I know what the problem is, I just don't know
how to fix it.


Oops, sorry about that, that is my fault. I did warn that my code was
untested!

If you know what the problem is, the solution should be obvious. Before
reading ahead, try explaining to yourself what you think the problem
actually is. If you do that, the answer should pop right out at you.

Still need a hint? Okay, try this:


"I need to convert the Fibonacci integers to musical notes, using the
notes.int_to_note function imported from mingus.core. But inside Steven's
make_notes function, he sets a local variable `notes = []`, which means
that inside the function I cannot access the global notes.int_to_note."

Or, a shorter version:

"I have notes.int_to_note, which I need, but accessing it is blocked by
the local variable notes which is a list."

So the obvious solution is...


...rename the local variable `notes` to something else.


def make_notes(num_notes):
it = fib()
music = []  # start with an empty list
for i in range(num_notes):
n = next(it) % 12  # get the next Fibonacci number, modulo 12
music.append(notes.int_to_note(n))
return music



Still untested.



Also, I believe I've fixed my email so it will no longer be in HTML or
anything fancy, just plain text.


Many thanks!

More comments below.



So right now my code is:

import mingus.core.notes as notes

#fibonacci
def fib():
 a, b = 0, 1
 while True:
 yield b
a, b = b, a+b


I have now tested that, and it works fine:

py> it = fib()
py> [next(it) for i in range(15)]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]


Some people prefer to start the Fibonacci sequence with 0. That's an
easy change to make: change the line "yield b" to "yield a". But
Wolfram Mathworld and Mathematica start the Fibonacci sequence with
1, 1, 2, ... rather than 0, 1, 1, ... so that's good enough for me.

http://mathworld.wolfram.com/FibonacciNumber.html




def make_notes(num_notes):
it = fib()
notes = []
for i in range(num_notes):
n = next(it) % 12
notes.append(notes.int_to_note(n))
return notes

Which is pretty different from what my original code was.



Maybe so, but I'm teaching you a practice that will see you in good stead
whenever you program: each function should do *one* thing.

Think of programming as creating tools. You wouldn't try to create a single
tool for cutting wood, driving screws into it, and painting it. Instead we
have three tools: saw, screwdriver, paint brush. It's *much* easier to make
three separate tools than a single tool that tries to do all three jobs.

Likewise for your program. It is better to write separate functions to:

* create the Fibonacci numbers;

* turn them into musical notes;


than to do both jobs in one function. Now in *this* case, the labour saved
is relatively small. But it is a good habit to get into, and when you get
to large, complex programs it becomes essential.




 The
generator function doesn't actually do anything when called, it just
tells me it's a generator function and where it's located.


If you look at how my code uses the generator function, you will see the
correct way to use it. Here's another example:


py> it = fib()
py> n = next(it)
py> while n < 100:
... print n
... n = next(it)
...
1
1
2
3
5
8
13
21
34
55
89




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


[Tutor] Set LD_LIBRARY_PATH and equivalents platform-independently

2013-01-16 Thread Albert-Jan Roskam
Hello,

Is there a builtin function that can set LD_LIBRARY_PATH or equivalents 
platform-independently? It would be nice use such a function in a setup script. 
The code below illustrates what I mean, although it's entirely untested.

import sys
import os
def setPath(loc):
    """Set LD_LIBRARY_PATH and equivalents platform-independently"""
    p = {"win": "PATH",
        "lin": "LD_LIBRARY_PATH",
    "solaris": "LD_LIBRARY_PATH",
    "aix": "LIBPATH",
    "darwin":
 "DYLD_LIBRARY_PATH",
    "hpux": "SHLIB_PATH"}
    pf = sys.platform()
    sep = ";" if pf.startswith("win") else ":"
    try:
    os.environ[p[pf]] += (sep + loc)
    except KeyError:
    print "Platform %s not supported" % pf


 
Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for
 us?
~~ ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to track an entry in a large text file

2013-01-16 Thread 3n2 Solutions
Say I have a text file (example below) that has multiple records with
the same format but not always the same number of lines.  Each record
is separated by the line that starts with “Examining file” as in below
example. If you notice, the 3D Val is not there for the second record.
How do I report that the “3D Val” is not present for that particular
record “8866W2310089.txt”?
Thanks to Alan for his reply.



Examining file: 5521W231.txt

Duration :  0h : 59m
First meas : week   :  86 :   1721
Last  meas : week   :  89 :   1721

Min val   : 15
Max val : 18
3D :   3600  100.0%

summary Total  MissedRate
epoch1:1378   0   0\1000
epoch2:2154   1   0\1000



Examining file: 8866W2310089.txt

Duration :  0h : 59m
First meas : week   :  87 :   1721
Last  meas : week   :  84 :   1721

Min val   : 16
Max val : 19

summary Total  MissedRate
epoch1:1378   0   0\1000
epoch
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Json encode and decode on Puython 2.4

2013-01-16 Thread Prasad, Ramit
Dotan Cohen wrote:
> 
> Hi all, I'm just getting into porting some PHP scripts to Python. One
> issue that I'm having is that the scripts need to run on a host with
> Python 2.4 that has neither the json nor simplejson packages
> available. I figure that I'll have to include the loads() and dumps()
> functions in my application. Where might I find the source for these
> two functions? What would be the best way to include them?
> 
> Thanks.
> 
> --
> Dotan Cohen

Python 2.4 is quite old and simplejson supports 2.5+. I can see a 
yield in the encoder code, so it is unlikely to be easy to modify 
and get working with 2.4. According to a stack overflow question
you may want to look at older releases (example: 2.0.9/2.1) as being
more 2.4 compatible.
SimpleJson source: https://github.com/simplejson/simplejson
Change the version in the link to see if PyPi has a different version.
http://pypi.python.org/pypi/simplejson/2.1.0 

I have no idea if you can get the json library to work with 2.4.
I did not seen anything in my cursory glance that suggested it would
not. 
http://hg.python.org/cpython-fullhistory/file/8e0b617c6c22/Lib/json/

You may want to see if you can get a newer python installed on 
the machine. You probably want it alt-installed so you are not
replacing the system expected Python.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] learning to program in cython

2013-01-16 Thread Abhishek Pratap
Hi Guys

With the help of an awesome python community I have been able to pick up
the language and now willing to explore other cool extensions of it.

I routinely have large loops which could be ported to cython for speed.
However I have never written a single line of cython code. Any pointers on
getting started.

A tutorial text or video would be of great help.

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


Re: [Tutor] Json encode and decode on Puython 2.4

2013-01-16 Thread Dotan Cohen
On Wed, Jan 16, 2013 at 11:50 PM, Prasad, Ramit
 wrote:
> Python 2.4 is quite old and simplejson supports 2.5+. I can see a
> yield in the encoder code, so it is unlikely to be easy to modify
> and get working with 2.4. According to a stack overflow question
> you may want to look at older releases (example: 2.0.9/2.1) as being
> more 2.4 compatible.
> SimpleJson source: https://github.com/simplejson/simplejson
> Change the version in the link to see if PyPi has a different version.
> http://pypi.python.org/pypi/simplejson/2.1.0
>
> I have no idea if you can get the json library to work with 2.4.
> I did not seen anything in my cursory glance that suggested it would
> not.
> http://hg.python.org/cpython-fullhistory/file/8e0b617c6c22/Lib/json/
>
> You may want to see if you can get a newer python installed on
> the machine. You probably want it alt-installed so you are not
> replacing the system expected Python.
>
>

Thanks, Ramit. I'm now trying to install an older simplejson with
simple_install by defining the install directory since I don't have
root on this machine. Unfortunately, installing a newer Python (or
anything else needed by root) is not going to happen on this
particular webhost.

Thanks.


-- 
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] learning to program in cython

2013-01-16 Thread Oscar Benjamin
On 16 January 2013 21:54, Abhishek Pratap  wrote:
> Hi Guys
>
> With the help of an awesome python community I have been able to pick up the
> language and now willing to explore other cool extensions of it.

Good work!

>
> I routinely have large loops which could be ported to cython for speed.
> However I have never written a single line of cython code. Any pointers on
> getting started.

There are two reasons for using cython:
1) To interface directly with existing C libraries.
2) To speed up CPU-intensive code.

It sounds like you're interested in case 2). However, not all loops
benefit from cythonisation. Loosely cython is good when
a) you're doing something that can be written in a small amount of
efficient C code
b) but the corresponding Python code involves a lot of repeated
function calls or expression evaluations.

If you're already familiar with C then you'll probably have some idea
when a) and b) apply. I would say that a prerequisite for learning to
speed up CPU-intensive code with cython would be learning to use the
python profilers. In particular you should learn to use cProfile and
timeit:
http://docs.python.org/2/library/profile.html
http://docs.python.org/2/library/timeit.html

As with any optimisation it is important to study the performance of
your code and identify bottlenecks first. It is also generally true
that you should (at least) consider algorithmic optimisation before
you consider using something like cython for micro-optimisation. One
of the guiding principles in the design of the Python language is that
big-O complexity is often more important than micro-optimisation.
Similarly, you should normally try to optimise and benchmark your code
in pure Python before attempting to improve on that with cython.

>
> A tutorial text or video would be of great help.

Here is the official "basic tutorial" for cython:
http://docs.cython.org/src/userguide/tutorial.html

There are also lots of helpful people on the cython-users mailing list:
https://groups.google.com/forum/?fromgroups#!forum/cython-users

If you have a particular problem in mind that might benefit from
cythonisation, then why not post (a shortened but complete version of)
it here to see if anyone has any pointers about how to improve the
performance with/without cython.


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


Re: [Tutor] how to track an entry in a large text file

2013-01-16 Thread Oscar Benjamin
On 16 January 2013 21:22, 3n2 Solutions <3n2soluti...@gmail.com> wrote:
> Say I have a text file (example below) that has multiple records with
> the same format but not always the same number of lines.  Each record
> is separated by the line that starts with “Examining file” as in below
> example. If you notice, the 3D Val is not there for the second record.
> How do I report that the “3D Val” is not present for that particular
> record “8866W2310089.txt”?
> Thanks to Alan for his reply.

I don't follow what you mean by 'How do I report that the “3D Val” is
not present for that particular
record “8866W2310089.txt”?'

The obvious answer would be (assuming the filename is currently set to
“8866W2310089.txt”):
print('3D Val not present in %s' % filename)

I wonder if your actual question is:
1) How do I detect that it is not present? - check for a line starting
with '3D' while reading the file.
2) How do I mark it is not being present in my data structure? -
depends what kind of data structure you are using. A common idiom
would be to simply use the value None as a place-holder for situations
where the value is not supplied.
3) Something else...

>
> Examining file: 5521W231.txt
>
> Duration :  0h : 59m
> First meas : week   :  86 :   1721
> Last  meas : week   :  89 :   1721
>
> Min val   : 15
> Max val : 18
> 3D :   3600  100.0%
>
> summary Total  MissedRate
> epoch1:1378   0   0\1000
> epoch2:2154   1   0\1000
>
>
>
> Examining file: 8866W2310089.txt
>
> Duration :  0h : 59m
> First meas : week   :  87 :   1721
> Last  meas : week   :  84 :   1721
>
> Min val   : 16
> Max val : 19
>
> summary Total  MissedRate
> epoch1:1378   0   0\1000
> epoch


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


Re: [Tutor] Json encode and decode on Puython 2.4

2013-01-16 Thread Steven D'Aprano

On 17/01/13 08:50, Prasad, Ramit wrote:


Python 2.4 is quite old and simplejson supports 2.5+. I can see a
yield in the encoder code, so it is unlikely to be easy to modify
and get working with 2.4.


Python 2.4 supports yield.



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


Re: [Tutor] Json encode and decode on Puython 2.4

2013-01-16 Thread Steven D'Aprano

On 17/01/13 08:15, Dotan Cohen wrote:

Hi all, I'm just getting into porting some PHP scripts to Python. One
issue that I'm having is that the scripts need to run on a host with
Python 2.4 that has neither the json nor simplejson packages
available. I figure that I'll have to include the loads() and dumps()
functions in my application. Where might I find the source for these
two functions? What would be the best way to include them?


Python 2.4 is no longer receiving security updates. If you're exposing a
web app on the Internet using Python 2.4, it's just a matter of time
before you're hacked.

Time to change hosting companies, methinks.

If it is a Centos or RedHat based Linux system, it will support simplejson.

[steve@ando /]$ sudo yum install simplejson
[sudo] password for steve:
[...extremely long output deleted...]

Installed:
  python-simplejson.i386 0:2.0.9-8.el5

Complete!
[steve@ando /]$ python2.4
Python 2.4.3 (#1, Jun 18 2012, 08:55:31)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
py> import simplejson
py> print simplejson





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


Re: [Tutor] Json encode and decode on Puython 2.4

2013-01-16 Thread Prasad, Ramit
Steven D'Aprano wrote:
> 
> On 17/01/13 08:50, Prasad, Ramit wrote:
> 
> > Python 2.4 is quite old and simplejson supports 2.5+. I can see a
> > yield in the encoder code, so it is unlikely to be easy to modify
> > and get working with 2.4.
> 
> Python 2.4 supports yield.

Thanks for the correction; I did not realize it was added in 2.2/2.3.


Ramit



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to track an entry in a large text file

2013-01-16 Thread Mitya Sirenef

On 01/16/2013 04:22 PM, 3n2 Solutions wrote:

Say I have a text file (example below) that has multiple records with
the same format but not always the same number of lines.  Each record
is separated by the line that starts with “Examining file” as in below
example. If you notice, the 3D Val is not there for the second record.
How do I report that the “3D Val” is not present for that particular
record “8866W2310089.txt”?
Thanks to Alan for his reply.



Examining file: 5521W231.txt

Duration :  0h : 59m
First meas : week   :  86 :   1721
Last  meas : week   :  89 :   1721

Min val   : 15
Max val : 18
3D :   3600  100.0%

summary Total  MissedRate
epoch1:1378   0   0\1000
epoch2:2154   1   0\1000



Examining file: 8866W2310089.txt

Duration :  0h : 59m
First meas : week   :  87 :   1721
Last  meas : week   :  84 :   1721

Min val   : 16
Max val : 19

summary Total  MissedRate
epoch1:1378   0   0\1000
epoch
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




If the file isn't very huge, you can split by "Examining file: ",
then iterate over the list, check if '3D' is in the item; if it
isn't, split the item by newlines and print first line.

HTH,  - mitya

--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

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


Re: [Tutor] Set LD_LIBRARY_PATH and equivalents platform-independently

2013-01-16 Thread Alan Gauld

On 16/01/13 21:06, Albert-Jan Roskam wrote:


Is there a builtin function that can set LD_LIBRARY_PATH or equivalents
platform-independently?


No.
But there is putenv() in the os module.
But how well it works across OS I'm not sure.

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

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


Re: [Tutor] Set LD_LIBRARY_PATH and equivalents platform-independently

2013-01-16 Thread Dave Angel

On 01/16/2013 07:05 PM, Alan Gauld wrote:

On 16/01/13 21:06, Albert-Jan Roskam wrote:


Is there a builtin function that can set LD_LIBRARY_PATH or equivalents
platform-independently?


No.
But there is putenv() in the os module.
But how well it works across OS I'm not sure.



I don't recall enough about Windows to be sure whether putenv would 
work, but the environment variable Windows uses to search for DLL's is 
certainly not LD_LIBRARY_PATH




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