Re: [Tutor] ctypes and arrays of pointers

2011-10-15 Thread Albert-Jan Roskam


 Hi,

Got it already. Here's some incomplete code that shows how it could be done:
# Pointer to array of pointers to labels 
labelsPtr = ctypes.pointer((ctypes.POINTER(ctypes.c_char_p) * 
MAX_ARRAY_SIZE)()) 
retcode = self.theLib.GetValueLabels(self.fh, varName, valuesPtr, labelsPtr, 
numLabelsPtr)
labels = [unicode(labelsPtr.contents[0][i], "utf-8") for i in 
range(numLabels.value)] 

Thought I might share this with you ;-)


Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~


>
>From: Albert-Jan Roskam 
>To: Python Mailing List 
>Sent: Thursday, October 13, 2011 9:25 PM
>Subject: [Tutor] ctypes and arrays of pointers
>
>
>Hi,
>
>
>I have a question about ctypes. I am trying to call a C function but I am not 
>able to construct the arguments in ctypes. I need to construct two pointers. 
>Each is a pointer to an array of pointers to character values. I did it like 
>this (I tried numerous ways; this seemed the cleanest way):
 MAXSIZE = 10
 valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
 valueArrayPtr
>
 valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
 valueArrayPtrPtr
>
 
>
>
>
>But this yields an error: : Don't know how to 
>convert parameter ..
>
>
>Any idea what I am doing wrong? Below is more complete code.
>And once it works, can I simply use the ctypes as an iterable to retrieve the 
>values?
>
>
>
>Thanks in advance!
>Albert-Jan
>
>
>
>import ctypes
>
>def getValueLabels(self, varName):
>
>    MAXSIZE = 10
>
>    # Pointer to array of pointers to values
>    valueArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>    valueArrayPtrPtr = ctypes.POINTER(valueArrayPtr)
>    
>    # Pointer to array of pointers to labels
>    labelArrayPtr = ctypes.POINTER(ctypes.c_char_p) * MAXSIZE
>    labelArrayPtrPtr =
 ctypes.POINTER(labelArrayPtr)
>
>    # Pointer to number of values or labels
>    numLabels = ctypes.c_int()
>    numLabelsPtr = ctypes.byref(numLabels)
>    
>    # call C function with the following prototype:
>    # int GetValueLabels(int handle, const char *varName, char ***values, char 
>***labels, int *numLabels)
>    retcode = self.theLib.GetValueLabels(self.fh, varName, valueArrayPtrPtr, 
>labelArrayPtrPtr, numLabelsPtr)
>    # yields ArgumentError: argument 3: : Don't 
>know how to convert parameter 3
>
>
>Description
>
>This function gets the set of labeled values and associated labels for a short 
>string
>variable. The number of values is returned as *numLabels. Values are stored 
>into an
>array of *numLabels pointers, each pointing to a char string containing a
 nullterminated
>value, and *values is set to point to the first element of the array. Each 
>value
>string is as long as the variable. The corresponding labels are structured as 
>an array of
>*numLabels pointers, each pointing to a char string containing a 
>null-terminated label,
>and *labels is set to point to the first element of the array. 
>Parameter Description
>handle Handle to the data file
>varName Variable name
>values Pointer to array of pointers to values
>labels Pointer to array of pointers to labels
>numLabels Pointer to number of values or labels
>
> 
>Cheers!!
>Albert-Jan
>
>
>~~
>All right, but apart from the sanitation, the medicine, education, wine, 
>public order, irrigation, roads, a fresh water system, and public health, what 
>have the Romans ever done for
 us?
>~~
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Windows vs Linux processing speed.

2011-10-15 Thread Emile van Sebille

On 10/14/2011 9:45 AM Tony Pelletier said...


I have a question regarding the speed of my program on linux in
comparison to windows.





I mainly use windows, but I tend to use Arch Linux at home.  When I run
it via windows, it's relatively slow and didn't really bother me, but
when I ran it at home I got through 15 or so and got a message back from
google saying  I was exceeding the allowed amount.  Or something to that
effect.  Basically, I was exceeding the 10 per second that's allowed.

So, my question is.  Why is it running so much faster on linux?


Following up on Steven's reference to the py-dev thread, here's the link 
to the patch that details the simple changes that Chris Withers applied 
to httplib to improve his testcase performance from ~20 minutes to <3 
seconds.


http://svn.python.org/view/python/trunk/Lib/httplib.py?r1=74523&r2=74655


Emile



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


[Tutor] Running a loop

2011-10-15 Thread Jason Barry
I am using Windows 7 and python 3.1. This is a block from a slot machine
code. It takes the random generated words and indicates if it wins or loses.
I can't figure out why it wants to print the last print statement 'Loser' no
matter if the elif statements are true.

 

import random

wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

wheel2=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

wheel3=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

 

wheel1index=''

wheel2index=''

wheel3index=''

 

 

#decide images for each slot

 

wheel1index=wheel1[random.randint(0, len(wheel1) - 1)]

wheel2index=wheel2[random.randint(0, len(wheel2) - 1)]

wheel3index=wheel3[random.randint(0, len(wheel3) - 1)]

 

print(wheel1index)

print(wheel2index)

print(wheel3index)



 



 

 

#gets the player bet

#Winning bets

winning=0

 

 

 

 

#winning combonations

def checkwin (wheel1index, wheel2index, wheel3index):

if wheel1index=='zombie' and wheel2index=='zombie' and wheel3index==
'zombie':

return print ('wins Zombies.')

 





elif wheel1index=='witch' and wheel2index=='witch' and wheel3index==
'witch':

print ('wins witch.')

  

 



elif wheel1index=='cat' and wheel2index=='cat' and wheel3index== 'cat':

print ('wins cats.')

 



  

elif wheel1index=='pumpkin' and wheel2index=='pumpkin' and wheel3index==
'pumpkin':

print ('wins pumpkins.')

   



elif wheel1index=='ghost' and wheel2index=='ghost' and wheel3index==
'ghost':

print ('wins ghosts.')



 

elif wheel1index=='candy' and wheel2index=='candy' and wheel3index==
'candy':

print ('wins candies.')

   



elif wheel1index=='witch' and wheel2index=='witch' and wheel3index==
'cat':

print ('wins witchs and cat.')

  

   

elif wheel1index=='cat' and wheel2index=='witch' and wheel3index==
'witch':

print ('wins witchs and cat.')



  

elif wheel1index=='witch' and wheel2index=='cat' and wheel3index==
'witch':

print ('wins witchs and cat.')



  

#pumpkin ghost winner

elif wheel1index=='pumpkin' and wheel2index=='pumpkin' and wheel3index==
'ghost':

print ('wins pumpkins and ghost.')



 

elif wheel1index=='ghost' and wheel2index=='pumpkin' and wheel3index==
'pumpkin':

print ('wins pumpkins and ghost.')



 

elif wheel1index=='pumpkin' and wheel2index=='ghost' and wheel3index==
'pumpkin':

print ('wins pumpkins and ghost.')



   

#single combo winner

elif wheel1index=='candy' and wheel2index=='pumpkin' and wheel3index==
'ghost':

print ('wins pumpkin, ghost, and candy.')





elif wheel1index=='candy' and wheel2index=='ghost' and wheel3index==
'pumpkin':

print ('wins pumpkin, ghost, and candy..')



   

elif wheel1index=='ghost' and wheel2index=='pumpkin' and wheel3index==
'candie':

print ('wins pumpkin, ghost, and candy.')

   

 

elif wheel1index=='ghost' and wheel2index=='candy' and wheel3index==
'pumpkin':

print ('wins pumpkin, ghost, and candy.')



  

elif wheel1index=='pumpkin' and wheel2index=='candy' and wheel3index==
'ghost':

print ('wins pumpkin, ghost, and candy.')



 

 

elif wheel1index=='pumpkin' and wheel2index=='ghost' and wheel3index==
'candy':

print('wins pumpkin, ghost, and candy.')



else:

print ('Loser')

   



   

  

checkwin (wheel1index, wheel2index, wheel3index)

 

 

Jason 

 

__

Re: [Tutor] Running a loop

2011-10-15 Thread Alan Gauld

On 15/10/11 16:58, Jason Barry wrote:


wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']



def checkwin (wheel1index, wheel2index, wheel3index):

if wheel1index=='zombie' and wheel2index=='zombie' and wheel3index==
'zombie':



The values are defined as uppercase(eg 'ZOMBIE')
but the tests are for lowercase ('zombie').

Those are two entirely different things so far as Python is concerned.

HTH,

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

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


[Tutor] Can I set LD_LIBRARY_PATH within a .py file?

2011-10-15 Thread Albert-Jan Roskam
Hello,

Can I set the LD_LIBRARY_PATH environment variable (on-the-fly) within a .py 
file? 

I would like to use an .so-file that lives in a non-standard location.


This does not work:

try:

   os.environ["LD_LIBRARY_PATH"]  += (":" + path)
except KeyError:
   os.environ["LD_LIBRARY_PATH"] = path

Currently, I can only run the program in the terminal:
export LD_LIBRARY_PATH=/home/dude/Desktop/test
python /home/dude/Desktop/testLoadLibLinux.py

This works (yaaayy!), but I'd like to run the .py file directly.
Is this possible? I also don't  like the fact that I can't test the .py file in 
Idle.

Perhaps a complicating factor is a bug in LD_LIBRARY_PATH
in Linux Ubuntu 10 (the version I'm 
using):https://bugs.edge.launchpad.net/ubuntu/+source/xorg/+bug/366728
https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/366728/comments/21

solution:

sudo gedit /etc/X11/Xsession.options
(change "use-ssh-agent" into "no-use-ssh-agent")


Thank you in advance for your thoughts!

 
Cheers!!
Albert-Jan


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


Re: [Tutor] Running a loop

2011-10-15 Thread Robert Sjoblom
> Subject: [Tutor] Running a loop
> I am using Windows 7 and python 3.1. This is a block from a slot machine
> code. It takes the random generated words and indicates if it wins or loses.
> I can't figure out why it wants to print the last print statement 'Loser' no
> matter if the elif statements are true.

Let's begin with saying "never mind that, you have other issues".
First of all, whitespace is great in moderation. I don't know if it's
your mail client or your code that's messed up, but the kind of
whitespace you're sending along is just horrible and nigh on
unreadable. This leads to the second issue: you're misspelling strings
and comparing uppercase strings to lowercase ones (which will
automatically fail).

> import random
>
> wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
>
> wheel2=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
>
> wheel3=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

You don't really need 3 different wheels if they're all identical,
instead having one wheel is enough:
wheel = ["ZOMBIE", "WITCH", "CAT" #and so on]

> wheel1index=''
> wheel2index=''
> wheel3index=''

> #decide images for each slot
> wheel1index=wheel1[random.randint(0, len(wheel1) - 1)]
> wheel2index=wheel2[random.randint(0, len(wheel2) - 1)]
> wheel3index=wheel3[random.randint(0, len(wheel3) - 1)]

Here you can just do wheel1index = wheel[random.randint(o, len(wheel)
- 1)], then wheel2index = wheel[... and so on.

> print(wheel1index)
> print(wheel2index)
> print(wheel3index)

> #winning combonations
> def checkwin (wheel1index, wheel2index, wheel3index):
>    if wheel1index=='zombie' and wheel2index=='zombie' and wheel3index==
> 'zombie':
>        return print ('wins Zombies.')
I suspect that it's your mail client that makes it look horrible. You
can also do:
if wheel1index == wheel2index == wheel3index:
return print("Wins {0}".format(wheel1index))
or something similar for all of the cases where each wheel shows the
same. However, you won't get any results unless you change the case to
match what you've actually assigned to the wheels (remember, we assign
"WITCH" and "CAT" but it checks for "witch" and "cat" and will never,
ever find them).


>    elif wheel1index=='ghost' and wheel2index=='pumpkin' and wheel3index==
> 'candie':
>
>        print ('wins pumpkin, ghost, and candy.')

Here you've misspelled "candy" as well as having it in the wrong case.
ANYWAY, I'm sure there's some better way to compare stuff like:
Pumpkin, Pumpkin, Ghost
Pumpkin, Ghost, Pumpkin
Ghost, Pumpkin, Pumpkin

(I mean, it's the SAME SITUATION, and the same goes for almost all the
rest of the elif structures), but I'm in a hurry so I can't really
come up with a good way to check for that. Possibly have one index (a
list with three "wheel" items) and do some cool list comprehensions on
it, as it is I'm 100% sure that your if-elif-else structure can be
optimized a lot.

Also, no need to have a new line between each elif, makes it hard to
read, and they're all part of the same code block.

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


Re: [Tutor] Can I set LD_LIBRARY_PATH within a .py file?

2011-10-15 Thread Hugo Arts
On Sat, Oct 15, 2011 at 9:51 PM, Albert-Jan Roskam  wrote:
> Hello,
> Can I set the LD_LIBRARY_PATH environment variable (on-the-fly) within a .py
> file?
> I would like to use an .so-file that lives in a non-standard location.
>
> This does not work:
> try:
>    os.environ["LD_LIBRARY_PATH"]  += (":" + path)
> except KeyError:
>    os.environ["LD_LIBRARY_PATH"] = path
> Currently, I can only run the program in the terminal:
> export LD_LIBRARY_PATH=/home/dude/Desktop/test
> python /home/dude/Desktop/testLoadLibLinux.py
> This works (yaaayy!), but I'd like to run the .py file directly.
> Is this possible? I also don't  like the fact that I can't test the .py file
> in Idle.
> Perhaps a complicating factor is a bug in LD_LIBRARY_PATH
> in Linux Ubuntu 10 (the version I'm using):
> https://bugs.edge.launchpad.net/ubuntu/+source/xorg/+bug/366728
> https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/366728/comments/21
> solution:
> sudo gedit /etc/X11/Xsession.options
> (change "use-ssh-agent" into "no-use-ssh-agent")
>
> Thank you in advance for your thoughts!
>
> Cheers!!
> Albert-Jan
>

Alright, I'm not going to pretend to be an expert on this one, a bit
of this is speculation and inference from what I know about dynamic
linking. In short, I don't think you can modify LD_LIBRARY_PATH on the
fly and have it actually work. The reason for this is that the linker
runs and finds all the libraries *before* the python process actually
starts. So by the time you go and modify the environment, all
libraries have already been linked, and your modified variable is
never even read by the linker.

So the best you can do is write a tiny wrapper to set LD_LIBRARY_PATH
and then run your actual script through it. Or you could set the
environment variable and then fork(), I suppose, since the child will
inherit the modified environment. But the wrapper is your simplest
option.

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


Re: [Tutor] Running a loop

2011-10-15 Thread Steven D'Aprano

Jason Barry wrote:

I am using Windows 7 and python 3.1. This is a block from a slot machine
code. It takes the random generated words and indicates if it wins or loses.
I can't figure out why it wants to print the last print statement 'Loser' no
matter if the elif statements are true.


Alan has already explained that you are comparing UPPERCASE strings to 
lowercase strings, and they are not equal.


A few other comments below:



import random
wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

wheel2=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

wheel3=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']


Rather than duplicating values each time, it is better to do this:

images = tuple('''ZOMBIE WITCH CAT GHOST CANDY PUMPKIN PUMPKIN
  CANDY GHOST CANDY'''.split())

wheel1 = list(images)
wheel2 = list(images)
wheel3 = list(images)

(Note: I call list(images) instead of just images to ensure that each of 
the wheels has its own independent list of images, rather than all 
sharing the same one.)


Checking for winning sets also can be simplified a lot:


def checkwin (wheel1index, wheel2index, wheel3index):
combo = [wheel1index, wheel2index, wheel3index]
if combo.count('ZOMBIE') == 3:
print('wins zombies')
elif combo.count('WITCH') == 3:
print('wins witchs')
elif combo.count('CAT') == 3:
print('wins cats')
elif combo.count('PUMPKIN') == 3:
print('wins pumpkins')
elif combo.count('GHOST') == 3:
print('wins ghosts')
elif combo.count('CANDY') == 3:
print('wins candies')
elif combo.count('WITCH') == 2 and combo.count('CAT') == 1:
print('wins witches and cat')
elif combo.count('PUMPKIN') == 2 and combo.count('GHOST') == 1:
print('wins pumpkins and ghost')
# check for single combo winner
elif set(combo) = set(['CANDY', 'PUMPKIN', 'GHOST']):
print('wins pumpkin, ghost and candy')
else:
print('loses')





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


Re: [Tutor] Windows vs Linux processing speed.

2011-10-15 Thread Steven D'Aprano

Steven D'Aprano wrote:

Very few program's speed are greatly dependent on raw processor speed. 
Processor speed is one of the great marketing gimmicks of all time. Of 
course it has *some* effect, but the bottleneck is almost never the CPU, 
and usually the speed of getting data and/or code out of RAM and onto 
the CPU and from their into the core for the instructions to be 


Sigh. /s/their/there

executed. CPU cache faults are really, really expensive, so the bigger 
the pipeline into the core, the fewer the cache faults.


And double sigh. Obviously I wasn't paying too much attention to what I 
was writing. Obviously the number of cache faults is determined by the 
size of the cache, not the size of the pipeline.


Generally speaking, a processor with a fast core but a small cache will 
not perform as well as a processor with a slower core but a bigger 
cache. Within reason -- obviously it depends on the nature of the code 
being executed, some code doesn't benefit much from a processor cache.





--
Steven

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


Re: [Tutor] Python Job Scheduling package

2011-10-15 Thread Steven D'Aprano

bod...@googlemail.com wrote:

Have you thought about writing your own? Others have posted some
useful links, but in all honesty you could hack something together to
achieve that in next to no time


Anyone can "hack something together" in next to no time, but getting a 
quality package that is well-written, thoroughly tested and debugged may 
take a bit longer.



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