Re: Fast forward-backward (write-read)

2012-10-24 Thread Virgil Stokes

On 24-Oct-2012 00:36, David Hutto wrote:

Don't forget to use timeit for an average OS utilization.

I'd suggest two list comprehensions for now, until I've reviewed it some more:

forward =  ["%i = %s" % (i,chr(i)) for i in range(33,126)]
backward = ["%i = %s" % (i,chr(i)) for i in range(126,32,-1)]

for var in forward:
 print var

for var in backward:
 print var

You could also use a dict, and iterate through a straight loop that
assigned a front and back to a dict_one =  {0 : [0.100], 1 : [1.99]}
and the iterate through the loop, and call the first or second in the
dict's var list for frontwards , or backwards calls.


But there might be faster implementations, depending on other
function's usage of certain lower level functions.


Missed the part about it being a file. Use:

forward =  ["%i = %s" % (i,chr(i)) for i in range(33,126)]
backward = ["%i = %s" % (i,chr(i)) for i in range(126,32,-1)]

print forward,backward

Interesting approach for small data sets (or blocks from a much larger data 
set).

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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Virgil Stokes

On 24-Oct-2012 02:06, Oscar Benjamin wrote:

On 23 October 2012 15:31, Virgil Stokes  wrote:

I am working with some rather large data files (>100GB) that contain time
series data. The data (t_k,y(t_k)), k = 0,1,...,N are stored in ASCII
format. I perform various types of processing on these data (e.g. moving
median, moving average, and Kalman-filter, Kalman-smoother) in a sequential
manner and only a small number of these data need be stored in RAM when
being processed. When performing Kalman-filtering (forward in time pass, k =
0,1,...,N) I need to save to an external file several variables (e.g. 11*32
bytes) for each (t_k, y(t_k)). These are inputs to the Kalman-smoother
(backward in time pass, k = N,N-1,...,0). Thus, I will need to input these
variables saved to an external file from the forward pass, in reverse order
--- from last written to first written.

Finally, to my question --- What is a fast way to write these variables to
an external file and then read them in backwards?

You mentioned elsewhere that you are using numpy. I'll assume that the
data you want to read/write are numpy arrays.

Numpy arrays can be written very efficiently in binary form using
tofile/fromfile:


import numpy
a = numpy.array([1, 2, 5], numpy.int64)
a

array([1, 2, 5])

with open('data.bin', 'wb') as f:

...   a.tofile(f)
...

You can then reload the array with:


with open('data.bin', 'rb') as f:

...   a2 = numpy.fromfile(f, numpy.int64)
...

a2

array([1, 2, 5])

Numpy arrays can be reversed before writing or after reading using;


a2

array([1, 2, 5])

a2[::-1]

array([5, 2, 1])

Assuming you wrote the file forwards you can make an iterator to yield
the file in chunks backwards like so (untested):

def read_backwards(f, dtype, chunksize=1024 ** 2):
 dtype = numpy.dtype(dtype)
 nbytes = chunksize * dtype.itemsize
 f.seek(0, 2)
 fpos = f.tell()
 while fpos > nbytes:
 f.seek(fpos, 0)
 yield numpy.fromfile(f, dtype, chunksize)[::-1]
 fpos -= nbytes
 yield numpy.fromfile(f, dtype)[::-1]


Oscar

Ok Oscar,
Thanks for the tip and I will look into this more.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread Virgil Stokes

On 24-Oct-2012 00:57, Demian Brecht wrote:

This is a classic example of why the old external processing algorithms
of the 1960s and 70s will never be obsolete. No matter how much memory
you have, there will always be times when you want to process more data
than you can fit into memory.


But surely nobody will *ever* need more than 640k…

Right?

Demian Brecht
@demianbrecht
http://demianbrecht.github.com





Yes, I can still remember such quotes --- thanks for jogging my memory, Demian 
:-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread Virgil Stokes

On 24-Oct-2012 00:53, Steven D'Aprano wrote:

On Tue, 23 Oct 2012 17:50:55 -0400, David Hutto wrote:


On Tue, Oct 23, 2012 at 10:31 AM, Virgil Stokes  wrote:

I am working with some rather large data files (>100GB)

[...]

Finally, to my question --- What is a fast way to write these variables
to an external file and then read them in backwards?

Don't forget to use timeit for an average OS utilization.

Given that the data files are larger than 100 gigabytes, the time
required to process each file is likely to be in hours, not microseconds.
That being the case, timeit is the wrong tool for the job, it is
optimized for timings tiny code snippets. You could use it, of course,
but the added inconvenience doesn't gain you any added accuracy.

Here's a neat context manager that makes timing long-running code simple:


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

Thanks for this link





I'd suggest two list comprehensions for now, until I've reviewed it some
more:

I would be very surprised if the poster will be able to fit 100 gigabytes
of data into even a single list comprehension, let alone two.
You are correct and I have been looking at working with blocks that are sized to 
the RAM available for processing.


This is a classic example of why the old external processing algorithms
of the 1960s and 70s will never be obsolete. No matter how much memory
you have, there will always be times when you want to process more data
than you can fit into memory.




Thanks for your insights :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread David Hutto
On Wed, Oct 24, 2012 at 3:05 AM, Virgil Stokes  wrote:
> On 24-Oct-2012 00:36, David Hutto wrote:
>>>
>>> Don't forget to use timeit for an average OS utilization.
>>>
>>> I'd suggest two list comprehensions for now, until I've reviewed it some
>>> more:
>>>
>>> forward =  ["%i = %s" % (i,chr(i)) for i in range(33,126)]
>>> backward = ["%i = %s" % (i,chr(i)) for i in range(126,32,-1)]
>>>
>>> for var in forward:
>>>  print var
>>>
>>> for var in backward:
>>>  print var
>>>
>>> You could also use a dict, and iterate through a straight loop that
>>> assigned a front and back to a dict_one =  {0 : [0.100], 1 : [1.99]}
>>> and the iterate through the loop, and call the first or second in the
>>> dict's var list for frontwards , or backwards calls.
>>>
>>>
>>> But there might be faster implementations, depending on other
>>> function's usage of certain lower level functions.
>>>
>> Missed the part about it being a file. Use:
>>
>> forward =  ["%i = %s" % (i,chr(i)) for i in range(33,126)]
>> backward = ["%i = %s" % (i,chr(i)) for i in range(126,32,-1)]
>>
>> print forward,backward
>
> Interesting approach for small data sets (or blocks from a much larger data
> set).
>
> Thanks David :-)

No problem.

I think this was for a > 100GB, which might be able to be reduced for
parsing if I could see a snippet of the usual data being processed by
the function

But it does go to big O notation, and optimization of the average data
being passed through, unless the data varies in wide ranges, in which
that could be optimized to go from smaller to larger, vice versa, or
othe pieces of data with a higher priority level..




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread David Hutto
On Wed, Oct 24, 2012 at 3:17 AM, Virgil Stokes  wrote:
> On 24-Oct-2012 00:57, Demian Brecht wrote:
>>>
>>> This is a classic example of why the old external processing algorithms
>>> of the 1960s and 70s will never be obsolete. No matter how much memory
>>> you have, there will always be times when you want to process more data
>>> than you can fit into memory.
>>
>>
>> But surely nobody will *ever* need more than 640k…
>>
>> Right?
>>
>> Demian Brecht
>> @demianbrecht
>> http://demianbrecht.github.com
>>
>>
>>
>>
> Yes, I can still remember such quotes --- thanks for jogging my memory,
> Demian :-)


This is only on equipment designed by others, otherwise, you could
engineer the hardware yourself to perfom just certain functions for
you(RISC), and pass that back to the CISC(from a PCB design).


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread Virgil Stokes

On 23-Oct-2012 22:03, Cousin Stanley wrote:

Virgil Stokes wrote:


Not sure about "tac" --- could you provide more details on this
and/or a simple example of how it could be used for fast reversed
"reading" of a data file ?

   tac is available as a command under linux 

   $ whatis tac
   tac (1) - concatenate and print files in reverse

   $ whereis tac
   tac: /usr/bin/tac /usr/bin/X11/tac /usr/share/man/man1/tac.1.gz

   $ man tac

   SYNOPSIS
 tac [OPTION]... [FILE]...

   DESCRIPTION

 Write each FILE to standard output, last line first.

 With no FILE, or when FILE is -, read standard input.


   I only know that the  tac  command exists
   but have never used it myself 


Unfortunately, I may be forced to process the data on a Windows platform; but, 
thanks Cousin for the Linux tip.



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


Re: turn list of letters into an array of integers

2012-10-24 Thread Peter Otten
Chris Rebert wrote:

> line.strip().split()

No need to strip() if you are going to split on whitespace:

>>> line = " a b c \n"
>>> line.split() == line.strip().split()
True

Lest the new idiom takes on while you are bravely fighting the immortable 
readlines() ;)

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


Re: Split single file into multiple files based on patterns

2012-10-24 Thread Steven D'Aprano
On Tue, 23 Oct 2012 20:01:03 -0700, satyam wrote:

> I have a text file like this
> 
> A1980JE3937 2732 4195 12.527000
[...]

> I want to split the file and get multiple files like A1980JE3937.txt
> and A1980KK18700010.txt, where each file will contain column2, 3 and 4.

Are you just excited and want to tell everyone, or do you actually have a 
question?

Have you tried to write some code, or do you just expect others to do 
your work for you?

If so, I see that your expectation was correct.



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


Re: Split single file into multiple files based on patterns

2012-10-24 Thread David Hutto
On Wed, Oct 24, 2012 at 3:52 AM, Steven D'Aprano
 wrote:
> On Tue, 23 Oct 2012 20:01:03 -0700, satyam wrote:
>
>> I have a text file like this
>>
>> A1980JE3937 2732 4195 12.527000
> [...]
>
>> I want to split the file and get multiple files like A1980JE3937.txt
>> and A1980KK18700010.txt, where each file will contain column2, 3 and 4.
>
> Are you just excited and want to tell everyone, or do you actually have a
> question?
>
> Have you tried to write some code, or do you just expect others to do
> your work for you?
>
> If so, I see that your expectation was correct.
>
>
>
> --
> Steven

Some learn better with a full example, better than any small challenge
that can be thrown in at certain times.

I think it should be a little of both, especially if you (an
algorithmitist for the OP)only have enough time to throw out untested
pseudo code.

-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread Steven D'Aprano
On Wed, 24 Oct 2012 01:23:58 -0400, Dennis Lee Bieber wrote:

> On Tue, 23 Oct 2012 16:35:40 -0700, emile  declaimed the
> following in gmane.comp.python.general:
> 
>> On 10/23/2012 04:19 PM, David Hutto wrote:
>> > forward =  [line.rstrip('\n') for line in f.readlines()]
>> 
>> f.readlines() will be big(!) and have overhead... and forward results
>> in something again as big.
>>
>   Well, since file objects are iterable, could one just drop the
> .readlines() ? ( ... line in f )

Yes, but the bottleneck is still that the list comprehension will run to 
completion, trying to process the entire 100+ GB file in one go.

[...]
>   And since the line-ends have already been stripped from forward,
> backward should just be:
> 
>   backward = reversed(forward)

reversed returns a lazy iterator, but it requires that forward is a non-
lazy (eager) sequence. So again you're stuck trying to read the entire 
file into RAM.

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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Tim Golden
On 24/10/2012 08:07, Virgil Stokes wrote:
> On 23-Oct-2012 22:03, Cousin Stanley wrote:
>> Virgil Stokes wrote:
>>
>>> Not sure about "tac" --- could you provide more details on this
>>> and/or a simple example of how it could be used for fast reversed
>>> "reading" of a data file ?
>>tac is available as a command under linux 
>>
>>$ whatis tac
>>tac (1) - concatenate and print files in reverse
>>
>>$ whereis tac
>>tac: /usr/bin/tac /usr/bin/X11/tac /usr/share/man/man1/tac.1.gz
>>
>>$ man tac
>>
>>SYNOPSIS
>>  tac [OPTION]... [FILE]...
>>
>>DESCRIPTION
>>
>>  Write each FILE to standard output, last line first.
>>
>>  With no FILE, or when FILE is -, read standard input.
>>
>>
>>I only know that the  tac  command exists
>>but have never used it myself 
>>
>>
> Unfortunately, I may be forced to process the data on a Windows
> platform; but, thanks Cousin for the Linux tip.

Well, addressing that specific point, tac is available for Windows:

  http://unxutils.sourceforge.net/

No idea how efficient it is...

TJG

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


Re: Split single file into multiple files based on patterns

2012-10-24 Thread Peter Otten
satyam wrote:

> I have a text file like this
> 
> A1980JE3937 2732 4195 12.527000
> A1980JE3937 3465 9720 22.00
> A1980JE3937 1853 3278 12.50
> A1980JE3937 2732 2732 187.50
> A1980JE3937 19 4688 3.619000
> A1980JE3937 2995 9720 6.667000
> A1980JE3937 1603 9720 30.00
> A1980JE3937 234 4195 42.416000
> A1980JE3937 2732 9720 18.00
> A1980KK18700010 130 303 4.985000
> A1980KK18700010 7 4915 0.435000
> A1980KK18700010 25 1620 1.722000
> A1980KK18700010 25 186 0.654000
> A1980KK18700010 50 130 3.199000
> A1980KK18700010 186 3366 4.78
> A1980KK18700010 30 186 1.285000
> A1980KK18700010 30 185 4.395000
> A1980KK18700010 185 186 9.00
> A1980KK18700010 25 30 3.493000
> 
> I want to split the file and get multiple files like A1980JE3937.txt
> and A1980KK18700010.txt, where each file will contain column2, 3 and 4.
> Thanks Satyam

import os
from itertools import groupby
from operator import itemgetter

get_key = itemgetter(0)
get_value = itemgetter(1)

output_folder = "tmp"
with open("infile.txt") as instream:
pairs = (line.split(None, 1) for line in instream)
for key, group in groupby(pairs, key=get_key):
path = os.path.join(output_folder, key + ".txt")
with open(path, "a") as outstream:
outstream.writelines(get_value(line) for line in group)

If you are running the code more than once make sure that you remove the 
files from the previous run first.

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


Re: turn list of letters into an array of integers

2012-10-24 Thread Peter Otten
Peter Otten wrote:

Brave new words:

> immortable

should be "immortal"

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


Re: get each pair from a string.

2012-10-24 Thread wxjmfauth
Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :
> On Tue, Oct 23, 2012 at 12:47 AM,   wrote:
> 
> 
> 
> In any case, have you been following the progress of issue 16061?
> 
> There is a patch for the str.replace regression that you identified,
> 
> which results in better performance across the board than 3.2.  So
> 
> although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will
> 
> not be.

Yes.
A workaround to solve one of the corner cases of something
which is wrong by design.

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


[SOLVED] Re: Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`?

2012-10-24 Thread Daniel Dehennin
Daniel Dehennin  writes:

> Hello,

Hi


[...]

> I tried to setup the same for my scripts with
> "logging.config.dictConfig()" without much success, I want to limit
> output to stdout to INFO, everything bellow INFO should be sent to
> stderr instead.
>
> Any hints?

I finally find a solution for my script, I added a filter.

Regards.

#+begin_src python
class UniqLevelFilter(logging.Filter):
def __init__(self, level):
self._level = level

def filter(self, rec):
return rec.levelno == self._level

def init_logging(options):
# TODO: color stderr messages by level if sys.stderr.isatty()
# 
http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
# 
http://plumberjack.blogspot.fr/2010/12/colorizing-logging-output-in-terminals.html
config = { 'version' : 1,
   'filters' : { 'stdout' : { '()' : '__main__.UniqLevelFilter',
  'level' : logging.INFO, },
},
   'formatters' : { 'stdout' : { 'format' : '%(message)s',
 'datefmt' : '', },
'stderr' : { 'format' : '%(message)s',
 'datefmt' : '', },
},
   'handlers' : { 'stdout' : { 'class' : 'logging.StreamHandler',
   'stream' : 'ext://sys.stdout',
   'level' : 'INFO',
   'filters' : ['stdout'],
   'formatter' : 'stdout', },
  'stderr' : { 'class' : 'logging.StreamHandler', 
   'stream' : 'ext://sys.stderr',
   'level' : 'WARNING', 
   'formatter' : 'stderr', }
},
   'root' : { 'level' : options.log_level.upper(),
  'handlers' : ['stdout', 'stderr'],
},
}

logging.config.dictConfig( config )
return logging.getLogger()

#+end_src

-- 
Daniel Dehennin
EOLE


pgppxZX1W6r9Y.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Python input function not working in 2.7 version

2012-10-24 Thread Morten Engvoldsen
Hi,
I am facing issue with input() of Python 2.7. When i run the program it
doesn't display any line to take user input . Below is the code:

def user_input()
   fat_grams = input("How many grams of fat are in one serving? ")
   total_calories = input("How many total calories are in one serving? ")
   print("A food product having {0} fat grams and {1} total
calories",fat_grams,
total_calories);
   return;

def main():
   if __name__ == "__main__": main()

Also i would like to display fat_grams where {0} place holder is there and
total_calories where {1} place holder is there.
That is if :
fat_grams = 3
total_calories = 270

Output should be: A food product having 3 fat grams and 270 total calories.

But the above print function doesn't display the out put in same way. I am
new to Python and i appreciate your help in advance.

Look forward to hear from your team soon and have a nice day.

Regards,
Morten
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python input function not working in 2.7 version

2012-10-24 Thread Chris Angelico
On Wed, Oct 24, 2012 at 9:37 PM, Morten Engvoldsen  wrote:
> I am facing issue with input() of Python 2.7. When i run the program it
> doesn't display any line to take user input . Below is the code:
>
> But the above print function doesn't display the out put in same way. I am
> new to Python and i appreciate your help in advance.
>

Two points to note. Firstly, print is a statement in Python 2; you can
call on the print function with a future directive, but otherwise,
what you're doing there is passing a tuple to the print statement, so
it prints out like this:

('A food product having {0} fat grams and {1} total calories', 3, 270)


What you want, I think, is to explicitly use that as a format string:

print("A food product having {0} fat grams and {1} total
calories".format(fat_grams, total_calories))
A food product having 3 fat grams and 270 total calories

This then passes a single string to print, which works fine
(parentheses around a single thing do nothing; it's the comma that
makes the tuple, not the parens).

The second point to note is that, in Python 2, input() will *eval*
what the user types. Try your program and type in "1+2" (without the
quotes) at the prompt - you'll see that Python evaluates what you
typed, as though you'd entered "3". This is VERY DANGEROUS, and should
be avoided. Instead, use raw_input(), which does what you expect.

Alternatively, switch to Python 3, in which print is a function and
input() behaves exactly as you'd expect. But you'll still want to
explicitly call format().

Further reading:

http://docs.python.org/reference/simple_stmts.html#future-statements
http://docs.python.org/library/functions.html#input
http://docs.python.org/library/functions.html#raw_input
http://docs.python.org/library/stdtypes.html#str.format
http://docs.python.org/py3k/

Note, incidentally, that even though str.format() is called "new-style
formatting", there's no deprecation of "percent-style formatting"
(similar to C's sprintf function), so if you're familiar with that,
you needn't feel compelled to switch.

Hope that helps!

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Kevin Holleran
On Tue, Oct 23, 2012 at 11:44 AM, Kevin Holleran  wrote:

> On Tue, Oct 23, 2012 at 11:39 AM, Tim Golden  wrote:
>
>> On 23/10/2012 16:17, Kevin Holleran wrote:
>> > I am still having a small implementation problem
>> >
>> > [code]
>> > HKLM = winreg.HKEY_LOCAL_MACHINE
>> > NICs =
>> >
>> "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"
>> >
>> >  registry = wmi.WMI(host, namespace="default").StdRegProv
>> >   _, names = registry.EnumKey(HKLM,NICs) # <-- Error
>> > [/code]
>> >
>> > [error]
>> >   File "wmi.pyc", line 241, in handle_com_error
>> > wmi.x_wmi: > > occurred.', (0,
>> >  u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>
>> > [\error]
>>
>>
>> I can see nothing wrong with that. On my system the following code works
>> fine:
>>
>> 
>> import _winreg as winreg
>> import wmi
>>
>> host = "SVR18"
>>
>> HKLM = winreg.HKEY_LOCAL_MACHINE
>> NICs =
>>
>> "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"
>>
>> registry = wmi.WMI(host, namespace="default").StdRegProv
>> _, names = registry.EnumKey(HKLM,NICs) # <-- Error
>>
>> print names
>>
>> 
>>
>> Am I understanding you correctly? Can you send me an interpreter
>> screendump showing the failure with the traceback (just in case anything
>> useful occurs to me when I see it)?
>>
>>
>> TJG
>>
>
> I will but I have to run to a meeting.  I will send over.  Another note,
> probably should have mentioned earlier, I am using py2exe & running the
> executable from the the machine that has access to these systems.
>
> Kevin
>

Here is the full traceback:

[output]
Scan_NIC_Driver_Info_1.2.py -i testing_MSK_Server.csv -o
MSK_Test_output.csv -u unreachable.csv
Parsing input file...

Connecting to IP...

Traceback (most recent call last):
  File
"D:\Development\Scripts\Python\Scan_NIC_Driver_Info\Scan_NIC_Driver_Info_
1.2.py", line 70, in 
_, names = registry.EnumKey(HKLM,NICs)
  File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
handle_com_error ()
  File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: 

[/output]

Basically, it parses a list of hosts from a CSV, outputs a series of lines:
host, driverDesc, diverDate, driverVersion.  Basically it pings the host
first to make sure its up.

Here are the relevant parts to the script:

[code]
 registry = wmi.WMI(host, namespace="default").StdRegProv
_, names = registry.EnumKey(HKLM,NICs)
print("Connected successfully.")
try:
for name in names:
print name
NIC = NICs + "\\" + name
_, value_names, value_types = registry.EnumValues(HKLM, NIC)
for value_name, value_type in zip(value_names, value_types):
if value_type == winreg.REG_SZ:
if value_name == "BusType":
_, busType = registry.GetStringValue(HKLM,
NICs, value_name)
elif value_name == "DriverDesc":
_, driverDesc = registry.GetStringValue(HKLM,
NICs, value_name)
elif value_name == "DriverDate":
_, driverDate = registry.GetStringValue(HKLM,
NICs, value_name)
elif value_name == "DriverVersion":
_, driverVersion =
registry.GetStringValue(HKLM, NICs, value_name)
if busType == 5:
outputline = host + "," + driverDesc + "," +
driverDate + "," + driverVersion + "\n"
print outputline
outputFile.write(outputLine)
except:
print "Unable to query registry key for NIC adapters"
[/code]

Thanks again.

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 12:40, Kevin Holleran wrote:> Here is the full traceback:
>
> [output]
> Scan_NIC_Driver_Info_1.2.py  -i
> testing_MSK_Server.csv -o MSK_Test_output.csv -u unreachable.csv
> Parsing input file...
>
> Connecting to IP...
>
> Traceback (most recent call last):
>   File
> "D:\Development\Scripts\Python\Scan_NIC_Driver_Info\Scan_NIC_Driver_Info_
> 1.2.py ", line 70, in 
> _, names = registry.EnumKey(HKLM,NICs)
>   File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
> handle_com_error ()
>   File "C:\Python27\lib\site-packages\wmi.py", line 241, in
handle_com_error
> raise klass (com_error=err)
> wmi.x_wmi:  occurred.', (0,
>  u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>
>
> [/output]
>
> Basically, it parses a list of hosts from a CSV, outputs a series of
> lines: host, driverDesc, diverDate, driverVersion.  Basically it pings
> the host first to make sure its up.
>
> Here are the relevant parts to the script:
>
> [code]
>  registry = wmi.WMI(host, namespace="default").StdRegProv
> _, names = registry.EnumKey(HKLM,NICs)

Could you confirm what version of Windows is running on the remote
machine? Also, could you show the output of the following, please:


import wmi

host = "" # pick one

print wmi.WMI(host, namespace="default").StdRegProv




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


Re: turn list of letters into an array of integers

2012-10-24 Thread 88888 Dihedral
Chris Rebert於 2012年10月24日星期三UTC+8下午2時07分29秒寫道:
> On Tue, Oct 23, 2012 at 10:23 PM, seektime  wrote:
> 
> > Here's some example code. The input is a list which is a "matrix" of 
> > letters:
> 
> >a  b  a
> 
> >b  b  a
> 
> >
> 
> > and I'd like to turn this into a Python array:
> 
> 
> 
> You mean a Python list. The datatype Python calls an `array` is very
> 
> different and relatively uncommonly used.
> 
> Although, confusingly, Python's lists are implemented using C arrays
> 
> rather than linked lists.

The list in python is a list of valid python objects.
For the number crunching part, please use  arrays in numarray and scipy. 

> 
> >   1 2 1
> 
> >   2 2 1
> 
> >
> 
> > so 1 replaces a, and 2 replaces b. Here's the code I have so far:
> 
> >
> 
>  L=['a b a\n','b b a\n']
> 
> 
> 
>  seq
> 
> > '1 2 1\n 2 2 1\n'
> 
> >
> 
> > My question is how can I turn "seq" into a python array?
> 
> 
> 
> I'd say you're asking the wrong question. The better question is "Why
> 
> wasn't the result a list in the first place?". Many transformations
> 
> are cumbersome to express over just strings, which is why the first
> 
> job of most programs is to parse their input into a more convenient
> 
> structure that is suited to their main task(s).
> 
> 
> 
> This (along with some other improvements) leads to a better, somewhat
> 
> different program/algorithm:
> 
> 
> 
> letter2number = {'a': 1, 'b': 2}
> 
> with open("path/to/file.txt", "r") as f:
> 
> result = [[letter2number[letter] for letter in
> 
> line.strip().split()] for line in f]
> 
> 
> 
> If it's safe to assume that the correspondence between the letters and
> 
> numbers isn't completely arbitrary, some further improvements are also
> 
> possible.
> 
> 
> 
> Some relevant docs:
> 
> http://docs.python.org/library/stdtypes.html#string-methods
> 
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions
> 
> 
> 
> Cheers,
> 
> Chris
> 
> 
> 
> P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
> 
> it is worth noting for future reference that the readlines() method is
> 
> considered somewhat deprecated.

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


classes

2012-10-24 Thread inshu chauhan
I was just trying out a programme for learning classes in python

The prog below is showing an error which it should not show :

class Bag:
def __init__(self, x):
self.data = []

def add(self, x):
self.data.append(x)
def addtwice(self, x):
 self.add(x)
 self.add(x)
y = Bag(4)
print " Adding twice of %4.2f gives " % (y.addtwice())


Error is :

Traceback (most recent call last):
  File "Z:\learning Python\learn5.py", line 35, in 
print " Adding twice of %4.2f gives " % (y.addtwice())
TypeError: addtwice() takes exactly 2 arguments (1 given)

why the prog is having this error with self nd x as arguments ???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get each pair from a string.

2012-10-24 Thread Mark Lawrence

On 24/10/2012 10:32, wxjmfa...@gmail.com wrote:

Le mardi 23 octobre 2012 18:41:45 UTC+2, Ian a écrit :

On Tue, Oct 23, 2012 at 12:47 AM,   wrote:



In any case, have you been following the progress of issue 16061?

There is a patch for the str.replace regression that you identified,

which results in better performance across the board than 3.2.  So

although 3.3 is slower than 3.2 on this one microbenchmark, 3.4 will

not be.


Yes.
A workaround to solve one of the corner cases of something
which is wrong by design.

jmf



Do you get paid money for acting as a complete moron or do you do it for 
free?


--
Cheers.

Mark Lawrence.

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


Re: turn list of letters into an array of integers

2012-10-24 Thread Robert Kern

On 10/24/12 1:03 PM, 8 Dihedral wrote:


The list in python is a list of valid python objects.
For the number crunching part, please use  arrays in numarray and scipy.


Your bot's database is laughably out of date.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Kevin Holleran
On Wed, Oct 24, 2012 at 7:51 AM, Tim Golden  wrote:

> On 24/10/2012 12:40, Kevin Holleran wrote:> Here is the full traceback:
> >
> Could you confirm what version of Windows is running on the remote
> machine? Also, could you show the output of the following, please:
>
> 
> import wmi
>
> host = "" # pick one
>
> print wmi.WMI(host, namespace="default").StdRegProv
>
> 
>
>
> TJG
>

The machine I am testing from is Windows7, the machine I will run this from
is running an exe generated from py2exe and is running Windows 2008 server.

The machines I am targeting are all Windows 2003 Server machines, though I
am hoping to also expand and use this script on a number of Windows XP
devices (which shouldn't be a problem...)

Here is the output as you requested.  Again thanks for your time & help.  I
hate monopolizing one person's time so much


>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)]
on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wmi
>>> host=""#removed from ouput
>>> print wmi.WMI(host,namespace="default").StdRegProv
[Locale(1033), dynamic: ToInstance, provider("RegProv")]
class StdRegProv
{
[implemented, static] uint32 CreateKey([IN] uint32 hDefKey =
2147483650,
 [IN] string sSubKeyName);
[implemented, static] uint32 DeleteKey([IN] uint32 hDefKey =
2147483650,
 [IN] string sSubKeyName);
[implemented, static] uint32 EnumKey([IN] uint32 hDefKey =
2147483650, [
IN] string sSubKeyName, [out] string sNames[]);
[implemented, static] uint32 EnumValues([IN] uint32 hDefKey =
2147483650
, [IN] string sSubKeyName, [out] string sNames[], [out] sint32 Types[]);
[implemented, static] uint32 DeleteValue([IN] uint32 hDefKey =
214748365
0, [IN] string sSubKeyName, [in] string sValueName);
[implemented, static] uint32 SetDWORDValue([IN] uint32 hDefKey =
2147483
650, [IN] string sSubKeyName, [in] string sValueName, [in] uint32 uValue =
3);
[implemented, static] uint32 GetDWORDValue([IN] uint32 hDefKey =
2147483
650, [IN] string sSubKeyName, [in] string sValueName, [out] uint32 uValue);
[implemented, static] uint32 SetStringValue([IN] uint32 hDefKey =
214748
3650, [IN] string sSubKeyName, [in] string sValueName, [in] string sValue =
"hel
lo");
[implemented, static] uint32 GetStringValue([IN] uint32 hDefKey =
214748
3650, [IN] string sSubKeyName, [in] string sValueName, [out] string sValue);
[implemented, static] uint32 SetMultiStringValue([IN] uint32
hDefKey = 2
147483650, [IN] string sSubKeyName, [in] string sValueName, [in] string
sValue[]
 = {"hello", "there"});
[implemented, static] uint32 GetMultiStringValue([IN] uint32
hDefKey = 2
147483650, [IN] string sSubKeyName, [in] string sValueName, [out] string
sValue[
]);
[implemented, static] uint32 SetExpandedStringValue([IN] uint32
hDefKey
= 2147483650, [IN] string sSubKeyName, [in] string sValueName, [in] string
sValu
e = "%path%");
[implemented, static] uint32 GetExpandedStringValue([IN] uint32
hDefKey
= 2147483650, [IN] string sSubKeyName, [in] string sValueName, [out] string
sVal
ue);
[implemented, static] uint32 SetBinaryValue([IN] uint32 hDefKey =
214748
3650, [IN] string sSubKeyName, [in] string sValueName, [in] uint8 uValue[]
= {1,
 2});
[implemented, static] uint32 GetBinaryValue([IN] uint32 hDefKey =
214748
3650, [IN] string sSubKeyName, [in] string sValueName, [out] uint8
uValue[]);
[implemented, static] uint32 CheckAccess([IN] uint32 hDefKey =
214748365
0, [IN] string sSubKeyName, [in] uint32 uRequired = 3, [out] boolean
bGranted);
};

>>> registry=wmi.WMI(host,namespace="default").StdRegProv
>>> import _winreg as winreg
>>> HKLM = winreg.HKEY_LOCAL_MACHINE
>>> NICs =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-
08002bE10318}"
>>> _, names=registry.EnumKey(HKLM,NICs)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
handle_com_error ()
  File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: 
>>> print registry.EnumKey(HKLM,NICs)


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


Re: classes

2012-10-24 Thread Mark Lawrence

On 24/10/2012 13:11, inshu chauhan wrote:

I was just trying out a programme for learning classes in python

The prog below is showing an error which it should not show :

class Bag:
 def __init__(self, x):
 self.data = []


You do nothing with x here.



 def add(self, x):
 self.data.append(x)
 def addtwice(self, x):
  self.add(x)
  self.add(x)
y = Bag(4)


Create y with an argument of 4 which is discarded in the initialiser.


print " Adding twice of %4.2f gives " % (y.addtwice())


There's no argument passed to addtwice here.




Error is :

Traceback (most recent call last):
   File "Z:\learning Python\learn5.py", line 35, in 
 print " Adding twice of %4.2f gives " % (y.addtwice())
TypeError: addtwice() takes exactly 2 arguments (1 given)


Exactly what I'd expect to happen.  What did you expect?



why the prog is having this error with self nd x as arguments ???


What x argument?  Clearly wrong as I've pointed out above.







--
Cheers.

Mark Lawrence.

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


Re: classes

2012-10-24 Thread Chris Angelico
On Wed, Oct 24, 2012 at 11:11 PM, inshu chauhan  wrote:
> print " Adding twice of %4.2f gives " % (y.addtwice())
>
>
> Error is :
>
> Traceback (most recent call last):
>   File "Z:\learning Python\learn5.py", line 35, in 
> print " Adding twice of %4.2f gives " % (y.addtwice())
> TypeError: addtwice() takes exactly 2 arguments (1 given)
>
> why the prog is having this error with self nd x as arguments ???

The first argument is 'self', which is being passed y, but you also
need to pass it a value to add twice. You're ignoring the argument to
__init__ so I don't know what your purpose is, but possibly you should
be saving that into self somewhere??

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


Re: classes

2012-10-24 Thread inshu chauhan
I was just trying out a programme for learning classes in python
>>
>> The prog below is showing an error which it should not show :
>>
>> class Bag:
>>  def __init__(self, x):
>>  self.data = []
>>
>
> You do nothing with x here. Right so x shouldnot be in the argument.
>
Fine

*class Bag:
>  def __init__(self):
>  self.data = []*
>
>
>>  def add(self, x):
>>  self.data.append(x)
>>  def addtwice(self, x):
>>   self.add(x)
>>   self.add(x)
>> y = Bag(4)
>>
>
> Create y with an argument of 4 'which is discarded in the initialiser.'
> means ??
>
>
>  print " Adding twice of %4.2f gives " % (y.addtwice())
>>
>
> There's no argument passed to addtwice here. ' why am I not passing y to
> addtwice here ??
>
>
>
>>
>> Error is :
>>
>> Traceback (most recent call last):
>>File "Z:\learning Python\learn5.py", line 35, in 
>>  print " Adding twice of %4.2f gives " % (y.addtwice())
>> TypeError: addtwice() takes exactly 2 arguments (1 given)
>>
>
> Exactly what I'd expect to happen.  What did you expect? I am learning
> 
>
>
>
>> why the prog is having this error with self nd x as arguments ???
>>
>
> What x argument?  Clearly wrong as I've pointed out above. How can i
> correct it ??
>
>
>>
>>
>>
> --
> Cheers.
>
> Mark Lawrence.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread Dave Angel
On 10/24/2012 08:11 AM, inshu chauhan wrote:
> I was just trying out a programme for learning classes in python
>
> The prog below is showing an error which it should not show :
>
> class Bag:
> def __init__(self, x):
> self.data = []
>
> def add(self, x):
> self.data.append(x)
> def addtwice(self, x):
>  self.add(x)
>  self.add(x)
> y = Bag(4)
> print " Adding twice of %4.2f gives " % (y.addtwice())
>
Perhaps you're confusing the two x parameters.  They are totally
independent local variables, and the value of one is not in any way
"remembered" for the other.  When you want to save things between
multiple methods of an object, then store them in the object, perhaps in
self.data  As it stands, the __init__ does not save the x at all, so the
4 that's passed into the initializer is thrown away.

You call addtwice(), but don't supply any value to add.  y serves as the
self value, but you have no x value.  What value did you intend to add?

You'll have another problem, in that addtwice() doesn't return any value
(so it returns None).  Therefore the print isn't going to work.  Please
separate the print from the calculations, and the problems will be lots
easier to figure out.  The wording of the string implies it's going to
display two values, but the only value given it is None.

> Error is :
>
> Traceback (most recent call last):
>   File "Z:\learning Python\learn5.py", line 35, in 
> print " Adding twice of %4.2f gives " % (y.addtwice())
> TypeError: addtwice() takes exactly 2 arguments (1 given)
>
> why the prog is having this error with self nd x as arguments ???
>
>
self and x are parameters.  You don't pass an argument for x to be bound to.




-- 

DaveA

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


resume execution after catching with an excepthook?

2012-10-24 Thread andrea crotti
So I would like to be able to ask for confirmation when I receive a C-c,
and continue if the answer is "N/n".

I'm already using an exception handler set with sys.excepthook, but I
can't make it work with the confirm_exit, because it's going to quit in
any case..

A possible solution would be to do a global "try/except
KeyboardInterrupt", but since I already have an excepthook I wanted to
use this.  Any way to make it continue where it was running after the
exception is handled?


def confirm_exit():
while True:
q = raw_input("This will quit the program, are you sure? [y/N]")
if q in ('y', 'Y'):
sys.exit(0)
elif q in ('n', 'N'):
print("Continuing execution")
# just go back to normal execution, is it possible??
break


def _exception_handler(etype, value, tb):
if etype == KeyboardInterrupt:
confirm_exit()
else:
sys.exit(1)


def set_exception_handler():
sys.excepthook = _exception_handler
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread inshu chauhan
I changed the programme to this :
class Bag:
def __init__(self):
self.data = []

def add(self, x):
self.data.append(x)
def addtwice(self, x):
 self.add(x)
 self.add(x)
 return x
y = Bag()
print y.addtwice(4)

Now its not showing any error but result is same as the number passed for
adding twice 


On Wed, Oct 24, 2012 at 2:55 PM, Dave Angel  wrote:

> On 10/24/2012 08:11 AM, inshu chauhan wrote:
> > I was just trying out a programme for learning classes in python
> >
> > The prog below is showing an error which it should not show :
> >
> > class Bag:
> > def __init__(self, x):
> > self.data = []
> >
> > def add(self, x):
> > self.data.append(x)
> > def addtwice(self, x):
> >  self.add(x)
> >  self.add(x)
> > y = Bag(4)
> > print " Adding twice of %4.2f gives " % (y.addtwice())
> >
> Perhaps you're confusing the two x parameters.  They are totally
> independent local variables, and the value of one is not in any way
> "remembered" for the other.  When you want to save things between
> multiple methods of an object, then store them in the object, perhaps in
> self.data  As it stands, the __init__ does not save the x at all, so the
> 4 that's passed into the initializer is thrown away.
>
> You call addtwice(), but don't supply any value to add.  y serves as the
> self value, but you have no x value.  What value did you intend to add?
>
> You'll have another problem, in that addtwice() doesn't return any value
> (so it returns None).  Therefore the print isn't going to work.  Please
> separate the print from the calculations, and the problems will be lots
> easier to figure out.  The wording of the string implies it's going to
> display two values, but the only value given it is None.
>
> > Error is :
> >
> > Traceback (most recent call last):
> >   File "Z:\learning Python\learn5.py", line 35, in 
> > print " Adding twice of %4.2f gives " % (y.addtwice())
> > TypeError: addtwice() takes exactly 2 arguments (1 given)
> >
> > why the prog is having this error with self nd x as arguments ???
> >
> >
> self and x are parameters.  You don't pass an argument for x to be bound
> to.
>
>
>
>
> --
>
> DaveA
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread Mark Lawrence

On 24/10/2012 13:48, inshu chauhan wrote:

I was just trying out a programme for learning classes in python


The prog below is showing an error which it should not show :

class Bag:
  def __init__(self, x):
  self.data = []



You do nothing with x here. Right so x shouldnot be in the argument.


Fine

*class Bag:

  def __init__(self):
  self.data = []*



  def add(self, x):
  self.data.append(x)
  def addtwice(self, x):
   self.add(x)
   self.add(x)


No return given so it will default to returning None, but...


y = Bag(4)



Create y with an argument of 4 'which is discarded in the initialiser.'
means ??


  print " Adding twice of %4.2f gives " % (y.addtwice())


...you seem to be expecting addtwice to be returning a Python float. 
Only you can tell us why as I've not yet gone to first year mind reading 
classes :)






There's no argument passed to addtwice here. ' why am I not passing y to
addtwice here ??


You are passing y, it's called self.  Why aren't you passing x?







Error is :

Traceback (most recent call last):
File "Z:\learning Python\learn5.py", line 35, in 
  print " Adding twice of %4.2f gives " % (y.addtwice())
TypeError: addtwice() takes exactly 2 arguments (1 given)



Exactly what I'd expect to happen.  What did you expect? I am learning





why the prog is having this error with self nd x as arguments ???



What x argument?  Clearly wrong as I've pointed out above. How can i
correct it ??


Put whatever it is you want appended to self.data in the call to 
y.addtwice.  And/or get addtwice to return the correct data type. 
And/or correct anything that I've missed like I did the first time around.





--
Cheers.

Mark Lawrence.

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





--
Cheers.

Mark Lawrence.

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 13:36, Kevin Holleran wrote:
> Here is the output as you requested.  Again thanks for your time & help.
>  I hate monopolizing one person's time so much

Heh. Everyone else is welcome to chip in :)

Ok, try specifying the parameter names. (I remember someone having
problems before caused by mismatched param order):


import wmi
import _winreg as winreg

host = "..."
HKLM = winreg.HKEY_LOCAL_MACHINE
NICs =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"

registry = wmi.WMI(host, namespace="default").StdRegProv
registry.EnumKey(hDefKey=HKLM, sSubKeyName=NICs)



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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Kevin Holleran
On Wed, Oct 24, 2012 at 9:11 AM, Tim Golden  wrote:

> On 24/10/2012 13:36, Kevin Holleran wrote:
> > Here is the output as you requested.  Again thanks for your time & help.
> >  I hate monopolizing one person's time so much
>
> Heh. Everyone else is welcome to chip in :)
>
> Ok, try specifying the parameter names. (I remember someone having
> problems before caused by mismatched param order):
>
>
>
OK, tried that as well as specifying the parameters directly instead of
through variables, with the same result  I even looked back through my
old scripts but I have not used the WMI module in this way... only to do
WMI queries...

Strange

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


Re: PIL and requests don't get along

2012-10-24 Thread Roy Smith
In article ,
 Kushal Kumaran  wrote:

> On 23 Oct 2012 14:06:59 -0400, r...@panix.com (Roy Smith) wrote:
> > I have a url from which I can get an image.  I want to use PIL to
> > manipulate that image.  Getting the image is easy:
> > 
> > >>> import requests
> > >>> r = requests.get(url)
> > 
> > There's a bunch of factory functions for Image, but none of them seem
> > to take anything that requests is willing to give you.  Image.new()
> > requires that you pass it the image size.  Image.open() takes a file
> > object, but
> > 
> > >>> Image.open(r.raw)
> > 
> > doesn't work because r.raw gives you a socket which doesn't support
> > seek().  I end up doing:
> > 
> > >>> r = requests.get(url)
> > >>> data = cStringIO.StringIO(r.content)
> > >>> image = Image.open(data)
> > 
> > which works, but it's gross.  Is there something I'm missing here?
> 
> That is pretty much what the requests module documentation says here:
> 
> http://docs.python-requests.org/en/latest/user/quickstart/#binary-response-con
> tent

Heh, I hadn't even noticed that.  I guess this is as good as it gets.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast forward-backward (write-read)

2012-10-24 Thread Emile van Sebille

On 10/23/2012 4:35 PM, emile wrote:


So, let's see, at that point in time (building backward) you've got
probably somewhere close to 400-500Gb in memory.

My guess -- probably not so fast.  Thrashing is sure to be a factor on
all but machines I'll never have a chance to work on.



I went looking for a machine capable of this and got about halfway there 
with http://www.tech-news.com/publib/pl2818.html which allows up to 
248Gb memory -- near as I can tell the price for the maxed out system is 
$2,546,200. Plus $3k/mo maintenance. Memory's still not quite enough, 
but I'll bet it's fast.  And a lot more reasonable at $1000 per Gb of 
memory particularly when contrasted to the $1000 I paid for a single Mb 
of memory back in 1984 or thereabouts.


Emile


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


Re: classes

2012-10-24 Thread Chris Angelico
On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan  wrote:
> I changed the programme to this :
> def addtwice(self, x):
>  self.add(x)
>  self.add(x)
>  return x
> y = Bag()
> print y.addtwice(4)
>
> Now its not showing any error but result is same as the number passed for
> adding twice 

Do you understand how function calls work? A function like
"y.addtwice" is called with an argument of 4, and the return value
from the function is the value of the expression.

some_value = y.addtwice(4)
print some_value

Take the return value and put it in the place where the function call
was. In this case, the return value is x, the number you passed in as
an argument.

What exactly do you expect addtwice to return? Should it return the
bag object (self)? Should it return True to say that it's been added
successfully (or False if there's an error)? Should it return the
number of items in the bag? Should it return 0 for success and a
nonzero error code for failure? Should it always return None, throwing
an exception if anything goes wrong? All of these make sense, you just
have to choose which one you want.

(I'm handwaving away a lot of complexity here, like un/bound methods
and member lookups. Bear with me. I'm also ignoring the fact that some
things just aren't Pythonic. The bear isn't complaining about that, so
nor should you.)

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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Grant Edwards
On 2012-10-23, Steven D'Aprano  wrote:

> I would be very surprised if the poster will be able to fit 100
> gigabytes of data into even a single list comprehension, let alone
> two.
>
> This is a classic example of why the old external processing
> algorithms of the 1960s and 70s will never be obsolete. No matter how
> much memory you have, there will always be times when you want to
> process more data than you can fit into memory.

Too true.  One of the projects I did in grad school about 20 years ago
was a plugin for some fancy data visualization software (I think it
was DX: http://www.research.ibm.com/dx/). My plugin would subsample
"on the fly" a selected section of a huge 2D array of data in a file.
IBM and SGI had all sorts of widgets you could use to sample,
transform and visualize data, but they all assumed that the input data
would fit into virtual memory.

-- 
Grant Edwards   grant.b.edwardsYow! I Know A Joke!!
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread Chris Angelico
On Thu, Oct 25, 2012 at 1:02 AM, inshu chauhan  wrote:
> ok.. This was an example i was trying to run from
> http://docs.python.org/tutorial/classes.html   ...

I would strongly recommend going back a bit in the tutorial and
reading about functions:

http://docs.python.org/tutorial/controlflow.html#defining-functions

And unless you have a particular need for Python 2, consider learning
the current Python version instead:

http://docs.python.org/py3k/tutorial/index.html

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


Re: classes

2012-10-24 Thread inshu chauhan
ok.. This was an example i was trying to run from
http://docs.python.org/tutorial/classes.html   ...

On Wed, Oct 24, 2012 at 3:48 PM, Chris Angelico  wrote:

> On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan 
> wrote:
> > I changed the programme to this :
> > def addtwice(self, x):
> >  self.add(x)
> >  self.add(x)
> >  return x
> > y = Bag()
> > print y.addtwice(4)
> >
> > Now its not showing any error but result is same as the number passed for
> > adding twice 
>
> Do you understand how function calls work? A function like
> "y.addtwice" is called with an argument of 4, and the return value
> from the function is the value of the expression.
>
> some_value = y.addtwice(4)
> print some_value
>
> Take the return value and put it in the place where the function call
> was. In this case, the return value is x, the number you passed in as
> an argument.
>
> What exactly do you expect addtwice to return? Should it return the
> bag object (self)? Should it return True to say that it's been added
> successfully (or False if there's an error)? Should it return the
> number of items in the bag? Should it return 0 for success and a
> nonzero error code for failure? Should it always return None, throwing
> an exception if anything goes wrong? All of these make sense, you just
> have to choose which one you want.
>
> (I'm handwaving away a lot of complexity here, like un/bound methods
> and member lookups. Bear with me. I'm also ignoring the fact that some
> things just aren't Pythonic. The bear isn't complaining about that, so
> nor should you.)
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 14:26, Kevin Holleran wrote:
> On Wed, Oct 24, 2012 at 9:11 AM, Tim Golden  Ok, try specifying the parameter names. (I remember someone having
> problems before caused by mismatched param order):
> 
> 
> 
> OK, tried that as well as specifying the parameters directly instead of
> through variables, with the same result  I even looked back through
> my old scripts but I have not used the WMI module in this way... only to
> do WMI queries...

Couple of things:

Does the same thing happen if you run against your own machine?

What's the result of the code below (which is basically "raw" WMI,
without the wmi module):


import _winreg as winreg

import win32com.client

HKLM = winreg.HKEY_LOCAL_MACHINE
NICs =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"

registry = win32com.client.GetObject("winmgmts:/root/default:StdRegProv")
EnumKey = registry.Methods_("EnumKey")
params = EnumKey.InParameters.SpawnInstance_()
params.Properties_.Item("hDefKey").Value = HKLM
params.Properties_.Item("sSubKeyName").Value = NICs

OutParameters = registry.ExecMethod_("EnumKey", params)
print OutParameters.Properties_.Item("ReturnValue")
print OutParameters.Properties_.Item("sNames")




TJG

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread David M Chess
> d...@davea.name
>
> On 10/23/2012 11:23 AM, David M Chess wrote:
> > We have a TimedRotatingFileHandler with when='midnight'
>
> You give us no clue what's in this class, or how it comes up with the
> filenames used.

Sorry if I was unclear.  This isn't my own subclass of 
TimedRotatingFileHandler or anything, this is the bog-standard 
logging.handlers.TimedRotatingFileHandler I'm talking about.

So all clues about what's in the class, and how it comes up with the 
filenames used, is available at

http://docs.python.org/library/logging.handlers.html#timedrotatingfilehandler 


:)

The specific Python version involved here is Python 2.6.6 (r266:84297, Aug 
24 2010, 18:46:32), to the extent that that matters...

>> This works great, splitting the log information across files by date, 
as 
>> long as the process is actually up at midnight.
>>
>> But now the users have noticed that if the process isn't up at 
midnight, 
>> they can end up with lines from two (or I guess potentially more) dates 
in 
>> the same log file.
>>
>> Is there some way to fix this, either with cleverer arguments into the 
>> TimedRotatingFileHandler, or by some plausible subclassing of it or its 

>> superclass?

Tx,
DC

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


Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
I am having a problem,

I have a class which return me a list. I want to append another list using
this list outside the Class.
can i do it ? if yes how ?
And can i use Userlist function for this ??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list using list obtained from a class

2012-10-24 Thread Demian Brecht

On 2012-10-24, at 7:37 AM, inshu chauhan  wrote:
> I am having a problem, 
> 
> I have a class which return me a list. I want to append another list using 
> this list outside the Class. 
> can i do it ? if yes how ? 
> And can i use Userlist function for this ??
> -- 
> http://mail.python.org/mailman/listinfo/python-list


If I understand your question correctly, have you read 
http://docs.python.org/tutorial/datastructures.html? 

list_a + list_b = list_c
list_a.extend(list_b)

are two methods to concatenate two lists.

UserList is only there for backwards compatibility (see the docs: 
http://docs.python.org/release/2.3/lib/module-UserList.html)

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
yes this i know very well.. but the problem is not so simple...

the class is returning the list , and i want to append the list outside the
class... main problem is this

I know how to append a list with a list nd stuff..

On Wed, Oct 24, 2012 at 4:41 PM, Demian Brecht wrote:

>
> On 2012-10-24, at 7:37 AM, inshu chauhan  wrote:
> > I am having a problem,
> >
> > I have a class which return me a list. I want to append another list
> using this list outside the Class.
> > can i do it ? if yes how ?
> > And can i use Userlist function for this ??
> > --
> > http://mail.python.org/mailman/listinfo/python-list
>
>
> If I understand your question correctly, have you read
> http://docs.python.org/tutorial/datastructures.html?
>
> list_a + list_b = list_c
> list_a.extend(list_b)
>
> are two methods to concatenate two lists.
>
> UserList is only there for backwards compatibility (see the docs:
> http://docs.python.org/release/2.3/lib/module-UserList.html)
>
> Demian Brecht
> @demianbrecht
> http://demianbrecht.github.com
>
>
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list using list obtained from a class

2012-10-24 Thread Demian Brecht

On 2012-10-24, at 7:45 AM, inshu chauhan  wrote:

> the class is returning the list , and i want to append the list outside the 
> class... main problem is this

What do you mean "the class is returning the list"? Do you mean that a class 
stores a list internally and you want to be able to mutate that list? Do you 
mean that you have a class method that returns a list? How does your particular 
use case differ from simply appending one list to another?


> I know how to append a list with a list nd stuff..

Without further clarification, this seems to be exactly what you're asking how 
to do..


Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
> the class is returning the list , and i want to append the list outside
the class... main problem is this

>
> What do you mean "the class is returning the list"? Do you mean that a
> class stores a list internally and you want to be able to mutate that list?
> Do you mean that you have a class method that returns a list? How does your
> particular use case differ from simply appending one list to another?
>

Yes, a Class method returns a list. I am trying to append this in main() to
make another list.
But the list i am getting after appending i showing addresses like this
'<__main__.Point object at 0x0254FAB0>' but if i print the same in the same
loop its showing me numbers which i want. Why I dont know ??



>
>
> > I know how to append a list with a list nd stuff..
>
> Without further clarification, this seems to be exactly what you're asking
> how to do.. yeah may i make misunderstanding
>
>
> Demian Brecht
> @demianbrecht
> http://demianbrecht.github.com
>
>
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list using list obtained from a class

2012-10-24 Thread Demian Brecht
On 2012-10-24, at 8:00 AM, inshu chauhan  wrote:

> Yes, a Class method returns a list. I am trying to append this in main() to 
> make another list. 
> But the list i am getting after appending i showing addresses like this 
> '<__main__.Point object at 0x0254FAB0>' but if i print the same in the same 
> loop its showing me numbers which i want. Why I dont know ??

If you can, please post the relevant blocks of code. That'll be a tremendous 
help in figuring out your problem.

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Paul Rubin
Emile van Sebille  writes:
>> probably somewhere close to 400-500Gb in memory
> I went looking for a machine capable of this and got about halfway
> there with http://www.tech-news.com/publib/pl2818.html which allows up
> to 248Gb memory -- near as I can tell the price for the maxed out
> system is $2,546,200. Plus $3k/mo maintenance. 

1x http://www.newegg.com/Product/Product.aspx?Item=N82E16816101317
@ $1400

+ 8x http://www.newegg.com/Product/Product.aspx?Item=N82E16820239276
@ $658

+ 4x http://www.newegg.com/Product/Product.aspx?Item=N82E16819113036
@ $520

+ 2x 

= $8744 for a 64-core box with 512GB of ram.  Add another $1000 or so
for disks/SSD's depending on configuration.  Not bad.  There were times
I coulda used something like this.

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


Re: classes

2012-10-24 Thread Zero Piraeus
:

On 24 October 2012 09:02, inshu chauhan  wrote:
> I changed the programme to this :
> class Bag:
> def __init__(self):
>
> self.data = []
>
> def add(self, x):
> self.data.append(x)
> def addtwice(self, x):
>  self.add(x)
>  self.add(x)
>  return x
> y = Bag()
> print y.addtwice(4)
>
> Now its not showing any error but result is same as the number passed for
> adding twice 

That's because, although Bag.addtwice() is appending x to self.data a
couple of times, it isn't changing x - so

  return x

will just give back what you supplied as an argument. If you'd written

  return self.data

instead [or just done 'print y.data' after calling addtwice()], you'd
see that something has in fact happened to y.

By the way ... while Bag.addtwice() is legal Python [and I understand
that you're just playing around here], a method that both "does
something" [changes the object] and "gives something" [returns a
useful value] when that's not strictly necessary isn't brilliant
style.

Consider a couple of methods on built-in Python types:

>>> a = [4, 1, 3, 2]
>>> a.sort() # doesn't return anything useful, but ...
>>> a# ... changes 'a':
[1, 2, 3, 4]
>>> b = "one, two, three, four"
>>> b.title()# returns something useful ...
'One, Two, Three, Four'
>>> b# ... but doesn't change 'b':
'one, two, three, four'
>>>

A good rule for methods is "do one thing well". Sometimes doing that
one thing will necessarily mean both changing the object and returning
something - as in the pop() method on lists, for example - but where
possible, it's better to stick to one of "doing" or "giving".

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


Re: Fast forward-backward (write-read)

2012-10-24 Thread rusi
On Oct 23, 7:52 pm, Virgil Stokes  wrote:
> I am working with some rather large data files (>100GB) that contain time 
> series
> data. The data (t_k,y(t_k)), k = 0,1,...,N are stored in ASCII format. I 
> perform
> various types of processing on these data (e.g. moving median, moving average,
> and Kalman-filter, Kalman-smoother) in a sequential manner and only a small
> number of these data need be stored in RAM when being processed. When 
> performing
> Kalman-filtering (forward in time pass, k = 0,1,...,N) I need to save to an
> external file several variables (e.g. 11*32 bytes) for each (t_k, y(t_k)). 
> These
> are inputs to the Kalman-smoother (backward in time pass, k = N,N-1,...,0).
> Thus, I will need to input these variables saved to an external file from the
> forward pass, in reverse order --- from last written to first written.
>
> Finally, to my question --- What is a fast way to write these variables to an
> external file and then read them in backwards?

Have you tried gdbm/bsddbm? They are meant for such (I believe).
Probably needs to be installed for windows; works for linux.
If I were you I'd try out with the giant data on linux and see if the
problem is solved, then see how to install for windows
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread Chris Angelico
On Thu, Oct 25, 2012 at 2:14 AM, Zero Piraeus  wrote:
> By the way ... while Bag.addtwice() is legal Python [and I understand
> that you're just playing around here], a method that both "does
> something" [changes the object] and "gives something" [returns a
> useful value] when that's not strictly necessary isn't brilliant
> style.

Side point: It's not that it's *bad* code or even bad style; it just
isn't the way Python generally does things.

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread Vinay Sajip
David M Chess  us.ibm.com> writes:

> >> But now the users have noticed that if the process isn't up at
> midnight, 
> >> they can end up with lines from two (or I guess potentially more)
> dates in 
> >> the same log file.
> >>
> >> Is there some way to fix this, either with cleverer arguments
> into the 
> >> TimedRotatingFileHandler, or by some plausible subclassing of
> it or its 
> >> superclass?

Well, of course you can subclass and override it to do what you want - there's
no magic there. The behaviour is as you would expect: the default behaviour of
a TimedRotatingFileHandler is to append, and roll over at midnight. So if your
program isn't running at midnight, it won't rotate:

Day 1. Run your program, stop it before midnight. The log file contains dates
from this day. No rotation occurred.

Day 2. Run your program again, stop it before midnight. The log file contains
dates from this day, and Day 1. No rotation occurred.

That's the symptom you're seeing, right?

You could do as Dave Angel suggested - just use a FileHandler with the name
derived from the date. If you don't want to or can't actually rotate files at
midnight, you're using the wrong tool for the job :-)

If you sometimes want to rotate at midnight (the process is running at that
time) and at other times not (the process isn't running then), you might have
to code startup logic in your program to deal with the vagaries of your
environment, since only you would know what they are :-)

Work is afoot to make the actual rollover time configurable (i.e. not forced
to be literally midnight) - see http://bugs.python.org/issue9556 - but that's
an enhancement request, not a bug, and so it'll see the light of day in Python
3.4, if at all. An implementation is in my sandbox repo at

http://hg.python.org/sandbox/vsajip

in branch fix9556. If all you need to do is rollover at a different time daily
(say 7 a.m.), you might be able to use this. Feel free to use that code as
inspiration for your subclass.

Regards,

Vinay Sajip

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


Re: turn list of letters into an array of integers

2012-10-24 Thread Terry Reedy

On 10/24/2012 1:23 AM, seektime wrote:

Here's some example code. The input is a list which is a "matrix" of letters:
a  b  a
b  b  a

and I'd like to turn this into a Python array:

   1 2 1
   2 2 1

so 1 replaces a, and 2 replaces b.


If you are going to replace single characters (letters) with single 
characters (digits), use maketrans and translate.


>>> 'a b c'.translate(str.maketrans('abc', '123'))
'1 2 3'

--
Terry Jan Reedy

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


Re: classes

2012-10-24 Thread Terry Reedy

On 10/24/2012 8:11 AM, inshu chauhan wrote:

I was just trying out a program for learning classes in python


I think you should work through or reread the Tutorial, especially the 
chapters on functions and classes.


You will also be slightly better off with Python 3, which only has one 
category of classes rather than two.


--
Terry Jan Reedy

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


Re: classes

2012-10-24 Thread Zero Piraeus
:

On 24 October 2012 11:18, Chris Angelico  wrote:
> On Thu, Oct 25, 2012 at 2:14 AM, Zero Piraeus  wrote:
>> [... on return values and side effects ...]
>
> Side point: It's not that it's *bad* code or even bad style;

It's a matter of taste, sure, but especially for a beginner, it's easy
to find yourself using a method for its return value and forgetting
that it actually does something to the object as well ... and then
getting bitten later.

> it just isn't the way Python generally does things.

When in Rome ... :-)

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


Re: classes

2012-10-24 Thread Chris Angelico
On Thu, Oct 25, 2012 at 3:10 AM, Zero Piraeus  wrote:
> On 24 October 2012 11:18, Chris Angelico  wrote:
>> On Thu, Oct 25, 2012 at 2:14 AM, Zero Piraeus  wrote:
>>> [... on return values and side effects ...]
>>
>> Side point: It's not that it's *bad* code or even bad style;
>
> It's a matter of taste, sure, but especially for a beginner, it's easy
> to find yourself using a method for its return value and forgetting
> that it actually does something to the object as well ... and then
> getting bitten later.
>
>> it just isn't the way Python generally does things.
>
> When in Rome ... :-)

Of course, and I'm not disagreeing with the original point made. It's
worth writing Pythonic code when writing Python code. It's just not
the only way to write.

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread wrw

On Oct 24, 2012, at 10:22 AM, David M Chess  wrote:

> >> This works great, splitting the log information across files by date, as 
> >> long as the process is actually up at midnight.
> >>
> >> But now the users have noticed that if the process isn't up at midnight, 
> >> they can end up with lines from two (or I guess potentially more) dates in 
> >> the same log file.
> >>
> >> Is there some way to fix this, either with cleverer arguments into the 
> >> TimedRotatingFileHandler, or by some plausible subclassing of it or its 
> >> superclass? 
> 
> Tx, 
> DC 


(After a VERY brief scan of the logging cookbook and modules.)
I don't think you have any choice but brute force.  If the program
wasn't up at midnight, then somehow it got restarted. You will have
to add logic to the restart code (or to the program itself that gets
run as it initializes).

Something like:

Does a log file exist? -> No ->  First run; create log file & continue
  |
 Yes
  |
  Read backwards looking for date change, copy lines after change
  to new file, delete from old file.

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


Re: Getting a TimedRotatingFileHandler not to put two dates in the same file?

2012-10-24 Thread David M Chess
> w...@mac.com 

> Something like:

> Does a log file exist? -> No ->  First run; create log file & continue
>  |
> Yes
>  |
>  Read backwards looking for date change, copy lines after change
>  to new file, delete from old file.

Yep, I'm concluding that also.

It just wasn't clear to me from the documentation whether or not the 
existing TimedRotatingFileHandler had any "at startup, see if we missed 
any rollovers, and do them now if so" function, or if there was some known 
variant that does.  The answer, apparently, being "nope".  :)  Shouldn't 
be that hard to write, so that's probably what we'll do.

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


Re: turn list of letters into an array of integers

2012-10-24 Thread MRAB

On 2012-10-24 07:07, Chris Rebert wrote:

On Tue, Oct 23, 2012 at 10:23 PM, seektime  wrote:

Here's some example code. The input is a list which is a "matrix" of letters:
   a  b  a
   b  b  a

and I'd like to turn this into a Python array:


You mean a Python list. The datatype Python calls an `array` is very
different and relatively uncommonly used.
Although, confusingly, Python's lists are implemented using C arrays
rather than linked lists.


  1 2 1
  2 2 1

so 1 replaces a, and 2 replaces b. Here's the code I have so far:


L=['a b a\n','b b a\n']



seq

'1 2 1\n 2 2 1\n'

My question is how can I turn "seq" into a python array?


I'd say you're asking the wrong question. The better question is "Why
wasn't the result a list in the first place?". Many transformations
are cumbersome to express over just strings, which is why the first
job of most programs is to parse their input into a more convenient
structure that is suited to their main task(s).

This (along with some other improvements) leads to a better, somewhat
different program/algorithm:

letter2number = {'a': 1, 'b': 2}
with open("path/to/file.txt", "r") as f:
 result = [[letter2number[letter] for letter in line.strip().split()] for 
line in f]


If you're using .split() then you don't need to use .strip() as well:

result = [[letter2number[letter] for letter in line.split()] for 
line in f]



If it's safe to assume that the correspondence between the letters and
numbers isn't completely arbitrary, some further improvements are also
possible.

Some relevant docs:
http://docs.python.org/library/stdtypes.html#string-methods
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Cheers,
Chris

P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
it is worth noting for future reference that the readlines() method is
considered somewhat deprecated.



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


Re: turn list of letters into an array of integers

2012-10-24 Thread wxjmfauth
Le mercredi 24 octobre 2012 07:23:11 UTC+2, seektime a écrit :
> Here's some example code. The input is a list which is a "matrix" of letters:
> 
>a  b  a
> 
>b  b  a
> 
> 
> 
> and I'd like to turn this into a Python array:
> 
> 
> 
>   1 2 1
> 
>   2 2 1
> 
> 
> 
> so 1 replaces a, and 2 replaces b. Here's the code I have so far:
> 
> 
> 
> >>> L=['a b a\n','b b a\n']
> 
> >>> s=' '.join(L)
> 
> >>> seq1=('a','b')
> 
> >>> seq2=('1','2')
> 
> >>> d = dict(zip(seq1,seq2))
> 
> >>> # Define method to replace letters according to dictionary (got this from 
> >>> http://gomputor.wordpress.com/2008/09/27/search-replace-multiple-words-or-characters-with-python/).
> 
> ... def replace_all(text, dic):
> 
> ... for i, j in dic.iteritems():
> 
> ... text = text.replace(i, j)
> 
> ... return text
> 
> ... 
> 
> 
> 
> >>> seq = replace_all(s,d)
> 
> >>> print seq
> 
> 1 2 1
> 
>  2 2 1
> 
> 
> 
> >>> seq
> 
> '1 2 1\n 2 2 1\n'
> 
> 
> 
> My question is how can I turn "seq" into a python array?
> 
> 
> 
> Thanks
> 
> Michael

Not so sure what you mean by an "array of integers".

>>> def z(s):
... a = s.splitlines()
... b = [e.split() for e in a]
... for row in range(len(b)):
... for col in range(len(b[row])):
... b[row][col] = ord(b[row][col]) - ord('a')
... return b
... 
>>> z('a b a\n b b a')
[[0, 1, 0], [1, 1, 0]]
>>> 
>>> # or
>>> table = {'a': 111, 'b': 222}
>>> 
>>> def z2(s, table):
... a = s.splitlines()
... b = [e.split() for e in a]
... for row in range(len(b)):
... for col in range(len(b[row])):
... b[row][col] = table[b[row][col]]
... return b
... 
>>> z2('a b a\n b b a', table)
[[111, 222, 111], [222, 222, 111]]
>>> 
>>> # note
>>> z('a\n b b b b b\n a a')
[[0], [1, 1, 1, 1, 1], [0, 0]]

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


Re: turn list of letters into an array of integers

2012-10-24 Thread Demian Brecht

On 2012-10-24, at 10:27 AM, wxjmfa...@gmail.com wrote:
> Not so sure what you mean by an "array of integers".


I wasn't entirely sure about that either. I assumed given the subject that it 
was just a 1-D array and could then be accessed by arr[(y * width) + x].

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


A lock that prioritizes acquire()s?

2012-10-24 Thread David M Chess
Okay, next silly question.  :) 

We have a very simple multi-threaded system where a request comes in, 
starts running in a thread, and then (zero, one, or two times per request) 
gets to a serialization point, where the code does: 

with lock: 
  do_critical_section_stuff_that_might_take_awhile() 

and then continues. 

Which is almost the same as: 

lock.acquire() 
try: 
  do_critical_section_stuff_that_might_take_awhile() 
finally: 
  lock.release() 

Now we discover that It Would Be Nice if some requests got priority over 
others, as in something like: 

lock.acquire(importance=request.importance) 
try: 
  do_critical_section_stuff_that_might_take_awhile() 
finally: 
  lock.release() 

and when lock.release() occurs, the next thread that gets to run is one of 
the most important ones currently waiting in acquire() (that's the 
exciting new thing). 

Other requirements are that the code to do this be as simple as possible, 
and that it not mess anything else up.  :) 

My first thought was something like a new lock-ish class that would do 
roughly: 

class PriorityLock(object): 

def __init__(self): 
self._lock = threading.Lock() 
self._waiter_map = {}  # maps TIDs to importance
 
def acquire(self,importance=0): 
this_thread = threading.currentThread() 
self._waiter_map[this_thread] = importance # I want in 
while True: 
self._lock.acquire() 
if ( max( self._waiter_map.values())<=importance ): # we win 
del self._waiter_map[this_thread]  # not waiting anymore 
return # return with lock acquired 
self._lock.release()  # We are not most impt: release/retry
 
def release(self): 
self._lock.release() 

(Hope the mail doesn't garble that too badly.) 

Basically the acquire() method just immediately releases and tries again 
if it finds that someone more important is waiting. 

I think this is semantically correct, as long as the underlying lock 
implementation doesn't have starvation issues, and it's nice and simple, 
but on the other hand it looks eyerollingly inefficient. 

Seeking any thoughts on other/better ways to do this, or whether the 
inefficiency will be too eyerolling if we get say one request per second 
with an average service time a bit under a second but maximum service time 
well over a second, and most of them are importance zero, but every (many) 
seconds there will be one or two with higher importance. 

Tx, 
DC 

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


Listen for changes in variable (alsaaudio.Mixer(x,x).getvolume(x)

2012-10-24 Thread Muffinman
Hello all,

I'm new to Python (running 2.6.6 but if necessary 3.x should also be
fine). I have a little idea I hope to accomplish with Python. I want to
listen for changes in Alsa sound volume level and base some actions on
that. With the few lines below I can check the current volume level. Can
I extend this so that the script listens for changes in the volume level
and I can base some actions on it? As speed is quite important it's not
an option to poll every second or so for changes, it has to be close to
instantaneous.

If this is not possible with Python, any suggestions on what else are
also welcome of course.

Thanks in advance, Maarten


#
try
mixer = alsaaudio.Mixer(Fake, 0)
except alsaaudio.ALSAAudioError:
sys.stderr.write("No such mixer\n")
sys.exit(1)

volumes = mixer.getvolume(1)
#
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] The Python Papers Anthology IndexCopernicus Value for 2011

2012-10-24 Thread mauricel...@acm.org
Dear all

On behalf of the editorial committee of The Python Papers (overseeing The 
Python Papers, The Python Papers Monograph, and The Python Papers Source 
Codes), it is my pleasure to announce our first IndexCopernicus Value.

Title   ISSNINDEX COPERNICUS 2011
The Python Papers   1834-3147   5.09
The Python Papers Source Codes  1836-621X   5.09
The Python Papers Monograph 1837-7092   5.09


In terms of PDF views, for TPP, we have 89 articles in total (not including 
frontmatters and editorials). Total of 103,335 gallery views (of which, 103,035 
are PDF views), averaging 1161 views per article. The top view article 
"Implementation of Kalman Filter with Python Language", clocks up 8874 views.

For TPPM, we have 49 articles. Totaling 75,239 gallery views (of which, 73,536 
are PDF views). The top article "Introduction to Computer Vision in Python", 
clocks up 11,458 views.

For TPPSC, we have only 11 articles but even then, the total gallery views is 
17,259 (of which, 13,752 are PDF views).

Thank you everyone for your continued support.

Warm regards
Maurice Ling
Co-EIC, The Python Papers Anthology
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A lock that prioritizes acquire()s?

2012-10-24 Thread MRAB

On 2012-10-24 19:54, David M Chess wrote:


Okay, next silly question.  :)

We have a very simple multi-threaded system where a request comes in,
starts running in a thread, and then (zero, one, or two times per
request) gets to a serialization point, where the code does:

with lock:
  do_critical_section_stuff_that_might_take_awhile()

and then continues.

Which is almost the same as:

lock.acquire()
try:
  do_critical_section_stuff_that_might_take_awhile()
finally:
  lock.release()

Now we discover that It Would Be Nice if some requests got priority over
others, as in something like:

lock.acquire(importance=request.importance)
try:
  do_critical_section_stuff_that_might_take_awhile()
finally:
  lock.release()

and when lock.release() occurs, the next thread that gets to run is one
of the most important ones currently waiting in acquire() (that's the
exciting new thing).

Other requirements are that the code to do this be as simple as
possible, and that it not mess anything else up.  :)

My first thought was something like a new lock-ish class that would do
roughly:

class PriorityLock(object):

def __init__(self):
self._lock = threading.Lock()
self._waiter_map = {}  # maps TIDs to importance

def acquire(self,importance=0):
this_thread = threading.currentThread()
self._waiter_map[this_thread] = importance # I want in
while True:
self._lock.acquire()
if ( max( self._waiter_map.values())<=importance ): # we win
del self._waiter_map[this_thread]  # not waiting anymore
return # return with lock acquired
self._lock.release()  # We are not most impt: release/retry

def release(self):
self._lock.release()

(Hope the mail doesn't garble that too badly.)

Basically the acquire() method just immediately releases and tries again
if it finds that someone more important is waiting.

I think this is semantically correct, as long as the underlying lock
implementation doesn't have starvation issues, and it's nice and simple,
but on the other hand it looks eyerollingly inefficient.

Seeking any thoughts on other/better ways to do this, or whether the
inefficiency will be too eyerolling if we get say one request per second
with an average service time a bit under a second but maximum service
time well over a second, and most of them are importance zero, but every
(many) seconds there will be one or two with higher importance.


Here's my take on it:

class PriorityLock(object):

def __init__(self):
self._lock = threading.Lock()
self._waiter_queue = []
self._queue_lock = threading.Lock()

def acquire(self, importance=0):
this_thread = threading.currentThread()

# Add this thread to the queue
with self._queue_lock:
self._waiter_queue.append((importance, this_thread))
self._waiter_queue.sort(reverse=True, key=lambda pair: 
pair[0]) # Move the most important to the start.


# Acquire and retain the lock when this thread is at the start 
of the queue.

while True:
self._lock.acquire()

with self._queue_lock:
if self._waiter_queue[0][1] == this_thread: # We win.
del self._waiter_queue[0] # Not waiting anymore.
return # Return with lock acquired.

self._lock.release() # We are not most important: release 
and retry.

time.sleep(0.01) # Give the other threads a chance.

def release(self):
self._lock.release()

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


Re: A lock that prioritizes acquire()s?

2012-10-24 Thread Ian Kelly
On Wed, Oct 24, 2012 at 12:54 PM, David M Chess  wrote:
> Seeking any thoughts on other/better ways to do this, or whether the
> inefficiency will be too eyerolling if we get say one request per second
> with an average service time a bit under a second but maximum service time
> well over a second, and most of them are importance zero, but every (many)
> seconds there will be one or two with higher importance.

I used a PriorityQueue and Conditions to get rid of the ugly while True loop.


import threading
from Queue import PriorityQueue, Empty

class PriorityLock(object):

def __init__(self):
self._is_available = True
self._mutex = threading.Lock()
self._waiter_queue = PriorityQueue()

def acquire(self, priority=0):
self._mutex.acquire()
# First, just check the lock.
if self._is_available:
self._is_available = False
self._mutex.release()
return True
condition = threading.Condition()
condition.acquire()
self._waiter_queue.put((priority, condition))
self._mutex.release()
condition.wait()
condition.release()
return True

def release(self):
self._mutex.acquire()
# Notify the next thread in line, if any.
try:
_, condition = self._waiter_queue.get_nowait()
except Empty:
self._is_available = True
else:
condition.acquire()
condition.notify()
condition.release()
self._mutex.release()

def test():
import random, time

def thread(lock, priority):
lock.acquire(priority)
print("Thread %d running" % priority)
time.sleep(1)
lock.release()
lock = PriorityLock()
threads = [threading.Thread(target=thread, args=(lock, x)) for x
in range(10)]
random.shuffle(threads)
for thread in threads:
thread.start()
for thread in threads:
thread.join()

if __name__ == "__main__":
test()


Output:

Thread 9 running
Thread 0 running
Thread 1 running
Thread 2 running
Thread 3 running
Thread 4 running
Thread 5 running
Thread 6 running
Thread 7 running
Thread 8 running

Note that with the PriorityQueue, lower priority values are retrieved
first.  Thread 9 ran first just by virtue of being first to the gate,
and after that you can see that everything went in order.

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


while expression feature proposal

2012-10-24 Thread Dan Loewenherz
Hi all,

This is my first post to this group--I'm not subscribed, so please CC
me in responses.

So I'm sure a lot of you have run into the following pattern. I use it
all the time and it always has felt a bit awkward due to the duplicate
variable assignment.

VAR = EXPR
while VAR:
BLOCK
VAR = EXPR

I'm curious what the possibility of adding the following to Python as
syntactic sugar:

while EXPR as VAR:
BLOCK

Apologies if that has been proposed before. I searched the archives
and couldn't find any mention of it.

Best,
Dan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A lock that prioritizes acquire()s?

2012-10-24 Thread Ian Kelly
On Wed, Oct 24, 2012 at 2:19 PM, Ian Kelly  wrote:
> I used a PriorityQueue and Conditions to get rid of the ugly while True loop.

Same things, but with Events instead of Conditions.  This is just a
bit more readable.

The PriorityQueue is also probably unnecessary, since it's always
accessed with the mutex held.  A heapq would be fine.


import threading
import Queue

class PriorityLock(object):

def __init__(self):
self._is_available = True
self._mutex = threading.Lock()
self._waiter_queue = Queue.PriorityQueue()

def acquire(self, priority=0):
self._mutex.acquire()
# First, just check the lock.
if self._is_available:
self._is_available = False
self._mutex.release()
return True
event = threading.Event()
self._waiter_queue.put((priority, event))
self._mutex.release()
event.wait()
# When the event is triggered, we have the lock.
return True

def release(self):
self._mutex.acquire()
# Notify the next thread in line, if any.
try:
_, event = self._waiter_queue.get_nowait()
except Queue.Empty:
self._is_available = True
else:
event.set()
self._mutex.release()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Ian Kelly
On Wed, Oct 24, 2012 at 2:40 PM, Dan Loewenherz  wrote:
> So I'm sure a lot of you have run into the following pattern. I use it
> all the time and it always has felt a bit awkward due to the duplicate
> variable assignment.
>
> VAR = EXPR
> while VAR:
> BLOCK
> VAR = EXPR

The idiomatic way to do this is:

while True:
VAR = EXPR
if not VAR:
break
BLOCK
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Tim Chase
On 10/24/12 16:34, Ian Kelly wrote:
> On Wed, Oct 24, 2012 at 2:40 PM, Dan Loewenherz  wrote:
>> So I'm sure a lot of you have run into the following pattern. I use it
>> all the time and it always has felt a bit awkward due to the duplicate
>> variable assignment.
>>
>> VAR = EXPR
>> while VAR:
>> BLOCK
>> VAR = EXPR
> 
> The idiomatic way to do this is:
> 
> while True:
> VAR = EXPR
> if not VAR:
> break
> BLOCK

It may be idiomatic, but that doesn't stop it from being pretty
ugly.  I must say I really like the parity of Dan's

  while EXPR as VAR:
 BLOCK

proposal with the "with" statement.  It also doesn't fall prey to
the "mistaken-assignment vs. intentional-assignment" found in most
C-like languages.  I could see a pretty reasonable PEP coming from this.

-tkc






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


Re: A lock that prioritizes acquire()s?

2012-10-24 Thread David M Chess
Lovely, thanks for the ideas!  I remember considering having release() 
pick the next thread to notify, where all the waiters were sitting on 
separate Conditions or whatever; not sure why I didn't pursue it to the 
end.  Probably distracted by something shiny; or insufficient brainpower. 
:) DC
--

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


Re: while expression feature proposal

2012-10-24 Thread Paul Rubin
Dan Loewenherz  writes:
> VAR = EXPR
> while VAR:
> BLOCK
> VAR = EXPR

for VAR in iter(lambda: EXPR, None):
   BLOCK

where the termination sentinel might be False or '' or whatever instead
of None.  Of course if EXPR is a callable, there's no lambda.

> while EXPR as VAR:
> BLOCK

This is kind of nice.  I wonder if it could generalize "with" somehow,
i.e. use the context manager for EXPR if it has one.  Or maybe "iter"
could be generalized so you could pass an arbutrary predicate as
termination condition, instead of a single sentinel value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Cameron Simpson
On 24Oct2012 16:54, Tim Chase  wrote:
| On 10/24/12 16:34, Ian Kelly wrote:
| > On Wed, Oct 24, 2012 at 2:40 PM, Dan Loewenherz  
wrote:
| >> So I'm sure a lot of you have run into the following pattern. I use it
| >> all the time and it always has felt a bit awkward due to the duplicate
| >> variable assignment.
| >>
| >> VAR = EXPR
| >> while VAR:
| >> BLOCK
| >> VAR = EXPR
| > 
| > The idiomatic way to do this is:
| > 
| > while True:
| > VAR = EXPR
| > if not VAR:
| > break
| > BLOCK
| 
| It may be idiomatic, but that doesn't stop it from being pretty
| ugly.

Yes, but more flexible because it accomodates loops where the natural
place for the test is partway through the loop instead of right at the
top, which is quite common.

| I must say I really like the parity of Dan's
|   while EXPR as VAR:
|  BLOCK
| proposal with the "with" statement.

Well, it's nice. But usually EXPR will be a boolean. If you're inside
the loop you know it's true and don't need VAR. Of course, the glaring
counter example is things like regexp tests. I have a heap of code like
this:

  m = re_FUNKYPATTERN.match(test_string)
  if m:
do stuff with the results of the match, using "m"

If I could write this as:

  if re_FUNKYPATTERN.match(test_string) as m:
do stuff with the results of the match, using "m"

then some cascading parse decisions would feel a bit cleaner. Where I
current have this:

  m = re_CONSTRUCT1.match(line)
  if m:
... handle construct 1 ...
  else:
m = re_CONSTRUCT2.match(line)
if m:
  ... handle construct 2 ...
else:
  m = re_CONSTRUCT3.match(line)

I could have this:

  if re_CONSTRUCT1.match(line) as m:
... handle construct 1 ...
  elif re_CONSTRUCT2.match(line) as m:
... handle construct 2 ...
  elif re_CONSTRUCT3.match(line) as m:

which is both more concise and also doesn't step inward.

But I'm still -0 on it, because it supplants the glaringly obvious:

  m = ...

assignment with the far less in your face:

  possibly-long-expr as m

and I think it would get quite heavily used, to the detriment of
assignment readability in general. At present the nature of most effects
is at the left. An assignment is obvious on the left, an if/with/while/etc
is visible at the left.

With statements and except statements have concrete use cases for the
"as" part that aren't doable without it, but the while/if...as form
can always be written in the current convention.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Paul Rubin
Cameron Simpson  writes:
>   if re_FUNKYPATTERN.match(test_string) as m:
> do stuff with the results of the match, using "m"

  class memo:
 def __call__(f, *args, **kw):
self.result = f(*args, **kw)

  m = memo()

  if result(re_FUNKYPATTERN.match, test_string):
   do stuff with the results of the match,
   using "m.result"

then

  if re_CONSTRUCT1.match(line) as m:
... handle construct 1 ...
  elif re_CONSTRUCT2.match(line) as m:
... handle construct 2 ...
  elif re_CONSTRUCT3.match(line) as m:

becomes

   if m(re_CONSTRUCT1.match, line):
 .. handle construct 1 ...
   elif m(re_CONSTRUCT2.match, line):
 .. handle construct 2 ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Chris Angelico
On Thu, Oct 25, 2012 at 9:26 AM, Cameron Simpson  wrote:
> If I could write this as:
>
>   if re_FUNKYPATTERN.match(test_string) as m:
> do stuff with the results of the match, using "m"

Then you'd be right there with C-like languages where assignment is an
expression :)

while (tok = strtok(blah,blah)) ... do something with tok ...

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


Re: while expression feature proposal

2012-10-24 Thread Paul Rubin
Paul Rubin  writes:
>   class memo:
>  def __call__(f, *args, **kw):
> self.result = f(*args, **kw)

obviously add

  return self.result 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Tim Chase
On 10/24/12 17:26, Cameron Simpson wrote:
> On 24Oct2012 16:54, Tim Chase  wrote:
> | On 10/24/12 16:34, Ian Kelly wrote:
> | > The idiomatic way to do this is:
> | > 
> | > while True:
> | > VAR = EXPR
> | > if not VAR:
> | > break
> | > BLOCK
> | 
> | It may be idiomatic, but that doesn't stop it from being pretty
> | ugly.
> 
> Yes, but more flexible because it accomodates loops where the natural
> place for the test is partway through the loop instead of right at the
> top, which is quite common.

Just like the "with" doesn't stop you from using a try/finally block
to do something similar, you can still write mid-loop exits as code
currently exists.  The proposed syntax just makes coming from other
languages easier to translate.

> | I must say I really like the parity of Dan's
> |   while EXPR as VAR:
> |  BLOCK
> | proposal with the "with" statement.
> 
> Well, it's nice. But usually EXPR will be a boolean.

I think the most common use-cases are boolean'ish.  The falsehood
means stop, but the truthiness value is not merely True.  The common
use-case I hit is the

  f = file('foo.bin')
  while True:
data = f.read(CHUNK_SIZE)
if not data: break
process(data) #not just data=True

which tidily becomes

  while f.read(CHUNK_SIZE) as data:
process(data)

>   m = re_FUNKYPATTERN.match(test_string)
>   if m:
> do stuff with the results of the match, using "m"
> 
> If I could write this as:
> 
>   if re_FUNKYPATTERN.match(test_string) as m:
> do stuff with the results of the match, using "m"

I'm -0 on this.  I don't like the pattern, but I'm not sure the "if
CONDITION as FOO" syntax yields nearly the functional & readable
improvement of "while THING as FOO".

> then some cascading parse decisions would feel a bit cleaner. Where I
> current have this:
> 
>   m = re_CONSTRUCT1.match(line)
>   if m:
> ... handle construct 1 ...
>   else:
> m = re_CONSTRUCT2.match(line)
> if m:
>   ... handle construct 2 ...
> else:
>   m = re_CONSTRUCT3.match(line)

For those, I often end up putting the regexps in a list and
iterating accordingly (which I have done multiple times):

  regexes = [
re_one,
re_two,
re_three,
]
  for regex in regexes:
m = regex.match(line)
if m:
  handle(m)
  break
  else:
doh()

Granted, it does expect that you want to handle the results
uniformly, but this could (usually) be mitigated with a dispatch
function as well

  regex_dispatches = [
(re_one, handle1),
(re_two, handle2),
(re_three, handle3),
]
  for regex, fn in regex_dispatches:
m = regex.match(line):
if m:
  fn(m, extra_context)
  break
  else:
doh()


> But I'm still -0 on it, because it supplants the glaringly obvious:
> 
>   m = ...
> 
> assignment with the far less in your face:
> 
>   possibly-long-expr as m

If it were replacing standard assignment, I'd be as off-the-charts
-1 as possible.  Yech.  But when cued by the "while" and
indentation, it's not quite so bad.  It still feels yucky™ in an
"if", but I find the improvement in the "while" is certainly worthwhile.


> With statements and except statements have concrete use cases for the
> "as" part that aren't doable without it, but the while/if...as form
> can always be written in the current convention.

The syntax for the with *could* have been something like

  with foo = file("foo.txt"):
for line in foo:
  pass

but the "as" was chosen and I think it makes sense in the context.

-tkc

(To me, the "if CONDITION as VAR" feels much like the ternary
evaluation:

  x = foo if bar else baz

which made it into official syntax; I find the "while CONDITION as
VAR" much more readable/valuable in light of both)




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


Re: while expression feature proposal

2012-10-24 Thread Ian Kelly
On Wed, Oct 24, 2012 at 3:54 PM, Tim Chase
 wrote:
> It may be idiomatic, but that doesn't stop it from being pretty
> ugly.  I must say I really like the parity of Dan's
>
>   while EXPR as VAR:
>  BLOCK
>
> proposal with the "with" statement.  It also doesn't fall prey to
> the "mistaken-assignment vs. intentional-assignment" found in most
> C-like languages.  I could see a pretty reasonable PEP coming from this.

Often though the while test is not a simple boolean test of VAR.  For example:

j = int(random() * n)
while j in selected:
j = int(random() * n)

It also doesn't flow quite as naturally.  "with x as y" is
grammatically correct English.  "while x as y" is not, and I wonder
how easily it might be confused for "while x is y", which is valid
Python and means something completely different.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Cameron Simpson
On 25Oct2012 09:40, Chris Angelico  wrote:
| On Thu, Oct 25, 2012 at 9:26 AM, Cameron Simpson  wrote:
| > If I could write this as:
| >
| >   if re_FUNKYPATTERN.match(test_string) as m:
| > do stuff with the results of the match, using "m"
| 
| Then you'd be right there with C-like languages where assignment is an
| expression :)
| 
| while (tok = strtok(blah,blah)) ... do something with tok ...

Well, yes, that's the core convenience of the suggested syntax.
But without the =/== accident opportunity.
But with another assignment that doesn't look like an asisgnment syntax.

Like I said, I'm -0.
-- 
Cameron Simpson 

Be smart, be safe, be paranoid.
- Ryan Cousineau, cour...@compdyn.com DoD#863, KotRB, KotKWaWCRH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Cameron Simpson
On 24Oct2012 15:37, Paul Rubin  wrote:
| Cameron Simpson  writes:
| >   if re_FUNKYPATTERN.match(test_string) as m:
| > do stuff with the results of the match, using "m"
| 
|   class memo:
|  def __call__(f, *args, **kw):
| self.result = f(*args, **kw)
| 
|   m = memo()
|   if result(re_FUNKYPATTERN.match, test_string):
|do stuff with the results of the match,
|using "m.result"
| 
| then
| 
|   if re_CONSTRUCT1.match(line) as m:
| ... handle construct 1 ...
|   elif re_CONSTRUCT2.match(line) as m:
| ... handle construct 2 ...
|   elif re_CONSTRUCT3.match(line) as m:
| 
| becomes
| 
|if m(re_CONSTRUCT1.match, line):
|  .. handle construct 1 ...
|elif m(re_CONSTRUCT2.match, line):
|  .. handle construct 2 ...

Cute. Not sure I like it, but cute:-)
-- 
Cameron Simpson 

If you do not read the paper, you are uninformed. If you do read the
paper, you are misinformed. - Mark Twain
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Listen for changes in variable (alsaaudio.Mixer(x,x).getvolume(x)

2012-10-24 Thread Steven D'Aprano
On Wed, 24 Oct 2012 20:55:35 +0200, Muffinman wrote:

> Hello all,
> 
> I'm new to Python (running 2.6.6 but if necessary 3.x should also be
> fine). I have a little idea I hope to accomplish with Python. I want to
> listen for changes in Alsa sound volume level and base some actions on
> that. With the few lines below I can check the current volume level. Can
> I extend this so that the script listens for changes in the volume level
> and I can base some actions on it? As speed is quite important it's not
> an option to poll every second or so for changes, it has to be close to
> instantaneous.

Then poll every millisecond or so.

I don't believe it is possible to listen for changes in an arbitrary 
variable. But if you check the alsaaudio module, or ask on a dedicated 
alsa mailing list, you may be able to find out how alsa records the 
volume in the first place. If it is written to a file, you can listen for 
changes to the file without polling on Linux systems.

> If this is not possible with Python, any suggestions on what else are
> also welcome of course.

That's not a Python question, it's an Alsa question. Who knows how the 
sound volume is stored?


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


Re: while expression feature proposal

2012-10-24 Thread Paul Rubin
Ian Kelly  writes:
> j = int(random() * n)
> while j in selected:
> j = int(random() * n)

from itertools import dropwhile

j = dropwhile(lambda j: j in selected,
 iter(lambda: int(random() * n), object()))
 .next()

kind of ugly, makes me wish for a few more itertools primitives, but I
think it expresses reasonably directly what you are trying to do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: while expression feature proposal

2012-10-24 Thread Evan Driscoll
On 10/24/2012 05:26 PM, Cameron Simpson wrote:
> But I'm still -0 on it, because it supplants the glaringly obvious:
> 
>   m = ...
> 
> assignment with the far less in your face:
> 
>   possibly-long-expr as m
> 
> and I think it would get quite heavily used, to the detriment of
> assignment readability in general. At present the nature of most effects
> is at the left. An assignment is obvious on the left, an if/with/while/etc
> is visible at the left.

In the interest of brainstorming, what about

   while VAR from EXPR:

or something like that? I don't think I like 'from' on a couple counts,
but there's probably some word that fits.

Evan



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Ian Kelly
On Wed, Oct 24, 2012 at 5:08 PM, Paul Rubin  wrote:
> from itertools import dropwhile
>
> j = dropwhile(lambda j: j in selected,
>  iter(lambda: int(random() * n), object()))
>  .next()
>
> kind of ugly, makes me wish for a few more itertools primitives, but I
> think it expresses reasonably directly what you are trying to do.

Nice, although a bit opaque.  I think I prefer it as a generator expression:

j = next(j for j in iter(partial(randrange, n), None) if j not in selected)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: resume execution after catching with an excepthook?

2012-10-24 Thread Steven D'Aprano
On Wed, 24 Oct 2012 13:51:30 +0100, andrea crotti wrote:

> So I would like to be able to ask for confirmation when I receive a C-c,
> and continue if the answer is "N/n".

I don't think there is any way to do this directly.

Without a try...except block, execution will cease after an exception is 
caught, even when using sys.excepthook. I don't believe that there is any 
way to jump back to the line of code that just failed (and why would you, 
it will just fail again) or the next line (which will likely fail because 
the previous line failed).

I think the only way you can do this is to write your own execution loop:

while True:
try:
run(next_command())
except KeyboardInterrupt:
if confirm_quit():
break


Of course you need to make run() atomic, or use transactions that can be 
reverted or backed out of. How plausible this is depends on what you are 
trying to do -- Python's Ctrl-C is not really designed to be ignored.

Perhaps a better approach would be to treat Ctrl-C as an unconditional 
exit, and periodically poll the keyboard for another key press to use as 
a conditional exit. Here's a snippet of platform-specific code to get a 
key press:

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

Note however that it blocks if there is no key press waiting.

I suspect that you may need a proper event loop, as provided by GUI 
frameworks, or curses.



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


Re: Listen for changes in variable (alsaaudio.Mixer(x, x).getvolume(x)

2012-10-24 Thread David Hutto
On Wed, Oct 24, 2012 at 2:55 PM, Muffinman  wrote:
> Hello all,
>
> I'm new to Python (running 2.6.6 but if necessary 3.x should also be
> fine). I have a little idea I hope to accomplish with Python. I want to
> listen for changes in Alsa sound volume level and base some actions on
> that.

With the few lines below I can check the current volume level. Can
> I extend this so that the script listens for changes in the volume level
> and I can base some actions on it? As speed is quite important it's not
> an option to poll every second or so for changes, it has to be close to
> instantaneous.
>
> If this is not possible with Python, any suggestions on what else are
> also welcome of course.
>
> Thanks in advance, Maarten
>
>
> #
> try
> mixer = alsaaudio.Mixer(Fake, 0)
> except alsaaudio.ALSAAudioError:
> sys.stderr.write("No such mixer\n")
> sys.exit(1)
>
> volumes = mixer.getvolume(1)
> #
> --
> http://mail.python.org/mailman/listinfo/python-list


first look at the docs for command line functions that utilize the
command line for access to certain CL apps.

Once you've found a way to access the CL apps with python(I use
subprocess.call usually, I think, but there's popen, etc.) then use
man alsamixer, or man aplayer, or man arecorder. in the shell on your
linux distro.

Then throw in a little tkinter, or a windowing system of your choice,
and distort the changes with command line calls.


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Listen for changes in variable (alsaaudio.Mixer(x, x).getvolume(x)

2012-10-24 Thread David Hutto
On Wed, Oct 24, 2012 at 2:55 PM, Muffinman  wrote:
> Hello all,
>
> I'm new to Python (running 2.6.6 but if necessary 3.x should also be
> fine). I have a little idea I hope to accomplish with Python. I want to
> listen for changes in Alsa sound volume level and base some actions on
> that. With the few lines below I can check the current volume level. Can
> I extend this so that the script listens for changes in the volume level
> and I can base some actions on it? As speed is quite important it's not
> an option to poll every second or so for changes, it has to be close to
> instantaneous.
>

You might have to go to c++(ctypes function call), like I'm going to
have to for a oscilloscope(using arecord probably, or other options),
and use in line assembly to push straight from/to the register.

I haven't had a good look at  python's IO module yet though.

Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string format question

2012-10-24 Thread Piet van Oostrum
Adrien  writes:

> print "{:.3g}".format(2.356)  # this rounds up

But:

>>> print "{:.3g}".format(12.356) 
12.4
>>> print "{:.3g}".format(123.356) 
123

-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turn list of letters into an array of integers

2012-10-24 Thread seektime
On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote:
> On Tue, Oct 23, 2012 at 10:23 PM, seektime  wrote:
> 
> > Here's some example code. The input is a list which is a "matrix" of 
> > letters:
> 
> >a  b  a
> 
> >b  b  a
> 
> >
> 
> > and I'd like to turn this into a Python array:
> 
> 
> 
> You mean a Python list. The datatype Python calls an `array` is very
> 
> different and relatively uncommonly used.
> 
> Although, confusingly, Python's lists are implemented using C arrays
> 
> rather than linked lists.
> 
> 
> 
> >   1 2 1
> 
> >   2 2 1
> 
> >
> 
> > so 1 replaces a, and 2 replaces b. Here's the code I have so far:
> 
> >
> 
>  L=['a b a\n','b b a\n']
> 
> 
> 
>  seq
> 
> > '1 2 1\n 2 2 1\n'
> 
> >
> 
> > My question is how can I turn "seq" into a python array?
> 
> 
> 
> I'd say you're asking the wrong question. The better question is "Why
> 
> wasn't the result a list in the first place?". Many transformations
> 
> are cumbersome to express over just strings, which is why the first
> 
> job of most programs is to parse their input into a more convenient
> 
> structure that is suited to their main task(s).
> 
> 
> 
> This (along with some other improvements) leads to a better, somewhat
> 
> different program/algorithm:
> 
> 
> 
> letter2number = {'a': 1, 'b': 2}
> 
> with open("path/to/file.txt", "r") as f:
> 
> result = [[letter2number[letter] for letter in
> 
> line.strip().split()] for line in f]
> 
> 
> 
> If it's safe to assume that the correspondence between the letters and
> 
> numbers isn't completely arbitrary, some further improvements are also
> 
> possible.
> 
> 
> 
> Some relevant docs:
> 
> http://docs.python.org/library/stdtypes.html#string-methods
> 
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions
> 
> 
> 
> Cheers,
> 
> Chris
> 
> 
> 
> P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
> 
> it is worth noting for future reference that the readlines() method is
> 
> considered somewhat deprecated.

Thanks to everyone lots of great comments are actionable suggestions. 

My intension is to used the numpy/scipy packages to solve the task at hand. I 
agree that there's no point in loading a file into a format which only needs to 
be converted right after loading. But I'm new to Python the f.readline(s) 
command, according to the 2.7.3 tutorial and manual, is pretty much all there 
is for file i/o. If, as you indicated, f.readlines() is deprecated then what 
should I use instead? I'm using ver. 2.6 on Linux (it's a bit dated, I know).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-24 Thread Terry Reedy

On 10/24/2012 7:19 PM, Evan Driscoll wrote:

On 10/24/2012 05:26 PM, Cameron Simpson wrote:

But I'm still -0 on it, because it supplants the glaringly obvious:

   m = ...

assignment with the far less in your face:

   possibly-long-expr as m

and I think it would get quite heavily used, to the detriment of
assignment readability in general. At present the nature of most effects
is at the left. An assignment is obvious on the left, an if/with/while/etc
is visible at the left.


In the interest of brainstorming, what about

while VAR from EXPR:

or something like that? I don't think I like 'from' on a couple counts,
but there's probably some word that fits.


The op wondered if these proposals have been made before. They have 
been, and have been rejected. Some of the discussion has been on 
python-ideas list. But go ahead and brainstorm and discuss.


Keep in mind that any new syntax has to be a substantial improvement in 
some sense or make something new possible. There was no new syntax in 
3.2 and very little in 3.3.


--
Terry Jan Reedy

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


Re: turn list of letters into an array of integers

2012-10-24 Thread Chris Rebert
On Wed, Oct 24, 2012 at 9:27 PM, seektime  wrote:
> On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote:

>> P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
>> it is worth noting for future reference that the readlines() method is
>> considered somewhat deprecated.
>
> Thanks to everyone lots of great comments are actionable suggestions.
>
> My intension is to used the numpy/scipy packages to solve the task at hand. I 
> agree that there's no point in loading a file into a format which only needs 
> to be converted right after loading. But I'm new to Python the f.readline(s) 
> command, according to the 2.7.3 tutorial and manual, is pretty much all there 
> is for file i/o. If, as you indicated, f.readlines() is deprecated then what 
> should I use instead? I'm using ver. 2.6 on Linux (it's a bit dated, I know).

Just iterate over the file directly using a for-loop (e.g. `for line
in some_file:`). Each iteration yields one line of the file. I used a
very minor variation of this approach in my code (a list comprehension
is just syntax sugar for a for-loop).

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


[OT] Re: turn list of letters into an array of integers

2012-10-24 Thread Peter Otten
Dennis Lee Bieber wrote:

> On Wed, 24 Oct 2012 11:04:38 +0200, Peter Otten <__pete...@web.de>
> declaimed the following in gmane.comp.python.general:
> 
>> Peter Otten wrote:
>> 
>> Brave new words:
>> 
>> > immortable
>> 
>> should be "immortal"
> 
> Readlines() isn't immortal... It's a lich
> http://en.wikipedia.org/wiki/Lich

Wasn't there a Monty Python sketch where a man carrying a parrot in a cage 
comes into a shop full of stuffed animals and complains: No, I don't admire 
the taxidermist for making that parrot look like it were alive -- that beast 
bit me!

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


Re: Listen for changes in variable (alsaaudio.Mixer(x, x).getvolume(x)

2012-10-24 Thread Tim Roberts
Muffinman  wrote:
>
>I'm new to Python (running 2.6.6 but if necessary 3.x should also be
>fine). I have a little idea I hope to accomplish with Python. I want to
>listen for changes in Alsa sound volume level and base some actions on
>that. With the few lines below I can check the current volume level. 
...
>
>volumes = mixer.getvolume(1)

Now, do you understand that this is just fetching the current setting of
the volume control for the microphone?  It's not telling you anything about
the actual level of the sounds being captured.

The fact that you're talking about real-time response makes me think you
might be misunderstanding this.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list