Re: pls help me with this prog

2012-10-21 Thread 88888 Dihedral
On Friday, October 19, 2012 4:40:42 PM UTC+8, inshu chauhan wrote:
> in this prog I have written a code to calculate teh centre of a given 3D 
> data..
> 
> 
> 
> but i want to calculate it for every 3 points not the whole data, but
> 
> instead of giving me centre for every 3 data the prog is printing the
> 
> centre 3 times...
> 
> 
> 
> import cv
> 
> from math import floor, sqrt, ceil
> 
> from numpy import array, dot, subtract, add, linalg as lin
> 
> 
> 
> 
> 
> 
> 
> 
> 
> def CalcCentre(data):
> 
> centre = array([0,0,0])
> 
> count = 0
> 
> n = 0
> 
> for p in data[n:n+3]:
> 
> centre = add(centre, array(p[:3]))
> 
> count += 1
> 
> centre = dot(1./count, centre)
> 
> return centre
> 
> n += 1
> 
> def ReadPointCloud(filename):
> 
> f = open(filename)
> 
> result = []
> 
> for l in f:
> 
> sp = l.split()
> 
> t = tuple(map(float, sp[1:4]))
> 
> result.append(t)
> 
> return result
> 
> 
> 
> def main (data):
> 
> 
> 
> 
> 
> j = 0
> 
> for  i in data[:3]:
> 
> while j != 3:
> 
>  centre = CalcCentre(data)
> 
>  j += 1
> 
>  print centre
> 
> 
> 
> 
> 
> if __name__ == '__main__':
> 
> data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')
> 
> 
> 
> main(data)
> 
> 
> 
> 
> 
> 
> 
> 
> 
> PLS HELP 

# assume data is a list of 3 n numbers, n!=0

n3=data.length()
n=n/3
x=sum(data[0:n3:3])/n
y=sum(data[1:n3:3])/n
z=sum(data[2:n3:3])/n #(x,y,z)



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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Steven D'Aprano
On Sat, 20 Oct 2012 14:18:47 +, Grant Edwards wrote:

> On 2012-10-20, Dennis Lee Bieber  wrote:
> 
>>  Strangely, we've gone from 80-character fixed width displays to
>> who-knows-what (if I drop my font size I can probably get nearly 200
>> characters across in full-screen mode)...
>>
>>  But at the same time we've gone from 132-character line-printers
>> using fan-fold 11x17 pages, to office inkjet/laser printers using
>> 8.5x11 paper, defaulting to portrait orientation -- with a 10
>> character/inch font, and 1/4" left/right margins, we're back to 80
>> character limitation
>>
> 
> True, but nobody prints source code out on paper do they?

I do.

There's nothing better than spreading out a dozen sheets of source code 
over a table to get a good, high-level overview of what does what in 
preparation to refactoring it.


> Seriously -- I can't remember the last time I printed souce code...

I've never printed souce code either *wink*



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


Re: change the first letter into uppercase (ask)

2012-10-21 Thread Zero Piraeus
:

On 20 October 2012 20:22, Dennis Lee Bieber  wrote:
> Based on the documentation, most of that pattern is fluff: (?#...)
> is considered a comment.

The comment isn't entirely fluff ... it provides a Google target for
whoever's marking OP's assignment, should they choose to look it up:

https://www.google.com/search?q=nyh2p

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Chris Angelico
On Sun, Oct 21, 2012 at 7:07 PM, Steven D'Aprano
 wrote:
> On Sat, 20 Oct 2012 14:18:47 +, Grant Edwards wrote:
>> True, but nobody prints source code out on paper do they?
>
> I do.
>
> There's nothing better than spreading out a dozen sheets of source code
> over a table to get a good, high-level overview of what does what in
> preparation to refactoring it.
>
>> Seriously -- I can't remember the last time I printed souce code...
>
> I've never printed souce code either *wink*

So what you actually mean is that there's nothing _like_ spreading out
&c &c. I should think that throwing cold water over the code would be
better.

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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Steven D'Aprano
On Sun, 21 Oct 2012 20:20:41 +1100, Chris Angelico wrote:

> On Sun, Oct 21, 2012 at 7:07 PM, Steven D'Aprano
>  wrote:
>> On Sat, 20 Oct 2012 14:18:47 +, Grant Edwards wrote:
>>> True, but nobody prints source code out on paper do they?
>>
>> I do.
>>
>> There's nothing better than spreading out a dozen sheets of source code
>> over a table to get a good, high-level overview of what does what in
>> preparation to refactoring it.
>>
>>> Seriously -- I can't remember the last time I printed souce code...
>>
>> I've never printed souce code either *wink*
> 
> So what you actually mean is that there's nothing _like_ spreading out
> &c &c. I should think that throwing cold water over the code would be
> better.

Er, no. Note spelling of "source code" vs "souce code". Hence the grin.

I seriously do print out source code. When I'm having trouble seeing how 
the parts of a module fit together, reading print-outs is a good way 
around the problem. Class browsers don't show you duplicate code, and 
besides, only works with classes. Reading code on screen is limited in 
how much you can see at a time. Both of these things play a part in 
refactoring, and so does printing out the source and having a human being 
(i.e. me) look at it.


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


a prob.. error in prog ..dont knw how to correct

2012-10-21 Thread inshu chauhan
I am new to python and have a little problem to solve .. i have an
array with x, y, z co-ordinates in it as a tuple. I am trying to find
the distance between each point and sorting the points according to
the min distance.. i have tried a prog but m stuck bcoz of this error
which I am unable to correct



import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin

def calcdist(data):
for p in data:
x = p[0]
y = p[1]
z = p[2]
for i in range(len(data)):
  dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
  return dist


def ReadPointCloud(filename):
return [tuple(map(float, l.split()[1:4])) for l in open(filename)]

def main (data):

for i in range(len(data)): # Finding Neighbours
   for j in range(len(data)):
  dist = calcdist(data)
  print dist


if __name__ == '__main__':
data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')
data = data[0:100]
main(data)






the error m getting is...




Traceback (most recent call last):
  File "C:\Users\inshu\Desktop\cal-dist.py", line 29, in 
main(data)
  File "C:\Users\inshu\Desktop\cal-dist.py", line 22, in main
dist = calcdist(data)
  File "C:\Users\inshu\Desktop\cal-dist.py", line 11, in calcdist
dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
TypeError: 'float' object has no attribute '__getitem__'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: printing (was: A desperate lunge for on-topic-ness)

2012-10-21 Thread Tim Chase
On 10/21/12 05:00, Steven D'Aprano wrote:
> I seriously do print out source code. When I'm having trouble
> seeing how the parts of a module fit together, reading print-outs
> is a good way around the problem.

I don't print my personal code--both in light of the fact that I
know it much more intimately and I longer own a printer.  But when
trying to wrap my head around other people's code at work, printing
helps to get both the big picture and the details at the same time,
as well as allows me to annotate it with multi-colored
pens/highlighters.

Maybe I'll reconsider when I have a 300+ dpi desktop surface that is
as large as my desk+walls (where those printouts end up).

-tkc



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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Gene Heskett
On Sunday 21 October 2012 07:02:26 Steven D'Aprano did opine:

> On Sat, 20 Oct 2012 14:18:47 +, Grant Edwards wrote:
> > On 2012-10-20, Dennis Lee Bieber  wrote:
> >>Strangely, we've gone from 80-character fixed width displays to
> >> 
> >> who-knows-what (if I drop my font size I can probably get nearly 200
> >> characters across in full-screen mode)...
> >> 
> >>But at the same time we've gone from 132-character line-printers
> >> 
> >> using fan-fold 11x17 pages, to office inkjet/laser printers using
> >> 8.5x11 paper, defaulting to portrait orientation -- with a 10
> >> character/inch font, and 1/4" left/right margins, we're back to 80
> >> character limitation
> >>
> >>
> >>
> > True, but nobody prints source code out on paper do they?
> 
> I do.
> 
> There's nothing better than spreading out a dozen sheets of source code
> over a table to get a good, high-level overview of what does what in
> preparation to refactoring it.
> 
> > Seriously -- I can't remember the last time I printed souce code...
> 
> I've never printed souce code either *wink*

So do I, but I often am looking at assembler listings with the assembler 
set for 132 chars a line to preserve the src codes comments, so lp gets a 
use 17 cpi option on the cli that makes the listing.  I probably recycle 2 
reams of paper a year doing exactly that.  Those who won't take advantage 
of that are doomed to publish buggy code.

Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
My web page:  is up!
Not all men who drink are poets.  Some of us drink because we aren't poets.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 3.3 can't sort memoryviews as they're unorderable

2012-10-21 Thread Mark Lawrence
http://docs.python.org/dev/whatsnew/3.3.html states "memoryview 
comparisons now use the logical structure of the operands and compare 
all array elements by value".  So I'd have thought that you should be 
able to compare them and hence sort them, but this is the state of play.


Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 
bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> memoryview(bytearray(range(5))) == memoryview(bytearray(range(5)))
True
>>> memoryview(bytearray(range(5))) != memoryview(bytearray(range(5)))
False
>>> memoryview(bytearray(range(5))) < memoryview(bytearray(range(5)))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: memoryview() < memoryview()

Okay then, let's subclass memoryview to provide the functionality.

>>> class Test(memoryview):
... pass
...
Traceback (most recent call last):
  File "", line 1, in 
TypeError: type 'memoryview' is not an acceptable base type

Oh dear. 
http://docs.python.org/py3k/library/stdtypes.html#typememoryview only 
gives examples of equality comparisons and there was nothing that I 
could see in PEP3118 to explain the rationale behind the lack of other 
comparisons.  What have I missed?


--
Cheers.

Mark Lawrence.

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


Re: a prob.. error in prog ..dont knw how to correct

2012-10-21 Thread Zero Piraeus
:

On 21 October 2012 06:09, inshu chauhan  wrote:
> I am new to python and have a little problem to solve ..

> import cv

This module is not used in your code [and isn't part of the standard library].

> from math import floor, sqrt, ceil

You're only using one of these functions.

> from numpy import array, dot, subtract, add, linalg as lin

You're not using any of this.

> for p in data:
> x = p[0]
> y = p[1]
> z = p[2]

You're repeatedly making assignments in this loop, but because your
subsequent loop is not nested inside it, only the last iteration of x,
y, z will have anything done with them.

> for i in range(len(data)):

Whenever you find yourself writing

  for i in range(len(seq)):

you're doing something wrong. Instead use something like

  for a, b, c in seq:

or, if you really need the index,

  for i, (a, b, c) in enumerate(seq):

>   dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))

As previously noted, you're going to be repeatedly working with the
last values of x, y and z from the previous loop here. In addition,
since x, y and z are, according to your traceback, floats, trying to
access their members as though they are sequences isn't going to work.

>   return dist

This return statement is inside a loop, and will terminate the
function the first time it's called. That might not be what you want.

> def ReadPointCloud(filename):
> return [tuple(map(float, l.split()[1:4])) for l in open(filename)]

Assuming your file is along these lines:

  p1 1.23 4.56 7.89
  p2 9.87 6.54 3.21

... ReadPointCloud() ought to work. However, it's not very readable -
map() is sometimes useful, but usually a list comprehension is
clearer. This is better:

def ReadPointCloud(filename):
"""Return a list of 3-tuples from ``filename``."""
# not tested:
points = []
with open(filename) as f:
for line in f:
point = tuple(float(x) for x in l.split()[1:4])
points.append(point)
return points

> def main (data):
>
> for i in range(len(data)): # Finding Neighbours
>for j in range(len(data)):
>   dist = calcdist(data)
>   print dist

This will call calcdist() on everything in data, N**2 times [where N
is the number of points you're working with]. That's probably not what
you want.

> if __name__ == '__main__':
> data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')

Except for throwaway programs, it's not a good idea to hard-code
filenames like this. Better:

if __name__ == '__main__':
import sys
data = ReadPointCloud(sys.argv[1])

In summary: copy-pasting code you don't understand and mashing it
together randomly is not generally considered an effective development
technique.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Chris Angelico
On Sun, Oct 21, 2012 at 9:00 PM, Steven D'Aprano
 wrote:
> Er, no. Note spelling of "source code" vs "souce code". Hence the grin.

Ahh. I totally didn't see that, I'm way too used to reading past
typos. Sure. Printing out *source* code, that's altogether different.

Me, though, I don't print anything. Paper and I are not exactly on
speaking terms; the last time we met, he cut me, and that's one of the
rudest things you can do to someone.

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


Re: a prob.. error in prog ..dont knw how to correct

2012-10-21 Thread Jussi Piitulainen
inshu chauhan writes:

> I am new to python and have a little problem to solve .. i have an
> array with x, y, z co-ordinates in it as a tuple. I am trying to find
> the distance between each point and sorting the points according to
> the min distance.. i have tried a prog but m stuck bcoz of this error
> which I am unable to correct

You don't need help with your program. You need to learn debugging.
Your program is excellent material for that.

The first main step is to learn how to launch an interactive Python
session. Type in some expressions and learn how the interpreter
responds. The second main step is to learn how to send/import your
program to such an interactive Python session. Then you can call your
own functions on your own data and the interpreter's responses will
give you information about your own program.

> import cv
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, linalg as lin

Unused? Remove for the duration of the debugging exercise.

> def calcdist(data):
> for p in data:
> x = p[0]
> y = p[1]
> z = p[2]
> for i in range(len(data)):
>   dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
>   return dist

This is the one you need to call in your debugging session. When you
get that far, consider also adding temporary print statements to show
what the function is doing.

> def ReadPointCloud(filename):
> return [tuple(map(float, l.split()[1:4])) for l in open(filename)]
>
> def main (data):
> 
> for i in range(len(data)): # Finding Neighbours
>for j in range(len(data)):
>   dist = calcdist(data)
>   print dist
 
Well done separating the main routine as a function - you can also
call this in the interpreter.

> if __name__ == '__main__':
> data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')
> data = data[0:100]
> main(data)

Consider using a very small constant data set in the script while you
are debugging - one, two, three points. (When you get the program
working, make the file name a command line parameter, but that can
wait.)

> the error m getting is...
> 
> Traceback (most recent call last):
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 29, in 
> main(data)
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 22, in main
> dist = calcdist(data)
>   File "C:\Users\inshu\Desktop\cal-dist.py", line 11, in calcdist
> dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
> TypeError: 'float' object has no attribute '__getitem__'

Python version would be useful information, along with the information
on how you launch your program. (I don't know Microsoft environments,
but other people here do.)

Consider renaming your program as caldist.py, without the hyphen. I
think Python will complain about invalid syntax if you try to import a
module named cal-dist.

I get a different message when I try to index a float, saying
"unsubscriptable" or "not subcscriptable". Regardless, it seems that
your program is asking for x[i] where x is a number, and Python cannot
make sense of that.

(A 'float' is a type of number that lives in a computer and behaves
just a little bit like the real numbers of mathematics.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interest in seeing sh.py in the stdlib

2012-10-21 Thread Jason Friedman
> I'm interested in making sh.py more accessible to help bring Python forward
> in the area of shell scripting, so I'm interested in seeing if sh would be
> suitable for the standard library.  Is there any other interest in something
> like this?

Pretty slick.  My only concern is portability, are there other
examples of modules (excepting Win32) that work on some platforms and
not others?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread DJC

On 20/10/12 15:18, Grant Edwards wrote:

On 2012-10-20, Dennis Lee Bieber  wrote:


Strangely, we've gone from 80-character fixed width displays to
who-knows-what (if I drop my font size I can probably get nearly 200
characters across in full-screen mode)...

But at the same time we've gone from 132-character line-printers
using fan-fold 11x17 pages, to office inkjet/laser printers using 8.5x11
paper, defaulting to portrait orientation -- with a 10 character/inch
font, and 1/4" left/right margins, we're back to 80 character limitation



True, but nobody prints source code out on paper do they?


I print source code. Usually when the development has got to a stage 
that the program works but needs a lot of tidying up. It's a lot more 
comfortable than scrolling up and down screen to look through pages from 
the comfort of an armchair. Also I can take the listing to a Café and 
write notes all over it. Sometimes removing the temptation to 
immediately hit the keyboard is a good thing.


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


RE: Python does not take up available physical memory

2012-10-21 Thread Pradipto Banerjee
I tried this on a different PC with 12 GB RAM. As expected, this time, reading 
the data was no issue. I noticed that for large files, Python takes up 2.5x 
size in memory compared to size on disk, for the case when each line in the 
file is retained as a string within a Python list. As an anecdote, for MATLAB, 
the similar overhead is 2x, slightly lower than Python, and each line in the 
file was retained as string within a MATLAB cell. I'm curious, has any one 
compared the overhead of data in memory for other languages like for instance 
Ruby?


-Original Message-
From: Python-list 
[mailto:python-list-bounces+pradipto.banerjee=adainvestments@python.org] On 
Behalf Of Steven D'Aprano
Sent: Friday, October 19, 2012 6:12 PM
To: python-list@python.org
Subject: Re: Python does not take up available physical memory

On Fri, 19 Oct 2012 14:03:37 -0500, Pradipto Banerjee wrote:

> Thanks, I tried that. Still got MemoryError, but at least this time
> python tried to use the physical memory. What I noticed is that before
> it gave me the error it used up to 1.5GB (of the 2.23 GB originally
> showed as available) - so in general, python takes up more memory than
> the size of the file itself.

Well of course it does. Once you read the data into memory, it has its
own overhead for the object structure.

You haven't told us what the file is or how you are reading it. I'm going
to assume it is ASCII text and you are using Python 2.

py> open("test file", "w").write("abcde")
py> os.stat("test file").st_size
5L
py> text = open("test file", "r").read()
py> len(text)
5
py> sys.getsizeof(text)
26

So that confirms that a five byte ASCII string takes up five bytes on
disk but 26 bytes in memory as an object.

That overhead will depend on what sort of object, whether Unicode or not,
the version of Python, and how you read the data.

In general, if you have a huge amount of data to work with, you should
try to work with it one line at a time:

for line in open("some file"):
process(line)


rather than reading the whole file into memory at once:

lines = open("some file").readlines()
for line in lines:
process(line)



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

 This communication is for informational purposes only. It is not intended to 
be, nor should it be construed or used as, financial, legal, tax or investment 
advice or an offer to sell, or a solicitation of any offer to buy, an interest 
in any fund advised by Ada Investment Management LP, the Investment advisor.  
Any offer or solicitation of an investment in any of the Funds may be made only 
by delivery of such Funds confidential offering materials to authorized 
prospective investors.  An investment in any of the Funds is not suitable for 
all investors.  No representation is made that the Funds will or are likely to 
achieve their objectives, or that any investor will or is likely to achieve 
results comparable to those shown, or will make any profit at all or will be 
able to avoid incurring substantial losses.  Performance results are net of 
applicable fees, are unaudited and reflect reinvestment of income and profits.  
Past performance is no guarantee of future results. All financial 
 data and other information are not warranted as to completeness or accuracy 
and are subject to change without notice.

Any comments or statements made herein do not necessarily reflect those of Ada 
Investment Management LP and its affiliates. This transmission may contain 
information that is confidential, legally privileged, and/or exempt from 
disclosure under applicable law. If you are not the intended recipient, you are 
hereby notified that any disclosure, copying, distribution, or use of the 
information contained herein (including any reliance thereon) is strictly 
prohibited. If you received this transmission in error, please immediately 
contact the sender and destroy the material in its entirety, whether in 
electronic or hard copy format.
-- 
http://mail.python.org/mailman/listinfo/python-list


get each pair from a string.

2012-10-21 Thread Vincent Davis
I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)
In the end I am going to what to get triples, quads... also.

Thanks
Vincent
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 11:33 AM, Vincent Davis wrote:

I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)
In the end I am going to what to get triples, quads... also.



How far have you gotten?  Show us the loops you're trying now and any 
errors you're getting.


Emile



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


Re: get each pair from a string.

2012-10-21 Thread Ian Kelly
On Sun, Oct 21, 2012 at 12:33 PM, Vincent Davis
 wrote:
> I am looking for a good way to get every pair from a string. For example,
> input:
> x = 'apple'
> output
> 'ap'
> 'pp'
> 'pl'
> 'le'
>
> I am not seeing a obvious way to do this without multiple for loops, but
> maybe there is not :-)

Use the "pairwaise" recipe from the itertools docs:

def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)

> In the end I am going to what to get triples, quads... also.

Generalizing:

def nwise(iterable, n=2):
iters = tee(iterable, n)
for i, it in enumerate(iters):
for _ in range(i):
next(it, None)
return izip(*iters)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-21 Thread Vincent Davis
@Emile,
I feel a little stupid, in my mind it was more difficult than in reality.

x = 'apple'
for f in range(len(x)-1):
print(x[f:f+2])

@Ian,
Thanks for that I was just looking in to that. I wonder which is faster I
have a large set of strings to process. I'll try some timings if I get a
chance later today.


Thanks again!
Vincent




On Mon, Oct 22, 2012 at 12:45 AM, Emile van Sebille  wrote:

> On 10/21/2012 11:33 AM, Vincent Davis wrote:
>
>> I am looking for a good way to get every pair from a string. For example,
>> input:
>> x = 'apple'
>> output
>> 'ap'
>> 'pp'
>> 'pl'
>> 'le'
>>
>> I am not seeing a obvious way to do this without multiple for loops, but
>> maybe there is not :-)
>> In the end I am going to what to get triples, quads... also.
>>
>>
> How far have you gotten?  Show us the loops you're trying now and any
> errors you're getting.
>
> Emile
>
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-21 Thread Mark Lawrence

On 21/10/2012 19:33, Vincent Davis wrote:

I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)
In the end I am going to what to get triples, quads... also.

Thanks
Vincent



I suggest that you try taking slices out of your apple :)  Start here 
http://docs.python.org/tutorial/introduction.html


--
Cheers.

Mark Lawrence.

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


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 11:51 AM, Ian Kelly wrote:

On Sun, Oct 21, 2012 at 12:33 PM, Vincent Davis
 wrote:

I am looking for a good way to get every pair from a string. For example,
input:
x = 'apple'
output
'ap'
'pp'
'pl'
'le'

I am not seeing a obvious way to do this without multiple for loops, but
maybe there is not :-)


Use the "pairwaise" recipe from the itertools docs:

def pairwise(iterable):
 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
 a, b = tee(iterable)
 next(b, None)
 return izip(a, b)


In the end I am going to what to get triples, quads... also.


Generalizing:

def nwise(iterable, n=2):
 iters = tee(iterable, n)
 for i, it in enumerate(iters):
 for _ in range(i):
 next(it, None)
 return izip(*iters)





Hmmm.  And it seemed so straightforward to me as:

>>> groupsize=3
>>> a = "applesauce"
>>> for i in range(len(a)-groupsize+1): a[i:i+groupsize]
...
'app'
'ppl'
'ple'
'les'
'esa'
'sau'
'auc'
'uce'

Other than adding depth to my knowledge of the ever growing standard 
library, is there a reason to prefer pairwise over my simple loop?


Emile

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


Re: get each pair from a string.

2012-10-21 Thread Ian Kelly
On Sun, Oct 21, 2012 at 12:58 PM, Vincent Davis
 wrote:
> x = 'apple'
> for f in range(len(x)-1):
> print(x[f:f+2])
>
> @Ian,
> Thanks for that I was just looking in to that. I wonder which is faster I
> have a large set of strings to process. I'll try some timings if I get a
> chance later today.

The solution you came up with is probably faster, but less general --
it will only work on sliceable sequences like strings, not arbitrary
iterables.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Steven D'Aprano
On Sun, 21 Oct 2012 22:43:07 +1100, Chris Angelico wrote:

> On Sun, Oct 21, 2012 at 9:00 PM, Steven D'Aprano
>  wrote:
>> Er, no. Note spelling of "source code" vs "souce code". Hence the grin.
> 
> Ahh. I totally didn't see that, I'm way too used to reading past typos.

As a programmer, doesn't that screw up your debugging ability?


> Sure. Printing out *source* code, that's altogether different.
> 
> Me, though, I don't print anything. Paper and I are not exactly on
> speaking terms; the last time we met, he cut me, and that's one of the
> rudest things you can do to someone.

Man, you must have deserved it. Paper, he don't just cut anybody.


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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Grant Edwards
On 2012-10-21, Steven D'Aprano  wrote:
> On Sun, 21 Oct 2012 22:43:07 +1100, Chris Angelico wrote:
>
>> On Sun, Oct 21, 2012 at 9:00 PM, Steven D'Aprano
>>  wrote:
>>> Er, no. Note spelling of "source code" vs "souce code". Hence the grin.
>> 
>> Ahh. I totally didn't see that, I'm way too used to reading past typos.
>
> As a programmer, doesn't that screw up your debugging ability?

Indeed it does.

I spent a half hour the other day trying to figure out what was wrong
with a line of PHP code, when it was nothing but a mis-spelled
variable name.  [I've only been working with PHP a short time, but
have quickly grown to dislike it.]

-- 
Grant


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


Re: get each pair from a string.

2012-10-21 Thread Emile van Sebille

On 10/21/2012 12:06 PM, Ian Kelly wrote:

On Sun, Oct 21, 2012 at 12:58 PM, Vincent Davis
 wrote:

x = 'apple'
for f in range(len(x)-1):
 print(x[f:f+2])

@Ian,
Thanks for that I was just looking in to that. I wonder which is faster I
have a large set of strings to process. I'll try some timings if I get a
chance later today.


The solution you came up with is probably faster, but less general --
it will only work on sliceable sequences like strings, not arbitrary
iterables.



So the simple loop is the right answer for sliceable sequences like 
strings, but not if your code needs to deal with arbitrary iterables 
such as those that the standard library authors are expected to handle.


So, as OP's a self confessed newbie asking about slicing, why provide an 
example requiring knowledge of tee, enumerate, next and izip?


def nwise(iterable, n=2):
iters = tee(iterable, n)
for i, it in enumerate(iters):
for _ in range(i):
next(it, None)
return izip(*iters)

It's good that the standard library provides these tools as a 
convenience, but when all you need is a derringer, why reach for a howitzer?


Emile


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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Roy Smith
In article ,
 Grant Edwards  wrote:

> On 2012-10-21, Steven D'Aprano  wrote:
> > On Sun, 21 Oct 2012 22:43:07 +1100, Chris Angelico wrote:
> >
> >> On Sun, Oct 21, 2012 at 9:00 PM, Steven D'Aprano
> >>  wrote:
> >>> Er, no. Note spelling of "source code" vs "souce code". Hence the grin.
> >> 
> >> Ahh. I totally didn't see that, I'm way too used to reading past typos.
> >
> > As a programmer, doesn't that screw up your debugging ability?
> 
> Indeed it does.

The human brain is amazingly good at real-time error correction.  For 
the most part, this improves communication since it lets people make all 
sorts of minor errors in both spoken and written language without 
seriously degrading comprehension.

The down-side is that you hear (and read) what you're expecting to hear 
(or read).  This makes us really suck as things like finding typos in 
variable names.

> I spent a half hour the other day trying to figure out what was wrong
> with a line of PHP code, when it was nothing but a mis-spelled
> variable name.  [I've only been working with PHP a short time, but
> have quickly grown to dislike it.]

Of course, the same can happen in Python.  I could do:

foo = "default value"
if blah == 47:
   fooo = "some other value"
print foo

No syntax error, no NameError, just the wrong thing printing.  This does 
not in any way detract from the fact that PHP is a horrible language.  
Trust me, if you continue to use it, your dislike for it will only grow.  
It is truly evil.  Have you discovered "unexpected 
T_PAAMAYIM_NEKUDOTAYIM" yet?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Chris Angelico
On Mon, Oct 22, 2012 at 6:11 AM, Steven D'Aprano
 wrote:
> On Sun, 21 Oct 2012 22:43:07 +1100, Chris Angelico wrote:
>
>> On Sun, Oct 21, 2012 at 9:00 PM, Steven D'Aprano
>>  wrote:
>>> Er, no. Note spelling of "source code" vs "souce code". Hence the grin.
>>
>> Ahh. I totally didn't see that, I'm way too used to reading past typos.
>
> As a programmer, doesn't that screw up your debugging ability?

Reading-past-typos applies mainly to English, which is a pretty
redundant language. In code, it would only apply to variable names;
with (effectively) single words/tokens standing alone, the automatic
correction doesn't really apply. But yes, sometimes I have stared at a
piece of code for a long time without knowing why there's an error on
line X. (This is another good reason to require that all variables be
declared, incidentally. I might have a variable called "source" but
not "souce", so using the other causes an instant compile-time failure
on the exact line with the bug.)

And Grant, I agree; PHP does not make life easy.

>> Sure. Printing out *source* code, that's altogether different.
>>
>> Me, though, I don't print anything. Paper and I are not exactly on
>> speaking terms; the last time we met, he cut me, and that's one of the
>> rudest things you can do to someone.
>
> Man, you must have deserved it. Paper, he don't just cut anybody.

Perhaps. Also, perhaps I've just finished Hell Week and am behaving
less than sanely, with a strong tendency to quote/reference Through
The Looking Glass. :)

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


Re: Python does not take up available physical memory

2012-10-21 Thread Tim Delaney
On 22 October 2012 01:14, Pradipto Banerjee <
pradipto.baner...@adainvestments.com> wrote:

> I tried this on a different PC with 12 GB RAM. As expected, this time,
> reading the data was no issue. I noticed that for large files, Python takes
> up 2.5x size in memory compared to size on disk, for the case when each
> line in the file is retained as a string within a Python list. As an
> anecdote, for MATLAB, the similar overhead is 2x, slightly lower than
> Python, and each line in the file was retained as string within a MATLAB
> cell. I'm curious, has any one compared the overhead of data in memory for
> other languages like for instance Ruby?


What version of Python were you using? 2.7? 3.2? 3.3?

If you can, try running the same program in Python 3.3 and compare the
amount of memory used and report it here. It sounds like this might be a
case that would greatly benefit from the new string representation in 3.3.

If you're using Python 3.x then the "byte" and "bytearray" types might be
of interest to you:
http://docs.python.org/py3k/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview

Alternatively, the "array" type might be useful:
http://docs.python.org/py3k/library/array.html

As to the core problem, I can only echo what others have said - only hold
in memory what you absolutely have to. There are various techniques to
avoid holding unnecessary data in memory that have been mentioned. One I
haven't seen here yet (I may have missed it) is dumping the data into a
database of some form and using it's capabilities.

Tim Delaney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interest in seeing sh.py in the stdlib

2012-10-21 Thread Alex Clark

On 2012-10-21 16:59:16 +, Dennis Lee Bieber said:


On Sun, 21 Oct 2012 07:41:52 -0600, Jason Friedman 
declaimed the following in gmane.comp.python.general:



Pretty slick.  My only concern is portability, are there other
examples of modules (excepting Win32) that work on some platforms and
not others?


Just scan the library reference and you'll find a number of
modules/functions that, even if available on all OS, behave slightly
differently (look up mmap, for example; or readline)


I was just getting used to it as PBS :-) Other than really liking your 
lib… I'm not convinced it would be a good candidate for the stdlib, 
yet. E.g. Not every good lib belongs in the stdlib. That said, if sh.py 
continues to gain popularity (akin to e.g. requests) I suspect the 
"right people" (core developers?) will see it. I'm also curious how 
that happens, would a PEP be a good place to propose inclusion in the 
stdlib?



--
Alex Clark · https://www.gittip.com/aclark4life/


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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Chris Angelico
On Mon, Oct 22, 2012 at 7:19 AM, Roy Smith  wrote:
> Of course, the same can happen in Python.  I could do:
>
> foo = "default value"
> if blah == 47:
>fooo = "some other value"
> print foo
>
> No syntax error, no NameError, just the wrong thing printing.

Yeah, that's the worst kind of bug. No error, just wrong behaviour.
This kind of issue is one of the "balancing downsides" of the freedom
of not requiring variable declarations. For small scripts, it's not a
problem, and Python and PHP both save you the hassle of explicitly
telling the language that you really do know what you're doing, and
that's a Good Thing. For large modules, debugging creeps up in
significance, and variable declarations are less of a cost.
JaCMaScript in "use strict" mode and a good linter can catch a lot of
these sorts of bugs, though it has its own weirdnesses (why does a
'var' statement apply to the whole function regardless of where it
is?). C-derived languages with proper block scope have a good chance
of catching bugs of this nature at compile time, but at the cost of
demanding code that's mainly there to satisfy the compiler ("isn't it
OBVIOUS that I want this to be an integer? I'm assigning an integer to
it!").

> This does
> not in any way detract from the fact that PHP is a horrible language.
> Trust me, if you continue to use it, your dislike for it will only grow.
> It is truly evil.  Have you discovered "unexpected
> T_PAAMAYIM_NEKUDOTAYIM" yet?

The double-double-dot-in-Hebrew token name isn't actually a bad error;
the only problem is the token name itself. If it said "unexpected
T_SCOPE" or something, it'd be easier to debug. Several of PHP's most
annoying issues are solved in version 5.4 (array indexing a function
call that returns an array now works), but there's still a huge
fundamental that's unsolved: Unicode support. Python FTW there,
especially now that PEP 393 means strings are as compact as possible.

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


Re: get each pair from a string.

2012-10-21 Thread Joshua Landau
On 21 October 2012 19:33, Vincent Davis  wrote:

> I am looking for a good way to get every pair from a string. For example,
> input:
> x = 'apple'
> output
> 'ap'
> 'pp'
> 'pl'
> 'le'
>
> I am not seeing a obvious way to do this without multiple for loops, but
> maybe there is not :-)
> In the end I am going to what to get triples, quads... also.
>

The best way for *sliceable* objects is probably your way. However, not all
items can be sliced.

One way is this:

Let us say you have a string:

> >>> my_string = "abcdefghijklmnopqrstuvwxyz"
> >>> my_string
> 'abcdefghijklmnopqrstuvwxyz'


If you are to "zip" that, you get a "zip object"

>  >>> zip(my_string)
> 


So you want to turn it back into a list:

> >>> list(zip(my_string))
> [('a',), ('b',), ('c',), ('d',), ('e',), ('f',), ('g',), ('h',), ('i',),
> ('j',), ('k',), ('l',), ('m',), ('n',), ('o',), ('p',), ('q',), ('r',),
> ('s',), ('t',), ('u',), ('v',), ('w',), ('x',), ('y',), ('z',)]


So why would you want "zip" anyway? Let us see what it does with two inputs.

> >>> list(zip(my_string, my_string))
> [('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'), ('f', 'f'),
> ('g', 'g'), ('h', 'h'), ('i', 'i'), ('j', 'j'), ('k', 'k'), ('l', 'l'),
> ('m', 'm'), ('n', 'n'), ('o', 'o'), ('p', 'p'), ('q', 'q'), ('r', 'r'),
> ('s', 's'), ('t', 't'), ('u', 'u'), ('v', 'v'), ('w', 'w'), ('x', 'x'),
> ('y', 'y'), ('z', 'z')]


I see. It goes over the first and takes an item, then over the second and
takes an item, and then puts them together. It then does this for all the
items in each.

All we want to do is offset the second item:

> >>> list(zip(my_string, my_string[1:]))
> [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('f', 'g'),
> ('g', 'h'), ('h', 'i'), ('i', 'j'), ('j', 'k'), ('k', 'l'), ('l', 'm'),
> ('m', 'n'), ('n', 'o'), ('o', 'p'), ('p', 'q'), ('q', 'r'), ('r', 's'),
> ('s', 't'), ('t', 'u'), ('u', 'v'), ('v', 'w'), ('w', 'x'), ('x', 'y'),
> ('y', 'z')]


And then convert the results to single strings:

> >>> ["".join(strs) for strs in zip(my_string, my_string[1:])]
> ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm',
> 'mn', 'no', 'op', 'pq', 'qr', 'rs', 'st', 'tu', 'uv', 'vw', 'wx', 'xy',
> 'yz']


And this can be generalised in a more complicated way:

> >>> ["".join(strs) for strs in zip(*[my_string[n:] for n in range(4)])]
> ['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl',
> 'jklm', 'klmn', 'lmno', 'mnop', 'nopq', 'opqr', 'pqrs', 'qrst', 'rstu',
> 'stuv', 'tuvw', 'uvwx', 'vwxy', 'wxyz']


Which can work with iterables:

> >>> from itertools import islice
> >>> ["".join(strs) for strs in zip(*[islice(my_string, n, None) for n in
> range(4)])]
> ['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl',
> 'jklm', 'klmn', 'lmno', 'mnop', 'nopq', 'opqr', 'pqrs', 'qrst', 'rstu',
> 'stuv', 'tuvw', 'uvwx', 'vwxy', 'wxyz']


This will be much *faster* for short sequences made from massive strings
and much *slower* for long sequences made from medium-sized strings.
The first method *slices*, which copies a part of the whole string. This is
slow for large copies.
The second method* loops until it reaches the start*. This is slow when the
start is a long way in.

However, if you want to use slice-able items, the best way is the one
you've already worked out, as it does no extra copying or looping.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Joshua Landau
On 21 October 2012 21:38, Chris Angelico  wrote:

> On Mon, Oct 22, 2012 at 7:19 AM, Roy Smith  wrote:
> > Of course, the same can happen in Python.  I could do:
> >
> > foo = "default value"
> > if blah == 47:
> >fooo = "some other value"
> > print foo
> >
> > No syntax error, no NameError, just the wrong thing printing.
>
> Yeah, that's the worst kind of bug. No error, just wrong behaviour.
>

Au contraire, the *worst* kind is code that is wrong that - for now -
happens to work... and you have no clue why.


> This kind of issue is one of the "balancing downsides" of the freedom
> of not requiring variable declarations. For small scripts, it's not a
> problem, and Python and PHP both save you the hassle of explicitly
> telling the language that you really do know what you're doing, and
> that's a Good Thing. For large modules, debugging creeps up in
> significance, and variable declarations are less of a cost.
> JaCMaScript in "use strict" mode and a good linter can catch a lot of
> these sorts of bugs, though it has its own weirdnesses (why does a
> 'var' statement apply to the whole function regardless of where it
> is?). C-derived languages with proper block scope have a good chance
> of catching bugs of this nature at compile time, but at the cost of
> demanding code that's mainly there to satisfy the compiler ("isn't it
> OBVIOUS that I want this to be an integer? I'm assigning an integer to
> it!").
>
> > This does
> > not in any way detract from the fact that PHP is a horrible language.
> > Trust me, if you continue to use it, your dislike for it will only grow.
> > It is truly evil.  Have you discovered "unexpected
> > T_PAAMAYIM_NEKUDOTAYIM" yet?
>
> The double-double-dot-in-Hebrew token name isn't actually a bad error;
> the only problem is the token name itself. If it said "unexpected
> T_SCOPE" or something, it'd be easier to debug. Several of PHP's most
> annoying issues are solved in version 5.4 (array indexing a function
> call that returns an array now works), but there's still a huge
> fundamental that's unsolved: Unicode support. Python FTW there,
> especially now that PEP 393 means strings are as compact as possible.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-21 Thread Vlastimil Brom
2012/10/21 Vincent Davis :
> I am looking for a good way to get every pair from a string. For example,
> input:
> x = 'apple'
> output
> 'ap'
> 'pp'
> 'pl'
> 'le'
>
> I am not seeing a obvious way to do this without multiple for loops, but
> maybe there is not :-)
> In the end I am going to what to get triples, quads... also.
>
> Thanks
> Vincent
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
just another - probably less canonical - approach using the new regex
library could be (assuming the input sequence is always a string):

>>> import regex # http://pypi.python.org/pypi/regex
>>> regex.findall("..", "abcdefghijklm", overlapped=True)
['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm']
>>> regex.findall("...", "abcdefghijklm", overlapped=True)
['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij', 'ijk', 'jkl', 'klm']
>>>

If the newline \n could appear in the text, an appropriate pattern
would be e.g. "(?s)..."

regards,
   vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Walter Hurry
On Sat, 20 Oct 2012 16:37:23 -0400, Roy Smith wrote:

>  sys.stderr.write("Error: Can't find the file 'settings.py'
> in the directory containing %r.\nYou'll have to run django-profile.py,
> passing it your settings module.\n(If the file settings.py does indeed
> exist, it's causing an ImportError somehow.)\n" % __file__)

textwrap.dedent?

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


Recursive Generator Error?

2012-10-21 Thread David
I have a tree-like data structure, the basic elements are hash tables,
and they are grouped into lists, like [[{'a':1},[{'b':2}]]].
And I want to flat the lists and visit hash table one by one, like {'a':1}, 
{'b':2}.
But my program didn't work as I wish. When it entered the 2nd
flat_yield, it threw a GeneratorExit. Is there anything wrong?
Thank you very much!

#- - - - - - - - - - 
def flat_yield(tbl_list):
for t in tbl_list:
if type(t) == type({}):
yield t
elif type(t) == type([]):
flat_yield(t)
a = [[{'a':1},[{'b':2}]]]
for i in flat_yield(a):
print i
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recursive Generator Error?

2012-10-21 Thread Terry Reedy

On 10/21/2012 7:29 PM, David wrote:

I have a tree-like data structure, the basic elements are hash tables,
and they are grouped into lists, like [[{'a':1},[{'b':2}]]].
And I want to flat the lists and visit hash table one by one, like {'a':1}, 
{'b':2}.
But my program didn't work as I wish. When it entered the 2nd
flat_yield, it threw a GeneratorExit. Is there anything wrong?


1. The Python version is not specified.
2. You used 2.x; in 3.3 the code does exactly what I would expect, which 
is to say, nothing. No output, no error, no traceback ;-)

3. The traceback is missing from this post.


#- - - - - - - - - -
def flat_yield(tbl_list):
 for t in tbl_list:
 if type(t) == type({}):
 yield t
 elif type(t) == type([]):
 flat_yield(t)


4. Think harder about what that expression does.


a = [[{'a':1},[{'b':2}]]]
for i in flat_yield(a):
 print i


Hint: it calls flat_yield, which returns a generator, which is then 
discarded. You might have well written 'pass'.


Solution: use the recursively called generator and recursively yield 
what it yields. Replace 'flat_yield(t)' with


for item in flat_yield(t):
yield item

and the output is what you want.

--
Terry Jan Reedy

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


program loaded in memory

2012-10-21 Thread Anatoli Hristov
Hello,

I need an advice about a small script I run 24/24 7/7.

It's a script converted to EXE using py2exe and this script takes -
grows 30kb RAM on each loop which means that for 10hours it grows up
with 180mb memory. is there something I can do ?
>From the ini file I'm loading only the URL and the interval of
downloading the file
The script:

import time
import urllib

exec(open("iccm.ini").read())

loop = 0
while loop == 0:

time.sleep(interval*60)
try:
urllib.urlretrieve ('"'URL'"'+"/hours.xml", "c:\\config\\hours.xml")
except IOError:
pass

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


Re: program loaded in memory

2012-10-21 Thread Dave Angel
On 10/21/2012 08:02 PM, Anatoli Hristov wrote:
> Hello,
>
> I need an advice about a small script I run 24/24 7/7.
>
> It's a script converted to EXE using py2exe and this script takes -
> grows 30kb RAM on each loop which means that for 10hours it grows up
> with 180mb memory. is there something I can do ?
> >From the ini file I'm loading only the URL and the interval of
> downloading the file
> The script:
>
> import time
> import urllib
>
> exec(open("iccm.ini").read())

This line doesn't do anything useful.  And I would start by eliminating
the exec() call.

>
> loop = 0
> while loop == 0:
Since nothing ever modifies loop, you should just make it

while True:
>
> time.sleep(interval*60)

NameError: name 'interval' is not defined

> try:
> urllib.urlretrieve ('"'URL'"'+"/hours.xml", "c:\\config\\hours.xml")

SyntaxError: invalid syntax

> except IOError:
> pass
>
> Thanks

Please post the actual code you're running, as well as the Python
version.  Also, explain how you decided that it grows by 30kb each loop.

You also should try the same code without py2exe;  see if it runs any
differently.

The two errors I show are from Python 2.7. Naturally, if you post an
error, you should give the full traceback.

-- 

DaveA

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


Re: get each pair from a string.

2012-10-21 Thread Vincent Davis
@vbr
Thats interesting. I would never have come up with that.

Vincent



On Sun, Oct 21, 2012 at 3:48 PM, Vlastimil Brom wrote:

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


Re: program loaded in memory

2012-10-21 Thread Anatoli Hristov
Yes sorry, the name var(interval) is loaded from the ini file and also
the Url. The reason is that the ini file will be configured from
someone else. Example of the file

URL = www # define your url
interval = 1 # minutes for sync

I see in the task manager each time it downloads the file that it
grows up with 30kb.
And I'm using Python 2.7 on windows 7

Thanks


On 22 Oct 2012, at 02:21, Dave Angel  wrote:

> On 10/21/2012 08:02 PM, Anatoli Hristov wrote:
>> Hello,
>>
>> I need an advice about a small script I run 24/24 7/7.
>>
>> It's a script converted to EXE using py2exe and this script takes -
>> grows 30kb RAM on each loop which means that for 10hours it grows up
>> with 180mb memory. is there something I can do ?
>>> From the ini file I'm loading only the URL and the interval of
>> downloading the file
>> The script:
>>
>> import time
>> import urllib
>>
>> exec(open("iccm.ini").read())
>
> This line doesn't do anything useful.  And I would start by eliminating
> the exec() call.
>
>>
>> loop = 0
>> while loop == 0:
> Since nothing ever modifies loop, you should just make it
>
>while True:
>>
>>time.sleep(interval*60)
>
> NameError: name 'interval' is not defined
>
>>try:
>>urllib.urlretrieve ('"'URL'"'+"/hours.xml", "c:\\config\\hours.xml")
>
> SyntaxError: invalid syntax
>
>>except IOError:
>>pass
>>
>> Thanks
>
> Please post the actual code you're running, as well as the Python
> version.  Also, explain how you decided that it grows by 30kb each loop.
>
> You also should try the same code without py2exe;  see if it runs any
> differently.
>
> The two errors I show are from Python 2.7. Naturally, if you post an
> error, you should give the full traceback.
>
> --
>
> DaveA
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-21 Thread Vincent Davis
To All,
I appreciate the range of answers and the time each of you take to think
about and answer my question. Whether or not I use them I find them all
educational.
Thanks again.

Vincent



On Mon, Oct 22, 2012 at 2:03 AM, Emile van Sebille  wrote:

> On 10/21/2012 12:06 PM, Ian Kelly wrote:
>
>> On Sun, Oct 21, 2012 at 12:58 PM, Vincent Davis
>>  wrote:
>>
>>> x = 'apple'
>>> for f in range(len(x)-1):
>>>  print(x[f:f+2])
>>>
>>> @Ian,
>>> Thanks for that I was just looking in to that. I wonder which is faster I
>>> have a large set of strings to process. I'll try some timings if I get a
>>> chance later today.
>>>
>>
>> The solution you came up with is probably faster, but less general --
>> it will only work on sliceable sequences like strings, not arbitrary
>> iterables.
>>
>>
> So the simple loop is the right answer for sliceable sequences like
> strings, but not if your code needs to deal with arbitrary iterables such
> as those that the standard library authors are expected to handle.
>
> So, as OP's a self confessed newbie asking about slicing, why provide an
> example requiring knowledge of tee, enumerate, next and izip?
>
>
> def nwise(iterable, n=2):
> iters = tee(iterable, n)
> for i, it in enumerate(iters):
> for _ in range(i):
> next(it, None)
> return izip(*iters)
>
> It's good that the standard library provides these tools as a convenience,
> but when all you need is a derringer, why reach for a howitzer?
>
> Emile
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recursive Generator Error?

2012-10-21 Thread David
On Monday, October 22, 2012 7:59:53 AM UTC+8, Terry Reedy wrote:
> On 10/21/2012 7:29 PM, David wrote:
> 
> > I have a tree-like data structure, the basic elements are hash tables,
> 
> > and they are grouped into lists, like [[{'a':1},[{'b':2}]]].
> 
> > And I want to flat the lists and visit hash table one by one, like {'a':1}, 
> > {'b':2}.
> 
> > But my program didn't work as I wish. When it entered the 2nd
> 
> > flat_yield, it threw a GeneratorExit. Is there anything wrong?
> 
> 
> 
> 1. The Python version is not specified.
> 
> 2. You used 2.x; in 3.3 the code does exactly what I would expect, which 
> 
> is to say, nothing. No output, no error, no traceback ;-)
> 
> 3. The traceback is missing from this post.
> 
> 
> 
> > #- - - - - - - - - -
> 
> > def flat_yield(tbl_list):
> 
> >  for t in tbl_list:
> 
> >  if type(t) == type({}):
> 
> >  yield t
> 
> >  elif type(t) == type([]):
> 
> >  flat_yield(t)
> 
> 
> 
> 4. Think harder about what that expression does.
> 
> 
> 
> > a = [[{'a':1},[{'b':2}]]]
> 
> > for i in flat_yield(a):
> 
> >  print i
> 
> 
> 
> Hint: it calls flat_yield, which returns a generator, which is then 
> 
> discarded. You might have well written 'pass'.
> 
> 
> 
> Solution: use the recursively called generator and recursively yield 
> 
> what it yields. Replace 'flat_yield(t)' with
> 
> 
> 
>  for item in flat_yield(t):
> 
>  yield item
> 
> 
> 
> and the output is what you want.
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

Hi Terry, thank you! I use Python 2.7, and your solution works!

I have thought harder, still not very clear. 

If I have one "yield" in function, the function will become generator, and it 
can only be called in the form like "for item in function()" or 
"function.next()", and call the function directly will raise error, is it right?

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


Re: program loaded in memory

2012-10-21 Thread Dave Angel
On 10/21/2012 08:31 PM, Anatoli Hristov wrote:
> Yes sorry, the name var(interval) is loaded from the ini file and also
> the Url. The reason is that the ini file will be configured from
> someone else. Example of the file
> 
> URL = www # define your url
> interval = 1 # minutes for sync
> 
> I see in the task manager each time it downloads the file that it
> grows up with 30kb.
> And I'm using Python 2.7 on windows 7
> 
> Thanks
> 
So now, that eliminates one of the errors.  (Though import is much more
reasonable than exec(), and some config parser is better yet)

To fix the syntax error, you need + signs before and after the variable URL




-- 

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


Re: Recursive Generator Error?

2012-10-21 Thread Steven D'Aprano
On Sun, 21 Oct 2012 17:40:41 -0700, David wrote:

> If I have one "yield" in function, the function will become generator,

Almost correct. The function becomes a *generator function*, that is, a 
function that returns a generator object.

Sometimes people abbreviate that to "generator", but that is ambiguous -- 
the term "generator" can mean either the function which includes yield in 
it, or the object that is returned.

> and it can only be called in the form like "for item in function()" or
> "function.next()", and call the function directly will raise error, is
> it right?

You can call the function directly, and it will return an generator 
object. You don't have to iterate over that generator object, although 
you normally will.

Example:

py> def test():
... yield 42
... 
py> test

py> type(test)

py> x = test()
py> x

py> type(x)



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


Re: Preventing crap email from google?

2012-10-21 Thread Ben Finney
Grant Edwards  writes:

> Posts made via the google-groups web site are a problem, and I plonked
> them all years and years ago...

Walter Hurry  writes:

> It is Google bloody Groups which is the problem. I should have plonked 
> posts from there ages ago, and am about to remedy that omission.

What narrowly-defined, precise filter rule should be used for this
purpose?

-- 
 \  “Nothing is more sacred than the facts.” —Sam Harris, _The End |
  `\   of Faith_, 2004 |
_o__)  |
Ben Finney

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


Re: get each pair from a string.

2012-10-21 Thread Ian Foote

On 22/10/12 09:03, Emile van Sebille wrote:

So, as OP's a self confessed newbie asking about slicing, why provide an
example requiring knowledge of tee, enumerate, next and izip?



Because not only the newbie will read the thread? I for one was 
interested to see all the different possible approaches, and their 
upsides and downsides.


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


Re: A desperate lunge for on-topic-ness

2012-10-21 Thread Steven D'Aprano
On Mon, 22 Oct 2012 07:22:18 +1100, Chris Angelico wrote:

> On Mon, Oct 22, 2012 at 6:11 AM, Steven D'Aprano
>  wrote:

>>> Ahh. I totally didn't see that, I'm way too used to reading past
>>> typos.
>>
>> As a programmer, doesn't that screw up your debugging ability?
> 
> Reading-past-typos applies mainly to English, which is a pretty
> redundant language. In code, it would only apply to variable names; with
> (effectively) single words/tokens standing alone, the automatic
> correction doesn't really apply. But yes, sometimes I have stared at a
> piece of code for a long time without knowing why there's an error on
> line X. (This is another good reason to require that all variables be
> declared, incidentally. I might have a variable called "source" but not
> "souce", so using the other causes an instant compile-time failure on
> the exact line with the bug.)

"Another" good reason?

For languages without static types, what other reasons for declaring 
variables are there?



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