signed to unsigned

2012-02-17 Thread Brad Tilley
In C or C++, I can do this for integer conversion:

unsigned int j = -327681234; // Notice this is signed.

j will equal 3967286062. I thought with Python that I could use struct
to pack the signed int as an unsigned int, but that fails:

>>> x = struct.pack("", line 1, in 
struct.error: integer out of range for 'I' format code

Is there an easy way in Python to do the same conversion that C or C++
code does? Thanks for any advice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: signed to unsigned

2012-02-17 Thread Brad Tilley
> Pack it as the actual type, then unpack it as the desired type:
>
> Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> from struct import pack, unpack
> >>> unpack('=I', pack('=i',-327681234))
>
> (3967286062,)
>
> I would think there's some more efficient way to do this though.
>
> Cheers,
> Chris


Thanks Chris! I was doing it backwards. I only have a few of these
right now, so performance isn't a concern. I appreciate the advice.

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


Re: signed to unsigned

2012-02-17 Thread Brad Tilley

> >>> 0x & -327681234
>
> 3967286062

Very nice! Thanks for that example. Unsigned long longs:

0x & -9151314442815602945
9295429630893948671L
-- 
http://mail.python.org/mailman/listinfo/python-list


module imports and memory usage

2004-11-30 Thread Brad Tilley
When memory usage is a concern, is it better to do:
from X import Y
or
import X
Also, is there a way to load and unload modules as they are needed. I 
have some scripts that sleep for extended periods during a while loop 
and I need to be as memory friendly as possible. I can post a detailed 
script that currently uses ~ 10MB of memory if anyone is interested.

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


Re: module imports and memory usage

2004-11-30 Thread Brad Tilley
Jp Calderone wrote:
On Tue, 30 Nov 2004 10:02:27 -0500, Brad Tilley <[EMAIL PROTECTED]> wrote:
When memory usage is a concern, is it better to do:
from X import Y
or
import X

  There is no difference.  If you are concerned about memory usage, you 
probably need to take a look at the data structures holding your application's 
data, and find a way to make them smaller (most commonly, this is done by 
selecting a new algorithm that works on a sparser data structure).  This isn't 
specific to Python, of course.

Also, is there a way to load and unload modules as they are needed. I 
have some scripts that sleep for extended periods during a while loop 
and I need to be as memory friendly as possible. I can post a detailed 
script that currently uses ~ 10MB of memory if anyone is interested.

  Not really, no.  Deleting all references to a module may cause the module 
object to be collected by the vm, but there is no guarantee that the memory it 
used will be released to the platform.  It's possible Python will re-use it for 
the next object it needs memory for, which may help your memory problems if you 
are loading many (_many_) more modules than you ever use at once.
  On a reasonable platform, if your program is sleeping, much of the memory it 
uses will be swapped out, especially if other processes need it.  Are you sure 
you have good reason to be concerned over the size of the program?
  Jp
Thanks for the info JP. No, I'm not sure that I have good reason to be 
concerned over memory. I usually think in terms of percentages. 10MB is 
an insignificant percentage on a machine that has 1GB of RAM, but 
perhaps more significant when only 64MB or 128MB is present.
--
http://mail.python.org/mailman/listinfo/python-list


Re: module imports and memory usage

2004-11-30 Thread Brad Tilley
Brad Tilley wrote:
When memory usage is a concern, is it better to do:
from X import Y
or
import X
Also, is there a way to load and unload modules as they are needed. I 
have some scripts that sleep for extended periods during a while loop 
and I need to be as memory friendly as possible. I can post a detailed 
script that currently uses ~ 10MB of memory if anyone is interested.

Thanks,
Brad
I discovered that when I wrap my code up in a function def and call it 
that it uses around 4.6 MB of RAM all the time... even while sleeping. 
However, when I don't put it in a function def it uses around 2.6MB of 
data when it executes and only 100KB while sleeping. Why is this?
--
http://mail.python.org/mailman/listinfo/python-list


Python 2.4 Uninstall Entry in WinXP Registry

2004-11-30 Thread Brad Tilley
Python 2.3 placed a registry key under:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Python2.3'
When this key was removed, Python no longer appeared in the Windows 'Add 
Remove Programs' list. We would remove this registry key to keep users 
from uninstalling the software accidentally.

Python 2.4 does not use this registry entry on the two machines I have 
installed it on... any tips on how to locate this?

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


Re: Python 2.4 Uninstall Entry in WinXP Registry

2004-12-01 Thread Brad Tilley
Martin v. Löwis wrote:
Brad Tilley wrote:
Python 2.3 placed a registry key under:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Python2.3' 

[...]
Python 2.4 does not use this registry entry on the two machines I have 
installed it on... any tips on how to locate this?

It's under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{82D9302E-F209-4805-B548-52087047483A} 

which is the product code of Python 2.4. Notice that Python 2.4.1
will use a different product code.
However, if you merely want to avoid that users remove the package,
you can set the ARPNOREMOVE property during installation, e.g.
msiexec /i python24.msi ARPNOREMOVE=1
You might want to set ARPNOMODIFY and ARPNOREPAIR as well.
If you cannot readily pass that property on the command line during
installation, you can use orca.exe (or a python script) to add this
property to the Property table of python24.msi.
If you have W2k or later, you can also set the "NoRemove" registry
value under the key above, to hide the remove feature after
installation.
HTH,
Martin
That's very helpful, thanks Martin! How do I set the install path to 
c:\Program Files?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.4 Uninstall Entry in WinXP Registry

2004-12-01 Thread Brad Tilley
Brad Tilley wrote:
Martin v. Löwis wrote:
Brad Tilley wrote:
Python 2.3 placed a registry key under:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Python2.3' 


[...]
Python 2.4 does not use this registry entry on the two machines I 
have installed it on... any tips on how to locate this?

It's under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{82D9302E-F209-4805-B548-52087047483A} 

which is the product code of Python 2.4. Notice that Python 2.4.1
will use a different product code.
However, if you merely want to avoid that users remove the package,
you can set the ARPNOREMOVE property during installation, e.g.
msiexec /i python24.msi ARPNOREMOVE=1
You might want to set ARPNOMODIFY and ARPNOREPAIR as well.
If you cannot readily pass that property on the command line during
installation, you can use orca.exe (or a python script) to add this
property to the Property table of python24.msi.
If you have W2k or later, you can also set the "NoRemove" registry
value under the key above, to hide the remove feature after
installation.
HTH,
Martin

That's very helpful, thanks Martin! How do I set the install path to 
c:\Program Files?
I found the documentation here:
http://python.fyxm.net/2.4/msi.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3000 and "Python Regrets"

2004-12-01 Thread Brad Tilley
Matt Gerrans wrote:
Anyway, what's to worry about?When the time comes just whip out a little 
script that converts Python 1.6 (or whatever you like) to Python3K; it will 
only take seven lines of P3K code. 


How about 'import classic'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.4 Uninstall Entry in WinXP Registry

2004-12-01 Thread Brad Tilley
Martin v. Löwis wrote:
Brad Tilley wrote:
I found the documentation here:
http://python.fyxm.net/2.4/msi.html

The original, of course, is at
http://python.org/2.4/msi.html
Regards,
Martin
Thanks Martin... going to a .msi was a great move... we can do fully 
automated, unattended installs now. I appreaciate your work.

P.S. Here's what my batch install file currently looks like:
msiexec /i python-2.4.msi /qb! ALLUSERS=1 ARPNOREMOVE=1 ARPNOMODIFY=1 
ARPNOREPAIR=1 ADDLOCAL=ALL TARGETDIR="C:\Program Files\Python24"

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


Re: Python Win32 Silent Install

2004-12-01 Thread Brad Tilley
Peter Hansen wrote:
... innocent and ignorant users who are concerned about finding
this thing called "Python" on their new machines, and most of
them seem curiously more interested in removing it than in discovering
what it actually is there for.
This has been my experience as well. I don't have millions or even 
thousands of users... only hundreds. However, this is the exact same 
reason that I try to hide Python installs and make them unremovable. I 
guess all of the Windows based Spyware and Adware have made people 
overly cautious... if it looks suspicious, then remove it. To the 
average user, the name 'Python' doesn't sound harmless either.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help on program!!!

2004-12-03 Thread Brad Tilley
Grant Edwards wrote:
On 2004-12-03, Max M <[EMAIL PROTECTED]> wrote:

That was also my impression.  Even the description of the
problem looks like it's just copied from the assignment, so
probably didn't even take the time to restate the problem in
his own words.

[...]

Hopefully his teacher doesn't know about Google, or he can be
expelled from school for using it.

And there's always the chance the somebody intentionally posted
a wrong answer just to teach somebody a lesson.  I'm not
accusing Max of this, but I've seen it done in the past.
Perhaps Max teaches the class ;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help on program!!!

2004-12-03 Thread Brad Tilley
Darth Haggis wrote:
I need help writing a program
You are to write a python program to accomplish the following:

  a.. Play a dice game of Craps using a random number generator to simulate
the roll of the dice, the code for the rolling of the dice should take place
in a user written module named rolldice.
  b.. The rules of the game are as follows:
a.. 1st roll, a score of 7 or 11 wins.
b.. 1st roll, a score of 2, 3, or 12 loses.
c.. 1st roll, any number other than those listed above becomes the goal
number, you must keep rolling until you roll the goal number again.  If a 7
is rolled before you goal number, you lose.
Make the game continuous, until you tell it to stop.  You can bet the same
amount, or change you bet after each win or loss.  Print the results of each
roll and the running total of winnings or loses.

plz help!!!
Here's everything *except* the betting part. I don't gamble ;)
import random
class rolldice:
def first(self):
win = [7,11]
lose = [2,3,12]
x = random.randint(1,6)
y = random.randint(1,6)
z = x+y
print x, "+", y, "=", z
if z in win:
print "You won on the first roll!!!"
return 0
elif z in lose:
print "You lost on the first roll!!!"
return 0
else:
return z
def second(self, goal):
if goal > 0:
lose = 7
print "The goal number is:", goal
while True:
x = random.randint(1,6)
y = random.randint(1,6)
z = x+y
print x, "+", y, "=", z
if z == lose:
print "You lost!!!"
return 0
elif z == goal:
print "You won!!!"
return z
else:
continue
play = rolldice()
play.second(play.first())
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help on program!!!

2004-12-03 Thread Brad Tilley
Max M wrote:
Dan Perl wrote:
That was also my impression.  Even the description of the problem 
looks like it's just copied from the assignment, so probably didn't 
even take the time to restate the problem in his own words.

But we're speculating.  Either way, this is not a normal request: "I 
need a program to do this, plz help!"  To the OP: you can't seriously 
expect us to actually write the program for you, what kind of help are 
you looking for?

This?
# -*- coding: latin-1 -*-
"""
  a.. Play a dice game of Craps using a random number generator to simulate
the roll of the dice, the code for the rolling of the dice should take 
place
in a user written module named rolldice.

  b.. The rules of the game are as follows:
a.. 1st roll, a score of 7 or 11 wins.
b.. 1st roll, a score of 2, 3, or 12 loses.
c.. 1st roll, any number other than those listed above becomes the goal
number, you must keep rolling until you roll the goal number again.  If a 7
is rolled before you goal number, you lose.
"""
import random
def rolldice():
dice1, dice2 = random.randint(1,6), random.randint(1,6)
print dice1, dice2
return dice1, dice2
first1, first2 = rolldice()
first = first1, first2
roll = 1
if first in (7, 11):
print 'Congratulations, you win in roll %s' % roll
elif first in (2,3,7):
I think you mean 'elif first in (2,3,12):'
print 'Too bad, you loose in roll %s' % roll
result = 'draw'
while result == 'draw':
dice1, dice2 = rolldice()
roll += 1
if dice1 + dice2 == 7:
print 'Too bad, you loose in roll %s' % roll
result = None
elif (dice1, dice2) == (first1, first2):
print 'Congratulations, you win in roll %s' % roll
result = None
Hopefully his teacher doesn't know about Google, or he can be expelled 
from school for using it.

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


results of division

2004-12-09 Thread Brad Tilley
Hello,
What is the proper way to limit the results of division to only a few 
spaces after the decimal? I don't need rocket-science like precision. 
Here's an example:

1.775 is as exact as I need to be and normally, 1.70 will do.
Thank you,
Brad
--
http://mail.python.org/mailman/listinfo/python-list


Re: results of division

2004-12-09 Thread Brad Tilley
Simon Brunning wrote:
On Thu, 09 Dec 2004 09:38:55 -0500, Brad Tilley <[EMAIL PROTECTED]> wrote:
What is the proper way to limit the results of division to only a few
spaces after the decimal? I don't need rocket-science like precision.
Here's an example:
1.775 is as exact as I need to be and normally, 1.70 will do.
 
Use the new decimal type - <http://www.python.org/doc/2.4/whatsnew/node9.html>.
Ah, quantize() works great. Thank you for the tip.
--
http://mail.python.org/mailman/listinfo/python-list


Re: results of division

2004-12-09 Thread Brad Tilley
Peter Hansen wrote:
Brad Tilley wrote:
What is the proper way to limit the results of division to only a few 
spaces after the decimal? I don't need rocket-science like precision. 
Here's an example:

1.775 is as exact as I need to be and normally, 1.70 will do.

The answer is "what are you trying to do?".  The others have
given options and good advice, but the "right" approach
depends on what exactly you are doing.  Is this just for
display purposes, or is there more significant (though
perhaps not "precision-critical") calculation going on?
-Peter
I'm summing up the bytes in use on a hard disk drive and generating a 
report that's emailed based on the percentage of the drive in use. I 
know there are other ways to do this, but I like Python and I like to 
write my own code. I always find comp.lang.python a very helpful place.

Thank you,
Brad
--
http://mail.python.org/mailman/listinfo/python-list


Calling a C program from a Python Script

2004-12-09 Thread Brad Tilley
Is it possible to write a file open, then read program in C and then 
call the C program from a Python script like this:

for root, files, dirs in os.walk(path)
for f in files:
try:
EXECUTE_C_PROGRAM
If possible, how much faster would this be over a pure Python solution?
Thank you,
Brad
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling a C program from a Python Script

2004-12-09 Thread Brad Tilley
Grant Edwards wrote:
Huh?  What do you mean "write a file open"?  You want to read a
C source file and execute the C source?  If you have access to
a C interpreter, I guess you could invoke the interpreter from
python using popen, and feed the C source to it.  Alternatively
you could invoke a compiler and linker from C to generate an
executable and then execute the resulting file.

for root, files, dirs in os.walk(path)
for f in files:
try:
EXECUTE_C_PROGRAM

You're going to have to explain clearly what you mean by
"EXECUTE_C_PROGRAM".  If you want to, you can certainly run a
binary executable that was generated from C source, (e.g. an
ELF file under Linux or whatever a .exe file is under Windows).
Appears I was finger-tied. I meant "a C program that opens and reads 
files" while Python does everything else. How does one integrate C into 
a Python script like that?

So, instead of this:
for root, files, dirs in os.walk(path)
 for f in files:
 try:
 x = file(f, 'rb')
 data = x.read()
 x.close()
this:
for root, files, dirs in os.walk(path)
 for f in files:
 try:
 EXECUTE_C_PROGRAM
From the Simpsons:
Frink: "Here we have an ordinary square."
Wiggum: "Whoa! Slow down egghead!"
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling a C program from a Python Script

2004-12-09 Thread Brad Tilley
Steven Bethard wrote:
for root, files, dirs in os.walk(path)
 for f in files:
 try:
 x = file(f, 'rb')
 data = x.read()
 x.close()

Remember that CPython is implemented in C, and so all the builtin types 
(including file) basically execute C code directly.  My experience with 
Python file objects is that they are quite fast when you're doing simple 
things like the example above.
I'm dealing with a terabyte of files. Perhaps I should have mentioned that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling a C program from a Python Script

2004-12-09 Thread Brad Tilley
Grant Edwards wrote:
On 2004-12-09, Brad Tilley <[EMAIL PROTECTED]> wrote:
Steven Bethard wrote:
for root, files, dirs in os.walk(path)
for f in files:
try:
x = file(f, 'rb')
data = x.read()
x.close()

Remember that CPython is implemented in C, and so all the builtin types 
(including file) basically execute C code directly.  My experience with 
Python file objects is that they are quite fast when you're doing simple 
things like the example above.
I'm dealing with a terabyte of files. Perhaps I should have mentioned that.

And you think you're going to read the entire file consisting
of terabytes of data into memory using either C or Python?
[That's the example you gave.]
Sounds like maybe you need to mmap() the files?
Or at least tell us what you're trying to do so we can make
more intelligent suggestions.
It was an overly-simplistic example. I realize that I can't read all of 
the data into memory at once. I think that's obvious to most anyone.

I just want to know the basics of using C and Python together when the 
need arises, that's all, I don't want to write a book about what exactly 
it is that I'm involved in.

I'm going to take It's Me's advice and have a look at SWIG.
Thank you,
Brad
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.3.5 ?

2004-12-10 Thread Brad Tilley
Tim Peters wrote:
Not everyone is willing and able to switch to a
new 2.j release as soon as it appears. 
The reason I jumped on 2.4 right away was the msi installer for Windows 
systems. We can do unattended/automated installs... it's really great... 
a killer feature for Windows users who need to install Python on lots of 
machines.

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


Re: uptime for Win XP?

2004-12-12 Thread Brad Tilley
Esmail Bonakdarian wrote:
Hi,
Is there a way to display how long a Win XP system has been up?
Somewhat analogous to the *nix uptime command.
Thanks,
Esmail
Just run the built-in Windows utility 'systeminfo' from a cmd prompt. 
Python can call 'systeminfo' like this:

import os
uptime = os.popen('systeminfo', 'r')
data = uptime.readlines()
uptime.close
for line in data:
   if line contains "System Up Time":
  print line
Please note that 'systeminfo' is only present on Windows XP PRO and 
Windows Server 2003... Windows XP HOME does not have this command.
--
http://mail.python.org/mailman/listinfo/python-list


Re: uptime for Win XP?

2004-12-12 Thread Brad Tilley
Fredrik Lundh wrote:
Brad Tilley wrote

Just run the built-in Windows utility 'systeminfo' from a cmd prompt.

you're a bit late, aren't you?

for line in data:
  if line contains "System Up Time":
 print line

what Python version is this?
Sorry, lang mix-up:
x = "System Up Time"
if x in line:
   print line
--
http://mail.python.org/mailman/listinfo/python-list


Automate Python-2.4 Installs on Windows

2004-12-13 Thread Brad Tilley
Windows users may find this of interest:
http://filebox.vt.edu/users/rtilley/downloads/automatic_python_install.html
--
http://mail.python.org/mailman/listinfo/python-list


Make a small function thread safe

2011-12-16 Thread Brad Tilley
Hey guys,

I have a C++ function that I'd like to replicate (as closely as
possible) in Python. Here's an example:

107 void increment_counter( unsigned int& counter )
108 {
109 boost::mutex::scoped_lock lock( counter_lock );
110 ++counter;
111 }

A thread locks the function on entrance and then releases it on exit.
What is the equivalent way to do this in Python?

Many thanks!

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


Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote:

> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote:
> > 107 void increment_counter( unsigned int& counter )
> > 108 {
> > 109 boost::mutex::scoped_lock lock( counter_lock );
> > 110 ++counter;
> > 111 }
>
>
> with counter_lock:
>counter += 1
>
>
> ... where counter_lock is a threading.Lock instance.
>
> (see docs for the threading module)




So something like this then:

import threading

shared_container = []
lock = threading.Lock()

class thread_example( threading.Thread ):

def __init__( self ):
threading.Thread.__init__ (self)

def run(t):
lock
shared_container.append(t.name)

# main

threads = []
for i in xrange(10):
thread = thread_example()
threads.append(thread)

for thread in threads:
thread.start()

for item in shared_container:
print item
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Fri, Dec 16, 2011 at 9:24 AM, Brad Tilley  wrote:

>
>
> On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote:
>
>> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote:
>> > 107 void increment_counter( unsigned int& counter )
>> > 108 {
>> > 109 boost::mutex::scoped_lock lock( counter_lock );
>> > 110 ++counter;
>> > 111 }
>>
>>
>> with counter_lock:
>>counter += 1
>>
>>
>> ... where counter_lock is a threading.Lock instance.
>>
>> (see docs for the threading module)
>
>
>
>
> So something like this then:
>
> import threading
>
> shared_container = []
> lock = threading.Lock()
>
> class thread_example( threading.Thread ):
>
> def __init__( self ):
> threading.Thread.__init__ (self)
>
> def run(t):
> lock
> shared_container.append(t.name)
>
> # main
>
> threads = []
> for i in xrange(10):
> thread = thread_example()
> threads.append(thread)
>
> for thread in threads:
> thread.start()
>
> for item in shared_container:
> print item
>
>
Or perhaps run should look like this instead:

def run(t):
lock.acquire()
shared_container.append(t.name)
lock.release()

That seems a bit barbaric to me, not sure.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Make a small function thread safe

2011-12-16 Thread Brad Tilley
On Dec 16, 9:36 am, Tim Wintle  wrote:

> should be:
>       def run(t):
>           with lock:
>               shared_container.append(t.name)
>
> (or lock.acquire() and lock.release() as you mentioned)


Thanks Tim. The with statement is closer to the C++ code (IMO) more so
than the explicit acquire() and release() so I'll use that approach. I
appreciate your advice.

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


introspection

2006-08-31 Thread brad tilley
How do I import a module and then ask it to show me its methods or other 
aspects about itself during execution? I'd like to do something such as 
this:

import win32api

print win32api.methods()

I'd like to write some test scripts that load modules and probe them for 
information about themselves.
-- 
http://mail.python.org/mailman/listinfo/python-list