Re: [Tutor] What’s the differences between these two pieces of code ?

2012-07-13 Thread eryksun
On Sat, Jul 7, 2012 at 12:59 AM, redstone-cold  wrote:
>
> for i in range(1, 7):
>
> print(2 * i, end='   ')

The HTML attachment was scrubbed:
http://mail.python.org/pipermail/tutor/attachments/20120707/855bd6ce/attachment-0001.html

Here's the troublesome 'indent':



Please post using plain text and indent with spaces.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to binary and back... Python 3

2012-07-19 Thread eryksun
On Thu, Jul 19, 2012 at 1:41 AM, wolfrage8...@gmail.com
 wrote:
>
> I was comparing them but I think I understand how to compare them well, now
> I want to convert them both to binary so that I can XOR them together. Thank
> you for your time and help Dave, now I need to reply to Ramit.

A bytes object is a container of 8-bit numbers (i.e. range 0 to 255).
If you index it, you'll get an int that supports the XOR operation:

>>> b1 = b'a'
>>> b2 = b'b'
>>> b1[0]
97
>>> b2[0]
98
>>> bin(b1[0])
'0b111'
>>> bin(b2[0])
'0b1100010'
>>> bin(b1[0] ^ b2[0])
'0b11'

You can use the int method  "from_bytes" to XOR two bitstrings stored
as Python bytes:

>>> b3 = b''
>>> b4 = b''
>>> bin(int.from_bytes(b3, 'big') ^ int.from_bytes(b4, 'big'))
'0b11001100110011'

The computation is done between int objects, not strings. Creating a
string using "bin" is just for presentation.

P.S.:

Instead of "bin" you can use the "format" command to have more
control, such as for zero padding. The integer format code "b" is for
a binary representation. Preceding it by a number starting with zero
will pad with zeros to the given number of characters (e.g. 032 will
prepend zeros to make the result at least 32 characters long):

>>> r = int.from_bytes(b3, 'big') ^ int.from_bytes(b4, 'big')
>>> format(r, "032b")
'0011001100110011'

Instead of hard coding the length (e.g. "032"), you can use the length
of the input bitstrings to calculate the size of the result:

>>> size = 8 * max(len(b3), len(b4))
>>> format(r, "0%db" % size)
'0011001100110011'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to binary and back... Python 3

2012-07-19 Thread eryksun
On Thu, Jul 19, 2012 at 3:08 PM, Jordan  wrote:
>
> size = 8 * max(len(b3), len(b4))
> format(r, "0%db" % size)
>> '0011001100110011'
> Is this output the output for size rather than the two variables joined
> together?

Using "format" is useful if you need the string to be padded with
zeros for the most significant byte. I wouldn't think it's important
if you're just using the bitstring representation as a sanity check on
your algorithm. In that case you can more easily use "bin".

That said, len(b3) is the number of characters (bytes) in the bytes
object. Since b3 and b4 could be different lengths in general, I took
the max length to use for the zero padding. In this case both b3 and
b4 contain 4 bytes, so "size" is 32.

> OK. I am using one time pads to XOR data, but the one time pads (keys)
> are very large numbers, converting them to binary increases their size
> exponentially, which allows me to get more XORing done out of a single
> key. I am XORing both files and strings so I need to have code that can
> do both even if that means two branches of code via an if/else perhaps
> with an isinstance(data, str).

I'm not an expert with cryptography, but here's a simple XOR example:

>>> from itertools import cycle
>>> text = b'Mary had a little lamb.'
>>> key = b'1234'
>>> cypher = bytes(x^y for x,y in zip(text, cycle(key)))
>>> cypher
b'|SAM\x11ZRP\x11S\x13XXFGXT\x12_U\\P\x1d'
>>> text2 = bytes(x^y for x,y in zip(cypher, cycle(key)))
>>> text2
b'Mary had a little lamb.'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to binary and back... Python 3

2012-07-19 Thread eryksun
On Thu, Jul 19, 2012 at 5:32 PM, Jordan  wrote:
>
> I am not sure how to answer that question because all files are binary,
> but the files that I will parse have an encoding that allows them to be
> read in a non-binary output. But my program will not use the in a
> non-binary way, that is why I plan to open them with the 'b' mode to
> open them as binary with no encoding assumed by python. I just not have
> tested this new technique that you gave me on a binary file yet as I was
> still implementing it for strings.

Reading from a file in binary mode returns a bytes object in Python 3.
Since iterating over bytes returns ints, you can cycle the key over
the plain text using zip and compute the XOR without having to convert
the entire message into a single big number in memory. Here's my
example from before, adapted for files:

>>> from itertools import cycle
>>> key = b'1234'
>>> kit = cycle(key)
>>> with open('temp.txt', 'rb') as f, open('cipher.txt', 'wb') as fo:
... fit = iter(lambda: f.read(512), b'')
... for text in fit:
... fo.write(bytes(x^y for x,y in zip(text, kit)))

Since the input file could be arbitrarily large and lack newlines, I'm
using "iter" to create a special iterator that reads 512-byte chunks.
The iterator stops when "read" returns an empty bytes object (i.e.
b''). You could use a while loop instead.

I assume here that the key is possibly shorter than the message (e.g.
encrypting 1 megabyte of text with a 128 byte key). If you're making a
one-time pad I think the key is the same length as the message. In
that case you wouldn't have to worry about cycling it. Anyway, I'm not
particularly interested in cryptography. I'm just trying to help with
the operations.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string to binary and back... Python 3

2012-07-20 Thread eryksun
On Fri, Jul 20, 2012 at 3:16 AM, Alan Gauld  wrote:
> On 20/07/12 06:48, wolfrage8...@gmail.com wrote:
>
> Using the format method it would look like:
>
> newhexdata = bytes("{0:x}".format(numdata), "ascii")

binascii.unhexlify needs an even number of hexadecimal digits (two per
byte). So you need to either modify the above string to prepend a '0'
if the length of newhexdata is odd, or calculate the size ahead of
time and format with zero padding:

nbytes = (numdata.bit_length() + 7) // 8
size = nbytes * 2
newhexdata = bytes('{0:0{1}x}'.format(numdata, size), 'ascii')

Though creating an intermediate string of hex digits seems the long
way around. In Python 3, I'd skip binascii and instead use
int.from_bytes and int.to_bytes:

>>> numdata = 0x0102
>>> nbytes = (numdata.bit_length() + 7) // 8
>>> numdata.to_bytes(nbytes, 'big')
b'\x01\x02'

That said, it still seems more reasonable and flexible to me to use a
generator expression to do the XOR byte by byte instead of turning the
message into a big integer. You could share a large file of random
data and send a starting position along with the encrypted text. Best
of luck.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Original indices after Sorting

2012-07-23 Thread eryksun
On Mon, Jul 23, 2012 at 4:02 PM, Ali Torkamani  wrote:
> By the way, I myself, have this solution:
>
>> How can we get the indices of values in the original list after sorting a
>> list?
>>
>
> (Pdb) A=[ 1, -1, 0, 7, 9, 1.3, 2.9 ]
> (Pdb) sorted(zip(A, range(len(A))), key = lambda x: x[0])
> [(-1, 1), (0, 2), (1, 0), (1.3, 5), (2.9, 6), (7, 3), (9, 4)]

Here's a variation:

from operator import itemgetter

def sortix(seq):
L, B = zip(*sorted(enumerate(seq), key=itemgetter(1)))
return B, L

Your original result is shaped Nx2. zip(*seq) is a quick way to
transpose that to 2xN. The * operator turns each item into an argument
of zip.

Example:

>>> A = [1, -1, 0, 7, 9, 1.3, 2.9]
>>> B, L = sortix(A)
>>> B
(-1, 0, 1, 1.3, 2.9, 7, 9)
>>> L
(1, 2, 0, 5, 6, 3, 4)

If you need lists, you can use map:

>>> B, L = map(list, sortix(A))
>>> B
[-1, 0, 1, 1.3, 2.9, 7, 9]
>>> L
[1, 2, 0, 5, 6, 3, 4]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-28 Thread eryksun
On Sat, Jul 28, 2012 at 11:12 AM, Francesco Loffredo  wrote:
>
> I had to study carefully your present and desired lists, and I understood
> what follows (please, next time explain !):
> - each 7-tuple in your present list is a record for some measure relative to
> a person. Its fields are as follows:
> - field 0: code (I think you want that in growing order)
> - field 1: group code (could be a class or a group to which both of your
> example persons belong)
> - fields 2, 3: surname and name of the person
> - field 4: progressive number of the measure (these are in order
> already, but I think you want to enforce this) that you want to exclude from
> the output list while keeping the order
> - field 5, 6: numerator and denominator of a ratio that is the measure.
> you want the ratio to be written as a single string: "%s/%s" % field5,
> field6

This looks like a good problem for itertools.groupby. My solution
below needs error checking and testing, but this is where I'd start:

data = [
  (0, '3eA', 'Dupont', 'Juliette', 0, 11.0, 10.0),
  (0, '3eA', 'Dupont', 'Juliette', 1, 4.0, 5.0),
  (0, '3eA', 'Dupont', 'Juliette', 2, 17.5, 30.0),
  (0, '3eA', 'Dupont', 'Juliette', 3, 3.0, 5.0),
  (0, '3eA', 'Dupont', 'Juliette', 4, 4.5, 10.0),
  (0, '3eA', 'Dupont', 'Juliette', 5, 35.5, 60.0),
  (1, '3eA', 'Pop', 'Iggy', 0, 12.0, 10.0),
  (1, '3eA', 'Pop', 'Iggy', 1, 3.5, 5.0),
  (1, '3eA', 'Pop', 'Iggy', 2, 11.5, 30.0),
  (1, '3eA', 'Pop', 'Iggy', 3, 4.0, 5.0),
  (1, '3eA', 'Pop', 'Iggy', 4, 5.5, 10.0),
  (1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0),
]

from operator import itemgetter
from itertools import groupby

#first sort by keyfunc, then group by it
keyfunc = itemgetter(0,1,2,3)
groups = groupby(sorted(data, key=keyfunc), keyfunc)

result = []
for group, records in groups:
temp = tuple('%s/%s' % r[5:] for r in sorted(records, key=itemgetter(4)))
result.append(group + temp)

>>> result
[(0, '3eA', 'Dupont', 'Juliette', '11.0/10.0', '4.0/5.0', '17.5/30.0',
'3.0/5.0', '4.5/10.0', '35.5/60.0'), (1, '3eA', 'Pop', 'Iggy',
'12.0/10.0', '3.5/5.0', '11.5/30.0', '4.0/5.0', '5.5/10.0',
'40.5/60.0')]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-28 Thread eryksun
On Sat, Jul 28, 2012 at 7:12 PM, Francesco Loffredo  wrote:
>
> My bad, now I'll RTFM again and I will study very carefully the operator and
> itertools modules.

I forgot to mention a gotcha about groupby's implementation. The
grouby object and the yielded _grouper objects share a single
iterator. Here's a (somewhat contrived) example of a mistake:

>>> groups = groupby(sorted(data, key=keyfunc), keyfunc)
>>> groups = list(groups)  #DON'T DO THIS
>>> groups
[((0, '3eA', 'Dupont', 'Juliette'), ),
 ((1, '3eA', 'Pop', 'Iggy'), )]

>>> list(groups[0][1])  #EMPTY
[]
>>> list(groups[1][1])  #ONLY THE LAST ITEM
[(1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-29 Thread eryksun
On Sun, Jul 29, 2012 at 7:21 AM, Peter Otten <__pete...@web.de> wrote:
>
> If you don't have to deal with large datasets many of its functions can
> easily be emulated with lists and loops though. As an example here's the
> grouping with a plain vanilla dict:
>
> groups = {}
> for item in data:
> groups.setdefault(item[:4], []).append(item[-2:])
>
> result = [key + tuple("{}/{}".format(*v) for v in values) for key, values in
> groups.items()]

Or use a defaultdict:

from collections import defaultdict

groups = defaultdict(list)
for item in data:
groups[item[:4]].append(item[4:])

result = []
for key in sorted(groups):
groups[key].sort()
result.append(key + tuple('%s/%s' % v[1:] for v in groups[key]))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-29 Thread eryksun
On Sun, Jul 29, 2012 at 4:29 PM, Peter Otten <__pete...@web.de> wrote:
>
> If you have to sort your data anyway you can sort it first and then apply
> itertools.groupby()...

That was my thinking. I wrote it with groupby (see link below) because
Francesco described field 0 as "in growing order".

http://mail.python.org/pipermail/tutor/2012-July/090607.html

I see now the 2nd call to sorted() in the tuple generator was
redundant. I should have just sorted the data without the key function
to order within each group by field 4 in the first pass:

keyfunc = itemgetter(0,1,2,3)
groups = groupby(sorted(data), keyfunc)

result = []
for group, records in groups:
temp = tuple('%s/%s' % r[5:] for r in records)
result.append(group + temp)

In general though you have to sort and group by the same key.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread eryksun
On Mon, Jul 30, 2012 at 3:10 PM, Tino Dai  wrote:
> On Mon, Jul 30, 2012 at 1:52 PM, Mark Lawrence 
> wrote:
>
>> Sorry I'm not completely sure what you're asking for but will this help
>> http://docs.python.org/library/atexit.html ?
>>
> I think this might be what I'm looking for. But for about 2 minutes, I was
> like a-texitwhat does that have to do with it's at exit.

You might want to install a SIGINT (ctrl-c) signal handler. If you
manually handle the SIGINT signal, you can ignore it after the first
signal (users tend to press ctrl-c repeatedly). You could also apply
this to a regular try/finally approach instead of using atexit. If you
choose to use atexit, don't expect magic compared to try/finally. If
the OS kills the process or the interpreter crashes, your atexit
function won't run. I'd periodically save the data (probably based on
both time and quantity) for a long-run process.

For example:

import signal

def sigint(signum, stack):
#dummy handler to ignore future SIGINT signals
dummy = lambda si, st: None
signal.signal(signal.SIGINT, dummy)
raise KeyboardInterrupt

signal.signal(signal.SIGINT, sigint)

def save_data():
print "Saving"
for k in xrange(1<<24):
pass
print "\nSaved {0} entries".format(k + 1)

try:
print "Waiting for signal..."  #press ctrl-c
signal.pause()
except:
pass  #At least log here
finally:
save_data()

The original SIGINT handler is signal.default_int_handler in case you
need to restore it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ImportError

2012-07-31 Thread eryksun
On Tue, Jul 31, 2012 at 10:32 AM, Tino Dai  wrote:
>
>   File "/home/tdai/ProjectOne-TNT/leg_apps/etl/transfers/__init__.py", line
> 8, in 
> from api import models
>   File "/home/tdai/ProjectOne-TNT/leg_apps/api/models.py", line 20, in
> 
> from etl.transfers import eastern
> ImportError: cannot import name eastern

It looks to me like you have a circular import problem. Before the
etl.transfers package defines eastern it attempts to import
api.models, but executing the latter tries and fails to import (the as
yet undefined) eastern from etl.transfers. If this is the problem, you
could refactor the cyclic dependency into its own module, or move the
import to after eastern is defined, or do the import in
functions/methods that run after the modules have been loaded, etc.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __new__ and __init__

2012-08-01 Thread eryksun
On Wed, Aug 1, 2012 at 11:10 AM, Hugo Arts  wrote:
> On Wed, Aug 1, 2012 at 4:28 PM, rail shafigulin 
>
> * small caveat: I'm entirely unsure of this, but I *think* if you create
> CarModel with a metaclass that overrides __call__ you can change the way
> __new__ and __init__ work? If anyone can confirm this, be my guest.
>

That works, but if you're going to use a metaclass (i.e. a type
subclass of which the CarModel class is an instance), you may as well
skip overriding __new__. You don't want to mess around with
metaclasses, however, unless absolutely necessary. Don't needlessly
complicate your code to make it harder to understand and maintain.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding a windows registry value

2012-08-03 Thread eryksun
On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam  wrote:
> Hi,
>
> I am trying to change a registry value (Windows 7, Python 2.7) but it won't
> work when I try to do this using Python:
>
> import os
> # 1 using this from Python won't work, but double-clicking the file works.
> os.system(r"regedit /s C:\Users\Desktop\set_temp.reg")
> # 2: using this from Python won't work, but from the commandline or with a
> batch file works
> os.system("reg add HKEY_CURRENT_USER\Software .(etc)")
>
> Why is this not working using Python? Is there a built-in way to do this (I
> don't have win32api)?

Wouldn't it be simpler to use the  _winreg module?

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


Re: [Tutor] adding a windows registry value

2012-08-03 Thread eryksun
On Fri, Aug 3, 2012 at 6:28 PM, Alan Gauld  wrote:
>
> The problem with the  reply goes to list approach is that replying to a
> poster individually becomes very tedious - you have to manually remove the
> unwanted addresses.
>
> Of course the best solution is to have a client that recognises lists and
> automatically responds appropriately, mutt and Thunderbird both seem to do
> that well.

I have the Gmail lab enabled that defaults to "reply all", but the
default [reasonable] behavior in that case is to reply to the sender
and CC everyone else (i.e. the list). I generally don't want that on a
list, but I often forget to manually fix it. The list server adds a
"List-Post" field. I wish Gmail could use that as the default for
reply all, or make it an available option.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding a windows registry value

2012-08-03 Thread eryksun
On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam  wrote:
>
> import os
> # 1 using this from Python won't work, but double-clicking the file works.
> os.system(r"regedit /s C:\Users\Desktop\set_temp.reg")

Do you have a user named Desktop, or was this supposed to be
"C:\Users\USER_NAME\Desktop\set_temp.reg"?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Script won't run for no apparent reason

2012-08-10 Thread eryksun
On Fri, Aug 10, 2012 at 6:10 PM, Martin A. Brown  wrote:
>
>  : values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j',
>  : 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r',
>  : 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z',
>  : 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H',
>  : 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P',
>  : 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X',
>  : 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'}
>
> This sort of thing always catches my eye, and I think to myself
> 'Are there any tools or libraries in this language that I could use
> to generate this, instead of writing out this repetitive data
> structure?'
>
> Here's what I did for my own amusement and possibly of benefit to
> you.  There are probably better solutions out there for your Caesar
> cipher enjoyment, but I hope you may find this helpful.
>
>   # -- This code should create a dictionary that should look like the
>   #one above, but you can create it on the fly with a different
>   #value for the shift.  You could also use a different alphabet.
>   #
>   def generate_caesar_cipher(alphabet,shift):
>   offset = shift - len(alphabet)
>   cipheralpha = ''.join((alphabet[offset:], alphabet[0:offset]))
>   return dict(zip(alphabet,cipheralpha))
>
>   caesar_shift = 3
>
>   values = dict()
>   values.update(generate_caesar_cipher(string.ascii_letters,caesar_shift))

Close, but you're rotating the lower and uppercase letters together.
In the original they're separate. Here's a different approach using
itertools:

from itertools import islice, cycle
from string import ascii_lowercase, ascii_uppercase

def make_caesar_cipher(alphabet=ascii_lowercase, shift=3):
return dict(zip(alphabet, islice(cycle(alphabet), shift, None)))

values = make_caesar_cipher()
values.update(make_caesar_cipher(ascii_uppercase))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores  wrote:
>
> I wrote pickle_attempt.py as an exercise to try to learn to use the
> pickle module. See the version I edited for Tutor,
> pickle_attempt_for_web.py at
> .
> 
> But after closing the program and restarting it, those items have
> disappeared from D.

On line 68 you open the file in 'ab' mode. A pickle stream ends with
an ASCII period (\x2e). Anything appended after that is ignored. Use
'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary,
so you might want a file extension other than .txt, such as .pkl,
.dat, .bin, etc.

If you're curious about the protocol, you can disassemble a pickle
using pickletools.dis(pickle), where pickle is bytes or a file-like
object.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 9:18 PM, eryksun  wrote:
>
> On line 68 you open the file in 'ab' mode. A pickle stream ends with
> an ASCII period (\x2e). Anything appended after that is ignored. Use
> 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary,
> so you might want a file extension other than .txt, such as .pkl,
> .dat, .bin, etc.

To clarify, you can store multiple pickles in a file, but each needs
its own load. So you'd have to maintain a session dictionary for the
factors of new integers. Then append the pickled session to the file
when the user quits. When the program starts you'd have to loop
through the file to update D with each pickled session.

If you want an approach that's simpler and faster, use the shelve
module. A shelf is a dictionary-like object that uses pickle to
serialize objects stored to a database. The keys have to be strings,
so your code would change to  `D[str(n)] = factors`.

http://docs.python.org/py3k/library/shelve.html#module-shelve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 10:43 PM, Richard D. Moores  wrote:
>
> Changing to 'wb' mode and using the file extension .dat completely
> corrected the problems, it seems. Line 69 now reads,
> f = open("factors.dat", 'wb')

The extension doesn't affect anything about the file. It's just .txt
indicates a text file.

> But the reference I have says of 'wb': "Write to a binary file. If the
> file exists, its contents are overwritten. If the file doesn't exist,
> it's created. Why doesn't factors.dat get overwritten before the
> pickle.dump()? Is it because there isn't any new data to be written at
> that point?

Opening in mode 'wb' truncates the file. Then pickle.dump() writes the
pickle to it. Before you were appending to the end of the file, so you
had multiple pickled dictionaries stored in the file.

> And another question, if I might. If factors.dat doesn't exist, to use
> the program I need to manually create it and rem out lines 49-52 the
> first time I call the script. I thought I could replace lines 49-52
> with
>
> if "factors.dat":
> f = open("factors.dat", 'rb')
> data = pickle.load(f)
> f.close
> D = data
> else:
> f = open("factors.dat", 'wb')

You can catch the IOError if the file doesn't exist or
pickle.PickleError if it's corrupted, and just assign an empty dict.

try:
with open("factors.dat", 'rb') as f:
D = pickle.load(f)
except (IOError, pickle.PickleError):
D = {}

There's no reason to open the file for writing at this point. You do that later.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 11:19 PM, Richard D. Moores  wrote:
>
>> To clarify, you can store multiple pickles in a file, but each needs
>> its own load. So you'd have to maintain a session dictionary for the
>> factors of new integers. Then append the pickled session to the file
>> when the user quits. When the program starts you'd have to loop
>> through the file to update D with each pickled session.
>
> Isn't that essentially what my script does?

On line 69 your script (http://pastebin.com/SNwKRuSK) appends the
current D to the end. So only the last pickle appended would be
complete. My first response was for you to switch to 'wb' mode to
truncate the file and only save the latest complete session.

Then it occurred to me that you actually wanted to grow the pickle the
file. I proposed the above solution to append the new factorizations
per session. Then at the start load the pickled sessions in a loop,
updating D with each loaded dictionary. For example:

D = {}
session = {}
try:
with open('factors.dat', 'rb') as f:
while True:
D.update(pickle.load(f))
except EOFError:
pass
except (IOError, pickle.PickleError):
D = {}

if len(D):
print("Loaded", len(D), "entries.")

while True:
n = int(input("integer: "))

if n == 0:
print("D has", len(D), "entries.")
with open('factors.dat', 'ab') as f:
pickle.dump(session, f)
break

try:
factors = D[n]
print("the factors of", n, "are", factors)

except KeyError:
factors = factorsOfInteger(n)
print("the factors of", n, "are", factors)
D[n] = factors
session[n] = factors
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 4:44 AM, Richard D. Moores  wrote:
>
> OK, thanks for the code, which I will duly study. However, I just
> pasted my new version, pickle_attempt_for_web2.py at
> . I've tested it and tested it, and it
> does exactly what I wanted (thanks to you!). Yes, you're correct, I
> want to grow the pickle file, the dictionary. The new version puts
> every new item into it -- nothing gets lost.

I forgot to separate out the PickleError. It shouldn't append new data
to a file that gave an error, so rename the existing file. A new file
is created later when factors.dat is opened in append mode ('ab').

You asked about handling an empty file. This version (which grows the
existing file per session instead of writing a new file) always sees
EOFError since it calls pickle.load(f) until it reaches EOF. But the
same applies if you only call pickle.load(f) once on an empty file.
Just handle the exception with a simple "pass" statement.

I narrowed the IOError handling to only deal with ENOENT (file not
found). There are other reasons the file could fail to open for
reading (e.g. it's a directory or you don't have permission), none of
which you probably want to handle automatically, so it just exits with
an error.

I restructured the main loop to put everything in the try block and
handled the ValueError case as well as exiting with an error if the
pickle/save fails.

import sys, os, errno
import pickle
from pickle import PickleError

D = {}
session = {}
try:
with open("factors.dat", 'rb') as f:
while True:
D.update(pickle.load(f))
except EOFError:
pass#Empty file or finished loading
except IOError as e:
if e.errno != errno.ENOENT:
sys.exit("Can't open factors.dat for reading.")
except PickleError:
try:
#Rename so as not to append to a bad file
os.rename('factors.dat', 'factors.err')
except OSError:
sys.exit("Renaming damaged factors.dat failed.")

if len(D):
print("Loaded", len(D), "entries.")

while True:
try:
n = int(input("Enter a non-negative integer (0 to quit): "))
if n < 0:
raise ValueError

if n == 0:
print("D has", len(D), "entries")
if len(session):
with open('factors.dat', 'ab') as f:
   pickle.dump(session, f)
sys.exit(0)

factors = D[n]
print("the factors of", n, "are", factors)

except ValueError:
print("e.g. 0, 1, 2, 3")

except KeyError:
factors = factorsOfInteger(n)
print("the factors of", n, "are", factors)
D[n] = session[n] = factors

except (IOError, PickleError) as e:
sys.exit("Error saving data: {0}".format(e.args[-1]))


> One thing I'd like to implement is a monitor of the time
> factorsOfInteger(n) takes to process some of the 18-digit ints (line
> 153). Most are processed within a second or two, but some can take
> several minutes. I'd like to limit the time to 15 or 20 seconds, but
> is there a way to do this? Just a wild guess, but is this where
> threading would be useful?

I'd put the time check in the main loop of factorsOfInteger.

threading doesn't have an interface to stop a running thread. If you
want to use threads, use queues since they're a simple, thread-safe
way to communicate between threads. You can modify factorsOfInteger to
monitor a queue.Queue and break when it receives a command to halt.
Use a 2nd queue to get the result back from the worker thread.
Specifically, only call findFactor in factorsOfInteger if qin.empty()
is True. Otherwise, qout.put(False) and break. If the function
terminates normally, then qout.put(factors).

qin = queue.Queue()
qout = queue.Queue()

t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)).start()

try:
factors = qout.get(timeout=20)
except queue.Empty:
qin.put('halt')

t.join()  #wait for the thread to terminate
factors = qout.get()  #possibly False if factorsOfInteger quit early
if factors:
D[n] = session[n] = factors

See the docs:

http://docs.python.org/py3k/library/threading.html
http://docs.python.org/py3k/library/queue.html

You could also use a multiprocessing pool to speed things up if you
have multiple cores. You'd have to rewrite the factorization code a
bit. Partition the search range (i.e. up to the square root) among N
cores and run findFactor in parallel. If you have 4 cores, you'll get
up to 4 factors. Divide them out, repartition the new range, and
repeat. Or something like that. I'm sure you can find a good reference
online for parallel factorization.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 7:44 AM, Richard D. Moores  wrote:
>
> I just discovered
> os.path.getsize('factors.txt')
> and that factors.txt has a size of 2 bytes when "empty".
> (I changed the file extension to .txt so I could delete the contents.)

No, an empty file has no data; the size is 0. You must have saved a
text file in Windows that added a byte order mark (BOM). Windows adds
a BOM for UTF-8 and UTF-16 ('Unicode') files. It sounds silly to have
a BOM for UTF-8, which can't have a little endian or big endian byte
order, but it's there to distinguish UTF-8 from 8-bit ANSI (e.g.
Windows 1252).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 8:46 AM, eryksun  wrote:
>
> t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)).start()

Sorry I need to proofread better. That should be the following:

t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout))
t.start()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel  wrote:
>
> 2) You wind up with a floating point number.  If you're getting floats,
> then you're limited to their precision, maybe 18 digits, and limited to
> their speed.  Perhaps you need to use the // divide rather than the /
> one.  And perhaps it'd help to use divmod instead of using % and /
> separately.

Good catch in Kent Johnson's code. Maybe he'll search for his name and
find this. It should be `r = r // factor`.

> 3) You're doing a % on all the odd numbers above 1, where you really
> only need to try the primes.  If you build an iterator for the primes,
> you don't need to precalculate them, just cache them for reuse.  Again,
> gmp2 is likely to have something useful.

@Richard, if you haven't learned generators, see the docs for yield
expressions:

http://docs.python.org/py3k/reference/expressions.html#yield-expressions
http://www.python.org/dev/peps/pep-0255

First yield the cached primes. Then compute the next prime, cache it,
and yield it. Use the cache to speed up finding the next prime. You
can pickle the cache of primes instead of the factorizations.

> I'm not sure what the point is of preserving the factorizations,
> especially since you don't store the number you're factoring for each
> case.

The numbers are the dictionary keys.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output not in ANSI

2012-08-13 Thread eryksun
On Mon, Aug 13, 2012 at 2:04 PM, Joel Goldstick
 wrote:
>
> I believe in this context the OP means ASCII.  ASCII became an ANSI
> recognized standard many years ago

In Windows, ANSI refers to the locale-dependent 8-bit codepage. But
there is no ANSI standard for Windows1252. It's a common misnomer in
the OS dialog boxes and controls. Another MS misnomer is labeling
UTF-16 as 'Unicode'.

@leon zaat

Process your text with Unicode. Open the file using codecs.open set to
your platform's preferred encoding, e.g. 'cp1252' for Western,
'cp1251' for Cyrilic, or locale.getpreferredencoding() in general.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] overriding instance attributes with keywords

2012-08-13 Thread eryksun
On Mon, Aug 13, 2012 at 3:13 PM, Gregory, Matthew
 wrote:
>
> I'm trying to create a new instance from an existing instance

> def new_with_overrides(s1, **kwargs):
> new_params = {'a': s1.a, 'b': s1.b}
> for (k, v) in kwargs.iteritems():
> if k in new_params:
> new_params[k] = v
> return Spam(new_params['a'], new_params['b'])

> This works but it doesn't seem very extendable if new attributes are added to 
> Spam.

In general instance attributes won't map to __init__ arguments. You
can create a new instance by calling  __new__. Then update from the
old dict and add in override attributes.

def new_with_overrides(obj1, **kwds):
obj2 = obj1.__new__(obj1.__class__)
obj2.__dict__.update(obj1.__dict__)
for k, v in kwds.items():
if k in obj2.__dict__:
obj2.__dict__[k] = v
return obj2
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Mon, Aug 13, 2012 at 10:28 PM, Steven D'Aprano  wrote:
>
>> def new_with_overrides(obj1, **kwds):
>>  obj2 = obj1.__new__(obj1.__class__)
>>  obj2.__dict__.update(obj1.__dict__)
>>  for k, v in kwds.items():
>>  if k in obj2.__dict__:
>>  obj2.__dict__[k] = v
>>  return obj2
>
> In general, this can fail too. The instance may have __slots__, it
> may not have a __dict__ at all, it may be using properties or
> __getattr__ etc. to implement computed attributes. All sorts of things
> can go wrong when copying arbitrary instances. That's why there is an
> entire protocol so that types can make themselves copyable.
>
> Matt, if you're trying to make a "copy any instance" utility function, you
> are re-inventing the wheel. See the copy module instead.

Right, I overlooked classes with __slots__ and that override __new__
and other special methods. copy() is the better and more customizable
solution. This is the same route for customizing the pickle of an
object, so the pickle docs should help:

http://docs.python.org/library/pickle.html#pickling-and-unpickling-normal-class-instances

The default __reduce__(2) will call  __getnewargs__ and __getstate__
to create an info tuple used to construct a copy. The args from
__getnewargs__ are used to call a __newobj__ function that in turn
calls cls.__new__(cls, *args) to create a new instance. The copy
reconstructor will update the new instance with the state from
__getstate__ if it's non-false. If the state is a tuple, the 2nd item
should be the slots state. The reconstructor uses __setstate__ if it
exists. Otherwise it updates the instance dict with state[0] if it's
non-false, and uses setattr to set slots if state[1] is non-false.

class Test(object):
   def __new__(cls, v):
   print '__new__', v
   obj = object.__new__(cls)
   obj.new_attr = v
   return obj

   def __getnewargs__(self):
   print '__getnewargs__'
   return (self.new_attr, )

   def __getstate__(self):
   print '__getstate__'
   return ({'attr': 'value'}, {'slot_attr': 'value'})

   def __setstate__(self, state):
   print '__setstate__', state

>>> t1 = Test('test')
__new__ test
>>> t2 = copy.copy(t1)
__getnewargs__
__getstate__
__new__ test
__setstate__ ({'attr': 'value'}, {'slot_attr': 'value'})
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 6:01 AM, eryksun  wrote:
>
> Right, I overlooked classes with __slots__ and that override __new__
> and other special methods. copy() is the better and more customizable
> solution.

If copying is good enough, then this should work:

from copy import copy

def copy_with_overrides(obj1, **kwds):
obj2 = copy(obj1)
for k, v in kwds.items():
if hasattr(obj2, k):
setattr(obj2, k, v)
return obj2

However, I apologize for sidetracking you if you need __init__ to run
(possibly creating new instances of attributes instead of getting
copied references). That really should be a classmethod as Steve
suggests (or simple a regular method and use cls = self.__class__),
and you'll need to keep it in sync with any changes you make to
__init__.  A generic function can't know how to call the constructor.
Even if you use inspect.getfullargspec(), there's no guarantee every
argument will have a corresponding object attribute with the same name
or that the current value is the one __init__ needs  -- not unless you
design it that way.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding()

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 10:03 AM, Peter Otten <__pete...@web.de> wrote:
>
> You have to find out the database encoding

The character at 7 is 'ë', which is '\xc3\xab' in UTF-8. So that's
likely the db encoding.

> As you now have a bytestring again you can forget about codecs.open() which
> won't work anyway as the csv module doesn't support unicode properly in
> Python 2.x (The csv documentation has the details).

Sorry for suggesting codecs.open earlier. I looked at the source for
the _csv module, and sure enough it calls PyObject_Str(field).
Thankfully csv in 3.x has a new API.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 2:25 PM, Matt Gregory
 wrote:
>
> Thanks to you both for really helpful advice.  A quick follow-up question -
> would you want to create a deepcopy of obj1 in the above example if your
> obj1 contained other objects?  I think that Steven's class method gets
> around this?

Chances are you either want a shallow copy or to use a special
constructor as Steven wrote.  For example, consider an object with a
file attribute based on a filename argument. Say for example it's for
an MP3 file and the object reads in the ID3 tags in __init__. If you
want a new instance based on the same arguments, except for a
different MP3 file, then you don't want a copy or a deepcopy.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confusion regarding the 'from' statement

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 2:24 PM, Mazhar Hussain  wrote:
>
> #mod1.py
> from mod2 import test
> test('mod1.py')
>
> #mod2.py
> def countLines(name):
> print len(open(name).readlines())
>
> def countChars(name):
> print len(open(name).read())
>
> def test(name):
> print 'loading...'
> countLines(name)
> countChars(name)
> print '-'*10
>
> Here when I imported and ran the 'test' function, it ran successfully
> although I didn't even import countChars or countLines, and the 'from'
> statement had already deleted the mod2 module object.
>
> SO I basically need to know why does this code work although
> considering the problems I mentioned it shouldn't.

mod1 gets a reference to mod2.test, but the mod2 object isn't garbage
collected. A reference exists in sys.modules. After the import, add
the following:

import sys
print sys.modules['mod2']

Also test() can access countChars() and countLines() because it was
defined in the mod2 namespace. In other words, test.__globals__ is
mod2.__globals__. An example:

#mod1.py
import mod2
mod2.test_global()
print mod2.g

#mod2.py
def test_global():
global g
g = "I'm in mod2."

#run python mod1.py

Even though test_global is called from mod1, g is created in mod2.

More namespace trivia...
When you run a module as the main script, its name is '__main__' and
other modules can "import __main__". But if you import it by the
filename, you get a new module instance with a separate namespace:

#mod1
if __name__ == '__main__':
import mod2
mod2.imp_main() #create g in __main__ and also in mod1
print g  #__main__
import mod1  #self import, but mod1 instance, not __main__
print mod1.g  #mod1

#mod2
def imp_main():
import __main__
import mod1
__main__.g = "__main__"
mod1.g = "mod1"

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


Re: [Tutor] Confusion regarding the 'from' statement

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 9:04 PM, Dave Angel  wrote:
>
> 1) recursive imports.  If anybody has a non-trivial top-level code, or
> even certain class-initialization code, this can cause surprising errors.

Yes, it's a problem if you try to "from" import an object that hasn't
been defined yet  or use something that hasn't been properly
initialized. But AFAIK, just importing the current module won't
execute it twice unless it's running as __main__.  You get the
existing reference from sys.modules.

> 2) multiple imports under different names.  By importing the "same"
> module under the two names __main__ and mod1, you usually end up with
> two copies of stuff, and that can break a lot of implicit assumptions in
> working code.

I wasn't casting any judgment, but personally I agree it's a bad idea.
Without the "if" block the code executes twice, and everything gets
defined twice. If for some reason you need to import the main script
(for whatever reason, but the point is probably academic), import
__main__, not its module filename.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: output not in ANSI, conversing char set to locale.getpreferredencoding()

2012-08-16 Thread eryksun
On Thu, Aug 16, 2012 at 7:33 AM, leon zaat  wrote:
>
> Tried it with:
> openbareruimtenaam = openbareruimtenaam1.decode("UTF-8").encode("cp1252")
> but still the complains about the ascii error

Did you also change it for woonplaatsnaam? For example:


import locale
file_encoding = locale.getpreferredencoding()  #e.g. "cp1252"
db_encoding = "utf-8"

for row in num:
openbareruimtenaam1 = row[0].decode(db_encoding)
openbareruimtenaam = openbareruimtenaam1.encode(file_encoding)
woonplaatsnaam1 = row[0].decode(db_encoding)
woonplaatsnaam = woonplaatsnaam1.encode(file_encoding)
newrow = [openbareruimtenaam, woonplaatsnaam]
verblijfhoofd.writerow(newrow)

# Should "woonplaatsnaam1" be assigned to row[1] instead of row[0]?


Off -topic comments:

Try to avoid cluttering your code with redundant parentheses, unless
they make it easier to understand a complex expression. If func()
returns a string, you don't need to do "string" + (func()). The extra
parentheses are just noise. Use "string" + func().

Also, if you have a long string, use the compiler's implicit string
join instead of runtime concatenation. If you have "string1"
"string2", the compiler stores "string1string2". You can combine this
with a parenthesized multiline expression. For example:


sql = ("Select identificatie, hoofdadres, verblijfsobjectgeometrie "
   "from verblijfsobject where identificatie > '" +
   hulpIdentificatie)

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


Re: [Tutor] specifying my "default" python installation

2012-08-17 Thread eryksun
On Fri, Aug 17, 2012 at 11:05 AM, Prasad, Ramit
 wrote:
>
> I am not really familiar with BSD but *nix has the application
> update-alternatives. That will do what you want. Otherwise,
> you could change the name/location in the bin directory.
> It is likely that python is a symlink to python2.6 and all
> you need to do is change the symlink to point to python3.2.
> If no symlink is used you can rename the binaries instead.

Modifying the default to Python 3 sounds like a bad idea. Platforms
are still in transition to 3.x. Some scripts might assume
/usr/bin/python links to python2.x.

On Debian there's a python3 symlink. You can add your own if FreeBSD
doesn't have it. First, if ~/bin doesn't exist, run "mkdir ~/bin";
restart your session, and run "echo $PATH" to verify it's on the
search path.  Then make the link with "ln -s /usr/bin/python3.2
~/bin/python3". You'll have to update this when 3.3 is released.

If ~/bin isn't on the PATH, edit ~/.profile and add the following:

if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi

Also, while it's probably a bit much at first, I recommend using
virtualenv to configure environments with different versions of
Python/packages:

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


Re: [Tutor] Error in apparently correct code

2012-08-21 Thread eryksun
On Mon, Aug 20, 2012 at 11:21 PM, Steven D'Aprano  wrote:
>
> Indentation has meaning in Python. When you send email, if your email
> program (possibly GMail?) mangles the indentation, your code will become
> invalid, unreadable mess. You need to fix this. Often the easiest way to fix
> this is to *not* post so-called "rich text", actually HTML. Most mail
> programs will leave plain text emails alone, but if you send HTML, they will
> mangle *both* the text and the HTML.

Gmail's editor for plain text will hard wrap *some* lines at about 69
characters, but not rigidly (it seems to leave code untouched).
There's no live preview (the textarea wraps at the window width). You
just click "Send" and hope it doesn't mangle your formatting.

Also, the interface displays plain text mail with a proportional font.
Depending on your browser you can hack the CSS to use a monospace
font. I use Stylish in Firefox:

http://userstyles.org/styles/15618/gmail-monospace-font-for-body-messages-textarea
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread eryksun
On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__pete...@web.de> wrote:
>
> wanted = [line.strip("\n") for line in lines
>   if "vn" not in line and "vt" not in line and line != "\n"]

Here's an equivalent expression with the negation factored out:

not ("vn" in line or "vt" in line or line == "\n")

http://en.wikipedia.org/wiki/De_Morgan%27s_laws

If you have a lot of tests all using the same operator (e.g. "in"),
you can use "any" (OR) or "all" (AND) with a generator expression:

vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"]
wanted = [line for line in lines if not any(v in line for v in vals)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 1:02 PM, Ashley Fowler
 wrote:
>
>  Instructions: Your "main" function should do the following:
> (1) create an empty list;
> (2) ask the user if he/she wants to perform a list operation.
>  if "yes":
>  (a) prompt the user for the operation:
>   "test", "peek", "add", or "remove"; 
>  (b) perform the operation:
>  (i) if "test" then print whether the list is empty or not;
>  (ii) if "peek" then print the number at the beginning of the
>   list (with a label) but don't remove it from the list;
>  (iii) if "add", then prompt the user for a number and add
>   it to the end of the list;
>  (iv) if "remove", then delete the number at the beginning
>   of the list and print it (with a label);
>(c) print the entire list from beginning to end;
>  repeat step (2) until the user enters "no".
>
>
> def main():
> l = list()

You can also use "l = []". By the way, "l" is a bad variable name. It
looks like a number "1" in many fonts, and it isn't descriptive. You
could be very original and call it

numbers = []

> x = eval(input('Enter a number: '))
> while x >= 0:
> l.append(x)
> x = eval(input('Enter a number: '))

Don't use eval. Use float or int. Why are you looping until it's
negative? According to your problem specification, a number should be
appended when the user requests to "add" a number, and it says nothing
about the valid range.

More importantly this is your main loop and the statements that
follows aren't in it. Pay close attention to indent level in Python
code.

> ask = input (" Do you want to perform a list operation?")
> if "yes":

The "if" sees a literal "yes" and treats that as a True statement --
always. Instead you need to do something like the following:

run = input("Do you want to perform a list operation? (yes/no) ")
if run == "yes":

Let's show it in the while loop where it belongs:

def main():
numbers = []
run = "yes"
while run != "no":
run = input("Do you want to perform a list operation? (yes/no) ")
if run == "yes":

> input (" Do you want to test, peek, add, or remove?")
> if "test":

OK, now you haven't even assigned the input to a variable. You're
being cheeky, eh? Let's give this one an original name, too. Call it
"op" for operation.

op = input("Do you want to test, peek, add, or remove? ")
if op == "test":

> if not l:
> print("The list is not empty")
> else:
> print("The list is empty")

This is apparently the opposite of what you thought. The expression
"not l" is True when the list *is empty*. Let's just use "if numbers"
(using the new, more descriptive name for the list):

if numbers:
print("The list is not empty")
else:
print("The list is empty")

> elif "peek":
> print(l[0])

What will happen if the list is empty and you try to get index 0?

For the "remove" operation, look into the list method "pop".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 3:05 PM, Jared Nielsen  wrote:
> Hi all,
> I'm new to programming and Python.
> I want to write a script that takes a string input and breaks the string at
> keywords then outputs the pieces on separate lines.

This is just for printing? You can use replace():

>>> text = "Ham and cheese omelette with hasbrowns and coffee."
>>> print text.replace(" and ", "\nand\n")
Ham
and
cheese omelette with hasbrowns
and
coffee.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 4:03 PM, David Rock  wrote:
> text = raw_input("Enter text: ")
> sep = 'and'
> parts = text.split(sep)
> for i in parts[:-1]:
> print i
> print sep
> print [-1]

>>> "band".split("and")
['b', '']

It needs to be sep = " and ". That's assuming we're ignoring tabs.
Line feed shouldn't be an issue since the source is raw_input.
Anything more advanced should probably use regular expressions.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 5:25 PM, Alan Gauld  wrote:
>
> To use partition just call it repeatedly until the last string is empty. As
> ever the >>> prompt is your friend:
>
 st = 'here we go and there you are and we all go roundabout'
 h,s,t = st.partition(' and')
 results = [h,s]
 while t:
> ...h,s,t = t.partition(s)
> ...results += [h,s]
> ...

The keyword needs a space at both ends:

>>> st = 'an androphobic andromedan android'
>>> h,s,t = st.partition(' and')
>>> results = [h,s]
>>> while t:
... h,s,t = t.partition(s)
... results += [h,s]
...
>>> results
['an', ' and', 'rophobic', ' and', 'romedan', ' and', 'roid', '']

> Finally you can also find a solution using regular expressions, but they
> shouldn't be needed for something like this.

It depends on how flexible it needs to be about common whitespace
characters (space, tab, new line, carriage return) and punctuation.
But this should be fine for raw_input. Tabs and punctuation could be
replaced with spaces beforehand if necessary. Otherwise it won't
partition a sentence on " and " such as, "I want an omelet--and some
hash browns."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 9:08 PM,   wrote:
>
 s.dir()
> 

Surely you mean dir(s). Maybe you're thinking of the __dir__ special
method you can add to a class to override the default behavior.

You can also dir(str), or call help(str) to page through all of the doc strings.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 9:39 PM, Dave Angel  wrote:
>
>> theGoodLines = [line.strip("\n") for line in lines if "v " ==
>> line[0:2]]
>
> Better to use startswith(), since short lines will cause the if
> expression above to blow up.

A slice won't blow up. At worst you get an empty string. But the slice
does create a temporary string, and subsequently the expression uses a
high-level compare.

startswith is more efficient and flexible in CPython. It does a
low-level memory compare (memcmp). You can pass it a single string or
a tuple of strings to match against, and you can set a start/stop
position.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python working with Bash....arrrggggh!

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 11:55 PM, Ray Jones  wrote:

> For example, if I wish to test if a file exists, I might do
>
> test = Popen('[ -f file-i-want-to-test-for ]')
>
> But the moment I invoke Bash for a test, I must deal with the fact that
> Bash returns a zero for true and a non-zero for false. But in Python,

Please see os.path:

http://docs.python.org/library/os.path

That said, you can use check_call and check_output if you want a more
Pythonic interface. These raise an exception for a non-zero return
code. For example:

# check.py

from subprocess import check_call, CalledProcessError

try:

# in Debian [ is at /usr/bin/[
rc = check_call(["/usr/bin/[", "-f", "check.py", "]"])
print "success:", rc

# or pass the command string to the shell
rc = check_call("[ -f check.py ]", shell=True)
print "success:", rc

# force a failure
rc = check_call("[ -f notfound ]", shell=True)
print "success:", rc  # this won't execute

except CalledProcessError as e:
print "failed: %d" % e.returncode
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] better way to write this code

2012-08-24 Thread eryksun
On Thu, Aug 23, 2012 at 2:33 PM, Norman Khine  wrote:
>
> import operator, json
> from BeautifulSoup import BeautifulSoup

If you have the source of TABLE_CONTENT, why don't you soup that
instead? Otherwise, nevermind.

> combos={0: 'id',
> 2: 'country',
> 3: 'type',
> 5: 'lat',
> 6: 'lon',
> 12: 'name' }

Here's the style I'd use for the above:

combos = {
  0: 'id',
  2: 'country',
  3: 'type',
  5: 'lat',
  6: 'lon',
  12: 'name',
}

Put each entry on its own line, indented by two spaces, and leave a
trailing comma on the last entry. The latter is especially important
in sequences of strings to prevent them from being
"silently"<=>"concatenated" if you were to add an entry and forget the
comma.

Below I've formatted the rest of your code to use 4 spaces instead of
tabs. Please follow PEP 8 unless local style rules take precedence. If
your code gets copied into a file that uses spaces, you end up with
the headache of mixing tabs and spaces. So try to use spaces since
that's what most people use. Your editor should be able to insert
spaces when you press .

> event_list = []
> for event in TABLE_CONTENT:
> event_dict = {}
> for index, item in enumerate(event):
> if index == 8:
> if item == ' ':
> event_dict['depth'] = '0'
> else:
> event_dict['depth'] = item

You can simplify the above with a ternary expression. IMO, this
simplifies maintenance because you're not repeating the assignment to
"event_dict['depth']":

  if index == 8:
  event_dict['depth'] = item if item != ' ' else '0'

> if index == 9:
> try:
> items = item.split()
> if len(items) >= 2:
> event_dict['yield'] = items[-1]
> else:
> if item == ' ':
> event_dict['yield'] = '10'
> else:
> event_dict['yield'] = item
> except:
> pass

I fail to see why you need a try/except here. Avoid using a bare
"except". Handle specific exceptions.

> if index == 3:
> if 'Atmospheric' in item:
> event_dict['fill'] = 'red'
> if 'Underground' in item:
> event_dict['fill'] = 'green'

Should there be a default value for event_dict['fill']?

> event_list.append(event_dict)
> print event_dict
> event_list = sorted(event_list, key = operator.itemgetter('id'))

You may as well sort in place:

event_list.sort(operator.itemgetter('id'))

> f = open('detonations.json', 'w')
> f.write(json.dumps(event_list))
> f.close()
> print 'detonations.json, written!'

Use "with" to make sure the file gets closed even if there's an
exception. Also, you may as well "dump" directly to the file instead
of creating a string with "dumps":

with open('detonations.json', 'w') as f:
json.dump(event_list, f)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] better way to write this code

2012-08-24 Thread eryksun
On Fri, Aug 24, 2012 at 8:11 AM, Peter Otten <__pete...@web.de> wrote:
>
> for index, name, fixer in fixers:
> item = event[index]

@Norman
I forgot to mention this. You should index the event instead of
iterating over it. I suppose each event has a name in index 12, so you
shouldn't get an IndexError.

Then you can loop over combos:

for index, name in combos.items():
event_dict[name] = event[index]

Or you can generalize the process as Peter has shown.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to split/partition a string on keywords?

2012-08-24 Thread eryksun
On Fri, Aug 24, 2012 at 1:30 PM, Jared Nielsen  wrote:
>
> But if I run the following:
>
>
> #!/usr/bin/python
>
> text = raw_input("Enter text: ")
>
> text.replace("and", "\nand")
> text.replace("or", "\nor")
>
> print text
>
> I get the text as it was entered.
> Is there a way to replace text in a string without splitting or
> partitioning?

A Python string is immutable. So replace() just gives you a new
string. You can assign it to a different variable.

> The bigger picture for this little project is a "poetry machine", in which a
> user enters some prose and the program chops it up into modern poetry.

Based on your original problem description, I think you'll get the
best result using regular expression substitution:

import re

words = "|".join(["and", "or"])

pattern = r"(^|\W)({kwds})(?=\W|$)".format(kwds=words)

prose = []
line = raw_input("Enter text: ")
while line:
prose.append(line)
line = raw_input()

poetry = " ".join(re.sub(pattern, r"\1\n\2", line) for line in prose)
print poetry

# try
# or... ham and cheese or chicken and waffles...and...
# whatever she wants--and he'll have the special.

Output:

or... ham
and cheese
or chicken
and waffles...
and... whatever she wants--
and he'll have the special.

The first thing to note with regular expressions is always use (r)aw
string literals, such as r"text". The expressions make heavy use of
backslash escapes, so you don't want Python's compiler to process
regular string escapes. It's much simpler to use raw mode than to
quote all the backslashes as '\\'.

In "pattern" you see three sub-patterns (groups) in parentheses. In
group one, the ^ matches at the beginning of the line, the \W matches
any non-alphanumeric character, and the | means the group will match
on either ^ or \W. In group two, I'm using Python's string formatting
to map the string "and|or" into {kwds}. As before the | means this
group matches on either the literal "and" or the literal "or".

Group 3 is a bit more complicated. It starts with the ?= operator.
This is a lookahead operation. When this groups matches it won't
consume any of the string. This allows overlapping matches on the
non-alphanumeric character \W. In other words, if you have "some text
and and repeated", the whitespace joining the first " and " and the
second " and " can count toward both matches. The $ means this group
also matches at the end of the line.

Finally all of the prose lines are processed with re.sub. This looks
for "pattern" in "line" and replaces it with r"\1\n\2". In the
replacement string \1 is group 1, \2 is group 2, and \n is a new line
character.

Please see the re docs for further explanation:

http://docs.python.org/library/re

Here's a pretty good tutorial in general:

http://www.regular-expressions.info

> So, this is a long shot naive noob question, but is there any way to count
> syllables in words in a string? Or at least approximate this procedure?

At this point you're getting into natural language processing. You can
try the Natural Language Processing Toolkit (NLTK), but I don't know
if it breaks words into syllables:

http://nltk.org

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


Re: [Tutor] creating a subclass from superclass without __init__

2012-08-24 Thread eryksun
On Fri, Aug 24, 2012 at 4:03 PM, Matt Gregory
 wrote:
>
> There are two classes of interest in GDAL - a Dataset which typically
> describes a geospatial raster file and Band which described a single band
> from the Dataset.  gdal.Band just raises an AttributeError within __init__,
> but a gdal.Band instance can be created using
> gdal.Dataset.GetRasterBand(bandNum).

SWIG! Why'd it have it be SWIG? C++... very dangerous. You go first. ;)

GDAL Usage
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/val_repl.py

Proxy Classes
Driver
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L355
Dataset
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L647
Band
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L851

SWIG Interfaces
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Driver.i#L31
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Dataset.i#L308
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Band.i#L182
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/python/gdal_python.i#L260

_gdal module
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/extensions/gdal_wrap.cpp

Open, OpenShared, GetDriver, GetDriverByName, etc return proxies to
C++ shadow classes. It doesn't look like the SWIG interface was
written for extending the classes in Python (something called
"directors" according to the docs).

The proxies lack a callable __init__ because the C++ shadow classes
have private constructors. Instead, you have calls such as
Dataset.GetRasterBand, which calls _gdal.Dataset_GetRasterBand (i.e.
the C++ function _wrap_Dataset_GetRasterBand), which calls
GDALDatasetShadow_GetRasterBand, which calls GDALGetRasterBand. The
result gets cast to a GDALRasterBandShadow*. Finally, Python gets the
latter wrapped as a Band proxy.

> I had wanted to create additional methods on gdal.Band that would allow some
> GIS type operations (e.g. overloading __add__), thus I thought I would need
> a subclass of gdal.Band.  So I was unsure of how to get an instance of my
> subclass when the only way I knew how to create an instance of the
> superclass (gdal.Band) was through the call to GetRasterBand().

I don't see an easy way to do this without modifying the SWIG
interface or monkey patching the Python classes. But my knowledge of
SWIG is superficial. You should ask this on a more appropriate forum,
or Stack Overflow.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reason(s) for trailing comma in dict declarations

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 11:53 AM,   wrote:
>
> Put each entry on its own line, indented by two spaces, and leave a
> trailing comma on the last entry. The latter is especially important
> in sequences of strings to prevent them from being
> "silently"<=>"concatenated" if you were to add an entry and forget the
> comma.
> """
>
> When I first saw this I thought it would lead to a syntax error so tried
> it out..
> Then played with it to try to recreate the '"silently"<=>"concatenated"'
> problem but couldn't.  I like this syntax because it avoids the syntax
> error if the comma is omitted when adding an entry but I don't understand
> the (potential) concatenation problem.
>
> Could you explain please?

Sure, I said the problem is with "sequences", not mappings. The syntax
for a dictionary will catch the mistake. But try it with a list.

x = [
  "string1",
  "string2",
  "string3"   # no comma
]

Later you decide to add "string4" but forget to add the comma:

x = [
  "string1",
  "string2",
  "string3"   # no comma
  "string4"
]

Result:

['string1', 'string2', 'string3string4']
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reason(s) for trailing comma in dict declarations

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 1:39 PM, Alan Gauld  wrote:
>
> two adjacent strings without a comma get combined into a single string.
> Its a feature... mainly a remnant from the C foundations I suspect.

As a feature it can come in handy with long strings in expressions.

Just for reference about the "C foundations", here's a similar example in C.

#include 

int main() {

int i, x_len;

char *x[] = {
  "string1",
  "string2",
  "string3"  /* no comma */
  "string4"
};

x_len = sizeof(x) / sizeof(x[0]);

for (i = 0; i < x_len; i++) {
printf("%s\n", x[i]);
}

return 0;
}

Output:

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


Re: [Tutor] 2.7.3 Popen argument issues

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 8:46 PM, Ray Jones  wrote:
>
> Here is my Python call to vlc (error response to follow):
>
> vlcExec = sp.Popen(['vlc', 'http://' + ip + ':' + port, '-I dummy',
> '--sout
> \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=outFile
> + '.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\''])
>

Arguments are split on spaces. For example, it's ['-I', 'dummy'], not
['-I dummy']. You can put the command in a string and split() it.

Hopefully the following will work once you set the values you're using
for ip/port.

import subprocess

ip = "192.168.0.2"
port = "1234"

out_file = "testing.avi"
out_ip = "127.0.0.1"
out_port = "11300"
dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file
dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port)
sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http)

cmd = "vlc http://%s:%s -I dummy --sout %s" % (ip, port, sout)

p = subprocess.Popen(cmd.split())


To answer your initial question, you could call a Python script (or a
Bash script if you prefer) instead of vlc. In Python, just print out
the list sys.argv.

cmd = "python argtest.py http://%s:%s -I dummy --sout %s" % (ip, port, sout)
p = subprocess.Popen(cmd.split())

# argtest.py

import sys
print "\nArgs:"
for arg in sys.argv:
print arg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 Popen argument issues

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 11:02 PM, eryksun  wrote:
>
> import subprocess
>
> ip = "192.168.0.2"
> port = "1234"
>
> out_file = "testing.avi"
> out_ip = "127.0.0.1"
> out_port = "11300"
> dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file
> dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port)
> sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http)
>

If that doesn't work, vlc might not like the extra quotes around sout.
Try it without them:

sout = "#duplicate{dst=%s,dst=%s}" % (dst_file, dst_http)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] askopenfilename - columns missing in navigationwindow

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 9:56 PM, Joel Levine  wrote:
>
> from tkFileDialog import askopenfilename
> fn=askopenfilename()
>
> I use this program repeatedly.  Up to a few hours ago, the navigation window
> opened with a full array of columns, including name and date.
>
> Once I mistakenly typed  control-F  (Find) instead of letters (for the
> file).   Now the navigation window opens with only one column, the name
> column.

Try running "tclsh" in the terminal. Then enter:

package require Tk
set fn [tk_getOpenFile]
puts $fn
exit

Is it the same dialog as in Python? Knowing whether the problem is
with Tcl/Tk or with Tkinter might get you closer to a solution.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 Popen argument issues

2012-08-25 Thread eryksun
On Sat, Aug 25, 2012 at 11:02 PM, eryksun  wrote:
>
> out_file = "testing.avi"
> out_ip = "127.0.0.1"
> out_port = "11300"
> dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file
> dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port)
> sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http)
>
> cmd = "vlc http://%s:%s -I dummy --sout %s" % (ip, port, sout)
>
> p = subprocess.Popen(cmd.split())

Wow... That was silly of me. Don't use split(). It would split up
arguments that have internal spaces. Just build the list.

sout = "#duplicate{dst=%s,dst=%s}" % (dst_file, dst_http)

cmd = ["vlc", "http://%s:%s"; % (ip, port), "-I", "dummy", "--sout", sout]
p = subprocess.Popen(cmd)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 Popen argument issues

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 7:55 AM, Ray Jones  wrote:
>
> [0x8d42554] stream_out_standard stream out error: no mux specified or
> found by extension
> [0x8d42134] main stream output error: stream chain failed for
> `standard{mux="",access=""#duplicate{dst="transcode{vb=400}",dst="std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=localhost:11300}"}""}'
>
>
> Notice the addition of `standard{mux="",access='' ' before the
> `#duplicate' . I think --sout adds a default combination if it doesn't
> find a proper argument. Unfortunately, I know a bit less about vlc
> command line arguments than I do about Python's generation of those
> arguments ;)


But the Bash call worked?

cmd = 'vlc http://"HOST":PORT -I dummy --sout
\'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\''

print '\n'.join(shlex.split(cmd))   # shlex.split parses like the shell

Output:

vlc
http://HOST:PORT
-I
dummy
--sout
#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calculation issue.

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 2:09 PM, Matthew Ngaha  wrote:
>
> heres the code: self.time_til_drop = int(new_pizza.height * 1.3 /
> Pizza.speed) + 1

height / speed is the number of steps it takes for a pizza to fall its
complete height. It sets time_til_drop to approximately 130% of that
number (with a +1 fudge factor in case the int() value is 0). It seems
this function is called at every step to decrement time_til_drop until
it's 0. Once it's 0, a new pizza is created to be dropped. The
designer wants a 30% buffer (in steps) between the time one pizza has
finished dropping and the creation of a new pizza.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calculation issue.

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 4:36 PM, eryksun  wrote:
>
> it's 0. Once it's 0, a new pizza is created to be dropped. The
> designer wants a 30% buffer (in steps) between the time one pizza has
> finished dropping and the creation of a new pizza.

To clarify, say the chef is at y=100, and a pizza is 100 pixels high
with a speed of 2 pixels/step. check_drop() creates a new pizza and
sets time_til_drop to 1 + int(100 * 1.3 / 2) = 66. So check_drop will
be called 66 times, i.e. (65, 64, ..., 0), before another pizza gets
created. In that time the previous pizza has fallen 2 * 66 = 132
pixels, from y=100 down to y=232 (if 0,0 is the upper left-hand
corner). The bottom of the next pizza is at y = 100 + 100 = 200. That
leaves a buffer between them of 232 - 200 = 32 pixels, which is 32% in
this case.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calculation issue.

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 5:56 PM, Alan Gauld  wrote:
>
> For a 60px pizza that means a gap of 79 pixels approx.

Just to be clear, it's a gap of 79 pixels between the top y
coordinates, not between the bottom of one pizza and the top of the
other. The latter gap is 79 - 60 = 19 pixels, or a 31.67% buffer.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calculation issue.

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 5:56 PM, Alan Gauld  wrote:
>
> For a 60px pizza that means a gap of 79 pixels approx.

Just to be clear, it's a gap of 79 pixels between the top y
coordinates, not between the bottom of one pizza and the top of the
other. The latter gap is 79 - 60 = 19 pixels, or a 31.67% buffer.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calculation issue.

2012-08-26 Thread eryksun
On Sun, Aug 26, 2012 at 6:19 PM, Matthew Ngaha  wrote:
>
> if this Pizza (pizza.b) was on hold at position y = 100 and the
> previous pizza(pizza.a) had fallen an additional 132 pixels to 232
> before pizza.b was released, shouldn't pizza.b still only be at
> position y = 100? how did it gain the additional 100 pixels if it was
> on hold while pizza.a reached 232. i understand how you've worked it
> out, just need more clarification on how the Pizza.b fell an addition
> 100 pixels making its total distance 200.

The top of pizza.b is at y=100, but the bottom of pizza.b is at y=199
(sorry, I had a one-off error there). The gap between the pizzas is
232 - 199 - 1 = 32 pixels, from y=200 to y=231.

> Sorry Alan the code was rather big, i didnt want it to be  pain for
> you guys trying to help. here is the full code. I do understand the

You can link to the full code at a site such as pastbin.com, but be
sure to include the relevant parts in your question.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 Popen argument issues

2012-08-27 Thread eryksun
On Mon, Aug 27, 2012 at 3:52 AM, Ray Jones  wrote:
>
> Yes, the Bash call worked (in fact I tried it just prior to sending the
> original message just to be sure).
>
> I guess I'm a bit confused about 'splitting' the arguments. You said
> that Python splits arguments on spaces. What exactly happens to the
> arguments if we split them up into individual strings separated by commas?

I meant that only in comparison to how the shell tokenizes a command.
For example, in the shell if you want spaces to be ignored, you have
to escape them or use quotes, and if you want to include raw
quotes/backslashes you have to escape them too. To tokenize a command
string as the shell would, you can use shlex.split():

http://docs.python.org/library/shlex.html#shlex.split

Otherwise the space character doesn't have any special significance.
What matters is that all of the argument strings are in the list in
the right order. It doesn't matter how they get there. So instead of
using shlex.split you may as well build the list directly.

Popen uses the args list/tuple to call the executable (after forking
and setting up the pipes). This passes through the os module on to the
built-in posix module. There it eventually calls the POSIX system
function execv(path, argv). (FYI, in Windows there's no fork/exec;
Win32 CreateProcess is used instead.)

POSIX is a system standard that's part of the core UNIX standard
(unix.org). It's a collaboration between IEEE and The Open Group.
Here's the documentation for the exec family of calls:

http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html

Here's their description of the arguments of execv(path, argv):

* The argument "path" points to a pathname that identifies the new
  process image file.

* The argument "argv" is an array of character pointers to null-
  terminated strings. The application shall ensure that the last
  member of this array is a null pointer. These strings shall
  constitute the argument list available to the new process image.
  The value in argv[0] should point to a filename that is
  associated with the process being started by one of the exec
  functions.

Most programs expect their arguments to have been tokenized as the
shell would as a matter of convention. So, for example, if vlc gets
"-I" in argv[1] it expects that argv[2] will be a value such as
"dummy". A value of "-I dummy" in argv[1] in principle shouldn't work.
In practice vlc seems pretty flexible and accepts it both ways.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__pete...@web.de> wrote:
>
> Anyway here's an alternative implementation:
>
 def vi(x):
> ... if not isinstance(x, numbers.Number):
> ... raise TypeError
> ... if not int(x) == x:
> ... raise ValueError

You could test against numbers.Integral. But it's not fool-proof.
Someone can register an incompatible class with the abstract base
class (ABC).

>>> import numbers
>>> isinstance("42", numbers.Integral)
False
>>> numbers.Integral.register(str)
>>> isinstance("42", numbers.Integral)
True

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


Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 9:08 AM, Peter Otten <__pete...@web.de> wrote:
>
> That would reject "floats with an integral value" and therefore doesn't
> comply with the -- non-existing -- spec.

Gotcha.

>> >>> import numbers
>> >>> isinstance("42", numbers.Integral)
>> False
>> >>> numbers.Integral.register(str)
>> >>> isinstance("42", numbers.Integral)
>> True
>
> That's quite an elaborate scheme to shoot yourself in the foot ;)

It was just a quick example. In practice what could happen is someone
would register an integer-like class (instead of subclassing
numbers.Integral or numbers.Number) that is incomplete and ends up
raising an exception.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Installing modules with easy_install

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 2:39 PM, Ray Jones  wrote:
>
>> Do you have multiple python installations on your machine? Do you run
>> easy_install in one and ipython in another?
>
> Perhaps. But the module is not accessible from the 'python' shell, from
> 'idle', or from iPython.
>
> As I peruse Synaptic I find I have active installations of Ubuntu's
> basic python, python2.7, and python2.7-minimal. But are these separate
> installations? Virtually every system package thinks it's dependent on
> each one of these python packages.
>
> Suggestions?

Those are not separate installations. The python package depends on
python2.7 and python-minimal. The latter depends on python2.7-minimal.
You should be able to install pytz from the Ubuntu repository. Search
for the package python-tz. If you install from the repository, be sure
to manually delete the old installation in the local dist-packages
directory.

http://packages.ubuntu.com/search?searchon=names&keywords=python-tz

That said, in Debian Wheezy, pytz installs and runs fine from
/usr/local/lib/python2.7/dist-packages. You could hack a temporary fix
with a .pth file or using the PYTHONPATH environment variable, but
it's better to figure out the problem.

To help debug your problem, first check which Python installation
you're running. Run "ls -Hl `which python`". Is it /usr/bin/python?

Next check whether the latter path is actually in sys.path. In Debian,
on which Ubuntu is based, it gets added in /usr/lib/python2.7/site.py:

elif os.sep == '/':
sitepackages.append(os.path.join(prefix, "local/lib",
"python" + sys.version[:3],
"dist-packages"))
sitepackages.append(os.path.join(prefix, "lib",
"python" + sys.version[:3],
"dist-packages"))

Next check /usr/local/lib/python2.7/dist-packages/easy_install.pth.
Does it have a line with "./pytz-2012d-py2.7.egg" to include the
latter directory on the path (ignore the wonky code to modify the
path's insertion point) ? Check inside that directory for a pytz
directory that has an __init__.py.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Installing modules with easy_install

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones  wrote:
>
> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included
> in the sys.path. Now what?

Good, but does sys.path contain
/usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread eryksun
On Thu, Aug 30, 2012 at 4:44 AM, Peter Otten <__pete...@web.de> wrote:
>
 sum(["a", "b", "c"], "")
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: sum() can't sum strings [use ''.join(seq) instead]
>
> gives me the creeps even though it'd never occur to me to actually use sum()
> to join a sequence of strings.

class Start(object):
def __add__(self, other):
return other

>>> sum(['Bypassing', ' nanny', ' typecheck...'], Start())
'Bypassing nanny typecheck...'

FTFY

(It goes without saying: never do this.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread eryksun
On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten <__pete...@web.de> wrote:
>
> Allowing floats for a primality test is a can of worms anyway. You will
> inevitably run out of significant digits:

Allowing floats can also lead to type errors for operations that
require an integral type, but at least they're easier to catch with
proper testing. For example, line 323 will raise a TypeError if n is a
float:

http://code.google.com/p/pyprimes/source/browse/src/pyprimes.py#323

1.0 == 1, but range(1.0) is not allowed and neither is [0,1][1.0].
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem caused by installing 2.7.3

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 12:49 PM, Richard D. Moores  wrote:
>
> MS Windows 7 Home Premium 64-bit SP1
>
> I have some useful (to me) scripts that I use frequently, that I call
> with Windows shortcut keys. They used to open in a nice cmd.exe window
> I'd configured to my liking. Now I find that they open in the default
> white on black tiny window, C:\Python27\python.exe, which tries to run
> the 3.x scripts in 2.7. How did that change come about, and how can I
> correct it?

.py files are associated (assoc .py) with Python.File (ftype
Python.File), which defines the command to run .py console scripts.
The 2.x installer overwrote the registry keys. If you plan to write
scripts for both 2.x and 3.x, the best solution, IMO, is to install
pylauncher to add shebang support to your scripts:

https://bitbucket.org/vinay.sajip/pylauncher

The simpler of the two installations for you would be
launchwin.amd64.msi. It puts py.exe and pyw.exe in the Windows folder,
so you don't have to muck with PATH. Once installed your scripts will
run with py.exe, which takes care of parsing the shebang and starting
the required interpreter.

You can define additional shebangs in %LOCALAPPDATA%\py.ini (e.g. to
add support for pypy). I think this also works in %APPDATA%\py.ini if
you'd rather use your roaming profile. See the docs for more
information:

https://bitbucket.org/vinay.sajip/pylauncher/raw/tip/Doc/launcher.rst
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 102, Issue 98

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 8:20 PM, Steven D'Aprano  wrote:
>
> Sequence
>   The generalisation of lists, tuples and strings. Any object that has
>   a known length where individual items can be retrieved with the
>   __getitem__ method called sequentially: obj[0], obj[1], obj[2], ...

To expand on this, any object that has a __getitem__ method can be
iterated over until it raises an IndexError, not just object's that
have an __iter__ method, and __len__ is not a factor. For example:

class Test(object):
def __getitem__(self, n):
if n > 4:
raise IndexError
return 'test'

>>> list(Test())
['test', 'test', 'test', 'test', 'test']
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 9:08 PM, Scurvy Scott  wrote:
>
> My question is this- I've been trying for a month to generate a list of
> all possible 10 digit numbers. I've googled, looked on stackoverflow,
> experimented with itertools,

In itertools, look at count() and islice(). An alternative to islice
in this case is takewhile().

count() works with Python long integers. For example, counter =
count(start=10**9L).

islice() is limited to sys.maxint (sys.maxsize in Python 3), so you
need to chain several together on a 32-bit platform (also 64-bit
Windows, I gather, since a C long is always 32-bit on Windows). Use a
generator expression with chain.from_iterable:

counter = count(start=10**9L)
slices = (islice(counter, 10**9) for i in range(10))
nums = chain.from_iterable(slices)

Since printing performance is limited by the terminal's speed, you
probably don't need the efficiency of islice (with its machine word
size limit). Instead, you can use takewhile() with a pure Python
predicate. For example:

counter = count(start=10**9L)
nums = takewhile(lambda x: x < 10**10, counter)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread eryksun
On Sat, Sep 1, 2012 at 12:29 AM, Scurvy Scott  wrote:
>
> The while loop works for simply printing. Now I'm trying to save to a file
>
> I = 10
> Boogers = raw_input("file")
> Baseball = open(Boogers)

As ASCII, that's 11 bytes per number times 9 billion numbers. That's
approximately 92 GiB (about 21 DVD-5 discs). Are you sure you want to
do that?

Minor correction:

>>counter = count(start=10**9L)
>>slices = (islice(counter, 10**9) for i in range(10))
>>nums = chain.from_iterable(slices)

That should have been range(9), not range(10).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-09-01 Thread eryksun
On Sat, Sep 1, 2012 at 3:18 AM, Dave Angel  wrote:
>
> Somehow i missed the point that xrange() is NOT necessarily limited to
> Python int values. So it may be usable on your machine, if your Python
> is 64bit. All I really know is that it works on mine (2.7 64bit, on
> Linux). See the following quote

Since xrange uses a C long type, it's limited to sys.maxint. On a
64-bit Linux system a C long is 64-bit. In Windows, a C long is always
32-bit, so I would suppose sys.maxint is 2**31 - 1 on both 32-bit and
64-bit systems. Someone running 64-bit Windows can confirm that.

> islice(count(start,step),(stop-start+step-1+2*(step<0))//step)

Or more commonly with step==1:

islice(count(start), stop - start)

This works so long as the 2nd argument is less than sys.maxsize. On my
32-bit system, that's limited to 2**31 - 1. So instead I decided to
slice a billion numbers at a time and use chain.from_iterable() to
chain several slices from a generator expression.

Previously I thought it had to be less than sys.maxint. The error on
my 32-bit Debian system said the value had to be <= maxint, so I
assumed islice uses a C long type. But I looked in the
itertoolsmodule.c source (2.7.3) and discovered that islice uses a C
ssize_t. So the error should have said the size has to be <= maxsize.
On a 64-bit platform that's 2**63 - 1, even on 64-bit Windows. Again,
someone running 64-bit Windows can confirm that.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using multiprocessing efficiently to process large data file

2012-09-01 Thread eryksun
On Sat, Sep 1, 2012 at 9:14 AM, Wayne Werner  wrote:
>
> with open('inputfile') as f:
> for line1, line2, line3, line4 in zip(f,f,f,f):
> # do your processing here

Use itertools.izip_longest (zip_longest in 3.x) for this. Items in the
final batch are set to fillvalue (defaults to None) if the iterator
has reached the end of the file.

Below I've included a template that uses a multiprocessing.Pool, but
only if there are cores available. On a single-core system it falls
back to using itertools.imap (use built-in map in 3.x).

from multiprocessing import Pool, cpu_count
from itertools import izip_longest, imap

FILE_IN = '...'
FILE_OUT = '...'

NLINES = 100 # estimate this for a good chunk_size
BATCH_SIZE = 8

def func(batch):
""" test func """
import os, time
time.sleep(0.001)
return "%d: %s\n" % (os.getpid(), repr(batch))

if __name__ == '__main__': # <-- required for Windows

file_in, file_out = open(FILE_IN), open(FILE_OUT, 'w')
nworkers = cpu_count() - 1

with file_in, file_out:
batches = izip_longest(* [file_in] * BATCH_SIZE)
if nworkers > 0:
pool = Pool(nworkers)
chunk_size = NLINES // BATCH_SIZE // nworkers
result = pool.imap(func, batches, chunk_size)
else:
result = imap(func, batches)
file_out.writelines(result)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 generator objects

2012-09-02 Thread eryksun
On Sun, Sep 2, 2012 at 1:44 AM, Ray Jones  wrote:
>
> I was playing with os.walk today. I can use os.walk in a for loop (does
> that make it an iterator or just an irritable? ^_^),

The output from os.walk is a generator, which is an iterator. os.walk
actually calls itself recursively, creating a chain of generators.
Take a look:

print inspect.getsource(os.walk)

> os.walk to 'test' (test = os.walk()), that variable becomes a
> generator object that does not work in a for loop.

You'll have to provide more information. That should work fine if you
haven't already exhausted the generator.

> it's supposed to work in a generator function using 'yield', but I'm at
> a loss at how that all works.
>
> I suppose I should just stick with using the os.walk in the for loop,
> but I'd like to make sense of the whole thing. Please someone explain
> this to me?

A generator function is a function that uses the keyword "yield"
instead of "return" (an empty return statement is allowed). When you
call a generator function, the return value is a generator object.
Think of the generator function as a factory for generator objects. A
return in the generator function (implicit or with a "return"
statement) corresponds to the generator object raising StopIteration.

A generator object is an iterator. Specifically, it has the methods
__iter__ and "next" (in 3.x it's __next__), and "iter(genobject) is
genobject".

To be an iterable in general, it suffices to have either an __iter__
method or a __getitem__ method. Here are the glossary definitions:

http://docs.python.org/glossary.html#term-iterable
http://docs.python.org/glossary.html#term-iterator

Also, here is the PEP for simple generators:

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


Re: [Tutor] using multiprocessing efficiently to process large data file

2012-09-02 Thread eryksun
On Sun, Sep 2, 2012 at 2:41 AM, Alan Gauld  wrote:
>
>>  if __name__ == '__main__': # <-- required for Windows
>
> Why?
> What difference does that make in Windows?

It's a hack to get around the fact that Win32 doesn't fork(). Windows
calls CreateProcess(), which loads a fresh interpreter.
multiprocessing then loads the module under a different name (i.e. not
'__main__'). Otherwise another processing Pool would be created, etc,
etc.

This is also why you can't share global data in Windows. A forked
process in Linux uses copy on write, so you can load a large block of
data before calling fork() and share it. In Windows the module is
executed separately for each process, so each has its own copy. To
share data in Windows, I think the fastest option is to use a ctypes
shared Array. The example I wrote is just using the default Pool setup
that serializes (pickle) over pipes.

FYI, the Win32 API imposes the requirement to use CreateProcess(). The
native NT kernel has no problem forking (e.g. for the POSIX
subsystem). I haven't looked closely enough to know why they didn't
implement fork() in Win32.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2.7.3 generator objects

2012-09-02 Thread eryksun
On Sun, Sep 2, 2012 at 3:09 AM, Ray Jones  wrote:
>
> But didn't I read somewhere that you can reset an iterator to go through
> the whole process again?

You could implement that ability in your own objects, but it's not
part of the protocol.

I forgot to mention generator expressions. This is an expression
(typically requiring parentheses) that evaluates to a generator
object. You can omit the parentheses if it's the only argument in a
call. For example:

>>> g = (chr(i) for i in range(65, 69))
>>> ", ".join(g)
'A, B, C, D'

>>> ", ".join(chr(i) for i in range(65, 69))
'A, B, C, D'

http://docs.python.org/glossary.html#generator%20expression
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Running a script in the background

2012-09-02 Thread eryksun
On Sun, Sep 2, 2012 at 10:06 AM, Walter Prins  wrote:
>
> nohup python myscript.py &
>
> Then you can close the terminal afterwards.  "nohup" means"no hangup".
>  It tells the system that the python process launched as a result of
> this command should not be terminated when its parent shell is
> terminated.

bash starts a background process in a new group. When you close the
terminal (SIGHUP), the OS won't forward the HUP to this group, but
bash defaults to forwarding it. If you "exit", on the other hand, bash
won't be around to forward anything. To skip forwarding HUP in any
case, just "disown" the process.

If you use nohup, you can avoid creating nohup.out files if you
redirect stdout somewhere such as a log file or /dev/null.

If instead you just "exit" or use "disown", remember to redirect both
stdout and stderr so the program doesn't try to write to a
non-existent tty.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running a script in the background (this time Cc'd to the list)

2012-09-02 Thread eryksun
On Sun, Sep 2, 2012 at 9:04 PM, William R. Wing (Bill Wing)  
wrote:
>
> Apple's mechanism for launching applications at login is picky
> about what it will accept as a legitimate application to add to
> the list.

Here's an Ask Different (Apple Stack Exchange) answer with a template
for a launchd plist:

http://apple.stackexchange.com/a/822

I don't use OS X, so I can't offer anymore held than that.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running a script in the background

2012-09-04 Thread eryksun
On Mon, Sep 3, 2012 at 10:21 PM, Dwight Hutto  wrote:
> What's wrong with:
>
> But each OS(BIOS handler) has a way of providing/accepting instructions to
> the processor, which is constantly procedural. This has to have a terminal
> at some point.

What do you mean by 'terminal' in this context? A terminal is a device
(possibly virtual) for entering data and displaying output from a
computer.

cron isn't scheduling active processes/threads like the kernel does.
Every minute, it checks a job table and possibly starts one or more
programs. This is an efficient way to schedule a large number of
programs to run at various times throughout the day, week, and month.
Otherwise all of these programs would need to have at least a stub
that runs as a daemon, wasting memory and CPU resources needlessly.

Scheduling processes is an entirely different domain. In a preemptive
multitasking system, it's up to the kernel to schedule access to the
CPU. Each process (or thread on some systems) is assigned a quantum of
ticks. The ticks are based on a hardware timer. Periodically (e.g. 10
milliseconds) this timer triggers a hardware interrupt that enables
the kernel to preempt the current thread. The kernel schedules the
next (possibly the same) process to run, based on a priority scheme,
out of the pool of ready processes.

Preempting a running process requires a context switch, i.e. the
process control block (e.g. PID, priority, execution state such as
running/ready/waiting, CPU number/registers/flags, virtual memory,
signal handlers, I/O, credentials, accounting, etc) has to be saved on
the current stack. Then the PCB of the next process is loaded; the CPU
state is restored; and execution resumes seamlessly.

Here's the task_struct used by the Linux scheduler (lines 1235-1593):

http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/linux/sched.h#l1235

> What is meant by behind a console, and running without a console, just a
> window system, and a file set that deals with instructions/errors based on
> BIOS input/output?

Windows NT typically boots without a console. It doesn't have virtual
terminals, but you can run console programs in a Win32 console managed
by the Client/Server Runtime Subsystem (csrss.exe)

In contrast, Linux boots in console mode. Typically it maps the
"console" (boot parameter) to the current virtual terminal (i.e.
/dev/tty0), but it could also be a dumb terminal or modem. It creates
several virtual terminals depending on the system configuration.
Debian has tty's 1-63 and runs getty on tty 1-6 to allow text-mode
logins and an Xorg display manager (e.g. lightdm) on tty7 for logging
in to a graphical desktop environment (e.g. xfce4).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a shortcut in windows

2012-09-04 Thread eryksun
On Mon, Sep 3, 2012 at 9:57 PM, Garry Willgoose
 wrote:
> I want to put a shortcut onto the desktop in windows (XP and later) in
>
> AttributeError: function 'CreateSymbolicLinkW' not found

A simple search for "msdn CreateSymbolicLink" leads to the following page:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363866.aspx

Scroll down to the requirements, and you'll see that
CreateSymbolicLink was added to Kernel32 (and NTFS) in Windows Vista
(NT 6.x). Kernel32 in Windows XP (NT 5.x) doesn't have that function;
hence the AttributeError.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode? UTF-8? UTF-16? WTF-8? ;)

2012-09-05 Thread eryksun
On Wed, Sep 5, 2012 at 5:42 AM, Ray Jones  wrote:
> I have directory names that contain Russian characters, Romanian
> characters, French characters, et al. When I search for a file using
> glob.glob(), I end up with stuff like \x93\x8c\xd1 in place of the
> directory names. I thought simply identifying them as Unicode would
> clear that up. Nope. Now I have stuff like \u0456\u0439\u043e.

This is just an FYI in case you were manually decoding. Since glob
calls os.listdir(dirname), you can get Unicode output if you call it
with a Unicode arg:

>>> t = u"\u0456\u0439\u043e"
>>> open(t, 'w').close()

>>> import glob

>>> glob.glob('*')  # UTF-8 output
['\xd1\x96\xd0\xb9\xd0\xbe']

>>> glob.glob(u'*')
[u'\u0456\u0439\u043e']

Regarding subprocess.Popen, just use Unicode -- at least on a POSIX
system. Popen calls an exec function, such as posix.execv, which
handles encoding Unicode arguments to the file system encoding.

On Windows, the _subprocess C extension in 2.x is limited to calling
CreateProcessA with char* 8-bit strings. So Unicode characters beyond
ASCII (the default encoding) trigger an encoding error.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unicode? UTF-8? UTF-16? WTF-8? ;)

2012-09-05 Thread eryksun
On Wed, Sep 5, 2012 at 10:51 AM, Ray Jones  wrote:
>
> subprocess.call(['dolphin', '/my_home/testdir/\u044c\u043e\u0432'])
>
> Dolphin's error message: 'The file or folder
> /my_home/testdir/\u044c\u043e\u0432 does not exist'

"\u" only codes a BMP character in unicode literals, i.e. u"unicode
literal". You forgot the 'u'.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Student Class

2012-09-05 Thread eryksun
On Wed, Sep 5, 2012 at 7:43 PM, Ashley Fowler
 wrote:
>
> class Student:

Are you using Python 3? If not, Student should explicitly inherit from object.

> def __init__(self, first_name, last_name, numCredits, gpa):
> self.first_name = first_name
> self.last_name = last_name
> self.numCredits = numCredits
> self.gpa = gpa

Your requirements specify firstName and lastName, not first_name and last_name.

> def getFirstname(self):
> return self.first_name

All of the function definitions below __init__ need to be dedented one
level. You have them defined in __init__.

> def setFirstname(self, first_name):
> self.first_name = first

'first' isn't defined. You named the parameter "first_name".

> def setLastname(self, last_name):
> self.last_name = last

Neither is 'last' defined.

> def setNumcredits(self, numCredits):
> self.NumCredits = credit

Neither is 'credit' defined. Plus this method creates a new attribute
named NumCredits. The name in __init__ is numCredits.

> def __str__(self):
> return (self.first_name, self.last_name, self.numCredits, 
> self.gpa)

The __str__ method absolutely needs to return a string. Use string
formatting via 'format' or the old modulo formatting.

http://docs.python.org/py3k/library/string.html#format-examples
http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting-operations
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Formatting questions regarding datetime.isoformat()

2012-09-06 Thread eryksun
On Wed, Sep 5, 2012 at 11:00 PM, staticsafe  wrote:
>
> In [68]: showinfo['RFC3339']
> Out[68]: '2012-09-10T21:00:00-4:00'

You can parse the ISO format using the dateutil module:

http://labix.org/python-dateutil

>>> from dateutil.parser import parse
>>> show_time = parse('2012-09-10T21:00:00-4:00')

This produces a time-zone aware datetime object. You can pass its
tzinfo to datetime.now() to get the current time as an aware datetime
object:

>>> now = datetime.now(show_time.tzinfo)

Or you could use datetime.now(dateutil.tz.tzutc()) for a UTC tzinfo.
It doesn't matter if you're only interested in the timedelta.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hey.need help on time

2012-09-06 Thread eryksun
On Thu, Sep 6, 2012 at 3:21 AM, Keitaro Kaoru  wrote:
> been trying to change this so it wont use my server time. but my
> actual time here in the us.EST. havent been able to figure it out
>
> def sstime(user, body, m):
> os.environ['TZ'] = 'US/Eastern'

Now just call time.tzset(), and it should work.

> tstr1 = time.strftime("%a, %b-%d-%Y", time.tzset())

I don't know what you're doing here. strftime() uses localtime() if
you don't provide a time tuple. time.tzset() just returns None.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Formatting questions regarding datetime.isoformat()

2012-09-06 Thread eryksun
On Thu, Sep 6, 2012 at 3:50 AM, Dave Angel  wrote:
> On 09/06/2012 03:35 AM, eryksun wrote:
>> 
>> Or you could use datetime.now(dateutil.tz.tzutc()) for a UTC tzinfo.
>> It doesn't matter if you're only interested in the timedelta.
>
> Actually, it can matter.  Whenever possible, produce all times as UTC
> and do your manipulations (eg. difference) in that space.  With local
> time, there are times that don't exist and times that are ambiguous, one
> in the fall and one in the spring, due to setting the clock forward or back.
>
> i don't know for sure if it matters here, but I've found it's better to
> work that way.

These are aware datetime objects. See the datetime docs for supported
operations, case 3, timedelta = datetime1 - datetime2:

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


Re: [Tutor] Doing the same thing twice. Works first time but not the second.

2012-09-06 Thread eryksun
On Fri, Aug 17, 2012 at 4:11 PM, Matthew Love
 wrote:
> def inventory(self):
> self.inventory = ["torch"]
> return self.inventory

What is 'self.inventory' before you call inventory(), and what is it
afterwards? A quick fix would be to name the list "_inventory" and
return self._inventory.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hey.need help on time

2012-09-06 Thread eryksun
On Thu, Sep 6, 2012 at 4:25 AM, Ray Jones  wrote:
>
> Why the additional step of calling time.tzset()? Once os.environ['TZ']
> is set, I've found that time.localtime() responds to the new TZ without
> anything extra. Is that a difference in versions (2.7.3 here)?

It shouldn't strictly be necessary using glibc. I checked the source.
glibc localtime calls __tz_convert() on each call, which calls
tzset_internal() to update the TZ setting. However, I don't think
that's guaranteed in all C runtimes. As the Python docs say,
"[c]hanging the TZ environment variable without calling tzset *may*
change the local timezone used by methods such as localtime, but this
behaviour should not be relied on."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib2.urlopen(....., timeout=)

2012-09-07 Thread eryksun
On Fri, Sep 7, 2012 at 11:32 AM, Dave Angel  wrote:
>
> I'm curious why the docstring says...  timeout = 
> but have no clues for you.

socket._GLOBAL_DEFAULT_TIMEOUT is an object(). In other words, it's an
"object object". Also, that's not from the docstring but the call
signature. The function doesn't have a docstring.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pygame and TkInter

2012-09-09 Thread eryksun
On Sun, Sep 9, 2012 at 5:58 PM, Greg Nielsen  wrote:
> Hello Tutor,
>
>  Quick question. Working on a new game and want to build in a GUI.
> TkInter seems to fit the bill except for one small item. Both Pygame and
> TkInter create windows. (Screen in Pygame, and the Root widget in TkInter)
> Is there a way to combined these two so I can use all my Pygame sprites and
> objects and any TkInter bars and buttons all in one screen? My best guess
> would be something along this line.

I found this answer on Stack Overflow:

http://stackoverflow.com/questions/8584272/using-pygame-features-in-tkinter

It depends on initializing the SDL engine to use the window ID from a
Tk frame, based on the 'SDL_WINDOWID' environment variable.

I added a check of sys.platform == "win32" for the suggested use of
'SDL_VIDEODRIVER' on Windows, but I haven't verified that this is
required or that it works.

Instead of a while loop that manually pumps both SDL and Tk, I
switched to using the Tk mainloop. The frame's "after" method is used
to run pygame_loop every 5 ms, which updates the screen. You'll have
to experiment with how this affects pygame's ability to grab
keyboard/click events. You might have to use Tk for input instead.

Just to show the event loop in action, I added two buttons to
increment/decrement the current step size, tied to the incr_step
method.


import sys
import os
import Tkinter as tkinter
import pygame

class Game(object):
def __init__(self, root, w, h):
self.root = root
self.width = w
self.height = h

# Tk init
self.frame = tkinter.Frame(root, width=w, height=h)
self.frame.grid(row=0, columnspan=2)
self.button1 = tkinter.Button(
root, text='-', command=lambda: self.incr_step(-1))
self.button1.grid(row=1, column=0)
self.button2 = tkinter.Button(
root, text='+', command=lambda: self.incr_step(1))
self.button2.grid(row=1, column=1)
root.update()

# pygame init
os.environ['SDL_WINDOWID'] = str(self.frame.winfo_id())
if sys.platform == "win32":
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.display.init()
self.screen = pygame.display.set_mode((w, h))
self.bg_color = pygame.Color(0,0,0)
self.fg_color = pygame.Color(255,255,255)
self.position = 0
self.step = 1

self.game_loop()

def game_loop(self):
self.screen.fill(self.bg_color)
self.position = (self.position + self.step) % self.width
coord = self.position, self.height // 2
pygame.draw.circle(self.screen, self.fg_color, coord, 20)
pygame.display.flip()

self.frame.after(5, self.game_loop)

def incr_step(self, inc):
self.step += inc

if __name__ == "__main__":
root = tkinter.Tk()
game = Game(root, 200, 200)
root.mainloop()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pygame and TkInter

2012-09-10 Thread eryksun
On Mon, Sep 10, 2012 at 12:34 PM, Greg Nielsen
 wrote:
> I will admit that the whole Blender project is cool, but Jerry has a point.
> All I wanted to know was how to get Pygame and TkInter to work together, and
> I got a solid answer about 12 hours ago. (Check out Eryksun's code, it's
> beyond cool, past awsome, and close to sexy.) As such, I am considering this
> post closed. Thank you all for your help.

The credit goes to the answer on Stack Overflow. I just modified it a bit.

Another option would be to use a pygame GUI toolkit, especially if you
want full screen mode. The pygame wiki discusses two that I suppose
are the most popular:

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


Re: [Tutor] Seeing response from authorization page with urllib2

2012-09-11 Thread eryksun
On Tue, Sep 11, 2012 at 4:29 AM, Ray Jones  wrote:
>
> But when I attempt to get any part of an authorization-required page
> using urllib2.urlopen(), I immediately receive the 401 error. Even the
> intended object variable is left in an undefined state, so using info()
> doesn't seem to work. How can I get that information from the server?

The HTTPError object has the information you want: url, code, reason,
headers -- along with the methods info, read, readline, and readlines.

http://docs.python.org/library/urllib2#urllib2.HTTPError

For example, if you catch the error as e, then you can look at
e.headers['www-authenticate'] to find the realm.

Basic HTTP Authentication:
http://docs.python.org/library/urllib2#examples

You can also use an HTTPPasswordMgrWithDefaultRealm with a catch-all
realm (realm=None):

pass_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
auth_handler = urllib2.HTTPBasicAuthHandler(pass_mgr)
auth_handler.add_password(
realm=None,
uri='https://mahler:8092/site-updates.py',
user='klem',
passwd='kadidd!ehopper')
opener = urllib2.build_opener(auth_handler)

Optionally, install the opener:

urllib2.install_opener(opener)

Or just use opener.open().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question

2012-09-11 Thread eryksun
On Tue, Sep 11, 2012 at 12:08 PM, Ashley Fowler
 wrote:
>
> I have a question. In a assignment it asks for me to do the following
> below...
>
> if "peek" then print the Student object at the beginning
>   of the list (using __str__) but don't remove it from
>   the list;
>
>
> Could you explain what it means?

The __str__ special method of an object will be called when passed to
the built-in str() constructor. This method is required to return a
string.

For example, here's a class with an __str__ method that prints
"calling __str__" to the screen in addition to returning the string
"eggs". This demonstrates some of the ways __str__ is implicitly
called.


class Spam:
def __str__(self):
print("calling __str__")
return "eggs"


>>> s = Spam()

>>> str(s)
calling __str__
'eggs'

>>> "spam and {}".format(s)
calling __str__
'spam and eggs'

>>> print(s)
calling __str__
eggs


Make sure __str__ returns a suitable string representation of the
student. The assignment should specify the string formatting of
Student objects.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Constructing a object

2012-09-11 Thread eryksun
On Tue, Sep 11, 2012 at 10:23 PM, Ashley Fowler
 wrote:
> How do you construct a object using variables?
>
> For instance I have to construct a student object, by first prompting the
> user to enter variables for the Student class (their first and last names,
> credits and gpa) then construct a Student object using those variables.

You need to write a function that gets user input and converts it
where appropriate (e.g. int, float).


def make_student_from_input():

# input and convert
first_name = ...
last_name = ...
num_credits = ...
gpa = ...

return Student(first_name, last_name, num_credits, gpa)


This assumes the Student initializer (__init__) is as follows:


class Student(object):

def __init__(self, first_name, last_name, num_credits, gpa):
# ...


The constructor supplies the 'self' argument in position 0. The rest,
if supplied as "positional" arguments, need to be provided in exactly
the same order as the argument list. The names do not matter. Whatever
argument is in position 1 will be assigned to the local variable
'first_name'. Whatever argument is in position 4 will be assigned to
the local variable 'gpa'.

On the other hand, if you supply each argument name as a "keyword",
you're free to list them in any order. For example:

first_name, last_name = 'John', 'Doe'
num_credits, gpa = 60, 3.5

student = Student(
gpa=gpa,
num_credits=num_credits,
last_name=last_name,
first_name=first_name)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-12 Thread eryksun
On Wed, Sep 12, 2012 at 1:17 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
 wrote:
> How to produce  a musical note of given frequency,volume and duration in
> Python.

Do you just want a sinusoidal, pure tone, or do you need a more
complex waveform?

For a pure tone you can use math.sin(2 * math.pi * f * dt * n). The
sin is a function of radians. 2*math.pi is the constant number of
radians/cycle, f is the frequency in cycles/second (Hz), dt is your
sampling step size in seconds/sample, and n is the index in samples.
Multiply all those units together and you get an argument in radians.
The sin function just samples the y value of the (x,y) coordinate as
you step around a unit circle in the given radians/step. You can scale
the amplitude and iterate for however many steps you want.

The discrete frequency is f*dt in cycles/sample, and the discrete
period is 1/(f*dt) in samples/cycle. You need this to be at least 2
samples/cycle to uniquely define a sinusoid. In other words, 1/dt >=
2*f.  The term 1/dt is the sampling rate fs. Two times the highest
frequency of interest (2*f) is called the Nyquist rate. Sampling a a
sinusoid at a sub-Nyquist rate will alias to a different frequency
(the spectrum of a sampled signal goes from 0 to fs/2; higher
frequencies fold back around). Search the web for animations that show
aliasing (typically with clock hands, fan blades, etc). If your sample
rate is 48000 Hz, you can create tones up to 24000 Hz, which is beyond
the spectrum of human hearing (20Hz - 20kHz).

The example below creates a generator for 1 cycle of a tone at a
sample rate of 48 ksps. Next it packs the floating point values as
bytes using struct.pack(). The resulting byte string is written to a
PyAudio stream. PyAudio is a C extension wrapper around the
cross-platform PortAudio library:

http://people.csail.mit.edu/hubert/pyaudio


import math
import struct
import pyaudio

def play_tone(frequency, amplitude, duration, fs, stream):
N = int(fs / frequency)
T = int(frequency * duration)  # repeat for T cycles
dt = 1.0 / fs
# 1 cycle
tone = (amplitude * math.sin(2 * math.pi * frequency * n * dt)
for n in xrange(N))
# todo: get the format from the stream; this assumes Float32
data = ''.join(struct.pack('f', samp) for samp in tone)
for n in xrange(T):
stream.write(data)

fs = 48000
p = pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)

# play the C major scale
scale = [130.8, 146.8, 164.8, 174.6, 195.0, 220.0, 246.9, 261.6]
for tone in scale:
play_tone(tone, 0.5, 0.75, fs, stream)

# up an octave
for tone in scale[1:]:
play_tone(2*tone, 0.5, 0.75, fs, stream)

stream.close()
p.terminate()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-12 Thread eryksun
On Wed, Sep 12, 2012 at 4:49 AM, Dwight Hutto  wrote:
>
> pyaudio is compatible with python 3.0(just in case the OP has that
> version, and it doesn't look like on the main site it has it listed,
> nor if it's 64 bit, etc.

I'm surprised they don't have an official Python 3 port yet. I see now
the git repo hasn't seen a commit in 2 years. About a year ago I
ported PyAudio to Python 3 for my own use, based on the guide for
porting C extensions I found here:

http://python3porting.com/cextensions.html

But it was only a quick update (actually kind of tedious) of
_portaudiomodule.c. Christoph Gholke has a port online that modifies
setup.py as well. I prefer his version over my own (more eyes, fewer
bugs). It just needs a small modification for Linux (see below).

PyAudio for Windows Python 2.5 to 3.2 (32-bit and 64-bit):
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

Pre-release pygame for Python 3, in case you want to use SDL as a tone
generator:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Debian Build

I downloaded Christoph's port of PyAudio from the site above and built
it on my Debian Linux box. I needed to install portaudio19-dev. You'll
also need python3-all-dev if it's not already installed. I had to
comment out line 138 of setup.py (data_files=data_files). Christoph
added it for the Windows build, but it's not required for Linux. I
only got a few compiler warnings running "sudo python3 setup.py
install". YMMV. The tone-playing script works fine, after making a few
modifications to use range instead of xrange and b''.join instead of
''.join.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] convert ascii to binary

2012-09-12 Thread eryksun
On Wed, Sep 12, 2012 at 7:20 AM, Aaron Pilgrim  wrote:
> Hello,
> I am trying to write a small program that converts ascii to binary.
>
> I tried using the python reference library section 18.8 but had
> trouble understanding how to make it work.
>
>  Here is the code I am currently trying to use:
>
> def main():
> import binascii
> myWord = input("Enter the word to convert..")
> #convert text to ascii
> for ch in myWord:
> print(ord(ch))
> #convert ascii to binary
> binascii.a2b_uu(ord(ch))


I'm not sure what you want, based on the code above. uuencoding is
meant for sending data through 7-bit channels like email and
newsgroups. For example:

>>> binascii.b2a_uu('\x81\x82')  # 8-bit binary to 7-bit ascii
'"@8( \n'
>>> binascii.a2b_uu('"@8( \n')   # 7-bit ascii back to 8-bit
'\x81\x82'

http://docs.python.org/py3k/library/binascii.html#binascii.a2b_uu

Do you instead want an ASCII bitstring (i.e. 1s and 0s)?

These will raise a UnicodeError if the string isn't ASCII.

# Python 3
def iter_bin(s):
sb = s.encode('ascii')
return (format(b, '07b') for b in sb)

# Python 2
def iter_bin(s):
sb = s.encode('ascii')
return (format(ord(b), '07b') for b in sb)

For example:

>>> for s in iter_bin("Spam"):
...   print(s)
...
1010011
111
111
1101101

>>> print(*iter_bin("Spam"), sep='')  # Python 3
1010011111101101

>>> print ''.join(s for s in iter_bin("Spam")) # Python 2
1010011111101101
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setting a timer

2012-09-12 Thread eryksun
On Wed, Sep 12, 2012 at 1:56 PM, Matthew Ngaha  wrote:
> i have a way to set a timer for creating new objects copied from a
> book but as i have started my next exercise in a very different way, i
> want to avoid that math method/formula as it will cause me to
> rearrange some classes totally...
>
> i tried a simple method but its not working. any ideas?

threading has a Timer:

http://docs.python.org/library/threading#timer-objects

>>> def print_msg(msg):
... print msg

>>> t = threading.Timer(1, print_msg, ('Spam',))
>>> t.start()
>>> Spam

>>> t = threading.Timer(3, print_msg, ('Spam',))
>>> t.start(); t.join()  # waiting...
Spam

There's also the sched module, but only if you're not using threads:

http://docs.python.org/library/sched

>>> s = sched.scheduler(time.time, time.sleep)

>>> e = s.enter(3, 1, print_msg, ('Spam',))
>>> s.run()
Spam

>>> e = s.enterabs(time.time() + 5, 1, print_msg, ('Spam',))
>>> s.run()
Spam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sigh first real python task

2012-09-13 Thread eryksun
On Wed, Sep 12, 2012 at 11:06 PM, Mike S  wrote:

> try:
> ret = subprocess.call("smbclient //reportingmachine/Dashboard/; put
> %s\" -U Username%Password" % (fileName), shell=True)
> if ret < 0:
> print >>sys.stderr, "Child was terminated by signal", -ret
> else:
> os.unlink(path+fileName)
> except OSError, e:
> print >>sys.stderr, "Execution failed:", e

I don't see a need to run this through the shell. I'd just use a list
of arguments.

Do you only want to delete the file if smbclient is killed by a
signal? What if it fails for some other reason with a return code of
1? In the example below I assume the file is removed only if the put
command succeeds.

>From what I gather using "man smbclient", the basic template here is
the following:

smbclient servicename password -U username -c "put filename"

The code below uses subprocess.check_call, which raises a
CalledProcessError if the return code is non-zero. The variables
username, password, filename, and path are strings.

import sys
import os
from subprocess import check_call, CalledProcessError

servicename = "//reportingmachine/Dashboard/"

try:
check_call(["smbclient", servicename, password, "-U", username,
"-c", "put %s" % filename])

os.unlink(os.path.join(path, filename))

except CalledProcessError as e:
print >>sys.stderr, "call failed:", e

except OSError as e:
print >>sys.stderr, "unlink failed:", e
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-13 Thread eryksun
On Thu, Sep 13, 2012 at 11:48 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
 wrote:
>
> As far as programming volume is concerned winsound.Beep has only frequency
> and duration. pyaudio module appears to have provision for volume control.

You should be able to add a wave header to a raw byte string. For example:


import wave
import winsound
from cStringIO import StringIO

def get_wave(data):
f = StringIO()
w = wave.open(f, 'w')
w.setnchannels(1) # mono
w.setsampwidth(2) # 2 bytes
w.setframerate(48000) # samples/second
w.writeframes(data)
return f.getvalue()


Then play the sound like this (_untested_):


wave_data = get_wave(data)
windsound.PlaySound(wave_data, winsound.SND_MEMORY)



I've modified my previous script to add simple polyphonic sound. I
included a basic square wave and a sawtooth wave, plus a random
polyphonic wave. It's the kind of sound you might hear in a cheesy
greeting card. Also, since you're looking to use winsound.PlaySound(),
I changed the byte packing to use a little endian signed short
(int16). I think that's what Windows wave files use.

My demo below still uses pyaudio, which wraps the cross-platform
PortAudio library. To me it seems like a better solution than relying
on the standard library (e.g. the standard lib uses OSS on Linux; I
don't even have that installed).

Hopefully it performs OK on your computer. Using NumPy arrays would
speed up the number crunching.


import pyaudio
from math import sin, pi
from struct import pack
from random import sample, randrange

def polywave(freqs, amps, f0, fs):
"""create a polyphonic waveform

freqs: sequence of frequencies (Hz)
amps: sequence of amplitudes
f0: fundamental frequency (Hz)
fs: samples/second

output is normalized to the range [-1,1].
"""
N = int(fs / f0)
rad_step = 2 * pi / fs  # radians / (Hz * sample)
wave = [0.0] * N
for f, a in zip(freqs, amps):
for n in xrange(N):
wave[n] += a * sin(rad_step * f * n)
maxamp = abs(max(wave, key=lambda x: abs(x)))
return [samp / maxamp for samp in wave]

def packwave(wave, vol=1, duration=None, fs=None):
"""pack a waveform as bytes (int16)

wave: sample sequence, |mag| <= 1
vol: optional volume scale, |mag| <= 1
duration: optional loop time (seconds)
fs: samples/second

fs is required to set duration.
"""
scale = min(vol * 32767, 32767)
ncycles = 1
if not (duration is None or fs is None):
ncycles = int(round(1.0 * duration * fs / len(wave)))
data = b''.join(pack('http://en.wikipedia.org/wiki/
# Equal_temperament#Calculating_absolute_frequencies

note = lambda n: 440 * (2**(1/12.)) ** (-21 + n)

scale = [note(n) for n in range(12)]
rscale = [note(13-n) for n in range(12)]
vols = [0.2 + 0.05*n for n in range(len(scale))]
rvols = list(reversed(vols))

def play_scale(scale, vols, wave_func, master_vol=1):
duration = 0.5
nharmonics = 30
for f0, vol in zip(scale, vols):
freqs, amps = wave_func(nharmonics, f0, fs)
wave = polywave(freqs, amps, f0, fs)
data = packwave(wave, master_vol * vol, duration, fs)
play(data, stream)
sleep(0.5)

play_scale(scale, vols, make_square, 0.5)
play_scale(rscale, rvols, make_saw, 0.5)
play_scale(scale, vols, make_rand, 0.75)
play_scale(rscale, rvols, make_rand, 0.75)

stream.close()
p.terminate()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


  1   2   3   4   5   6   7   >